git commit 提交规范 & 规范校验

1、背景


在多人协作项目中,如果代码风格统一、代码提交信息的说明准确,那么在后期协作以及Bug处理时会更加方便。

因此,在本文章中,我会介绍怎么使用下面这个工具,在git push 代码之前检测commit messages

  1. commitlint
  2. husky
commitlint介绍

2、先来介绍博主采用的commit规范

Commit message格式

<type>: <subject>

注意冒号后面有空格。

type

用于说明 commit 的类别,只允许使用下面7个标识。

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

如果type为featfix,则该 commit 将肯定出现在 Change log 之中。

subject

subject是 commit 目的的简短描述,不超过50个字符,且结尾不加句号(.)。


3、使用工具校验commit是否符合规范

3.1 全局安装
npm install -g @commitlint/cli @commitlint/config-conventional
   
   
    3.2 生成配置配件

    这个文件在根目录下生成就可以了。

    echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
       
       
      3.2 在commitlint.config.js制定提交message规范
      "module.exports = {extends: ['@commitlint/config-conventional']}"
      

      module.exports = {
      extends: [‘@commitlint/config-conventional’],
      rules: {
      ‘type-enum’: [2, ‘always’, [
      “feat”, “fix”, “docs”, “style”, “refactor”, “test”, “chore”, “revert”
      ]],
      ‘subject-full-stop’: [0, ‘never’],
      ‘subject-case’: [0, ‘never’]
      }
      };

        上面我们就完成了commitlint的安装与提交规范的制定。检验commit message的最佳方式是结合git hook,所以需要配合Husky

        3.4 husky介绍

        husky继承了Git下所有的钩子,在触发钩子的时候,husky可以阻止不合法的commit,push等等。注意使用husky之前,必须先将代码放到git 仓库中,否则本地没有.git文件,就没有地方去继承钩子了。

        npm install husky --save-dev
         
         

          安装成功后需要在项目下的package.json中配置

          "scripts": {
              "commitmsg": "commitlint -e $GIT_PARAMS",
          

          },
          “config”: {
          “commitizen”: {
          “path”: “cz-customizable”
          }
          },

            最后我们可以正常的git操作

            git add .
            git commit -m ""
             
             

              git commit的时候会触发commlint。下面演示下不符合规范提交示例:

              F:\accesscontrol\access_control>git commit -m "featdf: aas"
              husky > npm run -s commitmsg (node v8.2.1)
              

              ⧗ input:
              featdf: aas

              type must be one of [feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert] [type-enum]
              ✖ found 1 problems, 0 warnings

              husky > commit-msg hook failed (add --no-verify to bypass)

              F:\accesscontrol\access_control>

                上面message不符合提交规范,所以会提示错误。

                我们修改下type

                F:\accesscontrol\access_control>git commit -m "feat: 新功能"
                husky > npm run -s commitmsg (node v8.2.1)
                

                ⧗ input: feat: 新功能
                ✔ found 0 problems, 0 warnings

                [develop 7a20657] feat: 新功能
                1 file changed, 1 insertion(+)

                F:\accesscontrol\access_control>

                  commit成功。

                  3.5 husky的钩子

                  可以在package.json下面添加如下的钩子。

                  "husky": {
                      "hooks": {
                        "pre-commit": "npm run lint"
                      }
                    },
                  
                   
                   

                    4、最后总结过程中遇到一些问题

                    1. git commit后可能报错相关‘regenerator-runtime’模块找不到;解决方式:npm install regenerator-runtime –save。
                    2. git commit -m “messge”,用双引号
                    • 0
                      点赞
                    • 0
                      收藏
                      觉得还不错? 一键收藏
                    • 0
                      评论

                    “相关推荐”对你有帮助么?

                    • 非常没帮助
                    • 没帮助
                    • 一般
                    • 有帮助
                    • 非常有帮助
                    提交
                    评论
                    添加红包

                    请填写红包祝福语或标题

                    红包个数最小为10个

                    红包金额最低5元

                    当前余额3.43前往充值 >
                    需支付:10.00
                    成就一亿技术人!
                    领取后你会自动成为博主和红包主的粉丝 规则
                    hope_wisdom
                    发出的红包
                    实付
                    使用余额支付
                    点击重新获取
                    扫码支付
                    钱包余额 0

                    抵扣说明:

                    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
                    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

                    余额充值