來源:人人貸大前端技術中心
juejin.im/post/5d0b3f8c6fb9a07ec07fc5d0
Git是現在市面上最流行的版本控制工具,書寫良好的commit message能大大提高代碼維護的效率。
但是在日常開發中由於缺少對commit message的約束,導致填寫內容隨意、質量參差不齊,可讀性低亦難以維護。
在項目中引入commit message規范已是迫在眉睫。
-
用什么規范?
-
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
-
示例
-
擴展閱讀
用什么規范?
現在市面上比較流行的方案是約定式提交規范
(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
。
關注微信公眾號:Java技術棧,在后台回復:git,可以獲取我整理的 N 篇最新Git 教程,都是干貨。
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
每一部分的填寫規則。大牛總結的 Git 使用技巧,這篇推薦大家看下。
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
。Git 的這個神技,學會爽歪歪~這篇推薦閱讀。
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
。
參考文檔:
https://docs.gitlab.com/ee/user/project/integrations/jira.html
填寫方式例如:
re #JIRA_ID
fix #JIRA_ID
示例
完整的commit message示例
相應的git log
擴展閱讀
conventional commits 必讀
介紹約定式提交標准。
Angular規范必讀
介紹Angular標准每個部分該寫什么、該怎么寫。
https://github.com/angular/angular/blob/22b96b9/CONTRIBUTING.md#-commit-message-guidelines
@commitlint/config-conventional必讀
介紹commitlint的校驗規則config-conventional,以及一些常見passes/fails情況。
https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
推薦去我的博客閱讀更多:
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
覺得不錯,別忘了點贊+轉發哦!