Skip to content

Commit fc4937c

Browse files
peffgitster
authored andcommitted
cat-file: add --buffer option
We use a direct write() to output the results of --batch and --batch-check. This is good for processes feeding the input and reading the output interactively, but it introduces measurable overhead if you do not want this feature. For example, on linux.git: $ git rev-list --objects --all | cut -d' ' -f1 >objects $ time git cat-file --batch-check='%(objectsize)' \ <objects >/dev/null real 0m5.440s user 0m5.060s sys 0m0.384s This patch adds an option to use regular stdio buffering: $ time git cat-file --batch-check='%(objectsize)' \ --buffer <objects >/dev/null real 0m4.975s user 0m4.888s sys 0m0.092s Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bfd1559 commit fc4937c

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Documentation/git-cat-file.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ OPTIONS
6969
not be combined with any other options or arguments. See the
7070
section `BATCH OUTPUT` below for details.
7171

72+
--buffer::
73+
Normally batch output is flushed after each object is output, so
74+
that a process can interactively read and write from
75+
`cat-file`. With this option, the output uses normal stdio
76+
buffering; this is much more efficient when invoking
77+
`--batch-check` on a large number of objects.
78+
7279
--allow-unknown-type::
7380
Allow -s or -t to query broken/corrupt objects of unknown type.
7481

builtin/cat-file.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct batch_options {
1414
int enabled;
1515
int follow_symlinks;
1616
int print_contents;
17+
int buffer_output;
1718
const char *format;
1819
};
1920

@@ -211,14 +212,25 @@ static size_t expand_format(struct strbuf *sb, const char *start, void *data)
211212
return end - start + 1;
212213
}
213214

214-
static void print_object_or_die(int fd, struct expand_data *data)
215+
static void batch_write(struct batch_options *opt, const void *data, int len)
216+
{
217+
if (opt->buffer_output) {
218+
if (fwrite(data, 1, len, stdout) != len)
219+
die_errno("unable to write to stdout");
220+
} else
221+
write_or_die(1, data, len);
222+
}
223+
224+
static void print_object_or_die(struct batch_options *opt, struct expand_data *data)
215225
{
216226
const unsigned char *sha1 = data->sha1;
217227

218228
assert(data->info.typep);
219229

220230
if (data->type == OBJ_BLOB) {
221-
if (stream_blob_to_fd(fd, sha1, NULL, 0) < 0)
231+
if (opt->buffer_output)
232+
fflush(stdout);
233+
if (stream_blob_to_fd(1, sha1, NULL, 0) < 0)
222234
die("unable to stream %s to stdout", sha1_to_hex(sha1));
223235
}
224236
else {
@@ -234,12 +246,11 @@ static void print_object_or_die(int fd, struct expand_data *data)
234246
if (data->info.sizep && size != data->size)
235247
die("object %s changed size!?", sha1_to_hex(sha1));
236248

237-
write_or_die(fd, contents, size);
249+
batch_write(opt, contents, size);
238250
free(contents);
239251
}
240252
}
241253

242-
243254
static int batch_one_object(const char *obj_name, struct batch_options *opt,
244255
struct expand_data *data)
245256
{
@@ -294,12 +305,12 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
294305

295306
strbuf_expand(&buf, opt->format, expand_format, data);
296307
strbuf_addch(&buf, '\n');
297-
write_or_die(1, buf.buf, buf.len);
308+
batch_write(opt, buf.buf, buf.len);
298309
strbuf_release(&buf);
299310

300311
if (opt->print_contents) {
301-
print_object_or_die(1, data);
302-
write_or_die(1, "\n", 1);
312+
print_object_or_die(opt, data);
313+
batch_write(opt, "\n", 1);
303314
}
304315
return 0;
305316
}
@@ -415,6 +426,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
415426
N_("for blob objects, run textconv on object's content"), 'c'),
416427
OPT_BOOL(0, "allow-unknown-type", &unknown_type,
417428
N_("allow -s and -t to work with broken/corrupt objects")),
429+
OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
418430
{ OPTION_CALLBACK, 0, "batch", &batch, "format",
419431
N_("show info and content of objects fed from the standard input"),
420432
PARSE_OPT_OPTARG, batch_option_callback },

0 commit comments

Comments
 (0)