3 test_description='commit-msg hook'
7 test_expect_success 'with no hook' '
15 # set up fake editor for interactive editing
16 cat > fake-editor <<'EOF'
23 ## Not using test_set_editor here so we can easily ensure the editor variable
24 ## is only set for the editor tests
25 FAKE_EDITOR="$(pwd)/fake-editor"
28 test_expect_success 'with no hook (editor)' '
30 echo "more foo" >> file &&
32 echo "more foo" > FAKE_MSG &&
33 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
37 test_expect_success '--no-verify with no hook' '
41 git commit --no-verify -m "bar"
45 test_expect_success '--no-verify with no hook (editor)' '
47 echo "more bar" > file &&
49 echo "more bar" > FAKE_MSG &&
50 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
54 # now install hook that always succeeds
55 HOOKDIR="$(git rev-parse --git-dir)/hooks"
56 HOOK="$HOOKDIR/commit-msg"
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 # now a hook that fails
105 test "$(git log --pretty=format:%s%b -1)" = "$1"
108 test_expect_success 'with failing hook' '
110 echo "another" >> file &&
112 test_must_fail git commit -m "another"
116 test_expect_success 'with failing hook (editor)' '
118 echo "more another" >> 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 &&
129 git commit --no-verify -m "stuff"
133 test_expect_success '--no-verify with failing hook (editor)' '
135 echo "more stuff" >> file &&
137 echo "more stuff" > FAKE_MSG &&
138 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
142 test_expect_success 'merge fails with failing hook' '
144 test_when_finished "git branch -D newbranch" &&
145 test_when_finished "git checkout -f master" &&
146 git checkout --orphan newbranch &&
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
155 test_expect_success 'merge bypasses failing hook with --no-verify' '
157 test_when_finished "git branch -D newbranch" &&
158 test_when_finished "git checkout -f master" &&
159 git checkout --orphan newbranch &&
162 git commit --no-verify file2 -m in-side-branch &&
163 git merge --no-verify --allow-unrelated-histories master &&
164 commit_msg_is "Merge branch '\''master'\'' into newbranch"
169 test_expect_success POSIXPERM 'with non-executable hook' '
171 echo "content" >> file &&
173 git commit -m "content"
177 test_expect_success POSIXPERM 'with non-executable hook (editor)' '
179 echo "content again" >> file &&
181 echo "content again" > FAKE_MSG &&
182 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
186 test_expect_success POSIXPERM '--no-verify with non-executable hook' '
188 echo "more content" >> file &&
190 git commit --no-verify -m "more content"
194 test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
196 echo "even more content" >> file &&
198 echo "even more content" > FAKE_MSG &&
199 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
203 # now a hook that edits the commit message
204 cat > "$HOOK" <<'EOF'
206 echo "new message" > "$1"
211 test_expect_success 'hook edits commit message' '
213 echo "additional" >> file &&
215 git commit -m "additional" &&
216 commit_msg_is "new message"
220 test_expect_success 'hook edits commit message (editor)' '
222 echo "additional content" >> file &&
224 echo "additional content" > FAKE_MSG &&
225 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
226 commit_msg_is "new message"
230 test_expect_success "hook doesn't edit commit message" '
232 echo "plus" >> file &&
234 git commit --no-verify -m "plus" &&
239 test_expect_success "hook doesn't edit commit message (editor)" '
241 echo "more plus" >> file &&
243 echo "more plus" > FAKE_MSG &&
244 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
245 commit_msg_is "more plus"
248 test_expect_success 'hook called in git-merge picks up commit message' '
249 test_when_finished "git branch -D newbranch" &&
250 test_when_finished "git checkout -f master" &&
251 git checkout --orphan newbranch &&
254 git commit --no-verify file2 -m in-side-branch &&
255 git merge --allow-unrelated-histories master &&
256 commit_msg_is "new message"
259 test_expect_failure 'merge --continue remembers --no-verify' '
260 test_when_finished "git branch -D newbranch" &&
261 test_when_finished "git checkout -f master" &&
262 git checkout master &&
265 git commit --no-verify -m "add file2 to master" &&
266 git checkout -b newbranch master^ &&
269 git commit --no-verify file2 -m in-side-branch &&
270 git merge --no-verify -m not-rewritten-by-hook master &&
274 git merge --continue &&
275 commit_msg_is not-rewritten-by-hook
278 # set up fake editor to replace `pick` by `reword`
279 cat > reword-editor <<'EOF'
282 sed 's/^pick/reword/' <"$1".bup >"$1"
284 chmod +x reword-editor
285 REWORD_EDITOR="$(pwd)/reword-editor"
288 test_expect_success 'hook is called for reword during `rebase -i`' '
290 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
291 commit_msg_is "new message"