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>

Christian Couder committed May 24, 2016 at 10:11 UTC d7263d097c4510d864f8c9ff808bc9b377f0bf8f
1 file changed +36 -35
builtin/apply.c
+36 -35
@@ -73,6 +73,9 @@ struct apply_state {
73 struct string_list limit_by_name;
74 int has_include;
75
76 + /* Various "current state" */
77 + int linenr; /* current line number */
78 +
79 /*
80 * For "diff-stat" like behaviour, we keep track of the biggest change
81 * 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)
152 state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
153 }
154
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 -
155 /*
156 * This represents one "hunk" from a patch, starting with
157 * "@@ -oldpos,oldlines +newpos,newlines @@" marker. The
@@ -932,7 +928,7 @@ static void parse_traditional_patch(struct apply_state *state,
928 }
929 }
930 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);
932 }
933
934 static int gitdiff_hdrend(struct apply_state *state,
@@ -970,17 +966,17 @@ static void gitdiff_verify_name(struct apply_state *state,
966 char *another;
967 if (isnull)
968 die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
973 - *name, state_linenr);
969 + *name, state->linenr);
970 another = find_name(state, line, NULL, state->p_value, TERM_TAB);
971 if (!another || memcmp(another, *name, len + 1))
972 die((side == DIFF_NEW_NAME) ?
973 _("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);
975 free(another);
976 } else {
977 /* expect "/dev/null" */
978 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);
980 }
981 }
982
@@ -1343,8 +1339,8 @@ static int parse_git_header(struct apply_state *state,
1339
1340 line += len;
1341 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++) {
1344 static const struct opentry {
1345 const char *str;
1346 int (*fn)(struct apply_state *, const char *, struct patch *);
@@ -1515,7 +1511,7 @@ static int find_header(struct apply_state *state,
1511 patch->is_new = patch->is_delete = -1;
1512 patch->old_mode = patch->new_mode = 0;
1513 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++) {
1515 unsigned long nextlen;
1516
1517 len = linelen(line, size);
@@ -1536,7 +1532,7 @@ static int find_header(struct apply_state *state,
1532 if (parse_fragment_header(line, len, &dummy) < 0)
1533 continue;
1534 die(_("patch fragment without header at line %d: %.*s"),
1539 - state_linenr, (int)len-1, line);
1535 + state->linenr, (int)len-1, line);
1536 }
1537
1538 if (size < len + 6)
@@ -1557,13 +1553,13 @@ static int find_header(struct apply_state *state,
1553 "git diff header lacks filename information when removing "
1554 "%d leading pathname components (line %d)",
1555 state->p_value),
1560 - state->p_value, state_linenr);
1556 + state->p_value, state->linenr);
1557 patch->old_name = xstrdup(patch->def_name);
1558 patch->new_name = xstrdup(patch->def_name);
1559 }
1560 if (!patch->is_delete && !patch->new_name)
1561 die("git diff header lacks filename information "
1566 - "(line %d)", state_linenr);
1562 + "(line %d)", state->linenr);
1563 patch->is_toplevel_relative = 1;
1564 *hdrsize = git_hdr_len;
1565 return offset;
@@ -1585,7 +1581,7 @@ static int find_header(struct apply_state *state,
1581 /* Ok, we'll consider it a patch */
1582 parse_traditional_patch(state, line, line+len, patch);
1583 *hdrsize = len + nextlen;
1588 - state_linenr += 2;
1584 + state->linenr += 2;
1585 return offset;
1586 }
1587 return -1;
@@ -1620,7 +1616,7 @@ static void check_whitespace(struct apply_state *state,
1616 {
1617 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1618
1623 - record_ws_error(state, result, line + 1, len - 2, state_linenr);
1619 + record_ws_error(state, result, line + 1, len - 2, state->linenr);
1620 }
1621
1622 /*
@@ -1653,11 +1649,11 @@ static int parse_fragment(struct apply_state *state,
1649 /* Parse the thing.. */
1650 line += len;
1651 size -= len;
1656 - state_linenr++;
1652 + state->linenr++;
1653 added = deleted = 0;
1654 for (offset = len;
1655 0 < size;
1660 - offset += len, size -= len, line += len, state_linenr++) {
1656 + offset += len, size -= len, line += len, state->linenr++) {
1657 if (!oldlines && !newlines)
1658 break;
1659 len = linelen(line, size);
@@ -1756,10 +1752,10 @@ static int parse_single_patch(struct apply_state *state,
1752 int len;
1753
1754 fragment = xcalloc(1, sizeof(*fragment));
1759 - fragment->linenr = state_linenr;
1755 + fragment->linenr = state->linenr;
1756 len = parse_fragment(state, line, size, patch, fragment);
1757 if (len <= 0)
1762 - die(_("corrupt patch at line %d"), state_linenr);
1758 + die(_("corrupt patch at line %d"), state->linenr);
1759 fragment->patch = line;
1760 fragment->size = len;
1761 oldlines += fragment->oldlines;
@@ -1845,7 +1841,8 @@ static char *inflate_it(const void *data, unsigned long size,
1841 * points at an allocated memory that the caller must free, so
1842 * it is marked as "->free_patch = 1".
1843 */
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,
1846 unsigned long *sz_p,
1847 int *status_p,
1848 int *used_p)
@@ -1887,13 +1884,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1884 else
1885 return NULL;
1886
1890 - state_linenr++;
1887 + state->linenr++;
1888 buffer += llen;
1889 while (1) {
1890 int byte_length, max_byte_length, newsize;
1891 llen = linelen(buffer, size);
1892 used += llen;
1896 - state_linenr++;
1893 + state->linenr++;
1894 if (llen == 1) {
1895 /* consume the blank line */
1896 buffer++;
@@ -1947,7 +1944,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1944 free(data);
1945 *status_p = -1;
1946 error(_("corrupt binary patch at line %d: %.*s"),
1950 - state_linenr-1, llen-1, buffer);
1947 + state->linenr-1, llen-1, buffer);
1948 return NULL;
1949 }
1950
@@ -1956,7 +1953,10 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1953 * -1 in case of error,
1954 * the length of the parsed binary patch otherwise
1955 */
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)
1960 {
1961 /*
1962 * 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)
1977 int status;
1978 int used, used_1;
1979
1980 - forward = parse_binary_hunk(&buffer, &size, &status, &used);
1980 + forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
1981 if (!forward && !status)
1982 /* 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);
1984 if (status)
1985 /* otherwise we already gave an error message */
1986 return status;
1987
1988 - reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
1988 + reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
1989 if (reverse)
1990 used += used_1;
1991 else if (status) {
@@ -2100,8 +2100,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2100 if (llen == sizeof(git_binary) - 1 &&
2101 !memcmp(git_binary, buffer + hd, llen)) {
2102 int used;
2103 - state_linenr++;
2104 - used = parse_binary(buffer + hd + llen,
2103 + state->linenr++;
2104 + used = parse_binary(state, buffer + hd + llen,
2105 size - hd - llen, patch);
2106 if (used < 0)
2107 return -1;
@@ -2121,7 +2121,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2121 int len = strlen(binhdr[i]);
2122 if (len < size - hd &&
2123 !memcmp(binhdr[i], buffer + hd, len)) {
2124 - state_linenr++;
2124 + state->linenr++;
2125 patch->is_binary = 1;
2126 patchsize = llen;
2127 break;
@@ -2135,7 +2135,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2135 */
2136 if ((state->apply || state->check) &&
2137 (!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);
2139 }
2140
2141 return offset + hdrsize + patchsize;
@@ -4654,6 +4654,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
4654 state->squelch_whitespace_errors = 5;
4655 state->ws_error_action = warn_on_ws_error;
4656 state->ws_ignore_action = ignore_ws_none;
4657 + state->linenr = 1;
4658 strbuf_init(&state->root, 0);
4659
4660 git_apply_config();