第二个颠覆预提交挂钩不起作用
Second subversion pre-commit hook not working
我正在尝试添加第二个预提交脚本,当我将它放入钩子时它似乎没有捕捉到。
第一个脚本基本上锁定了一个文件,使其无法编辑。第二个脚本查看路径并将字符串值与正在提交的文件进行比较,如果匹配则出错。
#!/bin/sh
REPOS=""
TXN=""
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 }'`
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 }' | 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
我正在尝试添加第二个预提交脚本,当我将它放入钩子时它似乎没有捕捉到。
第一个脚本基本上锁定了一个文件,使其无法编辑。第二个脚本查看路径并将字符串值与正在提交的文件进行比较,如果匹配则出错。
#!/bin/sh
REPOS=""
TXN=""
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 }'`
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 }' | 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