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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Nov 2, 2017 at 12:41 UTC
91904f5645196ceef92c6fca21cc9454928613f0
1 file changed
+31
-19
list-objects.c
+31
-19
@@ -183,25 +183,15 @@ static void add_pending_tree(struct rev_info *revs, struct tree *tree)
183
add_pending_object(revs, &tree->object, "");
184
}
185
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)
190
{
191
int i;
192
- struct commit *commit;
193
- struct strbuf base;
192
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
+
195
for (i = 0; i < revs->pending.nr; i++) {
196
struct object_array_entry *pending = revs->pending.objects + i;
197
struct object *obj = pending->item;
@@ -218,17 +208,39 @@ void traverse_commit_list(struct rev_info *revs,
208
path = "";
209
if (obj->type == OBJ_TREE) {
210
process_tree(revs, (struct tree *)obj, show_object,
221
- &base, path, data);
211
+ base, path, data);
212
continue;
213
}
214
if (obj->type == OBJ_BLOB) {
215
process_blob(revs, (struct blob *)obj, show_object,
226
- &base, path, data);
216
+ base, path, data);
217
continue;
218
}
219
die("unknown pending object %s (%s)",
220
oid_to_hex(&obj->oid), name);
221
}
222
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);
246
}