Skip to content

Instantly share code, notes, and snippets.

@kylewlacy
Created October 7, 2012 22:45
Show Gist options
  • Save kylewlacy/3849846 to your computer and use it in GitHub Desktop.
Save kylewlacy/3849846 to your computer and use it in GitHub Desktop.
Mirroring an SVN Repo to GitHub

Certainly everyone has run into countless projects featured a source code repo that you wanted to clone (to build from source, for example). And certainly some of those repos were SVN-based. Now, if you're like me, you prefer the almighty git to this archaic and decrepit format that coding dinosaurs eons ago once used. So, what do you do? Go to GitHub, search for <project name>, and clone that. But wait, what if you run into a repo that doesn't have a GitHub mirror? What to do? Well, my friend, have you come to the right place:

The De-Facto SVN Mirroring Guide

I'm gonna go out on a limb and assume that you are UNIX-y, so if you am Windows, go here immediately to remedy that. Now then, fire up your terminal emulator, and follow along:

1.) Clone the repo with git as follows: git svn clone <repo url> -T trunk -t tags -b branches <project name> 2.) Add a GitHub repo for the project ('cuz, you know, sharing and whatnot) 3.) cd <project name> and git remote add origin git@github.com:<username>/<project name>.git 4.) Download the attached script update.sh (and chmod +x update.sh, naturally), and run ./update.sh <project name>

That's it! You've now got a historically-accurate translation of on GitHub! And, if you'd like, add the following to your cronjobs (crontab -e):

* 3 * * * $HOME/git/mirrors/update.sh

This will call update.sh without arguments. This will call the script on every repo within it's directory, so you can have more than one mirrored repo setup. Just repeat the above process for every additional repo!

Hiccups

Some side notes:

  • Add MAILTO="" to the top of crontab -e. Trust me on this one.
  • Some repos can have different branch and tag directories (???), so visit . Replace trunk, tags, and branches, with whatever that repo has as alternatives
  • Make sure that you have ssh-agent or keychain setup if you're using cronjobs.
#!/bin/bash
# Based on John Albin's svn to git guide:
# http://john.albin.net/git/convert-subversion-to-git
# ...as well as this Stack Overflow answer:
# http://stackoverflow.com/questions/379081/track-all-remote-git-branches-as-local-branches
update_repo() {
cd "$1"
git svn fetch svn > /dev/null
for remote in `git branch -r`; do
git branch --track $remote
done
git for-each-ref --format='%(refname)' refs/heads/tags |
cut -d / -f 4 |
while read ref
do
git tag "$ref" "refs/heads/tags/$ref"
git branch -D "tags/$ref"
done
git merge trunk master
git push origin --mirror
cd ..
}
if [ -z "$1" ]; then
for repo in `find . -maxdepth 1 -type d`; do
if [ -d "$repo/.git" ]; then
echo "==> Updating $repo..."
update_repo $repo
fi
done
else
update_repo $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment