git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] pager: factor out a helper to prepare a child process to run the pager

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 16, 2016, 23:06 UTC
Message-ID
<1455664017-27588-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1455664017-27588-1-git-send-email-gitster@pobox.com>

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>
---
 cache.h |  4 ++++
 pager.c | 24 ++++++++++++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/cache.h b/cache.h
index 6bb7119..6827acb 100644
--- a/cache.h
+++ b/cache.h
@@ -210,7 +210,9 @@ struct cache_entry {
 #error "CE_EXTENDED_FLAGS out of range"
 #endif
 
+/* Forward structure decls */
 struct pathspec;
+struct child_process;
 
 /*
  * Copy the sha1 and stat state of a cache entry from one to
@@ -1550,6 +1552,8 @@ extern int pager_use_color;
 extern int term_columns(void);
 extern int decimal_width(uintmax_t);
 extern int check_pager_config(const char *cmd);
+LAST_ARG_MUST_BE_NULL
+extern void prepare_pager_args(struct child_process *, ...);
 
 extern const char *editor_program;
 extern const char *askpass_program;
diff --git a/pager.c b/pager.c
index 5dbcc5a..1406370 100644
--- a/pager.c
+++ b/pager.c
@@ -53,6 +53,23 @@ const char *git_pager(int stdout_is_tty)
 	return pager;
 }
 
+void prepare_pager_args(struct child_process *pager_process, ...)
+{
+	va_list ap;
+	const char *arg;
+
+	va_start(ap, pager_process);
+	while ((arg = va_arg(ap, const char *)))
+		argv_array_push(&pager_process->args, arg);
+	va_end(ap);
+
+	pager_process->use_shell = 1;
+	if (!getenv("LESS"))
+		argv_array_push(&pager_process->env_array, "LESS=FRX");
+	if (!getenv("LV"))
+		argv_array_push(&pager_process->env_array, "LV=-c");
+}
+
 void setup_pager(void)
 {
 	const char *pager = git_pager(isatty(1));
@@ -69,13 +86,8 @@ void setup_pager(void)
 	setenv("GIT_PAGER_IN_USE", "true", 1);
 
 	/* spawn the pager */
-	argv_array_push(&pager_process.args, pager);
-	pager_process.use_shell = 1;
+	prepare_pager_args(&pager_process, pager, NULL);
 	pager_process.in = -1;
-	if (!getenv("LESS"))
-		argv_array_push(&pager_process.env_array, "LESS=FRX");
-	if (!getenv("LV"))
-		argv_array_push(&pager_process.env_array, "LV=-c");
 	argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
 	if (start_command(&pager_process))
 		return;
-- 
2.7.1-460-gd45d0a4
Previous: Junio C HamanoNext: Jeff King
Message 3 of 12 in “fix "v"iew subcommand in "git am -i"”
  1. 0/3 fix "v"iew subcommand in "git am -i"Junio C Hamano, Feb 16, 2016
  2. 1/3 pager: lose a separate argv[]Junio C Hamano, Feb 16, 2016
  3. 2/3 pager: factor out a helper to prepare a child process to run the pagerJunio C Hamano, Feb 16, 2016
  4. Jeff KingFeb 16, 2016
  5. Junio C HamanoFeb 16, 2016
  6. Jeff KingFeb 17, 2016
  7. 3/3 am -i: fix "v"iewJunio C Hamano, Feb 16, 2016
  8. 0/3 fix "v"iew subcommand in "git am -i"Junio C Hamano, Feb 17, 2016
  9. 1/3 pager: lose a separate argv[]Junio C Hamano, Feb 17, 2016
  10. 2/3 pager: factor out a helper to prepare a child process to run the pagerJunio C Hamano, Feb 17, 2016
  11. 3/3 am -i: fix "v"iewJunio C Hamano, Feb 17, 2016
  12. Jeff KingFeb 17, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.