SVN学习笔记三——钩子的应用

钩子脚本的具体写法就是操作系统中shell脚本程序的写法,请根据自己SVN所在的操作系统和shell程序进行相应的写作
所谓钩子就是与一些版本库事件触发的程序,例如新修订版本的创建,或是未版本化属性的修改。每个钩子都会被告知足够多的信息,包括那是什么事件,所操作的对象,和触发事件的用户名。通过钩子的输出或返回状态,钩子程序能让工作继续、停止或是以某种方式挂起。

所以钩子实际上是一个能够针对svn的操作触发特定操作的脚本,其实看一下其中的模板可以知道,钩子也是shell脚本,通过这些脚本来完成一些我们想要实现的功能而已。


[root@mylinux ~]# tree /opt/svndata/idc/

/opt/svndata/idc/

├── conf

│   ├── authz

│   ├── passwd

│   └── svnserve.conf

├── db

│   ├── current

│   ├── format

│   ├── fsfs.conf

│   ├── fs-type

│   ├── min-unpacked-rev

│   ├── rep-cache.db

│   ├── revprops

│   │   └── 0

│   │       ├── 0

│   │       ├── 1

│   │       ├── 2

│   │       ├── 3

│   │       └── 4

│   ├── revs

│   │   └── 0

│   │       ├── 0

│   │       ├── 1

│   │       ├── 2

│   │       ├── 3

│   │       └── 4

│   ├── transactions

│   ├── txn-current

│   ├── txn-current-lock

│   ├── txn-protorevs

│   ├── uuid

│   └── write-lock

├── format

├── hooks

│   ├── post-commit.tmpl

│   ├── post-lock.tmpl

│   ├── post-revprop-change.tmpl

│   ├── post-unlock.tmpl

│   ├── pre-commit.tmpl

│   ├── pre-lock.tmpl

│   ├── pre-revprop-change.tmpl

│   ├── pre-unlock.tmpl

│   └── start-commit.tmpl

├── locks

│   ├── db.lock

│   └── db-logs.lock

└── README.txt


其中,hooks目录即idc库下对应的钩子目录,里面有一些钩子的模板,以.tmpl结尾的文件。我们可以将后缀名去掉,并赋予执行权限,就可以用作钩子。但是要注意权限的指派,最好是700权限。

-rwx------ 1 root root 2238 Sep  2 10:10 post-commit

-rwx------ 1 root root 1075 Sep  2 10:02 pre-commit

提示
由于安全原因,Subversion版本库在一个空环境中执行钩子脚本—就是没有任何环境变量,甚至没有$PATH或%PATH%。由于这个原因,许多管理员会感到很困惑,它们的钩子脚本手工运行时正常,可在Subversion中却不能运行。要注意,必须在你的钩子中设置好环境变量或为你的程序指定好绝对路径。


下面附上两个常用的钩子例子:

post-commit

1
2
3
4
5
6
7
8
9
10
# 文件提交到idc库中后,自动同步到/tmp目录下
REPOS= "$1"
REV= "$2"
export  LC_CTYPE= "en_US.UTF-8"
export  LC_ALL=
LogPath= "/opt/svnlog"
[ ! -d ${LogPath} ] &&  mkdir  ${LogPath} -p 
SVN= /usr/bin/svn
$SVN update --username test01 --password 123456  /opt/data/www
[ $? - eq  0 ] &&  /usr/bin/rsync  -avz --delete  /opt/data/www  /tmp/


pre-commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/sh
#
REPOS= "$1"
TXN= "$2"
export  LC_CTYPE =  "en_US.UTF-8"
export  LC_ALL =
#set the max size for the commit file
MAX_SIZE=1024000
# set the filter for the commit file type
FILTER= '\.(zip|rar|o|tar|gz|obj|exe)$'
# Make sure that the log message contains some text.
SVNLOOK= /usr/bin/svnlook
LogMsg=`$SVNLOOK log -t  "$TXN"  "$REPOS"  | grep  "[a-zA-Z0-9]" | wc  -c`
if  "$LogMsg"  -lt 8 ]; then
     echo  -e  "Log message cann't be empty! You must input more than 8 chars as commit!"  1>&2
     exit  1
fi
files=$($SVNLOOK changed -t $TXN $REPOS| awk  '{print $2}' )
for  in  $files
    do 
        #check file type
        if  echo  $f| tr  A-Z a-z| grep  -Eq $FILTER
           then 
                echo  "File $f is not allowed ($FILTER) file type."  >&2
                exit  1
        fi
        
        #check file size
        filesize=$($SVNLOOK  cat  -t $TXN $REPOS $f| wc  -c)
        if  "$filesize"  -gt  "$MAX_SIZE"  ]
           then
                echo  "File $f byond the max size limited($MAX_SIZE)."  >&2
                exit  1
        fi
done
#All checks passed,so allow the commit.
exit  0

第二个钩子是在提交前对文件进行检查,主要有三个作用:

1)提交时一定要填写评论,且评论不得少于8个字符;

2)不能提交大小超过10M的文件

3)不能提交zip/rar/o/tar/gz/obj/exe等类型的文件。


下面是测试钩子是否生效的截图:

wKiom1fJHmnSp_PqAADRrOvaBew913.png-wh_50

wKioL1fJHmrzAOdaAAFEaa99roY689.png-wh_50



本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1845583,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值