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
以下代码在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
联系客服