From 91b769c48f4cf46d9514770849a1bf9cb33430c3 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Tue, 24 May 2016 10:11:24 +0200 Subject: [PATCH] builtin/apply: move applying patches into apply_all_patches() To libify the apply functionality we should provide a function to apply many patches. Let's move the code to do that into a new apply_all_patches() function. Reviewed-by: Stefan Beller Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/apply.c | 129 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 58 deletions(-) diff --git a/builtin/apply.c b/builtin/apply.c index 8095026d33..5027f1b504 100644 --- a/builtin/apply.c +++ b/builtin/apply.c @@ -4707,13 +4707,80 @@ static void check_apply_state(struct apply_state *state, int force_apply) state->unsafe_paths = 0; } -int cmd_apply(int argc, const char **argv, const char *prefix) +static int apply_all_patches(struct apply_state *state, + int argc, + const char **argv, + int options) { int i; int errs = 0; + int read_stdin = 1; + + for (i = 0; i < argc; i++) { + const char *arg = argv[i]; + int fd; + + if (!strcmp(arg, "-")) { + errs |= apply_patch(state, 0, "", options); + read_stdin = 0; + continue; + } else if (0 < state->prefix_length) + arg = prefix_filename(state->prefix, + state->prefix_length, + arg); + + fd = open(arg, O_RDONLY); + if (fd < 0) + die_errno(_("can't open patch '%s'"), arg); + read_stdin = 0; + set_default_whitespace_mode(state); + errs |= apply_patch(state, fd, arg, options); + close(fd); + } + set_default_whitespace_mode(state); + if (read_stdin) + errs |= apply_patch(state, 0, "", options); + + if (state->whitespace_error) { + if (state->squelch_whitespace_errors && + state->squelch_whitespace_errors < state->whitespace_error) { + int squelched = + state->whitespace_error - state->squelch_whitespace_errors; + warning(Q_("squelched %d whitespace error", + "squelched %d whitespace errors", + squelched), + squelched); + } + if (state->ws_error_action == die_on_ws_error) + die(Q_("%d line adds whitespace errors.", + "%d lines add whitespace errors.", + state->whitespace_error), + state->whitespace_error); + if (state->applied_after_fixing_ws && state->apply) + warning("%d line%s applied after" + " fixing whitespace errors.", + state->applied_after_fixing_ws, + state->applied_after_fixing_ws == 1 ? "" : "s"); + else if (state->whitespace_error) + warning(Q_("%d line adds whitespace errors.", + "%d lines add whitespace errors.", + state->whitespace_error), + state->whitespace_error); + } + + if (state->update_index) { + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) + die(_("Unable to write new index file")); + } + + return !!errs; +} + +int cmd_apply(int argc, const char **argv, const char *prefix) +{ int force_apply = 0; int options = 0; - int read_stdin = 1; + int ret; struct apply_state state; struct option builtin_apply_options[] = { @@ -4792,63 +4859,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix) check_apply_state(&state, force_apply); - for (i = 0; i < argc; i++) { - const char *arg = argv[i]; - int fd; - - if (!strcmp(arg, "-")) { - errs |= apply_patch(&state, 0, "", options); - read_stdin = 0; - continue; - } else if (0 < state.prefix_length) - arg = prefix_filename(state.prefix, - state.prefix_length, - arg); - - fd = open(arg, O_RDONLY); - if (fd < 0) - die_errno(_("can't open patch '%s'"), arg); - read_stdin = 0; - set_default_whitespace_mode(&state); - errs |= apply_patch(&state, fd, arg, options); - close(fd); - } - set_default_whitespace_mode(&state); - if (read_stdin) - errs |= apply_patch(&state, 0, "", options); - if (state.whitespace_error) { - if (state.squelch_whitespace_errors && - state.squelch_whitespace_errors < state.whitespace_error) { - int squelched = - state.whitespace_error - state.squelch_whitespace_errors; - warning(Q_("squelched %d whitespace error", - "squelched %d whitespace errors", - squelched), - squelched); - } - if (state.ws_error_action == die_on_ws_error) - die(Q_("%d line adds whitespace errors.", - "%d lines add whitespace errors.", - state.whitespace_error), - state.whitespace_error); - if (state.applied_after_fixing_ws && state.apply) - warning("%d line%s applied after" - " fixing whitespace errors.", - state.applied_after_fixing_ws, - state.applied_after_fixing_ws == 1 ? "" : "s"); - else if (state.whitespace_error) - warning(Q_("%d line adds whitespace errors.", - "%d lines add whitespace errors.", - state.whitespace_error), - state.whitespace_error); - } - - if (state.update_index) { - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) - die(_("Unable to write new index file")); - } + ret = apply_all_patches(&state, argc, argv, options); clear_apply_state(&state); - return !!errs; + return ret; } -- 2.11.4.GIT