Subversion checkout script

Here’s a simple script to check out all subversion repositories on a remote host. It requires that you have SSH access on the host, to be able to fetch the repository names (otherwise you can hardcode them in $repositories). You’ll also need to have Perl installed.

How to use:

  1. Download checkout-all-svn.sh.
  2. chmod u+x path/to/checkout-all-svn.sh
  3. path/to/checkout-all-svn.sh -r http://example.org/svn/

Some features:

  • Works with plain /bin/sh, so it should work on any Linux / BSD distribution.
  • Works with repository names with spaces, but not yet with unusual characters.

checkout-all-svn.sh

#!/bin/sh
#
# $Id: checkout-all-svn.sh 387 2008-06-07 20:36:08Z vengmark $
#
# NAME
#    checkout-all-svn.sh - Check out all Subversion repositories.
#
# SYNOPSIS
#    checkout-all-svn.sh [options]
#
# OPTIONS
#    -v     Verbose output
#    -p     Target host SSH port
#    -u     Target host SSH user name
#    -d     Subversion repository directory on target host
#    -r     URL part before the repository name,
#           e.g. https://example.org/svn/
#
# EXAMPLE
#    ./checkout-all-svn.sh -v -p 1234 -u ssh-admin -d /var/lib/svn -r
#    https://example.com/reps/
#
# DESCRIPTION
#    Gets all your subversion repositories. If they are already
#    present, they will be updated.
#
#    The current (or specified with -u) user must have SSH access to the remote
#    host. To circumvent this you can specify the repository names in
#    $repositories, separated by newlines.
#
#    To avoid having to type your password several times, you can setup SSH
#    keys - See e.g. https://help.ubuntu.com/community/SSHHowto
#
# BUGS
#    Email bugs to victor dot engmark at gmail dot com. Please include the
#    output of running this script in verbose mode (-v).
#
# COPYRIGHT AND LICENSE
#    Copyright (C) 2008 Victor Engmark
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

# Init
ifs_original="$IFS" # Reset when done

PATH="/usr/bin:/bin"
cmdname=`basename $0`
directory=`dirname $0`
target_dir=~

# Remote host
target_port=22
target_user=`whoami`

# Subversion
svn_root="/var/lib/svn"

#Error messages
errm_unknown="Unknown error in $cmdname" #Code 1
#Code 2 is reserved: http://www.faqs.org/docs/abs/HTML/exitcodes.html
usage="Usage: ${cmdname} [-v] [-p port] [-u user] [-d svn_directory] -r repositories_url" #Code 3

# Process parameters
until [ $# -eq 0 ]
do
	case $1 in
		-v)
			verbose=1
			shift
			;;
		-p)
			if [ -z "$2" ]
			then
				echo "$usage" >&2
				exit 3
			fi
			target_port=$2
			shift 2
			;;
		-u)
			if [ -z "$2" ]
			then
				echo "$usage" >&2
				exit 3
			fi
			target_user=$2
			shift 2
			;;
		-d)
			if [ -z "$2" ]
			then
				echo "$usage" >&2
				exit 3
			fi
			svn_root=$2
			shift 2
			;;
		-r)
			if [ -z "$2" ]
			then
				echo "$usage" >&2
				exit 3
			fi
			base_url=$2
			shift 2
			;;
		*)
			#Unknown parameter
			if [ $verbose ]
			then
				echo "Unknown parameter: $1" >&2
			fi
			echo "$usage" >&2
			exit 3
			;;
	esac
done

if [ ! $base_url ]
then
	echo "$usage" >&2
	exit 3
fi
base_url=`echo $base_url | sed "s/ /%20/g"` # To avoid problems with spaces

target_host=$base_url
target_host=${target_host#*//}
target_host=${target_host%%/*}
if [ $verbose ]
then
	echo "Target host: ${target_host}"
fi

repositories=`ssh -p ${target_port} ${target_user}@${target_host} $(if [ $verbose ]; then echo '-v'; else echo '-q'; fi;) -x "ls '${svn_root}'"`
error=$?
if [ $error -ne 0 ]
then
	echo "Failed to get repository names. Error code $error" >&2
	exit 1
fi
if [ $verbose ]
then
	echo "Repositories:\n${repositories}"
fi

IFS="
" # Make sure paths with spaces don't make any trouble when looping

# Concatenate URLs
rep_urls=""
for repository in $repositories
do
	repository=`echo $repository | sed "s/ /%20/g"`
	rep_urls="${rep_urls} ${base_url}${repository}"
done

IFS="$ifs_original"

svn co $rep_urls --non-interactive -r 'HEAD' `if [ ! $verbose ]; then echo '--quiet'; fi;` "${target_dir}"

# End
exit 0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s