@@ -9,9 +9,20 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
99
1010. ./test-lib.sh
1111
12+ # Usage: check_post_checkout <file> <old-ref> <new-ref> <flag>
13+ #
14+ # Verify that the post-checkout hook arguments in <file> match the expected
15+ # values: <old-ref> for the previous HEAD, <new-ref> for the new HEAD, and
16+ # <flag> indicating whether this was a branch checkout (1) or file checkout (0).
17+ check_post_checkout () {
18+ test " $# " = 4 || BUG " check_post_checkout takes 4 args"
19+ echo " old=$2 new=$3 flag=$4 " > expect &&
20+ test_cmp expect " $1 "
21+ }
22+
1223test_expect_success setup '
1324 test_hook --setup post-checkout <<-\EOF &&
14- echo "$@ " >.git/post-checkout.args
25+ echo "old=$1 new=$2 flag=$3 " >.git/post-checkout.args
1526 EOF
1627 test_commit one &&
1728 test_commit two &&
@@ -23,29 +34,30 @@ test_expect_success setup '
2334test_expect_success ' post-checkout receives the right arguments with HEAD unchanged ' '
2435 test_when_finished "rm -f .git/post-checkout.args" &&
2536 git checkout main &&
26- read old new flag < .git/post-checkout.args &&
27- test $old = $new && test $flag = 1
37+ check_post_checkout .git/post-checkout.args \
38+ "$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
2839'
2940
3041test_expect_success ' post-checkout args are correct with git checkout -b ' '
3142 test_when_finished "rm -f .git/post-checkout.args" &&
3243 git checkout -b new1 &&
33- read old new flag < .git/post-checkout.args &&
34- test $old = $new && test $flag = 1
44+ check_post_checkout .git/post-checkout.args \
45+ "$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 1
3546'
3647
3748test_expect_success ' post-checkout receives the right args with HEAD changed ' '
3849 test_when_finished "rm -f .git/post-checkout.args" &&
50+ old=$(git rev-parse HEAD) &&
3951 git checkout two &&
40- read old new flag < .git/post-checkout.args &&
41- test $old != $new && test $flag = 1
52+ check_post_checkout .git/post-checkout.args \
53+ " $old" "$(git rev-parse HEAD)" 1
4254'
4355
4456test_expect_success ' post-checkout receives the right args when not switching branches ' '
4557 test_when_finished "rm -f .git/post-checkout.args" &&
4658 git checkout main -- three.t &&
47- read old new flag < .git/post-checkout.args &&
48- test $old = $new && test $flag = 0
59+ check_post_checkout .git/post-checkout.args \
60+ "$(git rev-parse HEAD)" "$(git rev-parse HEAD)" 0
4961'
5062
5163test_rebase () {
@@ -55,21 +67,17 @@ test_rebase () {
5567 git checkout -B rebase-test main &&
5668 rm -f .git/post-checkout.args &&
5769 git rebase $args rebase-on-me &&
58- read old new flag <.git/post-checkout.args &&
59- test_cmp_rev main $old &&
60- test_cmp_rev rebase-on-me $new &&
61- test $flag = 1
70+ check_post_checkout .git/post-checkout.args \
71+ "$(git rev-parse main)" "$(git rev-parse rebase-on-me)" 1
6272 '
6373
6474 test_expect_success " post-checkout is triggered on rebase $args with fast-forward" '
6575 test_when_finished "rm -f .git/post-checkout.args" &&
6676 git checkout -B ff-rebase-test rebase-on-me^ &&
6777 rm -f .git/post-checkout.args &&
6878 git rebase $args rebase-on-me &&
69- read old new flag <.git/post-checkout.args &&
70- test_cmp_rev rebase-on-me^ $old &&
71- test_cmp_rev rebase-on-me $new &&
72- test $flag = 1
79+ check_post_checkout .git/post-checkout.args \
80+ "$(git rev-parse rebase-on-me^)" "$(git rev-parse rebase-on-me)" 1
7381 '
7482
7583 test_expect_success " rebase $args fast-forward branch checkout runs post-checkout hook" '
@@ -79,10 +87,8 @@ test_rebase () {
7987 git checkout two &&
8088 rm -f .git/post-checkout.args &&
8189 git rebase $args HEAD rebase-fast-forward &&
82- read old new flag <.git/post-checkout.args &&
83- test_cmp_rev two $old &&
84- test_cmp_rev three $new &&
85- test $flag = 1
90+ check_post_checkout .git/post-checkout.args \
91+ "$(git rev-parse two)" "$(git rev-parse three)" 1
8692 '
8793
8894 test_expect_success " rebase $args checkout does not remove untracked files" '
@@ -106,10 +112,11 @@ test_rebase --merge
106112test_expect_success ' post-checkout hook is triggered by clone' '
107113 mkdir -p templates/hooks &&
108114 write_script templates/hooks/post-checkout <<-\EOF &&
109- echo "$@ " >"$GIT_DIR/post-checkout.args"
115+ echo "old=$1 new=$2 flag=$3 " >"$GIT_DIR/post-checkout.args"
110116 EOF
111117 git clone --template=templates . clone3 &&
112- test_path_is_file clone3/.git/post-checkout.args
118+ check_post_checkout clone3/.git/post-checkout.args \
119+ "$(test_oid zero)" "$(git -C clone3 rev-parse HEAD)" 1
113120'
114121
115122test_done
0 commit comments