簡體   English   中英

Git hook commit-msg 以確保提交消息包含符合正則表達式的字符串

[英]Git hook commit-msg to ensure the commit message contains a string that conforms to a regex

我正在嘗試將 bash 腳本用作 git commit-msg 鈎子。 本質上,我想確保提交消息包含與正則表達式匹配的字符串。 我的正則表達式是正確的,但我不確定如何檢查.git/COMMIT_EDITMSG中包含的消息.git/COMMIT_EDITMSG與正則表達式匹配。

#!/bin/sh
#
# This hook verifies that the commit message contains a reference to a tfs item

RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
# Regex to validate a string contains "#" followed by 4 or 5 digits anywhere in the commit message
regex="#[0-9]{4,5}($|[^0-9])"

# I NEED TO FIGURE OUT HOW TO READ THE CONTENTS OF THIS FILE AND ENSURE IT MATCHES THE REGEX
echo "MSG = $1" # This prints "MSG = .git/COMMIT_EDITMSG"

# If the commit message does not match the regex
if ! [[ $1 =~ $regex ]]; then
  echo "${RED}ERROR - Missing tfs item in commmit message.$NORMAL"
  exit 1
else
  echo "MESSAGE IS GOOD?"
fi

exit 0

我想出了這個:

#!/bin/sh
#
# This hook verifies that the commit message contains a reference to a tfs item

RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
# Regex to validate a string contains "#" followed by 4 or 5 digits anywhere in the commit message
regex="#[0-9]{4,5}($|[^0-9])"
file=`cat $1` # The file that contains the commit message

# If the commit message does not match the regex
if ! [[ $file =~ $regex ]]; then
  echo "${RED}ERROR - Missing tfs item in commmit message.$NORMAL"
  exit 1
else
  echo "MESSAGE IS GOOD?"
fi

exit 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM