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>

Taylor Blau committed Jul 22, 2022 at 19:29 UTC db9d67f2e9c9ea389f5558d6a168460d51631769
3 files changed +72 -5
Documentation/git-cat-file.txt
+6 -1
@@ -14,7 +14,7 @@ SYNOPSIS
14 'git cat-file' (-t | -s) [--allow-unknown-type] <object>
15 'git cat-file' (--batch | --batch-check | --batch-command) [--batch-all-objects]
16 [--buffer] [--follow-symlinks] [--unordered]
17 - [--textconv | --filters]
17 + [--textconv | --filters] [-z]
18 'git cat-file' (--textconv | --filters)
19 [<rev>:<path|tree-ish> | --path=<path|tree-ish> <rev>]
20
@@ -207,6 +207,11 @@ respectively print:
207 /etc/passwd
208 --
209
210 +-z::
211 + Only meaningful with `--batch`, `--batch-check`, or
212 + `--batch-command`; input is NUL-delimited instead of
213 + newline-delimited.
214 +
215
216 OUTPUT
217 ------
builtin/cat-file.c
+25 -3
@@ -31,6 +31,7 @@ struct batch_options {
31 int all_objects;
32 int unordered;
33 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
34 + int nul_terminated;
35 const char *format;
36 };
37
@@ -614,12 +615,20 @@ static void batch_objects_command(struct batch_options *opt,
615 struct queued_cmd *queued_cmd = NULL;
616 size_t alloc = 0, nr = 0;
617
617 - while (!strbuf_getline(&input, stdin)) {
618 - int i;
618 + while (1) {
619 + int i, ret;
620 const struct parse_cmd *cmd = NULL;
621 const char *p = NULL, *cmd_end;
622 struct queued_cmd call = {0};
623
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 +
632 if (!input.len)
633 die(_("empty command in input"));
634 if (isspace(*input.buf))
@@ -763,7 +772,16 @@ static int batch_objects(struct batch_options *opt)
772 goto cleanup;
773 }
774
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 +
785 if (data.split_on_whitespace) {
786 /*
787 * Split at first whitespace, tying off the beginning
@@ -866,6 +884,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
884 N_("like --batch, but don't emit <contents>"),
885 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
886 batch_option_callback),
887 + OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
888 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
889 N_("read commands from stdin"),
890 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -921,6 +940,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
940 else if (batch.all_objects)
941 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
942 "--batch-all-objects");
943 + else if (batch.nul_terminated)
944 + usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
945 + "-z");
946
947 /* Batch defaults */
948 if (batch.buffer_output < 0)
t/t1006-cat-file.sh
+41 -1
@@ -88,7 +88,8 @@ done
88
89 for opt in --buffer \
90 --follow-symlinks \
91 - --batch-all-objects
91 + --batch-all-objects \
92 + -z
93 do
94 test_expect_success "usage: bad option combination: $opt without batch mode" '
95 test_incompatible_usage git cat-file $opt &&
@@ -100,6 +101,10 @@ echo_without_newline () {
101 printf '%s' "$*"
102 }
103
104 +echo_without_newline_nul () {
105 + echo_without_newline "$@" | tr '\n' '\0'
106 +}
107 +
108 strlen () {
109 echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
110 }
@@ -398,6 +403,12 @@ test_expect_success '--batch with multiple sha1s gives correct format' '
403 test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)"
404 '
405
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 +
412 batch_check_input="$hello_sha1
413 $tree_sha1
414 $commit_sha1
@@ -418,6 +429,24 @@ test_expect_success "--batch-check with multiple sha1s gives correct format" '
429 "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
430 '
431
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 +
450 batch_command_multiple_info="info $hello_sha1
451 info $tree_sha1
452 info $commit_sha1
@@ -436,6 +465,11 @@ test_expect_success '--batch-command with multiple info calls gives correct form
465 echo "$batch_command_multiple_info" >in &&
466 git cat-file --batch-command --buffer <in >actual &&
467
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 +
473 test_cmp expect actual
474 '
475
@@ -459,6 +493,12 @@ test_expect_success '--batch-command with multiple command calls gives correct f
493 echo "$batch_command_multiple_contents" >in &&
494 git cat-file --batch-command --buffer <in >actual_raw &&
495
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 +
502 remove_timestamp <actual_raw >actual &&
503 test_cmp expect actual
504 '