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 <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Jul 11, 2019 at 17:08 UTC 80e184123202761a4be32be12be12f7ff469fe5b
1 file changed +35 -24
apply.c
+35 -24
@@ -22,6 +22,12 @@
22 #include "rerere.h"
23 #include "apply.h"
24
25 +struct gitdiff_data {
26 + struct strbuf *root;
27 + int linenr;
28 + int p_value;
29 +};
30 +
31 static void git_apply_config(void)
32 {
33 git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
@@ -914,7 +920,7 @@ static int parse_traditional_patch(struct apply_state *state,
920 return 0;
921 }
922
917 -static int gitdiff_hdrend(struct apply_state *state,
923 +static int gitdiff_hdrend(struct gitdiff_data *state,
924 const char *line,
925 struct patch *patch)
926 {
@@ -933,14 +939,14 @@ static int gitdiff_hdrend(struct apply_state *state,
939 #define DIFF_OLD_NAME 0
940 #define DIFF_NEW_NAME 1
941
936 -static int gitdiff_verify_name(struct apply_state *state,
942 +static int gitdiff_verify_name(struct gitdiff_data *state,
943 const char *line,
944 int isnull,
945 char **name,
946 int side)
947 {
948 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);
950 return 0;
951 }
952
@@ -949,7 +955,7 @@ static int gitdiff_verify_name(struct apply_state *state,
955 if (isnull)
956 return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
957 *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);
959 if (!another || strcmp(another, *name)) {
960 free(another);
961 return error((side == DIFF_NEW_NAME) ?
@@ -965,7 +971,7 @@ static int gitdiff_verify_name(struct apply_state *state,
971 return 0;
972 }
973
968 -static int gitdiff_oldname(struct apply_state *state,
974 +static int gitdiff_oldname(struct gitdiff_data *state,
975 const char *line,
976 struct patch *patch)
977 {
@@ -974,7 +980,7 @@ static int gitdiff_oldname(struct apply_state *state,
980 DIFF_OLD_NAME);
981 }
982
977 -static int gitdiff_newname(struct apply_state *state,
983 +static int gitdiff_newname(struct gitdiff_data *state,
984 const char *line,
985 struct patch *patch)
986 {
@@ -992,21 +998,21 @@ static int parse_mode_line(const char *line, int linenr, unsigned int *mode)
998 return 0;
999 }
1000
995 -static int gitdiff_oldmode(struct apply_state *state,
1001 +static int gitdiff_oldmode(struct gitdiff_data *state,
1002 const char *line,
1003 struct patch *patch)
1004 {
1005 return parse_mode_line(line, state->linenr, &patch->old_mode);
1006 }
1007
1002 -static int gitdiff_newmode(struct apply_state *state,
1008 +static int gitdiff_newmode(struct gitdiff_data *state,
1009 const char *line,
1010 struct patch *patch)
1011 {
1012 return parse_mode_line(line, state->linenr, &patch->new_mode);
1013 }
1014
1009 -static int gitdiff_delete(struct apply_state *state,
1015 +static int gitdiff_delete(struct gitdiff_data *state,
1016 const char *line,
1017 struct patch *patch)
1018 {
@@ -1016,7 +1022,7 @@ static int gitdiff_delete(struct apply_state *state,
1022 return gitdiff_oldmode(state, line, patch);
1023 }
1024
1019 -static int gitdiff_newfile(struct apply_state *state,
1025 +static int gitdiff_newfile(struct gitdiff_data *state,
1026 const char *line,
1027 struct patch *patch)
1028 {
@@ -1026,47 +1032,47 @@ static int gitdiff_newfile(struct apply_state *state,
1032 return gitdiff_newmode(state, line, patch);
1033 }
1034
1029 -static int gitdiff_copysrc(struct apply_state *state,
1035 +static int gitdiff_copysrc(struct gitdiff_data *state,
1036 const char *line,
1037 struct patch *patch)
1038 {
1039 patch->is_copy = 1;
1040 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);
1042 return 0;
1043 }
1044
1039 -static int gitdiff_copydst(struct apply_state *state,
1045 +static int gitdiff_copydst(struct gitdiff_data *state,
1046 const char *line,
1047 struct patch *patch)
1048 {
1049 patch->is_copy = 1;
1050 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);
1052 return 0;
1053 }
1054
1049 -static int gitdiff_renamesrc(struct apply_state *state,
1055 +static int gitdiff_renamesrc(struct gitdiff_data *state,
1056 const char *line,
1057 struct patch *patch)
1058 {
1059 patch->is_rename = 1;
1060 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);
1062 return 0;
1063 }
1064
1059 -static int gitdiff_renamedst(struct apply_state *state,
1065 +static int gitdiff_renamedst(struct gitdiff_data *state,
1066 const char *line,
1067 struct patch *patch)
1068 {
1069 patch->is_rename = 1;
1070 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);
1072 return 0;
1073 }
1074
1069 -static int gitdiff_similarity(struct apply_state *state,
1075 +static int gitdiff_similarity(struct gitdiff_data *state,
1076 const char *line,
1077 struct patch *patch)
1078 {
@@ -1076,7 +1082,7 @@ static int gitdiff_similarity(struct apply_state *state,
1082 return 0;
1083 }
1084
1079 -static int gitdiff_dissimilarity(struct apply_state *state,
1085 +static int gitdiff_dissimilarity(struct gitdiff_data *state,
1086 const char *line,
1087 struct patch *patch)
1088 {
@@ -1086,7 +1092,7 @@ static int gitdiff_dissimilarity(struct apply_state *state,
1092 return 0;
1093 }
1094
1089 -static int gitdiff_index(struct apply_state *state,
1095 +static int gitdiff_index(struct gitdiff_data *state,
1096 const char *line,
1097 struct patch *patch)
1098 {
@@ -1126,7 +1132,7 @@ static int gitdiff_index(struct apply_state *state,
1132 * This is normal for a diff that doesn't change anything: we'll fall through
1133 * into the next diff. Tell the parser to break out.
1134 */
1129 -static int gitdiff_unrecognized(struct apply_state *state,
1135 +static int gitdiff_unrecognized(struct gitdiff_data *state,
1136 const char *line,
1137 struct patch *patch)
1138 {
@@ -1322,6 +1328,7 @@ static int parse_git_header(struct apply_state *state,
1328 struct patch *patch)
1329 {
1330 unsigned long offset;
1331 + struct gitdiff_data parse_hdr_state;
1332
1333 /* A git diff has explicit new/delete information, so we don't guess */
1334 patch->is_new = 0;
@@ -1343,10 +1350,14 @@ static int parse_git_header(struct apply_state *state,
1350 line += len;
1351 size -= len;
1352 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 +
1357 for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1358 static const struct opentry {
1359 const char *str;
1349 - int (*fn)(struct apply_state *, const char *, struct patch *);
1360 + int (*fn)(struct gitdiff_data *, const char *, struct patch *);
1361 } optable[] = {
1362 { "@@ -", gitdiff_hdrend },
1363 { "--- ", gitdiff_oldname },
@@ -1377,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
1388 int res;
1389 if (len < oplen || memcmp(p->str, line, oplen))
1390 continue;
1380 - res = p->fn(state, line + oplen, patch);
1391 + res = p->fn(&parse_hdr_state, line + oplen, patch);
1392 if (res < 0)
1393 return -1;
1394 if (check_header_line(state->linenr, patch))