Skip to content

Commit db9d67f

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3639fef commit db9d67f

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

Documentation/git-cat-file.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SYNOPSIS
1414
'git cat-file' (-t | -s) [--allow-unknown-type] <object>
1515
'git cat-file' (--batch | --batch-check | --batch-command) [--batch-all-objects]
1616
[--buffer] [--follow-symlinks] [--unordered]
17-
[--textconv | --filters]
17+
[--textconv | --filters] [-z]
1818
'git cat-file' (--textconv | --filters)
1919
[<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]
2020

@@ -207,6 +207,11 @@ respectively print:
207207
/etc/passwd
208208
--
209209

210+
-z::
211+
Only meaningful with `--batch`, `--batch-check`, or
212+
`--batch-command`; input is NUL-delimited instead of
213+
newline-delimited.
214+
210215

211216
OUTPUT
212217
------

builtin/cat-file.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct batch_options {
3131
int all_objects;
3232
int unordered;
3333
int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
34+
int nul_terminated;
3435
const char *format;
3536
};
3637

@@ -614,12 +615,20 @@ static void batch_objects_command(struct batch_options *opt,
614615
struct queued_cmd *queued_cmd = NULL;
615616
size_t alloc = 0, nr = 0;
616617

617-
while (!strbuf_getline(&input, stdin)) {
618-
int i;
618+
while (1) {
619+
int i, ret;
619620
const struct parse_cmd *cmd = NULL;
620621
const char *p = NULL, *cmd_end;
621622
struct queued_cmd call = {0};
622623

624+
if (opt->nul_terminated)
625+
ret = strbuf_getline_nul(&input, stdin);
626+
else
627+
ret = strbuf_getline(&input, stdin);
628+
629+
if (ret)
630+
break;
631+
623632
if (!input.len)
624633
die(_("empty command in input"));
625634
if (isspace(*input.buf))
@@ -763,7 +772,16 @@ static int batch_objects(struct batch_options *opt)
763772
goto cleanup;
764773
}
765774

766-
while (strbuf_getline(&input, stdin) != EOF) {
775+
while (1) {
776+
int ret;
777+
if (opt->nul_terminated)
778+
ret = strbuf_getline_nul(&input, stdin);
779+
else
780+
ret = strbuf_getline(&input, stdin);
781+
782+
if (ret == EOF)
783+
break;
784+
767785
if (data.split_on_whitespace) {
768786
/*
769787
* Split at first whitespace, tying off the beginning
@@ -866,6 +884,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
866884
N_("like --batch, but don't emit <contents>"),
867885
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
868886
batch_option_callback),
887+
OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
869888
OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
870889
N_("read commands from stdin"),
871890
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -921,6 +940,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
921940
else if (batch.all_objects)
922941
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
923942
"--batch-all-objects");
943+
else if (batch.nul_terminated)
944+
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
945+
"-z");
924946

925947
/* Batch defaults */
926948
if (batch.buffer_output < 0)

t/t1006-cat-file.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ done
8888

8989
for opt in --buffer \
9090
--follow-symlinks \
91-
--batch-all-objects
91+
--batch-all-objects \
92+
-z
9293
do
9394
test_expect_success "usage: bad option combination: $opt without batch mode" '
9495
test_incompatible_usage git cat-file $opt &&
@@ -100,6 +101,10 @@ echo_without_newline () {
100101
printf '%s' "$*"
101102
}
102103

104+
echo_without_newline_nul () {
105+
echo_without_newline "$@" | tr '\n' '\0'
106+
}
107+
103108
strlen () {
104109
echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
105110
}
@@ -398,6 +403,12 @@ test_expect_success '--batch with multiple sha1s gives correct format' '
398403
test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
399404
'
400405

406+
test_expect_success '--batch, -z with multiple sha1s gives correct format' '
407+
echo_without_newline_nul "$batch_input" >in &&
408+
test "$(maybe_remove_timestamp "$batch_output" 1)" = \
409+
"$(maybe_remove_timestamp "$(git cat-file --batch -z <in)" 1)"
410+
'
411+
401412
batch_check_input="$hello_sha1
402413
$tree_sha1
403414
$commit_sha1
@@ -418,6 +429,24 @@ test_expect_success "--batch-check with multiple sha1s gives correct format" '
418429
"$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
419430
'
420431

432+
test_expect_success "--batch-check, -z with multiple sha1s gives correct format" '
433+
echo_without_newline_nul "$batch_check_input" >in &&
434+
test "$batch_check_output" = "$(git cat-file --batch-check -z <in)"
435+
'
436+
437+
test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
438+
touch -- "newline${LF}embedded" &&
439+
git add -- "newline${LF}embedded" &&
440+
git commit -m "file with newline embedded" &&
441+
test_tick &&
442+
443+
printf "HEAD:newline${LF}embedded" >in &&
444+
git cat-file --batch-check -z <in >actual &&
445+
446+
echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
447+
test_cmp expect actual
448+
'
449+
421450
batch_command_multiple_info="info $hello_sha1
422451
info $tree_sha1
423452
info $commit_sha1
@@ -436,6 +465,11 @@ test_expect_success '--batch-command with multiple info calls gives correct form
436465
echo "$batch_command_multiple_info" >in &&
437466
git cat-file --batch-command --buffer <in >actual &&
438467
468+
test_cmp expect actual &&
469+
470+
echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
471+
git cat-file --batch-command --buffer -z <in >actual &&
472+
439473
test_cmp expect actual
440474
'
441475

@@ -459,6 +493,12 @@ test_expect_success '--batch-command with multiple command calls gives correct f
459493
echo "$batch_command_multiple_contents" >in &&
460494
git cat-file --batch-command --buffer <in >actual_raw &&
461495
496+
remove_timestamp <actual_raw >actual &&
497+
test_cmp expect actual &&
498+
499+
echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
500+
git cat-file --batch-command --buffer -z <in >actual_raw &&
501+
462502
remove_timestamp <actual_raw >actual &&
463503
test_cmp expect actual
464504
'

0 commit comments

Comments
 (0)