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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Feb 11, 2025 at 15:59 UTC af8fc7be10fa486c93acfb177af4dd1fa7757deb
3 files changed +43 -3
sequencer.c
+9 -3
@@ -2510,9 +2510,15 @@ static int do_pick_commit(struct repository *r,
2510 *check_todo = !!(flags & EDIT_MSG);
2511 if (!res && reword) {
2512 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);
2522 *check_todo = 1;
2523 }
2524 }
t/t3404-rebase-interactive.sh
+14
@@ -791,6 +791,20 @@ test_expect_success 'reword' '
791 grep "C changed" actual
792 '
793
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 +
808 test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
809 git checkout E &&
810 test_when_finished "git checkout @{-1}" &&
t/t3430-rebase-merges.sh
+20
@@ -610,4 +610,24 @@ test_expect_success 'truncate label names' '
610 grep "label 0123456789-$" out
611 '
612
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 +
633 test_done