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