hg-to-git: improve popen calls
This patch improves all of the popen calls in hg-to-git.py by specifying the template 'hg log' should use instead of calling 'hg log' and grepping for the desired data. Signed-off-by: Mark Drago <markdrago@gmail.com> Acked-by: Stelian Pop <stelian@popies.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Mark Drago committed
Jan 14, 2008 at 20:11 UTC
1bc7c13af9f936aa80893100120b542338a10bf4
1 file changed
+22
-24
contrib/hg-to-git/hg-to-git.py
+22
-24
@@ -1,6 +1,6 @@
1
#! /usr/bin/python
2
3
-""" hg-to-svn.py - A Mercurial to GIT converter
3
+""" hg-to-git.py - A Mercurial to GIT converter
4
5
Copyright (C)2007 Stelian Pop <stelian@popies.net>
6
@@ -27,6 +27,8 @@ import re
27
hgvers = {}
28
# List of children for each hg revision
29
hgchildren = {}
30
+# List of parents for each hg revision
31
+hgparents = {}
32
# Current branch for each hg revision
33
hgbranch = {}
34
# Number of new changesets converted from hg
@@ -99,17 +101,19 @@ if state:
101
else:
102
print 'State does not exist, first run'
103
102
-tip = os.popen('hg tip | head -1 | cut -f 2 -d :').read().strip()
104
+tip = os.popen('hg tip --template "{rev}"').read()
105
print 'tip is', tip
106
107
# Calculate the branches
108
print 'analysing the branches...'
109
hgchildren["0"] = ()
110
+hgparents["0"] = (None, None)
111
hgbranch["0"] = "master"
112
for cset in range(1, int(tip) + 1):
113
hgchildren[str(cset)] = ()
111
- prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()
112
- if len(prnts) > 0:
114
+ prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().split(' ')
115
+ prnts = map(lambda x: x[:x.find(':')], prnts)
116
+ if prnts[0] != '':
117
parent = prnts[0].strip()
118
else:
119
parent = str(cset - 1)
@@ -120,6 +124,8 @@ for cset in range(1, int(tip) + 1):
124
else:
125
mparent = None
126
127
+ hgparents[str(cset)] = (parent, mparent)
128
+
129
if mparent:
130
# For merge changesets, take either one, preferably the 'master' branch
131
if hgbranch[mparent] == 'master':
@@ -147,34 +153,27 @@ for cset in range(int(tip) + 1):
153
hgnewcsets += 1
154
155
# get info
150
- prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()
151
- if len(prnts) > 0:
152
- parent = prnts[0].strip()
153
- else:
154
- parent = str(cset - 1)
155
- if len(prnts) > 1:
156
- mparent = prnts[1].strip()
157
- else:
158
- mparent = None
159
-
156
+ log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
157
+ tag = log_data[0].strip()
158
+ date = log_data[1].strip()
159
+ user = log_data[2].strip()
160
+ parent = hgparents[str(cset)][0]
161
+ mparent = hgparents[str(cset)][1]
162
+
163
+ #get comment
164
(fdcomment, filecomment) = tempfile.mkstemp()
161
- csetcomment = 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()
165
+ csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
166
os.write(fdcomment, csetcomment)
167
os.close(fdcomment)
168
165
- date = os.popen('hg log -r %d | grep ^date: | cut -f 2- -d :' % cset).read().strip()
166
-
167
- tag = os.popen('hg log -r %d | grep ^tag: | cut -f 2- -d :' % cset).read().strip()
168
-
169
- user = os.popen('hg log -r %d | grep ^user: | cut -f 2- -d :' % cset).read().strip()
170
-
169
print '-----------------------------------------'
170
print 'cset:', cset
171
print 'branch:', hgbranch[str(cset)]
172
print 'user:', user
173
print 'date:', date
174
print 'comment:', csetcomment
177
- print 'parent:', parent
175
+ if parent:
176
+ print 'parent:', parent
177
if mparent:
178
print 'mparent:', mparent
179
if tag:
@@ -224,8 +223,7 @@ for cset in range(int(tip) + 1):
223
os.system('git-branch -d %s' % otherbranch)
224
225
# retrieve and record the version
227
- vvv = os.popen('git-show | head -1').read()
228
- vvv = vvv[vvv.index(' ') + 1 : ].strip()
226
+ vvv = os.popen('git-show --quiet --pretty=format:%H').read()
227
print 'record', cset, '->', vvv
228
hgvers[str(cset)] = vvv
229