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
);
85 * Processing a gitlink entry currently does nothing, since
86 * we do not recurse into the subproject.
88 * We *could* eventually add a flag that actually does that,
89 * which would involve:
90 * - is the subproject actually checked out?
91 * - if so, see if the subproject has already been added
92 * to the alternates list, and add it if not.
93 * - process the commit (or tag) the gitlink points to
96 * However, it's unclear whether there is really ever any
97 * reason to see superprojects and subprojects as such a
98 * "unified" object pool (potentially resulting in a totally
99 * humongous pack - avoiding which was the whole point of
100 * having gitlinks in the first place!).
102 * So for now, there is just a note that we *could* follow
103 * the link, and how to do it. Whether it necessarily makes
104 * any sense what-so-ever to ever do that is another issue.
106 static void process_gitlink(struct traversal_context
*ctx
,
107 const unsigned char *sha1
,
114 static void process_tree(struct traversal_context
*ctx
,
119 static void process_tree_contents(struct traversal_context
*ctx
,
123 struct tree_desc desc
;
124 struct name_entry entry
;
125 enum interesting match
= ctx
->revs
->diffopt
.pathspec
.nr
== 0 ?
126 all_entries_interesting
: entry_not_interesting
;
128 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
130 while (tree_entry(&desc
, &entry
)) {
131 if (match
!= all_entries_interesting
) {
132 match
= tree_entry_interesting(ctx
->revs
->repo
->index
,
134 &ctx
->revs
->diffopt
.pathspec
);
135 if (match
== all_entries_not_interesting
)
137 if (match
== entry_not_interesting
)
141 if (S_ISDIR(entry
.mode
)) {
142 struct tree
*t
= lookup_tree(ctx
->revs
->repo
, &entry
.oid
);
144 die(_("entry '%s' in tree %s has tree mode, "
145 "but is not a tree"),
146 entry
.path
, oid_to_hex(&tree
->object
.oid
));
148 t
->object
.flags
|= NOT_USER_GIVEN
;
149 process_tree(ctx
, t
, base
, entry
.path
);
151 else if (S_ISGITLINK(entry
.mode
))
152 process_gitlink(ctx
, entry
.oid
.hash
,
155 struct blob
*b
= lookup_blob(ctx
->revs
->repo
, &entry
.oid
);
157 die(_("entry '%s' in tree %s has blob mode, "
158 "but is not a blob"),
159 entry
.path
, oid_to_hex(&tree
->object
.oid
));
161 b
->object
.flags
|= NOT_USER_GIVEN
;
162 process_blob(ctx
, b
, base
, entry
.path
);
167 static void process_tree(struct traversal_context
*ctx
,
172 struct object
*obj
= &tree
->object
;
173 struct rev_info
*revs
= ctx
->revs
;
174 int baselen
= base
->len
;
175 enum list_objects_filter_result r
;
178 if (!revs
->tree_objects
)
181 die("bad tree object");
182 if (obj
->flags
& (UNINTERESTING
| SEEN
))
184 if (revs
->include_check_obj
&&
185 !revs
->include_check_obj(&tree
->object
, revs
->include_check_data
))
188 failed_parse
= parse_tree_gently(tree
, 1);
190 if (revs
->ignore_missing_links
)
194 * Pre-filter known-missing tree objects when explicitly
195 * requested. This may cause the actual filter to report
196 * an incomplete list of missing objects.
198 if (revs
->exclude_promisor_objects
&&
199 is_promisor_object(&obj
->oid
))
202 if (!revs
->do_not_die_on_missing_tree
)
203 die("bad tree object %s", oid_to_hex(&obj
->oid
));
206 strbuf_addstr(base
, name
);
207 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
208 LOFS_BEGIN_TREE
, obj
,
209 base
->buf
, &base
->buf
[baselen
],
211 if (r
& LOFR_MARK_SEEN
)
213 if (r
& LOFR_DO_SHOW
)
214 show_object(ctx
, obj
, base
->buf
);
216 strbuf_addch(base
, '/');
218 if (r
& LOFR_SKIP_TREE
)
219 trace_printf("Skipping contents of tree %s...\n", base
->buf
);
220 else if (!failed_parse
)
221 process_tree_contents(ctx
, tree
, base
);
223 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
225 base
->buf
, &base
->buf
[baselen
],
227 if (r
& LOFR_MARK_SEEN
)
229 if (r
& LOFR_DO_SHOW
)
230 show_object(ctx
, obj
, base
->buf
);
232 strbuf_setlen(base
, baselen
);
233 free_tree_buffer(tree
);
236 static void process_tag(struct traversal_context
*ctx
,
240 enum list_objects_filter_result r
;
242 r
= list_objects_filter__filter_object(ctx
->revs
->repo
, LOFS_TAG
,
243 &tag
->object
, NULL
, NULL
,
245 if (r
& LOFR_MARK_SEEN
)
246 tag
->object
.flags
|= SEEN
;
247 if (r
& LOFR_DO_SHOW
)
248 show_object(ctx
, &tag
->object
, name
);
251 static void mark_edge_parents_uninteresting(struct commit
*commit
,
252 struct rev_info
*revs
,
253 show_edge_fn show_edge
)
255 struct commit_list
*parents
;
257 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
258 struct commit
*parent
= parents
->item
;
259 if (!(parent
->object
.flags
& UNINTERESTING
))
261 mark_tree_uninteresting(revs
->repo
, get_commit_tree(parent
));
262 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
263 parent
->object
.flags
|= SHOWN
;
269 static void add_edge_parents(struct commit
*commit
,
270 struct rev_info
*revs
,
271 show_edge_fn show_edge
,
274 struct commit_list
*parents
;
276 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
277 struct commit
*parent
= parents
->item
;
278 struct tree
*tree
= get_commit_tree(parent
);
283 oidset_insert(set
, &tree
->object
.oid
);
285 if (!(parent
->object
.flags
& UNINTERESTING
))
287 tree
->object
.flags
|= UNINTERESTING
;
289 if (revs
->edge_hint
&& !(parent
->object
.flags
& SHOWN
)) {
290 parent
->object
.flags
|= SHOWN
;
296 void mark_edges_uninteresting(struct rev_info
*revs
,
297 show_edge_fn show_edge
,
300 struct commit_list
*list
;
305 oidset_init(&set
, 16);
307 for (list
= revs
->commits
; list
; list
= list
->next
) {
308 struct commit
*commit
= list
->item
;
309 struct tree
*tree
= get_commit_tree(commit
);
311 if (commit
->object
.flags
& UNINTERESTING
)
312 tree
->object
.flags
|= UNINTERESTING
;
314 oidset_insert(&set
, &tree
->object
.oid
);
315 add_edge_parents(commit
, revs
, show_edge
, &set
);
318 mark_trees_uninteresting_sparse(revs
->repo
, &set
);
321 for (list
= revs
->commits
; list
; list
= list
->next
) {
322 struct commit
*commit
= list
->item
;
323 if (commit
->object
.flags
& UNINTERESTING
) {
324 mark_tree_uninteresting(revs
->repo
,
325 get_commit_tree(commit
));
326 if (revs
->edge_hint_aggressive
&& !(commit
->object
.flags
& SHOWN
)) {
327 commit
->object
.flags
|= SHOWN
;
332 mark_edge_parents_uninteresting(commit
, revs
, show_edge
);
336 if (revs
->edge_hint_aggressive
) {
337 for (i
= 0; i
< revs
->cmdline
.nr
; i
++) {
338 struct object
*obj
= revs
->cmdline
.rev
[i
].item
;
339 struct commit
*commit
= (struct commit
*)obj
;
340 if (obj
->type
!= OBJ_COMMIT
|| !(obj
->flags
& UNINTERESTING
))
342 mark_tree_uninteresting(revs
->repo
,
343 get_commit_tree(commit
));
344 if (!(obj
->flags
& SHOWN
)) {
352 static void add_pending_tree(struct rev_info
*revs
, struct tree
*tree
)
354 add_pending_object(revs
, &tree
->object
, "");
357 static void traverse_non_commits(struct traversal_context
*ctx
,
362 assert(base
->len
== 0);
364 for (i
= 0; i
< ctx
->revs
->pending
.nr
; i
++) {
365 struct object_array_entry
*pending
= ctx
->revs
->pending
.objects
+ i
;
366 struct object
*obj
= pending
->item
;
367 const char *name
= pending
->name
;
368 const char *path
= pending
->path
;
369 if (obj
->flags
& (UNINTERESTING
| SEEN
))
371 if (obj
->type
== OBJ_TAG
) {
372 process_tag(ctx
, (struct tag
*)obj
, name
);
377 if (obj
->type
== OBJ_TREE
) {
378 process_tree(ctx
, (struct tree
*)obj
, base
, path
);
381 if (obj
->type
== OBJ_BLOB
) {
382 process_blob(ctx
, (struct blob
*)obj
, base
, path
);
385 die("unknown pending object %s (%s)",
386 oid_to_hex(&obj
->oid
), name
);
388 object_array_clear(&ctx
->revs
->pending
);
391 static void do_traverse(struct traversal_context
*ctx
)
393 struct commit
*commit
;
394 struct strbuf csp
; /* callee's scratch pad */
395 strbuf_init(&csp
, PATH_MAX
);
397 while ((commit
= get_revision(ctx
->revs
)) != NULL
) {
398 enum list_objects_filter_result r
;
400 r
= list_objects_filter__filter_object(ctx
->revs
->repo
,
401 LOFS_COMMIT
, &commit
->object
,
402 NULL
, NULL
, ctx
->filter
);
405 * an uninteresting boundary commit may not have its tree
406 * parsed yet, but we are not going to show them anyway
408 if (!ctx
->revs
->tree_objects
)
409 ; /* do not bother loading tree */
410 else if (get_commit_tree(commit
)) {
411 struct tree
*tree
= get_commit_tree(commit
);
412 tree
->object
.flags
|= NOT_USER_GIVEN
;
413 add_pending_tree(ctx
->revs
, tree
);
414 } else if (commit
->object
.parsed
) {
415 die(_("unable to load root tree for commit %s"),
416 oid_to_hex(&commit
->object
.oid
));
419 if (r
& LOFR_MARK_SEEN
)
420 commit
->object
.flags
|= SEEN
;
421 if (r
& LOFR_DO_SHOW
)
422 show_commit(ctx
, commit
);
424 if (ctx
->revs
->tree_blobs_in_commit_order
)
426 * NEEDSWORK: Adding the tree and then flushing it here
427 * needs a reallocation for each commit. Can we pass the
428 * tree directory without allocation churn?
430 traverse_non_commits(ctx
, &csp
);
432 traverse_non_commits(ctx
, &csp
);
433 strbuf_release(&csp
);
436 void traverse_commit_list_filtered(
437 struct rev_info
*revs
,
438 show_commit_fn show_commit
,
439 show_object_fn show_object
,
441 struct oidset
*omitted
)
443 struct traversal_context ctx
= {
445 .show_object
= show_object
,
446 .show_commit
= show_commit
,
447 .show_data
= show_data
,
450 if (revs
->filter
.choice
)
451 ctx
.filter
= list_objects_filter__init(omitted
, &revs
->filter
);
456 list_objects_filter__free(ctx
.filter
);