第二个颠覆预提交钩子不起作用



我正在尝试添加第二个预提交脚本,当我将其放入钩子中时,它似乎没有捕获。

第一个脚本基本上锁定了文件,使其无法编辑。第二个脚本查看路径并将字符串值与正在提交的文件进行比较,如果匹配,则出错。

#!/bin/sh
REPOS="$1"
TXN="$2"
GREP=/bin/grep
SED=/bin/sed
AWK=/usr/bin/awk
SVNLOOK=/usr/bin/svnlook
AUTHOR=`$SVNLOOK author -t "$TXN" "$REPOS"`
if [ "$AUTHOR" == "testuser" ]; then
exit 0
fi
if [ "$AUTHOR" == "" ]; then
exit 0
fi
CHANGED=`$SVNLOOK changed -t "$TXN" "$REPOS" | $GREP "^[U|A]" | $AWK '{print $2}'`
COMPARE=`$SVNLOOK diff -t "$TXN" "$REPOS"`
#Operation 001 Beginning
#Restrict users from commiting against testfile
for PATH in $CHANGED
do
if [[ "$PATH" == *path/to/file/testfile.txt ]]; then
#allow testuser to have universal commit permissions in this path.
if [ "$AUTHOR" == "testuser" ]; then
exit 0
else
#User is trying to modify testfile.txt
echo "Only testuser can edit testfile.txt." 1>&2
exit 1
fi
fi
done
#Operation 001 Completed
#Operation 002 Beginning
#Restrict commits based on string found in file
for PATH in $COMPARE
do
if [[ "$PATH" == *path/to/look/at/only/* ]]; then
$SVNLOOK diff -t "$TXN" "$REPOS" | egrep 'string1|string2|string3' > /dev/null && { echo "Cannot commit using string1, string2 or string3 in files trying to commit" 1>&2; exit 1; }
else exit 0;
fi
done
#Operation 002 Completed

即使字符串存在,它也会保持成功提交文件。知道为什么它不会抓住它吗?

你的第一次测试:

if [ "$AUTHOR" == "testuser" ]; then
exit 0
fi

如果作者testuser,它会导致中止(退出值为零(!

所以你的第二个测试:

if [ "$AUTHOR" == "testuser" ]; then
exit 0
else
#User is trying to modify testfile.txt
echo "Only testuser can edit testfile.txt." 1>&2
exit 1
fi

这是不必要的,因为此时作者还不testuser

也许会更好而不是你的 for 循环:

if $SVNLOOK changed -t "$TXN" "$REPOS" | $GREP "^[U|A]" | $AWK '{print $2}' | grep -q 'path/to/file/testfile.txt'; then
echo "Only testuser can edit testfile.txt." 1>&2
exit 1
fi

if [[ "$PATH" == *path/to/file/testfile.txt ]]; then测试不起作用,因为此测试不理解 shell 变量(并且由于*,最好将括在引号之间(。

我会替换

for PATH in $COMPARE
do
if [[ "$PATH" == *path/to/look/at/only/* ]]; then

部分到

if echo ${COMPARE} | grep -q "path/to/look/at/only"; then

最新更新