pager: handle early config

The pager code is often run early in the git.c startup,
before we have actually found the repository. When we ask
git_config() to look for values like core.pager, it doesn't
know where to find the repo-level config, and will blindly
examine ".git/config" if it exists. That's why t7006 shows
that many pager-related features happen to work from the
top-level of a repository, but not from a subdirectory.

This patch pulls that ".git/config" hack explicitly into the
pager code. There are two reasons for this:

  1. We'd like to clean up the git_config() behavior, as
     looking at ".git/config" when we do not have a
     configured repository is often the wrong thing to do.
     But we'd prefer not to break the pager config any worse
     than it already is.

  2. It's one very tiny step on the road to ultimately
     making the pager config work consistently. If we
     eventually get an equivalent of setup_git_directory()
     that _just_ finds the directory and doesn't chdir() or
     set up any global state, we could plug it in here
     (instead of blindly looking at ".git/config").

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2016-09-12 20:23:56 -07:00 committed by Junio C Hamano
parent 6a1e1bc0a1
commit eed2707202

35
pager.c
View File

@ -43,6 +43,37 @@ static int core_pager_config(const char *var, const char *value, void *data)
return 0;
}
static void read_early_config(config_fn_t cb, void *data)
{
git_config_with_options(cb, data, NULL, 1);
/*
* Note that this is a really dirty hack that does the wrong thing in
* many cases. The crux of the problem is that we cannot run
* setup_git_directory() early on in git's setup, so we have no idea if
* we are in a repository or not, and therefore are not sure whether
* and how to read repository-local config.
*
* So if we _aren't_ in a repository (or we are but we would reject its
* core.repositoryformatversion), we'll read whatever is in .git/config
* blindly. Similarly, if we _are_ in a repository, but not at the
* root, we'll fail to find .git/config (because it's really
* ../.git/config, etc). See t7006 for a complete set of failures.
*
* However, we have historically provided this hack because it does
* work some of the time (namely when you are at the top-level of a
* valid repository), and would rarely make things worse (i.e., you do
* not generally have a .git/config file sitting around).
*/
if (!startup_info->have_repository) {
struct git_config_source repo_config;
memset(&repo_config, 0, sizeof(repo_config));
repo_config.file = ".git/config";
git_config_with_options(cb, data, &repo_config, 1);
}
}
const char *git_pager(int stdout_is_tty)
{
const char *pager;
@ -53,7 +84,7 @@ const char *git_pager(int stdout_is_tty)
pager = getenv("GIT_PAGER");
if (!pager) {
if (!pager_program)
git_config(core_pager_config, NULL);
read_early_config(core_pager_config, NULL);
pager = pager_program;
}
if (!pager)
@ -192,7 +223,7 @@ int check_pager_config(const char *cmd)
data.want = -1;
data.value = NULL;
git_config(pager_command_config, &data);
read_early_config(pager_command_config, &data);
if (data.value)
pager_program = data.value;