builtin/apply: move 'apply' global into 'struct apply_state'
[gitweb.git] / builtin / apply.c
index f174a42a557dffa78a386ce8795698d54830f657..c0c18ce9fd2197969444f52539d516832239cdf0 100644 (file)
@@ -26,6 +26,7 @@ struct apply_state {
        int prefix_length;
 
        /* These control what gets looked at and modified */
+       int apply; /* this is not a dry-run */
        int cached; /* apply to the index only */
        int check; /* preimage must match working tree, don't actually apply */
        int check_index; /* preimage must match the indexed version */
@@ -41,23 +42,22 @@ struct apply_state {
        int apply_in_reverse;
        int apply_with_reject;
        int apply_verbosely;
+       int no_add;
+       int threeway;
        int unidiff_zero;
+       int unsafe_paths;
+
+       /* Other non boolean parameters */
+       const char *fake_ancestor;
+       int line_termination;
+       unsigned int p_context;
 };
 
-/*
- *  --index-info shows the old and new index info for paths if available.
- */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int apply = 1;
-static int no_add;
-static int threeway;
-static int unsafe_paths;
-static const char *fake_ancestor;
-static int line_termination = '\n';
-static unsigned int p_context = UINT_MAX;
+
 static const char * const apply_usage[] = {
        N_("git apply [<options>] [<patch>...]"),
        NULL
@@ -127,10 +127,11 @@ static void parse_ignorewhitespace_option(const char *option)
        die(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
-static void set_default_whitespace_mode(const char *whitespace_option)
+static void set_default_whitespace_mode(struct apply_state *state,
+                                       const char *whitespace_option)
 {
        if (!whitespace_option && !apply_default_whitespace)
-               ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
+               ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
 /*
@@ -2059,7 +2060,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
                 * without metadata change.  A binary patch appears
                 * empty to us here.
                 */
-               if ((apply || state->check) &&
+               if ((state->apply || state->check) &&
                    (!patch->is_binary && !metadata_changes(patch)))
                        die(_("patch with only garbage at line %d"), state_linenr);
        }
@@ -2782,7 +2783,7 @@ static int apply_one_fragment(struct apply_state *state,
                /* Fall-through for ' ' */
                case '+':
                        /* --no-add does not add new lines */
-                       if (first == '+' && no_add)
+                       if (first == '+' && state->no_add)
                                break;
 
                        start = newlines.len;
@@ -2873,7 +2874,7 @@ static int apply_one_fragment(struct apply_state *state,
                        break;
 
                /* Am I at my context limits? */
-               if ((leading <= p_context) && (trailing <= p_context))
+               if ((leading <= state->p_context) && (trailing <= state->p_context))
                        break;
                if (match_beginning || match_end) {
                        match_beginning = match_end = 0;
@@ -2917,7 +2918,7 @@ static int apply_one_fragment(struct apply_state *state,
                         * apply_data->apply_fragments->apply_one_fragment
                         */
                        if (ws_error_action == die_on_ws_error)
-                               apply = 0;
+                               state->apply = 0;
                }
 
                if (state->apply_verbosely && applied_pos != pos) {
@@ -3491,7 +3492,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
        if (patch->direct_to_threeway ||
            apply_fragments(state, &image, patch) < 0) {
                /* Note: with --reject, apply_fragments() returns 0 */
-               if (!threeway || try_threeway(state, &image, patch, st, ce) < 0)
+               if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
                        return -1;
        }
        patch->result = image.buf;
@@ -3786,7 +3787,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
            ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
                int err = check_to_create(state, new_name, ok_if_exists);
 
-               if (err && threeway) {
+               if (err && state->threeway) {
                        patch->direct_to_threeway = 1;
                } else switch (err) {
                case 0:
@@ -3827,7 +3828,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
                }
        }
 
-       if (!unsafe_paths)
+       if (!state->unsafe_paths)
                die_on_unsafe_path(patch);
 
        /*
@@ -3977,7 +3978,8 @@ static void stat_patch_list(struct patch *patch)
        print_stat_summary(stdout, files, adds, dels);
 }
 
-static void numstat_patch_list(struct patch *patch)
+static void numstat_patch_list(struct apply_state *state,
+                              struct patch *patch)
 {
        for ( ; patch; patch = patch->next) {
                const char *name;
@@ -3986,7 +3988,7 @@ static void numstat_patch_list(struct patch *patch)
                        printf("-\t-\t");
                else
                        printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
-               write_name_quoted(name, stdout, line_termination);
+               write_name_quoted(name, stdout, state->line_termination);
        }
 }
 
@@ -4460,9 +4462,9 @@ static int apply_patch(struct apply_state *state,
                die(_("unrecognized input"));
 
        if (whitespace_error && (ws_error_action == die_on_ws_error))
-               apply = 0;
+               state->apply = 0;
 
-       state->update_index = state->check_index && apply;
+       state->update_index = state->check_index && state->apply;
        if (state->update_index && newfd < 0)
                newfd = hold_locked_index(&lock_file, 1);
 
@@ -4471,26 +4473,26 @@ static int apply_patch(struct apply_state *state,
                        die(_("unable to read index file"));
        }
 
-       if ((state->check || apply) &&
+       if ((state->check || state->apply) &&
            check_patch_list(state, list) < 0 &&
            !state->apply_with_reject)
                exit(1);
 
-       if (apply && write_out_results(state, list)) {
+       if (state->apply && write_out_results(state, list)) {
                if (state->apply_with_reject)
                        exit(1);
                /* with --3way, we still need to write the index out */
                return 1;
        }
 
-       if (fake_ancestor)
-               build_fake_ancestor(list, fake_ancestor);
+       if (state->fake_ancestor)
+               build_fake_ancestor(list, state->fake_ancestor);
 
        if (state->diffstat)
                stat_patch_list(list);
 
        if (state->numstat)
-               numstat_patch_list(list);
+               numstat_patch_list(state, list);
 
        if (state->summary)
                summary_patch_list(list);
@@ -4565,6 +4567,9 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
        memset(state, 0, sizeof(*state));
        state->prefix = prefix;
        state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+       state->apply = 1;
+       state->line_termination = '\n';
+       state->p_context = UINT_MAX;
 
        git_apply_config();
        if (apply_default_whitespace)
@@ -4600,7 +4605,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                { OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
                        N_("remove <num> leading slashes from traditional diff paths"),
                        0, option_parse_p },
-               OPT_BOOL(0, "no-add", &no_add,
+               OPT_BOOL(0, "no-add", &state.no_add,
                        N_("ignore additions made by the patch")),
                OPT_BOOL(0, "stat", &state.diffstat,
                        N_("instead of applying the patch, output diffstat for the input")),
@@ -4616,18 +4621,18 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                        N_("make sure the patch is applicable to the current index")),
                OPT_BOOL(0, "cached", &state.cached,
                        N_("apply a patch without touching the working tree")),
-               OPT_BOOL(0, "unsafe-paths", &unsafe_paths,
+               OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
                        N_("accept a patch that touches outside the working area")),
                OPT_BOOL(0, "apply", &force_apply,
                        N_("also apply the patch (use with --stat/--summary/--check)")),
-               OPT_BOOL('3', "3way", &threeway,
+               OPT_BOOL('3', "3way", &state.threeway,
                         N_( "attempt three-way merge if a patch does not apply")),
-               OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
+               OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
                        N_("build a temporary index based on embedded index information")),
                /* Think twice before adding "--nul" synonym to this */
-               OPT_SET_INT('z', NULL, &line_termination,
+               OPT_SET_INT('z', NULL, &state.line_termination,
                        N_("paths are separated with NUL character"), '\0'),
-               OPT_INTEGER('C', NULL, &p_context,
+               OPT_INTEGER('C', NULL, &state.p_context,
                                N_("ensure at least <n> lines of context match")),
                { OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),
                        N_("detect new or modified lines that have whitespace errors"),
@@ -4664,19 +4669,19 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
                        apply_usage, 0);
 
-       if (state.apply_with_reject && threeway)
+       if (state.apply_with_reject && state.threeway)
                die("--reject and --3way cannot be used together.");
-       if (state.cached && threeway)
+       if (state.cached && state.threeway)
                die("--cached and --3way cannot be used together.");
-       if (threeway) {
+       if (state.threeway) {
                if (is_not_gitdir)
                        die(_("--3way outside a repository"));
                state.check_index = 1;
        }
        if (state.apply_with_reject)
-               apply = state.apply_verbosely = 1;
-       if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || fake_ancestor))
-               apply = 0;
+               state.apply = state.apply_verbosely = 1;
+       if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))
+               state.apply = 0;
        if (state.check_index && is_not_gitdir)
                die(_("--index outside a repository"));
        if (state.cached) {
@@ -4685,7 +4690,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                state.check_index = 1;
        }
        if (state.check_index)
-               unsafe_paths = 0;
+               state.unsafe_paths = 0;
 
        for (i = 0; i < argc; i++) {
                const char *arg = argv[i];
@@ -4704,11 +4709,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                if (fd < 0)
                        die_errno(_("can't open patch '%s'"), arg);
                read_stdin = 0;
-               set_default_whitespace_mode(whitespace_option);
+               set_default_whitespace_mode(&state, whitespace_option);
                errs |= apply_patch(&state, fd, arg, options);
                close(fd);
        }
-       set_default_whitespace_mode(whitespace_option);
+       set_default_whitespace_mode(&state, whitespace_option);
        if (read_stdin)
                errs |= apply_patch(&state, 0, "<stdin>", options);
        if (whitespace_error) {
@@ -4726,7 +4731,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                               "%d lines add whitespace errors.",
                               whitespace_error),
                            whitespace_error);
-               if (applied_after_fixing_ws && apply)
+               if (applied_after_fixing_ws && state.apply)
                        warning("%d line%s applied after"
                                " fixing whitespace errors.",
                                applied_after_fixing_ws,