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

To libify the apply functionality the 'ws_ignore_action' 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 10a9ddba2c3478c8080acc1b2c83d75d09ee2492
1 file changed +20 -17
builtin/apply.c
+20 -17
@@ -28,6 +28,12 @@ enum ws_error_action {
28 correct_ws_error
29 };
30
31 +
32 +enum ws_ignore {
33 + ignore_ws_none,
34 + ignore_ws_change
35 +};
36 +
37 struct apply_state {
38 const char *prefix;
39 int prefix_length;
@@ -69,6 +75,7 @@ struct apply_state {
75
76 /* These control whitespace errors */
77 enum ws_error_action ws_error_action;
78 + enum ws_ignore ws_ignore_action;
79 const char *whitespace_option;
80 int whitespace_error;
81 int squelch_whitespace_errors;
@@ -82,13 +89,6 @@ static const char * const apply_usage[] = {
89 NULL
90 };
91
85 -
86 -static enum ws_ignore {
87 - ignore_ws_none,
88 - ignore_ws_change
89 -} ws_ignore_action = ignore_ws_none;
90 -
91 -
92 static void parse_whitespace_option(struct apply_state *state, const char *option)
93 {
94 if (!option) {
@@ -119,16 +119,17 @@ static void parse_whitespace_option(struct apply_state *state, const char *optio
119 die(_("unrecognized whitespace option '%s'"), option);
120 }
121
122 -static void parse_ignorewhitespace_option(const char *option)
122 +static void parse_ignorewhitespace_option(struct apply_state *state,
123 + const char *option)
124 {
125 if (!option || !strcmp(option, "no") ||
126 !strcmp(option, "false") || !strcmp(option, "never") ||
127 !strcmp(option, "none")) {
127 - ws_ignore_action = ignore_ws_none;
128 + state->ws_ignore_action = ignore_ws_none;
129 return;
130 }
131 if (!strcmp(option, "change")) {
131 - ws_ignore_action = ignore_ws_change;
132 + state->ws_ignore_action = ignore_ws_change;
133 return;
134 }
135 die(_("unrecognized whitespace ignore option '%s'"), option);
@@ -2488,7 +2489,7 @@ static int match_fragment(struct apply_state *state,
2489 * fuzzy matching. We collect all the line length information because
2490 * we need it to adjust whitespace if we match.
2491 */
2491 - if (ws_ignore_action == ignore_ws_change)
2492 + if (state->ws_ignore_action == ignore_ws_change)
2493 return line_by_line_fuzzy_match(img, preimage, postimage,
2494 try, try_lno, preimage_limit);
2495
@@ -4611,12 +4612,13 @@ static int option_parse_p(const struct option *opt,
4612 }
4613
4614 static int option_parse_space_change(const struct option *opt,
4614 - const char *arg, int unset)
4615 + const char *arg, int unset)
4616 {
4617 + struct apply_state *state = opt->value;
4618 if (unset)
4617 - ws_ignore_action = ignore_ws_none;
4619 + state->ws_ignore_action = ignore_ws_none;
4620 else
4619 - ws_ignore_action = ignore_ws_change;
4621 + state->ws_ignore_action = ignore_ws_change;
4622 return 0;
4623 }
4624
@@ -4650,13 +4652,14 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4652 state->p_context = UINT_MAX;
4653 state->squelch_whitespace_errors = 5;
4654 state->ws_error_action = warn_on_ws_error;
4655 + state->ws_ignore_action = ignore_ws_none;
4656 strbuf_init(&state->root, 0);
4657
4658 git_apply_config();
4659 if (apply_default_whitespace)
4660 parse_whitespace_option(state, apply_default_whitespace);
4661 if (apply_default_ignorewhitespace)
4659 - parse_ignorewhitespace_option(apply_default_ignorewhitespace);
4662 + parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
4663 }
4664
4665 static void clear_apply_state(struct apply_state *state)
@@ -4717,10 +4720,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4720 { OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
4721 N_("detect new or modified lines that have whitespace errors"),
4722 0, option_parse_whitespace },
4720 - { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
4723 + { OPTION_CALLBACK, 0, "ignore-space-change", &state, NULL,
4724 N_("ignore changes in whitespace when finding context"),
4725 PARSE_OPT_NOARG, option_parse_space_change },
4723 - { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
4726 + { OPTION_CALLBACK, 0, "ignore-whitespace", &state, NULL,
4727 N_("ignore changes in whitespace when finding context"),
4728 PARSE_OPT_NOARG, option_parse_space_change },
4729 OPT_BOOL('R', "reverse", &state.apply_in_reverse,