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

[PATCH] config: use GIT_CONFIG in git config sequence

From
MNMateusz Nowotyński <maxmati4@gmail.com>
Date
Apr 25, 2020, 23:57 UTC
Message-ID
<20200425235716.1822560-1-maxmati4@gmail.com>

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
Next: Junio C Hamano
Message 1 of 14 in “config: use GIT_CONFIG in git config sequence”
  1. config: use GIT_CONFIG in git config sequenceMateusz Nowotyński, Apr 25, 2020
  2. Junio C HamanoApr 26, 2020
  3. Philip OakleyApr 26, 2020
  4. Mateusz NowotyńskiApr 26, 2020
  5. brian m. carlsonApr 26, 2020
  6. Mateusz NowotynskiApr 27, 2020
  7. Junio C HamanoApr 26, 2020
  8. Matt RogersApr 26, 2020
  9. Philip OakleyApr 26, 2020
  10. Matt RogersApr 26, 2020
  11. Philip OakleyApr 26, 2020
  12. Junio C HamanoApr 26, 2020
  13. Junio C HamanoApr 26, 2020
  14. Mateusz NowotynskiApr 27, 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.