]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7505-prepare-commit-msg-hook.sh
completion tests: don't add test cruft to the test repository
[thirdparty/git.git] / t / t7505-prepare-commit-msg-hook.sh
CommitLineData
8089c85b
PB
1#!/bin/sh
2
3test_description='prepare-commit-msg hook'
4
5. ./test-lib.sh
6
7test_expect_success 'with no hook' '
8
9 echo "foo" > file &&
10 git add file &&
11 git commit -m "first"
12
13'
14
15# set up fake editor for interactive editing
16cat > fake-editor <<'EOF'
17#!/bin/sh
18exit 0
19EOF
20chmod +x fake-editor
f69e836f
BD
21
22## Not using test_set_editor here so we can easily ensure the editor variable
23## is only set for the editor tests
8089c85b
PB
24FAKE_EDITOR="$(pwd)/fake-editor"
25export FAKE_EDITOR
26
27# now install hook that always succeeds and adds a message
28HOOKDIR="$(git rev-parse --git-dir)/hooks"
29HOOK="$HOOKDIR/prepare-commit-msg"
30mkdir -p "$HOOKDIR"
462f8caf
JK
31echo "#!$SHELL_PATH" > "$HOOK"
32cat >> "$HOOK" <<'EOF'
33
8089c85b 34if test "$2" = commit; then
47a528ad 35 source=$(git rev-parse "$3")
8089c85b
PB
36else
37 source=${2-default}
38fi
39if test "$GIT_EDITOR" = :; then
40 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
41else
42 sed -e "1s/.*/$source/" "$1" > msg.tmp
43fi
44mv msg.tmp "$1"
45exit 0
46EOF
47chmod +x "$HOOK"
48
49echo dummy template > "$(git rev-parse --git-dir)/template"
50
51test_expect_success 'with hook (-m)' '
52
53 echo "more" >> file &&
54 git add file &&
55 git commit -m "more" &&
0c923256 56 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
57
58'
59
60test_expect_success 'with hook (-m editor)' '
61
62 echo "more" >> file &&
63 git add file &&
f69e836f 64 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
0c923256 65 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
66
67'
68
69test_expect_success 'with hook (-t)' '
70
71 echo "more" >> file &&
72 git add file &&
73 git commit -t "$(git rev-parse --git-dir)/template" &&
0c923256 74 test "$(git log -1 --pretty=format:%s)" = template
8089c85b
PB
75
76'
77
78test_expect_success 'with hook (-F)' '
79
80 echo "more" >> file &&
81 git add file &&
82 (echo more | git commit -F -) &&
0c923256 83 test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
8089c85b
PB
84
85'
86
87test_expect_success 'with hook (-F editor)' '
88
89 echo "more" >> file &&
90 git add file &&
f69e836f 91 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
0c923256 92 test "$(git log -1 --pretty=format:%s)" = message
8089c85b
PB
93
94'
95
96test_expect_success 'with hook (-C)' '
97
0c923256 98 head=$(git rev-parse HEAD) &&
8089c85b
PB
99 echo "more" >> file &&
100 git add file &&
101 git commit -C $head &&
0c923256 102 test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
8089c85b
PB
103
104'
105
106test_expect_success 'with hook (editor)' '
107
108 echo "more more" >> file &&
109 git add file &&
f69e836f 110 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
0c923256 111 test "$(git log -1 --pretty=format:%s)" = default
8089c85b
PB
112
113'
114
115test_expect_success 'with hook (--amend)' '
116
0c923256 117 head=$(git rev-parse HEAD) &&
8089c85b
PB
118 echo "more" >> file &&
119 git add file &&
f69e836f 120 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
0c923256 121 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
122
123'
124
125test_expect_success 'with hook (-c)' '
126
0c923256 127 head=$(git rev-parse HEAD) &&
8089c85b
PB
128 echo "more" >> file &&
129 git add file &&
f69e836f 130 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
0c923256 131 test "$(git log -1 --pretty=format:%s)" = "$head"
8089c85b
PB
132
133'
134
65969d43
JS
135test_expect_success 'with hook (merge)' '
136
1fc4f97d
BP
137 test_when_finished "git checkout -f master" &&
138 git checkout -B other HEAD@{1} &&
139 echo "more" >>file &&
140 git add file &&
141 git commit -m other &&
142 git checkout - &&
143 git merge --no-ff other &&
0c923256 144 test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
1fc4f97d
BP
145'
146
147test_expect_success 'with hook and editor (merge)' '
148
149 test_when_finished "git checkout -f master" &&
150 git checkout -B other HEAD@{1} &&
151 echo "more" >>file &&
65969d43
JS
152 git add file &&
153 git commit -m other &&
154 git checkout - &&
1fc4f97d 155 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
0c923256 156 test "$(git log -1 --pretty=format:%s)" = "merge"
65969d43
JS
157'
158
8089c85b
PB
159cat > "$HOOK" <<'EOF'
160#!/bin/sh
161exit 1
162EOF
163
164test_expect_success 'with failing hook' '
165
1fc4f97d 166 test_when_finished "git checkout -f master" &&
0c923256 167 head=$(git rev-parse HEAD) &&
8089c85b
PB
168 echo "more" >> file &&
169 git add file &&
b7ae1414 170 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
8089c85b
PB
171
172'
173
174test_expect_success 'with failing hook (--no-verify)' '
175
1fc4f97d 176 test_when_finished "git checkout -f master" &&
0c923256 177 head=$(git rev-parse HEAD) &&
8089c85b
PB
178 echo "more" >> file &&
179 git add file &&
b7ae1414 180 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
8089c85b
PB
181
182'
183
3e4141d0
AP
184test_expect_success 'with failing hook (merge)' '
185
1fc4f97d 186 test_when_finished "git checkout -f master" &&
3e4141d0
AP
187 git checkout -B other HEAD@{1} &&
188 echo "more" >> file &&
189 git add file &&
190 rm -f "$HOOK" &&
191 git commit -m other &&
3219bad9 192 write_script "$HOOK" <<-EOF &&
3e4141d0
AP
193 exit 1
194 EOF
195 git checkout - &&
1fc4f97d 196 test_must_fail git merge --no-ff other
3e4141d0
AP
197
198'
8089c85b
PB
199
200test_done