在把SVN服务器搭建好之后,其中有个versions目录需要限制提交,就是只能Add提交,不能Modify、Delete操作提交。
于是研究了钩子的使用,特做如下记录。
1、确认处理的时机
根据需求,必然要在提交之前进行处理。在查看了网上的资料,以及实际目录下的文件之后,必然是选择 pre-commit.impl进行编辑。
2、着手处理
(1)将 pre-commit.impl 文件重命名为 pre-commit,就是把后缀名干掉。
(2)编辑文件内容。我选择的是 bash。内容如下
#!/bin/bash
REPOS="$1"
TXN="$2"
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
SVNROOT=/home/svn/repos
VERSION=version
# wc -c, count in byte
# wc -m, count in charactor
LOGMSG=`$SVNLOOK log "$REPOS" -t "$TXN" | wc -m`
if [ $LOGMSG -lt 10 ]; then
echo -e "Log message cann‘t be empty! you must input more than 10 chars as comment!." 1>&2
exit 1
fi
StopFlag=0
STOP_MESSAGE="contact SVN repository admin."
if [ "x$REPOS" == "x$SVNROOT" ]; then
while read line; do
items=($line)
if [ ${#items[@]} == 2 ]; then
if [ "${items[1]%%/*}" == "$VERSION" ] && [ "x${items[0]}" != "xA" ]; then
echo -e "\"$REPOS/$VERSION\" folder only support add action.\n$line" 1>&2
StopFlag=$(($StopFlag+1))
echo -e "\nmust stop=$StopFlag" >>$SVNROOT/1.log
break
fi
else
STOP_MESSAGE="\"$REPOS/$VERSION\" folder only support add action.\n$line"
StopFlag=$(($StopFlag+1))
break
fi
done <<< $($SVNLOOK changed $REPOS -t $TXN)
else
if [ "x$REPOS" == "x$SVNROOT/$VERSION" ]; then
while read line; do
items=($line)
if [ ${#items[@]} == 2 ] && [ "x${items[0]}" -ne "xA" ]; then
STOP_MESSAGE="\"$REPOS\" folder only support add action.\n$line" 1>&2
StopFlag=$(($StopFlag+1))
break
else
STOP_MESSAGE="\"$REPOS\" folder only support add action.\n$line" 1>&2
StopFlag=$(($StopFlag+1))
break
fi
done <<< $($SVNLOOK changed $REPOS -t $TXN)
fi
fi
echo "stop? stop=$StopFlag" >>$SVNROOT/1.log
if [ $StopFlag -ne 0 ]; then
echo -e $STOP_MESSAGE 1>&2
exit 1
fi
# Exit on all errors.
set -e
# Check that the author of this commit has the rights to perform
# the commit on the files and directories being modified.
if [ -e "$REPOS"/hooks/commit-access-control.pl ] && [ -e "$REPOS"/hooks/commit-access-control.cfg ];
then
"$REPOS"/hooks/commit-access-control.pl "$REPOS" $TXN "$REPOS"/hooks/commit-access-control.cfg
fi
# All checks passed, so allow the commit.
exit 0
配置之后,进行了简单的验证,确认功能正常。先用着 ,以后实际有啥问题再补充记录。
3、弯路
在配置过程中,走了些弯路。
(1)如何获取本地提交的修改清单?
我前面使用svnlook的时候,只带了 repository 参数,没有带 transaction 参数,导致获取的提交信息全部是上一次已经提交的记录信息。为此查找无数网页,最后顿悟才发现是自己少传了参数。脚本开头传递进来的两个参数,第一个就是仓库地址,第二个就是本次事务ID。对于本次操作的各类信息,都是基于这个事务ID的。
(2)如何在shell的循环里面变更变量的值?
这个我搞来搞去,没搞定,翻了不少网页,最后确认将 command | while xx do ... done 模式改成 while xx do ... done <<< $(command) 模式才,且在循环里面采用 var=$(($var+1)) 这样的写法才达到目的。
(4)配置后,报错提示“commit-access-control.pl”找不到的错误
找来找去,没看到网上有什么资料,我 直接先判断文件是否存在然后屏蔽 这个文件的操作。
<完>
原文:https://www.cnblogs.com/ssdq/p/12693086.html