blob: c0f024eb1ef4e9e60edef15ea8c7edcf8d98f661 [file] [log] [blame]
Wincent Colaiuta264474f2007-12-08 13:29:47 +01001#!/bin/sh
2
3test_description='commit-msg hook'
4
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
Wincent Colaiuta80f86602007-12-10 08:33:26 +010010test_expect_success 'with no hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +010011
Wincent Colaiuta80f86602007-12-10 08:33:26 +010012 echo "foo" > file &&
13 git add file &&
14 git commit -m "first"
15
16'
17
18# set up fake editor for interactive editing
19cat > fake-editor <<'EOF'
20#!/bin/sh
21cp FAKE_MSG "$1"
22exit 0
23EOF
24chmod +x fake-editor
Bryan Donlanf69e8362008-05-04 01:37:59 -040025
26## Not using test_set_editor here so we can easily ensure the editor variable
27## is only set for the editor tests
Wincent Colaiuta80f86602007-12-10 08:33:26 +010028FAKE_EDITOR="$(pwd)/fake-editor"
29export FAKE_EDITOR
30
31test_expect_success 'with no hook (editor)' '
32
33 echo "more foo" >> file &&
34 git add file &&
35 echo "more foo" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040036 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
Wincent Colaiuta80f86602007-12-10 08:33:26 +010037
38'
39
40test_expect_success '--no-verify with no hook' '
41
42 echo "bar" > file &&
43 git add file &&
44 git commit --no-verify -m "bar"
45
46'
47
48test_expect_success '--no-verify with no hook (editor)' '
49
50 echo "more bar" > file &&
51 git add file &&
52 echo "more bar" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040053 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +010054
55'
Wincent Colaiuta264474f2007-12-08 13:29:47 +010056
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +010057test_expect_success 'setup: commit-msg hook that always succeeds' '
58 test_hook --setup commit-msg <<-\EOF
59 exit 0
60 EOF
61'
Wincent Colaiuta264474f2007-12-08 13:29:47 +010062
Wincent Colaiuta80f86602007-12-10 08:33:26 +010063test_expect_success 'with succeeding hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +010064
Wincent Colaiuta80f86602007-12-10 08:33:26 +010065 echo "more" >> file &&
66 git add file &&
67 git commit -m "more"
68
69'
70
71test_expect_success 'with succeeding hook (editor)' '
72
73 echo "more more" >> file &&
74 git add file &&
75 echo "more more" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040076 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
Wincent Colaiuta80f86602007-12-10 08:33:26 +010077
78'
79
80test_expect_success '--no-verify with succeeding hook' '
81
82 echo "even more" >> file &&
83 git add file &&
84 git commit --no-verify -m "even more"
85
86'
87
88test_expect_success '--no-verify with succeeding hook (editor)' '
89
90 echo "even more more" >> file &&
91 git add file &&
92 echo "even more more" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040093 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +010094
95'
Wincent Colaiuta264474f2007-12-08 13:29:47 +010096
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +010097test_expect_success 'setup: commit-msg hook that always fails' '
98 test_hook --clobber commit-msg <<-\EOF
99 exit 1
100 EOF
101'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100102
Stefan Bellerf8b86352017-09-07 15:04:29 -0700103commit_msg_is () {
Ævar Arnfjörð Bjarmason4bd07852023-02-06 23:44:31 +0100104 printf "%s" "$1" >expect &&
105 git log --pretty=format:%s%b -1 >actual &&
106 test_cmp expect actual
Stefan Bellerf8b86352017-09-07 15:04:29 -0700107}
108
Junio C Hamano41ac4142008-02-01 01:50:53 -0800109test_expect_success 'with failing hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100110
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100111 echo "another" >> file &&
112 git add file &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200113 test_must_fail git commit -m "another"
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100114
115'
116
Junio C Hamano41ac4142008-02-01 01:50:53 -0800117test_expect_success 'with failing hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100118
119 echo "more another" >> file &&
120 git add file &&
121 echo "more another" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400122 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100123
124'
125
126test_expect_success '--no-verify with failing hook' '
127
128 echo "stuff" >> file &&
129 git add file &&
130 git commit --no-verify -m "stuff"
131
132'
133
Alex Riesenfa212962021-10-29 15:45:45 +0200134test_expect_success '-n followed by --verify with failing hook' '
135
136 echo "even more" >> file &&
137 git add file &&
138 test_must_fail git commit -n --verify -m "even more"
139
140'
141
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100142test_expect_success '--no-verify with failing hook (editor)' '
143
144 echo "more stuff" >> file &&
145 git add file &&
146 echo "more stuff" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400147 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100148
149'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100150
Stefan Bellerf8b86352017-09-07 15:04:29 -0700151test_expect_success 'merge fails with failing hook' '
152
153 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000154 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700155 git checkout --orphan newbranch &&
156 : >file2 &&
157 git add file2 &&
158 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000159 test_must_fail git merge --allow-unrelated-histories main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700160 commit_msg_is "in-side-branch" # HEAD before merge
161
162'
163
164test_expect_success 'merge bypasses failing hook with --no-verify' '
165
166 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000167 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700168 git checkout --orphan newbranch &&
Elijah Newreneddd1a42018-06-30 18:25:02 -0700169 git rm -f file &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700170 : >file2 &&
171 git add file2 &&
172 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000173 git merge --no-verify --allow-unrelated-histories main &&
174 commit_msg_is "Merge branch '\''main'\'' into newbranch"
Stefan Bellerf8b86352017-09-07 15:04:29 -0700175'
176
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100177test_expect_success 'setup: commit-msg hook made non-executable' '
178 git_dir="$(git rev-parse --git-dir)" &&
179 chmod -x "$git_dir/hooks/commit-msg"
180'
Stefan Bellerf8b86352017-09-07 15:04:29 -0700181
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100182
Johannes Sixtee9fb682009-03-13 22:55:27 +0100183test_expect_success POSIXPERM 'with non-executable hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100184
Elijah Newreneddd1a42018-06-30 18:25:02 -0700185 echo "content" >file &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100186 git add file &&
187 git commit -m "content"
188
189'
190
Johannes Sixtee9fb682009-03-13 22:55:27 +0100191test_expect_success POSIXPERM 'with non-executable hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100192
193 echo "content again" >> file &&
194 git add file &&
195 echo "content again" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400196 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100197
198'
199
Johannes Sixtee9fb682009-03-13 22:55:27 +0100200test_expect_success POSIXPERM '--no-verify with non-executable hook' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100201
202 echo "more content" >> file &&
203 git add file &&
204 git commit --no-verify -m "more content"
205
206'
207
Johannes Sixtee9fb682009-03-13 22:55:27 +0100208test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100209
210 echo "even more content" >> file &&
211 git add file &&
212 echo "even more content" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400213 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100214
215'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100216
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100217test_expect_success 'setup: commit-msg hook that edits the commit message' '
218 test_hook --clobber commit-msg <<-\EOF
219 echo "new message" >"$1"
220 exit 0
221 EOF
222'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100223
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100224test_expect_success 'hook edits commit message' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100225
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100226 echo "additional" >> file &&
227 git add file &&
228 git commit -m "additional" &&
229 commit_msg_is "new message"
230
231'
232
233test_expect_success 'hook edits commit message (editor)' '
234
235 echo "additional content" >> file &&
236 git add file &&
237 echo "additional content" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400238 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100239 commit_msg_is "new message"
240
241'
242
243test_expect_success "hook doesn't edit commit message" '
244
245 echo "plus" >> file &&
246 git add file &&
247 git commit --no-verify -m "plus" &&
248 commit_msg_is "plus"
249
250'
251
252test_expect_success "hook doesn't edit commit message (editor)" '
253
254 echo "more plus" >> file &&
255 git add file &&
256 echo "more plus" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400257 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100258 commit_msg_is "more plus"
Stefan Bellerf8b86352017-09-07 15:04:29 -0700259'
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100260
Stefan Bellerf8b86352017-09-07 15:04:29 -0700261test_expect_success 'hook called in git-merge picks up commit message' '
262 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000263 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700264 git checkout --orphan newbranch &&
Elijah Newreneddd1a42018-06-30 18:25:02 -0700265 git rm -f file &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700266 : >file2 &&
267 git add file2 &&
268 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000269 git merge --allow-unrelated-histories main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700270 commit_msg_is "new message"
271'
272
273test_expect_failure 'merge --continue remembers --no-verify' '
274 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000275 test_when_finished "git checkout -f main" &&
276 git checkout main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700277 echo a >file2 &&
278 git add file2 &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000279 git commit --no-verify -m "add file2 to main" &&
280 git checkout -b newbranch main^ &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700281 echo b >file2 &&
282 git add file2 &&
283 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000284 git merge --no-verify -m not-rewritten-by-hook main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700285 # resolve conflict:
286 echo c >file2 &&
287 git add file2 &&
288 git merge --continue &&
289 commit_msg_is not-rewritten-by-hook
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100290'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100291
Johannes Schindelincb7fb9e2017-03-22 16:01:19 +0100292# set up fake editor to replace `pick` by `reword`
293cat > reword-editor <<'EOF'
294#!/bin/sh
295mv "$1" "$1".bup &&
296sed 's/^pick/reword/' <"$1".bup >"$1"
297EOF
298chmod +x reword-editor
299REWORD_EDITOR="$(pwd)/reword-editor"
300export REWORD_EDITOR
301
Johannes Schindelinb92ff6e2017-03-23 17:07:17 +0100302test_expect_success 'hook is called for reword during `rebase -i`' '
Johannes Schindelincb7fb9e2017-03-22 16:01:19 +0100303
304 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
305 commit_msg_is "new message"
306
307'
308
Stefan Bellerf8b86352017-09-07 15:04:29 -0700309
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100310test_done