1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
11 #include "tree-walk.h"
13 #include "list-objects.h"
14 #include "list-objects-filter.h"
15 #include "list-objects-filter-options.h"
17 #include "object-store.h"
19 #include "environment.h"
21 struct traversal_context
{
22 struct rev_info
*revs
;
23 show_object_fn show_object
;
24 show_commit_fn show_commit
;
26 struct filter
*filter
;
30 static void show_commit(struct traversal_context
*ctx
,
31 struct commit
*commit
)
33 if (!ctx
->show_commit
)
35 ctx
->show_commit(commit
, ctx
->show_data
);
38 static void show_object(struct traversal_context
*ctx
,
39 struct object
*object
,
42 if (!ctx
->show_object
)
44 if (ctx
->revs
->unpacked
&& has_object_pack(ctx
->revs
->repo
,
48 ctx
->show_object(object
, name
, ctx
->show_data
);
51 static void process_blob(struct traversal_context
*ctx
,
56 struct object
*obj
= &blob
->object
;
58 enum list_objects_filter_result r
;
60 if (!ctx
->revs
->blob_objects
)
63 die("bad blob object");
64 if (obj
->flags
& (UNINTERESTING
| SEEN
))
68 * Pre-filter known-missing objects when explicitly requested.
69 * Otherwise, a missing object error message may be reported
70 * later (depending on other filtering criteria).
72 * Note that this "--exclude-promisor-objects" pre-filtering
73 * may cause the actual filter to report an incomplete list
76 if (ctx
->revs
->exclude_promisor_objects
&&
77 !has_object(the_repository
, &obj
->oid
,
78 HAS_OBJECT_RECHECK_PACKED
| HAS_OBJECT_FETCH_PROMISOR
) &&
79 is_promisor_object(ctx
->revs
->repo
, &obj
->oid
))
83 strbuf_addstr(path
, name
);
84 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
86 path
->buf
, &path
->buf
[pathlen
],
88 if (r
& LOFR_MARK_SEEN
)
91 show_object(ctx
, obj
, path
->buf
);
92 strbuf_setlen(path
, pathlen
);
95 static void process_tree(struct traversal_context
*ctx
,
100 static void process_tree_contents(struct traversal_context
*ctx
,
104 struct tree_desc desc
;
105 struct name_entry entry
;
106 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
107 all_entries_interesting
: entry_not_interesting
;
109 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
111 while (tree_entry(&desc
, &entry
)) {
112 if (match
!= all_entries_interesting
) {
113 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
115 &ctx
->revs
->diffopt
.pathspec
);
116 if (match
== all_entries_not_interesting
)
118 if (match
== entry_not_interesting
)
122 if (S_ISDIR(entry
.mode
)) {
123 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
125 die(_("entry '%s' in tree %s has tree mode, "
126 "but is not a tree"),
127 entry
.path
, oid_to_hex(&tree
->object
.oid
));
129 t
->object
.flags
|= NOT_USER_GIVEN
;
131 process_tree(ctx
, t
, base
, entry
.path
);
134 else if (S_ISGITLINK(entry
.mode
))
135 ; /* ignore gitlink */
137 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
139 die(_("entry '%s' in tree %s has blob mode, "
140 "but is not a blob"),
141 entry
.path
, oid_to_hex(&tree
->object
.oid
));
143 b
->object
.flags
|= NOT_USER_GIVEN
;
144 process_blob(ctx
, b
, base
, entry
.path
);
149 static void process_tree(struct traversal_context
*ctx
,
154 struct object
*obj
= &tree
->object
;
155 struct rev_info
*revs
= ctx
->revs
;
156 int baselen
= base
->len
;
157 enum list_objects_filter_result r
;
160 if (!revs
->tree_objects
)
163 die("bad tree object");
164 if (obj
->flags
& (UNINTERESTING
| SEEN
))
166 if (revs
->include_check_obj
&&
167 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
170 if (ctx
->depth
> max_allowed_tree_depth
)
171 die("exceeded maximum allowed tree depth");
173 failed_parse
= parse_tree_gently(tree
, 1);
175 if (revs
->ignore_missing_links
)
179 * Pre-filter known-missing tree objects when explicitly
180 * requested. This may cause the actual filter to report
181 * an incomplete list of missing objects.
183 if (revs
->exclude_promisor_objects
&&
184 is_promisor_object(revs
->repo
, &obj
->oid
))
187 if (!revs
->do_not_die_on_missing_objects
)
188 die("bad tree object %s", oid_to_hex(&obj
->oid
));
191 strbuf_addstr(base
, name
);
192 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
193 LOFS_BEGIN_TREE
, obj
,
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_addch(base
, '/');
203 if (r
& LOFR_SKIP_TREE
)
204 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
205 else if (!failed_parse
)
206 process_tree_contents(ctx
, tree
, base
);
208 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
210 base
->buf
, &base
->buf
[baselen
],
212 if (r
& LOFR_MARK_SEEN
)
214 if (r
& LOFR_DO_SHOW
)
215 show_object(ctx
, obj
, base
->buf
);
217 strbuf_setlen(base
, baselen
);
218 free_tree_buffer(tree
);
221 static void process_tag(struct traversal_context
*ctx
,
225 enum list_objects_filter_result r
;
227 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
228 &tag
->object
, NULL
, NULL
,
230 if (r
& LOFR_MARK_SEEN
)
231 tag
->object
.flags
|= SEEN
;
232 if (r
& LOFR_DO_SHOW
)
233 show_object(ctx
, &tag
->object
, name
);
236 static void mark_edge_parents_uninteresting(struct commit
*commit
,
237 struct rev_info
*revs
,
238 show_edge_fn show_edge
)
240 struct commit_list
*parents
;
242 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
243 struct commit
*parent
= parents
->item
;
244 if (!(parent
->object
.flags
& UNINTERESTING
))
246 mark_tree_uninteresting(revs
->repo
,
247 repo_get_commit_tree(the_repository
, parent
));
248 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
249 parent
->object
.flags
|= SHOWN
;
255 static void add_edge_parents(struct commit
*commit
,
256 struct rev_info
*revs
,
257 show_edge_fn show_edge
,
260 struct commit_list
*parents
;
262 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
263 struct commit
*parent
= parents
->item
;
264 struct tree
*tree
= repo_get_commit_tree(the_repository
,
270 oidset_insert(set
, &tree
->object
.oid
);
272 if (!(parent
->object
.flags
& UNINTERESTING
))
274 tree
->object
.flags
|= UNINTERESTING
;
276 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
277 parent
->object
.flags
|= SHOWN
;
283 void mark_edges_uninteresting(struct rev_info
*revs
,
284 show_edge_fn show_edge
,
287 struct commit_list
*list
;
291 oidset_init(&set
, 16);
293 for (list
= revs
->commits
; list
; list
= list
->next
) {
294 struct commit
*commit
= list
->item
;
295 struct tree
*tree
= repo_get_commit_tree(the_repository
,
298 if (commit
->object
.flags
& UNINTERESTING
)
299 tree
->object
.flags
|= UNINTERESTING
;
301 oidset_insert(&set
, &tree
->object
.oid
);
302 add_edge_parents(commit
, revs
, show_edge
, &set
);
305 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
308 for (list
= revs
->commits
; list
; list
= list
->next
) {
309 struct commit
*commit
= list
->item
;
310 if (commit
->object
.flags
& UNINTERESTING
) {
311 mark_tree_uninteresting(revs
->repo
,
312 repo_get_commit_tree(the_repository
, commit
));
313 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
314 commit
->object
.flags
|= SHOWN
;
319 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
323 if (revs
->edge_hint_aggressive
) {
324 for (size_t i
= 0; i
< revs
->cmdline
.nr
; i
++) {
325 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
326 struct commit
*commit
= (struct commit
*)obj
;
327 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
329 mark_tree_uninteresting(revs
->repo
,
330 repo_get_commit_tree(the_repository
, commit
));
331 if (!(obj
->flags
& SHOWN
)) {
339 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
341 add_pending_object(revs
, &tree
->object
, "");
344 static void traverse_non_commits(struct traversal_context
*ctx
,
347 assert(base
->len
== 0);
349 for (size_t i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
350 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
351 struct object
*obj
= pending
->item
;
352 const char *name
= pending
->name
;
353 const char *path
= pending
->path
;
354 if (obj
->flags
& (UNINTERESTING
| SEEN
))
356 if (obj
->type
== OBJ_TAG
) {
357 process_tag(ctx
, (struct tag
*)obj
, name
);
362 if (obj
->type
== OBJ_TREE
) {
364 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
367 if (obj
->type
== OBJ_BLOB
) {
368 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
371 die("unknown pending object %s (%s)",
372 oid_to_hex(&obj
->oid
), name
);
374 object_array_clear(&ctx
->revs
->pending
);
377 static void do_traverse(struct traversal_context
*ctx
)
379 struct commit
*commit
;
380 struct strbuf csp
; /* callee's scratch pad */
381 strbuf_init(&csp
, PATH_MAX
);
383 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
384 enum list_objects_filter_result r
;
386 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
387 LOFS_COMMIT
, &commit
->object
,
388 NULL
, NULL
, ctx
->filter
);
391 * an uninteresting boundary commit may not have its tree
392 * parsed yet, but we are not going to show them anyway
394 if (!ctx
->revs
->tree_objects
)
395 ; /* do not bother loading tree */
396 else if (ctx
->revs
->do_not_die_on_missing_objects
&&
397 oidset_contains(&ctx
->revs
->missing_commits
, &commit
->object
.oid
))
399 else if (repo_get_commit_tree(the_repository
, commit
)) {
400 struct tree
*tree
= repo_get_commit_tree(the_repository
,
402 tree
->object
.flags
|= NOT_USER_GIVEN
;
403 add_pending_tree(ctx
->revs
, tree
);
404 } else if (commit
->object
.parsed
) {
405 die(_("unable to load root tree for commit %s"),
406 oid_to_hex(&commit
->object
.oid
));
409 if (r
& LOFR_MARK_SEEN
)
410 commit
->object
.flags
|= SEEN
;
411 if (r
& LOFR_DO_SHOW
)
412 show_commit(ctx
, commit
);
414 if (ctx
->revs
->tree_blobs_in_commit_order
)
416 * NEEDSWORK: Adding the tree and then flushing it here
417 * needs a reallocation for each commit. Can we pass the
418 * tree directory without allocation churn?
420 traverse_non_commits(ctx
, &csp
);
422 traverse_non_commits(ctx
, &csp
);
423 strbuf_release(&csp
);
426 void traverse_commit_list_filtered(
427 struct rev_info
*revs
,
428 show_commit_fn show_commit
,
429 show_object_fn show_object
,
431 struct oidset
*omitted
)
433 struct traversal_context ctx
= {
435 .show_object
= show_object
,
436 .show_commit
= show_commit
,
437 .show_data
= show_data
,
440 if (revs
->filter
.choice
)
441 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
446 list_objects_filter__free(ctx
.filter
);