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

To libify the apply functionality the 'ws_error_action' variable should not be static and global to the file. Let's move it into 'struct apply_state'. 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 e9c6b279b8226b2ab93a6a550a654640ccc5b256
1 file changed +33 -28
builtin/apply.c
+33 -28
@@ -21,6 +21,13 @@
21 #include "ll-merge.h"
22 #include "rerere.h"
23
24 +enum ws_error_action {
25 + nowarn_ws_error,
26 + warn_on_ws_error,
27 + die_on_ws_error,
28 + correct_ws_error
29 +};
30 +
31 struct apply_state {
32 const char *prefix;
33 int prefix_length;
@@ -61,6 +68,7 @@ struct apply_state {
68 int has_include;
69
70 /* These control whitespace errors */
71 + enum ws_error_action ws_error_action;
72 const char *whitespace_option;
73 int whitespace_error;
74 int squelch_whitespace_errors;
@@ -74,12 +82,6 @@ static const char * const apply_usage[] = {
82 NULL
83 };
84
77 -static enum ws_error_action {
78 - nowarn_ws_error,
79 - warn_on_ws_error,
80 - die_on_ws_error,
81 - correct_ws_error
82 -} ws_error_action = warn_on_ws_error;
85
86 static enum ws_ignore {
87 ignore_ws_none,
@@ -90,28 +92,28 @@ static enum ws_ignore {
92 static void parse_whitespace_option(struct apply_state *state, const char *option)
93 {
94 if (!option) {
93 - ws_error_action = warn_on_ws_error;
95 + state->ws_error_action = warn_on_ws_error;
96 return;
97 }
98 if (!strcmp(option, "warn")) {
97 - ws_error_action = warn_on_ws_error;
99 + state->ws_error_action = warn_on_ws_error;
100 return;
101 }
102 if (!strcmp(option, "nowarn")) {
101 - ws_error_action = nowarn_ws_error;
103 + state->ws_error_action = nowarn_ws_error;
104 return;
105 }
106 if (!strcmp(option, "error")) {
105 - ws_error_action = die_on_ws_error;
107 + state->ws_error_action = die_on_ws_error;
108 return;
109 }
110 if (!strcmp(option, "error-all")) {
109 - ws_error_action = die_on_ws_error;
111 + state->ws_error_action = die_on_ws_error;
112 state->squelch_whitespace_errors = 0;
113 return;
114 }
115 if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
114 - ws_error_action = correct_ws_error;
116 + state->ws_error_action = correct_ws_error;
117 return;
118 }
119 die(_("unrecognized whitespace option '%s'"), option);
@@ -135,7 +137,7 @@ static void parse_ignorewhitespace_option(const char *option)
137 static void set_default_whitespace_mode(struct apply_state *state)
138 {
139 if (!state->whitespace_option && !apply_default_whitespace)
138 - ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
140 + state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
141 }
142
143 /*
@@ -1670,12 +1672,12 @@ static int parse_fragment(struct apply_state *state,
1672 leading++;
1673 trailing++;
1674 if (!state->apply_in_reverse &&
1673 - ws_error_action == correct_ws_error)
1675 + state->ws_error_action == correct_ws_error)
1676 check_whitespace(state, line, len, patch->ws_rule);
1677 break;
1678 case '-':
1679 if (state->apply_in_reverse &&
1678 - ws_error_action != nowarn_ws_error)
1680 + state->ws_error_action != nowarn_ws_error)
1681 check_whitespace(state, line, len, patch->ws_rule);
1682 deleted++;
1683 oldlines--;
@@ -1683,7 +1685,7 @@ static int parse_fragment(struct apply_state *state,
1685 break;
1686 case '+':
1687 if (!state->apply_in_reverse &&
1686 - ws_error_action != nowarn_ws_error)
1688 + state->ws_error_action != nowarn_ws_error)
1689 check_whitespace(state, line, len, patch->ws_rule);
1690 added++;
1691 newlines--;
@@ -2396,7 +2398,8 @@ static int line_by_line_fuzzy_match(struct image *img,
2398 return 1;
2399 }
2400
2399 -static int match_fragment(struct image *img,
2401 +static int match_fragment(struct apply_state *state,
2402 + struct image *img,
2403 struct image *preimage,
2404 struct image *postimage,
2405 unsigned long try,
@@ -2417,7 +2420,7 @@ static int match_fragment(struct image *img,
2420 preimage_limit = preimage->nr;
2421 if (match_end && (preimage->nr + try_lno != img->nr))
2422 return 0;
2420 - } else if (ws_error_action == correct_ws_error &&
2423 + } else if (state->ws_error_action == correct_ws_error &&
2424 (ws_rule & WS_BLANK_AT_EOF)) {
2425 /*
2426 * This hunk extends beyond the end of img, and we are
@@ -2489,7 +2492,7 @@ static int match_fragment(struct image *img,
2492 return line_by_line_fuzzy_match(img, preimage, postimage,
2493 try, try_lno, preimage_limit);
2494
2492 - if (ws_error_action != correct_ws_error)
2495 + if (state->ws_error_action != correct_ws_error)
2496 return 0;
2497
2498 /*
@@ -2601,7 +2604,8 @@ static int match_fragment(struct image *img,
2604 return 0;
2605 }
2606
2604 -static int find_pos(struct image *img,
2607 +static int find_pos(struct apply_state *state,
2608 + struct image *img,
2609 struct image *preimage,
2610 struct image *postimage,
2611 int line,
@@ -2645,7 +2649,7 @@ static int find_pos(struct image *img,
2649 try_lno = line;
2650
2651 for (i = 0; ; i++) {
2648 - if (match_fragment(img, preimage, postimage,
2652 + if (match_fragment(state, img, preimage, postimage,
2653 try, try_lno, ws_rule,
2654 match_beginning, match_end))
2655 return try_lno;
@@ -2858,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,
2862 start = newlines.len;
2863 if (first != '+' ||
2864 !state->whitespace_error ||
2861 - ws_error_action != correct_ws_error) {
2865 + state->ws_error_action != correct_ws_error) {
2866 strbuf_add(&newlines, patch + 1, plen);
2867 }
2868 else {
@@ -2936,7 +2940,7 @@ static int apply_one_fragment(struct apply_state *state,
2940
2941 for (;;) {
2942
2939 - applied_pos = find_pos(img, &preimage, &postimage, pos,
2943 + applied_pos = find_pos(state, img, &preimage, &postimage, pos,
2944 ws_rule, match_beginning, match_end);
2945
2946 if (applied_pos >= 0)
@@ -2972,10 +2976,10 @@ static int apply_one_fragment(struct apply_state *state,
2976 if (new_blank_lines_at_end &&
2977 preimage.nr + applied_pos >= img->nr &&
2978 (ws_rule & WS_BLANK_AT_EOF) &&
2975 - ws_error_action != nowarn_ws_error) {
2979 + state->ws_error_action != nowarn_ws_error) {
2980 record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2981 found_new_blank_lines_at_end);
2978 - if (ws_error_action == correct_ws_error) {
2982 + if (state->ws_error_action == correct_ws_error) {
2983 while (new_blank_lines_at_end--)
2984 remove_last_line(&postimage);
2985 }
@@ -2986,7 +2990,7 @@ static int apply_one_fragment(struct apply_state *state,
2990 * apply_patch->check_patch_list->check_patch->
2991 * apply_data->apply_fragments->apply_one_fragment
2992 */
2989 - if (ws_error_action == die_on_ws_error)
2993 + if (state->ws_error_action == die_on_ws_error)
2994 state->apply = 0;
2995 }
2996
@@ -4530,7 +4534,7 @@ static int apply_patch(struct apply_state *state,
4534 if (!list && !skipped_patch)
4535 die(_("unrecognized input"));
4536
4533 - if (state->whitespace_error && (ws_error_action == die_on_ws_error))
4537 + if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
4538 state->apply = 0;
4539
4540 state->update_index = state->check_index && state->apply;
@@ -4645,6 +4649,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4649 state->p_value = 1;
4650 state->p_context = UINT_MAX;
4651 state->squelch_whitespace_errors = 5;
4652 + state->ws_error_action = warn_on_ws_error;
4653 strbuf_init(&state->root, 0);
4654
4655 git_apply_config();
@@ -4801,7 +4806,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4806 squelched),
4807 squelched);
4808 }
4804 - if (ws_error_action == die_on_ws_error)
4809 + if (state.ws_error_action == die_on_ws_error)
4810 die(Q_("%d line adds whitespace errors.",
4811 "%d lines add whitespace errors.",
4812 state.whitespace_error),