git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [Bug report] includeIf config is not displayed in normal directories

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 16, 2020, 00:23 UTC
Message-ID
<xmqq4kkm7g55.fsf@gitster.c.googlers.com>
In-Reply-To
<875z52wu2a.fsf@evledraar.gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
> But it's not a "no" until that point (and not even then, maybe we can
> keep the general idea of Y, but have Z which is mostly the same & works
> for most people), it's just "nobody's really worked on it yet".
Thanks for spelling these out.

Here is my snack-time hack to add the --pretend-gitdir option to "git config". It _might_ not be a bad idea to trigger this behaviour (using $(pwd)/.git as the pretended directory path) automatically when do not have a repository, but that would certainly be a compatibility breaking change and would break existing workflow.

Only very lightly tested, and certainly not ready for inclusion (it does not even have a doc update).

 builtin/config.c          |  4 ++++
 config.c                  |  2 ++
 config.h                  |  1 +
 t/t1305-config-include.sh | 21 +++++++++++++++++++++
 4 files changed, 28 insertions(+)
diff --git c/builtin/config.c w/builtin/config.c
index f71fa39b38..2603dc448c 100644
--- c/builtin/config.c
+++ w/builtin/config.c
@@ -30,6 +30,7 @@ static int use_worktree_config;
 static struct git_config_source given_config_source;
 static int actions, type;
 static char *default_value;
+static char *pretend_gitdir;
 static int end_nul;
 static int respect_includes_opt = -1;
 static struct config_options config_options;
@@ -165,6 +166,7 @@ static struct option builtin_config_options[] = {
 	OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
 	OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
 	OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
+	OPT_FILENAME(0, "pretend-gitdir", &pretend_gitdir, N_("when outside a repository, pretend this is the gitdir")),
 	OPT_END(),
 };
 
@@ -732,6 +734,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		config_options.commondir = get_git_common_dir();
 		config_options.git_dir = get_git_dir();
 	}
+	if (pretend_gitdir)
+		config_options.pretend_gitdir = pretend_gitdir;
 
 	if (end_nul) {
 		term = '\0';
diff --git c/config.c w/config.c
index 1137bd73af..d8d406ecda 100644
--- c/config.c
+++ w/config.c
@@ -224,6 +224,8 @@ static int include_by_gitdir(const struct config_options *opts,
 
 	if (opts->git_dir)
 		git_dir = opts->git_dir;
+	else if (opts->pretend_gitdir)
+		git_dir = opts->pretend_gitdir;
 	else
 		goto done;
 
diff --git c/config.h w/config.h
index c1449bb790..14e8d8c576 100644
--- c/config.h
+++ w/config.h
@@ -89,6 +89,7 @@ struct config_options {
 	unsigned int system_gently : 1;
 	const char *commondir;
 	const char *git_dir;
+	const char *pretend_gitdir;
 	config_parser_event_fn_t event_fn;
 	void *event_fn_data;
 	enum config_error_action {
diff --git c/t/t1305-config-include.sh w/t/t1305-config-include.sh
index f1e1b289f9..883762ad36 100755
--- c/t/t1305-config-include.sh
+++ w/t/t1305-config-include.sh
@@ -162,6 +162,27 @@ test_expect_success 'relative includes from stdin line fail' '
 	test_must_fail git config --file - test.one
 '
 
+test_expect_success 'conditional include, pretend gitdir' '
+	test_when_finished "git config --global --unset-all \"includeif.gitdir:*.path\"" &&
+	git config --global "includeif.gitdir:*.path" included &&
+
+	git config --file included "custom.variable" value &&
+	echo value >expect &&
+
+	# in the TRASH repository
+	git config --get custom.variable >actual &&
+	test_cmp expect actual &&
+
+	# nongit without pretend should not find stuff from included
+	nongit test_must_fail git config --get custom.variable >actual &&
+	test_must_be_empty actual &&
+
+	# nongit with pretend should find stuff from included
+	nongit git config --pretend-gitdir "$(pwd)/.git" \
+		--get custom.variable >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'conditional include, both unanchored' '
 	git init foo &&
 	(
Previous: Ævar Arnfjörð Bjarmason
Message 8 of 8 in “[Bug report] includeIf config is not displayed in normal directories”
  1. Stuart MacDonaldDec 15, 2020
  2. Junio C HamanoDec 15, 2020
  3. Jeff KingDec 16, 2020
  4. Stuart MacDonaldDec 16, 2020
  5. Junio C HamanoDec 16, 2020
  6. Jeff KingDec 18, 2020
  7. Ævar Arnfjörð BjarmasonDec 15, 2020
  8. Junio C HamanoDec 16, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.