Wednesday, April 26, 2006

Checking file contents using Subversion Hooks

I found lots of examples of verifying checkin comments using Subversion commit hooks but nothing which showed me how to validate the contents of the file.

In my case I want to make sure that I'm not checking in any unit test files that have classes decorated with [CurrentFixture]. FYI I use MbUnit for my unit tests.

Using perl or shell scripts would be relatively easy but I wanted to do things the batch file way. Not sure why. Something repressed from my childhood no doubt.

I'm "sure" there's an easier way even using batch files but here's what I came up with. In the hooks directory in your Subversion repository edit the pre-commit.bat file like so:

set REPOS=%1
set TXN=%2
set FIXQUOTES=c:\util\FixQuotes
set PROJECTROOT=G:\Projects\VolMan

for /F "usebackq tokens=2" %%f in (`svnlook changed -t "%TXN%" "%REPOS%"`) do (
for /F "usebackq" %%x in (`%FIXQUOTES% %%f`) do (
for /F "usebackq" %%g in (`findstr /LM "[CurrentFixture]" %PROJECTROOT%\%%x`) do (
if not x%%g==x (
echo Cannot check in a file containing a class decorated with [CurrentFixture] 1>&2
exit 1
)
)
)
)


* - the output of svnlook has forward slashes '/' instead of the dos-friendly backslashes '\' which caused findstr to puke. I needed to create a cheeseball little app FixQuotes to flip all forward slashes to backslashes. Here's where perl/shell script would've made things trivial.

It's sooo yucky to hardcode the path to FixQuotes and the root of the project too. But c'est la vie.

If anyone has an easier way pleeease tell me.

[Listening to: Staple - The Day the Blind Revolted]

No comments: