blob: ae79643363091f4967097bdff6e009ea4c7df5e0 [file] [log] [blame]
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08001#include "cache.h"
Jeff Kingea27a182008-07-22 03:14:12 -04002#include "run-command.h"
Jeff Kinga3da8822009-01-22 01:03:28 -05003#include "sigchain.h"
Linus Torvaldsf67b45f2006-02-28 11:26:21 -08004
Junio C Hamanoa3d023d2009-10-30 20:45:34 -05005#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
René Scharfed3180272014-08-19 21:09:35 +02009static struct child_process pager_process = CHILD_PROCESS_INIT;
Jeff Kingc0c08892016-09-12 20:23:48 -070010static const char *pager_program;
Jeff Kingea27a182008-07-22 03:14:12 -040011
Takashi Iwai507d7802015-09-04 11:35:57 +020012static void wait_for_pager(int in_signal)
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010013{
Takashi Iwai507d7802015-09-04 11:35:57 +020014 if (!in_signal) {
15 fflush(stdout);
16 fflush(stderr);
17 }
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010018 /* signal EOF to pager */
19 close(1);
20 close(2);
Takashi Iwai507d7802015-09-04 11:35:57 +020021 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);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +010030}
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080031
Jeff Kinga3da8822009-01-22 01:03:28 -050032static void wait_for_pager_signal(int signo)
33{
Takashi Iwai507d7802015-09-04 11:35:57 +020034 wait_for_pager(1);
Jeff Kinga3da8822009-01-22 01:03:28 -050035 sigchain_pop(signo);
36 raise(signo);
37}
38
Jeff King4babb832016-09-12 20:23:44 -070039static 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
Jeff Kingeed27072016-09-12 20:23:56 -070046static 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
Jonathan Nieder64778d22010-02-14 05:59:59 -060077const char *git_pager(int stdout_is_tty)
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080078{
Jonathan Nieder63618242009-10-30 20:41:27 -050079 const char *pager;
Linus Torvaldsf67b45f2006-02-28 11:26:21 -080080
Jonathan Nieder64778d22010-02-14 05:59:59 -060081 if (!stdout_is_tty)
Jonathan Nieder63618242009-10-30 20:41:27 -050082 return NULL;
83
84 pager = getenv("GIT_PAGER");
Junio C Hamanocad3a202007-08-06 21:08:43 -070085 if (!pager) {
86 if (!pager_program)
Jeff Kingeed27072016-09-12 20:23:56 -070087 read_early_config(core_pager_config, NULL);
Brian Gernhardt54adf372007-07-03 14:18:11 -040088 pager = pager_program;
Junio C Hamanocad3a202007-08-06 21:08:43 -070089 }
Brian Gernhardt54adf372007-07-03 14:18:11 -040090 if (!pager)
Matthias Lederhoferc27d2052006-07-31 15:27:00 +020091 pager = getenv("PAGER");
92 if (!pager)
Junio C Hamanoa3d023d2009-10-30 20:45:34 -050093 pager = DEFAULT_PAGER;
Jeff Kinged016612013-09-03 03:41:50 -040094 if (!*pager || !strcmp(pager, "cat"))
Jonathan Nieder63618242009-10-30 20:41:27 -050095 pager = NULL;
96
97 return pager;
98}
99
Eric Wong995bc222016-08-04 11:40:25 +0000100static 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
Junio C Hamano3e3a4a42016-02-16 14:34:44 -0800127void 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;
Eric Wong995bc222016-08-04 11:40:25 +0000131 setup_pager_env(&pager_process->env_array);
Junio C Hamano3e3a4a42016-02-16 14:34:44 -0800132}
133
Jonathan Nieder63618242009-10-30 20:41:27 -0500134void setup_pager(void)
135{
Jonathan Nieder64778d22010-02-14 05:59:59 -0600136 const char *pager = git_pager(isatty(1));
Jonathan Nieder63618242009-10-30 20:41:27 -0500137
Jörn Engelc0459ca2014-04-21 16:46:22 -0400138 if (!pager)
Johannes Schindelin402461a2006-04-16 04:44:25 +0200139 return;
140
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100141 /*
142 * force computing the width of the terminal before we redirect
143 * the standard output to the pager.
144 */
145 (void) term_columns();
146
Jeff King2e6c0122011-08-17 22:02:29 -0700147 setenv("GIT_PAGER_IN_USE", "true", 1);
Junio C Hamano85fb65e2006-06-06 16:58:40 -0700148
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100149 /* spawn the pager */
Junio C Hamano3e3a4a42016-02-16 14:34:44 -0800150 prepare_pager_args(&pager_process, pager);
Jeff Kingea27a182008-07-22 03:14:12 -0400151 pager_process.in = -1;
Junio C Hamano124b5192015-07-03 10:18:45 -0700152 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100153 if (start_command(&pager_process))
154 return;
155
156 /* original process continues, but writes to the pipe */
157 dup2(pager_process.in, 1);
Junio C Hamanoa8335022008-12-15 00:33:34 -0800158 if (isatty(2))
159 dup2(pager_process.in, 2);
Johannes Sixtbfdd9ff2007-12-08 21:28:41 +0100160 close(pager_process.in);
161
162 /* this makes sure that the parent terminates after the pager */
Jeff Kinga3da8822009-01-22 01:03:28 -0500163 sigchain_push_common(wait_for_pager_signal);
Takashi Iwai507d7802015-09-04 11:35:57 +0200164 atexit(wait_for_pager_atexit);
Linus Torvaldsf67b45f2006-02-28 11:26:21 -0800165}
Jeff King6e9af862007-12-11 01:27:33 -0500166
167int pager_in_use(void)
168{
169 const char *env;
Jeff King6e9af862007-12-11 01:27:33 -0500170 env = getenv("GIT_PAGER_IN_USE");
171 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
172}
Zbigniew Jędrzejewski-Szmekad6c3732012-02-12 15:12:32 +0100173
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}
Junio C Hamano4d9e0792012-02-20 00:15:11 -0800204
205/*
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100206 * How many columns do we need to show this number in decimal?
207 */
Jeff Kingd306f3d2015-02-05 03:14:19 -0500208int decimal_width(uintmax_t number)
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100209{
Jeff Kingd306f3d2015-02-05 03:14:19 -0500210 int width;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100211
Jeff Kingd306f3d2015-02-05 03:14:19 -0500212 for (width = 1; number >= 10; width++)
213 number /= 10;
Zbigniew Jędrzejewski-Szmekec7ff5b2012-02-12 15:16:20 +0100214 return width;
215}
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700216
Jeff King6a1e1bc2016-09-12 20:23:52 -0700217struct 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)
224{
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);
230 if (b >= 0)
231 data->want = b;
232 else {
233 data->want = 1;
234 data->value = xstrdup(value);
235 }
236 }
237
238 return 0;
239}
240
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700241/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
242int check_pager_config(const char *cmd)
243{
Jeff King6a1e1bc2016-09-12 20:23:52 -0700244 struct pager_command_config_data data;
245
246 data.cmd = cmd;
247 data.want = -1;
248 data.value = NULL;
249
Jeff Kingeed27072016-09-12 20:23:56 -0700250 read_early_config(pager_command_config, &data);
Jeff King6a1e1bc2016-09-12 20:23:52 -0700251
252 if (data.value)
253 pager_program = data.value;
254 return data.want;
Nguyễn Thái Ngọc Duy4914c962012-10-26 22:53:52 +0700255}