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

[RFC] sending errors to stdout under $PAGER

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 16, 2008, 19:15 UTC
Message-ID
<7vbq6g90gy.fsf@gitster.siamese.dyndns.org>

If you do this (and you are not an Emacs user who uses PAGER=cat in your *shell* buffer):

        $ git init
        Initialized empty Git repository in .git/
        $ echo hello world >foo
        $ H=$(git hash-object -w foo)
        $ git tag -a foo-tag -m "Tags $H" $H
        $ echo $H
        3b18e512dba79e4c8300dd08aeb37f8e728b8dad
        $ rm -f .git/objects/3b/18e5*
        $ git show foo-tag
        tag foo-tag
        Tagger: Junio C Hamano <gitster@pobox.com>
        Date:   Sat Feb 16 10:43:23 2008 -0800
        Tags 3b18e512dba79e4c8300dd08aeb37f8e728b8dad

you do not get any indication of error. If you are careful, you would notice that no contents from the tagged object is displayed, but that is about it. If you run the "show" command without pager, however, you will see the error:

        $ git --no-pager show foo-tag
        tag foo-tag
        Tagger: Junio C Hamano <gitster@pobox.com>
        Date:   Sat Feb 16 10:43:23 2008 -0800
        Tags 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
        error: Could not read object 3b18e512dba79e4c8300dd08aeb37f8e728b8dad

Because we spawn the pager as the foreground process and feed its input via pipe from the real command, we cannot affect the exit status the shell sees from git command when the pager is in use (I think there is not much gain we can have by working it around, though). But at least it may make sense to show the error message to the user sitting in front of the pager, perhaps like this.

What do people think?  Have I overlooked any downsides?
---
 usage.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/usage.c b/usage.c
index a5fc4ec..681b84a 100644
--- a/usage.c
+++ b/usage.c
@@ -4,12 +4,15 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "git-compat-util.h"
+#include "cache.h"
 
 static void report(const char *prefix, const char *err, va_list params)
 {
 	char msg[256];
+	FILE *outto = (pager_in_use() ? stdout : stderr);
+
 	vsnprintf(msg, sizeof(msg), err, params);
-	fprintf(stderr, "%s%s\n", prefix, msg);
+	fprintf(outto, "%s%s\n", prefix, msg);
 }
 
 static NORETURN void usage_builtin(const char *err)
Next: Shawn O. Pearce
Message 1 of 9 in “[RFC] sending errors to stdout under $PAGER”
  1. Junio C HamanoFeb 16, 2008
  2. Shawn O. PearceFeb 17, 2008
  3. Jeff KingFeb 17, 2008
  4. Edgar ToernigFeb 17, 2008
  5. Johannes SchindelinFeb 17, 2008
  6. Junio C HamanoFeb 17, 2008
  7. Jeff KingFeb 17, 2008
  8. Junio C HamanoFeb 17, 2008
  9. Johannes SchindelinFeb 17, 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.