#!/bin/bash
PATH=/usr/bin:/bin

REPO=/srv/git/wordpress-main.git
REMOTE=github

cd $REPO
git svn fetch
git svn rebase -l

# Make the git master branch always track the svn trunk
git update-ref refs/heads/master refs/remotes/upstream/trunk

# Copy all other remote branches (only svn branches, excluding trunk and tags) to normal git branches
git for-each-ref refs/remotes/upstream | cut -d / -f 4- | grep -v -x trunk | grep -v tags | while read ref
do
git update-ref "refs/heads/$ref" "refs/remotes/upstream/$ref"
done

#make a lightweight tag for any unseen svn tags, then delete the tags/tag branches
git for-each-ref refs/remotes/upstream/tags | cut -d / -f 5- | while read ref
do
git show-ref --quiet --verify "refs/tags/$ref" && continue
git tag "$ref" "refs/remotes/upstream/tags/$ref"
git update-ref -d "refs/remotes/upstream/tags/$ref" "refs/remotes/upstream/tags/$ref"
done

# Prune branches or tags that have been removed in svn
git for-each-ref refs/heads | cut -d / -f 3- | grep -v -x master | while read ref
do
git show-ref --quiet --verify "refs/remotes/upstream/$ref" || git update-ref -d "refs/heads/$ref" "refs/heads/$ref"
done

git gc --auto
git push $REMOTE --mirror