documentation: fix verb vs. noun
[git.git] / list-objects.c
blobc25c72b32c323c18da214fb7c0f5935556bb97a4
1 #include "git-compat-util.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "tree.h"
7 #include "blob.h"
8 #include "diff.h"
9 #include "tree-walk.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 #include "list-objects-filter.h"
13 #include "list-objects-filter-options.h"
14 #include "packfile.h"
15 #include "object-store-ll.h"
16 #include "trace.h"
17 #include "environment.h"
19 struct traversal_context {
20 struct rev_info *revs;
21 show_object_fn show_object;
22 show_commit_fn show_commit;
23 void *show_data;
24 struct filter *filter;
25 int depth;
28 static void show_commit(struct traversal_context *ctx,
29 struct commit *commit)
31 if (!ctx->show_commit)
32 return;
33 ctx->show_commit(commit, ctx->show_data);
36 static void show_object(struct traversal_context *ctx,
37 struct object *object,
38 const char *name)
40 if (!ctx->show_object)
41 return;
42 ctx->show_object(object, name, ctx->show_data);
45 static void process_blob(struct traversal_context *ctx,
46 struct blob *blob,
47 struct strbuf *path,
48 const char *name)
50 struct object *obj = &blob->object;
51 size_t pathlen;
52 enum list_objects_filter_result r;
54 if (!ctx->revs->blob_objects)
55 return;
56 if (!obj)
57 die("bad blob object");
58 if (obj->flags & (UNINTERESTING | SEEN))
59 return;
62 * Pre-filter known-missing objects when explicitly requested.
63 * Otherwise, a missing object error message may be reported
64 * later (depending on other filtering criteria).
66 * Note that this "--exclude-promisor-objects" pre-filtering
67 * may cause the actual filter to report an incomplete list
68 * of missing objects.
70 if (ctx->revs->exclude_promisor_objects &&
71 !repo_has_object_file(the_repository, &obj->oid) &&
72 is_promisor_object(&obj->oid))
73 return;
75 pathlen = path->len;
76 strbuf_addstr(path, name);
77 r = list_objects_filter__filter_object(ctx->revs->repo,
78 LOFS_BLOB, obj,
79 path->buf, &path->buf[pathlen],
80 ctx->filter);
81 if (r & LOFR_MARK_SEEN)
82 obj->flags |= SEEN;
83 if (r & LOFR_DO_SHOW)
84 show_object(ctx, obj, path->buf);
85 strbuf_setlen(path, pathlen);
88 static void process_tree(struct traversal_context *ctx,
89 struct tree *tree,
90 struct strbuf *base,
91 const char *name);
93 static void process_tree_contents(struct traversal_context *ctx,
94 struct tree *tree,
95 struct strbuf *base)
97 struct tree_desc desc;
98 struct name_entry entry;
99 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
100 all_entries_interesting : entry_not_interesting;
102 init_tree_desc(&desc, tree->buffer, tree->size);
104 while (tree_entry(&desc, &entry)) {
105 if (match != all_entries_interesting) {
106 match = tree_entry_interesting(ctx->revs->repo->index,
107 &entry, base,
108 &ctx->revs->diffopt.pathspec);
109 if (match == all_entries_not_interesting)
110 break;
111 if (match == entry_not_interesting)
112 continue;
115 if (S_ISDIR(entry.mode)) {
116 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
117 if (!t) {
118 die(_("entry '%s' in tree %s has tree mode, "
119 "but is not a tree"),
120 entry.path, oid_to_hex(&tree->object.oid));
122 t->object.flags |= NOT_USER_GIVEN;
123 ctx->depth++;
124 process_tree(ctx, t, base, entry.path);
125 ctx->depth--;
127 else if (S_ISGITLINK(entry.mode))
128 ; /* ignore gitlink */
129 else {
130 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
131 if (!b) {
132 die(_("entry '%s' in tree %s has blob mode, "
133 "but is not a blob"),
134 entry.path, oid_to_hex(&tree->object.oid));
136 b->object.flags |= NOT_USER_GIVEN;
137 process_blob(ctx, b, base, entry.path);
142 static void process_tree(struct traversal_context *ctx,
143 struct tree *tree,
144 struct strbuf *base,
145 const char *name)
147 struct object *obj = &tree->object;
148 struct rev_info *revs = ctx->revs;
149 int baselen = base->len;
150 enum list_objects_filter_result r;
151 int failed_parse;
153 if (!revs->tree_objects)
154 return;
155 if (!obj)
156 die("bad tree object");
157 if (obj->flags & (UNINTERESTING | SEEN))
158 return;
159 if (revs->include_check_obj &&
160 !revs->include_check_obj(&tree->object, revs->include_check_data))
161 return;
163 if (ctx->depth > max_allowed_tree_depth)
164 die("exceeded maximum allowed tree depth");
166 failed_parse = parse_tree_gently(tree, 1);
167 if (failed_parse) {
168 if (revs->ignore_missing_links)
169 return;
172 * Pre-filter known-missing tree objects when explicitly
173 * requested. This may cause the actual filter to report
174 * an incomplete list of missing objects.
176 if (revs->exclude_promisor_objects &&
177 is_promisor_object(&obj->oid))
178 return;
180 if (!revs->do_not_die_on_missing_tree)
181 die("bad tree object %s", oid_to_hex(&obj->oid));
184 strbuf_addstr(base, name);
185 r = list_objects_filter__filter_object(ctx->revs->repo,
186 LOFS_BEGIN_TREE, obj,
187 base->buf, &base->buf[baselen],
188 ctx->filter);
189 if (r & LOFR_MARK_SEEN)
190 obj->flags |= SEEN;
191 if (r & LOFR_DO_SHOW)
192 show_object(ctx, obj, base->buf);
193 if (base->len)
194 strbuf_addch(base, '/');
196 if (r & LOFR_SKIP_TREE)
197 trace_printf("Skipping contents of tree %s...\n", base->buf);
198 else if (!failed_parse)
199 process_tree_contents(ctx, tree, base);
201 r = list_objects_filter__filter_object(ctx->revs->repo,
202 LOFS_END_TREE, obj,
203 base->buf, &base->buf[baselen],
204 ctx->filter);
205 if (r & LOFR_MARK_SEEN)
206 obj->flags |= SEEN;
207 if (r & LOFR_DO_SHOW)
208 show_object(ctx, obj, base->buf);
210 strbuf_setlen(base, baselen);
211 free_tree_buffer(tree);
214 static void process_tag(struct traversal_context *ctx,
215 struct tag *tag,
216 const char *name)
218 enum list_objects_filter_result r;
220 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
221 &tag->object, NULL, NULL,
222 ctx->filter);
223 if (r & LOFR_MARK_SEEN)
224 tag->object.flags |= SEEN;
225 if (r & LOFR_DO_SHOW)
226 show_object(ctx, &tag->object, name);
229 static void mark_edge_parents_uninteresting(struct commit *commit,
230 struct rev_info *revs,
231 show_edge_fn show_edge)
233 struct commit_list *parents;
235 for (parents = commit->parents; parents; parents = parents->next) {
236 struct commit *parent = parents->item;
237 if (!(parent->object.flags & UNINTERESTING))
238 continue;
239 mark_tree_uninteresting(revs->repo,
240 repo_get_commit_tree(the_repository, parent));
241 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
242 parent->object.flags |= SHOWN;
243 show_edge(parent);
248 static void add_edge_parents(struct commit *commit,
249 struct rev_info *revs,
250 show_edge_fn show_edge,
251 struct oidset *set)
253 struct commit_list *parents;
255 for (parents = commit->parents; parents; parents = parents->next) {
256 struct commit *parent = parents->item;
257 struct tree *tree = repo_get_commit_tree(the_repository,
258 parent);
260 if (!tree)
261 continue;
263 oidset_insert(set, &tree->object.oid);
265 if (!(parent->object.flags & UNINTERESTING))
266 continue;
267 tree->object.flags |= UNINTERESTING;
269 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
270 parent->object.flags |= SHOWN;
271 show_edge(parent);
276 void mark_edges_uninteresting(struct rev_info *revs,
277 show_edge_fn show_edge,
278 int sparse)
280 struct commit_list *list;
281 int i;
283 if (sparse) {
284 struct oidset set;
285 oidset_init(&set, 16);
287 for (list = revs->commits; list; list = list->next) {
288 struct commit *commit = list->item;
289 struct tree *tree = repo_get_commit_tree(the_repository,
290 commit);
292 if (commit->object.flags & UNINTERESTING)
293 tree->object.flags |= UNINTERESTING;
295 oidset_insert(&set, &tree->object.oid);
296 add_edge_parents(commit, revs, show_edge, &set);
299 mark_trees_uninteresting_sparse(revs->repo, &set);
300 oidset_clear(&set);
301 } else {
302 for (list = revs->commits; list; list = list->next) {
303 struct commit *commit = list->item;
304 if (commit->object.flags & UNINTERESTING) {
305 mark_tree_uninteresting(revs->repo,
306 repo_get_commit_tree(the_repository, commit));
307 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
308 commit->object.flags |= SHOWN;
309 show_edge(commit);
311 continue;
313 mark_edge_parents_uninteresting(commit, revs, show_edge);
317 if (revs->edge_hint_aggressive) {
318 for (i = 0; i < revs->cmdline.nr; i++) {
319 struct object *obj = revs->cmdline.rev[i].item;
320 struct commit *commit = (struct commit *)obj;
321 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
322 continue;
323 mark_tree_uninteresting(revs->repo,
324 repo_get_commit_tree(the_repository, commit));
325 if (!(obj->flags & SHOWN)) {
326 obj->flags |= SHOWN;
327 show_edge(commit);
333 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
335 add_pending_object(revs, &tree->object, "");
338 static void traverse_non_commits(struct traversal_context *ctx,
339 struct strbuf *base)
341 int i;
343 assert(base->len == 0);
345 for (i = 0; i < ctx->revs->pending.nr; i++) {
346 struct object_array_entry *pending = ctx->revs->pending.objects + i;
347 struct object *obj = pending->item;
348 const char *name = pending->name;
349 const char *path = pending->path;
350 if (obj->flags & (UNINTERESTING | SEEN))
351 continue;
352 if (obj->type == OBJ_TAG) {
353 process_tag(ctx, (struct tag *)obj, name);
354 continue;
356 if (!path)
357 path = "";
358 if (obj->type == OBJ_TREE) {
359 ctx->depth = 0;
360 process_tree(ctx, (struct tree *)obj, base, path);
361 continue;
363 if (obj->type == OBJ_BLOB) {
364 process_blob(ctx, (struct blob *)obj, base, path);
365 continue;
367 die("unknown pending object %s (%s)",
368 oid_to_hex(&obj->oid), name);
370 object_array_clear(&ctx->revs->pending);
373 static void do_traverse(struct traversal_context *ctx)
375 struct commit *commit;
376 struct strbuf csp; /* callee's scratch pad */
377 strbuf_init(&csp, PATH_MAX);
379 while ((commit = get_revision(ctx->revs)) != NULL) {
380 enum list_objects_filter_result r;
382 r = list_objects_filter__filter_object(ctx->revs->repo,
383 LOFS_COMMIT, &commit->object,
384 NULL, NULL, ctx->filter);
387 * an uninteresting boundary commit may not have its tree
388 * parsed yet, but we are not going to show them anyway
390 if (!ctx->revs->tree_objects)
391 ; /* do not bother loading tree */
392 else if (repo_get_commit_tree(the_repository, commit)) {
393 struct tree *tree = repo_get_commit_tree(the_repository,
394 commit);
395 tree->object.flags |= NOT_USER_GIVEN;
396 add_pending_tree(ctx->revs, tree);
397 } else if (commit->object.parsed) {
398 die(_("unable to load root tree for commit %s"),
399 oid_to_hex(&commit->object.oid));
402 if (r & LOFR_MARK_SEEN)
403 commit->object.flags |= SEEN;
404 if (r & LOFR_DO_SHOW)
405 show_commit(ctx, commit);
407 if (ctx->revs->tree_blobs_in_commit_order)
409 * NEEDSWORK: Adding the tree and then flushing it here
410 * needs a reallocation for each commit. Can we pass the
411 * tree directory without allocation churn?
413 traverse_non_commits(ctx, &csp);
415 traverse_non_commits(ctx, &csp);
416 strbuf_release(&csp);
419 void traverse_commit_list_filtered(
420 struct rev_info *revs,
421 show_commit_fn show_commit,
422 show_object_fn show_object,
423 void *show_data,
424 struct oidset *omitted)
426 struct traversal_context ctx = {
427 .revs = revs,
428 .show_object = show_object,
429 .show_commit = show_commit,
430 .show_data = show_data,
433 if (revs->filter.choice)
434 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
436 do_traverse(&ctx);
438 if (ctx.filter)
439 list_objects_filter__free(ctx.filter);