]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7504-commit-msg-hook.sh
Merge branch 'ab/reftable-aix-xlc-12'
[thirdparty/git.git] / t / t7504-commit-msg-hook.sh
1 #!/bin/sh
2
3 test_description='commit-msg hook'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success 'with no hook' '
11
12 echo "foo" > file &&
13 git add file &&
14 git commit -m "first"
15
16 '
17
18 # set up fake editor for interactive editing
19 cat > fake-editor <<'EOF'
20 #!/bin/sh
21 cp FAKE_MSG "$1"
22 exit 0
23 EOF
24 chmod +x fake-editor
25
26 ## Not using test_set_editor here so we can easily ensure the editor variable
27 ## is only set for the editor tests
28 FAKE_EDITOR="$(pwd)/fake-editor"
29 export FAKE_EDITOR
30
31 test_expect_success 'with no hook (editor)' '
32
33 echo "more foo" >> file &&
34 git add file &&
35 echo "more foo" > FAKE_MSG &&
36 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
37
38 '
39
40 test_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
48 test_expect_success '--no-verify with no hook (editor)' '
49
50 echo "more bar" > file &&
51 git add file &&
52 echo "more bar" > FAKE_MSG &&
53 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
54
55 '
56
57 test_expect_success 'setup: commit-msg hook that always succeeds' '
58 test_hook --setup commit-msg <<-\EOF
59 exit 0
60 EOF
61 '
62
63 test_expect_success 'with succeeding hook' '
64
65 echo "more" >> file &&
66 git add file &&
67 git commit -m "more"
68
69 '
70
71 test_expect_success 'with succeeding hook (editor)' '
72
73 echo "more more" >> file &&
74 git add file &&
75 echo "more more" > FAKE_MSG &&
76 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
77
78 '
79
80 test_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
88 test_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 &&
93 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
94
95 '
96
97 test_expect_success 'setup: commit-msg hook that always fails' '
98 test_hook --clobber commit-msg <<-\EOF
99 exit 1
100 EOF
101 '
102
103 commit_msg_is () {
104 test "$(git log --pretty=format:%s%b -1)" = "$1"
105 }
106
107 test_expect_success 'with failing hook' '
108
109 echo "another" >> file &&
110 git add file &&
111 test_must_fail git commit -m "another"
112
113 '
114
115 test_expect_success 'with failing hook (editor)' '
116
117 echo "more another" >> file &&
118 git add file &&
119 echo "more another" > FAKE_MSG &&
120 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
121
122 '
123
124 test_expect_success '--no-verify with failing hook' '
125
126 echo "stuff" >> file &&
127 git add file &&
128 git commit --no-verify -m "stuff"
129
130 '
131
132 test_expect_success '-n followed by --verify with failing hook' '
133
134 echo "even more" >> file &&
135 git add file &&
136 test_must_fail git commit -n --verify -m "even more"
137
138 '
139
140 test_expect_success '--no-verify with failing hook (editor)' '
141
142 echo "more stuff" >> file &&
143 git add file &&
144 echo "more stuff" > FAKE_MSG &&
145 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
146
147 '
148
149 test_expect_success 'merge fails with failing hook' '
150
151 test_when_finished "git branch -D newbranch" &&
152 test_when_finished "git checkout -f main" &&
153 git checkout --orphan newbranch &&
154 : >file2 &&
155 git add file2 &&
156 git commit --no-verify file2 -m in-side-branch &&
157 test_must_fail git merge --allow-unrelated-histories main &&
158 commit_msg_is "in-side-branch" # HEAD before merge
159
160 '
161
162 test_expect_success 'merge bypasses failing hook with --no-verify' '
163
164 test_when_finished "git branch -D newbranch" &&
165 test_when_finished "git checkout -f main" &&
166 git checkout --orphan newbranch &&
167 git rm -f file &&
168 : >file2 &&
169 git add file2 &&
170 git commit --no-verify file2 -m in-side-branch &&
171 git merge --no-verify --allow-unrelated-histories main &&
172 commit_msg_is "Merge branch '\''main'\'' into newbranch"
173 '
174
175 test_expect_success 'setup: commit-msg hook made non-executable' '
176 git_dir="$(git rev-parse --git-dir)" &&
177 chmod -x "$git_dir/hooks/commit-msg"
178 '
179
180
181 test_expect_success POSIXPERM 'with non-executable hook' '
182
183 echo "content" >file &&
184 git add file &&
185 git commit -m "content"
186
187 '
188
189 test_expect_success POSIXPERM 'with non-executable hook (editor)' '
190
191 echo "content again" >> file &&
192 git add file &&
193 echo "content again" > FAKE_MSG &&
194 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
195
196 '
197
198 test_expect_success POSIXPERM '--no-verify with non-executable hook' '
199
200 echo "more content" >> file &&
201 git add file &&
202 git commit --no-verify -m "more content"
203
204 '
205
206 test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
207
208 echo "even more content" >> file &&
209 git add file &&
210 echo "even more content" > FAKE_MSG &&
211 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
212
213 '
214
215 test_expect_success 'setup: commit-msg hook that edits the commit message' '
216 test_hook --clobber commit-msg <<-\EOF
217 echo "new message" >"$1"
218 exit 0
219 EOF
220 '
221
222 test_expect_success 'hook edits commit message' '
223
224 echo "additional" >> file &&
225 git add file &&
226 git commit -m "additional" &&
227 commit_msg_is "new message"
228
229 '
230
231 test_expect_success 'hook edits commit message (editor)' '
232
233 echo "additional content" >> file &&
234 git add file &&
235 echo "additional content" > FAKE_MSG &&
236 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
237 commit_msg_is "new message"
238
239 '
240
241 test_expect_success "hook doesn't edit commit message" '
242
243 echo "plus" >> file &&
244 git add file &&
245 git commit --no-verify -m "plus" &&
246 commit_msg_is "plus"
247
248 '
249
250 test_expect_success "hook doesn't edit commit message (editor)" '
251
252 echo "more plus" >> file &&
253 git add file &&
254 echo "more plus" > FAKE_MSG &&
255 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
256 commit_msg_is "more plus"
257 '
258
259 test_expect_success 'hook called in git-merge picks up commit message' '
260 test_when_finished "git branch -D newbranch" &&
261 test_when_finished "git checkout -f main" &&
262 git checkout --orphan newbranch &&
263 git rm -f file &&
264 : >file2 &&
265 git add file2 &&
266 git commit --no-verify file2 -m in-side-branch &&
267 git merge --allow-unrelated-histories main &&
268 commit_msg_is "new message"
269 '
270
271 test_expect_failure 'merge --continue remembers --no-verify' '
272 test_when_finished "git branch -D newbranch" &&
273 test_when_finished "git checkout -f main" &&
274 git checkout main &&
275 echo a >file2 &&
276 git add file2 &&
277 git commit --no-verify -m "add file2 to main" &&
278 git checkout -b newbranch main^ &&
279 echo b >file2 &&
280 git add file2 &&
281 git commit --no-verify file2 -m in-side-branch &&
282 git merge --no-verify -m not-rewritten-by-hook main &&
283 # resolve conflict:
284 echo c >file2 &&
285 git add file2 &&
286 git merge --continue &&
287 commit_msg_is not-rewritten-by-hook
288 '
289
290 # set up fake editor to replace `pick` by `reword`
291 cat > reword-editor <<'EOF'
292 #!/bin/sh
293 mv "$1" "$1".bup &&
294 sed 's/^pick/reword/' <"$1".bup >"$1"
295 EOF
296 chmod +x reword-editor
297 REWORD_EDITOR="$(pwd)/reword-editor"
298 export REWORD_EDITOR
299
300 test_expect_success 'hook is called for reword during `rebase -i`' '
301
302 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
303 commit_msg_is "new message"
304
305 '
306
307
308 test_done