* [PATCH] hg-to-git: improve popen calls
@ 2008-01-15 1:11 Mark Drago
2008-01-15 9:52 ` Stelian Pop
2008-01-15 10:17 ` Junio C Hamano
0 siblings, 2 replies; 6+ messages in thread
From: Mark Drago @ 2008-01-15 1:11 UTC (permalink / raw)
To: gitster; +Cc: git, brian.ewins, stelian
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>
---
Hello,
I wrote this patch back when the popen calls within hg-to-git came up on the
list and I'm just now getting around to sending it in. Junio commented on one
of the popen calls by saying the following:
"Isn't this one of the ugliest lines in the whole git.git project, I have
to wonder?" -- Junio, http://marc.info/?l=git&m=119673122106601&w=2
So, this patch removes that line and improves the rest of the popen calls
as well. It also reduces the overall number of popen calls by combining a
bunch of them.
diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
index 9befb92..c35b158 100755
--- a/contrib/hg-to-git/hg-to-git.py
+++ b/contrib/hg-to-git/hg-to-git.py
@@ -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>
@@ -27,6 +27,8 @@ import re
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
@@ -99,17 +101,19 @@ if state:
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)
@@ -120,6 +124,8 @@ for cset in range(1, int(tip) + 1):
else:
mparent = None
+ hgparents[str(cset)] = (parent, mparent)
+
if mparent:
# For merge changesets, take either one, preferably the 'master' branch
if hgbranch[mparent] == 'master':
@@ -147,34 +153,27 @@ for cset in range(int(tip) + 1):
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:
@@ -224,8 +223,7 @@ for cset in range(int(tip) + 1):
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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] hg-to-git: improve popen calls
2008-01-15 1:11 [PATCH] hg-to-git: improve popen calls Mark Drago
@ 2008-01-15 9:52 ` Stelian Pop
2008-01-15 10:15 ` Junio C Hamano
2008-01-15 10:17 ` Junio C Hamano
1 sibling, 1 reply; 6+ messages in thread
From: Stelian Pop @ 2008-01-15 9:52 UTC (permalink / raw)
To: Mark Drago; +Cc: gitster, git, brian.ewins
Le lundi 14 janvier 2008 à 20:11 -0500, Mark Drago a écrit :
> 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>
Nice !
Acked-by: Stelian Pop <stelian@popies.net>
Thanks.
Stelian.
--
Stelian Pop <stelian@popies.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] hg-to-git: improve popen calls
2008-01-15 9:52 ` Stelian Pop
@ 2008-01-15 10:15 ` Junio C Hamano
0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2008-01-15 10:15 UTC (permalink / raw)
To: Stelian Pop; +Cc: Mark Drago, git, brian.ewins
Thanks both. Will apply (but I am about to head to bed for the
night).
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] hg-to-git: improve popen calls
2008-01-15 1:11 [PATCH] hg-to-git: improve popen calls Mark Drago
2008-01-15 9:52 ` Stelian Pop
@ 2008-01-15 10:17 ` Junio C Hamano
2008-01-15 13:03 ` Mark Drago
1 sibling, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2008-01-15 10:17 UTC (permalink / raw)
To: Mark Drago; +Cc: git, brian.ewins, stelian
Mark Drago <markdrago@gmail.com> writes:
> + log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
I've already seen Stelian's Ack, but just for my sanity, let me
make sure one thing. The above {date|date} is correct, not a
typo?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] hg-to-git: improve popen calls
2008-01-15 10:17 ` Junio C Hamano
@ 2008-01-15 13:03 ` Mark Drago
2008-01-15 13:11 ` Johannes Schindelin
0 siblings, 1 reply; 6+ messages in thread
From: Mark Drago @ 2008-01-15 13:03 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git, brian.ewins, stelian
On Jan 15, 2008 5:17 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Mark Drago <markdrago@gmail.com> writes:
>
> > + log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
>
> I've already seen Stelian's Ack, but just for my sanity, let me
> make sure one thing. The above {date|date} is correct, not a
> typo?
Yeah. Everything after the pipe character is a filter and there is a
template keyword and a filter that are both called 'date'.
mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date}\n'
1197080765.018000
mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date|date}\n'
Fri Dec 07 21:26:05 2007 -0500
Mark.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] hg-to-git: improve popen calls
2008-01-15 13:03 ` Mark Drago
@ 2008-01-15 13:11 ` Johannes Schindelin
0 siblings, 0 replies; 6+ messages in thread
From: Johannes Schindelin @ 2008-01-15 13:11 UTC (permalink / raw)
To: Mark Drago; +Cc: Junio C Hamano, git, brian.ewins, stelian
Hi,
On Tue, 15 Jan 2008, Mark Drago wrote:
> On Jan 15, 2008 5:17 AM, Junio C Hamano <gitster@pobox.com> wrote:
> > Mark Drago <markdrago@gmail.com> writes:
> >
> > > + log_data = os.popen('hg log -r %d --template "{tags}\n{date|date}\n{author}\n"' % cset).readlines()
> >
> > I've already seen Stelian's Ack, but just for my sanity, let me
> > make sure one thing. The above {date|date} is correct, not a
> > typo?
>
> Yeah. Everything after the pipe character is a filter and there is a
> template keyword and a filter that are both called 'date'.
>
> mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date}\n'
> 1197080765.018000
> mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date|date}\n'
> Fri Dec 07 21:26:05 2007 -0500
Heh ;-) And people call _git_ confusing...
Ciao,
Dscho
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-01-15 13:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-15 1:11 [PATCH] hg-to-git: improve popen calls Mark Drago
2008-01-15 9:52 ` Stelian Pop
2008-01-15 10:15 ` Junio C Hamano
2008-01-15 10:17 ` Junio C Hamano
2008-01-15 13:03 ` Mark Drago
2008-01-15 13:11 ` Johannes Schindelin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).