]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - list-objects.c
The eighth batch
[thirdparty/git.git] / list-objects.c
... / ...
CommitLineData
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "tag.h"
5#include "commit.h"
6#include "gettext.h"
7#include "hex.h"
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"
14#include "list-objects-filter.h"
15#include "list-objects-filter-options.h"
16#include "packfile.h"
17#include "object-store.h"
18#include "trace.h"
19#include "environment.h"
20
21struct traversal_context {
22 struct rev_info *revs;
23 show_object_fn show_object;
24 show_commit_fn show_commit;
25 void *show_data;
26 struct filter *filter;
27 int depth;
28};
29
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;
44 if (ctx->revs->unpacked && has_object_pack(ctx->revs->repo,
45 &object->oid))
46 return;
47
48 ctx->show_object(object, name, ctx->show_data);
49}
50
51static void process_blob(struct traversal_context *ctx,
52 struct blob *blob,
53 struct strbuf *path,
54 const char *name)
55{
56 struct object *obj = &blob->object;
57 size_t pathlen;
58 enum list_objects_filter_result r;
59
60 if (!ctx->revs->blob_objects)
61 return;
62 if (!obj)
63 die("bad blob object");
64 if (obj->flags & (UNINTERESTING | SEEN))
65 return;
66
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 */
76 if (ctx->revs->exclude_promisor_objects &&
77 !has_object(the_repository, &obj->oid,
78 HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR) &&
79 is_promisor_object(ctx->revs->repo, &obj->oid))
80 return;
81
82 pathlen = path->len;
83 strbuf_addstr(path, name);
84 r = list_objects_filter__filter_object(ctx->revs->repo,
85 LOFS_BLOB, obj,
86 path->buf, &path->buf[pathlen],
87 ctx->filter);
88 if (r & LOFR_MARK_SEEN)
89 obj->flags |= SEEN;
90 if (r & LOFR_DO_SHOW)
91 show_object(ctx, obj, path->buf);
92 strbuf_setlen(path, pathlen);
93}
94
95static void process_tree(struct traversal_context *ctx,
96 struct tree *tree,
97 struct strbuf *base,
98 const char *name);
99
100static void process_tree_contents(struct traversal_context *ctx,
101 struct tree *tree,
102 struct strbuf *base)
103{
104 struct tree_desc desc;
105 struct name_entry entry;
106 enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
107 all_entries_interesting : entry_not_interesting;
108
109 init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
110
111 while (tree_entry(&desc, &entry)) {
112 if (match != all_entries_interesting) {
113 match = tree_entry_interesting(ctx->revs->repo->index,
114 &entry, base,
115 &ctx->revs->diffopt.pathspec);
116 if (match == all_entries_not_interesting)
117 break;
118 if (match == entry_not_interesting)
119 continue;
120 }
121
122 if (S_ISDIR(entry.mode)) {
123 struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
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 }
129 t->object.flags |= NOT_USER_GIVEN;
130 ctx->depth++;
131 process_tree(ctx, t, base, entry.path);
132 ctx->depth--;
133 }
134 else if (S_ISGITLINK(entry.mode))
135 ; /* ignore gitlink */
136 else {
137 struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
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 }
143 b->object.flags |= NOT_USER_GIVEN;
144 process_blob(ctx, b, base, entry.path);
145 }
146 }
147}
148
149static void process_tree(struct traversal_context *ctx,
150 struct tree *tree,
151 struct strbuf *base,
152 const char *name)
153{
154 struct object *obj = &tree->object;
155 struct rev_info *revs = ctx->revs;
156 int baselen = base->len;
157 enum list_objects_filter_result r;
158 int failed_parse;
159
160 if (!revs->tree_objects)
161 return;
162 if (!obj)
163 die("bad tree object");
164 if (obj->flags & (UNINTERESTING | SEEN))
165 return;
166 if (revs->include_check_obj &&
167 !revs->include_check_obj(&tree->object, revs->include_check_data))
168 return;
169
170 if (ctx->depth > max_allowed_tree_depth)
171 die("exceeded maximum allowed tree depth");
172
173 failed_parse = parse_tree_gently(tree, 1);
174 if (failed_parse) {
175 if (revs->ignore_missing_links)
176 return;
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 &&
184 is_promisor_object(revs->repo, &obj->oid))
185 return;
186
187 if (!revs->do_not_die_on_missing_objects)
188 die("bad tree object %s", oid_to_hex(&obj->oid));
189 }
190
191 strbuf_addstr(base, name);
192 r = list_objects_filter__filter_object(ctx->revs->repo,
193 LOFS_BEGIN_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)
199 show_object(ctx, obj, base->buf);
200 if (base->len)
201 strbuf_addch(base, '/');
202
203 if (r & LOFR_SKIP_TREE)
204 trace_printf("Skipping contents of tree %s...\n", base->buf);
205 else if (!failed_parse)
206 process_tree_contents(ctx, tree, base);
207
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)
215 show_object(ctx, obj, base->buf);
216
217 strbuf_setlen(base, baselen);
218 free_tree_buffer(tree);
219}
220
221static void process_tag(struct traversal_context *ctx,
222 struct tag *tag,
223 const char *name)
224{
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)
233 show_object(ctx, &tag->object, name);
234}
235
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;
246 mark_tree_uninteresting(revs->repo,
247 repo_get_commit_tree(the_repository, parent));
248 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
249 parent->object.flags |= SHOWN;
250 show_edge(parent);
251 }
252 }
253}
254
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;
264 struct tree *tree = repo_get_commit_tree(the_repository,
265 parent);
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)
286{
287 struct commit_list *list;
288
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;
295 struct tree *tree = repo_get_commit_tree(the_repository,
296 commit);
297
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,
312 repo_get_commit_tree(the_repository, commit));
313 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
314 commit->object.flags |= SHOWN;
315 show_edge(commit);
316 }
317 continue;
318 }
319 mark_edge_parents_uninteresting(commit, revs, show_edge);
320 }
321 }
322
323 if (revs->edge_hint_aggressive) {
324 for (size_t i = 0; i < revs->cmdline.nr; i++) {
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;
329 mark_tree_uninteresting(revs->repo,
330 repo_get_commit_tree(the_repository, commit));
331 if (!(obj->flags & SHOWN)) {
332 obj->flags |= SHOWN;
333 show_edge(commit);
334 }
335 }
336 }
337}
338
339static void add_pending_tree(struct rev_info *revs, struct tree *tree)
340{
341 add_pending_object(revs, &tree->object, "");
342}
343
344static void traverse_non_commits(struct traversal_context *ctx,
345 struct strbuf *base)
346{
347 assert(base->len == 0);
348
349 for (size_t i = 0; i < ctx->revs->pending.nr; i++) {
350 struct object_array_entry *pending = ctx->revs->pending.objects + i;
351 struct object *obj = pending->item;
352 const char *name = pending->name;
353 const char *path = pending->path;
354 if (obj->flags & (UNINTERESTING | SEEN))
355 continue;
356 if (obj->type == OBJ_TAG) {
357 process_tag(ctx, (struct tag *)obj, name);
358 continue;
359 }
360 if (!path)
361 path = "";
362 if (obj->type == OBJ_TREE) {
363 ctx->depth = 0;
364 process_tree(ctx, (struct tree *)obj, base, path);
365 continue;
366 }
367 if (obj->type == OBJ_BLOB) {
368 process_blob(ctx, (struct blob *)obj, base, path);
369 continue;
370 }
371 die("unknown pending object %s (%s)",
372 oid_to_hex(&obj->oid), name);
373 }
374 object_array_clear(&ctx->revs->pending);
375}
376
377static void do_traverse(struct traversal_context *ctx)
378{
379 struct commit *commit;
380 struct strbuf csp; /* callee's scratch pad */
381 strbuf_init(&csp, PATH_MAX);
382
383 while ((commit = get_revision(ctx->revs)) != NULL) {
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
390 /*
391 * an uninteresting boundary commit may not have its tree
392 * parsed yet, but we are not going to show them anyway
393 */
394 if (!ctx->revs->tree_objects)
395 ; /* do not bother loading tree */
396 else if (ctx->revs->do_not_die_on_missing_objects &&
397 oidset_contains(&ctx->revs->missing_commits, &commit->object.oid))
398 ;
399 else if (repo_get_commit_tree(the_repository, commit)) {
400 struct tree *tree = repo_get_commit_tree(the_repository,
401 commit);
402 tree->object.flags |= NOT_USER_GIVEN;
403 add_pending_tree(ctx->revs, tree);
404 } else if (commit->object.parsed) {
405 die(_("unable to load root tree for commit %s"),
406 oid_to_hex(&commit->object.oid));
407 }
408
409 if (r & LOFR_MARK_SEEN)
410 commit->object.flags |= SEEN;
411 if (r & LOFR_DO_SHOW)
412 show_commit(ctx, commit);
413
414 if (ctx->revs->tree_blobs_in_commit_order)
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 */
420 traverse_non_commits(ctx, &csp);
421 }
422 traverse_non_commits(ctx, &csp);
423 strbuf_release(&csp);
424}
425
426void traverse_commit_list_filtered(
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{
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);
442
443 do_traverse(&ctx);
444
445 if (ctx.filter)
446 list_objects_filter__free(ctx.filter);
447}