Documentation/technical: describe bitmap lookup table extension
[git/debian.git] / list-objects.c
blob250d9de41cb56072e95420530b203ce417191a2b
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "diff.h"
7 #include "tree-walk.h"
8 #include "revision.h"
9 #include "list-objects.h"
10 #include "list-objects-filter.h"
11 #include "list-objects-filter-options.h"
12 #include "packfile.h"
13 #include "object-store.h"
14 #include "trace.h"
16 struct traversal_context {
17 struct rev_info *revs;
18 show_object_fn show_object;
19 show_commit_fn show_commit;
20 void *show_data;
21 struct filter *filter;
24 static void show_commit(struct traversal_context *ctx,
25 struct commit *commit)
27 if (!ctx->show_commit)
28 return;
29 ctx->show_commit(commit, ctx->show_data);
32 static void show_object(struct traversal_context *ctx,
33 struct object *object,
34 const char *name)
36 if (!ctx->show_object)
37 return;
38 ctx->show_object(object, name, ctx->show_data);
41 static void process_blob(struct traversal_context *ctx,
42 struct blob *blob,
43 struct strbuf *path,
44 const char *name)
46 struct object *obj = &blob->object;
47 size_t pathlen;
48 enum list_objects_filter_result r;
50 if (!ctx->revs->blob_objects)
51 return;
52 if (!obj)
53 die("bad blob object");
54 if (obj->flags & (UNINTERESTING | SEEN))
55 return;
58 * Pre-filter known-missing objects when explicitly requested.
59 * Otherwise, a missing object error message may be reported
60 * later (depending on other filtering criteria).
62 * Note that this "--exclude-promisor-objects" pre-filtering
63 * may cause the actual filter to report an incomplete list
64 * of missing objects.
66 if (ctx->revs->exclude_promisor_objects &&
67 !has_object_file(&obj->oid) &&
68 is_promisor_object(&obj->oid))
69 return;
71 pathlen = path->len;
72 strbuf_addstr(path, name);
73 r = list_objects_filter__filter_object(ctx->revs->repo,
74 LOFS_BLOB, obj,
75 path->buf, &path->buf[pathlen],
76 ctx->filter);
77 if (r & LOFR_MARK_SEEN)
78 obj->flags |= SEEN;
79 if (r & LOFR_DO_SHOW)
80 show_object(ctx, obj, path->buf);
81 strbuf_setlen(path, pathlen);
85 * Processing a gitlink entry currently does nothing, since
86 * we do not recurse into the subproject.
88 * We *could* eventually add a flag that actually does that,
89 * which would involve:
90 * - is the subproject actually checked out?
91 * - if so, see if the subproject has already been added
92 * to the alternates list, and add it if not.
93 * - process the commit (or tag) the gitlink points to
94 * recursively.
96 * However, it's unclear whether there is really ever any
97 * reason to see superprojects and subprojects as such a
98 * "unified" object pool (potentially resulting in a totally
99 * humongous pack - avoiding which was the whole point of
100 * having gitlinks in the first place!).
102 * So for now, there is just a note that we *could* follow
103 * the link, and how to do it. Whether it necessarily makes
104 * any sense what-so-ever to ever do that is another issue.
106 static void process_gitlink(struct traversal_context *ctx,
107 const unsigned char *sha1,
108 struct strbuf *path,
109 const char *name)
111 /* Nothing to do */
114 static void process_tree(struct traversal_context *ctx,
115 struct tree *tree,
116 struct strbuf *base,
117 const char *name);
119 static void process_tree_contents(struct traversal_context *ctx,
120 struct tree *tree,
121 struct strbuf *base)
123 struct tree_desc desc;
124 struct name_entry entry;
125 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
126 all_entries_interesting : entry_not_interesting;
128 init_tree_desc(&desc, tree->buffer, tree->size);
130 while (tree_entry(&desc, &entry)) {
131 if (match != all_entries_interesting) {
132 match = tree_entry_interesting(ctx->revs->repo->index,
133 &entry, base, 0,
134 &ctx->revs->diffopt.pathspec);
135 if (match == all_entries_not_interesting)
136 break;
137 if (match == entry_not_interesting)
138 continue;
141 if (S_ISDIR(entry.mode)) {
142 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
143 if (!t) {
144 die(_("entry '%s' in tree %s has tree mode, "
145 "but is not a tree"),
146 entry.path, oid_to_hex(&tree->object.oid));
148 t->object.flags |= NOT_USER_GIVEN;
149 process_tree(ctx, t, base, entry.path);
151 else if (S_ISGITLINK(entry.mode))
152 process_gitlink(ctx, entry.oid.hash,
153 base, entry.path);
154 else {
155 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
156 if (!b) {
157 die(_("entry '%s' in tree %s has blob mode, "
158 "but is not a blob"),
159 entry.path, oid_to_hex(&tree->object.oid));
161 b->object.flags |= NOT_USER_GIVEN;
162 process_blob(ctx, b, base, entry.path);
167 static void process_tree(struct traversal_context *ctx,
168 struct tree *tree,
169 struct strbuf *base,
170 const char *name)
172 struct object *obj = &tree->object;
173 struct rev_info *revs = ctx->revs;
174 int baselen = base->len;
175 enum list_objects_filter_result r;
176 int failed_parse;
178 if (!revs->tree_objects)
179 return;
180 if (!obj)
181 die("bad tree object");
182 if (obj->flags & (UNINTERESTING | SEEN))
183 return;
184 if (revs->include_check_obj &&
185 !revs->include_check_obj(&tree->object, revs->include_check_data))
186 return;
188 failed_parse = parse_tree_gently(tree, 1);
189 if (failed_parse) {
190 if (revs->ignore_missing_links)
191 return;
194 * Pre-filter known-missing tree objects when explicitly
195 * requested. This may cause the actual filter to report
196 * an incomplete list of missing objects.
198 if (revs->exclude_promisor_objects &&
199 is_promisor_object(&obj->oid))
200 return;
202 if (!revs->do_not_die_on_missing_tree)
203 die("bad tree object %s", oid_to_hex(&obj->oid));
206 strbuf_addstr(base, name);
207 r = list_objects_filter__filter_object(ctx->revs->repo,
208 LOFS_BEGIN_TREE, obj,
209 base->buf, &base->buf[baselen],
210 ctx->filter);
211 if (r & LOFR_MARK_SEEN)
212 obj->flags |= SEEN;
213 if (r & LOFR_DO_SHOW)
214 show_object(ctx, obj, base->buf);
215 if (base->len)
216 strbuf_addch(base, '/');
218 if (r & LOFR_SKIP_TREE)
219 trace_printf("Skipping contents of tree %s...\n", base->buf);
220 else if (!failed_parse)
221 process_tree_contents(ctx, tree, base);
223 r = list_objects_filter__filter_object(ctx->revs->repo,
224 LOFS_END_TREE, obj,
225 base->buf, &base->buf[baselen],
226 ctx->filter);
227 if (r & LOFR_MARK_SEEN)
228 obj->flags |= SEEN;
229 if (r & LOFR_DO_SHOW)
230 show_object(ctx, obj, base->buf);
232 strbuf_setlen(base, baselen);
233 free_tree_buffer(tree);
236 static void process_tag(struct traversal_context *ctx,
237 struct tag *tag,
238 const char *name)
240 enum list_objects_filter_result r;
242 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
243 &tag->object, NULL, NULL,
244 ctx->filter);
245 if (r & LOFR_MARK_SEEN)
246 tag->object.flags |= SEEN;
247 if (r & LOFR_DO_SHOW)
248 show_object(ctx, &tag->object, name);
251 static void mark_edge_parents_uninteresting(struct commit *commit,
252 struct rev_info *revs,
253 show_edge_fn show_edge)
255 struct commit_list *parents;
257 for (parents = commit->parents; parents; parents = parents->next) {
258 struct commit *parent = parents->item;
259 if (!(parent->object.flags & UNINTERESTING))
260 continue;
261 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
262 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
263 parent->object.flags |= SHOWN;
264 show_edge(parent);
269 static void add_edge_parents(struct commit *commit,
270 struct rev_info *revs,
271 show_edge_fn show_edge,
272 struct oidset *set)
274 struct commit_list *parents;
276 for (parents = commit->parents; parents; parents = parents->next) {
277 struct commit *parent = parents->item;
278 struct tree *tree = get_commit_tree(parent);
280 if (!tree)
281 continue;
283 oidset_insert(set, &tree->object.oid);
285 if (!(parent->object.flags & UNINTERESTING))
286 continue;
287 tree->object.flags |= UNINTERESTING;
289 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
290 parent->object.flags |= SHOWN;
291 show_edge(parent);
296 void mark_edges_uninteresting(struct rev_info *revs,
297 show_edge_fn show_edge,
298 int sparse)
300 struct commit_list *list;
301 int i;
303 if (sparse) {
304 struct oidset set;
305 oidset_init(&set, 16);
307 for (list = revs->commits; list; list = list->next) {
308 struct commit *commit = list->item;
309 struct tree *tree = get_commit_tree(commit);
311 if (commit->object.flags & UNINTERESTING)
312 tree->object.flags |= UNINTERESTING;
314 oidset_insert(&set, &tree->object.oid);
315 add_edge_parents(commit, revs, show_edge, &set);
318 mark_trees_uninteresting_sparse(revs->repo, &set);
319 oidset_clear(&set);
320 } else {
321 for (list = revs->commits; list; list = list->next) {
322 struct commit *commit = list->item;
323 if (commit->object.flags & UNINTERESTING) {
324 mark_tree_uninteresting(revs->repo,
325 get_commit_tree(commit));
326 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
327 commit->object.flags |= SHOWN;
328 show_edge(commit);
330 continue;
332 mark_edge_parents_uninteresting(commit, revs, show_edge);
336 if (revs->edge_hint_aggressive) {
337 for (i = 0; i < revs->cmdline.nr; i++) {
338 struct object *obj = revs->cmdline.rev[i].item;
339 struct commit *commit = (struct commit *)obj;
340 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
341 continue;
342 mark_tree_uninteresting(revs->repo,
343 get_commit_tree(commit));
344 if (!(obj->flags & SHOWN)) {
345 obj->flags |= SHOWN;
346 show_edge(commit);
352 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
354 add_pending_object(revs, &tree->object, "");
357 static void traverse_non_commits(struct traversal_context *ctx,
358 struct strbuf *base)
360 int i;
362 assert(base->len == 0);
364 for (i = 0; i < ctx->revs->pending.nr; i++) {
365 struct object_array_entry *pending = ctx->revs->pending.objects + i;
366 struct object *obj = pending->item;
367 const char *name = pending->name;
368 const char *path = pending->path;
369 if (obj->flags & (UNINTERESTING | SEEN))
370 continue;
371 if (obj->type == OBJ_TAG) {
372 process_tag(ctx, (struct tag *)obj, name);
373 continue;
375 if (!path)
376 path = "";
377 if (obj->type == OBJ_TREE) {
378 process_tree(ctx, (struct tree *)obj, base, path);
379 continue;
381 if (obj->type == OBJ_BLOB) {
382 process_blob(ctx, (struct blob *)obj, base, path);
383 continue;
385 die("unknown pending object %s (%s)",
386 oid_to_hex(&obj->oid), name);
388 object_array_clear(&ctx->revs->pending);
391 static void do_traverse(struct traversal_context *ctx)
393 struct commit *commit;
394 struct strbuf csp; /* callee's scratch pad */
395 strbuf_init(&csp, PATH_MAX);
397 while ((commit = get_revision(ctx->revs)) != NULL) {
398 enum list_objects_filter_result r;
400 r = list_objects_filter__filter_object(ctx->revs->repo,
401 LOFS_COMMIT, &commit->object,
402 NULL, NULL, ctx->filter);
405 * an uninteresting boundary commit may not have its tree
406 * parsed yet, but we are not going to show them anyway
408 if (!ctx->revs->tree_objects)
409 ; /* do not bother loading tree */
410 else if (get_commit_tree(commit)) {
411 struct tree *tree = get_commit_tree(commit);
412 tree->object.flags |= NOT_USER_GIVEN;
413 add_pending_tree(ctx->revs, tree);
414 } else if (commit->object.parsed) {
415 die(_("unable to load root tree for commit %s"),
416 oid_to_hex(&commit->object.oid));
419 if (r & LOFR_MARK_SEEN)
420 commit->object.flags |= SEEN;
421 if (r & LOFR_DO_SHOW)
422 show_commit(ctx, commit);
424 if (ctx->revs->tree_blobs_in_commit_order)
426 * NEEDSWORK: Adding the tree and then flushing it here
427 * needs a reallocation for each commit. Can we pass the
428 * tree directory without allocation churn?
430 traverse_non_commits(ctx, &csp);
432 traverse_non_commits(ctx, &csp);
433 strbuf_release(&csp);
436 void traverse_commit_list_filtered(
437 struct rev_info *revs,
438 show_commit_fn show_commit,
439 show_object_fn show_object,
440 void *show_data,
441 struct oidset *omitted)
443 struct traversal_context ctx = {
444 .revs = revs,
445 .show_object = show_object,
446 .show_commit = show_commit,
447 .show_data = show_data,
450 if (revs->filter.choice)
451 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
453 do_traverse(&ctx);
455 if (ctx.filter)
456 list_objects_filter__free(ctx.filter);