Show More
@@ -1,225 +1,224 b'' | |||
|
1 | 1 | # darcs.py - darcs support for the convert extension |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2007-2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | from __future__ import absolute_import |
|
8 | 8 | |
|
9 | 9 | import errno |
|
10 | 10 | import os |
|
11 | 11 | import re |
|
12 | 12 | import shutil |
|
13 | 13 | |
|
14 | 14 | from mercurial.i18n import _ |
|
15 | 15 | from mercurial import ( |
|
16 | 16 | error, |
|
17 | 17 | pycompat, |
|
18 | 18 | util, |
|
19 | 19 | ) |
|
20 | 20 | from mercurial.utils import dateutil |
|
21 | 21 | from . import common |
|
22 | 22 | NoRepo = common.NoRepo |
|
23 | 23 | |
|
24 | 24 | # The naming drift of ElementTree is fun! |
|
25 | 25 | |
|
26 | 26 | try: |
|
27 | 27 | import xml.etree.cElementTree.ElementTree as ElementTree |
|
28 | 28 | import xml.etree.cElementTree.XMLParser as XMLParser |
|
29 | 29 | except ImportError: |
|
30 | 30 | try: |
|
31 | 31 | import xml.etree.ElementTree.ElementTree as ElementTree |
|
32 | 32 | import xml.etree.ElementTree.XMLParser as XMLParser |
|
33 | 33 | except ImportError: |
|
34 | 34 | try: |
|
35 | 35 | import elementtree.cElementTree.ElementTree as ElementTree |
|
36 | 36 | import elementtree.cElementTree.XMLParser as XMLParser |
|
37 | 37 | except ImportError: |
|
38 | 38 | try: |
|
39 | 39 | import elementtree.ElementTree.ElementTree as ElementTree |
|
40 | 40 | import elementtree.ElementTree.XMLParser as XMLParser |
|
41 | 41 | except ImportError: |
|
42 | 42 | pass |
|
43 | 43 | |
|
44 | 44 | class darcs_source(common.converter_source, common.commandline): |
|
45 | 45 | def __init__(self, ui, repotype, path, revs=None): |
|
46 | 46 | common.converter_source.__init__(self, ui, repotype, path, revs=revs) |
|
47 | 47 | common.commandline.__init__(self, ui, 'darcs') |
|
48 | 48 | |
|
49 | 49 | # check for _darcs, ElementTree so that we can easily skip |
|
50 | 50 | # test-convert-darcs if ElementTree is not around |
|
51 | 51 | if not os.path.exists(os.path.join(path, '_darcs')): |
|
52 | 52 | raise NoRepo(_("%s does not look like a darcs repository") % path) |
|
53 | 53 | |
|
54 | 54 | common.checktool('darcs') |
|
55 | 55 | version = self.run0('--version').splitlines()[0].strip() |
|
56 | 56 | if version < '2.1': |
|
57 | 57 | raise error.Abort(_('darcs version 2.1 or newer needed (found %r)') |
|
58 | 58 | % version) |
|
59 | 59 | |
|
60 | 60 | if "ElementTree" not in globals(): |
|
61 | 61 | raise error.Abort(_("Python ElementTree module is not available")) |
|
62 | 62 | |
|
63 | 63 | self.path = os.path.realpath(path) |
|
64 | 64 | |
|
65 | 65 | self.lastrev = None |
|
66 | 66 | self.changes = {} |
|
67 | 67 | self.parents = {} |
|
68 | 68 | self.tags = {} |
|
69 | 69 | |
|
70 | 70 | # Check darcs repository format |
|
71 | 71 | format = self.format() |
|
72 | 72 | if format: |
|
73 | 73 | if format in ('darcs-1.0', 'hashed'): |
|
74 | 74 | raise NoRepo(_("%s repository format is unsupported, " |
|
75 | 75 | "please upgrade") % format) |
|
76 | 76 | else: |
|
77 | 77 | self.ui.warn(_('failed to detect repository format!')) |
|
78 | 78 | |
|
79 | 79 | def before(self): |
|
80 | 80 | self.tmppath = pycompat.mkdtemp( |
|
81 | 81 | prefix='convert-' + os.path.basename(self.path) + '-') |
|
82 | 82 | output, status = self.run('init', repodir=self.tmppath) |
|
83 | 83 | self.checkexit(status) |
|
84 | 84 | |
|
85 | 85 | tree = self.xml('changes', xml_output=True, summary=True, |
|
86 | 86 | repodir=self.path) |
|
87 | 87 | tagname = None |
|
88 | 88 | child = None |
|
89 | 89 | for elt in tree.findall('patch'): |
|
90 | 90 | node = elt.get('hash') |
|
91 | 91 | name = elt.findtext('name', '') |
|
92 | 92 | if name.startswith('TAG '): |
|
93 | 93 | tagname = name[4:].strip() |
|
94 | 94 | elif tagname is not None: |
|
95 | 95 | self.tags[tagname] = node |
|
96 | 96 | tagname = None |
|
97 | 97 | self.changes[node] = elt |
|
98 | 98 | self.parents[child] = [node] |
|
99 | 99 | child = node |
|
100 | 100 | self.parents[child] = [] |
|
101 | 101 | |
|
102 | 102 | def after(self): |
|
103 | 103 | self.ui.debug('cleaning up %s\n' % self.tmppath) |
|
104 | 104 | shutil.rmtree(self.tmppath, ignore_errors=True) |
|
105 | 105 | |
|
106 | 106 | def recode(self, s, encoding=None): |
|
107 | 107 | if isinstance(s, pycompat.unicode): |
|
108 | 108 | # XMLParser returns unicode objects for anything it can't |
|
109 | 109 | # encode into ASCII. We convert them back to str to get |
|
110 | 110 | # recode's normal conversion behavior. |
|
111 | 111 | s = s.encode('latin-1') |
|
112 | 112 | return super(darcs_source, self).recode(s, encoding) |
|
113 | 113 | |
|
114 | 114 | def xml(self, cmd, **kwargs): |
|
115 | 115 | # NOTE: darcs is currently encoding agnostic and will print |
|
116 | 116 | # patch metadata byte-for-byte, even in the XML changelog. |
|
117 | 117 | etree = ElementTree() |
|
118 | 118 | # While we are decoding the XML as latin-1 to be as liberal as |
|
119 | 119 | # possible, etree will still raise an exception if any |
|
120 | 120 | # non-printable characters are in the XML changelog. |
|
121 | 121 | parser = XMLParser(encoding='latin-1') |
|
122 | 122 | p = self._run(cmd, **kwargs) |
|
123 | 123 | etree.parse(p.stdout, parser=parser) |
|
124 | 124 | p.wait() |
|
125 | 125 | self.checkexit(p.returncode) |
|
126 | 126 | return etree.getroot() |
|
127 | 127 | |
|
128 | 128 | def format(self): |
|
129 |
output, status = self.run('show', 'repo', |
|
|
130 | repodir=self.path) | |
|
129 | output, status = self.run('show', 'repo', repodir=self.path) | |
|
131 | 130 | self.checkexit(status) |
|
132 | 131 | m = re.search(r'^\s*Format:\s*(.*)$', output, re.MULTILINE) |
|
133 | 132 | if not m: |
|
134 | 133 | return None |
|
135 | 134 | return ','.join(sorted(f.strip() for f in m.group(1).split(','))) |
|
136 | 135 | |
|
137 | 136 | def manifest(self): |
|
138 | 137 | man = [] |
|
139 | 138 | output, status = self.run('show', 'files', no_directories=True, |
|
140 | 139 | repodir=self.tmppath) |
|
141 | 140 | self.checkexit(status) |
|
142 | 141 | for line in output.split('\n'): |
|
143 | 142 | path = line[2:] |
|
144 | 143 | if path: |
|
145 | 144 | man.append(path) |
|
146 | 145 | return man |
|
147 | 146 | |
|
148 | 147 | def getheads(self): |
|
149 | 148 | return self.parents[None] |
|
150 | 149 | |
|
151 | 150 | def getcommit(self, rev): |
|
152 | 151 | elt = self.changes[rev] |
|
153 | 152 | dateformat = '%a %b %d %H:%M:%S %Z %Y' |
|
154 | 153 | date = dateutil.strdate(elt.get('local_date'), dateformat) |
|
155 | 154 | desc = elt.findtext('name') + '\n' + elt.findtext('comment', '') |
|
156 | 155 | # etree can return unicode objects for name, comment, and author, |
|
157 | 156 | # so recode() is used to ensure str objects are emitted. |
|
158 | 157 | newdateformat = '%Y-%m-%d %H:%M:%S %1%2' |
|
159 | 158 | return common.commit(author=self.recode(elt.get('author')), |
|
160 | 159 | date=dateutil.datestr(date, newdateformat), |
|
161 | 160 | desc=self.recode(desc).strip(), |
|
162 | 161 | parents=self.parents[rev]) |
|
163 | 162 | |
|
164 | 163 | def pull(self, rev): |
|
165 | 164 | output, status = self.run('pull', self.path, all=True, |
|
166 | 165 | match='hash %s' % rev, |
|
167 | 166 | no_test=True, no_posthook=True, |
|
168 | 167 | external_merge='/bin/false', |
|
169 | 168 | repodir=self.tmppath) |
|
170 | 169 | if status: |
|
171 | 170 | if output.find('We have conflicts in') == -1: |
|
172 | 171 | self.checkexit(status, output) |
|
173 | 172 | output, status = self.run('revert', all=True, repodir=self.tmppath) |
|
174 | 173 | self.checkexit(status, output) |
|
175 | 174 | |
|
176 | 175 | def getchanges(self, rev, full): |
|
177 | 176 | if full: |
|
178 | 177 | raise error.Abort(_("convert from darcs does not support --full")) |
|
179 | 178 | copies = {} |
|
180 | 179 | changes = [] |
|
181 | 180 | man = None |
|
182 | 181 | for elt in self.changes[rev].find('summary').getchildren(): |
|
183 | 182 | if elt.tag in ('add_directory', 'remove_directory'): |
|
184 | 183 | continue |
|
185 | 184 | if elt.tag == 'move': |
|
186 | 185 | if man is None: |
|
187 | 186 | man = self.manifest() |
|
188 | 187 | source, dest = elt.get('from'), elt.get('to') |
|
189 | 188 | if source in man: |
|
190 | 189 | # File move |
|
191 | 190 | changes.append((source, rev)) |
|
192 | 191 | changes.append((dest, rev)) |
|
193 | 192 | copies[dest] = source |
|
194 | 193 | else: |
|
195 | 194 | # Directory move, deduce file moves from manifest |
|
196 | 195 | source = source + '/' |
|
197 | 196 | for f in man: |
|
198 | 197 | if not f.startswith(source): |
|
199 | 198 | continue |
|
200 | 199 | fdest = dest + '/' + f[len(source):] |
|
201 | 200 | changes.append((f, rev)) |
|
202 | 201 | changes.append((fdest, rev)) |
|
203 | 202 | copies[fdest] = f |
|
204 | 203 | else: |
|
205 | 204 | changes.append((elt.text.strip(), rev)) |
|
206 | 205 | self.pull(rev) |
|
207 | 206 | self.lastrev = rev |
|
208 | 207 | return sorted(changes), copies, set() |
|
209 | 208 | |
|
210 | 209 | def getfile(self, name, rev): |
|
211 | 210 | if rev != self.lastrev: |
|
212 | 211 | raise error.Abort(_('internal calling inconsistency')) |
|
213 | 212 | path = os.path.join(self.tmppath, name) |
|
214 | 213 | try: |
|
215 | 214 | data = util.readfile(path) |
|
216 | 215 | mode = os.lstat(path).st_mode |
|
217 | 216 | except IOError as inst: |
|
218 | 217 | if inst.errno == errno.ENOENT: |
|
219 | 218 | return None, None |
|
220 | 219 | raise |
|
221 | 220 | mode = (mode & 0o111) and 'x' or '' |
|
222 | 221 | return data, mode |
|
223 | 222 | |
|
224 | 223 | def gettags(self): |
|
225 | 224 | return self.tags |
General Comments 0
You need to be logged in to leave comments.
Login now