git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/5] cat-file: simplify reading from standard input

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 6, 2023, 05:19 UTC
Message-ID
<e7cba8dc4cebde0841ac04702155890d87e74492.1686028409.git.ps@pks.im>
In-Reply-To
<cover.1686028409.git.ps@pks.im>

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>
---
 builtin/cat-file.c | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 0bafc14e6c..001dcb24d6 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -42,7 +42,7 @@ struct batch_options {
 	int all_objects;
 	int unordered;
 	int transform_mode; /* may be 'w' or 'c' for --filters or --textconv */
-	int nul_terminated;
+	char input_delim;
 	const char *format;
 };
 
@@ -694,20 +694,12 @@ static void batch_objects_command(struct batch_options *opt,
 	struct queued_cmd *queued_cmd = NULL;
 	size_t alloc = 0, nr = 0;
 
-	while (1) {
-		int i, ret;
+	while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
+		int i;
 		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))
@@ -851,16 +843,7 @@ static int batch_objects(struct batch_options *opt)
 		goto cleanup;
 	}
 
-	while (1) {
-		int ret;
-		if (opt->nul_terminated)
-			ret = strbuf_getline_nul(&input, stdin);
-		else
-			ret = strbuf_getline(&input, stdin);
-
-		if (ret == EOF)
-			break;
-
+	while (strbuf_getdelim_strip_crlf(&input, stdin, opt->input_delim) != EOF) {
 		if (data.split_on_whitespace) {
 			/*
 			 * Split at first whitespace, tying off the beginning
@@ -929,6 +912,7 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 	const char *exp_type = NULL, *obj_name = NULL;
 	struct batch_options batch = {0};
 	int unknown_type = 0;
+	int input_nul_terminated = 0;
 
 	const char * const usage[] = {
 		N_("git cat-file <type> <object>"),
@@ -965,7 +949,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_BOOL('z', NULL, &input_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,
@@ -1024,10 +1008,12 @@ 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)
+	else if (input_nul_terminated)
 		usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
 			       "-z");
 
+	batch.input_delim = input_nul_terminated ? '\0' : '\n';
+
 	/* Batch defaults */
 	if (batch.buffer_output < 0)
 		batch.buffer_output = batch.all_objects;
-- 
2.41.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 20 of 22 in “cat-file: introduce NUL-terminated output format”
  1. 0/5 cat-file: introduce NUL-terminated output formatPatrick Steinhardt, Jun 2, 2023
  2. 1/5 t1006: don't strip timestamps from expected resultsPatrick Steinhardt, Jun 2, 2023
  3. 2/5 t1006: modernize test style to use `test_cmp`Patrick Steinhardt, Jun 2, 2023
  4. 3/5 strbuf: provide CRLF-aware helper to read until a specified delimiterPatrick Steinhardt, Jun 2, 2023
  5. 4/5 cat-file: simplify reading from standard inputPatrick Steinhardt, Jun 2, 2023
  6. 5/5 cat-file: Introduce new option to delimit output with NUL charactersPatrick Steinhardt, Jun 2, 2023
  7. Phillip WoodJun 5, 2023
  8. Junio C HamanoJun 5, 2023
  9. Patrick SteinhardtJun 6, 2023
  10. Junio C HamanoJun 6, 2023
  11. Patrick SteinhardtJun 6, 2023
  12. Junio C HamanoJun 12, 2023
  13. Patrick SteinhardtJun 6, 2023
  14. Junio C HamanoJun 6, 2023
  15. Junio C HamanoJun 3, 2023
  16. 0/5 catfile: introduce NUL-terminated output formatPatrick Steinhardt, Jun 6, 2023
  17. 1/5 t1006: don't strip timestamps from expected resultsPatrick Steinhardt, Jun 6, 2023
  18. 2/5 t1006: modernize test style to use `test_cmp`Patrick Steinhardt, Jun 6, 2023
  19. 3/5 strbuf: provide CRLF-aware helper to read until a specified delimiterPatrick Steinhardt, Jun 6, 2023
  20. 4/5 cat-file: simplify reading from standard inputPatrick Steinhardt, Jun 6, 2023
  21. 5/5 cat-file: introduce option to delimit input and output with NULPatrick Steinhardt, Jun 6, 2023
  22. Junio C HamanoJun 12, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.