git commit 代码提交规范 朱雀 2022-12-27 06:26 128阅读 0赞 我们在代码提交时 git 的 commit 信息是很重要的,但是每次写的时候总想偷懒怎么办,咱们可以制定个规范必须按照某种格式提交注释。 ### 法一:设置 git commit 模板 ### 在项目中建立 .git\_template 文件,内容可以自定义: .git\_template type: description: 运行如下命令: git config commit.template .git\_template ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70][] git commit 时会出现上面的模板 这种方法只是规范了一些,但是没有校验方式,不用模板的方式也能提交。 ### 法二:使用 [commitlint][] 和 husky 插件 ### npm install --save-dev @commitlint/config-conventional @commitlint/cli husky 生成配置文件commitlint.config.js module.exports = {extends: ['@commitlint/config-conventional']} 可用下面命令生成,但是生成的文件可能不是utf8格式的,会有问题 echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js 在 package.json 里加入下面配置 { ......, "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, ...... } 目前公开的配置如下 : ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 1][] 我们用的是 [@commitlint/config-conventional][commitlint_config-conventional] 提交错误的 commit 时,报错: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 2][] 正确的 commit: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 3][] 可以和法一联合使用,同时提供一个 sequlize 的 commitlint.config.js 文件以供参考: 'use strict'; const message = process.env['HUSKY_GIT_PARAMS']; const fs = require('fs'); const types = ['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'release', 'revert', 'style', 'test']; const scopes = ['showcase', 'release', 'packaging', 'changelog', 'schematics', 'module:*']; function parseMessage(message) { const PATTERN = /^(\w+)(?:\(([^)]+)\))?\: (.+)$/; const match = PATTERN.exec(message); if (!match) { return null; } return { type: match[1] || null, scope: match[2] || null }; } function getScopesRule() { const messages = fs.readFileSync(message, { encoding: 'utf-8' }); const parsed = parseMessage(messages.split('\n')[0]); if (!parsed) { return [2, 'always', scopes]; } const { scope, type } = parsed; if (scope && !scopes.includes(scope) && type !== 'release' && !/module:.+/.test(scope)) { return [2, 'always', scopes]; } else { return [2, 'always', []]; } } module.exports = { extends: ['@commitlint/config-angular'], rules: { 'type-enum': [2, 'always', types], 'scope-enum': getScopesRule } }; [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70]: /images/20221120/f9067fa5a28a4f94b796d5ea0ff78d8a.png [commitlint]: https://github.com/conventional-changelog/commitlint#readme [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 1]: /images/20221120/d198f6915fca448e9eccae4c2099f409.png [commitlint_config-conventional]: https://github.com/conventional-changelog/commitlint/tree/master/@commitlint/config-conventional [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 2]: /images/20221120/86cd2fdee3c0412fb1be4f1833518e29.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODg4MzMzOA_size_16_color_FFFFFF_t_70 3]: /images/20221120/73b8a31d49244b9cac4e81a00959564c.png
还没有评论,来说两句吧...