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
中包含的消息是否与正则表达式匹配。
#!/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 = " # This prints "MSG = .git/COMMIT_EDITMSG"
# If the commit message does not match the regex
if ! [[ =~ $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 ` # 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
我正在尝试使 bash 脚本用作 git commit-msg 挂钩。本质上,我想确保提交消息包含与正则表达式匹配的字符串。我的正则表达式是正确的,但我不确定如何检查 .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 = " # This prints "MSG = .git/COMMIT_EDITMSG"
# If the commit message does not match the regex
if ! [[ =~ $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 ` # 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