9 #include "list-objects.h"
10 #include "list-objects-filter.h"
11 #include "list-objects-filter-options.h"
13 #include "object-store.h"
16 struct traversal_context
{
17 struct rev_info
*revs
;
18 show_object_fn show_object
;
19 show_commit_fn show_commit
;
21 struct filter
*filter
;
24 static void show_commit(struct traversal_context
*ctx
,
25 struct commit
*commit
)
27 if (!ctx
->show_commit
)
29 ctx
->show_commit(commit
, ctx
->show_data
);
32 static void show_object(struct traversal_context
*ctx
,
33 struct object
*object
,
36 if (!ctx
->show_object
)
38 ctx
->show_object(object
, name
, ctx
->show_data
);
41 static void process_blob(struct traversal_context
*ctx
,
46 struct object
*obj
= &blob
->object
;
48 enum list_objects_filter_result r
;
50 if (!ctx
->revs
->blob_objects
)
53 die("bad blob object");
54 if (obj
->flags
& (UNINTERESTING
| SEEN
))
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).
62 * Note that this "--exclude-promisor-objects" pre-filtering
63 * may cause the actual filter to report an incomplete list
66 if (ctx
->revs
->exclude_promisor_objects
&&
67 !has_object_file(&obj
->oid
) &&
68 is_promisor_object(&obj
->oid
))
72 strbuf_addstr(path
, name
);
73 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
75 path
->buf
, &path
->buf
[pathlen
],
77 if (r
& LOFR_MARK_SEEN
)
80 show_object(ctx
, obj
, path
->buf
);
81 strbuf_setlen(path
, pathlen
);
84 static void process_tree(struct traversal_context
*ctx
,
89 static void process_tree_contents(struct traversal_context
*ctx
,
93 struct tree_desc desc
;
94 struct name_entry entry
;
95 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
96 all_entries_interesting
: entry_not_interesting
;
98 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
100 while (tree_entry(&desc
, &entry
)) {
101 if (match
!= all_entries_interesting
) {
102 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
104 &ctx
->revs
->diffopt
.pathspec
);
105 if (match
== all_entries_not_interesting
)
107 if (match
== entry_not_interesting
)
111 if (S_ISDIR(entry
.mode
)) {
112 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
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
));
118 t
->object
.flags
|= NOT_USER_GIVEN
;
119 process_tree(ctx
, t
, base
, entry
.path
);
121 else if (S_ISGITLINK(entry
.mode
))
122 ; /* ignore gitlink */
124 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
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
));
130 b
->object
.flags
|= NOT_USER_GIVEN
;
131 process_blob(ctx
, b
, base
, entry
.path
);
136 static void process_tree(struct traversal_context
*ctx
,
141 struct object
*obj
= &tree
->object
;
142 struct rev_info
*revs
= ctx
->revs
;
143 int baselen
= base
->len
;
144 enum list_objects_filter_result r
;
147 if (!revs
->tree_objects
)
150 die("bad tree object");
151 if (obj
->flags
& (UNINTERESTING
| SEEN
))
153 if (revs
->include_check_obj
&&
154 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
157 failed_parse
= parse_tree_gently(tree
, 1);
159 if (revs
->ignore_missing_links
)
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.
167 if (revs
->exclude_promisor_objects
&&
168 is_promisor_object(&obj
->oid
))
171 if (!revs
->do_not_die_on_missing_tree
)
172 die("bad tree object %s", oid_to_hex(&obj
->oid
));
175 strbuf_addstr(base
, name
);
176 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
177 LOFS_BEGIN_TREE
, obj
,
178 base
->buf
, &base
->buf
[baselen
],
180 if (r
& LOFR_MARK_SEEN
)
182 if (r
& LOFR_DO_SHOW
)
183 show_object(ctx
, obj
, base
->buf
);
185 strbuf_addch(base
, '/');
187 if (r
& LOFR_SKIP_TREE
)
188 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
189 else if (!failed_parse
)
190 process_tree_contents(ctx
, tree
, base
);
192 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
194 base
->buf
, &base
->buf
[baselen
],
196 if (r
& LOFR_MARK_SEEN
)
198 if (r
& LOFR_DO_SHOW
)
199 show_object(ctx
, obj
, base
->buf
);
201 strbuf_setlen(base
, baselen
);
202 free_tree_buffer(tree
);
205 static void process_tag(struct traversal_context
*ctx
,
209 enum list_objects_filter_result r
;
211 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
212 &tag
->object
, NULL
, NULL
,
214 if (r
& LOFR_MARK_SEEN
)
215 tag
->object
.flags
|= SEEN
;
216 if (r
& LOFR_DO_SHOW
)
217 show_object(ctx
, &tag
->object
, name
);
220 static void mark_edge_parents_uninteresting(struct commit
*commit
,
221 struct rev_info
*revs
,
222 show_edge_fn show_edge
)
224 struct commit_list
*parents
;
226 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
227 struct commit
*parent
= parents
->item
;
228 if (!(parent
->object
.flags
& UNINTERESTING
))
230 mark_tree_uninteresting(revs
->repo
, get_commit_tree(parent
));
231 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
232 parent
->object
.flags
|= SHOWN
;
238 static void add_edge_parents(struct commit
*commit
,
239 struct rev_info
*revs
,
240 show_edge_fn show_edge
,
243 struct commit_list
*parents
;
245 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
246 struct commit
*parent
= parents
->item
;
247 struct tree
*tree
= get_commit_tree(parent
);
252 oidset_insert(set
, &tree
->object
.oid
);
254 if (!(parent
->object
.flags
& UNINTERESTING
))
256 tree
->object
.flags
|= UNINTERESTING
;
258 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
259 parent
->object
.flags
|= SHOWN
;
265 void mark_edges_uninteresting(struct rev_info
*revs
,
266 show_edge_fn show_edge
,
269 struct commit_list
*list
;
274 oidset_init(&set
, 16);
276 for (list
= revs
->commits
; list
; list
= list
->next
) {
277 struct commit
*commit
= list
->item
;
278 struct tree
*tree
= get_commit_tree(commit
);
280 if (commit
->object
.flags
& UNINTERESTING
)
281 tree
->object
.flags
|= UNINTERESTING
;
283 oidset_insert(&set
, &tree
->object
.oid
);
284 add_edge_parents(commit
, revs
, show_edge
, &set
);
287 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
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
;
301 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
305 if (revs
->edge_hint_aggressive
) {
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
))
311 mark_tree_uninteresting(revs
->repo
,
312 get_commit_tree(commit
));
313 if (!(obj
->flags
& SHOWN
)) {
321 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
323 add_pending_object(revs
, &tree
->object
, "");
326 static void traverse_non_commits(struct traversal_context
*ctx
,
331 assert(base
->len
== 0);
333 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
334 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
335 struct object
*obj
= pending
->item
;
336 const char *name
= pending
->name
;
337 const char *path
= pending
->path
;
338 if (obj
->flags
& (UNINTERESTING
| SEEN
))
340 if (obj
->type
== OBJ_TAG
) {
341 process_tag(ctx
, (struct tag
*)obj
, name
);
346 if (obj
->type
== OBJ_TREE
) {
347 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
350 if (obj
->type
== OBJ_BLOB
) {
351 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
354 die("unknown pending object %s (%s)",
355 oid_to_hex(&obj
->oid
), name
);
357 object_array_clear(&ctx
->revs
->pending
);
360 static void do_traverse(struct traversal_context
*ctx
)
362 struct commit
*commit
;
363 struct strbuf csp
; /* callee's scratch pad */
364 strbuf_init(&csp
, PATH_MAX
);
366 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
367 enum list_objects_filter_result r
;
369 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
370 LOFS_COMMIT
, &commit
->object
,
371 NULL
, NULL
, ctx
->filter
);
374 * an uninteresting boundary commit may not have its tree
375 * parsed yet, but we are not going to show them anyway
377 if (!ctx
->revs
->tree_objects
)
378 ; /* do not bother loading tree */
379 else if (get_commit_tree(commit
)) {
380 struct tree
*tree
= get_commit_tree(commit
);
381 tree
->object
.flags
|= NOT_USER_GIVEN
;
382 add_pending_tree(ctx
->revs
, tree
);
383 } else if (commit
->object
.parsed
) {
384 die(_("unable to load root tree for commit %s"),
385 oid_to_hex(&commit
->object
.oid
));
388 if (r
& LOFR_MARK_SEEN
)
389 commit
->object
.flags
|= SEEN
;
390 if (r
& LOFR_DO_SHOW
)
391 show_commit(ctx
, commit
);
393 if (ctx
->revs
->tree_blobs_in_commit_order
)
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?
399 traverse_non_commits(ctx
, &csp
);
401 traverse_non_commits(ctx
, &csp
);
402 strbuf_release(&csp
);
405 void traverse_commit_list_filtered(
406 struct rev_info
*revs
,
407 show_commit_fn show_commit
,
408 show_object_fn show_object
,
410 struct oidset
*omitted
)
412 struct traversal_context ctx
= {
414 .show_object
= show_object
,
415 .show_commit
= show_commit
,
416 .show_data
= show_data
,
419 if (revs
->filter
.choice
)
420 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
425 list_objects_filter__free(ctx
.filter
);