apply: make parse_git_diff_header public

Make 'parse_git_header()' (renamed to 'parse_git_diff_header()') a "public" function in apply.h, so we can re-use it in range-diff in a subsequent commit. We're renaming the function to make it clearer in other parts of the codebase that we're talking about a diff header and not just any header. 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 ef283b3699f91c9138753946dd53bec55289498a
2 files changed +67 -50
apply.c
+19 -50
@@ -207,40 +207,6 @@ struct fragment {
207 #define BINARY_DELTA_DEFLATED 1
208 #define BINARY_LITERAL_DEFLATED 2
209
210 -/*
211 - * This represents a "patch" to a file, both metainfo changes
212 - * such as creation/deletion, filemode and content changes represented
213 - * as a series of fragments.
214 - */
215 -struct patch {
216 - char *new_name, *old_name, *def_name;
217 - unsigned int old_mode, new_mode;
218 - int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
219 - int rejected;
220 - unsigned ws_rule;
221 - int lines_added, lines_deleted;
222 - int score;
223 - int extension_linenr; /* first line specifying delete/new/rename/copy */
224 - unsigned int is_toplevel_relative:1;
225 - unsigned int inaccurate_eof:1;
226 - unsigned int is_binary:1;
227 - unsigned int is_copy:1;
228 - unsigned int is_rename:1;
229 - unsigned int recount:1;
230 - unsigned int conflicted_threeway:1;
231 - unsigned int direct_to_threeway:1;
232 - unsigned int crlf_in_old:1;
233 - struct fragment *fragments;
234 - char *result;
235 - size_t resultsize;
236 - char old_oid_prefix[GIT_MAX_HEXSZ + 1];
237 - char new_oid_prefix[GIT_MAX_HEXSZ + 1];
238 - struct patch *next;
239 -
240 - /* three-way fallback result */
241 - struct object_id threeway_stage[3];
242 -};
243 -
210 static void free_fragment_list(struct fragment *list)
211 {
212 while (list) {
@@ -1320,12 +1286,13 @@ static int check_header_line(int linenr, struct patch *patch)
1286 return 0;
1287 }
1288
1323 -/* Verify that we recognize the lines following a git header */
1324 -static int parse_git_header(struct apply_state *state,
1325 - const char *line,
1326 - int len,
1327 - unsigned int size,
1328 - struct patch *patch)
1289 +int parse_git_diff_header(struct strbuf *root,
1290 + int *linenr,
1291 + int p_value,
1292 + const char *line,
1293 + int len,
1294 + unsigned int size,
1295 + struct patch *patch)
1296 {
1297 unsigned long offset;
1298 struct gitdiff_data parse_hdr_state;
@@ -1340,21 +1307,21 @@ static int parse_git_header(struct apply_state *state,
1307 * or removing or adding empty files), so we get
1308 * the default name from the header.
1309 */
1343 - patch->def_name = git_header_name(state->p_value, line, len);
1344 - if (patch->def_name && state->root.len) {
1345 - char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
1310 + patch->def_name = git_header_name(p_value, line, len);
1311 + if (patch->def_name && root->len) {
1312 + char *s = xstrfmt("%s%s", root->buf, patch->def_name);
1313 free(patch->def_name);
1314 patch->def_name = s;
1315 }
1316
1317 line += len;
1318 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;
1319 + (*linenr)++;
1320 + parse_hdr_state.root = root;
1321 + parse_hdr_state.linenr = *linenr;
1322 + parse_hdr_state.p_value = p_value;
1323
1357 - for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
1324 + for (offset = len ; size > 0 ; offset += len, size -= len, line += len, (*linenr)++) {
1325 static const struct opentry {
1326 const char *str;
1327 int (*fn)(struct gitdiff_data *, const char *, struct patch *);
@@ -1391,7 +1358,7 @@ static int parse_git_header(struct apply_state *state,
1358 res = p->fn(&parse_hdr_state, line + oplen, patch);
1359 if (res < 0)
1360 return -1;
1394 - if (check_header_line(state->linenr, patch))
1361 + if (check_header_line(*linenr, patch))
1362 return -1;
1363 if (res > 0)
1364 return offset;
@@ -1572,7 +1539,9 @@ static int find_header(struct apply_state *state,
1539 * or mode change, so we handle that specially
1540 */
1541 if (!memcmp("diff --git ", line, 11)) {
1575 - int git_hdr_len = parse_git_header(state, line, len, size, patch);
1542 + int git_hdr_len = parse_git_diff_header(&state->root, &state->linenr,
1543 + state->p_value, line, len,
1544 + size, patch);
1545 if (git_hdr_len < 0)
1546 return -128;
1547 if (git_hdr_len <= len)
apply.h
+48
@@ -117,6 +117,40 @@ struct apply_state {
117 int applied_after_fixing_ws;
118 };
119
120 +/*
121 + * This represents a "patch" to a file, both metainfo changes
122 + * such as creation/deletion, filemode and content changes represented
123 + * as a series of fragments.
124 + */
125 +struct patch {
126 + char *new_name, *old_name, *def_name;
127 + unsigned int old_mode, new_mode;
128 + int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
129 + int rejected;
130 + unsigned ws_rule;
131 + int lines_added, lines_deleted;
132 + int score;
133 + int extension_linenr; /* first line specifying delete/new/rename/copy */
134 + unsigned int is_toplevel_relative:1;
135 + unsigned int inaccurate_eof:1;
136 + unsigned int is_binary:1;
137 + unsigned int is_copy:1;
138 + unsigned int is_rename:1;
139 + unsigned int recount:1;
140 + unsigned int conflicted_threeway:1;
141 + unsigned int direct_to_threeway:1;
142 + unsigned int crlf_in_old:1;
143 + struct fragment *fragments;
144 + char *result;
145 + size_t resultsize;
146 + char old_oid_prefix[GIT_MAX_HEXSZ + 1];
147 + char new_oid_prefix[GIT_MAX_HEXSZ + 1];
148 + struct patch *next;
149 +
150 + /* three-way fallback result */
151 + struct object_id threeway_stage[3];
152 +};
153 +
154 int apply_parse_options(int argc, const char **argv,
155 struct apply_state *state,
156 int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
161 void clear_apply_state(struct apply_state *state);
162 int check_apply_state(struct apply_state *state, int force_apply);
163
164 +/*
165 + * Parse a git diff header, starting at line. Fills the relevant
166 + * metadata information in 'struct patch'.
167 + *
168 + * Returns -1 on failure, the length of the parsed header otherwise.
169 + */
170 +int parse_git_diff_header(struct strbuf *root,
171 + int *linenr,
172 + int p_value,
173 + const char *line,
174 + int len,
175 + unsigned int size,
176 + struct patch *patch);
177 +
178 /*
179 * Some aspects of the apply behavior are controlled by the following
180 * bits in the "options" parameter passed to apply_all_patches().