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 <sunshine@sunshineco.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Aug 8, 2016 at 23:03 UTC ccceb7bb13a37b1834bc1c455e40abc710997dd3
1 file changed +21 -6
builtin/apply.c
+21 -6
@@ -4383,6 +4383,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
4383 return -1;
4384 }
4385
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 + */
4392 static int write_out_results(struct apply_state *state, struct patch *list)
4393 {
4394 int phase;
@@ -4396,8 +4402,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)
4402 if (l->rejected)
4403 errs = 1;
4404 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 + }
4409 if (phase == 1) {
4410 if (write_out_one_reject(state, l))
4411 errs = 1;
@@ -4517,10 +4525,17 @@ static int apply_patch(struct apply_state *state,
4525 }
4526 }
4527
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 + }
4539 }
4540
4541 if (state->fake_ancestor &&