sending errors to stdout under $PAGER
RFC 1/1 sending errors to stdout under $PAGER
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 <[email protected]>
Date: Sat Feb 16 10:43:23 2008 -0800Tags 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 <[email protected]>
Date: Sat Feb 16 10:43:23 2008 -0800 Tags 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
error: Could not read object 3b18e512dba79e4c8300dd08aeb37f8e728b8dadBecause 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(-)
usage.c +4 -1
@@ -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)
Quoted 9 lines · Junio C Hamano
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?
I think this is a good idea.
If you are using an interactive pager, you have asked for the content to come to you through that. Not unlike how I have chosen to have the content come to me through a virtual pty and not a printer with green-and-white bar paper. :)
I've been bitten by this in the past a few times, but I have also been knowledgable enough about git, the command I ran, and the project I ran it on to realize something wasn't right with the output I am seeing in the pager and retry without the pager to see the real error(s).
Quoted 16 lines
+++
b/usage.cusage.c +4
-1@@ -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); }
Signature · 1 line
Shawn.
Quoted 9 lines · Junio C Hamano
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?
I think this makes sense. It could be annoying if chatty stderr output got mixed in with the actual output, making things harder to read. But git is not very chatty in general, and the point is that things sent to stderr _should_ grab the user's attention.
The only downside I see is that it disrupts the parsing of the output. In most cases, this doesn't matter, since anything parsing the output will disable the pager. The notable exception is something like 'tig', which I believe can act as a git pager which understands the output; it can potentially be confused by the extra lines on stdout.
-Peff
Quoted 8 lines · Junio C Hamano
usage.c +3
-1+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);What do people think? Have I overlooked any downsides?
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
So, instead of the current
foo | less
use
foo 2>&1 | less
or, in pager.c:
/* return in the child */
if (!pid) {
dup2(fd[1], 1);
+ dup2(fd[1], 2);
close(fd[0]);
close(fd[1]);
return;
}Ciao, ET.
Quoted 13 lines · Edgar Toernig
Quoted 8 lines · Junio C Hamano
usage.c +3
-1+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);What do people think? Have I overlooked any downsides?
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
Quoted 5 lines
So, instead of the current
foo | less
usefoo 2>&1 | less
I like it. Much nicer. Thanks.
Quoted 10 lines · Junio C Hamano
Quoted 8 lines
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
So, instead of the current
foo | less
usefoo 2>&1 | lessI like it. Much nicer. Thanks.
This will also put the stderr of any sub-programs into the pager, which is probably worse if you have, e.g., a chatty external diff program. I don't know if we care enough about that.
-Peff
Quoted 11 lines · Jeff King
Quoted 9 lines · Junio C Hamano
Quoted 8 lines
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
So, instead of the current
foo | less
usefoo 2>&1 | less
Quoted 5 lines
Quoted 1 line
I like it. Much nicer. Thanks.
This will also put the stderr of any sub-programs into the pager, which is probably worse if you have, e.g., a chatty external diff program. I don't know if we care enough about that.
We'll soon find out and the change would be a single liner that is very easy to back out, so let's put it in and see what happens ;-).
Hi,
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Jeff King
Quoted 9 lines · Junio C Hamano
Quoted 8 lines
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
So, instead of the current
foo | less
usefoo 2>&1 | less
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I like it. Much nicer. Thanks.
This will also put the stderr of any sub-programs into the pager, which is probably worse if you have, e.g., a chatty external diff program. I don't know if we care enough about that.
We'll soon find out and the change would be a single liner that is very easy to back out, so let's put it in and see what happens ;-).
I think this is a non-problem. External diff programs are virtually all scripts written for git, because of the calling convention. So all this does is tell the people who use external diff wrappers (and let them be chatty) to fix their scripts.
Ciao,
Dscho
Hi,
Quoted 15 lines · Edgar Toernig
Quoted 8 lines · Junio C Hamano
usage.c +3
-1+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);What do people think? Have I overlooked any downsides?
Wouldn't it be better/safer to redirect stderr to the pager in the first place?
[...]
Quoted 8 lines
/* return in the child */
if (!pid) {
dup2(fd[1], 1);
+ dup2(fd[1], 2);
close(fd[0]);
close(fd[1]);
return;
}I like it.
Ciao,
Dscho