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