pager: factor out a helper to prepare a child process to run the pager

When running a pager, we need to run the program git_pager() gave us, but we need to make sure we spawn it via the shell (i.e. it is valid to say PAGER='less -S', for example) and give default values to $LESS and $LV environment variables. Factor out these details to a separate helper function. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 16, 2016 at 14:34 UTC 3e3a4a41b0dac564c0302ced4ccc423d0d39bc21
2 files changed +14 -6
cache.h
+3
@@ -210,7 +210,9 @@ struct cache_entry {
210 #error "CE_EXTENDED_FLAGS out of range"
211 #endif
212
213 +/* Forward structure decls */
214 struct pathspec;
215 +struct child_process;
216
217 /*
218 * Copy the sha1 and stat state of a cache entry from one to
@@ -1550,6 +1552,7 @@ extern int pager_use_color;
1552 extern int term_columns(void);
1553 extern int decimal_width(uintmax_t);
1554 extern int check_pager_config(const char *cmd);
1555 +extern void prepare_pager_args(struct child_process *, const char *pager);
1556
1557 extern const char *editor_program;
1558 extern const char *askpass_program;
pager.c
+11 -6
@@ -53,6 +53,16 @@ const char *git_pager(int stdout_is_tty)
53 return pager;
54 }
55
56 +void prepare_pager_args(struct child_process *pager_process, const char *pager)
57 +{
58 + argv_array_push(&pager_process->args, pager);
59 + pager_process->use_shell = 1;
60 + if (!getenv("LESS"))
61 + argv_array_push(&pager_process->env_array, "LESS=FRX");
62 + if (!getenv("LV"))
63 + argv_array_push(&pager_process->env_array, "LV=-c");
64 +}
65 +
66 void setup_pager(void)
67 {
68 const char *pager = git_pager(isatty(1));
@@ -69,13 +79,8 @@ void setup_pager(void)
79 setenv("GIT_PAGER_IN_USE", "true", 1);
80
81 /* spawn the pager */
72 - argv_array_push(&pager_process.args, pager);
73 - pager_process.use_shell = 1;
82 + prepare_pager_args(&pager_process, pager);
83 pager_process.in = -1;
75 - if (!getenv("LESS"))
76 - argv_array_push(&pager_process.env_array, "LESS=FRX");
77 - if (!getenv("LV"))
78 - argv_array_push(&pager_process.env_array, "LV=-c");
84 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
85 if (start_command(&pager_process))
86 return;