Skip to content

Commit fef7ba5

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: make apply_all_patches() return 128 or 1 on error
To finish libifying the apply functionality, apply_all_patches() should not die() or exit() in case of error, but return either 128 or 1, so that it gives the same exit code as when die() or exit(1) is called. This way scripts relying on the exit code don't need to be changed. While doing that we must take care that file descriptors are properly closed and, if needed, reset to a sensible value. Also, according to the lockfile API, when finished with a lockfile, one should either commit it or roll it back. This is even more important now that the same lockfile can be passed to init_apply_state() many times to be reused by series of calls to the apply lib functions. Helped-by: Nguyễn Thái Ngọc Duy <[email protected]> Helped-by: Johannes Schindelin <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b6446d5 commit fef7ba5

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

builtin/apply.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,15 +4578,18 @@ static int apply_all_patches(struct apply_state *state,
45784578
arg);
45794579

45804580
fd = open(arg, O_RDONLY);
4581-
if (fd < 0)
4582-
die_errno(_("can't open patch '%s'"), arg);
4581+
if (fd < 0) {
4582+
error(_("can't open patch '%s': %s"), arg, strerror(errno));
4583+
res = -128;
4584+
goto end;
4585+
}
45834586
read_stdin = 0;
45844587
set_default_whitespace_mode(state);
45854588
res = apply_patch(state, fd, arg, options);
4589+
close(fd);
45864590
if (res < 0)
45874591
goto end;
45884592
errs |= res;
4589-
close(fd);
45904593
}
45914594
set_default_whitespace_mode(state);
45924595
if (read_stdin) {
@@ -4606,11 +4609,14 @@ static int apply_all_patches(struct apply_state *state,
46064609
squelched),
46074610
squelched);
46084611
}
4609-
if (state->ws_error_action == die_on_ws_error)
4610-
die(Q_("%d line adds whitespace errors.",
4611-
"%d lines add whitespace errors.",
4612-
state->whitespace_error),
4613-
state->whitespace_error);
4612+
if (state->ws_error_action == die_on_ws_error) {
4613+
error(Q_("%d line adds whitespace errors.",
4614+
"%d lines add whitespace errors.",
4615+
state->whitespace_error),
4616+
state->whitespace_error);
4617+
res = -128;
4618+
goto end;
4619+
}
46144620
if (state->applied_after_fixing_ws && state->apply)
46154621
warning("%d line%s applied after"
46164622
" fixing whitespace errors.",
@@ -4624,15 +4630,24 @@ static int apply_all_patches(struct apply_state *state,
46244630
}
46254631

46264632
if (state->update_index) {
4627-
if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
4628-
die(_("Unable to write new index file"));
4633+
res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
4634+
if (res) {
4635+
error(_("Unable to write new index file"));
4636+
res = -128;
4637+
goto end;
4638+
}
46294639
state->newfd = -1;
46304640
}
46314641

46324642
return !!errs;
46334643

46344644
end:
4635-
exit(res == -1 ? 1 : 128);
4645+
if (state->newfd >= 0) {
4646+
rollback_lock_file(state->lock_file);
4647+
state->newfd = -1;
4648+
}
4649+
4650+
return (res == -1 ? 1 : 128);
46364651
}
46374652

46384653
int cmd_apply(int argc, const char **argv, const char *prefix)

0 commit comments

Comments
 (0)