你们好,我是洛竹🎋,一只住在杭城的木系前端🧚🏻♀️,若是你喜欢个人文章📚,能够经过点赞帮我汇集灵力⭐️。前端
本文的最佳实践已加入洛竹的渐进式脚手架体系,执行
npx @luozhu/create-commitlint
便可为项目赋能。node
规范化 git commit
对于提升 git log
可读性、可控的版本控制和 changelog 生成都有着重要的做用。然而阻碍咱们脚步的不仅是团队的推广,单单对于一系列工具的配置都让人头大。这其中主要就是 commitlint 和 commitizen 的配合使用以及自定义提交规范。本文总结了目前的最佳实践给你们,若是有帮助,赏个star足矣。git
Conventional Commits 是一种用于给提交信息增长人机可读含义的规范。约定式提交规范是一种基于消息的轻量级约定。它提供了一组用于建立清晰的提交历史的简单规则;这使得编写基于规范的自动化工具变得更容易。这个约定与 SemVer 相吻合,在提交信息中描述新特性、bug 修复和破坏性变动。github
提交说明的结构以下所示:shell
<类型>([可选的做用域]): <描述>
[可选的正文]
[可选的脚注]
复制代码
feat:
: 类型为 feat
的提交表示在代码库中新增了一个功能(这和语义化版本中的 MINOR
相对应)。fix:
:类型为 fix
的 提交表示在代码库中修复了一个 bug (这和语义化版本中的 PATCH
相对应)。docs:
: 只是更改文档。style:
: 不影响代码含义的变化(空白、格式化、缺乏分号等)。refactor:
: 代码重构,既不修复错误也不添加功能。perf:
: 改进性能的代码更改。test:
: 添加确实测试或更正现有的测试。build:
: 影响构建系统或外部依赖关系的更改(示例范围:gulp、broccoli、NPM)。ci:
: 更改持续集成文件和脚本(示例范围:Travis、Circle、BrowserStack、SauceLabs)。chore:
: 其余不修改src
或test
文件。revert:
: commit 回退。能够为提交类型添加一个围在圆括号内的做用域,觉得其提供额外的上下文信息。例如 feat(parser): adds ability to parse arrays.
。npm
在可选的正文或脚注的起始位置带有 BREAKING CHANGE:
的提交,表示引入了破坏性 API 变动(这和语义化版本中的 MAJOR
相对应)。 破坏性变动能够是任意 类型 提交的一部分。json
feat: allow provided config object to extend other configs
BREAKING CHANGE: `extends` key in config file is now used for extending other config files
复制代码
!
字符以提醒注意破坏性变动的提交说明chore!: drop Node 6 from testing matrix
BREAKING CHANGE: dropping Node 6 which hits end of life in April
复制代码
docs: correct spelling of CHANGELOG
复制代码
feat(lang): add polish language
复制代码
fix: correct minor typos in code
see the issue for details on the typos fixed
closes issue #12
复制代码
feat
或fix
,其后接一个可选的做用域字段,以及一个必要的冒号(英文半角)和空格。feat
类型。fix
类型。fix(parser):
fix:array parsing issue when multiplejspaces were contained in string
。BREAKING CHANGE
,后面紧跟冒号和空格。BREAKING CHANGE:
以后必须提供描述,以描述对 API 的变动。例如:BREAKING CHANGE: enviroment variables now take precedence over cofig files
。feat
和fix
以外的类型。BREAKING CHANGE
必须是大写的。:
以前,附加!
字符,以进一步提醒注意破坏性变动。当有!
前缀时,正文或脚注内必须包含BREAKING CHANGE: description
可自定义的Commitizen插件(或独立实用运行)可帮助实现一致的提交消息。gulp
安装 commitizen、cz-customizable:ubuntu
$ yarn add -D commitizen cz-customizable
复制代码
向 package.json 添加新的 script:bash
{
"scripts" : {
...
"commit": "git cz"
}
"config": {
"commitizen": {
"path": "cz-customizable"
}
}
}
复制代码
在根目录新建 .cz-config.js
并复制 cz-config-EXAMPLE.js 到文件。
效果:
commitlint检查您的提交消息是否符合conventional commit format。
一、安装 @commitlint/cli、yorkie:
$ yarn add -D @commitlint/cli yorkie
复制代码
二、添加 git commit hooks 到 package.json:
{
...
"gitHooks": {
"commit-msg": "commitlint -e -V"
}
}
复制代码
三、安装 commitlint-config-cz:
commitlint-config-cz 合并 cz-customizable 的配置 {types,scopes,scopeOverrides}
和 commitlint 的配置 {type-enum,scope-enum}
。这样,你就能够在一个地方维护 types 和 scopes。
$ yarn add commitlint-config-cz -D
复制代码
四、在 commitlint.config.js
中用 cz
扩展您的 commitlint 配置:
module.exports = {
extends: ['cz'],
rules: {
// must add these rules
'type-empty': [2, 'never'],
'subject-empty': [2, 'never']
}
};
复制代码
在 VS Code 中搜索装 vscode commitizen,而后就能够摆脱命令行了,并且这个插件是和前面全部的配置兼容的,效果以下:
新建一个 github workflow .github/workflows/commitlint.yml
,做用是在提交 pull_request 时,检查信息:
name: Lint Commit Messages
on: [pull_request]
jobs:
commitlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-node@v1
with:
node-version: '10.x'
- run: npm install
- name: Add dependencies for commitlint action
# $GITHUB_WORKSPACE is the path to your repository
run: echo "::set-env name=NODE_PATH::$GITHUB_WORKSPACE/node_modules"
# Now the commitlint action will run considering its own dependencies and yours as well 🚀
- uses: wagoid/commitlint-github-action@v2
复制代码
standard-version 是一款遵循语义化版本( semver)和 commit message 标准规范 的版本和 changelog 自动化工具。一般状况线下,咱们会在 master 分支进行以下的版本发布操做:
git pull origin master
package.json
中的 version
更新版本号,更新 CHANGELOGgit add .
git commit
git tag
打版本操做git push --tags
:push 版本 tag 和 master 分支到仓库其中 2,3,4,5 是 standard-version 工具会自动完成的工做,配合本地的 shell 脚本,则能够自动完成一系列版本发布的工做了。
$ yarn add -D standard-version
复制代码
// package.json
{
"scripts": {
"release": "standard-version"
}
}
复制代码
yarn release --first-release
yarn release
yarn release --prerelease
or yarn release --prerelease alpha
npm version
-like):yarn release --release-as minor
or yarn release --release-as 1.1.0
,能够合并 --prerelease
以此方便发布实验性特性yarn release --no-verify