Svn pre-commit hook to disallow svn:mergeinfo on non-root directories
Asked Answered
A

2

19

I would like to use a pre-commit hook that prevents developers from setting svn:mergeinfo on non-root directories. That is, I want to enforce that svn:mergeinfo can only be set on directories like "trunk" or "branches/branchName". Developers sometimes need to be "reminded" that it's not good practice to use a subdirectory of the root as a merge target (per the best practices listed here). Does anyone have such a hook script or know where I could find one? I am in a windows environment, so batch or powershell would be preferable, but anything would certainly be helpful.

Amathist answered 22/7, 2010 at 4:0 Comment(5)
Strictly curiosity - are you using Subversion 1.5 or 1.6? (I too have suffered at the hands of svn:mergeinfos on all sorts of directories/files due to merges to/from non-root directories, but on 1.5)Newcomer
We're on 1.6. The issues I'm having are not due to the old 1.5 bugs where the svn client set mergeinfo on everything in sight. Rather, the issues are due to "user error", where a user performs a merge using a non-root directory like "trunk/mySubProject" as a merge target, therefore setting merge info on that directory. This screws up subsequent merges, as I'm sure you're aware.Amathist
Indeed - thanks for the info. We are only recently on 1.6, so I haven't had time to observe how things changed. I need to address the same problem, though. +FavoriteNewcomer
Oh, this seems like it'd be quite helpful...Sarraute
@StuartLange: It does not really screw up subsequent merges, because SVN takes this info into account. Where it really becomes difficult is when you as a human want to figure out what has been merged or not.Beckerman
K
7

First of all, I'd recommend using perl or python to do the task, windows batch leaves much to be desired.

You can use some example script from http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ to start from. For example, verify-po.py script checks file encodings and commit-access-control.pl.in checks whether author has permissions to commit. You'd probably employ svnlook diff in your script (as in the latter one) to get changed properties for directories and go through the corresponding paths whether they're branches or tags using regular expression.

Update

Found enforcer pre-commit hook script which seems to be what you're looking for.

What this script does is it uses svnlook to peek into the transaction is progress. As it sifts through the transaction, it calls out to a set of hooks which allow the repository administrator to examine what is going on and decide whether it is acceptable.

It contains several methods and verify_property_line_added() among them, since it's called for each line that is added to a property on a file.

Kremer answered 4/8, 2010 at 21:31 Comment(2)
Looks like this is going to be the most likely solution and thus the bounty winner, unless anyone else can come up with something.Sarraute
By the way, after your question I began interested in svn hooks, and I am going to implement some (starting with forbidding commits with no log message), and probably will reach the checking properties on commit - then will put here an another update.Kremer
B
1

If you can CPAN on the server:

https://github.com/gnustavo/SVN-Hooks/blob/master/examples/check-mergeinfo.pl

If not (based on http://comments.gmane.org/gmane.comp.version-control.subversion.user/118969):

REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook

if !($SVNLOOK log -t "$TXN" "$REPOS" | grep -q '\[override] ';) then

    # Get list of paths which have changed      
    TXN_PATHS=$($SVNLOOK changed -t "$TXN" "$REPOS")

    # Filter those which are allowed: /trunk, /branches/*,...
    TXN_PATHS=$(echo "$TXN_PATHS" | grep -Ev "^....(trunk/|branches/[^/]+/)$")

    # Iterate over all paths, which are not allowed to have mergeinfo
    while IFS= read -r TXN_PATH; do
      ELEM_PATH=$(echo "$TXN_PATH" | cut -c 5-)

      MERGEINFO=$($SVNLOOK propget "$REPOS" svn:mergeinfo -t "$TXN" "$ELEM_PATH" 2>/dev/null)
      if [ ! "$MERGEINFO" = "" ]; then 
        echo "  Cannot merge into directory that is not trunk or a branch:" >&2;
        echo "  $ELEM_PATH" >&2;
        echo "  Override by using [override]." >&2;
        exit 1;
      fi      
    done <<< "$TXN_PATHS"

    # echo "Merge info check: OK"
fi
Beckerman answered 14/2, 2016 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.