Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t7504-commit-msg-hook.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t7504-commit-msg-hook.sh` 310 lines ยท 6911 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='commit-msg hook' 4 5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 7 8 . ./test-lib.sh 9 10 test_expect_success 'with no hook' ' 11 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 25 26 ## Not using test_set_editor here so we can easily ensure the editor variable 27 ## is only set for the editor tests 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 && 36 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit 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 && 53 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify 54 55 ' 56 57 test_expect_success 'setup: commit-msg hook that always succeeds' ' 58 test_hook --setup commit-msg <<-\EOF 59 exit 0 60 EOF 61 ' 62 63 test_expect_success 'with succeeding hook' ' 64 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 && 76 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit 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 && 93 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify 94 95 ' 96 97 test_expect_success 'setup: commit-msg hook that always fails' ' 98 test_hook --clobber commit-msg <<-\EOF 99 exit 1 100 EOF 101 ' 102 103 commit_msg_is () { 104 printf "%s" "$1" >expect && 105 git log --pretty=format:%s%b -1 >actual && 106 test_cmp expect actual 107 } 108 109 test_expect_success 'with failing hook' ' 110 111 echo "another" >> file && 112 git add file && 113 test_must_fail git commit -m "another" 114 115 ' 116 117 test_expect_success 'with failing hook (editor)' ' 118 119 echo "more another" >> file && 120 git add file && 121 echo "more another" > FAKE_MSG && 122 ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit) 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 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 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 && 147 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify 148 149 ' 150 151 test_expect_success 'merge fails with failing hook' ' 152 153 test_when_finished "git branch -D newbranch" && 154 test_when_finished "git checkout -f main" && 155 git checkout --orphan newbranch && 156 : >file2 && 157 git add file2 && 158 git commit --no-verify file2 -m in-side-branch && 159 test_must_fail git merge --allow-unrelated-histories main && 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" && 167 test_when_finished "git checkout -f main" && 168 git checkout --orphan newbranch && 169 git rm -f file && 170 : >file2 && 171 git add file2 && 172 git commit --no-verify file2 -m in-side-branch && 173 git merge --no-verify --allow-unrelated-histories main && 174 commit_msg_is "Merge branch '\''main'\'' into newbranch" 175 ' 176 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 ' 181 182 183 test_expect_success POSIXPERM 'with non-executable hook' ' 184 185 echo "content" >file && 186 git add file && 187 git commit -m "content" 188 189 ' 190 191 test_expect_success POSIXPERM 'with non-executable hook (editor)' ' 192 193 echo "content again" >> file && 194 git add file && 195 echo "content again" > FAKE_MSG && 196 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again" 197 198 ' 199 200 test_expect_success POSIXPERM '--no-verify with non-executable hook' ' 201 202 echo "more content" >> file && 203 git add file && 204 git commit --no-verify -m "more content" 205 206 ' 207 208 test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' ' 209 210 echo "even more content" >> file && 211 git add file && 212 echo "even more content" > FAKE_MSG && 213 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify 214 215 ' 216 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 ' 223 224 test_expect_success 'hook edits commit message' ' 225 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 && 238 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && 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 && 257 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify && 258 commit_msg_is "more plus" 259 ' 260 261 test_expect_success 'hook called in git-merge picks up commit message' ' 262 test_when_finished "git branch -D newbranch" && 263 test_when_finished "git checkout -f main" && 264 git checkout --orphan newbranch && 265 git rm -f file && 266 : >file2 && 267 git add file2 && 268 git commit --no-verify file2 -m in-side-branch && 269 git merge --allow-unrelated-histories main && 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" && 275 test_when_finished "git checkout -f main" && 276 git checkout main && 277 echo a >file2 && 278 git add file2 && 279 git commit --no-verify -m "add file2 to main" && 280 git checkout -b newbranch main^ && 281 echo b >file2 && 282 git add file2 && 283 git commit --no-verify file2 -m in-side-branch && 284 git merge --no-verify -m not-rewritten-by-hook main && 285 # resolve conflict: 286 echo c >file2 && 287 git add file2 && 288 git merge --continue && 289 commit_msg_is not-rewritten-by-hook 290 ' 291 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 302 test_expect_success 'hook is called for reword during `rebase -i`' ' 303 304 GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ && 305 commit_msg_is "new message" 306 307 ' 308 309 310 test_done