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