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 # now install hook that always succeeds
58 HOOKDIR
="$(git rev-parse --git-dir)/hooks"
59 HOOK
="$HOOKDIR/commit-msg"
67 test_expect_success
'with succeeding hook' '
69 echo "more" >> file &&
75 test_expect_success
'with succeeding hook (editor)' '
77 echo "more more" >> file &&
79 echo "more more" > FAKE_MSG &&
80 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
84 test_expect_success
'--no-verify with succeeding hook' '
86 echo "even more" >> file &&
88 git commit --no-verify -m "even more"
92 test_expect_success
'--no-verify with succeeding hook (editor)' '
94 echo "even more more" >> file &&
96 echo "even more more" > FAKE_MSG &&
97 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
101 # now a hook that fails
108 test "$(git log --pretty=format:%s%b -1)" = "$1"
111 test_expect_success
'with failing hook' '
113 echo "another" >> file &&
115 test_must_fail git commit -m "another"
119 test_expect_success
'with failing hook (editor)' '
121 echo "more another" >> file &&
123 echo "more another" > FAKE_MSG &&
124 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
128 test_expect_success
'--no-verify with failing hook' '
130 echo "stuff" >> file &&
132 git commit --no-verify -m "stuff"
136 test_expect_success
'--no-verify with failing hook (editor)' '
138 echo "more stuff" >> file &&
140 echo "more stuff" > FAKE_MSG &&
141 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
145 test_expect_success
'merge fails with failing hook' '
147 test_when_finished "git branch -D newbranch" &&
148 test_when_finished "git checkout -f main" &&
149 git checkout --orphan newbranch &&
152 git commit --no-verify file2 -m in-side-branch &&
153 test_must_fail git merge --allow-unrelated-histories main &&
154 commit_msg_is "in-side-branch" # HEAD before merge
158 test_expect_success
'merge bypasses failing hook with --no-verify' '
160 test_when_finished "git branch -D newbranch" &&
161 test_when_finished "git checkout -f main" &&
162 git checkout --orphan newbranch &&
166 git commit --no-verify file2 -m in-side-branch &&
167 git merge --no-verify --allow-unrelated-histories main &&
168 commit_msg_is "Merge branch '\''main'\'' into newbranch"
173 test_expect_success POSIXPERM
'with non-executable hook' '
175 echo "content" >file &&
177 git commit -m "content"
181 test_expect_success POSIXPERM
'with non-executable hook (editor)' '
183 echo "content again" >> file &&
185 echo "content again" > FAKE_MSG &&
186 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
190 test_expect_success POSIXPERM
'--no-verify with non-executable hook' '
192 echo "more content" >> file &&
194 git commit --no-verify -m "more content"
198 test_expect_success POSIXPERM
'--no-verify with non-executable hook (editor)' '
200 echo "even more content" >> file &&
202 echo "even more content" > FAKE_MSG &&
203 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
207 # now a hook that edits the commit message
208 cat > "$HOOK" <<'EOF'
210 echo "new message" > "$1"
215 test_expect_success
'hook edits commit message' '
217 echo "additional" >> file &&
219 git commit -m "additional" &&
220 commit_msg_is "new message"
224 test_expect_success
'hook edits commit message (editor)' '
226 echo "additional content" >> file &&
228 echo "additional content" > FAKE_MSG &&
229 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
230 commit_msg_is "new message"
234 test_expect_success
"hook doesn't edit commit message" '
236 echo "plus" >> file &&
238 git commit --no-verify -m "plus" &&
243 test_expect_success
"hook doesn't edit commit message (editor)" '
245 echo "more plus" >> file &&
247 echo "more plus" > FAKE_MSG &&
248 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
249 commit_msg_is "more plus"
252 test_expect_success
'hook called in git-merge picks up commit message' '
253 test_when_finished "git branch -D newbranch" &&
254 test_when_finished "git checkout -f main" &&
255 git checkout --orphan newbranch &&
259 git commit --no-verify file2 -m in-side-branch &&
260 git merge --allow-unrelated-histories main &&
261 commit_msg_is "new message"
264 test_expect_failure
'merge --continue remembers --no-verify' '
265 test_when_finished "git branch -D newbranch" &&
266 test_when_finished "git checkout -f main" &&
270 git commit --no-verify -m "add file2 to main" &&
271 git checkout -b newbranch main^ &&
274 git commit --no-verify file2 -m in-side-branch &&
275 git merge --no-verify -m not-rewritten-by-hook main &&
279 git merge --continue &&
280 commit_msg_is not-rewritten-by-hook
283 # set up fake editor to replace `pick` by `reword`
284 cat > reword-editor
<<'EOF'
287 sed 's/^pick/reword/' <"$1".bup >"$1"
289 chmod +x reword-editor
290 REWORD_EDITOR
="$(pwd)/reword-editor"
293 test_expect_success
'hook is called for reword during `rebase -i`' '
295 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
296 commit_msg_is "new message"