builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'

To libify the apply functionality the 'max_change' and 'max_len' variables should not be static and global to the file. Let's move them 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 1ffec303ab043c392e480d37df2b34e40f3d4bd0
1 file changed +25 -24
builtin/apply.c
+25 -24
@@ -73,6 +73,14 @@ struct apply_state {
73 struct string_list limit_by_name;
74 int has_include;
75
76 + /*
77 + * For "diff-stat" like behaviour, we keep track of the biggest change
78 + * we've seen, and the longest filename. That allows us to do simple
79 + * scaling.
80 + */
81 + int max_change;
82 + int max_len;
83 +
84 /* These control whitespace errors */
85 enum ws_error_action ws_error_action;
86 enum ws_ignore ws_ignore_action;
@@ -141,13 +149,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
149 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
150 }
151
144 -/*
145 - * For "diff-stat" like behaviour, we keep track of the biggest change
146 - * we've seen, and the longest filename. That allows us to do simple
147 - * scaling.
148 - */
149 -static int max_change, max_len;
150 -
152 /*
153 * Various "current state", notably line numbers and what
154 * file (and how) we're patching right now.. The "is_xxxx"
@@ -2172,7 +2173,7 @@ static const char pluses[] =
2173 static const char minuses[]=
2174 "----------------------------------------------------------------------";
2175
2175 -static void show_stats(struct patch *patch)
2176 +static void show_stats(struct apply_state *state, struct patch *patch)
2177 {
2178 struct strbuf qname = STRBUF_INIT;
2179 char *cp = patch->new_name ? patch->new_name : patch->old_name;
@@ -2183,7 +2184,7 @@ static void show_stats(struct patch *patch)
2184 /*
2185 * "scale" the filename
2186 */
2186 - max = max_len;
2187 + max = state->max_len;
2188 if (max > 50)
2189 max = 50;
2190
@@ -2206,13 +2207,13 @@ static void show_stats(struct patch *patch)
2207 /*
2208 * scale the add/delete
2209 */
2209 - max = max + max_change > 70 ? 70 - max : max_change;
2210 + max = max + state->max_change > 70 ? 70 - max : state->max_change;
2211 add = patch->lines_added;
2212 del = patch->lines_deleted;
2213
2213 - if (max_change > 0) {
2214 - int total = ((add + del) * max + max_change / 2) / max_change;
2215 - add = (add * max + max_change / 2) / max_change;
2214 + if (state->max_change > 0) {
2215 + int total = ((add + del) * max + state->max_change / 2) / state->max_change;
2216 + add = (add * max + state->max_change / 2) / state->max_change;
2217 del = total - add;
2218 }
2219 printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
@@ -4038,7 +4039,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
4039 discard_index(&result);
4040 }
4041
4041 -static void stat_patch_list(struct patch *patch)
4042 +static void stat_patch_list(struct apply_state *state, struct patch *patch)
4043 {
4044 int files, adds, dels;
4045
@@ -4046,7 +4047,7 @@ static void stat_patch_list(struct patch *patch)
4047 files++;
4048 adds += patch->lines_added;
4049 dels += patch->lines_deleted;
4049 - show_stats(patch);
4050 + show_stats(state, patch);
4051 }
4052
4053 print_stat_summary(stdout, files, adds, dels);
@@ -4144,25 +4145,25 @@ static void summary_patch_list(struct patch *patch)
4145 }
4146 }
4147
4147 -static void patch_stats(struct patch *patch)
4148 +static void patch_stats(struct apply_state *state, struct patch *patch)
4149 {
4150 int lines = patch->lines_added + patch->lines_deleted;
4151
4151 - if (lines > max_change)
4152 - max_change = lines;
4152 + if (lines > state->max_change)
4153 + state->max_change = lines;
4154 if (patch->old_name) {
4155 int len = quote_c_style(patch->old_name, NULL, NULL, 0);
4156 if (!len)
4157 len = strlen(patch->old_name);
4157 - if (len > max_len)
4158 - max_len = len;
4158 + if (len > state->max_len)
4159 + state->max_len = len;
4160 }
4161 if (patch->new_name) {
4162 int len = quote_c_style(patch->new_name, NULL, NULL, 0);
4163 if (!len)
4164 len = strlen(patch->new_name);
4164 - if (len > max_len)
4165 - max_len = len;
4165 + if (len > state->max_len)
4166 + state->max_len = len;
4167 }
4168 }
4169
@@ -4519,7 +4520,7 @@ static int apply_patch(struct apply_state *state,
4520 if (state->apply_in_reverse)
4521 reverse_patches(patch);
4522 if (use_patch(state, patch)) {
4522 - patch_stats(patch);
4523 + patch_stats(state, patch);
4524 *listp = patch;
4525 listp = &patch->next;
4526 }
@@ -4563,7 +4564,7 @@ static int apply_patch(struct apply_state *state,
4564 build_fake_ancestor(list, state->fake_ancestor);
4565
4566 if (state->diffstat)
4566 - stat_patch_list(list);
4567 + stat_patch_list(state, list);
4568
4569 if (state->numstat)
4570 numstat_patch_list(state, list);