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()`
This commit is contained in:
commit
e706aaf3bc
@ -340,6 +340,11 @@ GIT_CONFIG::
|
||||
Using the "--global" option forces this to ~/.gitconfig. Using the
|
||||
"--system" option forces this to $(prefix)/etc/gitconfig.
|
||||
|
||||
GIT_CONFIG_GLOBAL::
|
||||
GIT_CONFIG_SYSTEM::
|
||||
Take the configuration from the given files instead from global or
|
||||
system-level configuration. See linkgit:git[1] for details.
|
||||
|
||||
GIT_CONFIG_NOSYSTEM::
|
||||
Whether to skip reading settings from the system-wide
|
||||
$(prefix)/etc/gitconfig file. See linkgit:git[1] for details.
|
||||
|
@ -670,6 +670,16 @@ for further details.
|
||||
If this environment variable is set to `0`, git will not prompt
|
||||
on the terminal (e.g., when asking for HTTP authentication).
|
||||
|
||||
`GIT_CONFIG_GLOBAL`::
|
||||
`GIT_CONFIG_SYSTEM`::
|
||||
Take the configuration from the given files instead from global or
|
||||
system-level configuration files. If `GIT_CONFIG_SYSTEM` is set, the
|
||||
system config file defined at build time (usually `/etc/gitconfig`)
|
||||
will not be read. Likewise, if `GIT_CONFIG_GLOBAL` is set, neither
|
||||
`$HOME/.gitconfig` nor `$XDG_CONFIG_HOME/git/config` will be read. Can
|
||||
be set to `/dev/null` to skip reading configuration files of the
|
||||
respective level.
|
||||
|
||||
`GIT_CONFIG_NOSYSTEM`::
|
||||
Whether to skip reading settings from the system-wide
|
||||
`$(prefix)/etc/gitconfig` file. This environment variable can
|
||||
|
@ -671,9 +671,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
|
||||
}
|
||||
|
||||
if (use_global_config) {
|
||||
char *user_config = expand_user_path("~/.gitconfig", 0);
|
||||
char *xdg_config = xdg_config_home("config");
|
||||
char *user_config, *xdg_config;
|
||||
|
||||
git_global_config(&user_config, &xdg_config);
|
||||
if (!user_config)
|
||||
/*
|
||||
* It is unknown if HOME/.gitconfig exists, so
|
||||
@ -695,7 +695,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
|
||||
}
|
||||
}
|
||||
else if (use_system_config) {
|
||||
given_config_source.file = git_etc_gitconfig();
|
||||
given_config_source.file = git_system_config();
|
||||
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
|
||||
} else if (use_local_config) {
|
||||
given_config_source.file = git_pathdup("config");
|
||||
|
41
config.c
41
config.c
@ -1830,12 +1830,26 @@ static int git_config_from_blob_ref(config_fn_t fn,
|
||||
return git_config_from_blob_oid(fn, name, &oid, data);
|
||||
}
|
||||
|
||||
const char *git_etc_gitconfig(void)
|
||||
char *git_system_config(void)
|
||||
{
|
||||
static const char *system_wide;
|
||||
if (!system_wide)
|
||||
system_wide = system_path(ETC_GITCONFIG);
|
||||
return system_wide;
|
||||
char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
|
||||
if (system_config)
|
||||
return system_config;
|
||||
return system_path(ETC_GITCONFIG);
|
||||
}
|
||||
|
||||
void git_global_config(char **user_out, char **xdg_out)
|
||||
{
|
||||
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
|
||||
char *xdg_config = NULL;
|
||||
|
||||
if (!user_config) {
|
||||
user_config = expand_user_path("~/.gitconfig", 0);
|
||||
xdg_config = xdg_config_home("config");
|
||||
}
|
||||
|
||||
*user_out = user_config;
|
||||
*xdg_out = xdg_config;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -1869,8 +1883,9 @@ static int do_git_config_sequence(const struct config_options *opts,
|
||||
config_fn_t fn, void *data)
|
||||
{
|
||||
int ret = 0;
|
||||
char *xdg_config = xdg_config_home("config");
|
||||
char *user_config = expand_user_path("~/.gitconfig", 0);
|
||||
char *system_config = git_system_config();
|
||||
char *xdg_config = NULL;
|
||||
char *user_config = NULL;
|
||||
char *repo_config;
|
||||
enum config_scope prev_parsing_scope = current_parsing_scope;
|
||||
|
||||
@ -1882,13 +1897,14 @@ static int do_git_config_sequence(const struct config_options *opts,
|
||||
repo_config = NULL;
|
||||
|
||||
current_parsing_scope = CONFIG_SCOPE_SYSTEM;
|
||||
if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK,
|
||||
opts->system_gently ?
|
||||
ACCESS_EACCES_OK : 0))
|
||||
ret += git_config_from_file(fn, git_etc_gitconfig(),
|
||||
data);
|
||||
if (git_config_system() && system_config &&
|
||||
!access_or_die(system_config, R_OK,
|
||||
opts->system_gently ? ACCESS_EACCES_OK : 0))
|
||||
ret += git_config_from_file(fn, system_config, data);
|
||||
|
||||
current_parsing_scope = CONFIG_SCOPE_GLOBAL;
|
||||
git_global_config(&user_config, &xdg_config);
|
||||
|
||||
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
|
||||
ret += git_config_from_file(fn, xdg_config, data);
|
||||
|
||||
@ -1913,6 +1929,7 @@ static int do_git_config_sequence(const struct config_options *opts,
|
||||
die(_("unable to parse command-line config"));
|
||||
|
||||
current_parsing_scope = prev_parsing_scope;
|
||||
free(system_config);
|
||||
free(xdg_config);
|
||||
free(user_config);
|
||||
free(repo_config);
|
||||
|
4
config.h
4
config.h
@ -318,7 +318,6 @@ int git_config_rename_section(const char *, const char *);
|
||||
int git_config_rename_section_in_file(const char *, const char *, const char *);
|
||||
int git_config_copy_section(const char *, const char *);
|
||||
int git_config_copy_section_in_file(const char *, const char *, const char *);
|
||||
const char *git_etc_gitconfig(void);
|
||||
int git_env_bool(const char *, int);
|
||||
unsigned long git_env_ulong(const char *, unsigned long);
|
||||
int git_config_system(void);
|
||||
@ -327,6 +326,9 @@ int config_error_nonbool(const char *);
|
||||
#define config_error_nonbool(s) (config_error_nonbool(s), const_error())
|
||||
#endif
|
||||
|
||||
char *git_system_config(void);
|
||||
void git_global_config(char **user, char **xdg);
|
||||
|
||||
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
|
||||
|
||||
enum config_scope current_config_scope(void);
|
||||
|
@ -2072,6 +2072,91 @@ test_expect_success '--show-scope with --show-origin' '
|
||||
test_cmp expect output
|
||||
'
|
||||
|
||||
test_expect_success 'override global and system config' '
|
||||
test_when_finished rm -f "$HOME"/.config/git &&
|
||||
|
||||
cat >"$HOME"/.gitconfig <<-EOF &&
|
||||
[home]
|
||||
config = true
|
||||
EOF
|
||||
mkdir -p "$HOME"/.config/git &&
|
||||
cat >"$HOME"/.config/git/config <<-EOF &&
|
||||
[xdg]
|
||||
config = true
|
||||
EOF
|
||||
cat >.git/config <<-EOF &&
|
||||
[local]
|
||||
config = true
|
||||
EOF
|
||||
cat >custom-global-config <<-EOF &&
|
||||
[global]
|
||||
config = true
|
||||
EOF
|
||||
cat >custom-system-config <<-EOF &&
|
||||
[system]
|
||||
config = true
|
||||
EOF
|
||||
|
||||
cat >expect <<-EOF &&
|
||||
global xdg.config=true
|
||||
global home.config=true
|
||||
local local.config=true
|
||||
EOF
|
||||
git config --show-scope --list >output &&
|
||||
test_cmp expect output &&
|
||||
|
||||
cat >expect <<-EOF &&
|
||||
system system.config=true
|
||||
global global.config=true
|
||||
local local.config=true
|
||||
EOF
|
||||
GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=custom-system-config GIT_CONFIG_GLOBAL=custom-global-config \
|
||||
git config --show-scope --list >output &&
|
||||
test_cmp expect output &&
|
||||
|
||||
cat >expect <<-EOF &&
|
||||
local local.config=true
|
||||
EOF
|
||||
GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null \
|
||||
git config --show-scope --list >output &&
|
||||
test_cmp expect output
|
||||
'
|
||||
|
||||
test_expect_success 'override global and system config with missing file' '
|
||||
test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config --global --list &&
|
||||
test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config --system --list &&
|
||||
GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version
|
||||
'
|
||||
|
||||
test_expect_success 'system override has no effect with GIT_CONFIG_NOSYSTEM' '
|
||||
# `git config --system` has different semantics compared to other
|
||||
# commands as it ignores GIT_CONFIG_NOSYSTEM. We thus test whether the
|
||||
# variable has an effect via a different proxy.
|
||||
cat >alias-config <<-EOF &&
|
||||
[alias]
|
||||
hello-world = !echo "hello world"
|
||||
EOF
|
||||
test_must_fail env GIT_CONFIG_NOSYSTEM=true GIT_CONFIG_SYSTEM=alias-config \
|
||||
git hello-world &&
|
||||
GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=alias-config \
|
||||
git hello-world >actual &&
|
||||
echo "hello world" >expect &&
|
||||
test_cmp expect actual
|
||||
'
|
||||
|
||||
test_expect_success 'write to overridden global and system config' '
|
||||
cat >expect <<EOF &&
|
||||
[config]
|
||||
key = value
|
||||
EOF
|
||||
|
||||
GIT_CONFIG_GLOBAL=write-to-global git config --global config.key value &&
|
||||
test_cmp expect write-to-global &&
|
||||
|
||||
GIT_CONFIG_SYSTEM=write-to-system git config --system config.key value &&
|
||||
test_cmp expect write-to-system
|
||||
'
|
||||
|
||||
for opt in --local --worktree
|
||||
do
|
||||
test_expect_success "$opt requires a repo" '
|
||||
|
Loading…
Reference in New Issue
Block a user