Skip to content

Instantly share code, notes, and snippets.

@alecthegeek
Last active February 5, 2016 01:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alecthegeek/4134951 to your computer and use it in GitHub Desktop.
Save alecthegeek/4134951 to your computer and use it in GitHub Desktop.
SVN Wrapper Script -- support for standard commit message templates and svn info output
#!/usr/bin/env bash
# A wrapper for SVN. Designed for
# GIt users who need access to command line SVN. Tries to adopt as much information from Git config
# Command Line SVN users who are working with people using TortoiseSVN on Windows
# People who have use build scripts that depend on the outfrom from svn info when they in a Git checkout
shopt -s compat31 # Need fancy fatures
# In case we don'to have a template
defaulttemplate="Subject:
Description:"
# Is SVN actually installed?
if [[ -x /usr/local/bin/svn ]] ; then
SVN_CMD=/usr/local/bin/svn
elif [[ -x /usr/bin/svn ]] ; then
SVN_CMD=/usr/bin/svn
else
echo svn not installed
exit 1
fi
# Are we in the middle if a Git checkout? If requested let's provide info for a build script
if [[ $1 == "info" ]] && git rev-parse --git-dir > /dev/null 2>&1 ; then
exec git svn info
fi
#Always prefer the editor setting from Git
[[ -n $(git config --global --get core.editor) ]] && export SVN_EDITOR=$(git config --global --get core.editor)
#Did not get an editor from Git, so let's see if SVN has an editor (NB Need to install GNU Sed)
[[ -n $SVN_EDITOR ]] ||
export SVN_EDITOR=$(gsed -ne '/^[ \t]*\[helpers\]/,/^[ \t]*\[.\+\]/s/[\t ]*editor-cmd[ ]*=[ \t'\''"]*\([^'\''" \t]\+\).*$/\1/p' ~/.subversion/config)
# if still no editor then use env strings
[[ -z $SVN_EDITOR && -n $VISUAL ]] && export SVN_EDITOR=$VISUAL
[[ -z $SVN_EDITOR && -n $EDITOR ]] && export SVN_EDITOR=$EDITOR
# Really no editor? Lets hard code one
if [[ -z $SVN_EDITOR ]] ; then
if [[ -x /usr/local/bin/mvim ]] ; then
export SVN_EDITOR="mvim -f "
elif [[ -x /usr/bin/gvim ]] ; then
export SVN_EDITOR="gvim -f "
else
export SVN_EDITOR=vim
fi
fi
if [[ $1 == 'ci' || $1 == 'commit' ]] ; then
for i in "$@" ; do
if [[ $i == "-F" || $i == "--file" || $i == "--message" || $i == "-m" ]] ; then
# If we are doing a commit and been provided with the commit message already?
$SVN_CMD "$@"
exit
fi
done
if [[ -z $SVN_EDITOR ]] ; then
echo Could not find an editor to create a commit message
exit 1
fi
# We need to try an set up a commit template
shift
msgfile=/tmp/$$msgfile
templatefile=/tmp/$$template
if [[ -n "$(svn propget tsvn:logtemplate)" ]] ; then # TortoiseSVN template
svn propget tsvn:logtemplate > $templatefile
elif [[ -n "$(git config --global --get commit.template)" ]] ; then #Git Template
templatefile="$(git config --global --get commit.template)"
else
echo $defaulttemplate > $templatefile # use the default
fi
cp $templatefile $msgfile
$SVN_EDITOR $msgfile
if diff -w $templatefile $msgfile > /dev/null ; then
# Follow Git convention -- no update to commit msg -> no commit
echo No commit message -- no commit
else
$SVN_CMD commit --file "$msgfile"
fi
# Get ride of the working files
rm $msgfile
[[ $templatefile == "$(git config --global --get commit.template)" ]] || rm $templatefile
else
# Check if we have uncommitted changes in the working copy
if [[ $1 == 'up' || $1 == 'update' ]] && $SVN_CMD status -q | egrep '^M' ; then
echo 'uncommitted changes. Do you wish to processed with update? [N/y]'
echo '(use "svn revert -R ." to throw away)'
read x
if [[ ! ( $x == 'Y' || $x == 'y') ]] ; then
exit 1
fi
fi
$SVN_CMD "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment