#!/bin/sh
#
# This script converts an existing svn repository into a mercurial repository.
#
# USAGE:   Edit "from" and "to" variables below to suit your needs, then fire
#          the script.
# NOTE:    This script is very dumb: it just repeats the history of the svn
#          repository one commit at a time, and as such is very slow.  Other
#          than that, it shouldn't do anything nasty, e.g. burn your computer
#          down...
#
# AUTHOR:  Jules Villard <jules.villard.nospam.remove.me.please@ens-lyon.org>
# LICENSE: This program is free software. It comes without any warranty, to
#          the extent permitted by applicable law. You can redistribute it
#          and/or modify it under the terms of the Do What The Fuck You Want
#          To Public License, Version 2, as published by Sam Hocevar. See
#          http://sam.zoy.org/wtfpl/COPYING for more details.
#

from=/path/to/existing/svn/repo
to=/path/to/target/hg/repo

# Not sure how it would behave under arbitrary locale
LANG=C

# A temporary directory
tmp=$(mktemp)

# Initialize hg repository at $to
svn export -r 1 "$from" "$to"
hg init --cwd "$to"

# Repeat history
cd "$from"
tip=$(svn up | cut -d ' ' -f 3 | tr -d .)
pretip=$(($tip -1))
for i in $(seq 1 $pretip); do
  # Retrieve log message
  svn log -r $i | tail +4 | head -n-2 > $tmp
  # If none, at least put some dumb message
  [ -s $tmp ] || (echo "some dumb message" > $tmp)
  # Retrieve date of commit
  date=$(svn log -r $i | head -n2 | tail -1 | cut -d \| -f 3 | cut -d \( -f 1)
  # Redo the actual commit
  hg add --cwd "$to"
  hg commit -l $tmp --date "$date" --cwd "$to"

  # Jump to next commit
  if [ "$i" != "$pretip" ]; then
    svn diff -r $i:$(($i+1)) | patch -d "$to"
  fi
done

# Put hg repo in the same temporary state
svn diff | patch -d "$to"
