Skip to content

Instantly share code, notes, and snippets.

@garymacindoe
Last active August 29, 2015 14:28
Show Gist options
  • Save garymacindoe/7626e2b3beaaa8944f9a to your computer and use it in GitHub Desktop.
Save garymacindoe/7626e2b3beaaa8944f9a to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Converts mercurial repostories to git repositories.
# Requires git-remote-hg from https://github.com/felipec/git-remote-hg.
#
if [ $# -lt 2 ] || [ $# -gt 3 ]
then
echo "Usage: ${0} <url> <local_directory> [user_map]"
echo "Where"
echo " url is the url of a mercurial repository"
echo " local_directory is a directory to put the bare git repository in"
echo " user_map is the path to a file containing hg to git user mappings in CSV"
echo " eg:"
echo " User Name,[email protected],[email protected]"
exit
fi
URL="${1}"
LOCAL_DIR="${2}"
USER_MAP="${3}"
# Clone from remote mercurial to local bare git (using --mirror)
git clone --mirror "hg::${URL}" "${LOCAL_DIR}" || exit $?
git -C "${LOCAL_DIR}" --bare gc --aggressive || exit $? # Recommended by git-remote-hg
# Rewrite history to map mercurial usernames to git usernames and email
# addresses
# Copied from https://gist.github.com/octocat/0831f3fbd83ac4d46451
# This needs to be done in a bare git repository
if [ "${USER_MAP}" ] && [ -f "${USER_MAP}" ]
then
export correct_name
export old_email
export correct_email
grep -v "^#" "${USER_MAP}" | \
while IFS=',' read correct_name old_email correct_email
do
git -C "${LOCAL_DIR}" --bare filter-branch --env-filter '
if [ "${GIT_COMMITER_EMAIL}" = "${old_email}" ]
then
export GIT_COMMITER_NAME="${correct_name}"
export GIT_COMMITER_EMAIL="${correct_email}"
fi
if [ "${GIT_AUTHOR_EMAIL}" = "${old_email}" ]
then
export GIT_AUTHOR_NAME="${correct_name}"
export GIT_AUTHOR_EMAIL="${correct_email}"
fi
' --force --tag-name-filter cat -- --branches --tags || exit $?
done
fi
# Rename branches from "branches/${branch}" to "${branch}"
git -C "${LOCAL_DIR}" --bare branch -a | \
grep branches | \
while read branch
do
git -C "${LOCAL_DIR}" --bare branch -m ${branch} ${branch//branches\/} || exit $?
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment