3 """ hg-to-git.py - A Mercurial to GIT converter
5 Copyright (C)2007 Stelian Pop <stelian@popies.net>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 import os, os.path, sys
23 import tempfile, pickle, getopt
26 # Maps hg version -> git version
28 # List of children for each hg revision
30 # List of parents for each hg revision
32 # Current branch for each hg revision
34 # Number of new changesets converted from hg
37 #------------------------------------------------------------------------------
45 -s, --gitstate=FILE: name of the state to be saved/read
47 -n, --nrepack=INT: number of changesets that will trigger
48 a repack (default=0, -1 to deactivate)
49 -v, --verbose: be verbose
52 hgprj: name of the HG project to import (directory)
55 #------------------------------------------------------------------------------
57 def getgitenv(user, date):
59 elems = re.compile('(.*?)\s+<(.*)>').match(user)
61 env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
62 env += 'export GIT_COMMITTER_NAME="%s" ;' % elems.group(1)
63 env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
64 env += 'export GIT_COMMITTER_EMAIL="%s" ;' % elems.group(2)
66 env += 'export GIT_AUTHOR_NAME="%s" ;' % user
67 env += 'export GIT_COMMITTER_NAME="%s" ;' % user
68 env += 'export GIT_AUTHOR_EMAIL= ;'
69 env += 'export GIT_COMMITTER_EMAIL= ;'
71 env += 'export GIT_AUTHOR_DATE="%s" ;' % date
72 env += 'export GIT_COMMITTER_DATE="%s" ;' % date
75 #------------------------------------------------------------------------------
82 opts, args = getopt.getopt(sys.argv[1:], 's:t:n:v', ['gitstate=', 'tempdir=', 'nrepack=', 'verbose'])
84 if o in ('-s', '--gitstate'):
86 state = os.path.abspath(state)
87 if o in ('-n', '--nrepack'):
89 if o in ('-v', '--verbose'):
92 raise Exception('params')
101 if os.path.exists(state):
103 print 'State does exist, reading'
105 hgvers = pickle.load(f)
107 print 'State does not exist, first run'
109 sock = os.popen('hg tip --template "{rev}"')
116 # Calculate the branches
118 print 'analysing the branches...'
120 hgparents["0"] = (None, None)
121 hgbranch["0"] = "master"
122 for cset in range(1, int(tip) + 1):
123 hgchildren[str(cset)] = ()
124 prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
125 prnts = map(lambda x: x[:x.find(':')], prnts)
127 parent = prnts[0].strip()
129 parent = str(cset - 1)
130 hgchildren[parent] += ( str(cset), )
132 mparent = prnts[1].strip()
133 hgchildren[mparent] += ( str(cset), )
137 hgparents[str(cset)] = (parent, mparent)
140 # For merge changesets, take either one, preferably the 'master' branch
141 if hgbranch[mparent] == 'master':
142 hgbranch[str(cset)] = 'master'
144 hgbranch[str(cset)] = hgbranch[parent]
147 # For first children, take the parent branch, for the others create a new branch
148 if hgchildren[parent][0] == str(cset):
149 hgbranch[str(cset)] = hgbranch[parent]
151 hgbranch[str(cset)] = "branch-" + str(cset)
153 if not hgvers.has_key("0"):
154 print 'creating repository'
155 os.system('git init')
157 # loop through every hg changeset
158 for cset in range(int(tip) + 1):
160 # incremental, already seen
161 if hgvers.has_key(str(cset)):
166 log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
167 tag = log_data[0].strip()
168 date = log_data[1].strip()
169 user = log_data[2].strip()
170 parent = hgparents[str(cset)][0]
171 mparent = hgparents[str(cset)][1]
174 (fdcomment, filecomment) = tempfile.mkstemp()
175 csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
176 os.write(fdcomment, csetcomment)
179 print '-----------------------------------------'
181 print 'branch:', hgbranch[str(cset)]
184 print 'comment:', csetcomment
186 print 'parent:', parent
188 print 'mparent:', mparent
191 print '-----------------------------------------'
193 # checkout the parent if necessary
195 if hgbranch[str(cset)] == "branch-" + str(cset):
196 print 'creating new branch', hgbranch[str(cset)]
197 os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
199 print 'checking out branch', hgbranch[str(cset)]
200 os.system('git checkout %s' % hgbranch[str(cset)])
204 if hgbranch[parent] == hgbranch[str(cset)]:
205 otherbranch = hgbranch[mparent]
207 otherbranch = hgbranch[parent]
208 print 'merging', otherbranch, 'into', hgbranch[str(cset)]
209 os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
211 # remove everything except .git and .hg directories
212 os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
214 # repopulate with checkouted files
215 os.system('hg update -C %d' % cset)
218 os.system('git ls-files -x .hg --others | git update-index --add --stdin')
219 # delete removed files
220 os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
223 os.system(getgitenv(user, date) + 'git commit --allow-empty -a -F %s' % filecomment)
224 os.unlink(filecomment)
227 if tag and tag != 'tip':
228 os.system(getgitenv(user, date) + 'git tag %s' % tag)
230 # delete branch if not used anymore...
231 if mparent and len(hgchildren[str(cset)]):
232 print "Deleting unused branch:", otherbranch
233 os.system('git branch -d %s' % otherbranch)
235 # retrieve and record the version
236 vvv = os.popen('git show --quiet --pretty=format:%H').read()
237 print 'record', cset, '->', vvv
238 hgvers[str(cset)] = vvv
240 if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
241 os.system('git repack -a -d')
243 # write the state for incrementals
246 print 'Writing state'
248 pickle.dump(hgvers, f)
250 # vim: et ts=8 sw=4 sts=4