Skip to content

Commit 4179b48

Browse files
pks-tgitster
authored andcommitted
config: allow overriding of global and system configuration
In order to have git run in a fully controlled environment without any misconfiguration, it may be desirable for users or scripts to override global- and system-level configuration files. We already have a way of doing this, which is to unset both HOME and XDG_CONFIG_HOME environment variables and to set `GIT_CONFIG_NOGLOBAL=true`. This is quite kludgy, and unsetting the first two variables likely has an impact on other executables spawned by such a script. The obvious way to fix this would be to introduce `GIT_CONFIG_NOGLOBAL` as an equivalent to `GIT_CONFIG_NOSYSTEM`. But in the past, it has turned out that this design is inflexible: we cannot test system-level parsing of the git configuration in our test harness because there is no way to change its location, so all tests run with `GIT_CONFIG_NOSYSTEM` set. Instead of doing the same mistake with `GIT_CONFIG_NOGLOBAL`, introduce two new variables `GIT_CONFIG_GLOBAL` and `GIT_CONFIG_SYSTEM`: - If unset, git continues to use the usual locations. - If set to a specific path, we skip reading the normal configuration files and instead take the path. By setting the path to `/dev/null`, no configuration will be loaded for the respective level. This implements the usecase where we want to execute code in a sanitized environment without any potential misconfigurations via `/dev/null`, but is more flexible and allows for more usecases than simply adding `GIT_CONFIG_NOGLOBAL`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e06eb9 commit 4179b48

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

Documentation/git-config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ GIT_CONFIG::
340340
Using the "--global" option forces this to ~/.gitconfig. Using the
341341
"--system" option forces this to $(prefix)/etc/gitconfig.
342342

343+
GIT_CONFIG_GLOBAL::
344+
GIT_CONFIG_SYSTEM::
345+
Take the configuration from the given files instead from global or
346+
system-level configuration. See linkgit:git[1] for details.
347+
343348
GIT_CONFIG_NOSYSTEM::
344349
Whether to skip reading settings from the system-wide
345350
$(prefix)/etc/gitconfig file. See linkgit:git[1] for details.

Documentation/git.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,16 @@ for further details.
670670
If this environment variable is set to `0`, git will not prompt
671671
on the terminal (e.g., when asking for HTTP authentication).
672672

673+
`GIT_CONFIG_GLOBAL`::
674+
`GIT_CONFIG_SYSTEM`::
675+
Take the configuration from the given files instead from global or
676+
system-level configuration files. If `GIT_CONFIG_SYSTEM` is set, the
677+
system config file defined at build time (usually `/etc/gitconfig`)
678+
will not be read. Likewise, if `GIT_CONFIG_GLOBAL` is set, neither
679+
`$HOME/.gitconfig` nor `$XDG_CONFIG_HOME/git/config` will be read. Can
680+
be set to `/dev/null` to skip reading configuration files of the
681+
respective level.
682+
673683
`GIT_CONFIG_NOSYSTEM`::
674684
Whether to skip reading settings from the system-wide
675685
`$(prefix)/etc/gitconfig` file. This environment variable can

config.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,13 +1846,24 @@ static int git_config_from_blob_ref(config_fn_t fn,
18461846

18471847
char *git_system_config(void)
18481848
{
1849+
char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
1850+
if (system_config)
1851+
return system_config;
18491852
return system_path(ETC_GITCONFIG);
18501853
}
18511854

1852-
void git_global_config(char **user_config, char **xdg_config)
1855+
void git_global_config(char **user_out, char **xdg_out)
18531856
{
1854-
*user_config = expand_user_path("~/.gitconfig", 0);
1855-
*xdg_config = xdg_config_home("config");
1857+
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
1858+
char *xdg_config = NULL;
1859+
1860+
if (!user_config) {
1861+
user_config = expand_user_path("~/.gitconfig", 0);
1862+
xdg_config = xdg_config_home("config");
1863+
}
1864+
1865+
*user_out = user_config;
1866+
*xdg_out = xdg_config;
18561867
}
18571868

18581869
/*

t/t1300-config.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,92 @@ test_expect_success '--show-scope with --show-origin' '
20592059
test_cmp expect output
20602060
'
20612061

2062+
test_expect_success 'override global and system config' '
2063+
test_when_finished rm -f "$HOME"/.config/git &&
2064+
2065+
cat >"$HOME"/.gitconfig <<-EOF &&
2066+
[home]
2067+
config = true
2068+
EOF
2069+
mkdir -p "$HOME"/.config/git &&
2070+
cat >"$HOME"/.config/git/config <<-EOF &&
2071+
[xdg]
2072+
config = true
2073+
EOF
2074+
cat >.git/config <<-EOF &&
2075+
[local]
2076+
config = true
2077+
EOF
2078+
cat >custom-global-config <<-EOF &&
2079+
[global]
2080+
config = true
2081+
EOF
2082+
cat >custom-system-config <<-EOF &&
2083+
[system]
2084+
config = true
2085+
EOF
2086+
2087+
cat >expect <<-EOF &&
2088+
global xdg.config=true
2089+
global home.config=true
2090+
local local.config=true
2091+
EOF
2092+
git config --show-scope --list >output &&
2093+
test_cmp expect output &&
2094+
2095+
sane_unset GIT_CONFIG_NOSYSTEM &&
2096+
2097+
cat >expect <<-EOF &&
2098+
system system.config=true
2099+
global global.config=true
2100+
local local.config=true
2101+
EOF
2102+
GIT_CONFIG_SYSTEM=custom-system-config GIT_CONFIG_GLOBAL=custom-global-config \
2103+
git config --show-scope --list >output &&
2104+
test_cmp expect output &&
2105+
2106+
cat >expect <<-EOF &&
2107+
local local.config=true
2108+
EOF
2109+
GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null git config --show-scope --list >output &&
2110+
test_cmp expect output
2111+
'
2112+
2113+
test_expect_success 'override global and system config with missing file' '
2114+
test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config --global --list &&
2115+
test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config --system --list &&
2116+
GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version
2117+
'
2118+
2119+
test_expect_success 'system override has no effect with GIT_CONFIG_NOSYSTEM' '
2120+
# `git config --system` has different semantics compared to other
2121+
# commands as it ignores GIT_CONFIG_NOSYSTEM. We thus test whether the
2122+
# variable has an effect via a different proxy.
2123+
cat >alias-config <<-EOF &&
2124+
[alias]
2125+
hello-world = !echo "hello world"
2126+
EOF
2127+
test_must_fail env GIT_CONFIG_NOSYSTEM=true GIT_CONFIG_SYSTEM=alias-config \
2128+
git hello-world &&
2129+
GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=alias-config \
2130+
git hello-world >actual &&
2131+
echo "hello world" >expect &&
2132+
test_cmp expect actual
2133+
'
2134+
2135+
test_expect_success 'write to overridden global and system config' '
2136+
cat >expect <<EOF &&
2137+
[config]
2138+
key = value
2139+
EOF
2140+
2141+
GIT_CONFIG_GLOBAL=write-to-global git config --global config.key value &&
2142+
test_cmp expect write-to-global &&
2143+
2144+
GIT_CONFIG_SYSTEM=write-to-system git config --system config.key value &&
2145+
test_cmp expect write-to-system
2146+
'
2147+
20622148
for opt in --local --worktree
20632149
do
20642150
test_expect_success "$opt requires a repo" '

0 commit comments

Comments
 (0)