sequencer (rebase -i): implement the 'edit' command
[gitweb.git] / sequencer.c
index 28061dcb78b5db5711f77da1026c0cb81acee27b..b138a3906cf386d3335aba01beadcdb447cd8721 100644 (file)
@@ -17,6 +17,7 @@
 #include "argv-array.h"
 #include "quote.h"
 #include "trailer.h"
+#include "log-tree.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
@@ -28,32 +29,58 @@ GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
 static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
 static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
 static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
+static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety")
 
+static GIT_PATH_FUNC(rebase_path, "rebase-merge")
+/*
+ * The file containing rebase commands, comments, and empty lines.
+ * This file is created by "git rebase -i" then edited by the user. As
+ * the lines are processed, they are removed from the front of this
+ * file and written to the tail of 'done'.
+ */
+static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo")
 /*
  * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
  * GIT_AUTHOR_DATE that will be used for the commit that is currently
  * being rebased.
  */
 static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+/*
+ * When an "edit" rebase command is being processed, the SHA1 of the
+ * commit to be edited is recorded in this file.  When "git rebase
+ * --continue" is executed, if there are any staged changes then they
+ * will be amended to the HEAD commit, but only provided the HEAD
+ * commit is still the commit to be edited.  When any other rebase
+ * command is processed, this file is deleted.
+ */
+static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend")
+/*
+ * When we stop at a given patch via the "edit" command, this file contains
+ * the abbreviated commit name of the corresponding patch.
+ */
+static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
 /*
  * The following files are written by git-rebase just after parsing the
  * command-line (and are only consumed, not modified, by the sequencer).
  */
 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
 
-/* We will introduce the 'interactive rebase' mode later */
 static inline int is_rebase_i(const struct replay_opts *opts)
 {
-       return 0;
+       return opts->action == REPLAY_INTERACTIVE_REBASE;
 }
 
 static const char *get_dir(const struct replay_opts *opts)
 {
+       if (is_rebase_i(opts))
+               return rebase_path();
        return git_path_seq_dir();
 }
 
 static const char *get_todo_path(const struct replay_opts *opts)
 {
+       if (is_rebase_i(opts))
+               return rebase_path_todo();
        return git_path_todo_file();
 }
 
@@ -121,7 +148,15 @@ int sequencer_remove_state(struct replay_opts *opts)
 
 static const char *action_name(const struct replay_opts *opts)
 {
-       return opts->action == REPLAY_REVERT ? N_("revert") : N_("cherry-pick");
+       switch (opts->action) {
+       case REPLAY_REVERT:
+               return N_("revert");
+       case REPLAY_PICK:
+               return N_("cherry-pick");
+       case REPLAY_INTERACTIVE_REBASE:
+               return N_("rebase -i");
+       }
+       die(_("Unknown action: %d"), opts->action);
 }
 
 struct commit_message {
@@ -263,6 +298,20 @@ static int error_dirty_index(struct replay_opts *opts)
        return -1;
 }
 
+static void update_abort_safety_file(void)
+{
+       struct object_id head;
+
+       /* Do nothing on a single-pick */
+       if (!file_exists(git_path_seq_dir()))
+               return;
+
+       if (!get_oid("HEAD", &head))
+               write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head));
+       else
+               write_file(git_path_abort_safety_file(), "%s", "");
+}
+
 static int fast_forward_to(const unsigned char *to, const unsigned char *from,
                        int unborn, struct replay_opts *opts)
 {
@@ -292,6 +341,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,
        strbuf_release(&sb);
        strbuf_release(&err);
        ref_transaction_free(transaction);
+       update_abort_safety_file();
        return 0;
 }
 
@@ -348,7 +398,9 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
 
        if (active_cache_changed &&
            write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
-               /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
+               /* TRANSLATORS: %s will be "revert", "cherry-pick" or
+                * "rebase -i".
+                */
                return error(_("%s: Unable to write new index file"),
                        _(action_name(opts)));
        rollback_lock_file(&index_lock);
@@ -570,14 +622,25 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
                return 1;
 }
 
+/*
+ * Note that ordering matters in this enum. Not only must it match the mapping
+ * below, it is also divided into several sections that matter.  When adding
+ * new commands, make sure you add it in the right section.
+ */
 enum todo_command {
+       /* commands that handle commits */
        TODO_PICK = 0,
-       TODO_REVERT
+       TODO_REVERT,
+       TODO_EDIT,
+       /* commands that do nothing but are counted for reporting progress */
+       TODO_NOOP
 };
 
 static const char *todo_command_strings[] = {
        "pick",
-       "revert"
+       "revert",
+       "edit",
+       "noop"
 };
 
 static const char *command_to_string(const enum todo_command command)
@@ -587,6 +650,10 @@ static const char *command_to_string(const enum todo_command command)
        die("Unknown command: %d", command);
 }
 
+static int is_noop(const enum todo_command command)
+{
+       return TODO_NOOP <= (size_t)command;
+}
 
 static int do_pick_commit(enum todo_command command, struct commit *commit,
                struct replay_opts *opts)
@@ -616,9 +683,8 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
        }
        discard_cache();
 
-       if (!commit->parents) {
+       if (!commit->parents)
                parent = NULL;
-       }
        else if (commit->parents->next) {
                /* Reverting or cherry-picking a merge commit */
                int cnt;
@@ -688,14 +754,9 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
                next = commit;
                next_label = msg.label;
 
-               /*
-                * Append the commit log message to msgbuf; it starts
-                * after the tree, parent, author, committer
-                * information followed by "\n\n".
-                */
-               p = strstr(msg.message, "\n\n");
-               if (p)
-                       strbuf_addstr(&msgbuf, skip_blank_lines(p + 2));
+               /* Append the commit log message to msgbuf. */
+               if (find_commit_subject(msg.message, &p))
+                       strbuf_addstr(&msgbuf, p);
 
                if (opts->record_origin) {
                        if (!has_conforming_footer(&msgbuf, NULL, 0))
@@ -766,6 +827,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 
 leave:
        free_message(commit, &msg);
+       update_abort_safety_file();
 
        return res;
 }
@@ -847,6 +909,14 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
        /* left-trim */
        bol += strspn(bol, " \t");
 
+       if (bol == eol || *bol == '\r' || *bol == comment_line_char) {
+               item->command = TODO_NOOP;
+               item->commit = NULL;
+               item->arg = bol;
+               item->arg_len = eol - bol;
+               return 0;
+       }
+
        for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
                if (skip_prefix(bol, todo_command_strings[i], &bol)) {
                        item->command = i;
@@ -855,6 +925,13 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
        if (i >= ARRAY_SIZE(todo_command_strings))
                return -1;
 
+       if (item->command == TODO_NOOP) {
+               item->commit = NULL;
+               item->arg = bol;
+               item->arg_len = eol - bol;
+               return 0;
+       }
+
        /* Eat up extra spaces/ tabs before object name */
        padding = strspn(bol, " \t");
        if (!padding)
@@ -1054,8 +1131,7 @@ static int create_seq_dir(void)
                error(_("a cherry-pick or revert is already in progress"));
                advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
                return -1;
-       }
-       else if (mkdir(git_path_seq_dir(), 0777) < 0)
+       } else if (mkdir(git_path_seq_dir(), 0777) < 0)
                return error_errno(_("could not create sequencer directory '%s'"),
                                   git_path_seq_dir());
        return 0;
@@ -1085,9 +1161,34 @@ static int save_head(const char *head)
        return 0;
 }
 
+static int rollback_is_safe(void)
+{
+       struct strbuf sb = STRBUF_INIT;
+       struct object_id expected_head, actual_head;
+
+       if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) {
+               strbuf_trim(&sb);
+               if (get_oid_hex(sb.buf, &expected_head)) {
+                       strbuf_release(&sb);
+                       die(_("could not parse %s"), git_path_abort_safety_file());
+               }
+               strbuf_release(&sb);
+       }
+       else if (errno == ENOENT)
+               oidclr(&expected_head);
+       else
+               die_errno(_("could not read '%s'"), git_path_abort_safety_file());
+
+       if (get_oid("HEAD", &actual_head))
+               oidclr(&actual_head);
+
+       return !oidcmp(&actual_head, &expected_head);
+}
+
 static int reset_for_rollback(const unsigned char *sha1)
 {
        const char *argv[4];    /* reset --merge <arg> + NULL */
+
        argv[0] = "reset";
        argv[1] = "--merge";
        argv[2] = sha1_to_hex(sha1);
@@ -1142,6 +1243,12 @@ int sequencer_rollback(struct replay_opts *opts)
                error(_("cannot abort from a branch yet to be born"));
                goto fail;
        }
+
+       if (!rollback_is_safe()) {
+               /* Do not error, just do not rollback */
+               warning(_("You seem to have moved HEAD. "
+                         "Not rewinding, check your HEAD!"));
+       } else
        if (reset_for_rollback(sha1))
                goto fail;
        strbuf_release(&buf);
@@ -1157,6 +1264,13 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
        const char *todo_path = get_todo_path(opts);
        int next = todo_list->current, offset, fd;
 
+       /*
+        * rebase -i writes "git-rebase-todo" without the currently executing
+        * command, appending it to "done" instead.
+        */
+       if (is_rebase_i(opts))
+               next++;
+
        fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
        if (fd < 0)
                return error_errno(_("could not lock '%s'"), todo_path);
@@ -1205,9 +1319,87 @@ static int save_opts(struct replay_opts *opts)
        return res;
 }
 
+static int make_patch(struct commit *commit, struct replay_opts *opts)
+{
+       struct strbuf buf = STRBUF_INIT;
+       struct rev_info log_tree_opt;
+       const char *subject, *p;
+       int res = 0;
+
+       p = short_commit_name(commit);
+       if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0)
+               return -1;
+
+       strbuf_addf(&buf, "%s/patch", get_dir(opts));
+       memset(&log_tree_opt, 0, sizeof(log_tree_opt));
+       init_revisions(&log_tree_opt, NULL);
+       log_tree_opt.abbrev = 0;
+       log_tree_opt.diff = 1;
+       log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH;
+       log_tree_opt.disable_stdin = 1;
+       log_tree_opt.no_commit_id = 1;
+       log_tree_opt.diffopt.file = fopen(buf.buf, "w");
+       log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER;
+       if (!log_tree_opt.diffopt.file)
+               res |= error_errno(_("could not open '%s'"), buf.buf);
+       else {
+               res |= log_tree_commit(&log_tree_opt, commit);
+               fclose(log_tree_opt.diffopt.file);
+       }
+       strbuf_reset(&buf);
+
+       strbuf_addf(&buf, "%s/message", get_dir(opts));
+       if (!file_exists(buf.buf)) {
+               const char *commit_buffer = get_commit_buffer(commit, NULL);
+               find_commit_subject(commit_buffer, &subject);
+               res |= write_message(subject, strlen(subject), buf.buf, 1);
+               unuse_commit_buffer(commit, commit_buffer);
+       }
+       strbuf_release(&buf);
+
+       return res;
+}
+
+static int intend_to_amend(void)
+{
+       unsigned char head[20];
+       char *p;
+
+       if (get_sha1("HEAD", head))
+               return error(_("cannot read HEAD"));
+
+       p = sha1_to_hex(head);
+       return write_message(p, strlen(p), rebase_path_amend(), 1);
+}
+
+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 (to_amend) {
+               if (intend_to_amend())
+                       return -1;
+
+               fprintf(stderr, "You can amend the commit now, with\n"
+                       "\n"
+                       "  git commit --amend %s\n"
+                       "\n"
+                       "Once you are satisfied with your changes, run\n"
+                       "\n"
+                       "  git rebase --continue\n", gpg_sign_opt_quoted(opts));
+       } else if (exit_code)
+               fprintf(stderr, "Could not apply %s... %.*s\n",
+                       short_commit_name(commit), subject_len, subject);
+
+       return exit_code;
+}
+
 static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 {
-       int res;
+       int res = 0;
 
        setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
        if (opts->allow_ff)
@@ -1220,12 +1412,33 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
                struct todo_item *item = todo_list->items + todo_list->current;
                if (save_todo(todo_list, opts))
                        return -1;
-               res = do_pick_commit(item->command, item->commit, opts);
+               if (item->command <= TODO_EDIT) {
+                       res = do_pick_commit(item->command, item->commit,
+                                       opts);
+                       if (item->command == TODO_EDIT) {
+                               struct commit *commit = item->commit;
+                               if (!res)
+                                       warning(_("stopped at %s... %.*s"),
+                                               short_commit_name(commit),
+                                               item->arg_len, item->arg);
+                               return error_with_patch(commit,
+                                       item->arg, item->arg_len, opts, res,
+                                       !res);
+                       }
+               } else if (!is_noop(item->command))
+                       return error(_("unknown command %d"), item->command);
+
                todo_list->current++;
                if (res)
                        return res;
        }
 
+       if (is_rebase_i(opts)) {
+               /* Stopped in the middle, as planned? */
+               if (todo_list->current < todo_list->nr)
+                       return 0;
+       }
+
        /*
         * Sequence of picks finished successfully; cleanup by
         * removing the .git/sequencer directory
@@ -1346,6 +1559,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
                return -1;
        if (save_opts(opts))
                return -1;
+       update_abort_safety_file();
        res = pick_commits(&todo_list, opts);
        todo_list_release(&todo_list);
        return res;