Skip to content

Commit 03b1380

Browse files
committed
sequencer (rebase -i): allow continuing with staged changes
When an interactive rebase is interrupted, the user may stage changes before continuing, and we need to commit those changes in that case. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 1b3255e commit 03b1380

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

sequencer.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,39 @@ static int continue_single_pick(void)
15871587
return run_command_v_opt(argv, RUN_GIT_CMD);
15881588
}
15891589

1590+
static int commit_staged_changes(struct replay_opts *opts)
1591+
{
1592+
int amend = 0;
1593+
1594+
if (has_unstaged_changes(1))
1595+
return error(_("Cannot rebase: You have unstaged changes."));
1596+
if (!has_uncommitted_changes(0))
1597+
return 0;
1598+
1599+
if (file_exists(git_path_rebase_amend())) {
1600+
struct strbuf rev = STRBUF_INIT;
1601+
unsigned char head[20], to_amend[20];
1602+
1603+
if (get_sha1("HEAD", head))
1604+
return error("Cannot amend non-existing commit");
1605+
if (strbuf_read_file(&rev, git_path_rebase_amend(), 41) <= 0)
1606+
return error("Invalid file: %s",
1607+
git_path_rebase_amend());
1608+
if (get_sha1_hex(rev.buf, to_amend))
1609+
return error("Invalid contents: %s",
1610+
git_path_rebase_amend());
1611+
if (hashcmp(head, to_amend))
1612+
return error("\nYou have uncommitted changes in your "
1613+
"working tree. Please, commit them\nfirst and "
1614+
"then run 'git rebase --continue' again.");
1615+
1616+
strbuf_release(&rev);
1617+
amend = 1;
1618+
}
1619+
1620+
return sequencer_commit(git_path_rebase_msg(), opts, 1, 1, amend);
1621+
}
1622+
15901623
int sequencer_continue(struct replay_opts *opts)
15911624
{
15921625
struct todo_list todo_list = TODO_LIST_INIT;
@@ -1595,7 +1628,11 @@ int sequencer_continue(struct replay_opts *opts)
15951628
if (read_and_refresh_cache(opts))
15961629
return -1;
15971630

1598-
if (!file_exists(get_todo_path(opts)))
1631+
if (IS_REBASE_I()) {
1632+
if (commit_staged_changes(opts))
1633+
return -1;
1634+
}
1635+
else if (!file_exists(get_todo_path(opts)))
15991636
return continue_single_pick();
16001637
if (read_populate_opts(opts) ||
16011638
read_populate_todo(&todo_list, opts))
@@ -1608,10 +1645,11 @@ int sequencer_continue(struct replay_opts *opts)
16081645
if (ret)
16091646
return ret;
16101647
}
1611-
if (index_differs_from("HEAD", 0))
1612-
return error_dirty_index(opts);
1613-
if (opts->action <= REPLAY_REVERT)
1648+
if (opts->action != REPLAY_INTERACTIVE_REBASE) {
1649+
if (index_differs_from("HEAD", 0))
1650+
return error_dirty_index(opts);
16141651
todo_list.current++;
1652+
}
16151653
res = pick_commits(&todo_list, opts);
16161654
todo_list_release(&todo_list);
16171655
return res;

0 commit comments

Comments
 (0)