views:

313

answers:

2

I'd like to avoid creating svn mergeinfo property on anything but the root of branches. We have a trunk branch and some other branches.

Assuming I know absolutely nothing about svn triggers, what steps do I need to follow to setup a pre-commit hook that prevents additions of mergeinfo properties to anything but the root of branches?

The trunk is located in the repository's base "/trunk", and all branches are in "/branches/foo"

+2  A: 

Basically, you need to define a SVN pre-commit hook. First though, read about hooks in general.

You will likely want to start with copying the pre-commit.tmpl file to pre-commit, and implement the logic in there.

You will likely want to examine the diff of the currently executing transaction for lines like: Added: svn:mergeinfo

Since the information about which file this was added for is on a different line, you may need a more complex processing tool than grep - perhaps a simple perl script.

When you determine that a transaction has an added property that you don't want, you can block the commit; or, if you are especially daring, you could try to modify the transaction and continue.

Avi
FTA: WarningWhile hook scripts can do almost anything, there is one dimension in which hook script authors should show restraint: do not modify a commit transaction using hook scripts. While it might be tempting to use hook scripts to automatically correct errors, shortcomings, or policy violations present in the files being committed, doing so can cause problems.
dpurrington
BTW, this is a good answer. To clarify, a hook is just a script. Use svnadmin to query the repo or the transaction, and return a non-zero exit code to reject the commit.
dpurrington
I agree. In general, a hook should not be used to modify the commit in progress.
Avi
+1  A: 

The following should work. Your repository already has the svn:mergeinfo on the trunk, so you never want anyone to add one again.

By the way, our actual hook also ensures that every commit is accompanied by a log message and also protects against the deletion of some of our important top level directories via repo browser.

@echo off

set logfile=%TEMP%\%2.txt

"c:\program files\subversion\svnlook" log -t %2 %1 > %logfile%

for /f "tokens=1,2*" %%i in (%logfile%) do (
    if %%i==UU goto checkmergetracking
)

for /f "tokens=1,2*" %%i in (%logfile%) do (
    if %%i==_U goto checkmergetracking
)

del %logfile%
exit 0

:checkmergetracking
"c:\program files\subversion\svnlook" diff -t %2 %1 > %logfile%
find "Added: svn:mergeinfo" %logfile%
if ERRORLEVEL 1 goto nomergeinfo

del %logfile%
echo "Addition of Merge Info Not Allowed" >&2
exit 1

:nomergeinfo
del %logfile%
exit 0
Sean Kinsella