Merge branch 'tb/doc-max-tree-depth-fix'
[git/gitster.git] / list-objects.c
blob985d008799d0bb20a56ccaf6f821c699125373f6
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "tag.h"
5 #include "commit.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "tree.h"
9 #include "blob.h"
10 #include "diff.h"
11 #include "tree-walk.h"
12 #include "revision.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
16 #include "packfile.h"
17 #include "object-store-ll.h"
18 #include "trace.h"
19 #include "environment.h"
21 struct traversal_context {
22 struct rev_info *revs;
23 show_object_fn show_object;
24 show_commit_fn show_commit;
25 void *show_data;
26 struct filter *filter;
27 int depth;
30 static void show_commit(struct traversal_context *ctx,
31 struct commit *commit)
33 if (!ctx->show_commit)
34 return;
35 ctx->show_commit(commit, ctx->show_data);
38 static void show_object(struct traversal_context *ctx,
39 struct object *object,
40 const char *name)
42 if (!ctx->show_object)
43 return;
44 if (ctx->revs->unpacked && has_object_pack(&object->oid))
45 return;
47 ctx->show_object(object, name, ctx->show_data);
50 static void process_blob(struct traversal_context *ctx,
51 struct blob *blob,
52 struct strbuf *path,
53 const char *name)
55 struct object *obj = &blob->object;
56 size_t pathlen;
57 enum list_objects_filter_result r;
59 if (!ctx->revs->blob_objects)
60 return;
61 if (!obj)
62 die("bad blob object");
63 if (obj->flags & (UNINTERESTING | SEEN))
64 return;
67 * Pre-filter known-missing objects when explicitly requested.
68 * Otherwise, a missing object error message may be reported
69 * later (depending on other filtering criteria).
71 * Note that this "--exclude-promisor-objects" pre-filtering
72 * may cause the actual filter to report an incomplete list
73 * of missing objects.
75 if (ctx->revs->exclude_promisor_objects &&
76 !repo_has_object_file(the_repository, &obj->oid) &&
77 is_promisor_object(&obj->oid))
78 return;
80 pathlen = path->len;
81 strbuf_addstr(path, name);
82 r = list_objects_filter__filter_object(ctx->revs->repo,
83 LOFS_BLOB, obj,
84 path->buf, &path->buf[pathlen],
85 ctx->filter);
86 if (r & LOFR_MARK_SEEN)
87 obj->flags |= SEEN;
88 if (r & LOFR_DO_SHOW)
89 show_object(ctx, obj, path->buf);
90 strbuf_setlen(path, pathlen);
93 static void process_tree(struct traversal_context *ctx,
94 struct tree *tree,
95 struct strbuf *base,
96 const char *name);
98 static void process_tree_contents(struct traversal_context *ctx,
99 struct tree *tree,
100 struct strbuf *base)
102 struct tree_desc desc;
103 struct name_entry entry;
104 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
105 all_entries_interesting : entry_not_interesting;
107 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
109 while (tree_entry(&desc, &entry)) {
110 if (match != all_entries_interesting) {
111 match = tree_entry_interesting(ctx->revs->repo->index,
112 &entry, base,
113 &ctx->revs->diffopt.pathspec);
114 if (match == all_entries_not_interesting)
115 break;
116 if (match == entry_not_interesting)
117 continue;
120 if (S_ISDIR(entry.mode)) {
121 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
122 if (!t) {
123 die(_("entry '%s' in tree %s has tree mode, "
124 "but is not a tree"),
125 entry.path, oid_to_hex(&tree->object.oid));
127 t->object.flags |= NOT_USER_GIVEN;
128 ctx->depth++;
129 process_tree(ctx, t, base, entry.path);
130 ctx->depth--;
132 else if (S_ISGITLINK(entry.mode))
133 ; /* ignore gitlink */
134 else {
135 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
136 if (!b) {
137 die(_("entry '%s' in tree %s has blob mode, "
138 "but is not a blob"),
139 entry.path, oid_to_hex(&tree->object.oid));
141 b->object.flags |= NOT_USER_GIVEN;
142 process_blob(ctx, b, base, entry.path);
147 static void process_tree(struct traversal_context *ctx,
148 struct tree *tree,
149 struct strbuf *base,
150 const char *name)
152 struct object *obj = &tree->object;
153 struct rev_info *revs = ctx->revs;
154 int baselen = base->len;
155 enum list_objects_filter_result r;
156 int failed_parse;
158 if (!revs->tree_objects)
159 return;
160 if (!obj)
161 die("bad tree object");
162 if (obj->flags & (UNINTERESTING | SEEN))
163 return;
164 if (revs->include_check_obj &&
165 !revs->include_check_obj(&tree->object, revs->include_check_data))
166 return;
168 if (ctx->depth > max_allowed_tree_depth)
169 die("exceeded maximum allowed tree depth");
171 failed_parse = parse_tree_gently(tree, 1);
172 if (failed_parse) {
173 if (revs->ignore_missing_links)
174 return;
177 * Pre-filter known-missing tree objects when explicitly
178 * requested. This may cause the actual filter to report
179 * an incomplete list of missing objects.
181 if (revs->exclude_promisor_objects &&
182 is_promisor_object(&obj->oid))
183 return;
185 if (!revs->do_not_die_on_missing_objects)
186 die("bad tree object %s", oid_to_hex(&obj->oid));
189 strbuf_addstr(base, name);
190 r = list_objects_filter__filter_object(ctx->revs->repo,
191 LOFS_BEGIN_TREE, obj,
192 base->buf, &base->buf[baselen],
193 ctx->filter);
194 if (r & LOFR_MARK_SEEN)
195 obj->flags |= SEEN;
196 if (r & LOFR_DO_SHOW)
197 show_object(ctx, obj, base->buf);
198 if (base->len)
199 strbuf_addch(base, '/');
201 if (r & LOFR_SKIP_TREE)
202 trace_printf("Skipping contents of tree %s...\n", base->buf);
203 else if (!failed_parse)
204 process_tree_contents(ctx, tree, base);
206 r = list_objects_filter__filter_object(ctx->revs->repo,
207 LOFS_END_TREE, obj,
208 base->buf, &base->buf[baselen],
209 ctx->filter);
210 if (r & LOFR_MARK_SEEN)
211 obj->flags |= SEEN;
212 if (r & LOFR_DO_SHOW)
213 show_object(ctx, obj, base->buf);
215 strbuf_setlen(base, baselen);
216 free_tree_buffer(tree);
219 static void process_tag(struct traversal_context *ctx,
220 struct tag *tag,
221 const char *name)
223 enum list_objects_filter_result r;
225 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
226 &tag->object, NULL, NULL,
227 ctx->filter);
228 if (r & LOFR_MARK_SEEN)
229 tag->object.flags |= SEEN;
230 if (r & LOFR_DO_SHOW)
231 show_object(ctx, &tag->object, name);
234 static void mark_edge_parents_uninteresting(struct commit *commit,
235 struct rev_info *revs,
236 show_edge_fn show_edge)
238 struct commit_list *parents;
240 for (parents = commit->parents; parents; parents = parents->next) {
241 struct commit *parent = parents->item;
242 if (!(parent->object.flags & UNINTERESTING))
243 continue;
244 mark_tree_uninteresting(revs->repo,
245 repo_get_commit_tree(the_repository, parent));
246 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
247 parent->object.flags |= SHOWN;
248 show_edge(parent);
253 static void add_edge_parents(struct commit *commit,
254 struct rev_info *revs,
255 show_edge_fn show_edge,
256 struct oidset *set)
258 struct commit_list *parents;
260 for (parents = commit->parents; parents; parents = parents->next) {
261 struct commit *parent = parents->item;
262 struct tree *tree = repo_get_commit_tree(the_repository,
263 parent);
265 if (!tree)
266 continue;
268 oidset_insert(set, &tree->object.oid);
270 if (!(parent->object.flags & UNINTERESTING))
271 continue;
272 tree->object.flags |= UNINTERESTING;
274 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
275 parent->object.flags |= SHOWN;
276 show_edge(parent);
281 void mark_edges_uninteresting(struct rev_info *revs,
282 show_edge_fn show_edge,
283 int sparse)
285 struct commit_list *list;
286 int i;
288 if (sparse) {
289 struct oidset set;
290 oidset_init(&set, 16);
292 for (list = revs->commits; list; list = list->next) {
293 struct commit *commit = list->item;
294 struct tree *tree = repo_get_commit_tree(the_repository,
295 commit);
297 if (commit->object.flags & UNINTERESTING)
298 tree->object.flags |= UNINTERESTING;
300 oidset_insert(&set, &tree->object.oid);
301 add_edge_parents(commit, revs, show_edge, &set);
304 mark_trees_uninteresting_sparse(revs->repo, &set);
305 oidset_clear(&set);
306 } else {
307 for (list = revs->commits; list; list = list->next) {
308 struct commit *commit = list->item;
309 if (commit->object.flags & UNINTERESTING) {
310 mark_tree_uninteresting(revs->repo,
311 repo_get_commit_tree(the_repository, commit));
312 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
313 commit->object.flags |= SHOWN;
314 show_edge(commit);
316 continue;
318 mark_edge_parents_uninteresting(commit, revs, show_edge);
322 if (revs->edge_hint_aggressive) {
323 for (i = 0; i < revs->cmdline.nr; i++) {
324 struct object *obj = revs->cmdline.rev[i].item;
325 struct commit *commit = (struct commit *)obj;
326 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
327 continue;
328 mark_tree_uninteresting(revs->repo,
329 repo_get_commit_tree(the_repository, commit));
330 if (!(obj->flags & SHOWN)) {
331 obj->flags |= SHOWN;
332 show_edge(commit);
338 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
340 add_pending_object(revs, &tree->object, "");
343 static void traverse_non_commits(struct traversal_context *ctx,
344 struct strbuf *base)
346 int i;
348 assert(base->len == 0);
350 for (i = 0; i < ctx->revs->pending.nr; i++) {
351 struct object_array_entry *pending = ctx->revs->pending.objects + i;
352 struct object *obj = pending->item;
353 const char *name = pending->name;
354 const char *path = pending->path;
355 if (obj->flags & (UNINTERESTING | SEEN))
356 continue;
357 if (obj->type == OBJ_TAG) {
358 process_tag(ctx, (struct tag *)obj, name);
359 continue;
361 if (!path)
362 path = "";
363 if (obj->type == OBJ_TREE) {
364 ctx->depth = 0;
365 process_tree(ctx, (struct tree *)obj, base, path);
366 continue;
368 if (obj->type == OBJ_BLOB) {
369 process_blob(ctx, (struct blob *)obj, base, path);
370 continue;
372 die("unknown pending object %s (%s)",
373 oid_to_hex(&obj->oid), name);
375 object_array_clear(&ctx->revs->pending);
378 static void do_traverse(struct traversal_context *ctx)
380 struct commit *commit;
381 struct strbuf csp; /* callee's scratch pad */
382 strbuf_init(&csp, PATH_MAX);
384 while ((commit = get_revision(ctx->revs)) != NULL) {
385 enum list_objects_filter_result r;
387 r = list_objects_filter__filter_object(ctx->revs->repo,
388 LOFS_COMMIT, &commit->object,
389 NULL, NULL, ctx->filter);
392 * an uninteresting boundary commit may not have its tree
393 * parsed yet, but we are not going to show them anyway
395 if (!ctx->revs->tree_objects)
396 ; /* do not bother loading tree */
397 else if (ctx->revs->do_not_die_on_missing_objects &&
398 oidset_contains(&ctx->revs->missing_commits, &commit->object.oid))
400 else if (repo_get_commit_tree(the_repository, commit)) {
401 struct tree *tree = repo_get_commit_tree(the_repository,
402 commit);
403 tree->object.flags |= NOT_USER_GIVEN;
404 add_pending_tree(ctx->revs, tree);
405 } else if (commit->object.parsed) {
406 die(_("unable to load root tree for commit %s"),
407 oid_to_hex(&commit->object.oid));
410 if (r & LOFR_MARK_SEEN)
411 commit->object.flags |= SEEN;
412 if (r & LOFR_DO_SHOW)
413 show_commit(ctx, commit);
415 if (ctx->revs->tree_blobs_in_commit_order)
417 * NEEDSWORK: Adding the tree and then flushing it here
418 * needs a reallocation for each commit. Can we pass the
419 * tree directory without allocation churn?
421 traverse_non_commits(ctx, &csp);
423 traverse_non_commits(ctx, &csp);
424 strbuf_release(&csp);
427 void traverse_commit_list_filtered(
428 struct rev_info *revs,
429 show_commit_fn show_commit,
430 show_object_fn show_object,
431 void *show_data,
432 struct oidset *omitted)
434 struct traversal_context ctx = {
435 .revs = revs,
436 .show_object = show_object,
437 .show_commit = show_commit,
438 .show_data = show_data,
441 if (revs->filter.choice)
442 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
444 do_traverse(&ctx);
446 if (ctx.filter)
447 list_objects_filter__free(ctx.filter);