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 !has_object_file(&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
, get_commit_tree(parent
));
232 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
233 parent
->object
.flags
|= SHOWN
;
239 static void add_edge_parents(struct commit
*commit
,
240 struct rev_info
*revs
,
241 show_edge_fn show_edge
,
244 struct commit_list
*parents
;
246 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
247 struct commit
*parent
= parents
->item
;
248 struct tree
*tree
= get_commit_tree(parent
);
253 oidset_insert(set
, &tree
->object
.oid
);
255 if (!(parent
->object
.flags
& UNINTERESTING
))
257 tree
->object
.flags
|= UNINTERESTING
;
259 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
260 parent
->object
.flags
|= SHOWN
;
266 void mark_edges_uninteresting(struct rev_info
*revs
,
267 show_edge_fn show_edge
,
270 struct commit_list
*list
;
275 oidset_init(&set
, 16);
277 for (list
= revs
->commits
; list
; list
= list
->next
) {
278 struct commit
*commit
= list
->item
;
279 struct tree
*tree
= get_commit_tree(commit
);
281 if (commit
->object
.flags
& UNINTERESTING
)
282 tree
->object
.flags
|= UNINTERESTING
;
284 oidset_insert(&set
, &tree
->object
.oid
);
285 add_edge_parents(commit
, revs
, show_edge
, &set
);
288 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
291 for (list
= revs
->commits
; list
; list
= list
->next
) {
292 struct commit
*commit
= list
->item
;
293 if (commit
->object
.flags
& UNINTERESTING
) {
294 mark_tree_uninteresting(revs
->repo
,
295 get_commit_tree(commit
));
296 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
297 commit
->object
.flags
|= SHOWN
;
302 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
306 if (revs
->edge_hint_aggressive
) {
307 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
308 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
309 struct commit
*commit
= (struct commit
*)obj
;
310 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
312 mark_tree_uninteresting(revs
->repo
,
313 get_commit_tree(commit
));
314 if (!(obj
->flags
& SHOWN
)) {
322 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
324 add_pending_object(revs
, &tree
->object
, "");
327 static void traverse_non_commits(struct traversal_context
*ctx
,
332 assert(base
->len
== 0);
334 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
335 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
336 struct object
*obj
= pending
->item
;
337 const char *name
= pending
->name
;
338 const char *path
= pending
->path
;
339 if (obj
->flags
& (UNINTERESTING
| SEEN
))
341 if (obj
->type
== OBJ_TAG
) {
342 process_tag(ctx
, (struct tag
*)obj
, name
);
347 if (obj
->type
== OBJ_TREE
) {
348 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
351 if (obj
->type
== OBJ_BLOB
) {
352 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
355 die("unknown pending object %s (%s)",
356 oid_to_hex(&obj
->oid
), name
);
358 object_array_clear(&ctx
->revs
->pending
);
361 static void do_traverse(struct traversal_context
*ctx
)
363 struct commit
*commit
;
364 struct strbuf csp
; /* callee's scratch pad */
365 strbuf_init(&csp
, PATH_MAX
);
367 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
368 enum list_objects_filter_result r
;
370 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
371 LOFS_COMMIT
, &commit
->object
,
372 NULL
, NULL
, ctx
->filter
);
375 * an uninteresting boundary commit may not have its tree
376 * parsed yet, but we are not going to show them anyway
378 if (!ctx
->revs
->tree_objects
)
379 ; /* do not bother loading tree */
380 else if (get_commit_tree(commit
)) {
381 struct tree
*tree
= get_commit_tree(commit
);
382 tree
->object
.flags
|= NOT_USER_GIVEN
;
383 add_pending_tree(ctx
->revs
, tree
);
384 } else if (commit
->object
.parsed
) {
385 die(_("unable to load root tree for commit %s"),
386 oid_to_hex(&commit
->object
.oid
));
389 if (r
& LOFR_MARK_SEEN
)
390 commit
->object
.flags
|= SEEN
;
391 if (r
& LOFR_DO_SHOW
)
392 show_commit(ctx
, commit
);
394 if (ctx
->revs
->tree_blobs_in_commit_order
)
396 * NEEDSWORK: Adding the tree and then flushing it here
397 * needs a reallocation for each commit. Can we pass the
398 * tree directory without allocation churn?
400 traverse_non_commits(ctx
, &csp
);
402 traverse_non_commits(ctx
, &csp
);
403 strbuf_release(&csp
);
406 void traverse_commit_list_filtered(
407 struct rev_info
*revs
,
408 show_commit_fn show_commit
,
409 show_object_fn show_object
,
411 struct oidset
*omitted
)
413 struct traversal_context ctx
= {
415 .show_object
= show_object
,
416 .show_commit
= show_commit
,
417 .show_data
= show_data
,
420 if (revs
->filter
.choice
)
421 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
426 list_objects_filter__free(ctx
.filter
);