]> git.ipfire.org Git - thirdparty/git.git/blobdiff - repo-config.c
cvsserver: avoid warning about active db handles
[thirdparty/git.git] / repo-config.c
index 127afd784c1fcdb531c1d3435f724d0041151e90..743f02b7de7b3b819d3db29ddfcdcf8ebacaccc4 100644 (file)
@@ -29,16 +29,13 @@ static int show_config(const char* key_, const char* value_)
        const char *vptr = value;
        int dup_error = 0;
 
-       if (value_ == NULL)
-               value_ = "";
-
        if (!use_key_regexp && strcmp(key_, key))
                return 0;
        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
                return 0;
        if (regexp != NULL &&
                         (do_not_match ^
-                         regexec(regexp, value_, 0, NULL, 0)))
+                         regexec(regexp, (value_?value_:""), 0, NULL, 0)))
                return 0;
 
        if (show_keys)
@@ -46,11 +43,11 @@ static int show_config(const char* key_, const char* value_)
        if (seen && !do_all)
                dup_error = 1;
        if (type == T_INT)
-               sprintf(value, "%d", git_config_int(key_, value_));
+               sprintf(value, "%d", git_config_int(key_, value_?value_:""));
        else if (type == T_BOOL)
                vptr = git_config_bool(key_, value_) ? "true" : "false";
        else
-               vptr = value_;
+               vptr = value_?value_:"";
        seen++;
        if (dup_error) {
                error("More than one value for the key %s: %s",
@@ -64,7 +61,20 @@ static int show_config(const char* key_, const char* value_)
 
 static int get_value(const char* key_, const char* regex_)
 {
+       int ret = -1;
        char *tl;
+       char *global = NULL, *repo_config = NULL;
+       const char *local;
+
+       local = getenv("GIT_CONFIG");
+       if (!local) {
+               const char *home = getenv("HOME");
+               local = getenv("GIT_CONFIG_LOCAL");
+               if (!local)
+                       local = repo_config = strdup(git_path("config"));
+               if (home)
+                       global = strdup(mkpath("%s/.gitconfig", home));
+       }
 
        key = strdup(key_);
        for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
@@ -76,7 +86,7 @@ static int get_value(const char* key_, const char* regex_)
                key_regexp = (regex_t*)malloc(sizeof(regex_t));
                if (regcomp(key_regexp, key, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid key pattern: %s\n", key_);
-                       return -1;
+                       goto free_strings;
                }
        }
 
@@ -89,11 +99,16 @@ static int get_value(const char* key_, const char* regex_)
                regexp = (regex_t*)malloc(sizeof(regex_t));
                if (regcomp(regexp, regex_, REG_EXTENDED)) {
                        fprintf(stderr, "Invalid pattern: %s\n", regex_);
-                       return -1;
+                       goto free_strings;
                }
        }
 
-       git_config(show_config);
+       if (do_all && global)
+               git_config_from_file(show_config, global);
+       git_config_from_file(show_config, local);
+       if (!do_all && !seen && global)
+               git_config_from_file(show_config, global);
+
        free(key);
        if (regexp) {
                regfree(regexp);
@@ -101,14 +116,22 @@ static int get_value(const char* key_, const char* regex_)
        }
 
        if (do_all)
-               return !seen;
-
-       return (seen == 1) ? 0 : 1;
+               ret = !seen;
+       else
+               ret =  (seen == 1) ? 0 : 1;
+
+free_strings:
+       if (repo_config)
+               free(repo_config);
+       if (global)
+               free(global);
+       return ret;
 }
 
 int main(int argc, const char **argv)
 {
-       setup_git_directory();
+       int nongit = 0;
+       setup_git_directory_gently(&nongit);
 
        while (1 < argc) {
                if (!strcmp(argv[1], "--int"))