[Contrib] Basic Darcs to Mercurial conversion script

TK Soh teekaysoh at gmail.com
Mon May 29 16:58:30 CDT 2006


On 5/29/06, Sébastien Pierre <sebastien at xprima.com> wrote:
> > * It outputs a "<filename> already tracked!" message, when updating
> > the hg repo, for each file that was already in the repo. I think it's
> > harmless.
>
> Yes, this is harmless. This was simpler for me as I would otherwise have
> to give the list of all files minus the list of already tracked files,
> and wanted to keep the script simple.

Actual that's a sign of a bug. I have tried to fix it in the attached
patch, along with a few others I found in the script:

1. removed files were missed
2. hidden files in top directory were not added/removed correctly
3. suppressed spurious warning on adding new files.
4. hg changeset now checked in with original author and date
5. __file__ not supported by pdb.py

That's quite a bit to put in one single patch, but what the heck...
-------------- next part --------------
# HG changeset patch
# User TK Soh <teekaysoh at yahoo.com>
# Date 1148892391 -28800
# Node ID 61909dfb316da52e0069e8cf92cfc81cffc21523
# Parent  075d2ddc4639c263d94f90027ed9baeda5310655
various fixes to darcs conversion script

1. removed files were missed
2. hidden files in top directory were not added/removed correctly
3. suppressed spurious warning on adding new files.
4. hg changeset now checked in with original author and date
5. __file__ not supported by pdb.py

diff -r 075d2ddc4639 -r 61909dfb316d contrib/darcs2hg.py
--- a/contrib/darcs2hg.py	Sat May 27 20:45:22 2006 -0700
+++ b/contrib/darcs2hg.py	Mon May 29 16:46:31 2006 +0800
@@ -13,7 +13,9 @@
 # -----------------------------------------------------------------------------
 
 import os, sys
+import tempfile
 import xml.dom.minidom as xml_dom
+from time import strptime, mktime
 
 DARCS_REPO = None
 HG_REPO    = None
@@ -25,7 +27,7 @@ USAGE = """\
     HGREPO must not exist, as it will be created and filled up (this will avoid
     overwriting valuable data.
 
-""" % (os.path.basename(__file__))
+""" % (os.path.basename(sys.argv[0]))
 
 # ------------------------------------------------------------------------------
 #
@@ -70,8 +72,9 @@ def darcs_changes(darcsRepo):
 		else: name = name[0].childNodes[0].data
 		if not comm: comm = ""
 		else: comm = comm[0].childNodes[0].data
-		res.append([name, comm])
-	return res
+		author = patch_node.getAttribute("author")
+		date = patch_node.getAttribute("date")
+		yield author, date, name, comm
 
 def darcs_pull(hg_repo, darcs_repo, change):
 	cmd("darcs pull '%s' --all --patches='%s'" % (darcs_repo, change), hg_repo)
@@ -82,11 +85,13 @@ def darcs_pull(hg_repo, darcs_repo, chan
 #
 # ------------------------------------------------------------------------------
 
-def hg_commit( hg_repo, text ):
-	writefile("/tmp/msg", text)
-	cmd("hg add -X _darcs *", hg_repo)
-	cmd("hg commit -l /tmp/msg", hg_repo)
-	os.unlink("/tmp/msg")
+def hg_commit( hg_repo, text, author, date ):
+	fd, tmpfile = tempfile.mkstemp(prefix="darcs2hg_")
+	writefile(tmpfile, text)
+	cmd("hg add -X _darcs", hg_repo)
+	cmd("hg remove -X _darcs --after", hg_repo)
+	cmd("hg commit -l %s -u '%s' -d '%s 0'"  % (tmpfile, author, date), hg_repo)
+	os.unlink(tmpfile)
 
 # ------------------------------------------------------------------------------
 #
@@ -115,10 +120,11 @@ if __name__ == "__main__":
 	cmd("hg init '%s'" % (hg_repo))
 	cmd("darcs initialize", hg_repo)
 	# Get the changes from the Darcs repository
-	for summary, description in darcs_changes(darcs_repo):
+	for author, date, summary, description in darcs_changes(darcs_repo):
 		text = summary + "\n" + description
 		darcs_pull(hg_repo, darcs_repo, summary)
-		hg_commit(hg_repo, text)
+		epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
+		hg_commit(hg_repo, text, author, epoch)
 
 # EOF
 


More information about the Mercurial mailing list