find_hook(): refactor the `STRIP_EXTENSION` logic
[git/debian.git] / list-objects.c
blob7528fe1db2c85c0b4d923854ef3ec53c3a8f1fad
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);
84 static void process_tree(struct traversal_context *ctx,
85 struct tree *tree,
86 struct strbuf *base,
87 const char *name);
89 static void process_tree_contents(struct traversal_context *ctx,
90 struct tree *tree,
91 struct strbuf *base)
93 struct tree_desc desc;
94 struct name_entry entry;
95 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
96 all_entries_interesting : entry_not_interesting;
98 init_tree_desc(&desc, tree->buffer, tree->size);
100 while (tree_entry(&desc, &entry)) {
101 if (match != all_entries_interesting) {
102 match = tree_entry_interesting(ctx->revs->repo->index,
103 &entry, base, 0,
104 &ctx->revs->diffopt.pathspec);
105 if (match == all_entries_not_interesting)
106 break;
107 if (match == entry_not_interesting)
108 continue;
111 if (S_ISDIR(entry.mode)) {
112 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
113 if (!t) {
114 die(_("entry '%s' in tree %s has tree mode, "
115 "but is not a tree"),
116 entry.path, oid_to_hex(&tree->object.oid));
118 t->object.flags |= NOT_USER_GIVEN;
119 process_tree(ctx, t, base, entry.path);
121 else if (S_ISGITLINK(entry.mode))
122 ; /* ignore gitlink */
123 else {
124 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
125 if (!b) {
126 die(_("entry '%s' in tree %s has blob mode, "
127 "but is not a blob"),
128 entry.path, oid_to_hex(&tree->object.oid));
130 b->object.flags |= NOT_USER_GIVEN;
131 process_blob(ctx, b, base, entry.path);
136 static void process_tree(struct traversal_context *ctx,
137 struct tree *tree,
138 struct strbuf *base,
139 const char *name)
141 struct object *obj = &tree->object;
142 struct rev_info *revs = ctx->revs;
143 int baselen = base->len;
144 enum list_objects_filter_result r;
145 int failed_parse;
147 if (!revs->tree_objects)
148 return;
149 if (!obj)
150 die("bad tree object");
151 if (obj->flags & (UNINTERESTING | SEEN))
152 return;
153 if (revs->include_check_obj &&
154 !revs->include_check_obj(&tree->object, revs->include_check_data))
155 return;
157 failed_parse = parse_tree_gently(tree, 1);
158 if (failed_parse) {
159 if (revs->ignore_missing_links)
160 return;
163 * Pre-filter known-missing tree objects when explicitly
164 * requested. This may cause the actual filter to report
165 * an incomplete list of missing objects.
167 if (revs->exclude_promisor_objects &&
168 is_promisor_object(&obj->oid))
169 return;
171 if (!revs->do_not_die_on_missing_tree)
172 die("bad tree object %s", oid_to_hex(&obj->oid));
175 strbuf_addstr(base, name);
176 r = list_objects_filter__filter_object(ctx->revs->repo,
177 LOFS_BEGIN_TREE, obj,
178 base->buf, &base->buf[baselen],
179 ctx->filter);
180 if (r & LOFR_MARK_SEEN)
181 obj->flags |= SEEN;
182 if (r & LOFR_DO_SHOW)
183 show_object(ctx, obj, base->buf);
184 if (base->len)
185 strbuf_addch(base, '/');
187 if (r & LOFR_SKIP_TREE)
188 trace_printf("Skipping contents of tree %s...\n", base->buf);
189 else if (!failed_parse)
190 process_tree_contents(ctx, tree, base);
192 r = list_objects_filter__filter_object(ctx->revs->repo,
193 LOFS_END_TREE, obj,
194 base->buf, &base->buf[baselen],
195 ctx->filter);
196 if (r & LOFR_MARK_SEEN)
197 obj->flags |= SEEN;
198 if (r & LOFR_DO_SHOW)
199 show_object(ctx, obj, base->buf);
201 strbuf_setlen(base, baselen);
202 free_tree_buffer(tree);
205 static void process_tag(struct traversal_context *ctx,
206 struct tag *tag,
207 const char *name)
209 enum list_objects_filter_result r;
211 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
212 &tag->object, NULL, NULL,
213 ctx->filter);
214 if (r & LOFR_MARK_SEEN)
215 tag->object.flags |= SEEN;
216 if (r & LOFR_DO_SHOW)
217 show_object(ctx, &tag->object, name);
220 static void mark_edge_parents_uninteresting(struct commit *commit,
221 struct rev_info *revs,
222 show_edge_fn show_edge)
224 struct commit_list *parents;
226 for (parents = commit->parents; parents; parents = parents->next) {
227 struct commit *parent = parents->item;
228 if (!(parent->object.flags & UNINTERESTING))
229 continue;
230 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
231 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
232 parent->object.flags |= SHOWN;
233 show_edge(parent);
238 static void add_edge_parents(struct commit *commit,
239 struct rev_info *revs,
240 show_edge_fn show_edge,
241 struct oidset *set)
243 struct commit_list *parents;
245 for (parents = commit->parents; parents; parents = parents->next) {
246 struct commit *parent = parents->item;
247 struct tree *tree = get_commit_tree(parent);
249 if (!tree)
250 continue;
252 oidset_insert(set, &tree->object.oid);
254 if (!(parent->object.flags & UNINTERESTING))
255 continue;
256 tree->object.flags |= UNINTERESTING;
258 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
259 parent->object.flags |= SHOWN;
260 show_edge(parent);
265 void mark_edges_uninteresting(struct rev_info *revs,
266 show_edge_fn show_edge,
267 int sparse)
269 struct commit_list *list;
270 int i;
272 if (sparse) {
273 struct oidset set;
274 oidset_init(&set, 16);
276 for (list = revs->commits; list; list = list->next) {
277 struct commit *commit = list->item;
278 struct tree *tree = get_commit_tree(commit);
280 if (commit->object.flags & UNINTERESTING)
281 tree->object.flags |= UNINTERESTING;
283 oidset_insert(&set, &tree->object.oid);
284 add_edge_parents(commit, revs, show_edge, &set);
287 mark_trees_uninteresting_sparse(revs->repo, &set);
288 oidset_clear(&set);
289 } else {
290 for (list = revs->commits; list; list = list->next) {
291 struct commit *commit = list->item;
292 if (commit->object.flags & UNINTERESTING) {
293 mark_tree_uninteresting(revs->repo,
294 get_commit_tree(commit));
295 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
296 commit->object.flags |= SHOWN;
297 show_edge(commit);
299 continue;
301 mark_edge_parents_uninteresting(commit, revs, show_edge);
305 if (revs->edge_hint_aggressive) {
306 for (i = 0; i < revs->cmdline.nr; i++) {
307 struct object *obj = revs->cmdline.rev[i].item;
308 struct commit *commit = (struct commit *)obj;
309 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
310 continue;
311 mark_tree_uninteresting(revs->repo,
312 get_commit_tree(commit));
313 if (!(obj->flags & SHOWN)) {
314 obj->flags |= SHOWN;
315 show_edge(commit);
321 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
323 add_pending_object(revs, &tree->object, "");
326 static void traverse_non_commits(struct traversal_context *ctx,
327 struct strbuf *base)
329 int i;
331 assert(base->len == 0);
333 for (i = 0; i < ctx->revs->pending.nr; i++) {
334 struct object_array_entry *pending = ctx->revs->pending.objects + i;
335 struct object *obj = pending->item;
336 const char *name = pending->name;
337 const char *path = pending->path;
338 if (obj->flags & (UNINTERESTING | SEEN))
339 continue;
340 if (obj->type == OBJ_TAG) {
341 process_tag(ctx, (struct tag *)obj, name);
342 continue;
344 if (!path)
345 path = "";
346 if (obj->type == OBJ_TREE) {
347 process_tree(ctx, (struct tree *)obj, base, path);
348 continue;
350 if (obj->type == OBJ_BLOB) {
351 process_blob(ctx, (struct blob *)obj, base, path);
352 continue;
354 die("unknown pending object %s (%s)",
355 oid_to_hex(&obj->oid), name);
357 object_array_clear(&ctx->revs->pending);
360 static void do_traverse(struct traversal_context *ctx)
362 struct commit *commit;
363 struct strbuf csp; /* callee's scratch pad */
364 strbuf_init(&csp, PATH_MAX);
366 while ((commit = get_revision(ctx->revs)) != NULL) {
367 enum list_objects_filter_result r;
369 r = list_objects_filter__filter_object(ctx->revs->repo,
370 LOFS_COMMIT, &commit->object,
371 NULL, NULL, ctx->filter);
374 * an uninteresting boundary commit may not have its tree
375 * parsed yet, but we are not going to show them anyway
377 if (!ctx->revs->tree_objects)
378 ; /* do not bother loading tree */
379 else if (get_commit_tree(commit)) {
380 struct tree *tree = get_commit_tree(commit);
381 tree->object.flags |= NOT_USER_GIVEN;
382 add_pending_tree(ctx->revs, tree);
383 } else if (commit->object.parsed) {
384 die(_("unable to load root tree for commit %s"),
385 oid_to_hex(&commit->object.oid));
388 if (r & LOFR_MARK_SEEN)
389 commit->object.flags |= SEEN;
390 if (r & LOFR_DO_SHOW)
391 show_commit(ctx, commit);
393 if (ctx->revs->tree_blobs_in_commit_order)
395 * NEEDSWORK: Adding the tree and then flushing it here
396 * needs a reallocation for each commit. Can we pass the
397 * tree directory without allocation churn?
399 traverse_non_commits(ctx, &csp);
401 traverse_non_commits(ctx, &csp);
402 strbuf_release(&csp);
405 void traverse_commit_list_filtered(
406 struct rev_info *revs,
407 show_commit_fn show_commit,
408 show_object_fn show_object,
409 void *show_data,
410 struct oidset *omitted)
412 struct traversal_context ctx = {
413 .revs = revs,
414 .show_object = show_object,
415 .show_commit = show_commit,
416 .show_data = show_data,
419 if (revs->filter.choice)
420 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
422 do_traverse(&ctx);
424 if (ctx.filter)
425 list_objects_filter__free(ctx.filter);