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