Skip to content

Commit f517a11

Browse files
phillipwoodgitster
authored andcommitted
rebase --apply: fix reflog
move_to_original_branch() passes the message intended for the branch reflog as `orig_head_msg`. Fix this by adding a `branch_msg` member to struct reset_head_opts and add a regression test. Note that these reflog messages do not respect GIT_REFLOG_ACTION. They are not alone in that and will be fixed in a future series. The "merge" backend already has tests that check both the branch and HEAD reflogs. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bab6f80 commit f517a11

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

builtin/rebase.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
778778

779779
static int move_to_original_branch(struct rebase_options *opts)
780780
{
781-
struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
781+
struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
782782
struct reset_head_opts ropts = { 0 };
783783
int ret;
784784

@@ -788,17 +788,17 @@ static int move_to_original_branch(struct rebase_options *opts)
788788
if (!opts->onto)
789789
BUG("move_to_original_branch without onto");
790790

791-
strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
791+
strbuf_addf(&branch_reflog, "rebase finished: %s onto %s",
792792
opts->head_name, oid_to_hex(&opts->onto->object.oid));
793793
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
794794
opts->head_name);
795795
ropts.branch = opts->head_name;
796796
ropts.flags = RESET_HEAD_REFS_ONLY;
797-
ropts.orig_head_msg = orig_head_reflog.buf;
797+
ropts.branch_msg = branch_reflog.buf;
798798
ropts.head_msg = head_reflog.buf;
799799
ret = reset_head(the_repository, &ropts);
800800

801-
strbuf_release(&orig_head_reflog);
801+
strbuf_release(&branch_reflog);
802802
strbuf_release(&head_reflog);
803803
return ret;
804804
}

reset.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static int update_refs(const struct reset_head_opts *opts,
1515
unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1616
unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;
1717
const char *switch_to_branch = opts->branch;
18+
const char *reflog_branch = opts->branch_msg;
1819
const char *reflog_head = opts->head_msg;
1920
const char *reflog_orig_head = opts->orig_head_msg;
2021
const char *default_reflog_action = opts->default_reflog_action;
@@ -58,8 +59,9 @@ static int update_refs(const struct reset_head_opts *opts,
5859
detach_head ? REF_NO_DEREF : 0,
5960
UPDATE_REFS_MSG_ON_ERR);
6061
else {
61-
ret = update_ref(reflog_head, switch_to_branch, oid,
62-
NULL, 0, UPDATE_REFS_MSG_ON_ERR);
62+
ret = update_ref(reflog_branch ? reflog_branch : reflog_head,
63+
switch_to_branch, oid, NULL, 0,
64+
UPDATE_REFS_MSG_ON_ERR);
6365
if (!ret)
6466
ret = create_symref("HEAD", switch_to_branch,
6567
reflog_head);
@@ -90,6 +92,12 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)
9092
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
9193
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
9294

95+
if (opts->orig_head_msg && !update_orig_head)
96+
BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD");
97+
98+
if (opts->branch_msg && !opts->branch)
99+
BUG("branch reflog message given without a branch");
100+
93101
if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
94102
ret = -1;
95103
goto leave_reset_head;

reset.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct reset_head_opts {
1919
const char *branch;
2020
/* Flags defined above */
2121
unsigned flags;
22+
/* Optional reflog message for branch, defaults to head_msg. */
23+
const char *branch_msg;
2224
/*
2325
* Optional reflog message for HEAD, if this is not set then
2426
* default_reflog_action must be.

t/t3406-rebase-message.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ test_expect_success 'GIT_REFLOG_ACTION' '
105105
test_cmp expect actual
106106
'
107107

108+
test_expect_success 'rebase --apply reflog' '
109+
git checkout -b reflog-apply start &&
110+
old_head_reflog="$(git log -g --format=%gs -1 HEAD)" &&
111+
112+
git rebase --apply Y &&
113+
114+
git log -g --format=%gs -4 HEAD >actual &&
115+
cat >expect <<-EOF &&
116+
rebase finished: returning to refs/heads/reflog-apply
117+
rebase: Z
118+
rebase: checkout Y
119+
$old_head_reflog
120+
EOF
121+
test_cmp expect actual &&
122+
123+
git log -g --format=%gs -2 reflog-apply >actual &&
124+
cat >expect <<-EOF &&
125+
rebase finished: refs/heads/reflog-apply onto $(git rev-parse Y)
126+
branch: Created from start
127+
EOF
128+
test_cmp expect actual
129+
'
130+
108131
test_expect_success 'rebase -i onto unrelated history' '
109132
git init unrelated &&
110133
test_commit -C unrelated 1 &&

0 commit comments

Comments
 (0)