builtin/apply: move 'apply' global into 'struct apply_state'

To libify the apply functionality the 'apply' variable should not be static and global to the file. Let's move it into 'struct apply_state'. 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 574f5a59d85153309ea9856f425ca610074e1116
1 file changed +17 -14
builtin/apply.c
+17 -14
@@ -26,6 +26,7 @@ struct apply_state {
26 int prefix_length;
27
28 /* These control what gets looked at and modified */
29 + int apply; /* this is not a dry-run */
30 int cached; /* apply to the index only */
31 int check; /* preimage must match working tree, don't actually apply */
32 int check_index; /* preimage must match the indexed version */
@@ -56,7 +57,7 @@ static int newfd = -1;
57
58 static int state_p_value = 1;
59 static int p_value_known;
59 -static int apply = 1;
60 +
61 static const char * const apply_usage[] = {
62 N_("git apply [<options>] [<patch>...]"),
63 NULL
@@ -126,10 +127,11 @@ static void parse_ignorewhitespace_option(const char *option)
127 die(_("unrecognized whitespace ignore option '%s'"), option);
128 }
129
129 -static void set_default_whitespace_mode(const char *whitespace_option)
130 +static void set_default_whitespace_mode(struct apply_state *state,
131 + const char *whitespace_option)
132 {
133 if (!whitespace_option && !apply_default_whitespace)
132 - ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
134 + ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
135 }
136
137 /*
@@ -2058,7 +2060,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2060 * without metadata change. A binary patch appears
2061 * empty to us here.
2062 */
2061 - if ((apply || state->check) &&
2063 + if ((state->apply || state->check) &&
2064 (!patch->is_binary && !metadata_changes(patch)))
2065 die(_("patch with only garbage at line %d"), state_linenr);
2066 }
@@ -2916,7 +2918,7 @@ static int apply_one_fragment(struct apply_state *state,
2918 * apply_data->apply_fragments->apply_one_fragment
2919 */
2920 if (ws_error_action == die_on_ws_error)
2919 - apply = 0;
2921 + state->apply = 0;
2922 }
2923
2924 if (state->apply_verbosely && applied_pos != pos) {
@@ -4460,9 +4462,9 @@ static int apply_patch(struct apply_state *state,
4462 die(_("unrecognized input"));
4463
4464 if (whitespace_error && (ws_error_action == die_on_ws_error))
4463 - apply = 0;
4465 + state->apply = 0;
4466
4465 - state->update_index = state->check_index && apply;
4467 + state->update_index = state->check_index && state->apply;
4468 if (state->update_index && newfd < 0)
4469 newfd = hold_locked_index(&lock_file, 1);
4470
@@ -4471,12 +4473,12 @@ static int apply_patch(struct apply_state *state,
4473 die(_("unable to read index file"));
4474 }
4475
4474 - if ((state->check || apply) &&
4476 + if ((state->check || state->apply) &&
4477 check_patch_list(state, list) < 0 &&
4478 !state->apply_with_reject)
4479 exit(1);
4480
4479 - if (apply && write_out_results(state, list)) {
4481 + if (state->apply && write_out_results(state, list)) {
4482 if (state->apply_with_reject)
4483 exit(1);
4484 /* with --3way, we still need to write the index out */
@@ -4565,6 +4567,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4567 memset(state, 0, sizeof(*state));
4568 state->prefix = prefix;
4569 state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
4570 + state->apply = 1;
4571 state->line_termination = '\n';
4572 state->p_context = UINT_MAX;
4573
@@ -4676,9 +4679,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4679 state.check_index = 1;
4680 }
4681 if (state.apply_with_reject)
4679 - apply = state.apply_verbosely = 1;
4682 + state.apply = state.apply_verbosely = 1;
4683 if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))
4681 - apply = 0;
4684 + state.apply = 0;
4685 if (state.check_index && is_not_gitdir)
4686 die(_("--index outside a repository"));
4687 if (state.cached) {
@@ -4706,11 +4709,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4709 if (fd < 0)
4710 die_errno(_("can't open patch '%s'"), arg);
4711 read_stdin = 0;
4709 - set_default_whitespace_mode(whitespace_option);
4712 + set_default_whitespace_mode(&state, whitespace_option);
4713 errs |= apply_patch(&state, fd, arg, options);
4714 close(fd);
4715 }
4713 - set_default_whitespace_mode(whitespace_option);
4716 + set_default_whitespace_mode(&state, whitespace_option);
4717 if (read_stdin)
4718 errs |= apply_patch(&state, 0, "<stdin>", options);
4719 if (whitespace_error) {
@@ -4728,7 +4731,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4731 "%d lines add whitespace errors.",
4732 whitespace_error),
4733 whitespace_error);
4731 - if (applied_after_fixing_ws && apply)
4734 + if (applied_after_fixing_ws && state.apply)
4735 warning("%d line%s applied after"
4736 " fixing whitespace errors.",
4737 applied_after_fixing_ws,