builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
To libify the apply functionality the 'unidiff_zero' 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
1da16e1ed85c5be6eb243ca91225757b25088623
1 file changed
+25
-18
builtin/apply.c
+25
-18
@@ -24,6 +24,9 @@
24
struct apply_state {
25
const char *prefix;
26
int prefix_length;
27
+
28
+ /* These boolean parameters control how the apply is done */
29
+ int unidiff_zero;
30
};
31
32
/*
@@ -37,7 +40,6 @@ struct apply_state {
40
*/
41
static int newfd = -1;
42
40
-static int unidiff_zero;
43
static int state_p_value = 1;
44
static int p_value_known;
45
static int check_index;
@@ -2694,7 +2696,8 @@ static void update_image(struct image *img,
2696
* postimage) for the hunk. Find lines that match "preimage" in "img" and
2697
* replace the part of "img" with "postimage" text.
2698
*/
2697
-static int apply_one_fragment(struct image *img, struct fragment *frag,
2699
+static int apply_one_fragment(struct apply_state *state,
2700
+ struct image *img, struct fragment *frag,
2701
int inaccurate_eof, unsigned ws_rule,
2702
int nth_fragment)
2703
{
@@ -2836,7 +2839,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2839
* without leading context must match at the beginning.
2840
*/
2841
match_beginning = (!frag->oldpos ||
2839
- (frag->oldpos == 1 && !unidiff_zero));
2842
+ (frag->oldpos == 1 && !state->unidiff_zero));
2843
2844
/*
2845
* A hunk without trailing lines must match at the end.
@@ -2844,7 +2847,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
2847
* from the lack of trailing lines if the patch was generated
2848
* with unidiff without any context.
2849
*/
2847
- match_end = !unidiff_zero && !trailing;
2850
+ match_end = !state->unidiff_zero && !trailing;
2851
2852
pos = frag->newpos ? (frag->newpos - 1) : 0;
2853
preimage.buf = oldlines;
@@ -3067,7 +3070,7 @@ static int apply_binary(struct image *img, struct patch *patch)
3070
return 0;
3071
}
3072
3070
-static int apply_fragments(struct image *img, struct patch *patch)
3073
+static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)
3074
{
3075
struct fragment *frag = patch->fragments;
3076
const char *name = patch->old_name ? patch->old_name : patch->new_name;
@@ -3080,7 +3083,7 @@ static int apply_fragments(struct image *img, struct patch *patch)
3083
3084
while (frag) {
3085
nth++;
3083
- if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, nth)) {
3086
+ if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3087
error(_("patch failed: %s:%ld"), name, frag->oldpos);
3088
if (!apply_with_reject)
3089
return -1;
@@ -3388,8 +3391,11 @@ static int load_current(struct image *image, struct patch *patch)
3391
return 0;
3392
}
3393
3391
-static int try_threeway(struct image *image, struct patch *patch,
3392
- struct stat *st, const struct cache_entry *ce)
3394
+static int try_threeway(struct apply_state *state,
3395
+ struct image *image,
3396
+ struct patch *patch,
3397
+ struct stat *st,
3398
+ const struct cache_entry *ce)
3399
{
3400
unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
3401
struct strbuf buf = STRBUF_INIT;
@@ -3415,7 +3421,7 @@ static int try_threeway(struct image *image, struct patch *patch,
3421
img = strbuf_detach(&buf, &len);
3422
prepare_image(&tmp_image, img, len, 1);
3423
/* Apply the patch to get the post image */
3418
- if (apply_fragments(&tmp_image, patch) < 0) {
3424
+ if (apply_fragments(state, &tmp_image, patch) < 0) {
3425
clear_image(&tmp_image);
3426
return -1;
3427
}
@@ -3459,7 +3465,8 @@ static int try_threeway(struct image *image, struct patch *patch,
3465
return 0;
3466
}
3467
3462
-static int apply_data(struct patch *patch, struct stat *st, const struct cache_entry *ce)
3468
+static int apply_data(struct apply_state *state, struct patch *patch,
3469
+ struct stat *st, const struct cache_entry *ce)
3470
{
3471
struct image image;
3472
@@ -3467,9 +3474,9 @@ static int apply_data(struct patch *patch, struct stat *st, const struct cache_e
3474
return -1;
3475
3476
if (patch->direct_to_threeway ||
3470
- apply_fragments(&image, patch) < 0) {
3477
+ apply_fragments(state, &image, patch) < 0) {
3478
/* Note: with --reject, apply_fragments() returns 0 */
3472
- if (!threeway || try_threeway(&image, patch, st, ce) < 0)
3479
+ if (!threeway || try_threeway(state, &image, patch, st, ce) < 0)
3480
return -1;
3481
}
3482
patch->result = image.buf;
@@ -3717,7 +3724,7 @@ static void die_on_unsafe_path(struct patch *patch)
3724
* Check and apply the patch in-core; leave the result in patch->result
3725
* for the caller to write it out to the final destination.
3726
*/
3720
-static int check_patch(struct patch *patch)
3727
+static int check_patch(struct apply_state *state, struct patch *patch)
3728
{
3729
struct stat st;
3730
const char *old_name = patch->old_name;
@@ -3816,13 +3823,13 @@ static int check_patch(struct patch *patch)
3823
return error(_("affected file '%s' is beyond a symbolic link"),
3824
patch->new_name);
3825
3819
- if (apply_data(patch, &st, ce) < 0)
3826
+ if (apply_data(state, patch, &st, ce) < 0)
3827
return error(_("%s: patch does not apply"), name);
3828
patch->rejected = 0;
3829
return 0;
3830
}
3831
3825
-static int check_patch_list(struct patch *patch)
3832
+static int check_patch_list(struct apply_state *state, struct patch *patch)
3833
{
3834
int err = 0;
3835
@@ -3832,7 +3839,7 @@ static int check_patch_list(struct patch *patch)
3839
if (apply_verbosely)
3840
say_patch_name(stderr,
3841
_("Checking patch %s..."), patch);
3835
- err |= check_patch(patch);
3842
+ err |= check_patch(state, patch);
3843
patch = patch->next;
3844
}
3845
return err;
@@ -4434,7 +4441,7 @@ static int apply_patch(struct apply_state *state,
4441
}
4442
4443
if ((check || apply) &&
4437
- check_patch_list(list) < 0 &&
4444
+ check_patch_list(state, list) < 0 &&
4445
!apply_with_reject)
4446
exit(1);
4447
@@ -4602,7 +4609,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4609
PARSE_OPT_NOARG, option_parse_space_change },
4610
OPT_BOOL('R', "reverse", &apply_in_reverse,
4611
N_("apply the patch in reverse")),
4605
- OPT_BOOL(0, "unidiff-zero", &unidiff_zero,
4612
+ OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
4613
N_("don't expect at least one line of context")),
4614
OPT_BOOL(0, "reject", &apply_with_reject,
4615
N_("leave the rejected hunks in corresponding *.rej files")),