sequencer (rebase -i): allow continuing with staged changes
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Mon, 2 Jan 2017 15:27:21 +0000 (16:27 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Jan 2017 22:57:29 +0000 (14:57 -0800)
When an interactive rebase is interrupted, the user may stage changes
before continuing, and we need to commit those changes in that case.

Please note that the nested "if" added to the sequencer_continue() is
not combined into a single "if" because it will be extended with an
"else" clause in a later patch in this patch series.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sequencer.c
index 991388260354f26e016c449d0fc6de9e4cf7a71c..69301fecc6cf7d9392f5a2d1c1ba31ace9f30a23 100644 (file)
@@ -1826,6 +1826,42 @@ static int continue_single_pick(void)
        return run_command_v_opt(argv, RUN_GIT_CMD);
 }
 
+static int commit_staged_changes(struct replay_opts *opts)
+{
+       int amend = 0;
+
+       if (has_unstaged_changes(1))
+               return error(_("cannot rebase: You have unstaged changes."));
+       if (!has_uncommitted_changes(0))
+               return 0;
+
+       if (file_exists(rebase_path_amend())) {
+               struct strbuf rev = STRBUF_INIT;
+               unsigned char head[20], to_amend[20];
+
+               if (get_sha1("HEAD", head))
+                       return error(_("cannot amend non-existing commit"));
+               if (!read_oneliner(&rev, rebase_path_amend(), 0))
+                       return error(_("invalid file: '%s'"), rebase_path_amend());
+               if (get_sha1_hex(rev.buf, to_amend))
+                       return error(_("invalid contents: '%s'"),
+                               rebase_path_amend());
+               if (hashcmp(head, to_amend))
+                       return error(_("\nYou have uncommitted changes in your "
+                                      "working tree. Please, commit them\n"
+                                      "first and then run 'git rebase "
+                                      "--continue' again."));
+
+               strbuf_release(&rev);
+               amend = 1;
+       }
+
+       if (run_git_commit(rebase_path_message(), opts, 1, 1, amend, 0))
+               return error(_("could not commit staged changes."));
+       unlink(rebase_path_amend());
+       return 0;
+}
+
 int sequencer_continue(struct replay_opts *opts)
 {
        struct todo_list todo_list = TODO_LIST_INIT;
@@ -1834,6 +1870,10 @@ int sequencer_continue(struct replay_opts *opts)
        if (read_and_refresh_cache(opts))
                return -1;
 
+       if (is_rebase_i(opts)) {
+               if (commit_staged_changes(opts))
+                       return -1;
+       }
        if (!file_exists(get_todo_path(opts)))
                return continue_single_pick();
        if (read_populate_opts(opts))