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

[PATCH 1/2] cat-file: force flush of stdout on empty string

From
John Cai via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 5, 2021, 21:56 UTC
Message-ID
<2d687baeed82e7b90d383bad8e209f50e0ce8c87.1636149400.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1124.git.git.1636149400.gitgitgadget@gmail.com>
From: John Cai <johncai86@gmail.com>

When in --buffer mode, it is very useful for the caller to have control over when the buffer is flushed. Currently there is no convenient way to signal for the buffer to be flushed. One workaround is to provide any nonexisting commit to git-cat-file's stdin, in which case the buffer will be flushed and a "$FOO missing" message will be displayed. However, this is not an ideal workaround.

Instead, this commit teaches git-cat-file to look for an empty string in stdin, which will trigger a flush of stdout.

Signed-off-by: John Cai <johncai86@gmail.com>
---
 builtin/cat-file.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 86fc03242b8..4d17f30f24e 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -405,6 +405,11 @@ static void batch_one_object(const char *obj_name,
 	int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0;
 	enum get_oid_result result;
 
+	if (opt->buffer_output && obj_name[0] == '\0') {
+		fflush(stdout);
+		return;
+	}
+
 	result = get_oid_with_context(the_repository, obj_name,
 				      flags, &data->oid, &ctx);
 	if (result != FOUND) {
@@ -609,7 +614,11 @@ static int batch_objects(struct batch_options *opt)
 			data.rest = p;
 		}
 
-		batch_one_object(input.buf, &output, opt, &data);
+		 /*
+		  * When in buffer mode and input.buf is an empty string,
+		  * flush to stdout.
+		  */
+		 batch_one_object(input.buf, &output, opt, &data);
 	}
 
 	strbuf_release(&input);
-- 
gitgitgadget
Previous: John Cai via GitGitGadgetNext: Junio C Hamano
Message 2 of 8 in “cat-file: force flush of stdout on empty string”
  1. 0/2 cat-file: force flush of stdout on empty stringJohn Cai via GitGitGadget, Nov 5, 2021
  2. 1/2 cat-file: force flush of stdout on empty stringJohn Cai via GitGitGadget, Nov 5, 2021
  3. Junio C HamanoNov 6, 2021
  4. Ævar Arnfjörð BjarmasonNov 6, 2021
  5. John CaiNov 8, 2021
  6. Ævar Arnfjörð BjarmasonNov 8, 2021
  7. Junio C HamanoNov 8, 2021
  8. 2/2 docs: update behavior of git-cat-file --bufferJohn Cai via GitGitGadget, Nov 5, 2021

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.