SVN本身并不提供這種強(qiáng)制寫log的功能,而是通過(guò)一系列的鉤子程序(我們稱為hook腳本),在提交之前(pre-commit),提交過(guò)程中(start-commit),提交之后(post-commit),調(diào)用預(yù)定的鉤子程序來(lái)完成一些附加的功能。
本次我們要實(shí)現(xiàn)的是在提交到版本庫(kù)之前檢查用戶是否已經(jīng)寫了注釋,當(dāng)然要使用pre-commit這個(gè)鉤子程序。我們打開(kāi)SVN的repository下的hook目錄,可以發(fā)現(xiàn)有好幾個(gè)文件,其中一個(gè)是“pre-commit.tmpl”。這個(gè)文件是一個(gè)模板文件,它告訴了我們?nèi)绾螌?shí)現(xiàn)提交前控制。打開(kāi)該模板文件,我們看到如下一段說(shuō)明:
# 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.
我們看到在一個(gè)提交事務(wù)執(zhí)行之前,該hook腳本會(huì)被調(diào)用。然后向該腳本傳遞兩個(gè)參數(shù):REPOS-PATH和TXN-NAME,一個(gè)是用戶要提交的URL,一個(gè)是本次事務(wù)的一個(gè)事務(wù)號(hào)。如果提交成功則返回0,否則返回其它非0結(jié)果。那么我們的鉤子程序就是要在事務(wù)提交之前,攔截這些請(qǐng)求,然后通過(guò)svnlook命令來(lái)檢查是否已經(jīng)寫了log。
示例代碼
下面這段代碼是網(wǎng)上直接拷貝的一段代碼:
@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下親測(cè)可行:
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
并且更改運(yùn)行權(quán)限:chmod +x pre-commit
在windows下需要將pre-commit.tmpl改正pre-commit.bat
聯(lián)系客服