SVN hooks for Windows
Asked Answered
Z

9

35

I did a little googling and found that there isn't really a resource of SVN hooks for Windows. So I figured I'd start a wiki here to centralize it.

If you contribute, please be sure to indicate:

  1. The name of the hook
  2. What the script does
  3. The actual script

NOTE: I suspect posting an epic script will not be useful.

Zonazonal answered 11/3, 2009 at 7:10 Comment(1)
What is "an epic script"? Is it Eclipse Perl Integration (EPIC)?Munford
Z
11

Prevent commits with empty comments

  1. pre-commit
  2. prevents commits with empty comment

Source:

"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
Zonazonal answered 11/3, 2009 at 7:10 Comment(0)
Z
8

Prevents edits to revision props other than svn::log

  1. pre-revprop-change.bat
  2. Prevents edits to revision properties other than 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
Zonazonal answered 11/3, 2009 at 7:10 Comment(1)
lol thanks for the edit. I was really struggling with the markdownZonazonal
S
6

* UPDATE: This no longer works, as Twitter has deprecated username/password authentication in favor of OAuth. *

Publishes commit info to Twitter

  1. The name of the hook = post-commit
  2. What the script does = publishes the revision, author, and commit message to Twitter

Usage notes:

  • Replace twitterUsername and twitterPassword with your actual Twitter
  • This is tested against VisualSVN, and the only way I could get it to work was by dumping everything into a hard-coded path, c:\hook\post-commit. You can change that to any path that VisualSVN has read/write access to.
  • Requires Wget to be installed. Installer can be downloaded here
  • Comments and improvements welcome. This is my first SVN hook on Windows and my GAWD was it a pain.

The actual script

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
Sherris answered 11/3, 2009 at 7:10 Comment(3)
Doesn't look like this works - has Twitter changed its authentication method?Unmixed
@gt Yes, the have. OAuth only now; no username and password anymore. I'll update.Sherris
Pity, looked like a neat utility to have!Unmixed
G
4

I like to use subHooker, which is coded in java. It provides pre and post commit hook functionality.

Pre commit:

  • Enforces commit message requirement, or minimal length or both (or not)
  • Can enforce a RegEX expression requirements in the commit message, good for requiring a back-log or defect number (or not)

Post Commit:

  • Send HTML or Plain text e-mail messages
    • Both plain and html messages use a template system
    • Can turn diff on or off
    • Can turn change set on or off
  • supports localization
  • supports standardized logging.

You can find it on google code @ http://code.google.com/p/subhooker/

It's free, and yes, I am the author, I've been running subversion for a few years now, I love it a lot which is why I am contributing this back to the community.

Goober answered 11/3, 2009 at 7:10 Comment(0)
D
4

Checks for common "lazy" commit messages

  1. The name of the hook = pre-commit
  2. What the script does = Check for blank line or '.' line. Also check a file of words not allowed to be the sole comment.

The actual script

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

Sample Excluded words file: Updated updated updated. Updated. Fix fix Fix. fix. .. . ... . . . . sorted sorted. Sorted Sorted.

etc etc etc etc

Decarbonize answered 11/3, 2009 at 7:10 Comment(0)
M
3

Prevent edits to revision props other than svn::log

  1. pre-revprop-change.bat
  2. Prevents edits to revision properties other than svn::log (alternate version)

Source:

rem Only allow log messages to be changed.
if "%4" == "svn:log" exit 0
echo Property '%4' cannot be changed >&2
exit 1
Megalocardia answered 11/3, 2009 at 7:10 Comment(0)
J
3

For the .NET developers using Subversion on Windows, Phil Haack posted about CaptainHook.

CaptainHook is a simple plugin framework for writing Subversion hooks using .NET

The project is hosted at Source Forge

Jeromejeromy answered 11/3, 2009 at 7:10 Comment(3)
No, I was very keen to get use it at my previous job to deal with some problems we had managing configuration files in source control, but it never got high enough on my TODO list. I'd personally probably be more interested in using perl scripts these days.Jeromejeromy
I'm currently playing with CaptainHook. Be sure to grab the patch file - it fixes 2x bugsComplexion
I haven't tried CaptainHook, but all the most recent comments on the linked page report that it is broken and all the useful functionality is commented out (and won't compile). The author said he'd try to fix it, but that was in 2007. Looks like it would be prudent to steer clear of this one.Mephistopheles
P
1

This hook prevents commits to a specific branch

(branch-16E in this case):

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
Peracid answered 11/3, 2009 at 7:10 Comment(0)
T
1

I started a repository of hooks using C#. My first hook was one to send check in notices to a RSS feed: SubversionRss I'm currently working on one post-commit hook to send check in notices to a Twitter feed.

Twiddle answered 11/3, 2009 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.