sequencer: lib'ify checkout_fast_forward()
[gitweb.git] / builtin / apply.c
index 5027f1b504a0f1892369a5ccc070e61478018683..1a488f9e888b6e9ebb1357a6cf3f7f007cf732fe 100644 (file)
@@ -52,6 +52,10 @@ struct apply_state {
        const char *prefix;
        int prefix_length;
 
+       /* These are lock_file related */
+       struct lock_file *lock_file;
+       int newfd;
+
        /* These control what gets looked at and modified */
        int apply; /* this is not a dry-run */
        int cached; /* apply to the index only */
@@ -114,8 +118,6 @@ struct apply_state {
        int applied_after_fixing_ws;
 };
 
-static int newfd = -1;
-
 static const char * const apply_usage[] = {
        N_("git apply [<options>] [<patch>...]"),
        NULL
@@ -462,7 +464,7 @@ static int is_dev_null(const char *str)
 #define TERM_SPACE     1
 #define TERM_TAB       2
 
-static int name_terminate(const char *name, int namelen, int c, int terminate)
+static int name_terminate(int c, int terminate)
 {
        if (c == ' ' && !(terminate & TERM_SPACE))
                return 0;
@@ -698,7 +700,7 @@ static char *find_name_common(struct apply_state *state,
                if (!end && isspace(c)) {
                        if (c == '\n')
                                break;
-                       if (name_terminate(start, line-start, c, terminate))
+                       if (name_terminate(c, terminate))
                                break;
                }
                line++;
@@ -3357,7 +3359,7 @@ static int load_patch_target(struct apply_state *state,
 {
        if (state->cached || state->check_index) {
                if (read_file_or_gitlink(ce, buf))
-                       return error(_("read of %s failed"), name);
+                       return error(_("failed to read %s"), name);
        } else if (name) {
                if (S_ISGITLINK(expected_mode)) {
                        if (ce)
@@ -3368,7 +3370,7 @@ static int load_patch_target(struct apply_state *state,
                        return error(_("reading from '%s' beyond a symbolic link"), name);
                } else {
                        if (read_old_data(st, name, buf))
-                               return error(_("read of %s failed"), name);
+                               return error(_("failed to read %s"), name);
                }
        }
        return 0;
@@ -3414,7 +3416,7 @@ static int load_preimage(struct apply_state *state,
                        free_fragment_list(patch->fragments);
                        patch->fragments = NULL;
                } else if (status) {
-                       return error(_("read of %s failed"), patch->old_name);
+                       return error(_("failed to read %s"), patch->old_name);
                }
        }
 
@@ -4546,8 +4548,8 @@ static int apply_patch(struct apply_state *state,
                state->apply = 0;
 
        state->update_index = state->check_index && state->apply;
-       if (state->update_index && newfd < 0)
-               newfd = hold_locked_index(&lock_file, 1);
+       if (state->update_index && state->newfd < 0)
+               state->newfd = hold_locked_index(state->lock_file, 1);
 
        if (state->check_index) {
                if (read_cache() < 0)
@@ -4648,11 +4650,15 @@ static int option_parse_directory(const struct option *opt,
        return 0;
 }
 
-static void init_apply_state(struct apply_state *state, const char *prefix)
+static void init_apply_state(struct apply_state *state,
+                            const char *prefix,
+                            struct lock_file *lock_file)
 {
        memset(state, 0, sizeof(*state));
        state->prefix = prefix;
        state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+       state->lock_file = lock_file;
+       state->newfd = -1;
        state->apply = 1;
        state->line_termination = '\n';
        state->p_value = 1;
@@ -4661,6 +4667,9 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
        state->ws_error_action = warn_on_ws_error;
        state->ws_ignore_action = ignore_ws_none;
        state->linenr = 1;
+       string_list_init(&state->fn_table, 0);
+       string_list_init(&state->limit_by_name, 0);
+       string_list_init(&state->symlink_changes, 0);
        strbuf_init(&state->root, 0);
 
        git_apply_config();
@@ -4705,6 +4714,8 @@ static void check_apply_state(struct apply_state *state, int force_apply)
        }
        if (state->check_index)
                state->unsafe_paths = 0;
+       if (!state->lock_file)
+               die("BUG: state->lock_file should not be NULL");
 }
 
 static int apply_all_patches(struct apply_state *state,
@@ -4769,8 +4780,9 @@ static int apply_all_patches(struct apply_state *state,
        }
 
        if (state->update_index) {
-               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+               if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
                        die(_("Unable to write new index file"));
+               state->newfd = -1;
        }
 
        return !!errs;
@@ -4852,7 +4864,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
-       init_apply_state(&state, prefix);
+       init_apply_state(&state, prefix, &lock_file);
 
        argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
                        apply_usage, 0);