Skip to content

Commit

Permalink
hg-to-git: improve popen calls
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Mark Drago authored and Junio C Hamano committed Jan 16, 2008
1 parent 3e34526 commit 1bc7c13
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions contrib/hg-to-git/hg-to-git.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /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>
Expand All @@ -27,6 +27,8 @@
hgvers = {}
# List of children for each hg revision
hgchildren = {}
# List of parents for each hg revision
hgparents = {}
# Current branch for each hg revision
hgbranch = {}
# Number of new changesets converted from hg
Expand Down Expand Up @@ -99,17 +101,19 @@ def getgitenv(user, date):
else:
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

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

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

if mparent:
# For merge changesets, take either one, preferably the 'master' branch
if hgbranch[mparent] == 'master':
Expand Down Expand Up @@ -147,34 +153,27 @@ def getgitenv(user, date):
hgnewcsets += 1

# get info
prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()
if len(prnts) > 0:
parent = prnts[0].strip()
else:
parent = str(cset - 1)
if len(prnts) > 1:
mparent = prnts[1].strip()
else:
mparent = None

log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
tag = log_data[0].strip()
date = log_data[1].strip()
user = log_data[2].strip()
parent = hgparents[str(cset)][0]
mparent = hgparents[str(cset)][1]

#get comment
(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.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 'cset:', cset
print 'branch:', hgbranch[str(cset)]
print 'user:', user
print 'date:', date
print 'comment:', csetcomment
print 'parent:', parent
if parent:
print 'parent:', parent
if mparent:
print 'mparent:', mparent
if tag:
Expand Down Expand Up @@ -224,8 +223,7 @@ def getgitenv(user, date):
os.system('git-branch -d %s' % otherbranch)

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

Expand Down

0 comments on commit 1bc7c13

Please sign in to comment.