]>
Commit | Line | Data |
---|---|---|
264474f2 WC |
1 | #!/bin/sh |
2 | ||
6098817f | 3 | test_description='pre-commit and pre-merge-commit hooks' |
264474f2 | 4 | |
1e2ae142 | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
055e57b7 | 8 | TEST_PASSES_SANITIZE_LEAK=true |
264474f2 WC |
9 | . ./test-lib.sh |
10 | ||
6098817f MG |
11 | test_expect_success 'root commit' ' |
12 | echo "root" >file && | |
13 | git add file && | |
14 | git commit -m "zeroth" && | |
15 | git checkout -b side && | |
16 | echo "foo" >foo && | |
17 | git add foo && | |
18 | git commit -m "make it non-ff" && | |
19 | git branch side-orig side && | |
1e2ae142 | 20 | git checkout main |
6098817f MG |
21 | ' |
22 | ||
23 | test_expect_success 'setup conflicting branches' ' | |
1e2ae142 JS |
24 | test_when_finished "git checkout main" && |
25 | git checkout -b conflicting-a main && | |
6098817f MG |
26 | echo a >conflicting && |
27 | git add conflicting && | |
28 | git commit -m conflicting-a && | |
1e2ae142 | 29 | git checkout -b conflicting-b main && |
6098817f MG |
30 | echo b >conflicting && |
31 | git add conflicting && | |
32 | git commit -m conflicting-b | |
33 | ' | |
34 | ||
f78f6c7e JS |
35 | test_expect_success 'with no hook' ' |
36 | test_when_finished "rm -f actual_hooks" && | |
37 | echo "foo" >file && | |
cf7e147c | 38 | git add file && |
f78f6c7e JS |
39 | git commit -m "first" && |
40 | test_path_is_missing actual_hooks | |
cf7e147c WC |
41 | ' |
42 | ||
6098817f MG |
43 | test_expect_success 'with no hook (merge)' ' |
44 | test_when_finished "rm -f actual_hooks" && | |
45 | git branch -f side side-orig && | |
46 | git checkout side && | |
1e2ae142 JS |
47 | git merge -m "merge main" main && |
48 | git checkout main && | |
6098817f MG |
49 | test_path_is_missing actual_hooks |
50 | ' | |
51 | ||
cf7e147c | 52 | test_expect_success '--no-verify with no hook' ' |
f78f6c7e JS |
53 | test_when_finished "rm -f actual_hooks" && |
54 | echo "bar" >file && | |
cf7e147c | 55 | git add file && |
f78f6c7e JS |
56 | git commit --no-verify -m "bar" && |
57 | test_path_is_missing actual_hooks | |
cf7e147c | 58 | ' |
264474f2 | 59 | |
bc40ce4d MG |
60 | test_expect_success '--no-verify with no hook (merge)' ' |
61 | test_when_finished "rm -f actual_hooks" && | |
62 | git branch -f side side-orig && | |
63 | git checkout side && | |
1e2ae142 JS |
64 | git merge --no-verify -m "merge main" main && |
65 | git checkout main && | |
bc40ce4d MG |
66 | test_path_is_missing actual_hooks |
67 | ' | |
68 | ||
66865d12 ÆAB |
69 | setup_success_hook () { |
70 | test_when_finished "rm -f actual_hooks expected_hooks" && | |
71 | echo "$1" >expected_hooks && | |
72 | test_hook "$1" <<-EOF | |
73 | echo $1 >>actual_hooks | |
74 | EOF | |
75 | } | |
76 | ||
cf7e147c | 77 | test_expect_success 'with succeeding hook' ' |
66865d12 | 78 | setup_success_hook "pre-commit" && |
f78f6c7e | 79 | echo "more" >>file && |
cf7e147c | 80 | git add file && |
f78f6c7e JS |
81 | git commit -m "more" && |
82 | test_cmp expected_hooks actual_hooks | |
cf7e147c WC |
83 | ' |
84 | ||
6098817f | 85 | test_expect_success 'with succeeding hook (merge)' ' |
66865d12 | 86 | setup_success_hook "pre-merge-commit" && |
6098817f | 87 | git checkout side && |
1e2ae142 JS |
88 | git merge -m "merge main" main && |
89 | git checkout main && | |
6098817f MG |
90 | test_cmp expected_hooks actual_hooks |
91 | ' | |
92 | ||
93 | test_expect_success 'automatic merge fails; both hooks are available' ' | |
66865d12 ÆAB |
94 | setup_success_hook "pre-commit" && |
95 | setup_success_hook "pre-merge-commit" && | |
6098817f MG |
96 | |
97 | git checkout conflicting-a && | |
98 | test_must_fail git merge -m "merge conflicting-b" conflicting-b && | |
99 | test_path_is_missing actual_hooks && | |
100 | ||
66865d12 | 101 | echo "pre-commit" >expected_hooks && |
6098817f MG |
102 | echo a+b >conflicting && |
103 | git add conflicting && | |
104 | git commit -m "resolve conflict" && | |
105 | test_cmp expected_hooks actual_hooks | |
106 | ' | |
107 | ||
cf7e147c | 108 | test_expect_success '--no-verify with succeeding hook' ' |
66865d12 | 109 | setup_success_hook "pre-commit" && |
f78f6c7e | 110 | echo "even more" >>file && |
cf7e147c | 111 | git add file && |
f78f6c7e JS |
112 | git commit --no-verify -m "even more" && |
113 | test_path_is_missing actual_hooks | |
cf7e147c | 114 | ' |
264474f2 | 115 | |
bc40ce4d | 116 | test_expect_success '--no-verify with succeeding hook (merge)' ' |
66865d12 | 117 | setup_success_hook "pre-merge-commit" && |
bc40ce4d MG |
118 | git branch -f side side-orig && |
119 | git checkout side && | |
1e2ae142 JS |
120 | git merge --no-verify -m "merge main" main && |
121 | git checkout main && | |
bc40ce4d MG |
122 | test_path_is_missing actual_hooks |
123 | ' | |
124 | ||
66865d12 ÆAB |
125 | setup_failing_hook () { |
126 | test_when_finished "rm -f actual_hooks" && | |
127 | test_hook "$1" <<-EOF | |
128 | echo $1-failing-hook >>actual_hooks | |
129 | exit 1 | |
130 | EOF | |
131 | } | |
132 | ||
41ac414e | 133 | test_expect_success 'with failing hook' ' |
66865d12 ÆAB |
134 | setup_failing_hook "pre-commit" && |
135 | test_when_finished "rm -f expected_hooks" && | |
136 | echo "pre-commit-failing-hook" >expected_hooks && | |
137 | ||
f78f6c7e | 138 | echo "another" >>file && |
cf7e147c | 139 | git add file && |
f78f6c7e JS |
140 | test_must_fail git commit -m "another" && |
141 | test_cmp expected_hooks actual_hooks | |
cf7e147c WC |
142 | ' |
143 | ||
144 | test_expect_success '--no-verify with failing hook' ' | |
66865d12 | 145 | setup_failing_hook "pre-commit" && |
f78f6c7e | 146 | echo "stuff" >>file && |
cf7e147c | 147 | git add file && |
f78f6c7e JS |
148 | git commit --no-verify -m "stuff" && |
149 | test_path_is_missing actual_hooks | |
cf7e147c | 150 | ' |
264474f2 | 151 | |
6098817f | 152 | test_expect_success 'with failing hook (merge)' ' |
66865d12 ÆAB |
153 | setup_failing_hook "pre-merge-commit" && |
154 | echo "pre-merge-commit-failing-hook" >expected_hooks && | |
6098817f | 155 | git checkout side && |
1e2ae142 JS |
156 | test_must_fail git merge -m "merge main" main && |
157 | git checkout main && | |
6098817f MG |
158 | test_cmp expected_hooks actual_hooks |
159 | ' | |
160 | ||
bc40ce4d | 161 | test_expect_success '--no-verify with failing hook (merge)' ' |
66865d12 ÆAB |
162 | setup_failing_hook "pre-merge-commit" && |
163 | ||
bc40ce4d MG |
164 | git branch -f side side-orig && |
165 | git checkout side && | |
1e2ae142 JS |
166 | git merge --no-verify -m "merge main" main && |
167 | git checkout main && | |
bc40ce4d MG |
168 | test_path_is_missing actual_hooks |
169 | ' | |
170 | ||
66865d12 ÆAB |
171 | setup_non_exec_hook () { |
172 | test_when_finished "rm -f actual_hooks" && | |
173 | test_hook "$1" <<-\EOF && | |
174 | echo non-exec >>actual_hooks | |
175 | exit 1 | |
176 | EOF | |
177 | test_hook --disable "$1" | |
178 | } | |
179 | ||
180 | ||
ee9fb68c | 181 | test_expect_success POSIXPERM 'with non-executable hook' ' |
66865d12 | 182 | setup_non_exec_hook "pre-commit" && |
f78f6c7e | 183 | echo "content" >>file && |
cf7e147c | 184 | git add file && |
f78f6c7e JS |
185 | git commit -m "content" && |
186 | test_path_is_missing actual_hooks | |
cf7e147c WC |
187 | ' |
188 | ||
ee9fb68c | 189 | test_expect_success POSIXPERM '--no-verify with non-executable hook' ' |
66865d12 | 190 | setup_non_exec_hook "pre-commit" && |
f78f6c7e | 191 | echo "more content" >>file && |
cf7e147c | 192 | git add file && |
f78f6c7e JS |
193 | git commit --no-verify -m "more content" && |
194 | test_path_is_missing actual_hooks | |
cf7e147c | 195 | ' |
c35ec8c9 | 196 | |
6098817f | 197 | test_expect_success POSIXPERM 'with non-executable hook (merge)' ' |
66865d12 | 198 | setup_non_exec_hook "pre-merge" && |
6098817f MG |
199 | git branch -f side side-orig && |
200 | git checkout side && | |
1e2ae142 JS |
201 | git merge -m "merge main" main && |
202 | git checkout main && | |
6098817f MG |
203 | test_path_is_missing actual_hooks |
204 | ' | |
205 | ||
bc40ce4d | 206 | test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' ' |
66865d12 | 207 | setup_non_exec_hook "pre-merge" && |
bc40ce4d MG |
208 | git branch -f side side-orig && |
209 | git checkout side && | |
1e2ae142 JS |
210 | git merge --no-verify -m "merge main" main && |
211 | git checkout main && | |
bc40ce4d MG |
212 | test_path_is_missing actual_hooks |
213 | ' | |
214 | ||
66865d12 ÆAB |
215 | setup_require_prefix_hook () { |
216 | test_when_finished "rm -f expected_hooks" && | |
217 | echo require-prefix >expected_hooks && | |
218 | test_hook pre-commit <<-\EOF | |
219 | echo require-prefix >>actual_hooks | |
220 | test $GIT_PREFIX = "success/" | |
221 | EOF | |
222 | } | |
223 | ||
c35ec8c9 | 224 | test_expect_success 'with hook requiring GIT_PREFIX' ' |
66865d12 ÆAB |
225 | test_when_finished "rm -rf actual_hooks success" && |
226 | setup_require_prefix_hook && | |
f78f6c7e | 227 | echo "more content" >>file && |
c35ec8c9 DA |
228 | git add file && |
229 | mkdir success && | |
230 | ( | |
231 | cd success && | |
232 | git commit -m "hook requires GIT_PREFIX = success/" | |
233 | ) && | |
f78f6c7e | 234 | test_cmp expected_hooks actual_hooks |
c35ec8c9 DA |
235 | ' |
236 | ||
237 | test_expect_success 'with failing hook requiring GIT_PREFIX' ' | |
66865d12 ÆAB |
238 | test_when_finished "rm -rf actual_hooks fail" && |
239 | setup_require_prefix_hook && | |
f78f6c7e | 240 | echo "more content" >>file && |
c35ec8c9 DA |
241 | git add file && |
242 | mkdir fail && | |
243 | ( | |
244 | cd fail && | |
245 | test_must_fail git commit -m "hook must fail" | |
246 | ) && | |
f78f6c7e JS |
247 | git checkout -- file && |
248 | test_cmp expected_hooks actual_hooks | |
c35ec8c9 | 249 | ' |
264474f2 | 250 | |
66865d12 ÆAB |
251 | setup_require_author_hook () { |
252 | test_when_finished "rm -f expected_hooks actual_hooks" && | |
253 | echo check-author >expected_hooks && | |
254 | test_hook pre-commit <<-\EOF | |
255 | echo check-author >>actual_hooks | |
256 | test "$GIT_AUTHOR_NAME" = "New Author" && | |
257 | test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com" | |
258 | EOF | |
259 | } | |
260 | ||
261 | ||
7dfe8ad6 | 262 | test_expect_success 'check the author in hook' ' |
66865d12 | 263 | setup_require_author_hook && |
f78f6c7e | 264 | cat >expected_hooks <<-EOF && |
66865d12 ÆAB |
265 | check-author |
266 | check-author | |
267 | check-author | |
04861982 JH |
268 | EOF |
269 | test_must_fail git commit --allow-empty -m "by a.u.thor" && | |
270 | ( | |
271 | GIT_AUTHOR_NAME="New Author" && | |
272 | GIT_AUTHOR_EMAIL="newauthor@example.com" && | |
273 | export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL && | |
274 | git commit --allow-empty -m "by new.author via env" && | |
275 | git show -s | |
276 | ) && | |
277 | git commit --author="New Author <newauthor@example.com>" \ | |
278 | --allow-empty -m "by new.author via command line" && | |
f78f6c7e JS |
279 | git show -s && |
280 | test_cmp expected_hooks actual_hooks | |
04861982 JH |
281 | ' |
282 | ||
264474f2 | 283 | test_done |