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@xxxxxxxxx> --- 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