]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7505-prepare-commit-msg-hook.sh
The third batch
[thirdparty/git.git] / t / t7505-prepare-commit-msg-hook.sh
CommitLineData
8089c85b
PB
1#!/bin/sh
2
3test_description='prepare-commit-msg hook'
4
1e2ae142 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8089c85b
PB
8. ./test-lib.sh
9
15cd6d3a
PW
10test_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)
18 do
0c51d6b4 19 test_commit rebase-$i c $i || return 1
15cd6d3a 20 done &&
1e2ae142 21 git checkout main &&
15cd6d3a
PW
22
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
40'
41
8089c85b
PB
42test_expect_success 'with no hook' '
43
44 echo "foo" > file &&
45 git add file &&
46 git commit -m "first"
47
48'
49
66865d12
ÆAB
50test_expect_success 'setup fake editor for interactive editing' '
51 write_script fake-editor <<-\EOF &&
52 exit 0
53 EOF
8089c85b 54
66865d12
ÆAB
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
59'
462f8caf 60
66865d12
ÆAB
61test_expect_success 'setup prepare-commit-msg hook' '
62 test_hook --setup prepare-commit-msg <<\EOF
15cd6d3a
PW
63GIT_DIR=$(git rev-parse --git-dir)
64if test -d "$GIT_DIR/rebase-merge"
65then
66 rebasing=1
67else
68 rebasing=0
69fi
70
71get_last_cmd () {
72 tail -n1 "$GIT_DIR/rebase-merge/done" | {
73 read cmd id _
74 git log --pretty="[$cmd %s]" -n1 $id
75 }
76}
77
4f8cbf2b
PW
78if test "$2" = commit
79then
15cd6d3a
PW
80 if test $rebasing = 1
81 then
82 source="$3"
83 else
84 source=$(git rev-parse "$3")
85 fi
8089c85b 86else
4f8cbf2b 87 source=${2-default}
8089c85b 88fi
15cd6d3a
PW
89test "$GIT_EDITOR" = : && source="$source (no editor)"
90
91if test $rebasing = 1
4f8cbf2b 92then
15cd6d3a 93 echo "$source $(get_last_cmd)" >"$1"
8089c85b 94else
4f8cbf2b 95 sed -e "1s/.*/$source/" "$1" >msg.tmp
15cd6d3a 96 mv msg.tmp "$1"
8089c85b 97fi
8089c85b
PB
98exit 0
99EOF
66865d12 100'
8089c85b
PB
101
102echo dummy template > "$(git rev-parse --git-dir)/template"
103
104test_expect_success 'with hook (-m)' '
105
106 echo "more" >> file &&
107 git add file &&
108 git commit -m "more" &&
0c923256 109 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
110
111'
112
113test_expect_success 'with hook (-m editor)' '
114
115 echo "more" >> file &&
116 git add file &&
f69e836f 117 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
0c923256 118 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
119
120'
121
122test_expect_success 'with hook (-t)' '
123
124 echo "more" >> file &&
125 git add file &&
126 git commit -t "$(git rev-parse --git-dir)/template" &&
0c923256 127 test "$(git log -1 --pretty=format:%s)" = template
8089c85b
PB
128
129'
130
131test_expect_success 'with hook (-F)' '
132
133 echo "more" >> file &&
134 git add file &&
135 (echo more | git commit -F -) &&
0c923256 136 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
137
138'
139
140test_expect_success 'with hook (-F editor)' '
141
142 echo "more" >> file &&
143 git add file &&
f69e836f 144 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
0c923256 145 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
146
147'
148
149test_expect_success 'with hook (-C)' '
150
0c923256 151 head=$(git rev-parse HEAD) &&
8089c85b
PB
152 echo "more" >> file &&
153 git add file &&
154 git commit -C $head &&
0c923256 155 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
8089c85b
PB
156
157'
158
159test_expect_success 'with hook (editor)' '
160
161 echo "more more" >> file &&
162 git add file &&
f69e836f 163 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
0c923256 164 test "$(git log -1 --pretty=format:%s)" = default
8089c85b
PB
165
166'
167
168test_expect_success 'with hook (--amend)' '
169
0c923256 170 head=$(git rev-parse HEAD) &&
8089c85b
PB
171 echo "more" >> file &&
172 git add file &&
f69e836f 173 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
0c923256 174 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
175
176'
177
178test_expect_success 'with hook (-c)' '
179
0c923256 180 head=$(git rev-parse HEAD) &&
8089c85b
PB
181 echo "more" >> file &&
182 git add file &&
f69e836f 183 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
0c923256 184 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
185
186'
187
65969d43
JS
188test_expect_success 'with hook (merge)' '
189
1e2ae142 190 test_when_finished "git checkout -f main" &&
1fc4f97d
BP
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 &&
0c923256 197 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
1fc4f97d
BP
198'
199
200test_expect_success 'with hook and editor (merge)' '
201
1e2ae142 202 test_when_finished "git checkout -f main" &&
1fc4f97d
BP
203 git checkout -B other HEAD@{1} &&
204 echo "more" >>file &&
65969d43
JS
205 git add file &&
206 git commit -m other &&
207 git checkout - &&
1fc4f97d 208 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
0c923256 209 test "$(git log -1 --pretty=format:%s)" = "merge"
65969d43
JS
210'
211
15cd6d3a
PW
212test_rebase () {
213 expect=$1 &&
214 mode=$2 &&
a926c4b9 215 test_expect_$expect "with hook (rebase ${mode:--i})" '
15cd6d3a
PW
216 test_when_finished "\
217 git rebase --abort
1e2ae142 218 git checkout -f main
15cd6d3a
PW
219 git branch -D tmp" &&
220 git checkout -b tmp rebase-me &&
221 GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
222 GIT_EDITOR="\"$FAKE_EDITOR\"" &&
223 (
224 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
891d4a03 225 test_must_fail git rebase -i $mode b &&
15cd6d3a
PW
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 ) &&
450efe2d 241 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
891d4a03 242 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
15cd6d3a
PW
243 '
244}
245
891d4a03 246test_rebase success
15cd6d3a 247
66618a50 248test_expect_success 'with hook (cherry-pick)' '
1e2ae142 249 test_when_finished "git checkout -f main" &&
15cd6d3a
PW
250 git checkout -B other b &&
251 git cherry-pick rebase-1 &&
252 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
253'
254
255test_expect_success 'with hook and editor (cherry-pick)' '
1e2ae142 256 test_when_finished "git checkout -f main" &&
15cd6d3a
PW
257 git checkout -B other b &&
258 git cherry-pick -e rebase-1 &&
259 test "$(git log -1 --pretty=format:%s)" = merge
260'
261
66865d12
ÆAB
262test_expect_success 'setup: commit-msg hook that always fails' '
263 test_hook --setup --clobber prepare-commit-msg <<-\EOF
264 exit 1
265 EOF
266'
8089c85b
PB
267
268test_expect_success 'with failing hook' '
269
1e2ae142 270 test_when_finished "git checkout -f main" &&
0c923256 271 head=$(git rev-parse HEAD) &&
8089c85b
PB
272 echo "more" >> file &&
273 git add file &&
b7ae1414 274 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
8089c85b
PB
275
276'
277
278test_expect_success 'with failing hook (--no-verify)' '
279
1e2ae142 280 test_when_finished "git checkout -f main" &&
0c923256 281 head=$(git rev-parse HEAD) &&
8089c85b
PB
282 echo "more" >> file &&
283 git add file &&
b7ae1414 284 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
8089c85b
PB
285
286'
287
3e4141d0
AP
288test_expect_success 'with failing hook (merge)' '
289
1e2ae142 290 test_when_finished "git checkout -f main" &&
3e4141d0
AP
291 git checkout -B other HEAD@{1} &&
292 echo "more" >> file &&
293 git add file &&
66865d12 294 test_hook --remove prepare-commit-msg &&
3e4141d0 295 git commit -m other &&
66865d12 296 test_hook --setup prepare-commit-msg <<-\EOF &&
3e4141d0
AP
297 exit 1
298 EOF
299 git checkout - &&
1fc4f97d 300 test_must_fail git merge --no-ff other
3e4141d0
AP
301
302'
8089c85b 303
a926c4b9 304test_expect_success 'with failing hook (cherry-pick)' '
1e2ae142 305 test_when_finished "git checkout -f main" &&
15cd6d3a
PW
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
309'
310
8089c85b 311test_done