]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rebase -i: reword empty commit after fast-forward
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Tue, 11 Feb 2025 15:59:08 +0000 (15:59 +0000)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Feb 2025 17:50:53 +0000 (09:50 -0800)
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>
sequencer.c
t/t3404-rebase-interactive.sh
t/t3430-rebase-merges.sh

index 407ee4e90fea685a53504265ce528cb3b8443330..ad0ab75c8d4dd7c91d600276afe01935bdda0922 100644 (file)
@@ -2510,9 +2510,15 @@ static int do_pick_commit(struct repository *r,
                *check_todo = !!(flags & EDIT_MSG);
                if (!res && reword) {
 fast_forward_edit:
-                       res = run_git_commit(NULL, opts, EDIT_MSG |
-                                            VERIFY_MSG | AMEND_MSG |
-                                            (flags & ALLOW_EMPTY));
+                       /*
+                        * To reword we amend the commit we just
+                        * picked or fast-forwarded. As the commit has
+                        * already been picked we want to use the same
+                        * set of commit flags regardless of how we
+                        * got here.
+                        */
+                       flags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;
+                       res = run_git_commit(NULL, opts, flags);
                        *check_todo = 1;
                }
        }
index ecfc02062cd046d7121b7cc8535f3d91034eaf59..2aee9789a2fae2a4d6a6d5b1205238d229a0a4f5 100755 (executable)
@@ -791,6 +791,20 @@ test_expect_success 'reword' '
        grep "C changed" actual
 '
 
+test_expect_success 'reword fast-forwarded empty commit' '
+       git commit --allow-empty -m "empty commit" --only &&
+       (
+               set_fake_editor &&
+               FAKE_COMMIT_AMEND=edited FAKE_LINES="reword 1" \
+                       git rebase -i HEAD^
+       ) &&
+       test_commit_message HEAD <<-\EOF
+       empty commit
+
+       edited
+       EOF
+'
+
 test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
        git checkout E &&
        test_when_finished "git checkout @{-1}" &&
index 2593711fecdc9f18c1b83b8d921ae06fbd2495ac..b84d68c4b96bc9d531acfdd48d48571902b744ea 100755 (executable)
@@ -610,4 +610,24 @@ test_expect_success 'truncate label names' '
        grep "label 0123456789-$" out
 '
 
+test_expect_success 'reword fast-forwarded empty merge commit' '
+       oid="$(git commit-tree -m "D1" -p A D^{tree})" &&
+       oid="$(git commit-tree -m "empty merge" -p D -p $oid D^{tree})" &&
+
+       write_script sequence-editor.sh <<-\EOF &&
+       sed /^merge/s/-C/-c/ "$1" >"$1.tmp"
+       mv "$1.tmp" "$1"
+       EOF
+
+       (
+               test_set_sequence_editor "$(pwd)/sequence-editor.sh" &&
+               GIT_EDITOR="echo edited >>" git rebase -i -r D $oid
+       ) &&
+       test_commit_message HEAD <<-\EOF
+       empty merge
+
+       edited
+       EOF
+'
+
 test_done