开发svn hook阻止svn仓库特定分支的commit log中不包含关键字的提交

使用批处理脚本实现开发svn hook(pre-commit) 阻止svn仓库特定分支的commit log中不包含关键字的提交(批处理脚本+shell脚本实现)
  • 实现逻辑
    1)使用svnlook 的dirs-changed子命令去获取当前仓库的提交事务中子文件发生变化的目录,
    2)如果发现该路径下有该子路径则将DIR_FOUND标志变量设置为true,接下来对该子路径进行判断,
    3)如果发现标志的值是true,则在此子路径上使用svnlook log命令获取当前仓库提交事务的log中是否包含提交关键字,如果没有则限制提交。
  • 想让svn hook对svn仓库多个分支进行限制的实现逻辑
    1)如果想设置多个子路径,则可以在用svnlook 的dirs-changed子命令的这个for循环中增加一条子路径判断语句,
    2)并增加一个DIR_FOUND1标志变量,并在下方的if条件中增加一个DIR_FOUND1标志变量是否为true的判断即可。
    注意:svn hook默认为整个仓库的根目录起效,我们如果只想对仓库的某几个分支起效,则可这样去处理。
  1. windows环境下使用批处理脚本实现:
@echo off
setlocal

set REPOS=%1
set TXN=%2
set SVNLOOK=/usr/bin/svnlook
set KEYWORD=SOP-
set SEARCH_PATH=branches/dev
set SEARCH_PATH1=trunk
set FOUND=false
set DIR_FOUND=false
set DIR_FOUND1=false

for /f "delims=" %%G in ('svnlook dirs-changed -t %TXN% %REPOS%') do (
    echo %%G | findstr "%SEARCH_PATH%" >nul && set "DIR_FOUND=true"
    echo %%G | findstr "%SEARCH_PATH1%" >nul && set "DIR_FOUND1=true"
)

if not "%DIR_FOUND%"=="true" if not "%DIR_FOUND1%"=="true" (
    exit 0
)

for /f "delims=" %%G in ('('svnlook  log -t %TXN% %REPOS%') do (
    echo %%G | findstr "%KEYWORD%" >nul && set "FOUND=true"
)

if not "%FOUND%"=="true" (
    echo The commit message does not contain '%KEYWORD%' keyword. >&2
    exit 1
)

exit 0
  1. 在Linux下使用shell脚本实现:

REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
KEYWORD="SOP-"
SEARCH_PATH="branches/dev"
SEARCH_PATH="trunk"
DIR_FOUND=false
DIR_FOUND1=false
FOUND=false

for dir in `$SVNLOOK dirs-changed -t "$TXN" "$REPOS"`; do
    echo "$dir" | grep -q "$SEARCH_PATH" && DIR_FOUND=true
    echo "$dir" | grep -q "$SEARCH_PATH1" && DIR_FOUND1=true
done

if [ "$DIR_FOUND" != "true" ] && [ "$DIR_FOUND1" != "true" ]; then
    exit 0
fi

for log in `$SVNLOOK log -t "$TXN" "$REPOS"`; do
    echo "$log" | grep -q "SOP-[0-9]\+" && FOUND=true
done

if [ "$FOUND" != "true" ]; then
    echo "The commit message does not contain '$KEYWORD' keyword." >&2 
    exit 1
fi

exit 0

你可能感兴趣的:(bat,SVN,svn)