Coverage Report

Created: 2025-12-31 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/pager.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "config.h"
3
#include "editor.h"
4
#include "pager.h"
5
#include "run-command.h"
6
#include "sigchain.h"
7
#include "alias.h"
8
9
int pager_use_color = 1;
10
11
#ifndef DEFAULT_PAGER
12
0
#define DEFAULT_PAGER "less"
13
#endif
14
15
static struct child_process pager_process;
16
static char *pager_program;
17
static int old_fd1 = -1, old_fd2 = -1;
18
19
/* Is the value coming back from term_columns() just a guess? */
20
static int term_columns_guessed;
21
22
23
static void close_pager_fds(void)
24
0
{
25
  /* signal EOF to pager */
26
0
  close(1);
27
0
  if (old_fd2 != -1)
28
0
    close(2);
29
0
}
30
31
static void finish_pager(void)
32
0
{
33
0
  fflush(stdout);
34
0
  fflush(stderr);
35
0
  close_pager_fds();
36
0
  finish_command(&pager_process);
37
0
}
38
39
static void wait_for_pager_atexit(void)
40
0
{
41
0
  if (old_fd1 == -1)
42
0
    return;
43
44
0
  finish_pager();
45
0
}
46
47
void wait_for_pager(void)
48
0
{
49
0
  if (old_fd1 == -1)
50
0
    return;
51
52
0
  finish_pager();
53
0
  sigchain_pop_common();
54
0
  unsetenv("GIT_PAGER_IN_USE");
55
0
  dup2(old_fd1, 1);
56
0
  close(old_fd1);
57
0
  old_fd1 = -1;
58
0
  if (old_fd2 != -1) {
59
0
    dup2(old_fd2, 2);
60
0
    close(old_fd2);
61
0
    old_fd2 = -1;
62
0
  }
63
0
}
64
65
static void wait_for_pager_signal(int signo)
66
0
{
67
0
  if (old_fd1 == -1)
68
0
    return;
69
70
0
  close_pager_fds();
71
0
  finish_command_in_signal(&pager_process);
72
0
  sigchain_pop(signo);
73
0
  raise(signo);
74
0
}
75
76
static int core_pager_config(const char *var, const char *value,
77
           const struct config_context *ctx UNUSED,
78
           void *data UNUSED)
79
0
{
80
0
  if (!strcmp(var, "core.pager"))
81
0
    return git_config_string(&pager_program, var, value);
82
0
  return 0;
83
0
}
84
85
const char *git_pager(struct repository *r, int stdout_is_tty)
86
0
{
87
0
  const char *pager;
88
89
0
  if (!stdout_is_tty)
90
0
    return NULL;
91
92
0
  pager = getenv("GIT_PAGER");
93
0
  if (!pager) {
94
0
    if (!pager_program)
95
0
      read_early_config(r,
96
0
            core_pager_config, NULL);
97
0
    pager = pager_program;
98
0
  }
99
0
  if (!pager)
100
0
    pager = getenv("PAGER");
101
0
  if (!pager)
102
0
    pager = DEFAULT_PAGER;
103
0
  if (!*pager || !strcmp(pager, "cat"))
104
0
    pager = NULL;
105
106
0
  return pager;
107
0
}
108
109
static void setup_pager_env(struct strvec *env)
110
0
{
111
0
  const char **argv;
112
0
  int i;
113
0
  char *pager_env = xstrdup(PAGER_ENV);
114
0
  int n = split_cmdline(pager_env, &argv);
115
116
0
  if (n < 0)
117
0
    die("malformed build-time PAGER_ENV: %s",
118
0
      split_cmdline_strerror(n));
119
120
0
  for (i = 0; i < n; i++) {
121
0
    char *cp = strchr(argv[i], '=');
122
123
0
    if (!cp)
124
0
      die("malformed build-time PAGER_ENV");
125
126
0
    *cp = '\0';
127
0
    if (!getenv(argv[i])) {
128
0
      *cp = '=';
129
0
      strvec_push(env, argv[i]);
130
0
    }
131
0
  }
132
0
  free(pager_env);
133
0
  free(argv);
134
0
}
135
136
void prepare_pager_args(struct child_process *pager_process, const char *pager)
137
0
{
138
0
  strvec_push(&pager_process->args, pager);
139
0
  pager_process->use_shell = 1;
140
0
  setup_pager_env(&pager_process->env);
141
0
  pager_process->trace2_child_class = "pager";
142
0
}
143
144
void setup_pager(struct repository *r)
145
0
{
146
0
  static int once = 0;
147
0
  const char *pager = git_pager(r, isatty(1));
148
149
0
  if (!pager)
150
0
    return;
151
152
  /*
153
   * After we redirect standard output, we won't be able to use an ioctl
154
   * to get the terminal size. Let's grab it now, and then set $COLUMNS
155
   * to communicate it to any sub-processes.
156
   */
157
0
  {
158
0
    char buf[64];
159
0
    xsnprintf(buf, sizeof(buf), "%d", term_columns());
160
0
    if (!term_columns_guessed)
161
0
      setenv("COLUMNS", buf, 0);
162
0
  }
163
164
0
  setenv("GIT_PAGER_IN_USE", "true", 1);
165
166
0
  child_process_init(&pager_process);
167
168
  /* spawn the pager */
169
0
  prepare_pager_args(&pager_process, pager);
170
0
  pager_process.in = -1;
171
0
  strvec_push(&pager_process.env, "GIT_PAGER_IN_USE");
172
0
  if (start_command(&pager_process))
173
0
    die("unable to execute pager '%s'", pager);
174
175
  /* original process continues, but writes to the pipe */
176
0
  old_fd1 = dup(1);
177
0
  dup2(pager_process.in, 1);
178
0
  if (isatty(2)) {
179
0
    old_fd2 = dup(2);
180
0
    dup2(pager_process.in, 2);
181
0
  }
182
0
  close(pager_process.in);
183
184
0
  sigchain_push_common(wait_for_pager_signal);
185
186
0
  if (!once) {
187
0
    once++;
188
0
    atexit(wait_for_pager_atexit);
189
0
  }
190
0
}
191
192
int pager_in_use(void)
193
0
{
194
0
  return git_env_bool("GIT_PAGER_IN_USE", 0);
195
0
}
196
197
/*
198
 * Return cached value (if set) or $COLUMNS environment variable (if
199
 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
200
 * and default to 80 if all else fails.
201
 */
202
int term_columns(void)
203
0
{
204
0
  static int term_columns_at_startup;
205
206
0
  char *col_string;
207
0
  int n_cols;
208
209
0
  if (term_columns_at_startup)
210
0
    return term_columns_at_startup;
211
212
0
  term_columns_at_startup = 80;
213
0
  term_columns_guessed = 1;
214
215
0
  col_string = getenv("COLUMNS");
216
0
  if (col_string && (n_cols = atoi(col_string)) > 0) {
217
0
    term_columns_at_startup = n_cols;
218
0
    term_columns_guessed = 0;
219
0
  }
220
0
#ifdef TIOCGWINSZ
221
0
  else {
222
0
    struct winsize ws;
223
0
    if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) {
224
0
      term_columns_at_startup = ws.ws_col;
225
0
      term_columns_guessed = 0;
226
0
    }
227
0
  }
228
0
#endif
229
230
0
  return term_columns_at_startup;
231
0
}
232
233
/*
234
 * Clear the entire line, leave cursor in first column.
235
 */
236
void term_clear_line(void)
237
0
{
238
0
  if (!isatty(2))
239
0
    return;
240
0
  if (is_terminal_dumb())
241
    /*
242
     * Fall back to print a terminal width worth of space
243
     * characters (hoping that the terminal is still as wide
244
     * as it was upon the first call to term_columns()).
245
     */
246
0
    fprintf(stderr, "\r%*s\r", term_columns(), "");
247
0
  else
248
    /*
249
     * On non-dumb terminals use an escape sequence to clear
250
     * the whole line, no matter how wide the terminal.
251
     */
252
0
    fputs("\r\033[K", stderr);
253
0
}
254
255
/*
256
 * How many columns do we need to show this number in decimal?
257
 */
258
int decimal_width(uintmax_t number)
259
0
{
260
0
  int width;
261
262
0
  for (width = 1; number >= 10; width++)
263
0
    number /= 10;
264
0
  return width;
265
0
}
266
267
struct pager_command_config_data {
268
  const char *cmd;
269
  int want;
270
  char *value;
271
};
272
273
static int pager_command_config(const char *var, const char *value,
274
        const struct config_context *ctx UNUSED,
275
        void *vdata)
276
0
{
277
0
  struct pager_command_config_data *data = vdata;
278
0
  const char *cmd;
279
280
0
  if (skip_prefix(var, "pager.", &cmd) && !strcmp(cmd, data->cmd)) {
281
0
    int b = git_parse_maybe_bool(value);
282
0
    if (b >= 0)
283
0
      data->want = b;
284
0
    else {
285
0
      data->want = 1;
286
0
      data->value = xstrdup(value);
287
0
    }
288
0
  }
289
290
0
  return 0;
291
0
}
292
293
/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
294
int check_pager_config(struct repository *r, const char *cmd)
295
0
{
296
0
  struct pager_command_config_data data;
297
298
0
  data.cmd = cmd;
299
0
  data.want = -1;
300
0
  data.value = NULL;
301
302
0
  read_early_config(r, pager_command_config, &data);
303
304
0
  if (data.value)
305
0
    pager_program = data.value;
306
0
  return data.want;
307
0
}