{"thread":{"id":"11624","subject":"[PATCH] hg-to-git: improve popen calls","startedAt":"2008-01-15T01:11:19Z","lastAt":"2008-01-15T13:11:36Z","messageCount":6,"participants":["Mark Drago","Stelian Pop","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"65354","messageId":"478C0837.7090804@gmail.com","threadId":"11624","inReplyTo":null,"subject":"[PATCH] hg-to-git: improve popen calls","fromName":"Mark Drago","fromEmail":"markdrago@gmail.com","sentAt":"2008-01-15T01:11:19Z","receivedAt":"2008-01-15T01:11:19Z","isPatch":true,"sender":{"key":"markdrago@gmail.com","avatar":"https://gravatar.com/avatar/44978f3f99c6ac0e3f95f9a6be0846a07005d7ce1fd17a28d175f745b4401e6d?d=mp&s=160"},"body":"This patch improves all of the popen calls in hg-to-git.py by specifying the\ntemplate 'hg log' should use instead of calling 'hg log' and grepping for the\ndesired data.\n\nSigned-off-by: Mark Drago <markdrago@gmail.com>\n\n---\n\nHello,\n\nI wrote this patch back when the popen calls within hg-to-git came up on the\nlist and I'm just now getting around to sending it in.  Junio commented on one\nof the popen calls by saying the following:\n\n\"Isn't this one of the ugliest lines in the whole git.git project, I have\nto wonder?\" -- Junio, http://marc.info/?l=git&m=119673122106601&w=2\n\nSo, this patch removes that line and improves the rest of the popen calls\nas well.  It also reduces the overall number of popen calls by combining a\nbunch of them.\n\ndiff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py\nindex 9befb92..c35b158 100755\n--- a/contrib/hg-to-git/hg-to-git.py\n+++ b/contrib/hg-to-git/hg-to-git.py\n@@ -1,6 +1,6 @@\n #! /usr/bin/python\n \n-\"\"\" hg-to-svn.py - A Mercurial to GIT converter\n+\"\"\" hg-to-git.py - A Mercurial to GIT converter\n \n     Copyright (C)2007 Stelian Pop <stelian@popies.net>\n \n@@ -27,6 +27,8 @@ import re\n hgvers = {}\n # List of children for each hg revision\n hgchildren = {}\n+# List of parents for each hg revision\n+hgparents = {}\n # Current branch for each hg revision\n hgbranch = {}\n # Number of new changesets converted from hg\n@@ -99,17 +101,19 @@ if state:\n     else:\n         print 'State does not exist, first run'\n \n-tip = os.popen('hg tip | head -1 | cut -f 2 -d :').read().strip()\n+tip = os.popen('hg tip --template \"{rev}\"').read()\n print 'tip is', tip\n \n # Calculate the branches\n print 'analysing the branches...'\n hgchildren[\"0\"] = ()\n+hgparents[\"0\"] = (None, None)\n hgbranch[\"0\"] = \"master\"\n for cset in range(1, int(tip) + 1):\n     hgchildren[str(cset)] = ()\n-    prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()\n-    if len(prnts) > 0:\n+    prnts = os.popen('hg log -r %d --template \"{parents}\"' % cset).read().split(' ')\n+    prnts = map(lambda x: x[:x.find(':')], prnts)\n+    if prnts[0] != '':\n         parent = prnts[0].strip()\n     else:\n         parent = str(cset - 1)\n@@ -120,6 +124,8 @@ for cset in range(1, int(tip) + 1):\n     else:\n         mparent = None\n \n+    hgparents[str(cset)] = (parent, mparent)\n+\n     if mparent:\n         # For merge changesets, take either one, preferably the 'master' branch\n         if hgbranch[mparent] == 'master':\n@@ -147,34 +153,27 @@ for cset in range(int(tip) + 1):\n     hgnewcsets += 1\n \n     # get info\n-    prnts = os.popen('hg log -r %d | grep ^parent: | cut -f 2 -d :' % cset).readlines()\n-    if len(prnts) > 0:\n-        parent = prnts[0].strip()\n-    else:\n-        parent = str(cset - 1)\n-    if len(prnts) > 1:\n-        mparent = prnts[1].strip()\n-    else:\n-        mparent = None\n-\n+    log_data = os.popen('hg log -r %d --template \"{tags}\\n{date|date}\\n{author}\\n\"' % cset).readlines()\n+    tag = log_data[0].strip()\n+    date = log_data[1].strip()\n+    user = log_data[2].strip()\n+    parent = hgparents[str(cset)][0]\n+    mparent = hgparents[str(cset)][1]\n+\n+    #get comment\n     (fdcomment, filecomment) = tempfile.mkstemp()\n-    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()\n+    csetcomment = os.popen('hg log -r %d --template \"{desc}\"' % cset).read().strip()\n     os.write(fdcomment, csetcomment)\n     os.close(fdcomment)\n \n-    date = os.popen('hg log -r %d | grep ^date: | cut -f 2- -d :' % cset).read().strip()\n-\n-    tag = os.popen('hg log -r %d | grep ^tag: | cut -f 2- -d :' % cset).read().strip()\n-\n-    user = os.popen('hg log -r %d | grep ^user: | cut -f 2- -d :' % cset).read().strip()\n-\n     print '-----------------------------------------'\n     print 'cset:', cset\n     print 'branch:', hgbranch[str(cset)]\n     print 'user:', user\n     print 'date:', date\n     print 'comment:', csetcomment\n-    print 'parent:', parent\n+    if parent:\n+\tprint 'parent:', parent\n     if mparent:\n         print 'mparent:', mparent\n     if tag:\n@@ -224,8 +223,7 @@ for cset in range(int(tip) + 1):\n         os.system('git-branch -d %s' % otherbranch)\n \n     # retrieve and record the version\n-    vvv = os.popen('git-show | head -1').read()\n-    vvv = vvv[vvv.index(' ') + 1 : ].strip()\n+    vvv = os.popen('git-show --quiet --pretty=format:%H').read()\n     print 'record', cset, '->', vvv\n     hgvers[str(cset)] = vvv\n \n"},{"id":"65366","messageId":"1200390734.10025.28.camel@galileo","threadId":"11624","inReplyTo":"478C0837.7090804@gmail.com","subject":"Re: [PATCH] hg-to-git: improve popen calls","fromName":"Stelian Pop","fromEmail":"stelian@popies.net","sentAt":"2008-01-15T09:52:14Z","receivedAt":"2008-01-15T09:52:14Z","isPatch":true,"sender":{"key":"stelian@popies.net","avatar":"https://gravatar.com/avatar/c99e49c3b3d851deb85e4557b368712c890a09985211f183bd151f8ab6357c03?d=mp&s=160"},"body":"\nLe lundi 14 janvier 2008 à 20:11 -0500, Mark Drago a écrit :\n> This patch improves all of the popen calls in hg-to-git.py by specifying the\n> template 'hg log' should use instead of calling 'hg log' and grepping for the\n> desired data.\n> \n> Signed-off-by: Mark Drago <markdrago@gmail.com>\n\nNice !\n\nAcked-by: Stelian Pop <stelian@popies.net>\n\nThanks.\n\nStelian.\n\n-- \nStelian Pop <stelian@popies.net>\n"},{"id":"65368","messageId":"7vve5vfl8g.fsf@gitster.siamese.dyndns.org","threadId":"11624","inReplyTo":"1200390734.10025.28.camel@galileo","subject":"Re: [PATCH] hg-to-git: improve popen calls","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-01-15T10:15:27Z","receivedAt":"2008-01-15T10:15:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks both.  Will apply (but I am about to head to bed for the\nnight).\n"},{"id":"65369","messageId":"7vr6gjfl4z.fsf@gitster.siamese.dyndns.org","threadId":"11624","inReplyTo":"478C0837.7090804@gmail.com","subject":"Re: [PATCH] hg-to-git: improve popen calls","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-01-15T10:17:32Z","receivedAt":"2008-01-15T10:17:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Mark Drago <markdrago@gmail.com> writes:\n\n> +    log_data = os.popen('hg log -r %d --template \"{tags}\\n{date|date}\\n{author}\\n\"' % cset).readlines()\n\nI've already seen Stelian's Ack, but just for my sanity, let me\nmake sure one thing.  The above {date|date} is correct, not a\ntypo?\n"},{"id":"65379","messageId":"af675f7f0801150503p26fba583tfb5827717b67705f@mail.gmail.com","threadId":"11624","inReplyTo":"7vr6gjfl4z.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] hg-to-git: improve popen calls","fromName":"Mark Drago","fromEmail":"markdrago@gmail.com","sentAt":"2008-01-15T13:03:37Z","receivedAt":"2008-01-15T13:03:37Z","isPatch":true,"sender":{"key":"markdrago@gmail.com","avatar":"https://gravatar.com/avatar/44978f3f99c6ac0e3f95f9a6be0846a07005d7ce1fd17a28d175f745b4401e6d?d=mp&s=160"},"body":"On Jan 15, 2008 5:17 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Mark Drago <markdrago@gmail.com> writes:\n>\n> > +    log_data = os.popen('hg log -r %d --template \"{tags}\\n{date|date}\\n{author}\\n\"' % cset).readlines()\n>\n> I've already seen Stelian's Ack, but just for my sanity, let me\n> make sure one thing.  The above {date|date} is correct, not a\n> typo?\n\nYeah.  Everything after the pipe character is a filter and there is a\ntemplate keyword and a filter that are both called 'date'.\n\nmdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date}\\n'\n1197080765.018000\nmdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date|date}\\n'\nFri Dec 07 21:26:05 2007 -0500\n\nMark.\n"},{"id":"65381","messageId":"alpine.DEB.1.00.0801151311130.5289@eeepc-johanness","threadId":"11624","inReplyTo":"af675f7f0801150503p26fba583tfb5827717b67705f@mail.gmail.com","subject":"Re: [PATCH] hg-to-git: improve popen calls","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-01-15T13:11:36Z","receivedAt":"2008-01-15T13:11:36Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Tue, 15 Jan 2008, Mark Drago wrote:\n\n> On Jan 15, 2008 5:17 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> > Mark Drago <markdrago@gmail.com> writes:\n> >\n> > > +    log_data = os.popen('hg log -r %d --template \"{tags}\\n{date|date}\\n{author}\\n\"' % cset).readlines()\n> >\n> > I've already seen Stelian's Ack, but just for my sanity, let me\n> > make sure one thing.  The above {date|date} is correct, not a\n> > typo?\n> \n> Yeah.  Everything after the pipe character is a filter and there is a\n> template keyword and a filter that are both called 'date'.\n> \n> mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date}\\n'\n> 1197080765.018000\n> mdrago@laptop:~/Code/trunk$ hg log -r 4 --template '{date|date}\\n'\n> Fri Dec 07 21:26:05 2007 -0500\n\nHeh ;-)  And people call _git_ confusing...\n\nCiao,\nDscho\n"}]}