pager: move pager-specific setup into the build
Allowing PAGER_ENV to be set at build-time allows us to move pager-specific knowledge out of our build. This allows us to set a better default for FreeBSD more(1), which pretends not to understand ANSI color escapes if the MORE environment variable is left empty, but accepts the same variables as less(1) Originally-from: https://public-inbox.org/git/xmqq61piw4yf.fsf@gitster.dls.corp.google.com/ Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Eric Wong <e@80x24.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Wong committed
Aug 4, 2016 at 11:40 UTC
995bc22d7f8c611e342095a211065f8585a08e65
5 files changed
+67
-8
Makefile
+20
-1
@@ -367,6 +367,14 @@ all::
367
# Define HAVE_BSD_SYSCTL if your platform has a BSD-compatible sysctl function.
368
#
369
# Define HAVE_GETDELIM if your system has the getdelim() function.
370
+#
371
+# Define PAGER_ENV to a SP separated VAR=VAL pairs to define
372
+# default environment variables to be passed when a pager is spawned, e.g.
373
+#
374
+# PAGER_ENV = LESS=FRX LV=-c
375
+#
376
+# to say "export LESS=FRX (and LV=-c) if the environment variable
377
+# LESS (and LV) is not set, respectively".
378
379
GIT-VERSION-FILE: FORCE
380
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1492,6 +1500,10 @@ ifeq ($(PYTHON_PATH),)
1500
NO_PYTHON = NoThanks
1501
endif
1502
1503
+ifndef PAGER_ENV
1504
+PAGER_ENV = LESS=FRX LV=-c
1505
+endif
1506
+
1507
QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
1508
QUIET_SUBDIR1 =
1509
@@ -1613,6 +1625,11 @@ ifdef DEFAULT_HELP_FORMAT
1625
BASIC_CFLAGS += -DDEFAULT_HELP_FORMAT='"$(DEFAULT_HELP_FORMAT)"'
1626
endif
1627
1628
+PAGER_ENV_SQ = $(subst ','\'',$(PAGER_ENV))
1629
+PAGER_ENV_CQ = "$(subst ",\",$(subst \,\\,$(PAGER_ENV)))"
1630
+PAGER_ENV_CQ_SQ = $(subst ','\'',$(PAGER_ENV_CQ))
1631
+BASIC_CFLAGS += -DPAGER_ENV='$(PAGER_ENV_CQ_SQ)'
1632
+
1633
ALL_CFLAGS += $(BASIC_CFLAGS)
1634
ALL_LDFLAGS += $(BASIC_LDFLAGS)
1635
@@ -1737,7 +1754,7 @@ common-cmds.h: $(wildcard Documentation/git-*.txt)
1754
1755
SCRIPT_DEFINES = $(SHELL_PATH_SQ):$(DIFF_SQ):$(GIT_VERSION):\
1756
$(localedir_SQ):$(NO_CURL):$(USE_GETTEXT_SCHEME):$(SANE_TOOL_PATH_SQ):\
1740
- $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP)
1757
+ $(gitwebdir_SQ):$(PERL_PATH_SQ):$(SANE_TEXT_GREP):$(PAGER_ENV)
1758
define cmd_munge_script
1759
$(RM) $@ $@+ && \
1760
sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
@@ -1750,6 +1767,7 @@ sed -e '1s|#!.*/sh|#!$(SHELL_PATH_SQ)|' \
1767
-e 's|@@GITWEBDIR@@|$(gitwebdir_SQ)|g' \
1768
-e 's|@@PERL@@|$(PERL_PATH_SQ)|g' \
1769
-e 's|@@SANE_TEXT_GREP@@|$(SANE_TEXT_GREP)|g' \
1770
+ -e 's|@@PAGER_ENV@@|$(PAGER_ENV_SQ)|g' \
1771
$@.sh >$@+
1772
endef
1773
@@ -2153,6 +2171,7 @@ GIT-BUILD-OPTIONS: FORCE
2171
@echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@+
2172
@echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@+
2173
@echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@+
2174
+ @echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+
2175
ifdef TEST_OUTPUT_DIRECTORY
2176
@echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+
2177
endif
config.mak.uname
+1
@@ -206,6 +206,7 @@ ifeq ($(uname_S),FreeBSD)
206
HAVE_PATHS_H = YesPlease
207
GMTIME_UNRELIABLE_ERRORS = UnfortunatelyYes
208
HAVE_BSD_SYSCTL = YesPlease
209
+ PAGER_ENV = LESS=FRX LV=-c MORE=FRX
210
endif
211
ifeq ($(uname_S),OpenBSD)
212
NO_STRCASESTR = YesPlease
git-sh-setup.sh
+5
-3
@@ -160,9 +160,11 @@ git_pager() {
160
else
161
GIT_PAGER=cat
162
fi
163
- : "${LESS=-FRX}"
164
- : "${LV=-c}"
165
- export LESS LV
163
+ for vardef in @@PAGER_ENV@@
164
+ do
165
+ var=${vardef%%=*}
166
+ eval ": \"\${$vardef}\" && export $var"
167
+ done
168
169
eval "$GIT_PAGER" '"$@"'
170
}
pager.c
+28
-4
@@ -63,14 +63,38 @@ const char *git_pager(int stdout_is_tty)
63
return pager;
64
}
65
66
+static void setup_pager_env(struct argv_array *env)
67
+{
68
+ const char **argv;
69
+ int i;
70
+ char *pager_env = xstrdup(PAGER_ENV);
71
+ int n = split_cmdline(pager_env, &argv);
72
+
73
+ if (n < 0)
74
+ die("malformed build-time PAGER_ENV: %s",
75
+ split_cmdline_strerror(n));
76
+
77
+ for (i = 0; i < n; i++) {
78
+ char *cp = strchr(argv[i], '=');
79
+
80
+ if (!cp)
81
+ die("malformed build-time PAGER_ENV");
82
+
83
+ *cp = '\0';
84
+ if (!getenv(argv[i])) {
85
+ *cp = '=';
86
+ argv_array_push(env, argv[i]);
87
+ }
88
+ }
89
+ free(pager_env);
90
+ free(argv);
91
+}
92
+
93
void prepare_pager_args(struct child_process *pager_process, const char *pager)
94
{
95
argv_array_push(&pager_process->args, pager);
96
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");
97
+ setup_pager_env(&pager_process->env_array);
98
}
99
100
void setup_pager(void)
t/t7006-pager.sh
+13
@@ -49,6 +49,19 @@ test_expect_success TTY 'LESS and LV envvars are set for pagination' '
49
grep ^LV= pager-env.out
50
'
51
52
+test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
53
+ (
54
+ sane_unset LESS LV &&
55
+ PAGER="env >pager-env.out; wc" &&
56
+ export PAGER &&
57
+ PATH="$(git --exec-path):$PATH" &&
58
+ export PATH &&
59
+ test_terminal sh -c ". git-sh-setup && git_pager"
60
+ ) &&
61
+ grep ^LESS= pager-env.out &&
62
+ grep ^LV= pager-env.out
63
+'
64
+
65
test_expect_success TTY 'some commands do not use a pager' '
66
rm -f paginated.out &&
67
test_terminal git rev-list HEAD &&