git commit 規(guī)范及約束設(shè)置

業(yè)界通用規(guī)范

AngularJS 在 github上 的提交記錄被業(yè)內(nèi)許多人認(rèn)可,逐漸被大家引用

格式

1
type(scope):subject

  1. type(必須) : commit 的類別,只允許使用下面幾個(gè)標(biāo)識(shí)
  • feat : 新功能

  • fix : 修復(fù)bug

  • docs : 文檔改變

  • style : 代碼格式改變

  • refactor : 某個(gè)已有功能重構(gòu)

  • perf : 性能優(yōu)化

  • test : 增加測試

  • build : 改變了build工具 如 grunt換成了 npm

  • revert : 撤銷上一次的 commit

  • chore : 構(gòu)建過程或輔助工具的變動(dòng)

    1. scope(可選) : 用于說明 commit 影響的范圍,比如數(shù)據(jù)層、控制層、視圖層等等,視項(xiàng)目不同而不同。

    2. subject(必須) : commit 的簡短描述,不超過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á)式
# 以類型開頭,類型可以是 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。