blob: d11818169067bb349c45cc42abb33826d99fe60c [file] [log] [blame]
Josh England1abbe472007-09-26 15:31:01 -06001#!/bin/sh
2#
3# Copyright (c) 2006 Josh England
4#
5
6test_description='Test the post-checkout hook.'
Johannes Schindelin966b4be2020-11-18 23:44:29 +00007GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00008export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
Josh England1abbe472007-09-26 15:31:01 -060010. ./test-lib.sh
11
12test_expect_success setup '
Orgad Shaneh10499a92018-12-29 23:37:58 +020013 mkdir -p .git/hooks &&
14 write_script .git/hooks/post-checkout <<-\EOF &&
15 echo "$@" >.git/post-checkout.args
16 EOF
17 test_commit one &&
18 test_commit two &&
Orgad Shaneh8581df62018-12-29 23:37:59 +020019 test_commit rebase-on-me &&
20 git reset --hard HEAD^ &&
Orgad Shaneh10499a92018-12-29 23:37:58 +020021 test_commit three
Josh England1abbe472007-09-26 15:31:01 -060022'
23
24test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
Orgad Shaneh10499a92018-12-29 23:37:58 +020025 test_when_finished "rm -f .git/post-checkout.args" &&
Johannes Schindelin966b4be2020-11-18 23:44:29 +000026 git checkout main &&
Orgad Shaneh10499a92018-12-29 23:37:58 +020027 read old new flag <.git/post-checkout.args &&
Elia Pinto7281f362014-06-06 07:55:59 -070028 test $old = $new && test $flag = 1
Josh England1abbe472007-09-26 15:31:01 -060029'
30
Josh England1abbe472007-09-26 15:31:01 -060031test_expect_success 'post-checkout args are correct with git checkout -b ' '
Orgad Shaneh10499a92018-12-29 23:37:58 +020032 test_when_finished "rm -f .git/post-checkout.args" &&
33 git checkout -b new1 &&
34 read old new flag <.git/post-checkout.args &&
Elia Pinto7281f362014-06-06 07:55:59 -070035 test $old = $new && test $flag = 1
Josh England1abbe472007-09-26 15:31:01 -060036'
37
38test_expect_success 'post-checkout receives the right args with HEAD changed ' '
Orgad Shaneh10499a92018-12-29 23:37:58 +020039 test_when_finished "rm -f .git/post-checkout.args" &&
40 git checkout two &&
41 read old new flag <.git/post-checkout.args &&
Elia Pinto7281f362014-06-06 07:55:59 -070042 test $old != $new && test $flag = 1
Josh England1abbe472007-09-26 15:31:01 -060043'
44
45test_expect_success 'post-checkout receives the right args when not switching branches ' '
Orgad Shaneh10499a92018-12-29 23:37:58 +020046 test_when_finished "rm -f .git/post-checkout.args" &&
Johannes Schindelin966b4be2020-11-18 23:44:29 +000047 git checkout main -- three.t &&
Orgad Shaneh10499a92018-12-29 23:37:58 +020048 read old new flag <.git/post-checkout.args &&
Elia Pinto7281f362014-06-06 07:55:59 -070049 test $old = $new && test $flag = 0
Josh England1abbe472007-09-26 15:31:01 -060050'
51
Phillip Woodbd55eee2022-01-26 13:05:37 +000052test_rebase () {
53 args="$*" &&
54 test_expect_success "post-checkout is triggered on rebase $args" '
55 test_when_finished "rm -f .git/post-checkout.args" &&
56 git checkout -B rebase-test main &&
57 rm -f .git/post-checkout.args &&
58 git rebase $args rebase-on-me &&
59 read old new flag <.git/post-checkout.args &&
60 test_cmp_rev main $old &&
61 test_cmp_rev rebase-on-me $new &&
62 test $flag = 1
63 '
Orgad Shaneh8581df62018-12-29 23:37:59 +020064
Phillip Woodbd55eee2022-01-26 13:05:37 +000065 test_expect_success "post-checkout is triggered on rebase $args with fast-forward" '
66 test_when_finished "rm -f .git/post-checkout.args" &&
67 git checkout -B ff-rebase-test rebase-on-me^ &&
68 rm -f .git/post-checkout.args &&
69 git rebase $args rebase-on-me &&
70 read old new flag <.git/post-checkout.args &&
71 test_cmp_rev rebase-on-me^ $old &&
72 test_cmp_rev rebase-on-me $new &&
73 test $flag = 1
74 '
Phillip Wood69f4c232022-01-26 13:05:38 +000075
76 test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" '
77 test_when_finished "test_might_fail git rebase --abort" &&
78 test_when_finished "rm -f .git/post-checkout.args" &&
79 git update-ref refs/heads/rebase-fast-forward three &&
80 git checkout two &&
81 rm -f .git/post-checkout.args &&
82 git rebase $args HEAD rebase-fast-forward &&
83 read old new flag <.git/post-checkout.args &&
84 test_cmp_rev two $old &&
85 test_cmp_rev three $new &&
86 test $flag = 1
87 '
Phillip Woodab2fba02022-01-26 13:05:39 +000088
89 test_expect_success "rebase $args checkout does not remove untracked files" '
90 test_when_finished "test_might_fail git rebase --abort" &&
Phillip Wood48400022022-01-26 13:05:40 +000091 test_when_finished "rm -f .git/post-checkout.args" &&
Phillip Woodab2fba02022-01-26 13:05:39 +000092 git update-ref refs/heads/rebase-fast-forward three &&
93 git checkout two &&
Phillip Wood48400022022-01-26 13:05:40 +000094 rm -f .git/post-checkout.args &&
Phillip Woodab2fba02022-01-26 13:05:39 +000095 echo untracked >three.t &&
96 test_when_finished "rm three.t" &&
97 test_must_fail git rebase $args HEAD rebase-fast-forward 2>err &&
Phillip Wood48400022022-01-26 13:05:40 +000098 grep "untracked working tree files would be overwritten by checkout" err &&
99 test_path_is_missing .git/post-checkout.args
100
Phillip Woodab2fba02022-01-26 13:05:39 +0000101'
Phillip Woodbd55eee2022-01-26 13:05:37 +0000102}
103
104test_rebase --apply &&
105test_rebase --merge
Orgad Shaneh8581df62018-12-29 23:37:59 +0200106
Jeff Kingdfa7a6c2009-03-03 00:37:51 -0500107test_expect_success 'post-checkout hook is triggered by clone' '
Orgad Shaneh10499a92018-12-29 23:37:58 +0200108 mkdir -p templates/hooks &&
109 write_script templates/hooks/post-checkout <<-\EOF &&
Randall S. Beckera250d412019-02-08 06:32:33 -0500110 echo "$@" >"$GIT_DIR/post-checkout.args"
Orgad Shaneh10499a92018-12-29 23:37:58 +0200111 EOF
Jeff Kingdfa7a6c2009-03-03 00:37:51 -0500112 git clone --template=templates . clone3 &&
113 test -f clone3/.git/post-checkout.args
114'
115
Josh England1abbe472007-09-26 15:31:01 -0600116test_done