Merge branch 'ma/mailing-list-address-in-git-help' into maint
[gitweb.git] / sequencer.c
index f74dafb3259771571a67e6cbeaf22c4129a020d9..f388405b4e0d2c260f74777323a738ac845195e0 100644 (file)
@@ -225,13 +225,16 @@ static const char *get_todo_path(const struct replay_opts *opts)
  * Returns 3 when sob exists within conforming footer as last entry
  */
 static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
-       int ignore_footer)
+       size_t ignore_footer)
 {
+       struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
        struct trailer_info info;
-       int i;
+       size_t i;
        int found_sob = 0, found_sob_last = 0;
 
-       trailer_info_get(&info, sb->buf);
+       opts.no_divider = 1;
+
+       trailer_info_get(&info, sb->buf, &opts);
 
        if (info.trailer_start == info.trailer_end)
                return 0;
@@ -639,7 +642,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 +650,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 +688,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);
@@ -720,7 +745,7 @@ static const char *read_author_ident(struct strbuf *buf)
        /* dequote values and construct ident line in-place */
        for (in = buf->buf; i < 3 && in - buf->buf < buf->len; i++) {
                if (!skip_prefix(in, keys[i], (const char **)&in)) {
-                       warning(_("could not parse '%s' (looking for '%s'"),
+                       warning(_("could not parse '%s' (looking for '%s')"),
                                rebase_path_author_script(), keys[i]);
                        return NULL;
                }
@@ -798,11 +823,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");
 
@@ -870,7 +902,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
        if ((flags & ALLOW_EMPTY))
                argv_array_push(&cmd.args, "--allow-empty");
 
-       if (opts->allow_empty_message)
+       if (!(flags & EDIT_MSG))
                argv_array_push(&cmd.args, "--allow-empty-message");
 
        if (cmd.err == -1) {
@@ -1253,7 +1285,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
                commit_list_insert(current_head, &parents);
        }
 
-       if (write_cache_as_tree(&tree, 0, NULL)) {
+       if (write_index_as_tree(&tree, &the_index, get_index_file(), 0, NULL)) {
                res = error(_("git write-tree failed to write a tree"));
                goto out;
        }
@@ -1284,7 +1316,7 @@ static int try_to_commit(struct strbuf *msg, const char *author,
 
        if (cleanup != COMMIT_MSG_CLEANUP_NONE)
                strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL);
-       if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) {
+       if ((flags & EDIT_MSG) && message_is_empty(msg, cleanup)) {
                res = 1; /* run 'git commit' to display error message */
                goto out;
        }
@@ -1552,13 +1584,13 @@ static int update_squash_messages(enum todo_command command,
                unlink(rebase_path_fixup_msg());
                strbuf_addf(&buf, "\n%c ", comment_line_char);
                strbuf_addf(&buf, _("This is the commit message #%d:"),
-                           ++opts->current_fixup_count);
+                           ++opts->current_fixup_count + 1);
                strbuf_addstr(&buf, "\n\n");
                strbuf_addstr(&buf, body);
        } else if (command == TODO_FIXUP) {
                strbuf_addf(&buf, "\n%c ", comment_line_char);
                strbuf_addf(&buf, _("The commit message #%d will be skipped:"),
-                           ++opts->current_fixup_count);
+                           ++opts->current_fixup_count + 1);
                strbuf_addstr(&buf, "\n\n");
                strbuf_add_commented_lines(&buf, body, strlen(body));
        } else
@@ -1639,7 +1671,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                 * that represents the "current" state for merge-recursive
                 * to work on.
                 */
-               if (write_cache_as_tree(&head, 0, NULL))
+               if (write_index_as_tree(&head, &the_index, get_index_file(), 0, NULL))
                        return error(_("your index file is unmerged."));
        } else {
                unborn = get_oid("HEAD", &head);
@@ -2610,8 +2642,13 @@ static int error_with_patch(struct commit *commit,
        const char *subject, int subject_len,
        struct replay_opts *opts, int exit_code, int to_amend)
 {
-       if (make_patch(commit, opts))
-               return -1;
+       if (commit) {
+               if (make_patch(commit, opts))
+                       return -1;
+       } else if (copy_file(rebase_path_message(),
+                            git_path_merge_msg(the_repository), 0666))
+               return error(_("unable to copy '%s' to '%s'"),
+                            git_path_merge_msg(the_repository), rebase_path_message());
 
        if (to_amend) {
                if (intend_to_amend())
@@ -2626,9 +2663,18 @@ static int error_with_patch(struct commit *commit,
                          "\n"
                          "  git rebase --continue\n"),
                        gpg_sign_opt_quoted(opts));
-       } else if (exit_code)
-               fprintf_ln(stderr, _("Could not apply %s... %.*s"),
-                       short_commit_name(commit), subject_len, subject);
+       } else if (exit_code) {
+               if (commit)
+                       fprintf_ln(stderr, _("Could not apply %s... %.*s"),
+                                  short_commit_name(commit), subject_len, subject);
+               else
+                       /*
+                        * We don't have the hash of the parent so
+                        * just print the line from the todo file.
+                        */
+                       fprintf_ln(stderr, _("Could not merge %.*s"),
+                                  subject_len, subject);
+       }
 
        return exit_code;
 }
@@ -3564,9 +3610,20 @@ static int commit_staged_changes(struct replay_opts *opts,
                 * the commit message and if there was a squash, let the user
                 * edit it.
                 */
-               if (is_clean && !oidcmp(&head, &to_amend) &&
-                   opts->current_fixup_count > 0 &&
-                   file_exists(rebase_path_stopped_sha())) {
+               if (!is_clean || !opts->current_fixup_count)
+                       ; /* this is not the final fixup */
+               else if (oidcmp(&head, &to_amend) ||
+                        !file_exists(rebase_path_stopped_sha())) {
+                       /* was a final fixup or squash done manually? */
+                       if (!is_fixup(peek_command(todo_list, 0))) {
+                               unlink(rebase_path_fixup_msg());
+                               unlink(rebase_path_squash_msg());
+                               unlink(rebase_path_current_fixups());
+                               strbuf_reset(&opts->current_fixups);
+                               opts->current_fixup_count = 0;
+                       }
+               } else {
+                       /* we are in a fixup/squash chain */
                        const char *p = opts->current_fixups.buf;
                        int len = opts->current_fixups.len;
 
@@ -3785,7 +3842,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
        return res;
 }
 
-void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
+void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
 {
        unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
        struct strbuf sob = STRBUF_INIT;
@@ -4255,10 +4312,9 @@ int sequencer_add_exec_commands(const char *commands)
 {
        const char *todo_file = rebase_path_todo();
        struct todo_list todo_list = TODO_LIST_INIT;
-       struct todo_item *item;
        struct strbuf *buf = &todo_list.buf;
        size_t offset = 0, commands_len = strlen(commands);
-       int i, first;
+       int i, insert;
 
        if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
                return error(_("could not read '%s'."), todo_file);
@@ -4268,19 +4324,40 @@ int sequencer_add_exec_commands(const char *commands)
                return error(_("unusable todo list: '%s'"), todo_file);
        }
 
-       first = 1;
-       /* insert <commands> before every pick except the first one */
-       for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
-               if (item->command == TODO_PICK && !first) {
-                       strbuf_insert(buf, item->offset_in_buf + offset,
-                                     commands, commands_len);
+       /*
+        * Insert <commands> after every pick. Here, fixup/squash chains
+        * are considered part of the pick, so we insert the commands *after*
+        * those chains if there are any.
+        */
+       insert = -1;
+       for (i = 0; i < todo_list.nr; i++) {
+               enum todo_command command = todo_list.items[i].command;
+
+               if (insert >= 0) {
+                       /* skip fixup/squash chains */
+                       if (command == TODO_COMMENT)
+                               continue;
+                       else if (is_fixup(command)) {
+                               insert = i + 1;
+                               continue;
+                       }
+                       strbuf_insert(buf,
+                                     todo_list.items[insert].offset_in_buf +
+                                     offset, commands, commands_len);
                        offset += commands_len;
+                       insert = -1;
                }
-               first = 0;
+
+               if (command == TODO_PICK || command == TODO_MERGE)
+                       insert = i + 1;
        }
 
-       /* append final <commands> */
-       strbuf_add(buf, commands, commands_len);
+       /* insert or append final <commands> */
+       if (insert >= 0 && insert < todo_list.nr)
+               strbuf_insert(buf, todo_list.items[insert].offset_in_buf +
+                             offset, commands, commands_len);
+       else if (insert >= 0 || !offset)
+               strbuf_add(buf, commands, commands_len);
 
        i = write_message(buf->buf, buf->len, todo_file, 0);
        todo_list_release(&todo_list);