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>
Kristoffer Haugsbakk committed
Jan 18, 2024 at 17:12 UTC
c15129b699d9b225e916b84f542b9d3665fccc3d
3 files changed
+24
-22
builtin/config.c
+3
-22
@@ -708,30 +708,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
708
}
709
710
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)
713
die(_("$HOME not set"));
722
-
714
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) {
716
given_config_source.file = git_system_config();
717
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
718
} else if (use_local_config) {
config.c
+20
@@ -1987,6 +1987,26 @@ char *git_system_config(void)
1987
return system_config;
1988
}
1989
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
+
2010
void git_global_config_paths(char **user_out, char **xdg_out)
2011
{
2012
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
config.h
+1
@@ -382,6 +382,7 @@ int config_error_nonbool(const char *);
382
#endif
383
384
char *git_system_config(void);
385
+char *git_global_config(void);
386
void git_global_config_paths(char **user, char **xdg);
387
388
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);