前言
此篇文檔基于 21 年年終述職大家提出的開發(fā)規(guī)范化管理和文檔化管理的建議买乃,結(jié)合 Conventional Commits 約定式提交規(guī)范
對 git 代碼作出提交規(guī)范化。
那什么是 Conventional Commits 約定式提交規(guī)范
呢金赦?
定義
Conventional Commits
是一種用于給提交信息增加人機可讀含義的規(guī)范蕴坪。它提供了一組用于創(chuàng)建清晰的提交歷史的簡單規(guī)則。
提交說明的結(jié)構(gòu)如下:
<類型>([可選的作用于]): <描述>
[可選的正文]
[可選的腳注]
作用
- 自動化生成 CHANGELOG
- 基于提交類型块蚌,自動決定語義化的版本變更
- 向項目相關(guān)合作開發(fā)者發(fā)送變更信心
- 觸發(fā)自動化構(gòu)建和部署流程
- 給開發(fā)者提供一個更加結(jié)構(gòu)化的提交歷史,便于減低對項目做貢獻的難度
使用流程
git commit
最佳實踐膘格,cimmitizen + husky + commitlint
規(guī)范化校驗
cimmitizen
- 執(zhí)行以下命令
npm install -g commitizen
npm install -g cz-conventional-changelog
echo '{"path":"cz-conventional-changelog"}' > ~/.czrc
- 測試功能
執(zhí)行 git cz
提交代碼峭范,出現(xiàn)以下流程
// 選擇一個提交的類型
1. select the type of change that you're committing
type | 含義 |
---|---|
feat | 新增功能 |
fix | 修復(fù) bug |
docs | 更改文檔 |
style | 不影響代碼含義的變化(空白,格式化瘪贱,缺少分號等) |
refactor | 重構(gòu)纱控,不修復(fù) bug 且不添加功能 |
perf | 性能優(yōu)化代碼 |
build | 構(gòu)建方面相關(guān) |
// 更改影響的范圍,可不填
2. What is the scope of this change (e.g. component or file name): (press enter to skip)
// 簡短的描述
3. Write a short, imperative tense description of the change (max 88 chars)
// 具體描述
4. rovide a longer description of the change: (press enter to skip)
// 是否有破壞性的更改
5. Are there any breaking changes?
// 是否有關(guān)聯(lián)性的問題
6. Does this change affect any open issues?
如下圖:
git log
查看提交日志
由上圖可以看出菜秦,提交了一個 fix(test)項甜害,也是符合了約定式提交規(guī)范
husky
- 安裝 husky
npm install husky —save-dev
- 安裝 git hooks
方法1:
npx husky install
方法2:配置package.json文件中script: "prepare": "husky install"
npm run prepare
- 測試
husky
鉤子,添加pre-commit
鉤子
npx husky add .husky/pre-commit "npm test"
根目錄.husky 文件夾下生成 pre-commit 文件球昨,刪除 pre-commit 鉤子只需刪除文件即可
commitlint
- 安裝
npm install -g @commitlint/cli @commitlint/config-conventional
- 生成規(guī)范配置文件
echo "module.export = {extends:['@commitlint/config-conventional']}" > commitlint.config.js
- 根目錄.husky 文件夾下 pre-commit 文件中添加規(guī)則
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
- 在 commitlint.config.js 文件中寫入規(guī)則
module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
"type-enum": [
2,
"always",
[
"build",
"ci",
"chore",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
],
],
},
};
- 測試提交功能
git add .
git commit -m 'xxx'
執(zhí)行完上兩步操作后尔店,終端出現(xiàn)報錯:
? input: xxx
? subject may not be empty [subject-empty]
? type may not be empty [type-empty]
? found 2 problems, 0 warnings
? Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky - commit-msg hook exited with code 1 (error)
兩個報錯:1. 缺少類型參數(shù) 2. 缺少描述信息
碰到這兩個報錯可以選擇兩種方式解決提交報錯問題
方法一: 按照規(guī)范,為 commit 信息添加入?yún)ⅰ@?git commit -m 'fix: 修改bug'
方法二: 使用 git cz
嚣州,按步驟操作
standard-version
1.安裝
npm install standard-version
- package.json文件中配置
"scripts": {
"release": "standard-version"
}
-
提交代碼后執(zhí)行
npm run release
release.png
執(zhí)行完后自動升級版本號鲫售、自動生成CHAGNELOG文件,里面是之前提交的對應(yīng)版本號 -
生成的tags不會推送到遠程该肴,所以還要執(zhí)行最后一步
git push --tag
tag.png
生成新tag并推送到遠程
最后去git上驗證就可以啦~