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. 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>

Johannes Schindelin committed Jan 2, 2017 at 16:27 UTC 9d93ccd1d2d2d40a3c7ecc76740caf28683144ac
1 file changed +40
sequencer.c
+40
@@ -1826,6 +1826,42 @@ static int continue_single_pick(void)
1826 return run_command_v_opt(argv, RUN_GIT_CMD);
1827 }
1828
1829 +static int commit_staged_changes(struct replay_opts *opts)
1830 +{
1831 + int amend = 0;
1832 +
1833 + if (has_unstaged_changes(1))
1834 + return error(_("cannot rebase: You have unstaged changes."));
1835 + if (!has_uncommitted_changes(0))
1836 + return 0;
1837 +
1838 + if (file_exists(rebase_path_amend())) {
1839 + struct strbuf rev = STRBUF_INIT;
1840 + unsigned char head[20], to_amend[20];
1841 +
1842 + if (get_sha1("HEAD", head))
1843 + return error(_("cannot amend non-existing commit"));
1844 + if (!read_oneliner(&rev, rebase_path_amend(), 0))
1845 + return error(_("invalid file: '%s'"), rebase_path_amend());
1846 + if (get_sha1_hex(rev.buf, to_amend))
1847 + return error(_("invalid contents: '%s'"),
1848 + rebase_path_amend());
1849 + if (hashcmp(head, to_amend))
1850 + return error(_("\nYou have uncommitted changes in your "
1851 + "working tree. Please, commit them\n"
1852 + "first and then run 'git rebase "
1853 + "--continue' again."));
1854 +
1855 + strbuf_release(&rev);
1856 + amend = 1;
1857 + }
1858 +
1859 + if (run_git_commit(rebase_path_message(), opts, 1, 1, amend, 0))
1860 + return error(_("could not commit staged changes."));
1861 + unlink(rebase_path_amend());
1862 + return 0;
1863 +}
1864 +
1865 int sequencer_continue(struct replay_opts *opts)
1866 {
1867 struct todo_list todo_list = TODO_LIST_INIT;
@@ -1834,6 +1870,10 @@ int sequencer_continue(struct replay_opts *opts)
1870 if (read_and_refresh_cache(opts))
1871 return -1;
1872
1873 + if (is_rebase_i(opts)) {
1874 + if (commit_staged_changes(opts))
1875 + return -1;
1876 + }
1877 if (!file_exists(get_todo_path(opts)))
1878 return continue_single_pick();
1879 if (read_populate_opts(opts))