目录
1. 这种开发日常,你有过么?
2. 业界怎么做?
2.1. 规范化 git commit 的 message
2.2. 交互式 commit message 生成
2.3. 强制校验 commit message 格式
2.4. 自动生成 CHANGELOG
3. 综合示例
1. 这种开发日常,你有过么?
2. 业界怎么做?
2.1. 规范化 git commit 的 message
从 git commit 的 message 开始进行规范化(主流:angular 规范),进而可以通过工具(例如:conventional-changelog)把关键信息找出来,并自动生成到 CHANGELOG 中。
规范节选
Each commit message consists of a header, a body, and a footer.
<header>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
header
is mandatory and must conform to the Commit Message Header format.body
is mandatory for all commits except for those of type "docs". When the body is present it must be at least 20 characters long and must conform to the Commit Message Body format.footer
is optional. The Commit Message Footer format describes what the footer is used for and the structure it must have.<type>(<scope>): <short summary>
│ │ │
│ │ └─⫸ Summary in present tense. Not capitalized. No period at the end.
│ │
│ └─⫸ Commit Scope: The scope should be the name of the component affected
│ (as perceived by the person reading the
│ changelog generated from commit messages).
│
└─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test
The<type>
and <summary>
fields are mandatory, the (<scope>)
field is optional.
Must be one of the following:
The scope should be the name of the npm package affected (as perceived by the person reading the changelog generated from commit messages).
规范示例
2.2. 交互式 commit message 生成
commitizen 是一款可以交互式建立提交信息的工具。它帮助我们从 type 开始一步步建立提交信息。
2.3. 强制校验 commit message 格式
在日常开发中,为保证小伙伴们都能按照规范书写 commit message,我们可以使用 commitlint + husky 的方式强制推行规范。原理是在实际的 git commit 提交到远程仓库之前使用 git 钩子来验证信息,将阻止不符合规则的信息提交到远程仓库。
2.4. 自动生成 CHANGELOG
如果你的所有 Commit 都符合规范,那么发布新版本时,可以使用工具(例如:conventional-changelog)自动生 CHANGELOG。
CHANGELOG 示例
3. 综合示例
git init
npm init
npm install --save-dev @commitlint/cli @commitlint/config-conventional
npm install --save-dev conventional-changelog conventional-changelog-cli
npm install --save-dev husky
module.exports = {
extends: ['@commitlint/config-conventional']
};
{
"husky": {
"hooks": {
"commit-msg": "npx commitlint -E HUSKY_GIT_PARAMS"
}
}
}
"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
refactor!: drop support for Node 6
feat: allow provided config object to extend other configs
BREAKING CHANGE: `extends` key in config file is now used for extending other config files
refactor!: drop support for Node 6
BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.
npx conventional-changelog -p angular -i CHANGELOG.md -s -r 0
参考:
commit message 格式规范:angular 规范 https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines commit message 格式规范: https://www.conventionalcommits.org/en/v1.0.0/#specification 交互式 commit message 书写工具:commitizen https://github.com/commitizen/cz-cli commit message 校验工具:commitlint https://github.com/conventional-changelog/commitlint Git钩子辅助: https://typicode.github.io/husky/#/ CHANGELOG 生成工具:conventional-changelog https://github.com/conventional-changelog/conventional-changelog