#!/usr/bin/env bash
starttime=$(date +'%Y-%m-%d--%H-%M-%S')
output_dir="portage-git-repo--${starttime}"

# Open logged subshell
(

# Print executed bash commands
set -x

# Rip/sync anon SVN using rsync
rsync -r rsync://anonvcs.gentoo.org/vcs-public-svnroot/portage/ \
    portage-anon-svn-repo-dump/ || exit 1

# Init git-svn repo, note double use of --trunk
[ -d "${output_dir}" ] && exit 1
git svn init file://${PWD}/portage-anon-svn-repo-dump/ \
    --rewrite-root=svn://anonsvn.gentoo.org/portage/ \
    --trunk=main/trunk --tags=main/tags --branches=main/branches \
    "${output_dir}"
cd "${output_dir}" || exit 1

# Convert commits
git config svn.authorsfile ../portage-author-map.txt
time git svn fetch || exit 1

# Make real Git tags from remotes/tags/* (two special cases)
for branch_tag in $(git branch -r | grep 'tags/' \
    | fgrep -v 'tags/portage-2.1_pre5'); do
  tag=$(sed 's|^tags/||' <<<"${branch_tag}")
  git tag "v${tag}" "remotes/${branch_tag}" || exit 1
done
git tag 'trunk@1888' 'remotes/trunk@1888' || exit 1

# Make local branches from remotes/* (excluding tags and trunk)
for branch in $(git branch -r | grep -v 'tags\|trunk'); do
  git checkout -b "${branch}" "remotes/${branch}" || exit 1
done
git checkout master || exit 1

# Reduce size of repository
dotgitsize() { du --human --total .git | tail -n 1; }
dotgitsize
git gc --aggressive
dotgitsize

# Wipe all traces of SVN
git config --remove-section 'svn-remote.svn'
git config --remove-section 'svn'
rm -R .git/svn
rm -R .git/logs/refs/remotes
for file in .git/info/refs .git/packed-refs ; do
  sed -e '/remotes\//d' -i "${file}"
done

# Hide executed bash commands
set +x


cat <<INFO

DONE.


NEXT STEPS:
  # cd "${output_dir}"

  Verify that branches and tags
  1. make sense
  2. are unambiguous
  3. are free of SVN
    # git branch -a
    # git show-branch --list
    # git tag -l

  Push full repository
    # git remote add \${remote_name} \${push_url}
    # git push --mirror \${remote_name}
INFO

) |& tee conversion--${starttime}.log
