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