]> git.ipfire.org Git - thirdparty/git.git/commitdiff
submodule API: don't handle SM_..{UNSPECIFIED,COMMAND} in to_string()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Wed, 31 Aug 2022 23:18:05 +0000 (01:18 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 2 Sep 2022 16:16:24 +0000 (09:16 -0700)
Change the submodule_strategy_to_string() function added in
3604242f080 (submodule: port init from shell to C, 2016-04-15) to
really return a "const char *". In the "SM_UPDATE_COMMAND" case it
would return a strbuf_detach().

Furthermore, this function would return NULL on SM_UPDATE_UNSPECIFIED,
so it wasn't safe to xstrdup() its return value in the general case,
or to use it in a sprintf() format as the code removed in the
preceding commit did.

But its callers would never call it with either SM_UPDATE_UNSPECIFIED
or SM_UPDATE_COMMAND. Let's have its behavior reflect how its only
user expects it to behave, and BUG() out on the rest.

By doing this we can also stop needlessly xstrdup()-ing and free()-ing
the memory for the config we're setting. We can instead always use
constant strings. We can also use the *_tmp() variant of
git_config_get_string().

Let's also rename this submodule_strategy_to_string() function to
submodule_update_type_to_string(). Now that it's only tasked with
returning a string version of the "enum submodule_update_type type".
Before it would look at the "command" field in "struct
submodule_update_strategy".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Reviewed-by: Glen Choo <chooglen@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/submodule--helper.c
submodule.c
submodule.h

index 44c6dfdfbe17b9e385179d997e73295306cae618..a0505212cb004f736076bf127246b808aa56317a 100644 (file)
@@ -418,7 +418,8 @@ static void init_submodule(const char *path, const char *prefix,
 {
        const struct submodule *sub;
        struct strbuf sb = STRBUF_INIT;
-       char *upd = NULL, *url = NULL, *displaypath;
+       const char *upd;
+       char *url = NULL, *displaypath;
 
        displaypath = get_submodule_displaypath(path, prefix);
 
@@ -474,14 +475,14 @@ static void init_submodule(const char *path, const char *prefix,
 
        /* Copy "update" setting when it is not set yet */
        strbuf_addf(&sb, "submodule.%s.update", sub->name);
-       if (git_config_get_string(sb.buf, &upd) &&
+       if (git_config_get_string_tmp(sb.buf, &upd) &&
            sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
                if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
                        fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
                                sub->name);
-                       upd = xstrdup("none");
+                       upd = "none";
                } else {
-                       upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
+                       upd = submodule_update_type_to_string(sub->update_strategy.type);
                }
 
                if (git_config_set_gently(sb.buf, upd))
@@ -490,7 +491,6 @@ static void init_submodule(const char *path, const char *prefix,
        strbuf_release(&sb);
        free(displaypath);
        free(url);
-       free(upd);
 }
 
 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
index 3fa5db3ecdf87f4f02d583cfa66a87a2abbe5d2a..1ebda30c5069fd803971954e282cb0c551d05493 100644 (file)
@@ -415,10 +415,9 @@ int parse_submodule_update_strategy(const char *value,
        return 0;
 }
 
-const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
+const char *submodule_update_type_to_string(enum submodule_update_type type)
 {
-       struct strbuf sb = STRBUF_INIT;
-       switch (s->type) {
+       switch (type) {
        case SM_UPDATE_CHECKOUT:
                return "checkout";
        case SM_UPDATE_MERGE:
@@ -428,12 +427,11 @@ const char *submodule_strategy_to_string(const struct submodule_update_strategy
        case SM_UPDATE_NONE:
                return "none";
        case SM_UPDATE_UNSPECIFIED:
-               return NULL;
        case SM_UPDATE_COMMAND:
-               strbuf_addf(&sb, "!%s", s->command);
-               return strbuf_detach(&sb, NULL);
+               BUG("init_submodule() should handle type %d", type);
+       default:
+               BUG("unexpected update strategy type: %d", type);
        }
-       return NULL;
 }
 
 void handle_ignore_submodules_arg(struct diff_options *diffopt,
index bfaa9da1868af5e201db3a831b283924cf7c8a89..6a9fec6de1159f0389df2391c2d18e8d5fd7d297 100644 (file)
@@ -72,7 +72,7 @@ void die_path_inside_submodule(struct index_state *istate,
 enum submodule_update_type parse_submodule_update_type(const char *value);
 int parse_submodule_update_strategy(const char *value,
                                    struct submodule_update_strategy *dst);
-const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);
+const char *submodule_update_type_to_string(enum submodule_update_type type);
 void handle_ignore_submodules_arg(struct diff_options *, const char *);
 void show_submodule_diff_summary(struct diff_options *o, const char *path,
                            struct object_id *one, struct object_id *two,