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