打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
使用svn pre
关于SVN提交强制加入注释
增强SVN的Commit权限控制
集成SVN源码管理和Mantis缺陷跟踪
Windows下WSH/JS实现SVN服务器钩子脚本阻止提交空日志信息和垃圾文件
svn的备份与还原怎么用脚本实现svn备份(linux/windows)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服