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

To libify the apply functionality the 'check_index' 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 ee87a6e7404549d2be738bd54f2f191e8b5ea352
1 file changed +37 -29
builtin/apply.c
+37 -29
@@ -27,6 +27,7 @@ struct apply_state {
27
28 /* These control what gets looked at and modified */
29 int check; /* preimage must match working tree, don't actually apply */
30 + int check_index; /* preimage must match the indexed version */
31
32 /* These boolean parameters control how the apply is done */
33 int unidiff_zero;
@@ -36,14 +37,12 @@ struct apply_state {
37 * --stat does just a diffstat, and doesn't actually apply
38 * --numstat does numeric diffstat, and doesn't actually apply
39 * --index-info shows the old and new index info for paths if available.
39 - * --index updates the cache as well.
40 * --cached updates only the cache without ever touching the working tree.
41 */
42 static int newfd = -1;
43
44 static int state_p_value = 1;
45 static int p_value_known;
46 -static int check_index;
46 static int update_index;
47 static int cached;
48 static int diffstat;
@@ -3245,13 +3244,14 @@ static int verify_index_match(const struct cache_entry *ce, struct stat *st)
3244
3245 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
3246
3248 -static int load_patch_target(struct strbuf *buf,
3247 +static int load_patch_target(struct apply_state *state,
3248 + struct strbuf *buf,
3249 const struct cache_entry *ce,
3250 struct stat *st,
3251 const char *name,
3252 unsigned expected_mode)
3253 {
3254 - if (cached || check_index) {
3254 + if (cached || state->check_index) {
3255 if (read_file_or_gitlink(ce, buf))
3256 return error(_("read of %s failed"), name);
3257 } else if (name) {
@@ -3277,7 +3277,8 @@ static int load_patch_target(struct strbuf *buf,
3277 * applying a non-git patch that incrementally updates the tree,
3278 * we read from the result of a previous diff.
3279 */
3280 -static int load_preimage(struct image *image,
3280 +static int load_preimage(struct apply_state *state,
3281 + struct image *image,
3282 struct patch *patch, struct stat *st,
3283 const struct cache_entry *ce)
3284 {
@@ -3295,7 +3296,7 @@ static int load_preimage(struct image *image,
3296 /* We have a patched copy in memory; use that. */
3297 strbuf_add(&buf, previous->result, previous->resultsize);
3298 } else {
3298 - status = load_patch_target(&buf, ce, st,
3299 + status = load_patch_target(state, &buf, ce, st,
3300 patch->old_name, patch->old_mode);
3301 if (status < 0)
3302 return status;
@@ -3354,7 +3355,9 @@ static int three_way_merge(struct image *image,
3355 * the current contents of the new_name. In no cases other than that
3356 * this function will be called.
3357 */
3357 -static int load_current(struct image *image, struct patch *patch)
3358 +static int load_current(struct apply_state *state,
3359 + struct image *image,
3360 + struct patch *patch)
3361 {
3362 struct strbuf buf = STRBUF_INIT;
3363 int status, pos;
@@ -3381,7 +3384,7 @@ static int load_current(struct image *image, struct patch *patch)
3384 if (verify_index_match(ce, &st))
3385 return error(_("%s: does not match index"), name);
3386
3384 - status = load_patch_target(&buf, ce, &st, name, mode);
3387 + status = load_patch_target(state, &buf, ce, &st, name, mode);
3388 if (status < 0)
3389 return status;
3390 else if (status)
@@ -3431,11 +3434,11 @@ static int try_threeway(struct apply_state *state,
3434
3435 /* our_sha1[] is ours */
3436 if (patch->is_new) {
3434 - if (load_current(&tmp_image, patch))
3437 + if (load_current(state, &tmp_image, patch))
3438 return error("cannot read the current contents of '%s'",
3439 patch->new_name);
3440 } else {
3438 - if (load_preimage(&tmp_image, patch, st, ce))
3441 + if (load_preimage(state, &tmp_image, patch, st, ce))
3442 return error("cannot read the current contents of '%s'",
3443 patch->old_name);
3444 }
@@ -3470,7 +3473,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
3473 {
3474 struct image image;
3475
3473 - if (load_preimage(&image, patch, st, ce) < 0)
3476 + if (load_preimage(state, &image, patch, st, ce) < 0)
3477 return -1;
3478
3479 if (patch->direct_to_threeway ||
@@ -3501,7 +3504,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,
3504 * check_patch() separately makes sure (and errors out otherwise) that
3505 * the path the patch creates does not exist in the current tree.
3506 */
3504 -static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
3507 +static int check_preimage(struct apply_state *state,
3508 + struct patch *patch,
3509 + struct cache_entry **ce,
3510 + struct stat *st)
3511 {
3512 const char *old_name = patch->old_name;
3513 struct patch *previous = NULL;
@@ -3524,7 +3530,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
3530 return error(_("%s: %s"), old_name, strerror(errno));
3531 }
3532
3527 - if (check_index && !previous) {
3533 + if (state->check_index && !previous) {
3534 int pos = cache_name_pos(old_name, strlen(old_name));
3535 if (pos < 0) {
3536 if (patch->is_new < 0)
@@ -3574,11 +3580,13 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
3580 #define EXISTS_IN_INDEX 1
3581 #define EXISTS_IN_WORKTREE 2
3582
3577 -static int check_to_create(const char *new_name, int ok_if_exists)
3583 +static int check_to_create(struct apply_state *state,
3584 + const char *new_name,
3585 + int ok_if_exists)
3586 {
3587 struct stat nst;
3588
3581 - if (check_index &&
3589 + if (state->check_index &&
3590 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3591 !ok_if_exists)
3592 return EXISTS_IN_INDEX;
@@ -3654,7 +3662,7 @@ static void prepare_symlink_changes(struct patch *patch)
3662 }
3663 }
3664
3657 -static int path_is_beyond_symlink_1(struct strbuf *name)
3665 +static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3666 {
3667 do {
3668 unsigned int change;
@@ -3675,7 +3683,7 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
3683 continue;
3684
3685 /* otherwise, check the preimage */
3678 - if (check_index) {
3686 + if (state->check_index) {
3687 struct cache_entry *ce;
3688
3689 ce = cache_file_exists(name->buf, name->len, ignore_case);
@@ -3690,14 +3698,14 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
3698 return 0;
3699 }
3700
3693 -static int path_is_beyond_symlink(const char *name_)
3701 +static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
3702 {
3703 int ret;
3704 struct strbuf name = STRBUF_INIT;
3705
3706 assert(*name_ != '\0');
3707 strbuf_addstr(&name, name_);
3700 - ret = path_is_beyond_symlink_1(&name);
3708 + ret = path_is_beyond_symlink_1(state, &name);
3709 strbuf_release(&name);
3710
3711 return ret;
@@ -3737,7 +3745,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
3745
3746 patch->rejected = 1; /* we will drop this after we succeed */
3747
3740 - status = check_preimage(patch, &ce, &st);
3748 + status = check_preimage(state, patch, &ce, &st);
3749 if (status)
3750 return status;
3751 old_name = patch->old_name;
@@ -3764,7 +3772,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
3772
3773 if (new_name &&
3774 ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
3767 - int err = check_to_create(new_name, ok_if_exists);
3775 + int err = check_to_create(state, new_name, ok_if_exists);
3776
3777 if (err && threeway) {
3778 patch->direct_to_threeway = 1;
@@ -3819,7 +3827,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
3827 * is not deposited to a path that is beyond a symbolic link
3828 * here.
3829 */
3822 - if (!patch->is_delete && path_is_beyond_symlink(patch->new_name))
3830 + if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
3831 return error(_("affected file '%s' is beyond a symbolic link"),
3832 patch->new_name);
3833
@@ -4431,11 +4439,11 @@ static int apply_patch(struct apply_state *state,
4439 if (whitespace_error && (ws_error_action == die_on_ws_error))
4440 apply = 0;
4441
4434 - update_index = check_index && apply;
4442 + update_index = state->check_index && apply;
4443 if (update_index && newfd < 0)
4444 newfd = hold_locked_index(&lock_file, 1);
4445
4438 - if (check_index) {
4446 + if (state->check_index) {
4447 if (read_cache() < 0)
4448 die(_("unable to read index file"));
4449 }
@@ -4581,7 +4589,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4589 N_("instead of applying the patch, output a summary for the input")),
4590 OPT_BOOL(0, "check", &state.check,
4591 N_("instead of applying the patch, see if the patch is applicable")),
4584 - OPT_BOOL(0, "index", &check_index,
4592 + OPT_BOOL(0, "index", &state.check_index,
4593 N_("make sure the patch is applicable to the current index")),
4594 OPT_BOOL(0, "cached", &cached,
4595 N_("apply a patch without touching the working tree")),
@@ -4640,20 +4648,20 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4648 if (threeway) {
4649 if (is_not_gitdir)
4650 die(_("--3way outside a repository"));
4643 - check_index = 1;
4651 + state.check_index = 1;
4652 }
4653 if (apply_with_reject)
4654 apply = apply_verbosely = 1;
4655 if (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))
4656 apply = 0;
4649 - if (check_index && is_not_gitdir)
4657 + if (state.check_index && is_not_gitdir)
4658 die(_("--index outside a repository"));
4659 if (cached) {
4660 if (is_not_gitdir)
4661 die(_("--cached outside a repository"));
4654 - check_index = 1;
4662 + state.check_index = 1;
4663 }
4656 - if (check_index)
4664 + if (state.check_index)
4665 unsafe_paths = 0;
4666
4667 for (i = 0; i < argc; i++) {