{"thread":{"id":"62905","subject":"[PATCH] rebase -i: reword empty commit after fast-forward","startedAt":"2025-02-05T10:37:24Z","lastAt":"2025-02-11T17:51:58Z","messageCount":4,"participants":["Phillip Wood via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"511867","messageId":"pull.1860.git.1738751840816.gitgitgadget@gmail.com","threadId":"62905","inReplyTo":null,"subject":"[PATCH] rebase -i: reword empty commit after fast-forward","fromName":"Phillip Wood via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-02-05T10:37:20Z","receivedAt":"2025-02-05T10:37:24Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nWhen rebase rewords a commit it picks the commit and then runs \"git\ncommit --amend\" to reword it. When the commit is picked the sequencer\ntries to reuse existing commits by fast-forwarding if the parents are\nunchanged. Rewording an empty commit that has been fast-forwarded fails\nbecause \"git commit --amend\" is called without \"--allow-empty\". This\nhappens because when a commit is fast-forwarded the logic that checks\nwhether we should pass \"--allow-empty\" is skipped. Fix this by always\npassing \"--allow-empty\" when rewording a commit. This is safe because we\nare amending a commit that has already been picked so if it had become\nempty when it was picked we'd have already returned an error.\n\nAs \"git commit\" will happily create empty merge commits without\n\"--allow-empty\" we do not need to pass that flag when rewording merge\ncommits.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n    rebase -i: reword empty commit after fast-forward\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1860%2Fphillipwood%2Frebase-reword-empty-commit-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1860/phillipwood/rebase-reword-empty-commit-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1860\n\n sequencer.c                   |  5 ++---\n t/t3404-rebase-interactive.sh | 14 ++++++++++++++\n t/t3430-rebase-merges.sh      | 20 ++++++++++++++++++++\n 3 files changed, 36 insertions(+), 3 deletions(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex 407ee4e90fe..763bef1c898 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -2510,9 +2510,8 @@ static int do_pick_commit(struct repository *r,\n \t\t*check_todo = !!(flags & EDIT_MSG);\n \t\tif (!res && reword) {\n fast_forward_edit:\n-\t\t\tres = run_git_commit(NULL, opts, EDIT_MSG |\n-\t\t\t\t\t     VERIFY_MSG | AMEND_MSG |\n-\t\t\t\t\t     (flags & ALLOW_EMPTY));\n+\t\t\tflags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;\n+\t\t\tres = run_git_commit(NULL, opts, flags);\n \t\t\t*check_todo = 1;\n \t\t}\n \t}\ndiff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh\nindex ecfc02062cd..2aee9789a2f 100755\n--- a/t/t3404-rebase-interactive.sh\n+++ b/t/t3404-rebase-interactive.sh\n@@ -791,6 +791,20 @@ test_expect_success 'reword' '\n \tgrep \"C changed\" actual\n '\n \n+test_expect_success 'reword fast-forwarded empty commit' '\n+\tgit commit --allow-empty -m \"empty commit\" --only &&\n+\t(\n+\t\tset_fake_editor &&\n+\t\tFAKE_COMMIT_AMEND=edited FAKE_LINES=\"reword 1\" \\\n+\t\t\tgit rebase -i HEAD^\n+\t) &&\n+\ttest_commit_message HEAD <<-\\EOF\n+\tempty commit\n+\n+\tedited\n+\tEOF\n+'\n+\n test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '\n \tgit checkout E &&\n \ttest_when_finished \"git checkout @{-1}\" &&\ndiff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh\nindex 2593711fecd..b84d68c4b96 100755\n--- a/t/t3430-rebase-merges.sh\n+++ b/t/t3430-rebase-merges.sh\n@@ -610,4 +610,24 @@ test_expect_success 'truncate label names' '\n \tgrep \"label 0123456789-$\" out\n '\n \n+test_expect_success 'reword fast-forwarded empty merge commit' '\n+\toid=\"$(git commit-tree -m \"D1\" -p A D^{tree})\" &&\n+\toid=\"$(git commit-tree -m \"empty merge\" -p D -p $oid D^{tree})\" &&\n+\n+\twrite_script sequence-editor.sh <<-\\EOF &&\n+\tsed /^merge/s/-C/-c/ \"$1\" >\"$1.tmp\"\n+\tmv \"$1.tmp\" \"$1\"\n+\tEOF\n+\n+\t(\n+\t\ttest_set_sequence_editor \"$(pwd)/sequence-editor.sh\" &&\n+\t\tGIT_EDITOR=\"echo edited >>\" git rebase -i -r D $oid\n+\t) &&\n+\ttest_commit_message HEAD <<-\\EOF\n+\tempty merge\n+\n+\tedited\n+\tEOF\n+'\n+\n test_done\n\nbase-commit: 58b5801aa94ad5031978f8e42c1be1230b3d352f\n-- \ngitgitgadget\n"},{"id":"511895","messageId":"xmqqtt98nt89.fsf@gitster.g","threadId":"62905","inReplyTo":"pull.1860.git.1738751840816.gitgitgadget@gmail.com","subject":"Re: [PATCH] rebase -i: reword empty commit after fast-forward","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-02-05T18:26:14Z","receivedAt":"2025-02-05T18:26:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Phillip Wood via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> @@ -2510,9 +2510,8 @@ static int do_pick_commit(struct repository *r,\n>  \t\t*check_todo = !!(flags & EDIT_MSG);\n>  \t\tif (!res && reword) {\n>  fast_forward_edit:\n> -\t\t\tres = run_git_commit(NULL, opts, EDIT_MSG |\n> -\t\t\t\t\t     VERIFY_MSG | AMEND_MSG |\n> -\t\t\t\t\t     (flags & ALLOW_EMPTY));\n> +\t\t\tflags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;\n> +\t\t\tres = run_git_commit(NULL, opts, flags);\n>  \t\t\t*check_todo = 1;\n>  \t\t}\n>  \t}\n\nI am perfectly OK with the idea of run_git_commit() with the fixed\nset of flags bits, ignoring everything the preceding code did to\nincrementally compute it before the control reaches this point.  In\nthe fast-forward-edit scenario in which the control reaches this\npoint, the flag bits like CREATE_ROOT_COMMIT the earlier steps may\nhave added to \"flags\".\n\nBut the way \"flags\" variable is used elsewhere in this function is\n\"we check how this 'pick' step needs to work, and compute bits to\npass when we eventually call run_git_commit() or do_commit(),\nincrementally\", so making an unconditional assignment to it looked\na bit surprising.  At least the unconditional assignment deserves\na bit of comment explaining why other bits do not matter and these\nbits are what we want to use here.\n\nThanks.\n"},{"id":"512235","messageId":"pull.1860.v2.git.1739289549299.gitgitgadget@gmail.com","threadId":"62905","inReplyTo":"pull.1860.git.1738751840816.gitgitgadget@gmail.com","subject":"[PATCH v2] rebase -i: reword empty commit after fast-forward","fromName":"Phillip Wood via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2025-02-11T15:59:08Z","receivedAt":"2025-02-11T15:59:12Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nWhen rebase rewords a commit it picks the commit and then runs \"git\ncommit --amend\" to reword it. When the commit is picked the sequencer\ntries to reuse existing commits by fast-forwarding if the parents are\nunchanged. Rewording an empty commit that has been fast-forwarded fails\nbecause \"git commit --amend\" is called without \"--allow-empty\". This\nhappens because when a commit is fast-forwarded the logic that checks\nwhether we should pass \"--allow-empty\" is skipped. Fix this by always\npassing \"--allow-empty\" when rewording a commit. This is safe because we\nare amending a commit that has already been picked so if it had become\nempty when it was picked we'd have already returned an error.\n\nAs \"git commit\" will happily create empty merge commits without\n\"--allow-empty\" we do not need to pass that flag when rewording merge\ncommits.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n    rebase -i: reword empty commit after fast-forward\n    \n    Thanks to Junio for his comments on V1. I've added a comment explaining\n    why we use a fixed set of commits flags as he suggested.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1860%2Fphillipwood%2Frebase-reword-empty-commit-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1860/phillipwood/rebase-reword-empty-commit-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1860\n\nRange-diff vs v1:\n\n 1:  06de47a91d8 ! 1:  826edd3ba48 rebase -i: reword empty commit after fast-forward\n     @@ sequencer.c: static int do_pick_commit(struct repository *r,\n      -\t\t\tres = run_git_commit(NULL, opts, EDIT_MSG |\n      -\t\t\t\t\t     VERIFY_MSG | AMEND_MSG |\n      -\t\t\t\t\t     (flags & ALLOW_EMPTY));\n     ++\t\t\t/*\n     ++\t\t\t * To reword we amend the commit we just\n     ++\t\t\t * picked or fast-forwarded. As the commit has\n     ++\t\t\t * already been picked we want to use the same\n     ++\t\t\t * set of commit flags regardless of how we\n     ++\t\t\t * got here.\n     ++\t\t\t */\n      +\t\t\tflags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;\n      +\t\t\tres = run_git_commit(NULL, opts, flags);\n       \t\t\t*check_todo = 1;\n\n\n sequencer.c                   | 12 +++++++++---\n t/t3404-rebase-interactive.sh | 14 ++++++++++++++\n t/t3430-rebase-merges.sh      | 20 ++++++++++++++++++++\n 3 files changed, 43 insertions(+), 3 deletions(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex 407ee4e90fe..ad0ab75c8d4 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -2510,9 +2510,15 @@ static int do_pick_commit(struct repository *r,\n \t\t*check_todo = !!(flags & EDIT_MSG);\n \t\tif (!res && reword) {\n fast_forward_edit:\n-\t\t\tres = run_git_commit(NULL, opts, EDIT_MSG |\n-\t\t\t\t\t     VERIFY_MSG | AMEND_MSG |\n-\t\t\t\t\t     (flags & ALLOW_EMPTY));\n+\t\t\t/*\n+\t\t\t * To reword we amend the commit we just\n+\t\t\t * picked or fast-forwarded. As the commit has\n+\t\t\t * already been picked we want to use the same\n+\t\t\t * set of commit flags regardless of how we\n+\t\t\t * got here.\n+\t\t\t */\n+\t\t\tflags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;\n+\t\t\tres = run_git_commit(NULL, opts, flags);\n \t\t\t*check_todo = 1;\n \t\t}\n \t}\ndiff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh\nindex ecfc02062cd..2aee9789a2f 100755\n--- a/t/t3404-rebase-interactive.sh\n+++ b/t/t3404-rebase-interactive.sh\n@@ -791,6 +791,20 @@ test_expect_success 'reword' '\n \tgrep \"C changed\" actual\n '\n \n+test_expect_success 'reword fast-forwarded empty commit' '\n+\tgit commit --allow-empty -m \"empty commit\" --only &&\n+\t(\n+\t\tset_fake_editor &&\n+\t\tFAKE_COMMIT_AMEND=edited FAKE_LINES=\"reword 1\" \\\n+\t\t\tgit rebase -i HEAD^\n+\t) &&\n+\ttest_commit_message HEAD <<-\\EOF\n+\tempty commit\n+\n+\tedited\n+\tEOF\n+'\n+\n test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '\n \tgit checkout E &&\n \ttest_when_finished \"git checkout @{-1}\" &&\ndiff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh\nindex 2593711fecd..b84d68c4b96 100755\n--- a/t/t3430-rebase-merges.sh\n+++ b/t/t3430-rebase-merges.sh\n@@ -610,4 +610,24 @@ test_expect_success 'truncate label names' '\n \tgrep \"label 0123456789-$\" out\n '\n \n+test_expect_success 'reword fast-forwarded empty merge commit' '\n+\toid=\"$(git commit-tree -m \"D1\" -p A D^{tree})\" &&\n+\toid=\"$(git commit-tree -m \"empty merge\" -p D -p $oid D^{tree})\" &&\n+\n+\twrite_script sequence-editor.sh <<-\\EOF &&\n+\tsed /^merge/s/-C/-c/ \"$1\" >\"$1.tmp\"\n+\tmv \"$1.tmp\" \"$1\"\n+\tEOF\n+\n+\t(\n+\t\ttest_set_sequence_editor \"$(pwd)/sequence-editor.sh\" &&\n+\t\tGIT_EDITOR=\"echo edited >>\" git rebase -i -r D $oid\n+\t) &&\n+\ttest_commit_message HEAD <<-\\EOF\n+\tempty merge\n+\n+\tedited\n+\tEOF\n+'\n+\n test_done\n\nbase-commit: 58b5801aa94ad5031978f8e42c1be1230b3d352f\n-- \ngitgitgadget\n"},{"id":"512246","messageId":"xmqq8qqcz7wk.fsf@gitster.g","threadId":"62905","inReplyTo":"pull.1860.v2.git.1739289549299.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] rebase -i: reword empty commit after fast-forward","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2025-02-11T17:51:55Z","receivedAt":"2025-02-11T17:51:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Phillip Wood via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> As \"git commit\" will happily create empty merge commits without\n> \"--allow-empty\" we do not need to pass that flag when rewording merge\n> commits.\n\nThanks, will queue.  Let's mark it for 'next'.\n"}]}