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>

Jeff King committed Sep 12, 2016 at 20:23 UTC eed270720227c761213a61fb6d75a44ac5a6f290
1 file changed +33 -2
pager.c
+33 -2
@@ -43,6 +43,37 @@ static int core_pager_config(const char *var, const char *value, void *data)
43 return 0;
44 }
45
46 +static void read_early_config(config_fn_t cb, void *data)
47 +{
48 + git_config_with_options(cb, data, NULL, 1);
49 +
50 + /*
51 + * Note that this is a really dirty hack that does the wrong thing in
52 + * many cases. The crux of the problem is that we cannot run
53 + * setup_git_directory() early on in git's setup, so we have no idea if
54 + * we are in a repository or not, and therefore are not sure whether
55 + * and how to read repository-local config.
56 + *
57 + * So if we _aren't_ in a repository (or we are but we would reject its
58 + * core.repositoryformatversion), we'll read whatever is in .git/config
59 + * blindly. Similarly, if we _are_ in a repository, but not at the
60 + * root, we'll fail to find .git/config (because it's really
61 + * ../.git/config, etc). See t7006 for a complete set of failures.
62 + *
63 + * However, we have historically provided this hack because it does
64 + * work some of the time (namely when you are at the top-level of a
65 + * valid repository), and would rarely make things worse (i.e., you do
66 + * not generally have a .git/config file sitting around).
67 + */
68 + if (!startup_info->have_repository) {
69 + struct git_config_source repo_config;
70 +
71 + memset(&repo_config, 0, sizeof(repo_config));
72 + repo_config.file = ".git/config";
73 + git_config_with_options(cb, data, &repo_config, 1);
74 + }
75 +}
76 +
77 const char *git_pager(int stdout_is_tty)
78 {
79 const char *pager;
@@ -53,7 +84,7 @@ const char *git_pager(int stdout_is_tty)
84 pager = getenv("GIT_PAGER");
85 if (!pager) {
86 if (!pager_program)
56 - git_config(core_pager_config, NULL);
87 + read_early_config(core_pager_config, NULL);
88 pager = pager_program;
89 }
90 if (!pager)
@@ -192,7 +223,7 @@ int check_pager_config(const char *cmd)
223 data.want = -1;
224 data.value = NULL;
225
195 - git_config(pager_command_config, &data);
226 + read_early_config(pager_command_config, &data);
227
228 if (data.value)
229 pager_program = data.value;