Skip to content

Commit 2e6c012

Browse files
peffgitster
authored andcommitted
setup_pager: set GIT_PAGER_IN_USE
We have always set a global "spawned_pager" variable when we start the pager. This lets us make the auto-color decision later in the program as as "we are outputting to a terminal, or to a pager which can handle colors". Commit 6e9af86 added support for the GIT_PAGER_IN_USE environment variable. An external program calling git (e.g., git-svn) could set this variable to indicate that it had already started the pager, and that the decision about auto-coloring should take that into account. However, 6e9af86 failed to do the reverse, which is to tell external programs when git itself has started the pager. Thus a git command implemented as an external script that has the pager turned on (e.g., "git -p stash show") would not realize it was going to a pager, and would suppress colors. This patch remedies that; we always set GIT_PAGER_IN_USE when we start the pager, and the value is respected by both this program and any spawned children. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d68a6d commit 2e6c012

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

pager.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* something different on Windows.
1212
*/
1313

14-
static int spawned_pager;
15-
1614
#ifndef WIN32
1715
static void pager_preexec(void)
1816
{
@@ -78,7 +76,7 @@ void setup_pager(void)
7876
if (!pager)
7977
return;
8078

81-
spawned_pager = 1; /* means we are emitting to terminal */
79+
setenv("GIT_PAGER_IN_USE", "true", 1);
8280

8381
/* spawn the pager */
8482
pager_argv[0] = pager;
@@ -109,10 +107,6 @@ void setup_pager(void)
109107
int pager_in_use(void)
110108
{
111109
const char *env;
112-
113-
if (spawned_pager)
114-
return 1;
115-
116110
env = getenv("GIT_PAGER_IN_USE");
117111
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
118112
}

t/t7006-pager.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ test_expect_success 'color when writing to a file intended for a pager' '
181181
colorful colorful.log
182182
'
183183

184+
test_expect_success TTY 'colors are sent to pager for external commands' '
185+
test_config alias.externallog "!git log" &&
186+
test_config color.ui auto &&
187+
(
188+
TERM=vt100 &&
189+
export TERM &&
190+
test_terminal git -p externallog
191+
) &&
192+
colorful paginated.out
193+
'
194+
184195
# Use this helper to make it easy for the caller of your
185196
# terminal-using function to specify whether it should fail.
186197
# If you write

0 commit comments

Comments
 (0)