2006-02-28 20:26:21 +01:00
|
|
|
#include "cache.h"
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
#include "run-command.h"
|
2006-02-28 20:26:21 +01:00
|
|
|
|
|
|
|
/*
|
2007-12-08 21:28:41 +01:00
|
|
|
* This is split up from the rest of git so that we can do
|
|
|
|
* something different on Windows.
|
2006-02-28 20:26:21 +01:00
|
|
|
*/
|
|
|
|
|
2007-12-11 07:27:33 +01:00
|
|
|
static int spawned_pager;
|
|
|
|
|
2007-12-08 21:28:41 +01:00
|
|
|
#ifndef __MINGW32__
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
static void pager_preexec(void)
|
2006-02-28 20:26:21 +01:00
|
|
|
{
|
2007-01-24 20:21:10 +01:00
|
|
|
/*
|
|
|
|
* Work around bug in "less" by not starting it until we
|
|
|
|
* have real input
|
|
|
|
*/
|
|
|
|
fd_set in;
|
|
|
|
|
|
|
|
FD_ZERO(&in);
|
|
|
|
FD_SET(0, &in);
|
|
|
|
select(1, &in, NULL, &in, NULL);
|
|
|
|
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
setenv("LESS", "FRSX", 0);
|
2006-02-28 20:26:21 +01:00
|
|
|
}
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
#endif
|
2007-12-08 21:28:41 +01:00
|
|
|
|
|
|
|
static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
static struct child_process pager_process;
|
|
|
|
|
2007-12-08 21:28:41 +01:00
|
|
|
static void wait_for_pager(void)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
/* signal EOF to pager */
|
|
|
|
close(1);
|
|
|
|
close(2);
|
|
|
|
finish_command(&pager_process);
|
|
|
|
}
|
2006-02-28 20:26:21 +01:00
|
|
|
|
|
|
|
void setup_pager(void)
|
|
|
|
{
|
2006-07-31 15:27:00 +02:00
|
|
|
const char *pager = getenv("GIT_PAGER");
|
2006-02-28 20:26:21 +01:00
|
|
|
|
|
|
|
if (!isatty(1))
|
|
|
|
return;
|
2007-08-07 06:08:43 +02:00
|
|
|
if (!pager) {
|
|
|
|
if (!pager_program)
|
2008-05-14 19:46:53 +02:00
|
|
|
git_config(git_default_config, NULL);
|
2007-07-03 20:18:11 +02:00
|
|
|
pager = pager_program;
|
2007-08-07 06:08:43 +02:00
|
|
|
}
|
2006-07-31 15:27:00 +02:00
|
|
|
if (!pager)
|
|
|
|
pager = getenv("PAGER");
|
2006-04-16 04:44:25 +02:00
|
|
|
if (!pager)
|
|
|
|
pager = "less";
|
2006-04-16 10:46:08 +02:00
|
|
|
else if (!*pager || !strcmp(pager, "cat"))
|
2006-04-16 04:44:25 +02:00
|
|
|
return;
|
|
|
|
|
2007-12-11 07:27:33 +01:00
|
|
|
spawned_pager = 1; /* means we are emitting to terminal */
|
2006-06-07 01:58:40 +02:00
|
|
|
|
2007-12-08 21:28:41 +01:00
|
|
|
/* spawn the pager */
|
|
|
|
pager_argv[2] = pager;
|
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>
2008-07-22 09:14:12 +02:00
|
|
|
pager_process.argv = pager_argv;
|
|
|
|
pager_process.in = -1;
|
|
|
|
#ifndef __MINGW32__
|
|
|
|
pager_process.preexec_cb = pager_preexec;
|
|
|
|
#endif
|
2007-12-08 21:28:41 +01:00
|
|
|
if (start_command(&pager_process))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* original process continues, but writes to the pipe */
|
|
|
|
dup2(pager_process.in, 1);
|
2008-12-15 09:33:34 +01:00
|
|
|
if (isatty(2))
|
|
|
|
dup2(pager_process.in, 2);
|
2007-12-08 21:28:41 +01:00
|
|
|
close(pager_process.in);
|
|
|
|
|
|
|
|
/* this makes sure that the parent terminates after the pager */
|
|
|
|
atexit(wait_for_pager);
|
2006-02-28 20:26:21 +01:00
|
|
|
}
|
2007-12-11 07:27:33 +01:00
|
|
|
|
|
|
|
int pager_in_use(void)
|
|
|
|
{
|
|
|
|
const char *env;
|
|
|
|
|
|
|
|
if (spawned_pager)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
env = getenv("GIT_PAGER_IN_USE");
|
|
|
|
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
|
|
|
|
}
|