list-objects: filter objects in traverse_commit_list
[git/git.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
13 static void process_blob(struct rev_info *revs,
14 struct blob *blob,
15 show_object_fn show,
16 struct strbuf *path,
17 const char *name,
18 void *cb_data,
19 filter_object_fn filter_fn,
20 void *filter_data)
21 {
22 struct object *obj = &blob->object;
23 size_t pathlen;
24 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
25
26 if (!revs->blob_objects)
27 return;
28 if (!obj)
29 die("bad blob object");
30 if (obj->flags & (UNINTERESTING | SEEN))
31 return;
32
33 pathlen = path->len;
34 strbuf_addstr(path, name);
35 if (filter_fn)
36 r = filter_fn(LOFS_BLOB, obj,
37 path->buf, &path->buf[pathlen],
38 filter_data);
39 if (r & LOFR_MARK_SEEN)
40 obj->flags |= SEEN;
41 if (r & LOFR_DO_SHOW)
42 show(obj, path->buf, cb_data);
43 strbuf_setlen(path, pathlen);
44 }
45
46 /*
47 * Processing a gitlink entry currently does nothing, since
48 * we do not recurse into the subproject.
49 *
50 * We *could* eventually add a flag that actually does that,
51 * which would involve:
52 * - is the subproject actually checked out?
53 * - if so, see if the subproject has already been added
54 * to the alternates list, and add it if not.
55 * - process the commit (or tag) the gitlink points to
56 * recursively.
57 *
58 * However, it's unclear whether there is really ever any
59 * reason to see superprojects and subprojects as such a
60 * "unified" object pool (potentially resulting in a totally
61 * humongous pack - avoiding which was the whole point of
62 * having gitlinks in the first place!).
63 *
64 * So for now, there is just a note that we *could* follow
65 * the link, and how to do it. Whether it necessarily makes
66 * any sense what-so-ever to ever do that is another issue.
67 */
68 static void process_gitlink(struct rev_info *revs,
69 const unsigned char *sha1,
70 show_object_fn show,
71 struct strbuf *path,
72 const char *name,
73 void *cb_data)
74 {
75 /* Nothing to do */
76 }
77
78 static void process_tree(struct rev_info *revs,
79 struct tree *tree,
80 show_object_fn show,
81 struct strbuf *base,
82 const char *name,
83 void *cb_data,
84 filter_object_fn filter_fn,
85 void *filter_data)
86 {
87 struct object *obj = &tree->object;
88 struct tree_desc desc;
89 struct name_entry entry;
90 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
91 all_entries_interesting: entry_not_interesting;
92 int baselen = base->len;
93 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
94
95 if (!revs->tree_objects)
96 return;
97 if (!obj)
98 die("bad tree object");
99 if (obj->flags & (UNINTERESTING | SEEN))
100 return;
101 if (parse_tree_gently(tree, revs->ignore_missing_links) < 0) {
102 if (revs->ignore_missing_links)
103 return;
104 die("bad tree object %s", oid_to_hex(&obj->oid));
105 }
106
107 strbuf_addstr(base, name);
108 if (filter_fn)
109 r = filter_fn(LOFS_BEGIN_TREE, obj,
110 base->buf, &base->buf[baselen],
111 filter_data);
112 if (r & LOFR_MARK_SEEN)
113 obj->flags |= SEEN;
114 if (r & LOFR_DO_SHOW)
115 show(obj, base->buf, cb_data);
116 if (base->len)
117 strbuf_addch(base, '/');
118
119 init_tree_desc(&desc, tree->buffer, tree->size);
120
121 while (tree_entry(&desc, &entry)) {
122 if (match != all_entries_interesting) {
123 match = tree_entry_interesting(&entry, base, 0,
124 &revs->diffopt.pathspec);
125 if (match == all_entries_not_interesting)
126 break;
127 if (match == entry_not_interesting)
128 continue;
129 }
130
131 if (S_ISDIR(entry.mode))
132 process_tree(revs,
133 lookup_tree(entry.oid),
134 show, base, entry.path,
135 cb_data, filter_fn, filter_data);
136 else if (S_ISGITLINK(entry.mode))
137 process_gitlink(revs, entry.oid->hash,
138 show, base, entry.path,
139 cb_data);
140 else
141 process_blob(revs,
142 lookup_blob(entry.oid),
143 show, base, entry.path,
144 cb_data, filter_fn, filter_data);
145 }
146
147 if (filter_fn) {
148 r = filter_fn(LOFS_END_TREE, obj,
149 base->buf, &base->buf[baselen],
150 filter_data);
151 if (r & LOFR_MARK_SEEN)
152 obj->flags |= SEEN;
153 if (r & LOFR_DO_SHOW)
154 show(obj, base->buf, cb_data);
155 }
156
157 strbuf_setlen(base, baselen);
158 free_tree_buffer(tree);
159 }
160
161 static void mark_edge_parents_uninteresting(struct commit *commit,
162 struct rev_info *revs,
163 show_edge_fn show_edge)
164 {
165 struct commit_list *parents;
166
167 for (parents = commit->parents; parents; parents = parents->next) {
168 struct commit *parent = parents->item;
169 if (!(parent->object.flags & UNINTERESTING))
170 continue;
171 mark_tree_uninteresting(parent->tree);
172 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
173 parent->object.flags |= SHOWN;
174 show_edge(parent);
175 }
176 }
177 }
178
179 void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
180 {
181 struct commit_list *list;
182 int i;
183
184 for (list = revs->commits; list; list = list->next) {
185 struct commit *commit = list->item;
186
187 if (commit->object.flags & UNINTERESTING) {
188 mark_tree_uninteresting(commit->tree);
189 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
190 commit->object.flags |= SHOWN;
191 show_edge(commit);
192 }
193 continue;
194 }
195 mark_edge_parents_uninteresting(commit, revs, show_edge);
196 }
197 if (revs->edge_hint_aggressive) {
198 for (i = 0; i < revs->cmdline.nr; i++) {
199 struct object *obj = revs->cmdline.rev[i].item;
200 struct commit *commit = (struct commit *)obj;
201 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
202 continue;
203 mark_tree_uninteresting(commit->tree);
204 if (!(obj->flags & SHOWN)) {
205 obj->flags |= SHOWN;
206 show_edge(commit);
207 }
208 }
209 }
210 }
211
212 static void add_pending_tree(struct rev_info *revs, struct tree *tree)
213 {
214 add_pending_object(revs, &tree->object, "");
215 }
216
217 static void do_traverse(struct rev_info *revs,
218 show_commit_fn show_commit,
219 show_object_fn show_object,
220 void *show_data,
221 filter_object_fn filter_fn,
222 void *filter_data)
223 {
224 int i;
225 struct commit *commit;
226 struct strbuf base;
227
228 strbuf_init(&base, PATH_MAX);
229 while ((commit = get_revision(revs)) != NULL) {
230 /*
231 * an uninteresting boundary commit may not have its tree
232 * parsed yet, but we are not going to show them anyway
233 */
234 if (commit->tree)
235 add_pending_tree(revs, commit->tree);
236 show_commit(commit, show_data);
237 }
238 for (i = 0; i < revs->pending.nr; i++) {
239 struct object_array_entry *pending = revs->pending.objects + i;
240 struct object *obj = pending->item;
241 const char *name = pending->name;
242 const char *path = pending->path;
243 if (obj->flags & (UNINTERESTING | SEEN))
244 continue;
245 if (obj->type == OBJ_TAG) {
246 obj->flags |= SEEN;
247 show_object(obj, name, show_data);
248 continue;
249 }
250 if (!path)
251 path = "";
252 if (obj->type == OBJ_TREE) {
253 process_tree(revs, (struct tree *)obj, show_object,
254 &base, path, show_data,
255 filter_fn, filter_data);
256 continue;
257 }
258 if (obj->type == OBJ_BLOB) {
259 process_blob(revs, (struct blob *)obj, show_object,
260 &base, path, show_data,
261 filter_fn, filter_data);
262 continue;
263 }
264 die("unknown pending object %s (%s)",
265 oid_to_hex(&obj->oid), name);
266 }
267 object_array_clear(&revs->pending);
268 strbuf_release(&base);
269 }
270
271 void traverse_commit_list(struct rev_info *revs,
272 show_commit_fn show_commit,
273 show_object_fn show_object,
274 void *show_data)
275 {
276 do_traverse(revs, show_commit, show_object, show_data, NULL, NULL);
277 }
278
279 void traverse_commit_list_filtered(
280 struct list_objects_filter_options *filter_options,
281 struct rev_info *revs,
282 show_commit_fn show_commit,
283 show_object_fn show_object,
284 void *show_data,
285 struct oidset *omitted)
286 {
287 filter_object_fn filter_fn = NULL;
288 filter_free_fn filter_free_fn = NULL;
289 void *filter_data = NULL;
290
291 filter_data = list_objects_filter__init(omitted, filter_options,
292 &filter_fn, &filter_free_fn);
293 do_traverse(revs, show_commit, show_object, show_data,
294 filter_fn, filter_data);
295 if (filter_data && filter_free_fn)
296 filter_free_fn(filter_data);
297 }