Skip to content

Instantly share code, notes, and snippets.

@jeffhung
Created February 23, 2010 18:23
Show Gist options
  • Save jeffhung/312523 to your computer and use it in GitHub Desktop.
Save jeffhung/312523 to your computer and use it in GitHub Desktop.
#!/bin/sh
# ---------------------------------------------------------------------------
# Copyright (c) 2010, Jeff Hung
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------
# $Date: 2010-02-24 00:02:41 +0800 (Wed, 24 Feb 2010) $
# $Rev: 578 $
# $Author: jeffhung $
# ----------------------------------------------------------------------------
# revid: "@(#) $Id: svn-rexport.sh 578 2010-02-23 16:02:41Z jeffhung $"
# ----------------------------------------------------------------------------
__exe_name__=`basename $0`;
__revision__=`echo '$Rev: 578 $' | cut -d' ' -f2`;
__rev_date__=`echo '$Date: 2010-02-24 00:02:41 +0800 (Wed, 24 Feb 2010) $' | cut -d' ' -f2`;
usage()
{
local ex=0;
if [ $# -gt 0 ]; then
ex=$1; shift;
fi;
echo >&2 "\
Usage: ssh [USER@]HOST $__exe_name__ [OPTIONS...] URL [PATH] | sh -s
Export Subversion URL in remote host and extract to local PATH>, even without
this script. Assuming this script is already deployed in HOST, and USER can
run Subversion commands non-interactively in HOST. This command will run this
script in HOST as USER, export from Subversion URL, pack as shar(1) ball, send
back via ssh(1), and extract locally to PATH.
Alternatively, we can specify USER@HOST via --connect option if we have this
script deployed in local machine. When --connect is specified, we should run
this script locally. This script will manipulate all of above via ssh(1).
Options:
-h,--help Show this help message.
-u,--username USERNAME Use USERNAME to login to Subversion repository.
-p,--password PASSWORD Use PASSWORD to login to Subversion repository.
(USERNAME and PASSWORD are required if these is no
credentials cached in USER@HOST, since Subversion
command will be ran non-interactively.)
-c,--connect [USER@]HOST When run this script locally with this option,
will connect to USER@HOST via ssh(1) to export URL
to local PATH. (USER default to \$USER).
-r,--revision REV Use REV as the Subversion revision argument.
-v,--verbose Show verbose messages.
Example:
- Run remotely: will run this script in HOST as USER, export from Subversion
URL, pack as shar(1) ball, send back via ssh(1), and extract locally to PATH.
SHELL> ssh USER@HOST $__exe_name__ http://foo.com/svn/ bar | sh -s
- Run locally: will execute subversion command remotely via ssh(1).
SHELL> $__exe_name__ --connect user@foo.com http://foo.com/svn/ bar
Revision: r$__revision__ ($__rev_date__)";
while [ $# -gt 0 ]; do
echo >&2 "ERROR: $1";
shift;
done;
exit $ex;
}
msg_exit()
{
local ex=0;
if [ $# -gt 0 ]; then
ex=$1; shift;
fi;
echo >&2 "\
Usage: ssh [USER@]HOST $__exe_name__ [OPTIONS...] URL [PATH] | sh -s
Type '$__exe_name__ --help' for help messages.";
exit $ex;
}
opt_username='';
opt_password='';
opt_connect='';
opt_revision='';
opt_verbose='no';
arg_url='';
arg_path='';
while [ $# -gt 0 ]; do
arg="$1"; shift;
case "$arg" in
-h|--help)
usage;
;;
-u|--username)
if [ $# -gt 0 ]; then
opt_username="$1"; shift;
else
msg_exit 1 'Missing USERNAME';
fi;
;;
-p|--password)
if [ $# -gt 0 ]; then
opt_password="$1"; shift;
else
msg_exit 1 'Missing PASSWORD';
fi;
;;
-c|--connect)
if [ $# -gt 0 ]; then
opt_connect="$1"; shift;
else
msg_exit 1 'Missing [USER]@HOST';
fi;
;;
-r|--revision)
if [ $# -gt 0 ]; then
opt_revision="$1"; shift;
else
msg_exit 1 'Missing <rev>.';
fi;
;;
-v|--verbose)
opt_verbose='yes';
;;
-*)
usage 1 "Invalid option: $arg.";
;;
*)
if [ -z "$arg_url" ]; then
arg_url="$arg";
elif [ -z "$arg_path" ]; then
arg_path="$arg";
else
msg_exit 1 "Invalud argument: $arg";
fi;
;;
esac;
done;
if [ -z "$arg_url" ]; then
msg_exit 1 'Missing URL.';
fi;
if [ -n "$arg_path" ]; then
# Remove leading /.
arg_path=`echo "$arg_path" | sed -e 's/^\///'`;
else
# Set PATH as basename of URL.
arg_path=`echo "$arg_url" | sed -e 's/^.*\/\([^\/]*\)$/\1/'`;
fi;
SVN=`which svn`;
dir=`mktemp -d /tmp/$__exe_name__.XXXXXX`;
svn_opts=''
svn_opts="$svn_opts --quiet";
svn_opts="$svn_opts --non-interactive";
svn_opts="$svn_opts --trust-server-cert";
if [ -n "$opt_username" ]; then
svn_opts="$svn_opts --username $opt_username";
fi;
if [ -n "$opt_password" ]; then
svn_opts="$svn_opts --password $opt_password";
fi;
if [ -n "$opt_revision" ]; then
svn_opts="$svn_opts --revision $opt_revision";
fi;
errmsg='';
# export
if [ -z "$errmsg" ]; then
`$SVN export $svn_opts $arg_url $dir/$arg_path`;
if [ $? != 0 ]; then
errmsg="ERROR: $SVN: $?";
fi;
fi;
# pack
if [ -z "$errmsg" ]; then
( cd $dir; shar `cd $dir; find $arg_path -print` > $arg_path.shar; )
fi;
# output
cat $dir/$arg_path.shar;
# cleanup
rm -rf $dir;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment