Skip to content

Commit 2cb9294

Browse files
avargitster
authored andcommitted
submodule--helper: don't exit() on failure, return
Change code downstream of module_update() to short-circuit and return to the top-level on failure, rather than calling exit(). To do so we need to diligently check whether we "must_die_on_failure", which is a pattern started in c51f8f9 (submodule--helper: run update procedures from C, 2021-08-24), but which hadn't been completed to the point where we could avoid calling exit() here. This introduces no functional changes, but makes it easier to both call these routines as a library in the future, and to eventually avoid leaking memory. This and similar control flow in submodule--helper.c could be made simpler by properly "libifying" it, i.e. to have it consistently return -1 on failures, and to early return on any non-success. But let's leave that larger project for now, and (mostly) emulate what were doing with the "exit(128)" before this change. 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 6870cdc commit 2cb9294

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

builtin/submodule--helper.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,8 @@ static int fetch_in_submodule(const char *module_path, int depth, int quiet,
21232123
return run_command(&cp);
21242124
}
21252125

2126-
static int run_update_command(const struct update_data *ud, int subforce)
2126+
static int run_update_command(const struct update_data *ud, int subforce,
2127+
int *must_die_on_failure)
21272128
{
21282129
struct child_process cp = CHILD_PROCESS_INIT;
21292130
char *oid = oid_to_hex(&ud->oid);
@@ -2187,7 +2188,7 @@ static int run_update_command(const struct update_data *ud, int subforce)
21872188
}
21882189

21892190
if (ret == 128)
2190-
exit(ret);
2191+
*must_die_on_failure = 1;
21912192
return ret;
21922193
}
21932194

@@ -2219,7 +2220,8 @@ static int run_update_command(const struct update_data *ud, int subforce)
22192220
return 0;
22202221
}
22212222

2222-
static int run_update_procedure(const struct update_data *ud)
2223+
static int run_update_procedure(const struct update_data *ud,
2224+
int *must_die_on_failure)
22232225
{
22242226
int subforce = is_null_oid(&ud->suboid) || ud->force;
22252227

@@ -2246,7 +2248,7 @@ static int run_update_procedure(const struct update_data *ud)
22462248
ud->displaypath, oid_to_hex(&ud->oid));
22472249
}
22482250

2249-
return run_update_command(ud, subforce);
2251+
return run_update_command(ud, subforce, must_die_on_failure);
22502252
}
22512253

22522254
static const char *remote_submodule_branch(const char *path)
@@ -2383,7 +2385,8 @@ static void update_data_to_args(const struct update_data *update_data,
23832385
"--no-single-branch");
23842386
}
23852387

2386-
static int update_submodule(struct update_data *update_data)
2388+
static int update_submodule(struct update_data *update_data,
2389+
int *must_die_on_failure)
23872390
{
23882391
ensure_core_worktree(update_data->sm_path);
23892392

@@ -2419,9 +2422,15 @@ static int update_submodule(struct update_data *update_data)
24192422
free(remote_ref);
24202423
}
24212424

2422-
if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
2423-
if (run_update_procedure(update_data))
2425+
if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2426+
int ret;
2427+
2428+
ret = run_update_procedure(update_data, must_die_on_failure);
2429+
if (*must_die_on_failure)
2430+
return ret;
2431+
if (ret)
24242432
return 1;
2433+
}
24252434

24262435
if (update_data->recursive) {
24272436
struct child_process cp = CHILD_PROCESS_INIT;
@@ -2437,14 +2446,13 @@ static int update_submodule(struct update_data *update_data)
24372446
prepare_submodule_repo_env(&cp.env);
24382447
update_data_to_args(&next, &cp.args);
24392448

2440-
/* die() if child process die()'d */
24412449
ret = run_command(&cp);
24422450
if (!ret)
24432451
return 0;
24442452
die_message(_("Failed to recurse into submodule path '%s'"),
24452453
update_data->displaypath);
24462454
if (ret == 128)
2447-
exit(ret);
2455+
*must_die_on_failure = 1;
24482456
return ret;
24492457
}
24502458

@@ -2477,12 +2485,19 @@ static int update_submodules(struct update_data *update_data)
24772485

24782486
for (i = 0; i < suc.update_clone_nr; i++) {
24792487
struct update_clone_data ucd = suc.update_clone[i];
2488+
int must_die_on_failure = 0;
2489+
int code;
24802490

24812491
oidcpy(&update_data->oid, &ucd.oid);
24822492
update_data->just_cloned = ucd.just_cloned;
24832493
update_data->sm_path = ucd.sub->path;
24842494

2485-
if (update_submodule(update_data))
2495+
code = update_submodule(update_data, &must_die_on_failure);
2496+
if (code)
2497+
ret = code;
2498+
if (must_die_on_failure)
2499+
goto cleanup;
2500+
else if (code)
24862501
ret = 1;
24872502
}
24882503

0 commit comments

Comments
 (0)