2010-03-21 20:01:50 +01:00
|
|
|
#!/usr/bin/env python
|
2007-01-12 22:57:03 +01:00
|
|
|
|
2008-01-15 02:11:19 +01:00
|
|
|
""" hg-to-git.py - A Mercurial to GIT converter
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
Copyright (C)2007 Stelian Pop <stelian@popies.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os, os.path, sys
|
2009-08-03 18:41:34 +02:00
|
|
|
import tempfile, pickle, getopt
|
2007-01-12 22:57:03 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
# Maps hg version -> git version
|
|
|
|
hgvers = {}
|
|
|
|
# List of children for each hg revision
|
|
|
|
hgchildren = {}
|
2008-01-15 02:11:19 +01:00
|
|
|
# List of parents for each hg revision
|
|
|
|
hgparents = {}
|
2007-01-12 22:57:03 +01:00
|
|
|
# Current branch for each hg revision
|
|
|
|
hgbranch = {}
|
2007-10-06 23:16:51 +02:00
|
|
|
# Number of new changesets converted from hg
|
|
|
|
hgnewcsets = 0
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def usage():
|
|
|
|
|
|
|
|
print """\
|
|
|
|
%s: [OPTIONS] <hgprj>
|
|
|
|
|
|
|
|
options:
|
|
|
|
-s, --gitstate=FILE: name of the state to be saved/read
|
|
|
|
for incrementals
|
2007-10-06 23:16:51 +02:00
|
|
|
-n, --nrepack=INT: number of changesets that will trigger
|
|
|
|
a repack (default=0, -1 to deactivate)
|
2008-05-26 15:20:54 +02:00
|
|
|
-v, --verbose: be verbose
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
required:
|
|
|
|
hgprj: name of the HG project to import (directory)
|
|
|
|
""" % sys.argv[0]
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def getgitenv(user, date):
|
|
|
|
env = ''
|
|
|
|
elems = re.compile('(.*?)\s+<(.*)>').match(user)
|
|
|
|
if elems:
|
|
|
|
env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
|
2010-01-09 01:54:39 +01:00
|
|
|
env += 'export GIT_COMMITTER_NAME="%s" ;' % elems.group(1)
|
2007-01-12 22:57:03 +01:00
|
|
|
env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
|
2010-01-09 01:54:39 +01:00
|
|
|
env += 'export GIT_COMMITTER_EMAIL="%s" ;' % elems.group(2)
|
2007-01-12 22:57:03 +01:00
|
|
|
else:
|
|
|
|
env += 'export GIT_AUTHOR_NAME="%s" ;' % user
|
2010-01-09 01:54:39 +01:00
|
|
|
env += 'export GIT_COMMITTER_NAME="%s" ;' % user
|
2007-01-12 22:57:03 +01:00
|
|
|
env += 'export GIT_AUTHOR_EMAIL= ;'
|
2010-01-09 01:54:39 +01:00
|
|
|
env += 'export GIT_COMMITTER_EMAIL= ;'
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
env += 'export GIT_AUTHOR_DATE="%s" ;' % date
|
|
|
|
env += 'export GIT_COMMITTER_DATE="%s" ;' % date
|
|
|
|
return env
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
state = ''
|
2007-10-06 23:16:51 +02:00
|
|
|
opt_nrepack = 0
|
2008-05-26 15:20:54 +02:00
|
|
|
verbose = False
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
try:
|
2008-05-26 15:20:54 +02:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 's:t:n:v', ['gitstate=', 'tempdir=', 'nrepack=', 'verbose'])
|
2007-01-12 22:57:03 +01:00
|
|
|
for o, a in opts:
|
|
|
|
if o in ('-s', '--gitstate'):
|
|
|
|
state = a
|
|
|
|
state = os.path.abspath(state)
|
2007-10-06 23:16:51 +02:00
|
|
|
if o in ('-n', '--nrepack'):
|
|
|
|
opt_nrepack = int(a)
|
2008-05-26 15:20:54 +02:00
|
|
|
if o in ('-v', '--verbose'):
|
|
|
|
verbose = True
|
2007-01-12 22:57:03 +01:00
|
|
|
if len(args) != 1:
|
2008-07-06 05:15:17 +02:00
|
|
|
raise Exception('params')
|
2007-01-12 22:57:03 +01:00
|
|
|
except:
|
|
|
|
usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
hgprj = args[0]
|
|
|
|
os.chdir(hgprj)
|
|
|
|
|
|
|
|
if state:
|
|
|
|
if os.path.exists(state):
|
2008-05-26 15:20:54 +02:00
|
|
|
if verbose:
|
|
|
|
print 'State does exist, reading'
|
2007-01-12 22:57:03 +01:00
|
|
|
f = open(state, 'r')
|
|
|
|
hgvers = pickle.load(f)
|
|
|
|
else:
|
|
|
|
print 'State does not exist, first run'
|
|
|
|
|
2008-07-06 05:15:18 +02:00
|
|
|
sock = os.popen('hg tip --template "{rev}"')
|
|
|
|
tip = sock.read()
|
|
|
|
if sock.close():
|
|
|
|
sys.exit(1)
|
2008-05-26 15:20:54 +02:00
|
|
|
if verbose:
|
|
|
|
print 'tip is', tip
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# Calculate the branches
|
2008-05-26 15:20:54 +02:00
|
|
|
if verbose:
|
|
|
|
print 'analysing the branches...'
|
2007-01-12 22:57:03 +01:00
|
|
|
hgchildren["0"] = ()
|
2008-01-15 02:11:19 +01:00
|
|
|
hgparents["0"] = (None, None)
|
2007-01-12 22:57:03 +01:00
|
|
|
hgbranch["0"] = "master"
|
|
|
|
for cset in range(1, int(tip) + 1):
|
|
|
|
hgchildren[str(cset)] = ()
|
2008-02-15 22:20:44 +01:00
|
|
|
prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
|
2008-01-15 02:11:19 +01:00
|
|
|
prnts = map(lambda x: x[:x.find(':')], prnts)
|
|
|
|
if prnts[0] != '':
|
2007-01-12 22:57:03 +01:00
|
|
|
parent = prnts[0].strip()
|
|
|
|
else:
|
|
|
|
parent = str(cset - 1)
|
|
|
|
hgchildren[parent] += ( str(cset), )
|
|
|
|
if len(prnts) > 1:
|
|
|
|
mparent = prnts[1].strip()
|
|
|
|
hgchildren[mparent] += ( str(cset), )
|
|
|
|
else:
|
|
|
|
mparent = None
|
|
|
|
|
2008-01-15 02:11:19 +01:00
|
|
|
hgparents[str(cset)] = (parent, mparent)
|
|
|
|
|
2007-01-12 22:57:03 +01:00
|
|
|
if mparent:
|
|
|
|
# For merge changesets, take either one, preferably the 'master' branch
|
|
|
|
if hgbranch[mparent] == 'master':
|
|
|
|
hgbranch[str(cset)] = 'master'
|
|
|
|
else:
|
|
|
|
hgbranch[str(cset)] = hgbranch[parent]
|
|
|
|
else:
|
|
|
|
# Normal changesets
|
|
|
|
# For first children, take the parent branch, for the others create a new branch
|
|
|
|
if hgchildren[parent][0] == str(cset):
|
|
|
|
hgbranch[str(cset)] = hgbranch[parent]
|
|
|
|
else:
|
|
|
|
hgbranch[str(cset)] = "branch-" + str(cset)
|
|
|
|
|
|
|
|
if not hgvers.has_key("0"):
|
|
|
|
print 'creating repository'
|
2008-07-06 05:15:20 +02:00
|
|
|
os.system('git init')
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# loop through every hg changeset
|
|
|
|
for cset in range(int(tip) + 1):
|
|
|
|
|
|
|
|
# incremental, already seen
|
|
|
|
if hgvers.has_key(str(cset)):
|
|
|
|
continue
|
2007-10-06 23:16:51 +02:00
|
|
|
hgnewcsets += 1
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# get info
|
2008-01-15 02:11:19 +01:00
|
|
|
log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
|
|
|
|
tag = log_data[0].strip()
|
|
|
|
date = log_data[1].strip()
|
|
|
|
user = log_data[2].strip()
|
|
|
|
parent = hgparents[str(cset)][0]
|
|
|
|
mparent = hgparents[str(cset)][1]
|
|
|
|
|
|
|
|
#get comment
|
2007-01-12 22:57:03 +01:00
|
|
|
(fdcomment, filecomment) = tempfile.mkstemp()
|
2008-01-15 02:11:19 +01:00
|
|
|
csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
|
2007-01-12 22:57:03 +01:00
|
|
|
os.write(fdcomment, csetcomment)
|
|
|
|
os.close(fdcomment)
|
|
|
|
|
|
|
|
print '-----------------------------------------'
|
|
|
|
print 'cset:', cset
|
|
|
|
print 'branch:', hgbranch[str(cset)]
|
|
|
|
print 'user:', user
|
|
|
|
print 'date:', date
|
|
|
|
print 'comment:', csetcomment
|
2008-01-15 02:11:19 +01:00
|
|
|
if parent:
|
|
|
|
print 'parent:', parent
|
2007-01-12 22:57:03 +01:00
|
|
|
if mparent:
|
|
|
|
print 'mparent:', mparent
|
|
|
|
if tag:
|
|
|
|
print 'tag:', tag
|
|
|
|
print '-----------------------------------------'
|
|
|
|
|
|
|
|
# checkout the parent if necessary
|
|
|
|
if cset != 0:
|
|
|
|
if hgbranch[str(cset)] == "branch-" + str(cset):
|
|
|
|
print 'creating new branch', hgbranch[str(cset)]
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
|
2007-01-12 22:57:03 +01:00
|
|
|
else:
|
|
|
|
print 'checking out branch', hgbranch[str(cset)]
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git checkout %s' % hgbranch[str(cset)])
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# merge
|
|
|
|
if mparent:
|
|
|
|
if hgbranch[parent] == hgbranch[str(cset)]:
|
|
|
|
otherbranch = hgbranch[mparent]
|
|
|
|
else:
|
|
|
|
otherbranch = hgbranch[parent]
|
|
|
|
print 'merging', otherbranch, 'into', hgbranch[str(cset)]
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# remove everything except .git and .hg directories
|
|
|
|
os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
|
|
|
|
|
|
|
|
# repopulate with checkouted files
|
|
|
|
os.system('hg update -C %d' % cset)
|
|
|
|
|
|
|
|
# add new files
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git ls-files -x .hg --others | git update-index --add --stdin')
|
2007-01-12 22:57:03 +01:00
|
|
|
# delete removed files
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# commit
|
2007-12-06 16:26:29 +01:00
|
|
|
os.system(getgitenv(user, date) + 'git commit --allow-empty -a -F %s' % filecomment)
|
2007-01-12 22:57:03 +01:00
|
|
|
os.unlink(filecomment)
|
|
|
|
|
|
|
|
# tag
|
|
|
|
if tag and tag != 'tip':
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system(getgitenv(user, date) + 'git tag %s' % tag)
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# delete branch if not used anymore...
|
|
|
|
if mparent and len(hgchildren[str(cset)]):
|
|
|
|
print "Deleting unused branch:", otherbranch
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git branch -d %s' % otherbranch)
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# retrieve and record the version
|
2008-07-06 05:15:19 +02:00
|
|
|
vvv = os.popen('git show --quiet --pretty=format:%H').read()
|
2007-01-12 22:57:03 +01:00
|
|
|
print 'record', cset, '->', vvv
|
|
|
|
hgvers[str(cset)] = vvv
|
|
|
|
|
2007-10-06 23:16:51 +02:00
|
|
|
if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
|
2008-07-06 05:15:19 +02:00
|
|
|
os.system('git repack -a -d')
|
2007-01-12 22:57:03 +01:00
|
|
|
|
|
|
|
# write the state for incrementals
|
|
|
|
if state:
|
2008-05-26 15:20:54 +02:00
|
|
|
if verbose:
|
|
|
|
print 'Writing state'
|
2007-01-12 22:57:03 +01:00
|
|
|
f = open(state, 'w')
|
|
|
|
pickle.dump(hgvers, f)
|
|
|
|
|
|
|
|
# vim: et ts=8 sw=4 sts=4
|