Skip to content

Commit c15129b

Browse files
LemmingAvalanchegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ecffa3e commit c15129b

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

builtin/config.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -708,30 +708,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
708708
}
709709

710710
if (use_global_config) {
711-
char *user_config, *xdg_config;
712-
713-
git_global_config_paths(&user_config, &xdg_config);
714-
if (!user_config)
715-
/*
716-
* It is unknown if HOME/.gitconfig exists, so
717-
* we do not know if we should write to XDG
718-
* location; error out even if XDG_CONFIG_HOME
719-
* is set and points at a sane location.
720-
*/
711+
given_config_source.file = git_global_config();
712+
if (!given_config_source.file)
721713
die(_("$HOME not set"));
722-
723714
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
724-
725-
if (access_or_warn(user_config, R_OK, 0) &&
726-
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
727-
given_config_source.file = xdg_config;
728-
free(user_config);
729-
} else {
730-
given_config_source.file = user_config;
731-
free(xdg_config);
732-
}
733-
}
734-
else if (use_system_config) {
715+
} else if (use_system_config) {
735716
given_config_source.file = git_system_config();
736717
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
737718
} else if (use_local_config) {

config.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,26 @@ char *git_system_config(void)
19871987
return system_config;
19881988
}
19891989

1990+
char *git_global_config(void)
1991+
{
1992+
char *user_config, *xdg_config;
1993+
1994+
git_global_config_paths(&user_config, &xdg_config);
1995+
if (!user_config) {
1996+
free(xdg_config);
1997+
return NULL;
1998+
}
1999+
2000+
if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
2001+
!access_or_warn(xdg_config, R_OK, 0)) {
2002+
free(user_config);
2003+
return xdg_config;
2004+
} else {
2005+
free(xdg_config);
2006+
return user_config;
2007+
}
2008+
}
2009+
19902010
void git_global_config_paths(char **user_out, char **xdg_out)
19912011
{
19922012
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));

config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ int config_error_nonbool(const char *);
382382
#endif
383383

384384
char *git_system_config(void);
385+
char *git_global_config(void);
385386
void git_global_config_paths(char **user, char **xdg);
386387

387388
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);

0 commit comments

Comments
 (0)