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