目的
git commit消息规范化的目的就是为了能自动生成 changelog, 自动生成changelog的目的,是为了方便让自己和别人知道,每次版本更新,都有哪些变动(增减了哪些功能, 修复了哪些bug)
依赖包安装
npm install --save-dev git-cz husky commitlint @commitlint/config-conventional standard-version
生成/更改相关配置文件
执行如下命令, 生成husky
命令文件夹.husky
git init
npx husky install
在.husky
文件夹新建commit-msg
文件,文件内容如下
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no-install commitlint --edit "$1"
新增.commitlintrc.js
配置文件,文件内容如下:(用于对git commit消息进行验证)commitlint工具的配置文件
module.exports = {
extends: ['@commitlint/config-conventional']
};
新增changelog.config.js
配置文件, 文件内容如下: (用于规定git commit消息的规范)git-cz工具的配置文件
module.exports = {
disableEmoji: false,
format: '{type}{scope}: {emoji}{subject}',
list: [
'test',
'feat',
'fix',
'chore',
'docs',
'refactor',
'style',
'ci',
'perf',
],
maxMessageLength: 64,
minMessageLength: 3,
questions: [
'type',
'scope',
'subject',
'body',
'breaking',
'issues',
'lerna',
],
scopes: [],
types: {
chore: {
description: '构建过程或辅助工具更改',
emoji: '🤖',
value: 'chore',
},
ci: {
description: 'CI 相关的改变',
emoji: '🎡',
value: 'ci',
},
docs: {
description: '只修改了文档',
emoji: '✏️',
value: 'docs',
},
feat: {
description: '一个新功能',
emoji: '🎸',
value: 'feat',
},
fix: {
description: '故障修复',
emoji: '🐛',
value: 'fix',
},
perf: {
description: '改进性能的代码修改',
emoji: '⚡️',
value: 'perf',
},
refactor: {
description: '既不修复故障也不增加新功能的代码修改',
emoji: '💡',
value: 'refactor',
},
release: {
description: '创建发布提交',
emoji: '🏹',
value: 'release',
},
style: {
description: '仅仅是代码格式/风格的修改',
emoji: '💄',
value: 'style',
},
test: {
description: '添加缺失的测试',
emoji: '💍',
value: 'test',
},
},
}
修改package.json
{
...
"scripts": {
"git-commit": "git-cz"
},
"config": {
"commitizen": {
"path": "git-cz"
}
}
...
}
git-commit
该命令用于代替原始的git commit, 让用户直接按照模板填写规范化的消息
此时可以进行一次git commit,测试是否有对提交消息进行验证
新增.versionrc.js
配置文件(用于自动生成changlog),文件内容如下:(hidden属性,可以控制这类git commit消息是否显示到changelog中)standard-version工具的配置文件
module.exports = {
header: '# xxx系统 \n## 更新历史',
commitUrlFormat:
'http://git项目地址/tree/{{hash}}',
issueUrlFormat:
'http://gitissu地址{{id}}',
types: [
{ type: 'feat', section: '✨ Features | 新功能' },
{ type: 'fix', section: '🐛 Bug Fixes | Bug 修复' },
{ type: 'init', section: '🎉 Init | 初始化' },
{ type: 'docs', section: '✏️ Documentation | 文档' },
{ type: 'style', section: '💄 Styles | 风格' },
{ type: 'refactor', section: '♻️ Code Refactoring | 代码重构' },
{ type: 'perf', section: '⚡ Performance Improvements | 性能优化' },
{ type: 'test', section: '✅ Tests | 测试' },
{ type: 'revert', section: '⏪ Revert | 回退', hidden: true },
{ type: 'build', section: '📦 Build System | 打包构建' },
{ type: 'chore', section: '🚀 Chore | 构建/工程依赖/工具' },
{ type: 'ci', section: '👷 Continuous Integration | CI 配置' },
],
}
修改package.json
{
...
"scripts": {
"release": "standard-version",
"release-major": "standard-version --release-as major",
"release-minor": "standard-version --release-as minor",
"release-patch": "standard-version --release-as patch",
}
...
}
命令说明:
release
: 按照standard-version
默认规则升级版本号release-major
: 升级major版本号release-patch
: 升级patch版本号
standard-version 默认版本升级规则
- feature 会更新 patch
- bug fix 会更新 patch
- BREAKING CHANGES 会更新 minor
版本构成
版本号 major.minor.patch
如果想跳过changelog生成
所有可配置跳过的有: bump
, changelog
, commit
, tag
修改package.json
{
...
"standard-version": { "skip": { "changelog": true } }
...
}
最终生成的changelog效果
其他补充说明
如果需要让.versionrc.js
配置文件的commitUrlFormat
, issueUrlFormat
配置生效,必须在package.json
文件中配置repository
属性