我正在跟踪关于git钩子的这一职位,在一个例子中,有一个常规的表达式来检查提交消息。
消息应该与[XXX-123] My message或[hotfix] My message匹配。
REGEX="^\[(hotfix|\w+\-[0-9]+)\]( \w+)+$"但是,运行示例"[hotfix] Fixing unit-tests"和"[PSBO-456] It's a valid one this time",我会从bash脚本中得到一个错误响应。
这是剧本
#!/usr/bin/env bash
echo "Checking commit-message format..."
## the first arg is the path to the git commit temporary file
TEMPORARY_FILE_PATH=$1
## get commit-message from the temporary file
COMMIT_MSG=`head -n1 $TEMPORARY_FILE_PATH`
## init regex to match commit-message format
REGEX="^\[(hotfix|\w+\-[0-9]+)\]( \w+)+$"
## checking commit-message format
if ! [[ $COMMIT_MSG =~ $REGEX ]];then
echo -e "Your commit-message format is not valid:\n$COMMIT_MSG\n"
echo "Valid format examples:"
echo "[PSBO-123] My commit message"
echo "[hotfix] My commit message"
exit 1
else
echo "Well done! Your commit-message is valid."
exit 0
fi发布于 2021-12-13 20:39:35
您忘记支持输入字符串中的'和-字符。而且,在这个环境中,\w是非标准的,您应该比Perl类速记字符类更依赖POSIX字符类。\w是[[:alnum:]_],\d是[[:digit:]],\s是[[:space:]]。
我建议:
REGEX='^\[(hotfix|[[:alnum:]_]+-[0-9]+)]([[:blank:]]+[[:alnum:]_'"'"'-]+)+$'请参阅regex演示 (只是为了说明它是如何工作的,不要使用regex101来测试POSIX的模式有效性)。
详细信息
^ -字符串的开始\[ -a [ char(hotfix|[[:alnum:]_]+-[0-9]+)] -a ] char([[:blank:]]+[[:alnum:]_'-]+)+ -发生一个或多个事件[[:blank:]]+ -一个或多个水平空白空间[[:alnum:]_'-]+ -一个或多个字母数字,_,'或-字符$ -字符串的末端。https://stackoverflow.com/questions/70339477
复制相似问题