業(yè)界通用規(guī)范
AngularJS 在 github上 的提交記錄被業(yè)內(nèi)許多人認(rèn)可勺爱,逐漸被大家引用
格式
1
type(scope):subject
- type(必須) : commit 的類別,只允許使用下面幾個(gè)標(biāo)識(shí)
feat : 新功能
fix : 修復(fù)bug
docs : 文檔改變
style : 代碼格式改變
refactor : 某個(gè)已有功能重構(gòu)
perf : 性能優(yōu)化
test : 增加測(cè)試
build : 改變了build工具 如 grunt換成了 npm
revert : 撤銷上一次的 commit
-
chore : 構(gòu)建過(guò)程或輔助工具的變動(dòng)
scope(可選) : 用于說(shuō)明 commit 影響的范圍卫旱,比如數(shù)據(jù)層围段、控制層、視圖層等等奈泪,視項(xiàng)目不同而不同。
subject(必須) : commit 的簡(jiǎn)短描述涝桅,不超過(guò)50個(gè)字符.commitizen 是一個(gè)撰寫合格 Commit message 的工具,遵循 Angular 的提交規(guī)范蕊肥。
添加 git commit約束
一蛤肌、向工程的.git目錄下添加約束文件
二、cd 到工程中裸准,含.git的目錄下。
執(zhí)行如下命令chmod +x .git/hooks/commit-msg琅锻,其中.git/hooks/commit-msg為約束文件路徑向胡。
三、約束文件具體內(nèi)容為
#!/bin/bash
# 獲取提交消息
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# 定義提交消息類型的正則表達(dá)式
# 以類型開(kāi)頭僵芹,類型可以是 feat, fix, docs, style, refactor, perf, test, build, revert, chore
TYPE_REGEX="^(feat|fix|docs|style|refactor|perf|test|build|revert|chore): "
# 檢查提交消息類型是否符合規(guī)范
if ! [[ "$COMMIT_MSG" =~ $TYPE_REGEX ]]; then
echo "Error: Commit message type is invalid."
echo "Detail:"
echo "Your commit message was: '$COMMIT_MSG'"
echo "It should start with one of the following types followed by a colon and a space:"
echo "feat, fix, docs, style, refactor, perf, test, build, revert, chore"
echo "Example: 'feat: Add user authentication feature'"
exit 1
fi
# 獲取描述信息部分
DESCRIPTION=$(echo "$COMMIT_MSG" | sed -E "s/$TYPE_REGEX//")
# 檢查描述信息字?jǐn)?shù)是否在 2 到 50 個(gè)字符之間
if [ ${#DESCRIPTION} -lt 2 ] || [ ${#DESCRIPTION} -gt 50 ]; then
echo "Error: Commit message description length is invalid."
echo "Detail:"
echo "Your commit message was: '$COMMIT_MSG'"
echo "The description should be between 2 and 50 characters long."
echo "Example: 'feat: Add user authentication feature'"
exit 1
fi
exit 0