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
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 # set up fake editor for interactive editing
51 cat > fake-editor
<<'EOF'
57 ## Not using test_set_editor here so we can easily ensure the editor variable
58 ## is only set for the editor tests
59 FAKE_EDITOR
="$(pwd)/fake-editor"
62 # now install hook that always succeeds and adds a message
63 HOOKDIR
="$(git rev-parse --git-dir)/hooks"
64 HOOK
="$HOOKDIR/prepare-commit-msg"
66 echo "#!$SHELL_PATH" > "$HOOK"
67 cat >> "$HOOK" <<'EOF'
69 GIT_DIR=$(git rev-parse --git-dir)
70 if test -d "$GIT_DIR/rebase-merge"
78 tail -n1 "$GIT_DIR/rebase-merge/done" | {
80 git log --pretty="[$cmd %s]" -n1 $id
90 source=$(git rev-parse "$3")
95 test "$GIT_EDITOR" = : && source="$source (no editor)"
99 echo "$source $(get_last_cmd)" >"$1"
101 sed -e "1s/.*/$source/" "$1" >msg.tmp
108 echo dummy template
> "$(git rev-parse --git-dir)/template"
110 test_expect_success
'with hook (-m)' '
112 echo "more" >> file &&
114 git commit -m "more" &&
115 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
119 test_expect_success
'with hook (-m editor)' '
121 echo "more" >> file &&
123 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
124 test "$(git log -1 --pretty=format:%s)" = message
128 test_expect_success
'with hook (-t)' '
130 echo "more" >> file &&
132 git commit -t "$(git rev-parse --git-dir)/template" &&
133 test "$(git log -1 --pretty=format:%s)" = template
137 test_expect_success
'with hook (-F)' '
139 echo "more" >> file &&
141 (echo more | git commit -F -) &&
142 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
146 test_expect_success
'with hook (-F editor)' '
148 echo "more" >> file &&
150 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
151 test "$(git log -1 --pretty=format:%s)" = message
155 test_expect_success
'with hook (-C)' '
157 head=$(git rev-parse HEAD) &&
158 echo "more" >> file &&
160 git commit -C $head &&
161 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
165 test_expect_success
'with hook (editor)' '
167 echo "more more" >> file &&
169 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
170 test "$(git log -1 --pretty=format:%s)" = default
174 test_expect_success
'with hook (--amend)' '
176 head=$(git rev-parse HEAD) &&
177 echo "more" >> file &&
179 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
180 test "$(git log -1 --pretty=format:%s)" = "$head"
184 test_expect_success
'with hook (-c)' '
186 head=$(git rev-parse HEAD) &&
187 echo "more" >> file &&
189 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
190 test "$(git log -1 --pretty=format:%s)" = "$head"
194 test_expect_success
'with hook (merge)' '
196 test_when_finished "git checkout -f main" &&
197 git checkout -B other HEAD@{1} &&
198 echo "more" >>file &&
200 git commit -m other &&
202 git merge --no-ff other &&
203 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
206 test_expect_success
'with hook and editor (merge)' '
208 test_when_finished "git checkout -f main" &&
209 git checkout -B other HEAD@{1} &&
210 echo "more" >>file &&
212 git commit -m other &&
214 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
215 test "$(git log -1 --pretty=format:%s)" = "merge"
221 test_expect_
$expect "with hook (rebase ${mode:--i})" '
222 test_when_finished "\
225 git branch -D tmp" &&
226 git checkout -b tmp rebase-me &&
227 GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
228 GIT_EDITOR="\"$FAKE_EDITOR\"" &&
230 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
231 test_must_fail git rebase -i $mode b &&
234 test_must_fail git rebase --continue &&
238 git rebase --continue &&
242 git rebase --continue &&
245 git rebase --continue
247 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
248 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
254 test_expect_success
'with hook (cherry-pick)' '
255 test_when_finished "git checkout -f main" &&
256 git checkout -B other b &&
257 git cherry-pick rebase-1 &&
258 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
261 test_expect_success
'with hook and editor (cherry-pick)' '
262 test_when_finished "git checkout -f main" &&
263 git checkout -B other b &&
264 git cherry-pick -e rebase-1 &&
265 test "$(git log -1 --pretty=format:%s)" = merge
268 cat > "$HOOK" <<'EOF'
273 test_expect_success
'with failing hook' '
275 test_when_finished "git checkout -f main" &&
276 head=$(git rev-parse HEAD) &&
277 echo "more" >> file &&
279 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
283 test_expect_success
'with failing hook (--no-verify)' '
285 test_when_finished "git checkout -f main" &&
286 head=$(git rev-parse HEAD) &&
287 echo "more" >> file &&
289 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
293 test_expect_success
'with failing hook (merge)' '
295 test_when_finished "git checkout -f main" &&
296 git checkout -B other HEAD@{1} &&
297 echo "more" >> file &&
300 git commit -m other &&
301 write_script "$HOOK" <<-EOF &&
305 test_must_fail git merge --no-ff other
309 test_expect_success
'with failing hook (cherry-pick)' '
310 test_when_finished "git checkout -f main" &&
311 git checkout -B other b &&
312 test_must_fail git cherry-pick rebase-1 2>actual &&
313 test $(grep -c prepare-commit-msg actual) = 1