annotate contrib/darcs2hg.py @ 5348:b62a59fa9d26

darcs2hg: Now understands files that were explicitly renamed in darcs.
author Terry Smith <terry@t11e.com>
date Tue, 11 Sep 2007 12:42:52 -0400
parents eb0b4a2d70a9
children ad8783fe20f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
1 #!/usr/bin/env python
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
2 # Encoding: iso-8859-1
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
3 # vim: tw=80 ts=4 sw=4 noet
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
4 # -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
5 # Project : Basic Darcs to Mercurial conversion script
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
6 # -----------------------------------------------------------------------------
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
7 # Authors : Sebastien Pierre <sebastien@xprima.com>
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
8 # TK Soh <teekaysoh@gmail.com>
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
9 # -----------------------------------------------------------------------------
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
10 # Creation : 24-May-2006
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
11 # Last mod : 05-Jun-2006
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
12 # -----------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
13
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
14 import os, sys
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
15 import tempfile
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
16 import xml.dom.minidom as xml_dom
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
17 from time import strptime, mktime
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
18
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
19 DARCS_REPO = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
20 HG_REPO = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
21
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
22 USAGE = """\
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
23 %s DARCSREPO HGREPO [SKIP]
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
24
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
25 Converts the given Darcs repository to a new Mercurial repository. The given
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
26 HGREPO must not exist, as it will be created and filled up (this will avoid
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
27 overwriting valuable data.
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
28
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
29 In case an error occurs within the process, you can resume the process by
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
30 giving the last successfuly applied change number.
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
31 """ % (os.path.basename(sys.argv[0]))
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
32
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
33 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
34 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
35 # Utilities
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
36 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
37 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
38
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
39 def cmd(text, path=None, silent=False):
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
40 """Executes a command, in the given directory (if any), and returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
41 command result as a string."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
42 cwd = None
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
43 if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
44 path = os.path.abspath(path)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
45 cwd = os.getcwd()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
46 os.chdir(path)
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
47 if not silent: print "> ", text
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
48 res = os.popen(text).read()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
49 if path:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
50 os.chdir(cwd)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
51 return res
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
52
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
53 def writefile(path, data):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
54 """Writes the given data into the given file."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
55 f = file(path, "w") ; f.write(data) ; f.close()
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
56
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
57 def error( *args ):
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
58 sys.stderr.write("ERROR: ")
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
59 for a in args: sys.stderr.write(str(a))
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
60 sys.stderr.write("\n")
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
61 sys.stderr.write("You can make manual fixes if necessary and then resume by"
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
62 " giving the last changeset number")
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
63 sys.exit(-1)
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
64
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
65 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
66 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
67 # Darcs interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
68 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
69 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
70
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
71 def darcs_changes(darcsRepo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
72 """Gets the changes list from the given darcs repository. This returns the
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
73 chronological list of changes as (change name, change summary)."""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
74 changes = cmd("darcs changes --reverse --xml-output", darcsRepo)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
75 doc = xml_dom.parseString(changes)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
76 for patch_node in doc.childNodes[0].childNodes:
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2749
diff changeset
77 name = filter(lambda n: n.nodeName == "name", patch_node.childNodes)
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2749
diff changeset
78 comm = filter(lambda n: n.nodeName == "comment", patch_node.childNodes)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
79 if not name:continue
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
80 else: name = name[0].childNodes[0].data
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
81 if not comm: comm = ""
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
82 else: comm = comm[0].childNodes[0].data
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
83 author = patch_node.getAttribute("author")
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
84 date = patch_node.getAttribute("date")
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
85 chash = os.path.splitext(patch_node.getAttribute("hash"))[0]
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
86 yield author, date, name, chash, comm
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
87
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
88 def darcs_tip(darcs_repo):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
89 changes = cmd("darcs changes",darcs_repo,silent=True)
3673
eb0b4a2d70a9 white space and line break cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2749
diff changeset
90 changes = filter(lambda l: l.strip().startswith("* "), changes.split("\n"))
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
91 return len(changes)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
92
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
93 def darcs_pull(hg_repo, darcs_repo, chash):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
94 old_tip = darcs_tip(darcs_repo)
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
95 res = cmd("darcs pull \"%s\" --all --match=\"hash %s\"" % (darcs_repo, chash), hg_repo)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
96 print res
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
97 new_tip = darcs_tip(darcs_repo)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
98 if not new_tip != old_tip + 1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
99 error("Darcs pull did not work as expected: " + res)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
100
5348
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
101 def darcs_changes_summary(darcs_repo, chash):
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
102 """Gets the changes from the darcs summary. This returns the chronological
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
103 list of changes as (change_type, args). Eg. ('add_file', 'foo.txt') or
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
104 ('move', ['foo.txt','bar.txt'])."""
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
105 change = cmd("darcs changes --summary --xml-output --match=\"hash %s\"" % (chash), darcs_repo)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
106 doc = xml_dom.parseString(change)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
107 for patch_node in doc.childNodes[0].childNodes:
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
108 summary_nodes = filter(lambda n: n.nodeName == "summary" and n.nodeType == n.ELEMENT_NODE, patch_node.childNodes)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
109 for summary_node in summary_nodes:
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
110 change_nodes = filter(lambda n: n.nodeType == n.ELEMENT_NODE, summary_node.childNodes)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
111 for change_node in change_nodes:
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
112 change = change_node.nodeName
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
113 if change == 'modify_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
114 yield change, change_node.childNodes[0].data.strip()
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
115 elif change == 'add_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
116 yield change, change_node.childNodes[0].data.strip()
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
117 elif change == 'remove_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
118 yield change, change_node.childNodes[0].data.strip()
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
119 elif change == 'add_directory':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
120 yield change, change_node.childNodes[0].data.strip()
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
121 elif change == 'remove_directory':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
122 yield change, change_node.childNodes[0].data.strip()
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
123 elif change == 'move':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
124 yield change, (change_node.getAttribute('from'), change_node.getAttribute('to'))
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
125 else:
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
126 error('Problem parsing summary xml: Unexpected element: ' + change_node.toxml())
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
127
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
128 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
129 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
130 # Mercurial interface
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
131 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
132 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
133
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
134 def hg_commit( hg_repo, text, author, date ):
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
135 fd, tmpfile = tempfile.mkstemp(prefix="darcs2hg_")
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
136 writefile(tmpfile, text)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
137 old_tip = hg_tip(hg_repo)
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
138 cmd("hg add -X _darcs", hg_repo)
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
139 cmd("hg remove -X _darcs --after", hg_repo)
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
140 res = cmd("hg commit -l %s -u \"%s\" -d \"%s 0\"" % (tmpfile, author, date), hg_repo)
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
141 os.close(fd)
2352
61909dfb316d various fixes to darcs conversion script
TK Soh <teekaysoh@yahoo.com>
parents: 2349
diff changeset
142 os.unlink(tmpfile)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
143 new_tip = hg_tip(hg_repo)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
144 if not new_tip == old_tip + 1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
145 # Sometimes we may have empty commits, we simply skip them
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
146 if res.strip().lower().find("nothing changed") != -1:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
147 pass
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
148 else:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
149 error("Mercurial commit did not work as expected: " + res)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
150
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
151 def hg_tip( hg_repo ):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
152 """Returns the latest local revision number in the given repository."""
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
153 tip = cmd("hg tip", hg_repo, silent=True)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
154 tip = tip.split("\n")[0].split(":")[1].strip()
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
155 return int(tip)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
156
5348
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
157 def hg_rename( hg_repo, from_file, to_file ):
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
158 cmd("hg rename --after \"%s\" \"%s\"" % (from_file, to_file), hg_repo);
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
159
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
160 def hg_handle_change( hg_repo, change, arg ):
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
161 """Processes a change event as output by darcs_changes_summary. These
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
162 consist of file move/rename/add/delete commands."""
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
163 if change == 'modify_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
164 pass
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
165 elif change == 'add_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
166 pass
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
167 elif change =='remove_file':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
168 pass
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
169 elif change == 'add_directory':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
170 pass
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
171 elif change == 'remove_directory':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
172 pass
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
173 elif change == 'move':
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
174 hg_rename(hg_repo, arg[0], arg[1])
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
175 else:
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
176 error('Unknown change type ' + change + ': ' + arg)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
177
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
178 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
179 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
180 # Main
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
181 #
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
182 # ------------------------------------------------------------------------------
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
183
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
184 if __name__ == "__main__":
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
185 args = sys.argv[1:]
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
186 # We parse the arguments
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
187 if len(args) == 2:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
188 darcs_repo = os.path.abspath(args[0])
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
189 hg_repo = os.path.abspath(args[1])
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
190 skip = None
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
191 elif len(args) == 3:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
192 darcs_repo = os.path.abspath(args[0])
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
193 hg_repo = os.path.abspath(args[1])
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
194 skip = int(args[2])
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
195 else:
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
196 print USAGE
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
197 sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
198 # Initializes the target repo
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
199 if not os.path.isdir(darcs_repo + "/_darcs"):
2586
bb63d29ce03d darcs2hg: improved logging
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
200 print "No darcs directory found at: " + darcs_repo
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
201 sys.exit(-1)
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
202 if not os.path.isdir(hg_repo):
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
203 os.mkdir(hg_repo)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
204 elif skip == None:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
205 print "Given HG repository must not exist when no SKIP is specified."
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
206 sys.exit(-1)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
207 if skip == None:
2749
d13e4ffaa79d [darcs2hg] Windows compatibilty patct
Sébastien Pierre <sebastien@xprima.com>
parents: 2588
diff changeset
208 cmd("hg init \"%s\"" % (hg_repo))
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
209 cmd("darcs initialize", hg_repo)
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
210 # Get the changes from the Darcs repository
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
211 change_number = 0
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
212 for author, date, summary, chash, description in darcs_changes(darcs_repo):
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
213 print "== changeset", change_number,
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
214 if skip != None and change_number <= skip:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
215 print "(skipping)"
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
216 else:
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
217 text = summary + "\n" + description
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
218 # The commit hash has a date like 20021020201112
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
219 # --------------------------------YYYYMMDDHHMMSS
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
220 date = chash.split("-")[0]
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
221 epoch = int(mktime(strptime(date, '%Y%m%d%H%M%S')))
5348
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
222 darcs_pull(hg_repo, darcs_repo, chash)
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
223 for change, arg in darcs_changes_summary(darcs_repo, chash):
b62a59fa9d26 darcs2hg: Now understands files that were explicitly renamed in darcs.
Terry Smith <terry@t11e.com>
parents: 3673
diff changeset
224 hg_handle_change(hg_repo, change, arg)
2587
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
225 hg_commit(hg_repo, text, author, epoch)
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
226 change_number += 1
fe3e87358b47 darcs2hg: pause and resume support, date extraction from commit hash, does not break on empty commits
Sébastien Pierre <sebastien@xprima.com>
parents: 2585
diff changeset
227 print "Darcs repository (_darcs) was not deleted. You can keep or remove it."
2349
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
228
88c881bda888 darcs2hg.py: import darcs project into mercurial
Sébastien Pierre <sebastien@xprima.com>
parents:
diff changeset
229 # EOF