]>
Commit | Line | Data |
---|---|---|
c64ed70d JH |
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" | |
25ec7bca JH |
10 | #include "list-objects-filter.h" |
11 | #include "list-objects-filter-options.h" | |
df11e196 | 12 | #include "packfile.h" |
cbd53a21 | 13 | #include "object-store.h" |
8b10a206 | 14 | #include "trace.h" |
c64ed70d | 15 | |
f447a499 MD |
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; | |
9430147c | 21 | struct filter *filter; |
f447a499 MD |
22 | }; |
23 | ||
4f33a634 ÆAB |
24 | static void show_commit(struct traversal_context *ctx, |
25 | struct commit *commit) | |
26 | { | |
27 | if (!ctx->show_commit) | |
28 | return; | |
29 | ctx->show_commit(commit, ctx->show_data); | |
30 | } | |
31 | ||
32 | static void show_object(struct traversal_context *ctx, | |
33 | struct object *object, | |
34 | const char *name) | |
35 | { | |
36 | if (!ctx->show_object) | |
37 | return; | |
38 | ctx->show_object(object, name, ctx->show_data); | |
39 | } | |
40 | ||
f447a499 | 41 | static void process_blob(struct traversal_context *ctx, |
c64ed70d | 42 | struct blob *blob, |
bd64516a | 43 | struct strbuf *path, |
f447a499 | 44 | const char *name) |
c64ed70d JH |
45 | { |
46 | struct object *obj = &blob->object; | |
de1e67d0 | 47 | size_t pathlen; |
9430147c | 48 | enum list_objects_filter_result r; |
c64ed70d | 49 | |
f447a499 | 50 | if (!ctx->revs->blob_objects) |
c64ed70d | 51 | return; |
a301b0c8 MK |
52 | if (!obj) |
53 | die("bad blob object"); | |
c64ed70d JH |
54 | if (obj->flags & (UNINTERESTING | SEEN)) |
55 | return; | |
de1e67d0 | 56 | |
df11e196 JT |
57 | /* |
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). | |
61 | * | |
62 | * Note that this "--exclude-promisor-objects" pre-filtering | |
63 | * may cause the actual filter to report an incomplete list | |
64 | * of missing objects. | |
65 | */ | |
f447a499 | 66 | if (ctx->revs->exclude_promisor_objects && |
df11e196 JT |
67 | !has_object_file(&obj->oid) && |
68 | is_promisor_object(&obj->oid)) | |
69 | return; | |
70 | ||
de1e67d0 JK |
71 | pathlen = path->len; |
72 | strbuf_addstr(path, name); | |
9430147c MD |
73 | r = list_objects_filter__filter_object(ctx->revs->repo, |
74 | LOFS_BLOB, obj, | |
75 | path->buf, &path->buf[pathlen], | |
76 | ctx->filter); | |
25ec7bca JH |
77 | if (r & LOFR_MARK_SEEN) |
78 | obj->flags |= SEEN; | |
79 | if (r & LOFR_DO_SHOW) | |
4f33a634 | 80 | show_object(ctx, obj, path->buf); |
de1e67d0 | 81 | strbuf_setlen(path, pathlen); |
c64ed70d JH |
82 | } |
83 | ||
6e2f441b LT |
84 | /* |
85 | * Processing a gitlink entry currently does nothing, since | |
86 | * we do not recurse into the subproject. | |
87 | * | |
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. | |
95 | * | |
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!). | |
101 | * | |
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. | |
105 | */ | |
f447a499 | 106 | static void process_gitlink(struct traversal_context *ctx, |
6e2f441b | 107 | const unsigned char *sha1, |
bd64516a | 108 | struct strbuf *path, |
f447a499 | 109 | const char *name) |
6e2f441b LT |
110 | { |
111 | /* Nothing to do */ | |
112 | } | |
113 | ||
92024891 | 114 | static void process_tree(struct traversal_context *ctx, |
c64ed70d | 115 | struct tree *tree, |
cc5fa2fd | 116 | struct strbuf *base, |
92024891 MD |
117 | const char *name); |
118 | ||
119 | static void process_tree_contents(struct traversal_context *ctx, | |
120 | struct tree *tree, | |
121 | struct strbuf *base) | |
c64ed70d | 122 | { |
c64ed70d JH |
123 | struct tree_desc desc; |
124 | struct name_entry entry; | |
92024891 MD |
125 | enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ? |
126 | all_entries_interesting : entry_not_interesting; | |
127 | ||
128 | init_tree_desc(&desc, tree->buffer, tree->size); | |
129 | ||
130 | while (tree_entry(&desc, &entry)) { | |
131 | if (match != all_entries_interesting) { | |
67022e02 NTND |
132 | match = tree_entry_interesting(ctx->revs->repo->index, |
133 | &entry, base, 0, | |
92024891 MD |
134 | &ctx->revs->diffopt.pathspec); |
135 | if (match == all_entries_not_interesting) | |
136 | break; | |
137 | if (match == entry_not_interesting) | |
138 | continue; | |
139 | } | |
140 | ||
99c9aa95 | 141 | if (S_ISDIR(entry.mode)) { |
ea82b2a0 | 142 | struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid); |
b49e74ea TB |
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)); | |
147 | } | |
99c9aa95 MD |
148 | t->object.flags |= NOT_USER_GIVEN; |
149 | process_tree(ctx, t, base, entry.path); | |
150 | } | |
92024891 | 151 | else if (S_ISGITLINK(entry.mode)) |
ea82b2a0 | 152 | process_gitlink(ctx, entry.oid.hash, |
92024891 | 153 | base, entry.path); |
99c9aa95 | 154 | else { |
ea82b2a0 | 155 | struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid); |
23c20445 TB |
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)); | |
160 | } | |
99c9aa95 MD |
161 | b->object.flags |= NOT_USER_GIVEN; |
162 | process_blob(ctx, b, base, entry.path); | |
163 | } | |
92024891 MD |
164 | } |
165 | } | |
166 | ||
f447a499 | 167 | static void process_tree(struct traversal_context *ctx, |
c64ed70d | 168 | struct tree *tree, |
cc5fa2fd | 169 | struct strbuf *base, |
f447a499 | 170 | const char *name) |
c64ed70d JH |
171 | { |
172 | struct object *obj = &tree->object; | |
f447a499 | 173 | struct rev_info *revs = ctx->revs; |
cc5fa2fd | 174 | int baselen = base->len; |
9430147c | 175 | enum list_objects_filter_result r; |
7c0fe330 | 176 | int failed_parse; |
c64ed70d JH |
177 | |
178 | if (!revs->tree_objects) | |
179 | return; | |
a301b0c8 MK |
180 | if (!obj) |
181 | die("bad tree object"); | |
c64ed70d JH |
182 | if (obj->flags & (UNINTERESTING | SEEN)) |
183 | return; | |
aa9ad6fe JK |
184 | if (revs->include_check_obj && |
185 | !revs->include_check_obj(&tree->object, revs->include_check_data)) | |
186 | return; | |
7c0fe330 MD |
187 | |
188 | failed_parse = parse_tree_gently(tree, 1); | |
189 | if (failed_parse) { | |
2db1a43f VM |
190 | if (revs->ignore_missing_links) |
191 | return; | |
df11e196 JT |
192 | |
193 | /* | |
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. | |
197 | */ | |
198 | if (revs->exclude_promisor_objects && | |
199 | is_promisor_object(&obj->oid)) | |
200 | return; | |
201 | ||
7c0fe330 MD |
202 | if (!revs->do_not_die_on_missing_tree) |
203 | die("bad tree object %s", oid_to_hex(&obj->oid)); | |
2db1a43f | 204 | } |
13528ab3 | 205 | |
13528ab3 | 206 | strbuf_addstr(base, name); |
9430147c MD |
207 | r = list_objects_filter__filter_object(ctx->revs->repo, |
208 | LOFS_BEGIN_TREE, obj, | |
209 | base->buf, &base->buf[baselen], | |
210 | ctx->filter); | |
25ec7bca JH |
211 | if (r & LOFR_MARK_SEEN) |
212 | obj->flags |= SEEN; | |
213 | if (r & LOFR_DO_SHOW) | |
4f33a634 | 214 | show_object(ctx, obj, base->buf); |
13528ab3 JK |
215 | if (base->len) |
216 | strbuf_addch(base, '/'); | |
cc5fa2fd | 217 | |
8b10a206 MD |
218 | if (r & LOFR_SKIP_TREE) |
219 | trace_printf("Skipping contents of tree %s...\n", base->buf); | |
220 | else if (!failed_parse) | |
7c0fe330 | 221 | process_tree_contents(ctx, tree, base); |
c64ed70d | 222 | |
9430147c MD |
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) | |
4f33a634 | 230 | show_object(ctx, obj, base->buf); |
25ec7bca | 231 | |
cc5fa2fd | 232 | strbuf_setlen(base, baselen); |
6e454b9a | 233 | free_tree_buffer(tree); |
c64ed70d JH |
234 | } |
235 | ||
628d81be PS |
236 | static void process_tag(struct traversal_context *ctx, |
237 | struct tag *tag, | |
238 | const char *name) | |
239 | { | |
9a2a4f95 PS |
240 | enum list_objects_filter_result r; |
241 | ||
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) | |
4f33a634 | 248 | show_object(ctx, &tag->object, name); |
628d81be PS |
249 | } |
250 | ||
8d1d8f83 JH |
251 | static void mark_edge_parents_uninteresting(struct commit *commit, |
252 | struct rev_info *revs, | |
253 | show_edge_fn show_edge) | |
254 | { | |
255 | struct commit_list *parents; | |
256 | ||
257 | for (parents = commit->parents; parents; parents = parents->next) { | |
258 | struct commit *parent = parents->item; | |
259 | if (!(parent->object.flags & UNINTERESTING)) | |
260 | continue; | |
b3c7eef9 | 261 | mark_tree_uninteresting(revs->repo, get_commit_tree(parent)); |
8d1d8f83 JH |
262 | if (revs->edge_hint && !(parent->object.flags & SHOWN)) { |
263 | parent->object.flags |= SHOWN; | |
264 | show_edge(parent); | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
4f6d26b1 DS |
269 | static void add_edge_parents(struct commit *commit, |
270 | struct rev_info *revs, | |
271 | show_edge_fn show_edge, | |
272 | struct oidset *set) | |
273 | { | |
274 | struct commit_list *parents; | |
275 | ||
276 | for (parents = commit->parents; parents; parents = parents->next) { | |
277 | struct commit *parent = parents->item; | |
278 | struct tree *tree = get_commit_tree(parent); | |
279 | ||
280 | if (!tree) | |
281 | continue; | |
282 | ||
283 | oidset_insert(set, &tree->object.oid); | |
284 | ||
285 | if (!(parent->object.flags & UNINTERESTING)) | |
286 | continue; | |
287 | tree->object.flags |= UNINTERESTING; | |
288 | ||
289 | if (revs->edge_hint && !(parent->object.flags & SHOWN)) { | |
290 | parent->object.flags |= SHOWN; | |
291 | show_edge(parent); | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | void mark_edges_uninteresting(struct rev_info *revs, | |
297 | show_edge_fn show_edge, | |
298 | int sparse) | |
8d1d8f83 | 299 | { |
e76a5fb4 | 300 | struct commit_list *list; |
fbd4a703 NTND |
301 | int i; |
302 | ||
4f6d26b1 DS |
303 | if (sparse) { |
304 | struct oidset set; | |
305 | oidset_init(&set, 16); | |
306 | ||
307 | for (list = revs->commits; list; list = list->next) { | |
308 | struct commit *commit = list->item; | |
309 | struct tree *tree = get_commit_tree(commit); | |
8d1d8f83 | 310 | |
4f6d26b1 DS |
311 | if (commit->object.flags & UNINTERESTING) |
312 | tree->object.flags |= UNINTERESTING; | |
313 | ||
314 | oidset_insert(&set, &tree->object.oid); | |
315 | add_edge_parents(commit, revs, show_edge, &set); | |
316 | } | |
317 | ||
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); | |
329 | } | |
330 | continue; | |
fbd4a703 | 331 | } |
4f6d26b1 | 332 | mark_edge_parents_uninteresting(commit, revs, show_edge); |
8d1d8f83 | 333 | } |
8d1d8f83 | 334 | } |
4f6d26b1 | 335 | |
1684c1b2 | 336 | if (revs->edge_hint_aggressive) { |
200abe74 JK |
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; | |
b3c7eef9 NTND |
342 | mark_tree_uninteresting(revs->repo, |
343 | get_commit_tree(commit)); | |
200abe74 JK |
344 | if (!(obj->flags & SHOWN)) { |
345 | obj->flags |= SHOWN; | |
346 | show_edge(commit); | |
347 | } | |
fbd4a703 NTND |
348 | } |
349 | } | |
8d1d8f83 JH |
350 | } |
351 | ||
8d2dfc49 LT |
352 | static void add_pending_tree(struct rev_info *revs, struct tree *tree) |
353 | { | |
354 | add_pending_object(revs, &tree->object, ""); | |
355 | } | |
356 | ||
b3e36df0 TL |
357 | static void traverse_non_commits(struct traversal_context *ctx, |
358 | struct strbuf *base) | |
c64ed70d JH |
359 | { |
360 | int i; | |
c64ed70d | 361 | |
91904f56 SB |
362 | assert(base->len == 0); |
363 | ||
f447a499 MD |
364 | for (i = 0; i < ctx->revs->pending.nr; i++) { |
365 | struct object_array_entry *pending = ctx->revs->pending.objects + i; | |
c64ed70d JH |
366 | struct object *obj = pending->item; |
367 | const char *name = pending->name; | |
20739490 | 368 | const char *path = pending->path; |
c64ed70d JH |
369 | if (obj->flags & (UNINTERESTING | SEEN)) |
370 | continue; | |
371 | if (obj->type == OBJ_TAG) { | |
628d81be | 372 | process_tag(ctx, (struct tag *)obj, name); |
c64ed70d JH |
373 | continue; |
374 | } | |
20739490 JK |
375 | if (!path) |
376 | path = ""; | |
c64ed70d | 377 | if (obj->type == OBJ_TREE) { |
f447a499 | 378 | process_tree(ctx, (struct tree *)obj, base, path); |
c64ed70d JH |
379 | continue; |
380 | } | |
381 | if (obj->type == OBJ_BLOB) { | |
f447a499 | 382 | process_blob(ctx, (struct blob *)obj, base, path); |
c64ed70d JH |
383 | continue; |
384 | } | |
385 | die("unknown pending object %s (%s)", | |
f2fd0760 | 386 | oid_to_hex(&obj->oid), name); |
c64ed70d | 387 | } |
f447a499 | 388 | object_array_clear(&ctx->revs->pending); |
91904f56 SB |
389 | } |
390 | ||
f447a499 | 391 | static void do_traverse(struct traversal_context *ctx) |
91904f56 SB |
392 | { |
393 | struct commit *commit; | |
394 | struct strbuf csp; /* callee's scratch pad */ | |
395 | strbuf_init(&csp, PATH_MAX); | |
396 | ||
f447a499 | 397 | while ((commit = get_revision(ctx->revs)) != NULL) { |
9a2a4f95 PS |
398 | enum list_objects_filter_result r; |
399 | ||
400 | r = list_objects_filter__filter_object(ctx->revs->repo, | |
401 | LOFS_COMMIT, &commit->object, | |
402 | NULL, NULL, ctx->filter); | |
403 | ||
91904f56 SB |
404 | /* |
405 | * an uninteresting boundary commit may not have its tree | |
406 | * parsed yet, but we are not going to show them anyway | |
407 | */ | |
72ed80c7 JK |
408 | if (!ctx->revs->tree_objects) |
409 | ; /* do not bother loading tree */ | |
410 | else if (get_commit_tree(commit)) { | |
99c9aa95 MD |
411 | struct tree *tree = get_commit_tree(commit); |
412 | tree->object.flags |= NOT_USER_GIVEN; | |
413 | add_pending_tree(ctx->revs, tree); | |
97dd512a JK |
414 | } else if (commit->object.parsed) { |
415 | die(_("unable to load root tree for commit %s"), | |
416 | oid_to_hex(&commit->object.oid)); | |
99c9aa95 | 417 | } |
9a2a4f95 PS |
418 | |
419 | if (r & LOFR_MARK_SEEN) | |
420 | commit->object.flags |= SEEN; | |
421 | if (r & LOFR_DO_SHOW) | |
4f33a634 | 422 | show_commit(ctx, commit); |
ce5b6f9b | 423 | |
f447a499 | 424 | if (ctx->revs->tree_blobs_in_commit_order) |
ce5b6f9b SB |
425 | /* |
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? | |
429 | */ | |
b3e36df0 | 430 | traverse_non_commits(ctx, &csp); |
91904f56 | 431 | } |
b3e36df0 | 432 | traverse_non_commits(ctx, &csp); |
91904f56 | 433 | strbuf_release(&csp); |
c64ed70d | 434 | } |
25ec7bca | 435 | |
25ec7bca | 436 | void traverse_commit_list_filtered( |
25ec7bca JH |
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) | |
442 | { | |
3e0370a8 DS |
443 | struct traversal_context ctx = { |
444 | .revs = revs, | |
445 | .show_object = show_object, | |
446 | .show_commit = show_commit, | |
447 | .show_data = show_data, | |
448 | }; | |
449 | ||
450 | if (revs->filter.choice) | |
451 | ctx.filter = list_objects_filter__init(omitted, &revs->filter); | |
f447a499 | 452 | |
f447a499 | 453 | do_traverse(&ctx); |
3e0370a8 DS |
454 | |
455 | if (ctx.filter) | |
456 | list_objects_filter__free(ctx.filter); | |
25ec7bca | 457 | } |