]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7505-prepare-commit-msg-hook.sh
The sixth 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
5. ./test-lib.sh
6
15cd6d3a
PW
7test_expect_success 'set up commits for rebasing' '
8 test_commit root &&
9 test_commit a a a &&
10 test_commit b b b &&
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)
15 do
16 test_commit rebase-$i c $i
17 done &&
18 git checkout master &&
19
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)
36 EOF
37'
38
8089c85b
PB
39test_expect_success 'with no hook' '
40
41 echo "foo" > file &&
42 git add file &&
43 git commit -m "first"
44
45'
46
47# set up fake editor for interactive editing
48cat > fake-editor <<'EOF'
49#!/bin/sh
50exit 0
51EOF
52chmod +x fake-editor
f69e836f
BD
53
54## Not using test_set_editor here so we can easily ensure the editor variable
55## is only set for the editor tests
8089c85b
PB
56FAKE_EDITOR="$(pwd)/fake-editor"
57export FAKE_EDITOR
58
59# now install hook that always succeeds and adds a message
60HOOKDIR="$(git rev-parse --git-dir)/hooks"
61HOOK="$HOOKDIR/prepare-commit-msg"
62mkdir -p "$HOOKDIR"
462f8caf
JK
63echo "#!$SHELL_PATH" > "$HOOK"
64cat >> "$HOOK" <<'EOF'
65
15cd6d3a
PW
66GIT_DIR=$(git rev-parse --git-dir)
67if test -d "$GIT_DIR/rebase-merge"
68then
69 rebasing=1
70else
71 rebasing=0
72fi
73
74get_last_cmd () {
75 tail -n1 "$GIT_DIR/rebase-merge/done" | {
76 read cmd id _
77 git log --pretty="[$cmd %s]" -n1 $id
78 }
79}
80
4f8cbf2b
PW
81if test "$2" = commit
82then
15cd6d3a
PW
83 if test $rebasing = 1
84 then
85 source="$3"
86 else
87 source=$(git rev-parse "$3")
88 fi
8089c85b 89else
4f8cbf2b 90 source=${2-default}
8089c85b 91fi
15cd6d3a
PW
92test "$GIT_EDITOR" = : && source="$source (no editor)"
93
94if test $rebasing = 1
4f8cbf2b 95then
15cd6d3a 96 echo "$source $(get_last_cmd)" >"$1"
8089c85b 97else
4f8cbf2b 98 sed -e "1s/.*/$source/" "$1" >msg.tmp
15cd6d3a 99 mv msg.tmp "$1"
8089c85b 100fi
8089c85b
PB
101exit 0
102EOF
103chmod +x "$HOOK"
104
105echo dummy template > "$(git rev-parse --git-dir)/template"
106
107test_expect_success 'with hook (-m)' '
108
109 echo "more" >> file &&
110 git add file &&
111 git commit -m "more" &&
0c923256 112 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
113
114'
115
116test_expect_success 'with hook (-m editor)' '
117
118 echo "more" >> file &&
119 git add file &&
f69e836f 120 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
0c923256 121 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
122
123'
124
125test_expect_success 'with hook (-t)' '
126
127 echo "more" >> file &&
128 git add file &&
129 git commit -t "$(git rev-parse --git-dir)/template" &&
0c923256 130 test "$(git log -1 --pretty=format:%s)" = template
8089c85b
PB
131
132'
133
134test_expect_success 'with hook (-F)' '
135
136 echo "more" >> file &&
137 git add file &&
138 (echo more | git commit -F -) &&
0c923256 139 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
140
141'
142
143test_expect_success 'with hook (-F editor)' '
144
145 echo "more" >> file &&
146 git add file &&
f69e836f 147 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
0c923256 148 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
149
150'
151
152test_expect_success 'with hook (-C)' '
153
0c923256 154 head=$(git rev-parse HEAD) &&
8089c85b
PB
155 echo "more" >> file &&
156 git add file &&
157 git commit -C $head &&
0c923256 158 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
8089c85b
PB
159
160'
161
162test_expect_success 'with hook (editor)' '
163
164 echo "more more" >> file &&
165 git add file &&
f69e836f 166 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
0c923256 167 test "$(git log -1 --pretty=format:%s)" = default
8089c85b
PB
168
169'
170
171test_expect_success 'with hook (--amend)' '
172
0c923256 173 head=$(git rev-parse HEAD) &&
8089c85b
PB
174 echo "more" >> file &&
175 git add file &&
f69e836f 176 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
0c923256 177 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
178
179'
180
181test_expect_success 'with hook (-c)' '
182
0c923256 183 head=$(git rev-parse HEAD) &&
8089c85b
PB
184 echo "more" >> file &&
185 git add file &&
f69e836f 186 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
0c923256 187 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
188
189'
190
65969d43
JS
191test_expect_success 'with hook (merge)' '
192
1fc4f97d
BP
193 test_when_finished "git checkout -f master" &&
194 git checkout -B other HEAD@{1} &&
195 echo "more" >>file &&
196 git add file &&
197 git commit -m other &&
198 git checkout - &&
199 git merge --no-ff other &&
0c923256 200 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
1fc4f97d
BP
201'
202
203test_expect_success 'with hook and editor (merge)' '
204
205 test_when_finished "git checkout -f master" &&
206 git checkout -B other HEAD@{1} &&
207 echo "more" >>file &&
65969d43
JS
208 git add file &&
209 git commit -m other &&
210 git checkout - &&
1fc4f97d 211 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
0c923256 212 test "$(git log -1 --pretty=format:%s)" = "merge"
65969d43
JS
213'
214
15cd6d3a
PW
215test_rebase () {
216 expect=$1 &&
217 mode=$2 &&
891d4a03 218 test_expect_$expect C_LOCALE_OUTPUT "with hook (rebase ${mode:--i})" '
15cd6d3a
PW
219 test_when_finished "\
220 git rebase --abort
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\"" &&
226 (
227 export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
891d4a03 228 test_must_fail git rebase -i $mode b &&
15cd6d3a
PW
229 echo x >a &&
230 git add a &&
231 test_must_fail git rebase --continue &&
232 echo x >b &&
233 git add b &&
234 git commit &&
235 git rebase --continue &&
236 echo y >a &&
237 git add a &&
238 git commit &&
239 git rebase --continue &&
240 echo y >b &&
241 git add b &&
242 git rebase --continue
243 ) &&
450efe2d 244 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
891d4a03 245 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
15cd6d3a
PW
246 '
247}
248
891d4a03 249test_rebase success
11aad464 250test_have_prereq !REBASE_P || test_rebase success -p
15cd6d3a 251
66618a50 252test_expect_success 'with hook (cherry-pick)' '
15cd6d3a
PW
253 test_when_finished "git checkout -f master" &&
254 git checkout -B other b &&
255 git cherry-pick rebase-1 &&
256 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
257'
258
259test_expect_success 'with hook and editor (cherry-pick)' '
260 test_when_finished "git checkout -f master" &&
261 git checkout -B other b &&
262 git cherry-pick -e rebase-1 &&
263 test "$(git log -1 --pretty=format:%s)" = merge
264'
265
8089c85b
PB
266cat > "$HOOK" <<'EOF'
267#!/bin/sh
268exit 1
269EOF
270
271test_expect_success 'with failing hook' '
272
1fc4f97d 273 test_when_finished "git checkout -f master" &&
0c923256 274 head=$(git rev-parse HEAD) &&
8089c85b
PB
275 echo "more" >> file &&
276 git add file &&
b7ae1414 277 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
8089c85b
PB
278
279'
280
281test_expect_success 'with failing hook (--no-verify)' '
282
1fc4f97d 283 test_when_finished "git checkout -f master" &&
0c923256 284 head=$(git rev-parse HEAD) &&
8089c85b
PB
285 echo "more" >> file &&
286 git add file &&
b7ae1414 287 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
8089c85b
PB
288
289'
290
3e4141d0
AP
291test_expect_success 'with failing hook (merge)' '
292
1fc4f97d 293 test_when_finished "git checkout -f master" &&
3e4141d0
AP
294 git checkout -B other HEAD@{1} &&
295 echo "more" >> file &&
296 git add file &&
297 rm -f "$HOOK" &&
298 git commit -m other &&
3219bad9 299 write_script "$HOOK" <<-EOF &&
3e4141d0
AP
300 exit 1
301 EOF
302 git checkout - &&
1fc4f97d 303 test_must_fail git merge --no-ff other
3e4141d0
AP
304
305'
8089c85b 306
66618a50 307test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' '
15cd6d3a
PW
308 test_when_finished "git checkout -f master" &&
309 git checkout -B other b &&
310 test_must_fail git cherry-pick rebase-1 2>actual &&
311 test $(grep -c prepare-commit-msg actual) = 1
312'
313
8089c85b 314test_done