Skip to content

Commit 4b83ce9

Browse files
dschogitster
authored andcommitted
sequencer (rebase -i): update refs after a successful rebase
An interactive rebase operates on a detached HEAD (to keep the reflog of the original branch relatively clean), and updates the branch only at the end. Now that the sequencer learns to perform interactive rebases, it also needs to learn the trick to update the branch before removing the directory containing the state of the interactive rebase. We introduce a new head_ref variable in a wider scope than necessary at the moment, to allow for a later patch that prints out "Successfully rebased and updated <ref>". Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5286527 commit 4b83ce9

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

sequencer.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha")
102102
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
103103
static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
104104
static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose")
105+
static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name")
106+
static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto")
105107

106108
static inline int is_rebase_i(const struct replay_opts *opts)
107109
{
@@ -1784,12 +1786,53 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
17841786
}
17851787

17861788
if (is_rebase_i(opts)) {
1787-
struct strbuf buf = STRBUF_INIT;
1789+
struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT;
17881790

17891791
/* Stopped in the middle, as planned? */
17901792
if (todo_list->current < todo_list->nr)
17911793
return 0;
17921794

1795+
if (read_oneliner(&head_ref, rebase_path_head_name(), 0) &&
1796+
starts_with(head_ref.buf, "refs/")) {
1797+
unsigned char head[20], orig[20];
1798+
int res;
1799+
1800+
if (get_sha1("HEAD", head)) {
1801+
res = error(_("cannot read HEAD"));
1802+
cleanup_head_ref:
1803+
strbuf_release(&head_ref);
1804+
strbuf_release(&buf);
1805+
return res;
1806+
}
1807+
if (!read_oneliner(&buf, rebase_path_orig_head(), 0) ||
1808+
get_sha1_hex(buf.buf, orig)) {
1809+
res = error(_("could not read orig-head"));
1810+
goto cleanup_head_ref;
1811+
}
1812+
strbuf_addf(&buf, "rebase -i (finish): %s onto ",
1813+
head_ref.buf);
1814+
if (!read_oneliner(&buf, rebase_path_onto(), 0)) {
1815+
res = error(_("could not read 'onto'"));
1816+
goto cleanup_head_ref;
1817+
}
1818+
if (update_ref(buf.buf, head_ref.buf, head, orig,
1819+
REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) {
1820+
res = error(_("could not update %s"),
1821+
head_ref.buf);
1822+
goto cleanup_head_ref;
1823+
}
1824+
strbuf_reset(&buf);
1825+
strbuf_addf(&buf,
1826+
"rebase -i (finish): returning to %s",
1827+
head_ref.buf);
1828+
if (create_symref("HEAD", head_ref.buf, buf.buf)) {
1829+
res = error(_("could not update HEAD to %s"),
1830+
head_ref.buf);
1831+
goto cleanup_head_ref;
1832+
}
1833+
strbuf_reset(&buf);
1834+
}
1835+
17931836
if (opts->verbose) {
17941837
struct rev_info log_tree_opt;
17951838
struct object_id orig, head;
@@ -1810,6 +1853,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
18101853
}
18111854
}
18121855
strbuf_release(&buf);
1856+
strbuf_release(&head_ref);
18131857
}
18141858

18151859
/*

0 commit comments

Comments
 (0)