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