Skip to content

Commit f5373de

Browse files
avargitster
authored andcommitted
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 <[email protected]> Reviewed-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e8697b commit f5373de

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

builtin/submodule--helper.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,57 @@
3131
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
3232
void *cb_data);
3333

34-
static char *repo_get_default_remote(struct repository *repo)
34+
static int repo_get_default_remote(struct repository *repo, char **default_remote)
3535
{
36-
char *dest = NULL, *ret;
36+
char *dest = NULL;
3737
struct strbuf sb = STRBUF_INIT;
3838
struct ref_store *store = get_main_ref_store(repo);
3939
const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
4040
NULL);
4141

4242
if (!refname)
43-
die(_("No such ref: %s"), "HEAD");
43+
return die_message(_("No such ref: %s"), "HEAD");
4444

4545
/* 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+
}
4850

4951
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);
5154

5255
strbuf_addf(&sb, "branch.%s.remote", refname);
5356
if (repo_config_get_string(repo, sb.buf, &dest))
54-
ret = xstrdup("origin");
57+
*default_remote = xstrdup("origin");
5558
else
56-
ret = dest;
59+
*default_remote = dest;
5760

5861
strbuf_release(&sb);
59-
return ret;
62+
return 0;
6063
}
6164

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)
6366
{
6467
struct repository subrepo;
6568

6669
if (repo_submodule_init(&subrepo, the_repository, module_path,
6770
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);
7174
}
7275

7376
static char *get_default_remote(void)
7477
{
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;
7685
}
7786

7887
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,
11561165
char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
11571166
struct strbuf sb = STRBUF_INIT;
11581167
char *sub_config_path = NULL;
1168+
int code;
11591169

11601170
if (!is_submodule_active(the_repository, path))
11611171
return;
@@ -1195,10 +1205,9 @@ static void sync_submodule(const char *path, const char *prefix,
11951205
goto cleanup;
11961206

11971207
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);
12021211

12031212
remote_key = xstrfmt("remote.%s.url", default_remote);
12041213
free(default_remote);
@@ -2419,9 +2428,16 @@ static int update_submodule(struct update_data *update_data)
24192428
update_data->displaypath);
24202429

24212430
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);
24252441

24262442
if (!update_data->nofetch) {
24272443
if (fetch_in_submodule(update_data->sm_path, update_data->depth,

0 commit comments

Comments
 (0)