summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-02-05 13:08:57 -0500
committerJunio C Hamano <gitster@pobox.com>2014-02-05 11:31:38 -0800
commitf82c3ffd862c7677ec28f4a3438b733ab449c619 (patch)
tree2ca9c5a93a533704359f197f44cc823cc4fc4585
parentf85052c64ba85784020074bcd9c5805be8a921ae (diff)
downloadgit-f82c3ffd862c7677ec28f4a3438b733ab449c619.tar.gz
move LESS/LV pager environment to Makefile
We set the LESS and LV variables to sensible defaults if they are not already set. However, the code is brittle. There is no easy way to change the defaults at compile time, and we have to duplicate the code in git-sh-setup and in pager.c. Let's turn it into a normal Makefile knob instead. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--Makefile22
-rw-r--r--git-sh-setup.sh9
-rw-r--r--pager.c34
-rw-r--r--script/mkcarray25
4 files changed, 74 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index f60e5943e8..c7dd6b2257 100644
--- a/Makefile
+++ b/Makefile
@@ -342,6 +342,14 @@ all::
# Define DEFAULT_HELP_FORMAT to "man", "info" or "html"
# (defaults to "man") if you want to have a different default when
# "git help" is called without a parameter specifying the format.
+#
+# Define PAGER_ENV to a SP separated VAR=VAL pairs to define
+# default environment variables to be passed when a pager is spawned, e.g.
+#
+# PAGER_ENV = LESS=-FRSX LV=-c
+#
+# to say "export LESS=-FRSX (and LV=-c) if the environment variable
+# LESS (and LV) is not set, respectively".
GIT-VERSION-FILE: FORCE
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -1505,6 +1513,10 @@ ifeq ($(PYTHON_PATH),)
NO_PYTHON = NoThanks
endif
+ifndef PAGER_ENV
+PAGER_ENV = LESS=-FRSX LV=-c
+endif
+
QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
QUIET_SUBDIR1 =
@@ -1598,6 +1610,11 @@ MAKE/%-string.h: MAKE/% script/mkcstring
$(subst -,_,$*) <$< >$@+ && \
mv $@+ $@
+MAKE/%-array.h: MAKE/% script/mkcarray
+ $(QUIET_GEN)$(SHELL_PATH) script/mkcarray \
+ $(subst -,_,$*) <$< >$@+ && \
+ mv $@+ $@
+
MAKE/%.sh: MAKE/% script/mksh
$(QUIET_GEN)$(SHELL_PATH) script/mksh \
$(subst -,_,$*) <$< >$@+ && \
@@ -1726,6 +1743,9 @@ builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
version.sp version.s version.o: MAKE/VERSION-string.h MAKE/USER-AGENT-string.h
+$(eval $(call make-var,PAGER-ENV,pager environment,$(PAGER_ENV)))
+pager.sp pager.s pager.o: MAKE/PAGER-ENV-array.h
+
$(BUILT_INS): git$X
$(QUIET_BUILT_IN)$(RM) $@ && \
ln $< $@ 2>/dev/null || \
@@ -1776,7 +1796,7 @@ $(SCRIPT_LIB) : % : %.sh MAKE/SCRIPT-DEFINES
$(QUIET_GEN)$(cmd_munge_script) && \
mv $@+ $@
-git-sh-setup: MAKE/DIFF.sh
+git-sh-setup: MAKE/DIFF.sh MAKE/PAGER-ENV.sh
git.res: git.rc GIT-VERSION-FILE
$(QUIET_RC)$(RC) \
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index 627d289d19..be4a58a49c 100644
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -158,10 +158,11 @@ git_pager() {
else
GIT_PAGER=cat
fi
- : ${LESS=-FRSX}
- : ${LV=-c}
- export LESS LV
-
+ for vardef in $MAKE_PAGER_ENV
+ do
+ var=${vardef%%=*}
+ eval ": \${$vardef} && export $var"
+ done
eval "$GIT_PAGER" '"$@"'
}
diff --git a/pager.c b/pager.c
index 0cc75a8eee..6db84c6874 100644
--- a/pager.c
+++ b/pager.c
@@ -1,6 +1,8 @@
#include "cache.h"
#include "run-command.h"
#include "sigchain.h"
+#include "argv-array.h"
+#include "MAKE/PAGER-ENV-array.h"
#ifndef DEFAULT_PAGER
#define DEFAULT_PAGER "less"
@@ -60,9 +62,26 @@ const char *git_pager(int stdout_is_tty)
return pager;
}
+static void setup_pager_env(struct argv_array *env)
+{
+ int i;
+
+ for (i = 0; MAKE_PAGER_ENV[i]; i++) {
+ struct strbuf buf = STRBUF_INIT;
+ const char *p = MAKE_PAGER_ENV[i];
+ const char *eq = strchrnul(p, '=');
+
+ strbuf_add(&buf, p, eq - p);
+ if (!getenv(buf.buf))
+ argv_array_push(env, p);
+ strbuf_release(&buf);
+ }
+}
+
void setup_pager(void)
{
const char *pager = git_pager(isatty(1));
+ struct argv_array env = ARGV_ARRAY_INIT;
if (!pager || pager_in_use())
return;
@@ -80,17 +99,10 @@ void setup_pager(void)
pager_process.use_shell = 1;
pager_process.argv = pager_argv;
pager_process.in = -1;
- if (!getenv("LESS") || !getenv("LV")) {
- static const char *env[3];
- int i = 0;
-
- if (!getenv("LESS"))
- env[i++] = "LESS=FRSX";
- if (!getenv("LV"))
- env[i++] = "LV=-c";
- env[i] = NULL;
- pager_process.env = env;
- }
+
+ setup_pager_env(&env);
+ pager_process.env = argv_array_detach(&env, NULL);
+
if (start_command(&pager_process))
return;
diff --git a/script/mkcarray b/script/mkcarray
new file mode 100644
index 0000000000..ed980abd72
--- /dev/null
+++ b/script/mkcarray
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+name=$1; shift
+
+quote() {
+ echo "$1" | sed 's/\\/\\\\/g; s/"/\\"/'
+}
+
+cat <<-EOF
+#ifndef ${name}_H
+#define ${name}_H
+
+static const char *MAKE_${name}[] = {
+EOF
+
+for i in $(cat); do
+ printf '\t"%s",\n' "$(quote "$i")"
+done
+
+cat <<EOF
+ NULL
+};
+
+#endif /* ${name}_H */
+EOF