builtin/apply: make write_out_one_result() 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_one_result() should just return what remove_file() and create_file() are returning instead of calling exit(). 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 434389deb193016464702b25af2b79bb5c6fd098
1 file changed +16 -22
builtin/apply.c
+16 -22
@@ -4287,36 +4287,29 @@ static int create_file(struct apply_state *state, struct patch *patch)
4287 }
4288
4289 /* phase zero is to remove, phase one is to create */
4290 -static void write_out_one_result(struct apply_state *state,
4291 - struct patch *patch,
4292 - int phase)
4290 +static int write_out_one_result(struct apply_state *state,
4291 + struct patch *patch,
4292 + int phase)
4293 {
4294 if (patch->is_delete > 0) {
4295 - if (phase == 0) {
4296 - if (remove_file(state, patch, 1))
4297 - exit(128);
4298 - }
4299 - return;
4295 + if (phase == 0)
4296 + return remove_file(state, patch, 1);
4297 + return 0;
4298 }
4299 if (patch->is_new > 0 || patch->is_copy) {
4302 - if (phase == 1) {
4303 - if (create_file(state, patch))
4304 - exit(128);
4305 - }
4306 - return;
4300 + if (phase == 1)
4301 + return create_file(state, patch);
4302 + return 0;
4303 }
4304 /*
4305 * Rename or modification boils down to the same
4306 * thing: remove the old, write the new
4307 */
4312 - if (phase == 0) {
4313 - if (remove_file(state, patch, patch->is_rename))
4314 - exit(128);
4315 - }
4316 - if (phase == 1) {
4317 - if (create_file(state, patch))
4318 - exit(128);
4319 - }
4308 + if (phase == 0)
4309 + return remove_file(state, patch, patch->is_rename);
4310 + if (phase == 1)
4311 + return create_file(state, patch);
4312 + return 0;
4313 }
4314
4315 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
@@ -4403,7 +4396,8 @@ static int write_out_results(struct apply_state *state, struct patch *list)
4396 if (l->rejected)
4397 errs = 1;
4398 else {
4406 - write_out_one_result(state, l, phase);
4399 + if (write_out_one_result(state, l, phase))
4400 + exit(128);
4401 if (phase == 1) {
4402 if (write_out_one_reject(state, l))
4403 errs = 1;