]>
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 | ||
92024891 | 84 | static void process_tree(struct traversal_context *ctx, |
c64ed70d | 85 | struct tree *tree, |
cc5fa2fd | 86 | struct strbuf *base, |
92024891 MD |
87 | const char *name); |
88 | ||
89 | static void process_tree_contents(struct traversal_context *ctx, | |
90 | struct tree *tree, | |
91 | struct strbuf *base) | |
c64ed70d | 92 | { |
c64ed70d JH |
93 | struct tree_desc desc; |
94 | struct name_entry entry; | |
92024891 MD |
95 | enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ? |
96 | all_entries_interesting : entry_not_interesting; | |
97 | ||
98 | init_tree_desc(&desc, tree->buffer, tree->size); | |
99 | ||
100 | while (tree_entry(&desc, &entry)) { | |
101 | if (match != all_entries_interesting) { | |
67022e02 NTND |
102 | match = tree_entry_interesting(ctx->revs->repo->index, |
103 | &entry, base, 0, | |
92024891 MD |
104 | &ctx->revs->diffopt.pathspec); |
105 | if (match == all_entries_not_interesting) | |
106 | break; | |
107 | if (match == entry_not_interesting) | |
108 | continue; | |
109 | } | |
110 | ||
99c9aa95 | 111 | if (S_ISDIR(entry.mode)) { |
ea82b2a0 | 112 | struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid); |
b49e74ea TB |
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)); | |
117 | } | |
99c9aa95 MD |
118 | t->object.flags |= NOT_USER_GIVEN; |
119 | process_tree(ctx, t, base, entry.path); | |
120 | } | |
92024891 | 121 | else if (S_ISGITLINK(entry.mode)) |
00271485 | 122 | ; /* ignore gitlink */ |
99c9aa95 | 123 | else { |
ea82b2a0 | 124 | struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid); |
23c20445 TB |
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)); | |
129 | } | |
99c9aa95 MD |
130 | b->object.flags |= NOT_USER_GIVEN; |
131 | process_blob(ctx, b, base, entry.path); | |
132 | } | |
92024891 MD |
133 | } |
134 | } | |
135 | ||
f447a499 | 136 | static void process_tree(struct traversal_context *ctx, |
c64ed70d | 137 | struct tree *tree, |
cc5fa2fd | 138 | struct strbuf *base, |
f447a499 | 139 | const char *name) |
c64ed70d JH |
140 | { |
141 | struct object *obj = &tree->object; | |
f447a499 | 142 | struct rev_info *revs = ctx->revs; |
cc5fa2fd | 143 | int baselen = base->len; |
9430147c | 144 | enum list_objects_filter_result r; |
7c0fe330 | 145 | int failed_parse; |
c64ed70d JH |
146 | |
147 | if (!revs->tree_objects) | |
148 | return; | |
a301b0c8 MK |
149 | if (!obj) |
150 | die("bad tree object"); | |
c64ed70d JH |
151 | if (obj->flags & (UNINTERESTING | SEEN)) |
152 | return; | |
aa9ad6fe JK |
153 | if (revs->include_check_obj && |
154 | !revs->include_check_obj(&tree->object, revs->include_check_data)) | |
155 | return; | |
7c0fe330 MD |
156 | |
157 | failed_parse = parse_tree_gently(tree, 1); | |
158 | if (failed_parse) { | |
2db1a43f VM |
159 | if (revs->ignore_missing_links) |
160 | return; | |
df11e196 JT |
161 | |
162 | /* | |
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. | |
166 | */ | |
167 | if (revs->exclude_promisor_objects && | |
168 | is_promisor_object(&obj->oid)) | |
169 | return; | |
170 | ||
7c0fe330 MD |
171 | if (!revs->do_not_die_on_missing_tree) |
172 | die("bad tree object %s", oid_to_hex(&obj->oid)); | |
2db1a43f | 173 | } |
13528ab3 | 174 | |
13528ab3 | 175 | strbuf_addstr(base, name); |
9430147c MD |
176 | r = list_objects_filter__filter_object(ctx->revs->repo, |
177 | LOFS_BEGIN_TREE, obj, | |
178 | base->buf, &base->buf[baselen], | |
179 | ctx->filter); | |
25ec7bca JH |
180 | if (r & LOFR_MARK_SEEN) |
181 | obj->flags |= SEEN; | |
182 | if (r & LOFR_DO_SHOW) | |
4f33a634 | 183 | show_object(ctx, obj, base->buf); |
13528ab3 JK |
184 | if (base->len) |
185 | strbuf_addch(base, '/'); | |
cc5fa2fd | 186 | |
8b10a206 MD |
187 | if (r & LOFR_SKIP_TREE) |
188 | trace_printf("Skipping contents of tree %s...\n", base->buf); | |
189 | else if (!failed_parse) | |
7c0fe330 | 190 | process_tree_contents(ctx, tree, base); |
c64ed70d | 191 | |
9430147c MD |
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) | |
4f33a634 | 199 | show_object(ctx, obj, base->buf); |
25ec7bca | 200 | |
cc5fa2fd | 201 | strbuf_setlen(base, baselen); |
6e454b9a | 202 | free_tree_buffer(tree); |
c64ed70d JH |
203 | } |
204 | ||
628d81be PS |
205 | static void process_tag(struct traversal_context *ctx, |
206 | struct tag *tag, | |
207 | const char *name) | |
208 | { | |
9a2a4f95 PS |
209 | enum list_objects_filter_result r; |
210 | ||
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) | |
4f33a634 | 217 | show_object(ctx, &tag->object, name); |
628d81be PS |
218 | } |
219 | ||
8d1d8f83 JH |
220 | static void mark_edge_parents_uninteresting(struct commit *commit, |
221 | struct rev_info *revs, | |
222 | show_edge_fn show_edge) | |
223 | { | |
224 | struct commit_list *parents; | |
225 | ||
226 | for (parents = commit->parents; parents; parents = parents->next) { | |
227 | struct commit *parent = parents->item; | |
228 | if (!(parent->object.flags & UNINTERESTING)) | |
229 | continue; | |
b3c7eef9 | 230 | mark_tree_uninteresting(revs->repo, get_commit_tree(parent)); |
8d1d8f83 JH |
231 | if (revs->edge_hint && !(parent->object.flags & SHOWN)) { |
232 | parent->object.flags |= SHOWN; | |
233 | show_edge(parent); | |
234 | } | |
235 | } | |
236 | } | |
237 | ||
4f6d26b1 DS |
238 | static void add_edge_parents(struct commit *commit, |
239 | struct rev_info *revs, | |
240 | show_edge_fn show_edge, | |
241 | struct oidset *set) | |
242 | { | |
243 | struct commit_list *parents; | |
244 | ||
245 | for (parents = commit->parents; parents; parents = parents->next) { | |
246 | struct commit *parent = parents->item; | |
247 | struct tree *tree = get_commit_tree(parent); | |
248 | ||
249 | if (!tree) | |
250 | continue; | |
251 | ||
252 | oidset_insert(set, &tree->object.oid); | |
253 | ||
254 | if (!(parent->object.flags & UNINTERESTING)) | |
255 | continue; | |
256 | tree->object.flags |= UNINTERESTING; | |
257 | ||
258 | if (revs->edge_hint && !(parent->object.flags & SHOWN)) { | |
259 | parent->object.flags |= SHOWN; | |
260 | show_edge(parent); | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | void mark_edges_uninteresting(struct rev_info *revs, | |
266 | show_edge_fn show_edge, | |
267 | int sparse) | |
8d1d8f83 | 268 | { |
e76a5fb4 | 269 | struct commit_list *list; |
fbd4a703 NTND |
270 | int i; |
271 | ||
4f6d26b1 DS |
272 | if (sparse) { |
273 | struct oidset set; | |
274 | oidset_init(&set, 16); | |
275 | ||
276 | for (list = revs->commits; list; list = list->next) { | |
277 | struct commit *commit = list->item; | |
278 | struct tree *tree = get_commit_tree(commit); | |
8d1d8f83 | 279 | |
4f6d26b1 DS |
280 | if (commit->object.flags & UNINTERESTING) |
281 | tree->object.flags |= UNINTERESTING; | |
282 | ||
283 | oidset_insert(&set, &tree->object.oid); | |
284 | add_edge_parents(commit, revs, show_edge, &set); | |
285 | } | |
286 | ||
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); | |
298 | } | |
299 | continue; | |
fbd4a703 | 300 | } |
4f6d26b1 | 301 | mark_edge_parents_uninteresting(commit, revs, show_edge); |
8d1d8f83 | 302 | } |
8d1d8f83 | 303 | } |
4f6d26b1 | 304 | |
1684c1b2 | 305 | if (revs->edge_hint_aggressive) { |
200abe74 JK |
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; | |
b3c7eef9 NTND |
311 | mark_tree_uninteresting(revs->repo, |
312 | get_commit_tree(commit)); | |
200abe74 JK |
313 | if (!(obj->flags & SHOWN)) { |
314 | obj->flags |= SHOWN; | |
315 | show_edge(commit); | |
316 | } | |
fbd4a703 NTND |
317 | } |
318 | } | |
8d1d8f83 JH |
319 | } |
320 | ||
8d2dfc49 LT |
321 | static void add_pending_tree(struct rev_info *revs, struct tree *tree) |
322 | { | |
323 | add_pending_object(revs, &tree->object, ""); | |
324 | } | |
325 | ||
b3e36df0 TL |
326 | static void traverse_non_commits(struct traversal_context *ctx, |
327 | struct strbuf *base) | |
c64ed70d JH |
328 | { |
329 | int i; | |
c64ed70d | 330 | |
91904f56 SB |
331 | assert(base->len == 0); |
332 | ||
f447a499 MD |
333 | for (i = 0; i < ctx->revs->pending.nr; i++) { |
334 | struct object_array_entry *pending = ctx->revs->pending.objects + i; | |
c64ed70d JH |
335 | struct object *obj = pending->item; |
336 | const char *name = pending->name; | |
20739490 | 337 | const char *path = pending->path; |
c64ed70d JH |
338 | if (obj->flags & (UNINTERESTING | SEEN)) |
339 | continue; | |
340 | if (obj->type == OBJ_TAG) { | |
628d81be | 341 | process_tag(ctx, (struct tag *)obj, name); |
c64ed70d JH |
342 | continue; |
343 | } | |
20739490 JK |
344 | if (!path) |
345 | path = ""; | |
c64ed70d | 346 | if (obj->type == OBJ_TREE) { |
f447a499 | 347 | process_tree(ctx, (struct tree *)obj, base, path); |
c64ed70d JH |
348 | continue; |
349 | } | |
350 | if (obj->type == OBJ_BLOB) { | |
f447a499 | 351 | process_blob(ctx, (struct blob *)obj, base, path); |
c64ed70d JH |
352 | continue; |
353 | } | |
354 | die("unknown pending object %s (%s)", | |
f2fd0760 | 355 | oid_to_hex(&obj->oid), name); |
c64ed70d | 356 | } |
f447a499 | 357 | object_array_clear(&ctx->revs->pending); |
91904f56 SB |
358 | } |
359 | ||
f447a499 | 360 | static void do_traverse(struct traversal_context *ctx) |
91904f56 SB |
361 | { |
362 | struct commit *commit; | |
363 | struct strbuf csp; /* callee's scratch pad */ | |
364 | strbuf_init(&csp, PATH_MAX); | |
365 | ||
f447a499 | 366 | while ((commit = get_revision(ctx->revs)) != NULL) { |
9a2a4f95 PS |
367 | enum list_objects_filter_result r; |
368 | ||
369 | r = list_objects_filter__filter_object(ctx->revs->repo, | |
370 | LOFS_COMMIT, &commit->object, | |
371 | NULL, NULL, ctx->filter); | |
372 | ||
91904f56 SB |
373 | /* |
374 | * an uninteresting boundary commit may not have its tree | |
375 | * parsed yet, but we are not going to show them anyway | |
376 | */ | |
72ed80c7 JK |
377 | if (!ctx->revs->tree_objects) |
378 | ; /* do not bother loading tree */ | |
379 | else if (get_commit_tree(commit)) { | |
99c9aa95 MD |
380 | struct tree *tree = get_commit_tree(commit); |
381 | tree->object.flags |= NOT_USER_GIVEN; | |
382 | add_pending_tree(ctx->revs, tree); | |
97dd512a JK |
383 | } else if (commit->object.parsed) { |
384 | die(_("unable to load root tree for commit %s"), | |
385 | oid_to_hex(&commit->object.oid)); | |
99c9aa95 | 386 | } |
9a2a4f95 PS |
387 | |
388 | if (r & LOFR_MARK_SEEN) | |
389 | commit->object.flags |= SEEN; | |
390 | if (r & LOFR_DO_SHOW) | |
4f33a634 | 391 | show_commit(ctx, commit); |
ce5b6f9b | 392 | |
f447a499 | 393 | if (ctx->revs->tree_blobs_in_commit_order) |
ce5b6f9b SB |
394 | /* |
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? | |
398 | */ | |
b3e36df0 | 399 | traverse_non_commits(ctx, &csp); |
91904f56 | 400 | } |
b3e36df0 | 401 | traverse_non_commits(ctx, &csp); |
91904f56 | 402 | strbuf_release(&csp); |
c64ed70d | 403 | } |
25ec7bca | 404 | |
25ec7bca | 405 | void traverse_commit_list_filtered( |
25ec7bca JH |
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) | |
411 | { | |
3e0370a8 DS |
412 | struct traversal_context ctx = { |
413 | .revs = revs, | |
414 | .show_object = show_object, | |
415 | .show_commit = show_commit, | |
416 | .show_data = show_data, | |
417 | }; | |
418 | ||
419 | if (revs->filter.choice) | |
420 | ctx.filter = list_objects_filter__init(omitted, &revs->filter); | |
f447a499 | 421 | |
f447a499 | 422 | do_traverse(&ctx); |
3e0370a8 DS |
423 | |
424 | if (ctx.filter) | |
425 | list_objects_filter__free(ctx.filter); | |
25ec7bca | 426 | } |