Merge branch 'ds/commit-graph-write-refactor'
[git] / list-objects.c
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"
10 #include "list-objects-filter.h"
11 #include "list-objects-filter-options.h"
12 #include "packfile.h"
13 #include "object-store.h"
14 #include "trace.h"
15
16 struct traversal_context {
17         struct rev_info *revs;
18         show_object_fn show_object;
19         show_commit_fn show_commit;
20         void *show_data;
21         filter_object_fn filter_fn;
22         void *filter_data;
23 };
24
25 static void process_blob(struct traversal_context *ctx,
26                          struct blob *blob,
27                          struct strbuf *path,
28                          const char *name)
29 {
30         struct object *obj = &blob->object;
31         size_t pathlen;
32         enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
33
34         if (!ctx->revs->blob_objects)
35                 return;
36         if (!obj)
37                 die("bad blob object");
38         if (obj->flags & (UNINTERESTING | SEEN))
39                 return;
40
41         /*
42          * Pre-filter known-missing objects when explicitly requested.
43          * Otherwise, a missing object error message may be reported
44          * later (depending on other filtering criteria).
45          *
46          * Note that this "--exclude-promisor-objects" pre-filtering
47          * may cause the actual filter to report an incomplete list
48          * of missing objects.
49          */
50         if (ctx->revs->exclude_promisor_objects &&
51             !has_object_file(&obj->oid) &&
52             is_promisor_object(&obj->oid))
53                 return;
54
55         pathlen = path->len;
56         strbuf_addstr(path, name);
57         if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn)
58                 r = ctx->filter_fn(ctx->revs->repo,
59                                    LOFS_BLOB, obj,
60                                    path->buf, &path->buf[pathlen],
61                                    ctx->filter_data);
62         if (r & LOFR_MARK_SEEN)
63                 obj->flags |= SEEN;
64         if (r & LOFR_DO_SHOW)
65                 ctx->show_object(obj, path->buf, ctx->show_data);
66         strbuf_setlen(path, pathlen);
67 }
68
69 /*
70  * Processing a gitlink entry currently does nothing, since
71  * we do not recurse into the subproject.
72  *
73  * We *could* eventually add a flag that actually does that,
74  * which would involve:
75  *  - is the subproject actually checked out?
76  *  - if so, see if the subproject has already been added
77  *    to the alternates list, and add it if not.
78  *  - process the commit (or tag) the gitlink points to
79  *    recursively.
80  *
81  * However, it's unclear whether there is really ever any
82  * reason to see superprojects and subprojects as such a
83  * "unified" object pool (potentially resulting in a totally
84  * humongous pack - avoiding which was the whole point of
85  * having gitlinks in the first place!).
86  *
87  * So for now, there is just a note that we *could* follow
88  * the link, and how to do it. Whether it necessarily makes
89  * any sense what-so-ever to ever do that is another issue.
90  */
91 static void process_gitlink(struct traversal_context *ctx,
92                             const unsigned char *sha1,
93                             struct strbuf *path,
94                             const char *name)
95 {
96         /* Nothing to do */
97 }
98
99 static void process_tree(struct traversal_context *ctx,
100                          struct tree *tree,
101                          struct strbuf *base,
102                          const char *name);
103
104 static void process_tree_contents(struct traversal_context *ctx,
105                                   struct tree *tree,
106                                   struct strbuf *base)
107 {
108         struct tree_desc desc;
109         struct name_entry entry;
110         enum interesting match = ctx->revs->diffopt.pathspec.nr == 0 ?
111                 all_entries_interesting : entry_not_interesting;
112
113         init_tree_desc(&desc, tree->buffer, tree->size);
114
115         while (tree_entry(&desc, &entry)) {
116                 if (match != all_entries_interesting) {
117                         match = tree_entry_interesting(ctx->revs->repo->index,
118                                                        &entry, base, 0,
119                                                        &ctx->revs->diffopt.pathspec);
120                         if (match == all_entries_not_interesting)
121                                 break;
122                         if (match == entry_not_interesting)
123                                 continue;
124                 }
125
126                 if (S_ISDIR(entry.mode)) {
127                         struct tree *t = lookup_tree(ctx->revs->repo, &entry.oid);
128                         if (!t) {
129                                 die(_("entry '%s' in tree %s has tree mode, "
130                                       "but is not a tree"),
131                                     entry.path, oid_to_hex(&tree->object.oid));
132                         }
133                         t->object.flags |= NOT_USER_GIVEN;
134                         process_tree(ctx, t, base, entry.path);
135                 }
136                 else if (S_ISGITLINK(entry.mode))
137                         process_gitlink(ctx, entry.oid.hash,
138                                         base, entry.path);
139                 else {
140                         struct blob *b = lookup_blob(ctx->revs->repo, &entry.oid);
141                         if (!b) {
142                                 die(_("entry '%s' in tree %s has blob mode, "
143                                       "but is not a blob"),
144                                     entry.path, oid_to_hex(&tree->object.oid));
145                         }
146                         b->object.flags |= NOT_USER_GIVEN;
147                         process_blob(ctx, b, base, entry.path);
148                 }
149         }
150 }
151
152 static void process_tree(struct traversal_context *ctx,
153                          struct tree *tree,
154                          struct strbuf *base,
155                          const char *name)
156 {
157         struct object *obj = &tree->object;
158         struct rev_info *revs = ctx->revs;
159         int baselen = base->len;
160         enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
161         int failed_parse;
162
163         if (!revs->tree_objects)
164                 return;
165         if (!obj)
166                 die("bad tree object");
167         if (obj->flags & (UNINTERESTING | SEEN))
168                 return;
169
170         failed_parse = parse_tree_gently(tree, 1);
171         if (failed_parse) {
172                 if (revs->ignore_missing_links)
173                         return;
174
175                 /*
176                  * Pre-filter known-missing tree objects when explicitly
177                  * requested.  This may cause the actual filter to report
178                  * an incomplete list of missing objects.
179                  */
180                 if (revs->exclude_promisor_objects &&
181                     is_promisor_object(&obj->oid))
182                         return;
183
184                 if (!revs->do_not_die_on_missing_tree)
185                         die("bad tree object %s", oid_to_hex(&obj->oid));
186         }
187
188         strbuf_addstr(base, name);
189         if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn)
190                 r = ctx->filter_fn(ctx->revs->repo,
191                                    LOFS_BEGIN_TREE, obj,
192                                    base->buf, &base->buf[baselen],
193                                    ctx->filter_data);
194         if (r & LOFR_MARK_SEEN)
195                 obj->flags |= SEEN;
196         if (r & LOFR_DO_SHOW)
197                 ctx->show_object(obj, base->buf, ctx->show_data);
198         if (base->len)
199                 strbuf_addch(base, '/');
200
201         if (r & LOFR_SKIP_TREE)
202                 trace_printf("Skipping contents of tree %s...\n", base->buf);
203         else if (!failed_parse)
204                 process_tree_contents(ctx, tree, base);
205
206         if ((obj->flags & NOT_USER_GIVEN) && ctx->filter_fn) {
207                 r = ctx->filter_fn(ctx->revs->repo,
208                                    LOFS_END_TREE, obj,
209                                    base->buf, &base->buf[baselen],
210                                    ctx->filter_data);
211                 if (r & LOFR_MARK_SEEN)
212                         obj->flags |= SEEN;
213                 if (r & LOFR_DO_SHOW)
214                         ctx->show_object(obj, base->buf, ctx->show_data);
215         }
216
217         strbuf_setlen(base, baselen);
218         free_tree_buffer(tree);
219 }
220
221 static 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;
231                 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
232                 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
233                         parent->object.flags |= SHOWN;
234                         show_edge(parent);
235                 }
236         }
237 }
238
239 static 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
266 void mark_edges_uninteresting(struct rev_info *revs,
267                               show_edge_fn show_edge,
268                               int sparse)
269 {
270         struct commit_list *list;
271         int i;
272
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);
280
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;
301                         }
302                         mark_edge_parents_uninteresting(commit, revs, show_edge);
303                 }
304         }
305
306         if (revs->edge_hint_aggressive) {
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;
312                         mark_tree_uninteresting(revs->repo,
313                                                 get_commit_tree(commit));
314                         if (!(obj->flags & SHOWN)) {
315                                 obj->flags |= SHOWN;
316                                 show_edge(commit);
317                         }
318                 }
319         }
320 }
321
322 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
323 {
324         add_pending_object(revs, &tree->object, "");
325 }
326
327 static void traverse_trees_and_blobs(struct traversal_context *ctx,
328                                      struct strbuf *base)
329 {
330         int i;
331
332         assert(base->len == 0);
333
334         for (i = 0; i < ctx->revs->pending.nr; i++) {
335                 struct object_array_entry *pending = ctx->revs->pending.objects + i;
336                 struct object *obj = pending->item;
337                 const char *name = pending->name;
338                 const char *path = pending->path;
339                 if (obj->flags & (UNINTERESTING | SEEN))
340                         continue;
341                 if (obj->type == OBJ_TAG) {
342                         obj->flags |= SEEN;
343                         ctx->show_object(obj, name, ctx->show_data);
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                 /*
370                  * an uninteresting boundary commit may not have its tree
371                  * parsed yet, but we are not going to show them anyway
372                  */
373                 if (get_commit_tree(commit)) {
374                         struct tree *tree = get_commit_tree(commit);
375                         tree->object.flags |= NOT_USER_GIVEN;
376                         add_pending_tree(ctx->revs, tree);
377                 } else if (commit->object.parsed) {
378                         die(_("unable to load root tree for commit %s"),
379                               oid_to_hex(&commit->object.oid));
380                 }
381                 ctx->show_commit(commit, ctx->show_data);
382
383                 if (ctx->revs->tree_blobs_in_commit_order)
384                         /*
385                          * NEEDSWORK: Adding the tree and then flushing it here
386                          * needs a reallocation for each commit. Can we pass the
387                          * tree directory without allocation churn?
388                          */
389                         traverse_trees_and_blobs(ctx, &csp);
390         }
391         traverse_trees_and_blobs(ctx, &csp);
392         strbuf_release(&csp);
393 }
394
395 void traverse_commit_list(struct rev_info *revs,
396                           show_commit_fn show_commit,
397                           show_object_fn show_object,
398                           void *show_data)
399 {
400         struct traversal_context ctx;
401         ctx.revs = revs;
402         ctx.show_commit = show_commit;
403         ctx.show_object = show_object;
404         ctx.show_data = show_data;
405         ctx.filter_fn = NULL;
406         ctx.filter_data = NULL;
407         do_traverse(&ctx);
408 }
409
410 void traverse_commit_list_filtered(
411         struct list_objects_filter_options *filter_options,
412         struct rev_info *revs,
413         show_commit_fn show_commit,
414         show_object_fn show_object,
415         void *show_data,
416         struct oidset *omitted)
417 {
418         struct traversal_context ctx;
419         filter_free_fn filter_free_fn = NULL;
420
421         ctx.revs = revs;
422         ctx.show_object = show_object;
423         ctx.show_commit = show_commit;
424         ctx.show_data = show_data;
425         ctx.filter_fn = NULL;
426
427         ctx.filter_data = list_objects_filter__init(omitted, filter_options,
428                                                     &ctx.filter_fn, &filter_free_fn);
429         do_traverse(&ctx);
430         if (ctx.filter_data && filter_free_fn)
431                 filter_free_fn(ctx.filter_data);
432 }