commit-message规范和实施

1.commit-message规范必要性

  1. 统一格式的提交记录,更清晰和易读
  2. 能够经过提交记录来了解本次提交的目的,更好的CR和重构
  3. 更容易了解变动,定位和发现问题
  4. 每一个提交描述都是通过思考的,改善提交质量
  5. 生成变动记录

2.规范选型

通过commit-message规范评审,在业界经常使用的:atomeslintAngular等规范中, 能够选择最经常使用的Angular规范做为咱们平常项目中的提交规范node

3.Angular的Commit Message规范简介

每条提交记录包含三个部分:header,body和footergit

<header> <BLANK LINE> <body> <BLANK LINE> <footer>

Commit Message Headergithub

<type>(<scope>): <short summary>
  │       │             │
  │       │             └─⫸ Summary in present tense. Not capitalized. No period at the end.
  │       │
  │       └─⫸ Commit Scope: animations|bazel|benchpress|common|compiler|compiler-cli|core|
  │                          elements|forms|http|language-service|localize|platform-browser|
  │                          platform-browser-dynamic|platform-server|router|service-worker|
  │                          upgrade|zone.js|packaging|changelog|dev-infra|docs-infra|migrations|
  │                          ngcc|ve
  │
  └─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test

其中<type>和<summary>是必要的 <scope>是可选的shell

Type 必须是如下的类型

  • feat: 新增页面或功能
  • fix: bug修复
  • build: 影响构建系统或外部依赖项的更改
  • ci: 对 CI 配置文件和脚本的更改
  • docs: 文档改动
  • perf: 性能提高改动
  • refactor: 不影响功能的代码重构(既不修复错误也不添加功能)
  • test: 添加或修改测试用例

Summary用来提供更改的简洁描述

4.规范实施

经过commitizen进行交互式提交,husky + commit-msg hook进行提交校验,cz-customizable来自定义交互提交流程和文案

image-20210615173113038

1.使用commitizen工具,在commit时能够交互的方式选择type

安装commitizennpm

npm i -D commitizen

package.json中添加对应的npm scriptjson

"commit":"cz"

改动添加到暂存区后执行commit提交api

npm run commit

2. 经过husky在git hooks中对不符合规范的提交进行拦截,拦截commitlint进行校验

安装husky , commitlint 和 符合angular提交规范的配置bash

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

添加git hooks工具

npx husky install

package.json中添加prepare的npm hook, 在每次install自动执行(yarn v2+不支持prepare)性能

"prepare": "husky install"

执行添加commit-msg hook

若是使用husk v4.x版本(推荐升级到新版本),直接在package.json中或.huskyrc.json中新增commit-msg钩子便可

package.json

"husky": {    
    "hooks": {      
        "commit-msg": "commitlint --edit $1"    
        }  
}

.huskyrc.huskyrc.json.huskyrc.jshusky.config.js

"hooks": {  
    "commit-msg": "commitlint --edit $1" 
}

若是使用husky v6+版本,须要添加对应的shell调用方式(husky v6对添加方式作了改动,因此没法经过添加配置到package.json中运行)

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

添加commintlint配置(也能够放到package.json中指定)

echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js

package.json中添加commitlint配置

"commitlint": {    
  "extends": [      
      "@commitlint/config-conventional"    
  ]
}

3. 扩展和自定义规范

commitizen提供的交互式默认是英文的,若是改为中文或者对交互流程进行改动,可使用cz-customizable进行扩展和自定义

安装cz-customizable

npm i -D cz-customizable

package.json中添加配置

"config": {    
    "commitizen": {      
        "path": "node_modules/cz-customizable"    
    },    
    "cz-customizable": {      
        "config": ".cz-config.js"    
    } 
}

.cz-config.js就是cz-customizable配置的具体文件了,能够参考CZ-Config-Example并进行改动, 能够把文案翻译成中文,自定义修改提示等。

也能够经过fork cz-customizable建立内封配置文件的npm包

npm i -D your-own-package
"config": {    
  "commitizen": {      
    "path": "node_modules/your-own-package"    
  } 
}

配置文件能够自定义交互内容,好比能够只保留type scope breakchange confirm

  • 选择提交类型
  • 简单描述本次改动
  • 是否有重大变动
  • 肯定提交

配置文件中设置skipQuestions: ['body','customScope','scope','footer'],便可忽略其余选项

allowBreakingChanges: ['feat', 'fix'], 仅在feat和fix时提示 breakchange

4.其余

经过npm script进行commit,若是eslint没有经过(在pre-commit 钩子中作了eslint检测),可是又想提交能够经过加'––'来向npm script传参

npm run commit -- --no-verify # or npm run commit -- -n
相关文章
相关标签/搜索