Only to find a successful way, not to find excuses for failure!
分类: 系统运维
2012-12-27 10:17:10
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。
示例代码
REPOS="$1" TXN="$2" # 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 48 ]; then
#-eq 等于号 -gt 大于号 -lt小于号 ,显示输入的长短为10(如果数字或者字母表示最少要写9个,如果汉字是一个根据自
己的需求可以任意修改
echo -e "\n至少输入4个汉字" >&2
exit 1
fi
exit 0