Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='commit-msg hook' |
| 4 | |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 8 | . ./test-lib.sh |
| 9 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 10 | test_expect_success 'with no hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 11 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 12 | echo "foo" > file && |
| 13 | git add file && |
| 14 | git commit -m "first" |
| 15 | |
| 16 | ' |
| 17 | |
| 18 | # set up fake editor for interactive editing |
| 19 | cat > fake-editor <<'EOF' |
| 20 | #!/bin/sh |
| 21 | cp FAKE_MSG "$1" |
| 22 | exit 0 |
| 23 | EOF |
| 24 | chmod +x fake-editor |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 25 | |
| 26 | ## Not using test_set_editor here so we can easily ensure the editor variable |
| 27 | ## is only set for the editor tests |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 28 | FAKE_EDITOR="$(pwd)/fake-editor" |
| 29 | export FAKE_EDITOR |
| 30 | |
| 31 | test_expect_success 'with no hook (editor)' ' |
| 32 | |
| 33 | echo "more foo" >> file && |
| 34 | git add file && |
| 35 | echo "more foo" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 36 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 37 | |
| 38 | ' |
| 39 | |
| 40 | test_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 | |
| 48 | test_expect_success '--no-verify with no hook (editor)' ' |
| 49 | |
| 50 | echo "more bar" > file && |
| 51 | git add file && |
| 52 | echo "more bar" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 53 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 54 | |
| 55 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 56 | |
| 57 | # now install hook that always succeeds |
| 58 | HOOKDIR="$(git rev-parse --git-dir)/hooks" |
| 59 | HOOK="$HOOKDIR/commit-msg" |
| 60 | mkdir -p "$HOOKDIR" |
| 61 | cat > "$HOOK" <<EOF |
| 62 | #!/bin/sh |
| 63 | exit 0 |
| 64 | EOF |
| 65 | chmod +x "$HOOK" |
| 66 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 67 | test_expect_success 'with succeeding hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 68 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 69 | echo "more" >> file && |
| 70 | git add file && |
| 71 | git commit -m "more" |
| 72 | |
| 73 | ' |
| 74 | |
| 75 | test_expect_success 'with succeeding hook (editor)' ' |
| 76 | |
| 77 | echo "more more" >> file && |
| 78 | git add file && |
| 79 | echo "more more" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 80 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 81 | |
| 82 | ' |
| 83 | |
| 84 | test_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 | |
| 92 | test_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 && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 97 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 98 | |
| 99 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 100 | |
| 101 | # now a hook that fails |
| 102 | cat > "$HOOK" <<EOF |
| 103 | #!/bin/sh |
| 104 | exit 1 |
| 105 | EOF |
| 106 | |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 107 | commit_msg_is () { |
| 108 | test "$(git log --pretty=format:%s%b -1)" = "$1" |
| 109 | } |
| 110 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 111 | test_expect_success 'with failing hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 112 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 113 | echo "another" >> file && |
| 114 | git add file && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 115 | test_must_fail git commit -m "another" |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 116 | |
| 117 | ' |
| 118 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 119 | test_expect_success 'with failing hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 120 | |
| 121 | echo "more another" >> file && |
| 122 | git add file && |
| 123 | echo "more another" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 124 | ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit) |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 125 | |
| 126 | ' |
| 127 | |
| 128 | test_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 | |
Alex Riesen | fa21296 | 2021-10-29 15:45:45 +0200 | [diff] [blame] | 136 | test_expect_success '-n followed by --verify with failing hook' ' |
| 137 | |
| 138 | echo "even more" >> file && |
| 139 | git add file && |
| 140 | test_must_fail git commit -n --verify -m "even more" |
| 141 | |
| 142 | ' |
| 143 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 144 | test_expect_success '--no-verify with failing hook (editor)' ' |
| 145 | |
| 146 | echo "more stuff" >> file && |
| 147 | git add file && |
| 148 | echo "more stuff" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 149 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 150 | |
| 151 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 152 | |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 153 | test_expect_success 'merge fails with failing hook' ' |
| 154 | |
| 155 | test_when_finished "git branch -D newbranch" && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 156 | test_when_finished "git checkout -f main" && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 157 | git checkout --orphan newbranch && |
| 158 | : >file2 && |
| 159 | git add file2 && |
| 160 | git commit --no-verify file2 -m in-side-branch && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 161 | test_must_fail git merge --allow-unrelated-histories main && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 162 | commit_msg_is "in-side-branch" # HEAD before merge |
| 163 | |
| 164 | ' |
| 165 | |
| 166 | test_expect_success 'merge bypasses failing hook with --no-verify' ' |
| 167 | |
| 168 | test_when_finished "git branch -D newbranch" && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 169 | test_when_finished "git checkout -f main" && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 170 | git checkout --orphan newbranch && |
Elijah Newren | eddd1a4 | 2018-06-30 18:25:02 -0700 | [diff] [blame] | 171 | git rm -f file && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 172 | : >file2 && |
| 173 | git add file2 && |
| 174 | git commit --no-verify file2 -m in-side-branch && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 175 | git merge --no-verify --allow-unrelated-histories main && |
| 176 | commit_msg_is "Merge branch '\''main'\'' into newbranch" |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 177 | ' |
| 178 | |
| 179 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 180 | chmod -x "$HOOK" |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 181 | test_expect_success POSIXPERM 'with non-executable hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 182 | |
Elijah Newren | eddd1a4 | 2018-06-30 18:25:02 -0700 | [diff] [blame] | 183 | echo "content" >file && |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 184 | git add file && |
| 185 | git commit -m "content" |
| 186 | |
| 187 | ' |
| 188 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 189 | test_expect_success POSIXPERM 'with non-executable hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 190 | |
| 191 | echo "content again" >> file && |
| 192 | git add file && |
| 193 | echo "content again" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 194 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again" |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 195 | |
| 196 | ' |
| 197 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 198 | test_expect_success POSIXPERM '--no-verify with non-executable hook' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 199 | |
| 200 | echo "more content" >> file && |
| 201 | git add file && |
| 202 | git commit --no-verify -m "more content" |
| 203 | |
| 204 | ' |
| 205 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 206 | test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 207 | |
| 208 | echo "even more content" >> file && |
| 209 | git add file && |
| 210 | echo "even more content" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 211 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 212 | |
| 213 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 214 | |
| 215 | # now a hook that edits the commit message |
| 216 | cat > "$HOOK" <<'EOF' |
| 217 | #!/bin/sh |
| 218 | echo "new message" > "$1" |
| 219 | exit 0 |
| 220 | EOF |
| 221 | chmod +x "$HOOK" |
| 222 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 223 | test_expect_success 'hook edits commit message' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 224 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 225 | echo "additional" >> file && |
| 226 | git add file && |
| 227 | git commit -m "additional" && |
| 228 | commit_msg_is "new message" |
| 229 | |
| 230 | ' |
| 231 | |
| 232 | test_expect_success 'hook edits commit message (editor)' ' |
| 233 | |
| 234 | echo "additional content" >> file && |
| 235 | git add file && |
| 236 | echo "additional content" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 237 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 238 | commit_msg_is "new message" |
| 239 | |
| 240 | ' |
| 241 | |
| 242 | test_expect_success "hook doesn't edit commit message" ' |
| 243 | |
| 244 | echo "plus" >> file && |
| 245 | git add file && |
| 246 | git commit --no-verify -m "plus" && |
| 247 | commit_msg_is "plus" |
| 248 | |
| 249 | ' |
| 250 | |
| 251 | test_expect_success "hook doesn't edit commit message (editor)" ' |
| 252 | |
| 253 | echo "more plus" >> file && |
| 254 | git add file && |
| 255 | echo "more plus" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 256 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify && |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 257 | commit_msg_is "more plus" |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 258 | ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 259 | |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 260 | test_expect_success 'hook called in git-merge picks up commit message' ' |
| 261 | test_when_finished "git branch -D newbranch" && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 262 | test_when_finished "git checkout -f main" && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 263 | git checkout --orphan newbranch && |
Elijah Newren | eddd1a4 | 2018-06-30 18:25:02 -0700 | [diff] [blame] | 264 | git rm -f file && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 265 | : >file2 && |
| 266 | git add file2 && |
| 267 | git commit --no-verify file2 -m in-side-branch && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 268 | git merge --allow-unrelated-histories main && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 269 | commit_msg_is "new message" |
| 270 | ' |
| 271 | |
| 272 | test_expect_failure 'merge --continue remembers --no-verify' ' |
| 273 | test_when_finished "git branch -D newbranch" && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 274 | test_when_finished "git checkout -f main" && |
| 275 | git checkout main && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 276 | echo a >file2 && |
| 277 | git add file2 && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 278 | git commit --no-verify -m "add file2 to main" && |
| 279 | git checkout -b newbranch main^ && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 280 | echo b >file2 && |
| 281 | git add file2 && |
| 282 | git commit --no-verify file2 -m in-side-branch && |
Johannes Schindelin | 1e2ae14 | 2020-11-18 23:44:40 +0000 | [diff] [blame] | 283 | git merge --no-verify -m not-rewritten-by-hook main && |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 284 | # resolve conflict: |
| 285 | echo c >file2 && |
| 286 | git add file2 && |
| 287 | git merge --continue && |
| 288 | commit_msg_is not-rewritten-by-hook |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 289 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 290 | |
Johannes Schindelin | cb7fb9e | 2017-03-22 16:01:19 +0100 | [diff] [blame] | 291 | # set up fake editor to replace `pick` by `reword` |
| 292 | cat > reword-editor <<'EOF' |
| 293 | #!/bin/sh |
| 294 | mv "$1" "$1".bup && |
| 295 | sed 's/^pick/reword/' <"$1".bup >"$1" |
| 296 | EOF |
| 297 | chmod +x reword-editor |
| 298 | REWORD_EDITOR="$(pwd)/reword-editor" |
| 299 | export REWORD_EDITOR |
| 300 | |
Johannes Schindelin | b92ff6e | 2017-03-23 17:07:17 +0100 | [diff] [blame] | 301 | test_expect_success 'hook is called for reword during `rebase -i`' ' |
Johannes Schindelin | cb7fb9e | 2017-03-22 16:01:19 +0100 | [diff] [blame] | 302 | |
| 303 | GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ && |
| 304 | commit_msg_is "new message" |
| 305 | |
| 306 | ' |
| 307 | |
Stefan Beller | f8b8635 | 2017-09-07 15:04:29 -0700 | [diff] [blame] | 308 | |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 309 | test_done |