Skip to content

Commit af8fc7b

Browse files
phillipwoodgitster
authored andcommitted
rebase -i: reword empty commit after fast-forward
When rebase rewords a commit it picks the commit and then runs "git commit --amend" to reword it. When the commit is picked the sequencer tries to reuse existing commits by fast-forwarding if the parents are unchanged. Rewording an empty commit that has been fast-forwarded fails because "git commit --amend" is called without "--allow-empty". This happens because when a commit is fast-forwarded the logic that checks whether we should pass "--allow-empty" is skipped. Fix this by always passing "--allow-empty" when rewording a commit. This is safe because we are amending a commit that has already been picked so if it had become empty when it was picked we'd have already returned an error. As "git commit" will happily create empty merge commits without "--allow-empty" we do not need to pass that flag when rewording merge commits. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f93ff17 commit af8fc7b

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

sequencer.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,9 +2510,15 @@ static int do_pick_commit(struct repository *r,
25102510
*check_todo = !!(flags & EDIT_MSG);
25112511
if (!res && reword) {
25122512
fast_forward_edit:
2513-
res = run_git_commit(NULL, opts, EDIT_MSG |
2514-
VERIFY_MSG | AMEND_MSG |
2515-
(flags & ALLOW_EMPTY));
2513+
/*
2514+
* To reword we amend the commit we just
2515+
* picked or fast-forwarded. As the commit has
2516+
* already been picked we want to use the same
2517+
* set of commit flags regardless of how we
2518+
* got here.
2519+
*/
2520+
flags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;
2521+
res = run_git_commit(NULL, opts, flags);
25162522
*check_todo = 1;
25172523
}
25182524
}

t/t3404-rebase-interactive.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,20 @@ test_expect_success 'reword' '
791791
grep "C changed" actual
792792
'
793793

794+
test_expect_success 'reword fast-forwarded empty commit' '
795+
git commit --allow-empty -m "empty commit" --only &&
796+
(
797+
set_fake_editor &&
798+
FAKE_COMMIT_AMEND=edited FAKE_LINES="reword 1" \
799+
git rebase -i HEAD^
800+
) &&
801+
test_commit_message HEAD <<-\EOF
802+
empty commit
803+
804+
edited
805+
EOF
806+
'
807+
794808
test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
795809
git checkout E &&
796810
test_when_finished "git checkout @{-1}" &&

t/t3430-rebase-merges.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,24 @@ test_expect_success 'truncate label names' '
610610
grep "label 0123456789-$" out
611611
'
612612

613+
test_expect_success 'reword fast-forwarded empty merge commit' '
614+
oid="$(git commit-tree -m "D1" -p A D^{tree})" &&
615+
oid="$(git commit-tree -m "empty merge" -p D -p $oid D^{tree})" &&
616+
617+
write_script sequence-editor.sh <<-\EOF &&
618+
sed /^merge/s/-C/-c/ "$1" >"$1.tmp"
619+
mv "$1.tmp" "$1"
620+
EOF
621+
622+
(
623+
test_set_sequence_editor "$(pwd)/sequence-editor.sh" &&
624+
GIT_EDITOR="echo edited >>" git rebase -i -r D $oid
625+
) &&
626+
test_commit_message HEAD <<-\EOF
627+
empty merge
628+
629+
edited
630+
EOF
631+
'
632+
613633
test_done

0 commit comments

Comments
 (0)