Skip to main content

commitizen

什么是commitizen

commitizen 是一款标准化 git commit 信息的工具。在没有规范的情况下,开发人员的 commit 信息是常常是随意的,这就导致 commit 信息显的很无用。可是当你在做git logcode review、编写changelog等情况时,良好的 commit 规范就显的尤为重要。

使用流程

全局安装

npm install -g commitizen husky cz-conventional-changelog-zh

# 中文(选装)
npm i -g cz-conventional-changelog-zh
# 相关依赖

# 在git钩子中调用commitizen
npm install -g husky

# 让 commitizen 默认采用 Angular规范
npm install -g @commitlint/cli @commitlint/config-conventional

# 自动生成 changelog 文件 CHANGELOG.md
npm install -g conventional-changelog-cli
# 全局配置文件
chcp 65001
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

配置完成后,在你本地进入任何的 git repository, 使用 git cz 代替 git commit 都会出现选项,用来生成符合格式的 Commit message

本地安装

npm install --save-dev commitizen
// package.json
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}

自定义规范

配合 cz-customizable,可以采用自己定义的规范

npm i -D commitlint-config-cz  cz-customizable

npm i -G commitlint-config-cz cz-customizable

pnpm i -D commitlint-config-cz cz-customizable

文件配置

  • package.json
// 将原来commit配置, 变更为自定义配置
"config":{
"commitizen":{
"path":"node_modules/cz-customizable"
}
}
  • commitlint.config.js

{
extends:['cz'],
rules:{
// 自定规则
}
}
  • .cz-config.js
  module.exports = {
types: [
{ value: '✨新增', name: '新增: 新的内容' },
{ value: '🐛修复', name: '修复: 修复一个Bug' },
{ value: '📝文档', name: '文档: 变更的只有文档' },
{ value: '💄格式', name: '格式: 空格, 分号等格式修复' },
{ value: '♻️重构', name: '重构: 代码重构,注意和特性、修复区分开' },
{ value: '⚡️性能', name: '性能: 提升性能' },
{ value: '✅测试', name: '测试: 添加一个测试' },
{ value: '🔧工具', name: '工具: 开发工具变动(构建、脚手架工具等)' },
{ value: '⏪回滚', name: '回滚: 代码回退' }
],
scopes: [
{ name: 'leetcode' },
{ name: 'javascript' },
{ name: 'typescript' },
{ name: 'Vue' },
{ name: 'node' }
],
// it needs to match the value for field type. Eg.: 'fix'
/* scopeOverrides: {
fix: [
{name: 'merge'},
{name: 'style'},
{name: 'e2eTest'},
{name: 'unitTest'}
]
}, */
// override the messages, defaults are as follows
messages: {
type: '选择一种你的提交类型:',
scope: '选择一个scope (可选):',
// used if allowCustomScopes is true
customScope: 'Denote the SCOPE of this change:',
subject: '短说明:\n',
body: '长说明,使用"|"换行(可选):\n',
breaking: '非兼容性说明 (可选):\n',
footer: '关联关闭的issue,例如:#31, #34(可选):\n',
confirmCommit: '确定提交说明?(yes/no)'
},
allowCustomScopes: true,
allowBreakingChanges: ['特性', '修复'],
// limit subject length
subjectLimit: 100
}

适配器(Adapter)

常用适配器

  • cz-conventional-changelog

因为不同的项目本身的构建方式的不同,commitizen 支持不同适配器的扩展,从而去满足不同的构建需求的。

cz-conventional-changelog 适配器

# 安装适配器 以cz-conventional-changelog 为例
npm install -g cz-conventional-changelog
# or
commitizen init cz-conventional-changelog --save-dev --save-exact
// commitizen 工具会自动在package.json中添加配置相应的配置
// package.json
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
}

基础配置

// ommitlint.config.js
module.exports = {
extends: ['@commitlint/config-conventional']
};
// git 钩子
{
"husky": {
"hooks": {
"commit-msg": "npx commitlint -E HUSKY_GIT_PARAMS"
}
}
}
// package.json
// 配合 change log
{
"scripts": {
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
},
}
  • 模拟提交
chore(init): init npm & git repo 
fix(cli): exit CLI with 1 when received SIGINT
docs(readme): specify environment in code blocks
feat(core): use cz-conventional-changelog as default adapter
  • 生成change log
npx conventional-changelog -p angular -i CHANGELOG.md -s
# or
npm run changelog

参阅资料