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