Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created February 1, 2012 15:51
Show Gist options
  • Save naholyr/1717664 to your computer and use it in GitHub Desktop.
Save naholyr/1717664 to your computer and use it in GitHub Desktop.
Branching with SVN
# Retrieve repository URL
function svn-url() {
LANG=en_US svn info "$1" | grep '^URL *:' | sed 's/^URL *: //'
}
# Branching
function svn-branch() {
if [ ! -d .svn ]; then return 1; fi
if svn-url | grep '/trunk' &> /dev/null; then
echo trunk
elif svn-url | grep '/branches/[^/]*' &> /dev/null; then
echo "$(svn-url | grep -o '/branches/[^/]*' | sed 's/\/branches\/\(.*\)\/\?/\1/')"
fi
}
# Switch to branch (or list with if no parameter)
function svn-switch() {
branch="$(svn-branch)"
errmsg=""
url=""
if [ "$1" = "" ]; then
errmsg="Branch name (or 'trunk') expected"
elif [ "$1" = "$branch" ]; then
errmsg="Already in branch '$branch'"
elif [ "$branch" = "trunk" ]; then
url="$(svn-url | sed "s/\/trunk\(\/\?\)/\/branches\/$1\1/")"
elif [ "$branch" != "" ]; then
if [ "$1" = "trunk" ]; then
url="$(svn-url | sed "s/\/branches\/[^\/]*\(\/\?\)/\/trunk\1/")"
else
url="$(svn-url | sed "s/\/branches\/[^\/]*\(\/\?\)/\/branches\/$1\1/")"
fi
fi
if [ "$errmsg" = "" -a "$url" != "" ]; then
if ! svn ls "$url" &> /dev/null; then
errmsg="Branch '$1' not found"
else
echo "From: '$(svn-url)'"
echo "To: '$url'"
echo -n "Switch now (y/n) ? [y] "
read v
if [ "$v" = "" -o "$v" = "y" -o "$v" = "Y" ]; then
svn switch "$url"
else
errmsg="Interrupted by user"
fi
fi
fi
if [ "$errmsg" != "" ]; then
echo "Error: $errmsg" > /dev/stderr
echo "Available branches (currently '$branch'):" > /dev/stderr
svn ls $(svn-url | sed 's/\/\(trunk\|branches\)\/.*/\/branches/') | while read b; do
echo " * ${b:0:$(($#b-1))}" > /dev/stderr
done
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment