3 test_description
='prepare-commit-msg hook'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success
'set up commits for rebasing' '
14 git checkout -b rebase-me root &&
15 test_commit rebase-a a aa &&
16 test_commit rebase-b b bb &&
17 for i in $(test_seq 1 13)
19 test_commit rebase-$i c $i || return 1
23 cat >rebase-todo <<-EOF
24 pick $(git rev-parse rebase-a)
25 pick $(git rev-parse rebase-b)
26 fixup $(git rev-parse rebase-1)
27 fixup $(git rev-parse rebase-2)
28 pick $(git rev-parse rebase-3)
29 fixup $(git rev-parse rebase-4)
30 squash $(git rev-parse rebase-5)
31 reword $(git rev-parse rebase-6)
32 squash $(git rev-parse rebase-7)
33 fixup $(git rev-parse rebase-8)
34 fixup $(git rev-parse rebase-9)
35 edit $(git rev-parse rebase-10)
36 squash $(git rev-parse rebase-11)
37 squash $(git rev-parse rebase-12)
38 edit $(git rev-parse rebase-13)
42 test_expect_success
'with no hook' '
50 test_expect_success
'setup fake editor for interactive editing' '
51 write_script fake-editor <<-\EOF &&
55 ## Not using test_set_editor here so we can easily ensure the editor variable
56 ## is only set for the editor tests
57 FAKE_EDITOR="$(pwd)/fake-editor" &&
61 test_expect_success
'setup prepare-commit-msg hook' '
62 test_hook --setup prepare-commit-msg <<\EOF
63 GIT_DIR=$(git rev-parse --git-dir)
64 if test -d "$GIT_DIR/rebase-merge"
72 tail -n1 "$GIT_DIR/rebase-merge/done" | {
74 git log --pretty="[$cmd %s]" -n1 $id
84 source=$(git rev-parse "$3")
89 test "$GIT_EDITOR" = : && source="$source (no editor)"
93 echo "$source $(get_last_cmd)" >"$1"
95 sed -e "1s/.*/$source/" "$1" >msg.tmp
102 echo dummy template
> "$(git rev-parse --git-dir)/template"
104 test_expect_success
'with hook (-m)' '
106 echo "more" >> file &&
108 git commit -m "more" &&
109 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
113 test_expect_success
'with hook (-m editor)' '
115 echo "more" >> file &&
117 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
118 test "$(git log -1 --pretty=format:%s)" = message
122 test_expect_success
'with hook (-t)' '
124 echo "more" >> file &&
126 git commit -t "$(git rev-parse --git-dir)/template" &&
127 test "$(git log -1 --pretty=format:%s)" = template
131 test_expect_success
'with hook (-F)' '
133 echo "more" >> file &&
135 (echo more | git commit -F -) &&
136 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
140 test_expect_success
'with hook (-F editor)' '
142 echo "more" >> file &&
144 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
145 test "$(git log -1 --pretty=format:%s)" = message
149 test_expect_success
'with hook (-C)' '
151 head=$(git rev-parse HEAD) &&
152 echo "more" >> file &&
154 git commit -C $head &&
155 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
159 test_expect_success
'with hook (editor)' '
161 echo "more more" >> file &&
163 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
164 test "$(git log -1 --pretty=format:%s)" = default
168 test_expect_success
'with hook (--amend)' '
170 head=$(git rev-parse HEAD) &&
171 echo "more" >> file &&
173 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
174 test "$(git log -1 --pretty=format:%s)" = "$head"
178 test_expect_success
'with hook (-c)' '
180 head=$(git rev-parse HEAD) &&
181 echo "more" >> file &&
183 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
184 test "$(git log -1 --pretty=format:%s)" = "$head"
188 test_expect_success
'with hook (merge)' '
190 test_when_finished "git checkout -f main" &&
191 git checkout -B other HEAD@{1} &&
192 echo "more" >>file &&
194 git commit -m other &&
196 git merge --no-ff other &&
197 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
200 test_expect_success
'with hook and editor (merge)' '
202 test_when_finished "git checkout -f main" &&
203 git checkout -B other HEAD@{1} &&
204 echo "more" >>file &&
206 git commit -m other &&
208 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
209 test "$(git log -1 --pretty=format:%s)" = "merge"
215 test_expect_
$expect "with hook (rebase ${mode:--i})" '
216 test_when_finished "\
219 git branch -D tmp" &&
220 git checkout -b tmp rebase-me &&
221 GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
222 GIT_EDITOR="\"$FAKE_EDITOR\"" &&
224 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
225 test_must_fail git rebase -i $mode b &&
228 test_must_fail git rebase --continue &&
232 git rebase --continue &&
236 git rebase --continue &&
239 git rebase --continue
241 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
242 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
248 test_expect_success
'with hook (cherry-pick)' '
249 test_when_finished "git checkout -f main" &&
250 git checkout -B other b &&
251 git cherry-pick rebase-1 &&
252 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
255 test_expect_success
'with hook and editor (cherry-pick)' '
256 test_when_finished "git checkout -f main" &&
257 git checkout -B other b &&
258 git cherry-pick -e rebase-1 &&
259 test "$(git log -1 --pretty=format:%s)" = merge
262 test_expect_success
'setup: commit-msg hook that always fails' '
263 test_hook --setup --clobber prepare-commit-msg <<-\EOF
268 test_expect_success
'with failing hook' '
270 test_when_finished "git checkout -f main" &&
271 head=$(git rev-parse HEAD) &&
272 echo "more" >> file &&
274 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
278 test_expect_success
'with failing hook (--no-verify)' '
280 test_when_finished "git checkout -f main" &&
281 head=$(git rev-parse HEAD) &&
282 echo "more" >> file &&
284 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
288 test_expect_success
'with failing hook (merge)' '
290 test_when_finished "git checkout -f main" &&
291 git checkout -B other HEAD@{1} &&
292 echo "more" >> file &&
294 test_hook --remove prepare-commit-msg &&
295 git commit -m other &&
296 test_hook --setup prepare-commit-msg <<-\EOF &&
300 test_must_fail git merge --no-ff other
304 test_expect_success
'with failing hook (cherry-pick)' '
305 test_when_finished "git checkout -f main" &&
306 git checkout -B other b &&
307 test_must_fail git cherry-pick rebase-1 2>actual &&
308 test $(grep -c prepare-commit-msg actual) = 1