]>
Commit | Line | Data |
---|---|---|
8089c85b PB |
1 | #!/bin/sh |
2 | ||
3 | test_description='prepare-commit-msg hook' | |
4 | ||
1e2ae142 | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
7 | ||
8089c85b PB |
8 | . ./test-lib.sh |
9 | ||
15cd6d3a PW |
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 && | |
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 |
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 | |
f69e836f BD |
56 | |
57 | ## Not using test_set_editor here so we can easily ensure the editor variable | |
58 | ## is only set for the editor tests | |
8089c85b PB |
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" | |
462f8caf JK |
66 | echo "#!$SHELL_PATH" > "$HOOK" |
67 | cat >> "$HOOK" <<'EOF' | |
68 | ||
15cd6d3a PW |
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 | ||
4f8cbf2b PW |
84 | if test "$2" = commit |
85 | then | |
15cd6d3a PW |
86 | if test $rebasing = 1 |
87 | then | |
88 | source="$3" | |
89 | else | |
90 | source=$(git rev-parse "$3") | |
91 | fi | |
8089c85b | 92 | else |
4f8cbf2b | 93 | source=${2-default} |
8089c85b | 94 | fi |
15cd6d3a PW |
95 | test "$GIT_EDITOR" = : && source="$source (no editor)" |
96 | ||
97 | if test $rebasing = 1 | |
4f8cbf2b | 98 | then |
15cd6d3a | 99 | echo "$source $(get_last_cmd)" >"$1" |
8089c85b | 100 | else |
4f8cbf2b | 101 | sed -e "1s/.*/$source/" "$1" >msg.tmp |
15cd6d3a | 102 | mv msg.tmp "$1" |
8089c85b | 103 | fi |
8089c85b PB |
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" && | |
0c923256 | 115 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
8089c85b PB |
116 | |
117 | ' | |
118 | ||
119 | test_expect_success 'with hook (-m editor)' ' | |
120 | ||
121 | echo "more" >> file && | |
122 | git add file && | |
f69e836f | 123 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" && |
0c923256 | 124 | test "$(git log -1 --pretty=format:%s)" = message |
8089c85b PB |
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" && | |
0c923256 | 133 | test "$(git log -1 --pretty=format:%s)" = template |
8089c85b PB |
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 -) && | |
0c923256 | 142 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
8089c85b PB |
143 | |
144 | ' | |
145 | ||
146 | test_expect_success 'with hook (-F editor)' ' | |
147 | ||
148 | echo "more" >> file && | |
149 | git add file && | |
f69e836f | 150 | (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) && |
0c923256 | 151 | test "$(git log -1 --pretty=format:%s)" = message |
8089c85b PB |
152 | |
153 | ' | |
154 | ||
155 | test_expect_success 'with hook (-C)' ' | |
156 | ||
0c923256 | 157 | head=$(git rev-parse HEAD) && |
8089c85b PB |
158 | echo "more" >> file && |
159 | git add file && | |
160 | git commit -C $head && | |
0c923256 | 161 | test "$(git log -1 --pretty=format:%s)" = "$head (no editor)" |
8089c85b PB |
162 | |
163 | ' | |
164 | ||
165 | test_expect_success 'with hook (editor)' ' | |
166 | ||
167 | echo "more more" >> file && | |
168 | git add file && | |
f69e836f | 169 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && |
0c923256 | 170 | test "$(git log -1 --pretty=format:%s)" = default |
8089c85b PB |
171 | |
172 | ' | |
173 | ||
174 | test_expect_success 'with hook (--amend)' ' | |
175 | ||
0c923256 | 176 | head=$(git rev-parse HEAD) && |
8089c85b PB |
177 | echo "more" >> file && |
178 | git add file && | |
f69e836f | 179 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend && |
0c923256 | 180 | test "$(git log -1 --pretty=format:%s)" = "$head" |
8089c85b PB |
181 | |
182 | ' | |
183 | ||
184 | test_expect_success 'with hook (-c)' ' | |
185 | ||
0c923256 | 186 | head=$(git rev-parse HEAD) && |
8089c85b PB |
187 | echo "more" >> file && |
188 | git add file && | |
f69e836f | 189 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head && |
0c923256 | 190 | test "$(git log -1 --pretty=format:%s)" = "$head" |
8089c85b PB |
191 | |
192 | ' | |
193 | ||
65969d43 JS |
194 | test_expect_success 'with hook (merge)' ' |
195 | ||
1e2ae142 | 196 | test_when_finished "git checkout -f main" && |
1fc4f97d BP |
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 && | |
0c923256 | 203 | test "$(git log -1 --pretty=format:%s)" = "merge (no editor)" |
1fc4f97d BP |
204 | ' |
205 | ||
206 | test_expect_success 'with hook and editor (merge)' ' | |
207 | ||
1e2ae142 | 208 | test_when_finished "git checkout -f main" && |
1fc4f97d BP |
209 | git checkout -B other HEAD@{1} && |
210 | echo "more" >>file && | |
65969d43 JS |
211 | git add file && |
212 | git commit -m other && | |
213 | git checkout - && | |
1fc4f97d | 214 | env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other && |
0c923256 | 215 | test "$(git log -1 --pretty=format:%s)" = "merge" |
65969d43 JS |
216 | ' |
217 | ||
15cd6d3a PW |
218 | test_rebase () { |
219 | expect=$1 && | |
220 | mode=$2 && | |
891d4a03 | 221 | test_expect_$expect C_LOCALE_OUTPUT "with hook (rebase ${mode:--i})" ' |
15cd6d3a PW |
222 | test_when_finished "\ |
223 | git rebase --abort | |
1e2ae142 | 224 | git checkout -f main |
15cd6d3a PW |
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 && | |
891d4a03 | 231 | test_must_fail git rebase -i $mode b && |
15cd6d3a PW |
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 | ) && | |
450efe2d | 247 | git log --pretty=%s -g -n18 HEAD@{1} >actual && |
891d4a03 | 248 | test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual |
15cd6d3a PW |
249 | ' |
250 | } | |
251 | ||
891d4a03 | 252 | test_rebase success |
11aad464 | 253 | test_have_prereq !REBASE_P || test_rebase success -p |
15cd6d3a | 254 | |
66618a50 | 255 | test_expect_success 'with hook (cherry-pick)' ' |
1e2ae142 | 256 | test_when_finished "git checkout -f main" && |
15cd6d3a PW |
257 | git checkout -B other b && |
258 | git cherry-pick rebase-1 && | |
259 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" | |
260 | ' | |
261 | ||
262 | test_expect_success 'with hook and editor (cherry-pick)' ' | |
1e2ae142 | 263 | test_when_finished "git checkout -f main" && |
15cd6d3a PW |
264 | git checkout -B other b && |
265 | git cherry-pick -e rebase-1 && | |
266 | test "$(git log -1 --pretty=format:%s)" = merge | |
267 | ' | |
268 | ||
8089c85b PB |
269 | cat > "$HOOK" <<'EOF' |
270 | #!/bin/sh | |
271 | exit 1 | |
272 | EOF | |
273 | ||
274 | test_expect_success 'with failing hook' ' | |
275 | ||
1e2ae142 | 276 | test_when_finished "git checkout -f main" && |
0c923256 | 277 | head=$(git rev-parse HEAD) && |
8089c85b PB |
278 | echo "more" >> file && |
279 | git add file && | |
b7ae1414 | 280 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head |
8089c85b PB |
281 | |
282 | ' | |
283 | ||
284 | test_expect_success 'with failing hook (--no-verify)' ' | |
285 | ||
1e2ae142 | 286 | test_when_finished "git checkout -f main" && |
0c923256 | 287 | head=$(git rev-parse HEAD) && |
8089c85b PB |
288 | echo "more" >> file && |
289 | git add file && | |
b7ae1414 | 290 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head |
8089c85b PB |
291 | |
292 | ' | |
293 | ||
3e4141d0 AP |
294 | test_expect_success 'with failing hook (merge)' ' |
295 | ||
1e2ae142 | 296 | test_when_finished "git checkout -f main" && |
3e4141d0 AP |
297 | git checkout -B other HEAD@{1} && |
298 | echo "more" >> file && | |
299 | git add file && | |
300 | rm -f "$HOOK" && | |
301 | git commit -m other && | |
3219bad9 | 302 | write_script "$HOOK" <<-EOF && |
3e4141d0 AP |
303 | exit 1 |
304 | EOF | |
305 | git checkout - && | |
1fc4f97d | 306 | test_must_fail git merge --no-ff other |
3e4141d0 AP |
307 | |
308 | ' | |
8089c85b | 309 | |
66618a50 | 310 | test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' ' |
1e2ae142 | 311 | test_when_finished "git checkout -f main" && |
15cd6d3a PW |
312 | git checkout -B other b && |
313 | test_must_fail git cherry-pick rebase-1 2>actual && | |
314 | test $(grep -c prepare-commit-msg actual) = 1 | |
315 | ' | |
316 | ||
8089c85b | 317 | test_done |