treewide: remove unnecessary inclusions of parse-options.h from headers
[git/debian.git] / t / t7504-commit-msg-hook.sh
blob07ca46fb0d539c62c7dcbe066b8c95e550d0b3a3
1 #!/bin/sh
3 test_description='commit-msg hook'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'with no hook' '
13 echo "foo" > file &&
14 git add file &&
15 git commit -m "first"
19 # set up fake editor for interactive editing
20 cat > fake-editor <<'EOF'
21 #!/bin/sh
22 cp FAKE_MSG "$1"
23 exit 0
24 EOF
25 chmod +x fake-editor
27 ## Not using test_set_editor here so we can easily ensure the editor variable
28 ## is only set for the editor tests
29 FAKE_EDITOR="$(pwd)/fake-editor"
30 export FAKE_EDITOR
32 test_expect_success 'with no hook (editor)' '
34 echo "more foo" >> file &&
35 git add file &&
36 echo "more foo" > FAKE_MSG &&
37 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
41 test_expect_success '--no-verify with no hook' '
43 echo "bar" > file &&
44 git add file &&
45 git commit --no-verify -m "bar"
49 test_expect_success '--no-verify with no hook (editor)' '
51 echo "more bar" > file &&
52 git add file &&
53 echo "more bar" > FAKE_MSG &&
54 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
58 test_expect_success 'setup: commit-msg hook that always succeeds' '
59 test_hook --setup commit-msg <<-\EOF
60 exit 0
61 EOF
64 test_expect_success 'with succeeding hook' '
66 echo "more" >> file &&
67 git add file &&
68 git commit -m "more"
72 test_expect_success 'with succeeding hook (editor)' '
74 echo "more more" >> file &&
75 git add file &&
76 echo "more more" > FAKE_MSG &&
77 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
81 test_expect_success '--no-verify with succeeding hook' '
83 echo "even more" >> file &&
84 git add file &&
85 git commit --no-verify -m "even more"
89 test_expect_success '--no-verify with succeeding hook (editor)' '
91 echo "even more more" >> file &&
92 git add file &&
93 echo "even more more" > FAKE_MSG &&
94 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
98 test_expect_success 'setup: commit-msg hook that always fails' '
99 test_hook --clobber commit-msg <<-\EOF
100 exit 1
104 commit_msg_is () {
105 test "$(git log --pretty=format:%s%b -1)" = "$1"
108 test_expect_success 'with failing hook' '
110 echo "another" >> file &&
111 git add file &&
112 test_must_fail git commit -m "another"
116 test_expect_success 'with failing hook (editor)' '
118 echo "more another" >> file &&
119 git add file &&
120 echo "more another" > FAKE_MSG &&
121 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
125 test_expect_success '--no-verify with failing hook' '
127 echo "stuff" >> file &&
128 git add file &&
129 git commit --no-verify -m "stuff"
133 test_expect_success '-n followed by --verify with failing hook' '
135 echo "even more" >> file &&
136 git add file &&
137 test_must_fail git commit -n --verify -m "even more"
141 test_expect_success '--no-verify with failing hook (editor)' '
143 echo "more stuff" >> file &&
144 git add file &&
145 echo "more stuff" > FAKE_MSG &&
146 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
150 test_expect_success 'merge fails with failing hook' '
152 test_when_finished "git branch -D newbranch" &&
153 test_when_finished "git checkout -f main" &&
154 git checkout --orphan newbranch &&
155 : >file2 &&
156 git add file2 &&
157 git commit --no-verify file2 -m in-side-branch &&
158 test_must_fail git merge --allow-unrelated-histories main &&
159 commit_msg_is "in-side-branch" # HEAD before merge
163 test_expect_success 'merge bypasses failing hook with --no-verify' '
165 test_when_finished "git branch -D newbranch" &&
166 test_when_finished "git checkout -f main" &&
167 git checkout --orphan newbranch &&
168 git rm -f file &&
169 : >file2 &&
170 git add file2 &&
171 git commit --no-verify file2 -m in-side-branch &&
172 git merge --no-verify --allow-unrelated-histories main &&
173 commit_msg_is "Merge branch '\''main'\'' into newbranch"
176 test_expect_success 'setup: commit-msg hook made non-executable' '
177 git_dir="$(git rev-parse --git-dir)" &&
178 chmod -x "$git_dir/hooks/commit-msg"
182 test_expect_success POSIXPERM 'with non-executable hook' '
184 echo "content" >file &&
185 git add file &&
186 git commit -m "content"
190 test_expect_success POSIXPERM 'with non-executable hook (editor)' '
192 echo "content again" >> file &&
193 git add file &&
194 echo "content again" > FAKE_MSG &&
195 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
199 test_expect_success POSIXPERM '--no-verify with non-executable hook' '
201 echo "more content" >> file &&
202 git add file &&
203 git commit --no-verify -m "more content"
207 test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
209 echo "even more content" >> file &&
210 git add file &&
211 echo "even more content" > FAKE_MSG &&
212 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
216 test_expect_success 'setup: commit-msg hook that edits the commit message' '
217 test_hook --clobber commit-msg <<-\EOF
218 echo "new message" >"$1"
219 exit 0
223 test_expect_success 'hook edits commit message' '
225 echo "additional" >> file &&
226 git add file &&
227 git commit -m "additional" &&
228 commit_msg_is "new message"
232 test_expect_success 'hook edits commit message (editor)' '
234 echo "additional content" >> file &&
235 git add file &&
236 echo "additional content" > FAKE_MSG &&
237 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
238 commit_msg_is "new message"
242 test_expect_success "hook doesn't edit commit message" '
244 echo "plus" >> file &&
245 git add file &&
246 git commit --no-verify -m "plus" &&
247 commit_msg_is "plus"
251 test_expect_success "hook doesn't edit commit message (editor)" '
253 echo "more plus" >> file &&
254 git add file &&
255 echo "more plus" > FAKE_MSG &&
256 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
257 commit_msg_is "more plus"
260 test_expect_success 'hook called in git-merge picks up commit message' '
261 test_when_finished "git branch -D newbranch" &&
262 test_when_finished "git checkout -f main" &&
263 git checkout --orphan newbranch &&
264 git rm -f file &&
265 : >file2 &&
266 git add file2 &&
267 git commit --no-verify file2 -m in-side-branch &&
268 git merge --allow-unrelated-histories main &&
269 commit_msg_is "new message"
272 test_expect_failure 'merge --continue remembers --no-verify' '
273 test_when_finished "git branch -D newbranch" &&
274 test_when_finished "git checkout -f main" &&
275 git checkout main &&
276 echo a >file2 &&
277 git add file2 &&
278 git commit --no-verify -m "add file2 to main" &&
279 git checkout -b newbranch main^ &&
280 echo b >file2 &&
281 git add file2 &&
282 git commit --no-verify file2 -m in-side-branch &&
283 git merge --no-verify -m not-rewritten-by-hook main &&
284 # resolve conflict:
285 echo c >file2 &&
286 git add file2 &&
287 git merge --continue &&
288 commit_msg_is not-rewritten-by-hook
291 # set up fake editor to replace `pick` by `reword`
292 cat > reword-editor <<'EOF'
293 #!/bin/sh
294 mv "$1" "$1".bup &&
295 sed 's/^pick/reword/' <"$1".bup >"$1"
297 chmod +x reword-editor
298 REWORD_EDITOR="$(pwd)/reword-editor"
299 export REWORD_EDITOR
301 test_expect_success 'hook is called for reword during `rebase -i`' '
303 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
304 commit_msg_is "new message"
309 test_done