ci: use upload-artifacts v1 for dockerized jobs
[git/debian.git] / t / t7505-prepare-commit-msg-hook.sh
blob7a8194ce720ed5d9244caf2bbd882a56f491476c
1 #!/bin/sh
3 test_description='prepare-commit-msg hook'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
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)
19 test_commit rebase-$i c $i
20 done &&
21 git checkout main &&
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
42 test_expect_success 'with no hook' '
44 echo "foo" > file &&
45 git add file &&
46 git commit -m "first"
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
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
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'
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
77 get_last_cmd () {
78 tail -n1 "$GIT_DIR/rebase-merge/done" | {
79 read cmd id _
80 git log --pretty="[$cmd %s]" -n1 $id
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")
92 else
93 source=${2-default}
95 test "$GIT_EDITOR" = : && source="$source (no editor)"
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"
104 exit 0
106 chmod +x "$HOOK"
108 echo dummy template > "$(git rev-parse --git-dir)/template"
110 test_expect_success 'with hook (-m)' '
112 echo "more" >> file &&
113 git add file &&
114 git commit -m "more" &&
115 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
119 test_expect_success 'with hook (-m editor)' '
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
128 test_expect_success 'with hook (-t)' '
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
137 test_expect_success 'with hook (-F)' '
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)"
146 test_expect_success 'with hook (-F editor)' '
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
155 test_expect_success 'with hook (-C)' '
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)"
165 test_expect_success 'with hook (editor)' '
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
174 test_expect_success 'with hook (--amend)' '
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"
184 test_expect_success 'with hook (-c)' '
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"
194 test_expect_success 'with hook (merge)' '
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)"
206 test_expect_success 'with hook and editor (merge)' '
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"
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\"" &&
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
252 test_rebase success
253 test_have_prereq !REBASE_P || test_rebase success -p
255 test_expect_success 'with hook (cherry-pick)' '
256 test_when_finished "git checkout -f main" &&
257 git checkout -B other b &&
258 git cherry-pick rebase-1 &&
259 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
262 test_expect_success 'with hook and editor (cherry-pick)' '
263 test_when_finished "git checkout -f main" &&
264 git checkout -B other b &&
265 git cherry-pick -e rebase-1 &&
266 test "$(git log -1 --pretty=format:%s)" = merge
269 cat > "$HOOK" <<'EOF'
270 #!/bin/sh
271 exit 1
274 test_expect_success 'with failing hook' '
276 test_when_finished "git checkout -f main" &&
277 head=$(git rev-parse HEAD) &&
278 echo "more" >> file &&
279 git add file &&
280 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
284 test_expect_success 'with failing hook (--no-verify)' '
286 test_when_finished "git checkout -f main" &&
287 head=$(git rev-parse HEAD) &&
288 echo "more" >> file &&
289 git add file &&
290 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
294 test_expect_success 'with failing hook (merge)' '
296 test_when_finished "git checkout -f main" &&
297 git checkout -B other HEAD@{1} &&
298 echo "more" >> file &&
299 git add file &&
300 rm -f "$HOOK" &&
301 git commit -m other &&
302 write_script "$HOOK" <<-EOF &&
303 exit 1
305 git checkout - &&
306 test_must_fail git merge --no-ff other
310 test_expect_success 'with failing hook (cherry-pick)' '
311 test_when_finished "git checkout -f main" &&
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
317 test_done