Merge commit 'junio/next' into next
[git/platforms/storm.git] / t / t7504-commit-msg-hook.sh
blobf78c2f6568e09cfac1a83b1113f4126716b98d81
1 #!/bin/sh
3 test_description='commit-msg hook'
5 . ./test-lib.sh
7 test_expect_success 'with no hook' '
9 echo "foo" > file &&
10 git add file &&
11 git commit -m "first"
15 # set up fake editor for interactive editing
16 cat > fake-editor <<'EOF'
17 #!/bin/sh
18 cp FAKE_MSG "$1"
19 exit 0
20 EOF
21 chmod +x fake-editor
23 ## Not using test_set_editor here so we can easily ensure the editor variable
24 ## is only set for the editor tests
25 FAKE_EDITOR="$(pwd)/fake-editor"
26 export FAKE_EDITOR
28 test_expect_success 'with no hook (editor)' '
30 echo "more foo" >> file &&
31 git add file &&
32 echo "more foo" > FAKE_MSG &&
33 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
37 test_expect_success '--no-verify with no hook' '
39 echo "bar" > file &&
40 git add file &&
41 git commit --no-verify -m "bar"
45 test_expect_success '--no-verify with no hook (editor)' '
47 echo "more bar" > file &&
48 git add file &&
49 echo "more bar" > FAKE_MSG &&
50 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
54 # now install hook that always succeeds
55 HOOKDIR="$(git rev-parse --git-dir)/hooks"
56 HOOK="$HOOKDIR/commit-msg"
57 mkdir -p "$HOOKDIR"
58 cat > "$HOOK" <<EOF
59 #!/bin/sh
60 exit 0
61 EOF
62 chmod +x "$HOOK"
64 test_expect_success 'with succeeding hook' '
66 echo "more" >> file &&
67 git add file &&
68 git commit -m "more"
72 test_expect_success 'with succeeding hook (editor)' '
74 echo "more more" >> file &&
75 git add file &&
76 echo "more more" > FAKE_MSG &&
77 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
81 test_expect_success '--no-verify with succeeding hook' '
83 echo "even more" >> file &&
84 git add file &&
85 git commit --no-verify -m "even more"
89 test_expect_success '--no-verify with succeeding hook (editor)' '
91 echo "even more more" >> file &&
92 git add file &&
93 echo "even more more" > FAKE_MSG &&
94 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
98 # now a hook that fails
99 cat > "$HOOK" <<EOF
100 #!/bin/sh
101 exit 1
104 test_expect_success 'with failing hook' '
106 echo "another" >> file &&
107 git add file &&
108 ! git commit -m "another"
112 test_expect_success 'with failing hook (editor)' '
114 echo "more another" >> file &&
115 git add file &&
116 echo "more another" > FAKE_MSG &&
117 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
121 test_expect_success '--no-verify with failing hook' '
123 echo "stuff" >> file &&
124 git add file &&
125 git commit --no-verify -m "stuff"
129 test_expect_success '--no-verify with failing hook (editor)' '
131 echo "more stuff" >> file &&
132 git add file &&
133 echo "more stuff" > FAKE_MSG &&
134 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
138 chmod -x "$HOOK"
139 if test "$(git config --bool core.filemode)" = false; then
140 say "executable bit not honored - skipping tests of non-executable hook"
141 else
143 test_expect_success 'with non-executable hook' '
145 echo "content" >> file &&
146 git add file &&
147 git commit -m "content"
151 test_expect_success 'with non-executable hook (editor)' '
153 echo "content again" >> file &&
154 git add file &&
155 echo "content again" > FAKE_MSG &&
156 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
160 test_expect_success '--no-verify with non-executable hook' '
162 echo "more content" >> file &&
163 git add file &&
164 git commit --no-verify -m "more content"
168 test_expect_success '--no-verify with non-executable hook (editor)' '
170 echo "even more content" >> file &&
171 git add file &&
172 echo "even more content" > FAKE_MSG &&
173 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
177 fi # non-executable hooks
179 # now a hook that edits the commit message
180 cat > "$HOOK" <<'EOF'
181 #!/bin/sh
182 echo "new message" > "$1"
183 exit 0
185 chmod +x "$HOOK"
187 commit_msg_is () {
188 test "`git log --pretty=format:%s%b -1`" = "$1"
191 test_expect_success 'hook edits commit message' '
193 echo "additional" >> file &&
194 git add file &&
195 git commit -m "additional" &&
196 commit_msg_is "new message"
200 test_expect_success 'hook edits commit message (editor)' '
202 echo "additional content" >> file &&
203 git add file &&
204 echo "additional content" > FAKE_MSG &&
205 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
206 commit_msg_is "new message"
210 test_expect_success "hook doesn't edit commit message" '
212 echo "plus" >> file &&
213 git add file &&
214 git commit --no-verify -m "plus" &&
215 commit_msg_is "plus"
219 test_expect_success "hook doesn't edit commit message (editor)" '
221 echo "more plus" >> file &&
222 git add file &&
223 echo "more plus" > FAKE_MSG &&
224 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
225 commit_msg_is "more plus"
229 test_done