随笔 - 1, 文章 - 3, 评论 - 0, 引用 - 0
数据加载中……

Subversion命令2

六.svn hook--通过调用SVN内部制定的SVN hook函数来调用SVN后台命令完成用户需要的操作。

     SVN Hook函数有:
  1. start-commit
  2. pre-commit
  3. post-commit:执行commit操作之后会自动执行post-commit这个钩子程序.
  4. pre-lock
  5. post-lock
  6. pre-unlock
  7. post-unlock
  8. pre-revprop-change
  9. post-revprop-change
  10. commit-lock
说明:只有 pre-commit 的返回值被检查,post-commit 即便运行错误,也不影响提交;
pre-revprop-change 和 post-revprop-change 这两个脚本任何一个脚本错误,都导致对 revision 的属性修改失败


实例1:强制用户提交日志
pre-commit:

#!/bin/sh
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
# check that logmessage contains at least 10 alphanumeric characters
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-Z0-9]" | wc -c`
if [ "$LOGMSG" -lt 10 ];
then
 echo -e "\that logmessage contains at least 10 alphanumeric characters. Commit aborted!" 1>&2
 exit 1
fi

说明:REPOS和TXN。REPOS存储的是项目repository的路径信息;TXN则是此次提交的一个事务号名称。  

扩展:提交前对配置库日志,提交文件类型,配置库大小等限定
pre-commit: 
#!/bin/sh
REPOS="$1"
TXN="$2"
RES="OK"
#此处更改大小限制,这里是10M
MAX_SIZE=10240000
#此处增加限制文件后缀名
FILTER='\.(zip|rar|o|obj|tar|gz)$'
# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | egrep "[^[:space:]]+" >/dev/null ||
      unset RES
      if [ "$RES" != "OK" ]
      then
      echo "You must input some comments for you commit" >&2
      exit 1
      fi
      files=$($SVNLOOK changed -t $TXN $REPOS |awk '{print $2}')
      for f in $files
      do
#check file type
      if echo $f|tr A-Z a-z|grep -Eq $FILTER
      then
      echo "File $f is not allow ($FILTER) file" >&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 is too large(must <=$MAX_SIZE)" >&2
      exit 1
      fi
      done
      # All checks passed, so allow the commit.
      exit 0  


实例2:允许用户修改注释信
pre-revprop-change:

#/bin/bash
REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ];
then
    exit 0;
fi
echo "Changing revision properties other than svn:log is prohibited" >&2
exit 1

扩展:只允许作者修改注释信息
pre-revprop-change:

#/bin/bash
REPOS="$1"       //配置库信息
REV="$2"         //版本信息
USER="$3"        //用户
PROPNAME="$4"   //属性名称,这里定义只允许修改注释信息,不允许修改包括作者信息,时间信息等。
ACTION="$5"      //操作名称,这里定义只允许修改,不允许删除。
SVNLOOK=/usr/bin/svnlook
AUTHOR=`$SVNLOOK author $REPOS -r $REV`      //查看需要修改文件的原有作者
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then   //只是修改日志信息
   if [ "$AUTHOR" = "$USER" ]; then             //需要修改文件注释信息的用户,就是作者本人
       exit 0
   else
      echo "Only the author or superuser can modify the log information!" >&2
      exit 1
    fi
else
 echo "Changing revision properties other than svn:log is prohibited!" >&2
 exit 1
fi

扩展:只允许作者或者超级用户修改注释信息 
pre-revprop-change:

#/bin/bash
REPOS="$1"      
REV="$2"  
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"
SVNLOOK=/usr/bin/svnlook
AUTHOR=`$SVNLOOK author $REPOS -r $REV`
Superuser=(XX1  XX2 XX3)           //定义超级用户成员
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then
   if [ "$AUTHOR" = "$USER" ]; then
       exit 0
   else
      for (( i=0;i<${#Superuser[@]};i++ ))
       do
        if [ "$USER" = "${Superuser[i]}" ]; then
           exit 0
        fi
       done
      echo "Only the author or superuser can modify the log information!" >&2
      exit 1
    fi
else
 echo "Changing revision properties other than svn:log is prohibited!" >&2
 exit 1
fi  


实例3:限制SVN代码库的大小
start-commit:

#!/bin/sh
REPOS="$1"
USER="$2"
MAX_SIZE=880000 
repos_size=`du -s $REPOS | cut -f 1`
if [ $repos_size -gt $MAX_SIZE ]; then
echo "Repositroy $REPOS has exceeded maximum size: $MAX_SIZE" 1>&2
exit 1
fi
exit 0



实例4:检测SVN版本库中特定的目录的文件修改,邮件通知
post-commit:
SVN 提交后发邮件提醒一般有以下方法:
1.基于mailer.py
2.基于svnnotify
3.基于sendmail

#!/bin/sh
REPOS="$1"
REV="$2"
SVNLOOK=/usr/bin/svnlook
export LANG=en_US.UTF-8
SVNChangedURL=`$SVNLOOK changed $REPOS | cut -b5-7`
#SVNChangedURL=`$SVNLOOK dirs-changed $REPOS | cut -b1-2`
if [ $SVNChangedURL = add ];
then
/usr/share/doc/subversion-1.5.1/tools/hook-scripts/commit-email.pl "$REPOS" "$REV" --from XX@XXX.com -h mail.XXX.com -s "XXX modify" --diff y XX@XXX.com
exit 0
fi
exit 1  


实例五:在特定时间段锁定配置库特定目录
pre-commit:

#!/bin/sh
REPOS="$1"                 //操作的版本库
TXN="$2"                   //操作的事务
export LC_TIME=C            //把日期格式设置成英文的显示,这样方便后面的判断
a=`date +%a`                //获取当前的周日期
datestart="80000"           //锁定开始时间是早上8点
dateend="180000"           //锁定结束时间是晚上6点
curTime=`date +%H%M%S`    //获取当前的时间信息,显示格式为如:180000
SVNLOOK=/usr/bin/svnlook
MTRUNK=`$SVNLOOK changed -t $TXN $REPOS | grep trunk | wc -c` //查找操作是否涉及trunk目录

if [ "$a" = "Tues" -o "$a" = "Tus" ]; then           //每周二或者每周四

   if [[ $datestart -lt $curTime ]] && [[ $curTime -lt $dateend ]]; then
      if [[ "$MTRUNK" -gt 2 ]]; then
        echo "Today is a Release day. Please commit your work after 18:00 ! Any questions, Please contract scm. Thanks for your support!" >&2
        exit 1
      else
        exit 0
      fi
   else
      exit 0
   fi
else
 exit 0

fi

posted on 2011-10-09 14:29 Joise 阅读(177) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。
该文被作者在 2011-10-09 14:31 编辑过
网站导航: