rerere.c: remove implicit dependency on the_index
[gitweb.git] / sequencer.c
index 84bf598c3e2be8d378a72174239a076cf1eeaf8a..d9ff75a24543cfb6f0e0795914c782c320317b99 100644 (file)
@@ -470,8 +470,8 @@ static int fast_forward_to(const struct object_id *to, const struct object_id *f
        struct strbuf sb = STRBUF_INIT;
        struct strbuf err = STRBUF_INIT;
 
-       read_cache();
-       if (checkout_fast_forward(from, to, 1))
+       read_index(&the_index);
+       if (checkout_fast_forward(the_repository, from, to, 1))
                return -1; /* the callee should have complained already */
 
        strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts)));
@@ -639,7 +639,7 @@ static int write_author_script(const char *message)
                else if (*message != '\'')
                        strbuf_addch(&buf, *(message++));
                else
-                       strbuf_addf(&buf, "'\\\\%c'", *(message++));
+                       strbuf_addf(&buf, "'\\%c'", *(message++));
        strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='");
        while (*message && *message != '\n' && *message != '\r')
                if (skip_prefix(message, "> ", &message))
@@ -647,19 +647,37 @@ static int write_author_script(const char *message)
                else if (*message != '\'')
                        strbuf_addch(&buf, *(message++));
                else
-                       strbuf_addf(&buf, "'\\\\%c'", *(message++));
+                       strbuf_addf(&buf, "'\\%c'", *(message++));
        strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@");
        while (*message && *message != '\n' && *message != '\r')
                if (*message != '\'')
                        strbuf_addch(&buf, *(message++));
                else
-                       strbuf_addf(&buf, "'\\\\%c'", *(message++));
+                       strbuf_addf(&buf, "'\\%c'", *(message++));
        strbuf_addch(&buf, '\'');
        res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1);
        strbuf_release(&buf);
        return res;
 }
 
+
+/*
+ * write_author_script() used to fail to terminate the last line with a "'" and
+ * also escaped "'" incorrectly as "'\\\\''" rather than "'\\''". We check for
+ * the terminating "'" on the last line to see how "'" has been escaped in case
+ * git was upgraded while rebase was stopped.
+ */
+static int quoting_is_broken(const char *s, size_t n)
+{
+       /* Skip any empty lines in case the file was hand edited */
+       while (n > 0 && s[--n] == '\n')
+               ; /* empty */
+       if (n > 0 && s[n] != '\'')
+               return 1;
+
+       return 0;
+}
+
 /*
  * Read a list of environment variable assignments (such as the author-script
  * file) into an environment block. Returns -1 on error, 0 otherwise.
@@ -667,14 +685,18 @@ static int write_author_script(const char *message)
 static int read_env_script(struct argv_array *env)
 {
        struct strbuf script = STRBUF_INIT;
-       int i, count = 0;
-       char *p, *p2;
+       int i, count = 0, sq_bug;
+       const char *p2;
+       char *p;
 
        if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
                return -1;
-
+       /* write_author_script() used to quote incorrectly */
+       sq_bug = quoting_is_broken(script.buf, script.len);
        for (p = script.buf; *p; p++)
-               if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+               if (sq_bug && skip_prefix(p, "'\\\\''", &p2))
+                       strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+               else if (skip_prefix(p, "'\\''", &p2))
                        strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
                else if (*p == '\'')
                        strbuf_splice(&script, p-- - script.buf, 1, "", 0);
@@ -798,11 +820,18 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 
        if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
                struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
-               const char *author = is_rebase_i(opts) ?
-                       read_author_ident(&script) : NULL;
+               const char *author = NULL;
                struct object_id root_commit, *cache_tree_oid;
                int res = 0;
 
+               if (is_rebase_i(opts)) {
+                       author = read_author_ident(&script);
+                       if (!author) {
+                               strbuf_release(&script);
+                               return -1;
+                       }
+               }
+
                if (!defmsg)
                        BUG("root commit without message");
 
@@ -1798,7 +1827,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 
                commit_list_insert(base, &common);
                commit_list_insert(next, &remotes);
-               res |= try_merge_command(opts->strategy,
+               res |= try_merge_command(the_repository, opts->strategy,
                                         opts->xopts_nr, (const char **)opts->xopts,
                                        common, oid_to_hex(&head), remotes);
                free_commit_list(common);
@@ -1827,7 +1856,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                      : _("could not apply %s... %s"),
                      short_commit_name(commit), msg.subject);
                print_advice(res == 1, opts);
-               rerere(opts->allow_rerere_auto);
+               repo_rerere(the_repository, opts->allow_rerere_auto);
                goto leave;
        }
 
@@ -3146,7 +3175,7 @@ static int do_merge(struct commit *commit, const char *arg, int arg_len,
 
        rollback_lock_file(&lock);
        if (ret)
-               rerere(opts->allow_rerere_auto);
+               repo_rerere(the_repository, opts->allow_rerere_auto);
        else
                /*
                 * In case of problems, we now want to return a positive