The twelfth batch
[git/gitster.git] / t / t7505-prepare-commit-msg-hook.sh
blobb88383df9e48e19f76d2f494151a3bd0e7c0fa61
1 #!/bin/sh
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
9 . ./test-lib.sh
11 test_expect_success 'set up commits for rebasing' '
12 test_commit root &&
13 test_commit a a a &&
14 test_commit b b b &&
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
21 done &&
22 git checkout main &&
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)
40 EOF
43 test_expect_success 'with no hook' '
45 echo "foo" > file &&
46 git add file &&
47 git commit -m "first"
51 test_expect_success 'setup fake editor for interactive editing' '
52 write_script fake-editor <<-\EOF &&
53 exit 0
54 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" &&
59 export 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"
66 then
67 rebasing=1
68 else
69 rebasing=0
72 get_last_cmd () {
73 tail -n1 "$GIT_DIR/rebase-merge/done" | {
74 read cmd id _
75 git log --pretty="[$cmd %s]" -n1 $id
79 if test "$2" = commit
80 then
81 if test $rebasing = 1
82 then
83 source="$3"
84 else
85 source=$(git rev-parse "$3")
87 else
88 source=${2-default}
90 test "$GIT_EDITOR" = : && source="$source (no editor)"
92 if test $rebasing = 1
93 then
94 echo "$source $(get_last_cmd)" >"$1"
95 else
96 sed -e "1s/.*/$source/" "$1" >msg.tmp
97 mv msg.tmp "$1"
99 exit 0
103 echo dummy template > "$(git rev-parse --git-dir)/template"
105 test_expect_success 'with hook (-m)' '
107 echo "more" >> file &&
108 git add 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 &&
117 git add 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 &&
126 git add 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 &&
135 git add 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 &&
144 git add 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 &&
154 git add 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 &&
163 git add 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 &&
173 git add 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 &&
183 git add 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 &&
194 git add file &&
195 git commit -m other &&
196 git checkout - &&
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 &&
206 git add file &&
207 git commit -m other &&
208 git checkout - &&
209 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
210 test "$(git log -1 --pretty=format:%s)" = "merge"
213 test_rebase () {
214 expect=$1 &&
215 mode=$2 &&
216 test_expect_$expect "with hook (rebase ${mode:--i})" '
217 test_when_finished "\
218 git rebase --abort
219 git checkout -f main
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 &&
227 echo x >a &&
228 git add a &&
229 test_must_fail git rebase --continue &&
230 echo x >b &&
231 git add b &&
232 git commit &&
233 git rebase --continue &&
234 echo y >a &&
235 git add a &&
236 git commit &&
237 git rebase --continue &&
238 echo y >b &&
239 git add b &&
240 git rebase --continue
241 ) &&
242 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
243 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
247 test_rebase success
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
265 exit 1
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 &&
274 git add 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 &&
284 git add 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 &&
294 git add file &&
295 test_hook --remove prepare-commit-msg &&
296 git commit -m other &&
297 test_hook --setup prepare-commit-msg <<-\EOF &&
298 exit 1
300 git checkout - &&
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
312 test_done