Skip to content

Commit 3217f52

Browse files
pks-tgitster
authored andcommitted
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 db9d67f (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af35e56 commit 3217f52

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

builtin/cat-file.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct batch_options {
4242
int all_objects;
4343
int unordered;
4444
int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
45-
int nul_terminated;
45+
char input_delim;
4646
const char *format;
4747
};
4848

@@ -694,20 +694,12 @@ static void batch_objects_command(struct batch_options *opt,
694694
struct queued_cmd *queued_cmd = NULL;
695695
size_t alloc = 0, nr = 0;
696696

697-
while (1) {
698-
int i, ret;
697+
while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
698+
int i;
699699
const struct parse_cmd *cmd = NULL;
700700
const char *p = NULL, *cmd_end;
701701
struct queued_cmd call = {0};
702702

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-
711703
if (!input.len)
712704
die(_("empty command in input"));
713705
if (isspace(*input.buf))
@@ -851,16 +843,7 @@ static int batch_objects(struct batch_options *opt)
851843
goto cleanup;
852844
}
853845

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) {
864847
if (data.split_on_whitespace) {
865848
/*
866849
* Split at first whitespace, tying off the beginning
@@ -929,6 +912,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
929912
const char *exp_type = NULL, *obj_name = NULL;
930913
struct batch_options batch = {0};
931914
int unknown_type = 0;
915+
int input_nul_terminated = 0;
932916

933917
const char * const usage[] = {
934918
N_("git cat-file <type> <object>"),
@@ -965,7 +949,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
965949
N_("like --batch, but don't emit <contents>"),
966950
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
967951
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")),
969953
OPT_CALLBACK_F(0, "batch-command", &batch, N_("format"),
970954
N_("read commands from stdin"),
971955
PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
@@ -1024,10 +1008,12 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
10241008
else if (batch.all_objects)
10251009
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
10261010
"--batch-all-objects");
1027-
else if (batch.nul_terminated)
1011+
else if (input_nul_terminated)
10281012
usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
10291013
"-z");
10301014

1015+
batch.input_delim = input_nul_terminated ? '\0' : '\n';
1016+
10311017
/* Batch defaults */
10321018
if (batch.buffer_output < 0)
10331019
batch.buffer_output = batch.all_objects;

0 commit comments

Comments
 (0)