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