treewide: be explicit about dependence on oid-array.h
[alt-git.git] / t / t7505-prepare-commit-msg-hook.sh
blob2128142a61c60da918381f6b6f74ca612ac9227b
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-lib.sh
10 test_expect_success 'set up commits for rebasing' '
11 test_commit root &&
12 test_commit a a a &&
13 test_commit b b b &&
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
20 done &&
21 git checkout main &&
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)
39 EOF
42 test_expect_success 'with no hook' '
44 echo "foo" > file &&
45 git add file &&
46 git commit -m "first"
50 test_expect_success 'setup fake editor for interactive editing' '
51 write_script fake-editor <<-\EOF &&
52 exit 0
53 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" &&
58 export 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"
65 then
66 rebasing=1
67 else
68 rebasing=0
71 get_last_cmd () {
72 tail -n1 "$GIT_DIR/rebase-merge/done" | {
73 read cmd id _
74 git log --pretty="[$cmd %s]" -n1 $id
78 if test "$2" = commit
79 then
80 if test $rebasing = 1
81 then
82 source="$3"
83 else
84 source=$(git rev-parse "$3")
86 else
87 source=${2-default}
89 test "$GIT_EDITOR" = : && source="$source (no editor)"
91 if test $rebasing = 1
92 then
93 echo "$source $(get_last_cmd)" >"$1"
94 else
95 sed -e "1s/.*/$source/" "$1" >msg.tmp
96 mv msg.tmp "$1"
98 exit 0
99 EOF
102 echo dummy template > "$(git rev-parse --git-dir)/template"
104 test_expect_success 'with hook (-m)' '
106 echo "more" >> file &&
107 git add 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 &&
116 git add 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 &&
125 git add 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 &&
134 git add 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 &&
143 git add 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 &&
153 git add 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 &&
162 git add 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 &&
172 git add 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 &&
182 git add 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 &&
193 git add file &&
194 git commit -m other &&
195 git checkout - &&
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 &&
205 git add file &&
206 git commit -m other &&
207 git checkout - &&
208 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
209 test "$(git log -1 --pretty=format:%s)" = "merge"
212 test_rebase () {
213 expect=$1 &&
214 mode=$2 &&
215 test_expect_$expect "with hook (rebase ${mode:--i})" '
216 test_when_finished "\
217 git rebase --abort
218 git checkout -f main
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 &&
226 echo x >a &&
227 git add a &&
228 test_must_fail git rebase --continue &&
229 echo x >b &&
230 git add b &&
231 git commit &&
232 git rebase --continue &&
233 echo y >a &&
234 git add a &&
235 git commit &&
236 git rebase --continue &&
237 echo y >b &&
238 git add b &&
239 git rebase --continue
240 ) &&
241 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
242 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
246 test_rebase success
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
264 exit 1
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 &&
273 git add 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 &&
283 git add 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 &&
293 git add file &&
294 test_hook --remove prepare-commit-msg &&
295 git commit -m other &&
296 test_hook --setup prepare-commit-msg <<-\EOF &&
297 exit 1
299 git checkout - &&
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
311 test_done