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
11 test_expect_success
'with no hook' '
19 # set up fake editor for interactive editing
20 cat > fake-editor
<<'EOF'
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"
32 test_expect_success
'with no hook (editor)' '
34 echo "more foo" >> file &&
36 echo "more foo" > FAKE_MSG &&
37 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
41 test_expect_success
'--no-verify with no hook' '
45 git commit --no-verify -m "bar"
49 test_expect_success
'--no-verify with no hook (editor)' '
51 echo "more bar" > 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
64 test_expect_success
'with succeeding hook' '
66 echo "more" >> file &&
72 test_expect_success
'with succeeding hook (editor)' '
74 echo "more more" >> 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 &&
85 git commit --no-verify -m "even more"
89 test_expect_success
'--no-verify with succeeding hook (editor)' '
91 echo "even more more" >> 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
105 printf "%s" "$1" >expect
&&
106 git log
--pretty=format
:%s
%b
-1 >actual
&&
107 test_cmp expect actual
110 test_expect_success
'with failing hook' '
112 echo "another" >> file &&
114 test_must_fail git commit -m "another"
118 test_expect_success
'with failing hook (editor)' '
120 echo "more another" >> file &&
122 echo "more another" > FAKE_MSG &&
123 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
127 test_expect_success
'--no-verify with failing hook' '
129 echo "stuff" >> file &&
131 git commit --no-verify -m "stuff"
135 test_expect_success
'-n followed by --verify with failing hook' '
137 echo "even more" >> file &&
139 test_must_fail git commit -n --verify -m "even more"
143 test_expect_success
'--no-verify with failing hook (editor)' '
145 echo "more stuff" >> file &&
147 echo "more stuff" > FAKE_MSG &&
148 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
152 test_expect_success
'merge fails with failing hook' '
154 test_when_finished "git branch -D newbranch" &&
155 test_when_finished "git checkout -f main" &&
156 git checkout --orphan newbranch &&
159 git commit --no-verify file2 -m in-side-branch &&
160 test_must_fail git merge --allow-unrelated-histories main &&
161 commit_msg_is "in-side-branch" # HEAD before merge
165 test_expect_success
'merge bypasses failing hook with --no-verify' '
167 test_when_finished "git branch -D newbranch" &&
168 test_when_finished "git checkout -f main" &&
169 git checkout --orphan newbranch &&
173 git commit --no-verify file2 -m in-side-branch &&
174 git merge --no-verify --allow-unrelated-histories main &&
175 commit_msg_is "Merge branch '\''main'\'' into newbranch"
178 test_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"
184 test_expect_success POSIXPERM
'with non-executable hook' '
186 echo "content" >file &&
188 git commit -m "content"
192 test_expect_success POSIXPERM
'with non-executable hook (editor)' '
194 echo "content again" >> file &&
196 echo "content again" > FAKE_MSG &&
197 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
201 test_expect_success POSIXPERM
'--no-verify with non-executable hook' '
203 echo "more content" >> file &&
205 git commit --no-verify -m "more content"
209 test_expect_success POSIXPERM
'--no-verify with non-executable hook (editor)' '
211 echo "even more content" >> file &&
213 echo "even more content" > FAKE_MSG &&
214 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
218 test_expect_success
'setup: commit-msg hook that edits the commit message' '
219 test_hook --clobber commit-msg <<-\EOF
220 echo "new message" >"$1"
225 test_expect_success
'hook edits commit message' '
227 echo "additional" >> file &&
229 git commit -m "additional" &&
230 commit_msg_is "new message"
234 test_expect_success
'hook edits commit message (editor)' '
236 echo "additional content" >> file &&
238 echo "additional content" > FAKE_MSG &&
239 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
240 commit_msg_is "new message"
244 test_expect_success
"hook doesn't edit commit message" '
246 echo "plus" >> file &&
248 git commit --no-verify -m "plus" &&
253 test_expect_success
"hook doesn't edit commit message (editor)" '
255 echo "more plus" >> file &&
257 echo "more plus" > FAKE_MSG &&
258 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
259 commit_msg_is "more plus"
262 test_expect_success
'hook called in git-merge picks up commit message' '
263 test_when_finished "git branch -D newbranch" &&
264 test_when_finished "git checkout -f main" &&
265 git checkout --orphan newbranch &&
269 git commit --no-verify file2 -m in-side-branch &&
270 git merge --allow-unrelated-histories main &&
271 commit_msg_is "new message"
274 test_expect_failure
'merge --continue remembers --no-verify' '
275 test_when_finished "git branch -D newbranch" &&
276 test_when_finished "git checkout -f main" &&
280 git commit --no-verify -m "add file2 to main" &&
281 git checkout -b newbranch main^ &&
284 git commit --no-verify file2 -m in-side-branch &&
285 git merge --no-verify -m not-rewritten-by-hook main &&
289 git merge --continue &&
290 commit_msg_is not-rewritten-by-hook
293 # set up fake editor to replace `pick` by `reword`
294 cat > reword-editor
<<'EOF'
297 sed 's/^pick/reword/' <"$1".bup >"$1"
299 chmod +x reword-editor
300 REWORD_EDITOR
="$(pwd)/reword-editor"
303 test_expect_success
'hook is called for reword during `rebase -i`' '
305 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
306 commit_msg_is "new message"