debian: new upstream release
[git/debian.git] / list-objects.c
blobf39b68faf54889144abd81e2c1f9c6f19edd8d4f
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 if (ctx->revs->unpacked && has_object_pack(&object->oid))
43 return;
45 ctx->show_object(object, name, ctx->show_data);
48 static void process_blob(struct traversal_context *ctx,
49 struct blob *blob,
50 struct strbuf *path,
51 const char *name)
53 struct object *obj = &blob->object;
54 size_t pathlen;
55 enum list_objects_filter_result r;
57 if (!ctx->revs->blob_objects)
58 return;
59 if (!obj)
60 die("bad blob object");
61 if (obj->flags & (UNINTERESTING | SEEN))
62 return;
65 * Pre-filter known-missing objects when explicitly requested.
66 * Otherwise, a missing object error message may be reported
67 * later (depending on other filtering criteria).
69 * Note that this "--exclude-promisor-objects" pre-filtering
70 * may cause the actual filter to report an incomplete list
71 * of missing objects.
73 if (ctx->revs->exclude_promisor_objects &&
74 !repo_has_object_file(the_repository, &obj->oid) &&
75 is_promisor_object(&obj->oid))
76 return;
78 pathlen = path->len;
79 strbuf_addstr(path, name);
80 r = list_objects_filter__filter_object(ctx->revs->repo,
81 LOFS_BLOB, obj,
82 path->buf, &path->buf[pathlen],
83 ctx->filter);
84 if (r & LOFR_MARK_SEEN)
85 obj->flags |= SEEN;
86 if (r & LOFR_DO_SHOW)
87 show_object(ctx, obj, path->buf);
88 strbuf_setlen(path, pathlen);
91 static void process_tree(struct traversal_context *ctx,
92 struct tree *tree,
93 struct strbuf *base,
94 const char *name);
96 static void process_tree_contents(struct traversal_context *ctx,
97 struct tree *tree,
98 struct strbuf *base)
100 struct tree_desc desc;
101 struct name_entry entry;
102 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
103 all_entries_interesting : entry_not_interesting;
105 init_tree_desc(&desc, tree->buffer, tree->size);
107 while (tree_entry(&desc, &entry)) {
108 if (match != all_entries_interesting) {
109 match = tree_entry_interesting(ctx->revs->repo->index,
110 &entry, base,
111 &ctx->revs->diffopt.pathspec);
112 if (match == all_entries_not_interesting)
113 break;
114 if (match == entry_not_interesting)
115 continue;
118 if (S_ISDIR(entry.mode)) {
119 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
120 if (!t) {
121 die(_("entry '%s' in tree %s has tree mode, "
122 "but is not a tree"),
123 entry.path, oid_to_hex(&tree->object.oid));
125 t->object.flags |= NOT_USER_GIVEN;
126 ctx->depth++;
127 process_tree(ctx, t, base, entry.path);
128 ctx->depth--;
130 else if (S_ISGITLINK(entry.mode))
131 ; /* ignore gitlink */
132 else {
133 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
134 if (!b) {
135 die(_("entry '%s' in tree %s has blob mode, "
136 "but is not a blob"),
137 entry.path, oid_to_hex(&tree->object.oid));
139 b->object.flags |= NOT_USER_GIVEN;
140 process_blob(ctx, b, base, entry.path);
145 static void process_tree(struct traversal_context *ctx,
146 struct tree *tree,
147 struct strbuf *base,
148 const char *name)
150 struct object *obj = &tree->object;
151 struct rev_info *revs = ctx->revs;
152 int baselen = base->len;
153 enum list_objects_filter_result r;
154 int failed_parse;
156 if (!revs->tree_objects)
157 return;
158 if (!obj)
159 die("bad tree object");
160 if (obj->flags & (UNINTERESTING | SEEN))
161 return;
162 if (revs->include_check_obj &&
163 !revs->include_check_obj(&tree->object, revs->include_check_data))
164 return;
166 if (ctx->depth > max_allowed_tree_depth)
167 die("exceeded maximum allowed tree depth");
169 failed_parse = parse_tree_gently(tree, 1);
170 if (failed_parse) {
171 if (revs->ignore_missing_links)
172 return;
175 * Pre-filter known-missing tree objects when explicitly
176 * requested. This may cause the actual filter to report
177 * an incomplete list of missing objects.
179 if (revs->exclude_promisor_objects &&
180 is_promisor_object(&obj->oid))
181 return;
183 if (!revs->do_not_die_on_missing_objects)
184 die("bad tree object %s", oid_to_hex(&obj->oid));
187 strbuf_addstr(base, name);
188 r = list_objects_filter__filter_object(ctx->revs->repo,
189 LOFS_BEGIN_TREE, obj,
190 base->buf, &base->buf[baselen],
191 ctx->filter);
192 if (r & LOFR_MARK_SEEN)
193 obj->flags |= SEEN;
194 if (r & LOFR_DO_SHOW)
195 show_object(ctx, obj, base->buf);
196 if (base->len)
197 strbuf_addch(base, '/');
199 if (r & LOFR_SKIP_TREE)
200 trace_printf("Skipping contents of tree %s...\n", base->buf);
201 else if (!failed_parse)
202 process_tree_contents(ctx, tree, base);
204 r = list_objects_filter__filter_object(ctx->revs->repo,
205 LOFS_END_TREE, obj,
206 base->buf, &base->buf[baselen],
207 ctx->filter);
208 if (r & LOFR_MARK_SEEN)
209 obj->flags |= SEEN;
210 if (r & LOFR_DO_SHOW)
211 show_object(ctx, obj, base->buf);
213 strbuf_setlen(base, baselen);
214 free_tree_buffer(tree);
217 static void process_tag(struct traversal_context *ctx,
218 struct tag *tag,
219 const char *name)
221 enum list_objects_filter_result r;
223 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
224 &tag->object, NULL, NULL,
225 ctx->filter);
226 if (r & LOFR_MARK_SEEN)
227 tag->object.flags |= SEEN;
228 if (r & LOFR_DO_SHOW)
229 show_object(ctx, &tag->object, name);
232 static void mark_edge_parents_uninteresting(struct commit *commit,
233 struct rev_info *revs,
234 show_edge_fn show_edge)
236 struct commit_list *parents;
238 for (parents = commit->parents; parents; parents = parents->next) {
239 struct commit *parent = parents->item;
240 if (!(parent->object.flags & UNINTERESTING))
241 continue;
242 mark_tree_uninteresting(revs->repo,
243 repo_get_commit_tree(the_repository, parent));
244 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
245 parent->object.flags |= SHOWN;
246 show_edge(parent);
251 static void add_edge_parents(struct commit *commit,
252 struct rev_info *revs,
253 show_edge_fn show_edge,
254 struct oidset *set)
256 struct commit_list *parents;
258 for (parents = commit->parents; parents; parents = parents->next) {
259 struct commit *parent = parents->item;
260 struct tree *tree = repo_get_commit_tree(the_repository,
261 parent);
263 if (!tree)
264 continue;
266 oidset_insert(set, &tree->object.oid);
268 if (!(parent->object.flags & UNINTERESTING))
269 continue;
270 tree->object.flags |= UNINTERESTING;
272 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
273 parent->object.flags |= SHOWN;
274 show_edge(parent);
279 void mark_edges_uninteresting(struct rev_info *revs,
280 show_edge_fn show_edge,
281 int sparse)
283 struct commit_list *list;
284 int i;
286 if (sparse) {
287 struct oidset set;
288 oidset_init(&set, 16);
290 for (list = revs->commits; list; list = list->next) {
291 struct commit *commit = list->item;
292 struct tree *tree = repo_get_commit_tree(the_repository,
293 commit);
295 if (commit->object.flags & UNINTERESTING)
296 tree->object.flags |= UNINTERESTING;
298 oidset_insert(&set, &tree->object.oid);
299 add_edge_parents(commit, revs, show_edge, &set);
302 mark_trees_uninteresting_sparse(revs->repo, &set);
303 oidset_clear(&set);
304 } else {
305 for (list = revs->commits; list; list = list->next) {
306 struct commit *commit = list->item;
307 if (commit->object.flags & UNINTERESTING) {
308 mark_tree_uninteresting(revs->repo,
309 repo_get_commit_tree(the_repository, commit));
310 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
311 commit->object.flags |= SHOWN;
312 show_edge(commit);
314 continue;
316 mark_edge_parents_uninteresting(commit, revs, show_edge);
320 if (revs->edge_hint_aggressive) {
321 for (i = 0; i < revs->cmdline.nr; i++) {
322 struct object *obj = revs->cmdline.rev[i].item;
323 struct commit *commit = (struct commit *)obj;
324 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
325 continue;
326 mark_tree_uninteresting(revs->repo,
327 repo_get_commit_tree(the_repository, commit));
328 if (!(obj->flags & SHOWN)) {
329 obj->flags |= SHOWN;
330 show_edge(commit);
336 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
338 add_pending_object(revs, &tree->object, "");
341 static void traverse_non_commits(struct traversal_context *ctx,
342 struct strbuf *base)
344 int i;
346 assert(base->len == 0);
348 for (i = 0; i < ctx->revs->pending.nr; i++) {
349 struct object_array_entry *pending = ctx->revs->pending.objects + i;
350 struct object *obj = pending->item;
351 const char *name = pending->name;
352 const char *path = pending->path;
353 if (obj->flags & (UNINTERESTING | SEEN))
354 continue;
355 if (obj->type == OBJ_TAG) {
356 process_tag(ctx, (struct tag *)obj, name);
357 continue;
359 if (!path)
360 path = "";
361 if (obj->type == OBJ_TREE) {
362 ctx->depth = 0;
363 process_tree(ctx, (struct tree *)obj, base, path);
364 continue;
366 if (obj->type == OBJ_BLOB) {
367 process_blob(ctx, (struct blob *)obj, base, path);
368 continue;
370 die("unknown pending object %s (%s)",
371 oid_to_hex(&obj->oid), name);
373 object_array_clear(&ctx->revs->pending);
376 static void do_traverse(struct traversal_context *ctx)
378 struct commit *commit;
379 struct strbuf csp; /* callee's scratch pad */
380 strbuf_init(&csp, PATH_MAX);
382 while ((commit = get_revision(ctx->revs)) != NULL) {
383 enum list_objects_filter_result r;
385 r = list_objects_filter__filter_object(ctx->revs->repo,
386 LOFS_COMMIT, &commit->object,
387 NULL, NULL, ctx->filter);
390 * an uninteresting boundary commit may not have its tree
391 * parsed yet, but we are not going to show them anyway
393 if (!ctx->revs->tree_objects)
394 ; /* do not bother loading tree */
395 else if (ctx->revs->do_not_die_on_missing_objects &&
396 oidset_contains(&ctx->revs->missing_commits, &commit->object.oid))
398 else if (repo_get_commit_tree(the_repository, commit)) {
399 struct tree *tree = repo_get_commit_tree(the_repository,
400 commit);
401 tree->object.flags |= NOT_USER_GIVEN;
402 add_pending_tree(ctx->revs, tree);
403 } else if (commit->object.parsed) {
404 die(_("unable to load root tree for commit %s"),
405 oid_to_hex(&commit->object.oid));
408 if (r & LOFR_MARK_SEEN)
409 commit->object.flags |= SEEN;
410 if (r & LOFR_DO_SHOW)
411 show_commit(ctx, commit);
413 if (ctx->revs->tree_blobs_in_commit_order)
415 * NEEDSWORK: Adding the tree and then flushing it here
416 * needs a reallocation for each commit. Can we pass the
417 * tree directory without allocation churn?
419 traverse_non_commits(ctx, &csp);
421 traverse_non_commits(ctx, &csp);
422 strbuf_release(&csp);
425 void traverse_commit_list_filtered(
426 struct rev_info *revs,
427 show_commit_fn show_commit,
428 show_object_fn show_object,
429 void *show_data,
430 struct oidset *omitted)
432 struct traversal_context ctx = {
433 .revs = revs,
434 .show_object = show_object,
435 .show_commit = show_commit,
436 .show_data = show_data,
439 if (revs->filter.choice)
440 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
442 do_traverse(&ctx);
444 if (ctx.filter)
445 list_objects_filter__free(ctx.filter);