commit-reach(get_octopus_merge_bases): pass on "missing commits" errors
[git.git] / t / t7504-commit-msg-hook.sh
blobd1255228d5ffaa2b7b7333000e3e39468ee94bb1
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_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 test_expect_success 'with no hook' '
13 echo "foo" > file &&
14 git add file &&
15 git commit -m "first"
19 # set up fake editor for interactive editing
20 cat > fake-editor <<'EOF'
21 #!/bin/sh
22 cp FAKE_MSG "$1"
23 exit 0
24 EOF
25 chmod +x fake-editor
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"
30 export FAKE_EDITOR
32 test_expect_success 'with no hook (editor)' '
34 echo "more foo" >> file &&
35 git add file &&
36 echo "more foo" > FAKE_MSG &&
37 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
41 test_expect_success '--no-verify with no hook' '
43 echo "bar" > file &&
44 git add file &&
45 git commit --no-verify -m "bar"
49 test_expect_success '--no-verify with no hook (editor)' '
51 echo "more bar" > file &&
52 git add 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
60 exit 0
61 EOF
64 test_expect_success 'with succeeding hook' '
66 echo "more" >> file &&
67 git add file &&
68 git commit -m "more"
72 test_expect_success 'with succeeding hook (editor)' '
74 echo "more more" >> file &&
75 git add 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 &&
84 git add 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 &&
92 git add 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
100 exit 1
104 commit_msg_is () {
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 &&
113 git add file &&
114 test_must_fail git commit -m "another"
118 test_expect_success 'with failing hook (editor)' '
120 echo "more another" >> file &&
121 git add 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 &&
130 git add file &&
131 git commit --no-verify -m "stuff"
135 test_expect_success '-n followed by --verify with failing hook' '
137 echo "even more" >> file &&
138 git add 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 &&
146 git add 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 &&
157 : >file2 &&
158 git add file2 &&
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 &&
170 git rm -f file &&
171 : >file2 &&
172 git add file2 &&
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 &&
187 git add file &&
188 git commit -m "content"
192 test_expect_success POSIXPERM 'with non-executable hook (editor)' '
194 echo "content again" >> file &&
195 git add 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 &&
204 git add 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 &&
212 git add 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"
221 exit 0
225 test_expect_success 'hook edits commit message' '
227 echo "additional" >> file &&
228 git add 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 &&
237 git add 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 &&
247 git add file &&
248 git commit --no-verify -m "plus" &&
249 commit_msg_is "plus"
253 test_expect_success "hook doesn't edit commit message (editor)" '
255 echo "more plus" >> file &&
256 git add 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 &&
266 git rm -f file &&
267 : >file2 &&
268 git add file2 &&
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" &&
277 git checkout main &&
278 echo a >file2 &&
279 git add file2 &&
280 git commit --no-verify -m "add file2 to main" &&
281 git checkout -b newbranch main^ &&
282 echo b >file2 &&
283 git add file2 &&
284 git commit --no-verify file2 -m in-side-branch &&
285 git merge --no-verify -m not-rewritten-by-hook main &&
286 # resolve conflict:
287 echo c >file2 &&
288 git add file2 &&
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'
295 #!/bin/sh
296 mv "$1" "$1".bup &&
297 sed 's/^pick/reword/' <"$1".bup >"$1"
299 chmod +x reword-editor
300 REWORD_EDITOR="$(pwd)/reword-editor"
301 export 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"
311 test_done