#!/bin/sh
#
# svn-to-git - Subversion-to-git conversion-tool dispatcher
#
# The -t option sets the conversion tool. The default is 'reposurgeon'.
# Other possible values are 'git-svn', 'agito', and 'svn2git'.
#
# Requires two arguments: A Subversion repo directory (which must exist) 
# and a git repo output directory (which must not exist).
#
# The REPOSURGEON environment variable can be used to substitute in a
# different implementation.
#
# The TESTOPT variable can be used to pass an early command or option setting
# to reposurgeon.
#
# FIXME:  This has not yet been fully adapted to work with unreliable $PWD.
. ./common-setup.sh

PATH=$PATH:..

while getopts t: opt
do
    case $opt in
	t) tool="$OPTARG" ;;
    esac
done
shift $(($OPTIND - 1))

trap 'rm -fr git-scratch /tmp/agito$$' EXIT HUP INT TERM

svnrepo=$1
if [ -z "$svnrepo" ]
then
    echo "svn-to-git: requires a path to a Subversion repo as first argument."
    exit 1
fi
gitrepo=$2
if [ -z "$gitrepo" ]
then
    echo "svn-to-git: output directory argument is required."
    exit 1
fi
if [ -d "$gitrepo" ] 
then
    echo "svn-to-git: output directory $gitrepo must not already exist."
    exit 1
fi

(

# Convert the repo to a local git repository.
rm -fr $gitrepo
case $tool in
git-svn) git svn clone --stdlayout --no-metadata --quiet file://${PWD}/$svnrepo $gitrepo ;;
agito) #git submodule update
	cat >/tmp/agito$$ <<EOF
# URL of the Subversion repository.
SVN_REPO = "file://${PWD}/$svnrepo"

# Path to directory where the Git repository containing the converted
# history should be stored.
GIT_REPO = "$gitrepo"

BRANCHES = {
	"/trunk" : "master",
	"/branches/%" : "%",
}

TAGS = {
	"/tags/%" : "%",
}

# Subversion username to Git author mapping.

AUTHORS = {
	None : ("Nobody", "nobody@localhost"),
}

# Default author, when not found in the AUTHORS map above. The '%'
# is replaced with the Subversion username.

DEFAULT_AUTHOR = ("%", "%@example.com")

CREATE_TAG_OBJECTS = True

# These lines are always included in the root .gitignore file.

SVN_DEFAULT_IGNORES = """
# These are the default patterns globally ignored by Subversion:
*.o
*.lo
*.la
*.al
.libs
*.so
*.so.[0-9]*
*.a
*.pyc
*.pyo
*.rej
*~
.#*
.*.swp
.DS_store
"""

# Functions to apply to commit messages to convert from Subversion
# commit messages to Git ones.

COMMIT_MESSAGE_FILTERS = [
	agito.reflow_text,         # Reflow text to 72 columns
	agito.append_branch_info,  # Append SVN branch/rev info.
]

# List of revision numbers of Subversion revisions that should be
# "filtered" from the converted history: any changes made in a filtered
# revision will be included in the following revision instead. If you
# filter a commit that is the head of a branch, you're going to have a
# bad time.

FILTER_REVISIONS = []

# Callback function to invoke before creating each commit. This is
# similar to Git's 'filter-branch' command. Invoked with the branch
# path, log entry metadata dictionary, and Directory object representing
# the tree.
#
# The metadata dictionary contains the data used in constructing the Git
# commit - the keys are the same names as the environment variables set
# by 'git filter-branch'.

#def my_filter_callback(path, entry, metadata, treedir):
#	pass
#
#FILTER_BRANCH_CALLBACK = my_filter_callback

# Subversion properties used for tracking merges. This maps from a
# property name to a callback function to invoke when the property with
# that value is changed on the root of a branch. The function returns a
# tuple containing the Subversion path of the branch that was merged,
# and the revision number of the point on the branch that was merged.

#def my_merge_callback(path, entry, changed):
#	old_value, new_value = changed
#	...
#	return ("/branches/foo-branch", 1234)

MERGE_CALLBACKS = {

}

# vim: set ft=python:
EOF
	python ../externals/agito/agito.py /tmp/agito$$ ;;
svn2git) mkdir git-scratch; cp -r $svnrepo git-scratch;
	(cd git-scratch >/dev/null && \
	    ruby -I../../externals/svn2git/lib \
	    ../../externals/svn2git/bin/svn2git file://${PWD}/$svnrepo) && \
	    mv git-scratch $gitrepo ;;
*)  ${REPOSURGEON:-reposurgeon} "${TESTOPT}" "read $svnrepo" "prefer git" "rebuild $gitrepo"
esac

) >&2

exit 0
