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