Merge branch 'ab/remove-implicit-use-of-the-repository'
[git.git] / list-objects.c
blob6e6e7ce61ea0537cd0ca4dbe312f82b4628b5325
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "hex.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "diff.h"
8 #include "tree-walk.h"
9 #include "revision.h"
10 #include "list-objects.h"
11 #include "list-objects-filter.h"
12 #include "list-objects-filter-options.h"
13 #include "packfile.h"
14 #include "object-store.h"
15 #include "trace.h"
17 struct traversal_context {
18 struct rev_info *revs;
19 show_object_fn show_object;
20 show_commit_fn show_commit;
21 void *show_data;
22 struct filter *filter;
25 static void show_commit(struct traversal_context *ctx,
26 struct commit *commit)
28 if (!ctx->show_commit)
29 return;
30 ctx->show_commit(commit, ctx->show_data);
33 static void show_object(struct traversal_context *ctx,
34 struct object *object,
35 const char *name)
37 if (!ctx->show_object)
38 return;
39 ctx->show_object(object, name, ctx->show_data);
42 static void process_blob(struct traversal_context *ctx,
43 struct blob *blob,
44 struct strbuf *path,
45 const char *name)
47 struct object *obj = &blob->object;
48 size_t pathlen;
49 enum list_objects_filter_result r;
51 if (!ctx->revs->blob_objects)
52 return;
53 if (!obj)
54 die("bad blob object");
55 if (obj->flags & (UNINTERESTING | SEEN))
56 return;
59 * Pre-filter known-missing objects when explicitly requested.
60 * Otherwise, a missing object error message may be reported
61 * later (depending on other filtering criteria).
63 * Note that this "--exclude-promisor-objects" pre-filtering
64 * may cause the actual filter to report an incomplete list
65 * of missing objects.
67 if (ctx->revs->exclude_promisor_objects &&
68 !repo_has_object_file(the_repository, &obj->oid) &&
69 is_promisor_object(&obj->oid))
70 return;
72 pathlen = path->len;
73 strbuf_addstr(path, name);
74 r = list_objects_filter__filter_object(ctx->revs->repo,
75 LOFS_BLOB, obj,
76 path->buf, &path->buf[pathlen],
77 ctx->filter);
78 if (r & LOFR_MARK_SEEN)
79 obj->flags |= SEEN;
80 if (r & LOFR_DO_SHOW)
81 show_object(ctx, obj, path->buf);
82 strbuf_setlen(path, pathlen);
85 static void process_tree(struct traversal_context *ctx,
86 struct tree *tree,
87 struct strbuf *base,
88 const char *name);
90 static void process_tree_contents(struct traversal_context *ctx,
91 struct tree *tree,
92 struct strbuf *base)
94 struct tree_desc desc;
95 struct name_entry entry;
96 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
97 all_entries_interesting : entry_not_interesting;
99 init_tree_desc(&desc, tree->buffer, tree->size);
101 while (tree_entry(&desc, &entry)) {
102 if (match != all_entries_interesting) {
103 match = tree_entry_interesting(ctx->revs->repo->index,
104 &entry, base, 0,
105 &ctx->revs->diffopt.pathspec);
106 if (match == all_entries_not_interesting)
107 break;
108 if (match == entry_not_interesting)
109 continue;
112 if (S_ISDIR(entry.mode)) {
113 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
114 if (!t) {
115 die(_("entry '%s' in tree %s has tree mode, "
116 "but is not a tree"),
117 entry.path, oid_to_hex(&tree->object.oid));
119 t->object.flags |= NOT_USER_GIVEN;
120 process_tree(ctx, t, base, entry.path);
122 else if (S_ISGITLINK(entry.mode))
123 ; /* ignore gitlink */
124 else {
125 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
126 if (!b) {
127 die(_("entry '%s' in tree %s has blob mode, "
128 "but is not a blob"),
129 entry.path, oid_to_hex(&tree->object.oid));
131 b->object.flags |= NOT_USER_GIVEN;
132 process_blob(ctx, b, base, entry.path);
137 static void process_tree(struct traversal_context *ctx,
138 struct tree *tree,
139 struct strbuf *base,
140 const char *name)
142 struct object *obj = &tree->object;
143 struct rev_info *revs = ctx->revs;
144 int baselen = base->len;
145 enum list_objects_filter_result r;
146 int failed_parse;
148 if (!revs->tree_objects)
149 return;
150 if (!obj)
151 die("bad tree object");
152 if (obj->flags & (UNINTERESTING | SEEN))
153 return;
154 if (revs->include_check_obj &&
155 !revs->include_check_obj(&tree->object, revs->include_check_data))
156 return;
158 failed_parse = parse_tree_gently(tree, 1);
159 if (failed_parse) {
160 if (revs->ignore_missing_links)
161 return;
164 * Pre-filter known-missing tree objects when explicitly
165 * requested. This may cause the actual filter to report
166 * an incomplete list of missing objects.
168 if (revs->exclude_promisor_objects &&
169 is_promisor_object(&obj->oid))
170 return;
172 if (!revs->do_not_die_on_missing_tree)
173 die("bad tree object %s", oid_to_hex(&obj->oid));
176 strbuf_addstr(base, name);
177 r = list_objects_filter__filter_object(ctx->revs->repo,
178 LOFS_BEGIN_TREE, obj,
179 base->buf, &base->buf[baselen],
180 ctx->filter);
181 if (r & LOFR_MARK_SEEN)
182 obj->flags |= SEEN;
183 if (r & LOFR_DO_SHOW)
184 show_object(ctx, obj, base->buf);
185 if (base->len)
186 strbuf_addch(base, '/');
188 if (r & LOFR_SKIP_TREE)
189 trace_printf("Skipping contents of tree %s...\n", base->buf);
190 else if (!failed_parse)
191 process_tree_contents(ctx, tree, base);
193 r = list_objects_filter__filter_object(ctx->revs->repo,
194 LOFS_END_TREE, obj,
195 base->buf, &base->buf[baselen],
196 ctx->filter);
197 if (r & LOFR_MARK_SEEN)
198 obj->flags |= SEEN;
199 if (r & LOFR_DO_SHOW)
200 show_object(ctx, obj, base->buf);
202 strbuf_setlen(base, baselen);
203 free_tree_buffer(tree);
206 static void process_tag(struct traversal_context *ctx,
207 struct tag *tag,
208 const char *name)
210 enum list_objects_filter_result r;
212 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
213 &tag->object, NULL, NULL,
214 ctx->filter);
215 if (r & LOFR_MARK_SEEN)
216 tag->object.flags |= SEEN;
217 if (r & LOFR_DO_SHOW)
218 show_object(ctx, &tag->object, name);
221 static void mark_edge_parents_uninteresting(struct commit *commit,
222 struct rev_info *revs,
223 show_edge_fn show_edge)
225 struct commit_list *parents;
227 for (parents = commit->parents; parents; parents = parents->next) {
228 struct commit *parent = parents->item;
229 if (!(parent->object.flags & UNINTERESTING))
230 continue;
231 mark_tree_uninteresting(revs->repo,
232 repo_get_commit_tree(the_repository, parent));
233 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
234 parent->object.flags |= SHOWN;
235 show_edge(parent);
240 static void add_edge_parents(struct commit *commit,
241 struct rev_info *revs,
242 show_edge_fn show_edge,
243 struct oidset *set)
245 struct commit_list *parents;
247 for (parents = commit->parents; parents; parents = parents->next) {
248 struct commit *parent = parents->item;
249 struct tree *tree = repo_get_commit_tree(the_repository,
250 parent);
252 if (!tree)
253 continue;
255 oidset_insert(set, &tree->object.oid);
257 if (!(parent->object.flags & UNINTERESTING))
258 continue;
259 tree->object.flags |= UNINTERESTING;
261 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
262 parent->object.flags |= SHOWN;
263 show_edge(parent);
268 void mark_edges_uninteresting(struct rev_info *revs,
269 show_edge_fn show_edge,
270 int sparse)
272 struct commit_list *list;
273 int i;
275 if (sparse) {
276 struct oidset set;
277 oidset_init(&set, 16);
279 for (list = revs->commits; list; list = list->next) {
280 struct commit *commit = list->item;
281 struct tree *tree = repo_get_commit_tree(the_repository,
282 commit);
284 if (commit->object.flags & UNINTERESTING)
285 tree->object.flags |= UNINTERESTING;
287 oidset_insert(&set, &tree->object.oid);
288 add_edge_parents(commit, revs, show_edge, &set);
291 mark_trees_uninteresting_sparse(revs->repo, &set);
292 oidset_clear(&set);
293 } else {
294 for (list = revs->commits; list; list = list->next) {
295 struct commit *commit = list->item;
296 if (commit->object.flags & UNINTERESTING) {
297 mark_tree_uninteresting(revs->repo,
298 repo_get_commit_tree(the_repository, commit));
299 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
300 commit->object.flags |= SHOWN;
301 show_edge(commit);
303 continue;
305 mark_edge_parents_uninteresting(commit, revs, show_edge);
309 if (revs->edge_hint_aggressive) {
310 for (i = 0; i < revs->cmdline.nr; i++) {
311 struct object *obj = revs->cmdline.rev[i].item;
312 struct commit *commit = (struct commit *)obj;
313 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
314 continue;
315 mark_tree_uninteresting(revs->repo,
316 repo_get_commit_tree(the_repository, commit));
317 if (!(obj->flags & SHOWN)) {
318 obj->flags |= SHOWN;
319 show_edge(commit);
325 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
327 add_pending_object(revs, &tree->object, "");
330 static void traverse_non_commits(struct traversal_context *ctx,
331 struct strbuf *base)
333 int i;
335 assert(base->len == 0);
337 for (i = 0; i < ctx->revs->pending.nr; i++) {
338 struct object_array_entry *pending = ctx->revs->pending.objects + i;
339 struct object *obj = pending->item;
340 const char *name = pending->name;
341 const char *path = pending->path;
342 if (obj->flags & (UNINTERESTING | SEEN))
343 continue;
344 if (obj->type == OBJ_TAG) {
345 process_tag(ctx, (struct tag *)obj, name);
346 continue;
348 if (!path)
349 path = "";
350 if (obj->type == OBJ_TREE) {
351 process_tree(ctx, (struct tree *)obj, base, path);
352 continue;
354 if (obj->type == OBJ_BLOB) {
355 process_blob(ctx, (struct blob *)obj, base, path);
356 continue;
358 die("unknown pending object %s (%s)",
359 oid_to_hex(&obj->oid), name);
361 object_array_clear(&ctx->revs->pending);
364 static void do_traverse(struct traversal_context *ctx)
366 struct commit *commit;
367 struct strbuf csp; /* callee's scratch pad */
368 strbuf_init(&csp, PATH_MAX);
370 while ((commit = get_revision(ctx->revs)) != NULL) {
371 enum list_objects_filter_result r;
373 r = list_objects_filter__filter_object(ctx->revs->repo,
374 LOFS_COMMIT, &commit->object,
375 NULL, NULL, ctx->filter);
378 * an uninteresting boundary commit may not have its tree
379 * parsed yet, but we are not going to show them anyway
381 if (!ctx->revs->tree_objects)
382 ; /* do not bother loading tree */
383 else if (repo_get_commit_tree(the_repository, commit)) {
384 struct tree *tree = repo_get_commit_tree(the_repository,
385 commit);
386 tree->object.flags |= NOT_USER_GIVEN;
387 add_pending_tree(ctx->revs, tree);
388 } else if (commit->object.parsed) {
389 die(_("unable to load root tree for commit %s"),
390 oid_to_hex(&commit->object.oid));
393 if (r & LOFR_MARK_SEEN)
394 commit->object.flags |= SEEN;
395 if (r & LOFR_DO_SHOW)
396 show_commit(ctx, commit);
398 if (ctx->revs->tree_blobs_in_commit_order)
400 * NEEDSWORK: Adding the tree and then flushing it here
401 * needs a reallocation for each commit. Can we pass the
402 * tree directory without allocation churn?
404 traverse_non_commits(ctx, &csp);
406 traverse_non_commits(ctx, &csp);
407 strbuf_release(&csp);
410 void traverse_commit_list_filtered(
411 struct rev_info *revs,
412 show_commit_fn show_commit,
413 show_object_fn show_object,
414 void *show_data,
415 struct oidset *omitted)
417 struct traversal_context ctx = {
418 .revs = revs,
419 .show_object = show_object,
420 .show_commit = show_commit,
421 .show_data = show_data,
424 if (revs->filter.choice)
425 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
427 do_traverse(&ctx);
429 if (ctx.filter)
430 list_objects_filter__free(ctx.filter);