我编写了一个 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
为什么我的脚本没有正确检测到未注释的本地路径?