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>

Christian Couder committed May 24, 2016 at 10:11 UTC 91b769c48f4cf46d9514770849a1bf9cb33430c3
1 file changed +71 -58
builtin/apply.c
+71 -58
@@ -4707,13 +4707,80 @@ static void check_apply_state(struct apply_state *state, int force_apply)
4707 state->unsafe_paths = 0;
4708 }
4709
4710 -int cmd_apply(int argc, const char **argv, const char *prefix)
4710 +static int apply_all_patches(struct apply_state *state,
4711 + int argc,
4712 + const char **argv,
4713 + int options)
4714 {
4715 int i;
4716 int errs = 0;
4717 + int read_stdin = 1;
4718 +
4719 + for (i = 0; i < argc; i++) {
4720 + const char *arg = argv[i];
4721 + int fd;
4722 +
4723 + if (!strcmp(arg, "-")) {
4724 + errs |= apply_patch(state, 0, "<stdin>", options);
4725 + read_stdin = 0;
4726 + continue;
4727 + } else if (0 < state->prefix_length)
4728 + arg = prefix_filename(state->prefix,
4729 + state->prefix_length,
4730 + arg);
4731 +
4732 + fd = open(arg, O_RDONLY);
4733 + if (fd < 0)
4734 + die_errno(_("can't open patch '%s'"), arg);
4735 + read_stdin = 0;
4736 + set_default_whitespace_mode(state);
4737 + errs |= apply_patch(state, fd, arg, options);
4738 + close(fd);
4739 + }
4740 + set_default_whitespace_mode(state);
4741 + if (read_stdin)
4742 + errs |= apply_patch(state, 0, "<stdin>", options);
4743 +
4744 + if (state->whitespace_error) {
4745 + if (state->squelch_whitespace_errors &&
4746 + state->squelch_whitespace_errors < state->whitespace_error) {
4747 + int squelched =
4748 + state->whitespace_error - state->squelch_whitespace_errors;
4749 + warning(Q_("squelched %d whitespace error",
4750 + "squelched %d whitespace errors",
4751 + squelched),
4752 + squelched);
4753 + }
4754 + if (state->ws_error_action == die_on_ws_error)
4755 + die(Q_("%d line adds whitespace errors.",
4756 + "%d lines add whitespace errors.",
4757 + state->whitespace_error),
4758 + state->whitespace_error);
4759 + if (state->applied_after_fixing_ws && state->apply)
4760 + warning("%d line%s applied after"
4761 + " fixing whitespace errors.",
4762 + state->applied_after_fixing_ws,
4763 + state->applied_after_fixing_ws == 1 ? "" : "s");
4764 + else if (state->whitespace_error)
4765 + warning(Q_("%d line adds whitespace errors.",
4766 + "%d lines add whitespace errors.",
4767 + state->whitespace_error),
4768 + state->whitespace_error);
4769 + }
4770 +
4771 + if (state->update_index) {
4772 + if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4773 + die(_("Unable to write new index file"));
4774 + }
4775 +
4776 + return !!errs;
4777 +}
4778 +
4779 +int cmd_apply(int argc, const char **argv, const char *prefix)
4780 +{
4781 int force_apply = 0;
4782 int options = 0;
4716 - int read_stdin = 1;
4783 + int ret;
4784 struct apply_state state;
4785
4786 struct option builtin_apply_options[] = {
@@ -4792,63 +4859,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4859
4860 check_apply_state(&state, force_apply);
4861
4795 - for (i = 0; i < argc; i++) {
4796 - const char *arg = argv[i];
4797 - int fd;
4798 -
4799 - if (!strcmp(arg, "-")) {
4800 - errs |= apply_patch(&state, 0, "<stdin>", options);
4801 - read_stdin = 0;
4802 - continue;
4803 - } else if (0 < state.prefix_length)
4804 - arg = prefix_filename(state.prefix,
4805 - state.prefix_length,
4806 - arg);
4807 -
4808 - fd = open(arg, O_RDONLY);
4809 - if (fd < 0)
4810 - die_errno(_("can't open patch '%s'"), arg);
4811 - read_stdin = 0;
4812 - set_default_whitespace_mode(&state);
4813 - errs |= apply_patch(&state, fd, arg, options);
4814 - close(fd);
4815 - }
4816 - set_default_whitespace_mode(&state);
4817 - if (read_stdin)
4818 - errs |= apply_patch(&state, 0, "<stdin>", options);
4819 - if (state.whitespace_error) {
4820 - if (state.squelch_whitespace_errors &&
4821 - state.squelch_whitespace_errors < state.whitespace_error) {
4822 - int squelched =
4823 - state.whitespace_error - state.squelch_whitespace_errors;
4824 - warning(Q_("squelched %d whitespace error",
4825 - "squelched %d whitespace errors",
4826 - squelched),
4827 - squelched);
4828 - }
4829 - if (state.ws_error_action == die_on_ws_error)
4830 - die(Q_("%d line adds whitespace errors.",
4831 - "%d lines add whitespace errors.",
4832 - state.whitespace_error),
4833 - state.whitespace_error);
4834 - if (state.applied_after_fixing_ws && state.apply)
4835 - warning("%d line%s applied after"
4836 - " fixing whitespace errors.",
4837 - state.applied_after_fixing_ws,
4838 - state.applied_after_fixing_ws == 1 ? "" : "s");
4839 - else if (state.whitespace_error)
4840 - warning(Q_("%d line adds whitespace errors.",
4841 - "%d lines add whitespace errors.",
4842 - state.whitespace_error),
4843 - state.whitespace_error);
4844 - }
4845 -
4846 - if (state.update_index) {
4847 - if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
4848 - die(_("Unable to write new index file"));
4849 - }
4862 + ret = apply_all_patches(&state, argc, argv, options);
4863
4864 clear_apply_state(&state);
4865
4853 - return !!errs;
4866 + return ret;
4867 }