]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7504-commit-msg-hook.sh
Merge branch 'es/maintenance-of-bare-repositories'
[thirdparty/git.git] / t / t7504-commit-msg-hook.sh
CommitLineData
264474f2
WC
1#!/bin/sh
2
3test_description='commit-msg hook'
4
1e2ae142 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
264474f2
WC
8. ./test-lib.sh
9
80f86605 10test_expect_success 'with no hook' '
264474f2 11
80f86605
WC
12 echo "foo" > file &&
13 git add file &&
14 git commit -m "first"
15
16'
17
18# set up fake editor for interactive editing
19cat > fake-editor <<'EOF'
20#!/bin/sh
21cp FAKE_MSG "$1"
22exit 0
23EOF
24chmod +x fake-editor
f69e836f
BD
25
26## Not using test_set_editor here so we can easily ensure the editor variable
27## is only set for the editor tests
80f86605
WC
28FAKE_EDITOR="$(pwd)/fake-editor"
29export FAKE_EDITOR
30
31test_expect_success 'with no hook (editor)' '
32
33 echo "more foo" >> file &&
34 git add file &&
35 echo "more foo" > FAKE_MSG &&
f69e836f 36 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
80f86605
WC
37
38'
39
40test_expect_success '--no-verify with no hook' '
41
42 echo "bar" > file &&
43 git add file &&
44 git commit --no-verify -m "bar"
45
46'
47
48test_expect_success '--no-verify with no hook (editor)' '
49
50 echo "more bar" > file &&
51 git add file &&
52 echo "more bar" > FAKE_MSG &&
f69e836f 53 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
80f86605
WC
54
55'
264474f2
WC
56
57# now install hook that always succeeds
58HOOKDIR="$(git rev-parse --git-dir)/hooks"
59HOOK="$HOOKDIR/commit-msg"
60mkdir -p "$HOOKDIR"
61cat > "$HOOK" <<EOF
62#!/bin/sh
63exit 0
64EOF
65chmod +x "$HOOK"
66
80f86605 67test_expect_success 'with succeeding hook' '
264474f2 68
80f86605
WC
69 echo "more" >> file &&
70 git add file &&
71 git commit -m "more"
72
73'
74
75test_expect_success 'with succeeding hook (editor)' '
76
77 echo "more more" >> file &&
78 git add file &&
79 echo "more more" > FAKE_MSG &&
f69e836f 80 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
80f86605
WC
81
82'
83
84test_expect_success '--no-verify with succeeding hook' '
85
86 echo "even more" >> file &&
87 git add file &&
88 git commit --no-verify -m "even more"
89
90'
91
92test_expect_success '--no-verify with succeeding hook (editor)' '
93
94 echo "even more more" >> file &&
95 git add file &&
96 echo "even more more" > FAKE_MSG &&
f69e836f 97 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
80f86605
WC
98
99'
264474f2
WC
100
101# now a hook that fails
102cat > "$HOOK" <<EOF
103#!/bin/sh
104exit 1
105EOF
106
f8b86359
SB
107commit_msg_is () {
108 test "$(git log --pretty=format:%s%b -1)" = "$1"
109}
110
41ac414e 111test_expect_success 'with failing hook' '
80f86605
WC
112
113 echo "another" >> file &&
114 git add file &&
d492b31c 115 test_must_fail git commit -m "another"
80f86605
WC
116
117'
118
41ac414e 119test_expect_success 'with failing hook (editor)' '
80f86605
WC
120
121 echo "more another" >> file &&
122 git add file &&
123 echo "more another" > FAKE_MSG &&
f69e836f 124 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
264474f2 125
80f86605
WC
126'
127
128test_expect_success '--no-verify with failing hook' '
129
130 echo "stuff" >> file &&
131 git add file &&
132 git commit --no-verify -m "stuff"
133
134'
135
136test_expect_success '--no-verify with failing hook (editor)' '
137
138 echo "more stuff" >> file &&
139 git add file &&
140 echo "more stuff" > FAKE_MSG &&
f69e836f 141 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
80f86605
WC
142
143'
264474f2 144
f8b86359
SB
145test_expect_success 'merge fails with failing hook' '
146
147 test_when_finished "git branch -D newbranch" &&
1e2ae142 148 test_when_finished "git checkout -f main" &&
f8b86359
SB
149 git checkout --orphan newbranch &&
150 : >file2 &&
151 git add file2 &&
152 git commit --no-verify file2 -m in-side-branch &&
1e2ae142 153 test_must_fail git merge --allow-unrelated-histories main &&
f8b86359
SB
154 commit_msg_is "in-side-branch" # HEAD before merge
155
156'
157
158test_expect_success 'merge bypasses failing hook with --no-verify' '
159
160 test_when_finished "git branch -D newbranch" &&
1e2ae142 161 test_when_finished "git checkout -f main" &&
f8b86359 162 git checkout --orphan newbranch &&
eddd1a41 163 git rm -f file &&
f8b86359
SB
164 : >file2 &&
165 git add file2 &&
166 git commit --no-verify file2 -m in-side-branch &&
1e2ae142
JS
167 git merge --no-verify --allow-unrelated-histories main &&
168 commit_msg_is "Merge branch '\''main'\'' into newbranch"
f8b86359
SB
169'
170
171
264474f2 172chmod -x "$HOOK"
ee9fb68c 173test_expect_success POSIXPERM 'with non-executable hook' '
80f86605 174
eddd1a41 175 echo "content" >file &&
80f86605
WC
176 git add file &&
177 git commit -m "content"
178
179'
180
ee9fb68c 181test_expect_success POSIXPERM 'with non-executable hook (editor)' '
80f86605
WC
182
183 echo "content again" >> file &&
184 git add file &&
185 echo "content again" > FAKE_MSG &&
f69e836f 186 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
80f86605
WC
187
188'
189
ee9fb68c 190test_expect_success POSIXPERM '--no-verify with non-executable hook' '
80f86605
WC
191
192 echo "more content" >> file &&
193 git add file &&
194 git commit --no-verify -m "more content"
195
196'
264474f2 197
ee9fb68c 198test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
80f86605
WC
199
200 echo "even more content" >> file &&
201 git add file &&
202 echo "even more content" > FAKE_MSG &&
f69e836f 203 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
80f86605
WC
204
205'
264474f2
WC
206
207# now a hook that edits the commit message
208cat > "$HOOK" <<'EOF'
209#!/bin/sh
210echo "new message" > "$1"
211exit 0
212EOF
213chmod +x "$HOOK"
214
80f86605
WC
215test_expect_success 'hook edits commit message' '
216
217 echo "additional" >> file &&
218 git add file &&
219 git commit -m "additional" &&
220 commit_msg_is "new message"
221
222'
223
224test_expect_success 'hook edits commit message (editor)' '
225
226 echo "additional content" >> file &&
227 git add file &&
228 echo "additional content" > FAKE_MSG &&
f69e836f 229 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
80f86605
WC
230 commit_msg_is "new message"
231
232'
233
234test_expect_success "hook doesn't edit commit message" '
235
236 echo "plus" >> file &&
237 git add file &&
238 git commit --no-verify -m "plus" &&
239 commit_msg_is "plus"
240
241'
242
243test_expect_success "hook doesn't edit commit message (editor)" '
244
245 echo "more plus" >> file &&
246 git add file &&
247 echo "more plus" > FAKE_MSG &&
f69e836f 248 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
80f86605 249 commit_msg_is "more plus"
f8b86359 250'
80f86605 251
f8b86359
SB
252test_expect_success 'hook called in git-merge picks up commit message' '
253 test_when_finished "git branch -D newbranch" &&
1e2ae142 254 test_when_finished "git checkout -f main" &&
f8b86359 255 git checkout --orphan newbranch &&
eddd1a41 256 git rm -f file &&
f8b86359
SB
257 : >file2 &&
258 git add file2 &&
259 git commit --no-verify file2 -m in-side-branch &&
1e2ae142 260 git merge --allow-unrelated-histories main &&
f8b86359
SB
261 commit_msg_is "new message"
262'
263
264test_expect_failure 'merge --continue remembers --no-verify' '
265 test_when_finished "git branch -D newbranch" &&
1e2ae142
JS
266 test_when_finished "git checkout -f main" &&
267 git checkout main &&
f8b86359
SB
268 echo a >file2 &&
269 git add file2 &&
1e2ae142
JS
270 git commit --no-verify -m "add file2 to main" &&
271 git checkout -b newbranch main^ &&
f8b86359
SB
272 echo b >file2 &&
273 git add file2 &&
274 git commit --no-verify file2 -m in-side-branch &&
1e2ae142 275 git merge --no-verify -m not-rewritten-by-hook main &&
f8b86359
SB
276 # resolve conflict:
277 echo c >file2 &&
278 git add file2 &&
279 git merge --continue &&
280 commit_msg_is not-rewritten-by-hook
80f86605 281'
264474f2 282
cb7fb9ed
JS
283# set up fake editor to replace `pick` by `reword`
284cat > reword-editor <<'EOF'
285#!/bin/sh
286mv "$1" "$1".bup &&
287sed 's/^pick/reword/' <"$1".bup >"$1"
288EOF
289chmod +x reword-editor
290REWORD_EDITOR="$(pwd)/reword-editor"
291export REWORD_EDITOR
292
b92ff6e8 293test_expect_success 'hook is called for reword during `rebase -i`' '
cb7fb9ed
JS
294
295 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
296 commit_msg_is "new message"
297
298'
299
f8b86359 300
264474f2 301test_done