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