]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
config: make git_{config,parse}_maybe_bool equivalent
[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;
c0c08897 10static const char *pager_program;
ea27a18c 11
507d7804 12static void wait_for_pager(int in_signal)
bfdd9ffd 13{
507d7804
TI
14 if (!in_signal) {
15 fflush(stdout);
16 fflush(stderr);
17 }
bfdd9ffd
JS
18 /* signal EOF to pager */
19 close(1);
20 close(2);
507d7804
TI
21 if (in_signal)
22 finish_command_in_signal(&pager_process);
23 else
24 finish_command(&pager_process);
25}
26
27static void wait_for_pager_atexit(void)
28{
29 wait_for_pager(0);
bfdd9ffd 30}
f67b45f8 31
a3da8821
JK
32static void wait_for_pager_signal(int signo)
33{
507d7804 34 wait_for_pager(1);
a3da8821
JK
35 sigchain_pop(signo);
36 raise(signo);
37}
38
4babb839
JK
39static int core_pager_config(const char *var, const char *value, void *data)
40{
41 if (!strcmp(var, "core.pager"))
42 return git_config_string(&pager_program, var, value);
43 return 0;
44}
45
eed27072
JK
46static void read_early_config(config_fn_t cb, void *data)
47{
48 git_config_with_options(cb, data, NULL, 1);
49
50 /*
51 * Note that this is a really dirty hack that does the wrong thing in
52 * many cases. The crux of the problem is that we cannot run
53 * setup_git_directory() early on in git's setup, so we have no idea if
54 * we are in a repository or not, and therefore are not sure whether
55 * and how to read repository-local config.
56 *
57 * So if we _aren't_ in a repository (or we are but we would reject its
58 * core.repositoryformatversion), we'll read whatever is in .git/config
59 * blindly. Similarly, if we _are_ in a repository, but not at the
60 * root, we'll fail to find .git/config (because it's really
61 * ../.git/config, etc). See t7006 for a complete set of failures.
62 *
63 * However, we have historically provided this hack because it does
64 * work some of the time (namely when you are at the top-level of a
65 * valid repository), and would rarely make things worse (i.e., you do
66 * not generally have a .git/config file sitting around).
67 */
68 if (!startup_info->have_repository) {
69 struct git_config_source repo_config;
70
71 memset(&repo_config, 0, sizeof(repo_config));
72 repo_config.file = ".git/config";
73 git_config_with_options(cb, data, &repo_config, 1);
74 }
75}
76
64778d24 77const char *git_pager(int stdout_is_tty)
f67b45f8 78{
63618245 79 const char *pager;
f67b45f8 80
64778d24 81 if (!stdout_is_tty)
63618245
JN
82 return NULL;
83
84 pager = getenv("GIT_PAGER");
cad3a205
JH
85 if (!pager) {
86 if (!pager_program)
eed27072 87 read_early_config(core_pager_config, NULL);
54adf370 88 pager = pager_program;
cad3a205 89 }
c27d205a
ML
90 if (!pager)
91 pager = getenv("PAGER");
402461aa 92 if (!pager)
a3d023d0 93 pager = DEFAULT_PAGER;
ed016612 94 if (!*pager || !strcmp(pager, "cat"))
63618245
JN
95 pager = NULL;
96
97 return pager;
98}
99
995bc22d
EW
100static void setup_pager_env(struct argv_array *env)
101{
102 const char **argv;
103 int i;
104 char *pager_env = xstrdup(PAGER_ENV);
105 int n = split_cmdline(pager_env, &argv);
106
107 if (n < 0)
108 die("malformed build-time PAGER_ENV: %s",
109 split_cmdline_strerror(n));
110
111 for (i = 0; i < n; i++) {
112 char *cp = strchr(argv[i], '=');
113
114 if (!cp)
115 die("malformed build-time PAGER_ENV");
116
117 *cp = '\0';
118 if (!getenv(argv[i])) {
119 *cp = '=';
120 argv_array_push(env, argv[i]);
121 }
122 }
123 free(pager_env);
124 free(argv);
125}
126
3e3a4a41
JH
127void prepare_pager_args(struct child_process *pager_process, const char *pager)
128{
129 argv_array_push(&pager_process->args, pager);
130 pager_process->use_shell = 1;
995bc22d 131 setup_pager_env(&pager_process->env_array);
3e3a4a41
JH
132}
133
63618245
JN
134void setup_pager(void)
135{
64778d24 136 const char *pager = git_pager(isatty(1));
63618245 137
c0459ca4 138 if (!pager)
402461aa
JS
139 return;
140
ad6c3739
ZJS
141 /*
142 * force computing the width of the terminal before we redirect
143 * the standard output to the pager.
144 */
145 (void) term_columns();
146
2e6c012e 147 setenv("GIT_PAGER_IN_USE", "true", 1);
85fb65ed 148
bfdd9ffd 149 /* spawn the pager */
3e3a4a41 150 prepare_pager_args(&pager_process, pager);
ea27a18c 151 pager_process.in = -1;
124b5190 152 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
bfdd9ffd
JS
153 if (start_command(&pager_process))
154 return;
155
156 /* original process continues, but writes to the pipe */
157 dup2(pager_process.in, 1);
a8335024
JH
158 if (isatty(2))
159 dup2(pager_process.in, 2);
bfdd9ffd
JS
160 close(pager_process.in);
161
162 /* this makes sure that the parent terminates after the pager */
a3da8821 163 sigchain_push_common(wait_for_pager_signal);
507d7804 164 atexit(wait_for_pager_atexit);
f67b45f8 165}
6e9af863
JK
166
167int pager_in_use(void)
168{
169 const char *env;
6e9af863
JK
170 env = getenv("GIT_PAGER_IN_USE");
171 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
172}
ad6c3739
ZJS
173
174/*
175 * Return cached value (if set) or $COLUMNS environment variable (if
176 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
177 * and default to 80 if all else fails.
178 */
179int term_columns(void)
180{
181 static int term_columns_at_startup;
182
183 char *col_string;
184 int n_cols;
185
186 if (term_columns_at_startup)
187 return term_columns_at_startup;
188
189 term_columns_at_startup = 80;
190
191 col_string = getenv("COLUMNS");
192 if (col_string && (n_cols = atoi(col_string)) > 0)
193 term_columns_at_startup = n_cols;
194#ifdef TIOCGWINSZ
195 else {
196 struct winsize ws;
197 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
198 term_columns_at_startup = ws.ws_col;
199 }
200#endif
201
202 return term_columns_at_startup;
203}
4d9e079e 204
ec7ff5ba
ZJS
205/*
206 * How many columns do we need to show this number in decimal?
207 */
d306f3d3 208int decimal_width(uintmax_t number)
ec7ff5ba 209{
d306f3d3 210 int width;
ec7ff5ba 211
d306f3d3
JK
212 for (width = 1; number >= 10; width++)
213 number /= 10;
ec7ff5ba
ZJS
214 return width;
215}
4914c962 216
6a1e1bc0
JK
217struct pager_command_config_data {
218 const char *cmd;
219 int want;
220 char *value;
221};
222
223static int pager_command_config(const char *var, const char *value, void *vdata)
4914c962 224{
6a1e1bc0
JK
225 struct pager_command_config_data *data = vdata;
226 const char *cmd;
227
228 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
229 int b = git_config_maybe_bool(var, value);
4914c962 230 if (b >= 0)
6a1e1bc0 231 data->want = b;
4914c962 232 else {
6a1e1bc0
JK
233 data->want = 1;
234 data->value = xstrdup(value);
4914c962
NTND
235 }
236 }
6a1e1bc0
JK
237
238 return 0;
239}
240
241/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
242int check_pager_config(const char *cmd)
243{
244 struct pager_command_config_data data;
245
246 data.cmd = cmd;
247 data.want = -1;
248 data.value = NULL;
249
eed27072 250 read_early_config(pager_command_config, &data);
6a1e1bc0
JK
251
252 if (data.value)
253 pager_program = data.value;
254 return data.want;
4914c962 255}