list-objects: convert name_path to a strbuf
[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"
10
11static void process_blob(struct rev_info *revs,
12 struct blob *blob,
8d2dfc49 13 show_object_fn show,
c64ed70d 14 struct name_path *path,
49473672
JH
15 const char *name,
16 void *cb_data)
c64ed70d
JH
17{
18 struct object *obj = &blob->object;
19
20 if (!revs->blob_objects)
21 return;
a301b0c8
MK
22 if (!obj)
23 die("bad blob object");
c64ed70d
JH
24 if (obj->flags & (UNINTERESTING | SEEN))
25 return;
26 obj->flags |= SEEN;
49473672 27 show(obj, path, name, cb_data);
c64ed70d
JH
28}
29
6e2f441b
LT
30/*
31 * Processing a gitlink entry currently does nothing, since
32 * we do not recurse into the subproject.
33 *
34 * We *could* eventually add a flag that actually does that,
35 * which would involve:
36 * - is the subproject actually checked out?
37 * - if so, see if the subproject has already been added
38 * to the alternates list, and add it if not.
39 * - process the commit (or tag) the gitlink points to
40 * recursively.
41 *
42 * However, it's unclear whether there is really ever any
43 * reason to see superprojects and subprojects as such a
44 * "unified" object pool (potentially resulting in a totally
45 * humongous pack - avoiding which was the whole point of
46 * having gitlinks in the first place!).
47 *
48 * So for now, there is just a note that we *could* follow
49 * the link, and how to do it. Whether it necessarily makes
50 * any sense what-so-ever to ever do that is another issue.
51 */
52static void process_gitlink(struct rev_info *revs,
53 const unsigned char *sha1,
8d2dfc49 54 show_object_fn show,
6e2f441b 55 struct name_path *path,
49473672
JH
56 const char *name,
57 void *cb_data)
6e2f441b
LT
58{
59 /* Nothing to do */
60}
61
c64ed70d
JH
62static void process_tree(struct rev_info *revs,
63 struct tree *tree,
8d2dfc49 64 show_object_fn show,
cc5fa2fd 65 struct strbuf *base,
49473672
JH
66 const char *name,
67 void *cb_data)
c64ed70d
JH
68{
69 struct object *obj = &tree->object;
70 struct tree_desc desc;
71 struct name_entry entry;
72 struct name_path me;
d688cf07
NTND
73 enum interesting match = revs->diffopt.pathspec.nr == 0 ?
74 all_entries_interesting: entry_not_interesting;
cc5fa2fd 75 int baselen = base->len;
c64ed70d
JH
76
77 if (!revs->tree_objects)
78 return;
a301b0c8
MK
79 if (!obj)
80 die("bad tree object");
c64ed70d
JH
81 if (obj->flags & (UNINTERESTING | SEEN))
82 return;
daf7d867 83 if (parse_tree_gently(tree, revs->ignore_missing_links) < 0) {
2db1a43f
VM
84 if (revs->ignore_missing_links)
85 return;
f2fd0760 86 die("bad tree object %s", oid_to_hex(&obj->oid));
2db1a43f 87 }
13528ab3 88
c64ed70d 89 obj->flags |= SEEN;
13528ab3
JK
90 me.base = base;
91 show(obj, &me, name, cb_data);
92
93 strbuf_addstr(base, name);
94 if (base->len)
95 strbuf_addch(base, '/');
cc5fa2fd 96
6fda5e51 97 init_tree_desc(&desc, tree->buffer, tree->size);
c64ed70d
JH
98
99 while (tree_entry(&desc, &entry)) {
d688cf07 100 if (match != all_entries_interesting) {
97d0b74a
NTND
101 match = tree_entry_interesting(&entry, base, 0,
102 &revs->diffopt.pathspec);
d688cf07 103 if (match == all_entries_not_interesting)
cc5fa2fd 104 break;
d688cf07 105 if (match == entry_not_interesting)
cc5fa2fd 106 continue;
cc5fa2fd
EN
107 }
108
c64ed70d
JH
109 if (S_ISDIR(entry.mode))
110 process_tree(revs,
111 lookup_tree(entry.sha1),
13528ab3 112 show, base, entry.path,
49473672 113 cb_data);
302b9282 114 else if (S_ISGITLINK(entry.mode))
6e2f441b 115 process_gitlink(revs, entry.sha1,
49473672
JH
116 show, &me, entry.path,
117 cb_data);
c64ed70d
JH
118 else
119 process_blob(revs,
120 lookup_blob(entry.sha1),
49473672
JH
121 show, &me, entry.path,
122 cb_data);
c64ed70d 123 }
cc5fa2fd 124 strbuf_setlen(base, baselen);
6e454b9a 125 free_tree_buffer(tree);
c64ed70d
JH
126}
127
8d1d8f83
JH
128static void mark_edge_parents_uninteresting(struct commit *commit,
129 struct rev_info *revs,
130 show_edge_fn show_edge)
131{
132 struct commit_list *parents;
133
134 for (parents = commit->parents; parents; parents = parents->next) {
135 struct commit *parent = parents->item;
136 if (!(parent->object.flags & UNINTERESTING))
137 continue;
138 mark_tree_uninteresting(parent->tree);
139 if (revs->edge_hint && !(parent->object.flags & SHOWN)) {
140 parent->object.flags |= SHOWN;
141 show_edge(parent);
142 }
143 }
144}
145
e76a5fb4 146void mark_edges_uninteresting(struct rev_info *revs, show_edge_fn show_edge)
8d1d8f83 147{
e76a5fb4 148 struct commit_list *list;
fbd4a703
NTND
149 int i;
150
e76a5fb4 151 for (list = revs->commits; list; list = list->next) {
8d1d8f83
JH
152 struct commit *commit = list->item;
153
154 if (commit->object.flags & UNINTERESTING) {
155 mark_tree_uninteresting(commit->tree);
1684c1b2 156 if (revs->edge_hint_aggressive && !(commit->object.flags & SHOWN)) {
fbd4a703
NTND
157 commit->object.flags |= SHOWN;
158 show_edge(commit);
159 }
8d1d8f83
JH
160 continue;
161 }
162 mark_edge_parents_uninteresting(commit, revs, show_edge);
163 }
1684c1b2 164 if (revs->edge_hint_aggressive) {
200abe74
JK
165 for (i = 0; i < revs->cmdline.nr; i++) {
166 struct object *obj = revs->cmdline.rev[i].item;
167 struct commit *commit = (struct commit *)obj;
168 if (obj->type != OBJ_COMMIT || !(obj->flags & UNINTERESTING))
169 continue;
170 mark_tree_uninteresting(commit->tree);
171 if (!(obj->flags & SHOWN)) {
172 obj->flags |= SHOWN;
173 show_edge(commit);
174 }
fbd4a703
NTND
175 }
176 }
8d1d8f83
JH
177}
178
8d2dfc49
LT
179static void add_pending_tree(struct rev_info *revs, struct tree *tree)
180{
181 add_pending_object(revs, &tree->object, "");
182}
183
c64ed70d 184void traverse_commit_list(struct rev_info *revs,
11c211fa
CC
185 show_commit_fn show_commit,
186 show_object_fn show_object,
187 void *data)
c64ed70d
JH
188{
189 int i;
190 struct commit *commit;
cc5fa2fd 191 struct strbuf base;
c64ed70d 192
cc5fa2fd 193 strbuf_init(&base, PATH_MAX);
c64ed70d 194 while ((commit = get_revision(revs)) != NULL) {
6e7d0efa
JH
195 /*
196 * an uninteresting boundary commit may not have its tree
197 * parsed yet, but we are not going to show them anyway
198 */
199 if (commit->tree)
200 add_pending_tree(revs, commit->tree);
11c211fa 201 show_commit(commit, data);
c64ed70d
JH
202 }
203 for (i = 0; i < revs->pending.nr; i++) {
204 struct object_array_entry *pending = revs->pending.objects + i;
205 struct object *obj = pending->item;
206 const char *name = pending->name;
20739490 207 const char *path = pending->path;
c64ed70d
JH
208 if (obj->flags & (UNINTERESTING | SEEN))
209 continue;
210 if (obj->type == OBJ_TAG) {
211 obj->flags |= SEEN;
49473672 212 show_object(obj, NULL, name, data);
c64ed70d
JH
213 continue;
214 }
20739490
JK
215 if (!path)
216 path = "";
c64ed70d 217 if (obj->type == OBJ_TREE) {
8d2dfc49 218 process_tree(revs, (struct tree *)obj, show_object,
13528ab3 219 &base, path, data);
c64ed70d
JH
220 continue;
221 }
222 if (obj->type == OBJ_BLOB) {
8d2dfc49 223 process_blob(revs, (struct blob *)obj, show_object,
20739490 224 NULL, path, data);
c64ed70d
JH
225 continue;
226 }
227 die("unknown pending object %s (%s)",
f2fd0760 228 oid_to_hex(&obj->oid), name);
c64ed70d 229 }
46be8231 230 object_array_clear(&revs->pending);
cc5fa2fd 231 strbuf_release(&base);
c64ed70d 232}