]> git.ipfire.org Git - thirdparty/git.git/blame - list-objects.c
Merge branch 'jc/tree-walk-drop-base-offset'
[thirdparty/git.git] / list-objects.c
CommitLineData
d812c3b6 1#include "git-compat-util.h"
c64ed70d
JH
2#include "tag.h"
3#include "commit.h"
f394e093 4#include "gettext.h"
41771fa4 5#include "hex.h"
c64ed70d
JH
6#include "tree.h"
7#include "blob.h"
8#include "diff.h"
9#include "tree-walk.h"
10#include "revision.h"
11#include "list-objects.h"
25ec7bca
JH
12#include "list-objects-filter.h"
13#include "list-objects-filter-options.h"
df11e196 14#include "packfile.h"
a034e910 15#include "object-store-ll.h"
8b10a206 16#include "trace.h"
c64ed70d 17
f447a499
MD
18struct traversal_context {
19 struct rev_info *revs;
20 show_object_fn show_object;
21 show_commit_fn show_commit;
22 void *show_data;
9430147c 23 struct filter *filter;
f447a499
MD
24};
25
4f33a634
ÆAB
26static void show_commit(struct traversal_context *ctx,
27 struct commit *commit)
28{
29 if (!ctx->show_commit)
30 return;
31 ctx->show_commit(commit, ctx->show_data);
32}
33
34static void show_object(struct traversal_context *ctx,
35 struct object *object,
36 const char *name)
37{
38 if (!ctx->show_object)
39 return;
40 ctx->show_object(object, name, ctx->show_data);
41}
42
f447a499 43static void process_blob(struct traversal_context *ctx,
c64ed70d 44 struct blob *blob,
bd64516a 45 struct strbuf *path,
f447a499 46 const char *name)
c64ed70d
JH
47{
48 struct object *obj = &blob->object;
de1e67d0 49 size_t pathlen;
9430147c 50 enum list_objects_filter_result r;
c64ed70d 51
f447a499 52 if (!ctx->revs->blob_objects)
c64ed70d 53 return;
a301b0c8
MK
54 if (!obj)
55 die("bad blob object");
c64ed70d
JH
56 if (obj->flags & (UNINTERESTING | SEEN))
57 return;
de1e67d0 58
df11e196
JT
59 /*
60 * Pre-filter known-missing objects when explicitly requested.
61 * Otherwise, a missing object error message may be reported
62 * later (depending on other filtering criteria).
63 *
64 * Note that this "--exclude-promisor-objects" pre-filtering
65 * may cause the actual filter to report an incomplete list
66 * of missing objects.
67 */
f447a499 68 if (ctx->revs->exclude_promisor_objects &&
bc726bd0 69 !repo_has_object_file(the_repository, &obj->oid) &&
df11e196
JT
70 is_promisor_object(&obj->oid))
71 return;
72
de1e67d0
JK
73 pathlen = path->len;
74 strbuf_addstr(path, name);
9430147c
MD
75 r = list_objects_filter__filter_object(ctx->revs->repo,
76 LOFS_BLOB, obj,
77 path->buf, &path->buf[pathlen],
78 ctx->filter);
25ec7bca
JH
79 if (r & LOFR_MARK_SEEN)
80 obj->flags |= SEEN;
81 if (r & LOFR_DO_SHOW)
4f33a634 82 show_object(ctx, obj, path->buf);
de1e67d0 83 strbuf_setlen(path, pathlen);
c64ed70d
JH
84}
85
92024891 86static void process_tree(struct traversal_context *ctx,
c64ed70d 87 struct tree *tree,
cc5fa2fd 88 struct strbuf *base,
92024891
MD
89 const char *name);
90
91static void process_tree_contents(struct traversal_context *ctx,
92 struct tree *tree,
93 struct strbuf *base)
c64ed70d 94{
c64ed70d
JH
95 struct tree_desc desc;
96 struct name_entry entry;
92024891
MD
97 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
98 all_entries_interesting : entry_not_interesting;
99
100 init_tree_desc(&desc, tree->buffer, tree->size);
101
102 while (tree_entry(&desc, &entry)) {
103 if (match != all_entries_interesting) {
67022e02 104 match = tree_entry_interesting(ctx->revs->repo->index,
0ad927e9 105 &entry, base,
92024891
MD
106 &ctx->revs->diffopt.pathspec);
107 if (match == all_entries_not_interesting)
108 break;
109 if (match == entry_not_interesting)
110 continue;
111 }
112
99c9aa95 113 if (S_ISDIR(entry.mode)) {
ea82b2a0 114 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
b49e74ea
TB
115 if (!t) {
116 die(_("entry '%s' in tree %s has tree mode, "
117 "but is not a tree"),
118 entry.path, oid_to_hex(&tree->object.oid));
119 }
99c9aa95
MD
120 t->object.flags |= NOT_USER_GIVEN;
121 process_tree(ctx, t, base, entry.path);
122 }
92024891 123 else if (S_ISGITLINK(entry.mode))
00271485 124 ; /* ignore gitlink */
99c9aa95 125 else {
ea82b2a0 126 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
23c20445
TB
127 if (!b) {
128 die(_("entry '%s' in tree %s has blob mode, "
129 "but is not a blob"),
130 entry.path, oid_to_hex(&tree->object.oid));
131 }
99c9aa95
MD
132 b->object.flags |= NOT_USER_GIVEN;
133 process_blob(ctx, b, base, entry.path);
134 }
92024891
MD
135 }
136}
137
f447a499 138static void process_tree(struct traversal_context *ctx,
c64ed70d 139 struct tree *tree,
cc5fa2fd 140 struct strbuf *base,
f447a499 141 const char *name)
c64ed70d
JH
142{
143 struct object *obj = &tree->object;
f447a499 144 struct rev_info *revs = ctx->revs;
cc5fa2fd 145 int baselen = base->len;
9430147c 146 enum list_objects_filter_result r;
7c0fe330 147 int failed_parse;
c64ed70d
JH
148
149 if (!revs->tree_objects)
150 return;
a301b0c8
MK
151 if (!obj)
152 die("bad tree object");
c64ed70d
JH
153 if (obj->flags & (UNINTERESTING | SEEN))
154 return;
aa9ad6fe
JK
155 if (revs->include_check_obj &&
156 !revs->include_check_obj(&tree->object, revs->include_check_data))
157 return;
7c0fe330
MD
158
159 failed_parse = parse_tree_gently(tree, 1);
160 if (failed_parse) {
2db1a43f
VM
161 if (revs->ignore_missing_links)
162 return;
df11e196
JT
163
164 /*
165 * Pre-filter known-missing tree objects when explicitly
166 * requested. This may cause the actual filter to report
167 * an incomplete list of missing objects.
168 */
169 if (revs->exclude_promisor_objects &&
170 is_promisor_object(&obj->oid))
171 return;
172
7c0fe330
MD
173 if (!revs->do_not_die_on_missing_tree)
174 die("bad tree object %s", oid_to_hex(&obj->oid));
2db1a43f 175 }
13528ab3 176
13528ab3 177 strbuf_addstr(base, name);
9430147c
MD
178 r = list_objects_filter__filter_object(ctx->revs->repo,
179 LOFS_BEGIN_TREE, obj,
180 base->buf, &base->buf[baselen],
181 ctx->filter);
25ec7bca
JH
182 if (r & LOFR_MARK_SEEN)
183 obj->flags |= SEEN;
184 if (r & LOFR_DO_SHOW)
4f33a634 185 show_object(ctx, obj, base->buf);
13528ab3
JK
186 if (base->len)
187 strbuf_addch(base, '/');
cc5fa2fd 188
8b10a206
MD
189 if (r & LOFR_SKIP_TREE)
190 trace_printf("Skipping contents of tree %s...\n", base->buf);
191 else if (!failed_parse)
7c0fe330 192 process_tree_contents(ctx, tree, base);
c64ed70d 193
9430147c
MD
194 r = list_objects_filter__filter_object(ctx->revs->repo,
195 LOFS_END_TREE, obj,
196 base->buf, &base->buf[baselen],
197 ctx->filter);
198 if (r & LOFR_MARK_SEEN)
199 obj->flags |= SEEN;
200 if (r & LOFR_DO_SHOW)
4f33a634 201 show_object(ctx, obj, base->buf);
25ec7bca 202
cc5fa2fd 203 strbuf_setlen(base, baselen);
6e454b9a 204 free_tree_buffer(tree);
c64ed70d
JH
205}
206
628d81be
PS
207static void process_tag(struct traversal_context *ctx,
208 struct tag *tag,
209 const char *name)
210{
9a2a4f95
PS
211 enum list_objects_filter_result r;
212
213 r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
214 &tag->object, NULL, NULL,
215 ctx->filter);
216 if (r & LOFR_MARK_SEEN)
217 tag->object.flags |= SEEN;
218 if (r & LOFR_DO_SHOW)
4f33a634 219 show_object(ctx, &tag->object, name);
628d81be
PS
220}
221
8d1d8f83
JH
222static void mark_edge_parents_uninteresting(struct commit *commit,
223 struct rev_info *revs,
224 show_edge_fn show_edge)
225{
226 struct commit_list *parents;
227
228 for (parents = commit->parents; parents; parents = parents->next) {
229 struct commit *parent = parents->item;
230 if (!(parent->object.flags & UNINTERESTING))
231 continue;
ecb5091f
ÆAB
232 mark_tree_uninteresting(revs->repo,
233 repo_get_commit_tree(the_repository, parent));
8d1d8f83
JH
234 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
235 parent->object.flags |= SHOWN;
236 show_edge(parent);
237 }
238 }
239}
240
4f6d26b1
DS
241static void add_edge_parents(struct commit *commit,
242 struct rev_info *revs,
243 show_edge_fn show_edge,
244 struct oidset *set)
245{
246 struct commit_list *parents;
247
248 for (parents = commit->parents; parents; parents = parents->next) {
249 struct commit *parent = parents->item;
ecb5091f
ÆAB
250 struct tree *tree = repo_get_commit_tree(the_repository,
251 parent);
4f6d26b1
DS
252
253 if (!tree)
254 continue;
255
256 oidset_insert(set, &tree->object.oid);
257
258 if (!(parent->object.flags & UNINTERESTING))
259 continue;
260 tree->object.flags |= UNINTERESTING;
261
262 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
263 parent->object.flags |= SHOWN;
264 show_edge(parent);
265 }
266 }
267}
268
269void mark_edges_uninteresting(struct rev_info *revs,
270 show_edge_fn show_edge,
271 int sparse)
8d1d8f83 272{
e76a5fb4 273 struct commit_list *list;
fbd4a703
NTND
274 int i;
275
4f6d26b1
DS
276 if (sparse) {
277 struct oidset set;
278 oidset_init(&set, 16);
279
280 for (list = revs->commits; list; list = list->next) {
281 struct commit *commit = list->item;
ecb5091f
ÆAB
282 struct tree *tree = repo_get_commit_tree(the_repository,
283 commit);
8d1d8f83 284
4f6d26b1
DS
285 if (commit->object.flags & UNINTERESTING)
286 tree->object.flags |= UNINTERESTING;
287
288 oidset_insert(&set, &tree->object.oid);
289 add_edge_parents(commit, revs, show_edge, &set);
290 }
291
292 mark_trees_uninteresting_sparse(revs->repo, &set);
293 oidset_clear(&set);
294 } else {
295 for (list = revs->commits; list; list = list->next) {
296 struct commit *commit = list->item;
297 if (commit->object.flags & UNINTERESTING) {
298 mark_tree_uninteresting(revs->repo,
ecb5091f 299 repo_get_commit_tree(the_repository, commit));
4f6d26b1
DS
300 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
301 commit->object.flags |= SHOWN;
302 show_edge(commit);
303 }
304 continue;
fbd4a703 305 }
4f6d26b1 306 mark_edge_parents_uninteresting(commit, revs, show_edge);
8d1d8f83 307 }
8d1d8f83 308 }
4f6d26b1 309
1684c1b2 310 if (revs->edge_hint_aggressive) {
200abe74
JK
311 for (i = 0; i < revs->cmdline.nr; i++) {
312 struct object *obj = revs->cmdline.rev[i].item;
313 struct commit *commit = (struct commit *)obj;
314 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
315 continue;
b3c7eef9 316 mark_tree_uninteresting(revs->repo,
ecb5091f 317 repo_get_commit_tree(the_repository, commit));
200abe74
JK
318 if (!(obj->flags & SHOWN)) {
319 obj->flags |= SHOWN;
320 show_edge(commit);
321 }
fbd4a703
NTND
322 }
323 }
8d1d8f83
JH
324}
325
8d2dfc49
LT
326static void add_pending_tree(struct rev_info *revs, struct tree *tree)
327{
328 add_pending_object(revs, &tree->object, "");
329}
330
b3e36df0
TL
331static void traverse_non_commits(struct traversal_context *ctx,
332 struct strbuf *base)
c64ed70d
JH
333{
334 int i;
c64ed70d 335
91904f56
SB
336 assert(base->len == 0);
337
f447a499
MD
338 for (i = 0; i < ctx->revs->pending.nr; i++) {
339 struct object_array_entry *pending = ctx->revs->pending.objects + i;
c64ed70d
JH
340 struct object *obj = pending->item;
341 const char *name = pending->name;
20739490 342 const char *path = pending->path;
c64ed70d
JH
343 if (obj->flags & (UNINTERESTING | SEEN))
344 continue;
345 if (obj->type == OBJ_TAG) {
628d81be 346 process_tag(ctx, (struct tag *)obj, name);
c64ed70d
JH
347 continue;
348 }
20739490
JK
349 if (!path)
350 path = "";
c64ed70d 351 if (obj->type == OBJ_TREE) {
f447a499 352 process_tree(ctx, (struct tree *)obj, base, path);
c64ed70d
JH
353 continue;
354 }
355 if (obj->type == OBJ_BLOB) {
f447a499 356 process_blob(ctx, (struct blob *)obj, base, path);
c64ed70d
JH
357 continue;
358 }
359 die("unknown pending object %s (%s)",
f2fd0760 360 oid_to_hex(&obj->oid), name);
c64ed70d 361 }
f447a499 362 object_array_clear(&ctx->revs->pending);
91904f56
SB
363}
364
f447a499 365static void do_traverse(struct traversal_context *ctx)
91904f56
SB
366{
367 struct commit *commit;
368 struct strbuf csp; /* callee's scratch pad */
369 strbuf_init(&csp, PATH_MAX);
370
f447a499 371 while ((commit = get_revision(ctx->revs)) != NULL) {
9a2a4f95
PS
372 enum list_objects_filter_result r;
373
374 r = list_objects_filter__filter_object(ctx->revs->repo,
375 LOFS_COMMIT, &commit->object,
376 NULL, NULL, ctx->filter);
377
91904f56
SB
378 /*
379 * an uninteresting boundary commit may not have its tree
380 * parsed yet, but we are not going to show them anyway
381 */
72ed80c7
JK
382 if (!ctx->revs->tree_objects)
383 ; /* do not bother loading tree */
ecb5091f
ÆAB
384 else if (repo_get_commit_tree(the_repository, commit)) {
385 struct tree *tree = repo_get_commit_tree(the_repository,
386 commit);
99c9aa95
MD
387 tree->object.flags |= NOT_USER_GIVEN;
388 add_pending_tree(ctx->revs, tree);
97dd512a
JK
389 } else if (commit->object.parsed) {
390 die(_("unable to load root tree for commit %s"),
391 oid_to_hex(&commit->object.oid));
99c9aa95 392 }
9a2a4f95
PS
393
394 if (r & LOFR_MARK_SEEN)
395 commit->object.flags |= SEEN;
396 if (r & LOFR_DO_SHOW)
4f33a634 397 show_commit(ctx, commit);
ce5b6f9b 398
f447a499 399 if (ctx->revs->tree_blobs_in_commit_order)
ce5b6f9b
SB
400 /*
401 * NEEDSWORK: Adding the tree and then flushing it here
402 * needs a reallocation for each commit. Can we pass the
403 * tree directory without allocation churn?
404 */
b3e36df0 405 traverse_non_commits(ctx, &csp);
91904f56 406 }
b3e36df0 407 traverse_non_commits(ctx, &csp);
91904f56 408 strbuf_release(&csp);
c64ed70d 409}
25ec7bca 410
25ec7bca 411void traverse_commit_list_filtered(
25ec7bca
JH
412 struct rev_info *revs,
413 show_commit_fn show_commit,
414 show_object_fn show_object,
415 void *show_data,
416 struct oidset *omitted)
417{
3e0370a8
DS
418 struct traversal_context ctx = {
419 .revs = revs,
420 .show_object = show_object,
421 .show_commit = show_commit,
422 .show_data = show_data,
423 };
424
425 if (revs->filter.choice)
426 ctx.filter = list_objects_filter__init(omitted, &revs->filter);
f447a499 427
f447a499 428 do_traverse(&ctx);
3e0370a8
DS
429
430 if (ctx.filter)
431 list_objects_filter__free(ctx.filter);
25ec7bca 432}