3 test_description='pre-commit and pre-merge-commit hooks'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 HOOKDIR="$(git rev-parse --git-dir)/hooks"
11 PRECOMMIT="$HOOKDIR/pre-commit"
12 PREMERGE="$HOOKDIR/pre-merge-commit"
14 # Prepare sample scripts that write their $0 to actual_hooks
15 test_expect_success 'sample script setup' '
16 mkdir -p "$HOOKDIR" &&
17 write_script "$HOOKDIR/success.sample" <<-\EOF &&
18 echo $0 >>actual_hooks
21 write_script "$HOOKDIR/fail.sample" <<-\EOF &&
22 echo $0 >>actual_hooks
25 write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
26 echo $0 >>actual_hooks
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/"
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"
41 test_expect_success 'root commit' '
44 git commit -m "zeroth" &&
45 git checkout -b side &&
48 git commit -m "make it non-ff" &&
49 git branch side-orig side &&
53 test_expect_success 'setup conflicting branches' '
54 test_when_finished "git checkout main" &&
55 git checkout -b conflicting-a main &&
56 echo a >conflicting &&
57 git add conflicting &&
58 git commit -m conflicting-a &&
59 git checkout -b conflicting-b main &&
60 echo b >conflicting &&
61 git add conflicting &&
62 git commit -m conflicting-b
65 test_expect_success 'with no hook' '
66 test_when_finished "rm -f actual_hooks" &&
69 git commit -m "first" &&
70 test_path_is_missing actual_hooks
73 test_expect_success 'with no hook (merge)' '
74 test_when_finished "rm -f actual_hooks" &&
75 git branch -f side side-orig &&
77 git merge -m "merge main" main &&
79 test_path_is_missing actual_hooks
82 test_expect_success '--no-verify with no hook' '
83 test_when_finished "rm -f actual_hooks" &&
86 git commit --no-verify -m "bar" &&
87 test_path_is_missing actual_hooks
90 test_expect_success '--no-verify with no hook (merge)' '
91 test_when_finished "rm -f actual_hooks" &&
92 git branch -f side side-orig &&
94 git merge --no-verify -m "merge main" main &&
96 test_path_is_missing actual_hooks
99 test_expect_success 'with succeeding hook' '
100 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 &&
105 git commit -m "more" &&
106 test_cmp expected_hooks actual_hooks
109 test_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 &&
114 git merge -m "merge main" main &&
116 test_cmp expected_hooks actual_hooks
119 test_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" &&
122 test_when_finished "git checkout main" &&
123 cp "$HOOKDIR/success.sample" "$PREMERGE" &&
124 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
126 git checkout conflicting-a &&
127 test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
128 test_path_is_missing actual_hooks &&
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
137 test_expect_success '--no-verify with succeeding hook' '
138 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
139 cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
140 echo "even more" >>file &&
142 git commit --no-verify -m "even more" &&
143 test_path_is_missing actual_hooks
146 test_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 &&
151 git merge --no-verify -m "merge main" main &&
153 test_path_is_missing actual_hooks
156 test_expect_success 'with failing hook' '
157 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 &&
162 test_must_fail git commit -m "another" &&
163 test_cmp expected_hooks actual_hooks
166 test_expect_success '--no-verify with failing hook' '
167 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
168 cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
169 echo "stuff" >>file &&
171 git commit --no-verify -m "stuff" &&
172 test_path_is_missing actual_hooks
175 test_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 &&
180 test_must_fail git merge -m "merge main" main &&
182 test_cmp expected_hooks actual_hooks
185 test_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 &&
190 git merge --no-verify -m "merge main" main &&
192 test_path_is_missing actual_hooks
195 test_expect_success POSIXPERM 'with non-executable hook' '
196 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
197 cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
198 echo "content" >>file &&
200 git commit -m "content" &&
201 test_path_is_missing actual_hooks
204 test_expect_success POSIXPERM '--no-verify with non-executable hook' '
205 test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
206 cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
207 echo "more content" >>file &&
209 git commit --no-verify -m "more content" &&
210 test_path_is_missing actual_hooks
213 test_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 &&
218 git merge -m "merge main" main &&
220 test_path_is_missing actual_hooks
223 test_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 &&
228 git merge --no-verify -m "merge main" main &&
230 test_path_is_missing actual_hooks
233 test_expect_success 'with hook requiring GIT_PREFIX' '
234 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 &&
242 git commit -m "hook requires GIT_PREFIX = success/"
244 test_cmp expected_hooks actual_hooks
247 test_expect_success 'with failing hook requiring GIT_PREFIX' '
248 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 &&
256 test_must_fail git commit -m "hook must fail"
258 git checkout -- file &&
259 test_cmp expected_hooks actual_hooks
262 test_expect_success 'check the author in hook' '
263 test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
264 cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
265 cat >expected_hooks <<-EOF &&
270 test_must_fail git commit --allow-empty -m "by a.u.thor" &&
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" &&
278 git commit --author="New Author <newauthor@example.com>" \
279 --allow-empty -m "by new.author via command line" &&
281 test_cmp expected_hooks actual_hooks