- 用什麼規範?
- Quick Start
- 1. 全局安裝commitizen & cz-conventional-changelog
- 2. 項目内安裝commitlint & husky
- 3. 添加相應配置
- 4. 使用
- Commit message規範在rrd-fe落地使用情況
- 1. type
- 2. scope
- 3. body
- 4. break changes
- 5. affect issues
- 示例
- 擴充閱讀
git是現在市面上最流行的版本控制工具,書寫良好的commit message能大大提高代碼維護的效率。但是在日常開發中由于缺少對于commit message的限制,導緻填寫内容随意、品質參差不齊,可讀性低亦難以維護。在項目中引入commit message規範已是迫在眉睫。
用什麼規範?
現在市面上比較流行的方案是
約定式送出規範
(
Conventional Commits
),它受到了
Angular送出準則
的啟發,并在很大程度上以其為依據。
約定式送出規範
是一種基于送出消息的輕量級約定。它提供了一組用于建立清晰的送出曆史的簡單規則;這使得編寫基于規範的自動化工具變得更容易。這個約定與
SemVer
相吻合,在送出資訊中描述新特性、bug 修複和破壞性變更。它的 message 格式如下:
<類型>[可選的作用域]: <描述>
[可選的正文]
[可選的腳注]
複制
Quick Start
1. 全局安裝commitizen & cz-conventional-changelog
commitizen
是一個撰寫合格
commit message
的工具,用于代替
git commit
指令,而
cz-conventional-changelog
擴充卡提供conventional-changelog标準(約定式送出标準)。基于不同需求,也可以使用不同擴充卡。
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
複制
安裝完畢後,可直接使用
git cz
來取代
git commit
。
全局模式下,需要
~/.czrc
配置檔案, 為
commitizen
指定
Adapter
。
2. 項目内安裝commitlint & husky
commitlint
負責用于對
commit message
進行格式校驗,
husky
負責提供更易用的
git hook
。
Use npm
npm i -D husky @commitlint/config-conventional @commitlint/cli
Use yarn
yarn add husky @commitlint/config-conventional @commitlint/cli -D
複制
commitlint
隻能做格式規範,無法觸及内容。對于内容品質的把控隻能靠我們自己。
3. 添加相應配置
建立
commitlint.config.js
# In the same path as package.json
echo 'module.exports = {extends: ["@commitlint/config-conventional"]};' > ./commitlint.config.js
複制
引入
husky
# package.json
...,
"husky": {
"hooks": {
"commit-msg": "commitlint -e $GIT_PARAMS"
}
}
複制
4. 使用
執行
git cz
進入interactive模式,根據提示依次填寫
1.Select the type of change that you're committing 選擇改動類型 (<type>)
2.What is the scope of this change (e.g. component or file name)? 填寫改動範圍 (<scope>)
3.Write a short, imperative tense description of the change: 寫一個精簡的描述 (<subject>)
4.Provide a longer description of the change: (press enter to skip) 對于改動寫一段長描述 (<body>)
5.Are there any breaking changes? (y/n) 是破壞性修改嗎?預設n (<footer>)
6.Does this change affect any openreve issues? (y/n) 改動修複了哪個問題?預設n (<footer>)
複制
生成的commit message格式如下:
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
複制
填寫完畢後,
husky
會調用
commitlint
對message進行格式校驗,預設規定
type
及
subject
為必填項。
任何
git commit
指令的
option
都能用在
git cz
指令上, 例如
git cz -a
Commit message規範在rrd-fe落地使用情況
針對團隊目前使用的情況,我們讨論後拟定了
commit message
每一部分的填寫規則。
1. type
type
為必填項,用于指定commit的類型,約定了
feat
、
fix
兩個
主要type
,以及docs、style、build、refactor、revert五個
特殊type
,
其餘type
暫不使用。
# 主要type
feat: 增加新功能
fix: 修複bug
# 特殊type
docs: 隻改動了文檔相關的内容
style: 不影響代碼含義的改動,例如去掉空格、改變縮進、增删分号
build: 構造工具的或者外部依賴的改動,例如webpack,npm
refactor: 代碼重構時使用
revert: 執行git revert列印的message
# 暫不使用type
test: 添加測試或者修改現有測試
perf: 提高性能的改動
ci: 與CI(持續內建服務)有關的改動
chore: 不修改src或者test的其餘修改,例如建構過程或輔助工具的變動
複制
當一次改動包括
主要type
與
特殊type
時,統一采用
主要type
。
2. scope
scope
也為必填項,用于描述改動的範圍,格式為項目名/子產品名,例如:
node-pc/common
rrd-h5/activity
,而
we-sdk
不需指定子產品名。如果一次commit修改多個子產品,建議拆分成多次commit,以便更好追蹤和維護。
3. body
body
填寫較長的描述,主要描述
改動之前的情況
及
修改動機
,對于小的修改不作要求,但是重大需求、更新等必須添加body來作說明。
4. break changes
break changes
指明是否産生了破壞性修改,涉及break changes的改動必須指明該項,類似版本更新、接口參數減少、接口删除、遷移等。
5. affect issues
affect issues
指明是否影響了某個問題。例如我們使用jira時,我們在
commit message
中可以填寫其影響的
JIRA_ID
,若要開啟該功能需要先打通
jira
與
gitlab
。參考文檔:docs.gitlab.com/ee/user/pro…
填寫方式例如:
re #JIRA_ID
fix #JIRA_ID
複制
示例
- 完整的commit message示例

- 相應的git log