Windows下的SVN钩子

35

我通过简单搜索发现,目前并没有针对Windows的SVN挂钩资源。因此,我决定在这里创建一个Wiki以将其集中起来。

如果您想要为此做出贡献,请确保提供以下信息:

  1. 挂钩名称
  2. 脚本作用
  3. 实际脚本

注意:我怀疑发布一个庞大的脚本是没有用的。


"史诗脚本"是什么?它是指Eclipse Perl Integration* (EPIC)吗? - Peter Mortensen
9个回答

11

防止空注释提交

  1. pre-commit
  2. 防止提交空注释

来源:

"c:\Program Files\Subversion\bin\svnlook.exe" log -t %2 %1 | FindStr [a-zA-Z0-9]
IF %ERRORLEVEL% EQU 0 GOTO OK
echo "Commit Comments are Required" >&2
exit 1
:OK
exit 0

8

防止编辑svn::log以外的版本属性

  1. pre-revprop-change.bat
  2. 防止编辑svn::log以外的版本属性

@ECHO OFF
:: Set all parameters. Even though most are not used, in case you want to add
:: changes that allow, for example, editing of the author or addition of log messages.
set repository=%1
set revision=%2
set userName=%3
set propertyName=%4
set action=%5

:: Only allow the log message to be changed, but not author, etc.
if /I not "%propertyName%" == "svn:log" goto ERROR_PROPNAME

:: Only allow modification of a log message, not addition or deletion.
if /I not "%action%" == "M" goto ERROR_ACTION

:: Make sure that the new svn:log message is not empty.
set bIsEmpty=true
for /f "tokens=*" %%g in ('find /V ""') do (
set bIsEmpty=false
)
if "%bIsEmpty%" == "true" goto ERROR_EMPTY

goto :eof

:ERROR_EMPTY
echo Empty svn:log messages are not allowed. >&2
goto ERROR_EXIT

:ERROR_PROPNAME
echo Only changes to svn:log messages are allowed. >&2
goto ERROR_EXIT

:ERROR_ACTION
echo Only modifications to svn:log revision properties are allowed. >&2
goto ERROR_EXIT

:ERROR_EXIT
exit /b 1

谢谢你的编辑。我在处理Markdown时真的很困难。 - Greg Dean

6

* 更新:由于Twitter已经弃用用户名/密码验证,转而使用OAuth,因此此方法不再有效。 *

将提交信息发布到Twitter

  1. 钩子的名称= post-commit
  2. 脚本功能= 将修订版本、作者和提交消息发布到Twitter

使用说明:

  • twitterUsernametwitterPassword替换为您实际的Twitter帐户信息
  • 此代码已在VisualSVN上测试,我唯一能让它工作的方法是将所有内容都转储到硬编码路径c:\hook\post-commit中。您可以更改为任何VisualSVN具有读/写访问权限的路径。
  • 需要安装Wget。可从此处下载安装程序。
  • 欢迎评论和改进。这是我在Windows上的第一个SVN挂钩,真是太痛苦了。

实际脚本:

echo status= > c:\hook\post-commit\msg.txt
echo Rev#%2 by >> c:\hook\post-commit\msg.txt
"%VISUALSVN_SERVER%\bin\svnlook.exe" author -r %2 %1 >> c:\hook\post-commit\msg.txt
"%VISUALSVN_SERVER%\bin\svnlook.exe" log -r %2 %1 >> c:\hook\post-commit\msg.txt
"c:\Program Files (x86)\GnuWin32\bin\wget.exe" --user=twitterUsername --password=twitterPassword --post-file=c:\hook\post-commit\msg.txt --append-output=c:\hook\post-commit\log.txt --output-document=c:\hook\post-commit\download.txt --delete-after http://twitter.com/statuses/update.xml

看起来这个不起作用了 - Twitter 改变了它的认证方法吗? - g t
@gt 是的,他们有。现在只有OAuth;不再需要用户名和密码。我会更新。 - Portman
可惜了,看起来是一个很不错的工具! - g t

4

检查常见的“懒惰”提交信息

  1. 钩子的名称 = pre-commit
  2. 脚本的功能 = 检查空行或'.' 行。还会检查一个不允许作为唯一注释的单词文件。

实际脚本:

rem Make sure that the log message contains some text.
set REPOS=%1
set TXN=%2

"C:\Program Files\Subversion\bin\SVNlook.exe" log -t %TXN% %REPOS% | FindStr [a-zA-Z0-9]
IF %ERRORLEVEL% EQU 0 GOTO OK
echo Your commit has been blocked because you didn't provide a log message  1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2 
exit 1

:OK
rem Check if comment is in list of reserved words to not be used..

"C:\Program Files\Subversion\bin\SVNlook.exe" log -t %TXN% %REPOS% >comment
setlocal enabledelayedexpansion
Set SEPARATOR=
set COMMENT=
for /f "delims=" %%a in (comment) do (  
    set currentline=%%a
    set COMMENT=!COMMENT!%SEPARATOR%!currentline!
)

FIND "%COMMENT%" "C:\Program Files\Subversion\excludedwords.txt">Null
If %ERRORLEVEL% EQU 1 goto OK2

:Fail
echo Your commit has been blocked because the single word comment you provided is not allowed 1>&2
echo Line is -%COMMENT%- 1>&2
echo Please write a proper log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2 
exit 1


:OK2
rem Check that the author of this commit has the rights to perform
rem the commit on the files and directories being modified.
rem commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1

rem All checks passed, so allow the commit.
exit 0

排除词样例文件: 更新 更新 更新。 更新。 修复 修复 修复。 修复。 .. . ... . . . . 排序 排序。 排序 排序。

等等等等等等


4
我喜欢使用用Java编写的subHooker。它提供了预提交和后提交挂钩功能。
预提交: - 强制执行提交消息要求,或最小长度或两者都有(或没有) - 可以强制执行提交消息中的RegEX表达式要求,适用于要求回溯或缺陷号码(或不要求)
后提交: - 发送HTML或纯文本电子邮件消息 - 纯文本和HTML消息都使用模板系统 - 可以打开或关闭差异 - 可以打开或关闭更改集 - 支持本地化 - 支持标准化日志记录
您可以在Google Code上找到它@http://code.google.com/p/subhooker/ 它是免费的,而且是我编写的,我已经运行Subversion几年了,我非常喜欢它,这就是为什么我要将其贡献给社区的原因。

3

防止编辑除svn::log之外的修订属性

  1. pre-revprop-change.bat
  2. 防止编辑除svn::log之外的修订属性(备用版本)

来源:

rem Only allow log messages to be changed.
if "%4" == "svn:log" exit 0
echo Property '%4' cannot be changed >&2
exit 1

3

针对在Windows上使用Subversion的.NET开发人员,Phil Haack发布了关于CaptainHook的文章。

CaptainHook是一个简单的插件框架,用于使用.NET编写Subversion钩子。

该项目托管在Source Forge上。


不,我非常渴望在以前的工作中使用它来处理我们在源代码控制中管理配置文件时遇到的一些问题,但它从未高居我的待办事项列表。我个人现在可能更有兴趣使用 Perl 脚本。 - tarn
我目前正在使用CaptainHook进行开发。一定要获取补丁文件 - 它可以修复2个漏洞。 - Greg Woods
我没有尝试过CaptainHook,但是链接页面上所有最近的评论都报告说它已经损坏了,所有有用的功能都被注释掉了(并且无法编译)。作者说他会尝试修复它,但那是在2007年。看起来最好远离这个。 - kmote

1

这个钩子可以防止提交到特定的分支

(在这种情况下是branch-16E):

setlocal

rem Subversion sends through the path to the repository and transaction id  
set REPOS=%1
set TXN=%2

rem Committing to a branch is not allowed
svnlook changed -t %TXN% %REPOS% | findstr "\/branch-16E"
if %errorlevel% EQU 0 goto errb else exit 0

:errb
echo. 1>&2
echo This branch was closed. If you want to commit here contact your administrator. 1>&2
exit 1

1
我使用 C# 开始了一个钩子的存储库。我的第一个钩子是用于向 RSS 订阅源发送签入通知的:SubversionRss 我目前正在开发一个 post-commit 钩子,用于向 Twitter 订阅源发送签入通知。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接