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, see <http://www.gnu.org/licenses/>.
21 import os, os.path, sys
22 import tempfile, pickle, getopt
25 if sys.hexversion < 0x02030000:
26 # The behavior of the pickle module changed significantly in 2.3
27 sys.stderr.write("hg-to-git.py: requires Python 2.3 or later.\n")
30 # Maps hg version -> git version
32 # List of children for each hg revision
34 # List of parents for each hg revision
36 # Current branch for each hg revision
38 # Number of new changesets converted from hg
41 #------------------------------------------------------------------------------
49 -s, --gitstate=FILE: name of the state to be saved/read
51 -n, --nrepack=INT: number of changesets that will trigger
52 a repack (default=0, -1 to deactivate)
53 -v, --verbose: be verbose
56 hgprj: name of the HG project to import (directory)
59 #------------------------------------------------------------------------------
61 def getgitenv(user, date):
63 elems = re.compile('(.*?)\s+<(.*)>').match(user)
65 env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
66 env += 'export GIT_COMMITTER_NAME="%s" ;' % elems.group(1)
67 env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
68 env += 'export GIT_COMMITTER_EMAIL="%s" ;' % elems.group(2)
70 env += 'export GIT_AUTHOR_NAME="%s" ;' % user
71 env += 'export GIT_COMMITTER_NAME="%s" ;' % user
72 env += 'export GIT_AUTHOR_EMAIL= ;'
73 env += 'export GIT_COMMITTER_EMAIL= ;'
75 env += 'export GIT_AUTHOR_DATE="%s" ;' % date
76 env += 'export GIT_COMMITTER_DATE="%s" ;' % date
79 #------------------------------------------------------------------------------
86 opts, args = getopt.getopt(sys.argv[1:], 's:t:n:v', ['gitstate=', 'tempdir=', 'nrepack=', 'verbose'])
88 if o in ('-s', '--gitstate'):
90 state = os.path.abspath(state)
91 if o in ('-n', '--nrepack'):
93 if o in ('-v', '--verbose'):
96 raise Exception('params')
105 if os.path.exists(state):
107 print('State does exist, reading')
109 hgvers = pickle.load(f)
111 print('State does not exist, first run')
113 sock = os.popen('hg tip --template "{rev}"')
120 # Calculate the branches
122 print('analysing the branches...')
124 hgparents["0"] = (None, None)
125 hgbranch["0"] = "master"
126 for cset in range(1, int(tip) + 1):
127 hgchildren[str(cset)] = ()
128 prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().strip().split(' ')
129 prnts = map(lambda x: x[:x.find(':')], prnts)
131 parent = prnts[0].strip()
133 parent = str(cset - 1)
134 hgchildren[parent] += ( str(cset), )
136 mparent = prnts[1].strip()
137 hgchildren[mparent] += ( str(cset), )
141 hgparents[str(cset)] = (parent, mparent)
144 # For merge changesets, take either one, preferably the 'master' branch
145 if hgbranch[mparent] == 'master':
146 hgbranch[str(cset)] = 'master'
148 hgbranch[str(cset)] = hgbranch[parent]
151 # For first children, take the parent branch, for the others create a new branch
152 if hgchildren[parent][0] == str(cset):
153 hgbranch[str(cset)] = hgbranch[parent]
155 hgbranch[str(cset)] = "branch-" + str(cset)
157 if "0" not in hgvers:
158 print('creating repository')
159 os.system('git init')
161 # loop through every hg changeset
162 for cset in range(int(tip) + 1):
164 # incremental, already seen
165 if str(cset) in hgvers:
170 log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
171 tag = log_data[0].strip()
172 date = log_data[1].strip()
173 user = log_data[2].strip()
174 parent = hgparents[str(cset)][0]
175 mparent = hgparents[str(cset)][1]
178 (fdcomment, filecomment) = tempfile.mkstemp()
179 csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
180 os.write(fdcomment, csetcomment)
183 print('-----------------------------------------')
185 print('branch:', hgbranch[str(cset)])
188 print('comment:', csetcomment)
190 print('parent:', parent)
192 print('mparent:', mparent)
195 print('-----------------------------------------')
197 # checkout the parent if necessary
199 if hgbranch[str(cset)] == "branch-" + str(cset):
200 print('creating new branch', hgbranch[str(cset)])
201 os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
203 print('checking out branch', hgbranch[str(cset)])
204 os.system('git checkout %s' % hgbranch[str(cset)])
208 if hgbranch[parent] == hgbranch[str(cset)]:
209 otherbranch = hgbranch[mparent]
211 otherbranch = hgbranch[parent]
212 print('merging', otherbranch, 'into', hgbranch[str(cset)])
213 os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
215 # remove everything except .git and .hg directories
216 os.system('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
218 # repopulate with checkouted files
219 os.system('hg update -C %d' % cset)
222 os.system('git ls-files -x .hg --others | git update-index --add --stdin')
223 # delete removed files
224 os.system('git ls-files -x .hg --deleted | git update-index --remove --stdin')
227 os.system(getgitenv(user, date) + 'git commit --allow-empty --allow-empty-message -a -F %s' % filecomment)
228 os.unlink(filecomment)
231 if tag and tag != 'tip':
232 os.system(getgitenv(user, date) + 'git tag %s' % tag)
234 # delete branch if not used anymore...
235 if mparent and len(hgchildren[str(cset)]):
236 print("Deleting unused branch:", otherbranch)
237 os.system('git branch -d %s' % otherbranch)
239 # retrieve and record the version
240 vvv = os.popen('git show --quiet --pretty=format:%H').read()
241 print('record', cset, '->', vvv)
242 hgvers[str(cset)] = vvv
244 if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
245 os.system('git repack -a -d')
247 # write the state for incrementals
250 print('Writing state')
252 pickle.dump(hgvers, f)
254 # vim: et ts=8 sw=4 sts=4