Skip to content

Commit 44b877e

Browse files
peffgitster
authored andcommitted
cat-file: split batch_one_object into two stages
There are really two things going on in this function: 1. We convert the name we got on stdin to a sha1. 2. We look up and print information on the sha1. Let's split out the second half so that we can call it separately. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8233095 commit 44b877e

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

builtin/cat-file.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,31 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
251251
}
252252
}
253253

254+
static void batch_object_write(const char *obj_name, struct batch_options *opt,
255+
struct expand_data *data)
256+
{
257+
struct strbuf buf = STRBUF_INIT;
258+
259+
if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
260+
printf("%s missing\n", obj_name);
261+
fflush(stdout);
262+
return;
263+
}
264+
265+
strbuf_expand(&buf, opt->format, expand_format, data);
266+
strbuf_addch(&buf, '\n');
267+
batch_write(opt, buf.buf, buf.len);
268+
strbuf_release(&buf);
269+
270+
if (opt->print_contents) {
271+
print_object_or_die(opt, data);
272+
batch_write(opt, "\n", 1);
273+
}
274+
}
275+
254276
static void batch_one_object(const char *obj_name, struct batch_options *opt,
255277
struct expand_data *data)
256278
{
257-
struct strbuf buf = STRBUF_INIT;
258279
struct object_context ctx;
259280
int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
260281
enum follow_symlinks_result result;
@@ -294,21 +315,7 @@ static void batch_one_object(const char *obj_name, struct batch_options *opt,
294315
return;
295316
}
296317

297-
if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
298-
printf("%s missing\n", obj_name);
299-
fflush(stdout);
300-
return;
301-
}
302-
303-
strbuf_expand(&buf, opt->format, expand_format, data);
304-
strbuf_addch(&buf, '\n');
305-
batch_write(opt, buf.buf, buf.len);
306-
strbuf_release(&buf);
307-
308-
if (opt->print_contents) {
309-
print_object_or_die(opt, data);
310-
batch_write(opt, "\n", 1);
311-
}
318+
batch_object_write(obj_name, opt, data);
312319
}
313320

314321
static int batch_objects(struct batch_options *opt)

0 commit comments

Comments
 (0)