]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
pager: stop loading git_default_config()
[thirdparty/git.git] / pager.c
CommitLineData
f67b45f8 1#include "cache.h"
ea27a18c 2#include "run-command.h"
a3da8821 3#include "sigchain.h"
f67b45f8 4
a3d023d0
JH
5#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
d3180279 9static struct child_process pager_process = CHILD_PROCESS_INIT;
ea27a18c 10
507d7804 11static void wait_for_pager(int in_signal)
bfdd9ffd 12{
507d7804
TI
13 if (!in_signal) {
14 fflush(stdout);
15 fflush(stderr);
16 }
bfdd9ffd
JS
17 /* signal EOF to pager */
18 close(1);
19 close(2);
507d7804
TI
20 if (in_signal)
21 finish_command_in_signal(&pager_process);
22 else
23 finish_command(&pager_process);
24}
25
26static void wait_for_pager_atexit(void)
27{
28 wait_for_pager(0);
bfdd9ffd 29}
f67b45f8 30
a3da8821
JK
31static void wait_for_pager_signal(int signo)
32{
507d7804 33 wait_for_pager(1);
a3da8821
JK
34 sigchain_pop(signo);
35 raise(signo);
36}
37
4babb839
JK
38static int core_pager_config(const char *var, const char *value, void *data)
39{
40 if (!strcmp(var, "core.pager"))
41 return git_config_string(&pager_program, var, value);
42 return 0;
43}
44
64778d24 45const char *git_pager(int stdout_is_tty)
f67b45f8 46{
63618245 47 const char *pager;
f67b45f8 48
64778d24 49 if (!stdout_is_tty)
63618245
JN
50 return NULL;
51
52 pager = getenv("GIT_PAGER");
cad3a205
JH
53 if (!pager) {
54 if (!pager_program)
4babb839 55 git_config(core_pager_config, NULL);
54adf370 56 pager = pager_program;
cad3a205 57 }
c27d205a
ML
58 if (!pager)
59 pager = getenv("PAGER");
402461aa 60 if (!pager)
a3d023d0 61 pager = DEFAULT_PAGER;
ed016612 62 if (!*pager || !strcmp(pager, "cat"))
63618245
JN
63 pager = NULL;
64
65 return pager;
66}
67
3e3a4a41
JH
68void prepare_pager_args(struct child_process *pager_process, const char *pager)
69{
70 argv_array_push(&pager_process->args, pager);
71 pager_process->use_shell = 1;
72 if (!getenv("LESS"))
73 argv_array_push(&pager_process->env_array, "LESS=FRX");
74 if (!getenv("LV"))
75 argv_array_push(&pager_process->env_array, "LV=-c");
76}
77
63618245
JN
78void setup_pager(void)
79{
64778d24 80 const char *pager = git_pager(isatty(1));
63618245 81
c0459ca4 82 if (!pager)
402461aa
JS
83 return;
84
ad6c3739
ZJS
85 /*
86 * force computing the width of the terminal before we redirect
87 * the standard output to the pager.
88 */
89 (void) term_columns();
90
2e6c012e 91 setenv("GIT_PAGER_IN_USE", "true", 1);
85fb65ed 92
bfdd9ffd 93 /* spawn the pager */
3e3a4a41 94 prepare_pager_args(&pager_process, pager);
ea27a18c 95 pager_process.in = -1;
124b5190 96 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
bfdd9ffd
JS
97 if (start_command(&pager_process))
98 return;
99
100 /* original process continues, but writes to the pipe */
101 dup2(pager_process.in, 1);
a8335024
JH
102 if (isatty(2))
103 dup2(pager_process.in, 2);
bfdd9ffd
JS
104 close(pager_process.in);
105
106 /* this makes sure that the parent terminates after the pager */
a3da8821 107 sigchain_push_common(wait_for_pager_signal);
507d7804 108 atexit(wait_for_pager_atexit);
f67b45f8 109}
6e9af863
JK
110
111int pager_in_use(void)
112{
113 const char *env;
6e9af863
JK
114 env = getenv("GIT_PAGER_IN_USE");
115 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
116}
ad6c3739
ZJS
117
118/*
119 * Return cached value (if set) or $COLUMNS environment variable (if
120 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
121 * and default to 80 if all else fails.
122 */
123int term_columns(void)
124{
125 static int term_columns_at_startup;
126
127 char *col_string;
128 int n_cols;
129
130 if (term_columns_at_startup)
131 return term_columns_at_startup;
132
133 term_columns_at_startup = 80;
134
135 col_string = getenv("COLUMNS");
136 if (col_string && (n_cols = atoi(col_string)) > 0)
137 term_columns_at_startup = n_cols;
138#ifdef TIOCGWINSZ
139 else {
140 struct winsize ws;
141 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
142 term_columns_at_startup = ws.ws_col;
143 }
144#endif
145
146 return term_columns_at_startup;
147}
4d9e079e 148
ec7ff5ba
ZJS
149/*
150 * How many columns do we need to show this number in decimal?
151 */
d306f3d3 152int decimal_width(uintmax_t number)
ec7ff5ba 153{
d306f3d3 154 int width;
ec7ff5ba 155
d306f3d3
JK
156 for (width = 1; number >= 10; width++)
157 number /= 10;
ec7ff5ba
ZJS
158 return width;
159}
4914c962 160
586f414a
TA
161/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
162int check_pager_config(const char *cmd)
4914c962 163{
586f414a
TA
164 int want = -1;
165 struct strbuf key = STRBUF_INIT;
166 const char *value = NULL;
167 strbuf_addf(&key, "pager.%s", cmd);
9e9de18f
JK
168 if (git_config_key_is_valid(key.buf) &&
169 !git_config_get_value(key.buf, &value)) {
586f414a 170 int b = git_config_maybe_bool(key.buf, value);
4914c962 171 if (b >= 0)
586f414a 172 want = b;
4914c962 173 else {
586f414a
TA
174 want = 1;
175 pager_program = xstrdup(value);
4914c962
NTND
176 }
177 }
586f414a
TA
178 strbuf_release(&key);
179 return want;
4914c962 180}