最近再看Vue技術內幕,就把Vue項目fork了一份,然后邊看文章邊在代碼里加上注釋。看了幾節內容,做了一些筆記,準備提交一次,然后就直接:
git add .
git commit -m '添加代碼注釋'
……
然后就彈出這么個內容:
ERROR invalid commit message format.
Proper commit message format is required for automated changelog generation. Examples:
feat(compiler): add 'comments' option
fix(v-model): handle events on blur (close #28)
See .github/COMMIT_CONVENTION.md for more details.
You can also use npm run commit to interactively generate a commit message.
commit-msg hook failed (add --no-verify to bypass)
怎么提交還會報錯?這看上去不像沖突啥的啊。npm run commit
又是啥?
鍵入npm run commit
cz-cli@2.10.1, cz-conventional-changelog@2.1.0
Line 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.
? Select the type of change that you're committing: (Use arrow keys)
? feat: A new feature
fix: A bug fix
docs: Documentation only changes
style: Changes that do not affect the meaning of the code (white-space, format
ting, missing semi-colons, etc)
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
test: Adding missing tests or correcting existing tests
(Move up and down to reveal more choices)
選擇這次提交的類型?類型有很多種,除了這些還可以用方向鍵查看更多的:
build: Changes that affect the build system or external dependencies (example
scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Trav
is, Circle, BrowserStack, SauceLabs)
chore: Other changes that don't modify src or test files
revert: Reverts a previous commit
選擇對應的類型可以清楚明了的說明本次提交的目的。
接下來還會問一些問題:
? What is the scope of this change (e.g. component or file name)? (press enter to s
kip)
? Write a short, imperative tense description of the change:
添加筆記以及注釋
? Provide a longer description of the change: (press enter to skip)
? Are there any breaking changes? No
? Does this change affect any open issues? No
按著這個一步一步來,一個規范的的commit就出來了。
嗨呀,真的好啊,還能強制性的規范commit,所有項目上都弄這么個東西,這么一來所有項目的commit都會變得清晰明了。
這個命令到底執行了啥呢?跑去package.json
文件中去看,有這么一句"commit": "git-cz"
,git-cz
又是啥呢?接著又去google。
google git-cz
第一條點進去,是一個插件,叫
commitizen
。切到自己的項目,npm安裝,
package.json
里scripts
添加指令,再在package.json
的config
里添加:
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
然后隨便改點東西,先用
git add .
git commit -m 'x'
測試下。哈?怎么直接提交了?怎么沒報錯?這東西沒用?npm run commit
,這玩意兒有用呀,為啥直接提交不規范commit還能成功?
再看看vue的package.json
怎么寫的。
"gitHooks": {
"pre-commit": "lint-staged",
"commit-msg": "node scripts/verify-commit-msg.js"
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
gitHooks
:顧名思義,git相關的鉤子,可以在git的各個時間做不同的處理。實際上想這么用得裝個尤大寫的yorkie,實際上就是改了一丟丟的husky。
- commit之前,執行lint-staged
- 對commit的說明進行判定是否是符合規范的commit
lint-staged
:對代碼進行靜態分析,試圖找出潛在的問題。使用 lint-staged
只會對每次提交文件進行檢查。
- 對所有提交的js文件進行eslint修復,修復后重新添加該文件到暫存區
加上這兩句之后,直接提交不規范的commit就會報錯啦,而且還會對提交的js文件進行eslint驗證,以及嘗試修復eslint。