Automatically add svn:needs-lock
Asked Answered
E

5

21

Hi I was wondering if there is a way for the SVN server to automatically assign the svn:needs-lock property on any file that is binary and not textual.

We have a rather small developers team but resting on everyone to remember to set this property on newly created binary files doesn't make sense since it's very easy to forget such a thing.

Emigration answered 2/4, 2009 at 18:39 Comment(0)
P
14

Edit the svn config file and add an entry for auto props or use svn:auto-props versioned property with SVN 1.8 and newer clients. Read SVNBook!

EDIT:

From SVN 1.8 the you can apply the svn:auto-props property to the root path of your repository. See this release note and updated SVNBook 1.8 chapter.

Pharisee answered 2/4, 2009 at 19:14 Comment(3)
Beware! You have to configure auto props on each SVN client individually. There's no way to have the server automatically apply needs-lock, at least as of svn 1.7.Jarl
TSVN has the tsvn:auto-props property which is used for this: tortoisesvn.net/docs/release/TortoiseSVN_en/…Pharisee
@Dan but you could create a pre-commit script which rejects binary files that don't have this property.Southing
T
23

Apache Subversion 1.8 introduced the Repository Dictated Configuration feature which requires SVN 1.8 client, but 1.8 server is not necessary because this is a client-side feature.

With Subversion 1.8, you can configure auto-props patterns within a repository using the new Subversion svn:auto-props inherited property.

For example, set svn:auto-props value to *.exe = svn:needs-lock=* property on the root of your repository (or repository path that represents a root of a project). This will result into each newly added *.exe file having svn:needs-lock=* property applied.

You can store multi-line values in Subversion properties, so you can add the following standard svn:needs-lock and MIME pattern to svn:auto-props:

*.bmp = svn:mime-type=image/bmp;svn:needs-lock=*
*.gif = svn:mime-type=image/gif;svn:needs-lock=*
*.ico = svn:mime-type=image/x-icon;svn:needs-lock=*
*.jpeg = svn:mime-type=image/jpeg;svn:needs-lock=*
*.jpg = svn:mime-type=image/jpeg;svn:needs-lock=*
*.png = svn:mime-type=image/png;svn:needs-lock=*
*.tif = svn:mime-type=image/tiff;svn:needs-lock=*
*.tiff = svn:mime-type=image/tiff;svn:needs-lock=*    
*.doc = svn:mime-type=application/x-msword;svn:needs-lock=*
*.docx = svn:mime-type=application/x-msword;svn:needs-lock=*
*.jar = svn:mime-type=application/octet-stream;svn:needs-lock=*
*.odc = svn:mime-type=application/vnd.oasis.opendocument.chart;svn:needs-lock=*
*.odf = svn:mime-type=application/vnd.oasis.opendocument.formula;svn:needs-lock=*
*.odg = svn:mime-type=application/vnd.oasis.opendocument.graphics;svn:needs-lock=*
*.odi = svn:mime-type=application/vnd.oasis.opendocument.image;svn:needs-lock=*
*.odp = svn:mime-type=application/vnd.oasis.opendocument.presentation;svn:needs-lock=*
*.ods = svn:mime-type=application/vnd.oasis.opendocument.spreadsheet;svn:needs-lock=*
*.odt = svn:mime-type=application/vnd.oasis.opendocument.text;svn:needs-lock=*
*.pdf = svn:mime-type=application/pdf;svn:needs-lock=*
*.ppt = svn:mime-type=application/vnd.ms-powerpoint;svn:needs-lock=*
*.ser = svn:mime-type=application/octet-stream;svn:needs-lock=*
*.swf = svn:mime-type=application/x-shockwave-flash;svn:needs-lock=*
*.vsd = svn:mime-type=application/x-visio;svn:needs-lock=*
*.xls = svn:mime-type=application/vnd.ms-excel;svn:needs-lock=*
*.zip = svn:mime-type=application/zip;svn:needs-lock=*
Tantalus answered 2/9, 2013 at 11:7 Comment(3)
If * is a wildcard, would * = svn:needs-lock=* work if you wanted to simply apply needs-lock to ALL file types?Theatrical
@JonathonCowley I've tested it right now with TortoiseSVN 1.8.7 (SVN 1.8.9) and setting svn:auto-props to * = svn:needs-lock=* does exactly what you ask about, i.e. "applies" needs-lock to all files under the folder where you set the property.Tantalus
Thanks for the very fast response. I'd have tested it myself, but I'm on a PC where I don't have access to Subversion.Theatrical
P
14

Edit the svn config file and add an entry for auto props or use svn:auto-props versioned property with SVN 1.8 and newer clients. Read SVNBook!

EDIT:

From SVN 1.8 the you can apply the svn:auto-props property to the root path of your repository. See this release note and updated SVNBook 1.8 chapter.

Pharisee answered 2/4, 2009 at 19:14 Comment(3)
Beware! You have to configure auto props on each SVN client individually. There's no way to have the server automatically apply needs-lock, at least as of svn 1.7.Jarl
TSVN has the tsvn:auto-props property which is used for this: tortoisesvn.net/docs/release/TortoiseSVN_en/…Pharisee
@Dan but you could create a pre-commit script which rejects binary files that don't have this property.Southing
M
5

It should be noted that the auto props method has to be configured on each SVN client being used. So when you're setting up a new developer, or an existing developer on a new machine, you have to remember to perform this configuration.

If you are all using TortoiseSVN, you can set the tsvn:autoprops property on the base folder of each checkout and it will be honoured by all TortoiseSVN clients.

If you really want to nail it, you'll need to put a pre-commit hook in each repository. The enforcer script might be easily tooled for this.

If you have any python-fu, RepoGuard (the successor to SVNChecker) looks like it could be useful too.

No matter which you pick, there's no way to retroactively apply the property to existing files in the repository, I think. You can probably enforce it on the next commit of the file, however.

Microtone answered 3/4, 2009 at 1:57 Comment(2)
This is an excellent point. If you're used to server-side enforcement of this (such as in TFS), it's not immediately apparent. +1Ceceliacecil
Outdated, as well. See "Repository Dictated Configuration" feature of SVN 1.8: subversion.apache.org/docs/release-notes/…Tantalus
C
2

There's a page on this Subversion wiki that describes all the different options on how to automatically add needs-lock and how to guarantee it has been set. The page also gives example scripts and configuration details:

http://www.orcaware.com/svn/wiki/Automatic_lock-modify-unlock

Chat answered 15/1, 2010 at 10:6 Comment(2)
Note that there's no way to have the server automatically apply needs-lock. This documentation explains how to configure each SVN client individually to apply needs-lock automatically, and how to configure the SVN server to reject commits that don't use needs-lock correctly.Jarl
@DanFabulich now there is way do instruct SVN 1.8 client to apply auto-props from repository.Tantalus
O
2

Use a pre-commit hook

#!/bin/bash

REPOS="$1"
TXN="$2"

SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv

SVNLOOKOK=1

# Check files for svn:needs-lock property
# Exit on all errors.
set -e

echo "`$SVNLOOK changed -t "$TXN" "$REPOS"`" | while read REPOS_PATH
do
 if [[ $REPOS_PATH =~ (A|M|U)[[:blank:]]{3}(.*)\.(.*) ]]
 then
  if [ ${#BASH_REMATCH[*]} -ge 2 ]
    then
  FILENAME=${BASH_REMATCH[2]}.${BASH_REMATCH[3]};

  # Make sure every file has the svn:needs-lock property set
   if [ "" == "`$SVNLOOK propget -t \"$TXN\" \"$REPOS\" svn:needs-lock \"$FILENAME\" 2> /dev/null`" ]
    then
    ERROR=1;
    echo "" >&2
    echo "svn:needs-lock property has to be set on \"$FILENAME\"" >&2
    echo "" >&2
   fi
  fi
 fi
 test -z $ERROR || (exit 1)
done

# All checks passed, so allow the commit.
exit 0

and a pre-lock hook

#!/bin/bash

REPOS="$1"
PATH="$2"
USER="$3"

# If a lock exists and is owned by a different person, don't allow it
# to be stolen (e.g., with 'svn lock --force ...').

# (Maybe this script could send email to the lock owner?)
SVNLOOK=/usr/bin/svnlook
GREP=/bin/grep
SED=/bin/sed

LOCK_OWNER=`$SVNLOOK lock "$REPOS" "$PATH" | \
            $GREP '^Owner: ' | $SED 's/Owner: //'`

# If we get no result from svnlook, there's no lock, allow the lock to
# happen:
if [ "$LOCK_OWNER" = "" ]; then
  exit 0
fi

# If the person locking matches the lock's owner, allow the lock to
# happen:
if [ "$LOCK_OWNER" = "$USER" ]; then
  exit 0
fi
Overview answered 25/9, 2014 at 11:25 Comment(1)
Nice! I adapted the pre-commit hook to print out the svn propset command with the needed files: gist.github.com/spl/0affe6cc0e06aae6cb46a45b8a9edd36Ulrikeulster

© 2022 - 2024 — McMap. All rights reserved.