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>
This commit is contained in:
Christian Couder 2016-05-24 10:11:19 +02:00 коммит произвёл Junio C Hamano
Родитель 10a9ddba2c
Коммит 1ffec303ab
1 изменённых файлов: 25 добавлений и 24 удалений

Просмотреть файл

@ -73,6 +73,14 @@ struct apply_state {
struct string_list limit_by_name; struct string_list limit_by_name;
int has_include; int has_include;
/*
* For "diff-stat" like behaviour, we keep track of the biggest change
* we've seen, and the longest filename. That allows us to do simple
* scaling.
*/
int max_change;
int max_len;
/* These control whitespace errors */ /* These control whitespace errors */
enum ws_error_action ws_error_action; enum ws_error_action ws_error_action;
enum ws_ignore ws_ignore_action; enum ws_ignore ws_ignore_action;
@ -141,13 +149,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error); state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
} }
/*
* For "diff-stat" like behaviour, we keep track of the biggest change
* we've seen, and the longest filename. That allows us to do simple
* scaling.
*/
static int max_change, max_len;
/* /*
* Various "current state", notably line numbers and what * Various "current state", notably line numbers and what
* file (and how) we're patching right now.. The "is_xxxx" * file (and how) we're patching right now.. The "is_xxxx"
@ -2172,7 +2173,7 @@ static const char pluses[] =
static const char minuses[]= static const char minuses[]=
"----------------------------------------------------------------------"; "----------------------------------------------------------------------";
static void show_stats(struct patch *patch) static void show_stats(struct apply_state *state, struct patch *patch)
{ {
struct strbuf qname = STRBUF_INIT; struct strbuf qname = STRBUF_INIT;
char *cp = patch->new_name ? patch->new_name : patch->old_name; char *cp = patch->new_name ? patch->new_name : patch->old_name;
@ -2183,7 +2184,7 @@ static void show_stats(struct patch *patch)
/* /*
* "scale" the filename * "scale" the filename
*/ */
max = max_len; max = state->max_len;
if (max > 50) if (max > 50)
max = 50; max = 50;
@ -2206,13 +2207,13 @@ static void show_stats(struct patch *patch)
/* /*
* scale the add/delete * scale the add/delete
*/ */
max = max + max_change > 70 ? 70 - max : max_change; max = max + state->max_change > 70 ? 70 - max : state->max_change;
add = patch->lines_added; add = patch->lines_added;
del = patch->lines_deleted; del = patch->lines_deleted;
if (max_change > 0) { if (state->max_change > 0) {
int total = ((add + del) * max + max_change / 2) / max_change; int total = ((add + del) * max + state->max_change / 2) / state->max_change;
add = (add * max + max_change / 2) / max_change; add = (add * max + state->max_change / 2) / state->max_change;
del = total - add; del = total - add;
} }
printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted, 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)
discard_index(&result); discard_index(&result);
} }
static void stat_patch_list(struct patch *patch) static void stat_patch_list(struct apply_state *state, struct patch *patch)
{ {
int files, adds, dels; int files, adds, dels;
@ -4046,7 +4047,7 @@ static void stat_patch_list(struct patch *patch)
files++; files++;
adds += patch->lines_added; adds += patch->lines_added;
dels += patch->lines_deleted; dels += patch->lines_deleted;
show_stats(patch); show_stats(state, patch);
} }
print_stat_summary(stdout, files, adds, dels); print_stat_summary(stdout, files, adds, dels);
@ -4144,25 +4145,25 @@ static void summary_patch_list(struct patch *patch)
} }
} }
static void patch_stats(struct patch *patch) static void patch_stats(struct apply_state *state, struct patch *patch)
{ {
int lines = patch->lines_added + patch->lines_deleted; int lines = patch->lines_added + patch->lines_deleted;
if (lines > max_change) if (lines > state->max_change)
max_change = lines; state->max_change = lines;
if (patch->old_name) { if (patch->old_name) {
int len = quote_c_style(patch->old_name, NULL, NULL, 0); int len = quote_c_style(patch->old_name, NULL, NULL, 0);
if (!len) if (!len)
len = strlen(patch->old_name); len = strlen(patch->old_name);
if (len > max_len) if (len > state->max_len)
max_len = len; state->max_len = len;
} }
if (patch->new_name) { if (patch->new_name) {
int len = quote_c_style(patch->new_name, NULL, NULL, 0); int len = quote_c_style(patch->new_name, NULL, NULL, 0);
if (!len) if (!len)
len = strlen(patch->new_name); len = strlen(patch->new_name);
if (len > max_len) if (len > state->max_len)
max_len = len; state->max_len = len;
} }
} }
@ -4519,7 +4520,7 @@ static int apply_patch(struct apply_state *state,
if (state->apply_in_reverse) if (state->apply_in_reverse)
reverse_patches(patch); reverse_patches(patch);
if (use_patch(state, patch)) { if (use_patch(state, patch)) {
patch_stats(patch); patch_stats(state, patch);
*listp = patch; *listp = patch;
listp = &patch->next; listp = &patch->next;
} }
@ -4563,7 +4564,7 @@ static int apply_patch(struct apply_state *state,
build_fake_ancestor(list, state->fake_ancestor); build_fake_ancestor(list, state->fake_ancestor);
if (state->diffstat) if (state->diffstat)
stat_patch_list(list); stat_patch_list(state, list);
if (state->numstat) if (state->numstat)
numstat_patch_list(state, list); numstat_patch_list(state, list);