为了禁止包含本地文件路径的提交,我编写了一个git pre-commit钩子。通常git用户在提交和推送之前忘记用与服务器相关的路径替换本地路径,并且每次发生这种情况都会浪费很多时间。
我的正则表达式在使用指定文件在CLI上调用ack时工作正常,但在我的钩子脚本中调用它时拒绝匹配文本。我认为也许回应是责任,或者问题与换行/换行有关。
我的剧本:
if git-rev-parse --verify HEAD >/dev/null 2>&1
then
export rev="`git-diff --cached`"
echo "'$rev'"
export i="`echo "$rev" | ack '\+ *(?!#)[a-z$=" ]*/home/'`"
echo
echo "'$i'"
if [ ! -z "$i" ]
then
echo "Error: Attempt to commit file with uncommented path to local files"
echo
echo "This usually means that you are trying to commit code that has been"
echo "localised to your own machine, and that the paths will fail when"
echo "the code is run on the server."
echo
echo "Please check if you really meant to commit this code. Commenting out"
echo "the code in question will prevent this message from appearing."
echo
echo "Details: $i"
exit 1
fi
fi
这应该会阻止添加新路径的提交,就像这样,就像git diff中所显示的那样:
+ $foo = "/home/user/file.txt
但是应该接受注释掉的路径,例如:
+ #$foo = "/home/user/file.txt
为什么我的脚本没有正确检测未注释的本地路径?