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