config: factor out global config file retrieval

Factor out code that retrieves the global config file so that we can use
it in `gc.c` as well.

Use the old name from the previous commit since this function acts
functionally the same as `git_system_config` but for “global”.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Kristoffer Haugsbakk 2024-01-18 17:12:51 +01:00 committed by Junio C Hamano
parent ecffa3ed51
commit c15129b699
3 changed files with 24 additions and 22 deletions

View File

@ -708,30 +708,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
} }
if (use_global_config) { if (use_global_config) {
char *user_config, *xdg_config; given_config_source.file = git_global_config();
if (!given_config_source.file)
git_global_config_paths(&user_config, &xdg_config);
if (!user_config)
/*
* It is unknown if HOME/.gitconfig exists, so
* we do not know if we should write to XDG
* location; error out even if XDG_CONFIG_HOME
* is set and points at a sane location.
*/
die(_("$HOME not set")); die(_("$HOME not set"));
given_config_source.scope = CONFIG_SCOPE_GLOBAL; given_config_source.scope = CONFIG_SCOPE_GLOBAL;
} else if (use_system_config) {
if (access_or_warn(user_config, R_OK, 0) &&
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
given_config_source.file = xdg_config;
free(user_config);
} else {
given_config_source.file = user_config;
free(xdg_config);
}
}
else if (use_system_config) {
given_config_source.file = git_system_config(); given_config_source.file = git_system_config();
given_config_source.scope = CONFIG_SCOPE_SYSTEM; given_config_source.scope = CONFIG_SCOPE_SYSTEM;
} else if (use_local_config) { } else if (use_local_config) {

View File

@ -1987,6 +1987,26 @@ char *git_system_config(void)
return system_config; return system_config;
} }
char *git_global_config(void)
{
char *user_config, *xdg_config;
git_global_config_paths(&user_config, &xdg_config);
if (!user_config) {
free(xdg_config);
return NULL;
}
if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
!access_or_warn(xdg_config, R_OK, 0)) {
free(user_config);
return xdg_config;
} else {
free(xdg_config);
return user_config;
}
}
void git_global_config_paths(char **user_out, char **xdg_out) void git_global_config_paths(char **user_out, char **xdg_out)
{ {
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL")); char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));

View File

@ -382,6 +382,7 @@ int config_error_nonbool(const char *);
#endif #endif
char *git_system_config(void); char *git_system_config(void);
char *git_global_config(void);
void git_global_config_paths(char **user, char **xdg); void git_global_config_paths(char **user, char **xdg);
int git_config_parse_parameter(const char *, config_fn_t fn, void *data); int git_config_parse_parameter(const char *, config_fn_t fn, void *data);