Skip to content

Commit 052a346

Browse files
committed
Merge branch 'dw/config-global-list' into seen
"git config --list --global", unlike "git config --list", did not consult both of the two possible per-user sources of the configuration files, i.e. $HOME/.gitconfig and the XDG one, which has been corrected. Comments? * dw/config-global-list: config: keep bailing on unreadable global files config: read global scope via config_sequence config: test home and xdg files in `list --global` cleanup_path: force forward slashes on Windows
2 parents a4e81c4 + f141ee7 commit 052a346

File tree

6 files changed

+128
-18
lines changed

6 files changed

+128
-18
lines changed

builtin/config.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,18 @@ static void location_options_init(struct config_location_options *opts,
803803
}
804804

805805
if (opts->use_global_config) {
806+
/*
807+
* Since global config is sourced from more than one location,
808+
* use `config.c#do_git_config_sequence()` with `opts->options`
809+
* to read it. However, writing global config should point to a
810+
* single destination, set in `opts->source.file`.
811+
*/
812+
opts->options.ignore_repo = 1;
813+
opts->options.ignore_cmdline= 1;
814+
opts->options.ignore_worktree = 1;
815+
opts->options.ignore_system = 1;
816+
opts->source.scope = CONFIG_SCOPE_GLOBAL;
817+
806818
opts->source.file = opts->file_to_free = git_global_config();
807819
if (!opts->source.file)
808820
/*

config.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,8 +1513,8 @@ int git_config_system(void)
15131513
}
15141514

15151515
static int do_git_config_sequence(const struct config_options *opts,
1516-
const struct repository *repo,
1517-
config_fn_t fn, void *data)
1516+
const struct repository *repo, config_fn_t fn,
1517+
void *data, enum config_scope scope)
15181518
{
15191519
int ret = 0;
15201520
char *system_config = git_system_config();
@@ -1539,22 +1539,46 @@ static int do_git_config_sequence(const struct config_options *opts,
15391539
worktree_config = NULL;
15401540
}
15411541

1542-
if (git_config_system() && system_config &&
1542+
if (!opts->ignore_system && git_config_system() && system_config &&
15431543
!access_or_die(system_config, R_OK,
15441544
opts->system_gently ? ACCESS_EACCES_OK : 0))
15451545
ret += git_config_from_file_with_options(fn, system_config,
15461546
data, CONFIG_SCOPE_SYSTEM,
15471547
NULL);
15481548

1549-
git_global_config_paths(&user_config, &xdg_config);
1549+
if (!opts->ignore_global) {
1550+
int global_config_success_count = 0;
1551+
int nonzero_ret_on_global_config_error = scope == CONFIG_SCOPE_GLOBAL;
1552+
1553+
git_global_config_paths(&user_config, &xdg_config);
15501554

1551-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
1552-
ret += git_config_from_file_with_options(fn, xdg_config, data,
1553-
CONFIG_SCOPE_GLOBAL, NULL);
1555+
if (xdg_config &&
1556+
!access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1557+
ret += git_config_from_file_with_options(fn, xdg_config,
1558+
data,
1559+
CONFIG_SCOPE_GLOBAL,
1560+
NULL);
1561+
if (!ret)
1562+
global_config_success_count++;
1563+
}
15541564

1555-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
1556-
ret += git_config_from_file_with_options(fn, user_config, data,
1557-
CONFIG_SCOPE_GLOBAL, NULL);
1565+
if (user_config &&
1566+
!access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1567+
ret += git_config_from_file_with_options(fn, user_config,
1568+
data,
1569+
CONFIG_SCOPE_GLOBAL,
1570+
NULL);
1571+
if (!ret)
1572+
global_config_success_count++;
1573+
}
1574+
1575+
if (nonzero_ret_on_global_config_error &&
1576+
!global_config_success_count)
1577+
--ret;
1578+
1579+
free(xdg_config);
1580+
free(user_config);
1581+
}
15581582

15591583
if (!opts->ignore_repo && repo_config &&
15601584
!access_or_die(repo_config, R_OK, 0))
@@ -1573,8 +1597,6 @@ static int do_git_config_sequence(const struct config_options *opts,
15731597
die(_("unable to parse command-line config"));
15741598

15751599
free(system_config);
1576-
free(xdg_config);
1577-
free(user_config);
15781600
free(repo_config);
15791601
free(worktree_config);
15801602
return ret;
@@ -1604,15 +1626,19 @@ int config_with_options(config_fn_t fn, void *data,
16041626
*/
16051627
if (config_source && config_source->use_stdin) {
16061628
ret = git_config_from_stdin(fn, data, config_source->scope);
1607-
} else if (config_source && config_source->file) {
1629+
} else if (config_source && config_source->file &&
1630+
config_source->scope != CONFIG_SCOPE_GLOBAL) {
16081631
ret = git_config_from_file_with_options(fn, config_source->file,
16091632
data, config_source->scope,
16101633
NULL);
16111634
} else if (config_source && config_source->blob) {
16121635
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
16131636
data, config_source->scope);
16141637
} else {
1615-
ret = do_git_config_sequence(opts, repo, fn, data);
1638+
ret = do_git_config_sequence(opts, repo, fn, data,
1639+
config_source ?
1640+
config_source->scope :
1641+
CONFIG_SCOPE_UNKNOWN);
16161642
}
16171643

16181644
if (inc.remote_urls) {

config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ typedef int (*config_parser_event_fn_t)(enum config_event_t type,
8787

8888
struct config_options {
8989
unsigned int respect_includes : 1;
90+
unsigned int ignore_system : 1;
91+
unsigned int ignore_global : 1;
9092
unsigned int ignore_repo : 1;
9193
unsigned int ignore_worktree : 1;
9294
unsigned int ignore_cmdline : 1;

path.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ static struct strbuf *get_pathname(void)
4040
return sb;
4141
}
4242

43-
static const char *cleanup_path(const char *path)
43+
static char *cleanup_path(char *path)
4444
{
4545
/* Clean it up */
46-
if (skip_prefix(path, "./", &path)) {
46+
if (skip_prefix(path, "./", (const char **)&path))
4747
while (*path == '/')
4848
path++;
49-
}
49+
50+
#ifdef GIT_WINDOWS_NATIVE
51+
convert_slashes(path);
52+
#endif
53+
5054
return path;
5155
}
5256

t/t1300-config.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,71 @@ test_expect_success '--show-scope with --default' '
23622362
test_cmp expect actual
23632363
'
23642364

2365+
test_expect_success 'list with nonexistent global config' '
2366+
rm -rf "$HOME"/.gitconfig "$HOME"/.config/git/config &&
2367+
git config ${mode_prefix}list --show-scope
2368+
'
2369+
2370+
test_expect_success 'list --global with nonexistent global config' '
2371+
rm -rf "$HOME"/.gitconfig "$HOME"/.config/git/config &&
2372+
test_must_fail git config ${mode_prefix}list --global --show-scope
2373+
'
2374+
2375+
test_expect_success 'list --global with only home' '
2376+
rm -rf "$HOME"/.config/git/config &&
2377+
2378+
test_when_finished rm -f \"\$HOME\"/.gitconfig &&
2379+
cat >"$HOME"/.gitconfig <<-EOF &&
2380+
[home]
2381+
config = true
2382+
EOF
2383+
2384+
cat >expect <<-EOF &&
2385+
global home.config=true
2386+
EOF
2387+
git config ${mode_prefix}list --global --show-scope >output &&
2388+
test_cmp expect output
2389+
'
2390+
2391+
test_expect_success 'list --global with only xdg' '
2392+
rm -f "$HOME"/.gitconfig &&
2393+
2394+
test_when_finished rm -rf \"\$HOME\"/.config/git &&
2395+
mkdir -p "$HOME"/.config/git &&
2396+
cat >"$HOME"/.config/git/config <<-EOF &&
2397+
[xdg]
2398+
config = true
2399+
EOF
2400+
2401+
cat >expect <<-EOF &&
2402+
global xdg.config=true
2403+
EOF
2404+
git config ${mode_prefix}list --global --show-scope >output &&
2405+
test_cmp expect output
2406+
'
2407+
2408+
test_expect_success 'list --global with both home and xdg' '
2409+
test_when_finished rm -f \"\$HOME\"/.gitconfig &&
2410+
cat >"$HOME"/.gitconfig <<-EOF &&
2411+
[home]
2412+
config = true
2413+
EOF
2414+
2415+
test_when_finished rm -rf \"\$HOME\"/.config/git &&
2416+
mkdir -p "$HOME"/.config/git &&
2417+
cat >"$HOME"/.config/git/config <<-EOF &&
2418+
[xdg]
2419+
config = true
2420+
EOF
2421+
2422+
cat >expect <<-EOF &&
2423+
global file:$HOME/.config/git/config xdg.config=true
2424+
global file:$HOME/.gitconfig home.config=true
2425+
EOF
2426+
git config ${mode_prefix}list --global --show-scope --show-origin >output &&
2427+
test_cmp expect output
2428+
'
2429+
23652430
test_expect_success 'override global and system config' '
23662431
test_when_finished rm -f \"\$HOME\"/.gitconfig &&
23672432
cat >"$HOME"/.gitconfig <<-EOF &&

t/t1306-xdg-files.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ test_expect_success 'read with --list: xdg file exists and ~/.gitconfig exists'
6868
>.gitconfig &&
6969
echo "[user]" >.gitconfig &&
7070
echo " name = read_gitconfig" >>.gitconfig &&
71-
echo user.name=read_gitconfig >expected &&
71+
echo user.name=read_config >expected &&
72+
echo user.name=read_gitconfig >>expected &&
7273
git config --global --list >actual &&
7374
test_cmp expected actual
7475
'

0 commit comments

Comments
 (0)