blob: bba58f04806fdfc247c1b72112ed6805340934c1 [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
57# now install hook that always succeeds
58HOOKDIR="$(git rev-parse --git-dir)/hooks"
59HOOK="$HOOKDIR/commit-msg"
60mkdir -p "$HOOKDIR"
61cat > "$HOOK" <<EOF
62#!/bin/sh
63exit 0
64EOF
65chmod +x "$HOOK"
66
Wincent Colaiuta80f86602007-12-10 08:33:26 +010067test_expect_success 'with succeeding hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +010068
Wincent Colaiuta80f86602007-12-10 08:33:26 +010069 echo "more" >> file &&
70 git add file &&
71 git commit -m "more"
72
73'
74
75test_expect_success 'with succeeding hook (editor)' '
76
77 echo "more more" >> file &&
78 git add file &&
79 echo "more more" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040080 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
Wincent Colaiuta80f86602007-12-10 08:33:26 +010081
82'
83
84test_expect_success '--no-verify with succeeding hook' '
85
86 echo "even more" >> file &&
87 git add file &&
88 git commit --no-verify -m "even more"
89
90'
91
92test_expect_success '--no-verify with succeeding hook (editor)' '
93
94 echo "even more more" >> file &&
95 git add file &&
96 echo "even more more" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -040097 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +010098
99'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100100
101# now a hook that fails
102cat > "$HOOK" <<EOF
103#!/bin/sh
104exit 1
105EOF
106
Stefan Bellerf8b86352017-09-07 15:04:29 -0700107commit_msg_is () {
108 test "$(git log --pretty=format:%s%b -1)" = "$1"
109}
110
Junio C Hamano41ac4142008-02-01 01:50:53 -0800111test_expect_success 'with failing hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100112
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100113 echo "another" >> file &&
114 git add file &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200115 test_must_fail git commit -m "another"
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100116
117'
118
Junio C Hamano41ac4142008-02-01 01:50:53 -0800119test_expect_success 'with failing hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100120
121 echo "more another" >> file &&
122 git add file &&
123 echo "more another" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400124 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100125
126'
127
128test_expect_success '--no-verify with failing hook' '
129
130 echo "stuff" >> file &&
131 git add file &&
132 git commit --no-verify -m "stuff"
133
134'
135
Alex Riesenfa212962021-10-29 15:45:45 +0200136test_expect_success '-n followed by --verify with failing hook' '
137
138 echo "even more" >> file &&
139 git add file &&
140 test_must_fail git commit -n --verify -m "even more"
141
142'
143
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100144test_expect_success '--no-verify with failing hook (editor)' '
145
146 echo "more stuff" >> file &&
147 git add file &&
148 echo "more stuff" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400149 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100150
151'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100152
Stefan Bellerf8b86352017-09-07 15:04:29 -0700153test_expect_success 'merge fails with failing hook' '
154
155 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000156 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700157 git checkout --orphan newbranch &&
158 : >file2 &&
159 git add file2 &&
160 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000161 test_must_fail git merge --allow-unrelated-histories main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700162 commit_msg_is "in-side-branch" # HEAD before merge
163
164'
165
166test_expect_success 'merge bypasses failing hook with --no-verify' '
167
168 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000169 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700170 git checkout --orphan newbranch &&
Elijah Newreneddd1a42018-06-30 18:25:02 -0700171 git rm -f file &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700172 : >file2 &&
173 git add file2 &&
174 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000175 git merge --no-verify --allow-unrelated-histories main &&
176 commit_msg_is "Merge branch '\''main'\'' into newbranch"
Stefan Bellerf8b86352017-09-07 15:04:29 -0700177'
178
179
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100180chmod -x "$HOOK"
Johannes Sixtee9fb682009-03-13 22:55:27 +0100181test_expect_success POSIXPERM 'with non-executable hook' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100182
Elijah Newreneddd1a42018-06-30 18:25:02 -0700183 echo "content" >file &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100184 git add file &&
185 git commit -m "content"
186
187'
188
Johannes Sixtee9fb682009-03-13 22:55:27 +0100189test_expect_success POSIXPERM 'with non-executable hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100190
191 echo "content again" >> file &&
192 git add file &&
193 echo "content again" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400194 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100195
196'
197
Johannes Sixtee9fb682009-03-13 22:55:27 +0100198test_expect_success POSIXPERM '--no-verify with non-executable hook' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100199
200 echo "more content" >> file &&
201 git add file &&
202 git commit --no-verify -m "more content"
203
204'
205
Johannes Sixtee9fb682009-03-13 22:55:27 +0100206test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100207
208 echo "even more content" >> file &&
209 git add file &&
210 echo "even more content" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400211 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100212
213'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100214
215# now a hook that edits the commit message
216cat > "$HOOK" <<'EOF'
217#!/bin/sh
218echo "new message" > "$1"
219exit 0
220EOF
221chmod +x "$HOOK"
222
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100223test_expect_success 'hook edits commit message' '
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100224
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100225 echo "additional" >> file &&
226 git add file &&
227 git commit -m "additional" &&
228 commit_msg_is "new message"
229
230'
231
232test_expect_success 'hook edits commit message (editor)' '
233
234 echo "additional content" >> file &&
235 git add file &&
236 echo "additional content" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400237 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100238 commit_msg_is "new message"
239
240'
241
242test_expect_success "hook doesn't edit commit message" '
243
244 echo "plus" >> file &&
245 git add file &&
246 git commit --no-verify -m "plus" &&
247 commit_msg_is "plus"
248
249'
250
251test_expect_success "hook doesn't edit commit message (editor)" '
252
253 echo "more plus" >> file &&
254 git add file &&
255 echo "more plus" > FAKE_MSG &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400256 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100257 commit_msg_is "more plus"
Stefan Bellerf8b86352017-09-07 15:04:29 -0700258'
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100259
Stefan Bellerf8b86352017-09-07 15:04:29 -0700260test_expect_success 'hook called in git-merge picks up commit message' '
261 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000262 test_when_finished "git checkout -f main" &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700263 git checkout --orphan newbranch &&
Elijah Newreneddd1a42018-06-30 18:25:02 -0700264 git rm -f file &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700265 : >file2 &&
266 git add file2 &&
267 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000268 git merge --allow-unrelated-histories main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700269 commit_msg_is "new message"
270'
271
272test_expect_failure 'merge --continue remembers --no-verify' '
273 test_when_finished "git branch -D newbranch" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000274 test_when_finished "git checkout -f main" &&
275 git checkout main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700276 echo a >file2 &&
277 git add file2 &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000278 git commit --no-verify -m "add file2 to main" &&
279 git checkout -b newbranch main^ &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700280 echo b >file2 &&
281 git add file2 &&
282 git commit --no-verify file2 -m in-side-branch &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000283 git merge --no-verify -m not-rewritten-by-hook main &&
Stefan Bellerf8b86352017-09-07 15:04:29 -0700284 # resolve conflict:
285 echo c >file2 &&
286 git add file2 &&
287 git merge --continue &&
288 commit_msg_is not-rewritten-by-hook
Wincent Colaiuta80f86602007-12-10 08:33:26 +0100289'
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100290
Johannes Schindelincb7fb9e2017-03-22 16:01:19 +0100291# set up fake editor to replace `pick` by `reword`
292cat > reword-editor <<'EOF'
293#!/bin/sh
294mv "$1" "$1".bup &&
295sed 's/^pick/reword/' <"$1".bup >"$1"
296EOF
297chmod +x reword-editor
298REWORD_EDITOR="$(pwd)/reword-editor"
299export REWORD_EDITOR
300
Johannes Schindelinb92ff6e2017-03-23 17:07:17 +0100301test_expect_success 'hook is called for reword during `rebase -i`' '
Johannes Schindelincb7fb9e2017-03-22 16:01:19 +0100302
303 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
304 commit_msg_is "new message"
305
306'
307
Stefan Bellerf8b86352017-09-07 15:04:29 -0700308
Wincent Colaiuta264474f2007-12-08 13:29:47 +0100309test_done