summaryrefslogtreecommitdiff
path: root/builtin/apply.c
diff options
context:
space:
mode:
authorChristian Couder <christian.couder@gmail.com>2016-05-24 10:11:24 +0200
committerJunio C Hamano <gitster@pobox.com>2016-06-01 10:10:16 -0700
commit91b769c48f4cf46d9514770849a1bf9cb33430c3 (patch)
tree5f7eaf8aec6c5ce7595a27bbc8d760dc8265c73e /builtin/apply.c
parentc84a86c99526b88cfd7664888ecbfd28f35a025f (diff)
downloadgit-91b769c48f4cf46d9514770849a1bf9cb33430c3.tar.gz
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>
Diffstat (limited to 'builtin/apply.c')
-rw-r--r--builtin/apply.c129
1 files 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, "<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;
}