3 test_description
='commit-msg hook'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success
'with no hook' '
18 # set up fake editor for interactive editing
19 cat > fake-editor
<<'EOF'
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"
31 test_expect_success
'with no hook (editor)' '
33 echo "more foo" >> file &&
35 echo "more foo" > FAKE_MSG &&
36 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
40 test_expect_success
'--no-verify with no hook' '
44 git commit --no-verify -m "bar"
48 test_expect_success
'--no-verify with no hook (editor)' '
50 echo "more bar" > file &&
52 echo "more bar" > FAKE_MSG &&
53 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
57 test_expect_success
'setup: commit-msg hook that always succeeds' '
58 test_hook --setup commit-msg <<-\EOF
63 test_expect_success
'with succeeding hook' '
65 echo "more" >> file &&
71 test_expect_success
'with succeeding hook (editor)' '
73 echo "more more" >> file &&
75 echo "more more" > FAKE_MSG &&
76 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
80 test_expect_success
'--no-verify with succeeding hook' '
82 echo "even more" >> file &&
84 git commit --no-verify -m "even more"
88 test_expect_success
'--no-verify with succeeding hook (editor)' '
90 echo "even more more" >> file &&
92 echo "even more more" > FAKE_MSG &&
93 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
97 test_expect_success
'setup: commit-msg hook that always fails' '
98 test_hook --clobber commit-msg <<-\EOF
104 test "$(git log --pretty=format:%s%b -1)" = "$1"
107 test_expect_success
'with failing hook' '
109 echo "another" >> file &&
111 test_must_fail git commit -m "another"
115 test_expect_success
'with failing hook (editor)' '
117 echo "more another" >> file &&
119 echo "more another" > FAKE_MSG &&
120 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
124 test_expect_success
'--no-verify with failing hook' '
126 echo "stuff" >> file &&
128 git commit --no-verify -m "stuff"
132 test_expect_success
'-n followed by --verify with failing hook' '
134 echo "even more" >> file &&
136 test_must_fail git commit -n --verify -m "even more"
140 test_expect_success
'--no-verify with failing hook (editor)' '
142 echo "more stuff" >> file &&
144 echo "more stuff" > FAKE_MSG &&
145 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
149 test_expect_success
'merge fails with failing hook' '
151 test_when_finished "git branch -D newbranch" &&
152 test_when_finished "git checkout -f main" &&
153 git checkout --orphan newbranch &&
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
162 test_expect_success
'merge bypasses failing hook with --no-verify' '
164 test_when_finished "git branch -D newbranch" &&
165 test_when_finished "git checkout -f main" &&
166 git checkout --orphan newbranch &&
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"
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"
181 test_expect_success POSIXPERM
'with non-executable hook' '
183 echo "content" >file &&
185 git commit -m "content"
189 test_expect_success POSIXPERM
'with non-executable hook (editor)' '
191 echo "content again" >> file &&
193 echo "content again" > FAKE_MSG &&
194 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
198 test_expect_success POSIXPERM
'--no-verify with non-executable hook' '
200 echo "more content" >> file &&
202 git commit --no-verify -m "more content"
206 test_expect_success POSIXPERM
'--no-verify with non-executable hook (editor)' '
208 echo "even more content" >> file &&
210 echo "even more content" > FAKE_MSG &&
211 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
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"
222 test_expect_success
'hook edits commit message' '
224 echo "additional" >> file &&
226 git commit -m "additional" &&
227 commit_msg_is "new message"
231 test_expect_success
'hook edits commit message (editor)' '
233 echo "additional content" >> file &&
235 echo "additional content" > FAKE_MSG &&
236 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
237 commit_msg_is "new message"
241 test_expect_success
"hook doesn't edit commit message" '
243 echo "plus" >> file &&
245 git commit --no-verify -m "plus" &&
250 test_expect_success
"hook doesn't edit commit message (editor)" '
252 echo "more plus" >> file &&
254 echo "more plus" > FAKE_MSG &&
255 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
256 commit_msg_is "more plus"
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 &&
266 git commit --no-verify file2 -m in-side-branch &&
267 git merge --allow-unrelated-histories main &&
268 commit_msg_is "new message"
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" &&
277 git commit --no-verify -m "add file2 to main" &&
278 git checkout -b newbranch main^ &&
281 git commit --no-verify file2 -m in-side-branch &&
282 git merge --no-verify -m not-rewritten-by-hook main &&
286 git merge --continue &&
287 commit_msg_is not-rewritten-by-hook
290 # set up fake editor to replace `pick` by `reword`
291 cat > reword-editor
<<'EOF'
294 sed 's/^pick/reword/' <"$1".bup >"$1"
296 chmod +x reword-editor
297 REWORD_EDITOR
="$(pwd)/reword-editor"
300 test_expect_success
'hook is called for reword during `rebase -i`' '
302 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
303 commit_msg_is "new message"