Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@spl
Created April 20, 2017 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spl/0affe6cc0e06aae6cb46a45b8a9edd36 to your computer and use it in GitHub Desktop.
Save spl/0affe6cc0e06aae6cb46a45b8a9edd36 to your computer and use it in GitHub Desktop.
SVN pre-commit hook script to check for svn:needs-lock
#!/bin/bash
# This is a pre-commit hook script that checks files for the svn:needs-lock
# property. If the property is not set, the commit will fail with an error
# message that explains what must be done.
# Adapted from: http://stackoverflow.com/a/26037418/545794
# In order to use this script, copy it to:
# <svn-repos-data-dir>/<repos>/hooks/pre-commit
SVNLOOK=/usr/bin/svnlook
REPOS="$1"
TXN="$2"
FILES_WITHOUT_LOCK=""
# Exit on all errors.
set -e
while read REPOS_PATH; do
# Check for files that are Added, Modified, or Updated
if [[ $REPOS_PATH =~ (A|M|U)[[:blank:]]{3}(.*)\.(.*) ]]; then
# Get the file name.
if [ ${#BASH_REMATCH[*]} -ge 2 ]; then
FILENAME=${BASH_REMATCH[2]}.${BASH_REMATCH[3]};
# If a file does not have the property set, append its name to the files
# that need to be changed.
if ! ($SVNLOOK propget -t "$TXN" "$REPOS" svn:needs-lock "$FILENAME" &> /dev/null); then
FILES_WITHOUT_LOCK="$FILES_WITHOUT_LOCK '$FILENAME'"
fi
fi
fi
done <<< "$($SVNLOOK changed -t "$TXN" "$REPOS")"
# The <<< avoids piping to a child process, which make it more difficult to
# share variables. See http://stackoverflow.com/a/124349/545794
if [ -z "$FILES_WITHOUT_LOCK" ]; then
# All checks passed, so allow the commit.
exit 0
else
# If there are any file names, we exit with a failure code and error message.
echo "" >&2
echo "Some files you are committing do not have the svn:needs-lock property set." >&2
echo "Do the following and re-commit:" >&2
echo "" >&2
echo " svn propset svn:needs-lock '*'$FILES_WITHOUT_LOCK" >&2
exit 1
fi
@spl
Copy link
Author

spl commented Apr 25, 2017

The above script was adapted from here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment