Skip to content

Instantly share code, notes, and snippets.

@iangreenleaf
Created January 18, 2010 06:50
Show Gist options
  • Save iangreenleaf/279838 to your computer and use it in GitHub Desktop.
Save iangreenleaf/279838 to your computer and use it in GitHub Desktop.
svn switch disaster prevention
#!/bin/bash
### Copyright 2009 Ian Young
### Available under the terms of the MIT license
### ABOUT
### Prevents you from svn switching to a mismatched directory in a different branch.
### See http://blog.iangreenleaf.com/2009/11/avoiding-disaster-when-using-svn-switch.html
### USAGE
### Make sure this file is executable. In your .bashrc or other startup script, add:
### alias svn='/path/to/this_script.sh'
# We only care if it's a switch command
if [[ ( $# = 2 ) && $1 = "switch" ]]
then
# Get the current path of the working directory we're switching,
# then filter out the root of the URL
#
# Note: We append "=" simply to make sure that sed found a match, since our
# filtered path may be empty and we need to distinguish that from no match
CURRPATH=`svn info | sed -n -e 's_URL: .*/\(trunk\|branches/[^/]*\)/\?\(.*\)_=\2_p'`
# Now filter the path we're switching to the same way
NEWPATH=`echo -n "$2" | sed -n -e 's_/\?$__' -e 's_.*/\(trunk\|branches/[^/]*\)/\?\(.*\)_=\2_p'`
# If we found a match, make sure the paths are the same
if [[ ( -n "$CURRPATH" ) && ( "$CURRPATH" != "$NEWPATH" ) ]]
then
echo "It looks like you're about to make a big mistake."
exit 1;
fi
fi
# Otherwise we let SVN carry on with its business
svn "$@";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment