Skip to content

Commit d7263d0

Browse files
chriscoolgitster
authored andcommitted
builtin/apply: move 'state_linenr' global into 'struct apply_state'
To libify the apply functionality the 'state_linenr' variable should not be static and global to the file. Let's move it 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 1ffec30 commit d7263d0

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

builtin/apply.c

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

76+
/* Various "current state" */
77+
int linenr; /* current line number */
78+
7679
/*
7780
* For "diff-stat" like behaviour, we keep track of the biggest change
7881
* we've seen, and the longest filename. That allows us to do simple
@@ -149,13 +152,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
149152
state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
150153
}
151154

152-
/*
153-
* Various "current state", notably line numbers and what
154-
* file (and how) we're patching right now.. The "is_xxxx"
155-
* things are flags, where -1 means "don't know yet".
156-
*/
157-
static int state_linenr = 1;
158-
159155
/*
160156
* This represents one "hunk" from a patch, starting with
161157
* "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
@@ -932,7 +928,7 @@ static void parse_traditional_patch(struct apply_state *state,
932928
}
933929
}
934930
if (!name)
935-
die(_("unable to find filename in patch at line %d"), state_linenr);
931+
die(_("unable to find filename in patch at line %d"), state->linenr);
936932
}
937933

938934
static int gitdiff_hdrend(struct apply_state *state,
@@ -970,17 +966,17 @@ static void gitdiff_verify_name(struct apply_state *state,
970966
char *another;
971967
if (isnull)
972968
die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
973-
*name, state_linenr);
969+
*name, state->linenr);
974970
another = find_name(state, line, NULL, state->p_value, TERM_TAB);
975971
if (!another || memcmp(another, *name, len + 1))
976972
die((side == DIFF_NEW_NAME) ?
977973
_("git apply: bad git-diff - inconsistent new filename on line %d") :
978-
_("git apply: bad git-diff - inconsistent old filename on line %d"), state_linenr);
974+
_("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
979975
free(another);
980976
} else {
981977
/* expect "/dev/null" */
982978
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
983-
die(_("git apply: bad git-diff - expected /dev/null on line %d"), state_linenr);
979+
die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
984980
}
985981
}
986982

@@ -1343,8 +1339,8 @@ static int parse_git_header(struct apply_state *state,
13431339

13441340
line += len;
13451341
size -= len;
1346-
state_linenr++;
1347-
for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {
1342+
state->linenr++;
1343+
for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
13481344
static const struct opentry {
13491345
const char *str;
13501346
int (*fn)(struct apply_state *, const char *, struct patch *);
@@ -1515,7 +1511,7 @@ static int find_header(struct apply_state *state,
15151511
patch->is_new = patch->is_delete = -1;
15161512
patch->old_mode = patch->new_mode = 0;
15171513
patch->old_name = patch->new_name = NULL;
1518-
for (offset = 0; size > 0; offset += len, size -= len, line += len, state_linenr++) {
1514+
for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
15191515
unsigned long nextlen;
15201516

15211517
len = linelen(line, size);
@@ -1536,7 +1532,7 @@ static int find_header(struct apply_state *state,
15361532
if (parse_fragment_header(line, len, &dummy) < 0)
15371533
continue;
15381534
die(_("patch fragment without header at line %d: %.*s"),
1539-
state_linenr, (int)len-1, line);
1535+
state->linenr, (int)len-1, line);
15401536
}
15411537

15421538
if (size < len + 6)
@@ -1557,13 +1553,13 @@ static int find_header(struct apply_state *state,
15571553
"git diff header lacks filename information when removing "
15581554
"%d leading pathname components (line %d)",
15591555
state->p_value),
1560-
state->p_value, state_linenr);
1556+
state->p_value, state->linenr);
15611557
patch->old_name = xstrdup(patch->def_name);
15621558
patch->new_name = xstrdup(patch->def_name);
15631559
}
15641560
if (!patch->is_delete && !patch->new_name)
15651561
die("git diff header lacks filename information "
1566-
"(line %d)", state_linenr);
1562+
"(line %d)", state->linenr);
15671563
patch->is_toplevel_relative = 1;
15681564
*hdrsize = git_hdr_len;
15691565
return offset;
@@ -1585,7 +1581,7 @@ static int find_header(struct apply_state *state,
15851581
/* Ok, we'll consider it a patch */
15861582
parse_traditional_patch(state, line, line+len, patch);
15871583
*hdrsize = len + nextlen;
1588-
state_linenr += 2;
1584+
state->linenr += 2;
15891585
return offset;
15901586
}
15911587
return -1;
@@ -1620,7 +1616,7 @@ static void check_whitespace(struct apply_state *state,
16201616
{
16211617
unsigned result = ws_check(line + 1, len - 1, ws_rule);
16221618

1623-
record_ws_error(state, result, line + 1, len - 2, state_linenr);
1619+
record_ws_error(state, result, line + 1, len - 2, state->linenr);
16241620
}
16251621

16261622
/*
@@ -1653,11 +1649,11 @@ static int parse_fragment(struct apply_state *state,
16531649
/* Parse the thing.. */
16541650
line += len;
16551651
size -= len;
1656-
state_linenr++;
1652+
state->linenr++;
16571653
added = deleted = 0;
16581654
for (offset = len;
16591655
0 < size;
1660-
offset += len, size -= len, line += len, state_linenr++) {
1656+
offset += len, size -= len, line += len, state->linenr++) {
16611657
if (!oldlines && !newlines)
16621658
break;
16631659
len = linelen(line, size);
@@ -1756,10 +1752,10 @@ static int parse_single_patch(struct apply_state *state,
17561752
int len;
17571753

17581754
fragment = xcalloc(1, sizeof(*fragment));
1759-
fragment->linenr = state_linenr;
1755+
fragment->linenr = state->linenr;
17601756
len = parse_fragment(state, line, size, patch, fragment);
17611757
if (len <= 0)
1762-
die(_("corrupt patch at line %d"), state_linenr);
1758+
die(_("corrupt patch at line %d"), state->linenr);
17631759
fragment->patch = line;
17641760
fragment->size = len;
17651761
oldlines += fragment->oldlines;
@@ -1845,7 +1841,8 @@ static char *inflate_it(const void *data, unsigned long size,
18451841
* points at an allocated memory that the caller must free, so
18461842
* it is marked as "->free_patch = 1".
18471843
*/
1848-
static struct fragment *parse_binary_hunk(char **buf_p,
1844+
static struct fragment *parse_binary_hunk(struct apply_state *state,
1845+
char **buf_p,
18491846
unsigned long *sz_p,
18501847
int *status_p,
18511848
int *used_p)
@@ -1887,13 +1884,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,
18871884
else
18881885
return NULL;
18891886

1890-
state_linenr++;
1887+
state->linenr++;
18911888
buffer += llen;
18921889
while (1) {
18931890
int byte_length, max_byte_length, newsize;
18941891
llen = linelen(buffer, size);
18951892
used += llen;
1896-
state_linenr++;
1893+
state->linenr++;
18971894
if (llen == 1) {
18981895
/* consume the blank line */
18991896
buffer++;
@@ -1947,7 +1944,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,
19471944
free(data);
19481945
*status_p = -1;
19491946
error(_("corrupt binary patch at line %d: %.*s"),
1950-
state_linenr-1, llen-1, buffer);
1947+
state->linenr-1, llen-1, buffer);
19511948
return NULL;
19521949
}
19531950

@@ -1956,7 +1953,10 @@ static struct fragment *parse_binary_hunk(char **buf_p,
19561953
* -1 in case of error,
19571954
* the length of the parsed binary patch otherwise
19581955
*/
1959-
static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1956+
static int parse_binary(struct apply_state *state,
1957+
char *buffer,
1958+
unsigned long size,
1959+
struct patch *patch)
19601960
{
19611961
/*
19621962
* We have read "GIT binary patch\n"; what follows is a line
@@ -1977,15 +1977,15 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
19771977
int status;
19781978
int used, used_1;
19791979

1980-
forward = parse_binary_hunk(&buffer, &size, &status, &used);
1980+
forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
19811981
if (!forward && !status)
19821982
/* there has to be one hunk (forward hunk) */
1983-
return error(_("unrecognized binary patch at line %d"), state_linenr-1);
1983+
return error(_("unrecognized binary patch at line %d"), state->linenr-1);
19841984
if (status)
19851985
/* otherwise we already gave an error message */
19861986
return status;
19871987

1988-
reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1988+
reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
19891989
if (reverse)
19901990
used += used_1;
19911991
else if (status) {
@@ -2100,8 +2100,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
21002100
if (llen == sizeof(git_binary) - 1 &&
21012101
!memcmp(git_binary, buffer + hd, llen)) {
21022102
int used;
2103-
state_linenr++;
2104-
used = parse_binary(buffer + hd + llen,
2103+
state->linenr++;
2104+
used = parse_binary(state, buffer + hd + llen,
21052105
size - hd - llen, patch);
21062106
if (used < 0)
21072107
return -1;
@@ -2121,7 +2121,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
21212121
int len = strlen(binhdr[i]);
21222122
if (len < size - hd &&
21232123
!memcmp(binhdr[i], buffer + hd, len)) {
2124-
state_linenr++;
2124+
state->linenr++;
21252125
patch->is_binary = 1;
21262126
patchsize = llen;
21272127
break;
@@ -2135,7 +2135,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
21352135
*/
21362136
if ((state->apply || state->check) &&
21372137
(!patch->is_binary && !metadata_changes(patch)))
2138-
die(_("patch with only garbage at line %d"), state_linenr);
2138+
die(_("patch with only garbage at line %d"), state->linenr);
21392139
}
21402140

21412141
return offset + hdrsize + patchsize;
@@ -4654,6 +4654,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
46544654
state->squelch_whitespace_errors = 5;
46554655
state->ws_error_action = warn_on_ws_error;
46564656
state->ws_ignore_action = ignore_ws_none;
4657+
state->linenr = 1;
46574658
strbuf_init(&state->root, 0);
46584659

46594660
git_apply_config();

0 commit comments

Comments
 (0)