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-ll.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(&object
->oid
))
47 ctx
->show_object(object
, name
, ctx
->show_data
);
50 static void process_blob(struct traversal_context
*ctx
,
55 struct object
*obj
= &blob
->object
;
57 enum list_objects_filter_result r
;
59 if (!ctx
->revs
->blob_objects
)
62 die("bad blob object");
63 if (obj
->flags
& (UNINTERESTING
| SEEN
))
67 * Pre-filter known-missing objects when explicitly requested.
68 * Otherwise, a missing object error message may be reported
69 * later (depending on other filtering criteria).
71 * Note that this "--exclude-promisor-objects" pre-filtering
72 * may cause the actual filter to report an incomplete list
75 if (ctx
->revs
->exclude_promisor_objects
&&
76 !repo_has_object_file(the_repository
, &obj
->oid
) &&
77 is_promisor_object(&obj
->oid
))
81 strbuf_addstr(path
, name
);
82 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
84 path
->buf
, &path
->buf
[pathlen
],
86 if (r
& LOFR_MARK_SEEN
)
89 show_object(ctx
, obj
, path
->buf
);
90 strbuf_setlen(path
, pathlen
);
93 static void process_tree(struct traversal_context
*ctx
,
98 static void process_tree_contents(struct traversal_context
*ctx
,
102 struct tree_desc desc
;
103 struct name_entry entry
;
104 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
105 all_entries_interesting
: entry_not_interesting
;
107 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
109 while (tree_entry(&desc
, &entry
)) {
110 if (match
!= all_entries_interesting
) {
111 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
113 &ctx
->revs
->diffopt
.pathspec
);
114 if (match
== all_entries_not_interesting
)
116 if (match
== entry_not_interesting
)
120 if (S_ISDIR(entry
.mode
)) {
121 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
123 die(_("entry '%s' in tree %s has tree mode, "
124 "but is not a tree"),
125 entry
.path
, oid_to_hex(&tree
->object
.oid
));
127 t
->object
.flags
|= NOT_USER_GIVEN
;
129 process_tree(ctx
, t
, base
, entry
.path
);
132 else if (S_ISGITLINK(entry
.mode
))
133 ; /* ignore gitlink */
135 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
137 die(_("entry '%s' in tree %s has blob mode, "
138 "but is not a blob"),
139 entry
.path
, oid_to_hex(&tree
->object
.oid
));
141 b
->object
.flags
|= NOT_USER_GIVEN
;
142 process_blob(ctx
, b
, base
, entry
.path
);
147 static void process_tree(struct traversal_context
*ctx
,
152 struct object
*obj
= &tree
->object
;
153 struct rev_info
*revs
= ctx
->revs
;
154 int baselen
= base
->len
;
155 enum list_objects_filter_result r
;
158 if (!revs
->tree_objects
)
161 die("bad tree object");
162 if (obj
->flags
& (UNINTERESTING
| SEEN
))
164 if (revs
->include_check_obj
&&
165 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
168 if (ctx
->depth
> max_allowed_tree_depth
)
169 die("exceeded maximum allowed tree depth");
171 failed_parse
= parse_tree_gently(tree
, 1);
173 if (revs
->ignore_missing_links
)
177 * Pre-filter known-missing tree objects when explicitly
178 * requested. This may cause the actual filter to report
179 * an incomplete list of missing objects.
181 if (revs
->exclude_promisor_objects
&&
182 is_promisor_object(&obj
->oid
))
185 if (!revs
->do_not_die_on_missing_objects
)
186 die("bad tree object %s", oid_to_hex(&obj
->oid
));
189 strbuf_addstr(base
, name
);
190 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
191 LOFS_BEGIN_TREE
, obj
,
192 base
->buf
, &base
->buf
[baselen
],
194 if (r
& LOFR_MARK_SEEN
)
196 if (r
& LOFR_DO_SHOW
)
197 show_object(ctx
, obj
, base
->buf
);
199 strbuf_addch(base
, '/');
201 if (r
& LOFR_SKIP_TREE
)
202 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
203 else if (!failed_parse
)
204 process_tree_contents(ctx
, tree
, base
);
206 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
208 base
->buf
, &base
->buf
[baselen
],
210 if (r
& LOFR_MARK_SEEN
)
212 if (r
& LOFR_DO_SHOW
)
213 show_object(ctx
, obj
, base
->buf
);
215 strbuf_setlen(base
, baselen
);
216 free_tree_buffer(tree
);
219 static void process_tag(struct traversal_context
*ctx
,
223 enum list_objects_filter_result r
;
225 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
226 &tag
->object
, NULL
, NULL
,
228 if (r
& LOFR_MARK_SEEN
)
229 tag
->object
.flags
|= SEEN
;
230 if (r
& LOFR_DO_SHOW
)
231 show_object(ctx
, &tag
->object
, name
);
234 static void mark_edge_parents_uninteresting(struct commit
*commit
,
235 struct rev_info
*revs
,
236 show_edge_fn show_edge
)
238 struct commit_list
*parents
;
240 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
241 struct commit
*parent
= parents
->item
;
242 if (!(parent
->object
.flags
& UNINTERESTING
))
244 mark_tree_uninteresting(revs
->repo
,
245 repo_get_commit_tree(the_repository
, parent
));
246 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
247 parent
->object
.flags
|= SHOWN
;
253 static void add_edge_parents(struct commit
*commit
,
254 struct rev_info
*revs
,
255 show_edge_fn show_edge
,
258 struct commit_list
*parents
;
260 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
261 struct commit
*parent
= parents
->item
;
262 struct tree
*tree
= repo_get_commit_tree(the_repository
,
268 oidset_insert(set
, &tree
->object
.oid
);
270 if (!(parent
->object
.flags
& UNINTERESTING
))
272 tree
->object
.flags
|= UNINTERESTING
;
274 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
275 parent
->object
.flags
|= SHOWN
;
281 void mark_edges_uninteresting(struct rev_info
*revs
,
282 show_edge_fn show_edge
,
285 struct commit_list
*list
;
290 oidset_init(&set
, 16);
292 for (list
= revs
->commits
; list
; list
= list
->next
) {
293 struct commit
*commit
= list
->item
;
294 struct tree
*tree
= repo_get_commit_tree(the_repository
,
297 if (commit
->object
.flags
& UNINTERESTING
)
298 tree
->object
.flags
|= UNINTERESTING
;
300 oidset_insert(&set
, &tree
->object
.oid
);
301 add_edge_parents(commit
, revs
, show_edge
, &set
);
304 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
307 for (list
= revs
->commits
; list
; list
= list
->next
) {
308 struct commit
*commit
= list
->item
;
309 if (commit
->object
.flags
& UNINTERESTING
) {
310 mark_tree_uninteresting(revs
->repo
,
311 repo_get_commit_tree(the_repository
, commit
));
312 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
313 commit
->object
.flags
|= SHOWN
;
318 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
322 if (revs
->edge_hint_aggressive
) {
323 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
324 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
325 struct commit
*commit
= (struct commit
*)obj
;
326 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
328 mark_tree_uninteresting(revs
->repo
,
329 repo_get_commit_tree(the_repository
, commit
));
330 if (!(obj
->flags
& SHOWN
)) {
338 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
340 add_pending_object(revs
, &tree
->object
, "");
343 static void traverse_non_commits(struct traversal_context
*ctx
,
348 assert(base
->len
== 0);
350 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
351 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
352 struct object
*obj
= pending
->item
;
353 const char *name
= pending
->name
;
354 const char *path
= pending
->path
;
355 if (obj
->flags
& (UNINTERESTING
| SEEN
))
357 if (obj
->type
== OBJ_TAG
) {
358 process_tag(ctx
, (struct tag
*)obj
, name
);
363 if (obj
->type
== OBJ_TREE
) {
365 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
368 if (obj
->type
== OBJ_BLOB
) {
369 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
372 die("unknown pending object %s (%s)",
373 oid_to_hex(&obj
->oid
), name
);
375 object_array_clear(&ctx
->revs
->pending
);
378 static void do_traverse(struct traversal_context
*ctx
)
380 struct commit
*commit
;
381 struct strbuf csp
; /* callee's scratch pad */
382 strbuf_init(&csp
, PATH_MAX
);
384 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
385 enum list_objects_filter_result r
;
387 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
388 LOFS_COMMIT
, &commit
->object
,
389 NULL
, NULL
, ctx
->filter
);
392 * an uninteresting boundary commit may not have its tree
393 * parsed yet, but we are not going to show them anyway
395 if (!ctx
->revs
->tree_objects
)
396 ; /* do not bother loading tree */
397 else if (ctx
->revs
->do_not_die_on_missing_objects
&&
398 oidset_contains(&ctx
->revs
->missing_commits
, &commit
->object
.oid
))
400 else if (repo_get_commit_tree(the_repository
, commit
)) {
401 struct tree
*tree
= repo_get_commit_tree(the_repository
,
403 tree
->object
.flags
|= NOT_USER_GIVEN
;
404 add_pending_tree(ctx
->revs
, tree
);
405 } else if (commit
->object
.parsed
) {
406 die(_("unable to load root tree for commit %s"),
407 oid_to_hex(&commit
->object
.oid
));
410 if (r
& LOFR_MARK_SEEN
)
411 commit
->object
.flags
|= SEEN
;
412 if (r
& LOFR_DO_SHOW
)
413 show_commit(ctx
, commit
);
415 if (ctx
->revs
->tree_blobs_in_commit_order
)
417 * NEEDSWORK: Adding the tree and then flushing it here
418 * needs a reallocation for each commit. Can we pass the
419 * tree directory without allocation churn?
421 traverse_non_commits(ctx
, &csp
);
423 traverse_non_commits(ctx
, &csp
);
424 strbuf_release(&csp
);
427 void traverse_commit_list_filtered(
428 struct rev_info
*revs
,
429 show_commit_fn show_commit
,
430 show_object_fn show_object
,
432 struct oidset
*omitted
)
434 struct traversal_context ctx
= {
436 .show_object
= show_object
,
437 .show_commit
= show_commit
,
438 .show_data
= show_data
,
441 if (revs
->filter
.choice
)
442 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
447 list_objects_filter__free(ctx
.filter
);