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 ctx
->show_object(object
, name
, ctx
->show_data
);
45 static void process_blob(struct traversal_context
*ctx
,
50 struct object
*obj
= &blob
->object
;
52 enum list_objects_filter_result r
;
54 if (!ctx
->revs
->blob_objects
)
57 die("bad blob object");
58 if (obj
->flags
& (UNINTERESTING
| SEEN
))
62 * Pre-filter known-missing objects when explicitly requested.
63 * Otherwise, a missing object error message may be reported
64 * later (depending on other filtering criteria).
66 * Note that this "--exclude-promisor-objects" pre-filtering
67 * may cause the actual filter to report an incomplete list
70 if (ctx
->revs
->exclude_promisor_objects
&&
71 !repo_has_object_file(the_repository
, &obj
->oid
) &&
72 is_promisor_object(&obj
->oid
))
76 strbuf_addstr(path
, name
);
77 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
79 path
->buf
, &path
->buf
[pathlen
],
81 if (r
& LOFR_MARK_SEEN
)
84 show_object(ctx
, obj
, path
->buf
);
85 strbuf_setlen(path
, pathlen
);
88 static void process_tree(struct traversal_context
*ctx
,
93 static void process_tree_contents(struct traversal_context
*ctx
,
97 struct tree_desc desc
;
98 struct name_entry entry
;
99 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
100 all_entries_interesting
: entry_not_interesting
;
102 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
104 while (tree_entry(&desc
, &entry
)) {
105 if (match
!= all_entries_interesting
) {
106 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
108 &ctx
->revs
->diffopt
.pathspec
);
109 if (match
== all_entries_not_interesting
)
111 if (match
== entry_not_interesting
)
115 if (S_ISDIR(entry
.mode
)) {
116 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
118 die(_("entry '%s' in tree %s has tree mode, "
119 "but is not a tree"),
120 entry
.path
, oid_to_hex(&tree
->object
.oid
));
122 t
->object
.flags
|= NOT_USER_GIVEN
;
124 process_tree(ctx
, t
, base
, entry
.path
);
127 else if (S_ISGITLINK(entry
.mode
))
128 ; /* ignore gitlink */
130 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
132 die(_("entry '%s' in tree %s has blob mode, "
133 "but is not a blob"),
134 entry
.path
, oid_to_hex(&tree
->object
.oid
));
136 b
->object
.flags
|= NOT_USER_GIVEN
;
137 process_blob(ctx
, b
, base
, entry
.path
);
142 static void process_tree(struct traversal_context
*ctx
,
147 struct object
*obj
= &tree
->object
;
148 struct rev_info
*revs
= ctx
->revs
;
149 int baselen
= base
->len
;
150 enum list_objects_filter_result r
;
153 if (!revs
->tree_objects
)
156 die("bad tree object");
157 if (obj
->flags
& (UNINTERESTING
| SEEN
))
159 if (revs
->include_check_obj
&&
160 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
163 if (ctx
->depth
> max_allowed_tree_depth
)
164 die("exceeded maximum allowed tree depth");
166 failed_parse
= parse_tree_gently(tree
, 1);
168 if (revs
->ignore_missing_links
)
172 * Pre-filter known-missing tree objects when explicitly
173 * requested. This may cause the actual filter to report
174 * an incomplete list of missing objects.
176 if (revs
->exclude_promisor_objects
&&
177 is_promisor_object(&obj
->oid
))
180 if (!revs
->do_not_die_on_missing_tree
)
181 die("bad tree object %s", oid_to_hex(&obj
->oid
));
184 strbuf_addstr(base
, name
);
185 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
186 LOFS_BEGIN_TREE
, obj
,
187 base
->buf
, &base
->buf
[baselen
],
189 if (r
& LOFR_MARK_SEEN
)
191 if (r
& LOFR_DO_SHOW
)
192 show_object(ctx
, obj
, base
->buf
);
194 strbuf_addch(base
, '/');
196 if (r
& LOFR_SKIP_TREE
)
197 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
198 else if (!failed_parse
)
199 process_tree_contents(ctx
, tree
, base
);
201 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
203 base
->buf
, &base
->buf
[baselen
],
205 if (r
& LOFR_MARK_SEEN
)
207 if (r
& LOFR_DO_SHOW
)
208 show_object(ctx
, obj
, base
->buf
);
210 strbuf_setlen(base
, baselen
);
211 free_tree_buffer(tree
);
214 static void process_tag(struct traversal_context
*ctx
,
218 enum list_objects_filter_result r
;
220 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
221 &tag
->object
, NULL
, NULL
,
223 if (r
& LOFR_MARK_SEEN
)
224 tag
->object
.flags
|= SEEN
;
225 if (r
& LOFR_DO_SHOW
)
226 show_object(ctx
, &tag
->object
, name
);
229 static void mark_edge_parents_uninteresting(struct commit
*commit
,
230 struct rev_info
*revs
,
231 show_edge_fn show_edge
)
233 struct commit_list
*parents
;
235 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
236 struct commit
*parent
= parents
->item
;
237 if (!(parent
->object
.flags
& UNINTERESTING
))
239 mark_tree_uninteresting(revs
->repo
,
240 repo_get_commit_tree(the_repository
, parent
));
241 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
242 parent
->object
.flags
|= SHOWN
;
248 static void add_edge_parents(struct commit
*commit
,
249 struct rev_info
*revs
,
250 show_edge_fn show_edge
,
253 struct commit_list
*parents
;
255 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
256 struct commit
*parent
= parents
->item
;
257 struct tree
*tree
= repo_get_commit_tree(the_repository
,
263 oidset_insert(set
, &tree
->object
.oid
);
265 if (!(parent
->object
.flags
& UNINTERESTING
))
267 tree
->object
.flags
|= UNINTERESTING
;
269 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
270 parent
->object
.flags
|= SHOWN
;
276 void mark_edges_uninteresting(struct rev_info
*revs
,
277 show_edge_fn show_edge
,
280 struct commit_list
*list
;
285 oidset_init(&set
, 16);
287 for (list
= revs
->commits
; list
; list
= list
->next
) {
288 struct commit
*commit
= list
->item
;
289 struct tree
*tree
= repo_get_commit_tree(the_repository
,
292 if (commit
->object
.flags
& UNINTERESTING
)
293 tree
->object
.flags
|= UNINTERESTING
;
295 oidset_insert(&set
, &tree
->object
.oid
);
296 add_edge_parents(commit
, revs
, show_edge
, &set
);
299 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
302 for (list
= revs
->commits
; list
; list
= list
->next
) {
303 struct commit
*commit
= list
->item
;
304 if (commit
->object
.flags
& UNINTERESTING
) {
305 mark_tree_uninteresting(revs
->repo
,
306 repo_get_commit_tree(the_repository
, commit
));
307 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
308 commit
->object
.flags
|= SHOWN
;
313 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
317 if (revs
->edge_hint_aggressive
) {
318 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
319 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
320 struct commit
*commit
= (struct commit
*)obj
;
321 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
323 mark_tree_uninteresting(revs
->repo
,
324 repo_get_commit_tree(the_repository
, commit
));
325 if (!(obj
->flags
& SHOWN
)) {
333 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
335 add_pending_object(revs
, &tree
->object
, "");
338 static void traverse_non_commits(struct traversal_context
*ctx
,
343 assert(base
->len
== 0);
345 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
346 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
347 struct object
*obj
= pending
->item
;
348 const char *name
= pending
->name
;
349 const char *path
= pending
->path
;
350 if (obj
->flags
& (UNINTERESTING
| SEEN
))
352 if (obj
->type
== OBJ_TAG
) {
353 process_tag(ctx
, (struct tag
*)obj
, name
);
358 if (obj
->type
== OBJ_TREE
) {
360 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
363 if (obj
->type
== OBJ_BLOB
) {
364 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
367 die("unknown pending object %s (%s)",
368 oid_to_hex(&obj
->oid
), name
);
370 object_array_clear(&ctx
->revs
->pending
);
373 static void do_traverse(struct traversal_context
*ctx
)
375 struct commit
*commit
;
376 struct strbuf csp
; /* callee's scratch pad */
377 strbuf_init(&csp
, PATH_MAX
);
379 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
380 enum list_objects_filter_result r
;
382 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
383 LOFS_COMMIT
, &commit
->object
,
384 NULL
, NULL
, ctx
->filter
);
387 * an uninteresting boundary commit may not have its tree
388 * parsed yet, but we are not going to show them anyway
390 if (!ctx
->revs
->tree_objects
)
391 ; /* do not bother loading tree */
392 else if (repo_get_commit_tree(the_repository
, commit
)) {
393 struct tree
*tree
= repo_get_commit_tree(the_repository
,
395 tree
->object
.flags
|= NOT_USER_GIVEN
;
396 add_pending_tree(ctx
->revs
, tree
);
397 } else if (commit
->object
.parsed
) {
398 die(_("unable to load root tree for commit %s"),
399 oid_to_hex(&commit
->object
.oid
));
402 if (r
& LOFR_MARK_SEEN
)
403 commit
->object
.flags
|= SEEN
;
404 if (r
& LOFR_DO_SHOW
)
405 show_commit(ctx
, commit
);
407 if (ctx
->revs
->tree_blobs_in_commit_order
)
409 * NEEDSWORK: Adding the tree and then flushing it here
410 * needs a reallocation for each commit. Can we pass the
411 * tree directory without allocation churn?
413 traverse_non_commits(ctx
, &csp
);
415 traverse_non_commits(ctx
, &csp
);
416 strbuf_release(&csp
);
419 void traverse_commit_list_filtered(
420 struct rev_info
*revs
,
421 show_commit_fn show_commit
,
422 show_object_fn show_object
,
424 struct oidset
*omitted
)
426 struct traversal_context ctx
= {
428 .show_object
= show_object
,
429 .show_commit
= show_commit
,
430 .show_data
= show_data
,
433 if (revs
->filter
.choice
)
434 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
439 list_objects_filter__free(ctx
.filter
);