Skip to content

Commit 6e9af86

Browse files
peffgitster
authored andcommitted
Support GIT_PAGER_IN_USE environment variable
When deciding whether or not to turn on automatic color support, git_config_colorbool checks whether stdout is a tty. However, because we run a pager, if stdout is not a tty, we must check whether it is because we started the pager. This used to be done by checking the pager_in_use variable. This variable was set only when the git program being run started the pager; there was no way for an external program running git indicate that it had already started a pager. This patch allows a program to set GIT_PAGER_IN_USE to a true value to indicate that even though stdout is not a tty, it is because a pager is being used. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 591aa25 commit 6e9af86

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char
608608
/* pager.c */
609609
extern void setup_pager(void);
610610
extern char *pager_program;
611-
extern int pager_in_use;
611+
extern int pager_in_use(void);
612612
extern int pager_use_color;
613613

614614
extern char *editor_program;

color.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
135135
auto_color:
136136
if (stdout_is_tty < 0)
137137
stdout_is_tty = isatty(1);
138-
if (stdout_is_tty || (pager_in_use && pager_use_color)) {
138+
if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
139139
char *term = getenv("TERM");
140140
if (term && strcmp(term, "dumb"))
141141
return 1;

environment.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
3131
size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
3232
size_t delta_base_cache_limit = 16 * 1024 * 1024;
3333
char *pager_program;
34-
int pager_in_use;
3534
int pager_use_color = 1;
3635
char *editor_program;
3736
char *excludes_file;

pager.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* something different on Windows, for example.
66
*/
77

8+
static int spawned_pager;
9+
810
static void run_pager(const char *pager)
911
{
1012
/*
@@ -41,7 +43,7 @@ void setup_pager(void)
4143
else if (!*pager || !strcmp(pager, "cat"))
4244
return;
4345

44-
pager_in_use = 1; /* means we are emitting to terminal */
46+
spawned_pager = 1; /* means we are emitting to terminal */
4547

4648
if (pipe(fd) < 0)
4749
return;
@@ -70,3 +72,14 @@ void setup_pager(void)
7072
die("unable to execute pager '%s'", pager);
7173
exit(255);
7274
}
75+
76+
int pager_in_use(void)
77+
{
78+
const char *env;
79+
80+
if (spawned_pager)
81+
return 1;
82+
83+
env = getenv("GIT_PAGER_IN_USE");
84+
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
85+
}

0 commit comments

Comments
 (0)