git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rebase -i: reword empty commit after fast-forward
@ 2025-02-05 10:37 Phillip Wood via GitGitGadget
  2025-02-05 18:26 ` Junio C Hamano
  2025-02-11 15:59 ` [PATCH v2] " Phillip Wood via GitGitGadget
  0 siblings, 2 replies; 4+ messages in thread
From: Phillip Wood via GitGitGadget @ 2025-02-05 10:37 UTC (permalink / raw)
  To: git; +Cc: Phillip Wood, Phillip Wood

From: Phillip Wood <phillip.wood@dunelm.org.uk>

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>
---
    rebase -i: reword empty commit after fast-forward

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1860%2Fphillipwood%2Frebase-reword-empty-commit-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1860/phillipwood/rebase-reword-empty-commit-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1860

 sequencer.c                   |  5 ++---
 t/t3404-rebase-interactive.sh | 14 ++++++++++++++
 t/t3430-rebase-merges.sh      | 20 ++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index 407ee4e90fe..763bef1c898 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2510,9 +2510,8 @@ 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));
+			flags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;
+			res = run_git_commit(NULL, opts, flags);
 			*check_todo = 1;
 		}
 	}
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ecfc02062cd..2aee9789a2f 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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}" &&
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index 2593711fecd..b84d68c4b96 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -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

base-commit: 58b5801aa94ad5031978f8e42c1be1230b3d352f
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rebase -i: reword empty commit after fast-forward
  2025-02-05 10:37 [PATCH] rebase -i: reword empty commit after fast-forward Phillip Wood via GitGitGadget
@ 2025-02-05 18:26 ` Junio C Hamano
  2025-02-11 15:59 ` [PATCH v2] " Phillip Wood via GitGitGadget
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2025-02-05 18:26 UTC (permalink / raw)
  To: Phillip Wood via GitGitGadget; +Cc: git, Phillip Wood

"Phillip Wood via GitGitGadget" <gitgitgadget@gmail.com> writes:

> @@ -2510,9 +2510,8 @@ 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));
> +			flags = EDIT_MSG | VERIFY_MSG | AMEND_MSG | ALLOW_EMPTY;
> +			res = run_git_commit(NULL, opts, flags);
>  			*check_todo = 1;
>  		}
>  	}

I am perfectly OK with the idea of run_git_commit() with the fixed
set of flags bits, ignoring everything the preceding code did to
incrementally compute it before the control reaches this point.  In
the fast-forward-edit scenario in which the control reaches this
point, the flag bits like CREATE_ROOT_COMMIT the earlier steps may
have added to "flags".

But the way "flags" variable is used elsewhere in this function is
"we check how this 'pick' step needs to work, and compute bits to
pass when we eventually call run_git_commit() or do_commit(),
incrementally", so making an unconditional assignment to it looked
a bit surprising.  At least the unconditional assignment deserves
a bit of comment explaining why other bits do not matter and these
bits are what we want to use here.

Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] rebase -i: reword empty commit after fast-forward
  2025-02-05 10:37 [PATCH] rebase -i: reword empty commit after fast-forward Phillip Wood via GitGitGadget
  2025-02-05 18:26 ` Junio C Hamano
@ 2025-02-11 15:59 ` Phillip Wood via GitGitGadget
  2025-02-11 17:51   ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Phillip Wood via GitGitGadget @ 2025-02-11 15:59 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Phillip Wood, Phillip Wood

From: Phillip Wood <phillip.wood@dunelm.org.uk>

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>
---
    rebase -i: reword empty commit after fast-forward
    
    Thanks to Junio for his comments on V1. I've added a comment explaining
    why we use a fixed set of commits flags as he suggested.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1860%2Fphillipwood%2Frebase-reword-empty-commit-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1860/phillipwood/rebase-reword-empty-commit-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1860

Range-diff vs v1:

 1:  06de47a91d8 ! 1:  826edd3ba48 rebase -i: reword empty commit after fast-forward
     @@ sequencer.c: static int do_pick_commit(struct repository *r,
      -			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;


 sequencer.c                   | 12 +++++++++---
 t/t3404-rebase-interactive.sh | 14 ++++++++++++++
 t/t3430-rebase-merges.sh      | 20 ++++++++++++++++++++
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index 407ee4e90fe..ad0ab75c8d4 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -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;
 		}
 	}
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
index ecfc02062cd..2aee9789a2f 100755
--- a/t/t3404-rebase-interactive.sh
+++ b/t/t3404-rebase-interactive.sh
@@ -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}" &&
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index 2593711fecd..b84d68c4b96 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -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

base-commit: 58b5801aa94ad5031978f8e42c1be1230b3d352f
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] rebase -i: reword empty commit after fast-forward
  2025-02-11 15:59 ` [PATCH v2] " Phillip Wood via GitGitGadget
@ 2025-02-11 17:51   ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2025-02-11 17:51 UTC (permalink / raw)
  To: Phillip Wood via GitGitGadget; +Cc: git, Phillip Wood

"Phillip Wood via GitGitGadget" <gitgitgadget@gmail.com> writes:

> As "git commit" will happily create empty merge commits without
> "--allow-empty" we do not need to pass that flag when rewording merge
> commits.

Thanks, will queue.  Let's mark it for 'next'.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-02-11 17:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-05 10:37 [PATCH] rebase -i: reword empty commit after fast-forward Phillip Wood via GitGitGadget
2025-02-05 18:26 ` Junio C Hamano
2025-02-11 15:59 ` [PATCH v2] " Phillip Wood via GitGitGadget
2025-02-11 17:51   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).