git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] config: use GIT_CONFIG in git config sequence
@ 2020-04-25 23:57 Mateusz Nowotyński
  2020-04-26  0:16 ` Junio C Hamano
  2020-04-26  1:12 ` Matt Rogers
  0 siblings, 2 replies; 14+ messages in thread
From: Mateusz Nowotyński @ 2020-04-25 23:57 UTC (permalink / raw)
  To: git; +Cc: gitster, mattr94, bwilliams.eng, Mateusz Nowotyński

Currently, there is no way to use config file other then ~/.gitconfig.
This can cause a problem, for example, when running tests of software that
depends on git. In such a case user's gitconfig may contain settings that
are incompatible with tests.

This commit expands existing GIT_CONFIG environment variable, that is
currently used only by git config, that it is used also in other commands.
When it is set it's the only config file that's being used.

Signed-off-by: Mateusz Nowotyński <maxmati4@gmail.com>
---
 Documentation/git.txt |  4 ++++
 config.c              |  3 +++
 t/t0007-git-var.sh    | 20 ++++++++++++++++++++
 3 files changed, 27 insertions(+)

diff --git a/Documentation/git.txt b/Documentation/git.txt
index 9d6769e95a..c92411b8d9 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -882,6 +882,10 @@ standard output.
 	adequate and support for it is likely to be removed in the
 	foreseeable future (along with the variable).
 
+GIT_CONFIG::
+	Take the configuration from the given file instead of using typical
+	resolve mechanism.
+
 Discussion[[Discussion]]
 ------------------------
 
diff --git a/config.c b/config.c
index d17d2bd9dc..811b4e2186 100644
--- a/config.c
+++ b/config.c
@@ -1754,6 +1754,7 @@ int config_with_options(config_fn_t fn, void *data,
 			const struct config_options *opts)
 {
 	struct config_include_data inc = CONFIG_INCLUDE_INIT;
+	const char* env_config = getenv(CONFIG_ENVIRONMENT);
 
 	if (opts->respect_includes) {
 		inc.fn = fn;
@@ -1776,6 +1777,8 @@ int config_with_options(config_fn_t fn, void *data,
 		return git_config_from_file(fn, config_source->file, data);
 	else if (config_source && config_source->blob)
 		return git_config_from_blob_ref(fn, config_source->blob, data);
+	else if (env_config)
+		return git_config_from_file(fn, env_config, data);
 
 	return do_git_config_sequence(opts, fn, data);
 }
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
index 1f600e2cae..74ed80f5ca 100755
--- a/t/t0007-git-var.sh
+++ b/t/t0007-git-var.sh
@@ -46,4 +46,24 @@ test_expect_success 'listing and asking for variables are exclusive' '
 	test_must_fail git var -l GIT_COMMITTER_IDENT
 '
 
+
+cat > config_file << EOF
+[ein]
+	bahn = strasse
+EOF
+
+test_expect_success 'respect GIT_CONFIG' '
+	GIT_CONFIG=$(pwd)/config_file git var -l >actual &&
+	echo strasse >expect &&
+	sed -n s/ein\\.bahn=//p <actual >actual.bahn &&
+	test_cmp expect actual.bahn
+'
+
+test_expect_success 'GIT_CONFIG leads to not existsing file' '
+	test_when_finished "sane_unset GIT_CONFIG" &&
+	GIT_CONFIG=$(pwd)/not_existsing_config_file &&
+	export GIT_CONFIG &&
+	test_must_fail git var -l
+'
+
 test_done
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2020-04-27 20:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-25 23:57 [PATCH] config: use GIT_CONFIG in git config sequence Mateusz Nowotyński
2020-04-26  0:16 ` Junio C Hamano
2020-04-26  9:58   ` Philip Oakley
2020-04-26 19:32   ` Mateusz Nowotyński
2020-04-26 20:08     ` brian m. carlson
2020-04-27 20:04       ` Mateusz Nowotynski
2020-04-26 21:01     ` Junio C Hamano
2020-04-26  1:12 ` Matt Rogers
2020-04-26 10:00   ` Philip Oakley
2020-04-26 13:30     ` Matt Rogers
2020-04-26 20:04       ` Philip Oakley
2020-04-26 21:16         ` Junio C Hamano
2020-04-26 22:32           ` Junio C Hamano
2020-04-27 19:39             ` Mateusz Nowotynski

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 NNTP newsgroup(s).