SVN的钩子--限制强制写日志(log)

 

SVN的钩子--限制强制写日志(log)
2010/07/29 16:56

SVN本身并不提供这种强制写log的功能,而是通过一系列的钩子程序(我们称为hook脚本),在提交之前(pre-commit),提交过程中(start-commit),提交之后(post-commit),调用预定的钩子程序来完成一些附加的功能。

本次我们要实现的是在提交到版本库之前检查用户是否已经写了注释,当然要使用pre-commit这个钩子程序。我们打开SVN的repository下的hook目录,可以发现有好几个文件,其中一个是“pre-commit.tmpl”。这个文件是一个模板文件,它告诉了我们如何实现提交前控制。打开该模板文件,我们看到如下一段说明:

# The pre-commit hook is invoked before a Subversion txn is
# committed.   Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
#    [1] REPOS-PATH    (the path to this repository)                             
#    [2] TXN-NAME      (the name of the txn about to be committed) 
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# If the hook program exits with success, the txn is committed; but
# if it exits with failure (non-zero), the txn is aborted, no commit
# takes place, and STDERR is returned to the client.    The hook
# program can use the 'svnlook' utility to help it examine the txn.


我们看到在一个提交事务执行之前,该hook脚本会被调用。然后向该脚本传递两个参数:REPOS-PATH和TXN-NAME,一个是用户要提交的URL,一个是本次事务的一个事务号。如果提交成功则返回0,否则返回其它非0结果。那么我们的钩子程序就是要在事务提交之前,拦截这些请求,然后通过svnlook命令来检查是否已经写了log。

示例代码


下面这段代码是网上直接拷贝的一段代码:

@echo off
setlocal
set REPOS=%1
set TXN=%2   
rem check that logmessage contains at least 10 characters
svnlook log "%REPOS%" -t "%TXN%" | findstr " ." > nul
if %errorlevel% gtr 0 goto err
exit 0
:err
echo Empty log message not allowed. Commit aborted! 1>&2
exit 1

 

下面解析一下绿色高亮处代码的作用:

  set REPOS=%1 
    set TXN=%2
  还记得我们前面提到的但事务提交时,会传递两个参数吗?这里就是分别用来接收URL和事务号的

  svnlook log "%REPOS%" -t "%TXN%" | findstr ".........." > nul 
   这句是核心程序。首先svnlook log是用来查看某个版本库某次提交的log的,那么我们怎么知道这两个
  参数呢?答案就是我们前面已经保存的REPOS和TXN参数。
  它的作用是查看%REPOS%这个URL第%TXN%次提交的log信息,那么| findstr ".........."呢?细心
  的读者会发现这里有10个圆点号,即表示10个字符。
  整句的作用就是先获取当前提交的log内容,然后判断是否有10个字符以上

  echo Empty log message not allowed. Commit aborted! 1>&2 
  这句话的作用是当提交检查失败时,输出的提示信息

 

以下代码在Linux下亲测可行: 

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" |grep "[a-zA-Z0-9]" || exit

 

注意:在Linux下需要将pre-commit.tmpl改正成pre-commit

          并且更改运行权限:chmod +x pre-commit

          在windows下需要将pre-commit.tmpl改正pre-commit.bat

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值