所以,
我已经为pre-push编写了一个简单的git-hooks,它在Linux或Mac上运行得很好,但在Windows上不起作用。
脚本: 尝试将提交消息与正则表达式匹配,如果匹配则返回0,否则退出。
根据我读过的文章,他们说钩子应该正常工作。
命令:
if [[ "$message" =~ "$regular_expression" ]];
错误:
.git/hooks/pre-push: line 6: conditional binary operator expected
.git/hooks/pre-push: line 6: syntax error near `=~'
.git/hooks/pre-push: line 6: ` if [[ "$message" =~ "$regular_expression" ]]; then'
显然它似乎失败了,并且#34; [["和"]]"。
现在我也尝试删除双括号并只保留一个。
命令:
if [ "$message" =~ "$regular_expression" ];
错误:的
.git/hooks/pre-push: line 6: [: =~: binary operator expected
This message is flawed: TRY-1 Sample
有人知道如何解决这个问题吗?
答案 0 :(得分:2)
Git for Windows附带的bash版本不支持bash条件表达式中的=~
构造。看起来在bash 3.0中引入了=~
运算符,但是当Git for Windows使用bash 3.1时,似乎缺少了这个运算符。
$(echo $message | grep "$regexp")
可能会替代。例如:
$ bash -c '[[ "hello" =~ "^h" ]]'
bash: -c: line 0: conditional binary operator expected
bash: -c: line 0: syntax error near `=~'
bash: -c: line 0: `[[ "hello" =~ "^h" ]]'
$ bash -c '[ $(echo hello | grep "^h") ] && echo matched || echo nomatch'
matched
<强>更新强>
这是一个示例脚本,用于使用Git for Windows bash匹配类似的内容:
#!/bin/bash
#
# grep returns 0 on matching something, 1 whn it fails to match
msg='TEST-111 Sample'
re='([A-Z]{2,8}-[0-9]{1,4}[[:space:]])+[A-Za-z0-9]+[[:space:]]*[A-Za-z0-9]+$'
rx='^([A-Z]{2,8}-[0-9]{1,4})[[:space:]][[:alnum:]]+$'
echo $msg | grep -qE "$rx"
[ $? = 0 ] && echo matched || echo nomatch
此脚本使用第二个正则表达式返回匹配的示例短语。它不是很清楚原始表达式试图匹配的东西 - 看起来像多个单词所以我不确定为什么你不匹配.*$
。但是,这显示了尝试regexp的方法。注意:我们使用扩展正则表达式([[:space:]]
),因此我们必须使用grep -E
。此外,我们还需要注意引用,因为正则表达式中使用了$
。