Skip to content

Commit 1ffec30

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 10a9ddb commit 1ffec30

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

builtin/apply.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ struct apply_state {
7373
struct string_list limit_by_name;
7474
int has_include;
7575

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+
7684
/* These control whitespace errors */
7785
enum ws_error_action ws_error_action;
7886
enum ws_ignore ws_ignore_action;
@@ -141,13 +149,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
141149
state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
142150
}
143151

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-
151152
/*
152153
* Various "current state", notably line numbers and what
153154
* file (and how) we're patching right now.. The "is_xxxx"
@@ -2172,7 +2173,7 @@ static const char pluses[] =
21722173
static const char minuses[]=
21732174
"----------------------------------------------------------------------";
21742175

2175-
static void show_stats(struct patch *patch)
2176+
static void show_stats(struct apply_state *state, struct patch *patch)
21762177
{
21772178
struct strbuf qname = STRBUF_INIT;
21782179
char *cp = patch->new_name ? patch->new_name : patch->old_name;
@@ -2183,7 +2184,7 @@ static void show_stats(struct patch *patch)
21832184
/*
21842185
* "scale" the filename
21852186
*/
2186-
max = max_len;
2187+
max = state->max_len;
21872188
if (max > 50)
21882189
max = 50;
21892190

@@ -2206,13 +2207,13 @@ static void show_stats(struct patch *patch)
22062207
/*
22072208
* scale the add/delete
22082209
*/
2209-
max = max + max_change > 70 ? 70 - max : max_change;
2210+
max = max + state->max_change > 70 ? 70 - max : state->max_change;
22102211
add = patch->lines_added;
22112212
del = patch->lines_deleted;
22122213

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;
22162217
del = total - add;
22172218
}
22182219
printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
@@ -4038,15 +4039,15 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
40384039
discard_index(&result);
40394040
}
40404041

4041-
static void stat_patch_list(struct patch *patch)
4042+
static void stat_patch_list(struct apply_state *state, struct patch *patch)
40424043
{
40434044
int files, adds, dels;
40444045

40454046
for (files = adds = dels = 0 ; patch ; patch = patch->next) {
40464047
files++;
40474048
adds += patch->lines_added;
40484049
dels += patch->lines_deleted;
4049-
show_stats(patch);
4050+
show_stats(state, patch);
40504051
}
40514052

40524053
print_stat_summary(stdout, files, adds, dels);
@@ -4144,25 +4145,25 @@ static void summary_patch_list(struct patch *patch)
41444145
}
41454146
}
41464147

4147-
static void patch_stats(struct patch *patch)
4148+
static void patch_stats(struct apply_state *state, struct patch *patch)
41484149
{
41494150
int lines = patch->lines_added + patch->lines_deleted;
41504151

4151-
if (lines > max_change)
4152-
max_change = lines;
4152+
if (lines > state->max_change)
4153+
state->max_change = lines;
41534154
if (patch->old_name) {
41544155
int len = quote_c_style(patch->old_name, NULL, NULL, 0);
41554156
if (!len)
41564157
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;
41594160
}
41604161
if (patch->new_name) {
41614162
int len = quote_c_style(patch->new_name, NULL, NULL, 0);
41624163
if (!len)
41634164
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;
41664167
}
41674168
}
41684169

@@ -4519,7 +4520,7 @@ static int apply_patch(struct apply_state *state,
45194520
if (state->apply_in_reverse)
45204521
reverse_patches(patch);
45214522
if (use_patch(state, patch)) {
4522-
patch_stats(patch);
4523+
patch_stats(state, patch);
45234524
*listp = patch;
45244525
listp = &patch->next;
45254526
}
@@ -4563,7 +4564,7 @@ static int apply_patch(struct apply_state *state,
45634564
build_fake_ancestor(list, state->fake_ancestor);
45644565

45654566
if (state->diffstat)
4566-
stat_patch_list(list);
4567+
stat_patch_list(state, list);
45674568

45684569
if (state->numstat)
45694570
numstat_patch_list(state, list);

0 commit comments

Comments
 (0)