Skip to content

Instantly share code, notes, and snippets.

@danstuken
Created January 27, 2012 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danstuken/1688403 to your computer and use it in GitHub Desktop.
Save danstuken/1688403 to your computer and use it in GitHub Desktop.
Very simple script to import from svn repos to an initialised central git repo.
#!/bin/bash
usage()
{
echo "Usage: $0 <svn_url> <git_url> <svn_authorsfile> [--branches]"
exit 1
}
create_svn_fetch_dir()
{
mkdir "${svn_fetch_dir}"
}
track_remote_branches()
{
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
}
fetch_from_svn()
{
echo "Fetching from ${svn_repo_url}"
cd "${svn_fetch_dir}"
git svn init "${svn_repo_url}" "${import_branches_and_tags}" --no-metadata
git config svn.authorsfile "${svn_authors_file}"
if [ "${import_branches_and_tags}" != "" ]; then
git config --add remote.origin.fetch refs/remotes/*:refs/remotes/origin/*
fi
git svn fetch
if [ "${import_branches_and_tags}" != "" ]; then
track_remote_branches
fi
cd ..
}
clone_svn_repo()
{
echo "Locally cloning repo"
git clone "${svn_fetch_dir}" "${git_clone_dir}"
if [ "${import_branches_and_tags}" != "" ]; then
cd "${git_clone_dir}"
track_remote_branches
cd ..
fi
}
update_git_remotes()
{
echo "Updating git origin urls"
cd "${git_clone_dir}"
git remote add newpush "${git_repo_url}"
cd ..
}
push_to_remote()
{
echo "Pushing to origin"
cd "${git_clone_dir}"
git push --all newpush
cd ..
}
cleanup()
{
echo "Cleaning up"
rm -rf "${svn_fetch_dir}"
rm -rf "${git_clone_dir}"
}
echo "Welcome to the SVN -> git import script"
[ $# -lt 3 ] && usage
svn_repo_url="$1"
git_repo_url="$2"
svn_authors_file="$3"
import_branches_and_tags=""
if [ "$4" = "--branches" ]; then
import_branches_and_tags="--stdlayout"
fi
svn_fetch_dir=${$}_svn_tmp
git_clone_dir=${$}_git
create_svn_fetch_dir
fetch_from_svn
clone_svn_repo
update_git_remotes
push_to_remote
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment