blob: ab5745bbfe4d2149694eb3f358ea0ce184ad494d [file] [log] [blame]
Junio C Hamanoc64ed702006-09-04 21:50:12 -07001#include "cache.h"
2#include "tag.h"
3#include "commit.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Junio C Hamanoc64ed702006-09-04 21:50:12 -07005#include "tree.h"
6#include "blob.h"
7#include "diff.h"
8#include "tree-walk.h"
9#include "revision.h"
10#include "list-objects.h"
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000011#include "list-objects-filter.h"
12#include "list-objects-filter-options.h"
Jonathan Tandf11e192017-12-08 15:27:15 +000013#include "packfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070014#include "object-store.h"
Matthew DeVore8b10a202018-10-17 17:39:15 -070015#include "trace.h"
Junio C Hamanoc64ed702006-09-04 21:50:12 -070016
Matthew DeVoref447a492018-08-13 11:14:28 -070017struct traversal_context {
18 struct rev_info *revs;
19 show_object_fn show_object;
20 show_commit_fn show_commit;
21 void *show_data;
Matthew DeVore94301472019-06-27 15:54:05 -070022 struct filter *filter;
Matthew DeVoref447a492018-08-13 11:14:28 -070023};
24
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +000025static void show_commit(struct traversal_context *ctx,
26 struct commit *commit)
27{
28 if (!ctx->show_commit)
29 return;
30 ctx->show_commit(commit, ctx->show_data);
31}
32
33static void show_object(struct traversal_context *ctx,
34 struct object *object,
35 const char *name)
36{
37 if (!ctx->show_object)
38 return;
39 ctx->show_object(object, name, ctx->show_data);
40}
41
Matthew DeVoref447a492018-08-13 11:14:28 -070042static void process_blob(struct traversal_context *ctx,
Junio C Hamanoc64ed702006-09-04 21:50:12 -070043 struct blob *blob,
Jeff Kingbd645162016-02-11 17:26:44 -050044 struct strbuf *path,
Matthew DeVoref447a492018-08-13 11:14:28 -070045 const char *name)
Junio C Hamanoc64ed702006-09-04 21:50:12 -070046{
47 struct object *obj = &blob->object;
Jeff Kingde1e67d2016-02-11 17:28:36 -050048 size_t pathlen;
Matthew DeVore94301472019-06-27 15:54:05 -070049 enum list_objects_filter_result r;
Junio C Hamanoc64ed702006-09-04 21:50:12 -070050
Matthew DeVoref447a492018-08-13 11:14:28 -070051 if (!ctx->revs->blob_objects)
Junio C Hamanoc64ed702006-09-04 21:50:12 -070052 return;
Martin Koeglera301b0c2008-02-18 21:47:56 +010053 if (!obj)
54 die("bad blob object");
Junio C Hamanoc64ed702006-09-04 21:50:12 -070055 if (obj->flags & (UNINTERESTING | SEEN))
56 return;
Jeff Kingde1e67d2016-02-11 17:28:36 -050057
Jonathan Tandf11e192017-12-08 15:27:15 +000058 /*
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).
62 *
63 * Note that this "--exclude-promisor-objects" pre-filtering
64 * may cause the actual filter to report an incomplete list
65 * of missing objects.
66 */
Matthew DeVoref447a492018-08-13 11:14:28 -070067 if (ctx->revs->exclude_promisor_objects &&
Jonathan Tandf11e192017-12-08 15:27:15 +000068 !has_object_file(&obj->oid) &&
69 is_promisor_object(&obj->oid))
70 return;
71
Jeff Kingde1e67d2016-02-11 17:28:36 -050072 pathlen = path->len;
73 strbuf_addstr(path, name);
Matthew DeVore94301472019-06-27 15:54:05 -070074 r = list_objects_filter__filter_object(ctx->revs->repo,
75 LOFS_BLOB, obj,
76 path->buf, &path->buf[pathlen],
77 ctx->filter);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000078 if (r & LOFR_MARK_SEEN)
79 obj->flags |= SEEN;
80 if (r & LOFR_DO_SHOW)
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +000081 show_object(ctx, obj, path->buf);
Jeff Kingde1e67d2016-02-11 17:28:36 -050082 strbuf_setlen(path, pathlen);
Junio C Hamanoc64ed702006-09-04 21:50:12 -070083}
84
Matthew DeVoref447a492018-08-13 11:14:28 -070085static void process_tree(struct traversal_context *ctx,
Junio C Hamanoc64ed702006-09-04 21:50:12 -070086 struct tree *tree,
Elijah Newrencc5fa2f2010-12-17 20:26:47 +070087 struct strbuf *base,
Matthew DeVore92024892018-08-13 11:14:29 -070088 const char *name);
89
90static void process_tree_contents(struct traversal_context *ctx,
91 struct tree *tree,
92 struct strbuf *base)
Junio C Hamanoc64ed702006-09-04 21:50:12 -070093{
Junio C Hamanoc64ed702006-09-04 21:50:12 -070094 struct tree_desc desc;
95 struct name_entry entry;
Matthew DeVore92024892018-08-13 11:14:29 -070096 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
97 all_entries_interesting : entry_not_interesting;
98
99 init_tree_desc(&desc, tree->buffer, tree->size);
100
101 while (tree_entry(&desc, &entry)) {
102 if (match != all_entries_interesting) {
Nguyễn Thái Ngọc Duy67022e02018-11-18 17:47:57 +0100103 match = tree_entry_interesting(ctx->revs->repo->index,
104 &entry, base, 0,
Matthew DeVore92024892018-08-13 11:14:29 -0700105 &ctx->revs->diffopt.pathspec);
106 if (match == all_entries_not_interesting)
107 break;
108 if (match == entry_not_interesting)
109 continue;
110 }
111
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700112 if (S_ISDIR(entry.mode)) {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000113 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
Taylor Blaub49e74e2019-04-09 19:13:19 -0700114 if (!t) {
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));
118 }
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700119 t->object.flags |= NOT_USER_GIVEN;
120 process_tree(ctx, t, base, entry.path);
121 }
Matthew DeVore92024892018-08-13 11:14:29 -0700122 else if (S_ISGITLINK(entry.mode))
Jeff King00271482022-12-13 06:12:09 -0500123 ; /* ignore gitlink */
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700124 else {
brian m. carlsonea82b2a2019-01-15 00:39:44 +0000125 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
Taylor Blau23c20442019-04-09 19:13:17 -0700126 if (!b) {
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));
130 }
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700131 b->object.flags |= NOT_USER_GIVEN;
132 process_blob(ctx, b, base, entry.path);
133 }
Matthew DeVore92024892018-08-13 11:14:29 -0700134 }
135}
136
137static void process_tree(struct traversal_context *ctx,
138 struct tree *tree,
139 struct strbuf *base,
Matthew DeVoref447a492018-08-13 11:14:28 -0700140 const char *name)
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700141{
142 struct object *obj = &tree->object;
Matthew DeVoref447a492018-08-13 11:14:28 -0700143 struct rev_info *revs = ctx->revs;
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700144 int baselen = base->len;
Matthew DeVore94301472019-06-27 15:54:05 -0700145 enum list_objects_filter_result r;
Matthew DeVore7c0fe332018-10-05 14:31:23 -0700146 int failed_parse;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700147
148 if (!revs->tree_objects)
149 return;
Martin Koeglera301b0c2008-02-18 21:47:56 +0100150 if (!obj)
151 die("bad tree object");
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700152 if (obj->flags & (UNINTERESTING | SEEN))
153 return;
Jeff Kingaa9ad6f2021-06-14 08:05:44 -0400154 if (revs->include_check_obj &&
155 !revs->include_check_obj(&tree->object, revs->include_check_data))
156 return;
Matthew DeVore7c0fe332018-10-05 14:31:23 -0700157
158 failed_parse = parse_tree_gently(tree, 1);
159 if (failed_parse) {
Vicent Marti2db1a432014-03-28 06:00:43 -0400160 if (revs->ignore_missing_links)
161 return;
Jonathan Tandf11e192017-12-08 15:27:15 +0000162
163 /*
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.
167 */
168 if (revs->exclude_promisor_objects &&
169 is_promisor_object(&obj->oid))
170 return;
171
Matthew DeVore7c0fe332018-10-05 14:31:23 -0700172 if (!revs->do_not_die_on_missing_tree)
173 die("bad tree object %s", oid_to_hex(&obj->oid));
Vicent Marti2db1a432014-03-28 06:00:43 -0400174 }
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700175
Jeff King13528ab2016-02-11 17:26:18 -0500176 strbuf_addstr(base, name);
Matthew DeVore94301472019-06-27 15:54:05 -0700177 r = list_objects_filter__filter_object(ctx->revs->repo,
178 LOFS_BEGIN_TREE, obj,
179 base->buf, &base->buf[baselen],
180 ctx->filter);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000181 if (r & LOFR_MARK_SEEN)
182 obj->flags |= SEEN;
183 if (r & LOFR_DO_SHOW)
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000184 show_object(ctx, obj, base->buf);
Jeff King13528ab2016-02-11 17:26:18 -0500185 if (base->len)
186 strbuf_addch(base, '/');
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700187
Matthew DeVore8b10a202018-10-17 17:39:15 -0700188 if (r & LOFR_SKIP_TREE)
189 trace_printf("Skipping contents of tree %s...\n", base->buf);
190 else if (!failed_parse)
Matthew DeVore7c0fe332018-10-05 14:31:23 -0700191 process_tree_contents(ctx, tree, base);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700192
Matthew DeVore94301472019-06-27 15:54:05 -0700193 r = list_objects_filter__filter_object(ctx->revs->repo,
194 LOFS_END_TREE, obj,
195 base->buf, &base->buf[baselen],
196 ctx->filter);
197 if (r & LOFR_MARK_SEEN)
198 obj->flags |= SEEN;
199 if (r & LOFR_DO_SHOW)
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000200 show_object(ctx, obj, base->buf);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000201
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700202 strbuf_setlen(base, baselen);
Jeff King6e454b92013-06-05 18:37:39 -0400203 free_tree_buffer(tree);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700204}
205
Patrick Steinhardt628d81b2021-04-09 13:28:02 +0200206static void process_tag(struct traversal_context *ctx,
207 struct tag *tag,
208 const char *name)
209{
Patrick Steinhardt9a2a4f92021-04-12 15:37:35 +0200210 enum list_objects_filter_result r;
211
212 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
213 &tag->object, NULL, NULL,
214 ctx->filter);
215 if (r & LOFR_MARK_SEEN)
216 tag->object.flags |= SEEN;
217 if (r & LOFR_DO_SHOW)
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000218 show_object(ctx, &tag->object, name);
Patrick Steinhardt628d81b2021-04-09 13:28:02 +0200219}
220
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700221static void mark_edge_parents_uninteresting(struct commit *commit,
222 struct rev_info *revs,
223 show_edge_fn show_edge)
224{
225 struct commit_list *parents;
226
227 for (parents = commit->parents; parents; parents = parents->next) {
228 struct commit *parent = parents->item;
229 if (!(parent->object.flags & UNINTERESTING))
230 continue;
Nguyễn Thái Ngọc Duyb3c7eef2018-09-21 17:57:39 +0200231 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700232 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
233 parent->object.flags |= SHOWN;
234 show_edge(parent);
235 }
236 }
237}
238
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800239static void add_edge_parents(struct commit *commit,
240 struct rev_info *revs,
241 show_edge_fn show_edge,
242 struct oidset *set)
243{
244 struct commit_list *parents;
245
246 for (parents = commit->parents; parents; parents = parents->next) {
247 struct commit *parent = parents->item;
248 struct tree *tree = get_commit_tree(parent);
249
250 if (!tree)
251 continue;
252
253 oidset_insert(set, &tree->object.oid);
254
255 if (!(parent->object.flags & UNINTERESTING))
256 continue;
257 tree->object.flags |= UNINTERESTING;
258
259 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
260 parent->object.flags |= SHOWN;
261 show_edge(parent);
262 }
263 }
264}
265
266void mark_edges_uninteresting(struct rev_info *revs,
267 show_edge_fn show_edge,
268 int sparse)
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700269{
Nguyễn Thái Ngọc Duye76a5fb2013-08-16 16:52:06 +0700270 struct commit_list *list;
Nguyễn Thái Ngọc Duyfbd4a702013-08-16 16:52:07 +0700271 int i;
272
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800273 if (sparse) {
274 struct oidset set;
275 oidset_init(&set, 16);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700276
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800277 for (list = revs->commits; list; list = list->next) {
278 struct commit *commit = list->item;
279 struct tree *tree = get_commit_tree(commit);
280
281 if (commit->object.flags & UNINTERESTING)
282 tree->object.flags |= UNINTERESTING;
283
284 oidset_insert(&set, &tree->object.oid);
285 add_edge_parents(commit, revs, show_edge, &set);
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700286 }
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800287
288 mark_trees_uninteresting_sparse(revs->repo, &set);
289 oidset_clear(&set);
290 } else {
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;
298 show_edge(commit);
299 }
300 continue;
301 }
302 mark_edge_parents_uninteresting(commit, revs, show_edge);
303 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700304 }
Derrick Stolee4f6d26b2019-01-16 10:25:58 -0800305
brian m. carlson1684c1b2014-12-24 23:05:39 +0000306 if (revs->edge_hint_aggressive) {
Jeff King200abe72014-01-20 21:25:40 -0500307 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))
311 continue;
Nguyễn Thái Ngọc Duyb3c7eef2018-09-21 17:57:39 +0200312 mark_tree_uninteresting(revs->repo,
313 get_commit_tree(commit));
Jeff King200abe72014-01-20 21:25:40 -0500314 if (!(obj->flags & SHOWN)) {
315 obj->flags |= SHOWN;
316 show_edge(commit);
317 }
Nguyễn Thái Ngọc Duyfbd4a702013-08-16 16:52:07 +0700318 }
319 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700320}
321
Linus Torvalds8d2dfc42009-04-10 17:27:58 -0700322static void add_pending_tree(struct rev_info *revs, struct tree *tree)
323{
324 add_pending_object(revs, &tree->object, "");
325}
326
Teng Longb3e36df2021-08-12 16:59:31 +0800327static void traverse_non_commits(struct traversal_context *ctx,
328 struct strbuf *base)
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700329{
330 int i;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700331
Stefan Beller91904f52017-11-02 12:41:43 -0700332 assert(base->len == 0);
333
Matthew DeVoref447a492018-08-13 11:14:28 -0700334 for (i = 0; i < ctx->revs->pending.nr; i++) {
335 struct object_array_entry *pending = ctx->revs->pending.objects + i;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700336 struct object *obj = pending->item;
337 const char *name = pending->name;
Jeff King20739492014-10-15 18:43:19 -0400338 const char *path = pending->path;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700339 if (obj->flags & (UNINTERESTING | SEEN))
340 continue;
341 if (obj->type == OBJ_TAG) {
Patrick Steinhardt628d81b2021-04-09 13:28:02 +0200342 process_tag(ctx, (struct tag *)obj, name);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700343 continue;
344 }
Jeff King20739492014-10-15 18:43:19 -0400345 if (!path)
346 path = "";
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700347 if (obj->type == OBJ_TREE) {
Matthew DeVoref447a492018-08-13 11:14:28 -0700348 process_tree(ctx, (struct tree *)obj, base, path);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700349 continue;
350 }
351 if (obj->type == OBJ_BLOB) {
Matthew DeVoref447a492018-08-13 11:14:28 -0700352 process_blob(ctx, (struct blob *)obj, base, path);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700353 continue;
354 }
355 die("unknown pending object %s (%s)",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000356 oid_to_hex(&obj->oid), name);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700357 }
Matthew DeVoref447a492018-08-13 11:14:28 -0700358 object_array_clear(&ctx->revs->pending);
Stefan Beller91904f52017-11-02 12:41:43 -0700359}
360
Matthew DeVoref447a492018-08-13 11:14:28 -0700361static void do_traverse(struct traversal_context *ctx)
Stefan Beller91904f52017-11-02 12:41:43 -0700362{
363 struct commit *commit;
364 struct strbuf csp; /* callee's scratch pad */
365 strbuf_init(&csp, PATH_MAX);
366
Matthew DeVoref447a492018-08-13 11:14:28 -0700367 while ((commit = get_revision(ctx->revs)) != NULL) {
Patrick Steinhardt9a2a4f92021-04-12 15:37:35 +0200368 enum list_objects_filter_result r;
369
370 r = list_objects_filter__filter_object(ctx->revs->repo,
371 LOFS_COMMIT, &commit->object,
372 NULL, NULL, ctx->filter);
373
Stefan Beller91904f52017-11-02 12:41:43 -0700374 /*
375 * an uninteresting boundary commit may not have its tree
376 * parsed yet, but we are not going to show them anyway
377 */
Jeff King72ed80c2019-09-11 21:11:37 -0400378 if (!ctx->revs->tree_objects)
379 ; /* do not bother loading tree */
380 else if (get_commit_tree(commit)) {
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700381 struct tree *tree = get_commit_tree(commit);
382 tree->object.flags |= NOT_USER_GIVEN;
383 add_pending_tree(ctx->revs, tree);
Jeff King97dd5122019-04-09 19:13:25 -0700384 } else if (commit->object.parsed) {
385 die(_("unable to load root tree for commit %s"),
386 oid_to_hex(&commit->object.oid));
Matthew DeVore99c9aa92018-10-05 14:31:24 -0700387 }
Patrick Steinhardt9a2a4f92021-04-12 15:37:35 +0200388
389 if (r & LOFR_MARK_SEEN)
390 commit->object.flags |= SEEN;
391 if (r & LOFR_DO_SHOW)
Ævar Arnfjörð Bjarmason4f33a632022-03-09 16:01:38 +0000392 show_commit(ctx, commit);
Stefan Bellerce5b6f92017-11-15 18:00:35 -0800393
Matthew DeVoref447a492018-08-13 11:14:28 -0700394 if (ctx->revs->tree_blobs_in_commit_order)
Stefan Bellerce5b6f92017-11-15 18:00:35 -0800395 /*
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?
399 */
Teng Longb3e36df2021-08-12 16:59:31 +0800400 traverse_non_commits(ctx, &csp);
Stefan Beller91904f52017-11-02 12:41:43 -0700401 }
Teng Longb3e36df2021-08-12 16:59:31 +0800402 traverse_non_commits(ctx, &csp);
Stefan Beller91904f52017-11-02 12:41:43 -0700403 strbuf_release(&csp);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700404}
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000405
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000406void traverse_commit_list_filtered(
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000407 struct rev_info *revs,
408 show_commit_fn show_commit,
409 show_object_fn show_object,
410 void *show_data,
411 struct oidset *omitted)
412{
Derrick Stolee3e0370a2022-03-09 16:01:36 +0000413 struct traversal_context ctx = {
414 .revs = revs,
415 .show_object = show_object,
416 .show_commit = show_commit,
417 .show_data = show_data,
418 };
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000419
Derrick Stolee3e0370a2022-03-09 16:01:36 +0000420 if (revs->filter.choice)
421 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
422
Matthew DeVoref447a492018-08-13 11:14:28 -0700423 do_traverse(&ctx);
Derrick Stolee3e0370a2022-03-09 16:01:36 +0000424
425 if (ctx.filter)
426 list_objects_filter__free(ctx.filter);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000427}