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