Merge branch 'hi/merge-verify-sig-config'
[git/git.git] / list-objects.c
CommitLineData
c64ed70d
JH
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"
25ec7bca
JH
10#include "list-objects-filter.h"
11#include "list-objects-filter-options.h"
c64ed70d
JH
12
13static void process_blob(struct rev_info *revs,
14 struct blob *blob,
8d2dfc49 15 show_object_fn show,
bd64516a 16 struct strbuf *path,
49473672 17 const char *name,
25ec7bca
JH
18 void *cb_data,
19 filter_object_fn filter_fn,
20 void *filter_data)
c64ed70d
JH
21{
22 struct object *obj = &blob->object;
de1e67d0 23 size_t pathlen;
25ec7bca 24 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
c64ed70d
JH
25
26 if (!revs->blob_objects)
27 return;
a301b0c8
MK
28 if (!obj)
29 die("bad blob object");
c64ed70d
JH
30 if (obj->flags & (UNINTERESTING | SEEN))
31 return;
de1e67d0
JK
32
33 pathlen = path->len;
34 strbuf_addstr(path, name);
25ec7bca
JH
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);
de1e67d0 43 strbuf_setlen(path, pathlen);
c64ed70d
JH
44}
45
6e2f441b
LT
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 */
68static void process_gitlink(struct rev_info *revs,
69 const unsigned char *sha1,
8d2dfc49 70 show_object_fn show,
bd64516a 71 struct strbuf *path,
49473672
JH
72 const char *name,
73 void *cb_data)
6e2f441b
LT
74{
75 /* Nothing to do */
76}
77
c64ed70d
JH
78static void process_tree(struct rev_info *revs,
79 struct tree *tree,
8d2dfc49 80 show_object_fn show,
cc5fa2fd 81 struct strbuf *base,
49473672 82 const char *name,
25ec7bca
JH
83 void *cb_data,
84 filter_object_fn filter_fn,
85 void *filter_data)
c64ed70d
JH
86{
87 struct object *obj = &tree->object;
88 struct tree_desc desc;
89 struct name_entry entry;
d688cf07
NTND
90 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
91 all_entries_interesting: entry_not_interesting;
cc5fa2fd 92 int baselen = base->len;
25ec7bca 93 enum list_objects_filter_result r = LOFR_MARK_SEEN | LOFR_DO_SHOW;
c64ed70d
JH
94
95 if (!revs->tree_objects)
96 return;
a301b0c8
MK
97 if (!obj)
98 die("bad tree object");
c64ed70d
JH
99 if (obj->flags & (UNINTERESTING | SEEN))
100 return;
daf7d867 101 if (parse_tree_gently(tree, revs->ignore_missing_links) < 0) {
2db1a43f
VM
102 if (revs->ignore_missing_links)
103 return;
f2fd0760 104 die("bad tree object %s", oid_to_hex(&obj->oid));
2db1a43f 105 }
13528ab3 106
13528ab3 107 strbuf_addstr(base, name);
25ec7bca
JH
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);
13528ab3
JK
116 if (base->len)
117 strbuf_addch(base, '/');
cc5fa2fd 118
6fda5e51 119 init_tree_desc(&desc, tree->buffer, tree->size);
c64ed70d
JH
120
121 while (tree_entry(&desc, &entry)) {
d688cf07 122 if (match != all_entries_interesting) {
97d0b74a
NTND
123 match = tree_entry_interesting(&entry, base, 0,
124 &revs->diffopt.pathspec);
d688cf07 125 if (match == all_entries_not_interesting)
cc5fa2fd 126 break;
d688cf07 127 if (match == entry_not_interesting)
cc5fa2fd 128 continue;
cc5fa2fd
EN
129 }
130
c64ed70d
JH
131 if (S_ISDIR(entry.mode))
132 process_tree(revs,
740ee055 133 lookup_tree(entry.oid),
13528ab3 134 show, base, entry.path,
25ec7bca 135 cb_data, filter_fn, filter_data);
302b9282 136 else if (S_ISGITLINK(entry.mode))
7d924c91 137 process_gitlink(revs, entry.oid->hash,
bd64516a 138 show, base, entry.path,
49473672 139 cb_data);
c64ed70d
JH
140 else
141 process_blob(revs,
3aca1fc6 142 lookup_blob(entry.oid),
bd64516a 143 show, base, entry.path,
25ec7bca 144 cb_data, filter_fn, filter_data);
c64ed70d 145 }
25ec7bca
JH
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
cc5fa2fd 157 strbuf_setlen(base, baselen);
6e454b9a 158 free_tree_buffer(tree);
c64ed70d
JH
159}
160
8d1d8f83
JH
161static 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
e76a5fb4 179void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
8d1d8f83 180{
e76a5fb4 181 struct commit_list *list;
fbd4a703
NTND
182 int i;
183
e76a5fb4 184 for (list = revs->commits; list; list = list->next) {
8d1d8f83
JH
185 struct commit *commit = list->item;
186
187 if (commit->object.flags & UNINTERESTING) {
188 mark_tree_uninteresting(commit->tree);
1684c1b2 189 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
fbd4a703
NTND
190 commit->object.flags |= SHOWN;
191 show_edge(commit);
192 }
8d1d8f83
JH
193 continue;
194 }
195 mark_edge_parents_uninteresting(commit, revs, show_edge);
196 }
1684c1b2 197 if (revs->edge_hint_aggressive) {
200abe74
JK
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 }
fbd4a703
NTND
208 }
209 }
8d1d8f83
JH
210}
211
8d2dfc49
LT
212static void add_pending_tree(struct rev_info *revs, struct tree *tree)
213{
214 add_pending_object(revs, &tree->object, "");
215}
216
25ec7bca
JH
217static 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)
c64ed70d
JH
223{
224 int i;
225 struct commit *commit;
cc5fa2fd 226 struct strbuf base;
c64ed70d 227
cc5fa2fd 228 strbuf_init(&base, PATH_MAX);
c64ed70d 229 while ((commit = get_revision(revs)) != NULL) {
6e7d0efa
JH
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);
25ec7bca 236 show_commit(commit, show_data);
c64ed70d
JH
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;
20739490 242 const char *path = pending->path;
c64ed70d
JH
243 if (obj->flags & (UNINTERESTING | SEEN))
244 continue;
245 if (obj->type == OBJ_TAG) {
246 obj->flags |= SEEN;
25ec7bca 247 show_object(obj, name, show_data);
c64ed70d
JH
248 continue;
249 }
20739490
JK
250 if (!path)
251 path = "";
c64ed70d 252 if (obj->type == OBJ_TREE) {
8d2dfc49 253 process_tree(revs, (struct tree *)obj, show_object,
25ec7bca
JH
254 &base, path, show_data,
255 filter_fn, filter_data);
c64ed70d
JH
256 continue;
257 }
258 if (obj->type == OBJ_BLOB) {
8d2dfc49 259 process_blob(revs, (struct blob *)obj, show_object,
25ec7bca
JH
260 &base, path, show_data,
261 filter_fn, filter_data);
c64ed70d
JH
262 continue;
263 }
264 die("unknown pending object %s (%s)",
f2fd0760 265 oid_to_hex(&obj->oid), name);
c64ed70d 266 }
46be8231 267 object_array_clear(&revs->pending);
cc5fa2fd 268 strbuf_release(&base);
c64ed70d 269}
25ec7bca
JH
270
271void 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
279void 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}