Restore `stg sink --nopush` capability
[stgit.git] / t / t7504-commit-msg-hook.sh
blobd955e3e3ad360232017499f6fefbf441b34ed584
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 write_script fake-editor <<'EOF'
11 cp FAKE_MSG "$1"
12 exit 0
13 EOF
15 ## Not using test_set_editor here so we can easily ensure the editor variable
16 ## is only set for the editor tests
17 FAKE_EDITOR="$(pwd)/fake-editor"
18 export FAKE_EDITOR
20 test_expect_success 'new --no-verify with no hook' '
21 stg new --no-verify -m "bar" new-nv-no-hook
24 test_expect_success 'new --no-verify with no hook (editor)' '
25 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify new-nv-no-hook-edit
28 # now install hook
29 HOOKDIR="$(git rev-parse --git-dir)/hooks"
30 HOOK="$HOOKDIR/commit-msg"
31 mkdir -p "$HOOKDIR"
32 write_script "$HOOK" <<EOF
33 exit 0
34 EOF
36 test_expect_success 'new with succeeding hook' '
37 stg new -m "more" more
40 test_expect_success 'edit with succeeding hook' '
41 stg edit -m "mmore" more
44 test_expect_success 'refresh with succeeding hook' '
45 echo "more" >> file &&
46 git add file &&
47 stg refresh -m "mmmore"
50 test_expect_success 'squash with succeeding hook' '
51 stg squash -n more -m "mmmmore" new-nv-no-hook-edit more
54 test_expect_success 'new with succeeding hook (editor)' '
55 echo "more more" > FAKE_MSG &&
56 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new more-more
59 test_expect_success 'edit with succeeding hook (editor)' '
60 echo "mmore more" > FAKE_MSG &&
61 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit more-more
64 test_expect_success 'refresh with succeeding hook (editor)' '
65 echo "more more more" >> file &&
66 echo "more more more" > FAKE_MSG &&
67 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e
70 test_expect_success 'squash with succeeding hook (editor)' '
71 echo "more more" > FAKE_MSG &&
72 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash -n more-more more more-more
75 test_expect_success 'new --no-verify with succeeding hook' '
76 stg new --no-verify -m "even more" even-more
79 test_expect_success 'edit --no-verify with succeeding hook' '
80 stg edit --no-verify -m "even mmore"
83 test_expect_success 'refresh --no-verify with succeeding hook' '
84 echo "even more" >> file &&
85 stg refresh --no-verify -m "even mmmore"
88 test_expect_success 'squash --no-verify with succeeding hook' '
89 stg squash --no-verify -m "even mmmmore" -n e-m more-more even-more
92 test_expect_success 'new --no-verify with succeeding hook (editor)' '
93 echo "even more more" > FAKE_MSG &&
94 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify e-m-m
97 test_expect_success 'edit --no-verify with succeeding hook (editor)' '
98 echo "even mmore more" > FAKE_MSG &&
99 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify
102 test_expect_success 'refresh --no-verify with succeeding hook (editor)' '
103 echo "even more more" >> file &&
104 echo "even mmore mmore" > FAKE_MSG &&
105 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
108 test_expect_success 'squash --no-verify with succeeding hook (editor)' '
109 echo "even more more" > FAKE_MSG &&
110 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify -n mo e-m e-m-m
113 # now a hook that fails
114 write_script "$HOOK" <<'EOF'
115 exit 1
118 test_expect_success 'new with failing hook' '
119 command_error stg new -m "another" another
122 test_expect_success 'edit with failing hook' '
123 command_error stg edit -m "another"
126 test_expect_success 'edit --diff with failing hook' '
127 command_error stg edit --diff -m "another" 2>&1 |
128 grep -e "The commit-msg hook failed"
131 test_expect_success 'refresh with failing hook' '
132 command_error stg refresh -m "another" &&
133 stg delete refresh-temp
136 test_expect_success 'squash with failing hook' '
137 command_error stg squash -m "another" -n another new-nv-no-hook mo
140 test_expect_success 'new with failing hook (editor)' '
141 echo "more another" > FAKE_MSG &&
142 GIT_EDITOR="\"\$FAKE_EDITOR\"" command_error stg new more-another
145 test_expect_success 'edit with failing hook (editor)' '
146 echo "more another" > FAKE_MSG &&
147 GIT_EDITOR="\"\$FAKE_EDITOR\"" command_error stg edit
150 test_expect_success 'refresh with failing hook (editor)' '
151 echo "more another" >> file &&
152 echo "more another" > FAKE_MSG &&
153 GIT_EDITOR="\"\$FAKE_EDITOR\"" command_error stg refresh -e &&
154 stg delete refresh-temp
157 test_expect_success 'squash with failing hook (editor)' '
158 echo "more another" > FAKE_MSG &&
159 GIT_EDITOR="\"\$FAKE_EDITOR\"" command_error stg squash new-nv-no-hook mo
162 test_expect_success 'new --no-verify with failing hook' '
163 stg new --no-verify -m "stuff" stuff
166 test_expect_success 'edit --no-verify with failing hook' '
167 stg edit --no-verify -m "sstuff" stuff
170 test_expect_success 'refresh --no-verify with failing hook' '
171 echo "stuff" >> file &&
172 stg refresh --no-verify -m "ssstuff"
175 test_expect_success 'squash --no-verify with failing hook' '
176 stg squash --no-verify -m "stuff" -n s mo stuff
179 test_expect_success 'new --no-verify with failing hook (editor)' '
180 echo "more stuff" > FAKE_MSG &&
181 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify m-s
184 test_expect_success 'edit --no-verify with failing hook (editor)' '
185 echo "mmore stuff" > FAKE_MSG &&
186 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify m-s
189 test_expect_success 'refresh --no-verify with failing hook (editor)' '
190 echo "more stuff" >> file &&
191 echo "mmmore stuff" > FAKE_MSG &&
192 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
195 test_expect_success 'squash --no-verify with failing hook (editor)' '
196 echo "more stuff" > FAKE_MSG &&
197 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify -n m-s s m-s
200 chmod -x "$HOOK"
201 test_expect_success 'refresh with non-executable hook' '
202 echo "content" >> file &&
203 stg refresh -m "content"
206 test_expect_success 'refresh with non-executable hook (editor)' '
207 echo "content again" >> file &&
208 echo "content again" > FAKE_MSG &&
209 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e
212 test_expect_success 'refresh --no-verify with non-executable hook' '
213 echo "more content" >> file &&
214 stg refresh --no-verify -m "more content"
217 test_expect_success 'refresh --no-verify with non-executable hook (editor)' '
218 echo "even more content" >> file &&
219 echo "even more content" > FAKE_MSG &&
220 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify
223 # now a hook that edits the commit message
224 write_script "$HOOK" <<'EOF'
225 echo "new message" > "$1"
226 exit 0
229 commit_msg_is () {
230 test "$(git log --pretty=format:%s%b -1)" = "$1"
233 test_expect_success 'new hook edits commit message' '
234 stg new -m "additional" additional &&
235 commit_msg_is "new message"
238 test_expect_success "new hook doesn't edit commit message" '
239 stg new --no-verify -m "plus" plus &&
240 commit_msg_is "plus"
243 test_expect_success 'new hook edits commit message (editor)' '
244 echo "additional content" > FAKE_MSG &&
245 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new additional-content &&
246 commit_msg_is "new message"
249 test_expect_success "new hook doesn't edit commit message (editor)" '
250 echo "more plus" > FAKE_MSG &&
251 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg new --no-verify more-plus &&
252 commit_msg_is "more plus"
255 test_expect_success 'edit hook edits commit message' '
256 stg edit -m "additional" &&
257 commit_msg_is "new message"
260 test_expect_success "edit hook doesn't edit commit message" '
261 stg edit --no-verify -m "plus" &&
262 commit_msg_is "plus"
265 test_expect_success 'edit hook edits commit message (editor)' '
266 echo "additional content" > FAKE_MSG &&
267 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit &&
268 commit_msg_is "new message"
271 test_expect_success "edit hook doesn't edit commit message (editor)" '
272 echo "more plus" > FAKE_MSG &&
273 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg edit --no-verify &&
274 commit_msg_is "more plus"
277 test_expect_success 'refresh hook edits commit message' '
278 echo "additional" >> file &&
279 stg refresh -m "additional" &&
280 commit_msg_is "new message"
283 test_expect_success "refresh hook doesn't edit commit message" '
284 echo "plus" >> file &&
285 stg refresh --no-verify -m "plus" &&
286 commit_msg_is "plus"
289 test_expect_success 'refresh hook edits commit message (editor)' '
290 echo "additional content" >> file &&
291 echo "additional content" > FAKE_MSG &&
292 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e &&
293 commit_msg_is "new message"
296 test_expect_success "refresh hook doesn't edit commit message (editor)" '
297 echo "more plus" >> file &&
298 echo "more plus" > FAKE_MSG &&
299 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg refresh -e --no-verify &&
300 commit_msg_is "more plus"
303 test_expect_success 'squash hook edits commit message' '
304 stg squash -m "additional" -n add-plus additional-content more-plus &&
305 commit_msg_is "new message"
308 test_expect_success "squash hook doesn't edit commit message" '
309 stg squash --no-verify -m "plus" -n plusplus plus add-plus &&
310 commit_msg_is "plus"
313 test_expect_success 'squash hook edits commit message (editor)' '
314 echo "additional content" > FAKE_MSG &&
315 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash -n app additional plusplus &&
316 commit_msg_is "new message"
319 test_expect_success "squash hook doesn't edit commit message (editor)" '
320 echo "more plus" > FAKE_MSG &&
321 GIT_EDITOR="\"\$FAKE_EDITOR\"" stg squash --no-verify m-s app &&
322 commit_msg_is "more plus"
325 write_script diffedit <<EOF
326 sed \
327 -e 's/old content/new content/' \
328 -e 's/old message/new message/' "\$1" > "\$1".tmp && mv "\$1".tmp "\$1"
329 exit 0
331 test_expect_success "refresh hook edits message and diff (editor)" '
332 stg new -m "old message" p1 &&
333 echo "old content" > file &&
334 stg add file &&
335 stg refresh -i &&
336 GIT_EDITOR=./diffedit stg refresh -d -e &&
337 git log --pretty=format:%s%b -1 &&
338 commit_msg_is "new message" &&
339 test "$(grep "new content" file)" = "new content"
341 rm -f diffedit
343 test_done