]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: factor out global config file retrieval
authorKristoffer Haugsbakk <code@khaugsbakk.name>
Thu, 18 Jan 2024 16:12:51 +0000 (17:12 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Jan 2024 20:17:41 +0000 (12:17 -0800)
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>
builtin/config.c
config.c
config.h

index 6fff26558169e26a401ef3e02a81b2d51b3450e7..08fe36d4997d896b1378d25bd899d32af3679f64 100644 (file)
@@ -708,30 +708,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
        }
 
        if (use_global_config) {
-               char *user_config, *xdg_config;
-
-               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.
-                        */
+               given_config_source.file = git_global_config();
+               if (!given_config_source.file)
                        die(_("$HOME not set"));
-
                given_config_source.scope = CONFIG_SCOPE_GLOBAL;
-
-               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) {
+       } else if (use_system_config) {
                given_config_source.file = git_system_config();
                given_config_source.scope = CONFIG_SCOPE_SYSTEM;
        } else if (use_local_config) {
index ebc6a57e1c35a65dae58665ac0575387547e8b8c..3cfeb3d8bd99f4ca15d0f3a06cd4b1fe932f7f47 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1987,6 +1987,26 @@ char *git_system_config(void)
        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)
 {
        char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
index e5e523553ccfea20790df73ab4a93df7ad390eb1..5dba984f770e4e96d322874351a70d0f7d0ee8ba 100644 (file)
--- a/config.h
+++ b/config.h
@@ -382,6 +382,7 @@ int config_error_nonbool(const char *);
 #endif
 
 char *git_system_config(void);
+char *git_global_config(void);
 void git_global_config_paths(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);