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>
maint
Mark Drago 2008-01-14 20:11:19 -05:00 committed by Junio C Hamano
parent 3e34526960
commit 1bc7c13af9
1 changed files with 21 additions and 23 deletions

View File

@ -1,6 +1,6 @@
#! /usr/bin/python #! /usr/bin/python


""" hg-to-svn.py - A Mercurial to GIT converter """ hg-to-git.py - A Mercurial to GIT converter


Copyright (C)2007 Stelian Pop <stelian@popies.net> Copyright (C)2007 Stelian Pop <stelian@popies.net>


@ -27,6 +27,8 @@ import re
hgvers = {} hgvers = {}
# List of children for each hg revision # List of children for each hg revision
hgchildren = {} hgchildren = {}
# List of parents for each hg revision
hgparents = {}
# Current branch for each hg revision # Current branch for each hg revision
hgbranch = {} hgbranch = {}
# Number of new changesets converted from hg # Number of new changesets converted from hg
@ -99,17 +101,19 @@ if state:
else: else:
print 'State does not exist, first run' print 'State does not exist, first run'


tip = os.popen('hg tip | head -1 | cut -f 2 -d :').read().strip() tip = os.popen('hg tip --template "{rev}"').read()
print 'tip is', tip print 'tip is', tip


# Calculate the branches # Calculate the branches
print 'analysing the branches...' print 'analysing the branches...'
hgchildren["0"] = () hgchildren["0"] = ()
hgparents["0"] = (None, None)
hgbranch["0"] = "master" hgbranch["0"] = "master"
for cset in range(1, int(tip) + 1): for cset in range(1, int(tip) + 1):
hgchildren[str(cset)] = () hgchildren[str(cset)] = ()
prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines() prnts = os.popen('hg log -r %d --template "{parents}"' % cset).read().split(' ')
if len(prnts) > 0: prnts = map(lambda x: x[:x.find(':')], prnts)
if prnts[0] != '':
parent = prnts[0].strip() parent = prnts[0].strip()
else: else:
parent = str(cset - 1) parent = str(cset - 1)
@ -120,6 +124,8 @@ for cset in range(1, int(tip) + 1):
else: else:
mparent = None mparent = None


hgparents[str(cset)] = (parent, mparent)

if mparent: if mparent:
# For merge changesets, take either one, preferably the 'master' branch # For merge changesets, take either one, preferably the 'master' branch
if hgbranch[mparent] == 'master': if hgbranch[mparent] == 'master':
@ -147,33 +153,26 @@ for cset in range(int(tip) + 1):
hgnewcsets += 1 hgnewcsets += 1


# get info # get info
prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines() log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
if len(prnts) > 0: tag = log_data[0].strip()
parent = prnts[0].strip() date = log_data[1].strip()
else: user = log_data[2].strip()
parent = str(cset - 1) parent = hgparents[str(cset)][0]
if len(prnts) > 1: mparent = hgparents[str(cset)][1]
mparent = prnts[1].strip()
else:
mparent = None


#get comment
(fdcomment, filecomment) = tempfile.mkstemp() (fdcomment, filecomment) = tempfile.mkstemp()
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() csetcomment = os.popen('hg log -r %d --template "{desc}"' % cset).read().strip()
os.write(fdcomment, csetcomment) os.write(fdcomment, csetcomment)
os.close(fdcomment) os.close(fdcomment)


date = os.popen('hg log -r %d | grep ^date: | cut -f 2- -d :' % cset).read().strip()

tag = os.popen('hg log -r %d | grep ^tag: | cut -f 2- -d :' % cset).read().strip()

user = os.popen('hg log -r %d | grep ^user: | cut -f 2- -d :' % cset).read().strip()

print '-----------------------------------------' print '-----------------------------------------'
print 'cset:', cset print 'cset:', cset
print 'branch:', hgbranch[str(cset)] print 'branch:', hgbranch[str(cset)]
print 'user:', user print 'user:', user
print 'date:', date print 'date:', date
print 'comment:', csetcomment print 'comment:', csetcomment
if parent:
print 'parent:', parent print 'parent:', parent
if mparent: if mparent:
print 'mparent:', mparent print 'mparent:', mparent
@ -224,8 +223,7 @@ for cset in range(int(tip) + 1):
os.system('git-branch -d %s' % otherbranch) os.system('git-branch -d %s' % otherbranch)


# retrieve and record the version # retrieve and record the version
vvv = os.popen('git-show | head -1').read() vvv = os.popen('git-show --quiet --pretty=format:%H').read()
vvv = vvv[vvv.index(' ') + 1 : ].strip()
print 'record', cset, '->', vvv print 'record', cset, '->', vvv
hgvers[str(cset)] = vvv hgvers[str(cset)] = vvv