Skip to content

Commit c84a86c

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 2f63cea commit c84a86c

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

builtin/apply.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,11 +4679,38 @@ static void clear_apply_state(struct apply_state *state)
46794679
/* &state->fn_table is cleared at the end of apply_patch() */
46804680
}
46814681

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+
46824710
int cmd_apply(int argc, const char **argv, const char *prefix)
46834711
{
46844712
int i;
46854713
int errs = 0;
4686-
int is_not_gitdir = !startup_info->have_repository;
46874714
int force_apply = 0;
46884715
int options = 0;
46894716
int read_stdin = 1;
@@ -4763,28 +4790,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
47634790
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
47644791
apply_usage, 0);
47654792

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);
47884794

47894795
for (i = 0; i < argc; i++) {
47904796
const char *arg = argv[i];

0 commit comments

Comments
 (0)