builtin/cat-file.c: support NUL-delimited input with -z
When callers are using `cat-file` via one of the stdin-driven `--batch` modes, all input is newline-delimited. This presents a problem when callers wish to ask about, e.g. tree-entries that have a newline character present in their filename. To support this niche scenario, introduce a new `-z` mode to the `--batch`, `--batch-check`, and `--batch-command` suite of options that instructs `cat-file` to treat its input as NUL-delimited, allowing the individual commands themselves to have newlines present. The refactoring here is slightly unfortunate, since we turn loops like: while (strbuf_getline(&buf, stdin) != EOF) into: while (1) { int ret; if (opt->nul_terminated) ret = strbuf_getline_nul(&input, stdin); else ret = strbuf_getline(&input, stdin); if (ret == EOF) break; } It's tempting to think that we could use `strbuf_getwholeline()` and specify either `\n` or `\0` as the terminating character. But for input on platforms that include a CR character preceeding the LF, this wouldn't quite be the same, since `strbuf_getline(...)` will trim any trailing CR, while `strbuf_getwholeline(&buf, stdin, '\n')` will not. In the future, we could clean this up further by introducing a variant of `strbuf_getwholeline()` that addresses the aforementioned gap, but that approach felt too heavy-handed for this pair of uses. Some tests are added in t1006 to ensure that `cat-file` produces the same output in `--batch`, `--batch-check`, and `--batch-command` modes with and without the new `-z` option. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
3639fefe7d
commit
db9d67f2e9
@ -31,6 +31,7 @@ struct batch_options {
|
||||
int all_objects;
|
||||
int unordered;
|
||||
int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
|
||||
int nul_terminated;
|
||||
const char *format;
|
||||
};
|
||||
|
||||
@ -614,12 +615,20 @@ static void batch_objects_command(struct batch_options *opt,
|
||||
struct queued_cmd *queued_cmd = NULL;
|
||||
size_t alloc = 0, nr = 0;
|
||||
|
||||
while (!strbuf_getline(&input, stdin)) {
|
||||
int i;
|
||||
while (1) {
|
||||
int i, ret;
|
||||
const struct parse_cmd *cmd = NULL;
|
||||
const char *p = NULL, *cmd_end;
|
||||
struct queued_cmd call = {0};
|
||||
|
||||
if (opt->nul_terminated)
|
||||
ret = strbuf_getline_nul(&input, stdin);
|
||||
else
|
||||
ret = strbuf_getline(&input, stdin);
|
||||
|
||||
if (ret)
|
||||
break;
|
||||
|
||||
if (!input.len)
|
||||
die(_("empty command in input"));
|
||||
if (isspace(*input.buf))
|
||||
@ -763,7 +772,16 @@ static int batch_objects(struct batch_options *opt)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
while (strbuf_getline(&input, stdin) != EOF) {
|
||||
while (1) {
|
||||
int ret;
|
||||
if (opt->nul_terminated)
|
||||
ret = strbuf_getline_nul(&input, stdin);
|
||||
else
|
||||
ret = strbuf_getline(&input, stdin);
|
||||
|
||||
if (ret == EOF)
|
||||
break;
|
||||
|
||||
if (data.split_on_whitespace) {
|
||||
/*
|
||||
* Split at first whitespace, tying off the beginning
|
||||
@ -866,6 +884,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
|
||||
N_("like --batch, but don't emit <contents>"),
|
||||
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
|
||||
batch_option_callback),
|
||||
OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
|
||||
OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
|
||||
N_("read commands from stdin"),
|
||||
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
|
||||
@ -921,6 +940,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
|
||||
else if (batch.all_objects)
|
||||
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
|
||||
"--batch-all-objects");
|
||||
else if (batch.nul_terminated)
|
||||
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
|
||||
"-z");
|
||||
|
||||
/* Batch defaults */
|
||||
if (batch.buffer_output < 0)
|
||||
|
Reference in New Issue
Block a user