git的提交规范

Posted by pq1949 on 2018-08-14

参考这个文章

Commit message 和 Change log 编写指南 http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html

npm install -g commitizen
commitizen init cz-conventional-changelog --save --save-exact

validate-commit-msg 这个包作者已经不维护了,推荐用这个commitlint替换
https://github.com/marionebl/commitlint#readme

npm install --save-dev @commitlint/config-conventional @commitlint/cli
npm install husky --save-dev
# Configure commitlint to use angular config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js


{
"scripts": {
"commitmsg": "commitlint -E GIT_PARAMS"
}
}

上面这个配置不好使,GIT_PARAMS 总是找不到,只能用 -e 这个配置了 对应的commit文件日志取的是 .git/COMMIT_EDITMSG
https://github.com/typicode/husky/issues/291 这个地方做了说明

looking at git documentation from 1.9.0 to 2.16.0, it reads:
pre-commit
This hook is invoked by git commit, and can be bypassed with –no-verify option. It takes no parameter
Reading this I don’t understand how using $* is supposed to work in pre-commit. However, another hook called commit-msg reads:
commit-msg
This hook is invoked by git commit and git merge, and can be bypassed with the –no-verify option. It takes a single parameter, the name of the file that holds the proposed commit log message.

配置script时 在commit-msg这个钩子中进行配置, 要是在pre-commit中配置的话 .git/COMMIT_EDITMSG 总是取的前一个消息,所以一旦commit的message填错 就没法正常进行校验了

最终的配置可参考

{
"scripts": {
"test": "mocha **/**.spec.js",
"test_server": "node test/server.js",
"commit": "git cz",
"commitmsg": "commitlint -e",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"prepublishOnly": "gulp version"
},
"husky": {
"hooks": {
"commit-msg": "npm run commitmsg",
"pre-push": "npm run commitmsg"
}
}
}

npm run commit 即可走完整个流程