From 9fabb6d7513ab0a264de146d72a8447bc85b5e85 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 14 Feb 2010 05:55:53 -0600 Subject: [PATCH 1/8] Fix 'git var' usage synopsis The parameter to 'git var' is not optional. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Documentation/git-var.txt | 2 +- builtin-var.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/git-var.txt b/Documentation/git-var.txt index bb981822a4..458f3e2755 100644 --- a/Documentation/git-var.txt +++ b/Documentation/git-var.txt @@ -8,7 +8,7 @@ git-var - Show a git logical variable SYNOPSIS -------- -'git var' [ -l | ] +'git var' ( -l | ) DESCRIPTION ----------- diff --git a/builtin-var.c b/builtin-var.c index 2280518190..e6ee7bc0b6 100644 --- a/builtin-var.c +++ b/builtin-var.c @@ -6,7 +6,7 @@ #include "cache.h" #include "exec_cmd.h" -static const char var_usage[] = "git var [-l | ]"; +static const char var_usage[] = "git var (-l | )"; static const char *editor(int flag) { From 64778d24a93ad455e5883120aef350ede20061c4 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 14 Feb 2010 05:59:59 -0600 Subject: [PATCH 2/8] Make 'git var GIT_PAGER' always print the configured pager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scripted commands that want to use git’s configured pager know better than ‘git var’ does whether stdout is going to be a tty at the appropriate time. Checking isatty(1) as git_pager() does now won’t cut it, since the output of git var itself is almost never a terminal. The symptom is that when used by humans, ‘git var GIT_PAGER’ behaves as it should, but when used by scripts, it always returns ‘cat’! So avoid tricks with isatty() and just always print the configured pager. This does not fix the callers to check isatty(1) themselves yet. Nevertheless, this patch alone is enough to fix 'am --interactive'. Thanks to Sebastian Celis for the report and Jeff King for the analysis. Reported-by: Sebastian Celis Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- builtin-var.c | 2 +- cache.h | 2 +- pager.c | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/builtin-var.c b/builtin-var.c index e6ee7bc0b6..70fdb4dec7 100644 --- a/builtin-var.c +++ b/builtin-var.c @@ -20,7 +20,7 @@ static const char *editor(int flag) static const char *pager(int flag) { - const char *pgm = git_pager(); + const char *pgm = git_pager(1); if (!pgm) pgm = "cat"; diff --git a/cache.h b/cache.h index d478eff1f3..d454b7e686 100644 --- a/cache.h +++ b/cache.h @@ -775,7 +775,7 @@ extern const char *git_committer_info(int); extern const char *fmt_ident(const char *name, const char *email, const char *date_str, int); extern const char *fmt_name(const char *name, const char *email); extern const char *git_editor(void); -extern const char *git_pager(void); +extern const char *git_pager(int stdout_is_tty); struct checkout { const char *base_dir; diff --git a/pager.c b/pager.c index 2c7e8ecb3c..dac358f047 100644 --- a/pager.c +++ b/pager.c @@ -48,11 +48,11 @@ static void wait_for_pager_signal(int signo) raise(signo); } -const char *git_pager(void) +const char *git_pager(int stdout_is_tty) { const char *pager; - if (!isatty(1)) + if (!stdout_is_tty) return NULL; pager = getenv("GIT_PAGER"); @@ -73,7 +73,7 @@ const char *git_pager(void) void setup_pager(void) { - const char *pager = git_pager(); + const char *pager = git_pager(isatty(1)); if (!pager) return; From 06300d9753349a83212360445d241d70a46375fa Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 14 Feb 2010 06:02:35 -0600 Subject: [PATCH 3/8] git.1: Clarify the behavior of the --paginate option The --paginate option is meant to negate the effect of an explicit or implicit pager. = false setting. Thus it turns the pager on if output is going to a terminal rather than unconditionally. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- Documentation/git.txt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Documentation/git.txt b/Documentation/git.txt index 01c463101b..f26641a5f4 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -229,7 +229,10 @@ help ...`. -p:: --paginate:: - Pipe all output into 'less' (or if set, $PAGER). + Pipe all output into 'less' (or if set, $PAGER) if standard + output is a terminal. This overrides the `pager.` + configuration options (see the "Configuration Mechanism" section + below). --no-pager:: Do not pipe git output into a pager. @@ -401,7 +404,8 @@ people. Here is an example: ------------ Various commands read from the configuration file and adjust -their operation accordingly. +their operation accordingly. See linkgit:git-config[1] for a +list. Identifier Terminology From 190c1cda7eb6dc03be80f45d3d174c313d23da2c Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 14 Feb 2010 06:06:10 -0600 Subject: [PATCH 4/8] git svn: Fix launching of pager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In commit dec543e (am -i, git-svn: use "git var GIT_PAGER"), I tried to teach git svn to defer to git var on what pager to use. In the process, I introduced two bugs: - The value set for $pager in config_pager has local scope, so run_pager never sees it; - git var cannot tell whether git svn’s output is going to a terminal, so the value chosen for $pager does not reflect that information. Fix them. Reported-by: Sebastian Celis Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- git-svn.perl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/git-svn.perl b/git-svn.perl index 265852f459..473a0b9d55 100755 --- a/git-svn.perl +++ b/git-svn.perl @@ -5459,7 +5459,12 @@ sub git_svn_log_cmd { # adapted from pager.c sub config_pager { - chomp(my $pager = command_oneline(qw(var GIT_PAGER))); + if (! -t *STDOUT) { + $ENV{GIT_PAGER_IN_USE} = 'false'; + $pager = undef; + return; + } + chomp($pager = command_oneline(qw(var GIT_PAGER))); if ($pager eq 'cat') { $pager = undef; } @@ -5467,7 +5472,7 @@ sub config_pager { } sub run_pager { - return unless -t *STDOUT && defined $pager; + return unless defined $pager; pipe my ($rfd, $wfd) or return; defined(my $pid = fork) or ::fatal "Can't fork: $!"; if (!$pid) { From f6dff119d51e0067d213068093039bb2f939d139 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 14 Feb 2010 23:04:13 -0600 Subject: [PATCH 5/8] am: Fix launching of pager The pagination functionality in git am has some problems: - It does not check if stdout is a tty, so it always paginates. - If $GIT_PAGER uses any environment variables, they are being ignored, since it does not run $GIT_PAGER through eval. - If $GIT_PAGER is set to the empty string, instead of passing output through to stdout, it tries to run $dotest/patch. Fix them. While at it, move the definition of git_pager() to git-sh-setup so authors of other commands are not tempted to reimplement it with the same mistakes. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- git-am.sh | 5 +---- git-sh-setup.sh | 13 +++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/git-am.sh b/git-am.sh index 3c08d53161..b11af03e0b 100755 --- a/git-am.sh +++ b/git-am.sh @@ -663,10 +663,7 @@ do [eE]*) git_editor "$dotest/final-commit" action=again ;; [vV]*) action=again - : ${GIT_PAGER=$(git var GIT_PAGER)} - : ${LESS=-FRSX} - export LESS - $GIT_PAGER "$dotest/patch" ;; + git_pager "$dotest/patch" ;; *) action=again ;; esac done diff --git a/git-sh-setup.sh b/git-sh-setup.sh index d56426dd39..44fb4670ae 100755 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -107,6 +107,19 @@ git_editor() { eval "$GIT_EDITOR" '"$@"' } +git_pager() { + if test -t 1 + then + GIT_PAGER=$(git var GIT_PAGER) + else + GIT_PAGER=cat + fi + : ${LESS=-FRSX} + export LESS + + eval "$GIT_PAGER" '"$@"' +} + sane_grep () { GREP_OPTIONS= LC_ALL=C grep "$@" } From 60b6e2200deff208a9757721544a3a311034804f Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Fri, 19 Feb 2010 01:18:58 -0600 Subject: [PATCH 6/8] tests: Add tests for automatic use of pager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Git’s automatic pagination support has some subtleties. Add some tests to make sure we don’t break: - when git will use a pager by default; - the effect of the --paginate and --no-pager options; - the effect of pagination on use of color; - how the choice of pager is configured. This does not yet test: - use of pager by scripted commands (git svn and git am); - effect of the pager.* configuration variables; - setting of the LESS variable. Some features involve checking whether stdout is a terminal, so many of these tests are skipped unless output is passed through to the terminal (i.e., unless $GIT_TEST_OPTS includes --verbose). The immediate purpose for these tests was to avoid making things worse after the breakage from my jn/editor-pager series (see commit 376f39, 2009-11-20). Thanks to Sebastian Celis for the report. Helped-by: Jeff King Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- t/t7006-pager.sh | 163 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100755 t/t7006-pager.sh diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh new file mode 100755 index 0000000000..4f52ea5732 --- /dev/null +++ b/t/t7006-pager.sh @@ -0,0 +1,163 @@ +#!/bin/sh + +test_description='Test automatic use of a pager.' + +. ./test-lib.sh + +rm -f stdout_is_tty +test_expect_success 'is stdout a terminal?' ' + if test -t 1 + then + : > stdout_is_tty + fi +' + +if test -e stdout_is_tty +then + test_set_prereq TTY +else + say stdout is not a terminal, so skipping some tests. +fi + +unset GIT_PAGER GIT_PAGER_IN_USE +git config --unset core.pager +PAGER='cat > paginated.out' +export PAGER + +test_expect_success 'setup' ' + test_commit initial +' + +rm -f paginated.out +test_expect_success TTY 'some commands use a pager' ' + git log && + test -e paginated.out +' + +rm -f paginated.out +test_expect_success TTY 'some commands do not use a pager' ' + git rev-list HEAD && + ! test -e paginated.out +' + +rm -f paginated.out +test_expect_success 'no pager when stdout is a pipe' ' + git log | cat && + ! test -e paginated.out +' + +rm -f paginated.out +test_expect_success 'no pager when stdout is a regular file' ' + git log > file && + ! test -e paginated.out +' + +rm -f paginated.out +test_expect_success TTY 'git --paginate rev-list uses a pager' ' + git --paginate rev-list HEAD && + test -e paginated.out +' + +rm -f file paginated.out +test_expect_success 'no pager even with --paginate when stdout is a pipe' ' + git --paginate log | cat && + ! test -e paginated.out +' + +rm -f paginated.out +test_expect_success TTY 'no pager with --no-pager' ' + git --no-pager log && + ! test -e paginated.out +' + +# A colored commit log will begin with an appropriate ANSI escape +# for the first color; the text "commit" comes later. +colorful() { + read firstline < $1 + ! expr "$firstline" : "^[a-zA-Z]" >/dev/null +} + +rm -f colorful.log colorless.log +test_expect_success 'tests can detect color' ' + git log --no-color > colorless.log && + git log --color > colorful.log && + ! colorful colorless.log && + colorful colorful.log +' + +rm -f colorless.log +git config color.ui auto +test_expect_success 'no color when stdout is a regular file' ' + git log > colorless.log && + ! colorful colorless.log +' + +rm -f paginated.out +git config color.ui auto +test_expect_success TTY 'color when writing to a pager' ' + TERM=vt100 git log && + colorful paginated.out +' + +rm -f colorful.log +git config color.ui auto +test_expect_success 'color when writing to a file intended for a pager' ' + TERM=vt100 GIT_PAGER_IN_USE=true git log > colorful.log && + colorful colorful.log +' + +unset PAGER GIT_PAGER +git config --unset core.pager +test_expect_success 'determine default pager' ' + less=$(git var GIT_PAGER) && + test -n "$less" +' + +if expr "$less" : '^[a-z]*$' > /dev/null && test_have_prereq TTY +then + test_set_prereq SIMPLEPAGER +fi + +unset PAGER GIT_PAGER +git config --unset core.pager +rm -f default_pager_used +test_expect_success SIMPLEPAGER 'default pager is used by default' ' + cat > $less <<-EOF && + #!$SHELL_PATH + : > default_pager_used + EOF + chmod +x $less && + PATH=.:$PATH git log && + test -e default_pager_used +' + +unset GIT_PAGER +git config --unset core.pager +rm -f PAGER_used +test_expect_success TTY 'PAGER overrides default pager' ' + PAGER=": > PAGER_used" && + export PAGER && + git log && + test -e PAGER_used +' + +unset GIT_PAGER +rm -f core.pager_used +test_expect_success TTY 'core.pager overrides PAGER' ' + PAGER=: && + export PAGER && + git config core.pager ": > core.pager_used" && + git log && + test -e core.pager_used +' + +rm -f GIT_PAGER_used +test_expect_success TTY 'GIT_PAGER overrides core.pager' ' + git config core.pager : && + GIT_PAGER=": > GIT_PAGER_used" && + export GIT_PAGER && + git log && + test -e GIT_PAGER_used +' + +test_done From 2d3ca21677597902f66bf2f2b2cf1b4a623f1e4f Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sat, 20 Feb 2010 02:50:25 -0600 Subject: [PATCH 7/8] t7006-pager: if stdout is not a terminal, make a new one Testing pagination requires (fake or real) access to a terminal so we can see whether the pagination automatically kicks in, which makes it hard to get good coverage when running tests without --verbose. There are a number of ways to work around that: - Replace all isatty calls with calls to a custom xisatty wrapper that usually checks for a terminal but can be overridden for tests. This would be workable, but it would require implementing xisatty separately in three languages (C, shell, and perl) and making sure that any code that is to be tested always uses the wrapper. - Redirect stdout to /dev/tty. This would be problematic because there might be no terminal available, and even if a terminal is available, it might not be appropriate to spew output to it. - Create a new pseudo-terminal on the fly and capture its output. This patch implements the third approach. The new test-terminal.perl helper uses IO::Pty from Expect.pm to create a terminal and executes the program specified by its arguments with that terminal as stdout. If the IO::Pty module is missing or not working on a system, the test script will maintain its old behavior (skipping most of its tests unless GIT_TEST_OPTS includes --verbose). Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- t/t7006-pager.sh | 35 +++++++++++++++-------- t/t7006/test-terminal.perl | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 11 deletions(-) create mode 100755 t/t7006/test-terminal.perl diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index 4f52ea5732..da0f96262d 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -5,18 +5,31 @@ test_description='Test automatic use of a pager.' . ./test-lib.sh rm -f stdout_is_tty -test_expect_success 'is stdout a terminal?' ' +test_expect_success 'set up terminal for tests' ' if test -t 1 then : > stdout_is_tty + elif + test_have_prereq PERL && + "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \ + sh -c "test -t 1" + then + : > test_terminal_works fi ' if test -e stdout_is_tty then + test_terminal() { "$@"; } + test_set_prereq TTY +elif test -e test_terminal_works +then + test_terminal() { + "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@" + } test_set_prereq TTY else - say stdout is not a terminal, so skipping some tests. + say no usable terminal, so skipping some tests fi unset GIT_PAGER GIT_PAGER_IN_USE @@ -30,13 +43,13 @@ test_expect_success 'setup' ' rm -f paginated.out test_expect_success TTY 'some commands use a pager' ' - git log && + test_terminal git log && test -e paginated.out ' rm -f paginated.out test_expect_success TTY 'some commands do not use a pager' ' - git rev-list HEAD && + test_terminal git rev-list HEAD && ! test -e paginated.out ' @@ -54,7 +67,7 @@ test_expect_success 'no pager when stdout is a regular file' ' rm -f paginated.out test_expect_success TTY 'git --paginate rev-list uses a pager' ' - git --paginate rev-list HEAD && + test_terminal git --paginate rev-list HEAD && test -e paginated.out ' @@ -66,7 +79,7 @@ test_expect_success 'no pager even with --paginate when stdout is a pipe' ' rm -f paginated.out test_expect_success TTY 'no pager with --no-pager' ' - git --no-pager log && + test_terminal git --no-pager log && ! test -e paginated.out ' @@ -95,7 +108,7 @@ test_expect_success 'no color when stdout is a regular file' ' rm -f paginated.out git config color.ui auto test_expect_success TTY 'color when writing to a pager' ' - TERM=vt100 git log && + TERM=vt100 test_terminal git log && colorful paginated.out ' @@ -127,7 +140,7 @@ test_expect_success SIMPLEPAGER 'default pager is used by default' ' : > default_pager_used EOF chmod +x $less && - PATH=.:$PATH git log && + PATH=.:$PATH test_terminal git log && test -e default_pager_used ' @@ -137,7 +150,7 @@ rm -f PAGER_used test_expect_success TTY 'PAGER overrides default pager' ' PAGER=": > PAGER_used" && export PAGER && - git log && + test_terminal git log && test -e PAGER_used ' @@ -147,7 +160,7 @@ test_expect_success TTY 'core.pager overrides PAGER' ' PAGER=: && export PAGER && git config core.pager ": > core.pager_used" && - git log && + test_terminal git log && test -e core.pager_used ' @@ -156,7 +169,7 @@ test_expect_success TTY 'GIT_PAGER overrides core.pager' ' git config core.pager : && GIT_PAGER=": > GIT_PAGER_used" && export GIT_PAGER && - git log && + test_terminal git log && test -e GIT_PAGER_used ' diff --git a/t/t7006/test-terminal.perl b/t/t7006/test-terminal.perl new file mode 100755 index 0000000000..73ff809371 --- /dev/null +++ b/t/t7006/test-terminal.perl @@ -0,0 +1,58 @@ +#!/usr/bin/perl +use strict; +use warnings; +use IO::Pty; +use File::Copy; + +# Run @$argv in the background with stdout redirected to $out. +sub start_child { + my ($argv, $out) = @_; + my $pid = fork; + if (not defined $pid) { + die "fork failed: $!" + } elsif ($pid == 0) { + open STDOUT, ">&", $out; + close $out; + exec(@$argv) or die "cannot exec '$argv->[0]': $!" + } + return $pid; +} + +# Wait for $pid to finish. +sub finish_child { + # Simplified from wait_or_whine() in run-command.c. + my ($pid) = @_; + + my $waiting = waitpid($pid, 0); + if ($waiting < 0) { + die "waitpid failed: $!"; + } elsif ($? & 127) { + my $code = $? & 127; + warn "died of signal $code"; + return $code - 128; + } else { + return $? >> 8; + } +} + +sub xsendfile { + my ($out, $in) = @_; + + # Note: the real sendfile() cannot read from a terminal. + + # It is unspecified by POSIX whether reads + # from a disconnected terminal will return + # EIO (as in AIX 4.x, IRIX, and Linux) or + # end-of-file. Either is fine. + copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!"; +} + +if ($#ARGV < 1) { + die "usage: test-terminal program args"; +} +my $master = new IO::Pty; +my $slave = $master->slave; +my $pid = start_child(\@ARGV, $slave); +close $slave; +xsendfile(\*STDOUT, $master); +exit(finish_child($pid)); From 8c33b4cf67f47ee46fe0984751fd40c4cf7cf392 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Mon, 22 Feb 2010 02:46:33 -0600 Subject: [PATCH 8/8] tests: Fix race condition in t7006-pager Pagers that do not consume their input are dangerous: for example, $ GIT_PAGER=: git log $ echo $? 141 $ The only reason these tests were able to work before was that 'git log' would write to the pipe (and not fill it) before the pager had time to terminate and close the pipe. Fix it by using a program that consumes its input, namely wc (as suggested by Johannes). Reported-by: Johannes Sixt Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- t/t7006-pager.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh index da0f96262d..d9202d5af5 100755 --- a/t/t7006-pager.sh +++ b/t/t7006-pager.sh @@ -137,7 +137,7 @@ rm -f default_pager_used test_expect_success SIMPLEPAGER 'default pager is used by default' ' cat > $less <<-EOF && #!$SHELL_PATH - : > default_pager_used + wc > default_pager_used EOF chmod +x $less && PATH=.:$PATH test_terminal git log && @@ -148,7 +148,7 @@ unset GIT_PAGER git config --unset core.pager rm -f PAGER_used test_expect_success TTY 'PAGER overrides default pager' ' - PAGER=": > PAGER_used" && + PAGER="wc > PAGER_used" && export PAGER && test_terminal git log && test -e PAGER_used @@ -157,17 +157,17 @@ test_expect_success TTY 'PAGER overrides default pager' ' unset GIT_PAGER rm -f core.pager_used test_expect_success TTY 'core.pager overrides PAGER' ' - PAGER=: && + PAGER=wc && export PAGER && - git config core.pager ": > core.pager_used" && + git config core.pager "wc > core.pager_used" && test_terminal git log && test -e core.pager_used ' rm -f GIT_PAGER_used test_expect_success TTY 'GIT_PAGER overrides core.pager' ' - git config core.pager : && - GIT_PAGER=": > GIT_PAGER_used" && + git config core.pager wc && + GIT_PAGER="wc > GIT_PAGER_used" && export GIT_PAGER && test_terminal git log && test -e GIT_PAGER_used