config.txt: move init.* to a separate file
[git.git] / list-objects.c
blob0c2989d5ca7cc4df523376dce94829b1b6be9110
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"
15 static void process_blob(struct rev_info *revs,
16 struct blob *blob,
17 show_object_fn show,
18 struct strbuf *path,
19 const char *name,
20 void *cb_data,
21 filter_object_fn filter_fn,
22 void *filter_data)
24 struct object *obj = &blob->object;
25 size_t pathlen;
26 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
28 if (!revs->blob_objects)
29 return;
30 if (!obj)
31 die("bad blob object");
32 if (obj->flags & (UNINTERESTING | SEEN))
33 return;
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).
40 * Note that this "--exclude-promisor-objects" pre-filtering
41 * may cause the actual filter to report an incomplete list
42 * of missing objects.
44 if (revs->exclude_promisor_objects &&
45 !has_object_file(&obj->oid) &&
46 is_promisor_object(&obj->oid))
47 return;
49 pathlen = path->len;
50 strbuf_addstr(path, name);
51 if (!(obj->flags & USER_GIVEN) && filter_fn)
52 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);
59 strbuf_setlen(path, pathlen);
63 * Processing a gitlink entry currently does nothing, since
64 * we do not recurse into the subproject.
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.
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!).
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.
84 static void process_gitlink(struct rev_info *revs,
85 const unsigned char *sha1,
86 show_object_fn show,
87 struct strbuf *path,
88 const char *name,
89 void *cb_data)
91 /* Nothing to do */
94 static void process_tree(struct rev_info *revs,
95 struct tree *tree,
96 show_object_fn show,
97 struct strbuf *base,
98 const char *name,
99 void *cb_data,
100 filter_object_fn filter_fn,
101 void *filter_data)
103 struct object *obj = &tree->object;
104 struct tree_desc desc;
105 struct name_entry entry;
106 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
107 all_entries_interesting: entry_not_interesting;
108 int baselen = base->len;
109 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
110 int gently = revs->ignore_missing_links ||
111 revs->exclude_promisor_objects;
113 if (!revs->tree_objects)
114 return;
115 if (!obj)
116 die("bad tree object");
117 if (obj->flags & (UNINTERESTING | SEEN))
118 return;
119 if (parse_tree_gently(tree, gently) < 0) {
120 if (revs->ignore_missing_links)
121 return;
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.
128 if (revs->exclude_promisor_objects &&
129 is_promisor_object(&obj->oid))
130 return;
132 die("bad tree object %s", oid_to_hex(&obj->oid));
135 strbuf_addstr(base, name);
136 if (!(obj->flags & USER_GIVEN) && filter_fn)
137 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);
144 if (base->len)
145 strbuf_addch(base, '/');
147 init_tree_desc(&desc, tree->buffer, tree->size);
149 while (tree_entry(&desc, &entry)) {
150 if (match != all_entries_interesting) {
151 match = tree_entry_interesting(&entry, base, 0,
152 &revs->diffopt.pathspec);
153 if (match == all_entries_not_interesting)
154 break;
155 if (match == entry_not_interesting)
156 continue;
159 if (S_ISDIR(entry.mode))
160 process_tree(revs,
161 lookup_tree(the_repository, entry.oid),
162 show, base, entry.path,
163 cb_data, filter_fn, filter_data);
164 else if (S_ISGITLINK(entry.mode))
165 process_gitlink(revs, entry.oid->hash,
166 show, base, entry.path,
167 cb_data);
168 else
169 process_blob(revs,
170 lookup_blob(the_repository, entry.oid),
171 show, base, entry.path,
172 cb_data, filter_fn, filter_data);
175 if (!(obj->flags & USER_GIVEN) && filter_fn) {
176 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);
185 strbuf_setlen(base, baselen);
186 free_tree_buffer(tree);
189 static void mark_edge_parents_uninteresting(struct commit *commit,
190 struct rev_info *revs,
191 show_edge_fn show_edge)
193 struct commit_list *parents;
195 for (parents = commit->parents; parents; parents = parents->next) {
196 struct commit *parent = parents->item;
197 if (!(parent->object.flags & UNINTERESTING))
198 continue;
199 mark_tree_uninteresting(revs->repo, get_commit_tree(parent));
200 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
201 parent->object.flags |= SHOWN;
202 show_edge(parent);
207 void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
209 struct commit_list *list;
210 int i;
212 for (list = revs->commits; list; list = list->next) {
213 struct commit *commit = list->item;
215 if (commit->object.flags & UNINTERESTING) {
216 mark_tree_uninteresting(revs->repo,
217 get_commit_tree(commit));
218 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
219 commit->object.flags |= SHOWN;
220 show_edge(commit);
222 continue;
224 mark_edge_parents_uninteresting(commit, revs, show_edge);
226 if (revs->edge_hint_aggressive) {
227 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;
232 mark_tree_uninteresting(revs->repo,
233 get_commit_tree(commit));
234 if (!(obj->flags & SHOWN)) {
235 obj->flags |= SHOWN;
236 show_edge(commit);
242 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
244 add_pending_object(revs, &tree->object, "");
247 static void traverse_trees_and_blobs(struct rev_info *revs,
248 struct strbuf *base,
249 show_object_fn show_object,
250 void *show_data,
251 filter_object_fn filter_fn,
252 void *filter_data)
254 int i;
256 assert(base->len == 0);
258 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;
262 const char *path = pending->path;
263 if (obj->flags & (UNINTERESTING | SEEN))
264 continue;
265 if (obj->type == OBJ_TAG) {
266 obj->flags |= SEEN;
267 show_object(obj, name, show_data);
268 continue;
270 if (!path)
271 path = "";
272 if (obj->type == OBJ_TREE) {
273 process_tree(revs, (struct tree *)obj, show_object,
274 base, path, show_data,
275 filter_fn, filter_data);
276 continue;
278 if (obj->type == OBJ_BLOB) {
279 process_blob(revs, (struct blob *)obj, show_object,
280 base, path, show_data,
281 filter_fn, filter_data);
282 continue;
284 die("unknown pending object %s (%s)",
285 oid_to_hex(&obj->oid), name);
287 object_array_clear(&revs->pending);
290 static 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)
297 struct commit *commit;
298 struct strbuf csp; /* callee's scratch pad */
299 strbuf_init(&csp, PATH_MAX);
301 while ((commit = get_revision(revs)) != NULL) {
303 * an uninteresting boundary commit may not have its tree
304 * parsed yet, but we are not going to show them anyway
306 if (get_commit_tree(commit))
307 add_pending_tree(revs, get_commit_tree(commit));
308 show_commit(commit, show_data);
310 if (revs->tree_blobs_in_commit_order)
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?
316 traverse_trees_and_blobs(revs, &csp,
317 show_object, show_data,
318 filter_fn, filter_data);
320 traverse_trees_and_blobs(revs, &csp,
321 show_object, show_data,
322 filter_fn, filter_data);
323 strbuf_release(&csp);
326 void traverse_commit_list(struct rev_info *revs,
327 show_commit_fn show_commit,
328 show_object_fn show_object,
329 void *show_data)
331 do_traverse(revs, show_commit, show_object, show_data, NULL, NULL);
334 void 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)
342 filter_object_fn filter_fn = NULL;
343 filter_free_fn filter_free_fn = NULL;
344 void *filter_data = NULL;
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);