14
int enabled;
15
int follow_symlinks;
16
int print_contents;
17
+ int buffer_output;
18
const char *format;
19
};
20
212
return end - start + 1;
213
}
214
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)
225
{
226
const unsigned char *sha1 = data->sha1;
227
228
assert(data->info.typep);
229
230
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)
234
die("unable to stream %s to stdout", sha1_to_hex(sha1));
235
}
236
else {
246
if (data->info.sizep && size != data->size)
247
die("object %s changed size!?", sha1_to_hex(sha1));
248
237
- write_or_die(fd, contents, size);
249
+ batch_write(opt, contents, size);
250
free(contents);
251
}
252
}
253
242
-
254
static int batch_one_object(const char *obj_name, struct batch_options *opt,
255
struct expand_data *data)
256
{
305
306
strbuf_expand(&buf, opt->format, expand_format, data);
307
strbuf_addch(&buf, '\n');
297
- write_or_die(1, buf.buf, buf.len);
308
+ batch_write(opt, buf.buf, buf.len);
309
strbuf_release(&buf);
310
311
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);
314
}
315
return 0;
316
}
426
N_("for blob objects, run textconv on object's content"), 'c'),
427
OPT_BOOL(0, "allow-unknown-type", &unknown_type,
428
N_("allow -s and -t to work with broken/corrupt objects")),
429
+ OPT_BOOL(0, "buffer", &batch.buffer_output, N_("buffer --batch output")),
430
{ OPTION_CALLBACK, 0, "batch", &batch, "format",
431
N_("show info and content of objects fed from the standard input"),
432
PARSE_OPT_OPTARG, batch_option_callback },