Regex Git hook commit msg确保提交消息包含符合正则表达式的字符串

Regex Git hook commit msg确保提交消息包含符合正则表达式的字符串,regex,linux,git,bash,githooks,Regex,Linux,Git,Bash,Githooks,我试图使bash脚本用作git提交消息挂钩。本质上,我希望确保提交消息包含与正则表达式匹配的字符串。我的正则表达式是正确的,但我不确定如何检查.git/COMMIT\u 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

我试图使bash脚本用作git提交消息挂钩。本质上,我希望确保提交消息包含与正则表达式匹配的字符串。我的正则表达式是正确的,但我不确定如何检查
.git/COMMIT\u 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

我是这样想的:

#!/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

您是否正在寻求帮助以确保此代码适用于所有情况?(我就是这样解释的)。我们必须了解更多关于你试图陷害的案件。实际上,有一件事可能会导致代码出现问题。我建议将行更改为
if![[“$1”=~“$regex”];然后
。祝你好运我只是想弄清楚如何读取文件的内容并对我的正则表达式运行它。您是否需要帮助以确保此代码适用于所有情况?(我就是这样解释的)。我们必须了解更多关于你试图陷害的案件。实际上,有一件事可能会导致代码出现问题。我建议将行更改为
if![[“$1”=~“$regex”];然后
。祝你好运我只是想弄清楚如何读取文件的内容,并根据我的正则表达式运行它。