负责前端开发的同事提出需求,svn提交时必须添加注释,否则有可能不知道代码是谁提交的。
搭建svn服务器时,用的是CollabNetSubversionEdge。添加用户,管理工程权限都挺方便。让某个工程在提交时添加注释,需要在svn的hooks目录下,新建一个pre-commit的文件并给它可执行权限。
代码如下:
#!/bin/bash
REPOS="$1"
TXN="$2"
SVNLOOK=/local/csvn/bin/svnlook
LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS" | grep "[a-zA-A0-9]" | wc -c)
if [ "$LOGMSG" -lt 5 ]; then
echo -e "\n [Commit]log message cann‘t be empty! YOU MUST input more than 10 chars as comment" 1>&2
exit 1
fi
根据系统自带的pre-commit.teml中的内容弄清楚这段脚本的含义:
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)
#
# [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN.
#
# If STDIN contains the line "LOCK-TOKENS:\n" (the "\n" denotes a
# single newline), the lines following it are the lock tokens for
# this commit. The end of the list is marked by a line containing
# only a newline character.
脚本命令解释:
LOGMSG=$($SVNLOOK log -t “$TXN” “$REPOS” | grep “[a-zA-Z0-9]” | wc -c)
将提交的日志信息作为便来那个输入之后,将grep英文和数字的结果作为变量
if [ "$LOGMSG" -lt 10 ]; 传入变量来判断变量长度是否小于10
-eq 等于号 -gt 大于号 -lt小于号
1. echo 错误提示信息的时候必须重定向到 stderr ,即 1>&2
2. pre-commit 文件拷贝到 hooks 目录之后,需要执行 chmod 755 pre-commit ,给 pre-commit 文件增加可执行权限。