当前位置:   article > 正文

Git:Commit Message 规范和代码格式校验_allowbreakingchanges

allowbreakingchanges

Commit Message 规范

以 angular 规范为例,格式如下:

  1. type(scope): subject # head 必填,其中 type 和 subject 必填。
  2. # 空行
  3. 72-character wrapped. # body 选填。
  4. # 空行
  5. BREAKING CHANGE: msg. # footer 选填。

其中:

head 部分

type (只允许下列7个标识):

  • feat:新功能(feature)

  • fix:修补bug

  • docs:文档(documentation)

  • style: 格式(不影响代码运行的变动)

  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)

  • test:增加测试

  • chore:构建过程或辅助工具的变动

注意:feat 和 fix 类型的 commit 将出现在 Change log 中。

scope(选填):

此次提交的影响范围,如数据层、控制层、视图层等,多个可以用 * 代替,必须在type 之后、小括号之内。

subject:

此次提交的简要描述,必须在 type 之后的冒号之后的一个空格之后,结尾没有句号。

body 部分(选填)

此次提交的详细描述,应包含变动描述、变动理由等内容,使用第一人称现在时描述。

必须和 head 部分间隔一个空行,每超过72的字符必须换一行。

foot 部分(选填)

包含两种情况:1.当前代码不兼容上一个版本,BREAKING CHANGE冒号空格,后跟变动描述、变动理由和迁移方法;2.关闭 Issue,Closes #234 。

特殊情况:当前 commit 用于撤销之前的 commit,revert冒号空格,后跟要撤销的 commit 的 head。

Commit Message 规范约束工具

在项目中安装 commitizen :

npm i -D commitizen cz-conventional-changelog

在项目根目录创建 .czrc 文件:

{ "path": "cz-conventional-changelog" }

修改项目中 package.json 文件:

  1. "script":{
  2. "commit":"git-cz",
  3. },
  4. "config":{
  5. "commitizen":{
  6. "path":"node_modules/cz-conventional-changelog",
  7. },
  8. },

此时,在项目中使用 git cz 或 npm run commit 命令可以代替 git commit 命令,在提交时会自动带出 angular 规范的 commit message 编辑选项,下面将使用 cz-customizable 实现自定义规范。

在项目中安装 cz-customizable

npm i -D cz-customizable

修改项目中 .czrc 文件:

{ "path": "cz-customizable" }

修改项目中 package.json 文件:

  1. "script":{
  2. "commit":"git-cz",
  3. },
  4. "config":{
  5. "commitizen":{
  6. "path":"node_modules/cz-customizable",
  7. },
  8. },

在项目根目录创建 .cz-config.js 文件,配置项如下:

  • type: {Array of Object}:项目中使用的 type 和默认描述。
  • scopes: {Array of Strings}:预设项目中使用的可选 scope 。如:在一个银行系统项目中使用 [“acccounts”, “payments”];在一个旅行应用中使用 [“bookings”, “search”, “profile”]。
  • scopeOverrides: {Object where key contains a Array of String}:当您想重写特定提交类型的作用域时,使用此方法。如:在类型为“fix”时指定范围 { fix: [ {name: 'merge'}, {name: 'style'}, {name: 'e2eTest'},{name: 'unitTest'} ] }。
  • allowCustomScopes: {boolean, default false}:增加自定义 scope 选项,开启可以在设置 scope 时支持直接输入。
  • allowBreakingChanges: {Array of Strings: default none}:配置想要 breaking change 弹出提示的scope列表,如:[‘feat’, ‘fix’]。
  • appendBranchNameToCommitMessage:当配合 cz-customizable-ghooks 使用 cz-customizable 时, 可自动获取分支名称并添加到 commit message 中,此功能已经在 cz-customizable-ghooks实现,对应选项已经被添加到 cz-customizable-ghooks, v1.3.0. 中,默认值为 true。
  • breakingPrefix: {string, default ‘BREAKING CHANGE:’}:设置自定义 breaking change 块。
  • footerPrefix: {string, default ‘ISSUES CLOSED:’}:设置自定义 foot 块。

下面是是一个示例,具体可以参考项目根目录下node_modules下cz-customizable下cz-config-EXAMPLE.js文件:

  1. module.exports = {
  2. types: [
  3. {value: 'feat', name: 'feat: A new feature'},
  4. {value: 'fix', name: 'fix: A bug fix'}
  5. ],
  6. scopes: [
  7. {name: 'accounts'},
  8. {name: 'admin'}
  9. ],
  10. messages: {
  11. type: 'Select the type of change that you\'re committing:',
  12. scope: '\nDenote the SCOPE of this change (optional):',
  13. customScope: 'Denote the SCOPE of this change:',
  14. // used if allowCustomScopes is true
  15. },
  16. allowCustomScopes: true,
  17. allowBreakingChanges: ['feat', 'fix']
  18. }

此时,在使用 commit 命令会自动带出 自定义 规范的 commit message 编辑选项。

Commit Message 校验

在项目中安装 commitlint/cli

npm i -D @commitlint/cli @commitlint/config-conventional

在项目根目录创建 commitlint.config.js 文件:

module.exports = {extends: ['@commitlint/config-conventional']}

此时,commitlint 默认按照angular 规范对 commit message 校验,要校验自定义规范可以通过 rules 参数来实现。

  1. module.exports = {
  2. extends: ['@commitlint/config-conventional'],
  3. rules: {
  4. 'type-enum': [2, 'always',
  5. ["feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"]
  6. ],
  7. 'subject-full-stop': [0, 'never'],
  8. 'subject-case': [0, 'never']
  9. }
  10. }

由于前面使用了 cz 来自定义 commit message 规范,下面将实现根据 cz 规范进行校验。

在项目中安装 commitlint-config-cz :

npm i -D commitlint-config-cz @commitlint/cli

修改项目中 commitlint.config.js 文件:

module.exports = {extends: ['cz'], rules:{}}

此时 commitlint 虽然可以校验之前指定的 commit message 规范,但是没有关联到 commit 指令上,下面将实现在提交代码时按照规范约束并自动校验。

在项目中安装 husky :

npm i -D husky

修改项目 package.json 文件:

  1. "husky": {
  2. "hooks": { "commit-msg":"commitlint -e $GIT_PARAMS" }
  3. },

此时,在提交代码时将按照之前自定义的规范约束 commit message 并自动按照对应规范进行校验,下面将实现自动生成 Change log(需要符合 angular 规范)。

在项目中安装 conventional-changelog-cli

npm i -D conventional-changelog-cli

输出 Change log

conventional-changelog -p angular -i CHANGELOG.md -s

根据实际需要,可以将输出 Change log 命令,加入项目中 package.json 文件:

  1. "script":{
  2. "commit":"git-cz",
  3. "changelog":"conventional-changelog -p angular -i CHANGELOG.md -s",
  4. }

代码格式校验

在项目中安装eslint:

npm i -D eslint

在项目中创建 .eslintrc.json / .eslintrc / .eslintrc.js (任一即可,非 json 文件需要导出) ,详细配置参考 Eslint 规则详解,下面是示例代码:

  1. { // 务必删除注释
  2. "env": { // 环境变量
  3. "node": true, // brower、node、es6、mocha等
  4. "es6": true
  5. },
  6. "globals": { // 全局变量
  7. "vue": true, // "$"、"wx"、"ng"等
  8. },
  9. "parserOptions": { // 格式配置
  10. "ecmaVersion": 6, //es 版本,默认5,可选3、6(2015)、7(2016)等
  11. "ecmaFeatures": { // 附加语言特征,globalReturn、impliedStrict、jsx
  12. "globalReturn": true,
  13. "jsx": true
  14. }
  15. },
  16. "rules": { // 规则配置,0或'off':关闭规则;1或'warn':打开规则,且作为警告(检查通过);2或'error':打开规则,且作为错误(退出码为1,检查不通过)。
  17. "camelcase": 2,
  18. "curly": 2,
  19. "brace-style": [2, "1tbs"],
  20. "quotes": [2, "single"],
  21. "semi": [2, "always"],
  22. "space-in-brackets": [2, "never"],
  23. "space-infix-ops": 2
  24. }
  25. }

除了手动设置规范还可以使用目前 eslint 三种(googlestandardairbnb)流行规范,并在其基础上进一步自定义,安装方式如下:

  1. npm i eslint-config-google -D
  2. npm i eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard -D
  3. npm i eslint-config-airbnb eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react -D

修改项目中 package.json 文件:

  1. "husky": {
  2. "hooks": {
  3. "commit-msg": "commitlint -e $GIT_PARAMS",
  4. "pre-commit" : "eslint ./app/**/*.js --fix"
  5. }
  6. },

配置好eslint 规则之后,在提交时会自动带出之前的自定义约束,输入完成后自动校验 commit message ,并自动校验代码规范,只有全部通过才能提交成功,否则退出 commit ,同时还可以使用指令输出 change log 文档。

如果在项目中使用了 typescript,则可以继续添加响应校验:

在项目中安装tslint:

npm i -D tslint typescript

在项目中创建 tslint.json  ,详细配置参考 TSLlint Rules,下面是示例代码:

  1. {
  2. "lintOptions": {
  3. "typeCheck": true
  4. },
  5. "extends": [
  6. "tslint:recommended"
  7. ],
  8. "rules": {
  9. "no-constant-condition": true,
  10. "file-header": [
  11. true,
  12. "Author \\w{2,20}\nCopyright 20\\d{2} Qietv"
  13. ],
  14. "max-line-length": [
  15. true,
  16. 120
  17. ]
  18. }
  19. }

除了手动设置规范也可以使用诸如 tslint-config-standard 等规范,并在其基础上进一步自定义,安装方式如下:

  1. npm i tslint-config-standard -D
  2. npm i tslint-eslint-rules -D

修改项目中 tslint.json 文件

  1. {
  2. "lintOptions": {
  3. "typeCheck": true
  4. },
  5. "extends": [
  6. "tslint:recommended",
  7. "tslint-config-standard"
  8. ],
  9. "rulesDirectory": [
  10. "node_modules/tslint-eslint-rules/dist/rules"
  11. ],
  12. "rules": {
  13. "no-constant-condition": true,
  14. "file-header": [
  15. true,
  16. "Author \\w{2,20}\nCopyright 20\\d{2} Qietv"
  17. ],
  18. "max-line-length": [
  19. true,
  20. 120
  21. ]
  22. }
  23. }

除此之外,还需要在项目中创建 tsconfig.json 文件 ,下面是示例代码:

  1. {
  2. "compilerOptions": {
  3. "module": "commonjs",
  4. "target": "es2017",
  5. "sourceMap": true,
  6. "allowJs": true
  7. },
  8. "exclude": [
  9. "app/public",
  10. "app/views",
  11. "node_modules*"
  12. ]
  13. }

修改项目中 package.json 文件,把 ts 校验绑定到提交操作:

  1. "husky": {
  2. "hooks": {
  3. "commit-msg": "commitlint -e $GIT_PARAMS",
  4. "pre-commit" : "eslint ./app/**/*.js --fix && tslint ./app/**/*.ts --fix"
  5. }
  6. },

此时,在项目中提交时会自动带出之前的自定义约束,输入完成后自动校验 commit message ,并自动校验指定位置的 js 和 ts 代码规范,只有全部通过才能提交成功,否则退出 commit ,同时还可以使用指令输出 change log 文档。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/46339
推荐阅读
相关标签
  

闽ICP备14008679号