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