]> git.ipfire.org Git - thirdparty/git.git/blob - list-objects.c
Merge branch 'ab/config-multi-and-nonbool'
[thirdparty/git.git] / list-objects.c
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"
16
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;
23 };
24
25 static void show_commit(struct traversal_context *ctx,
26 struct commit *commit)
27 {
28 if (!ctx->show_commit)
29 return;
30 ctx->show_commit(commit, ctx->show_data);
31 }
32
33 static void show_object(struct traversal_context *ctx,
34 struct object *object,
35 const char *name)
36 {
37 if (!ctx->show_object)
38 return;
39 ctx->show_object(object, name, ctx->show_data);
40 }
41
42 static void process_blob(struct traversal_context *ctx,
43 struct blob *blob,
44 struct strbuf *path,
45 const char *name)
46 {
47 struct object *obj = &blob->object;
48 size_t pathlen;
49 enum list_objects_filter_result r;
50
51 if (!ctx->revs->blob_objects)
52 return;
53 if (!obj)
54 die("bad blob object");
55 if (obj->flags & (UNINTERESTING | SEEN))
56 return;
57
58 /*
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).
62 *
63 * Note that this "--exclude-promisor-objects" pre-filtering
64 * may cause the actual filter to report an incomplete list
65 * of missing objects.
66 */
67 if (ctx->revs->exclude_promisor_objects &&
68 !has_object_file(&obj->oid) &&
69 is_promisor_object(&obj->oid))
70 return;
71
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);
83 }
84
85 static void process_tree(struct traversal_context *ctx,
86 struct tree *tree,
87 struct strbuf *base,
88 const char *name);
89
90 static void process_tree_contents(struct traversal_context *ctx,
91 struct tree *tree,
92 struct strbuf *base)
93 {
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;
98
99 init_tree_desc(&desc, tree->buffer, tree->size);
100
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;
110 }
111
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));
118 }
119 t->object.flags |= NOT_USER_GIVEN;
120 process_tree(ctx, t, base, entry.path);
121 }
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));
130 }
131 b->object.flags |= NOT_USER_GIVEN;
132 process_blob(ctx, b, base, entry.path);
133 }
134 }
135 }
136
137 static void process_tree(struct traversal_context *ctx,
138 struct tree *tree,
139 struct strbuf *base,
140 const char *name)
141 {
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;
147
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;
157
158 failed_parse = parse_tree_gently(tree, 1);
159 if (failed_parse) {
160 if (revs->ignore_missing_links)
161 return;
162
163 /*
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.
167 */
168 if (revs->exclude_promisor_objects &&
169 is_promisor_object(&obj->oid))
170 return;
171
172 if (!revs->do_not_die_on_missing_tree)
173 die("bad tree object %s", oid_to_hex(&obj->oid));
174 }
175
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, '/');
187
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);
192
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);
201
202 strbuf_setlen(base, baselen);
203 free_tree_buffer(tree);
204 }
205
206 static void process_tag(struct traversal_context *ctx,
207 struct tag *tag,
208 const char *name)
209 {
210 enum list_objects_filter_result r;
211
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);
219 }
220
221 static void mark_edge_parents_uninteresting(struct commit *commit,
222 struct rev_info *revs,
223 show_edge_fn show_edge)
224 {
225 struct commit_list *parents;
226
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, get_commit_tree(parent));
232 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
233 parent->object.flags |= SHOWN;
234 show_edge(parent);
235 }
236 }
237 }
238
239 static void add_edge_parents(struct commit *commit,
240 struct rev_info *revs,
241 show_edge_fn show_edge,
242 struct oidset *set)
243 {
244 struct commit_list *parents;
245
246 for (parents = commit->parents; parents; parents = parents->next) {
247 struct commit *parent = parents->item;
248 struct tree *tree = get_commit_tree(parent);
249
250 if (!tree)
251 continue;
252
253 oidset_insert(set, &tree->object.oid);
254
255 if (!(parent->object.flags & UNINTERESTING))
256 continue;
257 tree->object.flags |= UNINTERESTING;
258
259 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
260 parent->object.flags |= SHOWN;
261 show_edge(parent);
262 }
263 }
264 }
265
266 void mark_edges_uninteresting(struct rev_info *revs,
267 show_edge_fn show_edge,
268 int sparse)
269 {
270 struct commit_list *list;
271 int i;
272
273 if (sparse) {
274 struct oidset set;
275 oidset_init(&set, 16);
276
277 for (list = revs->commits; list; list = list->next) {
278 struct commit *commit = list->item;
279 struct tree *tree = get_commit_tree(commit);
280
281 if (commit->object.flags & UNINTERESTING)
282 tree->object.flags |= UNINTERESTING;
283
284 oidset_insert(&set, &tree->object.oid);
285 add_edge_parents(commit, revs, show_edge, &set);
286 }
287
288 mark_trees_uninteresting_sparse(revs->repo, &set);
289 oidset_clear(&set);
290 } else {
291 for (list = revs->commits; list; list = list->next) {
292 struct commit *commit = list->item;
293 if (commit->object.flags & UNINTERESTING) {
294 mark_tree_uninteresting(revs->repo,
295 get_commit_tree(commit));
296 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
297 commit->object.flags |= SHOWN;
298 show_edge(commit);
299 }
300 continue;
301 }
302 mark_edge_parents_uninteresting(commit, revs, show_edge);
303 }
304 }
305
306 if (revs->edge_hint_aggressive) {
307 for (i = 0; i < revs->cmdline.nr; i++) {
308 struct object *obj = revs->cmdline.rev[i].item;
309 struct commit *commit = (struct commit *)obj;
310 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
311 continue;
312 mark_tree_uninteresting(revs->repo,
313 get_commit_tree(commit));
314 if (!(obj->flags & SHOWN)) {
315 obj->flags |= SHOWN;
316 show_edge(commit);
317 }
318 }
319 }
320 }
321
322 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
323 {
324 add_pending_object(revs, &tree->object, "");
325 }
326
327 static void traverse_non_commits(struct traversal_context *ctx,
328 struct strbuf *base)
329 {
330 int i;
331
332 assert(base->len == 0);
333
334 for (i = 0; i < ctx->revs->pending.nr; i++) {
335 struct object_array_entry *pending = ctx->revs->pending.objects + i;
336 struct object *obj = pending->item;
337 const char *name = pending->name;
338 const char *path = pending->path;
339 if (obj->flags & (UNINTERESTING | SEEN))
340 continue;
341 if (obj->type == OBJ_TAG) {
342 process_tag(ctx, (struct tag *)obj, name);
343 continue;
344 }
345 if (!path)
346 path = "";
347 if (obj->type == OBJ_TREE) {
348 process_tree(ctx, (struct tree *)obj, base, path);
349 continue;
350 }
351 if (obj->type == OBJ_BLOB) {
352 process_blob(ctx, (struct blob *)obj, base, path);
353 continue;
354 }
355 die("unknown pending object %s (%s)",
356 oid_to_hex(&obj->oid), name);
357 }
358 object_array_clear(&ctx->revs->pending);
359 }
360
361 static void do_traverse(struct traversal_context *ctx)
362 {
363 struct commit *commit;
364 struct strbuf csp; /* callee's scratch pad */
365 strbuf_init(&csp, PATH_MAX);
366
367 while ((commit = get_revision(ctx->revs)) != NULL) {
368 enum list_objects_filter_result r;
369
370 r = list_objects_filter__filter_object(ctx->revs->repo,
371 LOFS_COMMIT, &commit->object,
372 NULL, NULL, ctx->filter);
373
374 /*
375 * an uninteresting boundary commit may not have its tree
376 * parsed yet, but we are not going to show them anyway
377 */
378 if (!ctx->revs->tree_objects)
379 ; /* do not bother loading tree */
380 else if (get_commit_tree(commit)) {
381 struct tree *tree = get_commit_tree(commit);
382 tree->object.flags |= NOT_USER_GIVEN;
383 add_pending_tree(ctx->revs, tree);
384 } else if (commit->object.parsed) {
385 die(_("unable to load root tree for commit %s"),
386 oid_to_hex(&commit->object.oid));
387 }
388
389 if (r & LOFR_MARK_SEEN)
390 commit->object.flags |= SEEN;
391 if (r & LOFR_DO_SHOW)
392 show_commit(ctx, commit);
393
394 if (ctx->revs->tree_blobs_in_commit_order)
395 /*
396 * NEEDSWORK: Adding the tree and then flushing it here
397 * needs a reallocation for each commit. Can we pass the
398 * tree directory without allocation churn?
399 */
400 traverse_non_commits(ctx, &csp);
401 }
402 traverse_non_commits(ctx, &csp);
403 strbuf_release(&csp);
404 }
405
406 void traverse_commit_list_filtered(
407 struct rev_info *revs,
408 show_commit_fn show_commit,
409 show_object_fn show_object,
410 void *show_data,
411 struct oidset *omitted)
412 {
413 struct traversal_context ctx = {
414 .revs = revs,
415 .show_object = show_object,
416 .show_commit = show_commit,
417 .show_data = show_data,
418 };
419
420 if (revs->filter.choice)
421 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
422
423 do_traverse(&ctx);
424
425 if (ctx.filter)
426 list_objects_filter__free(ctx.filter);
427 }