使用 gitlint 讓 commit message 服從 conventional 規范
起因
看了阮一峰的文章 Commit message 和 Change log 編寫指南,認同 git commit message 應該規范化的觀點;但用來檢查是否符合規范的工具,阮提到 commitzen,要裝 node 環境,以及把 git commit
替換為 git cz
,着實麻煩。
考慮到 git 的 hook 機制,可以基於 commitlint 配置,能繼續用 git commit
命令;然而配置過程還是很繁瑣,yml 和 package.json 的寫法難以一下寫對。
相比之下, gitlint 則簡單了許多:用 pip 而不是 nodejs 的 npm 安裝;編寫 .gitlint 的說明文檔一看就懂。嘗試配置如下。
配置
安裝 gitlint:
pip install gitlint
找到一個 repo,要求至少有一次 commit,例如新建的項目:
mkdir -p ~/work/test/hhh
cd $_
git init
touch README
git add .
git commit -m "Initial commit"
添加 .gitlint
文件,內容:
[general]
# You HAVE to add the rule here to enable it, only configuring (such as below)
# does NOT enable it.
contrib=contrib-title-conventional-commits,CT1
ignore=B6
添加 gitlint 到 git 的 hook 中:
gitlint install-hook
接下來執行 git commit 的時候,都會按照 CT1 規則檢查 commit 信息中的 title 是否符合 conventionalcommits 規范。更多 gitlint 配置規則見官方文檔。