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

[PATCH] Use line buffering for standard output

From
AMAnders Melchiorsen <mail@cup.kalibalik.dk>
Date
Aug 3, 2008, 21:26 UTC
Message-ID
<1217798768-18021-1-git-send-email-mail@cup.kalibalik.dk>

Normally, stdout is fully buffered, unless it refers to a terminal device. This gives problems when fork() is in play: the buffer is cloned and output appears twice.

By always setting stdout to line buffering, we make the output work identically for all output devices.

Signed-off-by: Anders Melchiorsen <mail@cup.kalibalik.dk>
---

On #git, blix mentioned that running git clone through a pipe made it output the "Initialized empty" line twice. This seems to be due to bad interactions between fork() and buffered stdio.

Rather than putting in flushing at all the right places, this sledgehammer fix simply reverts to line buffering for all output devices.

Anders.
 git.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/git.c b/git.c
index 37b1d76..040b900 100644
--- a/git.c
+++ b/git.c
@@ -421,6 +421,12 @@ int main(int argc, const char **argv)
 	int done_alias = 0;
 
 	/*
+	 * Use line buffering, even if we do not have interactive
+	 * output. Full buffering mixes badly with fork().
+	 */
+	setvbuf(stdout, NULL, _IOLBF, 0);
+
+	/*
 	 * Take the basename of argv[0] as the command
 	 * name, and the dirname as the default exec_path
 	 * if we don't have anything better.
-- 
1.5.6.4
Next: Junio C Hamano
Message 1 of 8 in “Use line buffering for standard output”
  1. Use line buffering for standard outputAnders Melchiorsen, Aug 3, 2008
  2. Junio C HamanoAug 3, 2008
  3. Linus TorvaldsAug 3, 2008
  4. Anders MelchiorsenAug 3, 2008
  5. Junio C HamanoAug 4, 2008
  6. Flush output in start_asyncAnders Melchiorsen, Aug 4, 2008
  7. Linus TorvaldsAug 4, 2008
  8. Add output flushing before fork()Anders Melchiorsen, Aug 4, 2008

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.