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 <sbeller@google.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Christian Couder 2016-05-24 10:11:24 +02:00 committed by Junio C Hamano
parent c84a86c995
commit 91b769c48f

View file

@ -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, "<stdin>", 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, "<stdin>", 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, "<stdin>", 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, "<stdin>", 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;
}