#!/bin/bash
#
# git-me-up was written to automate the creation of a local git repo for a
# Rails app that is hosted in Subversion. See the README for more details.
#
# Synopsis: git-me-up <subversion-repo> <local-git-repo>
#
# Author: Graham Ashton <graham@effectif.com>
BRANCH="${BRANCH:-work}"
## Functions
log()
{
echo "$(basename $0): $1"
}
usage()
{
echo "Usage: $(basename $0) <svn-repository> <local-dir>" 1>&2
exit 1
}
check_for_local_dir()
{
local dir="$1"
if [ -e "$dir" ]; then
echo "$(basename $0): ERROR: $dir already exists - please move it!" 1>&2
exit 1
fi
}
get_latest_revision()
{
local repo="$1"
svn log "$repo" | sed -n "2p" | cut -f 1 -d " " | sed "s/^r//"
}
ignore_generated_files()
{
log "setting ignored files from subversion (this can take a while)"
git svn show-ignore >> $GIT_EXCLUDE
cat <<EOF >> $GIT_EXCLUDE
# Git files
.gitignore
EOF
}
clone_repository()
{
local svn_repo="$1"
local local_repo="$2"
[ -z "$REVISION" ] && log "finding latest revision of $svn_repo"
local revision=${REVISION:-$(get_latest_revision "$svn_repo")}
log "creating git repository in $(pwd)/$local_repo"
local empty_dirs=$(git svn clone -r $revision $svn_repo $local_repo 2>&1 | \
grep "W: +empty_dir:" | cut -f 3 -d " ")
local dir
for dir in $empty_dirs; do
log "making empty directory: $dir"
mkdir -p "$local_repo/$dir"
done
pushd "$local_repo" >/dev/null
ignore_generated_files
popd >/dev/null
}
clone_external_plugins()
{
log "checking for plugins installed with svn:externals"
local plugins=$(svn propget svn:externals "$SVN_REPO/vendor/plugins")
[ -z "$plugins" ] && return
pushd "$LOCAL_REPO" >/dev/null
local plugin_dir="../plugins"
mkdir -p "$plugin_dir"
local line plugin svn_repo
export IFS=$'\n' # iterate over lines, not words
for line in $plugins; do
plugin=$(echo $line | cut -f 1 -d " ")
if [ ! -e "$plugin_dir/$plugin" ]; then
svn_repo=$(echo $line | cut -f 2 -d " ")
clone_repository "$svn_repo" "$plugin_dir/$plugin"
fi
log "symlinking $plugin into $LOCAL_REPO/vendor/plugins"
pushd vendor/plugins >/dev/null
ln -sf "../../$plugin_dir/$plugin"
popd >/dev/null
echo "/vendor/plugins/$plugin" >> "$GIT_EXCLUDE"
done
unset IFS
popd >/dev/null
}
create_working_branch()
{
local local_repo="$1"
log "creating '$BRANCH' branch on $(pwd)/$local_repo"
pushd "$local_repo" >/dev/null
git checkout -b "$BRANCH"
popd >/dev/null
}
## Main program
[ -n "$DEBUG" ] && set -x
SVN_REPO="$1"
LOCAL_REPO="$2"
[ -z "$SVN_REPO" -o -z "$LOCAL_REPO" ] && usage
GIT_EXCLUDE="$(pwd)/$LOCAL_REPO/.git/info/exclude"
check_for_local_dir "$LOCAL_REPO"
clone_repository "$SVN_REPO" "$LOCAL_REPO"
if [ -e "$LOCAL_REPO/vendor/plugins" ]; then
clone_external_plugins
fi
create_working_branch "$LOCAL_REPO"