Skip to content

Commit ef283b3

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 80e1841 commit ef283b3

File tree

2 files changed

+67
-50
lines changed

2 files changed

+67
-50
lines changed

apply.c

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -207,40 +207,6 @@ struct fragment {
207207
#define BINARY_DELTA_DEFLATED 1
208208
#define BINARY_LITERAL_DEFLATED 2
209209

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-
244210
static void free_fragment_list(struct fragment *list)
245211
{
246212
while (list) {
@@ -1320,12 +1286,13 @@ static int check_header_line(int linenr, struct patch *patch)
13201286
return 0;
13211287
}
13221288

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)
13291296
{
13301297
unsigned long offset;
13311298
struct gitdiff_data parse_hdr_state;
@@ -1340,21 +1307,21 @@ static int parse_git_header(struct apply_state *state,
13401307
* or removing or adding empty files), so we get
13411308
* the default name from the header.
13421309
*/
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);
13461313
free(patch->def_name);
13471314
patch->def_name = s;
13481315
}
13491316

13501317
line += len;
13511318
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;
13561323

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)++) {
13581325
static const struct opentry {
13591326
const char *str;
13601327
int (*fn)(struct gitdiff_data *, const char *, struct patch *);
@@ -1391,7 +1358,7 @@ static int parse_git_header(struct apply_state *state,
13911358
res = p->fn(&parse_hdr_state, line + oplen, patch);
13921359
if (res < 0)
13931360
return -1;
1394-
if (check_header_line(state->linenr, patch))
1361+
if (check_header_line(*linenr, patch))
13951362
return -1;
13961363
if (res > 0)
13971364
return offset;
@@ -1572,7 +1539,9 @@ static int find_header(struct apply_state *state,
15721539
* or mode change, so we handle that specially
15731540
*/
15741541
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);
15761545
if (git_hdr_len < 0)
15771546
return -128;
15781547
if (git_hdr_len <= len)

apply.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ struct apply_state {
117117
int applied_after_fixing_ws;
118118
};
119119

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+
120154
int apply_parse_options(int argc, const char **argv,
121155
struct apply_state *state,
122156
int *force_apply, int *options,
@@ -127,6 +161,20 @@ int init_apply_state(struct apply_state *state,
127161
void clear_apply_state(struct apply_state *state);
128162
int check_apply_state(struct apply_state *state, int force_apply);
129163

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+
130178
/*
131179
* Some aspects of the apply behavior are controlled by the following
132180
* bits in the "options" parameter passed to apply_all_patches().

0 commit comments

Comments
 (0)