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

STALE1742d

3 messages, 1 author, 2021-11-05 · open the first message on its own page

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

From: John Cai via GitGitGadget <hidden>
Date: 2021-11-05 21:56:45

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.

John Cai (2):
  cat-file: force flush of stdout on empty string
  docs: update behavior of git-cat-file --buffer

 Documentation/git-cat-file.txt |  3 ++-
 builtin/cat-file.c             | 11 ++++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)


base-commit: 6d82a21a3b699caf378cb0f89b6b0e803fc58480
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1124%2Fjohn-cai%2Fjc%2Fflush-buffer-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1124/john-cai/jc/flush-buffer-v1
Pull-Request: https://github.com/git/git/pull/1124
-- 
gitgitgadget

[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

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

[PATCH 2/2] docs: update behavior of git-cat-file --buffer

From: John Cai via GitGitGadget <hidden>
Date: 2021-11-05 21:56:47

From: John Cai <redacted>

When an empty string is entered into stdin, git-cat-file --buffer will
flush stdout immediately. This commit updates the man page accordingly.

Signed-off-by: John Cai <redacted>
---
 Documentation/git-cat-file.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
index 27b27e2b300..c98e8dc3669 100644
--- a/Documentation/git-cat-file.txt
+++ b/Documentation/git-cat-file.txt
@@ -104,7 +104,8 @@ OPTIONS
 	that a process can interactively read and write from
 	`cat-file`. With this option, the output uses normal stdio
 	buffering; this is much more efficient when invoking
-	`--batch-check` on a large number of objects.
+	`--batch-check` on a large number of objects. An empty string will
+	force a flush of stdout.
 
 --unordered::
 	When `--batch-all-objects` is in use, visit objects in an
-- 
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