builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'
To libify the apply functionality the 'apply_in_reverse' 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:10 UTC
2595a8b146e86a3217cd6ee44bcc889cc2fe774f
1 file changed
+31
-19
builtin/apply.c
+31
-19
@@ -30,6 +30,7 @@ struct apply_state {
30
int check_index; /* preimage must match the indexed version */
31
32
/* These boolean parameters control how the apply is done */
33
+ int apply_in_reverse;
34
int unidiff_zero;
35
};
36
@@ -49,7 +50,6 @@ static int diffstat;
50
static int numstat;
51
static int summary;
52
static int apply = 1;
52
-static int apply_in_reverse;
53
static int apply_with_reject;
54
static int apply_verbosely;
55
static int allow_overlap;
@@ -1556,8 +1556,11 @@ static void check_whitespace(const char *line, int len, unsigned ws_rule)
1556
* between a "---" that is part of a patch, and a "---" that starts
1557
* the next patch is to look at the line counts..
1558
*/
1559
-static int parse_fragment(const char *line, unsigned long size,
1560
- struct patch *patch, struct fragment *fragment)
1559
+static int parse_fragment(struct apply_state *state,
1560
+ const char *line,
1561
+ unsigned long size,
1562
+ struct patch *patch,
1563
+ struct fragment *fragment)
1564
{
1565
int added, deleted;
1566
int len = linelen(line, size), offset;
@@ -1597,12 +1600,12 @@ static int parse_fragment(const char *line, unsigned long size,
1600
if (!deleted && !added)
1601
leading++;
1602
trailing++;
1600
- if (!apply_in_reverse &&
1603
+ if (!state->apply_in_reverse &&
1604
ws_error_action == correct_ws_error)
1605
check_whitespace(line, len, patch->ws_rule);
1606
break;
1607
case '-':
1605
- if (apply_in_reverse &&
1608
+ if (state->apply_in_reverse &&
1609
ws_error_action != nowarn_ws_error)
1610
check_whitespace(line, len, patch->ws_rule);
1611
deleted++;
@@ -1610,7 +1613,7 @@ static int parse_fragment(const char *line, unsigned long size,
1613
trailing = 0;
1614
break;
1615
case '+':
1613
- if (!apply_in_reverse &&
1616
+ if (!state->apply_in_reverse &&
1617
ws_error_action != nowarn_ws_error)
1618
check_whitespace(line, len, patch->ws_rule);
1619
added++;
@@ -1666,7 +1669,10 @@ static int parse_fragment(const char *line, unsigned long size,
1669
* The (fragment->patch, fragment->size) pair points into the memory given
1670
* by the caller, not a copy, when we return.
1671
*/
1669
-static int parse_single_patch(const char *line, unsigned long size, struct patch *patch)
1672
+static int parse_single_patch(struct apply_state *state,
1673
+ const char *line,
1674
+ unsigned long size,
1675
+ struct patch *patch)
1676
{
1677
unsigned long offset = 0;
1678
unsigned long oldlines = 0, newlines = 0, context = 0;
@@ -1678,7 +1684,7 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch
1684
1685
fragment = xcalloc(1, sizeof(*fragment));
1686
fragment->linenr = state_linenr;
1681
- len = parse_fragment(line, size, patch, fragment);
1687
+ len = parse_fragment(state, line, size, patch, fragment);
1688
if (len <= 0)
1689
die(_("corrupt patch at line %d"), state_linenr);
1690
fragment->patch = line;
@@ -2008,8 +2014,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2014
? patch->new_name
2015
: patch->old_name);
2016
2011
- patchsize = parse_single_patch(buffer + offset + hdrsize,
2012
- size - offset - hdrsize, patch);
2017
+ patchsize = parse_single_patch(state,
2018
+ buffer + offset + hdrsize,
2019
+ size - offset - hdrsize,
2020
+ patch);
2021
2022
if (!patchsize) {
2023
static const char git_binary[] = "GIT binary patch\n";
@@ -2741,7 +2749,7 @@ static int apply_one_fragment(struct apply_state *state,
2749
if (len < size && patch[len] == '\\')
2750
plen--;
2751
first = *patch;
2744
- if (apply_in_reverse) {
2752
+ if (state->apply_in_reverse) {
2753
if (first == '-')
2754
first = '+';
2755
else if (first == '+')
@@ -2914,7 +2922,7 @@ static int apply_one_fragment(struct apply_state *state,
2922
2923
if (apply_verbosely && applied_pos != pos) {
2924
int offset = applied_pos - pos;
2917
- if (apply_in_reverse)
2925
+ if (state->apply_in_reverse)
2926
offset = 0 - offset;
2927
fprintf_ln(stderr,
2928
Q_("Hunk #%d succeeded at %d (offset %d line).",
@@ -2948,7 +2956,9 @@ out:
2956
return (applied_pos < 0);
2957
}
2958
2951
-static int apply_binary_fragment(struct image *img, struct patch *patch)
2959
+static int apply_binary_fragment(struct apply_state *state,
2960
+ struct image *img,
2961
+ struct patch *patch)
2962
{
2963
struct fragment *fragment = patch->fragments;
2964
unsigned long len;
@@ -2961,7 +2971,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
2971
patch->old_name);
2972
2973
/* Binary patch is irreversible without the optional second hunk */
2964
- if (apply_in_reverse) {
2974
+ if (state->apply_in_reverse) {
2975
if (!fragment->next)
2976
return error("cannot reverse-apply a binary patch "
2977
"without the reverse hunk to '%s'",
@@ -2994,7 +3004,9 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
3004
* but the preimage prepared by the caller in "img" is freed here
3005
* or in the helper function apply_binary_fragment() this calls.
3006
*/
2997
-static int apply_binary(struct image *img, struct patch *patch)
3007
+static int apply_binary(struct apply_state *state,
3008
+ struct image *img,
3009
+ struct patch *patch)
3010
{
3011
const char *name = patch->old_name ? patch->old_name : patch->new_name;
3012
unsigned char sha1[20];
@@ -3055,7 +3067,7 @@ static int apply_binary(struct image *img, struct patch *patch)
3067
* apply the patch data to it, which is stored
3068
* in the patch->fragments->{patch,size}.
3069
*/
3058
- if (apply_binary_fragment(img, patch))
3070
+ if (apply_binary_fragment(state, img, patch))
3071
return error(_("binary patch does not apply to '%s'"),
3072
name);
3073
@@ -3078,7 +3090,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
3090
int nth = 0;
3091
3092
if (patch->is_binary)
3081
- return apply_binary(img, patch);
3093
+ return apply_binary(state, img, patch);
3094
3095
while (frag) {
3096
nth++;
@@ -4417,7 +4429,7 @@ static int apply_patch(struct apply_state *state,
4429
free_patch(patch);
4430
break;
4431
}
4420
- if (apply_in_reverse)
4432
+ if (state->apply_in_reverse)
4433
reverse_patches(patch);
4434
if (use_patch(state, patch)) {
4435
patch_stats(patch);
@@ -4615,7 +4627,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4627
{ OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
4628
N_("ignore changes in whitespace when finding context"),
4629
PARSE_OPT_NOARG, option_parse_space_change },
4618
- OPT_BOOL('R', "reverse", &apply_in_reverse,
4630
+ OPT_BOOL('R', "reverse", &state.apply_in_reverse,
4631
N_("apply the patch in reverse")),
4632
OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
4633
N_("don't expect at least one line of context")),