builtin/apply: move 'patch_input_file' global into 'struct apply_state'

To libify the apply functionality the 'patch_input_file' 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 b802355863814b331ffc4ae029b228472e874d1f
1 file changed +17 -10
builtin/apply.c
+17 -10
@@ -49,6 +49,7 @@ struct apply_state {
49
50 /* Other non boolean parameters */
51 const char *fake_ancestor;
52 + const char *patch_input_file;
53 int line_termination;
54 unsigned int p_context;
55 };
@@ -79,7 +80,6 @@ static enum ws_ignore {
80 } ws_ignore_action = ignore_ws_none;
81
82
82 -static const char *patch_input_file;
83 static struct strbuf root = STRBUF_INIT;
84
85 static void parse_whitespace_option(const char *option)
@@ -1525,7 +1525,11 @@ static int find_header(struct apply_state *state,
1525 return -1;
1526 }
1527
1528 -static void record_ws_error(unsigned result, const char *line, int len, int linenr)
1528 +static void record_ws_error(struct apply_state *state,
1529 + unsigned result,
1530 + const char *line,
1531 + int len,
1532 + int linenr)
1533 {
1534 char *err;
1535
@@ -1539,15 +1543,18 @@ static void record_ws_error(unsigned result, const char *line, int len, int line
1543
1544 err = whitespace_error_string(result);
1545 fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1542 - patch_input_file, linenr, err, len, line);
1546 + state->patch_input_file, linenr, err, len, line);
1547 free(err);
1548 }
1549
1546 -static void check_whitespace(const char *line, int len, unsigned ws_rule)
1550 +static void check_whitespace(struct apply_state *state,
1551 + const char *line,
1552 + int len,
1553 + unsigned ws_rule)
1554 {
1555 unsigned result = ws_check(line + 1, len - 1, ws_rule);
1556
1550 - record_ws_error(result, line + 1, len - 2, state_linenr);
1557 + record_ws_error(state, result, line + 1, len - 2, state_linenr);
1558 }
1559
1560 /*
@@ -1602,12 +1609,12 @@ static int parse_fragment(struct apply_state *state,
1609 trailing++;
1610 if (!state->apply_in_reverse &&
1611 ws_error_action == correct_ws_error)
1605 - check_whitespace(line, len, patch->ws_rule);
1612 + check_whitespace(state, line, len, patch->ws_rule);
1613 break;
1614 case '-':
1615 if (state->apply_in_reverse &&
1616 ws_error_action != nowarn_ws_error)
1610 - check_whitespace(line, len, patch->ws_rule);
1617 + check_whitespace(state, line, len, patch->ws_rule);
1618 deleted++;
1619 oldlines--;
1620 trailing = 0;
@@ -1615,7 +1622,7 @@ static int parse_fragment(struct apply_state *state,
1622 case '+':
1623 if (!state->apply_in_reverse &&
1624 ws_error_action != nowarn_ws_error)
1618 - check_whitespace(line, len, patch->ws_rule);
1625 + check_whitespace(state, line, len, patch->ws_rule);
1626 added++;
1627 newlines--;
1628 trailing = 0;
@@ -2904,7 +2911,7 @@ static int apply_one_fragment(struct apply_state *state,
2911 preimage.nr + applied_pos >= img->nr &&
2912 (ws_rule & WS_BLANK_AT_EOF) &&
2913 ws_error_action != nowarn_ws_error) {
2907 - record_ws_error(WS_BLANK_AT_EOF, "+", 1,
2914 + record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
2915 found_new_blank_lines_at_end);
2916 if (ws_error_action == correct_ws_error) {
2917 while (new_blank_lines_at_end--)
@@ -4427,7 +4434,7 @@ static int apply_patch(struct apply_state *state,
4434 struct patch *list = NULL, **listp = &list;
4435 int skipped_patch = 0;
4436
4430 - patch_input_file = filename;
4437 + state->patch_input_file = filename;
4438 read_patch_file(&buf, fd);
4439 offset = 0;
4440 while (offset < buf.len) {