Skip to content

Commit a44ef7c

Browse files
committed
config: keep bailing on unreadable global files
The 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 introduced a regression in scenario B: running `git config list --global` would not fail when both global config files are unreadable. For example, `GIT_CONFIG_GLOBAL=does-not-exist git config list --global` would exit 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: The logic to determine the exit code does not actually sum the return codes of the underlying operations. Instead, it uses a single decrement operation. If this is undesirable, we can change it to sum the return codes of the underlying operations instead. Signed-off-by: Delilah Ashley Wu <[email protected]>
1 parent c97026c commit a44ef7c

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

config.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,11 +2014,13 @@ int git_config_system(void)
20142014
}
20152015

20162016
static int do_git_config_sequence(const struct config_options *opts,
2017-
const struct repository *repo,
2018-
config_fn_t fn, void *data)
2017+
const struct repository *repo, config_fn_t fn,
2018+
void *data, enum config_scope scope)
20192019
{
20202020
int ret = 0;
20212021
char *system_config = git_system_config();
2022+
int global_config_success_count = 0;
2023+
int nonzero_ret_on_global_config_error = scope == CONFIG_SCOPE_GLOBAL;
20222024
char *xdg_config = NULL;
20232025
char *user_config = NULL;
20242026
char *repo_config;
@@ -2050,13 +2052,29 @@ static int do_git_config_sequence(const struct config_options *opts,
20502052
if (!opts->ignore_global) {
20512053
git_global_config_paths(&user_config, &xdg_config);
20522054

2053-
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
2054-
ret += git_config_from_file_with_options(fn, xdg_config, data,
2055-
CONFIG_SCOPE_GLOBAL, NULL);
2055+
if (xdg_config &&
2056+
!access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
2057+
ret += git_config_from_file_with_options(fn, xdg_config,
2058+
data,
2059+
CONFIG_SCOPE_GLOBAL,
2060+
NULL);
2061+
if (nonzero_ret_on_global_config_error && !ret)
2062+
++global_config_success_count;
2063+
}
2064+
2065+
if (user_config &&
2066+
!access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
2067+
ret += git_config_from_file_with_options(fn, user_config,
2068+
data,
2069+
CONFIG_SCOPE_GLOBAL,
2070+
NULL);
2071+
if (nonzero_ret_on_global_config_error && !ret)
2072+
++global_config_success_count;
2073+
}
20562074

2057-
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
2058-
ret += git_config_from_file_with_options(fn, user_config, data,
2059-
CONFIG_SCOPE_GLOBAL, NULL);
2075+
if (nonzero_ret_on_global_config_error &&
2076+
!global_config_success_count)
2077+
--ret;
20602078

20612079
free(xdg_config);
20622080
free(user_config);
@@ -2117,7 +2135,10 @@ int config_with_options(config_fn_t fn, void *data,
21172135
ret = git_config_from_blob_ref(fn, repo, config_source->blob,
21182136
data, config_source->scope);
21192137
} else {
2120-
ret = do_git_config_sequence(opts, repo, fn, data);
2138+
ret = do_git_config_sequence(opts, repo, fn, data,
2139+
config_source ?
2140+
config_source->scope :
2141+
CONFIG_SCOPE_UNKNOWN);
21212142
}
21222143

21232144
if (inc.remote_urls) {

0 commit comments

Comments
 (0)