]> git.ipfire.org Git - thirdparty/git.git/blob - t/t5403-post-checkout-hook.sh
The third batch
[thirdparty/git.git] / t / t5403-post-checkout-hook.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Josh England
4 #
5
6 test_description='Test the post-checkout hook.'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
12
13 test_expect_success setup '
14 test_hook --setup post-checkout <<-\EOF &&
15 echo "$@" >.git/post-checkout.args
16 EOF
17 test_commit one &&
18 test_commit two &&
19 test_commit rebase-on-me &&
20 git reset --hard HEAD^ &&
21 test_commit three
22 '
23
24 test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
25 test_when_finished "rm -f .git/post-checkout.args" &&
26 git checkout main &&
27 read old new flag <.git/post-checkout.args &&
28 test $old = $new && test $flag = 1
29 '
30
31 test_expect_success 'post-checkout args are correct with git checkout -b ' '
32 test_when_finished "rm -f .git/post-checkout.args" &&
33 git checkout -b new1 &&
34 read old new flag <.git/post-checkout.args &&
35 test $old = $new && test $flag = 1
36 '
37
38 test_expect_success 'post-checkout receives the right args with HEAD changed ' '
39 test_when_finished "rm -f .git/post-checkout.args" &&
40 git checkout two &&
41 read old new flag <.git/post-checkout.args &&
42 test $old != $new && test $flag = 1
43 '
44
45 test_expect_success 'post-checkout receives the right args when not switching branches ' '
46 test_when_finished "rm -f .git/post-checkout.args" &&
47 git checkout main -- three.t &&
48 read old new flag <.git/post-checkout.args &&
49 test $old = $new && test $flag = 0
50 '
51
52 test_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 '
64
65 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 '
75
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 '
88
89 test_expect_success "rebase $args checkout does not remove untracked files" '
90 test_when_finished "test_might_fail git rebase --abort" &&
91 test_when_finished "rm -f .git/post-checkout.args" &&
92 git update-ref refs/heads/rebase-fast-forward three &&
93 git checkout two &&
94 rm -f .git/post-checkout.args &&
95 echo untracked >three.t &&
96 test_when_finished "rm three.t" &&
97 test_must_fail git rebase $args HEAD rebase-fast-forward 2>err &&
98 grep "untracked working tree files would be overwritten by checkout" err &&
99 test_path_is_missing .git/post-checkout.args
100
101 '
102 }
103
104 test_rebase --apply &&
105 test_rebase --merge
106
107 test_expect_success 'post-checkout hook is triggered by clone' '
108 mkdir -p templates/hooks &&
109 write_script templates/hooks/post-checkout <<-\EOF &&
110 echo "$@" >"$GIT_DIR/post-checkout.args"
111 EOF
112 git clone --template=templates . clone3 &&
113 test -f clone3/.git/post-checkout.args
114 '
115
116 test_done