Skip to content

Commit ccceb7b

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: make write_out_results() return -1 on error
To libify `git apply` functionality we have to signal errors to the caller instead of exit()ing. To do that in a compatible manner with the rest of the error handling in "builtin/apply.c", write_out_results() should return -1 instead of calling exit(). Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 434389d commit ccceb7b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

builtin/apply.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4383,6 +4383,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
43834383
return -1;
43844384
}
43854385

4386+
/*
4387+
* Returns:
4388+
* -1 if an error happened
4389+
* 0 if the patch applied cleanly
4390+
* 1 if the patch did not apply cleanly
4391+
*/
43864392
static int write_out_results(struct apply_state *state, struct patch *list)
43874393
{
43884394
int phase;
@@ -4396,8 +4402,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)
43964402
if (l->rejected)
43974403
errs = 1;
43984404
else {
4399-
if (write_out_one_result(state, l, phase))
4400-
exit(128);
4405+
if (write_out_one_result(state, l, phase)) {
4406+
string_list_clear(&cpath, 0);
4407+
return -1;
4408+
}
44014409
if (phase == 1) {
44024410
if (write_out_one_reject(state, l))
44034411
errs = 1;
@@ -4517,10 +4525,17 @@ static int apply_patch(struct apply_state *state,
45174525
}
45184526
}
45194527

4520-
if (state->apply && write_out_results(state, list)) {
4521-
/* with --3way, we still need to write the index out */
4522-
res = state->apply_with_reject ? -1 : 1;
4523-
goto end;
4528+
if (state->apply) {
4529+
int write_res = write_out_results(state, list);
4530+
if (write_res < 0) {
4531+
res = -128;
4532+
goto end;
4533+
}
4534+
if (write_res > 0) {
4535+
/* with --3way, we still need to write the index out */
4536+
res = state->apply_with_reject ? -1 : 1;
4537+
goto end;
4538+
}
45244539
}
45254540

45264541
if (state->fake_ancestor &&

0 commit comments

Comments
 (0)