在Windows上执行git-hooks错误 [英] Executing git-hooks on windows errors out
问题描述
所以
我为pre-push写了一个简单的git-hooks,在Linux或Mac上都可以正常运行,但在Windows上则无法运行.
I have written a simple git-hooks for pre-push which works just fine on Linux or Mac, but doesn't work on Windows.
脚本: 尝试使提交消息与正则表达式匹配,如果匹配则退出,否则应返回0.
Script: Tries to match the commit message with a regular expression, and should return 0 if matches or else exit.
根据我阅读的文章,他们说该挂钩应该可以正常工作.
Based on the articles I read, they say that the hook should just work.
命令:
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'
因此,显然在"[[[和]]"上似乎失败了.
So apparently it seems to be failing on "[[" and "]]".
现在我也尝试过删除双括号并仅保留其中一个.
Now I have also tried removing the double brackets and keep only one.
命令:
if [ "$message" =~ "$regular_expression" ];
错误:
.git/hooks/pre-push: line 6: [: =~: binary operator expected
This message is flawed: TRY-1 Sample
有人知道如何解决这个问题吗?
Does anybody know how to solve this issue ?
推荐答案
Git for Windows附带的bash版本不支持bash条件表达式中的=~
构造.看来=~
运算符是bash 3.0中引入的,但是当Git for Windows使用bash 3.1时,似乎缺少了该运算符.
The =~
construct in bash conditional expressions is not supported in the version of bash shipped with Git for Windows. It looks like the =~
operator was introduced in bash 3.0 but while Git for Windows is using bash 3.1 it seems to be missing this operator.
可能$(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来匹配相似的内容:
Here is an example script that works to match something similar using the 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
此脚本使用第二个正则表达式返回与示例短语匹配的结果.目前尚不清楚原始表达式试图匹配什么-看起来像多个单词,所以我不确定为什么不匹配.*$
.但是,这显示了一种尝试正则表达式的方法.注意:我们正在使用扩展的正则表达式([[:space:]]
),因此我们必须使用grep -E
.另外,由于正则表达式中使用了$
,因此我们必须谨慎行事.
This script returns matched for the sample phrase using the second regular expression. Its not really clear what the original expression is attempting to match up -- looks like multiple words so I'm not sure why you don't just match .*$
. However, this shows a way to try out the regexp. Note: we are using extended regular expressions ([[:space:]]
) so we have to use grep -E
. Also we have to take some care about quoting as $
is being used in the regexp.
这篇关于在Windows上执行git-hooks错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!