git commit 规范工具

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaileilei1/article/details/83186047

为什么使用 commit 规范?

首先看一下国际知名项目 angularjs 提交历史

我的提交历史:

纳尼???我都写了什么???

 你有没有中枪 -->

因此,我们的 git commit 规范提上日程

1.commitizen    

拉取线上代码库,执行

sudo cnpm install -g commitizen

生成 package.json 文件 

npm init --yes

然后,运行下面命令,使其支持 Commit message 格式。

commitizen init cz-conventional-changelog --save --save-exact

 执行 git 操作

git add .

弃用 git commit -m 改为 git cz

出现以下选项供选择:

首先要选择的就是提交类型 (type),提交类型只允许使用以上几个标识:

feat A new feature 新功能
fix A bug fix 修复 bug
docs Documentation only changes 文档修改
style Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc) 格式(不影响代码运行的变动)
refactor A code change that neither fixes a bug nor adds a feature 重构
perf A code change that improves performance 提高性能
test Adding missing tests or correcting existing tests 添加缺失测试或更正现有测试
build Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)

依赖的外部资源变化

chore Other changes that don't modify src or test files 构建过程或辅助工具的变动
revert Reverts a previous commit 恢复先前的提交

之后还会询问其它选项,填写信息。。

git push origin master

到此,提交工具配置完成。如果想加入 commit 校验功能,继续 - ->

2.validate-commit-msg

校验 commit 是否符合规范

安装 validate-commit-msg

npm install --save-dev validate-commit-msg

安装 ghooks 

cnpm install ghooks --save-dev

在 package.json 配置 ghooks

"config": {
    "ghooks": {
      "commit-msg": "validate-commit-msg"
    },
    "validate-commit-msg": {
      "types": ["feat", "fix", "docs", "style", "refactor", "perf", "test", "build", "ci", "chore", "revert"],
      "scope": {
        "required": false,
        "allowed": ["*"],
        "validate": false,
        "multiple": false
      },
      "warnOnFail": false,
      "maxSubjectLength": 100,
      "subjectPattern": ".+",
      "subjectPatternErrorMsg": "subject does not match subject pattern!",
      "helpMessage": "",
      "autoFix": false
    }
  }

配置如下图: 

 此时,如果修改文件 再次写 git commit -m “***”,会报错

此时,我们的校验功能就实现了!!!如果想看更改日志 (change log),请继续 - ->

3.change log

安装 changelog

sudo cnpm install -g conventional-changelog

在 package.json 的 scripts 字段写入:

"changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0"

如图:

 运行  npm run changelog 查看更改日志。

参考资料:

阮一峰老师  

https://www.jianshu.com/p/55f681604fca

https://www.npmjs.com/package/validate-commit-msg

猜你喜欢

转载自blog.csdn.net/zhaileilei1/article/details/83186047