git的commit规范及强制校验

 

1、背景


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

先来介绍本人公司采用的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个字符,且结尾不加句号(.)。

 

注意

type    和    subject 之前有个英文冒号以及个空格!


 

 

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

  1. commitlint        https://marionebl.github.io/commitlint/#/ 
  2. husky              https://github.com/typicode/husky 

 

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

3.1 全局安装(这步很重要)
npm install -g @commitlint/cli @commitlint/config-conventional


项目没有package.json的话,要连忙 [npm | cnpm] init -y
 
 
 
4
 
 
 
 
 
1
npm install -g @commitlint/cli @commitlint/config-conventional
2
 
         
3
 
         
4
项目没有package.json的话,要连忙 [npm | cnpm] init -y
 
 
3.2 生成配置配件

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

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
 
 
 
1
 
 
 
 
 
1
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
 
 
3.3 在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']
  }
};
 
 
 
12
 
 
 
 
 
1
"module.exports = {extends: ['@commitlint/config-conventional']}"
2
 
         
3
module.exports = {
4
  extends: ['@commitlint/config-conventional'],
5
  rules: {
6
    'type-enum': [2, 'always', [
7
      "feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"
8
    ]],
9
    'subject-full-stop': [0, 'never'],
10
    'subject-case': [0, 'never']
11
  }
12
};
 
 

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

 

3.4 husky介绍


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

npm install husky --save-dev
 
 
 
1
 
 
 
 
 
1
npm install husky --save-dev
 
 

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

 

注意配置的最新,旧版本github把它的包干掉了,下载不了!

最新版本的配置(当前为v1.0.1版本,持续更新中):
"husky": {
   "hooks": {
     "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }



以下为0.14.3版本时的配置
"scripts": {
    "commitmsg": "commitlint -e $GIT_PARAMS",

 },
 "config": {
    "commitizen": {
      "path": "cz-customizable"
    }
  },
 
 
 
19
 
 
 
 
 
1
最新版本的配置(当前为v1.0.1版本,持续更新中):
2
"husky": {
3
   "hooks": {
4
     "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
5
    }
6
  }
7
 
         
8
 
         
9
 
         
10
以下为0.14.3版本时的配置
11
"scripts": {
12
    "commitmsg": "commitlint -e $GIT_PARAMS",
13
 
         
14
 },
15
 "config": {
16
    "commitizen": {
17
      "path": "cz-customizable"
18
    }
19
  },
 
 

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

git add .
 
 
 
1
 
 
 
 
 
1
git add .
 
 

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>
 
 
 
12
 
 
 
 
 
1
F:\accesscontrol\access_control>git commit -m "featdf: aas"
2
husky > npm run -s commitmsg (node v8.2.1)
3
 
         
4
   input:
5
featdf: aas
6
 
         
7
   type must be one of [feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert] [type-enum]
8
   found 1 problems, 0 warnings
9
 
         
10
husky > commit-msg hook failed (add --no-verify to bypass)
11
 
         
12
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>
 
 
 
10
 
 
 
 
 
1
F:\accesscontrol\access_control>git commit -m "feat: 新功能"
2
husky > npm run -s commitmsg (node v8.2.1)
3
 
         
4
   input: feat: 新功能
5
   found 0 problems, 0 warnings
6
 
         
7
[develop 7a20657] feat: 新功能
8
 1 file changed, 1 insertion(+)
9
 
         
10
F:\accesscontrol\access_control>
 
 

commit成功。

 

3.5 husky的钩子

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

"husky": {
    "hooks": {
      "pre-commit": "npm run lint"
    }
  },
 
 
 
5
 
 
 
 
 
1
"husky": {
2
    "hooks": {
3
      "pre-commit": "npm run lint"
4
    }
5
  },
 
 

 

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

    1. git commit后可能报错相关‘regenerator-runtime’模块找不到;解决方式:npm install regenerator-runtime –save。
    2. git commit -m “messge”,用双引号
    3. commit不上去,你先按这篇博客的步骤仔细检查一下,是不是哪里顺序错了或者漏了。还出错就去看看文章让安装的依赖是不是又更新了
文章原链接: https://blog.csdn.net/y491887095/article/details/80594043    (稍微补充了下本人踩的坑!)

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

猜你喜欢

转载自www.cnblogs.com/liuming666/p/e3f10b69324d50518ef3a2ea71de6ed9.html