@matheusbsbs / matheus-bagliano / commits / ea27a18ce2
Fork 0

spawn pager via run_command interface

This has two important effects: 1. The pager is now the _child_ process, instead of the parent. This means that whatever spawned git (e.g., the shell) will see the exit code of the git process, and not the pager. 2. The mingw and regular code are now unified, which makes the setup_pager function much simpler. There are two caveats: 1. We used to call execlp directly on the pager, followed by trying to exec it via the shall. We now just use the shell (which is what mingw has always done). This may have different results for pager names which contain shell metacharacters. It is also slightly less efficient because we unnecessarily run the shell; however, pager spawning is by definition an interactive task, so it shouldn't be a huge problem. 2. The git process will remain in memory while the user looks through the pager. This is potentially wasteful. We could get around this by turning the parent into a meta-process which spawns _both_ git and the pager, collects the exit status from git, waits for both to end, and then exits with git's exit code. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 22, 2008 at 03:14 UTC ea27a18ce2bf5860974745c04c24864231029e1d
1 file changed +11 -44
pager.c
+11 -44
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "run-command.h"
3
4 /*
5 * This is split up from the rest of git so that we can do
@@ -8,7 +9,7 @@
9 static int spawned_pager;
10
11 #ifndef __MINGW32__
11 -static void run_pager(const char *pager)
12 +static void pager_preexec(void)
13 {
14 /*
15 * Work around bug in "less" by not starting it until we
@@ -20,17 +21,13 @@ static void run_pager(const char *pager)
21 FD_SET(0, &in);
22 select(1, &in, NULL, &in, NULL);
23
23 - execlp(pager, pager, NULL);
24 - execl("/bin/sh", "sh", "-c", pager, NULL);
24 + setenv("LESS", "FRSX", 0);
25 }
26 -#else
27 -#include "run-command.h"
26 +#endif
27
28 static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
30 -static struct child_process pager_process = {
31 - .argv = pager_argv,
32 - .in = -1
33 -};
29 +static struct child_process pager_process;
30 +
31 static void wait_for_pager(void)
32 {
33 fflush(stdout);
@@ -40,14 +37,9 @@ static void wait_for_pager(void)
37 close(2);
38 finish_command(&pager_process);
39 }
43 -#endif
40
41 void setup_pager(void)
42 {
47 -#ifndef __MINGW32__
48 - pid_t pid;
49 - int fd[2];
50 -#endif
43 const char *pager = getenv("GIT_PAGER");
44
45 if (!isatty(1))
@@ -66,37 +58,13 @@ void setup_pager(void)
58
59 spawned_pager = 1; /* means we are emitting to terminal */
60
69 -#ifndef __MINGW32__
70 - if (pipe(fd) < 0)
71 - return;
72 - pid = fork();
73 - if (pid < 0) {
74 - close(fd[0]);
75 - close(fd[1]);
76 - return;
77 - }
78 -
79 - /* return in the child */
80 - if (!pid) {
81 - dup2(fd[1], 1);
82 - dup2(fd[1], 2);
83 - close(fd[0]);
84 - close(fd[1]);
85 - return;
86 - }
87 -
88 - /* The original process turns into the PAGER */
89 - dup2(fd[0], 0);
90 - close(fd[0]);
91 - close(fd[1]);
92 -
93 - setenv("LESS", "FRSX", 0);
94 - run_pager(pager);
95 - die("unable to execute pager '%s'", pager);
96 - exit(255);
97 -#else
61 /* spawn the pager */
62 pager_argv[2] = pager;
63 + pager_process.argv = pager_argv;
64 + pager_process.in = -1;
65 +#ifndef __MINGW32__
66 + pager_process.preexec_cb = pager_preexec;
67 +#endif
68 if (start_command(&pager_process))
69 return;
70
@@ -107,7 +75,6 @@ void setup_pager(void)
75
76 /* this makes sure that the parent terminates after the pager */
77 atexit(wait_for_pager);
110 -#endif
78 }
79
80 int pager_in_use(void)