blob: 606d8d0f08909622d7f916ba4fd55bdfb3d820f1 [file] [log] [blame]
Wincent Colaiuta264474f2007-12-08 13:29:47 +01001#!/bin/sh
2
Michael J Gruber60988172019-08-07 11:57:07 -07003test_description='pre-commit and pre-merge-commit hooks'
Wincent Colaiuta264474f2007-12-08 13:29:47 +01004
Johannes Schindelin1e2ae142020-11-18 23:44:40 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Wincent Colaiuta264474f2007-12-08 13:29:47 +01008. ./test-lib.sh
9
Josh Steadmonf78f6c72019-08-07 11:57:05 -070010HOOKDIR="$(git rev-parse --git-dir)/hooks"
11PRECOMMIT="$HOOKDIR/pre-commit"
Michael J Gruber60988172019-08-07 11:57:07 -070012PREMERGE="$HOOKDIR/pre-merge-commit"
Josh Steadmonf78f6c72019-08-07 11:57:05 -070013
14# Prepare sample scripts that write their $0 to actual_hooks
15test_expect_success 'sample script setup' '
16 mkdir -p "$HOOKDIR" &&
17 write_script "$HOOKDIR/success.sample" <<-\EOF &&
18 echo $0 >>actual_hooks
19 exit 0
20 EOF
21 write_script "$HOOKDIR/fail.sample" <<-\EOF &&
22 echo $0 >>actual_hooks
23 exit 1
24 EOF
25 write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
26 echo $0 >>actual_hooks
27 exit 1
28 EOF
29 chmod -x "$HOOKDIR/non-exec.sample" &&
30 write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
31 echo $0 >>actual_hooks
32 test $GIT_PREFIX = "success/"
33 EOF
34 write_script "$HOOKDIR/check-author.sample" <<-\EOF
35 echo $0 >>actual_hooks
36 test "$GIT_AUTHOR_NAME" = "New Author" &&
37 test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
38 EOF
39'
40
Michael J Gruber60988172019-08-07 11:57:07 -070041test_expect_success 'root commit' '
42 echo "root" >file &&
43 git add file &&
44 git commit -m "zeroth" &&
45 git checkout -b side &&
46 echo "foo" >foo &&
47 git add foo &&
48 git commit -m "make it non-ff" &&
49 git branch side-orig side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000050 git checkout main
Michael J Gruber60988172019-08-07 11:57:07 -070051'
52
53test_expect_success 'setup conflicting branches' '
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000054 test_when_finished "git checkout main" &&
55 git checkout -b conflicting-a main &&
Michael J Gruber60988172019-08-07 11:57:07 -070056 echo a >conflicting &&
57 git add conflicting &&
58 git commit -m conflicting-a &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000059 git checkout -b conflicting-b main &&
Michael J Gruber60988172019-08-07 11:57:07 -070060 echo b >conflicting &&
61 git add conflicting &&
62 git commit -m conflicting-b
63'
64
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010065test_expect_success 'with no hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -070066 test_when_finished "rm -f actual_hooks" &&
67 echo "foo" >file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010068 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -070069 git commit -m "first" &&
70 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010071'
72
Michael J Gruber60988172019-08-07 11:57:07 -070073test_expect_success 'with no hook (merge)' '
74 test_when_finished "rm -f actual_hooks" &&
75 git branch -f side side-orig &&
76 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000077 git merge -m "merge main" main &&
78 git checkout main &&
Michael J Gruber60988172019-08-07 11:57:07 -070079 test_path_is_missing actual_hooks
80'
81
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010082test_expect_success '--no-verify with no hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -070083 test_when_finished "rm -f actual_hooks" &&
84 echo "bar" >file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010085 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -070086 git commit --no-verify -m "bar" &&
87 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010088'
Wincent Colaiuta264474f2007-12-08 13:29:47 +010089
Michael J Gruberbc40ce42019-08-07 11:57:08 -070090test_expect_success '--no-verify with no hook (merge)' '
91 test_when_finished "rm -f actual_hooks" &&
92 git branch -f side side-orig &&
93 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000094 git merge --no-verify -m "merge main" main &&
95 git checkout main &&
Michael J Gruberbc40ce42019-08-07 11:57:08 -070096 test_path_is_missing actual_hooks
97'
98
Wincent Colaiutacf7e1472007-12-10 08:42:45 +010099test_expect_success 'with succeeding hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700100 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
101 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
102 echo "$PRECOMMIT" >expected_hooks &&
103 echo "more" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100104 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700105 git commit -m "more" &&
106 test_cmp expected_hooks actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100107'
108
Michael J Gruber60988172019-08-07 11:57:07 -0700109test_expect_success 'with succeeding hook (merge)' '
110 test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
111 cp "$HOOKDIR/success.sample" "$PREMERGE" &&
112 echo "$PREMERGE" >expected_hooks &&
113 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000114 git merge -m "merge main" main &&
115 git checkout main &&
Michael J Gruber60988172019-08-07 11:57:07 -0700116 test_cmp expected_hooks actual_hooks
117'
118
119test_expect_success 'automatic merge fails; both hooks are available' '
120 test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" &&
121 test_when_finished "rm -f expected_hooks actual_hooks" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000122 test_when_finished "git checkout main" &&
Michael J Gruber60988172019-08-07 11:57:07 -0700123 cp "$HOOKDIR/success.sample" "$PREMERGE" &&
124 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
125
126 git checkout conflicting-a &&
127 test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
128 test_path_is_missing actual_hooks &&
129
130 echo "$PRECOMMIT" >expected_hooks &&
131 echo a+b >conflicting &&
132 git add conflicting &&
133 git commit -m "resolve conflict" &&
134 test_cmp expected_hooks actual_hooks
135'
136
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100137test_expect_success '--no-verify with succeeding hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700138 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
139 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
140 echo "even more" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100141 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700142 git commit --no-verify -m "even more" &&
143 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100144'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100145
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700146test_expect_success '--no-verify with succeeding hook (merge)' '
147 test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
148 cp "$HOOKDIR/success.sample" "$PREMERGE" &&
149 git branch -f side side-orig &&
150 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000151 git merge --no-verify -m "merge main" main &&
152 git checkout main &&
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700153 test_path_is_missing actual_hooks
154'
155
Junio C Hamano41ac4142008-02-01 01:50:53 -0800156test_expect_success 'with failing hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700157 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
158 cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
159 echo "$PRECOMMIT" >expected_hooks &&
160 echo "another" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100161 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700162 test_must_fail git commit -m "another" &&
163 test_cmp expected_hooks actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100164'
165
166test_expect_success '--no-verify with failing hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700167 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
168 cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
169 echo "stuff" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100170 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700171 git commit --no-verify -m "stuff" &&
172 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100173'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100174
Michael J Gruber60988172019-08-07 11:57:07 -0700175test_expect_success 'with failing hook (merge)' '
176 test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
177 cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
178 echo "$PREMERGE" >expected_hooks &&
179 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000180 test_must_fail git merge -m "merge main" main &&
181 git checkout main &&
Michael J Gruber60988172019-08-07 11:57:07 -0700182 test_cmp expected_hooks actual_hooks
183'
184
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700185test_expect_success '--no-verify with failing hook (merge)' '
186 test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
187 cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
188 git branch -f side side-orig &&
189 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000190 git merge --no-verify -m "merge main" main &&
191 git checkout main &&
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700192 test_path_is_missing actual_hooks
193'
194
Johannes Sixtee9fb682009-03-13 22:55:27 +0100195test_expect_success POSIXPERM 'with non-executable hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700196 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
197 cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
198 echo "content" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100199 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700200 git commit -m "content" &&
201 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100202'
203
Johannes Sixtee9fb682009-03-13 22:55:27 +0100204test_expect_success POSIXPERM '--no-verify with non-executable hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700205 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
206 cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
207 echo "more content" >>file &&
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100208 git add file &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700209 git commit --no-verify -m "more content" &&
210 test_path_is_missing actual_hooks
Wincent Colaiutacf7e1472007-12-10 08:42:45 +0100211'
David Aguilarc35ec8c2011-06-02 02:26:25 -0700212
Michael J Gruber60988172019-08-07 11:57:07 -0700213test_expect_success POSIXPERM 'with non-executable hook (merge)' '
214 test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
215 cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
216 git branch -f side side-orig &&
217 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000218 git merge -m "merge main" main &&
219 git checkout main &&
Michael J Gruber60988172019-08-07 11:57:07 -0700220 test_path_is_missing actual_hooks
221'
222
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700223test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
224 test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
225 cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
226 git branch -f side side-orig &&
227 git checkout side &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000228 git merge --no-verify -m "merge main" main &&
229 git checkout main &&
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700230 test_path_is_missing actual_hooks
231'
232
David Aguilarc35ec8c2011-06-02 02:26:25 -0700233test_expect_success 'with hook requiring GIT_PREFIX' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700234 test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
235 cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
236 echo "$PRECOMMIT" >expected_hooks &&
237 echo "more content" >>file &&
David Aguilarc35ec8c2011-06-02 02:26:25 -0700238 git add file &&
239 mkdir success &&
240 (
241 cd success &&
242 git commit -m "hook requires GIT_PREFIX = success/"
243 ) &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700244 test_cmp expected_hooks actual_hooks
David Aguilarc35ec8c2011-06-02 02:26:25 -0700245'
246
247test_expect_success 'with failing hook requiring GIT_PREFIX' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700248 test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" &&
249 cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
250 echo "$PRECOMMIT" >expected_hooks &&
251 echo "more content" >>file &&
David Aguilarc35ec8c2011-06-02 02:26:25 -0700252 git add file &&
253 mkdir fail &&
254 (
255 cd fail &&
256 test_must_fail git commit -m "hook must fail"
257 ) &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700258 git checkout -- file &&
259 test_cmp expected_hooks actual_hooks
David Aguilarc35ec8c2011-06-02 02:26:25 -0700260'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100261
Junio C Hamano7dfe8ad2012-03-11 03:12:10 -0700262test_expect_success 'check the author in hook' '
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700263 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
264 cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
265 cat >expected_hooks <<-EOF &&
266 $PRECOMMIT
267 $PRECOMMIT
268 $PRECOMMIT
Junio C Hamano04861982012-03-11 03:51:32 -0700269 EOF
270 test_must_fail git commit --allow-empty -m "by a.u.thor" &&
271 (
272 GIT_AUTHOR_NAME="New Author" &&
273 GIT_AUTHOR_EMAIL="newauthor@example.com" &&
274 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
275 git commit --allow-empty -m "by new.author via env" &&
276 git show -s
277 ) &&
278 git commit --author="New Author <newauthor@example.com>" \
279 --allow-empty -m "by new.author via command line" &&
Josh Steadmonf78f6c72019-08-07 11:57:05 -0700280 git show -s &&
281 test_cmp expected_hooks actual_hooks
Junio C Hamano04861982012-03-11 03:51:32 -0700282'
283
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100284test_done