Skip to content

Commit 91904f5

Browse files
stefanbellergitster
authored andcommitted
list-objects.c: factor out traverse_trees_and_blobs
With traverse_trees_and_blobs factored out of the main traverse function, the next patch can introduce an in-order revision walking with ease. In the next patch we'll call `traverse_trees_and_blobs` from within the loop walking the commits, such that we'll have one invocation of that function per commit. That is why we do not want to have memory allocations in that function, such as we'd have if we were to use a strbuf locally. Pass a strbuf from traverse_commit_list into the blob and tree traversing function as a scratch pad that only needs to be allocated once. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2deda00 commit 91904f5

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

list-objects.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,15 @@ static void add_pending_tree(struct rev_info *revs, struct tree *tree)
183183
add_pending_object(revs, &tree->object, "");
184184
}
185185

186-
void traverse_commit_list(struct rev_info *revs,
187-
show_commit_fn show_commit,
188-
show_object_fn show_object,
189-
void *data)
186+
static void traverse_trees_and_blobs(struct rev_info *revs,
187+
struct strbuf *base,
188+
show_object_fn show_object,
189+
void *data)
190190
{
191191
int i;
192-
struct commit *commit;
193-
struct strbuf base;
194192

195-
strbuf_init(&base, PATH_MAX);
196-
while ((commit = get_revision(revs)) != NULL) {
197-
/*
198-
* an uninteresting boundary commit may not have its tree
199-
* parsed yet, but we are not going to show them anyway
200-
*/
201-
if (commit->tree)
202-
add_pending_tree(revs, commit->tree);
203-
show_commit(commit, data);
204-
}
193+
assert(base->len == 0);
194+
205195
for (i = 0; i < revs->pending.nr; i++) {
206196
struct object_array_entry *pending = revs->pending.objects + i;
207197
struct object *obj = pending->item;
@@ -218,17 +208,39 @@ void traverse_commit_list(struct rev_info *revs,
218208
path = "";
219209
if (obj->type == OBJ_TREE) {
220210
process_tree(revs, (struct tree *)obj, show_object,
221-
&base, path, data);
211+
base, path, data);
222212
continue;
223213
}
224214
if (obj->type == OBJ_BLOB) {
225215
process_blob(revs, (struct blob *)obj, show_object,
226-
&base, path, data);
216+
base, path, data);
227217
continue;
228218
}
229219
die("unknown pending object %s (%s)",
230220
oid_to_hex(&obj->oid), name);
231221
}
232222
object_array_clear(&revs->pending);
233-
strbuf_release(&base);
223+
}
224+
225+
void traverse_commit_list(struct rev_info *revs,
226+
show_commit_fn show_commit,
227+
show_object_fn show_object,
228+
void *data)
229+
{
230+
struct commit *commit;
231+
struct strbuf csp; /* callee's scratch pad */
232+
strbuf_init(&csp, PATH_MAX);
233+
234+
while ((commit = get_revision(revs)) != NULL) {
235+
/*
236+
* an uninteresting boundary commit may not have its tree
237+
* parsed yet, but we are not going to show them anyway
238+
*/
239+
if (commit->tree)
240+
add_pending_tree(revs, commit->tree);
241+
show_commit(commit, data);
242+
}
243+
traverse_trees_and_blobs(revs, &csp, show_object, data);
244+
245+
strbuf_release(&csp);
234246
}

0 commit comments

Comments
 (0)