您的位置:首页 > 其它

SVN钩子运用(二)

2015-06-16 12:57 239 查看
一、Svn钩子脚本简介
钩子脚本的具体写法就是操作系统shell脚本程序写法,请根据自己SVN所在的操作系统和shell程序进行相应的开发。

钩子脚本就是被某些版本库事件触发的程序,例如创建新版本或修改未被版本控制的属性。每个钩子都能掌管足够的信息来了解发生了什么事件,操作对象是什么以及触发事件用户的账号。根据钩子的输出或者返回状态,钩子程序能够以某种方式控制该动作继续执行,停止或者挂起。

svn的hooks模版功能介绍
start-commit提交前触发事务
pre-commit提交完成前触发事务
post-commit提交完成时触发事务
pre-revprop-change版本属性修改前触发事务
post-revprop-change版本属性修改后触发事务

post-commit
在提交完成,成功创建版本之后执行该钩子,提交已经完成,不可更改,因此脚本返回值被忽略

post-lock
对文件进行加锁操作之后执行该脚本

post-revprop-change
在修改revision属性之后,执行该脚本。因为修改稿已经完成,不可更改,因此本脚本的返回值被饭忽略
(不过实际上的实现似乎是该脚本的正确执行与否影响属性修改)

post-unlock
对文件进行解锁操作之后执行该脚本

pre-commit
在Subversion transaction完毕之后,在提交之前,执行该脚本

pre-revprop-change
在修改revision属性之前,执行该脚本

start-commit
对客户端还没有向服务器提交数据之前,即还没有建立Subversion transaction(缩写txn)之前,执行该脚本

默认情况下,钩子的子目录中包含各种版本库钩子模板
[root@svn ~]# ls -l /application/svndata/sadoc/hooks/
总用量 36
-rw-r--r-- 1 root root 1977 6月 15 16:42 post-commit.tmpl
-rw-r--r-- 1 root root 1638 6月 15 16:42 post-lock.tmpl
-rw-r--r-- 1 root root 2289 6月 15 16:42 post-revprop-change.tmpl
-rw-r--r-- 1 root root 1567 6月 15 16:42 post-unlock.tmpl
-rw-r--r-- 1 root root 3426 6月 15 16:42 pre-commit.tmpl
-rw-r--r-- 1 root root 2410 6月 15 16:42 pre-lock.tmpl
-rw-r--r-- 1 root root 2786 6月 15 16:42 pre-revprop-change.tmpl
-rw-r--r-- 1 root root 2100 6月 15 16:42 pre-unlock.tmpl
-rw-r--r-- 1 root root 2780 6月 15 16:42 start-commit.tmpl

[root@svn hooks]# cp post-commit.tmpl post-commit
[root@svn hooks]# vim post-commit
REPOS="$1"
REV="$2"

mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
touch /tmp/$(date +%MS).log

[root@svn hooks]# chmod 755 post-commit

二、在客户端下

在D:\oldboy\trunk\test\tom.txt












四、在返回svn服务器
提交完成,触发程序,在/tmp下多了日志文件
[root@svn hooks]# ll -lrt /tmp
总用量 16
drwx------. 2 root root 16384 4月 24 17:56 lost+found
-rw-r--r-- 1 root root 0 6月 16 11:04 04S.log
-rw-r--r-- 1 root root 0 6月 16 11:04 0452.log
-rw-r--r-- 1 root root 0 6月 16 11:06 0644.log
-rw-r--r-- 1 root root 0 6月 16 11:12 1224.log
-rw-r--r-- 1 root root 0 6月 16 11:15 1548.log

五、钩子脚本post-commit

REPOS="$1"
TXN="$2"
#此处更改大小限制,这里是5M
MAX_SIZE=5242880
#此处增加限制文件后比缀名
FILTER='\.(zip|rar|o|obj|tar|gz)$'
#Make sure that the log message contains some test

SVNLOOK=/usr/bin/svnlook

#LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | wc -c`

if [ "$LOGMSG" -lt 9 ];
then
echo -e "nLog message cann't be empty! you must input more than 8 chars as comment!." 1>&2
exit 1
fi

files=$($SVNLOOK changed -t $TXN $REPOS |cut -d "" -f 4-)
#echo "$files">&2
#echo "$r">&2
#exit 1

rc=0

echo "$files" | while read f;
do
#check file type

filesize=`$SVNLOOK cat -t "$TXN" "$REPOS" "$f" | wc -c `

if [ "filezie" -gt "$MAX_SIZE" ];
then
echo "File $f is too large(must <+$MAX_SIZE)B" >&2
exit 1
fi
done
#All checks passed,so allow the commit.
if [ $? -eq 1 ];
then
exit 1
else
exit 0
fi

本文出自 “散人” 博客,请务必保留此出处http://zouqingyun.blog.51cto.com/782246/1662285
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: