builtin/apply: move 'state' check into check_apply_state()

To libify the apply functionality we should provide a function to check that the values in a 'struct apply_state' instance are coherent. Let's move the code to do that into a new check_apply_state() 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 c84a86c99526b88cfd7664888ecbfd28f35a025f
1 file changed +29 -23
builtin/apply.c
+29 -23
@@ -4679,11 +4679,38 @@ static void clear_apply_state(struct apply_state *state)
4679 /* &state->fn_table is cleared at the end of apply_patch() */
4680 }
4681
4682 +static void check_apply_state(struct apply_state *state, int force_apply)
4683 +{
4684 + int is_not_gitdir = !startup_info->have_repository;
4685 +
4686 + if (state->apply_with_reject && state->threeway)
4687 + die("--reject and --3way cannot be used together.");
4688 + if (state->cached && state->threeway)
4689 + die("--cached and --3way cannot be used together.");
4690 + if (state->threeway) {
4691 + if (is_not_gitdir)
4692 + die(_("--3way outside a repository"));
4693 + state->check_index = 1;
4694 + }
4695 + if (state->apply_with_reject)
4696 + state->apply = state->apply_verbosely = 1;
4697 + if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))
4698 + state->apply = 0;
4699 + if (state->check_index && is_not_gitdir)
4700 + die(_("--index outside a repository"));
4701 + if (state->cached) {
4702 + if (is_not_gitdir)
4703 + die(_("--cached outside a repository"));
4704 + state->check_index = 1;
4705 + }
4706 + if (state->check_index)
4707 + state->unsafe_paths = 0;
4708 +}
4709 +
4710 int cmd_apply(int argc, const char **argv, const char *prefix)
4711 {
4712 int i;
4713 int errs = 0;
4686 - int is_not_gitdir = !startup_info->have_repository;
4714 int force_apply = 0;
4715 int options = 0;
4716 int read_stdin = 1;
@@ -4763,28 +4790,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4790 argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
4791 apply_usage, 0);
4792
4766 - if (state.apply_with_reject && state.threeway)
4767 - die("--reject and --3way cannot be used together.");
4768 - if (state.cached && state.threeway)
4769 - die("--cached and --3way cannot be used together.");
4770 - if (state.threeway) {
4771 - if (is_not_gitdir)
4772 - die(_("--3way outside a repository"));
4773 - state.check_index = 1;
4774 - }
4775 - if (state.apply_with_reject)
4776 - state.apply = state.apply_verbosely = 1;
4777 - if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))
4778 - state.apply = 0;
4779 - if (state.check_index && is_not_gitdir)
4780 - die(_("--index outside a repository"));
4781 - if (state.cached) {
4782 - if (is_not_gitdir)
4783 - die(_("--cached outside a repository"));
4784 - state.check_index = 1;
4785 - }
4786 - if (state.check_index)
4787 - state.unsafe_paths = 0;
4793 + check_apply_state(&state, force_apply);
4794
4795 for (i = 0; i < argc; i++) {
4796 const char *arg = argv[i];