git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: "Dennis Kaarsemaker" <dennis@kaarsemaker.net>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 11/16] pager: handle early config
Date: Mon, 12 Sep 2016 20:23:56 -0700	[thread overview]
Message-ID: <20160913032356.mj6q5qv6kllbcdfb@sigill.intra.peff.net> (raw)
In-Reply-To: <20160913032242.coyuhyhn6uklewuk@sigill.intra.peff.net>

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>
---
 pager.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/pager.c b/pager.c
index 46cc411..ae79643 100644
--- a/pager.c
+++ b/pager.c
@@ -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)
@@ -216,7 +247,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;
-- 
2.10.0.230.g6f8d04b


  parent reply	other threads:[~2016-09-13  3:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-13  3:22 [PATCH 0/16] fix config-reading in non-repos Jeff King
2016-09-13  3:23 ` [PATCH 01/16] t1007: factor out repeated setup Jeff King
2016-09-13 21:42   ` Stefan Beller
2016-09-13  3:23 ` [PATCH 02/16] hash-object: always try to set up the git repository Jeff King
2016-09-13  3:23 ` [PATCH 03/16] patch-id: use RUN_SETUP_GENTLY Jeff King
2016-09-13  3:23 ` [PATCH 04/16] diff: skip implicit no-index check when given --no-index Jeff King
2016-09-13  3:23 ` [PATCH 05/16] diff: handle --no-index prefixes consistently Jeff King
2016-09-13  3:23 ` [PATCH 06/16] diff: always try to set up the repository Jeff King
2016-09-13 22:00   ` Stefan Beller
2016-09-13 22:22     ` Jeff King
2016-09-13  3:23 ` [PATCH 07/16] pager: remove obsolete comment Jeff King
2016-09-13  3:23 ` [PATCH 08/16] pager: stop loading git_default_config() Jeff King
2016-09-13  3:23 ` [PATCH 09/16] pager: make pager_program a file-local static Jeff King
2016-09-13  3:23 ` [PATCH 10/16] pager: use callbacks instead of configset Jeff King
2016-09-13  3:23 ` Jeff King [this message]
2016-09-13  3:24 ` [PATCH 12/16] t1302: use "git -C" Jeff King
2016-09-13  3:24 ` [PATCH 13/16] test-config: setup git directory Jeff King
2016-09-13  3:24 ` [PATCH 14/16] config: only read .git/config from configured repos Jeff King
2016-09-13  3:24 ` [PATCH 15/16] init: expand comments explaining config trickery Jeff King
2016-09-13  3:24 ` [PATCH 16/16] init: reset cached config when entering new repo Jeff King
2016-09-13 22:18   ` Stefan Beller
2016-09-14 10:55 ` [PATCH 0/16] fix config-reading in non-repos Dennis Kaarsemaker
2016-09-14 15:31   ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160913032356.mj6q5qv6kllbcdfb@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=dennis@kaarsemaker.net \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).