submodule--helper: libify more "die" paths for module_update()

As noted in a preceding commit the get_default_remote_submodule() and remote_submodule_branch() functions would invoke die(), and thus leave update_submodule() only partially lib-ified. Let's address the former of those cases. Change the functions to return an int exit code (non-zero on failure), while leaving the get_default_remote() function for the callers that still want the die() semantics. This change addresses 1/2 of the "die" issue in these two lines in update_submodule(): char *remote_name = get_default_remote_submodule(update_data->sm_path); const char *branch = remote_submodule_branch(update_data->sm_path); We can safely remove the "!default_remote" case from sync_submodule(), because our get_default_remote_submodule() function now returns a die_message() on failure, so we can have it and other callers check if the exit code should be non-zero instead. 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>

Ævar Arnfjörð Bjarmason committed Sep 1, 2022 at 01:18 UTC f5373deabd537daae3d7c34f6dfa4b62ed45be51
1 file changed +37 -21
builtin/submodule--helper.c
+37 -21
@@ -31,48 +31,57 @@
31 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
32 void *cb_data);
33
34 -static char *repo_get_default_remote(struct repository *repo)
34 +static int repo_get_default_remote(struct repository *repo, char **default_remote)
35 {
36 - char *dest = NULL, *ret;
36 + char *dest = NULL;
37 struct strbuf sb = STRBUF_INIT;
38 struct ref_store *store = get_main_ref_store(repo);
39 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
40 NULL);
41
42 if (!refname)
43 - die(_("No such ref: %s"), "HEAD");
43 + return die_message(_("No such ref: %s"), "HEAD");
44
45 /* detached HEAD */
46 - if (!strcmp(refname, "HEAD"))
47 - return xstrdup("origin");
46 + if (!strcmp(refname, "HEAD")) {
47 + *default_remote = xstrdup("origin");
48 + return 0;
49 + }
50
51 if (!skip_prefix(refname, "refs/heads/", &refname))
50 - die(_("Expecting a full ref name, got %s"), refname);
52 + return die_message(_("Expecting a full ref name, got %s"),
53 + refname);
54
55 strbuf_addf(&sb, "branch.%s.remote", refname);
56 if (repo_config_get_string(repo, sb.buf, &dest))
54 - ret = xstrdup("origin");
57 + *default_remote = xstrdup("origin");
58 else
56 - ret = dest;
59 + *default_remote = dest;
60
61 strbuf_release(&sb);
59 - return ret;
62 + return 0;
63 }
64
62 -static char *get_default_remote_submodule(const char *module_path)
65 +static int get_default_remote_submodule(const char *module_path, char **default_remote)
66 {
67 struct repository subrepo;
68
69 if (repo_submodule_init(&subrepo, the_repository, module_path,
70 null_oid()) < 0)
68 - die(_("could not get a repository handle for submodule '%s'"),
69 - module_path);
70 - return repo_get_default_remote(&subrepo);
71 + return die_message(_("could not get a repository handle for submodule '%s'"),
72 + module_path);
73 + return repo_get_default_remote(&subrepo, default_remote);
74 }
75
76 static char *get_default_remote(void)
77 {
75 - return repo_get_default_remote(the_repository);
78 + char *default_remote;
79 + int code = repo_get_default_remote(the_repository, &default_remote);
80 +
81 + if (code)
82 + exit(code);
83 +
84 + return default_remote;
85 }
86
87 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
@@ -1156,6 +1165,7 @@ static void sync_submodule(const char *path, const char *prefix,
1165 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1166 struct strbuf sb = STRBUF_INIT;
1167 char *sub_config_path = NULL;
1168 + int code;
1169
1170 if (!is_submodule_active(the_repository, path))
1171 return;
@@ -1195,10 +1205,9 @@ static void sync_submodule(const char *path, const char *prefix,
1205 goto cleanup;
1206
1207 strbuf_reset(&sb);
1198 - default_remote = get_default_remote_submodule(path);
1199 - if (!default_remote)
1200 - die(_("failed to get the default remote for submodule '%s'"),
1201 - path);
1208 + code = get_default_remote_submodule(path, &default_remote);
1209 + if (code)
1210 + exit(code);
1211
1212 remote_key = xstrfmt("remote.%s.url", default_remote);
1213 free(default_remote);
@@ -2419,9 +2428,16 @@ static int update_submodule(struct update_data *update_data)
2428 update_data->displaypath);
2429
2430 if (update_data->remote) {
2422 - char *remote_name = get_default_remote_submodule(update_data->sm_path);
2423 - const char *branch = remote_submodule_branch(update_data->sm_path);
2424 - char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2431 + char *remote_name;
2432 + const char *branch;
2433 + char *remote_ref;
2434 + int code;
2435 +
2436 + code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2437 + if (code)
2438 + return code;
2439 + branch = remote_submodule_branch(update_data->sm_path);
2440 + remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2441
2442 if (!update_data->nofetch) {
2443 if (fetch_in_submodule(update_data->sm_path, update_data->depth,