init: call set_git_dir_init() from within init_db()
[git.git] / pager.c
blobf97a8bdc90bf88a686c0e7ed84b4be1df52d7fdd
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
5 #ifndef DEFAULT_PAGER
6 #define DEFAULT_PAGER "less"
7 #endif
9 static struct child_process pager_process = CHILD_PROCESS_INIT;
10 static const char *pager_program;
12 static void wait_for_pager(int in_signal)
14 if (!in_signal) {
15 fflush(stdout);
16 fflush(stderr);
18 /* signal EOF to pager */
19 close(1);
20 close(2);
21 if (in_signal)
22 finish_command_in_signal(&pager_process);
23 else
24 finish_command(&pager_process);
27 static void wait_for_pager_atexit(void)
29 wait_for_pager(0);
32 static void wait_for_pager_signal(int signo)
34 wait_for_pager(1);
35 sigchain_pop(signo);
36 raise(signo);
39 static int core_pager_config(const char *var, const char *value, void *data)
41 if (!strcmp(var, "core.pager"))
42 return git_config_string(&pager_program, var, value);
43 return 0;
46 static void read_early_config(config_fn_t cb, void *data)
48 git_config_with_options(cb, data, NULL, 1);
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.
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.
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).
68 if (!startup_info->have_repository) {
69 struct git_config_source repo_config;
71 memset(&repo_config, 0, sizeof(repo_config));
72 repo_config.file = ".git/config";
73 git_config_with_options(cb, data, &repo_config, 1);
77 const char *git_pager(int stdout_is_tty)
79 const char *pager;
81 if (!stdout_is_tty)
82 return NULL;
84 pager = getenv("GIT_PAGER");
85 if (!pager) {
86 if (!pager_program)
87 read_early_config(core_pager_config, NULL);
88 pager = pager_program;
90 if (!pager)
91 pager = getenv("PAGER");
92 if (!pager)
93 pager = DEFAULT_PAGER;
94 if (!*pager || !strcmp(pager, "cat"))
95 pager = NULL;
97 return pager;
100 void prepare_pager_args(struct child_process *pager_process, const char *pager)
102 argv_array_push(&pager_process->args, pager);
103 pager_process->use_shell = 1;
104 if (!getenv("LESS"))
105 argv_array_push(&pager_process->env_array, "LESS=FRX");
106 if (!getenv("LV"))
107 argv_array_push(&pager_process->env_array, "LV=-c");
110 void setup_pager(void)
112 const char *pager = git_pager(isatty(1));
114 if (!pager)
115 return;
118 * force computing the width of the terminal before we redirect
119 * the standard output to the pager.
121 (void) term_columns();
123 setenv("GIT_PAGER_IN_USE", "true", 1);
125 /* spawn the pager */
126 prepare_pager_args(&pager_process, pager);
127 pager_process.in = -1;
128 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
129 if (start_command(&pager_process))
130 return;
132 /* original process continues, but writes to the pipe */
133 dup2(pager_process.in, 1);
134 if (isatty(2))
135 dup2(pager_process.in, 2);
136 close(pager_process.in);
138 /* this makes sure that the parent terminates after the pager */
139 sigchain_push_common(wait_for_pager_signal);
140 atexit(wait_for_pager_atexit);
143 int pager_in_use(void)
145 const char *env;
146 env = getenv("GIT_PAGER_IN_USE");
147 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
151 * Return cached value (if set) or $COLUMNS environment variable (if
152 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
153 * and default to 80 if all else fails.
155 int term_columns(void)
157 static int term_columns_at_startup;
159 char *col_string;
160 int n_cols;
162 if (term_columns_at_startup)
163 return term_columns_at_startup;
165 term_columns_at_startup = 80;
167 col_string = getenv("COLUMNS");
168 if (col_string && (n_cols = atoi(col_string)) > 0)
169 term_columns_at_startup = n_cols;
170 #ifdef TIOCGWINSZ
171 else {
172 struct winsize ws;
173 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
174 term_columns_at_startup = ws.ws_col;
176 #endif
178 return term_columns_at_startup;
182 * How many columns do we need to show this number in decimal?
184 int decimal_width(uintmax_t number)
186 int width;
188 for (width = 1; number >= 10; width++)
189 number /= 10;
190 return width;
193 struct pager_command_config_data {
194 const char *cmd;
195 int want;
196 char *value;
199 static int pager_command_config(const char *var, const char *value, void *vdata)
201 struct pager_command_config_data *data = vdata;
202 const char *cmd;
204 if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
205 int b = git_config_maybe_bool(var, value);
206 if (b >= 0)
207 data->want = b;
208 else {
209 data->want = 1;
210 data->value = xstrdup(value);
214 return 0;
217 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
218 int check_pager_config(const char *cmd)
220 struct pager_command_config_data data;
222 data.cmd = cmd;
223 data.want = -1;
224 data.value = NULL;
226 read_early_config(pager_command_config, &data);
228 if (data.value)
229 pager_program = data.value;
230 return data.want;