]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7505-prepare-commit-msg-hook.sh
Merge branch 'wx/merge-ort-comment-typofix'
[thirdparty/git.git] / t / t7505-prepare-commit-msg-hook.sh
1 #!/bin/sh
2
3 test_description='prepare-commit-msg hook'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
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)
18 do
19 test_commit rebase-$i c $i || return 1
20 done &&
21 git checkout main &&
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
42 test_expect_success 'with no hook' '
43
44 echo "foo" > file &&
45 git add file &&
46 git commit -m "first"
47
48 '
49
50 test_expect_success 'setup fake editor for interactive editing' '
51 write_script fake-editor <<-\EOF &&
52 exit 0
53 EOF
54
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 '
60
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
69 fi
70
71 get_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
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")
85 fi
86 else
87 source=${2-default}
88 fi
89 test "$GIT_EDITOR" = : && source="$source (no editor)"
90
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"
97 fi
98 exit 0
99 EOF
100 '
101
102 echo dummy template > "$(git rev-parse --git-dir)/template"
103
104 test_expect_success 'with hook (-m)' '
105
106 echo "more" >> file &&
107 git add file &&
108 git commit -m "more" &&
109 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
110
111 '
112
113 test_expect_success 'with hook (-m editor)' '
114
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
119
120 '
121
122 test_expect_success 'with hook (-t)' '
123
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
128
129 '
130
131 test_expect_success 'with hook (-F)' '
132
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)"
137
138 '
139
140 test_expect_success 'with hook (-F editor)' '
141
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
146
147 '
148
149 test_expect_success 'with hook (-C)' '
150
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)"
156
157 '
158
159 test_expect_success 'with hook (editor)' '
160
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
165
166 '
167
168 test_expect_success 'with hook (--amend)' '
169
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"
175
176 '
177
178 test_expect_success 'with hook (-c)' '
179
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"
185
186 '
187
188 test_expect_success 'with hook (merge)' '
189
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)"
198 '
199
200 test_expect_success 'with hook and editor (merge)' '
201
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"
210 '
211
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\"" &&
223 (
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
243 '
244 }
245
246 test_rebase success
247
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)"
253 '
254
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
260 '
261
262 test_expect_success 'setup: commit-msg hook that always fails' '
263 test_hook --setup --clobber prepare-commit-msg <<-\EOF
264 exit 1
265 EOF
266 '
267
268 test_expect_success 'with failing hook' '
269
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
275
276 '
277
278 test_expect_success 'with failing hook (--no-verify)' '
279
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
285
286 '
287
288 test_expect_success 'with failing hook (merge)' '
289
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
298 EOF
299 git checkout - &&
300 test_must_fail git merge --no-ff other
301
302 '
303
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
309 '
310
311 test_done