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