Release 0.19
[stgit.git] / t / t7504-commit-msg-hook.sh
blob8246575ca76420c97941a65d23ae2dca1270a580
1 #!/bin/sh
3 test_description='commit-msg hook'
5 . ./test-lib.sh
7 stg init
9 # set up fake editor for interactive editing
10 cat > fake-editor <<'EOF'
11 #!/bin/sh
12 cp FAKE_MSG "$1"
13 exit 0
14 EOF
15 chmod +x fake-editor
17 ## Not using test_set_editor here so we can easily ensure the editor variable
18 ## is only set for the editor tests
19 FAKE_EDITOR="$(pwd)/fake-editor"
20 export FAKE_EDITOR
22 test_expect_success 'new --no-verify with no hook' '
24 stg new --no-verify -m "bar" new-nv-no-hook
28 test_expect_success 'new --no-verify with no hook (editor)' '
30 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify new-nv-no-hook-edit
34 # now install hook
35 HOOKDIR="$(git rev-parse --git-dir)/hooks"
36 HOOK="$HOOKDIR/commit-msg"
37 mkdir -p "$HOOKDIR"
38 cat > "$HOOK" <<EOF
39 #!/bin/sh
40 exit 0
41 EOF
42 chmod +x "$HOOK"
44 test_expect_success 'new with succeeding hook' '
46 stg new -m "more" more
50 test_expect_success 'edit with succeeding hook' '
52 stg edit -m "mmore" more
56 test_expect_success 'refresh with succeeding hook' '
58 echo "more" >> file &&
59 git add file &&
60 stg refresh -m "mmmore"
64 test_expect_success 'squash with succeeding hook' '
66 stg squash -n more -m "mmmmore" new-nv-no-hook-edit more
70 test_expect_success 'new with succeeding hook (editor)' '
72 echo "more more" > FAKE_MSG &&
73 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new more-more
77 test_expect_success 'edit with succeeding hook (editor)' '
79 echo "mmore more" > FAKE_MSG &&
80 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit more-more
84 test_expect_success 'refresh with succeeding hook (editor)' '
86 echo "more more more" >> file &&
87 echo "more more more" > FAKE_MSG &&
88 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e
92 test_expect_success 'squash with succeeding hook (editor)' '
94 echo "more more" > FAKE_MSG &&
95 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash -n more-more more more-more
99 test_expect_success 'new --no-verify with succeeding hook' '
101 stg new --no-verify -m "even more" even-more
105 test_expect_success 'edit --no-verify with succeeding hook' '
107 stg edit --no-verify -m "even mmore"
111 test_expect_success 'refresh --no-verify with succeeding hook' '
113 echo "even more" >> file &&
114 stg refresh --no-verify -m "even mmmore"
118 test_expect_success 'squash --no-verify with succeeding hook' '
120 stg squash --no-verify -m "even mmmmore" -n e-m more-more even-more
124 test_expect_success 'new --no-verify with succeeding hook (editor)' '
126 echo "even more more" > FAKE_MSG &&
127 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify e-m-m
130 test_expect_success 'edit --no-verify with succeeding hook (editor)' '
132 echo "even mmore more" > FAKE_MSG &&
133 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify
136 test_expect_success 'refresh --no-verify with succeeding hook (editor)' '
138 echo "even more more" >> file &&
139 echo "even mmore mmore" > FAKE_MSG &&
140 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
144 test_expect_success 'squash --no-verify with succeeding hook (editor)' '
146 echo "even more more" > FAKE_MSG &&
147 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify -n mo e-m e-m-m
150 # now a hook that fails
151 cat > "$HOOK" <<EOF
152 #!/bin/sh
153 exit 1
156 test_expect_success 'new with failing hook' '
158 ! stg new -m "another" another
162 test_expect_success 'edit with failing hook' '
164 ! stg edit -m "another"
168 test_expect_success 'refresh with failing hook' '
170 ! stg refresh -m "another" &&
171 stg delete refresh-temp
175 test_expect_success 'squash with failing hook' '
177 ! stg squash -m "another" -n another new-nv-no-hook mo
181 test_expect_success 'new with failing hook (editor)' '
183 echo "more another" > FAKE_MSG &&
184 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new more-another)
188 test_expect_success 'edit with failing hook (editor)' '
190 echo "more another" > FAKE_MSG &&
191 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit)
195 test_expect_success 'refresh with failing hook (editor)' '
197 echo "more another" >> file &&
198 echo "more another" > FAKE_MSG &&
199 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e) &&
200 stg delete refresh-temp
204 test_expect_success 'squash with failing hook (editor)' '
206 echo "more another" > FAKE_MSG &&
207 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash new-nv-no-hook mo)
211 test_expect_success 'new --no-verify with failing hook' '
213 stg new --no-verify -m "stuff" stuff
217 test_expect_success 'edit --no-verify with failing hook' '
219 stg edit --no-verify -m "sstuff" stuff
223 test_expect_success 'refresh --no-verify with failing hook' '
225 echo "stuff" >> file &&
226 stg refresh --no-verify -m "ssstuff"
230 test_expect_success 'squash --no-verify with failing hook' '
232 stg squash --no-verify -m "stuff" -n s mo stuff
236 test_expect_success 'new --no-verify with failing hook (editor)' '
238 echo "more stuff" > FAKE_MSG &&
239 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify m-s
243 test_expect_success 'edit --no-verify with failing hook (editor)' '
245 echo "mmore stuff" > FAKE_MSG &&
246 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify m-s
250 test_expect_success 'refresh --no-verify with failing hook (editor)' '
252 echo "more stuff" >> file &&
253 echo "mmmore stuff" > FAKE_MSG &&
254 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
258 test_expect_success 'squash --no-verify with failing hook (editor)' '
260 echo "more stuff" > FAKE_MSG &&
261 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify -n m-s s m-s
265 chmod -x "$HOOK"
266 test_expect_success 'refresh with non-executable hook' '
268 echo "content" >> file &&
269 stg refresh -m "content"
273 test_expect_success 'refresh with non-executable hook (editor)' '
275 echo "content again" >> file &&
276 echo "content again" > FAKE_MSG &&
277 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e
281 test_expect_success 'refresh --no-verify with non-executable hook' '
283 echo "more content" >> file &&
284 stg refresh --no-verify -m "more content"
288 test_expect_success 'refresh --no-verify with non-executable hook (editor)' '
290 echo "even more content" >> file &&
291 echo "even more content" > FAKE_MSG &&
292 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
296 # now a hook that edits the commit message
297 cat > "$HOOK" <<'EOF'
298 #!/bin/sh
299 echo "new message" > "$1"
300 exit 0
302 chmod +x "$HOOK"
304 commit_msg_is () {
305 test "$(git log --pretty=format:%s%b -1)" = "$1"
308 test_expect_success 'new hook edits commit message' '
310 stg new -m "additional" additional &&
311 commit_msg_is "new message"
315 test_expect_success "new hook doesn't edit commit message" '
317 stg new --no-verify -m "plus" plus &&
318 commit_msg_is "plus"
322 test_expect_success 'new hook edits commit message (editor)' '
324 echo "additional content" > FAKE_MSG &&
325 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new additional-content &&
326 commit_msg_is "new message"
330 test_expect_success "new hook doesn't edit commit message (editor)" '
332 echo "more plus" > FAKE_MSG &&
333 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify more-plus &&
334 commit_msg_is "more plus"
338 test_expect_success 'edit hook edits commit message' '
340 stg edit -m "additional" &&
341 commit_msg_is "new message"
345 test_expect_success "edit hook doesn't edit commit message" '
347 stg edit --no-verify -m "plus" &&
348 commit_msg_is "plus"
352 test_expect_success 'edit hook edits commit message (editor)' '
354 echo "additional content" > FAKE_MSG &&
355 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit &&
356 commit_msg_is "new message"
360 test_expect_success "edit hook doesn't edit commit message (editor)" '
362 echo "more plus" > FAKE_MSG &&
363 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify &&
364 commit_msg_is "more plus"
368 test_expect_success 'refresh hook edits commit message' '
370 echo "additional" >> file &&
371 stg refresh -m "additional" &&
372 commit_msg_is "new message"
376 test_expect_success "refresh hook doesn't edit commit message" '
378 echo "plus" >> file &&
379 stg refresh --no-verify -m "plus" &&
380 commit_msg_is "plus"
384 test_expect_success 'refresh hook edits commit message (editor)' '
386 echo "additional content" >> file &&
387 echo "additional content" > FAKE_MSG &&
388 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e &&
389 commit_msg_is "new message"
393 test_expect_success "refresh hook doesn't edit commit message (editor)" '
395 echo "more plus" >> file &&
396 echo "more plus" > FAKE_MSG &&
397 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify &&
398 commit_msg_is "more plus"
402 test_expect_success 'squash hook edits commit message' '
404 stg squash -m "additional" -n add-plus additional-content more-plus &&
405 commit_msg_is "new message"
409 test_expect_success "squash hook doesn't edit commit message" '
411 stg squash --no-verify -m "plus" -n plusplus plus add-plus &&
412 commit_msg_is "plus"
416 test_expect_success 'squash hook edits commit message (editor)' '
418 echo "additional content" > FAKE_MSG &&
419 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash -n app additional plusplus &&
420 commit_msg_is "new message"
424 test_expect_success "squash hook doesn't edit commit message (editor)" '
426 echo "more plus" > FAKE_MSG &&
427 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify m-s app &&
428 commit_msg_is "more plus"
432 test_done