Merge branch 'jc/maint-config-exit-status'
[gitweb.git] / builtin / config.c
index e8e1c0a4567f190c4b5d939bf9b0d363765bfd30..ada6e1211462558f307dce6142b7b65d641c8e02 100644 (file)
@@ -160,7 +160,7 @@ static int show_config(const char *key_, const char *value_, void *cb)
 
 static int get_value(const char *key_, const char *regex_)
 {
-       int ret = -1;
+       int ret = CONFIG_GENERIC_ERROR;
        char *global = NULL, *xdg = NULL, *repo_config = NULL;
        const char *system_wide = NULL, *local;
        struct config_include_data inc = CONFIG_INCLUDE_INIT;
@@ -196,11 +196,14 @@ static int get_value(const char *key_, const char *regex_)
                if (regcomp(key_regexp, key, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
                        free(key);
+                       ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
                }
        } else {
-               if (git_config_parse_key(key_, &key, NULL))
+               if (git_config_parse_key(key_, &key, NULL)) {
+                       ret = CONFIG_INVALID_KEY;
                        goto free_strings;
+               }
        }
 
        if (regex_) {
@@ -212,6 +215,7 @@ static int get_value(const char *key_, const char *regex_)
                regexp = (regex_t*)xmalloc(sizeof(regex_t));
                if (regcomp(regexp, regex_, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
+                       ret = CONFIG_INVALID_PATTERN;
                        goto free_strings;
                }
        }
@@ -387,12 +391,20 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 
                home_config_paths(&user_config, &xdg_config, "config");
 
-               if (access(user_config, R_OK) && !access(xdg_config, R_OK))
+               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");
+
+               if (access(user_config, R_OK) &&
+                   xdg_config && !access(xdg_config, R_OK))
                        given_config_file = xdg_config;
-               else if (user_config)
-                       given_config_file = user_config;
                else
-                       die("$HOME not set");
+                       given_config_file = user_config;
        }
        else if (use_system_config)
                given_config_file = git_etc_gitconfig();