3 test_description='prepare-commit-msg hook'
7 test_expect_success 'set up commits for rebasing' '
11 git checkout -b rebase-me root &&
12 test_commit rebase-a a aa &&
13 test_commit rebase-b b bb &&
14 for i in $(test_seq 1 13)
16 test_commit rebase-$i c $i
18 git checkout master &&
20 cat >rebase-todo <<-EOF
21 pick $(git rev-parse rebase-a)
22 pick $(git rev-parse rebase-b)
23 fixup $(git rev-parse rebase-1)
24 fixup $(git rev-parse rebase-2)
25 pick $(git rev-parse rebase-3)
26 fixup $(git rev-parse rebase-4)
27 squash $(git rev-parse rebase-5)
28 reword $(git rev-parse rebase-6)
29 squash $(git rev-parse rebase-7)
30 fixup $(git rev-parse rebase-8)
31 fixup $(git rev-parse rebase-9)
32 edit $(git rev-parse rebase-10)
33 squash $(git rev-parse rebase-11)
34 squash $(git rev-parse rebase-12)
35 edit $(git rev-parse rebase-13)
39 test_expect_success 'with no hook' '
47 # set up fake editor for interactive editing
48 cat > fake-editor <<'EOF'
54 ## Not using test_set_editor here so we can easily ensure the editor variable
55 ## is only set for the editor tests
56 FAKE_EDITOR="$(pwd)/fake-editor"
59 # now install hook that always succeeds and adds a message
60 HOOKDIR="$(git rev-parse --git-dir)/hooks"
61 HOOK="$HOOKDIR/prepare-commit-msg"
63 echo "#!$SHELL_PATH" > "$HOOK"
64 cat >> "$HOOK" <<'EOF'
66 GIT_DIR=$(git rev-parse --git-dir)
67 if test -d "$GIT_DIR/rebase-merge"
75 tail -n1 "$GIT_DIR/rebase-merge/done" | {
77 git log --pretty="[$cmd %s]" -n1 $id
87 source=$(git rev-parse "$3")
92 test "$GIT_EDITOR" = : && source="$source (no editor)"
96 echo "$source $(get_last_cmd)" >"$1"
98 sed -e "1s/.*/$source/" "$1" >msg.tmp
105 echo dummy template > "$(git rev-parse --git-dir)/template"
107 test_expect_success 'with hook (-m)' '
109 echo "more" >> file &&
111 git commit -m "more" &&
112 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
116 test_expect_success 'with hook (-m editor)' '
118 echo "more" >> file &&
120 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
121 test "$(git log -1 --pretty=format:%s)" = message
125 test_expect_success 'with hook (-t)' '
127 echo "more" >> file &&
129 git commit -t "$(git rev-parse --git-dir)/template" &&
130 test "$(git log -1 --pretty=format:%s)" = template
134 test_expect_success 'with hook (-F)' '
136 echo "more" >> file &&
138 (echo more | git commit -F -) &&
139 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
143 test_expect_success 'with hook (-F editor)' '
145 echo "more" >> file &&
147 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
148 test "$(git log -1 --pretty=format:%s)" = message
152 test_expect_success 'with hook (-C)' '
154 head=$(git rev-parse HEAD) &&
155 echo "more" >> file &&
157 git commit -C $head &&
158 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
162 test_expect_success 'with hook (editor)' '
164 echo "more more" >> file &&
166 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
167 test "$(git log -1 --pretty=format:%s)" = default
171 test_expect_success 'with hook (--amend)' '
173 head=$(git rev-parse HEAD) &&
174 echo "more" >> file &&
176 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
177 test "$(git log -1 --pretty=format:%s)" = "$head"
181 test_expect_success 'with hook (-c)' '
183 head=$(git rev-parse HEAD) &&
184 echo "more" >> file &&
186 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
187 test "$(git log -1 --pretty=format:%s)" = "$head"
191 test_expect_success 'with hook (merge)' '
193 test_when_finished "git checkout -f master" &&
194 git checkout -B other HEAD@{1} &&
195 echo "more" >>file &&
197 git commit -m other &&
199 git merge --no-ff other &&
200 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
203 test_expect_success 'with hook and editor (merge)' '
205 test_when_finished "git checkout -f master" &&
206 git checkout -B other HEAD@{1} &&
207 echo "more" >>file &&
209 git commit -m other &&
211 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
212 test "$(git log -1 --pretty=format:%s)" = "merge"
218 test_expect_$expect C_LOCALE_OUTPUT "with hook (rebase ${mode:--i})" '
219 test_when_finished "\
221 git checkout -f master
222 git branch -D tmp" &&
223 git checkout -b tmp rebase-me &&
224 GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
225 GIT_EDITOR="\"$FAKE_EDITOR\"" &&
227 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
228 test_must_fail git rebase -i $mode b &&
231 test_must_fail git rebase --continue &&
235 git rebase --continue &&
239 git rebase --continue &&
242 git rebase --continue
244 if test "$mode" = -p # reword amended after pick
250 git log --pretty=%s -g -n$n HEAD@{1} >actual &&
251 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
256 test_have_prereq !REBASE_P || test_rebase success -p
258 test_expect_success 'with hook (cherry-pick)' '
259 test_when_finished "git checkout -f master" &&
260 git checkout -B other b &&
261 git cherry-pick rebase-1 &&
262 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
265 test_expect_success 'with hook and editor (cherry-pick)' '
266 test_when_finished "git checkout -f master" &&
267 git checkout -B other b &&
268 git cherry-pick -e rebase-1 &&
269 test "$(git log -1 --pretty=format:%s)" = merge
272 cat > "$HOOK" <<'EOF'
277 test_expect_success 'with failing hook' '
279 test_when_finished "git checkout -f master" &&
280 head=$(git rev-parse HEAD) &&
281 echo "more" >> file &&
283 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
287 test_expect_success 'with failing hook (--no-verify)' '
289 test_when_finished "git checkout -f master" &&
290 head=$(git rev-parse HEAD) &&
291 echo "more" >> file &&
293 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
297 test_expect_success 'with failing hook (merge)' '
299 test_when_finished "git checkout -f master" &&
300 git checkout -B other HEAD@{1} &&
301 echo "more" >> file &&
304 git commit -m other &&
305 write_script "$HOOK" <<-EOF &&
309 test_must_fail git merge --no-ff other
313 test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
314 test_when_finished "git checkout -f master" &&
315 git checkout -B other b &&
316 test_must_fail git cherry-pick rebase-1 2>actual &&
317 test $(grep -c prepare-commit-msg actual) = 1