Tuesday, November 20, 2012

Setup SVN pre-commit hook script under Windows

It has been so long for me to have this feature to be ready before a project start since long long time ago. I'm talking about SVN under Windows environment. I would like to have a feature when the code is submit, the server will first verify whether the comment is empty. If it is, block them from commit the code.

There has been a time where I feel so frustrated on my team member who never put any comment on their code end up giving me a hard time to trace back their code. And feel so stupid that digging down each revision changes to open up each file to verify and review their code. But I do not have such luxury time for me to do this setup. Until this week, the UAT is sign-off, I take up the opportunity to continue my work.

There is a template code with a filename, pre-commit.tmpl is ready for use locate inside the directory /hooks, but never got a chance to execute it because the code is for *NIX and not for Windows. I am required modify the code provided from this article in order to get it work under Windows.

@echo off
:: Stops commits that don't include a log message of at least 6 characters.      
@echo off


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

svnlook log %REPOS% -t %TXN% | findstr ...... > nul
if %errorlevel% gtr 0 (goto err) else exit 0

echo --------------------------------------------------------------------------- 1>&2
echo Your commit has been blocked because it didn't include a log message. 1>&2
echo Do the commit again, this time with a log message that describes your changes. 1>&2
echo --------------------------------------------------------------------------- 1>&2
exit 1

There are few thing I need to take note of:
  1. The pre-commit file must be saved as bat or exe file extension.
  2. The pre-commit file must locate under the /hooks directory.
  3. Remember to restart the svn server service when done these changes.

No comments: