9 #include "list-objects.h"
10 #include "list-objects-filter.h"
11 #include "list-objects-filter-options.h"
14 static void process_blob(struct rev_info *revs,
20 filter_object_fn filter_fn,
23 struct object *obj = &blob->object;
25 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
27 if (!revs->blob_objects)
30 die("bad blob object");
31 if (obj->flags & (UNINTERESTING | SEEN))
35 * Pre-filter known-missing objects when explicitly requested.
36 * Otherwise, a missing object error message may be reported
37 * later (depending on other filtering criteria).
39 * Note that this "--exclude-promisor-objects" pre-filtering
40 * may cause the actual filter to report an incomplete list
43 if (revs->exclude_promisor_objects &&
44 !has_object_file(&obj->oid) &&
45 is_promisor_object(&obj->oid))
49 strbuf_addstr(path, name);
51 r = filter_fn(LOFS_BLOB, obj,
52 path->buf, &path->buf[pathlen],
54 if (r & LOFR_MARK_SEEN)
57 show(obj, path->buf, cb_data);
58 strbuf_setlen(path, pathlen);
62 * Processing a gitlink entry currently does nothing, since
63 * we do not recurse into the subproject.
65 * We *could* eventually add a flag that actually does that,
66 * which would involve:
67 * - is the subproject actually checked out?
68 * - if so, see if the subproject has already been added
69 * to the alternates list, and add it if not.
70 * - process the commit (or tag) the gitlink points to
73 * However, it's unclear whether there is really ever any
74 * reason to see superprojects and subprojects as such a
75 * "unified" object pool (potentially resulting in a totally
76 * humongous pack - avoiding which was the whole point of
77 * having gitlinks in the first place!).
79 * So for now, there is just a note that we *could* follow
80 * the link, and how to do it. Whether it necessarily makes
81 * any sense what-so-ever to ever do that is another issue.
83 static void process_gitlink(struct rev_info *revs,
84 const unsigned char *sha1,
93 static void process_tree(struct rev_info *revs,
99 filter_object_fn filter_fn,
102 struct object *obj = &tree->object;
103 struct tree_desc desc;
104 struct name_entry entry;
105 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
106 all_entries_interesting: entry_not_interesting;
107 int baselen = base->len;
108 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
109 int gently = revs->ignore_missing_links ||
110 revs->exclude_promisor_objects;
112 if (!revs->tree_objects)
115 die("bad tree object");
116 if (obj->flags & (UNINTERESTING | SEEN))
118 if (parse_tree_gently(tree, gently) < 0) {
119 if (revs->ignore_missing_links)
123 * Pre-filter known-missing tree objects when explicitly
124 * requested. This may cause the actual filter to report
125 * an incomplete list of missing objects.
127 if (revs->exclude_promisor_objects &&
128 is_promisor_object(&obj->oid))
131 die("bad tree object %s", oid_to_hex(&obj->oid));
134 strbuf_addstr(base, name);
136 r = filter_fn(LOFS_BEGIN_TREE, obj,
137 base->buf, &base->buf[baselen],
139 if (r & LOFR_MARK_SEEN)
141 if (r & LOFR_DO_SHOW)
142 show(obj, base->buf, cb_data);
144 strbuf_addch(base, '/');
146 init_tree_desc(&desc, tree->buffer, tree->size);
148 while (tree_entry(&desc, &entry)) {
149 if (match != all_entries_interesting) {
150 match = tree_entry_interesting(&entry, base, 0,
151 &revs->diffopt.pathspec);
152 if (match == all_entries_not_interesting)
154 if (match == entry_not_interesting)
158 if (S_ISDIR(entry.mode))
160 lookup_tree(entry.oid),
161 show, base, entry.path,
162 cb_data, filter_fn, filter_data);
163 else if (S_ISGITLINK(entry.mode))
164 process_gitlink(revs, entry.oid->hash,
165 show, base, entry.path,
169 lookup_blob(entry.oid),
170 show, base, entry.path,
171 cb_data, filter_fn, filter_data);
175 r = filter_fn(LOFS_END_TREE, obj,
176 base->buf, &base->buf[baselen],
178 if (r & LOFR_MARK_SEEN)
180 if (r & LOFR_DO_SHOW)
181 show(obj, base->buf, cb_data);
184 strbuf_setlen(base, baselen);
185 free_tree_buffer(tree);
188 static void mark_edge_parents_uninteresting(struct commit *commit,
189 struct rev_info *revs,
190 show_edge_fn show_edge)
192 struct commit_list *parents;
194 for (parents = commit->parents; parents; parents = parents->next) {
195 struct commit *parent = parents->item;
196 if (!(parent->object.flags & UNINTERESTING))
198 mark_tree_uninteresting(parent->tree);
199 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
200 parent->object.flags |= SHOWN;
206 void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
208 struct commit_list *list;
211 for (list = revs->commits; list; list = list->next) {
212 struct commit *commit = list->item;
214 if (commit->object.flags & UNINTERESTING) {
215 mark_tree_uninteresting(commit->tree);
216 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
217 commit->object.flags |= SHOWN;
222 mark_edge_parents_uninteresting(commit, revs, show_edge);
224 if (revs->edge_hint_aggressive) {
225 for (i = 0; i < revs->cmdline.nr; i++) {
226 struct object *obj = revs->cmdline.rev[i].item;
227 struct commit *commit = (struct commit *)obj;
228 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
230 mark_tree_uninteresting(commit->tree);
231 if (!(obj->flags & SHOWN)) {
239 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
241 add_pending_object(revs, &tree->object, "");
244 static void traverse_trees_and_blobs(struct rev_info *revs,
246 show_object_fn show_object,
248 filter_object_fn filter_fn,
253 assert(base->len == 0);
255 for (i = 0; i < revs->pending.nr; i++) {
256 struct object_array_entry *pending = revs->pending.objects + i;
257 struct object *obj = pending->item;
258 const char *name = pending->name;
259 const char *path = pending->path;
260 if (obj->flags & (UNINTERESTING | SEEN))
262 if (obj->type == OBJ_TAG) {
264 show_object(obj, name, show_data);
269 if (obj->type == OBJ_TREE) {
270 process_tree(revs, (struct tree *)obj, show_object,
271 base, path, show_data,
272 filter_fn, filter_data);
275 if (obj->type == OBJ_BLOB) {
276 process_blob(revs, (struct blob *)obj, show_object,
277 base, path, show_data,
278 filter_fn, filter_data);
281 die("unknown pending object %s (%s)",
282 oid_to_hex(&obj->oid), name);
284 object_array_clear(&revs->pending);
287 static void do_traverse(struct rev_info *revs,
288 show_commit_fn show_commit,
289 show_object_fn show_object,
291 filter_object_fn filter_fn,
294 struct commit *commit;
295 struct strbuf csp; /* callee's scratch pad */
296 strbuf_init(&csp, PATH_MAX);
298 while ((commit = get_revision(revs)) != NULL) {
300 * an uninteresting boundary commit may not have its tree
301 * parsed yet, but we are not going to show them anyway
304 add_pending_tree(revs, commit->tree);
305 show_commit(commit, show_data);
307 if (revs->tree_blobs_in_commit_order)
309 * NEEDSWORK: Adding the tree and then flushing it here
310 * needs a reallocation for each commit. Can we pass the
311 * tree directory without allocation churn?
313 traverse_trees_and_blobs(revs, &csp,
314 show_object, show_data,
315 filter_fn, filter_data);
317 traverse_trees_and_blobs(revs, &csp,
318 show_object, show_data,
319 filter_fn, filter_data);
320 strbuf_release(&csp);
323 void traverse_commit_list(struct rev_info *revs,
324 show_commit_fn show_commit,
325 show_object_fn show_object,
328 do_traverse(revs, show_commit, show_object, show_data, NULL, NULL);
331 void traverse_commit_list_filtered(
332 struct list_objects_filter_options *filter_options,
333 struct rev_info *revs,
334 show_commit_fn show_commit,
335 show_object_fn show_object,
337 struct oidset *omitted)
339 filter_object_fn filter_fn = NULL;
340 filter_free_fn filter_free_fn = NULL;
341 void *filter_data = NULL;
343 filter_data = list_objects_filter__init(omitted, filter_options,
344 &filter_fn, &filter_free_fn);
345 do_traverse(revs, show_commit, show_object, show_data,
346 filter_fn, filter_data);
347 if (filter_data && filter_free_fn)
348 filter_free_fn(filter_data);