submodule-config: combine early return code into one goto
[gitweb.git] / submodule-config.c
index 92502b594d055bbf99fd203175d7dffaf8dfb732..a8875745909f65325c055b5fe5c4a4dc1383ff1b 100644 (file)
@@ -30,7 +30,7 @@ enum lookup_type {
        lookup_path
 };
 
-static struct submodule_cache cache;
+static struct submodule_cache the_submodule_cache;
 static int is_cache_init;
 
 static int config_path_cmp(const struct submodule_entry *a,
@@ -349,9 +349,9 @@ static int parse_config(const char *var, const char *value, void *data)
 }
 
 static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
-                                     unsigned char *gitmodules_sha1)
+                                     unsigned char *gitmodules_sha1,
+                                     struct strbuf *rev)
 {
-       struct strbuf rev = STRBUF_INIT;
        int ret = 0;
 
        if (is_null_sha1(commit_sha1)) {
@@ -359,11 +359,10 @@ static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,
                return 1;
        }
 
-       strbuf_addf(&rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
-       if (get_sha1(rev.buf, gitmodules_sha1) >= 0)
+       strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(commit_sha1));
+       if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
                ret = 1;
 
-       strbuf_release(&rev);
        return ret;
 }
 
@@ -377,7 +376,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
 {
        struct strbuf rev = STRBUF_INIT;
        unsigned long config_size;
-       char *config;
+       char *config = NULL;
        unsigned char sha1[20];
        enum object_type type;
        const struct submodule *submodule = NULL;
@@ -392,15 +391,14 @@ static const struct submodule *config_from(struct submodule_cache *cache,
                struct hashmap_iter iter;
                struct submodule_entry *entry;
 
-               hashmap_iter_init(&cache->for_name, &iter);
-               entry = hashmap_iter_next(&iter);
+               entry = hashmap_iter_first(&cache->for_name, &iter);
                if (!entry)
                        return NULL;
                return entry->config;
        }
 
-       if (!gitmodule_sha1_from_commit(commit_sha1, sha1))
-               return NULL;
+       if (!gitmodule_sha1_from_commit(commit_sha1, sha1, &rev))
+               goto out;
 
        switch (lookup_type) {
        case lookup_name:
@@ -411,16 +409,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
                break;
        }
        if (submodule)
-               return submodule;
+               goto out;
 
        config = read_sha1_file(sha1, &type, &config_size);
-       if (!config)
-               return NULL;
-
-       if (type != OBJ_BLOB) {
-               free(config);
-               return NULL;
-       }
+       if (!config || type != OBJ_BLOB)
+               goto out;
 
        /* fill the submodule config into the cache */
        parameter.cache = cache;
@@ -429,6 +422,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,
        parameter.overwrite = 0;
        git_config_from_mem(parse_config, "submodule-blob", rev.buf,
                        config, config_size, &parameter);
+       strbuf_release(&rev);
        free(config);
 
        switch (lookup_type) {
@@ -439,6 +433,11 @@ static const struct submodule *config_from(struct submodule_cache *cache,
        default:
                return NULL;
        }
+
+out:
+       strbuf_release(&rev);
+       free(config);
+       return submodule;
 }
 
 static const struct submodule *config_from_path(struct submodule_cache *cache,
@@ -458,14 +457,14 @@ static void ensure_cache_init(void)
        if (is_cache_init)
                return;
 
-       cache_init(&cache);
+       cache_init(&the_submodule_cache);
        is_cache_init = 1;
 }
 
 int parse_submodule_config_option(const char *var, const char *value)
 {
        struct parse_config_parameter parameter;
-       parameter.cache = &cache;
+       parameter.cache = &the_submodule_cache;
        parameter.commit_sha1 = NULL;
        parameter.gitmodules_sha1 = null_sha1;
        parameter.overwrite = 1;
@@ -478,18 +477,18 @@ const struct submodule *submodule_from_name(const unsigned char *commit_sha1,
                const char *name)
 {
        ensure_cache_init();
-       return config_from_name(&cache, commit_sha1, name);
+       return config_from_name(&the_submodule_cache, commit_sha1, name);
 }
 
 const struct submodule *submodule_from_path(const unsigned char *commit_sha1,
                const char *path)
 {
        ensure_cache_init();
-       return config_from_path(&cache, commit_sha1, path);
+       return config_from_path(&the_submodule_cache, commit_sha1, path);
 }
 
 void submodule_free(void)
 {
-       cache_free(&cache);
+       cache_free(&the_submodule_cache);
        is_cache_init = 0;
 }