Skip to content

Commit 80e1841

Browse files
tgummerergitster
authored andcommitted
apply: only pass required data to gitdiff_* functions
Currently the 'gitdiff_*()' functions take 'struct apply_state' as parameter, even though they only needs the root, linenr and p_value from that struct. These functions are in the callchain of 'parse_git_header()', which we want to make more generally useful in a subsequent commit. To make that happen we only want to pass in the required data to 'parse_git_header()', and not the whole 'struct apply_state', and thus we want functions in the callchain of 'parse_git_header()' to only take arguments they really need. As these functions are called in a loop using their function pointers, each function needs to be passed all the parameters even if only one of the functions actually needs it. We therefore pass this data along in a struct to avoid adding too many unused parameters to each function and making the code very verbose in the process. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 877a833 commit 80e1841

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

apply.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
#include "rerere.h"
2323
#include "apply.h"
2424

25+
struct gitdiff_data {
26+
struct strbuf *root;
27+
int linenr;
28+
int p_value;
29+
};
30+
2531
static void git_apply_config(void)
2632
{
2733
git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
@@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
914920
return 0;
915921
}
916922

917-
static int gitdiff_hdrend(struct apply_state *state,
923+
static int gitdiff_hdrend(struct gitdiff_data *state,
918924
const char *line,
919925
struct patch *patch)
920926
{
@@ -933,14 +939,14 @@ static int gitdiff_hdrend(struct apply_state *state,
933939
#define DIFF_OLD_NAME 0
934940
#define DIFF_NEW_NAME 1
935941

936-
static int gitdiff_verify_name(struct apply_state *state,
942+
static int gitdiff_verify_name(struct gitdiff_data *state,
937943
const char *line,
938944
int isnull,
939945
char **name,
940946
int side)
941947
{
942948
if (!*name && !isnull) {
943-
*name = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
949+
*name = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
944950
return 0;
945951
}
946952

@@ -949,7 +955,7 @@ static int gitdiff_verify_name(struct apply_state *state,
949955
if (isnull)
950956
return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
951957
*name, state->linenr);
952-
another = find_name(&state->root, line, NULL, state->p_value, TERM_TAB);
958+
another = find_name(state->root, line, NULL, state->p_value, TERM_TAB);
953959
if (!another || strcmp(another, *name)) {
954960
free(another);
955961
return error((side == DIFF_NEW_NAME) ?
@@ -965,7 +971,7 @@ static int gitdiff_verify_name(struct apply_state *state,
965971
return 0;
966972
}
967973

968-
static int gitdiff_oldname(struct apply_state *state,
974+
static int gitdiff_oldname(struct gitdiff_data *state,
969975
const char *line,
970976
struct patch *patch)
971977
{
@@ -974,7 +980,7 @@ static int gitdiff_oldname(struct apply_state *state,
974980
DIFF_OLD_NAME);
975981
}
976982

977-
static int gitdiff_newname(struct apply_state *state,
983+
static int gitdiff_newname(struct gitdiff_data *state,
978984
const char *line,
979985
struct patch *patch)
980986
{
@@ -992,21 +998,21 @@ static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
992998
return 0;
993999
}
9941000

995-
static int gitdiff_oldmode(struct apply_state *state,
1001+
static int gitdiff_oldmode(struct gitdiff_data *state,
9961002
const char *line,
9971003
struct patch *patch)
9981004
{
9991005
return parse_mode_line(line, state->linenr, &patch->old_mode);
10001006
}
10011007

1002-
static int gitdiff_newmode(struct apply_state *state,
1008+
static int gitdiff_newmode(struct gitdiff_data *state,
10031009
const char *line,
10041010
struct patch *patch)
10051011
{
10061012
return parse_mode_line(line, state->linenr, &patch->new_mode);
10071013
}
10081014

1009-
static int gitdiff_delete(struct apply_state *state,
1015+
static int gitdiff_delete(struct gitdiff_data *state,
10101016
const char *line,
10111017
struct patch *patch)
10121018
{
@@ -1016,7 +1022,7 @@ static int gitdiff_delete(struct apply_state *state,
10161022
return gitdiff_oldmode(state, line, patch);
10171023
}
10181024

1019-
static int gitdiff_newfile(struct apply_state *state,
1025+
static int gitdiff_newfile(struct gitdiff_data *state,
10201026
const char *line,
10211027
struct patch *patch)
10221028
{
@@ -1026,47 +1032,47 @@ static int gitdiff_newfile(struct apply_state *state,
10261032
return gitdiff_newmode(state, line, patch);
10271033
}
10281034

1029-
static int gitdiff_copysrc(struct apply_state *state,
1035+
static int gitdiff_copysrc(struct gitdiff_data *state,
10301036
const char *line,
10311037
struct patch *patch)
10321038
{
10331039
patch->is_copy = 1;
10341040
free(patch->old_name);
1035-
patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1041+
patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
10361042
return 0;
10371043
}
10381044

1039-
static int gitdiff_copydst(struct apply_state *state,
1045+
static int gitdiff_copydst(struct gitdiff_data *state,
10401046
const char *line,
10411047
struct patch *patch)
10421048
{
10431049
patch->is_copy = 1;
10441050
free(patch->new_name);
1045-
patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1051+
patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
10461052
return 0;
10471053
}
10481054

1049-
static int gitdiff_renamesrc(struct apply_state *state,
1055+
static int gitdiff_renamesrc(struct gitdiff_data *state,
10501056
const char *line,
10511057
struct patch *patch)
10521058
{
10531059
patch->is_rename = 1;
10541060
free(patch->old_name);
1055-
patch->old_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1061+
patch->old_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
10561062
return 0;
10571063
}
10581064

1059-
static int gitdiff_renamedst(struct apply_state *state,
1065+
static int gitdiff_renamedst(struct gitdiff_data *state,
10601066
const char *line,
10611067
struct patch *patch)
10621068
{
10631069
patch->is_rename = 1;
10641070
free(patch->new_name);
1065-
patch->new_name = find_name(&state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
1071+
patch->new_name = find_name(state->root, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
10661072
return 0;
10671073
}
10681074

1069-
static int gitdiff_similarity(struct apply_state *state,
1075+
static int gitdiff_similarity(struct gitdiff_data *state,
10701076
const char *line,
10711077
struct patch *patch)
10721078
{
@@ -1076,7 +1082,7 @@ static int gitdiff_similarity(struct apply_state *state,
10761082
return 0;
10771083
}
10781084

1079-
static int gitdiff_dissimilarity(struct apply_state *state,
1085+
static int gitdiff_dissimilarity(struct gitdiff_data *state,
10801086
const char *line,
10811087
struct patch *patch)
10821088
{
@@ -1086,7 +1092,7 @@ static int gitdiff_dissimilarity(struct apply_state *state,
10861092
return 0;
10871093
}
10881094

1089-
static int gitdiff_index(struct apply_state *state,
1095+
static int gitdiff_index(struct gitdiff_data *state,
10901096
const char *line,
10911097
struct patch *patch)
10921098
{
@@ -1126,7 +1132,7 @@ static int gitdiff_index(struct apply_state *state,
11261132
* This is normal for a diff that doesn't change anything: we'll fall through
11271133
* into the next diff. Tell the parser to break out.
11281134
*/
1129-
static int gitdiff_unrecognized(struct apply_state *state,
1135+
static int gitdiff_unrecognized(struct gitdiff_data *state,
11301136
const char *line,
11311137
struct patch *patch)
11321138
{
@@ -1322,6 +1328,7 @@ static int parse_git_header(struct apply_state *state,
13221328
struct patch *patch)
13231329
{
13241330
unsigned long offset;
1331+
struct gitdiff_data parse_hdr_state;
13251332

13261333
/* A git diff has explicit new/delete information, so we don't guess */
13271334
patch->is_new = 0;
@@ -1343,10 +1350,14 @@ static int parse_git_header(struct apply_state *state,
13431350
line += len;
13441351
size -= len;
13451352
state->linenr++;
1353+
parse_hdr_state.root = &state->root;
1354+
parse_hdr_state.linenr = state->linenr;
1355+
parse_hdr_state.p_value = state->p_value;
1356+
13461357
for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
13471358
static const struct opentry {
13481359
const char *str;
1349-
int (*fn)(struct apply_state *, const char *, struct patch *);
1360+
int (*fn)(struct gitdiff_data *, const char *, struct patch *);
13501361
} optable[] = {
13511362
{ "@@ -", gitdiff_hdrend },
13521363
{ "--- ", gitdiff_oldname },
@@ -1377,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
13771388
int res;
13781389
if (len < oplen || memcmp(p->str, line, oplen))
13791390
continue;
1380-
res = p->fn(state, line + oplen, patch);
1391+
res = p->fn(&parse_hdr_state, line + oplen, patch);
13811392
if (res < 0)
13821393
return -1;
13831394
if (check_header_line(state->linenr, patch))

0 commit comments

Comments
 (0)