1 #include "git-compat-util.h"
11 #include "list-objects.h"
12 #include "list-objects-filter.h"
13 #include "list-objects-filter-options.h"
15 #include "object-store-ll.h"
17 #include "environment.h"
19 struct traversal_context
{
20 struct rev_info
*revs
;
21 show_object_fn show_object
;
22 show_commit_fn show_commit
;
24 struct filter
*filter
;
28 static void show_commit(struct traversal_context
*ctx
,
29 struct commit
*commit
)
31 if (!ctx
->show_commit
)
33 ctx
->show_commit(commit
, ctx
->show_data
);
36 static void show_object(struct traversal_context
*ctx
,
37 struct object
*object
,
40 if (!ctx
->show_object
)
42 if (ctx
->revs
->unpacked
&& has_object_pack(&object
->oid
))
45 ctx
->show_object(object
, name
, ctx
->show_data
);
48 static void process_blob(struct traversal_context
*ctx
,
53 struct object
*obj
= &blob
->object
;
55 enum list_objects_filter_result r
;
57 if (!ctx
->revs
->blob_objects
)
60 die("bad blob object");
61 if (obj
->flags
& (UNINTERESTING
| SEEN
))
65 * Pre-filter known-missing objects when explicitly requested.
66 * Otherwise, a missing object error message may be reported
67 * later (depending on other filtering criteria).
69 * Note that this "--exclude-promisor-objects" pre-filtering
70 * may cause the actual filter to report an incomplete list
73 if (ctx
->revs
->exclude_promisor_objects
&&
74 !repo_has_object_file(the_repository
, &obj
->oid
) &&
75 is_promisor_object(&obj
->oid
))
79 strbuf_addstr(path
, name
);
80 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
82 path
->buf
, &path
->buf
[pathlen
],
84 if (r
& LOFR_MARK_SEEN
)
87 show_object(ctx
, obj
, path
->buf
);
88 strbuf_setlen(path
, pathlen
);
91 static void process_tree(struct traversal_context
*ctx
,
96 static void process_tree_contents(struct traversal_context
*ctx
,
100 struct tree_desc desc
;
101 struct name_entry entry
;
102 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
103 all_entries_interesting
: entry_not_interesting
;
105 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
107 while (tree_entry(&desc
, &entry
)) {
108 if (match
!= all_entries_interesting
) {
109 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
111 &ctx
->revs
->diffopt
.pathspec
);
112 if (match
== all_entries_not_interesting
)
114 if (match
== entry_not_interesting
)
118 if (S_ISDIR(entry
.mode
)) {
119 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
121 die(_("entry '%s' in tree %s has tree mode, "
122 "but is not a tree"),
123 entry
.path
, oid_to_hex(&tree
->object
.oid
));
125 t
->object
.flags
|= NOT_USER_GIVEN
;
127 process_tree(ctx
, t
, base
, entry
.path
);
130 else if (S_ISGITLINK(entry
.mode
))
131 ; /* ignore gitlink */
133 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
135 die(_("entry '%s' in tree %s has blob mode, "
136 "but is not a blob"),
137 entry
.path
, oid_to_hex(&tree
->object
.oid
));
139 b
->object
.flags
|= NOT_USER_GIVEN
;
140 process_blob(ctx
, b
, base
, entry
.path
);
145 static void process_tree(struct traversal_context
*ctx
,
150 struct object
*obj
= &tree
->object
;
151 struct rev_info
*revs
= ctx
->revs
;
152 int baselen
= base
->len
;
153 enum list_objects_filter_result r
;
156 if (!revs
->tree_objects
)
159 die("bad tree object");
160 if (obj
->flags
& (UNINTERESTING
| SEEN
))
162 if (revs
->include_check_obj
&&
163 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
166 if (ctx
->depth
> max_allowed_tree_depth
)
167 die("exceeded maximum allowed tree depth");
169 failed_parse
= parse_tree_gently(tree
, 1);
171 if (revs
->ignore_missing_links
)
175 * Pre-filter known-missing tree objects when explicitly
176 * requested. This may cause the actual filter to report
177 * an incomplete list of missing objects.
179 if (revs
->exclude_promisor_objects
&&
180 is_promisor_object(&obj
->oid
))
183 if (!revs
->do_not_die_on_missing_objects
)
184 die("bad tree object %s", oid_to_hex(&obj
->oid
));
187 strbuf_addstr(base
, name
);
188 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
189 LOFS_BEGIN_TREE
, obj
,
190 base
->buf
, &base
->buf
[baselen
],
192 if (r
& LOFR_MARK_SEEN
)
194 if (r
& LOFR_DO_SHOW
)
195 show_object(ctx
, obj
, base
->buf
);
197 strbuf_addch(base
, '/');
199 if (r
& LOFR_SKIP_TREE
)
200 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
201 else if (!failed_parse
)
202 process_tree_contents(ctx
, tree
, base
);
204 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
206 base
->buf
, &base
->buf
[baselen
],
208 if (r
& LOFR_MARK_SEEN
)
210 if (r
& LOFR_DO_SHOW
)
211 show_object(ctx
, obj
, base
->buf
);
213 strbuf_setlen(base
, baselen
);
214 free_tree_buffer(tree
);
217 static void process_tag(struct traversal_context
*ctx
,
221 enum list_objects_filter_result r
;
223 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
224 &tag
->object
, NULL
, NULL
,
226 if (r
& LOFR_MARK_SEEN
)
227 tag
->object
.flags
|= SEEN
;
228 if (r
& LOFR_DO_SHOW
)
229 show_object(ctx
, &tag
->object
, name
);
232 static void mark_edge_parents_uninteresting(struct commit
*commit
,
233 struct rev_info
*revs
,
234 show_edge_fn show_edge
)
236 struct commit_list
*parents
;
238 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
239 struct commit
*parent
= parents
->item
;
240 if (!(parent
->object
.flags
& UNINTERESTING
))
242 mark_tree_uninteresting(revs
->repo
,
243 repo_get_commit_tree(the_repository
, parent
));
244 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
245 parent
->object
.flags
|= SHOWN
;
251 static void add_edge_parents(struct commit
*commit
,
252 struct rev_info
*revs
,
253 show_edge_fn show_edge
,
256 struct commit_list
*parents
;
258 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
259 struct commit
*parent
= parents
->item
;
260 struct tree
*tree
= repo_get_commit_tree(the_repository
,
266 oidset_insert(set
, &tree
->object
.oid
);
268 if (!(parent
->object
.flags
& UNINTERESTING
))
270 tree
->object
.flags
|= UNINTERESTING
;
272 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
273 parent
->object
.flags
|= SHOWN
;
279 void mark_edges_uninteresting(struct rev_info
*revs
,
280 show_edge_fn show_edge
,
283 struct commit_list
*list
;
288 oidset_init(&set
, 16);
290 for (list
= revs
->commits
; list
; list
= list
->next
) {
291 struct commit
*commit
= list
->item
;
292 struct tree
*tree
= repo_get_commit_tree(the_repository
,
295 if (commit
->object
.flags
& UNINTERESTING
)
296 tree
->object
.flags
|= UNINTERESTING
;
298 oidset_insert(&set
, &tree
->object
.oid
);
299 add_edge_parents(commit
, revs
, show_edge
, &set
);
302 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
305 for (list
= revs
->commits
; list
; list
= list
->next
) {
306 struct commit
*commit
= list
->item
;
307 if (commit
->object
.flags
& UNINTERESTING
) {
308 mark_tree_uninteresting(revs
->repo
,
309 repo_get_commit_tree(the_repository
, commit
));
310 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
311 commit
->object
.flags
|= SHOWN
;
316 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
320 if (revs
->edge_hint_aggressive
) {
321 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
322 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
323 struct commit
*commit
= (struct commit
*)obj
;
324 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
326 mark_tree_uninteresting(revs
->repo
,
327 repo_get_commit_tree(the_repository
, commit
));
328 if (!(obj
->flags
& SHOWN
)) {
336 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
338 add_pending_object(revs
, &tree
->object
, "");
341 static void traverse_non_commits(struct traversal_context
*ctx
,
346 assert(base
->len
== 0);
348 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
349 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
350 struct object
*obj
= pending
->item
;
351 const char *name
= pending
->name
;
352 const char *path
= pending
->path
;
353 if (obj
->flags
& (UNINTERESTING
| SEEN
))
355 if (obj
->type
== OBJ_TAG
) {
356 process_tag(ctx
, (struct tag
*)obj
, name
);
361 if (obj
->type
== OBJ_TREE
) {
363 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
366 if (obj
->type
== OBJ_BLOB
) {
367 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
370 die("unknown pending object %s (%s)",
371 oid_to_hex(&obj
->oid
), name
);
373 object_array_clear(&ctx
->revs
->pending
);
376 static void do_traverse(struct traversal_context
*ctx
)
378 struct commit
*commit
;
379 struct strbuf csp
; /* callee's scratch pad */
380 strbuf_init(&csp
, PATH_MAX
);
382 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
383 enum list_objects_filter_result r
;
385 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
386 LOFS_COMMIT
, &commit
->object
,
387 NULL
, NULL
, ctx
->filter
);
390 * an uninteresting boundary commit may not have its tree
391 * parsed yet, but we are not going to show them anyway
393 if (!ctx
->revs
->tree_objects
)
394 ; /* do not bother loading tree */
395 else if (ctx
->revs
->do_not_die_on_missing_objects
&&
396 oidset_contains(&ctx
->revs
->missing_commits
, &commit
->object
.oid
))
398 else if (repo_get_commit_tree(the_repository
, commit
)) {
399 struct tree
*tree
= repo_get_commit_tree(the_repository
,
401 tree
->object
.flags
|= NOT_USER_GIVEN
;
402 add_pending_tree(ctx
->revs
, tree
);
403 } else if (commit
->object
.parsed
) {
404 die(_("unable to load root tree for commit %s"),
405 oid_to_hex(&commit
->object
.oid
));
408 if (r
& LOFR_MARK_SEEN
)
409 commit
->object
.flags
|= SEEN
;
410 if (r
& LOFR_DO_SHOW
)
411 show_commit(ctx
, commit
);
413 if (ctx
->revs
->tree_blobs_in_commit_order
)
415 * NEEDSWORK: Adding the tree and then flushing it here
416 * needs a reallocation for each commit. Can we pass the
417 * tree directory without allocation churn?
419 traverse_non_commits(ctx
, &csp
);
421 traverse_non_commits(ctx
, &csp
);
422 strbuf_release(&csp
);
425 void traverse_commit_list_filtered(
426 struct rev_info
*revs
,
427 show_commit_fn show_commit
,
428 show_object_fn show_object
,
430 struct oidset
*omitted
)
432 struct traversal_context ctx
= {
434 .show_object
= show_object
,
435 .show_commit
= show_commit
,
436 .show_data
= show_data
,
439 if (revs
->filter
.choice
)
440 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
445 list_objects_filter__free(ctx
.filter
);