blob: 0c2989d5ca7cc4df523376dce94829b1b6be9110 [file] [log] [blame]
Junio C Hamanoc64ed702006-09-04 21:50:12 -07001#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"
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000010#include "list-objects-filter.h"
11#include "list-objects-filter-options.h"
Jonathan Tandf11e192017-12-08 15:27:15 +000012#include "packfile.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070013#include "object-store.h"
Junio C Hamanoc64ed702006-09-04 21:50:12 -070014
15static void process_blob(struct rev_info *revs,
16 struct blob *blob,
Linus Torvalds8d2dfc42009-04-10 17:27:58 -070017 show_object_fn show,
Jeff Kingbd645162016-02-11 17:26:44 -050018 struct strbuf *path,
Junio C Hamano49473672011-09-01 15:43:33 -070019 const char *name,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000020 void *cb_data,
21 filter_object_fn filter_fn,
22 void *filter_data)
Junio C Hamanoc64ed702006-09-04 21:50:12 -070023{
24 struct object *obj = &blob->object;
Jeff Kingde1e67d2016-02-11 17:28:36 -050025 size_t pathlen;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000026 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
Junio C Hamanoc64ed702006-09-04 21:50:12 -070027
28 if (!revs->blob_objects)
29 return;
Martin Koeglera301b0c2008-02-18 21:47:56 +010030 if (!obj)
31 die("bad blob object");
Junio C Hamanoc64ed702006-09-04 21:50:12 -070032 if (obj->flags & (UNINTERESTING | SEEN))
33 return;
Jeff Kingde1e67d2016-02-11 17:28:36 -050034
Jonathan Tandf11e192017-12-08 15:27:15 +000035 /*
36 * Pre-filter known-missing objects when explicitly requested.
37 * Otherwise, a missing object error message may be reported
38 * later (depending on other filtering criteria).
39 *
40 * Note that this "--exclude-promisor-objects" pre-filtering
41 * may cause the actual filter to report an incomplete list
42 * of missing objects.
43 */
44 if (revs->exclude_promisor_objects &&
45 !has_object_file(&obj->oid) &&
46 is_promisor_object(&obj->oid))
47 return;
48
Jeff Kingde1e67d2016-02-11 17:28:36 -050049 pathlen = path->len;
50 strbuf_addstr(path, name);
Jonathan Tana0c90162018-07-06 12:34:09 -070051 if (!(obj->flags & USER_GIVEN) && filter_fn)
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000052 r = filter_fn(LOFS_BLOB, obj,
53 path->buf, &path->buf[pathlen],
54 filter_data);
55 if (r & LOFR_MARK_SEEN)
56 obj->flags |= SEEN;
57 if (r & LOFR_DO_SHOW)
58 show(obj, path->buf, cb_data);
Jeff Kingde1e67d2016-02-11 17:28:36 -050059 strbuf_setlen(path, pathlen);
Junio C Hamanoc64ed702006-09-04 21:50:12 -070060}
61
Linus Torvalds6e2f4412007-04-13 09:25:01 -070062/*
63 * Processing a gitlink entry currently does nothing, since
64 * we do not recurse into the subproject.
65 *
66 * We *could* eventually add a flag that actually does that,
67 * which would involve:
68 * - is the subproject actually checked out?
69 * - if so, see if the subproject has already been added
70 * to the alternates list, and add it if not.
71 * - process the commit (or tag) the gitlink points to
72 * recursively.
73 *
74 * However, it's unclear whether there is really ever any
75 * reason to see superprojects and subprojects as such a
76 * "unified" object pool (potentially resulting in a totally
77 * humongous pack - avoiding which was the whole point of
78 * having gitlinks in the first place!).
79 *
80 * So for now, there is just a note that we *could* follow
81 * the link, and how to do it. Whether it necessarily makes
82 * any sense what-so-ever to ever do that is another issue.
83 */
84static void process_gitlink(struct rev_info *revs,
85 const unsigned char *sha1,
Linus Torvalds8d2dfc42009-04-10 17:27:58 -070086 show_object_fn show,
Jeff Kingbd645162016-02-11 17:26:44 -050087 struct strbuf *path,
Junio C Hamano49473672011-09-01 15:43:33 -070088 const char *name,
89 void *cb_data)
Linus Torvalds6e2f4412007-04-13 09:25:01 -070090{
91 /* Nothing to do */
92}
93
Junio C Hamanoc64ed702006-09-04 21:50:12 -070094static void process_tree(struct rev_info *revs,
95 struct tree *tree,
Linus Torvalds8d2dfc42009-04-10 17:27:58 -070096 show_object_fn show,
Elijah Newrencc5fa2f2010-12-17 20:26:47 +070097 struct strbuf *base,
Junio C Hamano49473672011-09-01 15:43:33 -070098 const char *name,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000099 void *cb_data,
100 filter_object_fn filter_fn,
101 void *filter_data)
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700102{
103 struct object *obj = &tree->object;
104 struct tree_desc desc;
105 struct name_entry entry;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100106 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
107 all_entries_interesting: entry_not_interesting;
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700108 int baselen = base->len;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000109 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
Jonathan Tandf11e192017-12-08 15:27:15 +0000110 int gently = revs->ignore_missing_links ||
111 revs->exclude_promisor_objects;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700112
113 if (!revs->tree_objects)
114 return;
Martin Koeglera301b0c2008-02-18 21:47:56 +0100115 if (!obj)
116 die("bad tree object");
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700117 if (obj->flags & (UNINTERESTING | SEEN))
118 return;
Jonathan Tandf11e192017-12-08 15:27:15 +0000119 if (parse_tree_gently(tree, gently) < 0) {
Vicent Marti2db1a432014-03-28 06:00:43 -0400120 if (revs->ignore_missing_links)
121 return;
Jonathan Tandf11e192017-12-08 15:27:15 +0000122
123 /*
124 * Pre-filter known-missing tree objects when explicitly
125 * requested. This may cause the actual filter to report
126 * an incomplete list of missing objects.
127 */
128 if (revs->exclude_promisor_objects &&
129 is_promisor_object(&obj->oid))
130 return;
131
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000132 die("bad tree object %s", oid_to_hex(&obj->oid));
Vicent Marti2db1a432014-03-28 06:00:43 -0400133 }
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700134
Jeff King13528ab2016-02-11 17:26:18 -0500135 strbuf_addstr(base, name);
Jonathan Tana0c90162018-07-06 12:34:09 -0700136 if (!(obj->flags & USER_GIVEN) && filter_fn)
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000137 r = filter_fn(LOFS_BEGIN_TREE, obj,
138 base->buf, &base->buf[baselen],
139 filter_data);
140 if (r & LOFR_MARK_SEEN)
141 obj->flags |= SEEN;
142 if (r & LOFR_DO_SHOW)
143 show(obj, base->buf, cb_data);
Jeff King13528ab2016-02-11 17:26:18 -0500144 if (base->len)
145 strbuf_addch(base, '/');
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700146
Linus Torvalds6fda5e52007-03-21 10:08:25 -0700147 init_tree_desc(&desc, tree->buffer, tree->size);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700148
149 while (tree_entry(&desc, &entry)) {
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100150 if (match != all_entries_interesting) {
Nguyễn Thái Ngọc Duy97d0b742011-03-25 16:34:20 +0700151 match = tree_entry_interesting(&entry, base, 0,
152 &revs->diffopt.pathspec);
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100153 if (match == all_entries_not_interesting)
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700154 break;
Nguyễn Thái Ngọc Duyd688cf02011-10-24 17:36:10 +1100155 if (match == entry_not_interesting)
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700156 continue;
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700157 }
158
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700159 if (S_ISDIR(entry.mode))
160 process_tree(revs,
Stefan Bellerf86bcc72018-06-28 18:21:56 -0700161 lookup_tree(the_repository, entry.oid),
Jeff King13528ab2016-02-11 17:26:18 -0500162 show, base, entry.path,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000163 cb_data, filter_fn, filter_data);
Martin Waitz302b9282007-05-21 22:08:28 +0200164 else if (S_ISGITLINK(entry.mode))
brian m. carlson7d924c92016-04-17 23:10:39 +0000165 process_gitlink(revs, entry.oid->hash,
Jeff Kingbd645162016-02-11 17:26:44 -0500166 show, base, entry.path,
Junio C Hamano49473672011-09-01 15:43:33 -0700167 cb_data);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700168 else
169 process_blob(revs,
Stefan Bellerda14a7f2018-06-28 18:21:55 -0700170 lookup_blob(the_repository, entry.oid),
Jeff Kingbd645162016-02-11 17:26:44 -0500171 show, base, entry.path,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000172 cb_data, filter_fn, filter_data);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700173 }
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000174
Jonathan Tana0c90162018-07-06 12:34:09 -0700175 if (!(obj->flags & USER_GIVEN) && filter_fn) {
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000176 r = filter_fn(LOFS_END_TREE, obj,
177 base->buf, &base->buf[baselen],
178 filter_data);
179 if (r & LOFR_MARK_SEEN)
180 obj->flags |= SEEN;
181 if (r & LOFR_DO_SHOW)
182 show(obj, base->buf, cb_data);
183 }
184
Elijah Newrencc5fa2f2010-12-17 20:26:47 +0700185 strbuf_setlen(base, baselen);
Jeff King6e454b92013-06-05 18:37:39 -0400186 free_tree_buffer(tree);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700187}
188
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700189static void mark_edge_parents_uninteresting(struct commit *commit,
190 struct rev_info *revs,
191 show_edge_fn show_edge)
192{
193 struct commit_list *parents;
194
195 for (parents = commit->parents; parents; parents = parents->next) {
196 struct commit *parent = parents->item;
197 if (!(parent->object.flags & UNINTERESTING))
198 continue;
Nguyễn Thái Ngọc Duyb3c7eef2018-09-21 17:57:39 +0200199 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700200 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
201 parent->object.flags |= SHOWN;
202 show_edge(parent);
203 }
204 }
205}
206
Nguyễn Thái Ngọc Duye76a5fb2013-08-16 16:52:06 +0700207void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700208{
Nguyễn Thái Ngọc Duye76a5fb2013-08-16 16:52:06 +0700209 struct commit_list *list;
Nguyễn Thái Ngọc Duyfbd4a702013-08-16 16:52:07 +0700210 int i;
211
Nguyễn Thái Ngọc Duye76a5fb2013-08-16 16:52:06 +0700212 for (list = revs->commits; list; list = list->next) {
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700213 struct commit *commit = list->item;
214
215 if (commit->object.flags & UNINTERESTING) {
Nguyễn Thái Ngọc Duyb3c7eef2018-09-21 17:57:39 +0200216 mark_tree_uninteresting(revs->repo,
217 get_commit_tree(commit));
brian m. carlson1684c1b2014-12-24 23:05:39 +0000218 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
Nguyễn Thái Ngọc Duyfbd4a702013-08-16 16:52:07 +0700219 commit->object.flags |= SHOWN;
220 show_edge(commit);
221 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700222 continue;
223 }
224 mark_edge_parents_uninteresting(commit, revs, show_edge);
225 }
brian m. carlson1684c1b2014-12-24 23:05:39 +0000226 if (revs->edge_hint_aggressive) {
Jeff King200abe72014-01-20 21:25:40 -0500227 for (i = 0; i < revs->cmdline.nr; i++) {
228 struct object *obj = revs->cmdline.rev[i].item;
229 struct commit *commit = (struct commit *)obj;
230 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
231 continue;
Nguyễn Thái Ngọc Duyb3c7eef2018-09-21 17:57:39 +0200232 mark_tree_uninteresting(revs->repo,
233 get_commit_tree(commit));
Jeff King200abe72014-01-20 21:25:40 -0500234 if (!(obj->flags & SHOWN)) {
235 obj->flags |= SHOWN;
236 show_edge(commit);
237 }
Nguyễn Thái Ngọc Duyfbd4a702013-08-16 16:52:07 +0700238 }
239 }
Junio C Hamano8d1d8f82006-09-06 01:42:23 -0700240}
241
Linus Torvalds8d2dfc42009-04-10 17:27:58 -0700242static void add_pending_tree(struct rev_info *revs, struct tree *tree)
243{
244 add_pending_object(revs, &tree->object, "");
245}
246
Stefan Beller91904f52017-11-02 12:41:43 -0700247static void traverse_trees_and_blobs(struct rev_info *revs,
248 struct strbuf *base,
249 show_object_fn show_object,
Junio C Hamano556de1a2017-12-28 14:08:50 -0800250 void *show_data,
251 filter_object_fn filter_fn,
252 void *filter_data)
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700253{
254 int i;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700255
Stefan Beller91904f52017-11-02 12:41:43 -0700256 assert(base->len == 0);
257
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700258 for (i = 0; i < revs->pending.nr; i++) {
259 struct object_array_entry *pending = revs->pending.objects + i;
260 struct object *obj = pending->item;
261 const char *name = pending->name;
Jeff King20739492014-10-15 18:43:19 -0400262 const char *path = pending->path;
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700263 if (obj->flags & (UNINTERESTING | SEEN))
264 continue;
265 if (obj->type == OBJ_TAG) {
266 obj->flags |= SEEN;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000267 show_object(obj, name, show_data);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700268 continue;
269 }
Jeff King20739492014-10-15 18:43:19 -0400270 if (!path)
271 path = "";
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700272 if (obj->type == OBJ_TREE) {
Linus Torvalds8d2dfc42009-04-10 17:27:58 -0700273 process_tree(revs, (struct tree *)obj, show_object,
Junio C Hamano556de1a2017-12-28 14:08:50 -0800274 base, path, show_data,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000275 filter_fn, filter_data);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700276 continue;
277 }
278 if (obj->type == OBJ_BLOB) {
Linus Torvalds8d2dfc42009-04-10 17:27:58 -0700279 process_blob(revs, (struct blob *)obj, show_object,
Junio C Hamano556de1a2017-12-28 14:08:50 -0800280 base, path, show_data,
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000281 filter_fn, filter_data);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700282 continue;
283 }
284 die("unknown pending object %s (%s)",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000285 oid_to_hex(&obj->oid), name);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700286 }
Jeff King46be8232014-10-15 18:34:34 -0400287 object_array_clear(&revs->pending);
Stefan Beller91904f52017-11-02 12:41:43 -0700288}
289
Junio C Hamano556de1a2017-12-28 14:08:50 -0800290static void do_traverse(struct rev_info *revs,
291 show_commit_fn show_commit,
292 show_object_fn show_object,
293 void *show_data,
294 filter_object_fn filter_fn,
295 void *filter_data)
Stefan Beller91904f52017-11-02 12:41:43 -0700296{
297 struct commit *commit;
298 struct strbuf csp; /* callee's scratch pad */
299 strbuf_init(&csp, PATH_MAX);
300
301 while ((commit = get_revision(revs)) != NULL) {
302 /*
303 * an uninteresting boundary commit may not have its tree
304 * parsed yet, but we are not going to show them anyway
305 */
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000306 if (get_commit_tree(commit))
307 add_pending_tree(revs, get_commit_tree(commit));
Junio C Hamano556de1a2017-12-28 14:08:50 -0800308 show_commit(commit, show_data);
Stefan Bellerce5b6f92017-11-15 18:00:35 -0800309
310 if (revs->tree_blobs_in_commit_order)
311 /*
312 * NEEDSWORK: Adding the tree and then flushing it here
313 * needs a reallocation for each commit. Can we pass the
314 * tree directory without allocation churn?
315 */
Junio C Hamano556de1a2017-12-28 14:08:50 -0800316 traverse_trees_and_blobs(revs, &csp,
317 show_object, show_data,
318 filter_fn, filter_data);
Stefan Beller91904f52017-11-02 12:41:43 -0700319 }
Junio C Hamano556de1a2017-12-28 14:08:50 -0800320 traverse_trees_and_blobs(revs, &csp,
321 show_object, show_data,
322 filter_fn, filter_data);
Stefan Beller91904f52017-11-02 12:41:43 -0700323 strbuf_release(&csp);
Junio C Hamanoc64ed702006-09-04 21:50:12 -0700324}
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000325
326void traverse_commit_list(struct rev_info *revs,
327 show_commit_fn show_commit,
328 show_object_fn show_object,
329 void *show_data)
330{
331 do_traverse(revs, show_commit, show_object, show_data, NULL, NULL);
332}
333
334void traverse_commit_list_filtered(
335 struct list_objects_filter_options *filter_options,
336 struct rev_info *revs,
337 show_commit_fn show_commit,
338 show_object_fn show_object,
339 void *show_data,
340 struct oidset *omitted)
341{
342 filter_object_fn filter_fn = NULL;
343 filter_free_fn filter_free_fn = NULL;
344 void *filter_data = NULL;
345
346 filter_data = list_objects_filter__init(omitted, filter_options,
347 &filter_fn, &filter_free_fn);
348 do_traverse(revs, show_commit, show_object, show_data,
349 filter_fn, filter_data);
350 if (filter_data && filter_free_fn)
351 filter_free_fn(filter_data);
352}