Skip to content

Instantly share code, notes, and snippets.

@nbrew
Created July 6, 2011 01:18
Show Gist options
  • Save nbrew/1066326 to your computer and use it in GitHub Desktop.
Save nbrew/1066326 to your computer and use it in GitHub Desktop.
Migrate a list of existing svn repositories to git using svn2git
#!/bin/bash
set -v # verbose output
#
# Migrate a list of existing subversion repositories to git using svn2git. Assumes
# the following repository layouts:
# svn://example.com/<repository>/
# assets/
# branches/
# trunk/
# tags/
#
# [email protected]:<repository>.git
#
# Creates two new repositories from the above structure: <repository> and <repository>-assets.
#
#
# Requirements:
# git
# grep
# ruby (for svn2git)
# rubygems (for svn2git)
# svn
# svn2git - http://github.com/nirvdrum/svn2git
REPOSITORY_NAMES="first_repo_name second_repo_name"
GIT_USER='git'
GIT_HOST='git.example.com'
BASE_SVN_URL='svn://svn.example.com'
BASE_WORKING_DIR='/home/myuser/workspace/'
##############################
# usage
##############################
function usage() {
echo ""
echo "Usage: ./$0"
echo ""
}
##############################
# check args
##############################
# function check_args() {
# if [ -z $SOME_ARG ]; then
# usage
# exit 1
# fi
# }
# ======================================================================
#
# Function: confirm
# Asks the user to confirm an action, If the user does not answer yes,
# then the script will immediately exit.
#
# Parameters:
# $@ - The confirmation message
#
# Examples:
# > # Example 1
# > # The preferred way to use confirm
# > confirm Delete file1? && echo rm file1
# >
# > # Example 2
# > # Use the $? variable to examine confirm's return value
# > confirm Delete file2?
# > if [ $? -eq 0 ]
# > then
# > echo Another file deleted
# > fi
# >
# > # Example 3
# > # Tell bash to exit right away if any command returns a non-zero code
# > set -o errexit
# > confirm Do you want to run the rest of the script?
# > echo Here is the rest of the script
#
# ======================================================================
#################
# confirm - taken from http://wuhrr.wordpress.com/2010/01/13/adding-confirmation-to-bash/
#################
function confirm() {
echo -n "$@ "
read -e answer
for response in y Y yes YES Yes Sure sure SURE OK ok Ok yup YUP Yup
do
if [ "_$answer" == "_$response" ]
then
return 0
fi
done
# Any answer other than the list above is considered a "no" answer
return 1
}
function install_svn2git() {
config Would you like to install the latest svn2git? [y/N]
if [ $? -eq 0 ]; then
`gem install svn2git`
fi
}
function mkdir_if_not() {
if [ ! -d $1 ]; then
echo "Making dir: $1"
mkdir "$1"
fi
}
function repo_dir() {
echo "${BASE_WORKING_DIR}/$1"
}
function mkrepodir() {
local repo_name=$1
local repository_path
repository_path=$(repo_dir ${repo_name})
mkdir_if_not $repository_path
echo "$repository_path"
}
function migrate_primary() {
local repo_name=$1
local repository_path=$(repo_dir ${repo_name})
mkdir_if_not ${repository_path}
cd ${repository_path}
svn2git ${BASE_SVN_URL}/${repo_name}
add_dotgitignore ${repository_path} true
add_remote_origin ${repo_name}
push_origin_to_master ${repository_path}
cd ..
}
function create_assets_repo() {
local repo_name=$1
local repository_path=$(repo_dir "${repo_name}/shared")
local r=`svn ls ${BASE_SVN_URL}/${repo_name}|grep assets`
if [ "" == "${r}" ]; then
echo "WARNING: No assets repository configured for: ${repo_name}"
else
mkdir_if_not ${repository_path}
cd ${repository_path}
svn2git ${BASE_SVN_URL}/${repo_name}/assets --rootistrunk --no-minimize-url
add_dotgitignore ${repository_path}
add_remote_origin ${repo_name}-assets
push_origin_to_master ${repository_path}
cd ..
fi
}
# Function: add_dotgitignore
#
# Adds/appends to a .gitignore
#
# Parameters:
# repository_path - the local directory path to the repository
# append_rails_app_options - boolean - Optional; default: no action app options appended
# Examples:
# > add_dotgitignore /path/to/my/git/repo
# > add_dotgitignore /path/to/my/git/repo true
function add_dotgitignore() {
local ignore_path="$1/.gitignore"
touch ${ignore_path}
cat <<EOFEOF >> ${ignore_path}
.DS_Store
.bundle
EOFEOF
if [ "$2" == true ]; then
cat <<EOFEOF >> ${ignore_path}
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
*.swp
index/*
public/assets
shared
vendor/gems
EOFEOF
fi
git add ${ignore_path} && git ci -m'Add/update .gitignore'
}
function add_remote_origin() {
local repo_name=$1
git remote add origin ${GIT_USER}@${GIT_HOST}:${repo_name}.git
}
function push_origin_to_master() {
local repository_path=$1
cd ${repository_path}
git push origin master
cd ..
}
function init() {
local r=`gem list svn2git|grep svn2git`
if [ "" == "${r}" ]; then
install_svn2git
fi
mkdir_if_not $BASE_WORKING_DIR
for repo_name in $REPOSITORY_NAMES; do
migrate_primary $repo_name
create_assets_repo $repo_name
done
}
init
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment