blob: 2128142a61c60da918381f6b6f74ca612ac9227b [file] [log] [blame]
Paolo Bonzini8089c852008-02-05 08:04:18 +01001#!/bin/sh
2
3test_description='prepare-commit-msg hook'
4
Johannes Schindelin1e2ae142020-11-18 23:44:40 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Paolo Bonzini8089c852008-02-05 08:04:18 +01008. ./test-lib.sh
9
Phillip Wood15cd6d32018-01-24 12:34:21 +000010test_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
Eric Sunshine0c51d6b2021-12-09 00:11:15 -050019 test_commit rebase-$i c $i || return 1
Phillip Wood15cd6d32018-01-24 12:34:21 +000020 done &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000021 git checkout main &&
Phillip Wood15cd6d32018-01-24 12:34:21 +000022
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
Paolo Bonzini8089c852008-02-05 08:04:18 +010042test_expect_success 'with no hook' '
43
44 echo "foo" > file &&
45 git add file &&
46 git commit -m "first"
47
48'
49
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +010050test_expect_success 'setup fake editor for interactive editing' '
51 write_script fake-editor <<-\EOF &&
52 exit 0
53 EOF
Bryan Donlanf69e8362008-05-04 01:37:59 -040054
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +010055 ## 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'
Paolo Bonzini8089c852008-02-05 08:04:18 +010060
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +010061test_expect_success 'setup prepare-commit-msg hook' '
62 test_hook --setup prepare-commit-msg <<\EOF
Phillip Wood15cd6d32018-01-24 12:34:21 +000063GIT_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
Phillip Wood4f8cbf22018-01-24 12:34:20 +000078if test "$2" = commit
79then
Phillip Wood15cd6d32018-01-24 12:34:21 +000080 if test $rebasing = 1
81 then
82 source="$3"
83 else
84 source=$(git rev-parse "$3")
85 fi
Paolo Bonzini8089c852008-02-05 08:04:18 +010086else
Phillip Wood4f8cbf22018-01-24 12:34:20 +000087 source=${2-default}
Paolo Bonzini8089c852008-02-05 08:04:18 +010088fi
Phillip Wood15cd6d32018-01-24 12:34:21 +000089test "$GIT_EDITOR" = : && source="$source (no editor)"
90
91if test $rebasing = 1
Phillip Wood4f8cbf22018-01-24 12:34:20 +000092then
Phillip Wood15cd6d32018-01-24 12:34:21 +000093 echo "$source $(get_last_cmd)" >"$1"
Paolo Bonzini8089c852008-02-05 08:04:18 +010094else
Phillip Wood4f8cbf22018-01-24 12:34:20 +000095 sed -e "1s/.*/$source/" "$1" >msg.tmp
Phillip Wood15cd6d32018-01-24 12:34:21 +000096 mv msg.tmp "$1"
Paolo Bonzini8089c852008-02-05 08:04:18 +010097fi
Paolo Bonzini8089c852008-02-05 08:04:18 +010098exit 0
99EOF
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100100'
Paolo Bonzini8089c852008-02-05 08:04:18 +0100101
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" &&
Elia Pinto0c923252016-01-08 12:06:23 +0100109 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
Paolo Bonzini8089c852008-02-05 08:04:18 +0100110
111'
112
113test_expect_success 'with hook (-m editor)' '
114
115 echo "more" >> file &&
116 git add file &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400117 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
Elia Pinto0c923252016-01-08 12:06:23 +0100118 test "$(git log -1 --pretty=format:%s)" = message
Paolo Bonzini8089c852008-02-05 08:04:18 +0100119
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" &&
Elia Pinto0c923252016-01-08 12:06:23 +0100127 test "$(git log -1 --pretty=format:%s)" = template
Paolo Bonzini8089c852008-02-05 08:04:18 +0100128
129'
130
131test_expect_success 'with hook (-F)' '
132
133 echo "more" >> file &&
134 git add file &&
135 (echo more | git commit -F -) &&
Elia Pinto0c923252016-01-08 12:06:23 +0100136 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
Paolo Bonzini8089c852008-02-05 08:04:18 +0100137
138'
139
140test_expect_success 'with hook (-F editor)' '
141
142 echo "more" >> file &&
143 git add file &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400144 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
Elia Pinto0c923252016-01-08 12:06:23 +0100145 test "$(git log -1 --pretty=format:%s)" = message
Paolo Bonzini8089c852008-02-05 08:04:18 +0100146
147'
148
149test_expect_success 'with hook (-C)' '
150
Elia Pinto0c923252016-01-08 12:06:23 +0100151 head=$(git rev-parse HEAD) &&
Paolo Bonzini8089c852008-02-05 08:04:18 +0100152 echo "more" >> file &&
153 git add file &&
154 git commit -C $head &&
Elia Pinto0c923252016-01-08 12:06:23 +0100155 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
Paolo Bonzini8089c852008-02-05 08:04:18 +0100156
157'
158
159test_expect_success 'with hook (editor)' '
160
161 echo "more more" >> file &&
162 git add file &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400163 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
Elia Pinto0c923252016-01-08 12:06:23 +0100164 test "$(git log -1 --pretty=format:%s)" = default
Paolo Bonzini8089c852008-02-05 08:04:18 +0100165
166'
167
168test_expect_success 'with hook (--amend)' '
169
Elia Pinto0c923252016-01-08 12:06:23 +0100170 head=$(git rev-parse HEAD) &&
Paolo Bonzini8089c852008-02-05 08:04:18 +0100171 echo "more" >> file &&
172 git add file &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400173 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
Elia Pinto0c923252016-01-08 12:06:23 +0100174 test "$(git log -1 --pretty=format:%s)" = "$head"
Paolo Bonzini8089c852008-02-05 08:04:18 +0100175
176'
177
178test_expect_success 'with hook (-c)' '
179
Elia Pinto0c923252016-01-08 12:06:23 +0100180 head=$(git rev-parse HEAD) &&
Paolo Bonzini8089c852008-02-05 08:04:18 +0100181 echo "more" >> file &&
182 git add file &&
Bryan Donlanf69e8362008-05-04 01:37:59 -0400183 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
Elia Pinto0c923252016-01-08 12:06:23 +0100184 test "$(git log -1 --pretty=format:%s)" = "$head"
Paolo Bonzini8089c852008-02-05 08:04:18 +0100185
186'
187
Jay Soffian65969d42011-02-14 20:07:50 -0500188test_expect_success 'with hook (merge)' '
189
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000190 test_when_finished "git checkout -f main" &&
Benoit Pierre1fc4f972014-03-18 11:00:55 +0100191 git checkout -B other HEAD@{1} &&
192 echo "more" >>file &&
Jay Soffian65969d42011-02-14 20:07:50 -0500193 git add file &&
194 git commit -m other &&
195 git checkout - &&
Benoit Pierre1fc4f972014-03-18 11:00:55 +0100196 git merge --no-ff other &&
Elia Pinto0c923252016-01-08 12:06:23 +0100197 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
Benoit Pierre1fc4f972014-03-18 11:00:55 +0100198'
199
200test_expect_success 'with hook and editor (merge)' '
201
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000202 test_when_finished "git checkout -f main" &&
Benoit Pierre1fc4f972014-03-18 11:00:55 +0100203 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 &&
Elia Pinto0c923252016-01-08 12:06:23 +0100209 test "$(git log -1 --pretty=format:%s)" = "merge"
Jay Soffian65969d42011-02-14 20:07:50 -0500210'
211
Phillip Wood15cd6d32018-01-24 12:34:21 +0000212test_rebase () {
213 expect=$1 &&
214 mode=$2 &&
Ævar Arnfjörð Bjarmasona926c4b2021-02-11 02:53:51 +0100215 test_expect_$expect "with hook (rebase ${mode:--i})" '
Phillip Wood15cd6d32018-01-24 12:34:21 +0000216 test_when_finished "\
217 git rebase --abort
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000218 git checkout -f main
Phillip Wood15cd6d32018-01-24 12:34:21 +0000219 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 &&
Phillip Wood891d4a02019-01-28 10:27:56 +0000225 test_must_fail git rebase -i $mode b &&
Phillip Wood15cd6d32018-01-24 12:34:21 +0000226 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 ) &&
Phillip Wood450efe22019-08-19 02:18:21 -0700241 git log --pretty=%s -g -n18 HEAD@{1} >actual &&
Phillip Wood891d4a02019-01-28 10:27:56 +0000242 test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
Phillip Wood15cd6d32018-01-24 12:34:21 +0000243 '
244}
245
Phillip Wood891d4a02019-01-28 10:27:56 +0000246test_rebase success
Phillip Wood15cd6d32018-01-24 12:34:21 +0000247
Phillip Wood66618a52018-01-24 12:34:22 +0000248test_expect_success 'with hook (cherry-pick)' '
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000249 test_when_finished "git checkout -f main" &&
Phillip Wood15cd6d32018-01-24 12:34:21 +0000250 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)' '
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000256 test_when_finished "git checkout -f main" &&
Phillip Wood15cd6d32018-01-24 12:34:21 +0000257 git checkout -B other b &&
258 git cherry-pick -e rebase-1 &&
259 test "$(git log -1 --pretty=format:%s)" = merge
260'
261
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100262test_expect_success 'setup: commit-msg hook that always fails' '
263 test_hook --setup --clobber prepare-commit-msg <<-\EOF
264 exit 1
265 EOF
266'
Paolo Bonzini8089c852008-02-05 08:04:18 +0100267
268test_expect_success 'with failing hook' '
269
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000270 test_when_finished "git checkout -f main" &&
Elia Pinto0c923252016-01-08 12:06:23 +0100271 head=$(git rev-parse HEAD) &&
Paolo Bonzini8089c852008-02-05 08:04:18 +0100272 echo "more" >> file &&
273 git add file &&
Benoit Pierreb7ae1412014-03-10 19:49:32 +0100274 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
Paolo Bonzini8089c852008-02-05 08:04:18 +0100275
276'
277
278test_expect_success 'with failing hook (--no-verify)' '
279
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000280 test_when_finished "git checkout -f main" &&
Elia Pinto0c923252016-01-08 12:06:23 +0100281 head=$(git rev-parse HEAD) &&
Paolo Bonzini8089c852008-02-05 08:04:18 +0100282 echo "more" >> file &&
283 git add file &&
Benoit Pierreb7ae1412014-03-10 19:49:32 +0100284 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
Paolo Bonzini8089c852008-02-05 08:04:18 +0100285
286'
287
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100288test_expect_success 'with failing hook (merge)' '
289
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000290 test_when_finished "git checkout -f main" &&
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100291 git checkout -B other HEAD@{1} &&
292 echo "more" >> file &&
293 git add file &&
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100294 test_hook --remove prepare-commit-msg &&
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100295 git commit -m other &&
Ævar Arnfjörð Bjarmason66865d12022-03-17 11:13:16 +0100296 test_hook --setup prepare-commit-msg <<-\EOF &&
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100297 exit 1
298 EOF
299 git checkout - &&
Benoit Pierre1fc4f972014-03-18 11:00:55 +0100300 test_must_fail git merge --no-ff other
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100301
302'
Paolo Bonzini8089c852008-02-05 08:04:18 +0100303
Ævar Arnfjörð Bjarmasona926c4b2021-02-11 02:53:51 +0100304test_expect_success 'with failing hook (cherry-pick)' '
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000305 test_when_finished "git checkout -f main" &&
Phillip Wood15cd6d32018-01-24 12:34:21 +0000306 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
Paolo Bonzini8089c852008-02-05 08:04:18 +0100311test_done