Skip to content

Instantly share code, notes, and snippets.

@tav
Created November 19, 2011 15:39
Show Gist options
  • Save tav/1378969 to your computer and use it in GitHub Desktop.
Save tav/1378969 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import sys
from commands import getoutput
from os import listdir, unlink
from subprocess import call
from time import sleep
from traceback import print_exc
BRANCH_MAP = {
'default': 'master'
}
def run(*args):
retcode = call(args)
if retcode:
raise RuntimeError(repr(args))
prev = {}
git_branches = set(listdir('.hg/git/refs/heads'))
git_branches.discard('hg')
def main():
"""Update hg, export to git and push up references."""
run('hg', 'pull', '-u')
run('hg', 'gexport')
branches = {}
hg_heads = getoutput('hg branches --debug')
for line in hg_heads.split('\n'):
branch, rev = line.split()[:2]
rev = rev.split(':')[1]
branches[branch] = rev
hg2git = {}
mapfile = open('.hg/git-mapfile', 'rb')
for line in mapfile:
gitsha, hgsha = line.strip().split(' ', 1)
hg2git[hgsha] = gitsha
mapfile.close()
add = {}
for branch, hgsha in branches.iteritems():
git_branch = BRANCH_MAP.get(branch, branch)
add[git_branch] = hg2git[hgsha]
for branch in list(git_branches):
if branch not in add:
if branch in ('master', 'tav-patches'):
continue
print "# Removing Branch:", branch
git_branches.remove(branch)
run('git', '--git-dir=.hg/git', 'push', '[email protected]:tav/go.git', ':' + branch)
unlink('.hg/git/refs/heads/' + branch)
changes_exist = 0
print "# Updating Branches"
for branch, id in add.iteritems():
if branch in prev:
if prev[branch] == id:
continue
changes_exist = 1
git_branches.add(branch)
ref = open('.hg/git/refs/heads/' + branch, 'wb')
ref.write(id + '\n')
ref.close()
run('git', '--git-dir=.hg/git', 'push', '[email protected]:tav/go.git', branch, '-v')
prev.clear()
prev.update(add)
if changes_exist:
run('git', '--git-dir=.hg/git', 'push', '[email protected]:tav/go.git', '--tags', '--force')
while 1:
print "# Syncing"
try:
main()
except Exception:
print_exc()
print "# Sleeping"
sleep(900)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment