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

To libify the apply functionality the 'cached' 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 885eefb12d35e416e637a92d8a6e7ac17eefd5cc
1 file changed +19 -16
builtin/apply.c
+19 -16
@@ -26,6 +26,7 @@ struct apply_state {
26 int prefix_length;
27
28 /* These control what gets looked at and modified */
29 + int cached; /* apply to the index only */
30 int check; /* preimage must match working tree, don't actually apply */
31 int check_index; /* preimage must match the indexed version */
32 int update_index; /* check_index && apply */
@@ -42,13 +43,11 @@ struct apply_state {
43 * --stat does just a diffstat, and doesn't actually apply
44 * --numstat does numeric diffstat, and doesn't actually apply
45 * --index-info shows the old and new index info for paths if available.
45 - * --cached updates only the cache without ever touching the working tree.
46 */
47 static int newfd = -1;
48
49 static int state_p_value = 1;
50 static int p_value_known;
51 -static int cached;
51 static int diffstat;
52 static int numstat;
53 static int summary;
@@ -3264,7 +3263,7 @@ static int load_patch_target(struct apply_state *state,
3263 const char *name,
3264 unsigned expected_mode)
3265 {
3267 - if (cached || state->check_index) {
3266 + if (state->cached || state->check_index) {
3267 if (read_file_or_gitlink(ce, buf))
3268 return error(_("read of %s failed"), name);
3269 } else if (name) {
@@ -3537,7 +3536,7 @@ static int check_preimage(struct apply_state *state,
3536 return error(_("path %s has been renamed/deleted"), old_name);
3537 if (previous) {
3538 st_mode = previous->new_mode;
3540 - } else if (!cached) {
3539 + } else if (!state->cached) {
3540 stat_ret = lstat(old_name, st);
3541 if (stat_ret && errno != ENOENT)
3542 return error(_("%s: %s"), old_name, strerror(errno));
@@ -3555,9 +3554,9 @@ static int check_preimage(struct apply_state *state,
3554 if (checkout_target(&the_index, *ce, st))
3555 return -1;
3556 }
3558 - if (!cached && verify_index_match(*ce, st))
3557 + if (!state->cached && verify_index_match(*ce, st))
3558 return error(_("%s: does not match index"), old_name);
3560 - if (cached)
3559 + if (state->cached)
3560 st_mode = (*ce)->ce_mode;
3561 } else if (stat_ret < 0) {
3562 if (patch->is_new < 0)
@@ -3565,7 +3564,7 @@ static int check_preimage(struct apply_state *state,
3564 return error(_("%s: %s"), old_name, strerror(errno));
3565 }
3566
3568 - if (!cached && !previous)
3567 + if (!state->cached && !previous)
3568 st_mode = ce_mode_from_stat(*ce, st->st_mode);
3569
3570 if (patch->is_new < 0)
@@ -3603,7 +3602,7 @@ static int check_to_create(struct apply_state *state,
3602 cache_name_pos(new_name, strlen(new_name)) >= 0 &&
3603 !ok_if_exists)
3604 return EXISTS_IN_INDEX;
3606 - if (cached)
3605 + if (state->cached)
3606 return 0;
3607
3608 if (!lstat(new_name, &nst)) {
@@ -4097,7 +4096,7 @@ static void remove_file(struct apply_state *state, struct patch *patch, int rmdi
4096 if (remove_file_from_cache(patch->old_name) < 0)
4097 die(_("unable to remove %s from index"), patch->old_name);
4098 }
4100 - if (!cached) {
4099 + if (!state->cached) {
4100 if (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {
4101 remove_path(patch->old_name);
4102 }
@@ -4130,7 +4129,7 @@ static void add_index_file(struct apply_state *state,
4129 get_sha1_hex(s, ce->sha1))
4130 die(_("corrupt patch for submodule %s"), path);
4131 } else {
4133 - if (!cached) {
4132 + if (!state->cached) {
4133 if (lstat(path, &st) < 0)
4134 die_errno(_("unable to stat newly created file '%s'"),
4135 path);
@@ -4182,9 +4181,13 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
4181 * which is true 99% of the time anyway. If they don't,
4182 * we create them and try again.
4183 */
4185 -static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
4184 +static void create_one_file(struct apply_state *state,
4185 + char *path,
4186 + unsigned mode,
4187 + const char *buf,
4188 + unsigned long size)
4189 {
4187 - if (cached)
4190 + if (state->cached)
4191 return;
4192 if (!try_create_file(path, mode, buf, size))
4193 return;
@@ -4262,7 +4265,7 @@ static void create_file(struct apply_state *state, struct patch *patch)
4265
4266 if (!mode)
4267 mode = S_IFREG | 0644;
4265 - create_one_file(path, mode, buf, size);
4268 + create_one_file(state, path, mode, buf, size);
4269
4270 if (patch->conflicted_threeway)
4271 add_conflicted_stages_file(state, patch);
@@ -4611,7 +4614,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4614 N_("instead of applying the patch, see if the patch is applicable")),
4615 OPT_BOOL(0, "index", &state.check_index,
4616 N_("make sure the patch is applicable to the current index")),
4614 - OPT_BOOL(0, "cached", &cached,
4617 + OPT_BOOL(0, "cached", &state.cached,
4618 N_("apply a patch without touching the working tree")),
4619 OPT_BOOL(0, "unsafe-paths", &unsafe_paths,
4620 N_("accept a patch that touches outside the working area")),
@@ -4663,7 +4666,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4666
4667 if (state.apply_with_reject && threeway)
4668 die("--reject and --3way cannot be used together.");
4666 - if (cached && threeway)
4669 + if (state.cached && threeway)
4670 die("--cached and --3way cannot be used together.");
4671 if (threeway) {
4672 if (is_not_gitdir)
@@ -4676,7 +4679,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
4679 apply = 0;
4680 if (state.check_index && is_not_gitdir)
4681 die(_("--index outside a repository"));
4679 - if (cached) {
4682 + if (state.cached) {
4683 if (is_not_gitdir)
4684 die(_("--cached outside a repository"));
4685 state.check_index = 1;