clone_submodule: avoid using `access()` on directories
[git.git] / t / t7504-commit-msg-hook.sh
bloba39de8c112674f070f8dc19745e27980b7b52c9c
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-lib.sh
10 test_expect_success 'with no hook' '
12 echo "foo" > file &&
13 git add file &&
14 git commit -m "first"
18 # set up fake editor for interactive editing
19 cat > fake-editor <<'EOF'
20 #!/bin/sh
21 cp FAKE_MSG "$1"
22 exit 0
23 EOF
24 chmod +x fake-editor
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"
29 export FAKE_EDITOR
31 test_expect_success 'with no hook (editor)' '
33 echo "more foo" >> file &&
34 git add file &&
35 echo "more foo" > FAKE_MSG &&
36 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
40 test_expect_success '--no-verify with no hook' '
42 echo "bar" > file &&
43 git add file &&
44 git commit --no-verify -m "bar"
48 test_expect_success '--no-verify with no hook (editor)' '
50 echo "more bar" > file &&
51 git add 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
59 exit 0
60 EOF
63 test_expect_success 'with succeeding hook' '
65 echo "more" >> file &&
66 git add file &&
67 git commit -m "more"
71 test_expect_success 'with succeeding hook (editor)' '
73 echo "more more" >> file &&
74 git add 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 &&
83 git add 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 &&
91 git add 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
99 exit 1
103 commit_msg_is () {
104 test "$(git log --pretty=format:%s%b -1)" = "$1"
107 test_expect_success 'with failing hook' '
109 echo "another" >> file &&
110 git add file &&
111 test_must_fail git commit -m "another"
115 test_expect_success 'with failing hook (editor)' '
117 echo "more another" >> file &&
118 git add 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 &&
127 git add file &&
128 git commit --no-verify -m "stuff"
132 test_expect_success '-n followed by --verify with failing hook' '
134 echo "even more" >> file &&
135 git add 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 &&
143 git add 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 &&
154 : >file2 &&
155 git add file2 &&
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 &&
167 git rm -f file &&
168 : >file2 &&
169 git add file2 &&
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 &&
184 git add file &&
185 git commit -m "content"
189 test_expect_success POSIXPERM 'with non-executable hook (editor)' '
191 echo "content again" >> file &&
192 git add 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 &&
201 git add 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 &&
209 git add 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"
218 exit 0
222 test_expect_success 'hook edits commit message' '
224 echo "additional" >> file &&
225 git add 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 &&
234 git add 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 &&
244 git add file &&
245 git commit --no-verify -m "plus" &&
246 commit_msg_is "plus"
250 test_expect_success "hook doesn't edit commit message (editor)" '
252 echo "more plus" >> file &&
253 git add 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 &&
263 git rm -f file &&
264 : >file2 &&
265 git add file2 &&
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" &&
274 git checkout main &&
275 echo a >file2 &&
276 git add file2 &&
277 git commit --no-verify -m "add file2 to main" &&
278 git checkout -b newbranch main^ &&
279 echo b >file2 &&
280 git add file2 &&
281 git commit --no-verify file2 -m in-side-branch &&
282 git merge --no-verify -m not-rewritten-by-hook main &&
283 # resolve conflict:
284 echo c >file2 &&
285 git add file2 &&
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'
292 #!/bin/sh
293 mv "$1" "$1".bup &&
294 sed 's/^pick/reword/' <"$1".bup >"$1"
296 chmod +x reword-editor
297 REWORD_EDITOR="$(pwd)/reword-editor"
298 export 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"
308 test_done