diff mbox series

[v3,05/14] rebase --apply: don't run post-checkout hook if there is an error

Message ID 4de5104d22d2ddd9c0cdf0e2173667c4796d3322.1643202349.git.gitgitgadget@gmail.com (mailing list archive)
State Accepted
Commit 4840002a5f44a4c256c55f70c59d3b0506d14e21
Headers show
Series rebase: reset_head() related fixes and improvements | expand

Commit Message

Phillip Wood Jan. 26, 2022, 1:05 p.m. UTC
From: Phillip Wood <phillip.wood@dunelm.org.uk>

The hook should only be run if the worktree and refs were successfully
updated. This primarily affects "rebase --apply" but also "rebase
--merge" when it fast-forwards.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 reset.c                       | 2 +-
 t/t5403-post-checkout-hook.sh | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/reset.c b/reset.c
index 3e7b9e2e131..3537de91f65 100644
--- a/reset.c
+++ b/reset.c
@@ -126,7 +126,7 @@  reset_head_refs:
 			ret = create_symref("HEAD", switch_to_branch,
 					    reflog_head);
 	}
-	if (run_hook)
+	if (!ret && run_hook)
 		run_hook_le(NULL, "post-checkout",
 			    oid_to_hex(head ? head : null_oid()),
 			    oid_to_hex(oid), "1", NULL);
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
index fd2817b4068..d1181816906 100755
--- a/t/t5403-post-checkout-hook.sh
+++ b/t/t5403-post-checkout-hook.sh
@@ -88,12 +88,16 @@  test_rebase () {
 
 	test_expect_success "rebase $args checkout does not remove untracked files" '
 		test_when_finished "test_might_fail git rebase --abort" &&
+		test_when_finished "rm -f .git/post-checkout.args" &&
 		git update-ref refs/heads/rebase-fast-forward three &&
 		git checkout two &&
+		rm -f .git/post-checkout.args &&
 		echo untracked >three.t &&
 		test_when_finished "rm three.t" &&
 		test_must_fail git rebase $args HEAD rebase-fast-forward 2>err &&
-		grep "untracked working tree files would be overwritten by checkout" err
+		grep "untracked working tree files would be overwritten by checkout" err &&
+		test_path_is_missing .git/post-checkout.args
+
 '
 }