这篇文章继续介绍Subverison Edge中如何使用SVN的Hook机制。
Subversion Edge环境准备和仓库创建的操作可参看如下文章:
关于SVN的hook使用,详细可参看如下文章:
在Subversion Edge中设定Hook可以在创建的仓库的时候,也可以在创建之后,在仓库列表页面选中相应的仓库,点击info按钮
选中pre-commit模版,点击edit按钮
缺省此模版的信息如下所示
#!/bin/sh
# POST-COMMIT HOOK
#
# The post-commit hook is invoked after a commit. Subversion runs
# this hook by invoking a program (script, executable, binary, etc.)
# named 'post-commit' (for which this file is a template) with the
# following ordered arguments:
#
# [1] REPOS-PATH (the path to this repository)
# [2] REV (the number of the revision just committed)
# [3] TXN-NAME (the name of the transaction that has become REV)
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# Because the commit has already completed and cannot be undone,
# the exit code of the hook program is ignored. The hook program
# can use the 'svnlook' utility to help it examine the
# newly-committed tree.
#
# On a Unix system, the normal procedure is to have 'post-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'post-commit' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# 'post-commit.bat' or 'post-commit.exe',
# but the basic idea is the same.
#
# The hook program typically does not inherit the environment of
# its parent process. For example, a common problem is for the
# PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
#
# Here is an example hook script, for a Unix /bin/sh interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and
# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/
REPOS="$1"
REV="$2"
TXN_NAME="$3"
mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
我们这里进行如下修改
#!/bin/sh
REPOS="$1"
TXN="$2"
LOG_INFO=`svnlook log -t "$TXN" "$REPOS"`
# check empty comment
if [ _"$LOG_INFO" = _"" ]; then
echo "" 1>&2
echo " ## LOG CHECK : NG: [$LOG_INFO] does not match the rules." 1>&2
exit 1
fi
# check log format with # :
TICKET_NO=`echo "$LOG_INFO" |sed s/#//g |awk -F : '{print $1}'`
COMMENT_MSG=`echo "$LOG_INFO" |sed s/#//g |awk -F : '{print $2}'`
if [ _"$TICKET_NO" = _"" -o _"$COMMENT_MSG" = _"" ]; then
echo "" 1>&2
echo " ## LOG CHECK : NG: [$LOG_INFO] does not match the rules." 1>&2
echo " Sample : # BUG1002: feature F1001 bug fixed" 1>&2
exit 1
fi
# pre-commit check passed.
exit 0
连续执行两次空comment都成功了
liumiaocn:trunk liumiao$ cat ReadME.md
Created in MacOS
liumiaocn:trunk liumiao$ echo "test for pre-commit hook" >>ReadME.md
liumiaocn:trunk liumiao$ echo "new line for ReadMe" >>ReadME.md
liumiaocn:trunk liumiao$ svn diff
Index: ReadME.md
===================================================================
--- ReadME.md (revision 3)
+++ ReadME.md (working copy)
@@ -1,2 +1,3 @@
Created in MacOS
test for pre-commit hook
+new line for ReadMe
liumiaocn:trunk liumiao$
liumiaocn:trunk liumiao$ svn commit -m ""
Sending ReadME.md
Transmitting file data .done
Committing transaction...
Committed revision 4.
liumiaocn:trunk liumiao$
原因是上述的模版只是模版,而没有真正起作用(详细参看文初的链接文章的说明),在Subversion Edge中还提供了rename的功能,实际就是真正将模版起效的过程,选中pre-commit模版,点击rename按钮
将.tmpl的后缀删除,点击OK按钮并保存
再次从客户端提交一个没有comment的变更,可以看到pre-comment的hook机制已经生效。
liumiaocn:trunk liumiao$ echo "test for pre-commit hook" >>ReadME.md
liumiaocn:trunk liumiao$ svn diff
Index: ReadME.md
===================================================================
--- ReadME.md (revision 4)
+++ ReadME.md (working copy)
@@ -1,3 +1,4 @@
Created in MacOS
test for pre-commit hook
new line for ReadMe
+test for pre-commit hook
liumiaocn:trunk liumiao$
liumiaocn:trunk liumiao$ svn commit -m ""
Sending ReadME.md
Transmitting file data .done
Committing transaction...
Committed revision 5.
Warning: post-commit hook failed (exit code 1) with output:
svnlook: E160043: Expected FS format between '1' and '4'; found format '6'
## LOG CHECK : NG: [] does not match the rules.
liumiaocn:trunk liumiao$