Skip to content

Commit e706aaf

Browse files
committed
Merge branch 'ps/config-global-override'
Replace GIT_CONFIG_NOSYSTEM mechanism to decline from reading the system-wide configuration file with GIT_CONFIG_SYSTEM that lets users specify from which file to read the system-wide configuration (setting it to an empty file would essentially be the same as setting NOSYSTEM), and introduce GIT_CONFIG_GLOBAL to override the per-user configuration in $HOME/.gitconfig. * ps/config-global-override: t1300: fix unset of GIT_CONFIG_NOSYSTEM leaking into subsequent tests config: allow overriding of global and system configuration config: unify code paths to get global config paths config: rename `git_etc_config()`
2 parents f16a466 + 482d549 commit e706aaf

File tree

6 files changed

+135
-16
lines changed

6 files changed

+135
-16
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

builtin/config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
671671
}
672672

673673
if (use_global_config) {
674-
char *user_config = expand_user_path("~/.gitconfig", 0);
675-
char *xdg_config = xdg_config_home("config");
674+
char *user_config, *xdg_config;
676675

676+
git_global_config(&user_config, &xdg_config);
677677
if (!user_config)
678678
/*
679679
* It is unknown if HOME/.gitconfig exists, so
@@ -695,7 +695,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
695695
}
696696
}
697697
else if (use_system_config) {
698-
given_config_source.file = git_etc_gitconfig();
698+
given_config_source.file = git_system_config();
699699
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
700700
} else if (use_local_config) {
701701
given_config_source.file = git_pathdup("config");

config.c

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,12 +1830,26 @@ static int git_config_from_blob_ref(config_fn_t fn,
18301830
return git_config_from_blob_oid(fn, name, &oid, data);
18311831
}
18321832

1833-
const char *git_etc_gitconfig(void)
1833+
char *git_system_config(void)
18341834
{
1835-
static const char *system_wide;
1836-
if (!system_wide)
1837-
system_wide = system_path(ETC_GITCONFIG);
1838-
return system_wide;
1835+
char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
1836+
if (system_config)
1837+
return system_config;
1838+
return system_path(ETC_GITCONFIG);
1839+
}
1840+
1841+
void git_global_config(char **user_out, char **xdg_out)
1842+
{
1843+
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
1844+
char *xdg_config = NULL;
1845+
1846+
if (!user_config) {
1847+
user_config = expand_user_path("~/.gitconfig", 0);
1848+
xdg_config = xdg_config_home("config");
1849+
}
1850+
1851+
*user_out = user_config;
1852+
*xdg_out = xdg_config;
18391853
}
18401854

18411855
/*
@@ -1869,8 +1883,9 @@ static int do_git_config_sequence(const struct config_options *opts,
18691883
config_fn_t fn, void *data)
18701884
{
18711885
int ret = 0;
1872-
char *xdg_config = xdg_config_home("config");
1873-
char *user_config = expand_user_path("~/.gitconfig", 0);
1886+
char *system_config = git_system_config();
1887+
char *xdg_config = NULL;
1888+
char *user_config = NULL;
18741889
char *repo_config;
18751890
enum config_scope prev_parsing_scope = current_parsing_scope;
18761891

@@ -1882,13 +1897,14 @@ static int do_git_config_sequence(const struct config_options *opts,
18821897
repo_config = NULL;
18831898

18841899
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
1885-
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK,
1886-
opts->system_gently ?
1887-
ACCESS_EACCES_OK : 0))
1888-
ret += git_config_from_file(fn, git_etc_gitconfig(),
1889-
data);
1900+
if (git_config_system() && system_config &&
1901+
!access_or_die(system_config, R_OK,
1902+
opts->system_gently ? ACCESS_EACCES_OK : 0))
1903+
ret += git_config_from_file(fn, system_config, data);
18901904

18911905
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
1906+
git_global_config(&user_config, &xdg_config);
1907+
18921908
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
18931909
ret += git_config_from_file(fn, xdg_config, data);
18941910

@@ -1913,6 +1929,7 @@ static int do_git_config_sequence(const struct config_options *opts,
19131929
die(_("unable to parse command-line config"));
19141930

19151931
current_parsing_scope = prev_parsing_scope;
1932+
free(system_config);
19161933
free(xdg_config);
19171934
free(user_config);
19181935
free(repo_config);

config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ int git_config_rename_section(const char *, const char *);
318318
int git_config_rename_section_in_file(const char *, const char *, const char *);
319319
int git_config_copy_section(const char *, const char *);
320320
int git_config_copy_section_in_file(const char *, const char *, const char *);
321-
const char *git_etc_gitconfig(void);
322321
int git_env_bool(const char *, int);
323322
unsigned long git_env_ulong(const char *, unsigned long);
324323
int git_config_system(void);
@@ -327,6 +326,9 @@ int config_error_nonbool(const char *);
327326
#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
328327
#endif
329328

329+
char *git_system_config(void);
330+
void git_global_config(char **user, char **xdg);
331+
330332
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
331333

332334
enum config_scope current_config_scope(void);

t/t1300-config.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,91 @@ test_expect_success '--show-scope with --show-origin' '
20722072
test_cmp expect output
20732073
'
20742074

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

0 commit comments

Comments
 (0)