1#! /usr/bin/python
23
""" hg-to-svn.py - A Mercurial to GIT converter
45
Copyright (C)2007 Stelian Pop <stelian@popies.net>
67
This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
1112
This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
1617
You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20"""
2122
import os, os.path, sys
23import tempfile, popen2, pickle, getopt
24import re
2526
# Maps hg version -> git version
27hgvers = {}
28# List of children for each hg revision
29hgchildren = {}
30# Current branch for each hg revision
31hgbranch = {}
3233
#------------------------------------------------------------------------------
3435
def usage():
3637
print """\
38%s: [OPTIONS] <hgprj>
3940
options:
41-s, --gitstate=FILE: name of the state to be saved/read
42for incrementals
4344
required:
45hgprj: name of the HG project to import (directory)
46""" % sys.argv[0]
4748
#------------------------------------------------------------------------------
4950
def getgitenv(user, date):
51env = ''
52elems = re.compile('(.*?)\s+<(.*)>').match(user)
53if elems:
54env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
55env += 'export GIT_COMMITER_NAME="%s" ;' % elems.group(1)
56env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
57env += 'export GIT_COMMITER_EMAIL="%s" ;' % elems.group(2)
58else:
59env += 'export GIT_AUTHOR_NAME="%s" ;' % user
60env += 'export GIT_COMMITER_NAME="%s" ;' % user
61env += 'export GIT_AUTHOR_EMAIL= ;'
62env += 'export GIT_COMMITER_EMAIL= ;'
6364
env += 'export GIT_AUTHOR_DATE="%s" ;' % date
65env += 'export GIT_COMMITTER_DATE="%s" ;' % date
66return env
6768
#------------------------------------------------------------------------------
6970
state = ''
7172
try:
73opts, args = getopt.getopt(sys.argv[1:], 's:t:', ['gitstate=', 'tempdir='])
74for o, a in opts:
75if o in ('-s', '--gitstate'):
76state = a
77state = os.path.abspath(state)
7879
if len(args) != 1:
80raise('params')
81except:
82usage()
83sys.exit(1)
8485
hgprj = args[0]
86os.chdir(hgprj)
8788
if state:
89if os.path.exists(state):
90print 'State does exist, reading'
91f = open(state, 'r')
92hgvers = pickle.load(f)
93else:
94print 'State does not exist, first run'
9596
tip = os.popen('hg tip | head -1 | cut -f 2 -d :').read().strip()
97print 'tip is', tip
9899
# Calculate the branches
100print 'analysing the branches...'
101hgchildren["0"] = ()
102hgbranch["0"] = "master"
103for cset in range(1, int(tip) + 1):
104hgchildren[str(cset)] = ()
105prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()
106if len(prnts) > 0:
107parent = prnts[0].strip()
108else:
109parent = str(cset - 1)
110hgchildren[parent] += ( str(cset), )
111if len(prnts) > 1:
112mparent = prnts[1].strip()
113hgchildren[mparent] += ( str(cset), )
114else:
115mparent = None
116117
if mparent:
118# For merge changesets, take either one, preferably the 'master' branch
119if hgbranch[mparent] == 'master':
120hgbranch[str(cset)] = 'master'
121else:
122hgbranch[str(cset)] = hgbranch[parent]
123else:
124# Normal changesets
125# For first children, take the parent branch, for the others create a new branch
126if hgchildren[parent][0] == str(cset):
127hgbranch[str(cset)] = hgbranch[parent]
128else:
129hgbranch[str(cset)] = "branch-" + str(cset)
130131
if not hgvers.has_key("0"):
132print 'creating repository'
133os.system('git-init-db')
134135
# loop through every hg changeset
136for cset in range(int(tip) + 1):
137138
# incremental, already seen
139if hgvers.has_key(str(cset)):
140continue
141142
# get info
143prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()
144if len(prnts) > 0:
145parent = prnts[0].strip()
146else:
147parent = str(cset - 1)
148if len(prnts) > 1:
149mparent = prnts[1].strip()
150else:
151mparent = None
152153
(fdcomment, filecomment) = tempfile.mkstemp()
154csetcomment = os.popen('hg log -r %d -v | grep -v ^changeset: | grep -v ^parent: | grep -v ^user: | grep -v ^date | grep -v ^files: | grep -v ^description: | grep -v ^tag:' % cset).read().strip()
155os.write(fdcomment, csetcomment)
156os.close(fdcomment)
157158
date = os.popen('hg log -r %d | grep ^date: | cut -f 2- -d :' % cset).read().strip()
159160
tag = os.popen('hg log -r %d | grep ^tag: | cut -f 2- -d :' % cset).read().strip()
161162
user = os.popen('hg log -r %d | grep ^user: | cut -f 2- -d :' % cset).read().strip()
163164
print '-----------------------------------------'
165print 'cset:', cset
166print 'branch:', hgbranch[str(cset)]
167print 'user:', user
168print 'date:', date
169print 'comment:', csetcomment
170print 'parent:', parent
171if mparent:
172print 'mparent:', mparent
173if tag:
174print 'tag:', tag
175print '-----------------------------------------'
176177
# checkout the parent if necessary
178if cset != 0:
179if hgbranch[str(cset)] == "branch-" + str(cset):
180print 'creating new branch', hgbranch[str(cset)]
181os.system('git-checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
182else:
183print 'checking out branch', hgbranch[str(cset)]
184os.system('git-checkout %s' % hgbranch[str(cset)])
185186
# merge
187if mparent:
188if hgbranch[parent] == hgbranch[str(cset)]:
189otherbranch = hgbranch[mparent]
190else:
191otherbranch = hgbranch[parent]
192print 'merging', otherbranch, 'into', hgbranch[str(cset)]
193os.system(getgitenv(user, date) + 'git-merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
194195
# remove everything except .git and .hg directories
196os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
197198
# repopulate with checkouted files
199os.system('hg update -C %d' % cset)
200201
# add new files
202os.system('git-ls-files -x .hg --others | git-update-index --add --stdin')
203# delete removed files
204os.system('git-ls-files -x .hg --deleted | git-update-index --remove --stdin')
205206
# commit
207os.system(getgitenv(user, date) + 'git-commit -a -F %s' % filecomment)
208os.unlink(filecomment)
209210
# tag
211if tag and tag != 'tip':
212os.system(getgitenv(user, date) + 'git-tag %s' % tag)
213214
# delete branch if not used anymore...
215if mparent and len(hgchildren[str(cset)]):
216print "Deleting unused branch:", otherbranch
217os.system('git-branch -d %s' % otherbranch)
218219
# retrieve and record the version
220vvv = os.popen('git-show | head -1').read()
221vvv = vvv[vvv.index(' ') + 1 : ].strip()
222print 'record', cset, '->', vvv
223hgvers[str(cset)] = vvv
224225
os.system('git-repack -a -d')
226227
# write the state for incrementals
228if state:
229print 'Writing state'
230f = open(state, 'w')
231pickle.dump(hgvers, f)
232233
# vim: et ts=8 sw=4 sts=4