public
Last active

SVN Wrapper Script -- support for standard commit message templates and svn info output

  • Download Gist
svn.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.