10 #include "list-objects.h"
11 #include "list-objects-filter.h"
12 #include "list-objects-filter-options.h"
14 #include "object-store.h"
17 struct traversal_context
{
18 struct rev_info
*revs
;
19 show_object_fn show_object
;
20 show_commit_fn show_commit
;
22 struct filter
*filter
;
25 static void show_commit(struct traversal_context
*ctx
,
26 struct commit
*commit
)
28 if (!ctx
->show_commit
)
30 ctx
->show_commit(commit
, ctx
->show_data
);
33 static void show_object(struct traversal_context
*ctx
,
34 struct object
*object
,
37 if (!ctx
->show_object
)
39 ctx
->show_object(object
, name
, ctx
->show_data
);
42 static void process_blob(struct traversal_context
*ctx
,
47 struct object
*obj
= &blob
->object
;
49 enum list_objects_filter_result r
;
51 if (!ctx
->revs
->blob_objects
)
54 die("bad blob object");
55 if (obj
->flags
& (UNINTERESTING
| SEEN
))
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).
63 * Note that this "--exclude-promisor-objects" pre-filtering
64 * may cause the actual filter to report an incomplete list
67 if (ctx
->revs
->exclude_promisor_objects
&&
68 !repo_has_object_file(the_repository
, &obj
->oid
) &&
69 is_promisor_object(&obj
->oid
))
73 strbuf_addstr(path
, name
);
74 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
76 path
->buf
, &path
->buf
[pathlen
],
78 if (r
& LOFR_MARK_SEEN
)
81 show_object(ctx
, obj
, path
->buf
);
82 strbuf_setlen(path
, pathlen
);
85 static void process_tree(struct traversal_context
*ctx
,
90 static void process_tree_contents(struct traversal_context
*ctx
,
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
;
99 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
101 while (tree_entry(&desc
, &entry
)) {
102 if (match
!= all_entries_interesting
) {
103 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
105 &ctx
->revs
->diffopt
.pathspec
);
106 if (match
== all_entries_not_interesting
)
108 if (match
== entry_not_interesting
)
112 if (S_ISDIR(entry
.mode
)) {
113 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
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
));
119 t
->object
.flags
|= NOT_USER_GIVEN
;
120 process_tree(ctx
, t
, base
, entry
.path
);
122 else if (S_ISGITLINK(entry
.mode
))
123 ; /* ignore gitlink */
125 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
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
));
131 b
->object
.flags
|= NOT_USER_GIVEN
;
132 process_blob(ctx
, b
, base
, entry
.path
);
137 static void process_tree(struct traversal_context
*ctx
,
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
;
148 if (!revs
->tree_objects
)
151 die("bad tree object");
152 if (obj
->flags
& (UNINTERESTING
| SEEN
))
154 if (revs
->include_check_obj
&&
155 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
158 failed_parse
= parse_tree_gently(tree
, 1);
160 if (revs
->ignore_missing_links
)
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.
168 if (revs
->exclude_promisor_objects
&&
169 is_promisor_object(&obj
->oid
))
172 if (!revs
->do_not_die_on_missing_tree
)
173 die("bad tree object %s", oid_to_hex(&obj
->oid
));
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
],
181 if (r
& LOFR_MARK_SEEN
)
183 if (r
& LOFR_DO_SHOW
)
184 show_object(ctx
, obj
, base
->buf
);
186 strbuf_addch(base
, '/');
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
);
193 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
195 base
->buf
, &base
->buf
[baselen
],
197 if (r
& LOFR_MARK_SEEN
)
199 if (r
& LOFR_DO_SHOW
)
200 show_object(ctx
, obj
, base
->buf
);
202 strbuf_setlen(base
, baselen
);
203 free_tree_buffer(tree
);
206 static void process_tag(struct traversal_context
*ctx
,
210 enum list_objects_filter_result r
;
212 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
213 &tag
->object
, NULL
, NULL
,
215 if (r
& LOFR_MARK_SEEN
)
216 tag
->object
.flags
|= SEEN
;
217 if (r
& LOFR_DO_SHOW
)
218 show_object(ctx
, &tag
->object
, name
);
221 static void mark_edge_parents_uninteresting(struct commit
*commit
,
222 struct rev_info
*revs
,
223 show_edge_fn show_edge
)
225 struct commit_list
*parents
;
227 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
228 struct commit
*parent
= parents
->item
;
229 if (!(parent
->object
.flags
& UNINTERESTING
))
231 mark_tree_uninteresting(revs
->repo
,
232 repo_get_commit_tree(the_repository
, parent
));
233 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
234 parent
->object
.flags
|= SHOWN
;
240 static void add_edge_parents(struct commit
*commit
,
241 struct rev_info
*revs
,
242 show_edge_fn show_edge
,
245 struct commit_list
*parents
;
247 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
248 struct commit
*parent
= parents
->item
;
249 struct tree
*tree
= repo_get_commit_tree(the_repository
,
255 oidset_insert(set
, &tree
->object
.oid
);
257 if (!(parent
->object
.flags
& UNINTERESTING
))
259 tree
->object
.flags
|= UNINTERESTING
;
261 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
262 parent
->object
.flags
|= SHOWN
;
268 void mark_edges_uninteresting(struct rev_info
*revs
,
269 show_edge_fn show_edge
,
272 struct commit_list
*list
;
277 oidset_init(&set
, 16);
279 for (list
= revs
->commits
; list
; list
= list
->next
) {
280 struct commit
*commit
= list
->item
;
281 struct tree
*tree
= repo_get_commit_tree(the_repository
,
284 if (commit
->object
.flags
& UNINTERESTING
)
285 tree
->object
.flags
|= UNINTERESTING
;
287 oidset_insert(&set
, &tree
->object
.oid
);
288 add_edge_parents(commit
, revs
, show_edge
, &set
);
291 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
294 for (list
= revs
->commits
; list
; list
= list
->next
) {
295 struct commit
*commit
= list
->item
;
296 if (commit
->object
.flags
& UNINTERESTING
) {
297 mark_tree_uninteresting(revs
->repo
,
298 repo_get_commit_tree(the_repository
, commit
));
299 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
300 commit
->object
.flags
|= SHOWN
;
305 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
309 if (revs
->edge_hint_aggressive
) {
310 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
311 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
312 struct commit
*commit
= (struct commit
*)obj
;
313 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
315 mark_tree_uninteresting(revs
->repo
,
316 repo_get_commit_tree(the_repository
, commit
));
317 if (!(obj
->flags
& SHOWN
)) {
325 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
327 add_pending_object(revs
, &tree
->object
, "");
330 static void traverse_non_commits(struct traversal_context
*ctx
,
335 assert(base
->len
== 0);
337 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
338 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
339 struct object
*obj
= pending
->item
;
340 const char *name
= pending
->name
;
341 const char *path
= pending
->path
;
342 if (obj
->flags
& (UNINTERESTING
| SEEN
))
344 if (obj
->type
== OBJ_TAG
) {
345 process_tag(ctx
, (struct tag
*)obj
, name
);
350 if (obj
->type
== OBJ_TREE
) {
351 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
354 if (obj
->type
== OBJ_BLOB
) {
355 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
358 die("unknown pending object %s (%s)",
359 oid_to_hex(&obj
->oid
), name
);
361 object_array_clear(&ctx
->revs
->pending
);
364 static void do_traverse(struct traversal_context
*ctx
)
366 struct commit
*commit
;
367 struct strbuf csp
; /* callee's scratch pad */
368 strbuf_init(&csp
, PATH_MAX
);
370 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
371 enum list_objects_filter_result r
;
373 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
374 LOFS_COMMIT
, &commit
->object
,
375 NULL
, NULL
, ctx
->filter
);
378 * an uninteresting boundary commit may not have its tree
379 * parsed yet, but we are not going to show them anyway
381 if (!ctx
->revs
->tree_objects
)
382 ; /* do not bother loading tree */
383 else if (repo_get_commit_tree(the_repository
, commit
)) {
384 struct tree
*tree
= repo_get_commit_tree(the_repository
,
386 tree
->object
.flags
|= NOT_USER_GIVEN
;
387 add_pending_tree(ctx
->revs
, tree
);
388 } else if (commit
->object
.parsed
) {
389 die(_("unable to load root tree for commit %s"),
390 oid_to_hex(&commit
->object
.oid
));
393 if (r
& LOFR_MARK_SEEN
)
394 commit
->object
.flags
|= SEEN
;
395 if (r
& LOFR_DO_SHOW
)
396 show_commit(ctx
, commit
);
398 if (ctx
->revs
->tree_blobs_in_commit_order
)
400 * NEEDSWORK: Adding the tree and then flushing it here
401 * needs a reallocation for each commit. Can we pass the
402 * tree directory without allocation churn?
404 traverse_non_commits(ctx
, &csp
);
406 traverse_non_commits(ctx
, &csp
);
407 strbuf_release(&csp
);
410 void traverse_commit_list_filtered(
411 struct rev_info
*revs
,
412 show_commit_fn show_commit
,
413 show_object_fn show_object
,
415 struct oidset
*omitted
)
417 struct traversal_context ctx
= {
419 .show_object
= show_object
,
420 .show_commit
= show_commit
,
421 .show_data
= show_data
,
424 if (revs
->filter
.choice
)
425 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
430 list_objects_filter__free(ctx
.filter
);