From ade546be4786637ba039478d6f027becb1160803 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Thu, 7 Dec 2017 16:20:17 -0500 Subject: worktree: invoke post-checkout hook (unless --no-checkout) git-clone and git-checkout both invoke the post-checkout hook following a successful checkout, yet git-worktree neglects to do so even though it too "checks out" the worktree. Fix this oversight. Implementation note: The newly-created worktree may reference a branch or be detached. In the latter case, a commit lookup is performed, though the result is used only in a boolean sense to (a) determine if the commit actually exists, and (b) assign either the branch name or commit ID to HEAD. Since the post-commit hook needs to know the ID of the checked-out commit, the lookup now needs to be done in all cases, rather than only when detached. Consequently, a new boolean is needed to handle (b) since the lookup result itself can no longer perform that role. Reported-by: Matthew K Gumbel Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- t/t2025-worktree-add.sh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 't') diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh index b5c47ac602..d6fb062f83 100755 --- a/t/t2025-worktree-add.sh +++ b/t/t2025-worktree-add.sh @@ -314,4 +314,33 @@ test_expect_success 'rename a branch under bisect not allowed' ' test_must_fail git branch -M under-bisect bisect-with-new-name ' +post_checkout_hook () { + test_when_finished "rm -f .git/hooks/post-checkout" && + mkdir -p .git/hooks && + write_script .git/hooks/post-checkout <<-\EOF + echo $* >hook.actual + EOF +} + +test_expect_success '"add" invokes post-checkout hook (branch)' ' + post_checkout_hook && + printf "%s %s 1\n" $_z40 $(git rev-parse HEAD) >hook.expect && + git worktree add gumby && + test_cmp hook.expect hook.actual +' + +test_expect_success '"add" invokes post-checkout hook (detached)' ' + post_checkout_hook && + printf "%s %s 1\n" $_z40 $(git rev-parse HEAD) >hook.expect && + git worktree add --detach grumpy && + test_cmp hook.expect hook.actual +' + +test_expect_success '"add --no-checkout" suppresses post-checkout hook' ' + post_checkout_hook && + rm -f hook.actual && + git worktree add --no-checkout gloopy && + test_path_is_missing hook.actual +' + test_done -- cgit v1.2.3