cat-file: simplify reading from standard input

The batch modes of git-cat-file(1) read queries from stantard input that are either newline- or NUL-delimited. This code was introduced via db9d67f2e9 (builtin/cat-file.c: support NUL-delimited input with `-z`, 2022-07-22), which notes that: """ 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; } """ The commit proposed introducing a helper function that is easier to use, which is just what we have done in the preceding commit. Refactor the code to use this new helper to simplify the loop. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 6, 2023 at 07:19 UTC 3217f52a499d5264d98065374a62deb15717b727
1 file changed +9 -23
builtin/cat-file.c
+9 -23
@@ -42,7 +42,7 @@ struct batch_options {
42 int all_objects;
43 int unordered;
44 int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
45 - int nul_terminated;
45 + char input_delim;
46 const char *format;
47 };
48
@@ -694,20 +694,12 @@ static void batch_objects_command(struct batch_options *opt,
694 struct queued_cmd *queued_cmd = NULL;
695 size_t alloc = 0, nr = 0;
696
697 - while (1) {
698 - int i, ret;
697 + while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
698 + int i;
699 const struct parse_cmd *cmd = NULL;
700 const char *p = NULL, *cmd_end;
701 struct queued_cmd call = {0};
702
703 - if (opt->nul_terminated)
704 - ret = strbuf_getline_nul(&input, stdin);
705 - else
706 - ret = strbuf_getline(&input, stdin);
707 -
708 - if (ret)
709 - break;
710 -
703 if (!input.len)
704 die(_("empty command in input"));
705 if (isspace(*input.buf))
@@ -851,16 +843,7 @@ static int batch_objects(struct batch_options *opt)
843 goto cleanup;
844 }
845
854 - while (1) {
855 - int ret;
856 - if (opt->nul_terminated)
857 - ret = strbuf_getline_nul(&input, stdin);
858 - else
859 - ret = strbuf_getline(&input, stdin);
860 -
861 - if (ret == EOF)
862 - break;
863 -
846 + while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
847 if (data.split_on_whitespace) {
848 /*
849 * Split at first whitespace, tying off the beginning
@@ -929,6 +912,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
912 const char *exp_type = NULL, *obj_name = NULL;
913 struct batch_options batch = {0};
914 int unknown_type = 0;
915 + int input_nul_terminated = 0;
916
917 const char * const usage[] = {
918 N_("git cat-file <type> <object>"),
@@ -965,7 +949,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
949 N_("like --batch, but don't emit <contents>"),
950 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
951 batch_option_callback),
968 - OPT_BOOL('z', NULL, &batch.nul_terminated, N_("stdin is NUL-terminated")),
952 + OPT_BOOL('z', NULL, &input_nul_terminated, N_("stdin is NUL-terminated")),
953 OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
954 N_("read commands from stdin"),
955 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -1024,10 +1008,12 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
1008 else if (batch.all_objects)
1009 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1010 "--batch-all-objects");
1027 - else if (batch.nul_terminated)
1011 + else if (input_nul_terminated)
1012 usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
1013 "-z");
1014
1015 + batch.input_delim = input_nul_terminated ? '\0' : '\n';
1016 +
1017 /* Batch defaults */
1018 if (batch.buffer_output < 0)
1019 batch.buffer_output = batch.all_objects;