Skip to content

Commit f141ee7

Browse files
delilahwgitster
authored andcommitted
config: keep bailing on unreadable global files
The expected behaviour for `git config list` is: A. Without `--global`, it should not bail on unreadable/non-existent global config files. B. With `--global`, it should bail when both `$HOME/.gitconfig` and `$XDG_CONFIG_HOME/git/config` are unreadable. It should not bail when one or more of them is readable. The previous patch, config: read global scope via config_sequence, introduced a regression in scenario B. When both global config files are unreadable, running `git config list --global` would not fail. For example, `GIT_CONFIG_GLOBAL=does-not-exist git config list --global` exits with status code 0. Assuming that `config_source->scope == CONFIG_SCOPE_GLOBAL` iff the `--global` argument is specified, use this to determine whether to bail. When reading only the global scope and both config files are unreadable, then adjust the return code to be non-zero. Note: When bailing, the exit code is not determined by sum of the return codes of the underlying operations. Instead, the exit code is modified via a single decrement. If this is undesirable, we can change it to sum the return codes of the underlying operations instead. Lastly, modify the tests to remove the known breakage/regression. The tests for scenario B will now pass. Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Delilah Ashley Wu <[email protected]> Reviewed-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb3a952 commit f141ee7

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

config.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,8 @@ int git_config_system(void)
15121512
}
15131513

15141514
static int do_git_config_sequence(const struct config_options *opts,
1515-
const struct repository *repo,
1516-
config_fn_t fn, void *data)
1515+
const struct repository *repo, config_fn_t fn,
1516+
void *data, enum config_scope scope)
15171517
{
15181518
int ret = 0;
15191519
char *system_config = git_system_config();
@@ -1546,15 +1546,34 @@ static int do_git_config_sequence(const struct config_options *opts,
15461546
NULL);
15471547

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

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);
1554+
if (xdg_config &&
1555+
!access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
1556+
ret += git_config_from_file_with_options(fn, xdg_config,
1557+
data,
1558+
CONFIG_SCOPE_GLOBAL,
1559+
NULL);
1560+
if (!ret)
1561+
global_config_success_count++;
1562+
}
1563+
1564+
if (user_config &&
1565+
!access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
1566+
ret += git_config_from_file_with_options(fn, user_config,
1567+
data,
1568+
CONFIG_SCOPE_GLOBAL,
1569+
NULL);
1570+
if (!ret)
1571+
global_config_success_count++;
1572+
}
15541573

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);
1574+
if (nonzero_ret_on_global_config_error &&
1575+
!global_config_success_count)
1576+
--ret;
15581577

15591578
free(xdg_config);
15601579
free(user_config);
@@ -1615,7 +1634,10 @@ int config_with_options(config_fn_t fn, void *data,
16151634
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
16161635
data, config_source->scope);
16171636
} else {
1618-
ret = do_git_config_sequence(opts, repo, fn, data);
1637+
ret = do_git_config_sequence(opts, repo, fn, data,
1638+
config_source ?
1639+
config_source->scope :
1640+
CONFIG_SCOPE_UNKNOWN);
16191641
}
16201642

16211643
if (inc.remote_urls) {

t/t1300-config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,7 +2367,7 @@ test_expect_success 'list with nonexistent global config' '
23672367
git config ${mode_prefix}list --show-scope
23682368
'
23692369

2370-
test_expect_failure 'list --global with nonexistent global config' '
2370+
test_expect_success 'list --global with nonexistent global config' '
23712371
rm -rf "$HOME"/.gitconfig "$HOME"/.config/git/config &&
23722372
test_must_fail git config ${mode_prefix}list --global --show-scope
23732373
'
@@ -2478,7 +2478,7 @@ test_expect_success 'override global and system config' '
24782478
test_cmp expect output
24792479
'
24802480

2481-
test_expect_failure 'override global and system config with missing file' '
2481+
test_expect_success 'override global and system config with missing file' '
24822482
test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config ${mode_prefix}list --global &&
24832483
test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config ${mode_prefix}list --system &&
24842484
GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version

0 commit comments

Comments
 (0)