Skip to content

Instantly share code, notes, and snippets.

@lietu
Last active September 15, 2019 09:18
Show Gist options
  • Save lietu/ae64ddbb8c3135b9a954fa1f3dd85013 to your computer and use it in GitHub Desktop.
Save lietu/ae64ddbb8c3135b9a954fa1f3dd85013 to your computer and use it in GitHub Desktop.
Script for unattended sync of a Mercurial (hg) repository to a Git repository
#!/usr/bin/env sh
#
# Unattended sync of Mercurial to Git
#
# You will need to configure GIT_REPO, possibly HG_REPO
# If using SSH you will also need to set up the SSH agent + key
#
# You will also need git-fast-import and git-remote-hg:
# https://github.com/felipec/git-remote-hg
#
set -exu
# Which Git repo to sync with - change this for sure
GIT_REPO="git@..."
# Paths
HG_REPO="$(pwd -P)" # You might need to change this
GIT_TMP="/tmp/git-sync-$(date +%s)"
# Ensure SSH host is "known", you might have to change the grep
REMOTE_HOST=$(echo "${GIT_REPO}" | grep -Eo "git@([^:]+)" | cut -d@ -f2)
if [ "$(ssh-keygen -F ${GIT_REPO})" == "" ]; then
ssh-keyscan -H "${GIT_REPO}" >> ~/.ssh/known_hosts
fi
# Set up temp git folder
mkdir "${GIT_TMP}"
cd "${GIT_TMP}"
git init .
# Sync from HG
git config core.notesRef refs/notes/hg
git remote add origin "hg::${HG_REPO}"
git fetch --all
git checkout master
# Sync to Git
git remote add other "${GIT_REPO}"
git config remote.other.mirror true
git config --add remote.other.push '+refs/heads/*:refs/heads/*'
git config --add remote.other.push '+refs/tags/*:refs/tags/*'
git push other
git push --all -f other
# Clean up
cd "${HG_REPO}"
rm -rf "${GIT_TMP}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment