Thread (3 messages) 3 messages, 1 author, 2021-11-05

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

From: John Cai via GitGitGadget <hidden>
Date: 2021-11-05 21:56:45
Subsystem: the rest · Maintainer: Linus Torvalds

From: John Cai <redacted>

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help