3 test_description
='prepare-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
'set up commits for rebasing' '
15 git checkout -b rebase-me root &&
16 test_commit rebase-a a aa &&
17 test_commit rebase-b b bb &&
18 for i in $(test_seq 1 13)
20 test_commit rebase-$i c $i || return 1
24 cat >rebase-todo <<-EOF
25 pick $(git rev-parse rebase-a)
26 pick $(git rev-parse rebase-b)
27 fixup $(git rev-parse rebase-1)
28 fixup $(git rev-parse rebase-2)
29 pick $(git rev-parse rebase-3)
30 fixup $(git rev-parse rebase-4)
31 squash $(git rev-parse rebase-5)
32 reword $(git rev-parse rebase-6)
33 squash $(git rev-parse rebase-7)
34 fixup $(git rev-parse rebase-8)
35 fixup $(git rev-parse rebase-9)
36 edit $(git rev-parse rebase-10)
37 squash $(git rev-parse rebase-11)
38 squash $(git rev-parse rebase-12)
39 edit $(git rev-parse rebase-13)
43 test_expect_success
'with no hook' '
51 test_expect_success
'setup fake editor for interactive editing' '
52 write_script fake-editor <<-\EOF &&
56 ## Not using test_set_editor here so we can easily ensure the editor variable
57 ## is only set for the editor tests
58 FAKE_EDITOR="$(pwd)/fake-editor" &&
62 test_expect_success
'setup prepare-commit-msg hook' '
63 test_hook --setup prepare-commit-msg <<\EOF
64 GIT_DIR=$(git rev-parse --git-dir)
65 if test -d "$GIT_DIR/rebase-merge"
73 tail -n1 "$GIT_DIR/rebase-merge/done" | {
75 git log --pretty="[$cmd %s]" -n1 $id
85 source=$(git rev-parse "$3")
90 test "$GIT_EDITOR" = : && source="$source (no editor)"
94 echo "$source $(get_last_cmd)" >"$1"
96 sed -e "1s/.*/$source/" "$1" >msg.tmp
103 echo dummy template
> "$(git rev-parse --git-dir)/template"
105 test_expect_success
'with hook (-m)' '
107 echo "more" >> file &&
109 git commit -m "more" &&
110 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
114 test_expect_success
'with hook (-m editor)' '
116 echo "more" >> file &&
118 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
119 test "$(git log -1 --pretty=format:%s)" = message
123 test_expect_success
'with hook (-t)' '
125 echo "more" >> file &&
127 git commit -t "$(git rev-parse --git-dir)/template" &&
128 test "$(git log -1 --pretty=format:%s)" = template
132 test_expect_success
'with hook (-F)' '
134 echo "more" >> file &&
136 (echo more | git commit -F -) &&
137 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
141 test_expect_success
'with hook (-F editor)' '
143 echo "more" >> file &&
145 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
146 test "$(git log -1 --pretty=format:%s)" = message
150 test_expect_success
'with hook (-C)' '
152 head=$(git rev-parse HEAD) &&
153 echo "more" >> file &&
155 git commit -C $head &&
156 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
160 test_expect_success
'with hook (editor)' '
162 echo "more more" >> file &&
164 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
165 test "$(git log -1 --pretty=format:%s)" = default
169 test_expect_success
'with hook (--amend)' '
171 head=$(git rev-parse HEAD) &&
172 echo "more" >> file &&
174 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
175 test "$(git log -1 --pretty=format:%s)" = "$head"
179 test_expect_success
'with hook (-c)' '
181 head=$(git rev-parse HEAD) &&
182 echo "more" >> file &&
184 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
185 test "$(git log -1 --pretty=format:%s)" = "$head"
189 test_expect_success
'with hook (merge)' '
191 test_when_finished "git checkout -f main" &&
192 git checkout -B other HEAD@{1} &&
193 echo "more" >>file &&
195 git commit -m other &&
197 git merge --no-ff other &&
198 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
201 test_expect_success
'with hook and editor (merge)' '
203 test_when_finished "git checkout -f main" &&
204 git checkout -B other HEAD@{1} &&
205 echo "more" >>file &&
207 git commit -m other &&
209 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
210 test "$(git log -1 --pretty=format:%s)" = "merge"
216 test_expect_
$expect "with hook (rebase ${mode:--i})" '
217 test_when_finished "\
220 git branch -D tmp" &&
221 git checkout -b tmp rebase-me &&
222 GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
223 GIT_EDITOR="\"$FAKE_EDITOR\"" &&
225 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
226 test_must_fail git rebase -i $mode b &&
229 test_must_fail git rebase --continue &&
233 git rebase --continue &&
237 git rebase --continue &&
240 git rebase --continue
242 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
243 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
249 test_expect_success
'with hook (cherry-pick)' '
250 test_when_finished "git checkout -f main" &&
251 git checkout -B other b &&
252 git cherry-pick rebase-1 &&
253 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
256 test_expect_success
'with hook and editor (cherry-pick)' '
257 test_when_finished "git checkout -f main" &&
258 git checkout -B other b &&
259 git cherry-pick -e rebase-1 &&
260 test "$(git log -1 --pretty=format:%s)" = merge
263 test_expect_success
'setup: commit-msg hook that always fails' '
264 test_hook --setup --clobber prepare-commit-msg <<-\EOF
269 test_expect_success
'with failing hook' '
271 test_when_finished "git checkout -f main" &&
272 head=$(git rev-parse HEAD) &&
273 echo "more" >> file &&
275 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
279 test_expect_success
'with failing hook (--no-verify)' '
281 test_when_finished "git checkout -f main" &&
282 head=$(git rev-parse HEAD) &&
283 echo "more" >> file &&
285 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
289 test_expect_success
'with failing hook (merge)' '
291 test_when_finished "git checkout -f main" &&
292 git checkout -B other HEAD@{1} &&
293 echo "more" >> file &&
295 test_hook --remove prepare-commit-msg &&
296 git commit -m other &&
297 test_hook --setup prepare-commit-msg <<-\EOF &&
301 test_must_fail git merge --no-ff other
305 test_expect_success
'with failing hook (cherry-pick)' '
306 test_when_finished "git checkout -f main" &&
307 git checkout -B other b &&
308 test_must_fail git cherry-pick rebase-1 2>actual &&
309 test $(grep -c prepare-commit-msg actual) = 1