##// END OF EJS Templates
py3: convert the mode argument of os.fdopen to unicodes (2 of 2)
Pulkit Goyal -
r30925:82f1ef8b default
parent child Browse files
Show More
@@ -1,1353 +1,1353 b''
1 # Subversion 1.4/1.5 Python API backend
1 # Subversion 1.4/1.5 Python API backend
2 #
2 #
3 # Copyright(C) 2007 Daniel Holth et al
3 # Copyright(C) 2007 Daniel Holth et al
4 from __future__ import absolute_import
4 from __future__ import absolute_import
5
5
6 import os
6 import os
7 import re
7 import re
8 import tempfile
8 import tempfile
9 import xml.dom.minidom
9 import xml.dom.minidom
10
10
11 from mercurial.i18n import _
11 from mercurial.i18n import _
12 from mercurial import (
12 from mercurial import (
13 encoding,
13 encoding,
14 error,
14 error,
15 pycompat,
15 pycompat,
16 scmutil,
16 scmutil,
17 util,
17 util,
18 )
18 )
19
19
20 from . import common
20 from . import common
21
21
22 pickle = util.pickle
22 pickle = util.pickle
23 stringio = util.stringio
23 stringio = util.stringio
24 propertycache = util.propertycache
24 propertycache = util.propertycache
25 urlerr = util.urlerr
25 urlerr = util.urlerr
26 urlreq = util.urlreq
26 urlreq = util.urlreq
27
27
28 commandline = common.commandline
28 commandline = common.commandline
29 commit = common.commit
29 commit = common.commit
30 converter_sink = common.converter_sink
30 converter_sink = common.converter_sink
31 converter_source = common.converter_source
31 converter_source = common.converter_source
32 decodeargs = common.decodeargs
32 decodeargs = common.decodeargs
33 encodeargs = common.encodeargs
33 encodeargs = common.encodeargs
34 makedatetimestamp = common.makedatetimestamp
34 makedatetimestamp = common.makedatetimestamp
35 mapfile = common.mapfile
35 mapfile = common.mapfile
36 MissingTool = common.MissingTool
36 MissingTool = common.MissingTool
37 NoRepo = common.NoRepo
37 NoRepo = common.NoRepo
38
38
39 # Subversion stuff. Works best with very recent Python SVN bindings
39 # Subversion stuff. Works best with very recent Python SVN bindings
40 # e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing
40 # e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing
41 # these bindings.
41 # these bindings.
42
42
43 try:
43 try:
44 import svn
44 import svn
45 import svn.client
45 import svn.client
46 import svn.core
46 import svn.core
47 import svn.ra
47 import svn.ra
48 import svn.delta
48 import svn.delta
49 from . import transport
49 from . import transport
50 import warnings
50 import warnings
51 warnings.filterwarnings('ignore',
51 warnings.filterwarnings('ignore',
52 module='svn.core',
52 module='svn.core',
53 category=DeprecationWarning)
53 category=DeprecationWarning)
54 svn.core.SubversionException # trigger import to catch error
54 svn.core.SubversionException # trigger import to catch error
55
55
56 except ImportError:
56 except ImportError:
57 svn = None
57 svn = None
58
58
59 class SvnPathNotFound(Exception):
59 class SvnPathNotFound(Exception):
60 pass
60 pass
61
61
62 def revsplit(rev):
62 def revsplit(rev):
63 """Parse a revision string and return (uuid, path, revnum).
63 """Parse a revision string and return (uuid, path, revnum).
64 >>> revsplit('svn:a2147622-4a9f-4db4-a8d3-13562ff547b2'
64 >>> revsplit('svn:a2147622-4a9f-4db4-a8d3-13562ff547b2'
65 ... '/proj%20B/mytrunk/mytrunk@1')
65 ... '/proj%20B/mytrunk/mytrunk@1')
66 ('a2147622-4a9f-4db4-a8d3-13562ff547b2', '/proj%20B/mytrunk/mytrunk', 1)
66 ('a2147622-4a9f-4db4-a8d3-13562ff547b2', '/proj%20B/mytrunk/mytrunk', 1)
67 >>> revsplit('svn:8af66a51-67f5-4354-b62c-98d67cc7be1d@1')
67 >>> revsplit('svn:8af66a51-67f5-4354-b62c-98d67cc7be1d@1')
68 ('', '', 1)
68 ('', '', 1)
69 >>> revsplit('@7')
69 >>> revsplit('@7')
70 ('', '', 7)
70 ('', '', 7)
71 >>> revsplit('7')
71 >>> revsplit('7')
72 ('', '', 0)
72 ('', '', 0)
73 >>> revsplit('bad')
73 >>> revsplit('bad')
74 ('', '', 0)
74 ('', '', 0)
75 """
75 """
76 parts = rev.rsplit('@', 1)
76 parts = rev.rsplit('@', 1)
77 revnum = 0
77 revnum = 0
78 if len(parts) > 1:
78 if len(parts) > 1:
79 revnum = int(parts[1])
79 revnum = int(parts[1])
80 parts = parts[0].split('/', 1)
80 parts = parts[0].split('/', 1)
81 uuid = ''
81 uuid = ''
82 mod = ''
82 mod = ''
83 if len(parts) > 1 and parts[0].startswith('svn:'):
83 if len(parts) > 1 and parts[0].startswith('svn:'):
84 uuid = parts[0][4:]
84 uuid = parts[0][4:]
85 mod = '/' + parts[1]
85 mod = '/' + parts[1]
86 return uuid, mod, revnum
86 return uuid, mod, revnum
87
87
88 def quote(s):
88 def quote(s):
89 # As of svn 1.7, many svn calls expect "canonical" paths. In
89 # As of svn 1.7, many svn calls expect "canonical" paths. In
90 # theory, we should call svn.core.*canonicalize() on all paths
90 # theory, we should call svn.core.*canonicalize() on all paths
91 # before passing them to the API. Instead, we assume the base url
91 # before passing them to the API. Instead, we assume the base url
92 # is canonical and copy the behaviour of svn URL encoding function
92 # is canonical and copy the behaviour of svn URL encoding function
93 # so we can extend it safely with new components. The "safe"
93 # so we can extend it safely with new components. The "safe"
94 # characters were taken from the "svn_uri__char_validity" table in
94 # characters were taken from the "svn_uri__char_validity" table in
95 # libsvn_subr/path.c.
95 # libsvn_subr/path.c.
96 return urlreq.quote(s, "!$&'()*+,-./:=@_~")
96 return urlreq.quote(s, "!$&'()*+,-./:=@_~")
97
97
98 def geturl(path):
98 def geturl(path):
99 try:
99 try:
100 return svn.client.url_from_path(svn.core.svn_path_canonicalize(path))
100 return svn.client.url_from_path(svn.core.svn_path_canonicalize(path))
101 except svn.core.SubversionException:
101 except svn.core.SubversionException:
102 # svn.client.url_from_path() fails with local repositories
102 # svn.client.url_from_path() fails with local repositories
103 pass
103 pass
104 if os.path.isdir(path):
104 if os.path.isdir(path):
105 path = os.path.normpath(os.path.abspath(path))
105 path = os.path.normpath(os.path.abspath(path))
106 if pycompat.osname == 'nt':
106 if pycompat.osname == 'nt':
107 path = '/' + util.normpath(path)
107 path = '/' + util.normpath(path)
108 # Module URL is later compared with the repository URL returned
108 # Module URL is later compared with the repository URL returned
109 # by svn API, which is UTF-8.
109 # by svn API, which is UTF-8.
110 path = encoding.tolocal(path)
110 path = encoding.tolocal(path)
111 path = 'file://%s' % quote(path)
111 path = 'file://%s' % quote(path)
112 return svn.core.svn_path_canonicalize(path)
112 return svn.core.svn_path_canonicalize(path)
113
113
114 def optrev(number):
114 def optrev(number):
115 optrev = svn.core.svn_opt_revision_t()
115 optrev = svn.core.svn_opt_revision_t()
116 optrev.kind = svn.core.svn_opt_revision_number
116 optrev.kind = svn.core.svn_opt_revision_number
117 optrev.value.number = number
117 optrev.value.number = number
118 return optrev
118 return optrev
119
119
120 class changedpath(object):
120 class changedpath(object):
121 def __init__(self, p):
121 def __init__(self, p):
122 self.copyfrom_path = p.copyfrom_path
122 self.copyfrom_path = p.copyfrom_path
123 self.copyfrom_rev = p.copyfrom_rev
123 self.copyfrom_rev = p.copyfrom_rev
124 self.action = p.action
124 self.action = p.action
125
125
126 def get_log_child(fp, url, paths, start, end, limit=0,
126 def get_log_child(fp, url, paths, start, end, limit=0,
127 discover_changed_paths=True, strict_node_history=False):
127 discover_changed_paths=True, strict_node_history=False):
128 protocol = -1
128 protocol = -1
129 def receiver(orig_paths, revnum, author, date, message, pool):
129 def receiver(orig_paths, revnum, author, date, message, pool):
130 paths = {}
130 paths = {}
131 if orig_paths is not None:
131 if orig_paths is not None:
132 for k, v in orig_paths.iteritems():
132 for k, v in orig_paths.iteritems():
133 paths[k] = changedpath(v)
133 paths[k] = changedpath(v)
134 pickle.dump((paths, revnum, author, date, message),
134 pickle.dump((paths, revnum, author, date, message),
135 fp, protocol)
135 fp, protocol)
136
136
137 try:
137 try:
138 # Use an ra of our own so that our parent can consume
138 # Use an ra of our own so that our parent can consume
139 # our results without confusing the server.
139 # our results without confusing the server.
140 t = transport.SvnRaTransport(url=url)
140 t = transport.SvnRaTransport(url=url)
141 svn.ra.get_log(t.ra, paths, start, end, limit,
141 svn.ra.get_log(t.ra, paths, start, end, limit,
142 discover_changed_paths,
142 discover_changed_paths,
143 strict_node_history,
143 strict_node_history,
144 receiver)
144 receiver)
145 except IOError:
145 except IOError:
146 # Caller may interrupt the iteration
146 # Caller may interrupt the iteration
147 pickle.dump(None, fp, protocol)
147 pickle.dump(None, fp, protocol)
148 except Exception as inst:
148 except Exception as inst:
149 pickle.dump(str(inst), fp, protocol)
149 pickle.dump(str(inst), fp, protocol)
150 else:
150 else:
151 pickle.dump(None, fp, protocol)
151 pickle.dump(None, fp, protocol)
152 fp.close()
152 fp.close()
153 # With large history, cleanup process goes crazy and suddenly
153 # With large history, cleanup process goes crazy and suddenly
154 # consumes *huge* amount of memory. The output file being closed,
154 # consumes *huge* amount of memory. The output file being closed,
155 # there is no need for clean termination.
155 # there is no need for clean termination.
156 os._exit(0)
156 os._exit(0)
157
157
158 def debugsvnlog(ui, **opts):
158 def debugsvnlog(ui, **opts):
159 """Fetch SVN log in a subprocess and channel them back to parent to
159 """Fetch SVN log in a subprocess and channel them back to parent to
160 avoid memory collection issues.
160 avoid memory collection issues.
161 """
161 """
162 if svn is None:
162 if svn is None:
163 raise error.Abort(_('debugsvnlog could not load Subversion python '
163 raise error.Abort(_('debugsvnlog could not load Subversion python '
164 'bindings'))
164 'bindings'))
165
165
166 args = decodeargs(ui.fin.read())
166 args = decodeargs(ui.fin.read())
167 get_log_child(ui.fout, *args)
167 get_log_child(ui.fout, *args)
168
168
169 class logstream(object):
169 class logstream(object):
170 """Interruptible revision log iterator."""
170 """Interruptible revision log iterator."""
171 def __init__(self, stdout):
171 def __init__(self, stdout):
172 self._stdout = stdout
172 self._stdout = stdout
173
173
174 def __iter__(self):
174 def __iter__(self):
175 while True:
175 while True:
176 try:
176 try:
177 entry = pickle.load(self._stdout)
177 entry = pickle.load(self._stdout)
178 except EOFError:
178 except EOFError:
179 raise error.Abort(_('Mercurial failed to run itself, check'
179 raise error.Abort(_('Mercurial failed to run itself, check'
180 ' hg executable is in PATH'))
180 ' hg executable is in PATH'))
181 try:
181 try:
182 orig_paths, revnum, author, date, message = entry
182 orig_paths, revnum, author, date, message = entry
183 except (TypeError, ValueError):
183 except (TypeError, ValueError):
184 if entry is None:
184 if entry is None:
185 break
185 break
186 raise error.Abort(_("log stream exception '%s'") % entry)
186 raise error.Abort(_("log stream exception '%s'") % entry)
187 yield entry
187 yield entry
188
188
189 def close(self):
189 def close(self):
190 if self._stdout:
190 if self._stdout:
191 self._stdout.close()
191 self._stdout.close()
192 self._stdout = None
192 self._stdout = None
193
193
194 class directlogstream(list):
194 class directlogstream(list):
195 """Direct revision log iterator.
195 """Direct revision log iterator.
196 This can be used for debugging and development but it will probably leak
196 This can be used for debugging and development but it will probably leak
197 memory and is not suitable for real conversions."""
197 memory and is not suitable for real conversions."""
198 def __init__(self, url, paths, start, end, limit=0,
198 def __init__(self, url, paths, start, end, limit=0,
199 discover_changed_paths=True, strict_node_history=False):
199 discover_changed_paths=True, strict_node_history=False):
200
200
201 def receiver(orig_paths, revnum, author, date, message, pool):
201 def receiver(orig_paths, revnum, author, date, message, pool):
202 paths = {}
202 paths = {}
203 if orig_paths is not None:
203 if orig_paths is not None:
204 for k, v in orig_paths.iteritems():
204 for k, v in orig_paths.iteritems():
205 paths[k] = changedpath(v)
205 paths[k] = changedpath(v)
206 self.append((paths, revnum, author, date, message))
206 self.append((paths, revnum, author, date, message))
207
207
208 # Use an ra of our own so that our parent can consume
208 # Use an ra of our own so that our parent can consume
209 # our results without confusing the server.
209 # our results without confusing the server.
210 t = transport.SvnRaTransport(url=url)
210 t = transport.SvnRaTransport(url=url)
211 svn.ra.get_log(t.ra, paths, start, end, limit,
211 svn.ra.get_log(t.ra, paths, start, end, limit,
212 discover_changed_paths,
212 discover_changed_paths,
213 strict_node_history,
213 strict_node_history,
214 receiver)
214 receiver)
215
215
216 def close(self):
216 def close(self):
217 pass
217 pass
218
218
219 # Check to see if the given path is a local Subversion repo. Verify this by
219 # Check to see if the given path is a local Subversion repo. Verify this by
220 # looking for several svn-specific files and directories in the given
220 # looking for several svn-specific files and directories in the given
221 # directory.
221 # directory.
222 def filecheck(ui, path, proto):
222 def filecheck(ui, path, proto):
223 for x in ('locks', 'hooks', 'format', 'db'):
223 for x in ('locks', 'hooks', 'format', 'db'):
224 if not os.path.exists(os.path.join(path, x)):
224 if not os.path.exists(os.path.join(path, x)):
225 return False
225 return False
226 return True
226 return True
227
227
228 # Check to see if a given path is the root of an svn repo over http. We verify
228 # Check to see if a given path is the root of an svn repo over http. We verify
229 # this by requesting a version-controlled URL we know can't exist and looking
229 # this by requesting a version-controlled URL we know can't exist and looking
230 # for the svn-specific "not found" XML.
230 # for the svn-specific "not found" XML.
231 def httpcheck(ui, path, proto):
231 def httpcheck(ui, path, proto):
232 try:
232 try:
233 opener = urlreq.buildopener()
233 opener = urlreq.buildopener()
234 rsp = opener.open('%s://%s/!svn/ver/0/.svn' % (proto, path))
234 rsp = opener.open('%s://%s/!svn/ver/0/.svn' % (proto, path))
235 data = rsp.read()
235 data = rsp.read()
236 except urlerr.httperror as inst:
236 except urlerr.httperror as inst:
237 if inst.code != 404:
237 if inst.code != 404:
238 # Except for 404 we cannot know for sure this is not an svn repo
238 # Except for 404 we cannot know for sure this is not an svn repo
239 ui.warn(_('svn: cannot probe remote repository, assume it could '
239 ui.warn(_('svn: cannot probe remote repository, assume it could '
240 'be a subversion repository. Use --source-type if you '
240 'be a subversion repository. Use --source-type if you '
241 'know better.\n'))
241 'know better.\n'))
242 return True
242 return True
243 data = inst.fp.read()
243 data = inst.fp.read()
244 except Exception:
244 except Exception:
245 # Could be urlerr.urlerror if the URL is invalid or anything else.
245 # Could be urlerr.urlerror if the URL is invalid or anything else.
246 return False
246 return False
247 return '<m:human-readable errcode="160013">' in data
247 return '<m:human-readable errcode="160013">' in data
248
248
249 protomap = {'http': httpcheck,
249 protomap = {'http': httpcheck,
250 'https': httpcheck,
250 'https': httpcheck,
251 'file': filecheck,
251 'file': filecheck,
252 }
252 }
253 def issvnurl(ui, url):
253 def issvnurl(ui, url):
254 try:
254 try:
255 proto, path = url.split('://', 1)
255 proto, path = url.split('://', 1)
256 if proto == 'file':
256 if proto == 'file':
257 if (pycompat.osname == 'nt' and path[:1] == '/'
257 if (pycompat.osname == 'nt' and path[:1] == '/'
258 and path[1:2].isalpha() and path[2:6].lower() == '%3a/'):
258 and path[1:2].isalpha() and path[2:6].lower() == '%3a/'):
259 path = path[:2] + ':/' + path[6:]
259 path = path[:2] + ':/' + path[6:]
260 path = urlreq.url2pathname(path)
260 path = urlreq.url2pathname(path)
261 except ValueError:
261 except ValueError:
262 proto = 'file'
262 proto = 'file'
263 path = os.path.abspath(url)
263 path = os.path.abspath(url)
264 if proto == 'file':
264 if proto == 'file':
265 path = util.pconvert(path)
265 path = util.pconvert(path)
266 check = protomap.get(proto, lambda *args: False)
266 check = protomap.get(proto, lambda *args: False)
267 while '/' in path:
267 while '/' in path:
268 if check(ui, path, proto):
268 if check(ui, path, proto):
269 return True
269 return True
270 path = path.rsplit('/', 1)[0]
270 path = path.rsplit('/', 1)[0]
271 return False
271 return False
272
272
273 # SVN conversion code stolen from bzr-svn and tailor
273 # SVN conversion code stolen from bzr-svn and tailor
274 #
274 #
275 # Subversion looks like a versioned filesystem, branches structures
275 # Subversion looks like a versioned filesystem, branches structures
276 # are defined by conventions and not enforced by the tool. First,
276 # are defined by conventions and not enforced by the tool. First,
277 # we define the potential branches (modules) as "trunk" and "branches"
277 # we define the potential branches (modules) as "trunk" and "branches"
278 # children directories. Revisions are then identified by their
278 # children directories. Revisions are then identified by their
279 # module and revision number (and a repository identifier).
279 # module and revision number (and a repository identifier).
280 #
280 #
281 # The revision graph is really a tree (or a forest). By default, a
281 # The revision graph is really a tree (or a forest). By default, a
282 # revision parent is the previous revision in the same module. If the
282 # revision parent is the previous revision in the same module. If the
283 # module directory is copied/moved from another module then the
283 # module directory is copied/moved from another module then the
284 # revision is the module root and its parent the source revision in
284 # revision is the module root and its parent the source revision in
285 # the parent module. A revision has at most one parent.
285 # the parent module. A revision has at most one parent.
286 #
286 #
287 class svn_source(converter_source):
287 class svn_source(converter_source):
288 def __init__(self, ui, url, revs=None):
288 def __init__(self, ui, url, revs=None):
289 super(svn_source, self).__init__(ui, url, revs=revs)
289 super(svn_source, self).__init__(ui, url, revs=revs)
290
290
291 if not (url.startswith('svn://') or url.startswith('svn+ssh://') or
291 if not (url.startswith('svn://') or url.startswith('svn+ssh://') or
292 (os.path.exists(url) and
292 (os.path.exists(url) and
293 os.path.exists(os.path.join(url, '.svn'))) or
293 os.path.exists(os.path.join(url, '.svn'))) or
294 issvnurl(ui, url)):
294 issvnurl(ui, url)):
295 raise NoRepo(_("%s does not look like a Subversion repository")
295 raise NoRepo(_("%s does not look like a Subversion repository")
296 % url)
296 % url)
297 if svn is None:
297 if svn is None:
298 raise MissingTool(_('could not load Subversion python bindings'))
298 raise MissingTool(_('could not load Subversion python bindings'))
299
299
300 try:
300 try:
301 version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR
301 version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR
302 if version < (1, 4):
302 if version < (1, 4):
303 raise MissingTool(_('Subversion python bindings %d.%d found, '
303 raise MissingTool(_('Subversion python bindings %d.%d found, '
304 '1.4 or later required') % version)
304 '1.4 or later required') % version)
305 except AttributeError:
305 except AttributeError:
306 raise MissingTool(_('Subversion python bindings are too old, 1.4 '
306 raise MissingTool(_('Subversion python bindings are too old, 1.4 '
307 'or later required'))
307 'or later required'))
308
308
309 self.lastrevs = {}
309 self.lastrevs = {}
310
310
311 latest = None
311 latest = None
312 try:
312 try:
313 # Support file://path@rev syntax. Useful e.g. to convert
313 # Support file://path@rev syntax. Useful e.g. to convert
314 # deleted branches.
314 # deleted branches.
315 at = url.rfind('@')
315 at = url.rfind('@')
316 if at >= 0:
316 if at >= 0:
317 latest = int(url[at + 1:])
317 latest = int(url[at + 1:])
318 url = url[:at]
318 url = url[:at]
319 except ValueError:
319 except ValueError:
320 pass
320 pass
321 self.url = geturl(url)
321 self.url = geturl(url)
322 self.encoding = 'UTF-8' # Subversion is always nominal UTF-8
322 self.encoding = 'UTF-8' # Subversion is always nominal UTF-8
323 try:
323 try:
324 self.transport = transport.SvnRaTransport(url=self.url)
324 self.transport = transport.SvnRaTransport(url=self.url)
325 self.ra = self.transport.ra
325 self.ra = self.transport.ra
326 self.ctx = self.transport.client
326 self.ctx = self.transport.client
327 self.baseurl = svn.ra.get_repos_root(self.ra)
327 self.baseurl = svn.ra.get_repos_root(self.ra)
328 # Module is either empty or a repository path starting with
328 # Module is either empty or a repository path starting with
329 # a slash and not ending with a slash.
329 # a slash and not ending with a slash.
330 self.module = urlreq.unquote(self.url[len(self.baseurl):])
330 self.module = urlreq.unquote(self.url[len(self.baseurl):])
331 self.prevmodule = None
331 self.prevmodule = None
332 self.rootmodule = self.module
332 self.rootmodule = self.module
333 self.commits = {}
333 self.commits = {}
334 self.paths = {}
334 self.paths = {}
335 self.uuid = svn.ra.get_uuid(self.ra)
335 self.uuid = svn.ra.get_uuid(self.ra)
336 except svn.core.SubversionException:
336 except svn.core.SubversionException:
337 ui.traceback()
337 ui.traceback()
338 svnversion = '%d.%d.%d' % (svn.core.SVN_VER_MAJOR,
338 svnversion = '%d.%d.%d' % (svn.core.SVN_VER_MAJOR,
339 svn.core.SVN_VER_MINOR,
339 svn.core.SVN_VER_MINOR,
340 svn.core.SVN_VER_MICRO)
340 svn.core.SVN_VER_MICRO)
341 raise NoRepo(_("%s does not look like a Subversion repository "
341 raise NoRepo(_("%s does not look like a Subversion repository "
342 "to libsvn version %s")
342 "to libsvn version %s")
343 % (self.url, svnversion))
343 % (self.url, svnversion))
344
344
345 if revs:
345 if revs:
346 if len(revs) > 1:
346 if len(revs) > 1:
347 raise error.Abort(_('subversion source does not support '
347 raise error.Abort(_('subversion source does not support '
348 'specifying multiple revisions'))
348 'specifying multiple revisions'))
349 try:
349 try:
350 latest = int(revs[0])
350 latest = int(revs[0])
351 except ValueError:
351 except ValueError:
352 raise error.Abort(_('svn: revision %s is not an integer') %
352 raise error.Abort(_('svn: revision %s is not an integer') %
353 revs[0])
353 revs[0])
354
354
355 self.trunkname = self.ui.config('convert', 'svn.trunk',
355 self.trunkname = self.ui.config('convert', 'svn.trunk',
356 'trunk').strip('/')
356 'trunk').strip('/')
357 self.startrev = self.ui.config('convert', 'svn.startrev', default=0)
357 self.startrev = self.ui.config('convert', 'svn.startrev', default=0)
358 try:
358 try:
359 self.startrev = int(self.startrev)
359 self.startrev = int(self.startrev)
360 if self.startrev < 0:
360 if self.startrev < 0:
361 self.startrev = 0
361 self.startrev = 0
362 except ValueError:
362 except ValueError:
363 raise error.Abort(_('svn: start revision %s is not an integer')
363 raise error.Abort(_('svn: start revision %s is not an integer')
364 % self.startrev)
364 % self.startrev)
365
365
366 try:
366 try:
367 self.head = self.latest(self.module, latest)
367 self.head = self.latest(self.module, latest)
368 except SvnPathNotFound:
368 except SvnPathNotFound:
369 self.head = None
369 self.head = None
370 if not self.head:
370 if not self.head:
371 raise error.Abort(_('no revision found in module %s')
371 raise error.Abort(_('no revision found in module %s')
372 % self.module)
372 % self.module)
373 self.last_changed = self.revnum(self.head)
373 self.last_changed = self.revnum(self.head)
374
374
375 self._changescache = (None, None)
375 self._changescache = (None, None)
376
376
377 if os.path.exists(os.path.join(url, '.svn/entries')):
377 if os.path.exists(os.path.join(url, '.svn/entries')):
378 self.wc = url
378 self.wc = url
379 else:
379 else:
380 self.wc = None
380 self.wc = None
381 self.convertfp = None
381 self.convertfp = None
382
382
383 def setrevmap(self, revmap):
383 def setrevmap(self, revmap):
384 lastrevs = {}
384 lastrevs = {}
385 for revid in revmap.iterkeys():
385 for revid in revmap.iterkeys():
386 uuid, module, revnum = revsplit(revid)
386 uuid, module, revnum = revsplit(revid)
387 lastrevnum = lastrevs.setdefault(module, revnum)
387 lastrevnum = lastrevs.setdefault(module, revnum)
388 if revnum > lastrevnum:
388 if revnum > lastrevnum:
389 lastrevs[module] = revnum
389 lastrevs[module] = revnum
390 self.lastrevs = lastrevs
390 self.lastrevs = lastrevs
391
391
392 def exists(self, path, optrev):
392 def exists(self, path, optrev):
393 try:
393 try:
394 svn.client.ls(self.url.rstrip('/') + '/' + quote(path),
394 svn.client.ls(self.url.rstrip('/') + '/' + quote(path),
395 optrev, False, self.ctx)
395 optrev, False, self.ctx)
396 return True
396 return True
397 except svn.core.SubversionException:
397 except svn.core.SubversionException:
398 return False
398 return False
399
399
400 def getheads(self):
400 def getheads(self):
401
401
402 def isdir(path, revnum):
402 def isdir(path, revnum):
403 kind = self._checkpath(path, revnum)
403 kind = self._checkpath(path, revnum)
404 return kind == svn.core.svn_node_dir
404 return kind == svn.core.svn_node_dir
405
405
406 def getcfgpath(name, rev):
406 def getcfgpath(name, rev):
407 cfgpath = self.ui.config('convert', 'svn.' + name)
407 cfgpath = self.ui.config('convert', 'svn.' + name)
408 if cfgpath is not None and cfgpath.strip() == '':
408 if cfgpath is not None and cfgpath.strip() == '':
409 return None
409 return None
410 path = (cfgpath or name).strip('/')
410 path = (cfgpath or name).strip('/')
411 if not self.exists(path, rev):
411 if not self.exists(path, rev):
412 if self.module.endswith(path) and name == 'trunk':
412 if self.module.endswith(path) and name == 'trunk':
413 # we are converting from inside this directory
413 # we are converting from inside this directory
414 return None
414 return None
415 if cfgpath:
415 if cfgpath:
416 raise error.Abort(_('expected %s to be at %r, but not found'
416 raise error.Abort(_('expected %s to be at %r, but not found'
417 ) % (name, path))
417 ) % (name, path))
418 return None
418 return None
419 self.ui.note(_('found %s at %r\n') % (name, path))
419 self.ui.note(_('found %s at %r\n') % (name, path))
420 return path
420 return path
421
421
422 rev = optrev(self.last_changed)
422 rev = optrev(self.last_changed)
423 oldmodule = ''
423 oldmodule = ''
424 trunk = getcfgpath('trunk', rev)
424 trunk = getcfgpath('trunk', rev)
425 self.tags = getcfgpath('tags', rev)
425 self.tags = getcfgpath('tags', rev)
426 branches = getcfgpath('branches', rev)
426 branches = getcfgpath('branches', rev)
427
427
428 # If the project has a trunk or branches, we will extract heads
428 # If the project has a trunk or branches, we will extract heads
429 # from them. We keep the project root otherwise.
429 # from them. We keep the project root otherwise.
430 if trunk:
430 if trunk:
431 oldmodule = self.module or ''
431 oldmodule = self.module or ''
432 self.module += '/' + trunk
432 self.module += '/' + trunk
433 self.head = self.latest(self.module, self.last_changed)
433 self.head = self.latest(self.module, self.last_changed)
434 if not self.head:
434 if not self.head:
435 raise error.Abort(_('no revision found in module %s')
435 raise error.Abort(_('no revision found in module %s')
436 % self.module)
436 % self.module)
437
437
438 # First head in the list is the module's head
438 # First head in the list is the module's head
439 self.heads = [self.head]
439 self.heads = [self.head]
440 if self.tags is not None:
440 if self.tags is not None:
441 self.tags = '%s/%s' % (oldmodule , (self.tags or 'tags'))
441 self.tags = '%s/%s' % (oldmodule , (self.tags or 'tags'))
442
442
443 # Check if branches bring a few more heads to the list
443 # Check if branches bring a few more heads to the list
444 if branches:
444 if branches:
445 rpath = self.url.strip('/')
445 rpath = self.url.strip('/')
446 branchnames = svn.client.ls(rpath + '/' + quote(branches),
446 branchnames = svn.client.ls(rpath + '/' + quote(branches),
447 rev, False, self.ctx)
447 rev, False, self.ctx)
448 for branch in sorted(branchnames):
448 for branch in sorted(branchnames):
449 module = '%s/%s/%s' % (oldmodule, branches, branch)
449 module = '%s/%s/%s' % (oldmodule, branches, branch)
450 if not isdir(module, self.last_changed):
450 if not isdir(module, self.last_changed):
451 continue
451 continue
452 brevid = self.latest(module, self.last_changed)
452 brevid = self.latest(module, self.last_changed)
453 if not brevid:
453 if not brevid:
454 self.ui.note(_('ignoring empty branch %s\n') % branch)
454 self.ui.note(_('ignoring empty branch %s\n') % branch)
455 continue
455 continue
456 self.ui.note(_('found branch %s at %d\n') %
456 self.ui.note(_('found branch %s at %d\n') %
457 (branch, self.revnum(brevid)))
457 (branch, self.revnum(brevid)))
458 self.heads.append(brevid)
458 self.heads.append(brevid)
459
459
460 if self.startrev and self.heads:
460 if self.startrev and self.heads:
461 if len(self.heads) > 1:
461 if len(self.heads) > 1:
462 raise error.Abort(_('svn: start revision is not supported '
462 raise error.Abort(_('svn: start revision is not supported '
463 'with more than one branch'))
463 'with more than one branch'))
464 revnum = self.revnum(self.heads[0])
464 revnum = self.revnum(self.heads[0])
465 if revnum < self.startrev:
465 if revnum < self.startrev:
466 raise error.Abort(
466 raise error.Abort(
467 _('svn: no revision found after start revision %d')
467 _('svn: no revision found after start revision %d')
468 % self.startrev)
468 % self.startrev)
469
469
470 return self.heads
470 return self.heads
471
471
472 def _getchanges(self, rev, full):
472 def _getchanges(self, rev, full):
473 (paths, parents) = self.paths[rev]
473 (paths, parents) = self.paths[rev]
474 copies = {}
474 copies = {}
475 if parents:
475 if parents:
476 files, self.removed, copies = self.expandpaths(rev, paths, parents)
476 files, self.removed, copies = self.expandpaths(rev, paths, parents)
477 if full or not parents:
477 if full or not parents:
478 # Perform a full checkout on roots
478 # Perform a full checkout on roots
479 uuid, module, revnum = revsplit(rev)
479 uuid, module, revnum = revsplit(rev)
480 entries = svn.client.ls(self.baseurl + quote(module),
480 entries = svn.client.ls(self.baseurl + quote(module),
481 optrev(revnum), True, self.ctx)
481 optrev(revnum), True, self.ctx)
482 files = [n for n, e in entries.iteritems()
482 files = [n for n, e in entries.iteritems()
483 if e.kind == svn.core.svn_node_file]
483 if e.kind == svn.core.svn_node_file]
484 self.removed = set()
484 self.removed = set()
485
485
486 files.sort()
486 files.sort()
487 files = zip(files, [rev] * len(files))
487 files = zip(files, [rev] * len(files))
488 return (files, copies)
488 return (files, copies)
489
489
490 def getchanges(self, rev, full):
490 def getchanges(self, rev, full):
491 # reuse cache from getchangedfiles
491 # reuse cache from getchangedfiles
492 if self._changescache[0] == rev and not full:
492 if self._changescache[0] == rev and not full:
493 (files, copies) = self._changescache[1]
493 (files, copies) = self._changescache[1]
494 else:
494 else:
495 (files, copies) = self._getchanges(rev, full)
495 (files, copies) = self._getchanges(rev, full)
496 # caller caches the result, so free it here to release memory
496 # caller caches the result, so free it here to release memory
497 del self.paths[rev]
497 del self.paths[rev]
498 return (files, copies, set())
498 return (files, copies, set())
499
499
500 def getchangedfiles(self, rev, i):
500 def getchangedfiles(self, rev, i):
501 # called from filemap - cache computed values for reuse in getchanges
501 # called from filemap - cache computed values for reuse in getchanges
502 (files, copies) = self._getchanges(rev, False)
502 (files, copies) = self._getchanges(rev, False)
503 self._changescache = (rev, (files, copies))
503 self._changescache = (rev, (files, copies))
504 return [f[0] for f in files]
504 return [f[0] for f in files]
505
505
506 def getcommit(self, rev):
506 def getcommit(self, rev):
507 if rev not in self.commits:
507 if rev not in self.commits:
508 uuid, module, revnum = revsplit(rev)
508 uuid, module, revnum = revsplit(rev)
509 self.module = module
509 self.module = module
510 self.reparent(module)
510 self.reparent(module)
511 # We assume that:
511 # We assume that:
512 # - requests for revisions after "stop" come from the
512 # - requests for revisions after "stop" come from the
513 # revision graph backward traversal. Cache all of them
513 # revision graph backward traversal. Cache all of them
514 # down to stop, they will be used eventually.
514 # down to stop, they will be used eventually.
515 # - requests for revisions before "stop" come to get
515 # - requests for revisions before "stop" come to get
516 # isolated branches parents. Just fetch what is needed.
516 # isolated branches parents. Just fetch what is needed.
517 stop = self.lastrevs.get(module, 0)
517 stop = self.lastrevs.get(module, 0)
518 if revnum < stop:
518 if revnum < stop:
519 stop = revnum + 1
519 stop = revnum + 1
520 self._fetch_revisions(revnum, stop)
520 self._fetch_revisions(revnum, stop)
521 if rev not in self.commits:
521 if rev not in self.commits:
522 raise error.Abort(_('svn: revision %s not found') % revnum)
522 raise error.Abort(_('svn: revision %s not found') % revnum)
523 revcommit = self.commits[rev]
523 revcommit = self.commits[rev]
524 # caller caches the result, so free it here to release memory
524 # caller caches the result, so free it here to release memory
525 del self.commits[rev]
525 del self.commits[rev]
526 return revcommit
526 return revcommit
527
527
528 def checkrevformat(self, revstr, mapname='splicemap'):
528 def checkrevformat(self, revstr, mapname='splicemap'):
529 """ fails if revision format does not match the correct format"""
529 """ fails if revision format does not match the correct format"""
530 if not re.match(r'svn:[0-9a-f]{8,8}-[0-9a-f]{4,4}-'
530 if not re.match(r'svn:[0-9a-f]{8,8}-[0-9a-f]{4,4}-'
531 r'[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]'
531 r'[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]'
532 r'{12,12}(.*)\@[0-9]+$',revstr):
532 r'{12,12}(.*)\@[0-9]+$',revstr):
533 raise error.Abort(_('%s entry %s is not a valid revision'
533 raise error.Abort(_('%s entry %s is not a valid revision'
534 ' identifier') % (mapname, revstr))
534 ' identifier') % (mapname, revstr))
535
535
536 def numcommits(self):
536 def numcommits(self):
537 return int(self.head.rsplit('@', 1)[1]) - self.startrev
537 return int(self.head.rsplit('@', 1)[1]) - self.startrev
538
538
539 def gettags(self):
539 def gettags(self):
540 tags = {}
540 tags = {}
541 if self.tags is None:
541 if self.tags is None:
542 return tags
542 return tags
543
543
544 # svn tags are just a convention, project branches left in a
544 # svn tags are just a convention, project branches left in a
545 # 'tags' directory. There is no other relationship than
545 # 'tags' directory. There is no other relationship than
546 # ancestry, which is expensive to discover and makes them hard
546 # ancestry, which is expensive to discover and makes them hard
547 # to update incrementally. Worse, past revisions may be
547 # to update incrementally. Worse, past revisions may be
548 # referenced by tags far away in the future, requiring a deep
548 # referenced by tags far away in the future, requiring a deep
549 # history traversal on every calculation. Current code
549 # history traversal on every calculation. Current code
550 # performs a single backward traversal, tracking moves within
550 # performs a single backward traversal, tracking moves within
551 # the tags directory (tag renaming) and recording a new tag
551 # the tags directory (tag renaming) and recording a new tag
552 # everytime a project is copied from outside the tags
552 # everytime a project is copied from outside the tags
553 # directory. It also lists deleted tags, this behaviour may
553 # directory. It also lists deleted tags, this behaviour may
554 # change in the future.
554 # change in the future.
555 pendings = []
555 pendings = []
556 tagspath = self.tags
556 tagspath = self.tags
557 start = svn.ra.get_latest_revnum(self.ra)
557 start = svn.ra.get_latest_revnum(self.ra)
558 stream = self._getlog([self.tags], start, self.startrev)
558 stream = self._getlog([self.tags], start, self.startrev)
559 try:
559 try:
560 for entry in stream:
560 for entry in stream:
561 origpaths, revnum, author, date, message = entry
561 origpaths, revnum, author, date, message = entry
562 if not origpaths:
562 if not origpaths:
563 origpaths = []
563 origpaths = []
564 copies = [(e.copyfrom_path, e.copyfrom_rev, p) for p, e
564 copies = [(e.copyfrom_path, e.copyfrom_rev, p) for p, e
565 in origpaths.iteritems() if e.copyfrom_path]
565 in origpaths.iteritems() if e.copyfrom_path]
566 # Apply moves/copies from more specific to general
566 # Apply moves/copies from more specific to general
567 copies.sort(reverse=True)
567 copies.sort(reverse=True)
568
568
569 srctagspath = tagspath
569 srctagspath = tagspath
570 if copies and copies[-1][2] == tagspath:
570 if copies and copies[-1][2] == tagspath:
571 # Track tags directory moves
571 # Track tags directory moves
572 srctagspath = copies.pop()[0]
572 srctagspath = copies.pop()[0]
573
573
574 for source, sourcerev, dest in copies:
574 for source, sourcerev, dest in copies:
575 if not dest.startswith(tagspath + '/'):
575 if not dest.startswith(tagspath + '/'):
576 continue
576 continue
577 for tag in pendings:
577 for tag in pendings:
578 if tag[0].startswith(dest):
578 if tag[0].startswith(dest):
579 tagpath = source + tag[0][len(dest):]
579 tagpath = source + tag[0][len(dest):]
580 tag[:2] = [tagpath, sourcerev]
580 tag[:2] = [tagpath, sourcerev]
581 break
581 break
582 else:
582 else:
583 pendings.append([source, sourcerev, dest])
583 pendings.append([source, sourcerev, dest])
584
584
585 # Filter out tags with children coming from different
585 # Filter out tags with children coming from different
586 # parts of the repository like:
586 # parts of the repository like:
587 # /tags/tag.1 (from /trunk:10)
587 # /tags/tag.1 (from /trunk:10)
588 # /tags/tag.1/foo (from /branches/foo:12)
588 # /tags/tag.1/foo (from /branches/foo:12)
589 # Here/tags/tag.1 discarded as well as its children.
589 # Here/tags/tag.1 discarded as well as its children.
590 # It happens with tools like cvs2svn. Such tags cannot
590 # It happens with tools like cvs2svn. Such tags cannot
591 # be represented in mercurial.
591 # be represented in mercurial.
592 addeds = dict((p, e.copyfrom_path) for p, e
592 addeds = dict((p, e.copyfrom_path) for p, e
593 in origpaths.iteritems()
593 in origpaths.iteritems()
594 if e.action == 'A' and e.copyfrom_path)
594 if e.action == 'A' and e.copyfrom_path)
595 badroots = set()
595 badroots = set()
596 for destroot in addeds:
596 for destroot in addeds:
597 for source, sourcerev, dest in pendings:
597 for source, sourcerev, dest in pendings:
598 if (not dest.startswith(destroot + '/')
598 if (not dest.startswith(destroot + '/')
599 or source.startswith(addeds[destroot] + '/')):
599 or source.startswith(addeds[destroot] + '/')):
600 continue
600 continue
601 badroots.add(destroot)
601 badroots.add(destroot)
602 break
602 break
603
603
604 for badroot in badroots:
604 for badroot in badroots:
605 pendings = [p for p in pendings if p[2] != badroot
605 pendings = [p for p in pendings if p[2] != badroot
606 and not p[2].startswith(badroot + '/')]
606 and not p[2].startswith(badroot + '/')]
607
607
608 # Tell tag renamings from tag creations
608 # Tell tag renamings from tag creations
609 renamings = []
609 renamings = []
610 for source, sourcerev, dest in pendings:
610 for source, sourcerev, dest in pendings:
611 tagname = dest.split('/')[-1]
611 tagname = dest.split('/')[-1]
612 if source.startswith(srctagspath):
612 if source.startswith(srctagspath):
613 renamings.append([source, sourcerev, tagname])
613 renamings.append([source, sourcerev, tagname])
614 continue
614 continue
615 if tagname in tags:
615 if tagname in tags:
616 # Keep the latest tag value
616 # Keep the latest tag value
617 continue
617 continue
618 # From revision may be fake, get one with changes
618 # From revision may be fake, get one with changes
619 try:
619 try:
620 tagid = self.latest(source, sourcerev)
620 tagid = self.latest(source, sourcerev)
621 if tagid and tagname not in tags:
621 if tagid and tagname not in tags:
622 tags[tagname] = tagid
622 tags[tagname] = tagid
623 except SvnPathNotFound:
623 except SvnPathNotFound:
624 # It happens when we are following directories
624 # It happens when we are following directories
625 # we assumed were copied with their parents
625 # we assumed were copied with their parents
626 # but were really created in the tag
626 # but were really created in the tag
627 # directory.
627 # directory.
628 pass
628 pass
629 pendings = renamings
629 pendings = renamings
630 tagspath = srctagspath
630 tagspath = srctagspath
631 finally:
631 finally:
632 stream.close()
632 stream.close()
633 return tags
633 return tags
634
634
635 def converted(self, rev, destrev):
635 def converted(self, rev, destrev):
636 if not self.wc:
636 if not self.wc:
637 return
637 return
638 if self.convertfp is None:
638 if self.convertfp is None:
639 self.convertfp = open(os.path.join(self.wc, '.svn', 'hg-shamap'),
639 self.convertfp = open(os.path.join(self.wc, '.svn', 'hg-shamap'),
640 'a')
640 'a')
641 self.convertfp.write('%s %d\n' % (destrev, self.revnum(rev)))
641 self.convertfp.write('%s %d\n' % (destrev, self.revnum(rev)))
642 self.convertfp.flush()
642 self.convertfp.flush()
643
643
644 def revid(self, revnum, module=None):
644 def revid(self, revnum, module=None):
645 return 'svn:%s%s@%s' % (self.uuid, module or self.module, revnum)
645 return 'svn:%s%s@%s' % (self.uuid, module or self.module, revnum)
646
646
647 def revnum(self, rev):
647 def revnum(self, rev):
648 return int(rev.split('@')[-1])
648 return int(rev.split('@')[-1])
649
649
650 def latest(self, path, stop=None):
650 def latest(self, path, stop=None):
651 """Find the latest revid affecting path, up to stop revision
651 """Find the latest revid affecting path, up to stop revision
652 number. If stop is None, default to repository latest
652 number. If stop is None, default to repository latest
653 revision. It may return a revision in a different module,
653 revision. It may return a revision in a different module,
654 since a branch may be moved without a change being
654 since a branch may be moved without a change being
655 reported. Return None if computed module does not belong to
655 reported. Return None if computed module does not belong to
656 rootmodule subtree.
656 rootmodule subtree.
657 """
657 """
658 def findchanges(path, start, stop=None):
658 def findchanges(path, start, stop=None):
659 stream = self._getlog([path], start, stop or 1)
659 stream = self._getlog([path], start, stop or 1)
660 try:
660 try:
661 for entry in stream:
661 for entry in stream:
662 paths, revnum, author, date, message = entry
662 paths, revnum, author, date, message = entry
663 if stop is None and paths:
663 if stop is None and paths:
664 # We do not know the latest changed revision,
664 # We do not know the latest changed revision,
665 # keep the first one with changed paths.
665 # keep the first one with changed paths.
666 break
666 break
667 if revnum <= stop:
667 if revnum <= stop:
668 break
668 break
669
669
670 for p in paths:
670 for p in paths:
671 if (not path.startswith(p) or
671 if (not path.startswith(p) or
672 not paths[p].copyfrom_path):
672 not paths[p].copyfrom_path):
673 continue
673 continue
674 newpath = paths[p].copyfrom_path + path[len(p):]
674 newpath = paths[p].copyfrom_path + path[len(p):]
675 self.ui.debug("branch renamed from %s to %s at %d\n" %
675 self.ui.debug("branch renamed from %s to %s at %d\n" %
676 (path, newpath, revnum))
676 (path, newpath, revnum))
677 path = newpath
677 path = newpath
678 break
678 break
679 if not paths:
679 if not paths:
680 revnum = None
680 revnum = None
681 return revnum, path
681 return revnum, path
682 finally:
682 finally:
683 stream.close()
683 stream.close()
684
684
685 if not path.startswith(self.rootmodule):
685 if not path.startswith(self.rootmodule):
686 # Requests on foreign branches may be forbidden at server level
686 # Requests on foreign branches may be forbidden at server level
687 self.ui.debug('ignoring foreign branch %r\n' % path)
687 self.ui.debug('ignoring foreign branch %r\n' % path)
688 return None
688 return None
689
689
690 if stop is None:
690 if stop is None:
691 stop = svn.ra.get_latest_revnum(self.ra)
691 stop = svn.ra.get_latest_revnum(self.ra)
692 try:
692 try:
693 prevmodule = self.reparent('')
693 prevmodule = self.reparent('')
694 dirent = svn.ra.stat(self.ra, path.strip('/'), stop)
694 dirent = svn.ra.stat(self.ra, path.strip('/'), stop)
695 self.reparent(prevmodule)
695 self.reparent(prevmodule)
696 except svn.core.SubversionException:
696 except svn.core.SubversionException:
697 dirent = None
697 dirent = None
698 if not dirent:
698 if not dirent:
699 raise SvnPathNotFound(_('%s not found up to revision %d')
699 raise SvnPathNotFound(_('%s not found up to revision %d')
700 % (path, stop))
700 % (path, stop))
701
701
702 # stat() gives us the previous revision on this line of
702 # stat() gives us the previous revision on this line of
703 # development, but it might be in *another module*. Fetch the
703 # development, but it might be in *another module*. Fetch the
704 # log and detect renames down to the latest revision.
704 # log and detect renames down to the latest revision.
705 revnum, realpath = findchanges(path, stop, dirent.created_rev)
705 revnum, realpath = findchanges(path, stop, dirent.created_rev)
706 if revnum is None:
706 if revnum is None:
707 # Tools like svnsync can create empty revision, when
707 # Tools like svnsync can create empty revision, when
708 # synchronizing only a subtree for instance. These empty
708 # synchronizing only a subtree for instance. These empty
709 # revisions created_rev still have their original values
709 # revisions created_rev still have their original values
710 # despite all changes having disappeared and can be
710 # despite all changes having disappeared and can be
711 # returned by ra.stat(), at least when stating the root
711 # returned by ra.stat(), at least when stating the root
712 # module. In that case, do not trust created_rev and scan
712 # module. In that case, do not trust created_rev and scan
713 # the whole history.
713 # the whole history.
714 revnum, realpath = findchanges(path, stop)
714 revnum, realpath = findchanges(path, stop)
715 if revnum is None:
715 if revnum is None:
716 self.ui.debug('ignoring empty branch %r\n' % realpath)
716 self.ui.debug('ignoring empty branch %r\n' % realpath)
717 return None
717 return None
718
718
719 if not realpath.startswith(self.rootmodule):
719 if not realpath.startswith(self.rootmodule):
720 self.ui.debug('ignoring foreign branch %r\n' % realpath)
720 self.ui.debug('ignoring foreign branch %r\n' % realpath)
721 return None
721 return None
722 return self.revid(revnum, realpath)
722 return self.revid(revnum, realpath)
723
723
724 def reparent(self, module):
724 def reparent(self, module):
725 """Reparent the svn transport and return the previous parent."""
725 """Reparent the svn transport and return the previous parent."""
726 if self.prevmodule == module:
726 if self.prevmodule == module:
727 return module
727 return module
728 svnurl = self.baseurl + quote(module)
728 svnurl = self.baseurl + quote(module)
729 prevmodule = self.prevmodule
729 prevmodule = self.prevmodule
730 if prevmodule is None:
730 if prevmodule is None:
731 prevmodule = ''
731 prevmodule = ''
732 self.ui.debug("reparent to %s\n" % svnurl)
732 self.ui.debug("reparent to %s\n" % svnurl)
733 svn.ra.reparent(self.ra, svnurl)
733 svn.ra.reparent(self.ra, svnurl)
734 self.prevmodule = module
734 self.prevmodule = module
735 return prevmodule
735 return prevmodule
736
736
737 def expandpaths(self, rev, paths, parents):
737 def expandpaths(self, rev, paths, parents):
738 changed, removed = set(), set()
738 changed, removed = set(), set()
739 copies = {}
739 copies = {}
740
740
741 new_module, revnum = revsplit(rev)[1:]
741 new_module, revnum = revsplit(rev)[1:]
742 if new_module != self.module:
742 if new_module != self.module:
743 self.module = new_module
743 self.module = new_module
744 self.reparent(self.module)
744 self.reparent(self.module)
745
745
746 for i, (path, ent) in enumerate(paths):
746 for i, (path, ent) in enumerate(paths):
747 self.ui.progress(_('scanning paths'), i, item=path,
747 self.ui.progress(_('scanning paths'), i, item=path,
748 total=len(paths), unit=_('paths'))
748 total=len(paths), unit=_('paths'))
749 entrypath = self.getrelpath(path)
749 entrypath = self.getrelpath(path)
750
750
751 kind = self._checkpath(entrypath, revnum)
751 kind = self._checkpath(entrypath, revnum)
752 if kind == svn.core.svn_node_file:
752 if kind == svn.core.svn_node_file:
753 changed.add(self.recode(entrypath))
753 changed.add(self.recode(entrypath))
754 if not ent.copyfrom_path or not parents:
754 if not ent.copyfrom_path or not parents:
755 continue
755 continue
756 # Copy sources not in parent revisions cannot be
756 # Copy sources not in parent revisions cannot be
757 # represented, ignore their origin for now
757 # represented, ignore their origin for now
758 pmodule, prevnum = revsplit(parents[0])[1:]
758 pmodule, prevnum = revsplit(parents[0])[1:]
759 if ent.copyfrom_rev < prevnum:
759 if ent.copyfrom_rev < prevnum:
760 continue
760 continue
761 copyfrom_path = self.getrelpath(ent.copyfrom_path, pmodule)
761 copyfrom_path = self.getrelpath(ent.copyfrom_path, pmodule)
762 if not copyfrom_path:
762 if not copyfrom_path:
763 continue
763 continue
764 self.ui.debug("copied to %s from %s@%s\n" %
764 self.ui.debug("copied to %s from %s@%s\n" %
765 (entrypath, copyfrom_path, ent.copyfrom_rev))
765 (entrypath, copyfrom_path, ent.copyfrom_rev))
766 copies[self.recode(entrypath)] = self.recode(copyfrom_path)
766 copies[self.recode(entrypath)] = self.recode(copyfrom_path)
767 elif kind == 0: # gone, but had better be a deleted *file*
767 elif kind == 0: # gone, but had better be a deleted *file*
768 self.ui.debug("gone from %s\n" % ent.copyfrom_rev)
768 self.ui.debug("gone from %s\n" % ent.copyfrom_rev)
769 pmodule, prevnum = revsplit(parents[0])[1:]
769 pmodule, prevnum = revsplit(parents[0])[1:]
770 parentpath = pmodule + "/" + entrypath
770 parentpath = pmodule + "/" + entrypath
771 fromkind = self._checkpath(entrypath, prevnum, pmodule)
771 fromkind = self._checkpath(entrypath, prevnum, pmodule)
772
772
773 if fromkind == svn.core.svn_node_file:
773 if fromkind == svn.core.svn_node_file:
774 removed.add(self.recode(entrypath))
774 removed.add(self.recode(entrypath))
775 elif fromkind == svn.core.svn_node_dir:
775 elif fromkind == svn.core.svn_node_dir:
776 oroot = parentpath.strip('/')
776 oroot = parentpath.strip('/')
777 nroot = path.strip('/')
777 nroot = path.strip('/')
778 children = self._iterfiles(oroot, prevnum)
778 children = self._iterfiles(oroot, prevnum)
779 for childpath in children:
779 for childpath in children:
780 childpath = childpath.replace(oroot, nroot)
780 childpath = childpath.replace(oroot, nroot)
781 childpath = self.getrelpath("/" + childpath, pmodule)
781 childpath = self.getrelpath("/" + childpath, pmodule)
782 if childpath:
782 if childpath:
783 removed.add(self.recode(childpath))
783 removed.add(self.recode(childpath))
784 else:
784 else:
785 self.ui.debug('unknown path in revision %d: %s\n' % \
785 self.ui.debug('unknown path in revision %d: %s\n' % \
786 (revnum, path))
786 (revnum, path))
787 elif kind == svn.core.svn_node_dir:
787 elif kind == svn.core.svn_node_dir:
788 if ent.action == 'M':
788 if ent.action == 'M':
789 # If the directory just had a prop change,
789 # If the directory just had a prop change,
790 # then we shouldn't need to look for its children.
790 # then we shouldn't need to look for its children.
791 continue
791 continue
792 if ent.action == 'R' and parents:
792 if ent.action == 'R' and parents:
793 # If a directory is replacing a file, mark the previous
793 # If a directory is replacing a file, mark the previous
794 # file as deleted
794 # file as deleted
795 pmodule, prevnum = revsplit(parents[0])[1:]
795 pmodule, prevnum = revsplit(parents[0])[1:]
796 pkind = self._checkpath(entrypath, prevnum, pmodule)
796 pkind = self._checkpath(entrypath, prevnum, pmodule)
797 if pkind == svn.core.svn_node_file:
797 if pkind == svn.core.svn_node_file:
798 removed.add(self.recode(entrypath))
798 removed.add(self.recode(entrypath))
799 elif pkind == svn.core.svn_node_dir:
799 elif pkind == svn.core.svn_node_dir:
800 # We do not know what files were kept or removed,
800 # We do not know what files were kept or removed,
801 # mark them all as changed.
801 # mark them all as changed.
802 for childpath in self._iterfiles(pmodule, prevnum):
802 for childpath in self._iterfiles(pmodule, prevnum):
803 childpath = self.getrelpath("/" + childpath)
803 childpath = self.getrelpath("/" + childpath)
804 if childpath:
804 if childpath:
805 changed.add(self.recode(childpath))
805 changed.add(self.recode(childpath))
806
806
807 for childpath in self._iterfiles(path, revnum):
807 for childpath in self._iterfiles(path, revnum):
808 childpath = self.getrelpath("/" + childpath)
808 childpath = self.getrelpath("/" + childpath)
809 if childpath:
809 if childpath:
810 changed.add(self.recode(childpath))
810 changed.add(self.recode(childpath))
811
811
812 # Handle directory copies
812 # Handle directory copies
813 if not ent.copyfrom_path or not parents:
813 if not ent.copyfrom_path or not parents:
814 continue
814 continue
815 # Copy sources not in parent revisions cannot be
815 # Copy sources not in parent revisions cannot be
816 # represented, ignore their origin for now
816 # represented, ignore their origin for now
817 pmodule, prevnum = revsplit(parents[0])[1:]
817 pmodule, prevnum = revsplit(parents[0])[1:]
818 if ent.copyfrom_rev < prevnum:
818 if ent.copyfrom_rev < prevnum:
819 continue
819 continue
820 copyfrompath = self.getrelpath(ent.copyfrom_path, pmodule)
820 copyfrompath = self.getrelpath(ent.copyfrom_path, pmodule)
821 if not copyfrompath:
821 if not copyfrompath:
822 continue
822 continue
823 self.ui.debug("mark %s came from %s:%d\n"
823 self.ui.debug("mark %s came from %s:%d\n"
824 % (path, copyfrompath, ent.copyfrom_rev))
824 % (path, copyfrompath, ent.copyfrom_rev))
825 children = self._iterfiles(ent.copyfrom_path, ent.copyfrom_rev)
825 children = self._iterfiles(ent.copyfrom_path, ent.copyfrom_rev)
826 for childpath in children:
826 for childpath in children:
827 childpath = self.getrelpath("/" + childpath, pmodule)
827 childpath = self.getrelpath("/" + childpath, pmodule)
828 if not childpath:
828 if not childpath:
829 continue
829 continue
830 copytopath = path + childpath[len(copyfrompath):]
830 copytopath = path + childpath[len(copyfrompath):]
831 copytopath = self.getrelpath(copytopath)
831 copytopath = self.getrelpath(copytopath)
832 copies[self.recode(copytopath)] = self.recode(childpath)
832 copies[self.recode(copytopath)] = self.recode(childpath)
833
833
834 self.ui.progress(_('scanning paths'), None)
834 self.ui.progress(_('scanning paths'), None)
835 changed.update(removed)
835 changed.update(removed)
836 return (list(changed), removed, copies)
836 return (list(changed), removed, copies)
837
837
838 def _fetch_revisions(self, from_revnum, to_revnum):
838 def _fetch_revisions(self, from_revnum, to_revnum):
839 if from_revnum < to_revnum:
839 if from_revnum < to_revnum:
840 from_revnum, to_revnum = to_revnum, from_revnum
840 from_revnum, to_revnum = to_revnum, from_revnum
841
841
842 self.child_cset = None
842 self.child_cset = None
843
843
844 def parselogentry(orig_paths, revnum, author, date, message):
844 def parselogentry(orig_paths, revnum, author, date, message):
845 """Return the parsed commit object or None, and True if
845 """Return the parsed commit object or None, and True if
846 the revision is a branch root.
846 the revision is a branch root.
847 """
847 """
848 self.ui.debug("parsing revision %d (%d changes)\n" %
848 self.ui.debug("parsing revision %d (%d changes)\n" %
849 (revnum, len(orig_paths)))
849 (revnum, len(orig_paths)))
850
850
851 branched = False
851 branched = False
852 rev = self.revid(revnum)
852 rev = self.revid(revnum)
853 # branch log might return entries for a parent we already have
853 # branch log might return entries for a parent we already have
854
854
855 if rev in self.commits or revnum < to_revnum:
855 if rev in self.commits or revnum < to_revnum:
856 return None, branched
856 return None, branched
857
857
858 parents = []
858 parents = []
859 # check whether this revision is the start of a branch or part
859 # check whether this revision is the start of a branch or part
860 # of a branch renaming
860 # of a branch renaming
861 orig_paths = sorted(orig_paths.iteritems())
861 orig_paths = sorted(orig_paths.iteritems())
862 root_paths = [(p, e) for p, e in orig_paths
862 root_paths = [(p, e) for p, e in orig_paths
863 if self.module.startswith(p)]
863 if self.module.startswith(p)]
864 if root_paths:
864 if root_paths:
865 path, ent = root_paths[-1]
865 path, ent = root_paths[-1]
866 if ent.copyfrom_path:
866 if ent.copyfrom_path:
867 branched = True
867 branched = True
868 newpath = ent.copyfrom_path + self.module[len(path):]
868 newpath = ent.copyfrom_path + self.module[len(path):]
869 # ent.copyfrom_rev may not be the actual last revision
869 # ent.copyfrom_rev may not be the actual last revision
870 previd = self.latest(newpath, ent.copyfrom_rev)
870 previd = self.latest(newpath, ent.copyfrom_rev)
871 if previd is not None:
871 if previd is not None:
872 prevmodule, prevnum = revsplit(previd)[1:]
872 prevmodule, prevnum = revsplit(previd)[1:]
873 if prevnum >= self.startrev:
873 if prevnum >= self.startrev:
874 parents = [previd]
874 parents = [previd]
875 self.ui.note(
875 self.ui.note(
876 _('found parent of branch %s at %d: %s\n') %
876 _('found parent of branch %s at %d: %s\n') %
877 (self.module, prevnum, prevmodule))
877 (self.module, prevnum, prevmodule))
878 else:
878 else:
879 self.ui.debug("no copyfrom path, don't know what to do.\n")
879 self.ui.debug("no copyfrom path, don't know what to do.\n")
880
880
881 paths = []
881 paths = []
882 # filter out unrelated paths
882 # filter out unrelated paths
883 for path, ent in orig_paths:
883 for path, ent in orig_paths:
884 if self.getrelpath(path) is None:
884 if self.getrelpath(path) is None:
885 continue
885 continue
886 paths.append((path, ent))
886 paths.append((path, ent))
887
887
888 # Example SVN datetime. Includes microseconds.
888 # Example SVN datetime. Includes microseconds.
889 # ISO-8601 conformant
889 # ISO-8601 conformant
890 # '2007-01-04T17:35:00.902377Z'
890 # '2007-01-04T17:35:00.902377Z'
891 date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
891 date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
892 if self.ui.configbool('convert', 'localtimezone'):
892 if self.ui.configbool('convert', 'localtimezone'):
893 date = makedatetimestamp(date[0])
893 date = makedatetimestamp(date[0])
894
894
895 if message:
895 if message:
896 log = self.recode(message)
896 log = self.recode(message)
897 else:
897 else:
898 log = ''
898 log = ''
899
899
900 if author:
900 if author:
901 author = self.recode(author)
901 author = self.recode(author)
902 else:
902 else:
903 author = ''
903 author = ''
904
904
905 try:
905 try:
906 branch = self.module.split("/")[-1]
906 branch = self.module.split("/")[-1]
907 if branch == self.trunkname:
907 if branch == self.trunkname:
908 branch = None
908 branch = None
909 except IndexError:
909 except IndexError:
910 branch = None
910 branch = None
911
911
912 cset = commit(author=author,
912 cset = commit(author=author,
913 date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'),
913 date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'),
914 desc=log,
914 desc=log,
915 parents=parents,
915 parents=parents,
916 branch=branch,
916 branch=branch,
917 rev=rev)
917 rev=rev)
918
918
919 self.commits[rev] = cset
919 self.commits[rev] = cset
920 # The parents list is *shared* among self.paths and the
920 # The parents list is *shared* among self.paths and the
921 # commit object. Both will be updated below.
921 # commit object. Both will be updated below.
922 self.paths[rev] = (paths, cset.parents)
922 self.paths[rev] = (paths, cset.parents)
923 if self.child_cset and not self.child_cset.parents:
923 if self.child_cset and not self.child_cset.parents:
924 self.child_cset.parents[:] = [rev]
924 self.child_cset.parents[:] = [rev]
925 self.child_cset = cset
925 self.child_cset = cset
926 return cset, branched
926 return cset, branched
927
927
928 self.ui.note(_('fetching revision log for "%s" from %d to %d\n') %
928 self.ui.note(_('fetching revision log for "%s" from %d to %d\n') %
929 (self.module, from_revnum, to_revnum))
929 (self.module, from_revnum, to_revnum))
930
930
931 try:
931 try:
932 firstcset = None
932 firstcset = None
933 lastonbranch = False
933 lastonbranch = False
934 stream = self._getlog([self.module], from_revnum, to_revnum)
934 stream = self._getlog([self.module], from_revnum, to_revnum)
935 try:
935 try:
936 for entry in stream:
936 for entry in stream:
937 paths, revnum, author, date, message = entry
937 paths, revnum, author, date, message = entry
938 if revnum < self.startrev:
938 if revnum < self.startrev:
939 lastonbranch = True
939 lastonbranch = True
940 break
940 break
941 if not paths:
941 if not paths:
942 self.ui.debug('revision %d has no entries\n' % revnum)
942 self.ui.debug('revision %d has no entries\n' % revnum)
943 # If we ever leave the loop on an empty
943 # If we ever leave the loop on an empty
944 # revision, do not try to get a parent branch
944 # revision, do not try to get a parent branch
945 lastonbranch = lastonbranch or revnum == 0
945 lastonbranch = lastonbranch or revnum == 0
946 continue
946 continue
947 cset, lastonbranch = parselogentry(paths, revnum, author,
947 cset, lastonbranch = parselogentry(paths, revnum, author,
948 date, message)
948 date, message)
949 if cset:
949 if cset:
950 firstcset = cset
950 firstcset = cset
951 if lastonbranch:
951 if lastonbranch:
952 break
952 break
953 finally:
953 finally:
954 stream.close()
954 stream.close()
955
955
956 if not lastonbranch and firstcset and not firstcset.parents:
956 if not lastonbranch and firstcset and not firstcset.parents:
957 # The first revision of the sequence (the last fetched one)
957 # The first revision of the sequence (the last fetched one)
958 # has invalid parents if not a branch root. Find the parent
958 # has invalid parents if not a branch root. Find the parent
959 # revision now, if any.
959 # revision now, if any.
960 try:
960 try:
961 firstrevnum = self.revnum(firstcset.rev)
961 firstrevnum = self.revnum(firstcset.rev)
962 if firstrevnum > 1:
962 if firstrevnum > 1:
963 latest = self.latest(self.module, firstrevnum - 1)
963 latest = self.latest(self.module, firstrevnum - 1)
964 if latest:
964 if latest:
965 firstcset.parents.append(latest)
965 firstcset.parents.append(latest)
966 except SvnPathNotFound:
966 except SvnPathNotFound:
967 pass
967 pass
968 except svn.core.SubversionException as xxx_todo_changeme:
968 except svn.core.SubversionException as xxx_todo_changeme:
969 (inst, num) = xxx_todo_changeme.args
969 (inst, num) = xxx_todo_changeme.args
970 if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION:
970 if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION:
971 raise error.Abort(_('svn: branch has no revision %s')
971 raise error.Abort(_('svn: branch has no revision %s')
972 % to_revnum)
972 % to_revnum)
973 raise
973 raise
974
974
975 def getfile(self, file, rev):
975 def getfile(self, file, rev):
976 # TODO: ra.get_file transmits the whole file instead of diffs.
976 # TODO: ra.get_file transmits the whole file instead of diffs.
977 if file in self.removed:
977 if file in self.removed:
978 return None, None
978 return None, None
979 mode = ''
979 mode = ''
980 try:
980 try:
981 new_module, revnum = revsplit(rev)[1:]
981 new_module, revnum = revsplit(rev)[1:]
982 if self.module != new_module:
982 if self.module != new_module:
983 self.module = new_module
983 self.module = new_module
984 self.reparent(self.module)
984 self.reparent(self.module)
985 io = stringio()
985 io = stringio()
986 info = svn.ra.get_file(self.ra, file, revnum, io)
986 info = svn.ra.get_file(self.ra, file, revnum, io)
987 data = io.getvalue()
987 data = io.getvalue()
988 # ra.get_file() seems to keep a reference on the input buffer
988 # ra.get_file() seems to keep a reference on the input buffer
989 # preventing collection. Release it explicitly.
989 # preventing collection. Release it explicitly.
990 io.close()
990 io.close()
991 if isinstance(info, list):
991 if isinstance(info, list):
992 info = info[-1]
992 info = info[-1]
993 mode = ("svn:executable" in info) and 'x' or ''
993 mode = ("svn:executable" in info) and 'x' or ''
994 mode = ("svn:special" in info) and 'l' or mode
994 mode = ("svn:special" in info) and 'l' or mode
995 except svn.core.SubversionException as e:
995 except svn.core.SubversionException as e:
996 notfound = (svn.core.SVN_ERR_FS_NOT_FOUND,
996 notfound = (svn.core.SVN_ERR_FS_NOT_FOUND,
997 svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND)
997 svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND)
998 if e.apr_err in notfound: # File not found
998 if e.apr_err in notfound: # File not found
999 return None, None
999 return None, None
1000 raise
1000 raise
1001 if mode == 'l':
1001 if mode == 'l':
1002 link_prefix = "link "
1002 link_prefix = "link "
1003 if data.startswith(link_prefix):
1003 if data.startswith(link_prefix):
1004 data = data[len(link_prefix):]
1004 data = data[len(link_prefix):]
1005 return data, mode
1005 return data, mode
1006
1006
1007 def _iterfiles(self, path, revnum):
1007 def _iterfiles(self, path, revnum):
1008 """Enumerate all files in path at revnum, recursively."""
1008 """Enumerate all files in path at revnum, recursively."""
1009 path = path.strip('/')
1009 path = path.strip('/')
1010 pool = svn.core.Pool()
1010 pool = svn.core.Pool()
1011 rpath = '/'.join([self.baseurl, quote(path)]).strip('/')
1011 rpath = '/'.join([self.baseurl, quote(path)]).strip('/')
1012 entries = svn.client.ls(rpath, optrev(revnum), True, self.ctx, pool)
1012 entries = svn.client.ls(rpath, optrev(revnum), True, self.ctx, pool)
1013 if path:
1013 if path:
1014 path += '/'
1014 path += '/'
1015 return ((path + p) for p, e in entries.iteritems()
1015 return ((path + p) for p, e in entries.iteritems()
1016 if e.kind == svn.core.svn_node_file)
1016 if e.kind == svn.core.svn_node_file)
1017
1017
1018 def getrelpath(self, path, module=None):
1018 def getrelpath(self, path, module=None):
1019 if module is None:
1019 if module is None:
1020 module = self.module
1020 module = self.module
1021 # Given the repository url of this wc, say
1021 # Given the repository url of this wc, say
1022 # "http://server/plone/CMFPlone/branches/Plone-2_0-branch"
1022 # "http://server/plone/CMFPlone/branches/Plone-2_0-branch"
1023 # extract the "entry" portion (a relative path) from what
1023 # extract the "entry" portion (a relative path) from what
1024 # svn log --xml says, i.e.
1024 # svn log --xml says, i.e.
1025 # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py"
1025 # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py"
1026 # that is to say "tests/PloneTestCase.py"
1026 # that is to say "tests/PloneTestCase.py"
1027 if path.startswith(module):
1027 if path.startswith(module):
1028 relative = path.rstrip('/')[len(module):]
1028 relative = path.rstrip('/')[len(module):]
1029 if relative.startswith('/'):
1029 if relative.startswith('/'):
1030 return relative[1:]
1030 return relative[1:]
1031 elif relative == '':
1031 elif relative == '':
1032 return relative
1032 return relative
1033
1033
1034 # The path is outside our tracked tree...
1034 # The path is outside our tracked tree...
1035 self.ui.debug('%r is not under %r, ignoring\n' % (path, module))
1035 self.ui.debug('%r is not under %r, ignoring\n' % (path, module))
1036 return None
1036 return None
1037
1037
1038 def _checkpath(self, path, revnum, module=None):
1038 def _checkpath(self, path, revnum, module=None):
1039 if module is not None:
1039 if module is not None:
1040 prevmodule = self.reparent('')
1040 prevmodule = self.reparent('')
1041 path = module + '/' + path
1041 path = module + '/' + path
1042 try:
1042 try:
1043 # ra.check_path does not like leading slashes very much, it leads
1043 # ra.check_path does not like leading slashes very much, it leads
1044 # to PROPFIND subversion errors
1044 # to PROPFIND subversion errors
1045 return svn.ra.check_path(self.ra, path.strip('/'), revnum)
1045 return svn.ra.check_path(self.ra, path.strip('/'), revnum)
1046 finally:
1046 finally:
1047 if module is not None:
1047 if module is not None:
1048 self.reparent(prevmodule)
1048 self.reparent(prevmodule)
1049
1049
1050 def _getlog(self, paths, start, end, limit=0, discover_changed_paths=True,
1050 def _getlog(self, paths, start, end, limit=0, discover_changed_paths=True,
1051 strict_node_history=False):
1051 strict_node_history=False):
1052 # Normalize path names, svn >= 1.5 only wants paths relative to
1052 # Normalize path names, svn >= 1.5 only wants paths relative to
1053 # supplied URL
1053 # supplied URL
1054 relpaths = []
1054 relpaths = []
1055 for p in paths:
1055 for p in paths:
1056 if not p.startswith('/'):
1056 if not p.startswith('/'):
1057 p = self.module + '/' + p
1057 p = self.module + '/' + p
1058 relpaths.append(p.strip('/'))
1058 relpaths.append(p.strip('/'))
1059 args = [self.baseurl, relpaths, start, end, limit,
1059 args = [self.baseurl, relpaths, start, end, limit,
1060 discover_changed_paths, strict_node_history]
1060 discover_changed_paths, strict_node_history]
1061 # developer config: convert.svn.debugsvnlog
1061 # developer config: convert.svn.debugsvnlog
1062 if not self.ui.configbool('convert', 'svn.debugsvnlog', True):
1062 if not self.ui.configbool('convert', 'svn.debugsvnlog', True):
1063 return directlogstream(*args)
1063 return directlogstream(*args)
1064 arg = encodeargs(args)
1064 arg = encodeargs(args)
1065 hgexe = util.hgexecutable()
1065 hgexe = util.hgexecutable()
1066 cmd = '%s debugsvnlog' % util.shellquote(hgexe)
1066 cmd = '%s debugsvnlog' % util.shellquote(hgexe)
1067 stdin, stdout = util.popen2(util.quotecommand(cmd))
1067 stdin, stdout = util.popen2(util.quotecommand(cmd))
1068 stdin.write(arg)
1068 stdin.write(arg)
1069 try:
1069 try:
1070 stdin.close()
1070 stdin.close()
1071 except IOError:
1071 except IOError:
1072 raise error.Abort(_('Mercurial failed to run itself, check'
1072 raise error.Abort(_('Mercurial failed to run itself, check'
1073 ' hg executable is in PATH'))
1073 ' hg executable is in PATH'))
1074 return logstream(stdout)
1074 return logstream(stdout)
1075
1075
1076 pre_revprop_change = '''#!/bin/sh
1076 pre_revprop_change = '''#!/bin/sh
1077
1077
1078 REPOS="$1"
1078 REPOS="$1"
1079 REV="$2"
1079 REV="$2"
1080 USER="$3"
1080 USER="$3"
1081 PROPNAME="$4"
1081 PROPNAME="$4"
1082 ACTION="$5"
1082 ACTION="$5"
1083
1083
1084 if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi
1084 if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi
1085 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-branch" ]; then exit 0; fi
1085 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-branch" ]; then exit 0; fi
1086 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-rev" ]; then exit 0; fi
1086 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-rev" ]; then exit 0; fi
1087
1087
1088 echo "Changing prohibited revision property" >&2
1088 echo "Changing prohibited revision property" >&2
1089 exit 1
1089 exit 1
1090 '''
1090 '''
1091
1091
1092 class svn_sink(converter_sink, commandline):
1092 class svn_sink(converter_sink, commandline):
1093 commit_re = re.compile(r'Committed revision (\d+).', re.M)
1093 commit_re = re.compile(r'Committed revision (\d+).', re.M)
1094 uuid_re = re.compile(r'Repository UUID:\s*(\S+)', re.M)
1094 uuid_re = re.compile(r'Repository UUID:\s*(\S+)', re.M)
1095
1095
1096 def prerun(self):
1096 def prerun(self):
1097 if self.wc:
1097 if self.wc:
1098 os.chdir(self.wc)
1098 os.chdir(self.wc)
1099
1099
1100 def postrun(self):
1100 def postrun(self):
1101 if self.wc:
1101 if self.wc:
1102 os.chdir(self.cwd)
1102 os.chdir(self.cwd)
1103
1103
1104 def join(self, name):
1104 def join(self, name):
1105 return os.path.join(self.wc, '.svn', name)
1105 return os.path.join(self.wc, '.svn', name)
1106
1106
1107 def revmapfile(self):
1107 def revmapfile(self):
1108 return self.join('hg-shamap')
1108 return self.join('hg-shamap')
1109
1109
1110 def authorfile(self):
1110 def authorfile(self):
1111 return self.join('hg-authormap')
1111 return self.join('hg-authormap')
1112
1112
1113 def __init__(self, ui, path):
1113 def __init__(self, ui, path):
1114
1114
1115 converter_sink.__init__(self, ui, path)
1115 converter_sink.__init__(self, ui, path)
1116 commandline.__init__(self, ui, 'svn')
1116 commandline.__init__(self, ui, 'svn')
1117 self.delete = []
1117 self.delete = []
1118 self.setexec = []
1118 self.setexec = []
1119 self.delexec = []
1119 self.delexec = []
1120 self.copies = []
1120 self.copies = []
1121 self.wc = None
1121 self.wc = None
1122 self.cwd = pycompat.getcwd()
1122 self.cwd = pycompat.getcwd()
1123
1123
1124 created = False
1124 created = False
1125 if os.path.isfile(os.path.join(path, '.svn', 'entries')):
1125 if os.path.isfile(os.path.join(path, '.svn', 'entries')):
1126 self.wc = os.path.realpath(path)
1126 self.wc = os.path.realpath(path)
1127 self.run0('update')
1127 self.run0('update')
1128 else:
1128 else:
1129 if not re.search(r'^(file|http|https|svn|svn\+ssh)\://', path):
1129 if not re.search(r'^(file|http|https|svn|svn\+ssh)\://', path):
1130 path = os.path.realpath(path)
1130 path = os.path.realpath(path)
1131 if os.path.isdir(os.path.dirname(path)):
1131 if os.path.isdir(os.path.dirname(path)):
1132 if not os.path.exists(os.path.join(path, 'db', 'fs-type')):
1132 if not os.path.exists(os.path.join(path, 'db', 'fs-type')):
1133 ui.status(_('initializing svn repository %r\n') %
1133 ui.status(_('initializing svn repository %r\n') %
1134 os.path.basename(path))
1134 os.path.basename(path))
1135 commandline(ui, 'svnadmin').run0('create', path)
1135 commandline(ui, 'svnadmin').run0('create', path)
1136 created = path
1136 created = path
1137 path = util.normpath(path)
1137 path = util.normpath(path)
1138 if not path.startswith('/'):
1138 if not path.startswith('/'):
1139 path = '/' + path
1139 path = '/' + path
1140 path = 'file://' + path
1140 path = 'file://' + path
1141
1141
1142 wcpath = os.path.join(pycompat.getcwd(), os.path.basename(path) +
1142 wcpath = os.path.join(pycompat.getcwd(), os.path.basename(path) +
1143 '-wc')
1143 '-wc')
1144 ui.status(_('initializing svn working copy %r\n')
1144 ui.status(_('initializing svn working copy %r\n')
1145 % os.path.basename(wcpath))
1145 % os.path.basename(wcpath))
1146 self.run0('checkout', path, wcpath)
1146 self.run0('checkout', path, wcpath)
1147
1147
1148 self.wc = wcpath
1148 self.wc = wcpath
1149 self.opener = scmutil.opener(self.wc)
1149 self.opener = scmutil.opener(self.wc)
1150 self.wopener = scmutil.opener(self.wc)
1150 self.wopener = scmutil.opener(self.wc)
1151 self.childmap = mapfile(ui, self.join('hg-childmap'))
1151 self.childmap = mapfile(ui, self.join('hg-childmap'))
1152 if util.checkexec(self.wc):
1152 if util.checkexec(self.wc):
1153 self.is_exec = util.isexec
1153 self.is_exec = util.isexec
1154 else:
1154 else:
1155 self.is_exec = None
1155 self.is_exec = None
1156
1156
1157 if created:
1157 if created:
1158 hook = os.path.join(created, 'hooks', 'pre-revprop-change')
1158 hook = os.path.join(created, 'hooks', 'pre-revprop-change')
1159 fp = open(hook, 'w')
1159 fp = open(hook, 'w')
1160 fp.write(pre_revprop_change)
1160 fp.write(pre_revprop_change)
1161 fp.close()
1161 fp.close()
1162 util.setflags(hook, False, True)
1162 util.setflags(hook, False, True)
1163
1163
1164 output = self.run0('info')
1164 output = self.run0('info')
1165 self.uuid = self.uuid_re.search(output).group(1).strip()
1165 self.uuid = self.uuid_re.search(output).group(1).strip()
1166
1166
1167 def wjoin(self, *names):
1167 def wjoin(self, *names):
1168 return os.path.join(self.wc, *names)
1168 return os.path.join(self.wc, *names)
1169
1169
1170 @propertycache
1170 @propertycache
1171 def manifest(self):
1171 def manifest(self):
1172 # As of svn 1.7, the "add" command fails when receiving
1172 # As of svn 1.7, the "add" command fails when receiving
1173 # already tracked entries, so we have to track and filter them
1173 # already tracked entries, so we have to track and filter them
1174 # ourselves.
1174 # ourselves.
1175 m = set()
1175 m = set()
1176 output = self.run0('ls', recursive=True, xml=True)
1176 output = self.run0('ls', recursive=True, xml=True)
1177 doc = xml.dom.minidom.parseString(output)
1177 doc = xml.dom.minidom.parseString(output)
1178 for e in doc.getElementsByTagName('entry'):
1178 for e in doc.getElementsByTagName('entry'):
1179 for n in e.childNodes:
1179 for n in e.childNodes:
1180 if n.nodeType != n.ELEMENT_NODE or n.tagName != 'name':
1180 if n.nodeType != n.ELEMENT_NODE or n.tagName != 'name':
1181 continue
1181 continue
1182 name = ''.join(c.data for c in n.childNodes
1182 name = ''.join(c.data for c in n.childNodes
1183 if c.nodeType == c.TEXT_NODE)
1183 if c.nodeType == c.TEXT_NODE)
1184 # Entries are compared with names coming from
1184 # Entries are compared with names coming from
1185 # mercurial, so bytes with undefined encoding. Our
1185 # mercurial, so bytes with undefined encoding. Our
1186 # best bet is to assume they are in local
1186 # best bet is to assume they are in local
1187 # encoding. They will be passed to command line calls
1187 # encoding. They will be passed to command line calls
1188 # later anyway, so they better be.
1188 # later anyway, so they better be.
1189 m.add(encoding.tolocal(name.encode('utf-8')))
1189 m.add(encoding.tolocal(name.encode('utf-8')))
1190 break
1190 break
1191 return m
1191 return m
1192
1192
1193 def putfile(self, filename, flags, data):
1193 def putfile(self, filename, flags, data):
1194 if 'l' in flags:
1194 if 'l' in flags:
1195 self.wopener.symlink(data, filename)
1195 self.wopener.symlink(data, filename)
1196 else:
1196 else:
1197 try:
1197 try:
1198 if os.path.islink(self.wjoin(filename)):
1198 if os.path.islink(self.wjoin(filename)):
1199 os.unlink(filename)
1199 os.unlink(filename)
1200 except OSError:
1200 except OSError:
1201 pass
1201 pass
1202 self.wopener.write(filename, data)
1202 self.wopener.write(filename, data)
1203
1203
1204 if self.is_exec:
1204 if self.is_exec:
1205 if self.is_exec(self.wjoin(filename)):
1205 if self.is_exec(self.wjoin(filename)):
1206 if 'x' not in flags:
1206 if 'x' not in flags:
1207 self.delexec.append(filename)
1207 self.delexec.append(filename)
1208 else:
1208 else:
1209 if 'x' in flags:
1209 if 'x' in flags:
1210 self.setexec.append(filename)
1210 self.setexec.append(filename)
1211 util.setflags(self.wjoin(filename), False, 'x' in flags)
1211 util.setflags(self.wjoin(filename), False, 'x' in flags)
1212
1212
1213 def _copyfile(self, source, dest):
1213 def _copyfile(self, source, dest):
1214 # SVN's copy command pukes if the destination file exists, but
1214 # SVN's copy command pukes if the destination file exists, but
1215 # our copyfile method expects to record a copy that has
1215 # our copyfile method expects to record a copy that has
1216 # already occurred. Cross the semantic gap.
1216 # already occurred. Cross the semantic gap.
1217 wdest = self.wjoin(dest)
1217 wdest = self.wjoin(dest)
1218 exists = os.path.lexists(wdest)
1218 exists = os.path.lexists(wdest)
1219 if exists:
1219 if exists:
1220 fd, tempname = tempfile.mkstemp(
1220 fd, tempname = tempfile.mkstemp(
1221 prefix='hg-copy-', dir=os.path.dirname(wdest))
1221 prefix='hg-copy-', dir=os.path.dirname(wdest))
1222 os.close(fd)
1222 os.close(fd)
1223 os.unlink(tempname)
1223 os.unlink(tempname)
1224 os.rename(wdest, tempname)
1224 os.rename(wdest, tempname)
1225 try:
1225 try:
1226 self.run0('copy', source, dest)
1226 self.run0('copy', source, dest)
1227 finally:
1227 finally:
1228 self.manifest.add(dest)
1228 self.manifest.add(dest)
1229 if exists:
1229 if exists:
1230 try:
1230 try:
1231 os.unlink(wdest)
1231 os.unlink(wdest)
1232 except OSError:
1232 except OSError:
1233 pass
1233 pass
1234 os.rename(tempname, wdest)
1234 os.rename(tempname, wdest)
1235
1235
1236 def dirs_of(self, files):
1236 def dirs_of(self, files):
1237 dirs = set()
1237 dirs = set()
1238 for f in files:
1238 for f in files:
1239 if os.path.isdir(self.wjoin(f)):
1239 if os.path.isdir(self.wjoin(f)):
1240 dirs.add(f)
1240 dirs.add(f)
1241 i = len(f)
1241 i = len(f)
1242 for i in iter(lambda: f.rfind('/', 0, i), -1):
1242 for i in iter(lambda: f.rfind('/', 0, i), -1):
1243 dirs.add(f[:i])
1243 dirs.add(f[:i])
1244 return dirs
1244 return dirs
1245
1245
1246 def add_dirs(self, files):
1246 def add_dirs(self, files):
1247 add_dirs = [d for d in sorted(self.dirs_of(files))
1247 add_dirs = [d for d in sorted(self.dirs_of(files))
1248 if d not in self.manifest]
1248 if d not in self.manifest]
1249 if add_dirs:
1249 if add_dirs:
1250 self.manifest.update(add_dirs)
1250 self.manifest.update(add_dirs)
1251 self.xargs(add_dirs, 'add', non_recursive=True, quiet=True)
1251 self.xargs(add_dirs, 'add', non_recursive=True, quiet=True)
1252 return add_dirs
1252 return add_dirs
1253
1253
1254 def add_files(self, files):
1254 def add_files(self, files):
1255 files = [f for f in files if f not in self.manifest]
1255 files = [f for f in files if f not in self.manifest]
1256 if files:
1256 if files:
1257 self.manifest.update(files)
1257 self.manifest.update(files)
1258 self.xargs(files, 'add', quiet=True)
1258 self.xargs(files, 'add', quiet=True)
1259 return files
1259 return files
1260
1260
1261 def addchild(self, parent, child):
1261 def addchild(self, parent, child):
1262 self.childmap[parent] = child
1262 self.childmap[parent] = child
1263
1263
1264 def revid(self, rev):
1264 def revid(self, rev):
1265 return u"svn:%s@%s" % (self.uuid, rev)
1265 return u"svn:%s@%s" % (self.uuid, rev)
1266
1266
1267 def putcommit(self, files, copies, parents, commit, source, revmap, full,
1267 def putcommit(self, files, copies, parents, commit, source, revmap, full,
1268 cleanp2):
1268 cleanp2):
1269 for parent in parents:
1269 for parent in parents:
1270 try:
1270 try:
1271 return self.revid(self.childmap[parent])
1271 return self.revid(self.childmap[parent])
1272 except KeyError:
1272 except KeyError:
1273 pass
1273 pass
1274
1274
1275 # Apply changes to working copy
1275 # Apply changes to working copy
1276 for f, v in files:
1276 for f, v in files:
1277 data, mode = source.getfile(f, v)
1277 data, mode = source.getfile(f, v)
1278 if data is None:
1278 if data is None:
1279 self.delete.append(f)
1279 self.delete.append(f)
1280 else:
1280 else:
1281 self.putfile(f, mode, data)
1281 self.putfile(f, mode, data)
1282 if f in copies:
1282 if f in copies:
1283 self.copies.append([copies[f], f])
1283 self.copies.append([copies[f], f])
1284 if full:
1284 if full:
1285 self.delete.extend(sorted(self.manifest.difference(files)))
1285 self.delete.extend(sorted(self.manifest.difference(files)))
1286 files = [f[0] for f in files]
1286 files = [f[0] for f in files]
1287
1287
1288 entries = set(self.delete)
1288 entries = set(self.delete)
1289 files = frozenset(files)
1289 files = frozenset(files)
1290 entries.update(self.add_dirs(files.difference(entries)))
1290 entries.update(self.add_dirs(files.difference(entries)))
1291 if self.copies:
1291 if self.copies:
1292 for s, d in self.copies:
1292 for s, d in self.copies:
1293 self._copyfile(s, d)
1293 self._copyfile(s, d)
1294 self.copies = []
1294 self.copies = []
1295 if self.delete:
1295 if self.delete:
1296 self.xargs(self.delete, 'delete')
1296 self.xargs(self.delete, 'delete')
1297 for f in self.delete:
1297 for f in self.delete:
1298 self.manifest.remove(f)
1298 self.manifest.remove(f)
1299 self.delete = []
1299 self.delete = []
1300 entries.update(self.add_files(files.difference(entries)))
1300 entries.update(self.add_files(files.difference(entries)))
1301 if self.delexec:
1301 if self.delexec:
1302 self.xargs(self.delexec, 'propdel', 'svn:executable')
1302 self.xargs(self.delexec, 'propdel', 'svn:executable')
1303 self.delexec = []
1303 self.delexec = []
1304 if self.setexec:
1304 if self.setexec:
1305 self.xargs(self.setexec, 'propset', 'svn:executable', '*')
1305 self.xargs(self.setexec, 'propset', 'svn:executable', '*')
1306 self.setexec = []
1306 self.setexec = []
1307
1307
1308 fd, messagefile = tempfile.mkstemp(prefix='hg-convert-')
1308 fd, messagefile = tempfile.mkstemp(prefix='hg-convert-')
1309 fp = os.fdopen(fd, 'w')
1309 fp = os.fdopen(fd, pycompat.sysstr('w'))
1310 fp.write(commit.desc)
1310 fp.write(commit.desc)
1311 fp.close()
1311 fp.close()
1312 try:
1312 try:
1313 output = self.run0('commit',
1313 output = self.run0('commit',
1314 username=util.shortuser(commit.author),
1314 username=util.shortuser(commit.author),
1315 file=messagefile,
1315 file=messagefile,
1316 encoding='utf-8')
1316 encoding='utf-8')
1317 try:
1317 try:
1318 rev = self.commit_re.search(output).group(1)
1318 rev = self.commit_re.search(output).group(1)
1319 except AttributeError:
1319 except AttributeError:
1320 if parents and not files:
1320 if parents and not files:
1321 return parents[0]
1321 return parents[0]
1322 self.ui.warn(_('unexpected svn output:\n'))
1322 self.ui.warn(_('unexpected svn output:\n'))
1323 self.ui.warn(output)
1323 self.ui.warn(output)
1324 raise error.Abort(_('unable to cope with svn output'))
1324 raise error.Abort(_('unable to cope with svn output'))
1325 if commit.rev:
1325 if commit.rev:
1326 self.run('propset', 'hg:convert-rev', commit.rev,
1326 self.run('propset', 'hg:convert-rev', commit.rev,
1327 revprop=True, revision=rev)
1327 revprop=True, revision=rev)
1328 if commit.branch and commit.branch != 'default':
1328 if commit.branch and commit.branch != 'default':
1329 self.run('propset', 'hg:convert-branch', commit.branch,
1329 self.run('propset', 'hg:convert-branch', commit.branch,
1330 revprop=True, revision=rev)
1330 revprop=True, revision=rev)
1331 for parent in parents:
1331 for parent in parents:
1332 self.addchild(parent, rev)
1332 self.addchild(parent, rev)
1333 return self.revid(rev)
1333 return self.revid(rev)
1334 finally:
1334 finally:
1335 os.unlink(messagefile)
1335 os.unlink(messagefile)
1336
1336
1337 def puttags(self, tags):
1337 def puttags(self, tags):
1338 self.ui.warn(_('writing Subversion tags is not yet implemented\n'))
1338 self.ui.warn(_('writing Subversion tags is not yet implemented\n'))
1339 return None, None
1339 return None, None
1340
1340
1341 def hascommitfrommap(self, rev):
1341 def hascommitfrommap(self, rev):
1342 # We trust that revisions referenced in a map still is present
1342 # We trust that revisions referenced in a map still is present
1343 # TODO: implement something better if necessary and feasible
1343 # TODO: implement something better if necessary and feasible
1344 return True
1344 return True
1345
1345
1346 def hascommitforsplicemap(self, rev):
1346 def hascommitforsplicemap(self, rev):
1347 # This is not correct as one can convert to an existing subversion
1347 # This is not correct as one can convert to an existing subversion
1348 # repository and childmap would not list all revisions. Too bad.
1348 # repository and childmap would not list all revisions. Too bad.
1349 if rev in self.childmap:
1349 if rev in self.childmap:
1350 return True
1350 return True
1351 raise error.Abort(_('splice map revision %s not found in subversion '
1351 raise error.Abort(_('splice map revision %s not found in subversion '
1352 'child map (revision lookups are not implemented)')
1352 'child map (revision lookups are not implemented)')
1353 % rev)
1353 % rev)
@@ -1,318 +1,319 b''
1 # Copyright 2005, 2006 Benoit Boissinot <benoit.boissinot@ens-lyon.org>
1 # Copyright 2005, 2006 Benoit Boissinot <benoit.boissinot@ens-lyon.org>
2 #
2 #
3 # This software may be used and distributed according to the terms of the
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
4 # GNU General Public License version 2 or any later version.
5
5
6 '''commands to sign and verify changesets'''
6 '''commands to sign and verify changesets'''
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import binascii
10 import binascii
11 import os
11 import os
12 import tempfile
12 import tempfile
13
13
14 from mercurial.i18n import _
14 from mercurial.i18n import _
15 from mercurial import (
15 from mercurial import (
16 cmdutil,
16 cmdutil,
17 commands,
17 commands,
18 error,
18 error,
19 match,
19 match,
20 node as hgnode,
20 node as hgnode,
21 pycompat,
21 util,
22 util,
22 )
23 )
23
24
24 cmdtable = {}
25 cmdtable = {}
25 command = cmdutil.command(cmdtable)
26 command = cmdutil.command(cmdtable)
26 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
27 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
27 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
28 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
28 # be specifying the version(s) of Mercurial they are tested with, or
29 # be specifying the version(s) of Mercurial they are tested with, or
29 # leave the attribute unspecified.
30 # leave the attribute unspecified.
30 testedwith = 'ships-with-hg-core'
31 testedwith = 'ships-with-hg-core'
31
32
32 class gpg(object):
33 class gpg(object):
33 def __init__(self, path, key=None):
34 def __init__(self, path, key=None):
34 self.path = path
35 self.path = path
35 self.key = (key and " --local-user \"%s\"" % key) or ""
36 self.key = (key and " --local-user \"%s\"" % key) or ""
36
37
37 def sign(self, data):
38 def sign(self, data):
38 gpgcmd = "%s --sign --detach-sign%s" % (self.path, self.key)
39 gpgcmd = "%s --sign --detach-sign%s" % (self.path, self.key)
39 return util.filter(data, gpgcmd)
40 return util.filter(data, gpgcmd)
40
41
41 def verify(self, data, sig):
42 def verify(self, data, sig):
42 """ returns of the good and bad signatures"""
43 """ returns of the good and bad signatures"""
43 sigfile = datafile = None
44 sigfile = datafile = None
44 try:
45 try:
45 # create temporary files
46 # create temporary files
46 fd, sigfile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".sig")
47 fd, sigfile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".sig")
47 fp = os.fdopen(fd, 'wb')
48 fp = os.fdopen(fd, pycompat.sysstr('wb'))
48 fp.write(sig)
49 fp.write(sig)
49 fp.close()
50 fp.close()
50 fd, datafile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".txt")
51 fd, datafile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".txt")
51 fp = os.fdopen(fd, 'wb')
52 fp = os.fdopen(fd, pycompat.sysstr('wb'))
52 fp.write(data)
53 fp.write(data)
53 fp.close()
54 fp.close()
54 gpgcmd = ("%s --logger-fd 1 --status-fd 1 --verify "
55 gpgcmd = ("%s --logger-fd 1 --status-fd 1 --verify "
55 "\"%s\" \"%s\"" % (self.path, sigfile, datafile))
56 "\"%s\" \"%s\"" % (self.path, sigfile, datafile))
56 ret = util.filter("", gpgcmd)
57 ret = util.filter("", gpgcmd)
57 finally:
58 finally:
58 for f in (sigfile, datafile):
59 for f in (sigfile, datafile):
59 try:
60 try:
60 if f:
61 if f:
61 os.unlink(f)
62 os.unlink(f)
62 except OSError:
63 except OSError:
63 pass
64 pass
64 keys = []
65 keys = []
65 key, fingerprint = None, None
66 key, fingerprint = None, None
66 for l in ret.splitlines():
67 for l in ret.splitlines():
67 # see DETAILS in the gnupg documentation
68 # see DETAILS in the gnupg documentation
68 # filter the logger output
69 # filter the logger output
69 if not l.startswith("[GNUPG:]"):
70 if not l.startswith("[GNUPG:]"):
70 continue
71 continue
71 l = l[9:]
72 l = l[9:]
72 if l.startswith("VALIDSIG"):
73 if l.startswith("VALIDSIG"):
73 # fingerprint of the primary key
74 # fingerprint of the primary key
74 fingerprint = l.split()[10]
75 fingerprint = l.split()[10]
75 elif l.startswith("ERRSIG"):
76 elif l.startswith("ERRSIG"):
76 key = l.split(" ", 3)[:2]
77 key = l.split(" ", 3)[:2]
77 key.append("")
78 key.append("")
78 fingerprint = None
79 fingerprint = None
79 elif (l.startswith("GOODSIG") or
80 elif (l.startswith("GOODSIG") or
80 l.startswith("EXPSIG") or
81 l.startswith("EXPSIG") or
81 l.startswith("EXPKEYSIG") or
82 l.startswith("EXPKEYSIG") or
82 l.startswith("BADSIG")):
83 l.startswith("BADSIG")):
83 if key is not None:
84 if key is not None:
84 keys.append(key + [fingerprint])
85 keys.append(key + [fingerprint])
85 key = l.split(" ", 2)
86 key = l.split(" ", 2)
86 fingerprint = None
87 fingerprint = None
87 if key is not None:
88 if key is not None:
88 keys.append(key + [fingerprint])
89 keys.append(key + [fingerprint])
89 return keys
90 return keys
90
91
91 def newgpg(ui, **opts):
92 def newgpg(ui, **opts):
92 """create a new gpg instance"""
93 """create a new gpg instance"""
93 gpgpath = ui.config("gpg", "cmd", "gpg")
94 gpgpath = ui.config("gpg", "cmd", "gpg")
94 gpgkey = opts.get('key')
95 gpgkey = opts.get('key')
95 if not gpgkey:
96 if not gpgkey:
96 gpgkey = ui.config("gpg", "key", None)
97 gpgkey = ui.config("gpg", "key", None)
97 return gpg(gpgpath, gpgkey)
98 return gpg(gpgpath, gpgkey)
98
99
99 def sigwalk(repo):
100 def sigwalk(repo):
100 """
101 """
101 walk over every sigs, yields a couple
102 walk over every sigs, yields a couple
102 ((node, version, sig), (filename, linenumber))
103 ((node, version, sig), (filename, linenumber))
103 """
104 """
104 def parsefile(fileiter, context):
105 def parsefile(fileiter, context):
105 ln = 1
106 ln = 1
106 for l in fileiter:
107 for l in fileiter:
107 if not l:
108 if not l:
108 continue
109 continue
109 yield (l.split(" ", 2), (context, ln))
110 yield (l.split(" ", 2), (context, ln))
110 ln += 1
111 ln += 1
111
112
112 # read the heads
113 # read the heads
113 fl = repo.file(".hgsigs")
114 fl = repo.file(".hgsigs")
114 for r in reversed(fl.heads()):
115 for r in reversed(fl.heads()):
115 fn = ".hgsigs|%s" % hgnode.short(r)
116 fn = ".hgsigs|%s" % hgnode.short(r)
116 for item in parsefile(fl.read(r).splitlines(), fn):
117 for item in parsefile(fl.read(r).splitlines(), fn):
117 yield item
118 yield item
118 try:
119 try:
119 # read local signatures
120 # read local signatures
120 fn = "localsigs"
121 fn = "localsigs"
121 for item in parsefile(repo.vfs(fn), fn):
122 for item in parsefile(repo.vfs(fn), fn):
122 yield item
123 yield item
123 except IOError:
124 except IOError:
124 pass
125 pass
125
126
126 def getkeys(ui, repo, mygpg, sigdata, context):
127 def getkeys(ui, repo, mygpg, sigdata, context):
127 """get the keys who signed a data"""
128 """get the keys who signed a data"""
128 fn, ln = context
129 fn, ln = context
129 node, version, sig = sigdata
130 node, version, sig = sigdata
130 prefix = "%s:%d" % (fn, ln)
131 prefix = "%s:%d" % (fn, ln)
131 node = hgnode.bin(node)
132 node = hgnode.bin(node)
132
133
133 data = node2txt(repo, node, version)
134 data = node2txt(repo, node, version)
134 sig = binascii.a2b_base64(sig)
135 sig = binascii.a2b_base64(sig)
135 keys = mygpg.verify(data, sig)
136 keys = mygpg.verify(data, sig)
136
137
137 validkeys = []
138 validkeys = []
138 # warn for expired key and/or sigs
139 # warn for expired key and/or sigs
139 for key in keys:
140 for key in keys:
140 if key[0] == "ERRSIG":
141 if key[0] == "ERRSIG":
141 ui.write(_("%s Unknown key ID \"%s\"\n")
142 ui.write(_("%s Unknown key ID \"%s\"\n")
142 % (prefix, shortkey(ui, key[1][:15])))
143 % (prefix, shortkey(ui, key[1][:15])))
143 continue
144 continue
144 if key[0] == "BADSIG":
145 if key[0] == "BADSIG":
145 ui.write(_("%s Bad signature from \"%s\"\n") % (prefix, key[2]))
146 ui.write(_("%s Bad signature from \"%s\"\n") % (prefix, key[2]))
146 continue
147 continue
147 if key[0] == "EXPSIG":
148 if key[0] == "EXPSIG":
148 ui.write(_("%s Note: Signature has expired"
149 ui.write(_("%s Note: Signature has expired"
149 " (signed by: \"%s\")\n") % (prefix, key[2]))
150 " (signed by: \"%s\")\n") % (prefix, key[2]))
150 elif key[0] == "EXPKEYSIG":
151 elif key[0] == "EXPKEYSIG":
151 ui.write(_("%s Note: This key has expired"
152 ui.write(_("%s Note: This key has expired"
152 " (signed by: \"%s\")\n") % (prefix, key[2]))
153 " (signed by: \"%s\")\n") % (prefix, key[2]))
153 validkeys.append((key[1], key[2], key[3]))
154 validkeys.append((key[1], key[2], key[3]))
154 return validkeys
155 return validkeys
155
156
156 @command("sigs", [], _('hg sigs'))
157 @command("sigs", [], _('hg sigs'))
157 def sigs(ui, repo):
158 def sigs(ui, repo):
158 """list signed changesets"""
159 """list signed changesets"""
159 mygpg = newgpg(ui)
160 mygpg = newgpg(ui)
160 revs = {}
161 revs = {}
161
162
162 for data, context in sigwalk(repo):
163 for data, context in sigwalk(repo):
163 node, version, sig = data
164 node, version, sig = data
164 fn, ln = context
165 fn, ln = context
165 try:
166 try:
166 n = repo.lookup(node)
167 n = repo.lookup(node)
167 except KeyError:
168 except KeyError:
168 ui.warn(_("%s:%d node does not exist\n") % (fn, ln))
169 ui.warn(_("%s:%d node does not exist\n") % (fn, ln))
169 continue
170 continue
170 r = repo.changelog.rev(n)
171 r = repo.changelog.rev(n)
171 keys = getkeys(ui, repo, mygpg, data, context)
172 keys = getkeys(ui, repo, mygpg, data, context)
172 if not keys:
173 if not keys:
173 continue
174 continue
174 revs.setdefault(r, [])
175 revs.setdefault(r, [])
175 revs[r].extend(keys)
176 revs[r].extend(keys)
176 for rev in sorted(revs, reverse=True):
177 for rev in sorted(revs, reverse=True):
177 for k in revs[rev]:
178 for k in revs[rev]:
178 r = "%5d:%s" % (rev, hgnode.hex(repo.changelog.node(rev)))
179 r = "%5d:%s" % (rev, hgnode.hex(repo.changelog.node(rev)))
179 ui.write("%-30s %s\n" % (keystr(ui, k), r))
180 ui.write("%-30s %s\n" % (keystr(ui, k), r))
180
181
181 @command("sigcheck", [], _('hg sigcheck REV'))
182 @command("sigcheck", [], _('hg sigcheck REV'))
182 def sigcheck(ui, repo, rev):
183 def sigcheck(ui, repo, rev):
183 """verify all the signatures there may be for a particular revision"""
184 """verify all the signatures there may be for a particular revision"""
184 mygpg = newgpg(ui)
185 mygpg = newgpg(ui)
185 rev = repo.lookup(rev)
186 rev = repo.lookup(rev)
186 hexrev = hgnode.hex(rev)
187 hexrev = hgnode.hex(rev)
187 keys = []
188 keys = []
188
189
189 for data, context in sigwalk(repo):
190 for data, context in sigwalk(repo):
190 node, version, sig = data
191 node, version, sig = data
191 if node == hexrev:
192 if node == hexrev:
192 k = getkeys(ui, repo, mygpg, data, context)
193 k = getkeys(ui, repo, mygpg, data, context)
193 if k:
194 if k:
194 keys.extend(k)
195 keys.extend(k)
195
196
196 if not keys:
197 if not keys:
197 ui.write(_("no valid signature for %s\n") % hgnode.short(rev))
198 ui.write(_("no valid signature for %s\n") % hgnode.short(rev))
198 return
199 return
199
200
200 # print summary
201 # print summary
201 ui.write(_("%s is signed by:\n") % hgnode.short(rev))
202 ui.write(_("%s is signed by:\n") % hgnode.short(rev))
202 for key in keys:
203 for key in keys:
203 ui.write(" %s\n" % keystr(ui, key))
204 ui.write(" %s\n" % keystr(ui, key))
204
205
205 def keystr(ui, key):
206 def keystr(ui, key):
206 """associate a string to a key (username, comment)"""
207 """associate a string to a key (username, comment)"""
207 keyid, user, fingerprint = key
208 keyid, user, fingerprint = key
208 comment = ui.config("gpg", fingerprint, None)
209 comment = ui.config("gpg", fingerprint, None)
209 if comment:
210 if comment:
210 return "%s (%s)" % (user, comment)
211 return "%s (%s)" % (user, comment)
211 else:
212 else:
212 return user
213 return user
213
214
214 @command("sign",
215 @command("sign",
215 [('l', 'local', None, _('make the signature local')),
216 [('l', 'local', None, _('make the signature local')),
216 ('f', 'force', None, _('sign even if the sigfile is modified')),
217 ('f', 'force', None, _('sign even if the sigfile is modified')),
217 ('', 'no-commit', None, _('do not commit the sigfile after signing')),
218 ('', 'no-commit', None, _('do not commit the sigfile after signing')),
218 ('k', 'key', '',
219 ('k', 'key', '',
219 _('the key id to sign with'), _('ID')),
220 _('the key id to sign with'), _('ID')),
220 ('m', 'message', '',
221 ('m', 'message', '',
221 _('use text as commit message'), _('TEXT')),
222 _('use text as commit message'), _('TEXT')),
222 ('e', 'edit', False, _('invoke editor on commit messages')),
223 ('e', 'edit', False, _('invoke editor on commit messages')),
223 ] + commands.commitopts2,
224 ] + commands.commitopts2,
224 _('hg sign [OPTION]... [REV]...'))
225 _('hg sign [OPTION]... [REV]...'))
225 def sign(ui, repo, *revs, **opts):
226 def sign(ui, repo, *revs, **opts):
226 """add a signature for the current or given revision
227 """add a signature for the current or given revision
227
228
228 If no revision is given, the parent of the working directory is used,
229 If no revision is given, the parent of the working directory is used,
229 or tip if no revision is checked out.
230 or tip if no revision is checked out.
230
231
231 The ``gpg.cmd`` config setting can be used to specify the command
232 The ``gpg.cmd`` config setting can be used to specify the command
232 to run. A default key can be specified with ``gpg.key``.
233 to run. A default key can be specified with ``gpg.key``.
233
234
234 See :hg:`help dates` for a list of formats valid for -d/--date.
235 See :hg:`help dates` for a list of formats valid for -d/--date.
235 """
236 """
236 with repo.wlock():
237 with repo.wlock():
237 return _dosign(ui, repo, *revs, **opts)
238 return _dosign(ui, repo, *revs, **opts)
238
239
239 def _dosign(ui, repo, *revs, **opts):
240 def _dosign(ui, repo, *revs, **opts):
240 mygpg = newgpg(ui, **opts)
241 mygpg = newgpg(ui, **opts)
241 sigver = "0"
242 sigver = "0"
242 sigmessage = ""
243 sigmessage = ""
243
244
244 date = opts.get('date')
245 date = opts.get('date')
245 if date:
246 if date:
246 opts['date'] = util.parsedate(date)
247 opts['date'] = util.parsedate(date)
247
248
248 if revs:
249 if revs:
249 nodes = [repo.lookup(n) for n in revs]
250 nodes = [repo.lookup(n) for n in revs]
250 else:
251 else:
251 nodes = [node for node in repo.dirstate.parents()
252 nodes = [node for node in repo.dirstate.parents()
252 if node != hgnode.nullid]
253 if node != hgnode.nullid]
253 if len(nodes) > 1:
254 if len(nodes) > 1:
254 raise error.Abort(_('uncommitted merge - please provide a '
255 raise error.Abort(_('uncommitted merge - please provide a '
255 'specific revision'))
256 'specific revision'))
256 if not nodes:
257 if not nodes:
257 nodes = [repo.changelog.tip()]
258 nodes = [repo.changelog.tip()]
258
259
259 for n in nodes:
260 for n in nodes:
260 hexnode = hgnode.hex(n)
261 hexnode = hgnode.hex(n)
261 ui.write(_("signing %d:%s\n") % (repo.changelog.rev(n),
262 ui.write(_("signing %d:%s\n") % (repo.changelog.rev(n),
262 hgnode.short(n)))
263 hgnode.short(n)))
263 # build data
264 # build data
264 data = node2txt(repo, n, sigver)
265 data = node2txt(repo, n, sigver)
265 sig = mygpg.sign(data)
266 sig = mygpg.sign(data)
266 if not sig:
267 if not sig:
267 raise error.Abort(_("error while signing"))
268 raise error.Abort(_("error while signing"))
268 sig = binascii.b2a_base64(sig)
269 sig = binascii.b2a_base64(sig)
269 sig = sig.replace("\n", "")
270 sig = sig.replace("\n", "")
270 sigmessage += "%s %s %s\n" % (hexnode, sigver, sig)
271 sigmessage += "%s %s %s\n" % (hexnode, sigver, sig)
271
272
272 # write it
273 # write it
273 if opts['local']:
274 if opts['local']:
274 repo.vfs.append("localsigs", sigmessage)
275 repo.vfs.append("localsigs", sigmessage)
275 return
276 return
276
277
277 if not opts["force"]:
278 if not opts["force"]:
278 msigs = match.exact(repo.root, '', ['.hgsigs'])
279 msigs = match.exact(repo.root, '', ['.hgsigs'])
279 if any(repo.status(match=msigs, unknown=True, ignored=True)):
280 if any(repo.status(match=msigs, unknown=True, ignored=True)):
280 raise error.Abort(_("working copy of .hgsigs is changed "),
281 raise error.Abort(_("working copy of .hgsigs is changed "),
281 hint=_("please commit .hgsigs manually"))
282 hint=_("please commit .hgsigs manually"))
282
283
283 sigsfile = repo.wfile(".hgsigs", "ab")
284 sigsfile = repo.wfile(".hgsigs", "ab")
284 sigsfile.write(sigmessage)
285 sigsfile.write(sigmessage)
285 sigsfile.close()
286 sigsfile.close()
286
287
287 if '.hgsigs' not in repo.dirstate:
288 if '.hgsigs' not in repo.dirstate:
288 repo[None].add([".hgsigs"])
289 repo[None].add([".hgsigs"])
289
290
290 if opts["no_commit"]:
291 if opts["no_commit"]:
291 return
292 return
292
293
293 message = opts['message']
294 message = opts['message']
294 if not message:
295 if not message:
295 # we don't translate commit messages
296 # we don't translate commit messages
296 message = "\n".join(["Added signature for changeset %s"
297 message = "\n".join(["Added signature for changeset %s"
297 % hgnode.short(n)
298 % hgnode.short(n)
298 for n in nodes])
299 for n in nodes])
299 try:
300 try:
300 editor = cmdutil.getcommiteditor(editform='gpg.sign', **opts)
301 editor = cmdutil.getcommiteditor(editform='gpg.sign', **opts)
301 repo.commit(message, opts['user'], opts['date'], match=msigs,
302 repo.commit(message, opts['user'], opts['date'], match=msigs,
302 editor=editor)
303 editor=editor)
303 except ValueError as inst:
304 except ValueError as inst:
304 raise error.Abort(str(inst))
305 raise error.Abort(str(inst))
305
306
306 def shortkey(ui, key):
307 def shortkey(ui, key):
307 if len(key) != 16:
308 if len(key) != 16:
308 ui.debug("key ID \"%s\" format error\n" % key)
309 ui.debug("key ID \"%s\" format error\n" % key)
309 return key
310 return key
310
311
311 return key[-8:]
312 return key[-8:]
312
313
313 def node2txt(repo, node, ver):
314 def node2txt(repo, node, ver):
314 """map a manifest into some text"""
315 """map a manifest into some text"""
315 if ver == "0":
316 if ver == "0":
316 return "%s\n" % hgnode.hex(node)
317 return "%s\n" % hgnode.hex(node)
317 else:
318 else:
318 raise error.Abort(_("unknown signature version"))
319 raise error.Abort(_("unknown signature version"))
@@ -1,743 +1,744 b''
1 # Patch transplanting extension for Mercurial
1 # Patch transplanting extension for Mercurial
2 #
2 #
3 # Copyright 2006, 2007 Brendan Cully <brendan@kublai.com>
3 # Copyright 2006, 2007 Brendan Cully <brendan@kublai.com>
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 '''command to transplant changesets from another branch
8 '''command to transplant changesets from another branch
9
9
10 This extension allows you to transplant changes to another parent revision,
10 This extension allows you to transplant changes to another parent revision,
11 possibly in another repository. The transplant is done using 'diff' patches.
11 possibly in another repository. The transplant is done using 'diff' patches.
12
12
13 Transplanted patches are recorded in .hg/transplant/transplants, as a
13 Transplanted patches are recorded in .hg/transplant/transplants, as a
14 map from a changeset hash to its hash in the source repository.
14 map from a changeset hash to its hash in the source repository.
15 '''
15 '''
16 from __future__ import absolute_import
16 from __future__ import absolute_import
17
17
18 import os
18 import os
19 import tempfile
19 import tempfile
20 from mercurial.i18n import _
20 from mercurial.i18n import _
21 from mercurial import (
21 from mercurial import (
22 bundlerepo,
22 bundlerepo,
23 cmdutil,
23 cmdutil,
24 error,
24 error,
25 exchange,
25 exchange,
26 hg,
26 hg,
27 match,
27 match,
28 merge,
28 merge,
29 node as nodemod,
29 node as nodemod,
30 patch,
30 patch,
31 pycompat,
31 registrar,
32 registrar,
32 revlog,
33 revlog,
33 revset,
34 revset,
34 scmutil,
35 scmutil,
35 util,
36 util,
36 )
37 )
37
38
38 class TransplantError(error.Abort):
39 class TransplantError(error.Abort):
39 pass
40 pass
40
41
41 cmdtable = {}
42 cmdtable = {}
42 command = cmdutil.command(cmdtable)
43 command = cmdutil.command(cmdtable)
43 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
44 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
44 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
45 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
45 # be specifying the version(s) of Mercurial they are tested with, or
46 # be specifying the version(s) of Mercurial they are tested with, or
46 # leave the attribute unspecified.
47 # leave the attribute unspecified.
47 testedwith = 'ships-with-hg-core'
48 testedwith = 'ships-with-hg-core'
48
49
49 class transplantentry(object):
50 class transplantentry(object):
50 def __init__(self, lnode, rnode):
51 def __init__(self, lnode, rnode):
51 self.lnode = lnode
52 self.lnode = lnode
52 self.rnode = rnode
53 self.rnode = rnode
53
54
54 class transplants(object):
55 class transplants(object):
55 def __init__(self, path=None, transplantfile=None, opener=None):
56 def __init__(self, path=None, transplantfile=None, opener=None):
56 self.path = path
57 self.path = path
57 self.transplantfile = transplantfile
58 self.transplantfile = transplantfile
58 self.opener = opener
59 self.opener = opener
59
60
60 if not opener:
61 if not opener:
61 self.opener = scmutil.opener(self.path)
62 self.opener = scmutil.opener(self.path)
62 self.transplants = {}
63 self.transplants = {}
63 self.dirty = False
64 self.dirty = False
64 self.read()
65 self.read()
65
66
66 def read(self):
67 def read(self):
67 abspath = os.path.join(self.path, self.transplantfile)
68 abspath = os.path.join(self.path, self.transplantfile)
68 if self.transplantfile and os.path.exists(abspath):
69 if self.transplantfile and os.path.exists(abspath):
69 for line in self.opener.read(self.transplantfile).splitlines():
70 for line in self.opener.read(self.transplantfile).splitlines():
70 lnode, rnode = map(revlog.bin, line.split(':'))
71 lnode, rnode = map(revlog.bin, line.split(':'))
71 list = self.transplants.setdefault(rnode, [])
72 list = self.transplants.setdefault(rnode, [])
72 list.append(transplantentry(lnode, rnode))
73 list.append(transplantentry(lnode, rnode))
73
74
74 def write(self):
75 def write(self):
75 if self.dirty and self.transplantfile:
76 if self.dirty and self.transplantfile:
76 if not os.path.isdir(self.path):
77 if not os.path.isdir(self.path):
77 os.mkdir(self.path)
78 os.mkdir(self.path)
78 fp = self.opener(self.transplantfile, 'w')
79 fp = self.opener(self.transplantfile, 'w')
79 for list in self.transplants.itervalues():
80 for list in self.transplants.itervalues():
80 for t in list:
81 for t in list:
81 l, r = map(nodemod.hex, (t.lnode, t.rnode))
82 l, r = map(nodemod.hex, (t.lnode, t.rnode))
82 fp.write(l + ':' + r + '\n')
83 fp.write(l + ':' + r + '\n')
83 fp.close()
84 fp.close()
84 self.dirty = False
85 self.dirty = False
85
86
86 def get(self, rnode):
87 def get(self, rnode):
87 return self.transplants.get(rnode) or []
88 return self.transplants.get(rnode) or []
88
89
89 def set(self, lnode, rnode):
90 def set(self, lnode, rnode):
90 list = self.transplants.setdefault(rnode, [])
91 list = self.transplants.setdefault(rnode, [])
91 list.append(transplantentry(lnode, rnode))
92 list.append(transplantentry(lnode, rnode))
92 self.dirty = True
93 self.dirty = True
93
94
94 def remove(self, transplant):
95 def remove(self, transplant):
95 list = self.transplants.get(transplant.rnode)
96 list = self.transplants.get(transplant.rnode)
96 if list:
97 if list:
97 del list[list.index(transplant)]
98 del list[list.index(transplant)]
98 self.dirty = True
99 self.dirty = True
99
100
100 class transplanter(object):
101 class transplanter(object):
101 def __init__(self, ui, repo, opts):
102 def __init__(self, ui, repo, opts):
102 self.ui = ui
103 self.ui = ui
103 self.path = repo.join('transplant')
104 self.path = repo.join('transplant')
104 self.opener = scmutil.opener(self.path)
105 self.opener = scmutil.opener(self.path)
105 self.transplants = transplants(self.path, 'transplants',
106 self.transplants = transplants(self.path, 'transplants',
106 opener=self.opener)
107 opener=self.opener)
107 def getcommiteditor():
108 def getcommiteditor():
108 editform = cmdutil.mergeeditform(repo[None], 'transplant')
109 editform = cmdutil.mergeeditform(repo[None], 'transplant')
109 return cmdutil.getcommiteditor(editform=editform, **opts)
110 return cmdutil.getcommiteditor(editform=editform, **opts)
110 self.getcommiteditor = getcommiteditor
111 self.getcommiteditor = getcommiteditor
111
112
112 def applied(self, repo, node, parent):
113 def applied(self, repo, node, parent):
113 '''returns True if a node is already an ancestor of parent
114 '''returns True if a node is already an ancestor of parent
114 or is parent or has already been transplanted'''
115 or is parent or has already been transplanted'''
115 if hasnode(repo, parent):
116 if hasnode(repo, parent):
116 parentrev = repo.changelog.rev(parent)
117 parentrev = repo.changelog.rev(parent)
117 if hasnode(repo, node):
118 if hasnode(repo, node):
118 rev = repo.changelog.rev(node)
119 rev = repo.changelog.rev(node)
119 reachable = repo.changelog.ancestors([parentrev], rev,
120 reachable = repo.changelog.ancestors([parentrev], rev,
120 inclusive=True)
121 inclusive=True)
121 if rev in reachable:
122 if rev in reachable:
122 return True
123 return True
123 for t in self.transplants.get(node):
124 for t in self.transplants.get(node):
124 # it might have been stripped
125 # it might have been stripped
125 if not hasnode(repo, t.lnode):
126 if not hasnode(repo, t.lnode):
126 self.transplants.remove(t)
127 self.transplants.remove(t)
127 return False
128 return False
128 lnoderev = repo.changelog.rev(t.lnode)
129 lnoderev = repo.changelog.rev(t.lnode)
129 if lnoderev in repo.changelog.ancestors([parentrev], lnoderev,
130 if lnoderev in repo.changelog.ancestors([parentrev], lnoderev,
130 inclusive=True):
131 inclusive=True):
131 return True
132 return True
132 return False
133 return False
133
134
134 def apply(self, repo, source, revmap, merges, opts=None):
135 def apply(self, repo, source, revmap, merges, opts=None):
135 '''apply the revisions in revmap one by one in revision order'''
136 '''apply the revisions in revmap one by one in revision order'''
136 if opts is None:
137 if opts is None:
137 opts = {}
138 opts = {}
138 revs = sorted(revmap)
139 revs = sorted(revmap)
139 p1, p2 = repo.dirstate.parents()
140 p1, p2 = repo.dirstate.parents()
140 pulls = []
141 pulls = []
141 diffopts = patch.difffeatureopts(self.ui, opts)
142 diffopts = patch.difffeatureopts(self.ui, opts)
142 diffopts.git = True
143 diffopts.git = True
143
144
144 lock = tr = None
145 lock = tr = None
145 try:
146 try:
146 lock = repo.lock()
147 lock = repo.lock()
147 tr = repo.transaction('transplant')
148 tr = repo.transaction('transplant')
148 for rev in revs:
149 for rev in revs:
149 node = revmap[rev]
150 node = revmap[rev]
150 revstr = '%s:%s' % (rev, nodemod.short(node))
151 revstr = '%s:%s' % (rev, nodemod.short(node))
151
152
152 if self.applied(repo, node, p1):
153 if self.applied(repo, node, p1):
153 self.ui.warn(_('skipping already applied revision %s\n') %
154 self.ui.warn(_('skipping already applied revision %s\n') %
154 revstr)
155 revstr)
155 continue
156 continue
156
157
157 parents = source.changelog.parents(node)
158 parents = source.changelog.parents(node)
158 if not (opts.get('filter') or opts.get('log')):
159 if not (opts.get('filter') or opts.get('log')):
159 # If the changeset parent is the same as the
160 # If the changeset parent is the same as the
160 # wdir's parent, just pull it.
161 # wdir's parent, just pull it.
161 if parents[0] == p1:
162 if parents[0] == p1:
162 pulls.append(node)
163 pulls.append(node)
163 p1 = node
164 p1 = node
164 continue
165 continue
165 if pulls:
166 if pulls:
166 if source != repo:
167 if source != repo:
167 exchange.pull(repo, source.peer(), heads=pulls)
168 exchange.pull(repo, source.peer(), heads=pulls)
168 merge.update(repo, pulls[-1], False, False)
169 merge.update(repo, pulls[-1], False, False)
169 p1, p2 = repo.dirstate.parents()
170 p1, p2 = repo.dirstate.parents()
170 pulls = []
171 pulls = []
171
172
172 domerge = False
173 domerge = False
173 if node in merges:
174 if node in merges:
174 # pulling all the merge revs at once would mean we
175 # pulling all the merge revs at once would mean we
175 # couldn't transplant after the latest even if
176 # couldn't transplant after the latest even if
176 # transplants before them fail.
177 # transplants before them fail.
177 domerge = True
178 domerge = True
178 if not hasnode(repo, node):
179 if not hasnode(repo, node):
179 exchange.pull(repo, source.peer(), heads=[node])
180 exchange.pull(repo, source.peer(), heads=[node])
180
181
181 skipmerge = False
182 skipmerge = False
182 if parents[1] != revlog.nullid:
183 if parents[1] != revlog.nullid:
183 if not opts.get('parent'):
184 if not opts.get('parent'):
184 self.ui.note(_('skipping merge changeset %s:%s\n')
185 self.ui.note(_('skipping merge changeset %s:%s\n')
185 % (rev, nodemod.short(node)))
186 % (rev, nodemod.short(node)))
186 skipmerge = True
187 skipmerge = True
187 else:
188 else:
188 parent = source.lookup(opts['parent'])
189 parent = source.lookup(opts['parent'])
189 if parent not in parents:
190 if parent not in parents:
190 raise error.Abort(_('%s is not a parent of %s') %
191 raise error.Abort(_('%s is not a parent of %s') %
191 (nodemod.short(parent),
192 (nodemod.short(parent),
192 nodemod.short(node)))
193 nodemod.short(node)))
193 else:
194 else:
194 parent = parents[0]
195 parent = parents[0]
195
196
196 if skipmerge:
197 if skipmerge:
197 patchfile = None
198 patchfile = None
198 else:
199 else:
199 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
200 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
200 fp = os.fdopen(fd, 'w')
201 fp = os.fdopen(fd, pycompat.sysstr('w'))
201 gen = patch.diff(source, parent, node, opts=diffopts)
202 gen = patch.diff(source, parent, node, opts=diffopts)
202 for chunk in gen:
203 for chunk in gen:
203 fp.write(chunk)
204 fp.write(chunk)
204 fp.close()
205 fp.close()
205
206
206 del revmap[rev]
207 del revmap[rev]
207 if patchfile or domerge:
208 if patchfile or domerge:
208 try:
209 try:
209 try:
210 try:
210 n = self.applyone(repo, node,
211 n = self.applyone(repo, node,
211 source.changelog.read(node),
212 source.changelog.read(node),
212 patchfile, merge=domerge,
213 patchfile, merge=domerge,
213 log=opts.get('log'),
214 log=opts.get('log'),
214 filter=opts.get('filter'))
215 filter=opts.get('filter'))
215 except TransplantError:
216 except TransplantError:
216 # Do not rollback, it is up to the user to
217 # Do not rollback, it is up to the user to
217 # fix the merge or cancel everything
218 # fix the merge or cancel everything
218 tr.close()
219 tr.close()
219 raise
220 raise
220 if n and domerge:
221 if n and domerge:
221 self.ui.status(_('%s merged at %s\n') % (revstr,
222 self.ui.status(_('%s merged at %s\n') % (revstr,
222 nodemod.short(n)))
223 nodemod.short(n)))
223 elif n:
224 elif n:
224 self.ui.status(_('%s transplanted to %s\n')
225 self.ui.status(_('%s transplanted to %s\n')
225 % (nodemod.short(node),
226 % (nodemod.short(node),
226 nodemod.short(n)))
227 nodemod.short(n)))
227 finally:
228 finally:
228 if patchfile:
229 if patchfile:
229 os.unlink(patchfile)
230 os.unlink(patchfile)
230 tr.close()
231 tr.close()
231 if pulls:
232 if pulls:
232 exchange.pull(repo, source.peer(), heads=pulls)
233 exchange.pull(repo, source.peer(), heads=pulls)
233 merge.update(repo, pulls[-1], False, False)
234 merge.update(repo, pulls[-1], False, False)
234 finally:
235 finally:
235 self.saveseries(revmap, merges)
236 self.saveseries(revmap, merges)
236 self.transplants.write()
237 self.transplants.write()
237 if tr:
238 if tr:
238 tr.release()
239 tr.release()
239 if lock:
240 if lock:
240 lock.release()
241 lock.release()
241
242
242 def filter(self, filter, node, changelog, patchfile):
243 def filter(self, filter, node, changelog, patchfile):
243 '''arbitrarily rewrite changeset before applying it'''
244 '''arbitrarily rewrite changeset before applying it'''
244
245
245 self.ui.status(_('filtering %s\n') % patchfile)
246 self.ui.status(_('filtering %s\n') % patchfile)
246 user, date, msg = (changelog[1], changelog[2], changelog[4])
247 user, date, msg = (changelog[1], changelog[2], changelog[4])
247 fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-')
248 fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-')
248 fp = os.fdopen(fd, 'w')
249 fp = os.fdopen(fd, pycompat.sysstr('w'))
249 fp.write("# HG changeset patch\n")
250 fp.write("# HG changeset patch\n")
250 fp.write("# User %s\n" % user)
251 fp.write("# User %s\n" % user)
251 fp.write("# Date %d %d\n" % date)
252 fp.write("# Date %d %d\n" % date)
252 fp.write(msg + '\n')
253 fp.write(msg + '\n')
253 fp.close()
254 fp.close()
254
255
255 try:
256 try:
256 self.ui.system('%s %s %s' % (filter, util.shellquote(headerfile),
257 self.ui.system('%s %s %s' % (filter, util.shellquote(headerfile),
257 util.shellquote(patchfile)),
258 util.shellquote(patchfile)),
258 environ={'HGUSER': changelog[1],
259 environ={'HGUSER': changelog[1],
259 'HGREVISION': nodemod.hex(node),
260 'HGREVISION': nodemod.hex(node),
260 },
261 },
261 onerr=error.Abort, errprefix=_('filter failed'))
262 onerr=error.Abort, errprefix=_('filter failed'))
262 user, date, msg = self.parselog(file(headerfile))[1:4]
263 user, date, msg = self.parselog(file(headerfile))[1:4]
263 finally:
264 finally:
264 os.unlink(headerfile)
265 os.unlink(headerfile)
265
266
266 return (user, date, msg)
267 return (user, date, msg)
267
268
268 def applyone(self, repo, node, cl, patchfile, merge=False, log=False,
269 def applyone(self, repo, node, cl, patchfile, merge=False, log=False,
269 filter=None):
270 filter=None):
270 '''apply the patch in patchfile to the repository as a transplant'''
271 '''apply the patch in patchfile to the repository as a transplant'''
271 (manifest, user, (time, timezone), files, message) = cl[:5]
272 (manifest, user, (time, timezone), files, message) = cl[:5]
272 date = "%d %d" % (time, timezone)
273 date = "%d %d" % (time, timezone)
273 extra = {'transplant_source': node}
274 extra = {'transplant_source': node}
274 if filter:
275 if filter:
275 (user, date, message) = self.filter(filter, node, cl, patchfile)
276 (user, date, message) = self.filter(filter, node, cl, patchfile)
276
277
277 if log:
278 if log:
278 # we don't translate messages inserted into commits
279 # we don't translate messages inserted into commits
279 message += '\n(transplanted from %s)' % nodemod.hex(node)
280 message += '\n(transplanted from %s)' % nodemod.hex(node)
280
281
281 self.ui.status(_('applying %s\n') % nodemod.short(node))
282 self.ui.status(_('applying %s\n') % nodemod.short(node))
282 self.ui.note('%s %s\n%s\n' % (user, date, message))
283 self.ui.note('%s %s\n%s\n' % (user, date, message))
283
284
284 if not patchfile and not merge:
285 if not patchfile and not merge:
285 raise error.Abort(_('can only omit patchfile if merging'))
286 raise error.Abort(_('can only omit patchfile if merging'))
286 if patchfile:
287 if patchfile:
287 try:
288 try:
288 files = set()
289 files = set()
289 patch.patch(self.ui, repo, patchfile, files=files, eolmode=None)
290 patch.patch(self.ui, repo, patchfile, files=files, eolmode=None)
290 files = list(files)
291 files = list(files)
291 except Exception as inst:
292 except Exception as inst:
292 seriespath = os.path.join(self.path, 'series')
293 seriespath = os.path.join(self.path, 'series')
293 if os.path.exists(seriespath):
294 if os.path.exists(seriespath):
294 os.unlink(seriespath)
295 os.unlink(seriespath)
295 p1 = repo.dirstate.p1()
296 p1 = repo.dirstate.p1()
296 p2 = node
297 p2 = node
297 self.log(user, date, message, p1, p2, merge=merge)
298 self.log(user, date, message, p1, p2, merge=merge)
298 self.ui.write(str(inst) + '\n')
299 self.ui.write(str(inst) + '\n')
299 raise TransplantError(_('fix up the working directory and run '
300 raise TransplantError(_('fix up the working directory and run '
300 'hg transplant --continue'))
301 'hg transplant --continue'))
301 else:
302 else:
302 files = None
303 files = None
303 if merge:
304 if merge:
304 p1, p2 = repo.dirstate.parents()
305 p1, p2 = repo.dirstate.parents()
305 repo.setparents(p1, node)
306 repo.setparents(p1, node)
306 m = match.always(repo.root, '')
307 m = match.always(repo.root, '')
307 else:
308 else:
308 m = match.exact(repo.root, '', files)
309 m = match.exact(repo.root, '', files)
309
310
310 n = repo.commit(message, user, date, extra=extra, match=m,
311 n = repo.commit(message, user, date, extra=extra, match=m,
311 editor=self.getcommiteditor())
312 editor=self.getcommiteditor())
312 if not n:
313 if not n:
313 self.ui.warn(_('skipping emptied changeset %s\n') %
314 self.ui.warn(_('skipping emptied changeset %s\n') %
314 nodemod.short(node))
315 nodemod.short(node))
315 return None
316 return None
316 if not merge:
317 if not merge:
317 self.transplants.set(n, node)
318 self.transplants.set(n, node)
318
319
319 return n
320 return n
320
321
321 def canresume(self):
322 def canresume(self):
322 return os.path.exists(os.path.join(self.path, 'journal'))
323 return os.path.exists(os.path.join(self.path, 'journal'))
323
324
324 def resume(self, repo, source, opts):
325 def resume(self, repo, source, opts):
325 '''recover last transaction and apply remaining changesets'''
326 '''recover last transaction and apply remaining changesets'''
326 if os.path.exists(os.path.join(self.path, 'journal')):
327 if os.path.exists(os.path.join(self.path, 'journal')):
327 n, node = self.recover(repo, source, opts)
328 n, node = self.recover(repo, source, opts)
328 if n:
329 if n:
329 self.ui.status(_('%s transplanted as %s\n') %
330 self.ui.status(_('%s transplanted as %s\n') %
330 (nodemod.short(node),
331 (nodemod.short(node),
331 nodemod.short(n)))
332 nodemod.short(n)))
332 else:
333 else:
333 self.ui.status(_('%s skipped due to empty diff\n')
334 self.ui.status(_('%s skipped due to empty diff\n')
334 % (nodemod.short(node),))
335 % (nodemod.short(node),))
335 seriespath = os.path.join(self.path, 'series')
336 seriespath = os.path.join(self.path, 'series')
336 if not os.path.exists(seriespath):
337 if not os.path.exists(seriespath):
337 self.transplants.write()
338 self.transplants.write()
338 return
339 return
339 nodes, merges = self.readseries()
340 nodes, merges = self.readseries()
340 revmap = {}
341 revmap = {}
341 for n in nodes:
342 for n in nodes:
342 revmap[source.changelog.rev(n)] = n
343 revmap[source.changelog.rev(n)] = n
343 os.unlink(seriespath)
344 os.unlink(seriespath)
344
345
345 self.apply(repo, source, revmap, merges, opts)
346 self.apply(repo, source, revmap, merges, opts)
346
347
347 def recover(self, repo, source, opts):
348 def recover(self, repo, source, opts):
348 '''commit working directory using journal metadata'''
349 '''commit working directory using journal metadata'''
349 node, user, date, message, parents = self.readlog()
350 node, user, date, message, parents = self.readlog()
350 merge = False
351 merge = False
351
352
352 if not user or not date or not message or not parents[0]:
353 if not user or not date or not message or not parents[0]:
353 raise error.Abort(_('transplant log file is corrupt'))
354 raise error.Abort(_('transplant log file is corrupt'))
354
355
355 parent = parents[0]
356 parent = parents[0]
356 if len(parents) > 1:
357 if len(parents) > 1:
357 if opts.get('parent'):
358 if opts.get('parent'):
358 parent = source.lookup(opts['parent'])
359 parent = source.lookup(opts['parent'])
359 if parent not in parents:
360 if parent not in parents:
360 raise error.Abort(_('%s is not a parent of %s') %
361 raise error.Abort(_('%s is not a parent of %s') %
361 (nodemod.short(parent),
362 (nodemod.short(parent),
362 nodemod.short(node)))
363 nodemod.short(node)))
363 else:
364 else:
364 merge = True
365 merge = True
365
366
366 extra = {'transplant_source': node}
367 extra = {'transplant_source': node}
367 try:
368 try:
368 p1, p2 = repo.dirstate.parents()
369 p1, p2 = repo.dirstate.parents()
369 if p1 != parent:
370 if p1 != parent:
370 raise error.Abort(_('working directory not at transplant '
371 raise error.Abort(_('working directory not at transplant '
371 'parent %s') % nodemod.hex(parent))
372 'parent %s') % nodemod.hex(parent))
372 if merge:
373 if merge:
373 repo.setparents(p1, parents[1])
374 repo.setparents(p1, parents[1])
374 modified, added, removed, deleted = repo.status()[:4]
375 modified, added, removed, deleted = repo.status()[:4]
375 if merge or modified or added or removed or deleted:
376 if merge or modified or added or removed or deleted:
376 n = repo.commit(message, user, date, extra=extra,
377 n = repo.commit(message, user, date, extra=extra,
377 editor=self.getcommiteditor())
378 editor=self.getcommiteditor())
378 if not n:
379 if not n:
379 raise error.Abort(_('commit failed'))
380 raise error.Abort(_('commit failed'))
380 if not merge:
381 if not merge:
381 self.transplants.set(n, node)
382 self.transplants.set(n, node)
382 else:
383 else:
383 n = None
384 n = None
384 self.unlog()
385 self.unlog()
385
386
386 return n, node
387 return n, node
387 finally:
388 finally:
388 # TODO: get rid of this meaningless try/finally enclosing.
389 # TODO: get rid of this meaningless try/finally enclosing.
389 # this is kept only to reduce changes in a patch.
390 # this is kept only to reduce changes in a patch.
390 pass
391 pass
391
392
392 def readseries(self):
393 def readseries(self):
393 nodes = []
394 nodes = []
394 merges = []
395 merges = []
395 cur = nodes
396 cur = nodes
396 for line in self.opener.read('series').splitlines():
397 for line in self.opener.read('series').splitlines():
397 if line.startswith('# Merges'):
398 if line.startswith('# Merges'):
398 cur = merges
399 cur = merges
399 continue
400 continue
400 cur.append(revlog.bin(line))
401 cur.append(revlog.bin(line))
401
402
402 return (nodes, merges)
403 return (nodes, merges)
403
404
404 def saveseries(self, revmap, merges):
405 def saveseries(self, revmap, merges):
405 if not revmap:
406 if not revmap:
406 return
407 return
407
408
408 if not os.path.isdir(self.path):
409 if not os.path.isdir(self.path):
409 os.mkdir(self.path)
410 os.mkdir(self.path)
410 series = self.opener('series', 'w')
411 series = self.opener('series', 'w')
411 for rev in sorted(revmap):
412 for rev in sorted(revmap):
412 series.write(nodemod.hex(revmap[rev]) + '\n')
413 series.write(nodemod.hex(revmap[rev]) + '\n')
413 if merges:
414 if merges:
414 series.write('# Merges\n')
415 series.write('# Merges\n')
415 for m in merges:
416 for m in merges:
416 series.write(nodemod.hex(m) + '\n')
417 series.write(nodemod.hex(m) + '\n')
417 series.close()
418 series.close()
418
419
419 def parselog(self, fp):
420 def parselog(self, fp):
420 parents = []
421 parents = []
421 message = []
422 message = []
422 node = revlog.nullid
423 node = revlog.nullid
423 inmsg = False
424 inmsg = False
424 user = None
425 user = None
425 date = None
426 date = None
426 for line in fp.read().splitlines():
427 for line in fp.read().splitlines():
427 if inmsg:
428 if inmsg:
428 message.append(line)
429 message.append(line)
429 elif line.startswith('# User '):
430 elif line.startswith('# User '):
430 user = line[7:]
431 user = line[7:]
431 elif line.startswith('# Date '):
432 elif line.startswith('# Date '):
432 date = line[7:]
433 date = line[7:]
433 elif line.startswith('# Node ID '):
434 elif line.startswith('# Node ID '):
434 node = revlog.bin(line[10:])
435 node = revlog.bin(line[10:])
435 elif line.startswith('# Parent '):
436 elif line.startswith('# Parent '):
436 parents.append(revlog.bin(line[9:]))
437 parents.append(revlog.bin(line[9:]))
437 elif not line.startswith('# '):
438 elif not line.startswith('# '):
438 inmsg = True
439 inmsg = True
439 message.append(line)
440 message.append(line)
440 if None in (user, date):
441 if None in (user, date):
441 raise error.Abort(_("filter corrupted changeset (no user or date)"))
442 raise error.Abort(_("filter corrupted changeset (no user or date)"))
442 return (node, user, date, '\n'.join(message), parents)
443 return (node, user, date, '\n'.join(message), parents)
443
444
444 def log(self, user, date, message, p1, p2, merge=False):
445 def log(self, user, date, message, p1, p2, merge=False):
445 '''journal changelog metadata for later recover'''
446 '''journal changelog metadata for later recover'''
446
447
447 if not os.path.isdir(self.path):
448 if not os.path.isdir(self.path):
448 os.mkdir(self.path)
449 os.mkdir(self.path)
449 fp = self.opener('journal', 'w')
450 fp = self.opener('journal', 'w')
450 fp.write('# User %s\n' % user)
451 fp.write('# User %s\n' % user)
451 fp.write('# Date %s\n' % date)
452 fp.write('# Date %s\n' % date)
452 fp.write('# Node ID %s\n' % nodemod.hex(p2))
453 fp.write('# Node ID %s\n' % nodemod.hex(p2))
453 fp.write('# Parent ' + nodemod.hex(p1) + '\n')
454 fp.write('# Parent ' + nodemod.hex(p1) + '\n')
454 if merge:
455 if merge:
455 fp.write('# Parent ' + nodemod.hex(p2) + '\n')
456 fp.write('# Parent ' + nodemod.hex(p2) + '\n')
456 fp.write(message.rstrip() + '\n')
457 fp.write(message.rstrip() + '\n')
457 fp.close()
458 fp.close()
458
459
459 def readlog(self):
460 def readlog(self):
460 return self.parselog(self.opener('journal'))
461 return self.parselog(self.opener('journal'))
461
462
462 def unlog(self):
463 def unlog(self):
463 '''remove changelog journal'''
464 '''remove changelog journal'''
464 absdst = os.path.join(self.path, 'journal')
465 absdst = os.path.join(self.path, 'journal')
465 if os.path.exists(absdst):
466 if os.path.exists(absdst):
466 os.unlink(absdst)
467 os.unlink(absdst)
467
468
468 def transplantfilter(self, repo, source, root):
469 def transplantfilter(self, repo, source, root):
469 def matchfn(node):
470 def matchfn(node):
470 if self.applied(repo, node, root):
471 if self.applied(repo, node, root):
471 return False
472 return False
472 if source.changelog.parents(node)[1] != revlog.nullid:
473 if source.changelog.parents(node)[1] != revlog.nullid:
473 return False
474 return False
474 extra = source.changelog.read(node)[5]
475 extra = source.changelog.read(node)[5]
475 cnode = extra.get('transplant_source')
476 cnode = extra.get('transplant_source')
476 if cnode and self.applied(repo, cnode, root):
477 if cnode and self.applied(repo, cnode, root):
477 return False
478 return False
478 return True
479 return True
479
480
480 return matchfn
481 return matchfn
481
482
482 def hasnode(repo, node):
483 def hasnode(repo, node):
483 try:
484 try:
484 return repo.changelog.rev(node) is not None
485 return repo.changelog.rev(node) is not None
485 except error.RevlogError:
486 except error.RevlogError:
486 return False
487 return False
487
488
488 def browserevs(ui, repo, nodes, opts):
489 def browserevs(ui, repo, nodes, opts):
489 '''interactively transplant changesets'''
490 '''interactively transplant changesets'''
490 displayer = cmdutil.show_changeset(ui, repo, opts)
491 displayer = cmdutil.show_changeset(ui, repo, opts)
491 transplants = []
492 transplants = []
492 merges = []
493 merges = []
493 prompt = _('apply changeset? [ynmpcq?]:'
494 prompt = _('apply changeset? [ynmpcq?]:'
494 '$$ &yes, transplant this changeset'
495 '$$ &yes, transplant this changeset'
495 '$$ &no, skip this changeset'
496 '$$ &no, skip this changeset'
496 '$$ &merge at this changeset'
497 '$$ &merge at this changeset'
497 '$$ show &patch'
498 '$$ show &patch'
498 '$$ &commit selected changesets'
499 '$$ &commit selected changesets'
499 '$$ &quit and cancel transplant'
500 '$$ &quit and cancel transplant'
500 '$$ &? (show this help)')
501 '$$ &? (show this help)')
501 for node in nodes:
502 for node in nodes:
502 displayer.show(repo[node])
503 displayer.show(repo[node])
503 action = None
504 action = None
504 while not action:
505 while not action:
505 action = 'ynmpcq?'[ui.promptchoice(prompt)]
506 action = 'ynmpcq?'[ui.promptchoice(prompt)]
506 if action == '?':
507 if action == '?':
507 for c, t in ui.extractchoices(prompt)[1]:
508 for c, t in ui.extractchoices(prompt)[1]:
508 ui.write('%s: %s\n' % (c, t))
509 ui.write('%s: %s\n' % (c, t))
509 action = None
510 action = None
510 elif action == 'p':
511 elif action == 'p':
511 parent = repo.changelog.parents(node)[0]
512 parent = repo.changelog.parents(node)[0]
512 for chunk in patch.diff(repo, parent, node):
513 for chunk in patch.diff(repo, parent, node):
513 ui.write(chunk)
514 ui.write(chunk)
514 action = None
515 action = None
515 if action == 'y':
516 if action == 'y':
516 transplants.append(node)
517 transplants.append(node)
517 elif action == 'm':
518 elif action == 'm':
518 merges.append(node)
519 merges.append(node)
519 elif action == 'c':
520 elif action == 'c':
520 break
521 break
521 elif action == 'q':
522 elif action == 'q':
522 transplants = ()
523 transplants = ()
523 merges = ()
524 merges = ()
524 break
525 break
525 displayer.close()
526 displayer.close()
526 return (transplants, merges)
527 return (transplants, merges)
527
528
528 @command('transplant',
529 @command('transplant',
529 [('s', 'source', '', _('transplant changesets from REPO'), _('REPO')),
530 [('s', 'source', '', _('transplant changesets from REPO'), _('REPO')),
530 ('b', 'branch', [], _('use this source changeset as head'), _('REV')),
531 ('b', 'branch', [], _('use this source changeset as head'), _('REV')),
531 ('a', 'all', None, _('pull all changesets up to the --branch revisions')),
532 ('a', 'all', None, _('pull all changesets up to the --branch revisions')),
532 ('p', 'prune', [], _('skip over REV'), _('REV')),
533 ('p', 'prune', [], _('skip over REV'), _('REV')),
533 ('m', 'merge', [], _('merge at REV'), _('REV')),
534 ('m', 'merge', [], _('merge at REV'), _('REV')),
534 ('', 'parent', '',
535 ('', 'parent', '',
535 _('parent to choose when transplanting merge'), _('REV')),
536 _('parent to choose when transplanting merge'), _('REV')),
536 ('e', 'edit', False, _('invoke editor on commit messages')),
537 ('e', 'edit', False, _('invoke editor on commit messages')),
537 ('', 'log', None, _('append transplant info to log message')),
538 ('', 'log', None, _('append transplant info to log message')),
538 ('c', 'continue', None, _('continue last transplant session '
539 ('c', 'continue', None, _('continue last transplant session '
539 'after fixing conflicts')),
540 'after fixing conflicts')),
540 ('', 'filter', '',
541 ('', 'filter', '',
541 _('filter changesets through command'), _('CMD'))],
542 _('filter changesets through command'), _('CMD'))],
542 _('hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] '
543 _('hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] '
543 '[-m REV] [REV]...'))
544 '[-m REV] [REV]...'))
544 def transplant(ui, repo, *revs, **opts):
545 def transplant(ui, repo, *revs, **opts):
545 '''transplant changesets from another branch
546 '''transplant changesets from another branch
546
547
547 Selected changesets will be applied on top of the current working
548 Selected changesets will be applied on top of the current working
548 directory with the log of the original changeset. The changesets
549 directory with the log of the original changeset. The changesets
549 are copied and will thus appear twice in the history with different
550 are copied and will thus appear twice in the history with different
550 identities.
551 identities.
551
552
552 Consider using the graft command if everything is inside the same
553 Consider using the graft command if everything is inside the same
553 repository - it will use merges and will usually give a better result.
554 repository - it will use merges and will usually give a better result.
554 Use the rebase extension if the changesets are unpublished and you want
555 Use the rebase extension if the changesets are unpublished and you want
555 to move them instead of copying them.
556 to move them instead of copying them.
556
557
557 If --log is specified, log messages will have a comment appended
558 If --log is specified, log messages will have a comment appended
558 of the form::
559 of the form::
559
560
560 (transplanted from CHANGESETHASH)
561 (transplanted from CHANGESETHASH)
561
562
562 You can rewrite the changelog message with the --filter option.
563 You can rewrite the changelog message with the --filter option.
563 Its argument will be invoked with the current changelog message as
564 Its argument will be invoked with the current changelog message as
564 $1 and the patch as $2.
565 $1 and the patch as $2.
565
566
566 --source/-s specifies another repository to use for selecting changesets,
567 --source/-s specifies another repository to use for selecting changesets,
567 just as if it temporarily had been pulled.
568 just as if it temporarily had been pulled.
568 If --branch/-b is specified, these revisions will be used as
569 If --branch/-b is specified, these revisions will be used as
569 heads when deciding which changesets to transplant, just as if only
570 heads when deciding which changesets to transplant, just as if only
570 these revisions had been pulled.
571 these revisions had been pulled.
571 If --all/-a is specified, all the revisions up to the heads specified
572 If --all/-a is specified, all the revisions up to the heads specified
572 with --branch will be transplanted.
573 with --branch will be transplanted.
573
574
574 Example:
575 Example:
575
576
576 - transplant all changes up to REV on top of your current revision::
577 - transplant all changes up to REV on top of your current revision::
577
578
578 hg transplant --branch REV --all
579 hg transplant --branch REV --all
579
580
580 You can optionally mark selected transplanted changesets as merge
581 You can optionally mark selected transplanted changesets as merge
581 changesets. You will not be prompted to transplant any ancestors
582 changesets. You will not be prompted to transplant any ancestors
582 of a merged transplant, and you can merge descendants of them
583 of a merged transplant, and you can merge descendants of them
583 normally instead of transplanting them.
584 normally instead of transplanting them.
584
585
585 Merge changesets may be transplanted directly by specifying the
586 Merge changesets may be transplanted directly by specifying the
586 proper parent changeset by calling :hg:`transplant --parent`.
587 proper parent changeset by calling :hg:`transplant --parent`.
587
588
588 If no merges or revisions are provided, :hg:`transplant` will
589 If no merges or revisions are provided, :hg:`transplant` will
589 start an interactive changeset browser.
590 start an interactive changeset browser.
590
591
591 If a changeset application fails, you can fix the merge by hand
592 If a changeset application fails, you can fix the merge by hand
592 and then resume where you left off by calling :hg:`transplant
593 and then resume where you left off by calling :hg:`transplant
593 --continue/-c`.
594 --continue/-c`.
594 '''
595 '''
595 with repo.wlock():
596 with repo.wlock():
596 return _dotransplant(ui, repo, *revs, **opts)
597 return _dotransplant(ui, repo, *revs, **opts)
597
598
598 def _dotransplant(ui, repo, *revs, **opts):
599 def _dotransplant(ui, repo, *revs, **opts):
599 def incwalk(repo, csets, match=util.always):
600 def incwalk(repo, csets, match=util.always):
600 for node in csets:
601 for node in csets:
601 if match(node):
602 if match(node):
602 yield node
603 yield node
603
604
604 def transplantwalk(repo, dest, heads, match=util.always):
605 def transplantwalk(repo, dest, heads, match=util.always):
605 '''Yield all nodes that are ancestors of a head but not ancestors
606 '''Yield all nodes that are ancestors of a head but not ancestors
606 of dest.
607 of dest.
607 If no heads are specified, the heads of repo will be used.'''
608 If no heads are specified, the heads of repo will be used.'''
608 if not heads:
609 if not heads:
609 heads = repo.heads()
610 heads = repo.heads()
610 ancestors = []
611 ancestors = []
611 ctx = repo[dest]
612 ctx = repo[dest]
612 for head in heads:
613 for head in heads:
613 ancestors.append(ctx.ancestor(repo[head]).node())
614 ancestors.append(ctx.ancestor(repo[head]).node())
614 for node in repo.changelog.nodesbetween(ancestors, heads)[0]:
615 for node in repo.changelog.nodesbetween(ancestors, heads)[0]:
615 if match(node):
616 if match(node):
616 yield node
617 yield node
617
618
618 def checkopts(opts, revs):
619 def checkopts(opts, revs):
619 if opts.get('continue'):
620 if opts.get('continue'):
620 if opts.get('branch') or opts.get('all') or opts.get('merge'):
621 if opts.get('branch') or opts.get('all') or opts.get('merge'):
621 raise error.Abort(_('--continue is incompatible with '
622 raise error.Abort(_('--continue is incompatible with '
622 '--branch, --all and --merge'))
623 '--branch, --all and --merge'))
623 return
624 return
624 if not (opts.get('source') or revs or
625 if not (opts.get('source') or revs or
625 opts.get('merge') or opts.get('branch')):
626 opts.get('merge') or opts.get('branch')):
626 raise error.Abort(_('no source URL, branch revision, or revision '
627 raise error.Abort(_('no source URL, branch revision, or revision '
627 'list provided'))
628 'list provided'))
628 if opts.get('all'):
629 if opts.get('all'):
629 if not opts.get('branch'):
630 if not opts.get('branch'):
630 raise error.Abort(_('--all requires a branch revision'))
631 raise error.Abort(_('--all requires a branch revision'))
631 if revs:
632 if revs:
632 raise error.Abort(_('--all is incompatible with a '
633 raise error.Abort(_('--all is incompatible with a '
633 'revision list'))
634 'revision list'))
634
635
635 checkopts(opts, revs)
636 checkopts(opts, revs)
636
637
637 if not opts.get('log'):
638 if not opts.get('log'):
638 # deprecated config: transplant.log
639 # deprecated config: transplant.log
639 opts['log'] = ui.config('transplant', 'log')
640 opts['log'] = ui.config('transplant', 'log')
640 if not opts.get('filter'):
641 if not opts.get('filter'):
641 # deprecated config: transplant.filter
642 # deprecated config: transplant.filter
642 opts['filter'] = ui.config('transplant', 'filter')
643 opts['filter'] = ui.config('transplant', 'filter')
643
644
644 tp = transplanter(ui, repo, opts)
645 tp = transplanter(ui, repo, opts)
645
646
646 p1, p2 = repo.dirstate.parents()
647 p1, p2 = repo.dirstate.parents()
647 if len(repo) > 0 and p1 == revlog.nullid:
648 if len(repo) > 0 and p1 == revlog.nullid:
648 raise error.Abort(_('no revision checked out'))
649 raise error.Abort(_('no revision checked out'))
649 if opts.get('continue'):
650 if opts.get('continue'):
650 if not tp.canresume():
651 if not tp.canresume():
651 raise error.Abort(_('no transplant to continue'))
652 raise error.Abort(_('no transplant to continue'))
652 else:
653 else:
653 cmdutil.checkunfinished(repo)
654 cmdutil.checkunfinished(repo)
654 if p2 != revlog.nullid:
655 if p2 != revlog.nullid:
655 raise error.Abort(_('outstanding uncommitted merges'))
656 raise error.Abort(_('outstanding uncommitted merges'))
656 m, a, r, d = repo.status()[:4]
657 m, a, r, d = repo.status()[:4]
657 if m or a or r or d:
658 if m or a or r or d:
658 raise error.Abort(_('outstanding local changes'))
659 raise error.Abort(_('outstanding local changes'))
659
660
660 sourcerepo = opts.get('source')
661 sourcerepo = opts.get('source')
661 if sourcerepo:
662 if sourcerepo:
662 peer = hg.peer(repo, opts, ui.expandpath(sourcerepo))
663 peer = hg.peer(repo, opts, ui.expandpath(sourcerepo))
663 heads = map(peer.lookup, opts.get('branch', ()))
664 heads = map(peer.lookup, opts.get('branch', ()))
664 target = set(heads)
665 target = set(heads)
665 for r in revs:
666 for r in revs:
666 try:
667 try:
667 target.add(peer.lookup(r))
668 target.add(peer.lookup(r))
668 except error.RepoError:
669 except error.RepoError:
669 pass
670 pass
670 source, csets, cleanupfn = bundlerepo.getremotechanges(ui, repo, peer,
671 source, csets, cleanupfn = bundlerepo.getremotechanges(ui, repo, peer,
671 onlyheads=sorted(target), force=True)
672 onlyheads=sorted(target), force=True)
672 else:
673 else:
673 source = repo
674 source = repo
674 heads = map(source.lookup, opts.get('branch', ()))
675 heads = map(source.lookup, opts.get('branch', ()))
675 cleanupfn = None
676 cleanupfn = None
676
677
677 try:
678 try:
678 if opts.get('continue'):
679 if opts.get('continue'):
679 tp.resume(repo, source, opts)
680 tp.resume(repo, source, opts)
680 return
681 return
681
682
682 tf = tp.transplantfilter(repo, source, p1)
683 tf = tp.transplantfilter(repo, source, p1)
683 if opts.get('prune'):
684 if opts.get('prune'):
684 prune = set(source.lookup(r)
685 prune = set(source.lookup(r)
685 for r in scmutil.revrange(source, opts.get('prune')))
686 for r in scmutil.revrange(source, opts.get('prune')))
686 matchfn = lambda x: tf(x) and x not in prune
687 matchfn = lambda x: tf(x) and x not in prune
687 else:
688 else:
688 matchfn = tf
689 matchfn = tf
689 merges = map(source.lookup, opts.get('merge', ()))
690 merges = map(source.lookup, opts.get('merge', ()))
690 revmap = {}
691 revmap = {}
691 if revs:
692 if revs:
692 for r in scmutil.revrange(source, revs):
693 for r in scmutil.revrange(source, revs):
693 revmap[int(r)] = source.lookup(r)
694 revmap[int(r)] = source.lookup(r)
694 elif opts.get('all') or not merges:
695 elif opts.get('all') or not merges:
695 if source != repo:
696 if source != repo:
696 alltransplants = incwalk(source, csets, match=matchfn)
697 alltransplants = incwalk(source, csets, match=matchfn)
697 else:
698 else:
698 alltransplants = transplantwalk(source, p1, heads,
699 alltransplants = transplantwalk(source, p1, heads,
699 match=matchfn)
700 match=matchfn)
700 if opts.get('all'):
701 if opts.get('all'):
701 revs = alltransplants
702 revs = alltransplants
702 else:
703 else:
703 revs, newmerges = browserevs(ui, source, alltransplants, opts)
704 revs, newmerges = browserevs(ui, source, alltransplants, opts)
704 merges.extend(newmerges)
705 merges.extend(newmerges)
705 for r in revs:
706 for r in revs:
706 revmap[source.changelog.rev(r)] = r
707 revmap[source.changelog.rev(r)] = r
707 for r in merges:
708 for r in merges:
708 revmap[source.changelog.rev(r)] = r
709 revmap[source.changelog.rev(r)] = r
709
710
710 tp.apply(repo, source, revmap, merges, opts)
711 tp.apply(repo, source, revmap, merges, opts)
711 finally:
712 finally:
712 if cleanupfn:
713 if cleanupfn:
713 cleanupfn()
714 cleanupfn()
714
715
715 revsetpredicate = registrar.revsetpredicate()
716 revsetpredicate = registrar.revsetpredicate()
716
717
717 @revsetpredicate('transplanted([set])')
718 @revsetpredicate('transplanted([set])')
718 def revsettransplanted(repo, subset, x):
719 def revsettransplanted(repo, subset, x):
719 """Transplanted changesets in set, or all transplanted changesets.
720 """Transplanted changesets in set, or all transplanted changesets.
720 """
721 """
721 if x:
722 if x:
722 s = revset.getset(repo, subset, x)
723 s = revset.getset(repo, subset, x)
723 else:
724 else:
724 s = subset
725 s = subset
725 return revset.baseset([r for r in s if
726 return revset.baseset([r for r in s if
726 repo[r].extra().get('transplant_source')])
727 repo[r].extra().get('transplant_source')])
727
728
728 templatekeyword = registrar.templatekeyword()
729 templatekeyword = registrar.templatekeyword()
729
730
730 @templatekeyword('transplanted')
731 @templatekeyword('transplanted')
731 def kwtransplanted(repo, ctx, **args):
732 def kwtransplanted(repo, ctx, **args):
732 """String. The node identifier of the transplanted
733 """String. The node identifier of the transplanted
733 changeset if any."""
734 changeset if any."""
734 n = ctx.extra().get('transplant_source')
735 n = ctx.extra().get('transplant_source')
735 return n and nodemod.hex(n) or ''
736 return n and nodemod.hex(n) or ''
736
737
737 def extsetup(ui):
738 def extsetup(ui):
738 cmdutil.unfinishedstates.append(
739 cmdutil.unfinishedstates.append(
739 ['transplant/journal', True, False, _('transplant in progress'),
740 ['transplant/journal', True, False, _('transplant in progress'),
740 _("use 'hg transplant --continue' or 'hg update' to abort")])
741 _("use 'hg transplant --continue' or 'hg update' to abort")])
741
742
742 # tell hggettext to extract docstrings from these functions:
743 # tell hggettext to extract docstrings from these functions:
743 i18nfunctions = [revsettransplanted, kwtransplanted]
744 i18nfunctions = [revsettransplanted, kwtransplanted]
@@ -1,1044 +1,1045 b''
1 # changegroup.py - Mercurial changegroup manipulation functions
1 # changegroup.py - Mercurial changegroup manipulation functions
2 #
2 #
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
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 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import os
10 import os
11 import struct
11 import struct
12 import tempfile
12 import tempfile
13 import weakref
13 import weakref
14
14
15 from .i18n import _
15 from .i18n import _
16 from .node import (
16 from .node import (
17 hex,
17 hex,
18 nullrev,
18 nullrev,
19 short,
19 short,
20 )
20 )
21
21
22 from . import (
22 from . import (
23 branchmap,
23 branchmap,
24 dagutil,
24 dagutil,
25 discovery,
25 discovery,
26 error,
26 error,
27 mdiff,
27 mdiff,
28 phases,
28 phases,
29 pycompat,
29 util,
30 util,
30 )
31 )
31
32
32 _CHANGEGROUPV1_DELTA_HEADER = "20s20s20s20s"
33 _CHANGEGROUPV1_DELTA_HEADER = "20s20s20s20s"
33 _CHANGEGROUPV2_DELTA_HEADER = "20s20s20s20s20s"
34 _CHANGEGROUPV2_DELTA_HEADER = "20s20s20s20s20s"
34 _CHANGEGROUPV3_DELTA_HEADER = ">20s20s20s20s20sH"
35 _CHANGEGROUPV3_DELTA_HEADER = ">20s20s20s20s20sH"
35
36
36 def readexactly(stream, n):
37 def readexactly(stream, n):
37 '''read n bytes from stream.read and abort if less was available'''
38 '''read n bytes from stream.read and abort if less was available'''
38 s = stream.read(n)
39 s = stream.read(n)
39 if len(s) < n:
40 if len(s) < n:
40 raise error.Abort(_("stream ended unexpectedly"
41 raise error.Abort(_("stream ended unexpectedly"
41 " (got %d bytes, expected %d)")
42 " (got %d bytes, expected %d)")
42 % (len(s), n))
43 % (len(s), n))
43 return s
44 return s
44
45
45 def getchunk(stream):
46 def getchunk(stream):
46 """return the next chunk from stream as a string"""
47 """return the next chunk from stream as a string"""
47 d = readexactly(stream, 4)
48 d = readexactly(stream, 4)
48 l = struct.unpack(">l", d)[0]
49 l = struct.unpack(">l", d)[0]
49 if l <= 4:
50 if l <= 4:
50 if l:
51 if l:
51 raise error.Abort(_("invalid chunk length %d") % l)
52 raise error.Abort(_("invalid chunk length %d") % l)
52 return ""
53 return ""
53 return readexactly(stream, l - 4)
54 return readexactly(stream, l - 4)
54
55
55 def chunkheader(length):
56 def chunkheader(length):
56 """return a changegroup chunk header (string)"""
57 """return a changegroup chunk header (string)"""
57 return struct.pack(">l", length + 4)
58 return struct.pack(">l", length + 4)
58
59
59 def closechunk():
60 def closechunk():
60 """return a changegroup chunk header (string) for a zero-length chunk"""
61 """return a changegroup chunk header (string) for a zero-length chunk"""
61 return struct.pack(">l", 0)
62 return struct.pack(">l", 0)
62
63
63 def combineresults(results):
64 def combineresults(results):
64 """logic to combine 0 or more addchangegroup results into one"""
65 """logic to combine 0 or more addchangegroup results into one"""
65 changedheads = 0
66 changedheads = 0
66 result = 1
67 result = 1
67 for ret in results:
68 for ret in results:
68 # If any changegroup result is 0, return 0
69 # If any changegroup result is 0, return 0
69 if ret == 0:
70 if ret == 0:
70 result = 0
71 result = 0
71 break
72 break
72 if ret < -1:
73 if ret < -1:
73 changedheads += ret + 1
74 changedheads += ret + 1
74 elif ret > 1:
75 elif ret > 1:
75 changedheads += ret - 1
76 changedheads += ret - 1
76 if changedheads > 0:
77 if changedheads > 0:
77 result = 1 + changedheads
78 result = 1 + changedheads
78 elif changedheads < 0:
79 elif changedheads < 0:
79 result = -1 + changedheads
80 result = -1 + changedheads
80 return result
81 return result
81
82
82 def writechunks(ui, chunks, filename, vfs=None):
83 def writechunks(ui, chunks, filename, vfs=None):
83 """Write chunks to a file and return its filename.
84 """Write chunks to a file and return its filename.
84
85
85 The stream is assumed to be a bundle file.
86 The stream is assumed to be a bundle file.
86 Existing files will not be overwritten.
87 Existing files will not be overwritten.
87 If no filename is specified, a temporary file is created.
88 If no filename is specified, a temporary file is created.
88 """
89 """
89 fh = None
90 fh = None
90 cleanup = None
91 cleanup = None
91 try:
92 try:
92 if filename:
93 if filename:
93 if vfs:
94 if vfs:
94 fh = vfs.open(filename, "wb")
95 fh = vfs.open(filename, "wb")
95 else:
96 else:
96 # Increase default buffer size because default is usually
97 # Increase default buffer size because default is usually
97 # small (4k is common on Linux).
98 # small (4k is common on Linux).
98 fh = open(filename, "wb", 131072)
99 fh = open(filename, "wb", 131072)
99 else:
100 else:
100 fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
101 fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
101 fh = os.fdopen(fd, "wb")
102 fh = os.fdopen(fd, pycompat.sysstr("wb"))
102 cleanup = filename
103 cleanup = filename
103 for c in chunks:
104 for c in chunks:
104 fh.write(c)
105 fh.write(c)
105 cleanup = None
106 cleanup = None
106 return filename
107 return filename
107 finally:
108 finally:
108 if fh is not None:
109 if fh is not None:
109 fh.close()
110 fh.close()
110 if cleanup is not None:
111 if cleanup is not None:
111 if filename and vfs:
112 if filename and vfs:
112 vfs.unlink(cleanup)
113 vfs.unlink(cleanup)
113 else:
114 else:
114 os.unlink(cleanup)
115 os.unlink(cleanup)
115
116
116 class cg1unpacker(object):
117 class cg1unpacker(object):
117 """Unpacker for cg1 changegroup streams.
118 """Unpacker for cg1 changegroup streams.
118
119
119 A changegroup unpacker handles the framing of the revision data in
120 A changegroup unpacker handles the framing of the revision data in
120 the wire format. Most consumers will want to use the apply()
121 the wire format. Most consumers will want to use the apply()
121 method to add the changes from the changegroup to a repository.
122 method to add the changes from the changegroup to a repository.
122
123
123 If you're forwarding a changegroup unmodified to another consumer,
124 If you're forwarding a changegroup unmodified to another consumer,
124 use getchunks(), which returns an iterator of changegroup
125 use getchunks(), which returns an iterator of changegroup
125 chunks. This is mostly useful for cases where you need to know the
126 chunks. This is mostly useful for cases where you need to know the
126 data stream has ended by observing the end of the changegroup.
127 data stream has ended by observing the end of the changegroup.
127
128
128 deltachunk() is useful only if you're applying delta data. Most
129 deltachunk() is useful only if you're applying delta data. Most
129 consumers should prefer apply() instead.
130 consumers should prefer apply() instead.
130
131
131 A few other public methods exist. Those are used only for
132 A few other public methods exist. Those are used only for
132 bundlerepo and some debug commands - their use is discouraged.
133 bundlerepo and some debug commands - their use is discouraged.
133 """
134 """
134 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
135 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
135 deltaheadersize = struct.calcsize(deltaheader)
136 deltaheadersize = struct.calcsize(deltaheader)
136 version = '01'
137 version = '01'
137 _grouplistcount = 1 # One list of files after the manifests
138 _grouplistcount = 1 # One list of files after the manifests
138
139
139 def __init__(self, fh, alg, extras=None):
140 def __init__(self, fh, alg, extras=None):
140 if alg is None:
141 if alg is None:
141 alg = 'UN'
142 alg = 'UN'
142 if alg not in util.compengines.supportedbundletypes:
143 if alg not in util.compengines.supportedbundletypes:
143 raise error.Abort(_('unknown stream compression type: %s')
144 raise error.Abort(_('unknown stream compression type: %s')
144 % alg)
145 % alg)
145 if alg == 'BZ':
146 if alg == 'BZ':
146 alg = '_truncatedBZ'
147 alg = '_truncatedBZ'
147
148
148 compengine = util.compengines.forbundletype(alg)
149 compengine = util.compengines.forbundletype(alg)
149 self._stream = compengine.decompressorreader(fh)
150 self._stream = compengine.decompressorreader(fh)
150 self._type = alg
151 self._type = alg
151 self.extras = extras or {}
152 self.extras = extras or {}
152 self.callback = None
153 self.callback = None
153
154
154 # These methods (compressed, read, seek, tell) all appear to only
155 # These methods (compressed, read, seek, tell) all appear to only
155 # be used by bundlerepo, but it's a little hard to tell.
156 # be used by bundlerepo, but it's a little hard to tell.
156 def compressed(self):
157 def compressed(self):
157 return self._type is not None and self._type != 'UN'
158 return self._type is not None and self._type != 'UN'
158 def read(self, l):
159 def read(self, l):
159 return self._stream.read(l)
160 return self._stream.read(l)
160 def seek(self, pos):
161 def seek(self, pos):
161 return self._stream.seek(pos)
162 return self._stream.seek(pos)
162 def tell(self):
163 def tell(self):
163 return self._stream.tell()
164 return self._stream.tell()
164 def close(self):
165 def close(self):
165 return self._stream.close()
166 return self._stream.close()
166
167
167 def _chunklength(self):
168 def _chunklength(self):
168 d = readexactly(self._stream, 4)
169 d = readexactly(self._stream, 4)
169 l = struct.unpack(">l", d)[0]
170 l = struct.unpack(">l", d)[0]
170 if l <= 4:
171 if l <= 4:
171 if l:
172 if l:
172 raise error.Abort(_("invalid chunk length %d") % l)
173 raise error.Abort(_("invalid chunk length %d") % l)
173 return 0
174 return 0
174 if self.callback:
175 if self.callback:
175 self.callback()
176 self.callback()
176 return l - 4
177 return l - 4
177
178
178 def changelogheader(self):
179 def changelogheader(self):
179 """v10 does not have a changelog header chunk"""
180 """v10 does not have a changelog header chunk"""
180 return {}
181 return {}
181
182
182 def manifestheader(self):
183 def manifestheader(self):
183 """v10 does not have a manifest header chunk"""
184 """v10 does not have a manifest header chunk"""
184 return {}
185 return {}
185
186
186 def filelogheader(self):
187 def filelogheader(self):
187 """return the header of the filelogs chunk, v10 only has the filename"""
188 """return the header of the filelogs chunk, v10 only has the filename"""
188 l = self._chunklength()
189 l = self._chunklength()
189 if not l:
190 if not l:
190 return {}
191 return {}
191 fname = readexactly(self._stream, l)
192 fname = readexactly(self._stream, l)
192 return {'filename': fname}
193 return {'filename': fname}
193
194
194 def _deltaheader(self, headertuple, prevnode):
195 def _deltaheader(self, headertuple, prevnode):
195 node, p1, p2, cs = headertuple
196 node, p1, p2, cs = headertuple
196 if prevnode is None:
197 if prevnode is None:
197 deltabase = p1
198 deltabase = p1
198 else:
199 else:
199 deltabase = prevnode
200 deltabase = prevnode
200 flags = 0
201 flags = 0
201 return node, p1, p2, deltabase, cs, flags
202 return node, p1, p2, deltabase, cs, flags
202
203
203 def deltachunk(self, prevnode):
204 def deltachunk(self, prevnode):
204 l = self._chunklength()
205 l = self._chunklength()
205 if not l:
206 if not l:
206 return {}
207 return {}
207 headerdata = readexactly(self._stream, self.deltaheadersize)
208 headerdata = readexactly(self._stream, self.deltaheadersize)
208 header = struct.unpack(self.deltaheader, headerdata)
209 header = struct.unpack(self.deltaheader, headerdata)
209 delta = readexactly(self._stream, l - self.deltaheadersize)
210 delta = readexactly(self._stream, l - self.deltaheadersize)
210 node, p1, p2, deltabase, cs, flags = self._deltaheader(header, prevnode)
211 node, p1, p2, deltabase, cs, flags = self._deltaheader(header, prevnode)
211 return {'node': node, 'p1': p1, 'p2': p2, 'cs': cs,
212 return {'node': node, 'p1': p1, 'p2': p2, 'cs': cs,
212 'deltabase': deltabase, 'delta': delta, 'flags': flags}
213 'deltabase': deltabase, 'delta': delta, 'flags': flags}
213
214
214 def getchunks(self):
215 def getchunks(self):
215 """returns all the chunks contains in the bundle
216 """returns all the chunks contains in the bundle
216
217
217 Used when you need to forward the binary stream to a file or another
218 Used when you need to forward the binary stream to a file or another
218 network API. To do so, it parse the changegroup data, otherwise it will
219 network API. To do so, it parse the changegroup data, otherwise it will
219 block in case of sshrepo because it don't know the end of the stream.
220 block in case of sshrepo because it don't know the end of the stream.
220 """
221 """
221 # an empty chunkgroup is the end of the changegroup
222 # an empty chunkgroup is the end of the changegroup
222 # a changegroup has at least 2 chunkgroups (changelog and manifest).
223 # a changegroup has at least 2 chunkgroups (changelog and manifest).
223 # after that, changegroup versions 1 and 2 have a series of groups
224 # after that, changegroup versions 1 and 2 have a series of groups
224 # with one group per file. changegroup 3 has a series of directory
225 # with one group per file. changegroup 3 has a series of directory
225 # manifests before the files.
226 # manifests before the files.
226 count = 0
227 count = 0
227 emptycount = 0
228 emptycount = 0
228 while emptycount < self._grouplistcount:
229 while emptycount < self._grouplistcount:
229 empty = True
230 empty = True
230 count += 1
231 count += 1
231 while True:
232 while True:
232 chunk = getchunk(self)
233 chunk = getchunk(self)
233 if not chunk:
234 if not chunk:
234 if empty and count > 2:
235 if empty and count > 2:
235 emptycount += 1
236 emptycount += 1
236 break
237 break
237 empty = False
238 empty = False
238 yield chunkheader(len(chunk))
239 yield chunkheader(len(chunk))
239 pos = 0
240 pos = 0
240 while pos < len(chunk):
241 while pos < len(chunk):
241 next = pos + 2**20
242 next = pos + 2**20
242 yield chunk[pos:next]
243 yield chunk[pos:next]
243 pos = next
244 pos = next
244 yield closechunk()
245 yield closechunk()
245
246
246 def _unpackmanifests(self, repo, revmap, trp, prog, numchanges):
247 def _unpackmanifests(self, repo, revmap, trp, prog, numchanges):
247 # We know that we'll never have more manifests than we had
248 # We know that we'll never have more manifests than we had
248 # changesets.
249 # changesets.
249 self.callback = prog(_('manifests'), numchanges)
250 self.callback = prog(_('manifests'), numchanges)
250 # no need to check for empty manifest group here:
251 # no need to check for empty manifest group here:
251 # if the result of the merge of 1 and 2 is the same in 3 and 4,
252 # if the result of the merge of 1 and 2 is the same in 3 and 4,
252 # no new manifest will be created and the manifest group will
253 # no new manifest will be created and the manifest group will
253 # be empty during the pull
254 # be empty during the pull
254 self.manifestheader()
255 self.manifestheader()
255 repo.manifestlog._revlog.addgroup(self, revmap, trp)
256 repo.manifestlog._revlog.addgroup(self, revmap, trp)
256 repo.ui.progress(_('manifests'), None)
257 repo.ui.progress(_('manifests'), None)
257 self.callback = None
258 self.callback = None
258
259
259 def apply(self, repo, srctype, url, emptyok=False,
260 def apply(self, repo, srctype, url, emptyok=False,
260 targetphase=phases.draft, expectedtotal=None):
261 targetphase=phases.draft, expectedtotal=None):
261 """Add the changegroup returned by source.read() to this repo.
262 """Add the changegroup returned by source.read() to this repo.
262 srctype is a string like 'push', 'pull', or 'unbundle'. url is
263 srctype is a string like 'push', 'pull', or 'unbundle'. url is
263 the URL of the repo where this changegroup is coming from.
264 the URL of the repo where this changegroup is coming from.
264
265
265 Return an integer summarizing the change to this repo:
266 Return an integer summarizing the change to this repo:
266 - nothing changed or no source: 0
267 - nothing changed or no source: 0
267 - more heads than before: 1+added heads (2..n)
268 - more heads than before: 1+added heads (2..n)
268 - fewer heads than before: -1-removed heads (-2..-n)
269 - fewer heads than before: -1-removed heads (-2..-n)
269 - number of heads stays the same: 1
270 - number of heads stays the same: 1
270 """
271 """
271 repo = repo.unfiltered()
272 repo = repo.unfiltered()
272 def csmap(x):
273 def csmap(x):
273 repo.ui.debug("add changeset %s\n" % short(x))
274 repo.ui.debug("add changeset %s\n" % short(x))
274 return len(cl)
275 return len(cl)
275
276
276 def revmap(x):
277 def revmap(x):
277 return cl.rev(x)
278 return cl.rev(x)
278
279
279 changesets = files = revisions = 0
280 changesets = files = revisions = 0
280
281
281 try:
282 try:
282 with repo.transaction("\n".join([srctype,
283 with repo.transaction("\n".join([srctype,
283 util.hidepassword(url)])) as tr:
284 util.hidepassword(url)])) as tr:
284 # The transaction could have been created before and already
285 # The transaction could have been created before and already
285 # carries source information. In this case we use the top
286 # carries source information. In this case we use the top
286 # level data. We overwrite the argument because we need to use
287 # level data. We overwrite the argument because we need to use
287 # the top level value (if they exist) in this function.
288 # the top level value (if they exist) in this function.
288 srctype = tr.hookargs.setdefault('source', srctype)
289 srctype = tr.hookargs.setdefault('source', srctype)
289 url = tr.hookargs.setdefault('url', url)
290 url = tr.hookargs.setdefault('url', url)
290 repo.hook('prechangegroup', throw=True, **tr.hookargs)
291 repo.hook('prechangegroup', throw=True, **tr.hookargs)
291
292
292 # write changelog data to temp files so concurrent readers
293 # write changelog data to temp files so concurrent readers
293 # will not see an inconsistent view
294 # will not see an inconsistent view
294 cl = repo.changelog
295 cl = repo.changelog
295 cl.delayupdate(tr)
296 cl.delayupdate(tr)
296 oldheads = cl.heads()
297 oldheads = cl.heads()
297
298
298 trp = weakref.proxy(tr)
299 trp = weakref.proxy(tr)
299 # pull off the changeset group
300 # pull off the changeset group
300 repo.ui.status(_("adding changesets\n"))
301 repo.ui.status(_("adding changesets\n"))
301 clstart = len(cl)
302 clstart = len(cl)
302 class prog(object):
303 class prog(object):
303 def __init__(self, step, total):
304 def __init__(self, step, total):
304 self._step = step
305 self._step = step
305 self._total = total
306 self._total = total
306 self._count = 1
307 self._count = 1
307 def __call__(self):
308 def __call__(self):
308 repo.ui.progress(self._step, self._count,
309 repo.ui.progress(self._step, self._count,
309 unit=_('chunks'), total=self._total)
310 unit=_('chunks'), total=self._total)
310 self._count += 1
311 self._count += 1
311 self.callback = prog(_('changesets'), expectedtotal)
312 self.callback = prog(_('changesets'), expectedtotal)
312
313
313 efiles = set()
314 efiles = set()
314 def onchangelog(cl, node):
315 def onchangelog(cl, node):
315 efiles.update(cl.readfiles(node))
316 efiles.update(cl.readfiles(node))
316
317
317 self.changelogheader()
318 self.changelogheader()
318 srccontent = cl.addgroup(self, csmap, trp,
319 srccontent = cl.addgroup(self, csmap, trp,
319 addrevisioncb=onchangelog)
320 addrevisioncb=onchangelog)
320 efiles = len(efiles)
321 efiles = len(efiles)
321
322
322 if not (srccontent or emptyok):
323 if not (srccontent or emptyok):
323 raise error.Abort(_("received changelog group is empty"))
324 raise error.Abort(_("received changelog group is empty"))
324 clend = len(cl)
325 clend = len(cl)
325 changesets = clend - clstart
326 changesets = clend - clstart
326 repo.ui.progress(_('changesets'), None)
327 repo.ui.progress(_('changesets'), None)
327 self.callback = None
328 self.callback = None
328
329
329 # pull off the manifest group
330 # pull off the manifest group
330 repo.ui.status(_("adding manifests\n"))
331 repo.ui.status(_("adding manifests\n"))
331 self._unpackmanifests(repo, revmap, trp, prog, changesets)
332 self._unpackmanifests(repo, revmap, trp, prog, changesets)
332
333
333 needfiles = {}
334 needfiles = {}
334 if repo.ui.configbool('server', 'validate', default=False):
335 if repo.ui.configbool('server', 'validate', default=False):
335 cl = repo.changelog
336 cl = repo.changelog
336 ml = repo.manifestlog
337 ml = repo.manifestlog
337 # validate incoming csets have their manifests
338 # validate incoming csets have their manifests
338 for cset in xrange(clstart, clend):
339 for cset in xrange(clstart, clend):
339 mfnode = cl.changelogrevision(cset).manifest
340 mfnode = cl.changelogrevision(cset).manifest
340 mfest = ml[mfnode].readdelta()
341 mfest = ml[mfnode].readdelta()
341 # store file nodes we must see
342 # store file nodes we must see
342 for f, n in mfest.iteritems():
343 for f, n in mfest.iteritems():
343 needfiles.setdefault(f, set()).add(n)
344 needfiles.setdefault(f, set()).add(n)
344
345
345 # process the files
346 # process the files
346 repo.ui.status(_("adding file changes\n"))
347 repo.ui.status(_("adding file changes\n"))
347 newrevs, newfiles = _addchangegroupfiles(
348 newrevs, newfiles = _addchangegroupfiles(
348 repo, self, revmap, trp, efiles, needfiles)
349 repo, self, revmap, trp, efiles, needfiles)
349 revisions += newrevs
350 revisions += newrevs
350 files += newfiles
351 files += newfiles
351
352
352 dh = 0
353 dh = 0
353 if oldheads:
354 if oldheads:
354 heads = cl.heads()
355 heads = cl.heads()
355 dh = len(heads) - len(oldheads)
356 dh = len(heads) - len(oldheads)
356 for h in heads:
357 for h in heads:
357 if h not in oldheads and repo[h].closesbranch():
358 if h not in oldheads and repo[h].closesbranch():
358 dh -= 1
359 dh -= 1
359 htext = ""
360 htext = ""
360 if dh:
361 if dh:
361 htext = _(" (%+d heads)") % dh
362 htext = _(" (%+d heads)") % dh
362
363
363 repo.ui.status(_("added %d changesets"
364 repo.ui.status(_("added %d changesets"
364 " with %d changes to %d files%s\n")
365 " with %d changes to %d files%s\n")
365 % (changesets, revisions, files, htext))
366 % (changesets, revisions, files, htext))
366 repo.invalidatevolatilesets()
367 repo.invalidatevolatilesets()
367
368
368 if changesets > 0:
369 if changesets > 0:
369 if 'node' not in tr.hookargs:
370 if 'node' not in tr.hookargs:
370 tr.hookargs['node'] = hex(cl.node(clstart))
371 tr.hookargs['node'] = hex(cl.node(clstart))
371 tr.hookargs['node_last'] = hex(cl.node(clend - 1))
372 tr.hookargs['node_last'] = hex(cl.node(clend - 1))
372 hookargs = dict(tr.hookargs)
373 hookargs = dict(tr.hookargs)
373 else:
374 else:
374 hookargs = dict(tr.hookargs)
375 hookargs = dict(tr.hookargs)
375 hookargs['node'] = hex(cl.node(clstart))
376 hookargs['node'] = hex(cl.node(clstart))
376 hookargs['node_last'] = hex(cl.node(clend - 1))
377 hookargs['node_last'] = hex(cl.node(clend - 1))
377 repo.hook('pretxnchangegroup', throw=True, **hookargs)
378 repo.hook('pretxnchangegroup', throw=True, **hookargs)
378
379
379 added = [cl.node(r) for r in xrange(clstart, clend)]
380 added = [cl.node(r) for r in xrange(clstart, clend)]
380 publishing = repo.publishing()
381 publishing = repo.publishing()
381 if srctype in ('push', 'serve'):
382 if srctype in ('push', 'serve'):
382 # Old servers can not push the boundary themselves.
383 # Old servers can not push the boundary themselves.
383 # New servers won't push the boundary if changeset already
384 # New servers won't push the boundary if changeset already
384 # exists locally as secret
385 # exists locally as secret
385 #
386 #
386 # We should not use added here but the list of all change in
387 # We should not use added here but the list of all change in
387 # the bundle
388 # the bundle
388 if publishing:
389 if publishing:
389 phases.advanceboundary(repo, tr, phases.public,
390 phases.advanceboundary(repo, tr, phases.public,
390 srccontent)
391 srccontent)
391 else:
392 else:
392 # Those changesets have been pushed from the
393 # Those changesets have been pushed from the
393 # outside, their phases are going to be pushed
394 # outside, their phases are going to be pushed
394 # alongside. Therefor `targetphase` is
395 # alongside. Therefor `targetphase` is
395 # ignored.
396 # ignored.
396 phases.advanceboundary(repo, tr, phases.draft,
397 phases.advanceboundary(repo, tr, phases.draft,
397 srccontent)
398 srccontent)
398 phases.retractboundary(repo, tr, phases.draft, added)
399 phases.retractboundary(repo, tr, phases.draft, added)
399 elif srctype != 'strip':
400 elif srctype != 'strip':
400 # publishing only alter behavior during push
401 # publishing only alter behavior during push
401 #
402 #
402 # strip should not touch boundary at all
403 # strip should not touch boundary at all
403 phases.retractboundary(repo, tr, targetphase, added)
404 phases.retractboundary(repo, tr, targetphase, added)
404
405
405 if changesets > 0:
406 if changesets > 0:
406 if srctype != 'strip':
407 if srctype != 'strip':
407 # During strip, branchcache is invalid but
408 # During strip, branchcache is invalid but
408 # coming call to `destroyed` will repair it.
409 # coming call to `destroyed` will repair it.
409 # In other case we can safely update cache on
410 # In other case we can safely update cache on
410 # disk.
411 # disk.
411 repo.ui.debug('updating the branch cache\n')
412 repo.ui.debug('updating the branch cache\n')
412 branchmap.updatecache(repo.filtered('served'))
413 branchmap.updatecache(repo.filtered('served'))
413
414
414 def runhooks():
415 def runhooks():
415 # These hooks run when the lock releases, not when the
416 # These hooks run when the lock releases, not when the
416 # transaction closes. So it's possible for the changelog
417 # transaction closes. So it's possible for the changelog
417 # to have changed since we last saw it.
418 # to have changed since we last saw it.
418 if clstart >= len(repo):
419 if clstart >= len(repo):
419 return
420 return
420
421
421 repo.hook("changegroup", **hookargs)
422 repo.hook("changegroup", **hookargs)
422
423
423 for n in added:
424 for n in added:
424 args = hookargs.copy()
425 args = hookargs.copy()
425 args['node'] = hex(n)
426 args['node'] = hex(n)
426 del args['node_last']
427 del args['node_last']
427 repo.hook("incoming", **args)
428 repo.hook("incoming", **args)
428
429
429 newheads = [h for h in repo.heads()
430 newheads = [h for h in repo.heads()
430 if h not in oldheads]
431 if h not in oldheads]
431 repo.ui.log("incoming",
432 repo.ui.log("incoming",
432 "%s incoming changes - new heads: %s\n",
433 "%s incoming changes - new heads: %s\n",
433 len(added),
434 len(added),
434 ', '.join([hex(c[:6]) for c in newheads]))
435 ', '.join([hex(c[:6]) for c in newheads]))
435
436
436 tr.addpostclose('changegroup-runhooks-%020i' % clstart,
437 tr.addpostclose('changegroup-runhooks-%020i' % clstart,
437 lambda tr: repo._afterlock(runhooks))
438 lambda tr: repo._afterlock(runhooks))
438 finally:
439 finally:
439 repo.ui.flush()
440 repo.ui.flush()
440 # never return 0 here:
441 # never return 0 here:
441 if dh < 0:
442 if dh < 0:
442 return dh - 1
443 return dh - 1
443 else:
444 else:
444 return dh + 1
445 return dh + 1
445
446
446 class cg2unpacker(cg1unpacker):
447 class cg2unpacker(cg1unpacker):
447 """Unpacker for cg2 streams.
448 """Unpacker for cg2 streams.
448
449
449 cg2 streams add support for generaldelta, so the delta header
450 cg2 streams add support for generaldelta, so the delta header
450 format is slightly different. All other features about the data
451 format is slightly different. All other features about the data
451 remain the same.
452 remain the same.
452 """
453 """
453 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
454 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
454 deltaheadersize = struct.calcsize(deltaheader)
455 deltaheadersize = struct.calcsize(deltaheader)
455 version = '02'
456 version = '02'
456
457
457 def _deltaheader(self, headertuple, prevnode):
458 def _deltaheader(self, headertuple, prevnode):
458 node, p1, p2, deltabase, cs = headertuple
459 node, p1, p2, deltabase, cs = headertuple
459 flags = 0
460 flags = 0
460 return node, p1, p2, deltabase, cs, flags
461 return node, p1, p2, deltabase, cs, flags
461
462
462 class cg3unpacker(cg2unpacker):
463 class cg3unpacker(cg2unpacker):
463 """Unpacker for cg3 streams.
464 """Unpacker for cg3 streams.
464
465
465 cg3 streams add support for exchanging treemanifests and revlog
466 cg3 streams add support for exchanging treemanifests and revlog
466 flags. It adds the revlog flags to the delta header and an empty chunk
467 flags. It adds the revlog flags to the delta header and an empty chunk
467 separating manifests and files.
468 separating manifests and files.
468 """
469 """
469 deltaheader = _CHANGEGROUPV3_DELTA_HEADER
470 deltaheader = _CHANGEGROUPV3_DELTA_HEADER
470 deltaheadersize = struct.calcsize(deltaheader)
471 deltaheadersize = struct.calcsize(deltaheader)
471 version = '03'
472 version = '03'
472 _grouplistcount = 2 # One list of manifests and one list of files
473 _grouplistcount = 2 # One list of manifests and one list of files
473
474
474 def _deltaheader(self, headertuple, prevnode):
475 def _deltaheader(self, headertuple, prevnode):
475 node, p1, p2, deltabase, cs, flags = headertuple
476 node, p1, p2, deltabase, cs, flags = headertuple
476 return node, p1, p2, deltabase, cs, flags
477 return node, p1, p2, deltabase, cs, flags
477
478
478 def _unpackmanifests(self, repo, revmap, trp, prog, numchanges):
479 def _unpackmanifests(self, repo, revmap, trp, prog, numchanges):
479 super(cg3unpacker, self)._unpackmanifests(repo, revmap, trp, prog,
480 super(cg3unpacker, self)._unpackmanifests(repo, revmap, trp, prog,
480 numchanges)
481 numchanges)
481 for chunkdata in iter(self.filelogheader, {}):
482 for chunkdata in iter(self.filelogheader, {}):
482 # If we get here, there are directory manifests in the changegroup
483 # If we get here, there are directory manifests in the changegroup
483 d = chunkdata["filename"]
484 d = chunkdata["filename"]
484 repo.ui.debug("adding %s revisions\n" % d)
485 repo.ui.debug("adding %s revisions\n" % d)
485 dirlog = repo.manifestlog._revlog.dirlog(d)
486 dirlog = repo.manifestlog._revlog.dirlog(d)
486 if not dirlog.addgroup(self, revmap, trp):
487 if not dirlog.addgroup(self, revmap, trp):
487 raise error.Abort(_("received dir revlog group is empty"))
488 raise error.Abort(_("received dir revlog group is empty"))
488
489
489 class headerlessfixup(object):
490 class headerlessfixup(object):
490 def __init__(self, fh, h):
491 def __init__(self, fh, h):
491 self._h = h
492 self._h = h
492 self._fh = fh
493 self._fh = fh
493 def read(self, n):
494 def read(self, n):
494 if self._h:
495 if self._h:
495 d, self._h = self._h[:n], self._h[n:]
496 d, self._h = self._h[:n], self._h[n:]
496 if len(d) < n:
497 if len(d) < n:
497 d += readexactly(self._fh, n - len(d))
498 d += readexactly(self._fh, n - len(d))
498 return d
499 return d
499 return readexactly(self._fh, n)
500 return readexactly(self._fh, n)
500
501
501 class cg1packer(object):
502 class cg1packer(object):
502 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
503 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
503 version = '01'
504 version = '01'
504 def __init__(self, repo, bundlecaps=None):
505 def __init__(self, repo, bundlecaps=None):
505 """Given a source repo, construct a bundler.
506 """Given a source repo, construct a bundler.
506
507
507 bundlecaps is optional and can be used to specify the set of
508 bundlecaps is optional and can be used to specify the set of
508 capabilities which can be used to build the bundle.
509 capabilities which can be used to build the bundle.
509 """
510 """
510 # Set of capabilities we can use to build the bundle.
511 # Set of capabilities we can use to build the bundle.
511 if bundlecaps is None:
512 if bundlecaps is None:
512 bundlecaps = set()
513 bundlecaps = set()
513 self._bundlecaps = bundlecaps
514 self._bundlecaps = bundlecaps
514 # experimental config: bundle.reorder
515 # experimental config: bundle.reorder
515 reorder = repo.ui.config('bundle', 'reorder', 'auto')
516 reorder = repo.ui.config('bundle', 'reorder', 'auto')
516 if reorder == 'auto':
517 if reorder == 'auto':
517 reorder = None
518 reorder = None
518 else:
519 else:
519 reorder = util.parsebool(reorder)
520 reorder = util.parsebool(reorder)
520 self._repo = repo
521 self._repo = repo
521 self._reorder = reorder
522 self._reorder = reorder
522 self._progress = repo.ui.progress
523 self._progress = repo.ui.progress
523 if self._repo.ui.verbose and not self._repo.ui.debugflag:
524 if self._repo.ui.verbose and not self._repo.ui.debugflag:
524 self._verbosenote = self._repo.ui.note
525 self._verbosenote = self._repo.ui.note
525 else:
526 else:
526 self._verbosenote = lambda s: None
527 self._verbosenote = lambda s: None
527
528
528 def close(self):
529 def close(self):
529 return closechunk()
530 return closechunk()
530
531
531 def fileheader(self, fname):
532 def fileheader(self, fname):
532 return chunkheader(len(fname)) + fname
533 return chunkheader(len(fname)) + fname
533
534
534 # Extracted both for clarity and for overriding in extensions.
535 # Extracted both for clarity and for overriding in extensions.
535 def _sortgroup(self, revlog, nodelist, lookup):
536 def _sortgroup(self, revlog, nodelist, lookup):
536 """Sort nodes for change group and turn them into revnums."""
537 """Sort nodes for change group and turn them into revnums."""
537 # for generaldelta revlogs, we linearize the revs; this will both be
538 # for generaldelta revlogs, we linearize the revs; this will both be
538 # much quicker and generate a much smaller bundle
539 # much quicker and generate a much smaller bundle
539 if (revlog._generaldelta and self._reorder is None) or self._reorder:
540 if (revlog._generaldelta and self._reorder is None) or self._reorder:
540 dag = dagutil.revlogdag(revlog)
541 dag = dagutil.revlogdag(revlog)
541 return dag.linearize(set(revlog.rev(n) for n in nodelist))
542 return dag.linearize(set(revlog.rev(n) for n in nodelist))
542 else:
543 else:
543 return sorted([revlog.rev(n) for n in nodelist])
544 return sorted([revlog.rev(n) for n in nodelist])
544
545
545 def group(self, nodelist, revlog, lookup, units=None):
546 def group(self, nodelist, revlog, lookup, units=None):
546 """Calculate a delta group, yielding a sequence of changegroup chunks
547 """Calculate a delta group, yielding a sequence of changegroup chunks
547 (strings).
548 (strings).
548
549
549 Given a list of changeset revs, return a set of deltas and
550 Given a list of changeset revs, return a set of deltas and
550 metadata corresponding to nodes. The first delta is
551 metadata corresponding to nodes. The first delta is
551 first parent(nodelist[0]) -> nodelist[0], the receiver is
552 first parent(nodelist[0]) -> nodelist[0], the receiver is
552 guaranteed to have this parent as it has all history before
553 guaranteed to have this parent as it has all history before
553 these changesets. In the case firstparent is nullrev the
554 these changesets. In the case firstparent is nullrev the
554 changegroup starts with a full revision.
555 changegroup starts with a full revision.
555
556
556 If units is not None, progress detail will be generated, units specifies
557 If units is not None, progress detail will be generated, units specifies
557 the type of revlog that is touched (changelog, manifest, etc.).
558 the type of revlog that is touched (changelog, manifest, etc.).
558 """
559 """
559 # if we don't have any revisions touched by these changesets, bail
560 # if we don't have any revisions touched by these changesets, bail
560 if len(nodelist) == 0:
561 if len(nodelist) == 0:
561 yield self.close()
562 yield self.close()
562 return
563 return
563
564
564 revs = self._sortgroup(revlog, nodelist, lookup)
565 revs = self._sortgroup(revlog, nodelist, lookup)
565
566
566 # add the parent of the first rev
567 # add the parent of the first rev
567 p = revlog.parentrevs(revs[0])[0]
568 p = revlog.parentrevs(revs[0])[0]
568 revs.insert(0, p)
569 revs.insert(0, p)
569
570
570 # build deltas
571 # build deltas
571 total = len(revs) - 1
572 total = len(revs) - 1
572 msgbundling = _('bundling')
573 msgbundling = _('bundling')
573 for r in xrange(len(revs) - 1):
574 for r in xrange(len(revs) - 1):
574 if units is not None:
575 if units is not None:
575 self._progress(msgbundling, r + 1, unit=units, total=total)
576 self._progress(msgbundling, r + 1, unit=units, total=total)
576 prev, curr = revs[r], revs[r + 1]
577 prev, curr = revs[r], revs[r + 1]
577 linknode = lookup(revlog.node(curr))
578 linknode = lookup(revlog.node(curr))
578 for c in self.revchunk(revlog, curr, prev, linknode):
579 for c in self.revchunk(revlog, curr, prev, linknode):
579 yield c
580 yield c
580
581
581 if units is not None:
582 if units is not None:
582 self._progress(msgbundling, None)
583 self._progress(msgbundling, None)
583 yield self.close()
584 yield self.close()
584
585
585 # filter any nodes that claim to be part of the known set
586 # filter any nodes that claim to be part of the known set
586 def prune(self, revlog, missing, commonrevs):
587 def prune(self, revlog, missing, commonrevs):
587 rr, rl = revlog.rev, revlog.linkrev
588 rr, rl = revlog.rev, revlog.linkrev
588 return [n for n in missing if rl(rr(n)) not in commonrevs]
589 return [n for n in missing if rl(rr(n)) not in commonrevs]
589
590
590 def _packmanifests(self, dir, mfnodes, lookuplinknode):
591 def _packmanifests(self, dir, mfnodes, lookuplinknode):
591 """Pack flat manifests into a changegroup stream."""
592 """Pack flat manifests into a changegroup stream."""
592 assert not dir
593 assert not dir
593 for chunk in self.group(mfnodes, self._repo.manifestlog._revlog,
594 for chunk in self.group(mfnodes, self._repo.manifestlog._revlog,
594 lookuplinknode, units=_('manifests')):
595 lookuplinknode, units=_('manifests')):
595 yield chunk
596 yield chunk
596
597
597 def _manifestsdone(self):
598 def _manifestsdone(self):
598 return ''
599 return ''
599
600
600 def generate(self, commonrevs, clnodes, fastpathlinkrev, source):
601 def generate(self, commonrevs, clnodes, fastpathlinkrev, source):
601 '''yield a sequence of changegroup chunks (strings)'''
602 '''yield a sequence of changegroup chunks (strings)'''
602 repo = self._repo
603 repo = self._repo
603 cl = repo.changelog
604 cl = repo.changelog
604
605
605 clrevorder = {}
606 clrevorder = {}
606 mfs = {} # needed manifests
607 mfs = {} # needed manifests
607 fnodes = {} # needed file nodes
608 fnodes = {} # needed file nodes
608 changedfiles = set()
609 changedfiles = set()
609
610
610 # Callback for the changelog, used to collect changed files and manifest
611 # Callback for the changelog, used to collect changed files and manifest
611 # nodes.
612 # nodes.
612 # Returns the linkrev node (identity in the changelog case).
613 # Returns the linkrev node (identity in the changelog case).
613 def lookupcl(x):
614 def lookupcl(x):
614 c = cl.read(x)
615 c = cl.read(x)
615 clrevorder[x] = len(clrevorder)
616 clrevorder[x] = len(clrevorder)
616 n = c[0]
617 n = c[0]
617 # record the first changeset introducing this manifest version
618 # record the first changeset introducing this manifest version
618 mfs.setdefault(n, x)
619 mfs.setdefault(n, x)
619 # Record a complete list of potentially-changed files in
620 # Record a complete list of potentially-changed files in
620 # this manifest.
621 # this manifest.
621 changedfiles.update(c[3])
622 changedfiles.update(c[3])
622 return x
623 return x
623
624
624 self._verbosenote(_('uncompressed size of bundle content:\n'))
625 self._verbosenote(_('uncompressed size of bundle content:\n'))
625 size = 0
626 size = 0
626 for chunk in self.group(clnodes, cl, lookupcl, units=_('changesets')):
627 for chunk in self.group(clnodes, cl, lookupcl, units=_('changesets')):
627 size += len(chunk)
628 size += len(chunk)
628 yield chunk
629 yield chunk
629 self._verbosenote(_('%8.i (changelog)\n') % size)
630 self._verbosenote(_('%8.i (changelog)\n') % size)
630
631
631 # We need to make sure that the linkrev in the changegroup refers to
632 # We need to make sure that the linkrev in the changegroup refers to
632 # the first changeset that introduced the manifest or file revision.
633 # the first changeset that introduced the manifest or file revision.
633 # The fastpath is usually safer than the slowpath, because the filelogs
634 # The fastpath is usually safer than the slowpath, because the filelogs
634 # are walked in revlog order.
635 # are walked in revlog order.
635 #
636 #
636 # When taking the slowpath with reorder=None and the manifest revlog
637 # When taking the slowpath with reorder=None and the manifest revlog
637 # uses generaldelta, the manifest may be walked in the "wrong" order.
638 # uses generaldelta, the manifest may be walked in the "wrong" order.
638 # Without 'clrevorder', we would get an incorrect linkrev (see fix in
639 # Without 'clrevorder', we would get an incorrect linkrev (see fix in
639 # cc0ff93d0c0c).
640 # cc0ff93d0c0c).
640 #
641 #
641 # When taking the fastpath, we are only vulnerable to reordering
642 # When taking the fastpath, we are only vulnerable to reordering
642 # of the changelog itself. The changelog never uses generaldelta, so
643 # of the changelog itself. The changelog never uses generaldelta, so
643 # it is only reordered when reorder=True. To handle this case, we
644 # it is only reordered when reorder=True. To handle this case, we
644 # simply take the slowpath, which already has the 'clrevorder' logic.
645 # simply take the slowpath, which already has the 'clrevorder' logic.
645 # This was also fixed in cc0ff93d0c0c.
646 # This was also fixed in cc0ff93d0c0c.
646 fastpathlinkrev = fastpathlinkrev and not self._reorder
647 fastpathlinkrev = fastpathlinkrev and not self._reorder
647 # Treemanifests don't work correctly with fastpathlinkrev
648 # Treemanifests don't work correctly with fastpathlinkrev
648 # either, because we don't discover which directory nodes to
649 # either, because we don't discover which directory nodes to
649 # send along with files. This could probably be fixed.
650 # send along with files. This could probably be fixed.
650 fastpathlinkrev = fastpathlinkrev and (
651 fastpathlinkrev = fastpathlinkrev and (
651 'treemanifest' not in repo.requirements)
652 'treemanifest' not in repo.requirements)
652
653
653 for chunk in self.generatemanifests(commonrevs, clrevorder,
654 for chunk in self.generatemanifests(commonrevs, clrevorder,
654 fastpathlinkrev, mfs, fnodes):
655 fastpathlinkrev, mfs, fnodes):
655 yield chunk
656 yield chunk
656 mfs.clear()
657 mfs.clear()
657 clrevs = set(cl.rev(x) for x in clnodes)
658 clrevs = set(cl.rev(x) for x in clnodes)
658
659
659 if not fastpathlinkrev:
660 if not fastpathlinkrev:
660 def linknodes(unused, fname):
661 def linknodes(unused, fname):
661 return fnodes.get(fname, {})
662 return fnodes.get(fname, {})
662 else:
663 else:
663 cln = cl.node
664 cln = cl.node
664 def linknodes(filerevlog, fname):
665 def linknodes(filerevlog, fname):
665 llr = filerevlog.linkrev
666 llr = filerevlog.linkrev
666 fln = filerevlog.node
667 fln = filerevlog.node
667 revs = ((r, llr(r)) for r in filerevlog)
668 revs = ((r, llr(r)) for r in filerevlog)
668 return dict((fln(r), cln(lr)) for r, lr in revs if lr in clrevs)
669 return dict((fln(r), cln(lr)) for r, lr in revs if lr in clrevs)
669
670
670 for chunk in self.generatefiles(changedfiles, linknodes, commonrevs,
671 for chunk in self.generatefiles(changedfiles, linknodes, commonrevs,
671 source):
672 source):
672 yield chunk
673 yield chunk
673
674
674 yield self.close()
675 yield self.close()
675
676
676 if clnodes:
677 if clnodes:
677 repo.hook('outgoing', node=hex(clnodes[0]), source=source)
678 repo.hook('outgoing', node=hex(clnodes[0]), source=source)
678
679
679 def generatemanifests(self, commonrevs, clrevorder, fastpathlinkrev, mfs,
680 def generatemanifests(self, commonrevs, clrevorder, fastpathlinkrev, mfs,
680 fnodes):
681 fnodes):
681 repo = self._repo
682 repo = self._repo
682 mfl = repo.manifestlog
683 mfl = repo.manifestlog
683 dirlog = mfl._revlog.dirlog
684 dirlog = mfl._revlog.dirlog
684 tmfnodes = {'': mfs}
685 tmfnodes = {'': mfs}
685
686
686 # Callback for the manifest, used to collect linkrevs for filelog
687 # Callback for the manifest, used to collect linkrevs for filelog
687 # revisions.
688 # revisions.
688 # Returns the linkrev node (collected in lookupcl).
689 # Returns the linkrev node (collected in lookupcl).
689 def makelookupmflinknode(dir):
690 def makelookupmflinknode(dir):
690 if fastpathlinkrev:
691 if fastpathlinkrev:
691 assert not dir
692 assert not dir
692 return mfs.__getitem__
693 return mfs.__getitem__
693
694
694 def lookupmflinknode(x):
695 def lookupmflinknode(x):
695 """Callback for looking up the linknode for manifests.
696 """Callback for looking up the linknode for manifests.
696
697
697 Returns the linkrev node for the specified manifest.
698 Returns the linkrev node for the specified manifest.
698
699
699 SIDE EFFECT:
700 SIDE EFFECT:
700
701
701 1) fclnodes gets populated with the list of relevant
702 1) fclnodes gets populated with the list of relevant
702 file nodes if we're not using fastpathlinkrev
703 file nodes if we're not using fastpathlinkrev
703 2) When treemanifests are in use, collects treemanifest nodes
704 2) When treemanifests are in use, collects treemanifest nodes
704 to send
705 to send
705
706
706 Note that this means manifests must be completely sent to
707 Note that this means manifests must be completely sent to
707 the client before you can trust the list of files and
708 the client before you can trust the list of files and
708 treemanifests to send.
709 treemanifests to send.
709 """
710 """
710 clnode = tmfnodes[dir][x]
711 clnode = tmfnodes[dir][x]
711 mdata = mfl.get(dir, x).readfast(shallow=True)
712 mdata = mfl.get(dir, x).readfast(shallow=True)
712 for p, n, fl in mdata.iterentries():
713 for p, n, fl in mdata.iterentries():
713 if fl == 't': # subdirectory manifest
714 if fl == 't': # subdirectory manifest
714 subdir = dir + p + '/'
715 subdir = dir + p + '/'
715 tmfclnodes = tmfnodes.setdefault(subdir, {})
716 tmfclnodes = tmfnodes.setdefault(subdir, {})
716 tmfclnode = tmfclnodes.setdefault(n, clnode)
717 tmfclnode = tmfclnodes.setdefault(n, clnode)
717 if clrevorder[clnode] < clrevorder[tmfclnode]:
718 if clrevorder[clnode] < clrevorder[tmfclnode]:
718 tmfclnodes[n] = clnode
719 tmfclnodes[n] = clnode
719 else:
720 else:
720 f = dir + p
721 f = dir + p
721 fclnodes = fnodes.setdefault(f, {})
722 fclnodes = fnodes.setdefault(f, {})
722 fclnode = fclnodes.setdefault(n, clnode)
723 fclnode = fclnodes.setdefault(n, clnode)
723 if clrevorder[clnode] < clrevorder[fclnode]:
724 if clrevorder[clnode] < clrevorder[fclnode]:
724 fclnodes[n] = clnode
725 fclnodes[n] = clnode
725 return clnode
726 return clnode
726 return lookupmflinknode
727 return lookupmflinknode
727
728
728 size = 0
729 size = 0
729 while tmfnodes:
730 while tmfnodes:
730 dir = min(tmfnodes)
731 dir = min(tmfnodes)
731 nodes = tmfnodes[dir]
732 nodes = tmfnodes[dir]
732 prunednodes = self.prune(dirlog(dir), nodes, commonrevs)
733 prunednodes = self.prune(dirlog(dir), nodes, commonrevs)
733 if not dir or prunednodes:
734 if not dir or prunednodes:
734 for x in self._packmanifests(dir, prunednodes,
735 for x in self._packmanifests(dir, prunednodes,
735 makelookupmflinknode(dir)):
736 makelookupmflinknode(dir)):
736 size += len(x)
737 size += len(x)
737 yield x
738 yield x
738 del tmfnodes[dir]
739 del tmfnodes[dir]
739 self._verbosenote(_('%8.i (manifests)\n') % size)
740 self._verbosenote(_('%8.i (manifests)\n') % size)
740 yield self._manifestsdone()
741 yield self._manifestsdone()
741
742
742 # The 'source' parameter is useful for extensions
743 # The 'source' parameter is useful for extensions
743 def generatefiles(self, changedfiles, linknodes, commonrevs, source):
744 def generatefiles(self, changedfiles, linknodes, commonrevs, source):
744 repo = self._repo
745 repo = self._repo
745 progress = self._progress
746 progress = self._progress
746 msgbundling = _('bundling')
747 msgbundling = _('bundling')
747
748
748 total = len(changedfiles)
749 total = len(changedfiles)
749 # for progress output
750 # for progress output
750 msgfiles = _('files')
751 msgfiles = _('files')
751 for i, fname in enumerate(sorted(changedfiles)):
752 for i, fname in enumerate(sorted(changedfiles)):
752 filerevlog = repo.file(fname)
753 filerevlog = repo.file(fname)
753 if not filerevlog:
754 if not filerevlog:
754 raise error.Abort(_("empty or missing revlog for %s") % fname)
755 raise error.Abort(_("empty or missing revlog for %s") % fname)
755
756
756 linkrevnodes = linknodes(filerevlog, fname)
757 linkrevnodes = linknodes(filerevlog, fname)
757 # Lookup for filenodes, we collected the linkrev nodes above in the
758 # Lookup for filenodes, we collected the linkrev nodes above in the
758 # fastpath case and with lookupmf in the slowpath case.
759 # fastpath case and with lookupmf in the slowpath case.
759 def lookupfilelog(x):
760 def lookupfilelog(x):
760 return linkrevnodes[x]
761 return linkrevnodes[x]
761
762
762 filenodes = self.prune(filerevlog, linkrevnodes, commonrevs)
763 filenodes = self.prune(filerevlog, linkrevnodes, commonrevs)
763 if filenodes:
764 if filenodes:
764 progress(msgbundling, i + 1, item=fname, unit=msgfiles,
765 progress(msgbundling, i + 1, item=fname, unit=msgfiles,
765 total=total)
766 total=total)
766 h = self.fileheader(fname)
767 h = self.fileheader(fname)
767 size = len(h)
768 size = len(h)
768 yield h
769 yield h
769 for chunk in self.group(filenodes, filerevlog, lookupfilelog):
770 for chunk in self.group(filenodes, filerevlog, lookupfilelog):
770 size += len(chunk)
771 size += len(chunk)
771 yield chunk
772 yield chunk
772 self._verbosenote(_('%8.i %s\n') % (size, fname))
773 self._verbosenote(_('%8.i %s\n') % (size, fname))
773 progress(msgbundling, None)
774 progress(msgbundling, None)
774
775
775 def deltaparent(self, revlog, rev, p1, p2, prev):
776 def deltaparent(self, revlog, rev, p1, p2, prev):
776 return prev
777 return prev
777
778
778 def revchunk(self, revlog, rev, prev, linknode):
779 def revchunk(self, revlog, rev, prev, linknode):
779 node = revlog.node(rev)
780 node = revlog.node(rev)
780 p1, p2 = revlog.parentrevs(rev)
781 p1, p2 = revlog.parentrevs(rev)
781 base = self.deltaparent(revlog, rev, p1, p2, prev)
782 base = self.deltaparent(revlog, rev, p1, p2, prev)
782
783
783 prefix = ''
784 prefix = ''
784 if revlog.iscensored(base) or revlog.iscensored(rev):
785 if revlog.iscensored(base) or revlog.iscensored(rev):
785 try:
786 try:
786 delta = revlog.revision(node, raw=True)
787 delta = revlog.revision(node, raw=True)
787 except error.CensoredNodeError as e:
788 except error.CensoredNodeError as e:
788 delta = e.tombstone
789 delta = e.tombstone
789 if base == nullrev:
790 if base == nullrev:
790 prefix = mdiff.trivialdiffheader(len(delta))
791 prefix = mdiff.trivialdiffheader(len(delta))
791 else:
792 else:
792 baselen = revlog.rawsize(base)
793 baselen = revlog.rawsize(base)
793 prefix = mdiff.replacediffheader(baselen, len(delta))
794 prefix = mdiff.replacediffheader(baselen, len(delta))
794 elif base == nullrev:
795 elif base == nullrev:
795 delta = revlog.revision(node, raw=True)
796 delta = revlog.revision(node, raw=True)
796 prefix = mdiff.trivialdiffheader(len(delta))
797 prefix = mdiff.trivialdiffheader(len(delta))
797 else:
798 else:
798 delta = revlog.revdiff(base, rev)
799 delta = revlog.revdiff(base, rev)
799 p1n, p2n = revlog.parents(node)
800 p1n, p2n = revlog.parents(node)
800 basenode = revlog.node(base)
801 basenode = revlog.node(base)
801 flags = revlog.flags(rev)
802 flags = revlog.flags(rev)
802 meta = self.builddeltaheader(node, p1n, p2n, basenode, linknode, flags)
803 meta = self.builddeltaheader(node, p1n, p2n, basenode, linknode, flags)
803 meta += prefix
804 meta += prefix
804 l = len(meta) + len(delta)
805 l = len(meta) + len(delta)
805 yield chunkheader(l)
806 yield chunkheader(l)
806 yield meta
807 yield meta
807 yield delta
808 yield delta
808 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
809 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
809 # do nothing with basenode, it is implicitly the previous one in HG10
810 # do nothing with basenode, it is implicitly the previous one in HG10
810 # do nothing with flags, it is implicitly 0 for cg1 and cg2
811 # do nothing with flags, it is implicitly 0 for cg1 and cg2
811 return struct.pack(self.deltaheader, node, p1n, p2n, linknode)
812 return struct.pack(self.deltaheader, node, p1n, p2n, linknode)
812
813
813 class cg2packer(cg1packer):
814 class cg2packer(cg1packer):
814 version = '02'
815 version = '02'
815 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
816 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
816
817
817 def __init__(self, repo, bundlecaps=None):
818 def __init__(self, repo, bundlecaps=None):
818 super(cg2packer, self).__init__(repo, bundlecaps)
819 super(cg2packer, self).__init__(repo, bundlecaps)
819 if self._reorder is None:
820 if self._reorder is None:
820 # Since generaldelta is directly supported by cg2, reordering
821 # Since generaldelta is directly supported by cg2, reordering
821 # generally doesn't help, so we disable it by default (treating
822 # generally doesn't help, so we disable it by default (treating
822 # bundle.reorder=auto just like bundle.reorder=False).
823 # bundle.reorder=auto just like bundle.reorder=False).
823 self._reorder = False
824 self._reorder = False
824
825
825 def deltaparent(self, revlog, rev, p1, p2, prev):
826 def deltaparent(self, revlog, rev, p1, p2, prev):
826 dp = revlog.deltaparent(rev)
827 dp = revlog.deltaparent(rev)
827 if dp == nullrev and revlog.storedeltachains:
828 if dp == nullrev and revlog.storedeltachains:
828 # Avoid sending full revisions when delta parent is null. Pick prev
829 # Avoid sending full revisions when delta parent is null. Pick prev
829 # in that case. It's tempting to pick p1 in this case, as p1 will
830 # in that case. It's tempting to pick p1 in this case, as p1 will
830 # be smaller in the common case. However, computing a delta against
831 # be smaller in the common case. However, computing a delta against
831 # p1 may require resolving the raw text of p1, which could be
832 # p1 may require resolving the raw text of p1, which could be
832 # expensive. The revlog caches should have prev cached, meaning
833 # expensive. The revlog caches should have prev cached, meaning
833 # less CPU for changegroup generation. There is likely room to add
834 # less CPU for changegroup generation. There is likely room to add
834 # a flag and/or config option to control this behavior.
835 # a flag and/or config option to control this behavior.
835 return prev
836 return prev
836 elif dp == nullrev:
837 elif dp == nullrev:
837 # revlog is configured to use full snapshot for a reason,
838 # revlog is configured to use full snapshot for a reason,
838 # stick to full snapshot.
839 # stick to full snapshot.
839 return nullrev
840 return nullrev
840 elif dp not in (p1, p2, prev):
841 elif dp not in (p1, p2, prev):
841 # Pick prev when we can't be sure remote has the base revision.
842 # Pick prev when we can't be sure remote has the base revision.
842 return prev
843 return prev
843 else:
844 else:
844 return dp
845 return dp
845
846
846 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
847 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
847 # Do nothing with flags, it is implicitly 0 in cg1 and cg2
848 # Do nothing with flags, it is implicitly 0 in cg1 and cg2
848 return struct.pack(self.deltaheader, node, p1n, p2n, basenode, linknode)
849 return struct.pack(self.deltaheader, node, p1n, p2n, basenode, linknode)
849
850
850 class cg3packer(cg2packer):
851 class cg3packer(cg2packer):
851 version = '03'
852 version = '03'
852 deltaheader = _CHANGEGROUPV3_DELTA_HEADER
853 deltaheader = _CHANGEGROUPV3_DELTA_HEADER
853
854
854 def _packmanifests(self, dir, mfnodes, lookuplinknode):
855 def _packmanifests(self, dir, mfnodes, lookuplinknode):
855 if dir:
856 if dir:
856 yield self.fileheader(dir)
857 yield self.fileheader(dir)
857
858
858 dirlog = self._repo.manifestlog._revlog.dirlog(dir)
859 dirlog = self._repo.manifestlog._revlog.dirlog(dir)
859 for chunk in self.group(mfnodes, dirlog, lookuplinknode,
860 for chunk in self.group(mfnodes, dirlog, lookuplinknode,
860 units=_('manifests')):
861 units=_('manifests')):
861 yield chunk
862 yield chunk
862
863
863 def _manifestsdone(self):
864 def _manifestsdone(self):
864 return self.close()
865 return self.close()
865
866
866 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
867 def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags):
867 return struct.pack(
868 return struct.pack(
868 self.deltaheader, node, p1n, p2n, basenode, linknode, flags)
869 self.deltaheader, node, p1n, p2n, basenode, linknode, flags)
869
870
870 _packermap = {'01': (cg1packer, cg1unpacker),
871 _packermap = {'01': (cg1packer, cg1unpacker),
871 # cg2 adds support for exchanging generaldelta
872 # cg2 adds support for exchanging generaldelta
872 '02': (cg2packer, cg2unpacker),
873 '02': (cg2packer, cg2unpacker),
873 # cg3 adds support for exchanging revlog flags and treemanifests
874 # cg3 adds support for exchanging revlog flags and treemanifests
874 '03': (cg3packer, cg3unpacker),
875 '03': (cg3packer, cg3unpacker),
875 }
876 }
876
877
877 def allsupportedversions(repo):
878 def allsupportedversions(repo):
878 versions = set(_packermap.keys())
879 versions = set(_packermap.keys())
879 if not (repo.ui.configbool('experimental', 'changegroup3') or
880 if not (repo.ui.configbool('experimental', 'changegroup3') or
880 repo.ui.configbool('experimental', 'treemanifest') or
881 repo.ui.configbool('experimental', 'treemanifest') or
881 'treemanifest' in repo.requirements):
882 'treemanifest' in repo.requirements):
882 versions.discard('03')
883 versions.discard('03')
883 return versions
884 return versions
884
885
885 # Changegroup versions that can be applied to the repo
886 # Changegroup versions that can be applied to the repo
886 def supportedincomingversions(repo):
887 def supportedincomingversions(repo):
887 return allsupportedversions(repo)
888 return allsupportedversions(repo)
888
889
889 # Changegroup versions that can be created from the repo
890 # Changegroup versions that can be created from the repo
890 def supportedoutgoingversions(repo):
891 def supportedoutgoingversions(repo):
891 versions = allsupportedversions(repo)
892 versions = allsupportedversions(repo)
892 if 'treemanifest' in repo.requirements:
893 if 'treemanifest' in repo.requirements:
893 # Versions 01 and 02 support only flat manifests and it's just too
894 # Versions 01 and 02 support only flat manifests and it's just too
894 # expensive to convert between the flat manifest and tree manifest on
895 # expensive to convert between the flat manifest and tree manifest on
895 # the fly. Since tree manifests are hashed differently, all of history
896 # the fly. Since tree manifests are hashed differently, all of history
896 # would have to be converted. Instead, we simply don't even pretend to
897 # would have to be converted. Instead, we simply don't even pretend to
897 # support versions 01 and 02.
898 # support versions 01 and 02.
898 versions.discard('01')
899 versions.discard('01')
899 versions.discard('02')
900 versions.discard('02')
900 return versions
901 return versions
901
902
902 def safeversion(repo):
903 def safeversion(repo):
903 # Finds the smallest version that it's safe to assume clients of the repo
904 # Finds the smallest version that it's safe to assume clients of the repo
904 # will support. For example, all hg versions that support generaldelta also
905 # will support. For example, all hg versions that support generaldelta also
905 # support changegroup 02.
906 # support changegroup 02.
906 versions = supportedoutgoingversions(repo)
907 versions = supportedoutgoingversions(repo)
907 if 'generaldelta' in repo.requirements:
908 if 'generaldelta' in repo.requirements:
908 versions.discard('01')
909 versions.discard('01')
909 assert versions
910 assert versions
910 return min(versions)
911 return min(versions)
911
912
912 def getbundler(version, repo, bundlecaps=None):
913 def getbundler(version, repo, bundlecaps=None):
913 assert version in supportedoutgoingversions(repo)
914 assert version in supportedoutgoingversions(repo)
914 return _packermap[version][0](repo, bundlecaps)
915 return _packermap[version][0](repo, bundlecaps)
915
916
916 def getunbundler(version, fh, alg, extras=None):
917 def getunbundler(version, fh, alg, extras=None):
917 return _packermap[version][1](fh, alg, extras=extras)
918 return _packermap[version][1](fh, alg, extras=extras)
918
919
919 def _changegroupinfo(repo, nodes, source):
920 def _changegroupinfo(repo, nodes, source):
920 if repo.ui.verbose or source == 'bundle':
921 if repo.ui.verbose or source == 'bundle':
921 repo.ui.status(_("%d changesets found\n") % len(nodes))
922 repo.ui.status(_("%d changesets found\n") % len(nodes))
922 if repo.ui.debugflag:
923 if repo.ui.debugflag:
923 repo.ui.debug("list of changesets:\n")
924 repo.ui.debug("list of changesets:\n")
924 for node in nodes:
925 for node in nodes:
925 repo.ui.debug("%s\n" % hex(node))
926 repo.ui.debug("%s\n" % hex(node))
926
927
927 def getsubsetraw(repo, outgoing, bundler, source, fastpath=False):
928 def getsubsetraw(repo, outgoing, bundler, source, fastpath=False):
928 repo = repo.unfiltered()
929 repo = repo.unfiltered()
929 commonrevs = outgoing.common
930 commonrevs = outgoing.common
930 csets = outgoing.missing
931 csets = outgoing.missing
931 heads = outgoing.missingheads
932 heads = outgoing.missingheads
932 # We go through the fast path if we get told to, or if all (unfiltered
933 # We go through the fast path if we get told to, or if all (unfiltered
933 # heads have been requested (since we then know there all linkrevs will
934 # heads have been requested (since we then know there all linkrevs will
934 # be pulled by the client).
935 # be pulled by the client).
935 heads.sort()
936 heads.sort()
936 fastpathlinkrev = fastpath or (
937 fastpathlinkrev = fastpath or (
937 repo.filtername is None and heads == sorted(repo.heads()))
938 repo.filtername is None and heads == sorted(repo.heads()))
938
939
939 repo.hook('preoutgoing', throw=True, source=source)
940 repo.hook('preoutgoing', throw=True, source=source)
940 _changegroupinfo(repo, csets, source)
941 _changegroupinfo(repo, csets, source)
941 return bundler.generate(commonrevs, csets, fastpathlinkrev, source)
942 return bundler.generate(commonrevs, csets, fastpathlinkrev, source)
942
943
943 def getsubset(repo, outgoing, bundler, source, fastpath=False):
944 def getsubset(repo, outgoing, bundler, source, fastpath=False):
944 gengroup = getsubsetraw(repo, outgoing, bundler, source, fastpath)
945 gengroup = getsubsetraw(repo, outgoing, bundler, source, fastpath)
945 return getunbundler(bundler.version, util.chunkbuffer(gengroup), None,
946 return getunbundler(bundler.version, util.chunkbuffer(gengroup), None,
946 {'clcount': len(outgoing.missing)})
947 {'clcount': len(outgoing.missing)})
947
948
948 def changegroupsubset(repo, roots, heads, source, version='01'):
949 def changegroupsubset(repo, roots, heads, source, version='01'):
949 """Compute a changegroup consisting of all the nodes that are
950 """Compute a changegroup consisting of all the nodes that are
950 descendants of any of the roots and ancestors of any of the heads.
951 descendants of any of the roots and ancestors of any of the heads.
951 Return a chunkbuffer object whose read() method will return
952 Return a chunkbuffer object whose read() method will return
952 successive changegroup chunks.
953 successive changegroup chunks.
953
954
954 It is fairly complex as determining which filenodes and which
955 It is fairly complex as determining which filenodes and which
955 manifest nodes need to be included for the changeset to be complete
956 manifest nodes need to be included for the changeset to be complete
956 is non-trivial.
957 is non-trivial.
957
958
958 Another wrinkle is doing the reverse, figuring out which changeset in
959 Another wrinkle is doing the reverse, figuring out which changeset in
959 the changegroup a particular filenode or manifestnode belongs to.
960 the changegroup a particular filenode or manifestnode belongs to.
960 """
961 """
961 outgoing = discovery.outgoing(repo, missingroots=roots, missingheads=heads)
962 outgoing = discovery.outgoing(repo, missingroots=roots, missingheads=heads)
962 bundler = getbundler(version, repo)
963 bundler = getbundler(version, repo)
963 return getsubset(repo, outgoing, bundler, source)
964 return getsubset(repo, outgoing, bundler, source)
964
965
965 def getlocalchangegroupraw(repo, source, outgoing, bundlecaps=None,
966 def getlocalchangegroupraw(repo, source, outgoing, bundlecaps=None,
966 version='01'):
967 version='01'):
967 """Like getbundle, but taking a discovery.outgoing as an argument.
968 """Like getbundle, but taking a discovery.outgoing as an argument.
968
969
969 This is only implemented for local repos and reuses potentially
970 This is only implemented for local repos and reuses potentially
970 precomputed sets in outgoing. Returns a raw changegroup generator."""
971 precomputed sets in outgoing. Returns a raw changegroup generator."""
971 if not outgoing.missing:
972 if not outgoing.missing:
972 return None
973 return None
973 bundler = getbundler(version, repo, bundlecaps)
974 bundler = getbundler(version, repo, bundlecaps)
974 return getsubsetraw(repo, outgoing, bundler, source)
975 return getsubsetraw(repo, outgoing, bundler, source)
975
976
976 def getlocalchangegroup(repo, source, outgoing, bundlecaps=None,
977 def getlocalchangegroup(repo, source, outgoing, bundlecaps=None,
977 version='01'):
978 version='01'):
978 """Like getbundle, but taking a discovery.outgoing as an argument.
979 """Like getbundle, but taking a discovery.outgoing as an argument.
979
980
980 This is only implemented for local repos and reuses potentially
981 This is only implemented for local repos and reuses potentially
981 precomputed sets in outgoing."""
982 precomputed sets in outgoing."""
982 if not outgoing.missing:
983 if not outgoing.missing:
983 return None
984 return None
984 bundler = getbundler(version, repo, bundlecaps)
985 bundler = getbundler(version, repo, bundlecaps)
985 return getsubset(repo, outgoing, bundler, source)
986 return getsubset(repo, outgoing, bundler, source)
986
987
987 def getchangegroup(repo, source, outgoing, bundlecaps=None,
988 def getchangegroup(repo, source, outgoing, bundlecaps=None,
988 version='01'):
989 version='01'):
989 """Like changegroupsubset, but returns the set difference between the
990 """Like changegroupsubset, but returns the set difference between the
990 ancestors of heads and the ancestors common.
991 ancestors of heads and the ancestors common.
991
992
992 If heads is None, use the local heads. If common is None, use [nullid].
993 If heads is None, use the local heads. If common is None, use [nullid].
993
994
994 The nodes in common might not all be known locally due to the way the
995 The nodes in common might not all be known locally due to the way the
995 current discovery protocol works.
996 current discovery protocol works.
996 """
997 """
997 return getlocalchangegroup(repo, source, outgoing, bundlecaps=bundlecaps,
998 return getlocalchangegroup(repo, source, outgoing, bundlecaps=bundlecaps,
998 version=version)
999 version=version)
999
1000
1000 def changegroup(repo, basenodes, source):
1001 def changegroup(repo, basenodes, source):
1001 # to avoid a race we use changegroupsubset() (issue1320)
1002 # to avoid a race we use changegroupsubset() (issue1320)
1002 return changegroupsubset(repo, basenodes, repo.heads(), source)
1003 return changegroupsubset(repo, basenodes, repo.heads(), source)
1003
1004
1004 def _addchangegroupfiles(repo, source, revmap, trp, expectedfiles, needfiles):
1005 def _addchangegroupfiles(repo, source, revmap, trp, expectedfiles, needfiles):
1005 revisions = 0
1006 revisions = 0
1006 files = 0
1007 files = 0
1007 for chunkdata in iter(source.filelogheader, {}):
1008 for chunkdata in iter(source.filelogheader, {}):
1008 files += 1
1009 files += 1
1009 f = chunkdata["filename"]
1010 f = chunkdata["filename"]
1010 repo.ui.debug("adding %s revisions\n" % f)
1011 repo.ui.debug("adding %s revisions\n" % f)
1011 repo.ui.progress(_('files'), files, unit=_('files'),
1012 repo.ui.progress(_('files'), files, unit=_('files'),
1012 total=expectedfiles)
1013 total=expectedfiles)
1013 fl = repo.file(f)
1014 fl = repo.file(f)
1014 o = len(fl)
1015 o = len(fl)
1015 try:
1016 try:
1016 if not fl.addgroup(source, revmap, trp):
1017 if not fl.addgroup(source, revmap, trp):
1017 raise error.Abort(_("received file revlog group is empty"))
1018 raise error.Abort(_("received file revlog group is empty"))
1018 except error.CensoredBaseError as e:
1019 except error.CensoredBaseError as e:
1019 raise error.Abort(_("received delta base is censored: %s") % e)
1020 raise error.Abort(_("received delta base is censored: %s") % e)
1020 revisions += len(fl) - o
1021 revisions += len(fl) - o
1021 if f in needfiles:
1022 if f in needfiles:
1022 needs = needfiles[f]
1023 needs = needfiles[f]
1023 for new in xrange(o, len(fl)):
1024 for new in xrange(o, len(fl)):
1024 n = fl.node(new)
1025 n = fl.node(new)
1025 if n in needs:
1026 if n in needs:
1026 needs.remove(n)
1027 needs.remove(n)
1027 else:
1028 else:
1028 raise error.Abort(
1029 raise error.Abort(
1029 _("received spurious file revlog entry"))
1030 _("received spurious file revlog entry"))
1030 if not needs:
1031 if not needs:
1031 del needfiles[f]
1032 del needfiles[f]
1032 repo.ui.progress(_('files'), None)
1033 repo.ui.progress(_('files'), None)
1033
1034
1034 for f, needs in needfiles.iteritems():
1035 for f, needs in needfiles.iteritems():
1035 fl = repo.file(f)
1036 fl = repo.file(f)
1036 for n in needs:
1037 for n in needs:
1037 try:
1038 try:
1038 fl.rev(n)
1039 fl.rev(n)
1039 except error.LookupError:
1040 except error.LookupError:
1040 raise error.Abort(
1041 raise error.Abort(
1041 _('missing file data for %s:%s - run hg verify') %
1042 _('missing file data for %s:%s - run hg verify') %
1042 (f, hex(n)))
1043 (f, hex(n)))
1043
1044
1044 return revisions, files
1045 return revisions, files
@@ -1,365 +1,365 b''
1 # osutil.py - pure Python version of osutil.c
1 # osutil.py - pure Python version of osutil.c
2 #
2 #
3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
3 # Copyright 2009 Matt Mackall <mpm@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 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import ctypes
10 import ctypes
11 import ctypes.util
11 import ctypes.util
12 import os
12 import os
13 import socket
13 import socket
14 import stat as statmod
14 import stat as statmod
15
15
16 from . import (
16 from . import (
17 policy,
17 policy,
18 pycompat,
18 pycompat,
19 )
19 )
20
20
21 modulepolicy = policy.policy
21 modulepolicy = policy.policy
22 policynocffi = policy.policynocffi
22 policynocffi = policy.policynocffi
23
23
24 def _mode_to_kind(mode):
24 def _mode_to_kind(mode):
25 if statmod.S_ISREG(mode):
25 if statmod.S_ISREG(mode):
26 return statmod.S_IFREG
26 return statmod.S_IFREG
27 if statmod.S_ISDIR(mode):
27 if statmod.S_ISDIR(mode):
28 return statmod.S_IFDIR
28 return statmod.S_IFDIR
29 if statmod.S_ISLNK(mode):
29 if statmod.S_ISLNK(mode):
30 return statmod.S_IFLNK
30 return statmod.S_IFLNK
31 if statmod.S_ISBLK(mode):
31 if statmod.S_ISBLK(mode):
32 return statmod.S_IFBLK
32 return statmod.S_IFBLK
33 if statmod.S_ISCHR(mode):
33 if statmod.S_ISCHR(mode):
34 return statmod.S_IFCHR
34 return statmod.S_IFCHR
35 if statmod.S_ISFIFO(mode):
35 if statmod.S_ISFIFO(mode):
36 return statmod.S_IFIFO
36 return statmod.S_IFIFO
37 if statmod.S_ISSOCK(mode):
37 if statmod.S_ISSOCK(mode):
38 return statmod.S_IFSOCK
38 return statmod.S_IFSOCK
39 return mode
39 return mode
40
40
41 def listdirpure(path, stat=False, skip=None):
41 def listdirpure(path, stat=False, skip=None):
42 '''listdir(path, stat=False) -> list_of_tuples
42 '''listdir(path, stat=False) -> list_of_tuples
43
43
44 Return a sorted list containing information about the entries
44 Return a sorted list containing information about the entries
45 in the directory.
45 in the directory.
46
46
47 If stat is True, each element is a 3-tuple:
47 If stat is True, each element is a 3-tuple:
48
48
49 (name, type, stat object)
49 (name, type, stat object)
50
50
51 Otherwise, each element is a 2-tuple:
51 Otherwise, each element is a 2-tuple:
52
52
53 (name, type)
53 (name, type)
54 '''
54 '''
55 result = []
55 result = []
56 prefix = path
56 prefix = path
57 if not prefix.endswith(pycompat.ossep):
57 if not prefix.endswith(pycompat.ossep):
58 prefix += pycompat.ossep
58 prefix += pycompat.ossep
59 names = os.listdir(path)
59 names = os.listdir(path)
60 names.sort()
60 names.sort()
61 for fn in names:
61 for fn in names:
62 st = os.lstat(prefix + fn)
62 st = os.lstat(prefix + fn)
63 if fn == skip and statmod.S_ISDIR(st.st_mode):
63 if fn == skip and statmod.S_ISDIR(st.st_mode):
64 return []
64 return []
65 if stat:
65 if stat:
66 result.append((fn, _mode_to_kind(st.st_mode), st))
66 result.append((fn, _mode_to_kind(st.st_mode), st))
67 else:
67 else:
68 result.append((fn, _mode_to_kind(st.st_mode)))
68 result.append((fn, _mode_to_kind(st.st_mode)))
69 return result
69 return result
70
70
71 ffi = None
71 ffi = None
72 if modulepolicy not in policynocffi and pycompat.sysplatform == 'darwin':
72 if modulepolicy not in policynocffi and pycompat.sysplatform == 'darwin':
73 try:
73 try:
74 from _osutil_cffi import ffi, lib
74 from _osutil_cffi import ffi, lib
75 except ImportError:
75 except ImportError:
76 if modulepolicy == 'cffi': # strict cffi import
76 if modulepolicy == 'cffi': # strict cffi import
77 raise
77 raise
78
78
79 if pycompat.sysplatform == 'darwin' and ffi is not None:
79 if pycompat.sysplatform == 'darwin' and ffi is not None:
80 listdir_batch_size = 4096
80 listdir_batch_size = 4096
81 # tweakable number, only affects performance, which chunks
81 # tweakable number, only affects performance, which chunks
82 # of bytes do we get back from getattrlistbulk
82 # of bytes do we get back from getattrlistbulk
83
83
84 attrkinds = [None] * 20 # we need the max no for enum VXXX, 20 is plenty
84 attrkinds = [None] * 20 # we need the max no for enum VXXX, 20 is plenty
85
85
86 attrkinds[lib.VREG] = statmod.S_IFREG
86 attrkinds[lib.VREG] = statmod.S_IFREG
87 attrkinds[lib.VDIR] = statmod.S_IFDIR
87 attrkinds[lib.VDIR] = statmod.S_IFDIR
88 attrkinds[lib.VLNK] = statmod.S_IFLNK
88 attrkinds[lib.VLNK] = statmod.S_IFLNK
89 attrkinds[lib.VBLK] = statmod.S_IFBLK
89 attrkinds[lib.VBLK] = statmod.S_IFBLK
90 attrkinds[lib.VCHR] = statmod.S_IFCHR
90 attrkinds[lib.VCHR] = statmod.S_IFCHR
91 attrkinds[lib.VFIFO] = statmod.S_IFIFO
91 attrkinds[lib.VFIFO] = statmod.S_IFIFO
92 attrkinds[lib.VSOCK] = statmod.S_IFSOCK
92 attrkinds[lib.VSOCK] = statmod.S_IFSOCK
93
93
94 class stat_res(object):
94 class stat_res(object):
95 def __init__(self, st_mode, st_mtime, st_size):
95 def __init__(self, st_mode, st_mtime, st_size):
96 self.st_mode = st_mode
96 self.st_mode = st_mode
97 self.st_mtime = st_mtime
97 self.st_mtime = st_mtime
98 self.st_size = st_size
98 self.st_size = st_size
99
99
100 tv_sec_ofs = ffi.offsetof("struct timespec", "tv_sec")
100 tv_sec_ofs = ffi.offsetof("struct timespec", "tv_sec")
101 buf = ffi.new("char[]", listdir_batch_size)
101 buf = ffi.new("char[]", listdir_batch_size)
102
102
103 def listdirinternal(dfd, req, stat, skip):
103 def listdirinternal(dfd, req, stat, skip):
104 ret = []
104 ret = []
105 while True:
105 while True:
106 r = lib.getattrlistbulk(dfd, req, buf, listdir_batch_size, 0)
106 r = lib.getattrlistbulk(dfd, req, buf, listdir_batch_size, 0)
107 if r == 0:
107 if r == 0:
108 break
108 break
109 if r == -1:
109 if r == -1:
110 raise OSError(ffi.errno, os.strerror(ffi.errno))
110 raise OSError(ffi.errno, os.strerror(ffi.errno))
111 cur = ffi.cast("val_attrs_t*", buf)
111 cur = ffi.cast("val_attrs_t*", buf)
112 for i in range(r):
112 for i in range(r):
113 lgt = cur.length
113 lgt = cur.length
114 assert lgt == ffi.cast('uint32_t*', cur)[0]
114 assert lgt == ffi.cast('uint32_t*', cur)[0]
115 ofs = cur.name_info.attr_dataoffset
115 ofs = cur.name_info.attr_dataoffset
116 str_lgt = cur.name_info.attr_length
116 str_lgt = cur.name_info.attr_length
117 base_ofs = ffi.offsetof('val_attrs_t', 'name_info')
117 base_ofs = ffi.offsetof('val_attrs_t', 'name_info')
118 name = str(ffi.buffer(ffi.cast("char*", cur) + base_ofs + ofs,
118 name = str(ffi.buffer(ffi.cast("char*", cur) + base_ofs + ofs,
119 str_lgt - 1))
119 str_lgt - 1))
120 tp = attrkinds[cur.obj_type]
120 tp = attrkinds[cur.obj_type]
121 if name == "." or name == "..":
121 if name == "." or name == "..":
122 continue
122 continue
123 if skip == name and tp == statmod.S_ISDIR:
123 if skip == name and tp == statmod.S_ISDIR:
124 return []
124 return []
125 if stat:
125 if stat:
126 mtime = cur.mtime.tv_sec
126 mtime = cur.mtime.tv_sec
127 mode = (cur.accessmask & ~lib.S_IFMT)| tp
127 mode = (cur.accessmask & ~lib.S_IFMT)| tp
128 ret.append((name, tp, stat_res(st_mode=mode, st_mtime=mtime,
128 ret.append((name, tp, stat_res(st_mode=mode, st_mtime=mtime,
129 st_size=cur.datalength)))
129 st_size=cur.datalength)))
130 else:
130 else:
131 ret.append((name, tp))
131 ret.append((name, tp))
132 cur = ffi.cast("val_attrs_t*", int(ffi.cast("intptr_t", cur))
132 cur = ffi.cast("val_attrs_t*", int(ffi.cast("intptr_t", cur))
133 + lgt)
133 + lgt)
134 return ret
134 return ret
135
135
136 def listdir(path, stat=False, skip=None):
136 def listdir(path, stat=False, skip=None):
137 req = ffi.new("struct attrlist*")
137 req = ffi.new("struct attrlist*")
138 req.bitmapcount = lib.ATTR_BIT_MAP_COUNT
138 req.bitmapcount = lib.ATTR_BIT_MAP_COUNT
139 req.commonattr = (lib.ATTR_CMN_RETURNED_ATTRS |
139 req.commonattr = (lib.ATTR_CMN_RETURNED_ATTRS |
140 lib.ATTR_CMN_NAME |
140 lib.ATTR_CMN_NAME |
141 lib.ATTR_CMN_OBJTYPE |
141 lib.ATTR_CMN_OBJTYPE |
142 lib.ATTR_CMN_ACCESSMASK |
142 lib.ATTR_CMN_ACCESSMASK |
143 lib.ATTR_CMN_MODTIME)
143 lib.ATTR_CMN_MODTIME)
144 req.fileattr = lib.ATTR_FILE_DATALENGTH
144 req.fileattr = lib.ATTR_FILE_DATALENGTH
145 dfd = lib.open(path, lib.O_RDONLY, 0)
145 dfd = lib.open(path, lib.O_RDONLY, 0)
146 if dfd == -1:
146 if dfd == -1:
147 raise OSError(ffi.errno, os.strerror(ffi.errno))
147 raise OSError(ffi.errno, os.strerror(ffi.errno))
148
148
149 try:
149 try:
150 ret = listdirinternal(dfd, req, stat, skip)
150 ret = listdirinternal(dfd, req, stat, skip)
151 finally:
151 finally:
152 try:
152 try:
153 lib.close(dfd)
153 lib.close(dfd)
154 except BaseException:
154 except BaseException:
155 pass # we ignore all the errors from closing, not
155 pass # we ignore all the errors from closing, not
156 # much we can do about that
156 # much we can do about that
157 return ret
157 return ret
158 else:
158 else:
159 listdir = listdirpure
159 listdir = listdirpure
160
160
161 if pycompat.osname != 'nt':
161 if pycompat.osname != 'nt':
162 posixfile = open
162 posixfile = open
163
163
164 _SCM_RIGHTS = 0x01
164 _SCM_RIGHTS = 0x01
165 _socklen_t = ctypes.c_uint
165 _socklen_t = ctypes.c_uint
166
166
167 if pycompat.sysplatform.startswith('linux'):
167 if pycompat.sysplatform.startswith('linux'):
168 # socket.h says "the type should be socklen_t but the definition of
168 # socket.h says "the type should be socklen_t but the definition of
169 # the kernel is incompatible with this."
169 # the kernel is incompatible with this."
170 _cmsg_len_t = ctypes.c_size_t
170 _cmsg_len_t = ctypes.c_size_t
171 _msg_controllen_t = ctypes.c_size_t
171 _msg_controllen_t = ctypes.c_size_t
172 _msg_iovlen_t = ctypes.c_size_t
172 _msg_iovlen_t = ctypes.c_size_t
173 else:
173 else:
174 _cmsg_len_t = _socklen_t
174 _cmsg_len_t = _socklen_t
175 _msg_controllen_t = _socklen_t
175 _msg_controllen_t = _socklen_t
176 _msg_iovlen_t = ctypes.c_int
176 _msg_iovlen_t = ctypes.c_int
177
177
178 class _iovec(ctypes.Structure):
178 class _iovec(ctypes.Structure):
179 _fields_ = [
179 _fields_ = [
180 (u'iov_base', ctypes.c_void_p),
180 (u'iov_base', ctypes.c_void_p),
181 (u'iov_len', ctypes.c_size_t),
181 (u'iov_len', ctypes.c_size_t),
182 ]
182 ]
183
183
184 class _msghdr(ctypes.Structure):
184 class _msghdr(ctypes.Structure):
185 _fields_ = [
185 _fields_ = [
186 (u'msg_name', ctypes.c_void_p),
186 (u'msg_name', ctypes.c_void_p),
187 (u'msg_namelen', _socklen_t),
187 (u'msg_namelen', _socklen_t),
188 (u'msg_iov', ctypes.POINTER(_iovec)),
188 (u'msg_iov', ctypes.POINTER(_iovec)),
189 (u'msg_iovlen', _msg_iovlen_t),
189 (u'msg_iovlen', _msg_iovlen_t),
190 (u'msg_control', ctypes.c_void_p),
190 (u'msg_control', ctypes.c_void_p),
191 (u'msg_controllen', _msg_controllen_t),
191 (u'msg_controllen', _msg_controllen_t),
192 (u'msg_flags', ctypes.c_int),
192 (u'msg_flags', ctypes.c_int),
193 ]
193 ]
194
194
195 class _cmsghdr(ctypes.Structure):
195 class _cmsghdr(ctypes.Structure):
196 _fields_ = [
196 _fields_ = [
197 (u'cmsg_len', _cmsg_len_t),
197 (u'cmsg_len', _cmsg_len_t),
198 (u'cmsg_level', ctypes.c_int),
198 (u'cmsg_level', ctypes.c_int),
199 (u'cmsg_type', ctypes.c_int),
199 (u'cmsg_type', ctypes.c_int),
200 (u'cmsg_data', ctypes.c_ubyte * 0),
200 (u'cmsg_data', ctypes.c_ubyte * 0),
201 ]
201 ]
202
202
203 _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True)
203 _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True)
204 _recvmsg = getattr(_libc, 'recvmsg', None)
204 _recvmsg = getattr(_libc, 'recvmsg', None)
205 if _recvmsg:
205 if _recvmsg:
206 _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long)
206 _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long)
207 _recvmsg.argtypes = (ctypes.c_int, ctypes.POINTER(_msghdr),
207 _recvmsg.argtypes = (ctypes.c_int, ctypes.POINTER(_msghdr),
208 ctypes.c_int)
208 ctypes.c_int)
209 else:
209 else:
210 # recvmsg isn't always provided by libc; such systems are unsupported
210 # recvmsg isn't always provided by libc; such systems are unsupported
211 def _recvmsg(sockfd, msg, flags):
211 def _recvmsg(sockfd, msg, flags):
212 raise NotImplementedError('unsupported platform')
212 raise NotImplementedError('unsupported platform')
213
213
214 def _CMSG_FIRSTHDR(msgh):
214 def _CMSG_FIRSTHDR(msgh):
215 if msgh.msg_controllen < ctypes.sizeof(_cmsghdr):
215 if msgh.msg_controllen < ctypes.sizeof(_cmsghdr):
216 return
216 return
217 cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr))
217 cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr))
218 return cmsgptr.contents
218 return cmsgptr.contents
219
219
220 # The pure version is less portable than the native version because the
220 # The pure version is less portable than the native version because the
221 # handling of socket ancillary data heavily depends on C preprocessor.
221 # handling of socket ancillary data heavily depends on C preprocessor.
222 # Also, some length fields are wrongly typed in Linux kernel.
222 # Also, some length fields are wrongly typed in Linux kernel.
223 def recvfds(sockfd):
223 def recvfds(sockfd):
224 """receive list of file descriptors via socket"""
224 """receive list of file descriptors via socket"""
225 dummy = (ctypes.c_ubyte * 1)()
225 dummy = (ctypes.c_ubyte * 1)()
226 iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy))
226 iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy))
227 cbuf = ctypes.create_string_buffer(256)
227 cbuf = ctypes.create_string_buffer(256)
228 msgh = _msghdr(None, 0,
228 msgh = _msghdr(None, 0,
229 ctypes.pointer(iov), 1,
229 ctypes.pointer(iov), 1,
230 ctypes.cast(cbuf, ctypes.c_void_p), ctypes.sizeof(cbuf),
230 ctypes.cast(cbuf, ctypes.c_void_p), ctypes.sizeof(cbuf),
231 0)
231 0)
232 r = _recvmsg(sockfd, ctypes.byref(msgh), 0)
232 r = _recvmsg(sockfd, ctypes.byref(msgh), 0)
233 if r < 0:
233 if r < 0:
234 e = ctypes.get_errno()
234 e = ctypes.get_errno()
235 raise OSError(e, os.strerror(e))
235 raise OSError(e, os.strerror(e))
236 # assumes that the first cmsg has fds because it isn't easy to write
236 # assumes that the first cmsg has fds because it isn't easy to write
237 # portable CMSG_NXTHDR() with ctypes.
237 # portable CMSG_NXTHDR() with ctypes.
238 cmsg = _CMSG_FIRSTHDR(msgh)
238 cmsg = _CMSG_FIRSTHDR(msgh)
239 if not cmsg:
239 if not cmsg:
240 return []
240 return []
241 if (cmsg.cmsg_level != socket.SOL_SOCKET or
241 if (cmsg.cmsg_level != socket.SOL_SOCKET or
242 cmsg.cmsg_type != _SCM_RIGHTS):
242 cmsg.cmsg_type != _SCM_RIGHTS):
243 return []
243 return []
244 rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int))
244 rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int))
245 rfdscount = ((cmsg.cmsg_len - _cmsghdr.cmsg_data.offset) /
245 rfdscount = ((cmsg.cmsg_len - _cmsghdr.cmsg_data.offset) /
246 ctypes.sizeof(ctypes.c_int))
246 ctypes.sizeof(ctypes.c_int))
247 return [rfds[i] for i in xrange(rfdscount)]
247 return [rfds[i] for i in xrange(rfdscount)]
248
248
249 else:
249 else:
250 import msvcrt
250 import msvcrt
251
251
252 _kernel32 = ctypes.windll.kernel32
252 _kernel32 = ctypes.windll.kernel32
253
253
254 _DWORD = ctypes.c_ulong
254 _DWORD = ctypes.c_ulong
255 _LPCSTR = _LPSTR = ctypes.c_char_p
255 _LPCSTR = _LPSTR = ctypes.c_char_p
256 _HANDLE = ctypes.c_void_p
256 _HANDLE = ctypes.c_void_p
257
257
258 _INVALID_HANDLE_VALUE = _HANDLE(-1).value
258 _INVALID_HANDLE_VALUE = _HANDLE(-1).value
259
259
260 # CreateFile
260 # CreateFile
261 _FILE_SHARE_READ = 0x00000001
261 _FILE_SHARE_READ = 0x00000001
262 _FILE_SHARE_WRITE = 0x00000002
262 _FILE_SHARE_WRITE = 0x00000002
263 _FILE_SHARE_DELETE = 0x00000004
263 _FILE_SHARE_DELETE = 0x00000004
264
264
265 _CREATE_ALWAYS = 2
265 _CREATE_ALWAYS = 2
266 _OPEN_EXISTING = 3
266 _OPEN_EXISTING = 3
267 _OPEN_ALWAYS = 4
267 _OPEN_ALWAYS = 4
268
268
269 _GENERIC_READ = 0x80000000
269 _GENERIC_READ = 0x80000000
270 _GENERIC_WRITE = 0x40000000
270 _GENERIC_WRITE = 0x40000000
271
271
272 _FILE_ATTRIBUTE_NORMAL = 0x80
272 _FILE_ATTRIBUTE_NORMAL = 0x80
273
273
274 # open_osfhandle flags
274 # open_osfhandle flags
275 _O_RDONLY = 0x0000
275 _O_RDONLY = 0x0000
276 _O_RDWR = 0x0002
276 _O_RDWR = 0x0002
277 _O_APPEND = 0x0008
277 _O_APPEND = 0x0008
278
278
279 _O_TEXT = 0x4000
279 _O_TEXT = 0x4000
280 _O_BINARY = 0x8000
280 _O_BINARY = 0x8000
281
281
282 # types of parameters of C functions used (required by pypy)
282 # types of parameters of C functions used (required by pypy)
283
283
284 _kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD, ctypes.c_void_p,
284 _kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD, ctypes.c_void_p,
285 _DWORD, _DWORD, _HANDLE]
285 _DWORD, _DWORD, _HANDLE]
286 _kernel32.CreateFileA.restype = _HANDLE
286 _kernel32.CreateFileA.restype = _HANDLE
287
287
288 def _raiseioerror(name):
288 def _raiseioerror(name):
289 err = ctypes.WinError()
289 err = ctypes.WinError()
290 raise IOError(err.errno, '%s: %s' % (name, err.strerror))
290 raise IOError(err.errno, '%s: %s' % (name, err.strerror))
291
291
292 class posixfile(object):
292 class posixfile(object):
293 '''a file object aiming for POSIX-like semantics
293 '''a file object aiming for POSIX-like semantics
294
294
295 CPython's open() returns a file that was opened *without* setting the
295 CPython's open() returns a file that was opened *without* setting the
296 _FILE_SHARE_DELETE flag, which causes rename and unlink to abort.
296 _FILE_SHARE_DELETE flag, which causes rename and unlink to abort.
297 This even happens if any hardlinked copy of the file is in open state.
297 This even happens if any hardlinked copy of the file is in open state.
298 We set _FILE_SHARE_DELETE here, so files opened with posixfile can be
298 We set _FILE_SHARE_DELETE here, so files opened with posixfile can be
299 renamed and deleted while they are held open.
299 renamed and deleted while they are held open.
300 Note that if a file opened with posixfile is unlinked, the file
300 Note that if a file opened with posixfile is unlinked, the file
301 remains but cannot be opened again or be recreated under the same name,
301 remains but cannot be opened again or be recreated under the same name,
302 until all reading processes have closed the file.'''
302 until all reading processes have closed the file.'''
303
303
304 def __init__(self, name, mode='r', bufsize=-1):
304 def __init__(self, name, mode='r', bufsize=-1):
305 if 'b' in mode:
305 if 'b' in mode:
306 flags = _O_BINARY
306 flags = _O_BINARY
307 else:
307 else:
308 flags = _O_TEXT
308 flags = _O_TEXT
309
309
310 m0 = mode[0]
310 m0 = mode[0]
311 if m0 == 'r' and '+' not in mode:
311 if m0 == 'r' and '+' not in mode:
312 flags |= _O_RDONLY
312 flags |= _O_RDONLY
313 access = _GENERIC_READ
313 access = _GENERIC_READ
314 else:
314 else:
315 # work around http://support.microsoft.com/kb/899149 and
315 # work around http://support.microsoft.com/kb/899149 and
316 # set _O_RDWR for 'w' and 'a', even if mode has no '+'
316 # set _O_RDWR for 'w' and 'a', even if mode has no '+'
317 flags |= _O_RDWR
317 flags |= _O_RDWR
318 access = _GENERIC_READ | _GENERIC_WRITE
318 access = _GENERIC_READ | _GENERIC_WRITE
319
319
320 if m0 == 'r':
320 if m0 == 'r':
321 creation = _OPEN_EXISTING
321 creation = _OPEN_EXISTING
322 elif m0 == 'w':
322 elif m0 == 'w':
323 creation = _CREATE_ALWAYS
323 creation = _CREATE_ALWAYS
324 elif m0 == 'a':
324 elif m0 == 'a':
325 creation = _OPEN_ALWAYS
325 creation = _OPEN_ALWAYS
326 flags |= _O_APPEND
326 flags |= _O_APPEND
327 else:
327 else:
328 raise ValueError("invalid mode: %s" % mode)
328 raise ValueError("invalid mode: %s" % mode)
329
329
330 fh = _kernel32.CreateFileA(name, access,
330 fh = _kernel32.CreateFileA(name, access,
331 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
331 _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE,
332 None, creation, _FILE_ATTRIBUTE_NORMAL, None)
332 None, creation, _FILE_ATTRIBUTE_NORMAL, None)
333 if fh == _INVALID_HANDLE_VALUE:
333 if fh == _INVALID_HANDLE_VALUE:
334 _raiseioerror(name)
334 _raiseioerror(name)
335
335
336 fd = msvcrt.open_osfhandle(fh, flags)
336 fd = msvcrt.open_osfhandle(fh, flags)
337 if fd == -1:
337 if fd == -1:
338 _kernel32.CloseHandle(fh)
338 _kernel32.CloseHandle(fh)
339 _raiseioerror(name)
339 _raiseioerror(name)
340
340
341 f = os.fdopen(fd, mode, bufsize)
341 f = os.fdopen(fd, pycompat.sysstr(mode), bufsize)
342 # unfortunately, f.name is '<fdopen>' at this point -- so we store
342 # unfortunately, f.name is '<fdopen>' at this point -- so we store
343 # the name on this wrapper. We cannot just assign to f.name,
343 # the name on this wrapper. We cannot just assign to f.name,
344 # because that attribute is read-only.
344 # because that attribute is read-only.
345 object.__setattr__(self, 'name', name)
345 object.__setattr__(self, 'name', name)
346 object.__setattr__(self, '_file', f)
346 object.__setattr__(self, '_file', f)
347
347
348 def __iter__(self):
348 def __iter__(self):
349 return self._file
349 return self._file
350
350
351 def __getattr__(self, name):
351 def __getattr__(self, name):
352 return getattr(self._file, name)
352 return getattr(self._file, name)
353
353
354 def __setattr__(self, name, value):
354 def __setattr__(self, name, value):
355 '''mimics the read-only attributes of Python file objects
355 '''mimics the read-only attributes of Python file objects
356 by raising 'TypeError: readonly attribute' if someone tries:
356 by raising 'TypeError: readonly attribute' if someone tries:
357 f = posixfile('foo.txt')
357 f = posixfile('foo.txt')
358 f.name = 'bla' '''
358 f.name = 'bla' '''
359 return self._file.__setattr__(name, value)
359 return self._file.__setattr__(name, value)
360
360
361 def __enter__(self):
361 def __enter__(self):
362 return self._file.__enter__()
362 return self._file.__enter__()
363
363
364 def __exit__(self, exc_type, exc_value, exc_tb):
364 def __exit__(self, exc_type, exc_value, exc_tb):
365 return self._file.__exit__(exc_type, exc_value, exc_tb)
365 return self._file.__exit__(exc_type, exc_value, exc_tb)
@@ -1,1431 +1,1431 b''
1 # ui.py - user interface bits for mercurial
1 # ui.py - user interface bits for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
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 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import contextlib
10 import contextlib
11 import errno
11 import errno
12 import getpass
12 import getpass
13 import inspect
13 import inspect
14 import os
14 import os
15 import re
15 import re
16 import socket
16 import socket
17 import sys
17 import sys
18 import tempfile
18 import tempfile
19 import traceback
19 import traceback
20
20
21 from .i18n import _
21 from .i18n import _
22 from .node import hex
22 from .node import hex
23
23
24 from . import (
24 from . import (
25 config,
25 config,
26 encoding,
26 encoding,
27 error,
27 error,
28 formatter,
28 formatter,
29 progress,
29 progress,
30 pycompat,
30 pycompat,
31 scmutil,
31 scmutil,
32 util,
32 util,
33 )
33 )
34
34
35 urlreq = util.urlreq
35 urlreq = util.urlreq
36
36
37 samplehgrcs = {
37 samplehgrcs = {
38 'user':
38 'user':
39 """# example user config (see 'hg help config' for more info)
39 """# example user config (see 'hg help config' for more info)
40 [ui]
40 [ui]
41 # name and email, e.g.
41 # name and email, e.g.
42 # username = Jane Doe <jdoe@example.com>
42 # username = Jane Doe <jdoe@example.com>
43 username =
43 username =
44
44
45 [extensions]
45 [extensions]
46 # uncomment these lines to enable some popular extensions
46 # uncomment these lines to enable some popular extensions
47 # (see 'hg help extensions' for more info)
47 # (see 'hg help extensions' for more info)
48 #
48 #
49 # pager =
49 # pager =
50 # color =""",
50 # color =""",
51
51
52 'cloned':
52 'cloned':
53 """# example repository config (see 'hg help config' for more info)
53 """# example repository config (see 'hg help config' for more info)
54 [paths]
54 [paths]
55 default = %s
55 default = %s
56
56
57 # path aliases to other clones of this repo in URLs or filesystem paths
57 # path aliases to other clones of this repo in URLs or filesystem paths
58 # (see 'hg help config.paths' for more info)
58 # (see 'hg help config.paths' for more info)
59 #
59 #
60 # default-push = ssh://jdoe@example.net/hg/jdoes-fork
60 # default-push = ssh://jdoe@example.net/hg/jdoes-fork
61 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
61 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
62 # my-clone = /home/jdoe/jdoes-clone
62 # my-clone = /home/jdoe/jdoes-clone
63
63
64 [ui]
64 [ui]
65 # name and email (local to this repository, optional), e.g.
65 # name and email (local to this repository, optional), e.g.
66 # username = Jane Doe <jdoe@example.com>
66 # username = Jane Doe <jdoe@example.com>
67 """,
67 """,
68
68
69 'local':
69 'local':
70 """# example repository config (see 'hg help config' for more info)
70 """# example repository config (see 'hg help config' for more info)
71 [paths]
71 [paths]
72 # path aliases to other clones of this repo in URLs or filesystem paths
72 # path aliases to other clones of this repo in URLs or filesystem paths
73 # (see 'hg help config.paths' for more info)
73 # (see 'hg help config.paths' for more info)
74 #
74 #
75 # default = http://example.com/hg/example-repo
75 # default = http://example.com/hg/example-repo
76 # default-push = ssh://jdoe@example.net/hg/jdoes-fork
76 # default-push = ssh://jdoe@example.net/hg/jdoes-fork
77 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
77 # my-fork = ssh://jdoe@example.net/hg/jdoes-fork
78 # my-clone = /home/jdoe/jdoes-clone
78 # my-clone = /home/jdoe/jdoes-clone
79
79
80 [ui]
80 [ui]
81 # name and email (local to this repository, optional), e.g.
81 # name and email (local to this repository, optional), e.g.
82 # username = Jane Doe <jdoe@example.com>
82 # username = Jane Doe <jdoe@example.com>
83 """,
83 """,
84
84
85 'global':
85 'global':
86 """# example system-wide hg config (see 'hg help config' for more info)
86 """# example system-wide hg config (see 'hg help config' for more info)
87
87
88 [extensions]
88 [extensions]
89 # uncomment these lines to enable some popular extensions
89 # uncomment these lines to enable some popular extensions
90 # (see 'hg help extensions' for more info)
90 # (see 'hg help extensions' for more info)
91 #
91 #
92 # blackbox =
92 # blackbox =
93 # color =
93 # color =
94 # pager =""",
94 # pager =""",
95 }
95 }
96
96
97 class ui(object):
97 class ui(object):
98 def __init__(self, src=None):
98 def __init__(self, src=None):
99 """Create a fresh new ui object if no src given
99 """Create a fresh new ui object if no src given
100
100
101 Use uimod.ui.load() to create a ui which knows global and user configs.
101 Use uimod.ui.load() to create a ui which knows global and user configs.
102 In most cases, you should use ui.copy() to create a copy of an existing
102 In most cases, you should use ui.copy() to create a copy of an existing
103 ui object.
103 ui object.
104 """
104 """
105 # _buffers: used for temporary capture of output
105 # _buffers: used for temporary capture of output
106 self._buffers = []
106 self._buffers = []
107 # 3-tuple describing how each buffer in the stack behaves.
107 # 3-tuple describing how each buffer in the stack behaves.
108 # Values are (capture stderr, capture subprocesses, apply labels).
108 # Values are (capture stderr, capture subprocesses, apply labels).
109 self._bufferstates = []
109 self._bufferstates = []
110 # When a buffer is active, defines whether we are expanding labels.
110 # When a buffer is active, defines whether we are expanding labels.
111 # This exists to prevent an extra list lookup.
111 # This exists to prevent an extra list lookup.
112 self._bufferapplylabels = None
112 self._bufferapplylabels = None
113 self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
113 self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
114 self._reportuntrusted = True
114 self._reportuntrusted = True
115 self._ocfg = config.config() # overlay
115 self._ocfg = config.config() # overlay
116 self._tcfg = config.config() # trusted
116 self._tcfg = config.config() # trusted
117 self._ucfg = config.config() # untrusted
117 self._ucfg = config.config() # untrusted
118 self._trustusers = set()
118 self._trustusers = set()
119 self._trustgroups = set()
119 self._trustgroups = set()
120 self.callhooks = True
120 self.callhooks = True
121 # Insecure server connections requested.
121 # Insecure server connections requested.
122 self.insecureconnections = False
122 self.insecureconnections = False
123
123
124 if src:
124 if src:
125 self.fout = src.fout
125 self.fout = src.fout
126 self.ferr = src.ferr
126 self.ferr = src.ferr
127 self.fin = src.fin
127 self.fin = src.fin
128
128
129 self._tcfg = src._tcfg.copy()
129 self._tcfg = src._tcfg.copy()
130 self._ucfg = src._ucfg.copy()
130 self._ucfg = src._ucfg.copy()
131 self._ocfg = src._ocfg.copy()
131 self._ocfg = src._ocfg.copy()
132 self._trustusers = src._trustusers.copy()
132 self._trustusers = src._trustusers.copy()
133 self._trustgroups = src._trustgroups.copy()
133 self._trustgroups = src._trustgroups.copy()
134 self.environ = src.environ
134 self.environ = src.environ
135 self.callhooks = src.callhooks
135 self.callhooks = src.callhooks
136 self.insecureconnections = src.insecureconnections
136 self.insecureconnections = src.insecureconnections
137 self.fixconfig()
137 self.fixconfig()
138
138
139 self.httppasswordmgrdb = src.httppasswordmgrdb
139 self.httppasswordmgrdb = src.httppasswordmgrdb
140 else:
140 else:
141 self.fout = util.stdout
141 self.fout = util.stdout
142 self.ferr = util.stderr
142 self.ferr = util.stderr
143 self.fin = util.stdin
143 self.fin = util.stdin
144
144
145 # shared read-only environment
145 # shared read-only environment
146 self.environ = encoding.environ
146 self.environ = encoding.environ
147
147
148 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm()
148 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm()
149
149
150 allowed = self.configlist('experimental', 'exportableenviron')
150 allowed = self.configlist('experimental', 'exportableenviron')
151 if '*' in allowed:
151 if '*' in allowed:
152 self._exportableenviron = self.environ
152 self._exportableenviron = self.environ
153 else:
153 else:
154 self._exportableenviron = {}
154 self._exportableenviron = {}
155 for k in allowed:
155 for k in allowed:
156 if k in self.environ:
156 if k in self.environ:
157 self._exportableenviron[k] = self.environ[k]
157 self._exportableenviron[k] = self.environ[k]
158
158
159 @classmethod
159 @classmethod
160 def load(cls):
160 def load(cls):
161 """Create a ui and load global and user configs"""
161 """Create a ui and load global and user configs"""
162 u = cls()
162 u = cls()
163 # we always trust global config files
163 # we always trust global config files
164 for f in scmutil.rcpath():
164 for f in scmutil.rcpath():
165 u.readconfig(f, trust=True)
165 u.readconfig(f, trust=True)
166 return u
166 return u
167
167
168 def copy(self):
168 def copy(self):
169 return self.__class__(self)
169 return self.__class__(self)
170
170
171 def resetstate(self):
171 def resetstate(self):
172 """Clear internal state that shouldn't persist across commands"""
172 """Clear internal state that shouldn't persist across commands"""
173 if self._progbar:
173 if self._progbar:
174 self._progbar.resetstate() # reset last-print time of progress bar
174 self._progbar.resetstate() # reset last-print time of progress bar
175 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm()
175 self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm()
176
176
177 def formatter(self, topic, opts):
177 def formatter(self, topic, opts):
178 return formatter.formatter(self, topic, opts)
178 return formatter.formatter(self, topic, opts)
179
179
180 def _trusted(self, fp, f):
180 def _trusted(self, fp, f):
181 st = util.fstat(fp)
181 st = util.fstat(fp)
182 if util.isowner(st):
182 if util.isowner(st):
183 return True
183 return True
184
184
185 tusers, tgroups = self._trustusers, self._trustgroups
185 tusers, tgroups = self._trustusers, self._trustgroups
186 if '*' in tusers or '*' in tgroups:
186 if '*' in tusers or '*' in tgroups:
187 return True
187 return True
188
188
189 user = util.username(st.st_uid)
189 user = util.username(st.st_uid)
190 group = util.groupname(st.st_gid)
190 group = util.groupname(st.st_gid)
191 if user in tusers or group in tgroups or user == util.username():
191 if user in tusers or group in tgroups or user == util.username():
192 return True
192 return True
193
193
194 if self._reportuntrusted:
194 if self._reportuntrusted:
195 self.warn(_('not trusting file %s from untrusted '
195 self.warn(_('not trusting file %s from untrusted '
196 'user %s, group %s\n') % (f, user, group))
196 'user %s, group %s\n') % (f, user, group))
197 return False
197 return False
198
198
199 def readconfig(self, filename, root=None, trust=False,
199 def readconfig(self, filename, root=None, trust=False,
200 sections=None, remap=None):
200 sections=None, remap=None):
201 try:
201 try:
202 fp = open(filename, u'rb')
202 fp = open(filename, u'rb')
203 except IOError:
203 except IOError:
204 if not sections: # ignore unless we were looking for something
204 if not sections: # ignore unless we were looking for something
205 return
205 return
206 raise
206 raise
207
207
208 cfg = config.config()
208 cfg = config.config()
209 trusted = sections or trust or self._trusted(fp, filename)
209 trusted = sections or trust or self._trusted(fp, filename)
210
210
211 try:
211 try:
212 cfg.read(filename, fp, sections=sections, remap=remap)
212 cfg.read(filename, fp, sections=sections, remap=remap)
213 fp.close()
213 fp.close()
214 except error.ConfigError as inst:
214 except error.ConfigError as inst:
215 if trusted:
215 if trusted:
216 raise
216 raise
217 self.warn(_("ignored: %s\n") % str(inst))
217 self.warn(_("ignored: %s\n") % str(inst))
218
218
219 if self.plain():
219 if self.plain():
220 for k in ('debug', 'fallbackencoding', 'quiet', 'slash',
220 for k in ('debug', 'fallbackencoding', 'quiet', 'slash',
221 'logtemplate', 'statuscopies', 'style',
221 'logtemplate', 'statuscopies', 'style',
222 'traceback', 'verbose'):
222 'traceback', 'verbose'):
223 if k in cfg['ui']:
223 if k in cfg['ui']:
224 del cfg['ui'][k]
224 del cfg['ui'][k]
225 for k, v in cfg.items('defaults'):
225 for k, v in cfg.items('defaults'):
226 del cfg['defaults'][k]
226 del cfg['defaults'][k]
227 # Don't remove aliases from the configuration if in the exceptionlist
227 # Don't remove aliases from the configuration if in the exceptionlist
228 if self.plain('alias'):
228 if self.plain('alias'):
229 for k, v in cfg.items('alias'):
229 for k, v in cfg.items('alias'):
230 del cfg['alias'][k]
230 del cfg['alias'][k]
231 if self.plain('revsetalias'):
231 if self.plain('revsetalias'):
232 for k, v in cfg.items('revsetalias'):
232 for k, v in cfg.items('revsetalias'):
233 del cfg['revsetalias'][k]
233 del cfg['revsetalias'][k]
234 if self.plain('templatealias'):
234 if self.plain('templatealias'):
235 for k, v in cfg.items('templatealias'):
235 for k, v in cfg.items('templatealias'):
236 del cfg['templatealias'][k]
236 del cfg['templatealias'][k]
237
237
238 if trusted:
238 if trusted:
239 self._tcfg.update(cfg)
239 self._tcfg.update(cfg)
240 self._tcfg.update(self._ocfg)
240 self._tcfg.update(self._ocfg)
241 self._ucfg.update(cfg)
241 self._ucfg.update(cfg)
242 self._ucfg.update(self._ocfg)
242 self._ucfg.update(self._ocfg)
243
243
244 if root is None:
244 if root is None:
245 root = os.path.expanduser('~')
245 root = os.path.expanduser('~')
246 self.fixconfig(root=root)
246 self.fixconfig(root=root)
247
247
248 def fixconfig(self, root=None, section=None):
248 def fixconfig(self, root=None, section=None):
249 if section in (None, 'paths'):
249 if section in (None, 'paths'):
250 # expand vars and ~
250 # expand vars and ~
251 # translate paths relative to root (or home) into absolute paths
251 # translate paths relative to root (or home) into absolute paths
252 root = root or pycompat.getcwd()
252 root = root or pycompat.getcwd()
253 for c in self._tcfg, self._ucfg, self._ocfg:
253 for c in self._tcfg, self._ucfg, self._ocfg:
254 for n, p in c.items('paths'):
254 for n, p in c.items('paths'):
255 # Ignore sub-options.
255 # Ignore sub-options.
256 if ':' in n:
256 if ':' in n:
257 continue
257 continue
258 if not p:
258 if not p:
259 continue
259 continue
260 if '%%' in p:
260 if '%%' in p:
261 s = self.configsource('paths', n) or 'none'
261 s = self.configsource('paths', n) or 'none'
262 self.warn(_("(deprecated '%%' in path %s=%s from %s)\n")
262 self.warn(_("(deprecated '%%' in path %s=%s from %s)\n")
263 % (n, p, s))
263 % (n, p, s))
264 p = p.replace('%%', '%')
264 p = p.replace('%%', '%')
265 p = util.expandpath(p)
265 p = util.expandpath(p)
266 if not util.hasscheme(p) and not os.path.isabs(p):
266 if not util.hasscheme(p) and not os.path.isabs(p):
267 p = os.path.normpath(os.path.join(root, p))
267 p = os.path.normpath(os.path.join(root, p))
268 c.set("paths", n, p)
268 c.set("paths", n, p)
269
269
270 if section in (None, 'ui'):
270 if section in (None, 'ui'):
271 # update ui options
271 # update ui options
272 self.debugflag = self.configbool('ui', 'debug')
272 self.debugflag = self.configbool('ui', 'debug')
273 self.verbose = self.debugflag or self.configbool('ui', 'verbose')
273 self.verbose = self.debugflag or self.configbool('ui', 'verbose')
274 self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
274 self.quiet = not self.debugflag and self.configbool('ui', 'quiet')
275 if self.verbose and self.quiet:
275 if self.verbose and self.quiet:
276 self.quiet = self.verbose = False
276 self.quiet = self.verbose = False
277 self._reportuntrusted = self.debugflag or self.configbool("ui",
277 self._reportuntrusted = self.debugflag or self.configbool("ui",
278 "report_untrusted", True)
278 "report_untrusted", True)
279 self.tracebackflag = self.configbool('ui', 'traceback', False)
279 self.tracebackflag = self.configbool('ui', 'traceback', False)
280
280
281 if section in (None, 'trusted'):
281 if section in (None, 'trusted'):
282 # update trust information
282 # update trust information
283 self._trustusers.update(self.configlist('trusted', 'users'))
283 self._trustusers.update(self.configlist('trusted', 'users'))
284 self._trustgroups.update(self.configlist('trusted', 'groups'))
284 self._trustgroups.update(self.configlist('trusted', 'groups'))
285
285
286 def backupconfig(self, section, item):
286 def backupconfig(self, section, item):
287 return (self._ocfg.backup(section, item),
287 return (self._ocfg.backup(section, item),
288 self._tcfg.backup(section, item),
288 self._tcfg.backup(section, item),
289 self._ucfg.backup(section, item),)
289 self._ucfg.backup(section, item),)
290 def restoreconfig(self, data):
290 def restoreconfig(self, data):
291 self._ocfg.restore(data[0])
291 self._ocfg.restore(data[0])
292 self._tcfg.restore(data[1])
292 self._tcfg.restore(data[1])
293 self._ucfg.restore(data[2])
293 self._ucfg.restore(data[2])
294
294
295 def setconfig(self, section, name, value, source=''):
295 def setconfig(self, section, name, value, source=''):
296 for cfg in (self._ocfg, self._tcfg, self._ucfg):
296 for cfg in (self._ocfg, self._tcfg, self._ucfg):
297 cfg.set(section, name, value, source)
297 cfg.set(section, name, value, source)
298 self.fixconfig(section=section)
298 self.fixconfig(section=section)
299
299
300 def _data(self, untrusted):
300 def _data(self, untrusted):
301 return untrusted and self._ucfg or self._tcfg
301 return untrusted and self._ucfg or self._tcfg
302
302
303 def configsource(self, section, name, untrusted=False):
303 def configsource(self, section, name, untrusted=False):
304 return self._data(untrusted).source(section, name)
304 return self._data(untrusted).source(section, name)
305
305
306 def config(self, section, name, default=None, untrusted=False):
306 def config(self, section, name, default=None, untrusted=False):
307 if isinstance(name, list):
307 if isinstance(name, list):
308 alternates = name
308 alternates = name
309 else:
309 else:
310 alternates = [name]
310 alternates = [name]
311
311
312 for n in alternates:
312 for n in alternates:
313 value = self._data(untrusted).get(section, n, None)
313 value = self._data(untrusted).get(section, n, None)
314 if value is not None:
314 if value is not None:
315 name = n
315 name = n
316 break
316 break
317 else:
317 else:
318 value = default
318 value = default
319
319
320 if self.debugflag and not untrusted and self._reportuntrusted:
320 if self.debugflag and not untrusted and self._reportuntrusted:
321 for n in alternates:
321 for n in alternates:
322 uvalue = self._ucfg.get(section, n)
322 uvalue = self._ucfg.get(section, n)
323 if uvalue is not None and uvalue != value:
323 if uvalue is not None and uvalue != value:
324 self.debug("ignoring untrusted configuration option "
324 self.debug("ignoring untrusted configuration option "
325 "%s.%s = %s\n" % (section, n, uvalue))
325 "%s.%s = %s\n" % (section, n, uvalue))
326 return value
326 return value
327
327
328 def configsuboptions(self, section, name, default=None, untrusted=False):
328 def configsuboptions(self, section, name, default=None, untrusted=False):
329 """Get a config option and all sub-options.
329 """Get a config option and all sub-options.
330
330
331 Some config options have sub-options that are declared with the
331 Some config options have sub-options that are declared with the
332 format "key:opt = value". This method is used to return the main
332 format "key:opt = value". This method is used to return the main
333 option and all its declared sub-options.
333 option and all its declared sub-options.
334
334
335 Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
335 Returns a 2-tuple of ``(option, sub-options)``, where `sub-options``
336 is a dict of defined sub-options where keys and values are strings.
336 is a dict of defined sub-options where keys and values are strings.
337 """
337 """
338 data = self._data(untrusted)
338 data = self._data(untrusted)
339 main = data.get(section, name, default)
339 main = data.get(section, name, default)
340 if self.debugflag and not untrusted and self._reportuntrusted:
340 if self.debugflag and not untrusted and self._reportuntrusted:
341 uvalue = self._ucfg.get(section, name)
341 uvalue = self._ucfg.get(section, name)
342 if uvalue is not None and uvalue != main:
342 if uvalue is not None and uvalue != main:
343 self.debug('ignoring untrusted configuration option '
343 self.debug('ignoring untrusted configuration option '
344 '%s.%s = %s\n' % (section, name, uvalue))
344 '%s.%s = %s\n' % (section, name, uvalue))
345
345
346 sub = {}
346 sub = {}
347 prefix = '%s:' % name
347 prefix = '%s:' % name
348 for k, v in data.items(section):
348 for k, v in data.items(section):
349 if k.startswith(prefix):
349 if k.startswith(prefix):
350 sub[k[len(prefix):]] = v
350 sub[k[len(prefix):]] = v
351
351
352 if self.debugflag and not untrusted and self._reportuntrusted:
352 if self.debugflag and not untrusted and self._reportuntrusted:
353 for k, v in sub.items():
353 for k, v in sub.items():
354 uvalue = self._ucfg.get(section, '%s:%s' % (name, k))
354 uvalue = self._ucfg.get(section, '%s:%s' % (name, k))
355 if uvalue is not None and uvalue != v:
355 if uvalue is not None and uvalue != v:
356 self.debug('ignoring untrusted configuration option '
356 self.debug('ignoring untrusted configuration option '
357 '%s:%s.%s = %s\n' % (section, name, k, uvalue))
357 '%s:%s.%s = %s\n' % (section, name, k, uvalue))
358
358
359 return main, sub
359 return main, sub
360
360
361 def configpath(self, section, name, default=None, untrusted=False):
361 def configpath(self, section, name, default=None, untrusted=False):
362 'get a path config item, expanded relative to repo root or config file'
362 'get a path config item, expanded relative to repo root or config file'
363 v = self.config(section, name, default, untrusted)
363 v = self.config(section, name, default, untrusted)
364 if v is None:
364 if v is None:
365 return None
365 return None
366 if not os.path.isabs(v) or "://" not in v:
366 if not os.path.isabs(v) or "://" not in v:
367 src = self.configsource(section, name, untrusted)
367 src = self.configsource(section, name, untrusted)
368 if ':' in src:
368 if ':' in src:
369 base = os.path.dirname(src.rsplit(':')[0])
369 base = os.path.dirname(src.rsplit(':')[0])
370 v = os.path.join(base, os.path.expanduser(v))
370 v = os.path.join(base, os.path.expanduser(v))
371 return v
371 return v
372
372
373 def configbool(self, section, name, default=False, untrusted=False):
373 def configbool(self, section, name, default=False, untrusted=False):
374 """parse a configuration element as a boolean
374 """parse a configuration element as a boolean
375
375
376 >>> u = ui(); s = 'foo'
376 >>> u = ui(); s = 'foo'
377 >>> u.setconfig(s, 'true', 'yes')
377 >>> u.setconfig(s, 'true', 'yes')
378 >>> u.configbool(s, 'true')
378 >>> u.configbool(s, 'true')
379 True
379 True
380 >>> u.setconfig(s, 'false', 'no')
380 >>> u.setconfig(s, 'false', 'no')
381 >>> u.configbool(s, 'false')
381 >>> u.configbool(s, 'false')
382 False
382 False
383 >>> u.configbool(s, 'unknown')
383 >>> u.configbool(s, 'unknown')
384 False
384 False
385 >>> u.configbool(s, 'unknown', True)
385 >>> u.configbool(s, 'unknown', True)
386 True
386 True
387 >>> u.setconfig(s, 'invalid', 'somevalue')
387 >>> u.setconfig(s, 'invalid', 'somevalue')
388 >>> u.configbool(s, 'invalid')
388 >>> u.configbool(s, 'invalid')
389 Traceback (most recent call last):
389 Traceback (most recent call last):
390 ...
390 ...
391 ConfigError: foo.invalid is not a boolean ('somevalue')
391 ConfigError: foo.invalid is not a boolean ('somevalue')
392 """
392 """
393
393
394 v = self.config(section, name, None, untrusted)
394 v = self.config(section, name, None, untrusted)
395 if v is None:
395 if v is None:
396 return default
396 return default
397 if isinstance(v, bool):
397 if isinstance(v, bool):
398 return v
398 return v
399 b = util.parsebool(v)
399 b = util.parsebool(v)
400 if b is None:
400 if b is None:
401 raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
401 raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
402 % (section, name, v))
402 % (section, name, v))
403 return b
403 return b
404
404
405 def configint(self, section, name, default=None, untrusted=False):
405 def configint(self, section, name, default=None, untrusted=False):
406 """parse a configuration element as an integer
406 """parse a configuration element as an integer
407
407
408 >>> u = ui(); s = 'foo'
408 >>> u = ui(); s = 'foo'
409 >>> u.setconfig(s, 'int1', '42')
409 >>> u.setconfig(s, 'int1', '42')
410 >>> u.configint(s, 'int1')
410 >>> u.configint(s, 'int1')
411 42
411 42
412 >>> u.setconfig(s, 'int2', '-42')
412 >>> u.setconfig(s, 'int2', '-42')
413 >>> u.configint(s, 'int2')
413 >>> u.configint(s, 'int2')
414 -42
414 -42
415 >>> u.configint(s, 'unknown', 7)
415 >>> u.configint(s, 'unknown', 7)
416 7
416 7
417 >>> u.setconfig(s, 'invalid', 'somevalue')
417 >>> u.setconfig(s, 'invalid', 'somevalue')
418 >>> u.configint(s, 'invalid')
418 >>> u.configint(s, 'invalid')
419 Traceback (most recent call last):
419 Traceback (most recent call last):
420 ...
420 ...
421 ConfigError: foo.invalid is not an integer ('somevalue')
421 ConfigError: foo.invalid is not an integer ('somevalue')
422 """
422 """
423
423
424 v = self.config(section, name, None, untrusted)
424 v = self.config(section, name, None, untrusted)
425 if v is None:
425 if v is None:
426 return default
426 return default
427 try:
427 try:
428 return int(v)
428 return int(v)
429 except ValueError:
429 except ValueError:
430 raise error.ConfigError(_("%s.%s is not an integer ('%s')")
430 raise error.ConfigError(_("%s.%s is not an integer ('%s')")
431 % (section, name, v))
431 % (section, name, v))
432
432
433 def configbytes(self, section, name, default=0, untrusted=False):
433 def configbytes(self, section, name, default=0, untrusted=False):
434 """parse a configuration element as a quantity in bytes
434 """parse a configuration element as a quantity in bytes
435
435
436 Units can be specified as b (bytes), k or kb (kilobytes), m or
436 Units can be specified as b (bytes), k or kb (kilobytes), m or
437 mb (megabytes), g or gb (gigabytes).
437 mb (megabytes), g or gb (gigabytes).
438
438
439 >>> u = ui(); s = 'foo'
439 >>> u = ui(); s = 'foo'
440 >>> u.setconfig(s, 'val1', '42')
440 >>> u.setconfig(s, 'val1', '42')
441 >>> u.configbytes(s, 'val1')
441 >>> u.configbytes(s, 'val1')
442 42
442 42
443 >>> u.setconfig(s, 'val2', '42.5 kb')
443 >>> u.setconfig(s, 'val2', '42.5 kb')
444 >>> u.configbytes(s, 'val2')
444 >>> u.configbytes(s, 'val2')
445 43520
445 43520
446 >>> u.configbytes(s, 'unknown', '7 MB')
446 >>> u.configbytes(s, 'unknown', '7 MB')
447 7340032
447 7340032
448 >>> u.setconfig(s, 'invalid', 'somevalue')
448 >>> u.setconfig(s, 'invalid', 'somevalue')
449 >>> u.configbytes(s, 'invalid')
449 >>> u.configbytes(s, 'invalid')
450 Traceback (most recent call last):
450 Traceback (most recent call last):
451 ...
451 ...
452 ConfigError: foo.invalid is not a byte quantity ('somevalue')
452 ConfigError: foo.invalid is not a byte quantity ('somevalue')
453 """
453 """
454
454
455 value = self.config(section, name)
455 value = self.config(section, name)
456 if value is None:
456 if value is None:
457 if not isinstance(default, str):
457 if not isinstance(default, str):
458 return default
458 return default
459 value = default
459 value = default
460 try:
460 try:
461 return util.sizetoint(value)
461 return util.sizetoint(value)
462 except error.ParseError:
462 except error.ParseError:
463 raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')")
463 raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')")
464 % (section, name, value))
464 % (section, name, value))
465
465
466 def configlist(self, section, name, default=None, untrusted=False):
466 def configlist(self, section, name, default=None, untrusted=False):
467 """parse a configuration element as a list of comma/space separated
467 """parse a configuration element as a list of comma/space separated
468 strings
468 strings
469
469
470 >>> u = ui(); s = 'foo'
470 >>> u = ui(); s = 'foo'
471 >>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
471 >>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
472 >>> u.configlist(s, 'list1')
472 >>> u.configlist(s, 'list1')
473 ['this', 'is', 'a small', 'test']
473 ['this', 'is', 'a small', 'test']
474 """
474 """
475
475
476 def _parse_plain(parts, s, offset):
476 def _parse_plain(parts, s, offset):
477 whitespace = False
477 whitespace = False
478 while offset < len(s) and (s[offset].isspace() or s[offset] == ','):
478 while offset < len(s) and (s[offset].isspace() or s[offset] == ','):
479 whitespace = True
479 whitespace = True
480 offset += 1
480 offset += 1
481 if offset >= len(s):
481 if offset >= len(s):
482 return None, parts, offset
482 return None, parts, offset
483 if whitespace:
483 if whitespace:
484 parts.append('')
484 parts.append('')
485 if s[offset] == '"' and not parts[-1]:
485 if s[offset] == '"' and not parts[-1]:
486 return _parse_quote, parts, offset + 1
486 return _parse_quote, parts, offset + 1
487 elif s[offset] == '"' and parts[-1][-1] == '\\':
487 elif s[offset] == '"' and parts[-1][-1] == '\\':
488 parts[-1] = parts[-1][:-1] + s[offset]
488 parts[-1] = parts[-1][:-1] + s[offset]
489 return _parse_plain, parts, offset + 1
489 return _parse_plain, parts, offset + 1
490 parts[-1] += s[offset]
490 parts[-1] += s[offset]
491 return _parse_plain, parts, offset + 1
491 return _parse_plain, parts, offset + 1
492
492
493 def _parse_quote(parts, s, offset):
493 def _parse_quote(parts, s, offset):
494 if offset < len(s) and s[offset] == '"': # ""
494 if offset < len(s) and s[offset] == '"': # ""
495 parts.append('')
495 parts.append('')
496 offset += 1
496 offset += 1
497 while offset < len(s) and (s[offset].isspace() or
497 while offset < len(s) and (s[offset].isspace() or
498 s[offset] == ','):
498 s[offset] == ','):
499 offset += 1
499 offset += 1
500 return _parse_plain, parts, offset
500 return _parse_plain, parts, offset
501
501
502 while offset < len(s) and s[offset] != '"':
502 while offset < len(s) and s[offset] != '"':
503 if (s[offset] == '\\' and offset + 1 < len(s)
503 if (s[offset] == '\\' and offset + 1 < len(s)
504 and s[offset + 1] == '"'):
504 and s[offset + 1] == '"'):
505 offset += 1
505 offset += 1
506 parts[-1] += '"'
506 parts[-1] += '"'
507 else:
507 else:
508 parts[-1] += s[offset]
508 parts[-1] += s[offset]
509 offset += 1
509 offset += 1
510
510
511 if offset >= len(s):
511 if offset >= len(s):
512 real_parts = _configlist(parts[-1])
512 real_parts = _configlist(parts[-1])
513 if not real_parts:
513 if not real_parts:
514 parts[-1] = '"'
514 parts[-1] = '"'
515 else:
515 else:
516 real_parts[0] = '"' + real_parts[0]
516 real_parts[0] = '"' + real_parts[0]
517 parts = parts[:-1]
517 parts = parts[:-1]
518 parts.extend(real_parts)
518 parts.extend(real_parts)
519 return None, parts, offset
519 return None, parts, offset
520
520
521 offset += 1
521 offset += 1
522 while offset < len(s) and s[offset] in [' ', ',']:
522 while offset < len(s) and s[offset] in [' ', ',']:
523 offset += 1
523 offset += 1
524
524
525 if offset < len(s):
525 if offset < len(s):
526 if offset + 1 == len(s) and s[offset] == '"':
526 if offset + 1 == len(s) and s[offset] == '"':
527 parts[-1] += '"'
527 parts[-1] += '"'
528 offset += 1
528 offset += 1
529 else:
529 else:
530 parts.append('')
530 parts.append('')
531 else:
531 else:
532 return None, parts, offset
532 return None, parts, offset
533
533
534 return _parse_plain, parts, offset
534 return _parse_plain, parts, offset
535
535
536 def _configlist(s):
536 def _configlist(s):
537 s = s.rstrip(' ,')
537 s = s.rstrip(' ,')
538 if not s:
538 if not s:
539 return []
539 return []
540 parser, parts, offset = _parse_plain, [''], 0
540 parser, parts, offset = _parse_plain, [''], 0
541 while parser:
541 while parser:
542 parser, parts, offset = parser(parts, s, offset)
542 parser, parts, offset = parser(parts, s, offset)
543 return parts
543 return parts
544
544
545 result = self.config(section, name, untrusted=untrusted)
545 result = self.config(section, name, untrusted=untrusted)
546 if result is None:
546 if result is None:
547 result = default or []
547 result = default or []
548 if isinstance(result, bytes):
548 if isinstance(result, bytes):
549 result = _configlist(result.lstrip(' ,\n'))
549 result = _configlist(result.lstrip(' ,\n'))
550 if result is None:
550 if result is None:
551 result = default or []
551 result = default or []
552 return result
552 return result
553
553
554 def hasconfig(self, section, name, untrusted=False):
554 def hasconfig(self, section, name, untrusted=False):
555 return self._data(untrusted).hasitem(section, name)
555 return self._data(untrusted).hasitem(section, name)
556
556
557 def has_section(self, section, untrusted=False):
557 def has_section(self, section, untrusted=False):
558 '''tell whether section exists in config.'''
558 '''tell whether section exists in config.'''
559 return section in self._data(untrusted)
559 return section in self._data(untrusted)
560
560
561 def configitems(self, section, untrusted=False, ignoresub=False):
561 def configitems(self, section, untrusted=False, ignoresub=False):
562 items = self._data(untrusted).items(section)
562 items = self._data(untrusted).items(section)
563 if ignoresub:
563 if ignoresub:
564 newitems = {}
564 newitems = {}
565 for k, v in items:
565 for k, v in items:
566 if ':' not in k:
566 if ':' not in k:
567 newitems[k] = v
567 newitems[k] = v
568 items = newitems.items()
568 items = newitems.items()
569 if self.debugflag and not untrusted and self._reportuntrusted:
569 if self.debugflag and not untrusted and self._reportuntrusted:
570 for k, v in self._ucfg.items(section):
570 for k, v in self._ucfg.items(section):
571 if self._tcfg.get(section, k) != v:
571 if self._tcfg.get(section, k) != v:
572 self.debug("ignoring untrusted configuration option "
572 self.debug("ignoring untrusted configuration option "
573 "%s.%s = %s\n" % (section, k, v))
573 "%s.%s = %s\n" % (section, k, v))
574 return items
574 return items
575
575
576 def walkconfig(self, untrusted=False):
576 def walkconfig(self, untrusted=False):
577 cfg = self._data(untrusted)
577 cfg = self._data(untrusted)
578 for section in cfg.sections():
578 for section in cfg.sections():
579 for name, value in self.configitems(section, untrusted):
579 for name, value in self.configitems(section, untrusted):
580 yield section, name, value
580 yield section, name, value
581
581
582 def plain(self, feature=None):
582 def plain(self, feature=None):
583 '''is plain mode active?
583 '''is plain mode active?
584
584
585 Plain mode means that all configuration variables which affect
585 Plain mode means that all configuration variables which affect
586 the behavior and output of Mercurial should be
586 the behavior and output of Mercurial should be
587 ignored. Additionally, the output should be stable,
587 ignored. Additionally, the output should be stable,
588 reproducible and suitable for use in scripts or applications.
588 reproducible and suitable for use in scripts or applications.
589
589
590 The only way to trigger plain mode is by setting either the
590 The only way to trigger plain mode is by setting either the
591 `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
591 `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
592
592
593 The return value can either be
593 The return value can either be
594 - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
594 - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
595 - True otherwise
595 - True otherwise
596 '''
596 '''
597 if ('HGPLAIN' not in encoding.environ and
597 if ('HGPLAIN' not in encoding.environ and
598 'HGPLAINEXCEPT' not in encoding.environ):
598 'HGPLAINEXCEPT' not in encoding.environ):
599 return False
599 return False
600 exceptions = encoding.environ.get('HGPLAINEXCEPT',
600 exceptions = encoding.environ.get('HGPLAINEXCEPT',
601 '').strip().split(',')
601 '').strip().split(',')
602 if feature and exceptions:
602 if feature and exceptions:
603 return feature not in exceptions
603 return feature not in exceptions
604 return True
604 return True
605
605
606 def username(self):
606 def username(self):
607 """Return default username to be used in commits.
607 """Return default username to be used in commits.
608
608
609 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
609 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL
610 and stop searching if one of these is set.
610 and stop searching if one of these is set.
611 If not found and ui.askusername is True, ask the user, else use
611 If not found and ui.askusername is True, ask the user, else use
612 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
612 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname".
613 """
613 """
614 user = encoding.environ.get("HGUSER")
614 user = encoding.environ.get("HGUSER")
615 if user is None:
615 if user is None:
616 user = self.config("ui", ["username", "user"])
616 user = self.config("ui", ["username", "user"])
617 if user is not None:
617 if user is not None:
618 user = os.path.expandvars(user)
618 user = os.path.expandvars(user)
619 if user is None:
619 if user is None:
620 user = encoding.environ.get("EMAIL")
620 user = encoding.environ.get("EMAIL")
621 if user is None and self.configbool("ui", "askusername"):
621 if user is None and self.configbool("ui", "askusername"):
622 user = self.prompt(_("enter a commit username:"), default=None)
622 user = self.prompt(_("enter a commit username:"), default=None)
623 if user is None and not self.interactive():
623 if user is None and not self.interactive():
624 try:
624 try:
625 user = '%s@%s' % (util.getuser(), socket.getfqdn())
625 user = '%s@%s' % (util.getuser(), socket.getfqdn())
626 self.warn(_("no username found, using '%s' instead\n") % user)
626 self.warn(_("no username found, using '%s' instead\n") % user)
627 except KeyError:
627 except KeyError:
628 pass
628 pass
629 if not user:
629 if not user:
630 raise error.Abort(_('no username supplied'),
630 raise error.Abort(_('no username supplied'),
631 hint=_("use 'hg config --edit' "
631 hint=_("use 'hg config --edit' "
632 'to set your username'))
632 'to set your username'))
633 if "\n" in user:
633 if "\n" in user:
634 raise error.Abort(_("username %s contains a newline\n")
634 raise error.Abort(_("username %s contains a newline\n")
635 % repr(user))
635 % repr(user))
636 return user
636 return user
637
637
638 def shortuser(self, user):
638 def shortuser(self, user):
639 """Return a short representation of a user name or email address."""
639 """Return a short representation of a user name or email address."""
640 if not self.verbose:
640 if not self.verbose:
641 user = util.shortuser(user)
641 user = util.shortuser(user)
642 return user
642 return user
643
643
644 def expandpath(self, loc, default=None):
644 def expandpath(self, loc, default=None):
645 """Return repository location relative to cwd or from [paths]"""
645 """Return repository location relative to cwd or from [paths]"""
646 try:
646 try:
647 p = self.paths.getpath(loc)
647 p = self.paths.getpath(loc)
648 if p:
648 if p:
649 return p.rawloc
649 return p.rawloc
650 except error.RepoError:
650 except error.RepoError:
651 pass
651 pass
652
652
653 if default:
653 if default:
654 try:
654 try:
655 p = self.paths.getpath(default)
655 p = self.paths.getpath(default)
656 if p:
656 if p:
657 return p.rawloc
657 return p.rawloc
658 except error.RepoError:
658 except error.RepoError:
659 pass
659 pass
660
660
661 return loc
661 return loc
662
662
663 @util.propertycache
663 @util.propertycache
664 def paths(self):
664 def paths(self):
665 return paths(self)
665 return paths(self)
666
666
667 def pushbuffer(self, error=False, subproc=False, labeled=False):
667 def pushbuffer(self, error=False, subproc=False, labeled=False):
668 """install a buffer to capture standard output of the ui object
668 """install a buffer to capture standard output of the ui object
669
669
670 If error is True, the error output will be captured too.
670 If error is True, the error output will be captured too.
671
671
672 If subproc is True, output from subprocesses (typically hooks) will be
672 If subproc is True, output from subprocesses (typically hooks) will be
673 captured too.
673 captured too.
674
674
675 If labeled is True, any labels associated with buffered
675 If labeled is True, any labels associated with buffered
676 output will be handled. By default, this has no effect
676 output will be handled. By default, this has no effect
677 on the output returned, but extensions and GUI tools may
677 on the output returned, but extensions and GUI tools may
678 handle this argument and returned styled output. If output
678 handle this argument and returned styled output. If output
679 is being buffered so it can be captured and parsed or
679 is being buffered so it can be captured and parsed or
680 processed, labeled should not be set to True.
680 processed, labeled should not be set to True.
681 """
681 """
682 self._buffers.append([])
682 self._buffers.append([])
683 self._bufferstates.append((error, subproc, labeled))
683 self._bufferstates.append((error, subproc, labeled))
684 self._bufferapplylabels = labeled
684 self._bufferapplylabels = labeled
685
685
686 def popbuffer(self):
686 def popbuffer(self):
687 '''pop the last buffer and return the buffered output'''
687 '''pop the last buffer and return the buffered output'''
688 self._bufferstates.pop()
688 self._bufferstates.pop()
689 if self._bufferstates:
689 if self._bufferstates:
690 self._bufferapplylabels = self._bufferstates[-1][2]
690 self._bufferapplylabels = self._bufferstates[-1][2]
691 else:
691 else:
692 self._bufferapplylabels = None
692 self._bufferapplylabels = None
693
693
694 return "".join(self._buffers.pop())
694 return "".join(self._buffers.pop())
695
695
696 def write(self, *args, **opts):
696 def write(self, *args, **opts):
697 '''write args to output
697 '''write args to output
698
698
699 By default, this method simply writes to the buffer or stdout,
699 By default, this method simply writes to the buffer or stdout,
700 but extensions or GUI tools may override this method,
700 but extensions or GUI tools may override this method,
701 write_err(), popbuffer(), and label() to style output from
701 write_err(), popbuffer(), and label() to style output from
702 various parts of hg.
702 various parts of hg.
703
703
704 An optional keyword argument, "label", can be passed in.
704 An optional keyword argument, "label", can be passed in.
705 This should be a string containing label names separated by
705 This should be a string containing label names separated by
706 space. Label names take the form of "topic.type". For example,
706 space. Label names take the form of "topic.type". For example,
707 ui.debug() issues a label of "ui.debug".
707 ui.debug() issues a label of "ui.debug".
708
708
709 When labeling output for a specific command, a label of
709 When labeling output for a specific command, a label of
710 "cmdname.type" is recommended. For example, status issues
710 "cmdname.type" is recommended. For example, status issues
711 a label of "status.modified" for modified files.
711 a label of "status.modified" for modified files.
712 '''
712 '''
713 if self._buffers and not opts.get('prompt', False):
713 if self._buffers and not opts.get('prompt', False):
714 self._buffers[-1].extend(a for a in args)
714 self._buffers[-1].extend(a for a in args)
715 else:
715 else:
716 self._progclear()
716 self._progclear()
717 for a in args:
717 for a in args:
718 self.fout.write(a)
718 self.fout.write(a)
719
719
720 def write_err(self, *args, **opts):
720 def write_err(self, *args, **opts):
721 self._progclear()
721 self._progclear()
722 try:
722 try:
723 if self._bufferstates and self._bufferstates[-1][0]:
723 if self._bufferstates and self._bufferstates[-1][0]:
724 return self.write(*args, **opts)
724 return self.write(*args, **opts)
725 if not getattr(self.fout, 'closed', False):
725 if not getattr(self.fout, 'closed', False):
726 self.fout.flush()
726 self.fout.flush()
727 for a in args:
727 for a in args:
728 self.ferr.write(a)
728 self.ferr.write(a)
729 # stderr may be buffered under win32 when redirected to files,
729 # stderr may be buffered under win32 when redirected to files,
730 # including stdout.
730 # including stdout.
731 if not getattr(self.ferr, 'closed', False):
731 if not getattr(self.ferr, 'closed', False):
732 self.ferr.flush()
732 self.ferr.flush()
733 except IOError as inst:
733 except IOError as inst:
734 if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
734 if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
735 raise
735 raise
736
736
737 def flush(self):
737 def flush(self):
738 try: self.fout.flush()
738 try: self.fout.flush()
739 except (IOError, ValueError): pass
739 except (IOError, ValueError): pass
740 try: self.ferr.flush()
740 try: self.ferr.flush()
741 except (IOError, ValueError): pass
741 except (IOError, ValueError): pass
742
742
743 def _isatty(self, fh):
743 def _isatty(self, fh):
744 if self.configbool('ui', 'nontty', False):
744 if self.configbool('ui', 'nontty', False):
745 return False
745 return False
746 return util.isatty(fh)
746 return util.isatty(fh)
747
747
748 def interface(self, feature):
748 def interface(self, feature):
749 """what interface to use for interactive console features?
749 """what interface to use for interactive console features?
750
750
751 The interface is controlled by the value of `ui.interface` but also by
751 The interface is controlled by the value of `ui.interface` but also by
752 the value of feature-specific configuration. For example:
752 the value of feature-specific configuration. For example:
753
753
754 ui.interface.histedit = text
754 ui.interface.histedit = text
755 ui.interface.chunkselector = curses
755 ui.interface.chunkselector = curses
756
756
757 Here the features are "histedit" and "chunkselector".
757 Here the features are "histedit" and "chunkselector".
758
758
759 The configuration above means that the default interfaces for commands
759 The configuration above means that the default interfaces for commands
760 is curses, the interface for histedit is text and the interface for
760 is curses, the interface for histedit is text and the interface for
761 selecting chunk is crecord (the best curses interface available).
761 selecting chunk is crecord (the best curses interface available).
762
762
763 Consider the following example:
763 Consider the following example:
764 ui.interface = curses
764 ui.interface = curses
765 ui.interface.histedit = text
765 ui.interface.histedit = text
766
766
767 Then histedit will use the text interface and chunkselector will use
767 Then histedit will use the text interface and chunkselector will use
768 the default curses interface (crecord at the moment).
768 the default curses interface (crecord at the moment).
769 """
769 """
770 alldefaults = frozenset(["text", "curses"])
770 alldefaults = frozenset(["text", "curses"])
771
771
772 featureinterfaces = {
772 featureinterfaces = {
773 "chunkselector": [
773 "chunkselector": [
774 "text",
774 "text",
775 "curses",
775 "curses",
776 ]
776 ]
777 }
777 }
778
778
779 # Feature-specific interface
779 # Feature-specific interface
780 if feature not in featureinterfaces.keys():
780 if feature not in featureinterfaces.keys():
781 # Programming error, not user error
781 # Programming error, not user error
782 raise ValueError("Unknown feature requested %s" % feature)
782 raise ValueError("Unknown feature requested %s" % feature)
783
783
784 availableinterfaces = frozenset(featureinterfaces[feature])
784 availableinterfaces = frozenset(featureinterfaces[feature])
785 if alldefaults > availableinterfaces:
785 if alldefaults > availableinterfaces:
786 # Programming error, not user error. We need a use case to
786 # Programming error, not user error. We need a use case to
787 # define the right thing to do here.
787 # define the right thing to do here.
788 raise ValueError(
788 raise ValueError(
789 "Feature %s does not handle all default interfaces" %
789 "Feature %s does not handle all default interfaces" %
790 feature)
790 feature)
791
791
792 if self.plain():
792 if self.plain():
793 return "text"
793 return "text"
794
794
795 # Default interface for all the features
795 # Default interface for all the features
796 defaultinterface = "text"
796 defaultinterface = "text"
797 i = self.config("ui", "interface", None)
797 i = self.config("ui", "interface", None)
798 if i in alldefaults:
798 if i in alldefaults:
799 defaultinterface = i
799 defaultinterface = i
800
800
801 choseninterface = defaultinterface
801 choseninterface = defaultinterface
802 f = self.config("ui", "interface.%s" % feature, None)
802 f = self.config("ui", "interface.%s" % feature, None)
803 if f in availableinterfaces:
803 if f in availableinterfaces:
804 choseninterface = f
804 choseninterface = f
805
805
806 if i is not None and defaultinterface != i:
806 if i is not None and defaultinterface != i:
807 if f is not None:
807 if f is not None:
808 self.warn(_("invalid value for ui.interface: %s\n") %
808 self.warn(_("invalid value for ui.interface: %s\n") %
809 (i,))
809 (i,))
810 else:
810 else:
811 self.warn(_("invalid value for ui.interface: %s (using %s)\n") %
811 self.warn(_("invalid value for ui.interface: %s (using %s)\n") %
812 (i, choseninterface))
812 (i, choseninterface))
813 if f is not None and choseninterface != f:
813 if f is not None and choseninterface != f:
814 self.warn(_("invalid value for ui.interface.%s: %s (using %s)\n") %
814 self.warn(_("invalid value for ui.interface.%s: %s (using %s)\n") %
815 (feature, f, choseninterface))
815 (feature, f, choseninterface))
816
816
817 return choseninterface
817 return choseninterface
818
818
819 def interactive(self):
819 def interactive(self):
820 '''is interactive input allowed?
820 '''is interactive input allowed?
821
821
822 An interactive session is a session where input can be reasonably read
822 An interactive session is a session where input can be reasonably read
823 from `sys.stdin'. If this function returns false, any attempt to read
823 from `sys.stdin'. If this function returns false, any attempt to read
824 from stdin should fail with an error, unless a sensible default has been
824 from stdin should fail with an error, unless a sensible default has been
825 specified.
825 specified.
826
826
827 Interactiveness is triggered by the value of the `ui.interactive'
827 Interactiveness is triggered by the value of the `ui.interactive'
828 configuration variable or - if it is unset - when `sys.stdin' points
828 configuration variable or - if it is unset - when `sys.stdin' points
829 to a terminal device.
829 to a terminal device.
830
830
831 This function refers to input only; for output, see `ui.formatted()'.
831 This function refers to input only; for output, see `ui.formatted()'.
832 '''
832 '''
833 i = self.configbool("ui", "interactive", None)
833 i = self.configbool("ui", "interactive", None)
834 if i is None:
834 if i is None:
835 # some environments replace stdin without implementing isatty
835 # some environments replace stdin without implementing isatty
836 # usually those are non-interactive
836 # usually those are non-interactive
837 return self._isatty(self.fin)
837 return self._isatty(self.fin)
838
838
839 return i
839 return i
840
840
841 def termwidth(self):
841 def termwidth(self):
842 '''how wide is the terminal in columns?
842 '''how wide is the terminal in columns?
843 '''
843 '''
844 if 'COLUMNS' in encoding.environ:
844 if 'COLUMNS' in encoding.environ:
845 try:
845 try:
846 return int(encoding.environ['COLUMNS'])
846 return int(encoding.environ['COLUMNS'])
847 except ValueError:
847 except ValueError:
848 pass
848 pass
849 return scmutil.termsize(self)[0]
849 return scmutil.termsize(self)[0]
850
850
851 def formatted(self):
851 def formatted(self):
852 '''should formatted output be used?
852 '''should formatted output be used?
853
853
854 It is often desirable to format the output to suite the output medium.
854 It is often desirable to format the output to suite the output medium.
855 Examples of this are truncating long lines or colorizing messages.
855 Examples of this are truncating long lines or colorizing messages.
856 However, this is not often not desirable when piping output into other
856 However, this is not often not desirable when piping output into other
857 utilities, e.g. `grep'.
857 utilities, e.g. `grep'.
858
858
859 Formatted output is triggered by the value of the `ui.formatted'
859 Formatted output is triggered by the value of the `ui.formatted'
860 configuration variable or - if it is unset - when `sys.stdout' points
860 configuration variable or - if it is unset - when `sys.stdout' points
861 to a terminal device. Please note that `ui.formatted' should be
861 to a terminal device. Please note that `ui.formatted' should be
862 considered an implementation detail; it is not intended for use outside
862 considered an implementation detail; it is not intended for use outside
863 Mercurial or its extensions.
863 Mercurial or its extensions.
864
864
865 This function refers to output only; for input, see `ui.interactive()'.
865 This function refers to output only; for input, see `ui.interactive()'.
866 This function always returns false when in plain mode, see `ui.plain()'.
866 This function always returns false when in plain mode, see `ui.plain()'.
867 '''
867 '''
868 if self.plain():
868 if self.plain():
869 return False
869 return False
870
870
871 i = self.configbool("ui", "formatted", None)
871 i = self.configbool("ui", "formatted", None)
872 if i is None:
872 if i is None:
873 # some environments replace stdout without implementing isatty
873 # some environments replace stdout without implementing isatty
874 # usually those are non-interactive
874 # usually those are non-interactive
875 return self._isatty(self.fout)
875 return self._isatty(self.fout)
876
876
877 return i
877 return i
878
878
879 def _readline(self, prompt=''):
879 def _readline(self, prompt=''):
880 if self._isatty(self.fin):
880 if self._isatty(self.fin):
881 try:
881 try:
882 # magically add command line editing support, where
882 # magically add command line editing support, where
883 # available
883 # available
884 import readline
884 import readline
885 # force demandimport to really load the module
885 # force demandimport to really load the module
886 readline.read_history_file
886 readline.read_history_file
887 # windows sometimes raises something other than ImportError
887 # windows sometimes raises something other than ImportError
888 except Exception:
888 except Exception:
889 pass
889 pass
890
890
891 # call write() so output goes through subclassed implementation
891 # call write() so output goes through subclassed implementation
892 # e.g. color extension on Windows
892 # e.g. color extension on Windows
893 self.write(prompt, prompt=True)
893 self.write(prompt, prompt=True)
894
894
895 # instead of trying to emulate raw_input, swap (self.fin,
895 # instead of trying to emulate raw_input, swap (self.fin,
896 # self.fout) with (sys.stdin, sys.stdout)
896 # self.fout) with (sys.stdin, sys.stdout)
897 oldin = sys.stdin
897 oldin = sys.stdin
898 oldout = sys.stdout
898 oldout = sys.stdout
899 sys.stdin = self.fin
899 sys.stdin = self.fin
900 sys.stdout = self.fout
900 sys.stdout = self.fout
901 # prompt ' ' must exist; otherwise readline may delete entire line
901 # prompt ' ' must exist; otherwise readline may delete entire line
902 # - http://bugs.python.org/issue12833
902 # - http://bugs.python.org/issue12833
903 line = raw_input(' ')
903 line = raw_input(' ')
904 sys.stdin = oldin
904 sys.stdin = oldin
905 sys.stdout = oldout
905 sys.stdout = oldout
906
906
907 # When stdin is in binary mode on Windows, it can cause
907 # When stdin is in binary mode on Windows, it can cause
908 # raw_input() to emit an extra trailing carriage return
908 # raw_input() to emit an extra trailing carriage return
909 if os.linesep == '\r\n' and line and line[-1] == '\r':
909 if os.linesep == '\r\n' and line and line[-1] == '\r':
910 line = line[:-1]
910 line = line[:-1]
911 return line
911 return line
912
912
913 def prompt(self, msg, default="y"):
913 def prompt(self, msg, default="y"):
914 """Prompt user with msg, read response.
914 """Prompt user with msg, read response.
915 If ui is not interactive, the default is returned.
915 If ui is not interactive, the default is returned.
916 """
916 """
917 if not self.interactive():
917 if not self.interactive():
918 self.write(msg, ' ', default or '', "\n")
918 self.write(msg, ' ', default or '', "\n")
919 return default
919 return default
920 try:
920 try:
921 r = self._readline(self.label(msg, 'ui.prompt'))
921 r = self._readline(self.label(msg, 'ui.prompt'))
922 if not r:
922 if not r:
923 r = default
923 r = default
924 if self.configbool('ui', 'promptecho'):
924 if self.configbool('ui', 'promptecho'):
925 self.write(r, "\n")
925 self.write(r, "\n")
926 return r
926 return r
927 except EOFError:
927 except EOFError:
928 raise error.ResponseExpected()
928 raise error.ResponseExpected()
929
929
930 @staticmethod
930 @staticmethod
931 def extractchoices(prompt):
931 def extractchoices(prompt):
932 """Extract prompt message and list of choices from specified prompt.
932 """Extract prompt message and list of choices from specified prompt.
933
933
934 This returns tuple "(message, choices)", and "choices" is the
934 This returns tuple "(message, choices)", and "choices" is the
935 list of tuple "(response character, text without &)".
935 list of tuple "(response character, text without &)".
936
936
937 >>> ui.extractchoices("awake? $$ &Yes $$ &No")
937 >>> ui.extractchoices("awake? $$ &Yes $$ &No")
938 ('awake? ', [('y', 'Yes'), ('n', 'No')])
938 ('awake? ', [('y', 'Yes'), ('n', 'No')])
939 >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No")
939 >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No")
940 ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
940 ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
941 >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o")
941 >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o")
942 ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
942 ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
943 """
943 """
944
944
945 # Sadly, the prompt string may have been built with a filename
945 # Sadly, the prompt string may have been built with a filename
946 # containing "$$" so let's try to find the first valid-looking
946 # containing "$$" so let's try to find the first valid-looking
947 # prompt to start parsing. Sadly, we also can't rely on
947 # prompt to start parsing. Sadly, we also can't rely on
948 # choices containing spaces, ASCII, or basically anything
948 # choices containing spaces, ASCII, or basically anything
949 # except an ampersand followed by a character.
949 # except an ampersand followed by a character.
950 m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt)
950 m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt)
951 msg = m.group(1)
951 msg = m.group(1)
952 choices = [p.strip(' ') for p in m.group(2).split('$$')]
952 choices = [p.strip(' ') for p in m.group(2).split('$$')]
953 return (msg,
953 return (msg,
954 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
954 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
955 for s in choices])
955 for s in choices])
956
956
957 def promptchoice(self, prompt, default=0):
957 def promptchoice(self, prompt, default=0):
958 """Prompt user with a message, read response, and ensure it matches
958 """Prompt user with a message, read response, and ensure it matches
959 one of the provided choices. The prompt is formatted as follows:
959 one of the provided choices. The prompt is formatted as follows:
960
960
961 "would you like fries with that (Yn)? $$ &Yes $$ &No"
961 "would you like fries with that (Yn)? $$ &Yes $$ &No"
962
962
963 The index of the choice is returned. Responses are case
963 The index of the choice is returned. Responses are case
964 insensitive. If ui is not interactive, the default is
964 insensitive. If ui is not interactive, the default is
965 returned.
965 returned.
966 """
966 """
967
967
968 msg, choices = self.extractchoices(prompt)
968 msg, choices = self.extractchoices(prompt)
969 resps = [r for r, t in choices]
969 resps = [r for r, t in choices]
970 while True:
970 while True:
971 r = self.prompt(msg, resps[default])
971 r = self.prompt(msg, resps[default])
972 if r.lower() in resps:
972 if r.lower() in resps:
973 return resps.index(r.lower())
973 return resps.index(r.lower())
974 self.write(_("unrecognized response\n"))
974 self.write(_("unrecognized response\n"))
975
975
976 def getpass(self, prompt=None, default=None):
976 def getpass(self, prompt=None, default=None):
977 if not self.interactive():
977 if not self.interactive():
978 return default
978 return default
979 try:
979 try:
980 self.write_err(self.label(prompt or _('password: '), 'ui.prompt'))
980 self.write_err(self.label(prompt or _('password: '), 'ui.prompt'))
981 # disable getpass() only if explicitly specified. it's still valid
981 # disable getpass() only if explicitly specified. it's still valid
982 # to interact with tty even if fin is not a tty.
982 # to interact with tty even if fin is not a tty.
983 if self.configbool('ui', 'nontty'):
983 if self.configbool('ui', 'nontty'):
984 l = self.fin.readline()
984 l = self.fin.readline()
985 if not l:
985 if not l:
986 raise EOFError
986 raise EOFError
987 return l.rstrip('\n')
987 return l.rstrip('\n')
988 else:
988 else:
989 return getpass.getpass('')
989 return getpass.getpass('')
990 except EOFError:
990 except EOFError:
991 raise error.ResponseExpected()
991 raise error.ResponseExpected()
992 def status(self, *msg, **opts):
992 def status(self, *msg, **opts):
993 '''write status message to output (if ui.quiet is False)
993 '''write status message to output (if ui.quiet is False)
994
994
995 This adds an output label of "ui.status".
995 This adds an output label of "ui.status".
996 '''
996 '''
997 if not self.quiet:
997 if not self.quiet:
998 opts['label'] = opts.get('label', '') + ' ui.status'
998 opts['label'] = opts.get('label', '') + ' ui.status'
999 self.write(*msg, **opts)
999 self.write(*msg, **opts)
1000 def warn(self, *msg, **opts):
1000 def warn(self, *msg, **opts):
1001 '''write warning message to output (stderr)
1001 '''write warning message to output (stderr)
1002
1002
1003 This adds an output label of "ui.warning".
1003 This adds an output label of "ui.warning".
1004 '''
1004 '''
1005 opts['label'] = opts.get('label', '') + ' ui.warning'
1005 opts['label'] = opts.get('label', '') + ' ui.warning'
1006 self.write_err(*msg, **opts)
1006 self.write_err(*msg, **opts)
1007 def note(self, *msg, **opts):
1007 def note(self, *msg, **opts):
1008 '''write note to output (if ui.verbose is True)
1008 '''write note to output (if ui.verbose is True)
1009
1009
1010 This adds an output label of "ui.note".
1010 This adds an output label of "ui.note".
1011 '''
1011 '''
1012 if self.verbose:
1012 if self.verbose:
1013 opts['label'] = opts.get('label', '') + ' ui.note'
1013 opts['label'] = opts.get('label', '') + ' ui.note'
1014 self.write(*msg, **opts)
1014 self.write(*msg, **opts)
1015 def debug(self, *msg, **opts):
1015 def debug(self, *msg, **opts):
1016 '''write debug message to output (if ui.debugflag is True)
1016 '''write debug message to output (if ui.debugflag is True)
1017
1017
1018 This adds an output label of "ui.debug".
1018 This adds an output label of "ui.debug".
1019 '''
1019 '''
1020 if self.debugflag:
1020 if self.debugflag:
1021 opts['label'] = opts.get('label', '') + ' ui.debug'
1021 opts['label'] = opts.get('label', '') + ' ui.debug'
1022 self.write(*msg, **opts)
1022 self.write(*msg, **opts)
1023
1023
1024 def edit(self, text, user, extra=None, editform=None, pending=None,
1024 def edit(self, text, user, extra=None, editform=None, pending=None,
1025 repopath=None):
1025 repopath=None):
1026 extra_defaults = {
1026 extra_defaults = {
1027 'prefix': 'editor',
1027 'prefix': 'editor',
1028 'suffix': '.txt',
1028 'suffix': '.txt',
1029 }
1029 }
1030 if extra is not None:
1030 if extra is not None:
1031 extra_defaults.update(extra)
1031 extra_defaults.update(extra)
1032 extra = extra_defaults
1032 extra = extra_defaults
1033
1033
1034 rdir = None
1034 rdir = None
1035 if self.configbool('experimental', 'editortmpinhg'):
1035 if self.configbool('experimental', 'editortmpinhg'):
1036 rdir = repopath
1036 rdir = repopath
1037 (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
1037 (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
1038 suffix=extra['suffix'], text=True,
1038 suffix=extra['suffix'], text=True,
1039 dir=rdir)
1039 dir=rdir)
1040 try:
1040 try:
1041 f = os.fdopen(fd, "w")
1041 f = os.fdopen(fd, pycompat.sysstr("w"))
1042 f.write(text)
1042 f.write(text)
1043 f.close()
1043 f.close()
1044
1044
1045 environ = {'HGUSER': user}
1045 environ = {'HGUSER': user}
1046 if 'transplant_source' in extra:
1046 if 'transplant_source' in extra:
1047 environ.update({'HGREVISION': hex(extra['transplant_source'])})
1047 environ.update({'HGREVISION': hex(extra['transplant_source'])})
1048 for label in ('intermediate-source', 'source', 'rebase_source'):
1048 for label in ('intermediate-source', 'source', 'rebase_source'):
1049 if label in extra:
1049 if label in extra:
1050 environ.update({'HGREVISION': extra[label]})
1050 environ.update({'HGREVISION': extra[label]})
1051 break
1051 break
1052 if editform:
1052 if editform:
1053 environ.update({'HGEDITFORM': editform})
1053 environ.update({'HGEDITFORM': editform})
1054 if pending:
1054 if pending:
1055 environ.update({'HG_PENDING': pending})
1055 environ.update({'HG_PENDING': pending})
1056
1056
1057 editor = self.geteditor()
1057 editor = self.geteditor()
1058
1058
1059 self.system("%s \"%s\"" % (editor, name),
1059 self.system("%s \"%s\"" % (editor, name),
1060 environ=environ,
1060 environ=environ,
1061 onerr=error.Abort, errprefix=_("edit failed"))
1061 onerr=error.Abort, errprefix=_("edit failed"))
1062
1062
1063 f = open(name)
1063 f = open(name)
1064 t = f.read()
1064 t = f.read()
1065 f.close()
1065 f.close()
1066 finally:
1066 finally:
1067 os.unlink(name)
1067 os.unlink(name)
1068
1068
1069 return t
1069 return t
1070
1070
1071 def system(self, cmd, environ=None, cwd=None, onerr=None, errprefix=None):
1071 def system(self, cmd, environ=None, cwd=None, onerr=None, errprefix=None):
1072 '''execute shell command with appropriate output stream. command
1072 '''execute shell command with appropriate output stream. command
1073 output will be redirected if fout is not stdout.
1073 output will be redirected if fout is not stdout.
1074 '''
1074 '''
1075 out = self.fout
1075 out = self.fout
1076 if any(s[1] for s in self._bufferstates):
1076 if any(s[1] for s in self._bufferstates):
1077 out = self
1077 out = self
1078 return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
1078 return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
1079 errprefix=errprefix, out=out)
1079 errprefix=errprefix, out=out)
1080
1080
1081 def traceback(self, exc=None, force=False):
1081 def traceback(self, exc=None, force=False):
1082 '''print exception traceback if traceback printing enabled or forced.
1082 '''print exception traceback if traceback printing enabled or forced.
1083 only to call in exception handler. returns true if traceback
1083 only to call in exception handler. returns true if traceback
1084 printed.'''
1084 printed.'''
1085 if self.tracebackflag or force:
1085 if self.tracebackflag or force:
1086 if exc is None:
1086 if exc is None:
1087 exc = sys.exc_info()
1087 exc = sys.exc_info()
1088 cause = getattr(exc[1], 'cause', None)
1088 cause = getattr(exc[1], 'cause', None)
1089
1089
1090 if cause is not None:
1090 if cause is not None:
1091 causetb = traceback.format_tb(cause[2])
1091 causetb = traceback.format_tb(cause[2])
1092 exctb = traceback.format_tb(exc[2])
1092 exctb = traceback.format_tb(exc[2])
1093 exconly = traceback.format_exception_only(cause[0], cause[1])
1093 exconly = traceback.format_exception_only(cause[0], cause[1])
1094
1094
1095 # exclude frame where 'exc' was chained and rethrown from exctb
1095 # exclude frame where 'exc' was chained and rethrown from exctb
1096 self.write_err('Traceback (most recent call last):\n',
1096 self.write_err('Traceback (most recent call last):\n',
1097 ''.join(exctb[:-1]),
1097 ''.join(exctb[:-1]),
1098 ''.join(causetb),
1098 ''.join(causetb),
1099 ''.join(exconly))
1099 ''.join(exconly))
1100 else:
1100 else:
1101 output = traceback.format_exception(exc[0], exc[1], exc[2])
1101 output = traceback.format_exception(exc[0], exc[1], exc[2])
1102 self.write_err(''.join(output))
1102 self.write_err(''.join(output))
1103 return self.tracebackflag or force
1103 return self.tracebackflag or force
1104
1104
1105 def geteditor(self):
1105 def geteditor(self):
1106 '''return editor to use'''
1106 '''return editor to use'''
1107 if pycompat.sysplatform == 'plan9':
1107 if pycompat.sysplatform == 'plan9':
1108 # vi is the MIPS instruction simulator on Plan 9. We
1108 # vi is the MIPS instruction simulator on Plan 9. We
1109 # instead default to E to plumb commit messages to
1109 # instead default to E to plumb commit messages to
1110 # avoid confusion.
1110 # avoid confusion.
1111 editor = 'E'
1111 editor = 'E'
1112 else:
1112 else:
1113 editor = 'vi'
1113 editor = 'vi'
1114 return (encoding.environ.get("HGEDITOR") or
1114 return (encoding.environ.get("HGEDITOR") or
1115 self.config("ui", "editor") or
1115 self.config("ui", "editor") or
1116 encoding.environ.get("VISUAL") or
1116 encoding.environ.get("VISUAL") or
1117 encoding.environ.get("EDITOR", editor))
1117 encoding.environ.get("EDITOR", editor))
1118
1118
1119 @util.propertycache
1119 @util.propertycache
1120 def _progbar(self):
1120 def _progbar(self):
1121 """setup the progbar singleton to the ui object"""
1121 """setup the progbar singleton to the ui object"""
1122 if (self.quiet or self.debugflag
1122 if (self.quiet or self.debugflag
1123 or self.configbool('progress', 'disable', False)
1123 or self.configbool('progress', 'disable', False)
1124 or not progress.shouldprint(self)):
1124 or not progress.shouldprint(self)):
1125 return None
1125 return None
1126 return getprogbar(self)
1126 return getprogbar(self)
1127
1127
1128 def _progclear(self):
1128 def _progclear(self):
1129 """clear progress bar output if any. use it before any output"""
1129 """clear progress bar output if any. use it before any output"""
1130 if '_progbar' not in vars(self): # nothing loaded yet
1130 if '_progbar' not in vars(self): # nothing loaded yet
1131 return
1131 return
1132 if self._progbar is not None and self._progbar.printed:
1132 if self._progbar is not None and self._progbar.printed:
1133 self._progbar.clear()
1133 self._progbar.clear()
1134
1134
1135 def progress(self, topic, pos, item="", unit="", total=None):
1135 def progress(self, topic, pos, item="", unit="", total=None):
1136 '''show a progress message
1136 '''show a progress message
1137
1137
1138 By default a textual progress bar will be displayed if an operation
1138 By default a textual progress bar will be displayed if an operation
1139 takes too long. 'topic' is the current operation, 'item' is a
1139 takes too long. 'topic' is the current operation, 'item' is a
1140 non-numeric marker of the current position (i.e. the currently
1140 non-numeric marker of the current position (i.e. the currently
1141 in-process file), 'pos' is the current numeric position (i.e.
1141 in-process file), 'pos' is the current numeric position (i.e.
1142 revision, bytes, etc.), unit is a corresponding unit label,
1142 revision, bytes, etc.), unit is a corresponding unit label,
1143 and total is the highest expected pos.
1143 and total is the highest expected pos.
1144
1144
1145 Multiple nested topics may be active at a time.
1145 Multiple nested topics may be active at a time.
1146
1146
1147 All topics should be marked closed by setting pos to None at
1147 All topics should be marked closed by setting pos to None at
1148 termination.
1148 termination.
1149 '''
1149 '''
1150 if self._progbar is not None:
1150 if self._progbar is not None:
1151 self._progbar.progress(topic, pos, item=item, unit=unit,
1151 self._progbar.progress(topic, pos, item=item, unit=unit,
1152 total=total)
1152 total=total)
1153 if pos is None or not self.configbool('progress', 'debug'):
1153 if pos is None or not self.configbool('progress', 'debug'):
1154 return
1154 return
1155
1155
1156 if unit:
1156 if unit:
1157 unit = ' ' + unit
1157 unit = ' ' + unit
1158 if item:
1158 if item:
1159 item = ' ' + item
1159 item = ' ' + item
1160
1160
1161 if total:
1161 if total:
1162 pct = 100.0 * pos / total
1162 pct = 100.0 * pos / total
1163 self.debug('%s:%s %s/%s%s (%4.2f%%)\n'
1163 self.debug('%s:%s %s/%s%s (%4.2f%%)\n'
1164 % (topic, item, pos, total, unit, pct))
1164 % (topic, item, pos, total, unit, pct))
1165 else:
1165 else:
1166 self.debug('%s:%s %s%s\n' % (topic, item, pos, unit))
1166 self.debug('%s:%s %s%s\n' % (topic, item, pos, unit))
1167
1167
1168 def log(self, service, *msg, **opts):
1168 def log(self, service, *msg, **opts):
1169 '''hook for logging facility extensions
1169 '''hook for logging facility extensions
1170
1170
1171 service should be a readily-identifiable subsystem, which will
1171 service should be a readily-identifiable subsystem, which will
1172 allow filtering.
1172 allow filtering.
1173
1173
1174 *msg should be a newline-terminated format string to log, and
1174 *msg should be a newline-terminated format string to log, and
1175 then any values to %-format into that format string.
1175 then any values to %-format into that format string.
1176
1176
1177 **opts currently has no defined meanings.
1177 **opts currently has no defined meanings.
1178 '''
1178 '''
1179
1179
1180 def label(self, msg, label):
1180 def label(self, msg, label):
1181 '''style msg based on supplied label
1181 '''style msg based on supplied label
1182
1182
1183 Like ui.write(), this just returns msg unchanged, but extensions
1183 Like ui.write(), this just returns msg unchanged, but extensions
1184 and GUI tools can override it to allow styling output without
1184 and GUI tools can override it to allow styling output without
1185 writing it.
1185 writing it.
1186
1186
1187 ui.write(s, 'label') is equivalent to
1187 ui.write(s, 'label') is equivalent to
1188 ui.write(ui.label(s, 'label')).
1188 ui.write(ui.label(s, 'label')).
1189 '''
1189 '''
1190 return msg
1190 return msg
1191
1191
1192 def develwarn(self, msg, stacklevel=1, config=None):
1192 def develwarn(self, msg, stacklevel=1, config=None):
1193 """issue a developer warning message
1193 """issue a developer warning message
1194
1194
1195 Use 'stacklevel' to report the offender some layers further up in the
1195 Use 'stacklevel' to report the offender some layers further up in the
1196 stack.
1196 stack.
1197 """
1197 """
1198 if not self.configbool('devel', 'all-warnings'):
1198 if not self.configbool('devel', 'all-warnings'):
1199 if config is not None and not self.configbool('devel', config):
1199 if config is not None and not self.configbool('devel', config):
1200 return
1200 return
1201 msg = 'devel-warn: ' + msg
1201 msg = 'devel-warn: ' + msg
1202 stacklevel += 1 # get in develwarn
1202 stacklevel += 1 # get in develwarn
1203 if self.tracebackflag:
1203 if self.tracebackflag:
1204 util.debugstacktrace(msg, stacklevel, self.ferr, self.fout)
1204 util.debugstacktrace(msg, stacklevel, self.ferr, self.fout)
1205 self.log('develwarn', '%s at:\n%s' %
1205 self.log('develwarn', '%s at:\n%s' %
1206 (msg, ''.join(util.getstackframes(stacklevel))))
1206 (msg, ''.join(util.getstackframes(stacklevel))))
1207 else:
1207 else:
1208 curframe = inspect.currentframe()
1208 curframe = inspect.currentframe()
1209 calframe = inspect.getouterframes(curframe, 2)
1209 calframe = inspect.getouterframes(curframe, 2)
1210 self.write_err('%s at: %s:%s (%s)\n'
1210 self.write_err('%s at: %s:%s (%s)\n'
1211 % ((msg,) + calframe[stacklevel][1:4]))
1211 % ((msg,) + calframe[stacklevel][1:4]))
1212 self.log('develwarn', '%s at: %s:%s (%s)\n',
1212 self.log('develwarn', '%s at: %s:%s (%s)\n',
1213 msg, *calframe[stacklevel][1:4])
1213 msg, *calframe[stacklevel][1:4])
1214 curframe = calframe = None # avoid cycles
1214 curframe = calframe = None # avoid cycles
1215
1215
1216 def deprecwarn(self, msg, version):
1216 def deprecwarn(self, msg, version):
1217 """issue a deprecation warning
1217 """issue a deprecation warning
1218
1218
1219 - msg: message explaining what is deprecated and how to upgrade,
1219 - msg: message explaining what is deprecated and how to upgrade,
1220 - version: last version where the API will be supported,
1220 - version: last version where the API will be supported,
1221 """
1221 """
1222 if not (self.configbool('devel', 'all-warnings')
1222 if not (self.configbool('devel', 'all-warnings')
1223 or self.configbool('devel', 'deprec-warn')):
1223 or self.configbool('devel', 'deprec-warn')):
1224 return
1224 return
1225 msg += ("\n(compatibility will be dropped after Mercurial-%s,"
1225 msg += ("\n(compatibility will be dropped after Mercurial-%s,"
1226 " update your code.)") % version
1226 " update your code.)") % version
1227 self.develwarn(msg, stacklevel=2, config='deprec-warn')
1227 self.develwarn(msg, stacklevel=2, config='deprec-warn')
1228
1228
1229 def exportableenviron(self):
1229 def exportableenviron(self):
1230 """The environment variables that are safe to export, e.g. through
1230 """The environment variables that are safe to export, e.g. through
1231 hgweb.
1231 hgweb.
1232 """
1232 """
1233 return self._exportableenviron
1233 return self._exportableenviron
1234
1234
1235 @contextlib.contextmanager
1235 @contextlib.contextmanager
1236 def configoverride(self, overrides, source=""):
1236 def configoverride(self, overrides, source=""):
1237 """Context manager for temporary config overrides
1237 """Context manager for temporary config overrides
1238 `overrides` must be a dict of the following structure:
1238 `overrides` must be a dict of the following structure:
1239 {(section, name) : value}"""
1239 {(section, name) : value}"""
1240 backups = {}
1240 backups = {}
1241 try:
1241 try:
1242 for (section, name), value in overrides.items():
1242 for (section, name), value in overrides.items():
1243 backups[(section, name)] = self.backupconfig(section, name)
1243 backups[(section, name)] = self.backupconfig(section, name)
1244 self.setconfig(section, name, value, source)
1244 self.setconfig(section, name, value, source)
1245 yield
1245 yield
1246 finally:
1246 finally:
1247 for __, backup in backups.items():
1247 for __, backup in backups.items():
1248 self.restoreconfig(backup)
1248 self.restoreconfig(backup)
1249 # just restoring ui.quiet config to the previous value is not enough
1249 # just restoring ui.quiet config to the previous value is not enough
1250 # as it does not update ui.quiet class member
1250 # as it does not update ui.quiet class member
1251 if ('ui', 'quiet') in overrides:
1251 if ('ui', 'quiet') in overrides:
1252 self.fixconfig(section='ui')
1252 self.fixconfig(section='ui')
1253
1253
1254 class paths(dict):
1254 class paths(dict):
1255 """Represents a collection of paths and their configs.
1255 """Represents a collection of paths and their configs.
1256
1256
1257 Data is initially derived from ui instances and the config files they have
1257 Data is initially derived from ui instances and the config files they have
1258 loaded.
1258 loaded.
1259 """
1259 """
1260 def __init__(self, ui):
1260 def __init__(self, ui):
1261 dict.__init__(self)
1261 dict.__init__(self)
1262
1262
1263 for name, loc in ui.configitems('paths', ignoresub=True):
1263 for name, loc in ui.configitems('paths', ignoresub=True):
1264 # No location is the same as not existing.
1264 # No location is the same as not existing.
1265 if not loc:
1265 if not loc:
1266 continue
1266 continue
1267 loc, sub = ui.configsuboptions('paths', name)
1267 loc, sub = ui.configsuboptions('paths', name)
1268 self[name] = path(ui, name, rawloc=loc, suboptions=sub)
1268 self[name] = path(ui, name, rawloc=loc, suboptions=sub)
1269
1269
1270 def getpath(self, name, default=None):
1270 def getpath(self, name, default=None):
1271 """Return a ``path`` from a string, falling back to default.
1271 """Return a ``path`` from a string, falling back to default.
1272
1272
1273 ``name`` can be a named path or locations. Locations are filesystem
1273 ``name`` can be a named path or locations. Locations are filesystem
1274 paths or URIs.
1274 paths or URIs.
1275
1275
1276 Returns None if ``name`` is not a registered path, a URI, or a local
1276 Returns None if ``name`` is not a registered path, a URI, or a local
1277 path to a repo.
1277 path to a repo.
1278 """
1278 """
1279 # Only fall back to default if no path was requested.
1279 # Only fall back to default if no path was requested.
1280 if name is None:
1280 if name is None:
1281 if not default:
1281 if not default:
1282 default = ()
1282 default = ()
1283 elif not isinstance(default, (tuple, list)):
1283 elif not isinstance(default, (tuple, list)):
1284 default = (default,)
1284 default = (default,)
1285 for k in default:
1285 for k in default:
1286 try:
1286 try:
1287 return self[k]
1287 return self[k]
1288 except KeyError:
1288 except KeyError:
1289 continue
1289 continue
1290 return None
1290 return None
1291
1291
1292 # Most likely empty string.
1292 # Most likely empty string.
1293 # This may need to raise in the future.
1293 # This may need to raise in the future.
1294 if not name:
1294 if not name:
1295 return None
1295 return None
1296
1296
1297 try:
1297 try:
1298 return self[name]
1298 return self[name]
1299 except KeyError:
1299 except KeyError:
1300 # Try to resolve as a local path or URI.
1300 # Try to resolve as a local path or URI.
1301 try:
1301 try:
1302 # We don't pass sub-options in, so no need to pass ui instance.
1302 # We don't pass sub-options in, so no need to pass ui instance.
1303 return path(None, None, rawloc=name)
1303 return path(None, None, rawloc=name)
1304 except ValueError:
1304 except ValueError:
1305 raise error.RepoError(_('repository %s does not exist') %
1305 raise error.RepoError(_('repository %s does not exist') %
1306 name)
1306 name)
1307
1307
1308 _pathsuboptions = {}
1308 _pathsuboptions = {}
1309
1309
1310 def pathsuboption(option, attr):
1310 def pathsuboption(option, attr):
1311 """Decorator used to declare a path sub-option.
1311 """Decorator used to declare a path sub-option.
1312
1312
1313 Arguments are the sub-option name and the attribute it should set on
1313 Arguments are the sub-option name and the attribute it should set on
1314 ``path`` instances.
1314 ``path`` instances.
1315
1315
1316 The decorated function will receive as arguments a ``ui`` instance,
1316 The decorated function will receive as arguments a ``ui`` instance,
1317 ``path`` instance, and the string value of this option from the config.
1317 ``path`` instance, and the string value of this option from the config.
1318 The function should return the value that will be set on the ``path``
1318 The function should return the value that will be set on the ``path``
1319 instance.
1319 instance.
1320
1320
1321 This decorator can be used to perform additional verification of
1321 This decorator can be used to perform additional verification of
1322 sub-options and to change the type of sub-options.
1322 sub-options and to change the type of sub-options.
1323 """
1323 """
1324 def register(func):
1324 def register(func):
1325 _pathsuboptions[option] = (attr, func)
1325 _pathsuboptions[option] = (attr, func)
1326 return func
1326 return func
1327 return register
1327 return register
1328
1328
1329 @pathsuboption('pushurl', 'pushloc')
1329 @pathsuboption('pushurl', 'pushloc')
1330 def pushurlpathoption(ui, path, value):
1330 def pushurlpathoption(ui, path, value):
1331 u = util.url(value)
1331 u = util.url(value)
1332 # Actually require a URL.
1332 # Actually require a URL.
1333 if not u.scheme:
1333 if not u.scheme:
1334 ui.warn(_('(paths.%s:pushurl not a URL; ignoring)\n') % path.name)
1334 ui.warn(_('(paths.%s:pushurl not a URL; ignoring)\n') % path.name)
1335 return None
1335 return None
1336
1336
1337 # Don't support the #foo syntax in the push URL to declare branch to
1337 # Don't support the #foo syntax in the push URL to declare branch to
1338 # push.
1338 # push.
1339 if u.fragment:
1339 if u.fragment:
1340 ui.warn(_('("#fragment" in paths.%s:pushurl not supported; '
1340 ui.warn(_('("#fragment" in paths.%s:pushurl not supported; '
1341 'ignoring)\n') % path.name)
1341 'ignoring)\n') % path.name)
1342 u.fragment = None
1342 u.fragment = None
1343
1343
1344 return str(u)
1344 return str(u)
1345
1345
1346 @pathsuboption('pushrev', 'pushrev')
1346 @pathsuboption('pushrev', 'pushrev')
1347 def pushrevpathoption(ui, path, value):
1347 def pushrevpathoption(ui, path, value):
1348 return value
1348 return value
1349
1349
1350 class path(object):
1350 class path(object):
1351 """Represents an individual path and its configuration."""
1351 """Represents an individual path and its configuration."""
1352
1352
1353 def __init__(self, ui, name, rawloc=None, suboptions=None):
1353 def __init__(self, ui, name, rawloc=None, suboptions=None):
1354 """Construct a path from its config options.
1354 """Construct a path from its config options.
1355
1355
1356 ``ui`` is the ``ui`` instance the path is coming from.
1356 ``ui`` is the ``ui`` instance the path is coming from.
1357 ``name`` is the symbolic name of the path.
1357 ``name`` is the symbolic name of the path.
1358 ``rawloc`` is the raw location, as defined in the config.
1358 ``rawloc`` is the raw location, as defined in the config.
1359 ``pushloc`` is the raw locations pushes should be made to.
1359 ``pushloc`` is the raw locations pushes should be made to.
1360
1360
1361 If ``name`` is not defined, we require that the location be a) a local
1361 If ``name`` is not defined, we require that the location be a) a local
1362 filesystem path with a .hg directory or b) a URL. If not,
1362 filesystem path with a .hg directory or b) a URL. If not,
1363 ``ValueError`` is raised.
1363 ``ValueError`` is raised.
1364 """
1364 """
1365 if not rawloc:
1365 if not rawloc:
1366 raise ValueError('rawloc must be defined')
1366 raise ValueError('rawloc must be defined')
1367
1367
1368 # Locations may define branches via syntax <base>#<branch>.
1368 # Locations may define branches via syntax <base>#<branch>.
1369 u = util.url(rawloc)
1369 u = util.url(rawloc)
1370 branch = None
1370 branch = None
1371 if u.fragment:
1371 if u.fragment:
1372 branch = u.fragment
1372 branch = u.fragment
1373 u.fragment = None
1373 u.fragment = None
1374
1374
1375 self.url = u
1375 self.url = u
1376 self.branch = branch
1376 self.branch = branch
1377
1377
1378 self.name = name
1378 self.name = name
1379 self.rawloc = rawloc
1379 self.rawloc = rawloc
1380 self.loc = str(u)
1380 self.loc = str(u)
1381
1381
1382 # When given a raw location but not a symbolic name, validate the
1382 # When given a raw location but not a symbolic name, validate the
1383 # location is valid.
1383 # location is valid.
1384 if not name and not u.scheme and not self._isvalidlocalpath(self.loc):
1384 if not name and not u.scheme and not self._isvalidlocalpath(self.loc):
1385 raise ValueError('location is not a URL or path to a local '
1385 raise ValueError('location is not a URL or path to a local '
1386 'repo: %s' % rawloc)
1386 'repo: %s' % rawloc)
1387
1387
1388 suboptions = suboptions or {}
1388 suboptions = suboptions or {}
1389
1389
1390 # Now process the sub-options. If a sub-option is registered, its
1390 # Now process the sub-options. If a sub-option is registered, its
1391 # attribute will always be present. The value will be None if there
1391 # attribute will always be present. The value will be None if there
1392 # was no valid sub-option.
1392 # was no valid sub-option.
1393 for suboption, (attr, func) in _pathsuboptions.iteritems():
1393 for suboption, (attr, func) in _pathsuboptions.iteritems():
1394 if suboption not in suboptions:
1394 if suboption not in suboptions:
1395 setattr(self, attr, None)
1395 setattr(self, attr, None)
1396 continue
1396 continue
1397
1397
1398 value = func(ui, self, suboptions[suboption])
1398 value = func(ui, self, suboptions[suboption])
1399 setattr(self, attr, value)
1399 setattr(self, attr, value)
1400
1400
1401 def _isvalidlocalpath(self, path):
1401 def _isvalidlocalpath(self, path):
1402 """Returns True if the given path is a potentially valid repository.
1402 """Returns True if the given path is a potentially valid repository.
1403 This is its own function so that extensions can change the definition of
1403 This is its own function so that extensions can change the definition of
1404 'valid' in this case (like when pulling from a git repo into a hg
1404 'valid' in this case (like when pulling from a git repo into a hg
1405 one)."""
1405 one)."""
1406 return os.path.isdir(os.path.join(path, '.hg'))
1406 return os.path.isdir(os.path.join(path, '.hg'))
1407
1407
1408 @property
1408 @property
1409 def suboptions(self):
1409 def suboptions(self):
1410 """Return sub-options and their values for this path.
1410 """Return sub-options and their values for this path.
1411
1411
1412 This is intended to be used for presentation purposes.
1412 This is intended to be used for presentation purposes.
1413 """
1413 """
1414 d = {}
1414 d = {}
1415 for subopt, (attr, _func) in _pathsuboptions.iteritems():
1415 for subopt, (attr, _func) in _pathsuboptions.iteritems():
1416 value = getattr(self, attr)
1416 value = getattr(self, attr)
1417 if value is not None:
1417 if value is not None:
1418 d[subopt] = value
1418 d[subopt] = value
1419 return d
1419 return d
1420
1420
1421 # we instantiate one globally shared progress bar to avoid
1421 # we instantiate one globally shared progress bar to avoid
1422 # competing progress bars when multiple UI objects get created
1422 # competing progress bars when multiple UI objects get created
1423 _progresssingleton = None
1423 _progresssingleton = None
1424
1424
1425 def getprogbar(ui):
1425 def getprogbar(ui):
1426 global _progresssingleton
1426 global _progresssingleton
1427 if _progresssingleton is None:
1427 if _progresssingleton is None:
1428 # passing 'ui' object to the singleton is fishy,
1428 # passing 'ui' object to the singleton is fishy,
1429 # this is how the extension used to work but feel free to rework it.
1429 # this is how the extension used to work but feel free to rework it.
1430 _progresssingleton = progress.progbar(ui)
1430 _progresssingleton = progress.progbar(ui)
1431 return _progresssingleton
1431 return _progresssingleton
@@ -1,3551 +1,3551 b''
1 # util.py - Mercurial utility functions and platform specific implementations
1 # util.py - Mercurial utility functions and platform specific implementations
2 #
2 #
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
3 # Copyright 2005 K. Thananchayan <thananck@yahoo.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
8 # GNU General Public License version 2 or any later version.
9
9
10 """Mercurial utility functions and platform specific implementations.
10 """Mercurial utility functions and platform specific implementations.
11
11
12 This contains helper routines that are independent of the SCM core and
12 This contains helper routines that are independent of the SCM core and
13 hide platform-specific details from the core.
13 hide platform-specific details from the core.
14 """
14 """
15
15
16 from __future__ import absolute_import
16 from __future__ import absolute_import
17
17
18 import bz2
18 import bz2
19 import calendar
19 import calendar
20 import collections
20 import collections
21 import datetime
21 import datetime
22 import errno
22 import errno
23 import gc
23 import gc
24 import hashlib
24 import hashlib
25 import imp
25 import imp
26 import os
26 import os
27 import platform as pyplatform
27 import platform as pyplatform
28 import re as remod
28 import re as remod
29 import shutil
29 import shutil
30 import signal
30 import signal
31 import socket
31 import socket
32 import stat
32 import stat
33 import string
33 import string
34 import subprocess
34 import subprocess
35 import sys
35 import sys
36 import tempfile
36 import tempfile
37 import textwrap
37 import textwrap
38 import time
38 import time
39 import traceback
39 import traceback
40 import zlib
40 import zlib
41
41
42 from . import (
42 from . import (
43 encoding,
43 encoding,
44 error,
44 error,
45 i18n,
45 i18n,
46 osutil,
46 osutil,
47 parsers,
47 parsers,
48 pycompat,
48 pycompat,
49 )
49 )
50
50
51 empty = pycompat.empty
51 empty = pycompat.empty
52 httplib = pycompat.httplib
52 httplib = pycompat.httplib
53 httpserver = pycompat.httpserver
53 httpserver = pycompat.httpserver
54 pickle = pycompat.pickle
54 pickle = pycompat.pickle
55 queue = pycompat.queue
55 queue = pycompat.queue
56 socketserver = pycompat.socketserver
56 socketserver = pycompat.socketserver
57 stderr = pycompat.stderr
57 stderr = pycompat.stderr
58 stdin = pycompat.stdin
58 stdin = pycompat.stdin
59 stdout = pycompat.stdout
59 stdout = pycompat.stdout
60 stringio = pycompat.stringio
60 stringio = pycompat.stringio
61 urlerr = pycompat.urlerr
61 urlerr = pycompat.urlerr
62 urlparse = pycompat.urlparse
62 urlparse = pycompat.urlparse
63 urlreq = pycompat.urlreq
63 urlreq = pycompat.urlreq
64 xmlrpclib = pycompat.xmlrpclib
64 xmlrpclib = pycompat.xmlrpclib
65
65
66 def isatty(fp):
66 def isatty(fp):
67 try:
67 try:
68 return fp.isatty()
68 return fp.isatty()
69 except AttributeError:
69 except AttributeError:
70 return False
70 return False
71
71
72 # glibc determines buffering on first write to stdout - if we replace a TTY
72 # glibc determines buffering on first write to stdout - if we replace a TTY
73 # destined stdout with a pipe destined stdout (e.g. pager), we want line
73 # destined stdout with a pipe destined stdout (e.g. pager), we want line
74 # buffering
74 # buffering
75 if isatty(stdout):
75 if isatty(stdout):
76 stdout = os.fdopen(stdout.fileno(), 'wb', 1)
76 stdout = os.fdopen(stdout.fileno(), 'wb', 1)
77
77
78 if pycompat.osname == 'nt':
78 if pycompat.osname == 'nt':
79 from . import windows as platform
79 from . import windows as platform
80 stdout = platform.winstdout(stdout)
80 stdout = platform.winstdout(stdout)
81 else:
81 else:
82 from . import posix as platform
82 from . import posix as platform
83
83
84 _ = i18n._
84 _ = i18n._
85
85
86 bindunixsocket = platform.bindunixsocket
86 bindunixsocket = platform.bindunixsocket
87 cachestat = platform.cachestat
87 cachestat = platform.cachestat
88 checkexec = platform.checkexec
88 checkexec = platform.checkexec
89 checklink = platform.checklink
89 checklink = platform.checklink
90 copymode = platform.copymode
90 copymode = platform.copymode
91 executablepath = platform.executablepath
91 executablepath = platform.executablepath
92 expandglobs = platform.expandglobs
92 expandglobs = platform.expandglobs
93 explainexit = platform.explainexit
93 explainexit = platform.explainexit
94 findexe = platform.findexe
94 findexe = platform.findexe
95 gethgcmd = platform.gethgcmd
95 gethgcmd = platform.gethgcmd
96 getuser = platform.getuser
96 getuser = platform.getuser
97 getpid = os.getpid
97 getpid = os.getpid
98 groupmembers = platform.groupmembers
98 groupmembers = platform.groupmembers
99 groupname = platform.groupname
99 groupname = platform.groupname
100 hidewindow = platform.hidewindow
100 hidewindow = platform.hidewindow
101 isexec = platform.isexec
101 isexec = platform.isexec
102 isowner = platform.isowner
102 isowner = platform.isowner
103 localpath = platform.localpath
103 localpath = platform.localpath
104 lookupreg = platform.lookupreg
104 lookupreg = platform.lookupreg
105 makedir = platform.makedir
105 makedir = platform.makedir
106 nlinks = platform.nlinks
106 nlinks = platform.nlinks
107 normpath = platform.normpath
107 normpath = platform.normpath
108 normcase = platform.normcase
108 normcase = platform.normcase
109 normcasespec = platform.normcasespec
109 normcasespec = platform.normcasespec
110 normcasefallback = platform.normcasefallback
110 normcasefallback = platform.normcasefallback
111 openhardlinks = platform.openhardlinks
111 openhardlinks = platform.openhardlinks
112 oslink = platform.oslink
112 oslink = platform.oslink
113 parsepatchoutput = platform.parsepatchoutput
113 parsepatchoutput = platform.parsepatchoutput
114 pconvert = platform.pconvert
114 pconvert = platform.pconvert
115 poll = platform.poll
115 poll = platform.poll
116 popen = platform.popen
116 popen = platform.popen
117 posixfile = platform.posixfile
117 posixfile = platform.posixfile
118 quotecommand = platform.quotecommand
118 quotecommand = platform.quotecommand
119 readpipe = platform.readpipe
119 readpipe = platform.readpipe
120 rename = platform.rename
120 rename = platform.rename
121 removedirs = platform.removedirs
121 removedirs = platform.removedirs
122 samedevice = platform.samedevice
122 samedevice = platform.samedevice
123 samefile = platform.samefile
123 samefile = platform.samefile
124 samestat = platform.samestat
124 samestat = platform.samestat
125 setbinary = platform.setbinary
125 setbinary = platform.setbinary
126 setflags = platform.setflags
126 setflags = platform.setflags
127 setsignalhandler = platform.setsignalhandler
127 setsignalhandler = platform.setsignalhandler
128 shellquote = platform.shellquote
128 shellquote = platform.shellquote
129 spawndetached = platform.spawndetached
129 spawndetached = platform.spawndetached
130 split = platform.split
130 split = platform.split
131 sshargs = platform.sshargs
131 sshargs = platform.sshargs
132 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
132 statfiles = getattr(osutil, 'statfiles', platform.statfiles)
133 statisexec = platform.statisexec
133 statisexec = platform.statisexec
134 statislink = platform.statislink
134 statislink = platform.statislink
135 testpid = platform.testpid
135 testpid = platform.testpid
136 umask = platform.umask
136 umask = platform.umask
137 unlink = platform.unlink
137 unlink = platform.unlink
138 unlinkpath = platform.unlinkpath
138 unlinkpath = platform.unlinkpath
139 username = platform.username
139 username = platform.username
140
140
141 # Python compatibility
141 # Python compatibility
142
142
143 _notset = object()
143 _notset = object()
144
144
145 # disable Python's problematic floating point timestamps (issue4836)
145 # disable Python's problematic floating point timestamps (issue4836)
146 # (Python hypocritically says you shouldn't change this behavior in
146 # (Python hypocritically says you shouldn't change this behavior in
147 # libraries, and sure enough Mercurial is not a library.)
147 # libraries, and sure enough Mercurial is not a library.)
148 os.stat_float_times(False)
148 os.stat_float_times(False)
149
149
150 def safehasattr(thing, attr):
150 def safehasattr(thing, attr):
151 return getattr(thing, attr, _notset) is not _notset
151 return getattr(thing, attr, _notset) is not _notset
152
152
153 def bitsfrom(container):
153 def bitsfrom(container):
154 bits = 0
154 bits = 0
155 for bit in container:
155 for bit in container:
156 bits |= bit
156 bits |= bit
157 return bits
157 return bits
158
158
159 DIGESTS = {
159 DIGESTS = {
160 'md5': hashlib.md5,
160 'md5': hashlib.md5,
161 'sha1': hashlib.sha1,
161 'sha1': hashlib.sha1,
162 'sha512': hashlib.sha512,
162 'sha512': hashlib.sha512,
163 }
163 }
164 # List of digest types from strongest to weakest
164 # List of digest types from strongest to weakest
165 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
165 DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5']
166
166
167 for k in DIGESTS_BY_STRENGTH:
167 for k in DIGESTS_BY_STRENGTH:
168 assert k in DIGESTS
168 assert k in DIGESTS
169
169
170 class digester(object):
170 class digester(object):
171 """helper to compute digests.
171 """helper to compute digests.
172
172
173 This helper can be used to compute one or more digests given their name.
173 This helper can be used to compute one or more digests given their name.
174
174
175 >>> d = digester(['md5', 'sha1'])
175 >>> d = digester(['md5', 'sha1'])
176 >>> d.update('foo')
176 >>> d.update('foo')
177 >>> [k for k in sorted(d)]
177 >>> [k for k in sorted(d)]
178 ['md5', 'sha1']
178 ['md5', 'sha1']
179 >>> d['md5']
179 >>> d['md5']
180 'acbd18db4cc2f85cedef654fccc4a4d8'
180 'acbd18db4cc2f85cedef654fccc4a4d8'
181 >>> d['sha1']
181 >>> d['sha1']
182 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
182 '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33'
183 >>> digester.preferred(['md5', 'sha1'])
183 >>> digester.preferred(['md5', 'sha1'])
184 'sha1'
184 'sha1'
185 """
185 """
186
186
187 def __init__(self, digests, s=''):
187 def __init__(self, digests, s=''):
188 self._hashes = {}
188 self._hashes = {}
189 for k in digests:
189 for k in digests:
190 if k not in DIGESTS:
190 if k not in DIGESTS:
191 raise Abort(_('unknown digest type: %s') % k)
191 raise Abort(_('unknown digest type: %s') % k)
192 self._hashes[k] = DIGESTS[k]()
192 self._hashes[k] = DIGESTS[k]()
193 if s:
193 if s:
194 self.update(s)
194 self.update(s)
195
195
196 def update(self, data):
196 def update(self, data):
197 for h in self._hashes.values():
197 for h in self._hashes.values():
198 h.update(data)
198 h.update(data)
199
199
200 def __getitem__(self, key):
200 def __getitem__(self, key):
201 if key not in DIGESTS:
201 if key not in DIGESTS:
202 raise Abort(_('unknown digest type: %s') % k)
202 raise Abort(_('unknown digest type: %s') % k)
203 return self._hashes[key].hexdigest()
203 return self._hashes[key].hexdigest()
204
204
205 def __iter__(self):
205 def __iter__(self):
206 return iter(self._hashes)
206 return iter(self._hashes)
207
207
208 @staticmethod
208 @staticmethod
209 def preferred(supported):
209 def preferred(supported):
210 """returns the strongest digest type in both supported and DIGESTS."""
210 """returns the strongest digest type in both supported and DIGESTS."""
211
211
212 for k in DIGESTS_BY_STRENGTH:
212 for k in DIGESTS_BY_STRENGTH:
213 if k in supported:
213 if k in supported:
214 return k
214 return k
215 return None
215 return None
216
216
217 class digestchecker(object):
217 class digestchecker(object):
218 """file handle wrapper that additionally checks content against a given
218 """file handle wrapper that additionally checks content against a given
219 size and digests.
219 size and digests.
220
220
221 d = digestchecker(fh, size, {'md5': '...'})
221 d = digestchecker(fh, size, {'md5': '...'})
222
222
223 When multiple digests are given, all of them are validated.
223 When multiple digests are given, all of them are validated.
224 """
224 """
225
225
226 def __init__(self, fh, size, digests):
226 def __init__(self, fh, size, digests):
227 self._fh = fh
227 self._fh = fh
228 self._size = size
228 self._size = size
229 self._got = 0
229 self._got = 0
230 self._digests = dict(digests)
230 self._digests = dict(digests)
231 self._digester = digester(self._digests.keys())
231 self._digester = digester(self._digests.keys())
232
232
233 def read(self, length=-1):
233 def read(self, length=-1):
234 content = self._fh.read(length)
234 content = self._fh.read(length)
235 self._digester.update(content)
235 self._digester.update(content)
236 self._got += len(content)
236 self._got += len(content)
237 return content
237 return content
238
238
239 def validate(self):
239 def validate(self):
240 if self._size != self._got:
240 if self._size != self._got:
241 raise Abort(_('size mismatch: expected %d, got %d') %
241 raise Abort(_('size mismatch: expected %d, got %d') %
242 (self._size, self._got))
242 (self._size, self._got))
243 for k, v in self._digests.items():
243 for k, v in self._digests.items():
244 if v != self._digester[k]:
244 if v != self._digester[k]:
245 # i18n: first parameter is a digest name
245 # i18n: first parameter is a digest name
246 raise Abort(_('%s mismatch: expected %s, got %s') %
246 raise Abort(_('%s mismatch: expected %s, got %s') %
247 (k, v, self._digester[k]))
247 (k, v, self._digester[k]))
248
248
249 try:
249 try:
250 buffer = buffer
250 buffer = buffer
251 except NameError:
251 except NameError:
252 if not pycompat.ispy3:
252 if not pycompat.ispy3:
253 def buffer(sliceable, offset=0, length=None):
253 def buffer(sliceable, offset=0, length=None):
254 if length is not None:
254 if length is not None:
255 return sliceable[offset:offset + length]
255 return sliceable[offset:offset + length]
256 return sliceable[offset:]
256 return sliceable[offset:]
257 else:
257 else:
258 def buffer(sliceable, offset=0, length=None):
258 def buffer(sliceable, offset=0, length=None):
259 if length is not None:
259 if length is not None:
260 return memoryview(sliceable)[offset:offset + length]
260 return memoryview(sliceable)[offset:offset + length]
261 return memoryview(sliceable)[offset:]
261 return memoryview(sliceable)[offset:]
262
262
263 closefds = pycompat.osname == 'posix'
263 closefds = pycompat.osname == 'posix'
264
264
265 _chunksize = 4096
265 _chunksize = 4096
266
266
267 class bufferedinputpipe(object):
267 class bufferedinputpipe(object):
268 """a manually buffered input pipe
268 """a manually buffered input pipe
269
269
270 Python will not let us use buffered IO and lazy reading with 'polling' at
270 Python will not let us use buffered IO and lazy reading with 'polling' at
271 the same time. We cannot probe the buffer state and select will not detect
271 the same time. We cannot probe the buffer state and select will not detect
272 that data are ready to read if they are already buffered.
272 that data are ready to read if they are already buffered.
273
273
274 This class let us work around that by implementing its own buffering
274 This class let us work around that by implementing its own buffering
275 (allowing efficient readline) while offering a way to know if the buffer is
275 (allowing efficient readline) while offering a way to know if the buffer is
276 empty from the output (allowing collaboration of the buffer with polling).
276 empty from the output (allowing collaboration of the buffer with polling).
277
277
278 This class lives in the 'util' module because it makes use of the 'os'
278 This class lives in the 'util' module because it makes use of the 'os'
279 module from the python stdlib.
279 module from the python stdlib.
280 """
280 """
281
281
282 def __init__(self, input):
282 def __init__(self, input):
283 self._input = input
283 self._input = input
284 self._buffer = []
284 self._buffer = []
285 self._eof = False
285 self._eof = False
286 self._lenbuf = 0
286 self._lenbuf = 0
287
287
288 @property
288 @property
289 def hasbuffer(self):
289 def hasbuffer(self):
290 """True is any data is currently buffered
290 """True is any data is currently buffered
291
291
292 This will be used externally a pre-step for polling IO. If there is
292 This will be used externally a pre-step for polling IO. If there is
293 already data then no polling should be set in place."""
293 already data then no polling should be set in place."""
294 return bool(self._buffer)
294 return bool(self._buffer)
295
295
296 @property
296 @property
297 def closed(self):
297 def closed(self):
298 return self._input.closed
298 return self._input.closed
299
299
300 def fileno(self):
300 def fileno(self):
301 return self._input.fileno()
301 return self._input.fileno()
302
302
303 def close(self):
303 def close(self):
304 return self._input.close()
304 return self._input.close()
305
305
306 def read(self, size):
306 def read(self, size):
307 while (not self._eof) and (self._lenbuf < size):
307 while (not self._eof) and (self._lenbuf < size):
308 self._fillbuffer()
308 self._fillbuffer()
309 return self._frombuffer(size)
309 return self._frombuffer(size)
310
310
311 def readline(self, *args, **kwargs):
311 def readline(self, *args, **kwargs):
312 if 1 < len(self._buffer):
312 if 1 < len(self._buffer):
313 # this should not happen because both read and readline end with a
313 # this should not happen because both read and readline end with a
314 # _frombuffer call that collapse it.
314 # _frombuffer call that collapse it.
315 self._buffer = [''.join(self._buffer)]
315 self._buffer = [''.join(self._buffer)]
316 self._lenbuf = len(self._buffer[0])
316 self._lenbuf = len(self._buffer[0])
317 lfi = -1
317 lfi = -1
318 if self._buffer:
318 if self._buffer:
319 lfi = self._buffer[-1].find('\n')
319 lfi = self._buffer[-1].find('\n')
320 while (not self._eof) and lfi < 0:
320 while (not self._eof) and lfi < 0:
321 self._fillbuffer()
321 self._fillbuffer()
322 if self._buffer:
322 if self._buffer:
323 lfi = self._buffer[-1].find('\n')
323 lfi = self._buffer[-1].find('\n')
324 size = lfi + 1
324 size = lfi + 1
325 if lfi < 0: # end of file
325 if lfi < 0: # end of file
326 size = self._lenbuf
326 size = self._lenbuf
327 elif 1 < len(self._buffer):
327 elif 1 < len(self._buffer):
328 # we need to take previous chunks into account
328 # we need to take previous chunks into account
329 size += self._lenbuf - len(self._buffer[-1])
329 size += self._lenbuf - len(self._buffer[-1])
330 return self._frombuffer(size)
330 return self._frombuffer(size)
331
331
332 def _frombuffer(self, size):
332 def _frombuffer(self, size):
333 """return at most 'size' data from the buffer
333 """return at most 'size' data from the buffer
334
334
335 The data are removed from the buffer."""
335 The data are removed from the buffer."""
336 if size == 0 or not self._buffer:
336 if size == 0 or not self._buffer:
337 return ''
337 return ''
338 buf = self._buffer[0]
338 buf = self._buffer[0]
339 if 1 < len(self._buffer):
339 if 1 < len(self._buffer):
340 buf = ''.join(self._buffer)
340 buf = ''.join(self._buffer)
341
341
342 data = buf[:size]
342 data = buf[:size]
343 buf = buf[len(data):]
343 buf = buf[len(data):]
344 if buf:
344 if buf:
345 self._buffer = [buf]
345 self._buffer = [buf]
346 self._lenbuf = len(buf)
346 self._lenbuf = len(buf)
347 else:
347 else:
348 self._buffer = []
348 self._buffer = []
349 self._lenbuf = 0
349 self._lenbuf = 0
350 return data
350 return data
351
351
352 def _fillbuffer(self):
352 def _fillbuffer(self):
353 """read data to the buffer"""
353 """read data to the buffer"""
354 data = os.read(self._input.fileno(), _chunksize)
354 data = os.read(self._input.fileno(), _chunksize)
355 if not data:
355 if not data:
356 self._eof = True
356 self._eof = True
357 else:
357 else:
358 self._lenbuf += len(data)
358 self._lenbuf += len(data)
359 self._buffer.append(data)
359 self._buffer.append(data)
360
360
361 def popen2(cmd, env=None, newlines=False):
361 def popen2(cmd, env=None, newlines=False):
362 # Setting bufsize to -1 lets the system decide the buffer size.
362 # Setting bufsize to -1 lets the system decide the buffer size.
363 # The default for bufsize is 0, meaning unbuffered. This leads to
363 # The default for bufsize is 0, meaning unbuffered. This leads to
364 # poor performance on Mac OS X: http://bugs.python.org/issue4194
364 # poor performance on Mac OS X: http://bugs.python.org/issue4194
365 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
365 p = subprocess.Popen(cmd, shell=True, bufsize=-1,
366 close_fds=closefds,
366 close_fds=closefds,
367 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
367 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
368 universal_newlines=newlines,
368 universal_newlines=newlines,
369 env=env)
369 env=env)
370 return p.stdin, p.stdout
370 return p.stdin, p.stdout
371
371
372 def popen3(cmd, env=None, newlines=False):
372 def popen3(cmd, env=None, newlines=False):
373 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
373 stdin, stdout, stderr, p = popen4(cmd, env, newlines)
374 return stdin, stdout, stderr
374 return stdin, stdout, stderr
375
375
376 def popen4(cmd, env=None, newlines=False, bufsize=-1):
376 def popen4(cmd, env=None, newlines=False, bufsize=-1):
377 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
377 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
378 close_fds=closefds,
378 close_fds=closefds,
379 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
379 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
380 stderr=subprocess.PIPE,
380 stderr=subprocess.PIPE,
381 universal_newlines=newlines,
381 universal_newlines=newlines,
382 env=env)
382 env=env)
383 return p.stdin, p.stdout, p.stderr, p
383 return p.stdin, p.stdout, p.stderr, p
384
384
385 def version():
385 def version():
386 """Return version information if available."""
386 """Return version information if available."""
387 try:
387 try:
388 from . import __version__
388 from . import __version__
389 return __version__.version
389 return __version__.version
390 except ImportError:
390 except ImportError:
391 return 'unknown'
391 return 'unknown'
392
392
393 def versiontuple(v=None, n=4):
393 def versiontuple(v=None, n=4):
394 """Parses a Mercurial version string into an N-tuple.
394 """Parses a Mercurial version string into an N-tuple.
395
395
396 The version string to be parsed is specified with the ``v`` argument.
396 The version string to be parsed is specified with the ``v`` argument.
397 If it isn't defined, the current Mercurial version string will be parsed.
397 If it isn't defined, the current Mercurial version string will be parsed.
398
398
399 ``n`` can be 2, 3, or 4. Here is how some version strings map to
399 ``n`` can be 2, 3, or 4. Here is how some version strings map to
400 returned values:
400 returned values:
401
401
402 >>> v = '3.6.1+190-df9b73d2d444'
402 >>> v = '3.6.1+190-df9b73d2d444'
403 >>> versiontuple(v, 2)
403 >>> versiontuple(v, 2)
404 (3, 6)
404 (3, 6)
405 >>> versiontuple(v, 3)
405 >>> versiontuple(v, 3)
406 (3, 6, 1)
406 (3, 6, 1)
407 >>> versiontuple(v, 4)
407 >>> versiontuple(v, 4)
408 (3, 6, 1, '190-df9b73d2d444')
408 (3, 6, 1, '190-df9b73d2d444')
409
409
410 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
410 >>> versiontuple('3.6.1+190-df9b73d2d444+20151118')
411 (3, 6, 1, '190-df9b73d2d444+20151118')
411 (3, 6, 1, '190-df9b73d2d444+20151118')
412
412
413 >>> v = '3.6'
413 >>> v = '3.6'
414 >>> versiontuple(v, 2)
414 >>> versiontuple(v, 2)
415 (3, 6)
415 (3, 6)
416 >>> versiontuple(v, 3)
416 >>> versiontuple(v, 3)
417 (3, 6, None)
417 (3, 6, None)
418 >>> versiontuple(v, 4)
418 >>> versiontuple(v, 4)
419 (3, 6, None, None)
419 (3, 6, None, None)
420
420
421 >>> v = '3.9-rc'
421 >>> v = '3.9-rc'
422 >>> versiontuple(v, 2)
422 >>> versiontuple(v, 2)
423 (3, 9)
423 (3, 9)
424 >>> versiontuple(v, 3)
424 >>> versiontuple(v, 3)
425 (3, 9, None)
425 (3, 9, None)
426 >>> versiontuple(v, 4)
426 >>> versiontuple(v, 4)
427 (3, 9, None, 'rc')
427 (3, 9, None, 'rc')
428
428
429 >>> v = '3.9-rc+2-02a8fea4289b'
429 >>> v = '3.9-rc+2-02a8fea4289b'
430 >>> versiontuple(v, 2)
430 >>> versiontuple(v, 2)
431 (3, 9)
431 (3, 9)
432 >>> versiontuple(v, 3)
432 >>> versiontuple(v, 3)
433 (3, 9, None)
433 (3, 9, None)
434 >>> versiontuple(v, 4)
434 >>> versiontuple(v, 4)
435 (3, 9, None, 'rc+2-02a8fea4289b')
435 (3, 9, None, 'rc+2-02a8fea4289b')
436 """
436 """
437 if not v:
437 if not v:
438 v = version()
438 v = version()
439 parts = remod.split('[\+-]', v, 1)
439 parts = remod.split('[\+-]', v, 1)
440 if len(parts) == 1:
440 if len(parts) == 1:
441 vparts, extra = parts[0], None
441 vparts, extra = parts[0], None
442 else:
442 else:
443 vparts, extra = parts
443 vparts, extra = parts
444
444
445 vints = []
445 vints = []
446 for i in vparts.split('.'):
446 for i in vparts.split('.'):
447 try:
447 try:
448 vints.append(int(i))
448 vints.append(int(i))
449 except ValueError:
449 except ValueError:
450 break
450 break
451 # (3, 6) -> (3, 6, None)
451 # (3, 6) -> (3, 6, None)
452 while len(vints) < 3:
452 while len(vints) < 3:
453 vints.append(None)
453 vints.append(None)
454
454
455 if n == 2:
455 if n == 2:
456 return (vints[0], vints[1])
456 return (vints[0], vints[1])
457 if n == 3:
457 if n == 3:
458 return (vints[0], vints[1], vints[2])
458 return (vints[0], vints[1], vints[2])
459 if n == 4:
459 if n == 4:
460 return (vints[0], vints[1], vints[2], extra)
460 return (vints[0], vints[1], vints[2], extra)
461
461
462 # used by parsedate
462 # used by parsedate
463 defaultdateformats = (
463 defaultdateformats = (
464 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
464 '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601
465 '%Y-%m-%dT%H:%M', # without seconds
465 '%Y-%m-%dT%H:%M', # without seconds
466 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
466 '%Y-%m-%dT%H%M%S', # another awful but legal variant without :
467 '%Y-%m-%dT%H%M', # without seconds
467 '%Y-%m-%dT%H%M', # without seconds
468 '%Y-%m-%d %H:%M:%S', # our common legal variant
468 '%Y-%m-%d %H:%M:%S', # our common legal variant
469 '%Y-%m-%d %H:%M', # without seconds
469 '%Y-%m-%d %H:%M', # without seconds
470 '%Y-%m-%d %H%M%S', # without :
470 '%Y-%m-%d %H%M%S', # without :
471 '%Y-%m-%d %H%M', # without seconds
471 '%Y-%m-%d %H%M', # without seconds
472 '%Y-%m-%d %I:%M:%S%p',
472 '%Y-%m-%d %I:%M:%S%p',
473 '%Y-%m-%d %H:%M',
473 '%Y-%m-%d %H:%M',
474 '%Y-%m-%d %I:%M%p',
474 '%Y-%m-%d %I:%M%p',
475 '%Y-%m-%d',
475 '%Y-%m-%d',
476 '%m-%d',
476 '%m-%d',
477 '%m/%d',
477 '%m/%d',
478 '%m/%d/%y',
478 '%m/%d/%y',
479 '%m/%d/%Y',
479 '%m/%d/%Y',
480 '%a %b %d %H:%M:%S %Y',
480 '%a %b %d %H:%M:%S %Y',
481 '%a %b %d %I:%M:%S%p %Y',
481 '%a %b %d %I:%M:%S%p %Y',
482 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
482 '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822"
483 '%b %d %H:%M:%S %Y',
483 '%b %d %H:%M:%S %Y',
484 '%b %d %I:%M:%S%p %Y',
484 '%b %d %I:%M:%S%p %Y',
485 '%b %d %H:%M:%S',
485 '%b %d %H:%M:%S',
486 '%b %d %I:%M:%S%p',
486 '%b %d %I:%M:%S%p',
487 '%b %d %H:%M',
487 '%b %d %H:%M',
488 '%b %d %I:%M%p',
488 '%b %d %I:%M%p',
489 '%b %d %Y',
489 '%b %d %Y',
490 '%b %d',
490 '%b %d',
491 '%H:%M:%S',
491 '%H:%M:%S',
492 '%I:%M:%S%p',
492 '%I:%M:%S%p',
493 '%H:%M',
493 '%H:%M',
494 '%I:%M%p',
494 '%I:%M%p',
495 )
495 )
496
496
497 extendeddateformats = defaultdateformats + (
497 extendeddateformats = defaultdateformats + (
498 "%Y",
498 "%Y",
499 "%Y-%m",
499 "%Y-%m",
500 "%b",
500 "%b",
501 "%b %Y",
501 "%b %Y",
502 )
502 )
503
503
504 def cachefunc(func):
504 def cachefunc(func):
505 '''cache the result of function calls'''
505 '''cache the result of function calls'''
506 # XXX doesn't handle keywords args
506 # XXX doesn't handle keywords args
507 if func.__code__.co_argcount == 0:
507 if func.__code__.co_argcount == 0:
508 cache = []
508 cache = []
509 def f():
509 def f():
510 if len(cache) == 0:
510 if len(cache) == 0:
511 cache.append(func())
511 cache.append(func())
512 return cache[0]
512 return cache[0]
513 return f
513 return f
514 cache = {}
514 cache = {}
515 if func.__code__.co_argcount == 1:
515 if func.__code__.co_argcount == 1:
516 # we gain a small amount of time because
516 # we gain a small amount of time because
517 # we don't need to pack/unpack the list
517 # we don't need to pack/unpack the list
518 def f(arg):
518 def f(arg):
519 if arg not in cache:
519 if arg not in cache:
520 cache[arg] = func(arg)
520 cache[arg] = func(arg)
521 return cache[arg]
521 return cache[arg]
522 else:
522 else:
523 def f(*args):
523 def f(*args):
524 if args not in cache:
524 if args not in cache:
525 cache[args] = func(*args)
525 cache[args] = func(*args)
526 return cache[args]
526 return cache[args]
527
527
528 return f
528 return f
529
529
530 class sortdict(dict):
530 class sortdict(dict):
531 '''a simple sorted dictionary'''
531 '''a simple sorted dictionary'''
532 def __init__(self, data=None):
532 def __init__(self, data=None):
533 self._list = []
533 self._list = []
534 if data:
534 if data:
535 self.update(data)
535 self.update(data)
536 def copy(self):
536 def copy(self):
537 return sortdict(self)
537 return sortdict(self)
538 def __setitem__(self, key, val):
538 def __setitem__(self, key, val):
539 if key in self:
539 if key in self:
540 self._list.remove(key)
540 self._list.remove(key)
541 self._list.append(key)
541 self._list.append(key)
542 dict.__setitem__(self, key, val)
542 dict.__setitem__(self, key, val)
543 def __iter__(self):
543 def __iter__(self):
544 return self._list.__iter__()
544 return self._list.__iter__()
545 def update(self, src):
545 def update(self, src):
546 if isinstance(src, dict):
546 if isinstance(src, dict):
547 src = src.iteritems()
547 src = src.iteritems()
548 for k, v in src:
548 for k, v in src:
549 self[k] = v
549 self[k] = v
550 def clear(self):
550 def clear(self):
551 dict.clear(self)
551 dict.clear(self)
552 self._list = []
552 self._list = []
553 def items(self):
553 def items(self):
554 return [(k, self[k]) for k in self._list]
554 return [(k, self[k]) for k in self._list]
555 def __delitem__(self, key):
555 def __delitem__(self, key):
556 dict.__delitem__(self, key)
556 dict.__delitem__(self, key)
557 self._list.remove(key)
557 self._list.remove(key)
558 def pop(self, key, *args, **kwargs):
558 def pop(self, key, *args, **kwargs):
559 dict.pop(self, key, *args, **kwargs)
559 dict.pop(self, key, *args, **kwargs)
560 try:
560 try:
561 self._list.remove(key)
561 self._list.remove(key)
562 except ValueError:
562 except ValueError:
563 pass
563 pass
564 def keys(self):
564 def keys(self):
565 return self._list[:]
565 return self._list[:]
566 def iterkeys(self):
566 def iterkeys(self):
567 return self._list.__iter__()
567 return self._list.__iter__()
568 def iteritems(self):
568 def iteritems(self):
569 for k in self._list:
569 for k in self._list:
570 yield k, self[k]
570 yield k, self[k]
571 def insert(self, index, key, val):
571 def insert(self, index, key, val):
572 self._list.insert(index, key)
572 self._list.insert(index, key)
573 dict.__setitem__(self, key, val)
573 dict.__setitem__(self, key, val)
574 def __repr__(self):
574 def __repr__(self):
575 if not self:
575 if not self:
576 return '%s()' % self.__class__.__name__
576 return '%s()' % self.__class__.__name__
577 return '%s(%r)' % (self.__class__.__name__, self.items())
577 return '%s(%r)' % (self.__class__.__name__, self.items())
578
578
579 class _lrucachenode(object):
579 class _lrucachenode(object):
580 """A node in a doubly linked list.
580 """A node in a doubly linked list.
581
581
582 Holds a reference to nodes on either side as well as a key-value
582 Holds a reference to nodes on either side as well as a key-value
583 pair for the dictionary entry.
583 pair for the dictionary entry.
584 """
584 """
585 __slots__ = (u'next', u'prev', u'key', u'value')
585 __slots__ = (u'next', u'prev', u'key', u'value')
586
586
587 def __init__(self):
587 def __init__(self):
588 self.next = None
588 self.next = None
589 self.prev = None
589 self.prev = None
590
590
591 self.key = _notset
591 self.key = _notset
592 self.value = None
592 self.value = None
593
593
594 def markempty(self):
594 def markempty(self):
595 """Mark the node as emptied."""
595 """Mark the node as emptied."""
596 self.key = _notset
596 self.key = _notset
597
597
598 class lrucachedict(object):
598 class lrucachedict(object):
599 """Dict that caches most recent accesses and sets.
599 """Dict that caches most recent accesses and sets.
600
600
601 The dict consists of an actual backing dict - indexed by original
601 The dict consists of an actual backing dict - indexed by original
602 key - and a doubly linked circular list defining the order of entries in
602 key - and a doubly linked circular list defining the order of entries in
603 the cache.
603 the cache.
604
604
605 The head node is the newest entry in the cache. If the cache is full,
605 The head node is the newest entry in the cache. If the cache is full,
606 we recycle head.prev and make it the new head. Cache accesses result in
606 we recycle head.prev and make it the new head. Cache accesses result in
607 the node being moved to before the existing head and being marked as the
607 the node being moved to before the existing head and being marked as the
608 new head node.
608 new head node.
609 """
609 """
610 def __init__(self, max):
610 def __init__(self, max):
611 self._cache = {}
611 self._cache = {}
612
612
613 self._head = head = _lrucachenode()
613 self._head = head = _lrucachenode()
614 head.prev = head
614 head.prev = head
615 head.next = head
615 head.next = head
616 self._size = 1
616 self._size = 1
617 self._capacity = max
617 self._capacity = max
618
618
619 def __len__(self):
619 def __len__(self):
620 return len(self._cache)
620 return len(self._cache)
621
621
622 def __contains__(self, k):
622 def __contains__(self, k):
623 return k in self._cache
623 return k in self._cache
624
624
625 def __iter__(self):
625 def __iter__(self):
626 # We don't have to iterate in cache order, but why not.
626 # We don't have to iterate in cache order, but why not.
627 n = self._head
627 n = self._head
628 for i in range(len(self._cache)):
628 for i in range(len(self._cache)):
629 yield n.key
629 yield n.key
630 n = n.next
630 n = n.next
631
631
632 def __getitem__(self, k):
632 def __getitem__(self, k):
633 node = self._cache[k]
633 node = self._cache[k]
634 self._movetohead(node)
634 self._movetohead(node)
635 return node.value
635 return node.value
636
636
637 def __setitem__(self, k, v):
637 def __setitem__(self, k, v):
638 node = self._cache.get(k)
638 node = self._cache.get(k)
639 # Replace existing value and mark as newest.
639 # Replace existing value and mark as newest.
640 if node is not None:
640 if node is not None:
641 node.value = v
641 node.value = v
642 self._movetohead(node)
642 self._movetohead(node)
643 return
643 return
644
644
645 if self._size < self._capacity:
645 if self._size < self._capacity:
646 node = self._addcapacity()
646 node = self._addcapacity()
647 else:
647 else:
648 # Grab the last/oldest item.
648 # Grab the last/oldest item.
649 node = self._head.prev
649 node = self._head.prev
650
650
651 # At capacity. Kill the old entry.
651 # At capacity. Kill the old entry.
652 if node.key is not _notset:
652 if node.key is not _notset:
653 del self._cache[node.key]
653 del self._cache[node.key]
654
654
655 node.key = k
655 node.key = k
656 node.value = v
656 node.value = v
657 self._cache[k] = node
657 self._cache[k] = node
658 # And mark it as newest entry. No need to adjust order since it
658 # And mark it as newest entry. No need to adjust order since it
659 # is already self._head.prev.
659 # is already self._head.prev.
660 self._head = node
660 self._head = node
661
661
662 def __delitem__(self, k):
662 def __delitem__(self, k):
663 node = self._cache.pop(k)
663 node = self._cache.pop(k)
664 node.markempty()
664 node.markempty()
665
665
666 # Temporarily mark as newest item before re-adjusting head to make
666 # Temporarily mark as newest item before re-adjusting head to make
667 # this node the oldest item.
667 # this node the oldest item.
668 self._movetohead(node)
668 self._movetohead(node)
669 self._head = node.next
669 self._head = node.next
670
670
671 # Additional dict methods.
671 # Additional dict methods.
672
672
673 def get(self, k, default=None):
673 def get(self, k, default=None):
674 try:
674 try:
675 return self._cache[k].value
675 return self._cache[k].value
676 except KeyError:
676 except KeyError:
677 return default
677 return default
678
678
679 def clear(self):
679 def clear(self):
680 n = self._head
680 n = self._head
681 while n.key is not _notset:
681 while n.key is not _notset:
682 n.markempty()
682 n.markempty()
683 n = n.next
683 n = n.next
684
684
685 self._cache.clear()
685 self._cache.clear()
686
686
687 def copy(self):
687 def copy(self):
688 result = lrucachedict(self._capacity)
688 result = lrucachedict(self._capacity)
689 n = self._head.prev
689 n = self._head.prev
690 # Iterate in oldest-to-newest order, so the copy has the right ordering
690 # Iterate in oldest-to-newest order, so the copy has the right ordering
691 for i in range(len(self._cache)):
691 for i in range(len(self._cache)):
692 result[n.key] = n.value
692 result[n.key] = n.value
693 n = n.prev
693 n = n.prev
694 return result
694 return result
695
695
696 def _movetohead(self, node):
696 def _movetohead(self, node):
697 """Mark a node as the newest, making it the new head.
697 """Mark a node as the newest, making it the new head.
698
698
699 When a node is accessed, it becomes the freshest entry in the LRU
699 When a node is accessed, it becomes the freshest entry in the LRU
700 list, which is denoted by self._head.
700 list, which is denoted by self._head.
701
701
702 Visually, let's make ``N`` the new head node (* denotes head):
702 Visually, let's make ``N`` the new head node (* denotes head):
703
703
704 previous/oldest <-> head <-> next/next newest
704 previous/oldest <-> head <-> next/next newest
705
705
706 ----<->--- A* ---<->-----
706 ----<->--- A* ---<->-----
707 | |
707 | |
708 E <-> D <-> N <-> C <-> B
708 E <-> D <-> N <-> C <-> B
709
709
710 To:
710 To:
711
711
712 ----<->--- N* ---<->-----
712 ----<->--- N* ---<->-----
713 | |
713 | |
714 E <-> D <-> C <-> B <-> A
714 E <-> D <-> C <-> B <-> A
715
715
716 This requires the following moves:
716 This requires the following moves:
717
717
718 C.next = D (node.prev.next = node.next)
718 C.next = D (node.prev.next = node.next)
719 D.prev = C (node.next.prev = node.prev)
719 D.prev = C (node.next.prev = node.prev)
720 E.next = N (head.prev.next = node)
720 E.next = N (head.prev.next = node)
721 N.prev = E (node.prev = head.prev)
721 N.prev = E (node.prev = head.prev)
722 N.next = A (node.next = head)
722 N.next = A (node.next = head)
723 A.prev = N (head.prev = node)
723 A.prev = N (head.prev = node)
724 """
724 """
725 head = self._head
725 head = self._head
726 # C.next = D
726 # C.next = D
727 node.prev.next = node.next
727 node.prev.next = node.next
728 # D.prev = C
728 # D.prev = C
729 node.next.prev = node.prev
729 node.next.prev = node.prev
730 # N.prev = E
730 # N.prev = E
731 node.prev = head.prev
731 node.prev = head.prev
732 # N.next = A
732 # N.next = A
733 # It is tempting to do just "head" here, however if node is
733 # It is tempting to do just "head" here, however if node is
734 # adjacent to head, this will do bad things.
734 # adjacent to head, this will do bad things.
735 node.next = head.prev.next
735 node.next = head.prev.next
736 # E.next = N
736 # E.next = N
737 node.next.prev = node
737 node.next.prev = node
738 # A.prev = N
738 # A.prev = N
739 node.prev.next = node
739 node.prev.next = node
740
740
741 self._head = node
741 self._head = node
742
742
743 def _addcapacity(self):
743 def _addcapacity(self):
744 """Add a node to the circular linked list.
744 """Add a node to the circular linked list.
745
745
746 The new node is inserted before the head node.
746 The new node is inserted before the head node.
747 """
747 """
748 head = self._head
748 head = self._head
749 node = _lrucachenode()
749 node = _lrucachenode()
750 head.prev.next = node
750 head.prev.next = node
751 node.prev = head.prev
751 node.prev = head.prev
752 node.next = head
752 node.next = head
753 head.prev = node
753 head.prev = node
754 self._size += 1
754 self._size += 1
755 return node
755 return node
756
756
757 def lrucachefunc(func):
757 def lrucachefunc(func):
758 '''cache most recent results of function calls'''
758 '''cache most recent results of function calls'''
759 cache = {}
759 cache = {}
760 order = collections.deque()
760 order = collections.deque()
761 if func.__code__.co_argcount == 1:
761 if func.__code__.co_argcount == 1:
762 def f(arg):
762 def f(arg):
763 if arg not in cache:
763 if arg not in cache:
764 if len(cache) > 20:
764 if len(cache) > 20:
765 del cache[order.popleft()]
765 del cache[order.popleft()]
766 cache[arg] = func(arg)
766 cache[arg] = func(arg)
767 else:
767 else:
768 order.remove(arg)
768 order.remove(arg)
769 order.append(arg)
769 order.append(arg)
770 return cache[arg]
770 return cache[arg]
771 else:
771 else:
772 def f(*args):
772 def f(*args):
773 if args not in cache:
773 if args not in cache:
774 if len(cache) > 20:
774 if len(cache) > 20:
775 del cache[order.popleft()]
775 del cache[order.popleft()]
776 cache[args] = func(*args)
776 cache[args] = func(*args)
777 else:
777 else:
778 order.remove(args)
778 order.remove(args)
779 order.append(args)
779 order.append(args)
780 return cache[args]
780 return cache[args]
781
781
782 return f
782 return f
783
783
784 class propertycache(object):
784 class propertycache(object):
785 def __init__(self, func):
785 def __init__(self, func):
786 self.func = func
786 self.func = func
787 self.name = func.__name__
787 self.name = func.__name__
788 def __get__(self, obj, type=None):
788 def __get__(self, obj, type=None):
789 result = self.func(obj)
789 result = self.func(obj)
790 self.cachevalue(obj, result)
790 self.cachevalue(obj, result)
791 return result
791 return result
792
792
793 def cachevalue(self, obj, value):
793 def cachevalue(self, obj, value):
794 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
794 # __dict__ assignment required to bypass __setattr__ (eg: repoview)
795 obj.__dict__[self.name] = value
795 obj.__dict__[self.name] = value
796
796
797 def pipefilter(s, cmd):
797 def pipefilter(s, cmd):
798 '''filter string S through command CMD, returning its output'''
798 '''filter string S through command CMD, returning its output'''
799 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
799 p = subprocess.Popen(cmd, shell=True, close_fds=closefds,
800 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
800 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
801 pout, perr = p.communicate(s)
801 pout, perr = p.communicate(s)
802 return pout
802 return pout
803
803
804 def tempfilter(s, cmd):
804 def tempfilter(s, cmd):
805 '''filter string S through a pair of temporary files with CMD.
805 '''filter string S through a pair of temporary files with CMD.
806 CMD is used as a template to create the real command to be run,
806 CMD is used as a template to create the real command to be run,
807 with the strings INFILE and OUTFILE replaced by the real names of
807 with the strings INFILE and OUTFILE replaced by the real names of
808 the temporary files generated.'''
808 the temporary files generated.'''
809 inname, outname = None, None
809 inname, outname = None, None
810 try:
810 try:
811 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
811 infd, inname = tempfile.mkstemp(prefix='hg-filter-in-')
812 fp = os.fdopen(infd, 'wb')
812 fp = os.fdopen(infd, pycompat.sysstr('wb'))
813 fp.write(s)
813 fp.write(s)
814 fp.close()
814 fp.close()
815 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
815 outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-')
816 os.close(outfd)
816 os.close(outfd)
817 cmd = cmd.replace('INFILE', inname)
817 cmd = cmd.replace('INFILE', inname)
818 cmd = cmd.replace('OUTFILE', outname)
818 cmd = cmd.replace('OUTFILE', outname)
819 code = os.system(cmd)
819 code = os.system(cmd)
820 if pycompat.sysplatform == 'OpenVMS' and code & 1:
820 if pycompat.sysplatform == 'OpenVMS' and code & 1:
821 code = 0
821 code = 0
822 if code:
822 if code:
823 raise Abort(_("command '%s' failed: %s") %
823 raise Abort(_("command '%s' failed: %s") %
824 (cmd, explainexit(code)))
824 (cmd, explainexit(code)))
825 return readfile(outname)
825 return readfile(outname)
826 finally:
826 finally:
827 try:
827 try:
828 if inname:
828 if inname:
829 os.unlink(inname)
829 os.unlink(inname)
830 except OSError:
830 except OSError:
831 pass
831 pass
832 try:
832 try:
833 if outname:
833 if outname:
834 os.unlink(outname)
834 os.unlink(outname)
835 except OSError:
835 except OSError:
836 pass
836 pass
837
837
838 filtertable = {
838 filtertable = {
839 'tempfile:': tempfilter,
839 'tempfile:': tempfilter,
840 'pipe:': pipefilter,
840 'pipe:': pipefilter,
841 }
841 }
842
842
843 def filter(s, cmd):
843 def filter(s, cmd):
844 "filter a string through a command that transforms its input to its output"
844 "filter a string through a command that transforms its input to its output"
845 for name, fn in filtertable.iteritems():
845 for name, fn in filtertable.iteritems():
846 if cmd.startswith(name):
846 if cmd.startswith(name):
847 return fn(s, cmd[len(name):].lstrip())
847 return fn(s, cmd[len(name):].lstrip())
848 return pipefilter(s, cmd)
848 return pipefilter(s, cmd)
849
849
850 def binary(s):
850 def binary(s):
851 """return true if a string is binary data"""
851 """return true if a string is binary data"""
852 return bool(s and '\0' in s)
852 return bool(s and '\0' in s)
853
853
854 def increasingchunks(source, min=1024, max=65536):
854 def increasingchunks(source, min=1024, max=65536):
855 '''return no less than min bytes per chunk while data remains,
855 '''return no less than min bytes per chunk while data remains,
856 doubling min after each chunk until it reaches max'''
856 doubling min after each chunk until it reaches max'''
857 def log2(x):
857 def log2(x):
858 if not x:
858 if not x:
859 return 0
859 return 0
860 i = 0
860 i = 0
861 while x:
861 while x:
862 x >>= 1
862 x >>= 1
863 i += 1
863 i += 1
864 return i - 1
864 return i - 1
865
865
866 buf = []
866 buf = []
867 blen = 0
867 blen = 0
868 for chunk in source:
868 for chunk in source:
869 buf.append(chunk)
869 buf.append(chunk)
870 blen += len(chunk)
870 blen += len(chunk)
871 if blen >= min:
871 if blen >= min:
872 if min < max:
872 if min < max:
873 min = min << 1
873 min = min << 1
874 nmin = 1 << log2(blen)
874 nmin = 1 << log2(blen)
875 if nmin > min:
875 if nmin > min:
876 min = nmin
876 min = nmin
877 if min > max:
877 if min > max:
878 min = max
878 min = max
879 yield ''.join(buf)
879 yield ''.join(buf)
880 blen = 0
880 blen = 0
881 buf = []
881 buf = []
882 if buf:
882 if buf:
883 yield ''.join(buf)
883 yield ''.join(buf)
884
884
885 Abort = error.Abort
885 Abort = error.Abort
886
886
887 def always(fn):
887 def always(fn):
888 return True
888 return True
889
889
890 def never(fn):
890 def never(fn):
891 return False
891 return False
892
892
893 def nogc(func):
893 def nogc(func):
894 """disable garbage collector
894 """disable garbage collector
895
895
896 Python's garbage collector triggers a GC each time a certain number of
896 Python's garbage collector triggers a GC each time a certain number of
897 container objects (the number being defined by gc.get_threshold()) are
897 container objects (the number being defined by gc.get_threshold()) are
898 allocated even when marked not to be tracked by the collector. Tracking has
898 allocated even when marked not to be tracked by the collector. Tracking has
899 no effect on when GCs are triggered, only on what objects the GC looks
899 no effect on when GCs are triggered, only on what objects the GC looks
900 into. As a workaround, disable GC while building complex (huge)
900 into. As a workaround, disable GC while building complex (huge)
901 containers.
901 containers.
902
902
903 This garbage collector issue have been fixed in 2.7.
903 This garbage collector issue have been fixed in 2.7.
904 """
904 """
905 if sys.version_info >= (2, 7):
905 if sys.version_info >= (2, 7):
906 return func
906 return func
907 def wrapper(*args, **kwargs):
907 def wrapper(*args, **kwargs):
908 gcenabled = gc.isenabled()
908 gcenabled = gc.isenabled()
909 gc.disable()
909 gc.disable()
910 try:
910 try:
911 return func(*args, **kwargs)
911 return func(*args, **kwargs)
912 finally:
912 finally:
913 if gcenabled:
913 if gcenabled:
914 gc.enable()
914 gc.enable()
915 return wrapper
915 return wrapper
916
916
917 def pathto(root, n1, n2):
917 def pathto(root, n1, n2):
918 '''return the relative path from one place to another.
918 '''return the relative path from one place to another.
919 root should use os.sep to separate directories
919 root should use os.sep to separate directories
920 n1 should use os.sep to separate directories
920 n1 should use os.sep to separate directories
921 n2 should use "/" to separate directories
921 n2 should use "/" to separate directories
922 returns an os.sep-separated path.
922 returns an os.sep-separated path.
923
923
924 If n1 is a relative path, it's assumed it's
924 If n1 is a relative path, it's assumed it's
925 relative to root.
925 relative to root.
926 n2 should always be relative to root.
926 n2 should always be relative to root.
927 '''
927 '''
928 if not n1:
928 if not n1:
929 return localpath(n2)
929 return localpath(n2)
930 if os.path.isabs(n1):
930 if os.path.isabs(n1):
931 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
931 if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]:
932 return os.path.join(root, localpath(n2))
932 return os.path.join(root, localpath(n2))
933 n2 = '/'.join((pconvert(root), n2))
933 n2 = '/'.join((pconvert(root), n2))
934 a, b = splitpath(n1), n2.split('/')
934 a, b = splitpath(n1), n2.split('/')
935 a.reverse()
935 a.reverse()
936 b.reverse()
936 b.reverse()
937 while a and b and a[-1] == b[-1]:
937 while a and b and a[-1] == b[-1]:
938 a.pop()
938 a.pop()
939 b.pop()
939 b.pop()
940 b.reverse()
940 b.reverse()
941 return pycompat.ossep.join((['..'] * len(a)) + b) or '.'
941 return pycompat.ossep.join((['..'] * len(a)) + b) or '.'
942
942
943 def mainfrozen():
943 def mainfrozen():
944 """return True if we are a frozen executable.
944 """return True if we are a frozen executable.
945
945
946 The code supports py2exe (most common, Windows only) and tools/freeze
946 The code supports py2exe (most common, Windows only) and tools/freeze
947 (portable, not much used).
947 (portable, not much used).
948 """
948 """
949 return (safehasattr(sys, "frozen") or # new py2exe
949 return (safehasattr(sys, "frozen") or # new py2exe
950 safehasattr(sys, "importers") or # old py2exe
950 safehasattr(sys, "importers") or # old py2exe
951 imp.is_frozen(u"__main__")) # tools/freeze
951 imp.is_frozen(u"__main__")) # tools/freeze
952
952
953 # the location of data files matching the source code
953 # the location of data files matching the source code
954 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
954 if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app':
955 # executable version (py2exe) doesn't support __file__
955 # executable version (py2exe) doesn't support __file__
956 datapath = os.path.dirname(pycompat.sysexecutable)
956 datapath = os.path.dirname(pycompat.sysexecutable)
957 else:
957 else:
958 datapath = os.path.dirname(__file__)
958 datapath = os.path.dirname(__file__)
959
959
960 if not isinstance(datapath, bytes):
960 if not isinstance(datapath, bytes):
961 datapath = pycompat.fsencode(datapath)
961 datapath = pycompat.fsencode(datapath)
962
962
963 i18n.setdatapath(datapath)
963 i18n.setdatapath(datapath)
964
964
965 _hgexecutable = None
965 _hgexecutable = None
966
966
967 def hgexecutable():
967 def hgexecutable():
968 """return location of the 'hg' executable.
968 """return location of the 'hg' executable.
969
969
970 Defaults to $HG or 'hg' in the search path.
970 Defaults to $HG or 'hg' in the search path.
971 """
971 """
972 if _hgexecutable is None:
972 if _hgexecutable is None:
973 hg = encoding.environ.get('HG')
973 hg = encoding.environ.get('HG')
974 mainmod = sys.modules['__main__']
974 mainmod = sys.modules['__main__']
975 if hg:
975 if hg:
976 _sethgexecutable(hg)
976 _sethgexecutable(hg)
977 elif mainfrozen():
977 elif mainfrozen():
978 if getattr(sys, 'frozen', None) == 'macosx_app':
978 if getattr(sys, 'frozen', None) == 'macosx_app':
979 # Env variable set by py2app
979 # Env variable set by py2app
980 _sethgexecutable(encoding.environ['EXECUTABLEPATH'])
980 _sethgexecutable(encoding.environ['EXECUTABLEPATH'])
981 else:
981 else:
982 _sethgexecutable(pycompat.sysexecutable)
982 _sethgexecutable(pycompat.sysexecutable)
983 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
983 elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg':
984 _sethgexecutable(mainmod.__file__)
984 _sethgexecutable(mainmod.__file__)
985 else:
985 else:
986 exe = findexe('hg') or os.path.basename(sys.argv[0])
986 exe = findexe('hg') or os.path.basename(sys.argv[0])
987 _sethgexecutable(exe)
987 _sethgexecutable(exe)
988 return _hgexecutable
988 return _hgexecutable
989
989
990 def _sethgexecutable(path):
990 def _sethgexecutable(path):
991 """set location of the 'hg' executable"""
991 """set location of the 'hg' executable"""
992 global _hgexecutable
992 global _hgexecutable
993 _hgexecutable = path
993 _hgexecutable = path
994
994
995 def _isstdout(f):
995 def _isstdout(f):
996 fileno = getattr(f, 'fileno', None)
996 fileno = getattr(f, 'fileno', None)
997 return fileno and fileno() == sys.__stdout__.fileno()
997 return fileno and fileno() == sys.__stdout__.fileno()
998
998
999 def shellenviron(environ=None):
999 def shellenviron(environ=None):
1000 """return environ with optional override, useful for shelling out"""
1000 """return environ with optional override, useful for shelling out"""
1001 def py2shell(val):
1001 def py2shell(val):
1002 'convert python object into string that is useful to shell'
1002 'convert python object into string that is useful to shell'
1003 if val is None or val is False:
1003 if val is None or val is False:
1004 return '0'
1004 return '0'
1005 if val is True:
1005 if val is True:
1006 return '1'
1006 return '1'
1007 return str(val)
1007 return str(val)
1008 env = dict(encoding.environ)
1008 env = dict(encoding.environ)
1009 if environ:
1009 if environ:
1010 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1010 env.update((k, py2shell(v)) for k, v in environ.iteritems())
1011 env['HG'] = hgexecutable()
1011 env['HG'] = hgexecutable()
1012 return env
1012 return env
1013
1013
1014 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
1014 def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None):
1015 '''enhanced shell command execution.
1015 '''enhanced shell command execution.
1016 run with environment maybe modified, maybe in different dir.
1016 run with environment maybe modified, maybe in different dir.
1017
1017
1018 if command fails and onerr is None, return status, else raise onerr
1018 if command fails and onerr is None, return status, else raise onerr
1019 object as exception.
1019 object as exception.
1020
1020
1021 if out is specified, it is assumed to be a file-like object that has a
1021 if out is specified, it is assumed to be a file-like object that has a
1022 write() method. stdout and stderr will be redirected to out.'''
1022 write() method. stdout and stderr will be redirected to out.'''
1023 try:
1023 try:
1024 stdout.flush()
1024 stdout.flush()
1025 except Exception:
1025 except Exception:
1026 pass
1026 pass
1027 origcmd = cmd
1027 origcmd = cmd
1028 cmd = quotecommand(cmd)
1028 cmd = quotecommand(cmd)
1029 if pycompat.sysplatform == 'plan9' and (sys.version_info[0] == 2
1029 if pycompat.sysplatform == 'plan9' and (sys.version_info[0] == 2
1030 and sys.version_info[1] < 7):
1030 and sys.version_info[1] < 7):
1031 # subprocess kludge to work around issues in half-baked Python
1031 # subprocess kludge to work around issues in half-baked Python
1032 # ports, notably bichued/python:
1032 # ports, notably bichued/python:
1033 if not cwd is None:
1033 if not cwd is None:
1034 os.chdir(cwd)
1034 os.chdir(cwd)
1035 rc = os.system(cmd)
1035 rc = os.system(cmd)
1036 else:
1036 else:
1037 env = shellenviron(environ)
1037 env = shellenviron(environ)
1038 if out is None or _isstdout(out):
1038 if out is None or _isstdout(out):
1039 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1039 rc = subprocess.call(cmd, shell=True, close_fds=closefds,
1040 env=env, cwd=cwd)
1040 env=env, cwd=cwd)
1041 else:
1041 else:
1042 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1042 proc = subprocess.Popen(cmd, shell=True, close_fds=closefds,
1043 env=env, cwd=cwd, stdout=subprocess.PIPE,
1043 env=env, cwd=cwd, stdout=subprocess.PIPE,
1044 stderr=subprocess.STDOUT)
1044 stderr=subprocess.STDOUT)
1045 for line in iter(proc.stdout.readline, ''):
1045 for line in iter(proc.stdout.readline, ''):
1046 out.write(line)
1046 out.write(line)
1047 proc.wait()
1047 proc.wait()
1048 rc = proc.returncode
1048 rc = proc.returncode
1049 if pycompat.sysplatform == 'OpenVMS' and rc & 1:
1049 if pycompat.sysplatform == 'OpenVMS' and rc & 1:
1050 rc = 0
1050 rc = 0
1051 if rc and onerr:
1051 if rc and onerr:
1052 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1052 errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]),
1053 explainexit(rc)[0])
1053 explainexit(rc)[0])
1054 if errprefix:
1054 if errprefix:
1055 errmsg = '%s: %s' % (errprefix, errmsg)
1055 errmsg = '%s: %s' % (errprefix, errmsg)
1056 raise onerr(errmsg)
1056 raise onerr(errmsg)
1057 return rc
1057 return rc
1058
1058
1059 def checksignature(func):
1059 def checksignature(func):
1060 '''wrap a function with code to check for calling errors'''
1060 '''wrap a function with code to check for calling errors'''
1061 def check(*args, **kwargs):
1061 def check(*args, **kwargs):
1062 try:
1062 try:
1063 return func(*args, **kwargs)
1063 return func(*args, **kwargs)
1064 except TypeError:
1064 except TypeError:
1065 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1065 if len(traceback.extract_tb(sys.exc_info()[2])) == 1:
1066 raise error.SignatureError
1066 raise error.SignatureError
1067 raise
1067 raise
1068
1068
1069 return check
1069 return check
1070
1070
1071 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1071 def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False):
1072 '''copy a file, preserving mode and optionally other stat info like
1072 '''copy a file, preserving mode and optionally other stat info like
1073 atime/mtime
1073 atime/mtime
1074
1074
1075 checkambig argument is used with filestat, and is useful only if
1075 checkambig argument is used with filestat, and is useful only if
1076 destination file is guarded by any lock (e.g. repo.lock or
1076 destination file is guarded by any lock (e.g. repo.lock or
1077 repo.wlock).
1077 repo.wlock).
1078
1078
1079 copystat and checkambig should be exclusive.
1079 copystat and checkambig should be exclusive.
1080 '''
1080 '''
1081 assert not (copystat and checkambig)
1081 assert not (copystat and checkambig)
1082 oldstat = None
1082 oldstat = None
1083 if os.path.lexists(dest):
1083 if os.path.lexists(dest):
1084 if checkambig:
1084 if checkambig:
1085 oldstat = checkambig and filestat(dest)
1085 oldstat = checkambig and filestat(dest)
1086 unlink(dest)
1086 unlink(dest)
1087 # hardlinks are problematic on CIFS, quietly ignore this flag
1087 # hardlinks are problematic on CIFS, quietly ignore this flag
1088 # until we find a way to work around it cleanly (issue4546)
1088 # until we find a way to work around it cleanly (issue4546)
1089 if False and hardlink:
1089 if False and hardlink:
1090 try:
1090 try:
1091 oslink(src, dest)
1091 oslink(src, dest)
1092 return
1092 return
1093 except (IOError, OSError):
1093 except (IOError, OSError):
1094 pass # fall back to normal copy
1094 pass # fall back to normal copy
1095 if os.path.islink(src):
1095 if os.path.islink(src):
1096 os.symlink(os.readlink(src), dest)
1096 os.symlink(os.readlink(src), dest)
1097 # copytime is ignored for symlinks, but in general copytime isn't needed
1097 # copytime is ignored for symlinks, but in general copytime isn't needed
1098 # for them anyway
1098 # for them anyway
1099 else:
1099 else:
1100 try:
1100 try:
1101 shutil.copyfile(src, dest)
1101 shutil.copyfile(src, dest)
1102 if copystat:
1102 if copystat:
1103 # copystat also copies mode
1103 # copystat also copies mode
1104 shutil.copystat(src, dest)
1104 shutil.copystat(src, dest)
1105 else:
1105 else:
1106 shutil.copymode(src, dest)
1106 shutil.copymode(src, dest)
1107 if oldstat and oldstat.stat:
1107 if oldstat and oldstat.stat:
1108 newstat = filestat(dest)
1108 newstat = filestat(dest)
1109 if newstat.isambig(oldstat):
1109 if newstat.isambig(oldstat):
1110 # stat of copied file is ambiguous to original one
1110 # stat of copied file is ambiguous to original one
1111 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1111 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1112 os.utime(dest, (advanced, advanced))
1112 os.utime(dest, (advanced, advanced))
1113 except shutil.Error as inst:
1113 except shutil.Error as inst:
1114 raise Abort(str(inst))
1114 raise Abort(str(inst))
1115
1115
1116 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1116 def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None):
1117 """Copy a directory tree using hardlinks if possible."""
1117 """Copy a directory tree using hardlinks if possible."""
1118 num = 0
1118 num = 0
1119
1119
1120 if hardlink is None:
1120 if hardlink is None:
1121 hardlink = (os.stat(src).st_dev ==
1121 hardlink = (os.stat(src).st_dev ==
1122 os.stat(os.path.dirname(dst)).st_dev)
1122 os.stat(os.path.dirname(dst)).st_dev)
1123 if hardlink:
1123 if hardlink:
1124 topic = _('linking')
1124 topic = _('linking')
1125 else:
1125 else:
1126 topic = _('copying')
1126 topic = _('copying')
1127
1127
1128 if os.path.isdir(src):
1128 if os.path.isdir(src):
1129 os.mkdir(dst)
1129 os.mkdir(dst)
1130 for name, kind in osutil.listdir(src):
1130 for name, kind in osutil.listdir(src):
1131 srcname = os.path.join(src, name)
1131 srcname = os.path.join(src, name)
1132 dstname = os.path.join(dst, name)
1132 dstname = os.path.join(dst, name)
1133 def nprog(t, pos):
1133 def nprog(t, pos):
1134 if pos is not None:
1134 if pos is not None:
1135 return progress(t, pos + num)
1135 return progress(t, pos + num)
1136 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1136 hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog)
1137 num += n
1137 num += n
1138 else:
1138 else:
1139 if hardlink:
1139 if hardlink:
1140 try:
1140 try:
1141 oslink(src, dst)
1141 oslink(src, dst)
1142 except (IOError, OSError):
1142 except (IOError, OSError):
1143 hardlink = False
1143 hardlink = False
1144 shutil.copy(src, dst)
1144 shutil.copy(src, dst)
1145 else:
1145 else:
1146 shutil.copy(src, dst)
1146 shutil.copy(src, dst)
1147 num += 1
1147 num += 1
1148 progress(topic, num)
1148 progress(topic, num)
1149 progress(topic, None)
1149 progress(topic, None)
1150
1150
1151 return hardlink, num
1151 return hardlink, num
1152
1152
1153 _winreservednames = '''con prn aux nul
1153 _winreservednames = '''con prn aux nul
1154 com1 com2 com3 com4 com5 com6 com7 com8 com9
1154 com1 com2 com3 com4 com5 com6 com7 com8 com9
1155 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1155 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split()
1156 _winreservedchars = ':*?"<>|'
1156 _winreservedchars = ':*?"<>|'
1157 def checkwinfilename(path):
1157 def checkwinfilename(path):
1158 r'''Check that the base-relative path is a valid filename on Windows.
1158 r'''Check that the base-relative path is a valid filename on Windows.
1159 Returns None if the path is ok, or a UI string describing the problem.
1159 Returns None if the path is ok, or a UI string describing the problem.
1160
1160
1161 >>> checkwinfilename("just/a/normal/path")
1161 >>> checkwinfilename("just/a/normal/path")
1162 >>> checkwinfilename("foo/bar/con.xml")
1162 >>> checkwinfilename("foo/bar/con.xml")
1163 "filename contains 'con', which is reserved on Windows"
1163 "filename contains 'con', which is reserved on Windows"
1164 >>> checkwinfilename("foo/con.xml/bar")
1164 >>> checkwinfilename("foo/con.xml/bar")
1165 "filename contains 'con', which is reserved on Windows"
1165 "filename contains 'con', which is reserved on Windows"
1166 >>> checkwinfilename("foo/bar/xml.con")
1166 >>> checkwinfilename("foo/bar/xml.con")
1167 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1167 >>> checkwinfilename("foo/bar/AUX/bla.txt")
1168 "filename contains 'AUX', which is reserved on Windows"
1168 "filename contains 'AUX', which is reserved on Windows"
1169 >>> checkwinfilename("foo/bar/bla:.txt")
1169 >>> checkwinfilename("foo/bar/bla:.txt")
1170 "filename contains ':', which is reserved on Windows"
1170 "filename contains ':', which is reserved on Windows"
1171 >>> checkwinfilename("foo/bar/b\07la.txt")
1171 >>> checkwinfilename("foo/bar/b\07la.txt")
1172 "filename contains '\\x07', which is invalid on Windows"
1172 "filename contains '\\x07', which is invalid on Windows"
1173 >>> checkwinfilename("foo/bar/bla ")
1173 >>> checkwinfilename("foo/bar/bla ")
1174 "filename ends with ' ', which is not allowed on Windows"
1174 "filename ends with ' ', which is not allowed on Windows"
1175 >>> checkwinfilename("../bar")
1175 >>> checkwinfilename("../bar")
1176 >>> checkwinfilename("foo\\")
1176 >>> checkwinfilename("foo\\")
1177 "filename ends with '\\', which is invalid on Windows"
1177 "filename ends with '\\', which is invalid on Windows"
1178 >>> checkwinfilename("foo\\/bar")
1178 >>> checkwinfilename("foo\\/bar")
1179 "directory name ends with '\\', which is invalid on Windows"
1179 "directory name ends with '\\', which is invalid on Windows"
1180 '''
1180 '''
1181 if path.endswith('\\'):
1181 if path.endswith('\\'):
1182 return _("filename ends with '\\', which is invalid on Windows")
1182 return _("filename ends with '\\', which is invalid on Windows")
1183 if '\\/' in path:
1183 if '\\/' in path:
1184 return _("directory name ends with '\\', which is invalid on Windows")
1184 return _("directory name ends with '\\', which is invalid on Windows")
1185 for n in path.replace('\\', '/').split('/'):
1185 for n in path.replace('\\', '/').split('/'):
1186 if not n:
1186 if not n:
1187 continue
1187 continue
1188 for c in n:
1188 for c in n:
1189 if c in _winreservedchars:
1189 if c in _winreservedchars:
1190 return _("filename contains '%s', which is reserved "
1190 return _("filename contains '%s', which is reserved "
1191 "on Windows") % c
1191 "on Windows") % c
1192 if ord(c) <= 31:
1192 if ord(c) <= 31:
1193 return _("filename contains %r, which is invalid "
1193 return _("filename contains %r, which is invalid "
1194 "on Windows") % c
1194 "on Windows") % c
1195 base = n.split('.')[0]
1195 base = n.split('.')[0]
1196 if base and base.lower() in _winreservednames:
1196 if base and base.lower() in _winreservednames:
1197 return _("filename contains '%s', which is reserved "
1197 return _("filename contains '%s', which is reserved "
1198 "on Windows") % base
1198 "on Windows") % base
1199 t = n[-1]
1199 t = n[-1]
1200 if t in '. ' and n not in '..':
1200 if t in '. ' and n not in '..':
1201 return _("filename ends with '%s', which is not allowed "
1201 return _("filename ends with '%s', which is not allowed "
1202 "on Windows") % t
1202 "on Windows") % t
1203
1203
1204 if pycompat.osname == 'nt':
1204 if pycompat.osname == 'nt':
1205 checkosfilename = checkwinfilename
1205 checkosfilename = checkwinfilename
1206 else:
1206 else:
1207 checkosfilename = platform.checkosfilename
1207 checkosfilename = platform.checkosfilename
1208
1208
1209 def makelock(info, pathname):
1209 def makelock(info, pathname):
1210 try:
1210 try:
1211 return os.symlink(info, pathname)
1211 return os.symlink(info, pathname)
1212 except OSError as why:
1212 except OSError as why:
1213 if why.errno == errno.EEXIST:
1213 if why.errno == errno.EEXIST:
1214 raise
1214 raise
1215 except AttributeError: # no symlink in os
1215 except AttributeError: # no symlink in os
1216 pass
1216 pass
1217
1217
1218 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1218 ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL)
1219 os.write(ld, info)
1219 os.write(ld, info)
1220 os.close(ld)
1220 os.close(ld)
1221
1221
1222 def readlock(pathname):
1222 def readlock(pathname):
1223 try:
1223 try:
1224 return os.readlink(pathname)
1224 return os.readlink(pathname)
1225 except OSError as why:
1225 except OSError as why:
1226 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1226 if why.errno not in (errno.EINVAL, errno.ENOSYS):
1227 raise
1227 raise
1228 except AttributeError: # no symlink in os
1228 except AttributeError: # no symlink in os
1229 pass
1229 pass
1230 fp = posixfile(pathname)
1230 fp = posixfile(pathname)
1231 r = fp.read()
1231 r = fp.read()
1232 fp.close()
1232 fp.close()
1233 return r
1233 return r
1234
1234
1235 def fstat(fp):
1235 def fstat(fp):
1236 '''stat file object that may not have fileno method.'''
1236 '''stat file object that may not have fileno method.'''
1237 try:
1237 try:
1238 return os.fstat(fp.fileno())
1238 return os.fstat(fp.fileno())
1239 except AttributeError:
1239 except AttributeError:
1240 return os.stat(fp.name)
1240 return os.stat(fp.name)
1241
1241
1242 # File system features
1242 # File system features
1243
1243
1244 def fscasesensitive(path):
1244 def fscasesensitive(path):
1245 """
1245 """
1246 Return true if the given path is on a case-sensitive filesystem
1246 Return true if the given path is on a case-sensitive filesystem
1247
1247
1248 Requires a path (like /foo/.hg) ending with a foldable final
1248 Requires a path (like /foo/.hg) ending with a foldable final
1249 directory component.
1249 directory component.
1250 """
1250 """
1251 s1 = os.lstat(path)
1251 s1 = os.lstat(path)
1252 d, b = os.path.split(path)
1252 d, b = os.path.split(path)
1253 b2 = b.upper()
1253 b2 = b.upper()
1254 if b == b2:
1254 if b == b2:
1255 b2 = b.lower()
1255 b2 = b.lower()
1256 if b == b2:
1256 if b == b2:
1257 return True # no evidence against case sensitivity
1257 return True # no evidence against case sensitivity
1258 p2 = os.path.join(d, b2)
1258 p2 = os.path.join(d, b2)
1259 try:
1259 try:
1260 s2 = os.lstat(p2)
1260 s2 = os.lstat(p2)
1261 if s2 == s1:
1261 if s2 == s1:
1262 return False
1262 return False
1263 return True
1263 return True
1264 except OSError:
1264 except OSError:
1265 return True
1265 return True
1266
1266
1267 try:
1267 try:
1268 import re2
1268 import re2
1269 _re2 = None
1269 _re2 = None
1270 except ImportError:
1270 except ImportError:
1271 _re2 = False
1271 _re2 = False
1272
1272
1273 class _re(object):
1273 class _re(object):
1274 def _checkre2(self):
1274 def _checkre2(self):
1275 global _re2
1275 global _re2
1276 try:
1276 try:
1277 # check if match works, see issue3964
1277 # check if match works, see issue3964
1278 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1278 _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
1279 except ImportError:
1279 except ImportError:
1280 _re2 = False
1280 _re2 = False
1281
1281
1282 def compile(self, pat, flags=0):
1282 def compile(self, pat, flags=0):
1283 '''Compile a regular expression, using re2 if possible
1283 '''Compile a regular expression, using re2 if possible
1284
1284
1285 For best performance, use only re2-compatible regexp features. The
1285 For best performance, use only re2-compatible regexp features. The
1286 only flags from the re module that are re2-compatible are
1286 only flags from the re module that are re2-compatible are
1287 IGNORECASE and MULTILINE.'''
1287 IGNORECASE and MULTILINE.'''
1288 if _re2 is None:
1288 if _re2 is None:
1289 self._checkre2()
1289 self._checkre2()
1290 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1290 if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
1291 if flags & remod.IGNORECASE:
1291 if flags & remod.IGNORECASE:
1292 pat = '(?i)' + pat
1292 pat = '(?i)' + pat
1293 if flags & remod.MULTILINE:
1293 if flags & remod.MULTILINE:
1294 pat = '(?m)' + pat
1294 pat = '(?m)' + pat
1295 try:
1295 try:
1296 return re2.compile(pat)
1296 return re2.compile(pat)
1297 except re2.error:
1297 except re2.error:
1298 pass
1298 pass
1299 return remod.compile(pat, flags)
1299 return remod.compile(pat, flags)
1300
1300
1301 @propertycache
1301 @propertycache
1302 def escape(self):
1302 def escape(self):
1303 '''Return the version of escape corresponding to self.compile.
1303 '''Return the version of escape corresponding to self.compile.
1304
1304
1305 This is imperfect because whether re2 or re is used for a particular
1305 This is imperfect because whether re2 or re is used for a particular
1306 function depends on the flags, etc, but it's the best we can do.
1306 function depends on the flags, etc, but it's the best we can do.
1307 '''
1307 '''
1308 global _re2
1308 global _re2
1309 if _re2 is None:
1309 if _re2 is None:
1310 self._checkre2()
1310 self._checkre2()
1311 if _re2:
1311 if _re2:
1312 return re2.escape
1312 return re2.escape
1313 else:
1313 else:
1314 return remod.escape
1314 return remod.escape
1315
1315
1316 re = _re()
1316 re = _re()
1317
1317
1318 _fspathcache = {}
1318 _fspathcache = {}
1319 def fspath(name, root):
1319 def fspath(name, root):
1320 '''Get name in the case stored in the filesystem
1320 '''Get name in the case stored in the filesystem
1321
1321
1322 The name should be relative to root, and be normcase-ed for efficiency.
1322 The name should be relative to root, and be normcase-ed for efficiency.
1323
1323
1324 Note that this function is unnecessary, and should not be
1324 Note that this function is unnecessary, and should not be
1325 called, for case-sensitive filesystems (simply because it's expensive).
1325 called, for case-sensitive filesystems (simply because it's expensive).
1326
1326
1327 The root should be normcase-ed, too.
1327 The root should be normcase-ed, too.
1328 '''
1328 '''
1329 def _makefspathcacheentry(dir):
1329 def _makefspathcacheentry(dir):
1330 return dict((normcase(n), n) for n in os.listdir(dir))
1330 return dict((normcase(n), n) for n in os.listdir(dir))
1331
1331
1332 seps = pycompat.ossep
1332 seps = pycompat.ossep
1333 if pycompat.osaltsep:
1333 if pycompat.osaltsep:
1334 seps = seps + pycompat.osaltsep
1334 seps = seps + pycompat.osaltsep
1335 # Protect backslashes. This gets silly very quickly.
1335 # Protect backslashes. This gets silly very quickly.
1336 seps.replace('\\','\\\\')
1336 seps.replace('\\','\\\\')
1337 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1337 pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps))
1338 dir = os.path.normpath(root)
1338 dir = os.path.normpath(root)
1339 result = []
1339 result = []
1340 for part, sep in pattern.findall(name):
1340 for part, sep in pattern.findall(name):
1341 if sep:
1341 if sep:
1342 result.append(sep)
1342 result.append(sep)
1343 continue
1343 continue
1344
1344
1345 if dir not in _fspathcache:
1345 if dir not in _fspathcache:
1346 _fspathcache[dir] = _makefspathcacheentry(dir)
1346 _fspathcache[dir] = _makefspathcacheentry(dir)
1347 contents = _fspathcache[dir]
1347 contents = _fspathcache[dir]
1348
1348
1349 found = contents.get(part)
1349 found = contents.get(part)
1350 if not found:
1350 if not found:
1351 # retry "once per directory" per "dirstate.walk" which
1351 # retry "once per directory" per "dirstate.walk" which
1352 # may take place for each patches of "hg qpush", for example
1352 # may take place for each patches of "hg qpush", for example
1353 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1353 _fspathcache[dir] = contents = _makefspathcacheentry(dir)
1354 found = contents.get(part)
1354 found = contents.get(part)
1355
1355
1356 result.append(found or part)
1356 result.append(found or part)
1357 dir = os.path.join(dir, part)
1357 dir = os.path.join(dir, part)
1358
1358
1359 return ''.join(result)
1359 return ''.join(result)
1360
1360
1361 def checknlink(testfile):
1361 def checknlink(testfile):
1362 '''check whether hardlink count reporting works properly'''
1362 '''check whether hardlink count reporting works properly'''
1363
1363
1364 # testfile may be open, so we need a separate file for checking to
1364 # testfile may be open, so we need a separate file for checking to
1365 # work around issue2543 (or testfile may get lost on Samba shares)
1365 # work around issue2543 (or testfile may get lost on Samba shares)
1366 f1 = testfile + ".hgtmp1"
1366 f1 = testfile + ".hgtmp1"
1367 if os.path.lexists(f1):
1367 if os.path.lexists(f1):
1368 return False
1368 return False
1369 try:
1369 try:
1370 posixfile(f1, 'w').close()
1370 posixfile(f1, 'w').close()
1371 except IOError:
1371 except IOError:
1372 try:
1372 try:
1373 os.unlink(f1)
1373 os.unlink(f1)
1374 except OSError:
1374 except OSError:
1375 pass
1375 pass
1376 return False
1376 return False
1377
1377
1378 f2 = testfile + ".hgtmp2"
1378 f2 = testfile + ".hgtmp2"
1379 fd = None
1379 fd = None
1380 try:
1380 try:
1381 oslink(f1, f2)
1381 oslink(f1, f2)
1382 # nlinks() may behave differently for files on Windows shares if
1382 # nlinks() may behave differently for files on Windows shares if
1383 # the file is open.
1383 # the file is open.
1384 fd = posixfile(f2)
1384 fd = posixfile(f2)
1385 return nlinks(f2) > 1
1385 return nlinks(f2) > 1
1386 except OSError:
1386 except OSError:
1387 return False
1387 return False
1388 finally:
1388 finally:
1389 if fd is not None:
1389 if fd is not None:
1390 fd.close()
1390 fd.close()
1391 for f in (f1, f2):
1391 for f in (f1, f2):
1392 try:
1392 try:
1393 os.unlink(f)
1393 os.unlink(f)
1394 except OSError:
1394 except OSError:
1395 pass
1395 pass
1396
1396
1397 def endswithsep(path):
1397 def endswithsep(path):
1398 '''Check path ends with os.sep or os.altsep.'''
1398 '''Check path ends with os.sep or os.altsep.'''
1399 return (path.endswith(pycompat.ossep)
1399 return (path.endswith(pycompat.ossep)
1400 or pycompat.osaltsep and path.endswith(pycompat.osaltsep))
1400 or pycompat.osaltsep and path.endswith(pycompat.osaltsep))
1401
1401
1402 def splitpath(path):
1402 def splitpath(path):
1403 '''Split path by os.sep.
1403 '''Split path by os.sep.
1404 Note that this function does not use os.altsep because this is
1404 Note that this function does not use os.altsep because this is
1405 an alternative of simple "xxx.split(os.sep)".
1405 an alternative of simple "xxx.split(os.sep)".
1406 It is recommended to use os.path.normpath() before using this
1406 It is recommended to use os.path.normpath() before using this
1407 function if need.'''
1407 function if need.'''
1408 return path.split(pycompat.ossep)
1408 return path.split(pycompat.ossep)
1409
1409
1410 def gui():
1410 def gui():
1411 '''Are we running in a GUI?'''
1411 '''Are we running in a GUI?'''
1412 if pycompat.sysplatform == 'darwin':
1412 if pycompat.sysplatform == 'darwin':
1413 if 'SSH_CONNECTION' in encoding.environ:
1413 if 'SSH_CONNECTION' in encoding.environ:
1414 # handle SSH access to a box where the user is logged in
1414 # handle SSH access to a box where the user is logged in
1415 return False
1415 return False
1416 elif getattr(osutil, 'isgui', None):
1416 elif getattr(osutil, 'isgui', None):
1417 # check if a CoreGraphics session is available
1417 # check if a CoreGraphics session is available
1418 return osutil.isgui()
1418 return osutil.isgui()
1419 else:
1419 else:
1420 # pure build; use a safe default
1420 # pure build; use a safe default
1421 return True
1421 return True
1422 else:
1422 else:
1423 return pycompat.osname == "nt" or encoding.environ.get("DISPLAY")
1423 return pycompat.osname == "nt" or encoding.environ.get("DISPLAY")
1424
1424
1425 def mktempcopy(name, emptyok=False, createmode=None):
1425 def mktempcopy(name, emptyok=False, createmode=None):
1426 """Create a temporary file with the same contents from name
1426 """Create a temporary file with the same contents from name
1427
1427
1428 The permission bits are copied from the original file.
1428 The permission bits are copied from the original file.
1429
1429
1430 If the temporary file is going to be truncated immediately, you
1430 If the temporary file is going to be truncated immediately, you
1431 can use emptyok=True as an optimization.
1431 can use emptyok=True as an optimization.
1432
1432
1433 Returns the name of the temporary file.
1433 Returns the name of the temporary file.
1434 """
1434 """
1435 d, fn = os.path.split(name)
1435 d, fn = os.path.split(name)
1436 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1436 fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
1437 os.close(fd)
1437 os.close(fd)
1438 # Temporary files are created with mode 0600, which is usually not
1438 # Temporary files are created with mode 0600, which is usually not
1439 # what we want. If the original file already exists, just copy
1439 # what we want. If the original file already exists, just copy
1440 # its mode. Otherwise, manually obey umask.
1440 # its mode. Otherwise, manually obey umask.
1441 copymode(name, temp, createmode)
1441 copymode(name, temp, createmode)
1442 if emptyok:
1442 if emptyok:
1443 return temp
1443 return temp
1444 try:
1444 try:
1445 try:
1445 try:
1446 ifp = posixfile(name, "rb")
1446 ifp = posixfile(name, "rb")
1447 except IOError as inst:
1447 except IOError as inst:
1448 if inst.errno == errno.ENOENT:
1448 if inst.errno == errno.ENOENT:
1449 return temp
1449 return temp
1450 if not getattr(inst, 'filename', None):
1450 if not getattr(inst, 'filename', None):
1451 inst.filename = name
1451 inst.filename = name
1452 raise
1452 raise
1453 ofp = posixfile(temp, "wb")
1453 ofp = posixfile(temp, "wb")
1454 for chunk in filechunkiter(ifp):
1454 for chunk in filechunkiter(ifp):
1455 ofp.write(chunk)
1455 ofp.write(chunk)
1456 ifp.close()
1456 ifp.close()
1457 ofp.close()
1457 ofp.close()
1458 except: # re-raises
1458 except: # re-raises
1459 try: os.unlink(temp)
1459 try: os.unlink(temp)
1460 except OSError: pass
1460 except OSError: pass
1461 raise
1461 raise
1462 return temp
1462 return temp
1463
1463
1464 class filestat(object):
1464 class filestat(object):
1465 """help to exactly detect change of a file
1465 """help to exactly detect change of a file
1466
1466
1467 'stat' attribute is result of 'os.stat()' if specified 'path'
1467 'stat' attribute is result of 'os.stat()' if specified 'path'
1468 exists. Otherwise, it is None. This can avoid preparative
1468 exists. Otherwise, it is None. This can avoid preparative
1469 'exists()' examination on client side of this class.
1469 'exists()' examination on client side of this class.
1470 """
1470 """
1471 def __init__(self, path):
1471 def __init__(self, path):
1472 try:
1472 try:
1473 self.stat = os.stat(path)
1473 self.stat = os.stat(path)
1474 except OSError as err:
1474 except OSError as err:
1475 if err.errno != errno.ENOENT:
1475 if err.errno != errno.ENOENT:
1476 raise
1476 raise
1477 self.stat = None
1477 self.stat = None
1478
1478
1479 __hash__ = object.__hash__
1479 __hash__ = object.__hash__
1480
1480
1481 def __eq__(self, old):
1481 def __eq__(self, old):
1482 try:
1482 try:
1483 # if ambiguity between stat of new and old file is
1483 # if ambiguity between stat of new and old file is
1484 # avoided, comparison of size, ctime and mtime is enough
1484 # avoided, comparison of size, ctime and mtime is enough
1485 # to exactly detect change of a file regardless of platform
1485 # to exactly detect change of a file regardless of platform
1486 return (self.stat.st_size == old.stat.st_size and
1486 return (self.stat.st_size == old.stat.st_size and
1487 self.stat.st_ctime == old.stat.st_ctime and
1487 self.stat.st_ctime == old.stat.st_ctime and
1488 self.stat.st_mtime == old.stat.st_mtime)
1488 self.stat.st_mtime == old.stat.st_mtime)
1489 except AttributeError:
1489 except AttributeError:
1490 return False
1490 return False
1491
1491
1492 def isambig(self, old):
1492 def isambig(self, old):
1493 """Examine whether new (= self) stat is ambiguous against old one
1493 """Examine whether new (= self) stat is ambiguous against old one
1494
1494
1495 "S[N]" below means stat of a file at N-th change:
1495 "S[N]" below means stat of a file at N-th change:
1496
1496
1497 - S[n-1].ctime < S[n].ctime: can detect change of a file
1497 - S[n-1].ctime < S[n].ctime: can detect change of a file
1498 - S[n-1].ctime == S[n].ctime
1498 - S[n-1].ctime == S[n].ctime
1499 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1499 - S[n-1].ctime < S[n].mtime: means natural advancing (*1)
1500 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1500 - S[n-1].ctime == S[n].mtime: is ambiguous (*2)
1501 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1501 - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care)
1502 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1502 - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care)
1503
1503
1504 Case (*2) above means that a file was changed twice or more at
1504 Case (*2) above means that a file was changed twice or more at
1505 same time in sec (= S[n-1].ctime), and comparison of timestamp
1505 same time in sec (= S[n-1].ctime), and comparison of timestamp
1506 is ambiguous.
1506 is ambiguous.
1507
1507
1508 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1508 Base idea to avoid such ambiguity is "advance mtime 1 sec, if
1509 timestamp is ambiguous".
1509 timestamp is ambiguous".
1510
1510
1511 But advancing mtime only in case (*2) doesn't work as
1511 But advancing mtime only in case (*2) doesn't work as
1512 expected, because naturally advanced S[n].mtime in case (*1)
1512 expected, because naturally advanced S[n].mtime in case (*1)
1513 might be equal to manually advanced S[n-1 or earlier].mtime.
1513 might be equal to manually advanced S[n-1 or earlier].mtime.
1514
1514
1515 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1515 Therefore, all "S[n-1].ctime == S[n].ctime" cases should be
1516 treated as ambiguous regardless of mtime, to avoid overlooking
1516 treated as ambiguous regardless of mtime, to avoid overlooking
1517 by confliction between such mtime.
1517 by confliction between such mtime.
1518
1518
1519 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1519 Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime !=
1520 S[n].mtime", even if size of a file isn't changed.
1520 S[n].mtime", even if size of a file isn't changed.
1521 """
1521 """
1522 try:
1522 try:
1523 return (self.stat.st_ctime == old.stat.st_ctime)
1523 return (self.stat.st_ctime == old.stat.st_ctime)
1524 except AttributeError:
1524 except AttributeError:
1525 return False
1525 return False
1526
1526
1527 def avoidambig(self, path, old):
1527 def avoidambig(self, path, old):
1528 """Change file stat of specified path to avoid ambiguity
1528 """Change file stat of specified path to avoid ambiguity
1529
1529
1530 'old' should be previous filestat of 'path'.
1530 'old' should be previous filestat of 'path'.
1531
1531
1532 This skips avoiding ambiguity, if a process doesn't have
1532 This skips avoiding ambiguity, if a process doesn't have
1533 appropriate privileges for 'path'.
1533 appropriate privileges for 'path'.
1534 """
1534 """
1535 advanced = (old.stat.st_mtime + 1) & 0x7fffffff
1535 advanced = (old.stat.st_mtime + 1) & 0x7fffffff
1536 try:
1536 try:
1537 os.utime(path, (advanced, advanced))
1537 os.utime(path, (advanced, advanced))
1538 except OSError as inst:
1538 except OSError as inst:
1539 if inst.errno == errno.EPERM:
1539 if inst.errno == errno.EPERM:
1540 # utime() on the file created by another user causes EPERM,
1540 # utime() on the file created by another user causes EPERM,
1541 # if a process doesn't have appropriate privileges
1541 # if a process doesn't have appropriate privileges
1542 return
1542 return
1543 raise
1543 raise
1544
1544
1545 def __ne__(self, other):
1545 def __ne__(self, other):
1546 return not self == other
1546 return not self == other
1547
1547
1548 class atomictempfile(object):
1548 class atomictempfile(object):
1549 '''writable file object that atomically updates a file
1549 '''writable file object that atomically updates a file
1550
1550
1551 All writes will go to a temporary copy of the original file. Call
1551 All writes will go to a temporary copy of the original file. Call
1552 close() when you are done writing, and atomictempfile will rename
1552 close() when you are done writing, and atomictempfile will rename
1553 the temporary copy to the original name, making the changes
1553 the temporary copy to the original name, making the changes
1554 visible. If the object is destroyed without being closed, all your
1554 visible. If the object is destroyed without being closed, all your
1555 writes are discarded.
1555 writes are discarded.
1556
1556
1557 checkambig argument of constructor is used with filestat, and is
1557 checkambig argument of constructor is used with filestat, and is
1558 useful only if target file is guarded by any lock (e.g. repo.lock
1558 useful only if target file is guarded by any lock (e.g. repo.lock
1559 or repo.wlock).
1559 or repo.wlock).
1560 '''
1560 '''
1561 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1561 def __init__(self, name, mode='w+b', createmode=None, checkambig=False):
1562 self.__name = name # permanent name
1562 self.__name = name # permanent name
1563 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1563 self._tempname = mktempcopy(name, emptyok=('w' in mode),
1564 createmode=createmode)
1564 createmode=createmode)
1565 self._fp = posixfile(self._tempname, mode)
1565 self._fp = posixfile(self._tempname, mode)
1566 self._checkambig = checkambig
1566 self._checkambig = checkambig
1567
1567
1568 # delegated methods
1568 # delegated methods
1569 self.read = self._fp.read
1569 self.read = self._fp.read
1570 self.write = self._fp.write
1570 self.write = self._fp.write
1571 self.seek = self._fp.seek
1571 self.seek = self._fp.seek
1572 self.tell = self._fp.tell
1572 self.tell = self._fp.tell
1573 self.fileno = self._fp.fileno
1573 self.fileno = self._fp.fileno
1574
1574
1575 def close(self):
1575 def close(self):
1576 if not self._fp.closed:
1576 if not self._fp.closed:
1577 self._fp.close()
1577 self._fp.close()
1578 filename = localpath(self.__name)
1578 filename = localpath(self.__name)
1579 oldstat = self._checkambig and filestat(filename)
1579 oldstat = self._checkambig and filestat(filename)
1580 if oldstat and oldstat.stat:
1580 if oldstat and oldstat.stat:
1581 rename(self._tempname, filename)
1581 rename(self._tempname, filename)
1582 newstat = filestat(filename)
1582 newstat = filestat(filename)
1583 if newstat.isambig(oldstat):
1583 if newstat.isambig(oldstat):
1584 # stat of changed file is ambiguous to original one
1584 # stat of changed file is ambiguous to original one
1585 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1585 advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff
1586 os.utime(filename, (advanced, advanced))
1586 os.utime(filename, (advanced, advanced))
1587 else:
1587 else:
1588 rename(self._tempname, filename)
1588 rename(self._tempname, filename)
1589
1589
1590 def discard(self):
1590 def discard(self):
1591 if not self._fp.closed:
1591 if not self._fp.closed:
1592 try:
1592 try:
1593 os.unlink(self._tempname)
1593 os.unlink(self._tempname)
1594 except OSError:
1594 except OSError:
1595 pass
1595 pass
1596 self._fp.close()
1596 self._fp.close()
1597
1597
1598 def __del__(self):
1598 def __del__(self):
1599 if safehasattr(self, '_fp'): # constructor actually did something
1599 if safehasattr(self, '_fp'): # constructor actually did something
1600 self.discard()
1600 self.discard()
1601
1601
1602 def __enter__(self):
1602 def __enter__(self):
1603 return self
1603 return self
1604
1604
1605 def __exit__(self, exctype, excvalue, traceback):
1605 def __exit__(self, exctype, excvalue, traceback):
1606 if exctype is not None:
1606 if exctype is not None:
1607 self.discard()
1607 self.discard()
1608 else:
1608 else:
1609 self.close()
1609 self.close()
1610
1610
1611 def makedirs(name, mode=None, notindexed=False):
1611 def makedirs(name, mode=None, notindexed=False):
1612 """recursive directory creation with parent mode inheritance
1612 """recursive directory creation with parent mode inheritance
1613
1613
1614 Newly created directories are marked as "not to be indexed by
1614 Newly created directories are marked as "not to be indexed by
1615 the content indexing service", if ``notindexed`` is specified
1615 the content indexing service", if ``notindexed`` is specified
1616 for "write" mode access.
1616 for "write" mode access.
1617 """
1617 """
1618 try:
1618 try:
1619 makedir(name, notindexed)
1619 makedir(name, notindexed)
1620 except OSError as err:
1620 except OSError as err:
1621 if err.errno == errno.EEXIST:
1621 if err.errno == errno.EEXIST:
1622 return
1622 return
1623 if err.errno != errno.ENOENT or not name:
1623 if err.errno != errno.ENOENT or not name:
1624 raise
1624 raise
1625 parent = os.path.dirname(os.path.abspath(name))
1625 parent = os.path.dirname(os.path.abspath(name))
1626 if parent == name:
1626 if parent == name:
1627 raise
1627 raise
1628 makedirs(parent, mode, notindexed)
1628 makedirs(parent, mode, notindexed)
1629 try:
1629 try:
1630 makedir(name, notindexed)
1630 makedir(name, notindexed)
1631 except OSError as err:
1631 except OSError as err:
1632 # Catch EEXIST to handle races
1632 # Catch EEXIST to handle races
1633 if err.errno == errno.EEXIST:
1633 if err.errno == errno.EEXIST:
1634 return
1634 return
1635 raise
1635 raise
1636 if mode is not None:
1636 if mode is not None:
1637 os.chmod(name, mode)
1637 os.chmod(name, mode)
1638
1638
1639 def readfile(path):
1639 def readfile(path):
1640 with open(path, 'rb') as fp:
1640 with open(path, 'rb') as fp:
1641 return fp.read()
1641 return fp.read()
1642
1642
1643 def writefile(path, text):
1643 def writefile(path, text):
1644 with open(path, 'wb') as fp:
1644 with open(path, 'wb') as fp:
1645 fp.write(text)
1645 fp.write(text)
1646
1646
1647 def appendfile(path, text):
1647 def appendfile(path, text):
1648 with open(path, 'ab') as fp:
1648 with open(path, 'ab') as fp:
1649 fp.write(text)
1649 fp.write(text)
1650
1650
1651 class chunkbuffer(object):
1651 class chunkbuffer(object):
1652 """Allow arbitrary sized chunks of data to be efficiently read from an
1652 """Allow arbitrary sized chunks of data to be efficiently read from an
1653 iterator over chunks of arbitrary size."""
1653 iterator over chunks of arbitrary size."""
1654
1654
1655 def __init__(self, in_iter):
1655 def __init__(self, in_iter):
1656 """in_iter is the iterator that's iterating over the input chunks.
1656 """in_iter is the iterator that's iterating over the input chunks.
1657 targetsize is how big a buffer to try to maintain."""
1657 targetsize is how big a buffer to try to maintain."""
1658 def splitbig(chunks):
1658 def splitbig(chunks):
1659 for chunk in chunks:
1659 for chunk in chunks:
1660 if len(chunk) > 2**20:
1660 if len(chunk) > 2**20:
1661 pos = 0
1661 pos = 0
1662 while pos < len(chunk):
1662 while pos < len(chunk):
1663 end = pos + 2 ** 18
1663 end = pos + 2 ** 18
1664 yield chunk[pos:end]
1664 yield chunk[pos:end]
1665 pos = end
1665 pos = end
1666 else:
1666 else:
1667 yield chunk
1667 yield chunk
1668 self.iter = splitbig(in_iter)
1668 self.iter = splitbig(in_iter)
1669 self._queue = collections.deque()
1669 self._queue = collections.deque()
1670 self._chunkoffset = 0
1670 self._chunkoffset = 0
1671
1671
1672 def read(self, l=None):
1672 def read(self, l=None):
1673 """Read L bytes of data from the iterator of chunks of data.
1673 """Read L bytes of data from the iterator of chunks of data.
1674 Returns less than L bytes if the iterator runs dry.
1674 Returns less than L bytes if the iterator runs dry.
1675
1675
1676 If size parameter is omitted, read everything"""
1676 If size parameter is omitted, read everything"""
1677 if l is None:
1677 if l is None:
1678 return ''.join(self.iter)
1678 return ''.join(self.iter)
1679
1679
1680 left = l
1680 left = l
1681 buf = []
1681 buf = []
1682 queue = self._queue
1682 queue = self._queue
1683 while left > 0:
1683 while left > 0:
1684 # refill the queue
1684 # refill the queue
1685 if not queue:
1685 if not queue:
1686 target = 2**18
1686 target = 2**18
1687 for chunk in self.iter:
1687 for chunk in self.iter:
1688 queue.append(chunk)
1688 queue.append(chunk)
1689 target -= len(chunk)
1689 target -= len(chunk)
1690 if target <= 0:
1690 if target <= 0:
1691 break
1691 break
1692 if not queue:
1692 if not queue:
1693 break
1693 break
1694
1694
1695 # The easy way to do this would be to queue.popleft(), modify the
1695 # The easy way to do this would be to queue.popleft(), modify the
1696 # chunk (if necessary), then queue.appendleft(). However, for cases
1696 # chunk (if necessary), then queue.appendleft(). However, for cases
1697 # where we read partial chunk content, this incurs 2 dequeue
1697 # where we read partial chunk content, this incurs 2 dequeue
1698 # mutations and creates a new str for the remaining chunk in the
1698 # mutations and creates a new str for the remaining chunk in the
1699 # queue. Our code below avoids this overhead.
1699 # queue. Our code below avoids this overhead.
1700
1700
1701 chunk = queue[0]
1701 chunk = queue[0]
1702 chunkl = len(chunk)
1702 chunkl = len(chunk)
1703 offset = self._chunkoffset
1703 offset = self._chunkoffset
1704
1704
1705 # Use full chunk.
1705 # Use full chunk.
1706 if offset == 0 and left >= chunkl:
1706 if offset == 0 and left >= chunkl:
1707 left -= chunkl
1707 left -= chunkl
1708 queue.popleft()
1708 queue.popleft()
1709 buf.append(chunk)
1709 buf.append(chunk)
1710 # self._chunkoffset remains at 0.
1710 # self._chunkoffset remains at 0.
1711 continue
1711 continue
1712
1712
1713 chunkremaining = chunkl - offset
1713 chunkremaining = chunkl - offset
1714
1714
1715 # Use all of unconsumed part of chunk.
1715 # Use all of unconsumed part of chunk.
1716 if left >= chunkremaining:
1716 if left >= chunkremaining:
1717 left -= chunkremaining
1717 left -= chunkremaining
1718 queue.popleft()
1718 queue.popleft()
1719 # offset == 0 is enabled by block above, so this won't merely
1719 # offset == 0 is enabled by block above, so this won't merely
1720 # copy via ``chunk[0:]``.
1720 # copy via ``chunk[0:]``.
1721 buf.append(chunk[offset:])
1721 buf.append(chunk[offset:])
1722 self._chunkoffset = 0
1722 self._chunkoffset = 0
1723
1723
1724 # Partial chunk needed.
1724 # Partial chunk needed.
1725 else:
1725 else:
1726 buf.append(chunk[offset:offset + left])
1726 buf.append(chunk[offset:offset + left])
1727 self._chunkoffset += left
1727 self._chunkoffset += left
1728 left -= chunkremaining
1728 left -= chunkremaining
1729
1729
1730 return ''.join(buf)
1730 return ''.join(buf)
1731
1731
1732 def filechunkiter(f, size=131072, limit=None):
1732 def filechunkiter(f, size=131072, limit=None):
1733 """Create a generator that produces the data in the file size
1733 """Create a generator that produces the data in the file size
1734 (default 131072) bytes at a time, up to optional limit (default is
1734 (default 131072) bytes at a time, up to optional limit (default is
1735 to read all data). Chunks may be less than size bytes if the
1735 to read all data). Chunks may be less than size bytes if the
1736 chunk is the last chunk in the file, or the file is a socket or
1736 chunk is the last chunk in the file, or the file is a socket or
1737 some other type of file that sometimes reads less data than is
1737 some other type of file that sometimes reads less data than is
1738 requested."""
1738 requested."""
1739 assert size >= 0
1739 assert size >= 0
1740 assert limit is None or limit >= 0
1740 assert limit is None or limit >= 0
1741 while True:
1741 while True:
1742 if limit is None:
1742 if limit is None:
1743 nbytes = size
1743 nbytes = size
1744 else:
1744 else:
1745 nbytes = min(limit, size)
1745 nbytes = min(limit, size)
1746 s = nbytes and f.read(nbytes)
1746 s = nbytes and f.read(nbytes)
1747 if not s:
1747 if not s:
1748 break
1748 break
1749 if limit:
1749 if limit:
1750 limit -= len(s)
1750 limit -= len(s)
1751 yield s
1751 yield s
1752
1752
1753 def makedate(timestamp=None):
1753 def makedate(timestamp=None):
1754 '''Return a unix timestamp (or the current time) as a (unixtime,
1754 '''Return a unix timestamp (or the current time) as a (unixtime,
1755 offset) tuple based off the local timezone.'''
1755 offset) tuple based off the local timezone.'''
1756 if timestamp is None:
1756 if timestamp is None:
1757 timestamp = time.time()
1757 timestamp = time.time()
1758 if timestamp < 0:
1758 if timestamp < 0:
1759 hint = _("check your clock")
1759 hint = _("check your clock")
1760 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1760 raise Abort(_("negative timestamp: %d") % timestamp, hint=hint)
1761 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1761 delta = (datetime.datetime.utcfromtimestamp(timestamp) -
1762 datetime.datetime.fromtimestamp(timestamp))
1762 datetime.datetime.fromtimestamp(timestamp))
1763 tz = delta.days * 86400 + delta.seconds
1763 tz = delta.days * 86400 + delta.seconds
1764 return timestamp, tz
1764 return timestamp, tz
1765
1765
1766 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1766 def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'):
1767 """represent a (unixtime, offset) tuple as a localized time.
1767 """represent a (unixtime, offset) tuple as a localized time.
1768 unixtime is seconds since the epoch, and offset is the time zone's
1768 unixtime is seconds since the epoch, and offset is the time zone's
1769 number of seconds away from UTC.
1769 number of seconds away from UTC.
1770
1770
1771 >>> datestr((0, 0))
1771 >>> datestr((0, 0))
1772 'Thu Jan 01 00:00:00 1970 +0000'
1772 'Thu Jan 01 00:00:00 1970 +0000'
1773 >>> datestr((42, 0))
1773 >>> datestr((42, 0))
1774 'Thu Jan 01 00:00:42 1970 +0000'
1774 'Thu Jan 01 00:00:42 1970 +0000'
1775 >>> datestr((-42, 0))
1775 >>> datestr((-42, 0))
1776 'Wed Dec 31 23:59:18 1969 +0000'
1776 'Wed Dec 31 23:59:18 1969 +0000'
1777 >>> datestr((0x7fffffff, 0))
1777 >>> datestr((0x7fffffff, 0))
1778 'Tue Jan 19 03:14:07 2038 +0000'
1778 'Tue Jan 19 03:14:07 2038 +0000'
1779 >>> datestr((-0x80000000, 0))
1779 >>> datestr((-0x80000000, 0))
1780 'Fri Dec 13 20:45:52 1901 +0000'
1780 'Fri Dec 13 20:45:52 1901 +0000'
1781 """
1781 """
1782 t, tz = date or makedate()
1782 t, tz = date or makedate()
1783 if "%1" in format or "%2" in format or "%z" in format:
1783 if "%1" in format or "%2" in format or "%z" in format:
1784 sign = (tz > 0) and "-" or "+"
1784 sign = (tz > 0) and "-" or "+"
1785 minutes = abs(tz) // 60
1785 minutes = abs(tz) // 60
1786 q, r = divmod(minutes, 60)
1786 q, r = divmod(minutes, 60)
1787 format = format.replace("%z", "%1%2")
1787 format = format.replace("%z", "%1%2")
1788 format = format.replace("%1", "%c%02d" % (sign, q))
1788 format = format.replace("%1", "%c%02d" % (sign, q))
1789 format = format.replace("%2", "%02d" % r)
1789 format = format.replace("%2", "%02d" % r)
1790 d = t - tz
1790 d = t - tz
1791 if d > 0x7fffffff:
1791 if d > 0x7fffffff:
1792 d = 0x7fffffff
1792 d = 0x7fffffff
1793 elif d < -0x80000000:
1793 elif d < -0x80000000:
1794 d = -0x80000000
1794 d = -0x80000000
1795 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1795 # Never use time.gmtime() and datetime.datetime.fromtimestamp()
1796 # because they use the gmtime() system call which is buggy on Windows
1796 # because they use the gmtime() system call which is buggy on Windows
1797 # for negative values.
1797 # for negative values.
1798 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1798 t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d)
1799 s = t.strftime(format)
1799 s = t.strftime(format)
1800 return s
1800 return s
1801
1801
1802 def shortdate(date=None):
1802 def shortdate(date=None):
1803 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1803 """turn (timestamp, tzoff) tuple into iso 8631 date."""
1804 return datestr(date, format='%Y-%m-%d')
1804 return datestr(date, format='%Y-%m-%d')
1805
1805
1806 def parsetimezone(s):
1806 def parsetimezone(s):
1807 """find a trailing timezone, if any, in string, and return a
1807 """find a trailing timezone, if any, in string, and return a
1808 (offset, remainder) pair"""
1808 (offset, remainder) pair"""
1809
1809
1810 if s.endswith("GMT") or s.endswith("UTC"):
1810 if s.endswith("GMT") or s.endswith("UTC"):
1811 return 0, s[:-3].rstrip()
1811 return 0, s[:-3].rstrip()
1812
1812
1813 # Unix-style timezones [+-]hhmm
1813 # Unix-style timezones [+-]hhmm
1814 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1814 if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit():
1815 sign = (s[-5] == "+") and 1 or -1
1815 sign = (s[-5] == "+") and 1 or -1
1816 hours = int(s[-4:-2])
1816 hours = int(s[-4:-2])
1817 minutes = int(s[-2:])
1817 minutes = int(s[-2:])
1818 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1818 return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip()
1819
1819
1820 # ISO8601 trailing Z
1820 # ISO8601 trailing Z
1821 if s.endswith("Z") and s[-2:-1].isdigit():
1821 if s.endswith("Z") and s[-2:-1].isdigit():
1822 return 0, s[:-1]
1822 return 0, s[:-1]
1823
1823
1824 # ISO8601-style [+-]hh:mm
1824 # ISO8601-style [+-]hh:mm
1825 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1825 if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and
1826 s[-5:-3].isdigit() and s[-2:].isdigit()):
1826 s[-5:-3].isdigit() and s[-2:].isdigit()):
1827 sign = (s[-6] == "+") and 1 or -1
1827 sign = (s[-6] == "+") and 1 or -1
1828 hours = int(s[-5:-3])
1828 hours = int(s[-5:-3])
1829 minutes = int(s[-2:])
1829 minutes = int(s[-2:])
1830 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1830 return -sign * (hours * 60 + minutes) * 60, s[:-6]
1831
1831
1832 return None, s
1832 return None, s
1833
1833
1834 def strdate(string, format, defaults=[]):
1834 def strdate(string, format, defaults=[]):
1835 """parse a localized time string and return a (unixtime, offset) tuple.
1835 """parse a localized time string and return a (unixtime, offset) tuple.
1836 if the string cannot be parsed, ValueError is raised."""
1836 if the string cannot be parsed, ValueError is raised."""
1837 # NOTE: unixtime = localunixtime + offset
1837 # NOTE: unixtime = localunixtime + offset
1838 offset, date = parsetimezone(string)
1838 offset, date = parsetimezone(string)
1839
1839
1840 # add missing elements from defaults
1840 # add missing elements from defaults
1841 usenow = False # default to using biased defaults
1841 usenow = False # default to using biased defaults
1842 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1842 for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity
1843 found = [True for p in part if ("%"+p) in format]
1843 found = [True for p in part if ("%"+p) in format]
1844 if not found:
1844 if not found:
1845 date += "@" + defaults[part][usenow]
1845 date += "@" + defaults[part][usenow]
1846 format += "@%" + part[0]
1846 format += "@%" + part[0]
1847 else:
1847 else:
1848 # We've found a specific time element, less specific time
1848 # We've found a specific time element, less specific time
1849 # elements are relative to today
1849 # elements are relative to today
1850 usenow = True
1850 usenow = True
1851
1851
1852 timetuple = time.strptime(date, format)
1852 timetuple = time.strptime(date, format)
1853 localunixtime = int(calendar.timegm(timetuple))
1853 localunixtime = int(calendar.timegm(timetuple))
1854 if offset is None:
1854 if offset is None:
1855 # local timezone
1855 # local timezone
1856 unixtime = int(time.mktime(timetuple))
1856 unixtime = int(time.mktime(timetuple))
1857 offset = unixtime - localunixtime
1857 offset = unixtime - localunixtime
1858 else:
1858 else:
1859 unixtime = localunixtime + offset
1859 unixtime = localunixtime + offset
1860 return unixtime, offset
1860 return unixtime, offset
1861
1861
1862 def parsedate(date, formats=None, bias=None):
1862 def parsedate(date, formats=None, bias=None):
1863 """parse a localized date/time and return a (unixtime, offset) tuple.
1863 """parse a localized date/time and return a (unixtime, offset) tuple.
1864
1864
1865 The date may be a "unixtime offset" string or in one of the specified
1865 The date may be a "unixtime offset" string or in one of the specified
1866 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1866 formats. If the date already is a (unixtime, offset) tuple, it is returned.
1867
1867
1868 >>> parsedate(' today ') == parsedate(\
1868 >>> parsedate(' today ') == parsedate(\
1869 datetime.date.today().strftime('%b %d'))
1869 datetime.date.today().strftime('%b %d'))
1870 True
1870 True
1871 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1871 >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\
1872 datetime.timedelta(days=1)\
1872 datetime.timedelta(days=1)\
1873 ).strftime('%b %d'))
1873 ).strftime('%b %d'))
1874 True
1874 True
1875 >>> now, tz = makedate()
1875 >>> now, tz = makedate()
1876 >>> strnow, strtz = parsedate('now')
1876 >>> strnow, strtz = parsedate('now')
1877 >>> (strnow - now) < 1
1877 >>> (strnow - now) < 1
1878 True
1878 True
1879 >>> tz == strtz
1879 >>> tz == strtz
1880 True
1880 True
1881 """
1881 """
1882 if bias is None:
1882 if bias is None:
1883 bias = {}
1883 bias = {}
1884 if not date:
1884 if not date:
1885 return 0, 0
1885 return 0, 0
1886 if isinstance(date, tuple) and len(date) == 2:
1886 if isinstance(date, tuple) and len(date) == 2:
1887 return date
1887 return date
1888 if not formats:
1888 if not formats:
1889 formats = defaultdateformats
1889 formats = defaultdateformats
1890 date = date.strip()
1890 date = date.strip()
1891
1891
1892 if date == 'now' or date == _('now'):
1892 if date == 'now' or date == _('now'):
1893 return makedate()
1893 return makedate()
1894 if date == 'today' or date == _('today'):
1894 if date == 'today' or date == _('today'):
1895 date = datetime.date.today().strftime('%b %d')
1895 date = datetime.date.today().strftime('%b %d')
1896 elif date == 'yesterday' or date == _('yesterday'):
1896 elif date == 'yesterday' or date == _('yesterday'):
1897 date = (datetime.date.today() -
1897 date = (datetime.date.today() -
1898 datetime.timedelta(days=1)).strftime('%b %d')
1898 datetime.timedelta(days=1)).strftime('%b %d')
1899
1899
1900 try:
1900 try:
1901 when, offset = map(int, date.split(' '))
1901 when, offset = map(int, date.split(' '))
1902 except ValueError:
1902 except ValueError:
1903 # fill out defaults
1903 # fill out defaults
1904 now = makedate()
1904 now = makedate()
1905 defaults = {}
1905 defaults = {}
1906 for part in ("d", "mb", "yY", "HI", "M", "S"):
1906 for part in ("d", "mb", "yY", "HI", "M", "S"):
1907 # this piece is for rounding the specific end of unknowns
1907 # this piece is for rounding the specific end of unknowns
1908 b = bias.get(part)
1908 b = bias.get(part)
1909 if b is None:
1909 if b is None:
1910 if part[0] in "HMS":
1910 if part[0] in "HMS":
1911 b = "00"
1911 b = "00"
1912 else:
1912 else:
1913 b = "0"
1913 b = "0"
1914
1914
1915 # this piece is for matching the generic end to today's date
1915 # this piece is for matching the generic end to today's date
1916 n = datestr(now, "%" + part[0])
1916 n = datestr(now, "%" + part[0])
1917
1917
1918 defaults[part] = (b, n)
1918 defaults[part] = (b, n)
1919
1919
1920 for format in formats:
1920 for format in formats:
1921 try:
1921 try:
1922 when, offset = strdate(date, format, defaults)
1922 when, offset = strdate(date, format, defaults)
1923 except (ValueError, OverflowError):
1923 except (ValueError, OverflowError):
1924 pass
1924 pass
1925 else:
1925 else:
1926 break
1926 break
1927 else:
1927 else:
1928 raise Abort(_('invalid date: %r') % date)
1928 raise Abort(_('invalid date: %r') % date)
1929 # validate explicit (probably user-specified) date and
1929 # validate explicit (probably user-specified) date and
1930 # time zone offset. values must fit in signed 32 bits for
1930 # time zone offset. values must fit in signed 32 bits for
1931 # current 32-bit linux runtimes. timezones go from UTC-12
1931 # current 32-bit linux runtimes. timezones go from UTC-12
1932 # to UTC+14
1932 # to UTC+14
1933 if when < -0x80000000 or when > 0x7fffffff:
1933 if when < -0x80000000 or when > 0x7fffffff:
1934 raise Abort(_('date exceeds 32 bits: %d') % when)
1934 raise Abort(_('date exceeds 32 bits: %d') % when)
1935 if offset < -50400 or offset > 43200:
1935 if offset < -50400 or offset > 43200:
1936 raise Abort(_('impossible time zone offset: %d') % offset)
1936 raise Abort(_('impossible time zone offset: %d') % offset)
1937 return when, offset
1937 return when, offset
1938
1938
1939 def matchdate(date):
1939 def matchdate(date):
1940 """Return a function that matches a given date match specifier
1940 """Return a function that matches a given date match specifier
1941
1941
1942 Formats include:
1942 Formats include:
1943
1943
1944 '{date}' match a given date to the accuracy provided
1944 '{date}' match a given date to the accuracy provided
1945
1945
1946 '<{date}' on or before a given date
1946 '<{date}' on or before a given date
1947
1947
1948 '>{date}' on or after a given date
1948 '>{date}' on or after a given date
1949
1949
1950 >>> p1 = parsedate("10:29:59")
1950 >>> p1 = parsedate("10:29:59")
1951 >>> p2 = parsedate("10:30:00")
1951 >>> p2 = parsedate("10:30:00")
1952 >>> p3 = parsedate("10:30:59")
1952 >>> p3 = parsedate("10:30:59")
1953 >>> p4 = parsedate("10:31:00")
1953 >>> p4 = parsedate("10:31:00")
1954 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1954 >>> p5 = parsedate("Sep 15 10:30:00 1999")
1955 >>> f = matchdate("10:30")
1955 >>> f = matchdate("10:30")
1956 >>> f(p1[0])
1956 >>> f(p1[0])
1957 False
1957 False
1958 >>> f(p2[0])
1958 >>> f(p2[0])
1959 True
1959 True
1960 >>> f(p3[0])
1960 >>> f(p3[0])
1961 True
1961 True
1962 >>> f(p4[0])
1962 >>> f(p4[0])
1963 False
1963 False
1964 >>> f(p5[0])
1964 >>> f(p5[0])
1965 False
1965 False
1966 """
1966 """
1967
1967
1968 def lower(date):
1968 def lower(date):
1969 d = {'mb': "1", 'd': "1"}
1969 d = {'mb': "1", 'd': "1"}
1970 return parsedate(date, extendeddateformats, d)[0]
1970 return parsedate(date, extendeddateformats, d)[0]
1971
1971
1972 def upper(date):
1972 def upper(date):
1973 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1973 d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"}
1974 for days in ("31", "30", "29"):
1974 for days in ("31", "30", "29"):
1975 try:
1975 try:
1976 d["d"] = days
1976 d["d"] = days
1977 return parsedate(date, extendeddateformats, d)[0]
1977 return parsedate(date, extendeddateformats, d)[0]
1978 except Abort:
1978 except Abort:
1979 pass
1979 pass
1980 d["d"] = "28"
1980 d["d"] = "28"
1981 return parsedate(date, extendeddateformats, d)[0]
1981 return parsedate(date, extendeddateformats, d)[0]
1982
1982
1983 date = date.strip()
1983 date = date.strip()
1984
1984
1985 if not date:
1985 if not date:
1986 raise Abort(_("dates cannot consist entirely of whitespace"))
1986 raise Abort(_("dates cannot consist entirely of whitespace"))
1987 elif date[0] == "<":
1987 elif date[0] == "<":
1988 if not date[1:]:
1988 if not date[1:]:
1989 raise Abort(_("invalid day spec, use '<DATE'"))
1989 raise Abort(_("invalid day spec, use '<DATE'"))
1990 when = upper(date[1:])
1990 when = upper(date[1:])
1991 return lambda x: x <= when
1991 return lambda x: x <= when
1992 elif date[0] == ">":
1992 elif date[0] == ">":
1993 if not date[1:]:
1993 if not date[1:]:
1994 raise Abort(_("invalid day spec, use '>DATE'"))
1994 raise Abort(_("invalid day spec, use '>DATE'"))
1995 when = lower(date[1:])
1995 when = lower(date[1:])
1996 return lambda x: x >= when
1996 return lambda x: x >= when
1997 elif date[0] == "-":
1997 elif date[0] == "-":
1998 try:
1998 try:
1999 days = int(date[1:])
1999 days = int(date[1:])
2000 except ValueError:
2000 except ValueError:
2001 raise Abort(_("invalid day spec: %s") % date[1:])
2001 raise Abort(_("invalid day spec: %s") % date[1:])
2002 if days < 0:
2002 if days < 0:
2003 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
2003 raise Abort(_("%s must be nonnegative (see 'hg help dates')")
2004 % date[1:])
2004 % date[1:])
2005 when = makedate()[0] - days * 3600 * 24
2005 when = makedate()[0] - days * 3600 * 24
2006 return lambda x: x >= when
2006 return lambda x: x >= when
2007 elif " to " in date:
2007 elif " to " in date:
2008 a, b = date.split(" to ")
2008 a, b = date.split(" to ")
2009 start, stop = lower(a), upper(b)
2009 start, stop = lower(a), upper(b)
2010 return lambda x: x >= start and x <= stop
2010 return lambda x: x >= start and x <= stop
2011 else:
2011 else:
2012 start, stop = lower(date), upper(date)
2012 start, stop = lower(date), upper(date)
2013 return lambda x: x >= start and x <= stop
2013 return lambda x: x >= start and x <= stop
2014
2014
2015 def stringmatcher(pattern, casesensitive=True):
2015 def stringmatcher(pattern, casesensitive=True):
2016 """
2016 """
2017 accepts a string, possibly starting with 're:' or 'literal:' prefix.
2017 accepts a string, possibly starting with 're:' or 'literal:' prefix.
2018 returns the matcher name, pattern, and matcher function.
2018 returns the matcher name, pattern, and matcher function.
2019 missing or unknown prefixes are treated as literal matches.
2019 missing or unknown prefixes are treated as literal matches.
2020
2020
2021 helper for tests:
2021 helper for tests:
2022 >>> def test(pattern, *tests):
2022 >>> def test(pattern, *tests):
2023 ... kind, pattern, matcher = stringmatcher(pattern)
2023 ... kind, pattern, matcher = stringmatcher(pattern)
2024 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
2024 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
2025 >>> def itest(pattern, *tests):
2025 >>> def itest(pattern, *tests):
2026 ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False)
2026 ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False)
2027 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
2027 ... return (kind, pattern, [bool(matcher(t)) for t in tests])
2028
2028
2029 exact matching (no prefix):
2029 exact matching (no prefix):
2030 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
2030 >>> test('abcdefg', 'abc', 'def', 'abcdefg')
2031 ('literal', 'abcdefg', [False, False, True])
2031 ('literal', 'abcdefg', [False, False, True])
2032
2032
2033 regex matching ('re:' prefix)
2033 regex matching ('re:' prefix)
2034 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
2034 >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar')
2035 ('re', 'a.+b', [False, False, True])
2035 ('re', 'a.+b', [False, False, True])
2036
2036
2037 force exact matches ('literal:' prefix)
2037 force exact matches ('literal:' prefix)
2038 >>> test('literal:re:foobar', 'foobar', 're:foobar')
2038 >>> test('literal:re:foobar', 'foobar', 're:foobar')
2039 ('literal', 're:foobar', [False, True])
2039 ('literal', 're:foobar', [False, True])
2040
2040
2041 unknown prefixes are ignored and treated as literals
2041 unknown prefixes are ignored and treated as literals
2042 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
2042 >>> test('foo:bar', 'foo', 'bar', 'foo:bar')
2043 ('literal', 'foo:bar', [False, False, True])
2043 ('literal', 'foo:bar', [False, False, True])
2044
2044
2045 case insensitive regex matches
2045 case insensitive regex matches
2046 >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar')
2046 >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar')
2047 ('re', 'A.+b', [False, False, True])
2047 ('re', 'A.+b', [False, False, True])
2048
2048
2049 case insensitive literal matches
2049 case insensitive literal matches
2050 >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg')
2050 >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg')
2051 ('literal', 'ABCDEFG', [False, False, True])
2051 ('literal', 'ABCDEFG', [False, False, True])
2052 """
2052 """
2053 if pattern.startswith('re:'):
2053 if pattern.startswith('re:'):
2054 pattern = pattern[3:]
2054 pattern = pattern[3:]
2055 try:
2055 try:
2056 flags = 0
2056 flags = 0
2057 if not casesensitive:
2057 if not casesensitive:
2058 flags = remod.I
2058 flags = remod.I
2059 regex = remod.compile(pattern, flags)
2059 regex = remod.compile(pattern, flags)
2060 except remod.error as e:
2060 except remod.error as e:
2061 raise error.ParseError(_('invalid regular expression: %s')
2061 raise error.ParseError(_('invalid regular expression: %s')
2062 % e)
2062 % e)
2063 return 're', pattern, regex.search
2063 return 're', pattern, regex.search
2064 elif pattern.startswith('literal:'):
2064 elif pattern.startswith('literal:'):
2065 pattern = pattern[8:]
2065 pattern = pattern[8:]
2066
2066
2067 match = pattern.__eq__
2067 match = pattern.__eq__
2068
2068
2069 if not casesensitive:
2069 if not casesensitive:
2070 ipat = encoding.lower(pattern)
2070 ipat = encoding.lower(pattern)
2071 match = lambda s: ipat == encoding.lower(s)
2071 match = lambda s: ipat == encoding.lower(s)
2072 return 'literal', pattern, match
2072 return 'literal', pattern, match
2073
2073
2074 def shortuser(user):
2074 def shortuser(user):
2075 """Return a short representation of a user name or email address."""
2075 """Return a short representation of a user name or email address."""
2076 f = user.find('@')
2076 f = user.find('@')
2077 if f >= 0:
2077 if f >= 0:
2078 user = user[:f]
2078 user = user[:f]
2079 f = user.find('<')
2079 f = user.find('<')
2080 if f >= 0:
2080 if f >= 0:
2081 user = user[f + 1:]
2081 user = user[f + 1:]
2082 f = user.find(' ')
2082 f = user.find(' ')
2083 if f >= 0:
2083 if f >= 0:
2084 user = user[:f]
2084 user = user[:f]
2085 f = user.find('.')
2085 f = user.find('.')
2086 if f >= 0:
2086 if f >= 0:
2087 user = user[:f]
2087 user = user[:f]
2088 return user
2088 return user
2089
2089
2090 def emailuser(user):
2090 def emailuser(user):
2091 """Return the user portion of an email address."""
2091 """Return the user portion of an email address."""
2092 f = user.find('@')
2092 f = user.find('@')
2093 if f >= 0:
2093 if f >= 0:
2094 user = user[:f]
2094 user = user[:f]
2095 f = user.find('<')
2095 f = user.find('<')
2096 if f >= 0:
2096 if f >= 0:
2097 user = user[f + 1:]
2097 user = user[f + 1:]
2098 return user
2098 return user
2099
2099
2100 def email(author):
2100 def email(author):
2101 '''get email of author.'''
2101 '''get email of author.'''
2102 r = author.find('>')
2102 r = author.find('>')
2103 if r == -1:
2103 if r == -1:
2104 r = None
2104 r = None
2105 return author[author.find('<') + 1:r]
2105 return author[author.find('<') + 1:r]
2106
2106
2107 def ellipsis(text, maxlength=400):
2107 def ellipsis(text, maxlength=400):
2108 """Trim string to at most maxlength (default: 400) columns in display."""
2108 """Trim string to at most maxlength (default: 400) columns in display."""
2109 return encoding.trim(text, maxlength, ellipsis='...')
2109 return encoding.trim(text, maxlength, ellipsis='...')
2110
2110
2111 def unitcountfn(*unittable):
2111 def unitcountfn(*unittable):
2112 '''return a function that renders a readable count of some quantity'''
2112 '''return a function that renders a readable count of some quantity'''
2113
2113
2114 def go(count):
2114 def go(count):
2115 for multiplier, divisor, format in unittable:
2115 for multiplier, divisor, format in unittable:
2116 if count >= divisor * multiplier:
2116 if count >= divisor * multiplier:
2117 return format % (count / float(divisor))
2117 return format % (count / float(divisor))
2118 return unittable[-1][2] % count
2118 return unittable[-1][2] % count
2119
2119
2120 return go
2120 return go
2121
2121
2122 bytecount = unitcountfn(
2122 bytecount = unitcountfn(
2123 (100, 1 << 30, _('%.0f GB')),
2123 (100, 1 << 30, _('%.0f GB')),
2124 (10, 1 << 30, _('%.1f GB')),
2124 (10, 1 << 30, _('%.1f GB')),
2125 (1, 1 << 30, _('%.2f GB')),
2125 (1, 1 << 30, _('%.2f GB')),
2126 (100, 1 << 20, _('%.0f MB')),
2126 (100, 1 << 20, _('%.0f MB')),
2127 (10, 1 << 20, _('%.1f MB')),
2127 (10, 1 << 20, _('%.1f MB')),
2128 (1, 1 << 20, _('%.2f MB')),
2128 (1, 1 << 20, _('%.2f MB')),
2129 (100, 1 << 10, _('%.0f KB')),
2129 (100, 1 << 10, _('%.0f KB')),
2130 (10, 1 << 10, _('%.1f KB')),
2130 (10, 1 << 10, _('%.1f KB')),
2131 (1, 1 << 10, _('%.2f KB')),
2131 (1, 1 << 10, _('%.2f KB')),
2132 (1, 1, _('%.0f bytes')),
2132 (1, 1, _('%.0f bytes')),
2133 )
2133 )
2134
2134
2135 def uirepr(s):
2135 def uirepr(s):
2136 # Avoid double backslash in Windows path repr()
2136 # Avoid double backslash in Windows path repr()
2137 return repr(s).replace('\\\\', '\\')
2137 return repr(s).replace('\\\\', '\\')
2138
2138
2139 # delay import of textwrap
2139 # delay import of textwrap
2140 def MBTextWrapper(**kwargs):
2140 def MBTextWrapper(**kwargs):
2141 class tw(textwrap.TextWrapper):
2141 class tw(textwrap.TextWrapper):
2142 """
2142 """
2143 Extend TextWrapper for width-awareness.
2143 Extend TextWrapper for width-awareness.
2144
2144
2145 Neither number of 'bytes' in any encoding nor 'characters' is
2145 Neither number of 'bytes' in any encoding nor 'characters' is
2146 appropriate to calculate terminal columns for specified string.
2146 appropriate to calculate terminal columns for specified string.
2147
2147
2148 Original TextWrapper implementation uses built-in 'len()' directly,
2148 Original TextWrapper implementation uses built-in 'len()' directly,
2149 so overriding is needed to use width information of each characters.
2149 so overriding is needed to use width information of each characters.
2150
2150
2151 In addition, characters classified into 'ambiguous' width are
2151 In addition, characters classified into 'ambiguous' width are
2152 treated as wide in East Asian area, but as narrow in other.
2152 treated as wide in East Asian area, but as narrow in other.
2153
2153
2154 This requires use decision to determine width of such characters.
2154 This requires use decision to determine width of such characters.
2155 """
2155 """
2156 def _cutdown(self, ucstr, space_left):
2156 def _cutdown(self, ucstr, space_left):
2157 l = 0
2157 l = 0
2158 colwidth = encoding.ucolwidth
2158 colwidth = encoding.ucolwidth
2159 for i in xrange(len(ucstr)):
2159 for i in xrange(len(ucstr)):
2160 l += colwidth(ucstr[i])
2160 l += colwidth(ucstr[i])
2161 if space_left < l:
2161 if space_left < l:
2162 return (ucstr[:i], ucstr[i:])
2162 return (ucstr[:i], ucstr[i:])
2163 return ucstr, ''
2163 return ucstr, ''
2164
2164
2165 # overriding of base class
2165 # overriding of base class
2166 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2166 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
2167 space_left = max(width - cur_len, 1)
2167 space_left = max(width - cur_len, 1)
2168
2168
2169 if self.break_long_words:
2169 if self.break_long_words:
2170 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2170 cut, res = self._cutdown(reversed_chunks[-1], space_left)
2171 cur_line.append(cut)
2171 cur_line.append(cut)
2172 reversed_chunks[-1] = res
2172 reversed_chunks[-1] = res
2173 elif not cur_line:
2173 elif not cur_line:
2174 cur_line.append(reversed_chunks.pop())
2174 cur_line.append(reversed_chunks.pop())
2175
2175
2176 # this overriding code is imported from TextWrapper of Python 2.6
2176 # this overriding code is imported from TextWrapper of Python 2.6
2177 # to calculate columns of string by 'encoding.ucolwidth()'
2177 # to calculate columns of string by 'encoding.ucolwidth()'
2178 def _wrap_chunks(self, chunks):
2178 def _wrap_chunks(self, chunks):
2179 colwidth = encoding.ucolwidth
2179 colwidth = encoding.ucolwidth
2180
2180
2181 lines = []
2181 lines = []
2182 if self.width <= 0:
2182 if self.width <= 0:
2183 raise ValueError("invalid width %r (must be > 0)" % self.width)
2183 raise ValueError("invalid width %r (must be > 0)" % self.width)
2184
2184
2185 # Arrange in reverse order so items can be efficiently popped
2185 # Arrange in reverse order so items can be efficiently popped
2186 # from a stack of chucks.
2186 # from a stack of chucks.
2187 chunks.reverse()
2187 chunks.reverse()
2188
2188
2189 while chunks:
2189 while chunks:
2190
2190
2191 # Start the list of chunks that will make up the current line.
2191 # Start the list of chunks that will make up the current line.
2192 # cur_len is just the length of all the chunks in cur_line.
2192 # cur_len is just the length of all the chunks in cur_line.
2193 cur_line = []
2193 cur_line = []
2194 cur_len = 0
2194 cur_len = 0
2195
2195
2196 # Figure out which static string will prefix this line.
2196 # Figure out which static string will prefix this line.
2197 if lines:
2197 if lines:
2198 indent = self.subsequent_indent
2198 indent = self.subsequent_indent
2199 else:
2199 else:
2200 indent = self.initial_indent
2200 indent = self.initial_indent
2201
2201
2202 # Maximum width for this line.
2202 # Maximum width for this line.
2203 width = self.width - len(indent)
2203 width = self.width - len(indent)
2204
2204
2205 # First chunk on line is whitespace -- drop it, unless this
2205 # First chunk on line is whitespace -- drop it, unless this
2206 # is the very beginning of the text (i.e. no lines started yet).
2206 # is the very beginning of the text (i.e. no lines started yet).
2207 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2207 if self.drop_whitespace and chunks[-1].strip() == '' and lines:
2208 del chunks[-1]
2208 del chunks[-1]
2209
2209
2210 while chunks:
2210 while chunks:
2211 l = colwidth(chunks[-1])
2211 l = colwidth(chunks[-1])
2212
2212
2213 # Can at least squeeze this chunk onto the current line.
2213 # Can at least squeeze this chunk onto the current line.
2214 if cur_len + l <= width:
2214 if cur_len + l <= width:
2215 cur_line.append(chunks.pop())
2215 cur_line.append(chunks.pop())
2216 cur_len += l
2216 cur_len += l
2217
2217
2218 # Nope, this line is full.
2218 # Nope, this line is full.
2219 else:
2219 else:
2220 break
2220 break
2221
2221
2222 # The current line is full, and the next chunk is too big to
2222 # The current line is full, and the next chunk is too big to
2223 # fit on *any* line (not just this one).
2223 # fit on *any* line (not just this one).
2224 if chunks and colwidth(chunks[-1]) > width:
2224 if chunks and colwidth(chunks[-1]) > width:
2225 self._handle_long_word(chunks, cur_line, cur_len, width)
2225 self._handle_long_word(chunks, cur_line, cur_len, width)
2226
2226
2227 # If the last chunk on this line is all whitespace, drop it.
2227 # If the last chunk on this line is all whitespace, drop it.
2228 if (self.drop_whitespace and
2228 if (self.drop_whitespace and
2229 cur_line and cur_line[-1].strip() == ''):
2229 cur_line and cur_line[-1].strip() == ''):
2230 del cur_line[-1]
2230 del cur_line[-1]
2231
2231
2232 # Convert current line back to a string and store it in list
2232 # Convert current line back to a string and store it in list
2233 # of all lines (return value).
2233 # of all lines (return value).
2234 if cur_line:
2234 if cur_line:
2235 lines.append(indent + ''.join(cur_line))
2235 lines.append(indent + ''.join(cur_line))
2236
2236
2237 return lines
2237 return lines
2238
2238
2239 global MBTextWrapper
2239 global MBTextWrapper
2240 MBTextWrapper = tw
2240 MBTextWrapper = tw
2241 return tw(**kwargs)
2241 return tw(**kwargs)
2242
2242
2243 def wrap(line, width, initindent='', hangindent=''):
2243 def wrap(line, width, initindent='', hangindent=''):
2244 maxindent = max(len(hangindent), len(initindent))
2244 maxindent = max(len(hangindent), len(initindent))
2245 if width <= maxindent:
2245 if width <= maxindent:
2246 # adjust for weird terminal size
2246 # adjust for weird terminal size
2247 width = max(78, maxindent + 1)
2247 width = max(78, maxindent + 1)
2248 line = line.decode(encoding.encoding, encoding.encodingmode)
2248 line = line.decode(encoding.encoding, encoding.encodingmode)
2249 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2249 initindent = initindent.decode(encoding.encoding, encoding.encodingmode)
2250 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2250 hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode)
2251 wrapper = MBTextWrapper(width=width,
2251 wrapper = MBTextWrapper(width=width,
2252 initial_indent=initindent,
2252 initial_indent=initindent,
2253 subsequent_indent=hangindent)
2253 subsequent_indent=hangindent)
2254 return wrapper.fill(line).encode(encoding.encoding)
2254 return wrapper.fill(line).encode(encoding.encoding)
2255
2255
2256 if (pyplatform.python_implementation() == 'CPython' and
2256 if (pyplatform.python_implementation() == 'CPython' and
2257 sys.version_info < (3, 0)):
2257 sys.version_info < (3, 0)):
2258 # There is an issue in CPython that some IO methods do not handle EINTR
2258 # There is an issue in CPython that some IO methods do not handle EINTR
2259 # correctly. The following table shows what CPython version (and functions)
2259 # correctly. The following table shows what CPython version (and functions)
2260 # are affected (buggy: has the EINTR bug, okay: otherwise):
2260 # are affected (buggy: has the EINTR bug, okay: otherwise):
2261 #
2261 #
2262 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2262 # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0
2263 # --------------------------------------------------
2263 # --------------------------------------------------
2264 # fp.__iter__ | buggy | buggy | okay
2264 # fp.__iter__ | buggy | buggy | okay
2265 # fp.read* | buggy | okay [1] | okay
2265 # fp.read* | buggy | okay [1] | okay
2266 #
2266 #
2267 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2267 # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo.
2268 #
2268 #
2269 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2269 # Here we workaround the EINTR issue for fileobj.__iter__. Other methods
2270 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2270 # like "read*" are ignored for now, as Python < 2.7.4 is a minority.
2271 #
2271 #
2272 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2272 # Although we can workaround the EINTR issue for fp.__iter__, it is slower:
2273 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2273 # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in
2274 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2274 # CPython 2, because CPython 2 maintains an internal readahead buffer for
2275 # fp.__iter__ but not other fp.read* methods.
2275 # fp.__iter__ but not other fp.read* methods.
2276 #
2276 #
2277 # On modern systems like Linux, the "read" syscall cannot be interrupted
2277 # On modern systems like Linux, the "read" syscall cannot be interrupted
2278 # when reading "fast" files like on-disk files. So the EINTR issue only
2278 # when reading "fast" files like on-disk files. So the EINTR issue only
2279 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2279 # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG)
2280 # files approximately as "fast" files and use the fast (unsafe) code path,
2280 # files approximately as "fast" files and use the fast (unsafe) code path,
2281 # to minimize the performance impact.
2281 # to minimize the performance impact.
2282 if sys.version_info >= (2, 7, 4):
2282 if sys.version_info >= (2, 7, 4):
2283 # fp.readline deals with EINTR correctly, use it as a workaround.
2283 # fp.readline deals with EINTR correctly, use it as a workaround.
2284 def _safeiterfile(fp):
2284 def _safeiterfile(fp):
2285 return iter(fp.readline, '')
2285 return iter(fp.readline, '')
2286 else:
2286 else:
2287 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2287 # fp.read* are broken too, manually deal with EINTR in a stupid way.
2288 # note: this may block longer than necessary because of bufsize.
2288 # note: this may block longer than necessary because of bufsize.
2289 def _safeiterfile(fp, bufsize=4096):
2289 def _safeiterfile(fp, bufsize=4096):
2290 fd = fp.fileno()
2290 fd = fp.fileno()
2291 line = ''
2291 line = ''
2292 while True:
2292 while True:
2293 try:
2293 try:
2294 buf = os.read(fd, bufsize)
2294 buf = os.read(fd, bufsize)
2295 except OSError as ex:
2295 except OSError as ex:
2296 # os.read only raises EINTR before any data is read
2296 # os.read only raises EINTR before any data is read
2297 if ex.errno == errno.EINTR:
2297 if ex.errno == errno.EINTR:
2298 continue
2298 continue
2299 else:
2299 else:
2300 raise
2300 raise
2301 line += buf
2301 line += buf
2302 if '\n' in buf:
2302 if '\n' in buf:
2303 splitted = line.splitlines(True)
2303 splitted = line.splitlines(True)
2304 line = ''
2304 line = ''
2305 for l in splitted:
2305 for l in splitted:
2306 if l[-1] == '\n':
2306 if l[-1] == '\n':
2307 yield l
2307 yield l
2308 else:
2308 else:
2309 line = l
2309 line = l
2310 if not buf:
2310 if not buf:
2311 break
2311 break
2312 if line:
2312 if line:
2313 yield line
2313 yield line
2314
2314
2315 def iterfile(fp):
2315 def iterfile(fp):
2316 fastpath = True
2316 fastpath = True
2317 if type(fp) is file:
2317 if type(fp) is file:
2318 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2318 fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode)
2319 if fastpath:
2319 if fastpath:
2320 return fp
2320 return fp
2321 else:
2321 else:
2322 return _safeiterfile(fp)
2322 return _safeiterfile(fp)
2323 else:
2323 else:
2324 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2324 # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed.
2325 def iterfile(fp):
2325 def iterfile(fp):
2326 return fp
2326 return fp
2327
2327
2328 def iterlines(iterator):
2328 def iterlines(iterator):
2329 for chunk in iterator:
2329 for chunk in iterator:
2330 for line in chunk.splitlines():
2330 for line in chunk.splitlines():
2331 yield line
2331 yield line
2332
2332
2333 def expandpath(path):
2333 def expandpath(path):
2334 return os.path.expanduser(os.path.expandvars(path))
2334 return os.path.expanduser(os.path.expandvars(path))
2335
2335
2336 def hgcmd():
2336 def hgcmd():
2337 """Return the command used to execute current hg
2337 """Return the command used to execute current hg
2338
2338
2339 This is different from hgexecutable() because on Windows we want
2339 This is different from hgexecutable() because on Windows we want
2340 to avoid things opening new shell windows like batch files, so we
2340 to avoid things opening new shell windows like batch files, so we
2341 get either the python call or current executable.
2341 get either the python call or current executable.
2342 """
2342 """
2343 if mainfrozen():
2343 if mainfrozen():
2344 if getattr(sys, 'frozen', None) == 'macosx_app':
2344 if getattr(sys, 'frozen', None) == 'macosx_app':
2345 # Env variable set by py2app
2345 # Env variable set by py2app
2346 return [encoding.environ['EXECUTABLEPATH']]
2346 return [encoding.environ['EXECUTABLEPATH']]
2347 else:
2347 else:
2348 return [pycompat.sysexecutable]
2348 return [pycompat.sysexecutable]
2349 return gethgcmd()
2349 return gethgcmd()
2350
2350
2351 def rundetached(args, condfn):
2351 def rundetached(args, condfn):
2352 """Execute the argument list in a detached process.
2352 """Execute the argument list in a detached process.
2353
2353
2354 condfn is a callable which is called repeatedly and should return
2354 condfn is a callable which is called repeatedly and should return
2355 True once the child process is known to have started successfully.
2355 True once the child process is known to have started successfully.
2356 At this point, the child process PID is returned. If the child
2356 At this point, the child process PID is returned. If the child
2357 process fails to start or finishes before condfn() evaluates to
2357 process fails to start or finishes before condfn() evaluates to
2358 True, return -1.
2358 True, return -1.
2359 """
2359 """
2360 # Windows case is easier because the child process is either
2360 # Windows case is easier because the child process is either
2361 # successfully starting and validating the condition or exiting
2361 # successfully starting and validating the condition or exiting
2362 # on failure. We just poll on its PID. On Unix, if the child
2362 # on failure. We just poll on its PID. On Unix, if the child
2363 # process fails to start, it will be left in a zombie state until
2363 # process fails to start, it will be left in a zombie state until
2364 # the parent wait on it, which we cannot do since we expect a long
2364 # the parent wait on it, which we cannot do since we expect a long
2365 # running process on success. Instead we listen for SIGCHLD telling
2365 # running process on success. Instead we listen for SIGCHLD telling
2366 # us our child process terminated.
2366 # us our child process terminated.
2367 terminated = set()
2367 terminated = set()
2368 def handler(signum, frame):
2368 def handler(signum, frame):
2369 terminated.add(os.wait())
2369 terminated.add(os.wait())
2370 prevhandler = None
2370 prevhandler = None
2371 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2371 SIGCHLD = getattr(signal, 'SIGCHLD', None)
2372 if SIGCHLD is not None:
2372 if SIGCHLD is not None:
2373 prevhandler = signal.signal(SIGCHLD, handler)
2373 prevhandler = signal.signal(SIGCHLD, handler)
2374 try:
2374 try:
2375 pid = spawndetached(args)
2375 pid = spawndetached(args)
2376 while not condfn():
2376 while not condfn():
2377 if ((pid in terminated or not testpid(pid))
2377 if ((pid in terminated or not testpid(pid))
2378 and not condfn()):
2378 and not condfn()):
2379 return -1
2379 return -1
2380 time.sleep(0.1)
2380 time.sleep(0.1)
2381 return pid
2381 return pid
2382 finally:
2382 finally:
2383 if prevhandler is not None:
2383 if prevhandler is not None:
2384 signal.signal(signal.SIGCHLD, prevhandler)
2384 signal.signal(signal.SIGCHLD, prevhandler)
2385
2385
2386 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2386 def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
2387 """Return the result of interpolating items in the mapping into string s.
2387 """Return the result of interpolating items in the mapping into string s.
2388
2388
2389 prefix is a single character string, or a two character string with
2389 prefix is a single character string, or a two character string with
2390 a backslash as the first character if the prefix needs to be escaped in
2390 a backslash as the first character if the prefix needs to be escaped in
2391 a regular expression.
2391 a regular expression.
2392
2392
2393 fn is an optional function that will be applied to the replacement text
2393 fn is an optional function that will be applied to the replacement text
2394 just before replacement.
2394 just before replacement.
2395
2395
2396 escape_prefix is an optional flag that allows using doubled prefix for
2396 escape_prefix is an optional flag that allows using doubled prefix for
2397 its escaping.
2397 its escaping.
2398 """
2398 """
2399 fn = fn or (lambda s: s)
2399 fn = fn or (lambda s: s)
2400 patterns = '|'.join(mapping.keys())
2400 patterns = '|'.join(mapping.keys())
2401 if escape_prefix:
2401 if escape_prefix:
2402 patterns += '|' + prefix
2402 patterns += '|' + prefix
2403 if len(prefix) > 1:
2403 if len(prefix) > 1:
2404 prefix_char = prefix[1:]
2404 prefix_char = prefix[1:]
2405 else:
2405 else:
2406 prefix_char = prefix
2406 prefix_char = prefix
2407 mapping[prefix_char] = prefix_char
2407 mapping[prefix_char] = prefix_char
2408 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2408 r = remod.compile(r'%s(%s)' % (prefix, patterns))
2409 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2409 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
2410
2410
2411 def getport(port):
2411 def getport(port):
2412 """Return the port for a given network service.
2412 """Return the port for a given network service.
2413
2413
2414 If port is an integer, it's returned as is. If it's a string, it's
2414 If port is an integer, it's returned as is. If it's a string, it's
2415 looked up using socket.getservbyname(). If there's no matching
2415 looked up using socket.getservbyname(). If there's no matching
2416 service, error.Abort is raised.
2416 service, error.Abort is raised.
2417 """
2417 """
2418 try:
2418 try:
2419 return int(port)
2419 return int(port)
2420 except ValueError:
2420 except ValueError:
2421 pass
2421 pass
2422
2422
2423 try:
2423 try:
2424 return socket.getservbyname(port)
2424 return socket.getservbyname(port)
2425 except socket.error:
2425 except socket.error:
2426 raise Abort(_("no port number associated with service '%s'") % port)
2426 raise Abort(_("no port number associated with service '%s'") % port)
2427
2427
2428 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2428 _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True,
2429 '0': False, 'no': False, 'false': False, 'off': False,
2429 '0': False, 'no': False, 'false': False, 'off': False,
2430 'never': False}
2430 'never': False}
2431
2431
2432 def parsebool(s):
2432 def parsebool(s):
2433 """Parse s into a boolean.
2433 """Parse s into a boolean.
2434
2434
2435 If s is not a valid boolean, returns None.
2435 If s is not a valid boolean, returns None.
2436 """
2436 """
2437 return _booleans.get(s.lower(), None)
2437 return _booleans.get(s.lower(), None)
2438
2438
2439 _hextochr = dict((a + b, chr(int(a + b, 16)))
2439 _hextochr = dict((a + b, chr(int(a + b, 16)))
2440 for a in string.hexdigits for b in string.hexdigits)
2440 for a in string.hexdigits for b in string.hexdigits)
2441
2441
2442 class url(object):
2442 class url(object):
2443 r"""Reliable URL parser.
2443 r"""Reliable URL parser.
2444
2444
2445 This parses URLs and provides attributes for the following
2445 This parses URLs and provides attributes for the following
2446 components:
2446 components:
2447
2447
2448 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2448 <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment>
2449
2449
2450 Missing components are set to None. The only exception is
2450 Missing components are set to None. The only exception is
2451 fragment, which is set to '' if present but empty.
2451 fragment, which is set to '' if present but empty.
2452
2452
2453 If parsefragment is False, fragment is included in query. If
2453 If parsefragment is False, fragment is included in query. If
2454 parsequery is False, query is included in path. If both are
2454 parsequery is False, query is included in path. If both are
2455 False, both fragment and query are included in path.
2455 False, both fragment and query are included in path.
2456
2456
2457 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2457 See http://www.ietf.org/rfc/rfc2396.txt for more information.
2458
2458
2459 Note that for backward compatibility reasons, bundle URLs do not
2459 Note that for backward compatibility reasons, bundle URLs do not
2460 take host names. That means 'bundle://../' has a path of '../'.
2460 take host names. That means 'bundle://../' has a path of '../'.
2461
2461
2462 Examples:
2462 Examples:
2463
2463
2464 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2464 >>> url('http://www.ietf.org/rfc/rfc2396.txt')
2465 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2465 <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'>
2466 >>> url('ssh://[::1]:2200//home/joe/repo')
2466 >>> url('ssh://[::1]:2200//home/joe/repo')
2467 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2467 <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'>
2468 >>> url('file:///home/joe/repo')
2468 >>> url('file:///home/joe/repo')
2469 <url scheme: 'file', path: '/home/joe/repo'>
2469 <url scheme: 'file', path: '/home/joe/repo'>
2470 >>> url('file:///c:/temp/foo/')
2470 >>> url('file:///c:/temp/foo/')
2471 <url scheme: 'file', path: 'c:/temp/foo/'>
2471 <url scheme: 'file', path: 'c:/temp/foo/'>
2472 >>> url('bundle:foo')
2472 >>> url('bundle:foo')
2473 <url scheme: 'bundle', path: 'foo'>
2473 <url scheme: 'bundle', path: 'foo'>
2474 >>> url('bundle://../foo')
2474 >>> url('bundle://../foo')
2475 <url scheme: 'bundle', path: '../foo'>
2475 <url scheme: 'bundle', path: '../foo'>
2476 >>> url(r'c:\foo\bar')
2476 >>> url(r'c:\foo\bar')
2477 <url path: 'c:\\foo\\bar'>
2477 <url path: 'c:\\foo\\bar'>
2478 >>> url(r'\\blah\blah\blah')
2478 >>> url(r'\\blah\blah\blah')
2479 <url path: '\\\\blah\\blah\\blah'>
2479 <url path: '\\\\blah\\blah\\blah'>
2480 >>> url(r'\\blah\blah\blah#baz')
2480 >>> url(r'\\blah\blah\blah#baz')
2481 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2481 <url path: '\\\\blah\\blah\\blah', fragment: 'baz'>
2482 >>> url(r'file:///C:\users\me')
2482 >>> url(r'file:///C:\users\me')
2483 <url scheme: 'file', path: 'C:\\users\\me'>
2483 <url scheme: 'file', path: 'C:\\users\\me'>
2484
2484
2485 Authentication credentials:
2485 Authentication credentials:
2486
2486
2487 >>> url('ssh://joe:xyz@x/repo')
2487 >>> url('ssh://joe:xyz@x/repo')
2488 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2488 <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'>
2489 >>> url('ssh://joe@x/repo')
2489 >>> url('ssh://joe@x/repo')
2490 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2490 <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'>
2491
2491
2492 Query strings and fragments:
2492 Query strings and fragments:
2493
2493
2494 >>> url('http://host/a?b#c')
2494 >>> url('http://host/a?b#c')
2495 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2495 <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'>
2496 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2496 >>> url('http://host/a?b#c', parsequery=False, parsefragment=False)
2497 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2497 <url scheme: 'http', host: 'host', path: 'a?b#c'>
2498
2498
2499 Empty path:
2499 Empty path:
2500
2500
2501 >>> url('')
2501 >>> url('')
2502 <url path: ''>
2502 <url path: ''>
2503 >>> url('#a')
2503 >>> url('#a')
2504 <url path: '', fragment: 'a'>
2504 <url path: '', fragment: 'a'>
2505 >>> url('http://host/')
2505 >>> url('http://host/')
2506 <url scheme: 'http', host: 'host', path: ''>
2506 <url scheme: 'http', host: 'host', path: ''>
2507 >>> url('http://host/#a')
2507 >>> url('http://host/#a')
2508 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2508 <url scheme: 'http', host: 'host', path: '', fragment: 'a'>
2509
2509
2510 Only scheme:
2510 Only scheme:
2511
2511
2512 >>> url('http:')
2512 >>> url('http:')
2513 <url scheme: 'http'>
2513 <url scheme: 'http'>
2514 """
2514 """
2515
2515
2516 _safechars = "!~*'()+"
2516 _safechars = "!~*'()+"
2517 _safepchars = "/!~*'()+:\\"
2517 _safepchars = "/!~*'()+:\\"
2518 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2518 _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match
2519
2519
2520 def __init__(self, path, parsequery=True, parsefragment=True):
2520 def __init__(self, path, parsequery=True, parsefragment=True):
2521 # We slowly chomp away at path until we have only the path left
2521 # We slowly chomp away at path until we have only the path left
2522 self.scheme = self.user = self.passwd = self.host = None
2522 self.scheme = self.user = self.passwd = self.host = None
2523 self.port = self.path = self.query = self.fragment = None
2523 self.port = self.path = self.query = self.fragment = None
2524 self._localpath = True
2524 self._localpath = True
2525 self._hostport = ''
2525 self._hostport = ''
2526 self._origpath = path
2526 self._origpath = path
2527
2527
2528 if parsefragment and '#' in path:
2528 if parsefragment and '#' in path:
2529 path, self.fragment = path.split('#', 1)
2529 path, self.fragment = path.split('#', 1)
2530
2530
2531 # special case for Windows drive letters and UNC paths
2531 # special case for Windows drive letters and UNC paths
2532 if hasdriveletter(path) or path.startswith('\\\\'):
2532 if hasdriveletter(path) or path.startswith('\\\\'):
2533 self.path = path
2533 self.path = path
2534 return
2534 return
2535
2535
2536 # For compatibility reasons, we can't handle bundle paths as
2536 # For compatibility reasons, we can't handle bundle paths as
2537 # normal URLS
2537 # normal URLS
2538 if path.startswith('bundle:'):
2538 if path.startswith('bundle:'):
2539 self.scheme = 'bundle'
2539 self.scheme = 'bundle'
2540 path = path[7:]
2540 path = path[7:]
2541 if path.startswith('//'):
2541 if path.startswith('//'):
2542 path = path[2:]
2542 path = path[2:]
2543 self.path = path
2543 self.path = path
2544 return
2544 return
2545
2545
2546 if self._matchscheme(path):
2546 if self._matchscheme(path):
2547 parts = path.split(':', 1)
2547 parts = path.split(':', 1)
2548 if parts[0]:
2548 if parts[0]:
2549 self.scheme, path = parts
2549 self.scheme, path = parts
2550 self._localpath = False
2550 self._localpath = False
2551
2551
2552 if not path:
2552 if not path:
2553 path = None
2553 path = None
2554 if self._localpath:
2554 if self._localpath:
2555 self.path = ''
2555 self.path = ''
2556 return
2556 return
2557 else:
2557 else:
2558 if self._localpath:
2558 if self._localpath:
2559 self.path = path
2559 self.path = path
2560 return
2560 return
2561
2561
2562 if parsequery and '?' in path:
2562 if parsequery and '?' in path:
2563 path, self.query = path.split('?', 1)
2563 path, self.query = path.split('?', 1)
2564 if not path:
2564 if not path:
2565 path = None
2565 path = None
2566 if not self.query:
2566 if not self.query:
2567 self.query = None
2567 self.query = None
2568
2568
2569 # // is required to specify a host/authority
2569 # // is required to specify a host/authority
2570 if path and path.startswith('//'):
2570 if path and path.startswith('//'):
2571 parts = path[2:].split('/', 1)
2571 parts = path[2:].split('/', 1)
2572 if len(parts) > 1:
2572 if len(parts) > 1:
2573 self.host, path = parts
2573 self.host, path = parts
2574 else:
2574 else:
2575 self.host = parts[0]
2575 self.host = parts[0]
2576 path = None
2576 path = None
2577 if not self.host:
2577 if not self.host:
2578 self.host = None
2578 self.host = None
2579 # path of file:///d is /d
2579 # path of file:///d is /d
2580 # path of file:///d:/ is d:/, not /d:/
2580 # path of file:///d:/ is d:/, not /d:/
2581 if path and not hasdriveletter(path):
2581 if path and not hasdriveletter(path):
2582 path = '/' + path
2582 path = '/' + path
2583
2583
2584 if self.host and '@' in self.host:
2584 if self.host and '@' in self.host:
2585 self.user, self.host = self.host.rsplit('@', 1)
2585 self.user, self.host = self.host.rsplit('@', 1)
2586 if ':' in self.user:
2586 if ':' in self.user:
2587 self.user, self.passwd = self.user.split(':', 1)
2587 self.user, self.passwd = self.user.split(':', 1)
2588 if not self.host:
2588 if not self.host:
2589 self.host = None
2589 self.host = None
2590
2590
2591 # Don't split on colons in IPv6 addresses without ports
2591 # Don't split on colons in IPv6 addresses without ports
2592 if (self.host and ':' in self.host and
2592 if (self.host and ':' in self.host and
2593 not (self.host.startswith('[') and self.host.endswith(']'))):
2593 not (self.host.startswith('[') and self.host.endswith(']'))):
2594 self._hostport = self.host
2594 self._hostport = self.host
2595 self.host, self.port = self.host.rsplit(':', 1)
2595 self.host, self.port = self.host.rsplit(':', 1)
2596 if not self.host:
2596 if not self.host:
2597 self.host = None
2597 self.host = None
2598
2598
2599 if (self.host and self.scheme == 'file' and
2599 if (self.host and self.scheme == 'file' and
2600 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2600 self.host not in ('localhost', '127.0.0.1', '[::1]')):
2601 raise Abort(_('file:// URLs can only refer to localhost'))
2601 raise Abort(_('file:// URLs can only refer to localhost'))
2602
2602
2603 self.path = path
2603 self.path = path
2604
2604
2605 # leave the query string escaped
2605 # leave the query string escaped
2606 for a in ('user', 'passwd', 'host', 'port',
2606 for a in ('user', 'passwd', 'host', 'port',
2607 'path', 'fragment'):
2607 'path', 'fragment'):
2608 v = getattr(self, a)
2608 v = getattr(self, a)
2609 if v is not None:
2609 if v is not None:
2610 setattr(self, a, pycompat.urlunquote(v))
2610 setattr(self, a, pycompat.urlunquote(v))
2611
2611
2612 def __repr__(self):
2612 def __repr__(self):
2613 attrs = []
2613 attrs = []
2614 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2614 for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path',
2615 'query', 'fragment'):
2615 'query', 'fragment'):
2616 v = getattr(self, a)
2616 v = getattr(self, a)
2617 if v is not None:
2617 if v is not None:
2618 attrs.append('%s: %r' % (a, v))
2618 attrs.append('%s: %r' % (a, v))
2619 return '<url %s>' % ', '.join(attrs)
2619 return '<url %s>' % ', '.join(attrs)
2620
2620
2621 def __str__(self):
2621 def __str__(self):
2622 r"""Join the URL's components back into a URL string.
2622 r"""Join the URL's components back into a URL string.
2623
2623
2624 Examples:
2624 Examples:
2625
2625
2626 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2626 >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar'))
2627 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2627 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar'
2628 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2628 >>> str(url('http://user:pw@host:80/?foo=bar&baz=42'))
2629 'http://user:pw@host:80/?foo=bar&baz=42'
2629 'http://user:pw@host:80/?foo=bar&baz=42'
2630 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2630 >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz'))
2631 'http://user:pw@host:80/?foo=bar%3dbaz'
2631 'http://user:pw@host:80/?foo=bar%3dbaz'
2632 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2632 >>> str(url('ssh://user:pw@[::1]:2200//home/joe#'))
2633 'ssh://user:pw@[::1]:2200//home/joe#'
2633 'ssh://user:pw@[::1]:2200//home/joe#'
2634 >>> str(url('http://localhost:80//'))
2634 >>> str(url('http://localhost:80//'))
2635 'http://localhost:80//'
2635 'http://localhost:80//'
2636 >>> str(url('http://localhost:80/'))
2636 >>> str(url('http://localhost:80/'))
2637 'http://localhost:80/'
2637 'http://localhost:80/'
2638 >>> str(url('http://localhost:80'))
2638 >>> str(url('http://localhost:80'))
2639 'http://localhost:80/'
2639 'http://localhost:80/'
2640 >>> str(url('bundle:foo'))
2640 >>> str(url('bundle:foo'))
2641 'bundle:foo'
2641 'bundle:foo'
2642 >>> str(url('bundle://../foo'))
2642 >>> str(url('bundle://../foo'))
2643 'bundle:../foo'
2643 'bundle:../foo'
2644 >>> str(url('path'))
2644 >>> str(url('path'))
2645 'path'
2645 'path'
2646 >>> str(url('file:///tmp/foo/bar'))
2646 >>> str(url('file:///tmp/foo/bar'))
2647 'file:///tmp/foo/bar'
2647 'file:///tmp/foo/bar'
2648 >>> str(url('file:///c:/tmp/foo/bar'))
2648 >>> str(url('file:///c:/tmp/foo/bar'))
2649 'file:///c:/tmp/foo/bar'
2649 'file:///c:/tmp/foo/bar'
2650 >>> print url(r'bundle:foo\bar')
2650 >>> print url(r'bundle:foo\bar')
2651 bundle:foo\bar
2651 bundle:foo\bar
2652 >>> print url(r'file:///D:\data\hg')
2652 >>> print url(r'file:///D:\data\hg')
2653 file:///D:\data\hg
2653 file:///D:\data\hg
2654 """
2654 """
2655 if self._localpath:
2655 if self._localpath:
2656 s = self.path
2656 s = self.path
2657 if self.scheme == 'bundle':
2657 if self.scheme == 'bundle':
2658 s = 'bundle:' + s
2658 s = 'bundle:' + s
2659 if self.fragment:
2659 if self.fragment:
2660 s += '#' + self.fragment
2660 s += '#' + self.fragment
2661 return s
2661 return s
2662
2662
2663 s = self.scheme + ':'
2663 s = self.scheme + ':'
2664 if self.user or self.passwd or self.host:
2664 if self.user or self.passwd or self.host:
2665 s += '//'
2665 s += '//'
2666 elif self.scheme and (not self.path or self.path.startswith('/')
2666 elif self.scheme and (not self.path or self.path.startswith('/')
2667 or hasdriveletter(self.path)):
2667 or hasdriveletter(self.path)):
2668 s += '//'
2668 s += '//'
2669 if hasdriveletter(self.path):
2669 if hasdriveletter(self.path):
2670 s += '/'
2670 s += '/'
2671 if self.user:
2671 if self.user:
2672 s += urlreq.quote(self.user, safe=self._safechars)
2672 s += urlreq.quote(self.user, safe=self._safechars)
2673 if self.passwd:
2673 if self.passwd:
2674 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2674 s += ':' + urlreq.quote(self.passwd, safe=self._safechars)
2675 if self.user or self.passwd:
2675 if self.user or self.passwd:
2676 s += '@'
2676 s += '@'
2677 if self.host:
2677 if self.host:
2678 if not (self.host.startswith('[') and self.host.endswith(']')):
2678 if not (self.host.startswith('[') and self.host.endswith(']')):
2679 s += urlreq.quote(self.host)
2679 s += urlreq.quote(self.host)
2680 else:
2680 else:
2681 s += self.host
2681 s += self.host
2682 if self.port:
2682 if self.port:
2683 s += ':' + urlreq.quote(self.port)
2683 s += ':' + urlreq.quote(self.port)
2684 if self.host:
2684 if self.host:
2685 s += '/'
2685 s += '/'
2686 if self.path:
2686 if self.path:
2687 # TODO: similar to the query string, we should not unescape the
2687 # TODO: similar to the query string, we should not unescape the
2688 # path when we store it, the path might contain '%2f' = '/',
2688 # path when we store it, the path might contain '%2f' = '/',
2689 # which we should *not* escape.
2689 # which we should *not* escape.
2690 s += urlreq.quote(self.path, safe=self._safepchars)
2690 s += urlreq.quote(self.path, safe=self._safepchars)
2691 if self.query:
2691 if self.query:
2692 # we store the query in escaped form.
2692 # we store the query in escaped form.
2693 s += '?' + self.query
2693 s += '?' + self.query
2694 if self.fragment is not None:
2694 if self.fragment is not None:
2695 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2695 s += '#' + urlreq.quote(self.fragment, safe=self._safepchars)
2696 return s
2696 return s
2697
2697
2698 def authinfo(self):
2698 def authinfo(self):
2699 user, passwd = self.user, self.passwd
2699 user, passwd = self.user, self.passwd
2700 try:
2700 try:
2701 self.user, self.passwd = None, None
2701 self.user, self.passwd = None, None
2702 s = str(self)
2702 s = str(self)
2703 finally:
2703 finally:
2704 self.user, self.passwd = user, passwd
2704 self.user, self.passwd = user, passwd
2705 if not self.user:
2705 if not self.user:
2706 return (s, None)
2706 return (s, None)
2707 # authinfo[1] is passed to urllib2 password manager, and its
2707 # authinfo[1] is passed to urllib2 password manager, and its
2708 # URIs must not contain credentials. The host is passed in the
2708 # URIs must not contain credentials. The host is passed in the
2709 # URIs list because Python < 2.4.3 uses only that to search for
2709 # URIs list because Python < 2.4.3 uses only that to search for
2710 # a password.
2710 # a password.
2711 return (s, (None, (s, self.host),
2711 return (s, (None, (s, self.host),
2712 self.user, self.passwd or ''))
2712 self.user, self.passwd or ''))
2713
2713
2714 def isabs(self):
2714 def isabs(self):
2715 if self.scheme and self.scheme != 'file':
2715 if self.scheme and self.scheme != 'file':
2716 return True # remote URL
2716 return True # remote URL
2717 if hasdriveletter(self.path):
2717 if hasdriveletter(self.path):
2718 return True # absolute for our purposes - can't be joined()
2718 return True # absolute for our purposes - can't be joined()
2719 if self.path.startswith(r'\\'):
2719 if self.path.startswith(r'\\'):
2720 return True # Windows UNC path
2720 return True # Windows UNC path
2721 if self.path.startswith('/'):
2721 if self.path.startswith('/'):
2722 return True # POSIX-style
2722 return True # POSIX-style
2723 return False
2723 return False
2724
2724
2725 def localpath(self):
2725 def localpath(self):
2726 if self.scheme == 'file' or self.scheme == 'bundle':
2726 if self.scheme == 'file' or self.scheme == 'bundle':
2727 path = self.path or '/'
2727 path = self.path or '/'
2728 # For Windows, we need to promote hosts containing drive
2728 # For Windows, we need to promote hosts containing drive
2729 # letters to paths with drive letters.
2729 # letters to paths with drive letters.
2730 if hasdriveletter(self._hostport):
2730 if hasdriveletter(self._hostport):
2731 path = self._hostport + '/' + self.path
2731 path = self._hostport + '/' + self.path
2732 elif (self.host is not None and self.path
2732 elif (self.host is not None and self.path
2733 and not hasdriveletter(path)):
2733 and not hasdriveletter(path)):
2734 path = '/' + path
2734 path = '/' + path
2735 return path
2735 return path
2736 return self._origpath
2736 return self._origpath
2737
2737
2738 def islocal(self):
2738 def islocal(self):
2739 '''whether localpath will return something that posixfile can open'''
2739 '''whether localpath will return something that posixfile can open'''
2740 return (not self.scheme or self.scheme == 'file'
2740 return (not self.scheme or self.scheme == 'file'
2741 or self.scheme == 'bundle')
2741 or self.scheme == 'bundle')
2742
2742
2743 def hasscheme(path):
2743 def hasscheme(path):
2744 return bool(url(path).scheme)
2744 return bool(url(path).scheme)
2745
2745
2746 def hasdriveletter(path):
2746 def hasdriveletter(path):
2747 return path and path[1:2] == ':' and path[0:1].isalpha()
2747 return path and path[1:2] == ':' and path[0:1].isalpha()
2748
2748
2749 def urllocalpath(path):
2749 def urllocalpath(path):
2750 return url(path, parsequery=False, parsefragment=False).localpath()
2750 return url(path, parsequery=False, parsefragment=False).localpath()
2751
2751
2752 def hidepassword(u):
2752 def hidepassword(u):
2753 '''hide user credential in a url string'''
2753 '''hide user credential in a url string'''
2754 u = url(u)
2754 u = url(u)
2755 if u.passwd:
2755 if u.passwd:
2756 u.passwd = '***'
2756 u.passwd = '***'
2757 return str(u)
2757 return str(u)
2758
2758
2759 def removeauth(u):
2759 def removeauth(u):
2760 '''remove all authentication information from a url string'''
2760 '''remove all authentication information from a url string'''
2761 u = url(u)
2761 u = url(u)
2762 u.user = u.passwd = None
2762 u.user = u.passwd = None
2763 return str(u)
2763 return str(u)
2764
2764
2765 timecount = unitcountfn(
2765 timecount = unitcountfn(
2766 (1, 1e3, _('%.0f s')),
2766 (1, 1e3, _('%.0f s')),
2767 (100, 1, _('%.1f s')),
2767 (100, 1, _('%.1f s')),
2768 (10, 1, _('%.2f s')),
2768 (10, 1, _('%.2f s')),
2769 (1, 1, _('%.3f s')),
2769 (1, 1, _('%.3f s')),
2770 (100, 0.001, _('%.1f ms')),
2770 (100, 0.001, _('%.1f ms')),
2771 (10, 0.001, _('%.2f ms')),
2771 (10, 0.001, _('%.2f ms')),
2772 (1, 0.001, _('%.3f ms')),
2772 (1, 0.001, _('%.3f ms')),
2773 (100, 0.000001, _('%.1f us')),
2773 (100, 0.000001, _('%.1f us')),
2774 (10, 0.000001, _('%.2f us')),
2774 (10, 0.000001, _('%.2f us')),
2775 (1, 0.000001, _('%.3f us')),
2775 (1, 0.000001, _('%.3f us')),
2776 (100, 0.000000001, _('%.1f ns')),
2776 (100, 0.000000001, _('%.1f ns')),
2777 (10, 0.000000001, _('%.2f ns')),
2777 (10, 0.000000001, _('%.2f ns')),
2778 (1, 0.000000001, _('%.3f ns')),
2778 (1, 0.000000001, _('%.3f ns')),
2779 )
2779 )
2780
2780
2781 _timenesting = [0]
2781 _timenesting = [0]
2782
2782
2783 def timed(func):
2783 def timed(func):
2784 '''Report the execution time of a function call to stderr.
2784 '''Report the execution time of a function call to stderr.
2785
2785
2786 During development, use as a decorator when you need to measure
2786 During development, use as a decorator when you need to measure
2787 the cost of a function, e.g. as follows:
2787 the cost of a function, e.g. as follows:
2788
2788
2789 @util.timed
2789 @util.timed
2790 def foo(a, b, c):
2790 def foo(a, b, c):
2791 pass
2791 pass
2792 '''
2792 '''
2793
2793
2794 def wrapper(*args, **kwargs):
2794 def wrapper(*args, **kwargs):
2795 start = time.time()
2795 start = time.time()
2796 indent = 2
2796 indent = 2
2797 _timenesting[0] += indent
2797 _timenesting[0] += indent
2798 try:
2798 try:
2799 return func(*args, **kwargs)
2799 return func(*args, **kwargs)
2800 finally:
2800 finally:
2801 elapsed = time.time() - start
2801 elapsed = time.time() - start
2802 _timenesting[0] -= indent
2802 _timenesting[0] -= indent
2803 stderr.write('%s%s: %s\n' %
2803 stderr.write('%s%s: %s\n' %
2804 (' ' * _timenesting[0], func.__name__,
2804 (' ' * _timenesting[0], func.__name__,
2805 timecount(elapsed)))
2805 timecount(elapsed)))
2806 return wrapper
2806 return wrapper
2807
2807
2808 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2808 _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30),
2809 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2809 ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1))
2810
2810
2811 def sizetoint(s):
2811 def sizetoint(s):
2812 '''Convert a space specifier to a byte count.
2812 '''Convert a space specifier to a byte count.
2813
2813
2814 >>> sizetoint('30')
2814 >>> sizetoint('30')
2815 30
2815 30
2816 >>> sizetoint('2.2kb')
2816 >>> sizetoint('2.2kb')
2817 2252
2817 2252
2818 >>> sizetoint('6M')
2818 >>> sizetoint('6M')
2819 6291456
2819 6291456
2820 '''
2820 '''
2821 t = s.strip().lower()
2821 t = s.strip().lower()
2822 try:
2822 try:
2823 for k, u in _sizeunits:
2823 for k, u in _sizeunits:
2824 if t.endswith(k):
2824 if t.endswith(k):
2825 return int(float(t[:-len(k)]) * u)
2825 return int(float(t[:-len(k)]) * u)
2826 return int(t)
2826 return int(t)
2827 except ValueError:
2827 except ValueError:
2828 raise error.ParseError(_("couldn't parse size: %s") % s)
2828 raise error.ParseError(_("couldn't parse size: %s") % s)
2829
2829
2830 class hooks(object):
2830 class hooks(object):
2831 '''A collection of hook functions that can be used to extend a
2831 '''A collection of hook functions that can be used to extend a
2832 function's behavior. Hooks are called in lexicographic order,
2832 function's behavior. Hooks are called in lexicographic order,
2833 based on the names of their sources.'''
2833 based on the names of their sources.'''
2834
2834
2835 def __init__(self):
2835 def __init__(self):
2836 self._hooks = []
2836 self._hooks = []
2837
2837
2838 def add(self, source, hook):
2838 def add(self, source, hook):
2839 self._hooks.append((source, hook))
2839 self._hooks.append((source, hook))
2840
2840
2841 def __call__(self, *args):
2841 def __call__(self, *args):
2842 self._hooks.sort(key=lambda x: x[0])
2842 self._hooks.sort(key=lambda x: x[0])
2843 results = []
2843 results = []
2844 for source, hook in self._hooks:
2844 for source, hook in self._hooks:
2845 results.append(hook(*args))
2845 results.append(hook(*args))
2846 return results
2846 return results
2847
2847
2848 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2848 def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'):
2849 '''Yields lines for a nicely formatted stacktrace.
2849 '''Yields lines for a nicely formatted stacktrace.
2850 Skips the 'skip' last entries.
2850 Skips the 'skip' last entries.
2851 Each file+linenumber is formatted according to fileline.
2851 Each file+linenumber is formatted according to fileline.
2852 Each line is formatted according to line.
2852 Each line is formatted according to line.
2853 If line is None, it yields:
2853 If line is None, it yields:
2854 length of longest filepath+line number,
2854 length of longest filepath+line number,
2855 filepath+linenumber,
2855 filepath+linenumber,
2856 function
2856 function
2857
2857
2858 Not be used in production code but very convenient while developing.
2858 Not be used in production code but very convenient while developing.
2859 '''
2859 '''
2860 entries = [(fileline % (fn, ln), func)
2860 entries = [(fileline % (fn, ln), func)
2861 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2861 for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
2862 if entries:
2862 if entries:
2863 fnmax = max(len(entry[0]) for entry in entries)
2863 fnmax = max(len(entry[0]) for entry in entries)
2864 for fnln, func in entries:
2864 for fnln, func in entries:
2865 if line is None:
2865 if line is None:
2866 yield (fnmax, fnln, func)
2866 yield (fnmax, fnln, func)
2867 else:
2867 else:
2868 yield line % (fnmax, fnln, func)
2868 yield line % (fnmax, fnln, func)
2869
2869
2870 def debugstacktrace(msg='stacktrace', skip=0, f=stderr, otherf=stdout):
2870 def debugstacktrace(msg='stacktrace', skip=0, f=stderr, otherf=stdout):
2871 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2871 '''Writes a message to f (stderr) with a nicely formatted stacktrace.
2872 Skips the 'skip' last entries. By default it will flush stdout first.
2872 Skips the 'skip' last entries. By default it will flush stdout first.
2873 It can be used everywhere and intentionally does not require an ui object.
2873 It can be used everywhere and intentionally does not require an ui object.
2874 Not be used in production code but very convenient while developing.
2874 Not be used in production code but very convenient while developing.
2875 '''
2875 '''
2876 if otherf:
2876 if otherf:
2877 otherf.flush()
2877 otherf.flush()
2878 f.write('%s at:\n' % msg)
2878 f.write('%s at:\n' % msg)
2879 for line in getstackframes(skip + 1):
2879 for line in getstackframes(skip + 1):
2880 f.write(line)
2880 f.write(line)
2881 f.flush()
2881 f.flush()
2882
2882
2883 class dirs(object):
2883 class dirs(object):
2884 '''a multiset of directory names from a dirstate or manifest'''
2884 '''a multiset of directory names from a dirstate or manifest'''
2885
2885
2886 def __init__(self, map, skip=None):
2886 def __init__(self, map, skip=None):
2887 self._dirs = {}
2887 self._dirs = {}
2888 addpath = self.addpath
2888 addpath = self.addpath
2889 if safehasattr(map, 'iteritems') and skip is not None:
2889 if safehasattr(map, 'iteritems') and skip is not None:
2890 for f, s in map.iteritems():
2890 for f, s in map.iteritems():
2891 if s[0] != skip:
2891 if s[0] != skip:
2892 addpath(f)
2892 addpath(f)
2893 else:
2893 else:
2894 for f in map:
2894 for f in map:
2895 addpath(f)
2895 addpath(f)
2896
2896
2897 def addpath(self, path):
2897 def addpath(self, path):
2898 dirs = self._dirs
2898 dirs = self._dirs
2899 for base in finddirs(path):
2899 for base in finddirs(path):
2900 if base in dirs:
2900 if base in dirs:
2901 dirs[base] += 1
2901 dirs[base] += 1
2902 return
2902 return
2903 dirs[base] = 1
2903 dirs[base] = 1
2904
2904
2905 def delpath(self, path):
2905 def delpath(self, path):
2906 dirs = self._dirs
2906 dirs = self._dirs
2907 for base in finddirs(path):
2907 for base in finddirs(path):
2908 if dirs[base] > 1:
2908 if dirs[base] > 1:
2909 dirs[base] -= 1
2909 dirs[base] -= 1
2910 return
2910 return
2911 del dirs[base]
2911 del dirs[base]
2912
2912
2913 def __iter__(self):
2913 def __iter__(self):
2914 return self._dirs.iterkeys()
2914 return self._dirs.iterkeys()
2915
2915
2916 def __contains__(self, d):
2916 def __contains__(self, d):
2917 return d in self._dirs
2917 return d in self._dirs
2918
2918
2919 if safehasattr(parsers, 'dirs'):
2919 if safehasattr(parsers, 'dirs'):
2920 dirs = parsers.dirs
2920 dirs = parsers.dirs
2921
2921
2922 def finddirs(path):
2922 def finddirs(path):
2923 pos = path.rfind('/')
2923 pos = path.rfind('/')
2924 while pos != -1:
2924 while pos != -1:
2925 yield path[:pos]
2925 yield path[:pos]
2926 pos = path.rfind('/', 0, pos)
2926 pos = path.rfind('/', 0, pos)
2927
2927
2928 class ctxmanager(object):
2928 class ctxmanager(object):
2929 '''A context manager for use in 'with' blocks to allow multiple
2929 '''A context manager for use in 'with' blocks to allow multiple
2930 contexts to be entered at once. This is both safer and more
2930 contexts to be entered at once. This is both safer and more
2931 flexible than contextlib.nested.
2931 flexible than contextlib.nested.
2932
2932
2933 Once Mercurial supports Python 2.7+, this will become mostly
2933 Once Mercurial supports Python 2.7+, this will become mostly
2934 unnecessary.
2934 unnecessary.
2935 '''
2935 '''
2936
2936
2937 def __init__(self, *args):
2937 def __init__(self, *args):
2938 '''Accepts a list of no-argument functions that return context
2938 '''Accepts a list of no-argument functions that return context
2939 managers. These will be invoked at __call__ time.'''
2939 managers. These will be invoked at __call__ time.'''
2940 self._pending = args
2940 self._pending = args
2941 self._atexit = []
2941 self._atexit = []
2942
2942
2943 def __enter__(self):
2943 def __enter__(self):
2944 return self
2944 return self
2945
2945
2946 def enter(self):
2946 def enter(self):
2947 '''Create and enter context managers in the order in which they were
2947 '''Create and enter context managers in the order in which they were
2948 passed to the constructor.'''
2948 passed to the constructor.'''
2949 values = []
2949 values = []
2950 for func in self._pending:
2950 for func in self._pending:
2951 obj = func()
2951 obj = func()
2952 values.append(obj.__enter__())
2952 values.append(obj.__enter__())
2953 self._atexit.append(obj.__exit__)
2953 self._atexit.append(obj.__exit__)
2954 del self._pending
2954 del self._pending
2955 return values
2955 return values
2956
2956
2957 def atexit(self, func, *args, **kwargs):
2957 def atexit(self, func, *args, **kwargs):
2958 '''Add a function to call when this context manager exits. The
2958 '''Add a function to call when this context manager exits. The
2959 ordering of multiple atexit calls is unspecified, save that
2959 ordering of multiple atexit calls is unspecified, save that
2960 they will happen before any __exit__ functions.'''
2960 they will happen before any __exit__ functions.'''
2961 def wrapper(exc_type, exc_val, exc_tb):
2961 def wrapper(exc_type, exc_val, exc_tb):
2962 func(*args, **kwargs)
2962 func(*args, **kwargs)
2963 self._atexit.append(wrapper)
2963 self._atexit.append(wrapper)
2964 return func
2964 return func
2965
2965
2966 def __exit__(self, exc_type, exc_val, exc_tb):
2966 def __exit__(self, exc_type, exc_val, exc_tb):
2967 '''Context managers are exited in the reverse order from which
2967 '''Context managers are exited in the reverse order from which
2968 they were created.'''
2968 they were created.'''
2969 received = exc_type is not None
2969 received = exc_type is not None
2970 suppressed = False
2970 suppressed = False
2971 pending = None
2971 pending = None
2972 self._atexit.reverse()
2972 self._atexit.reverse()
2973 for exitfunc in self._atexit:
2973 for exitfunc in self._atexit:
2974 try:
2974 try:
2975 if exitfunc(exc_type, exc_val, exc_tb):
2975 if exitfunc(exc_type, exc_val, exc_tb):
2976 suppressed = True
2976 suppressed = True
2977 exc_type = None
2977 exc_type = None
2978 exc_val = None
2978 exc_val = None
2979 exc_tb = None
2979 exc_tb = None
2980 except BaseException:
2980 except BaseException:
2981 pending = sys.exc_info()
2981 pending = sys.exc_info()
2982 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2982 exc_type, exc_val, exc_tb = pending = sys.exc_info()
2983 del self._atexit
2983 del self._atexit
2984 if pending:
2984 if pending:
2985 raise exc_val
2985 raise exc_val
2986 return received and suppressed
2986 return received and suppressed
2987
2987
2988 # compression code
2988 # compression code
2989
2989
2990 SERVERROLE = 'server'
2990 SERVERROLE = 'server'
2991 CLIENTROLE = 'client'
2991 CLIENTROLE = 'client'
2992
2992
2993 compewireprotosupport = collections.namedtuple(u'compenginewireprotosupport',
2993 compewireprotosupport = collections.namedtuple(u'compenginewireprotosupport',
2994 (u'name', u'serverpriority',
2994 (u'name', u'serverpriority',
2995 u'clientpriority'))
2995 u'clientpriority'))
2996
2996
2997 class compressormanager(object):
2997 class compressormanager(object):
2998 """Holds registrations of various compression engines.
2998 """Holds registrations of various compression engines.
2999
2999
3000 This class essentially abstracts the differences between compression
3000 This class essentially abstracts the differences between compression
3001 engines to allow new compression formats to be added easily, possibly from
3001 engines to allow new compression formats to be added easily, possibly from
3002 extensions.
3002 extensions.
3003
3003
3004 Compressors are registered against the global instance by calling its
3004 Compressors are registered against the global instance by calling its
3005 ``register()`` method.
3005 ``register()`` method.
3006 """
3006 """
3007 def __init__(self):
3007 def __init__(self):
3008 self._engines = {}
3008 self._engines = {}
3009 # Bundle spec human name to engine name.
3009 # Bundle spec human name to engine name.
3010 self._bundlenames = {}
3010 self._bundlenames = {}
3011 # Internal bundle identifier to engine name.
3011 # Internal bundle identifier to engine name.
3012 self._bundletypes = {}
3012 self._bundletypes = {}
3013 # Revlog header to engine name.
3013 # Revlog header to engine name.
3014 self._revlogheaders = {}
3014 self._revlogheaders = {}
3015 # Wire proto identifier to engine name.
3015 # Wire proto identifier to engine name.
3016 self._wiretypes = {}
3016 self._wiretypes = {}
3017
3017
3018 def __getitem__(self, key):
3018 def __getitem__(self, key):
3019 return self._engines[key]
3019 return self._engines[key]
3020
3020
3021 def __contains__(self, key):
3021 def __contains__(self, key):
3022 return key in self._engines
3022 return key in self._engines
3023
3023
3024 def __iter__(self):
3024 def __iter__(self):
3025 return iter(self._engines.keys())
3025 return iter(self._engines.keys())
3026
3026
3027 def register(self, engine):
3027 def register(self, engine):
3028 """Register a compression engine with the manager.
3028 """Register a compression engine with the manager.
3029
3029
3030 The argument must be a ``compressionengine`` instance.
3030 The argument must be a ``compressionengine`` instance.
3031 """
3031 """
3032 if not isinstance(engine, compressionengine):
3032 if not isinstance(engine, compressionengine):
3033 raise ValueError(_('argument must be a compressionengine'))
3033 raise ValueError(_('argument must be a compressionengine'))
3034
3034
3035 name = engine.name()
3035 name = engine.name()
3036
3036
3037 if name in self._engines:
3037 if name in self._engines:
3038 raise error.Abort(_('compression engine %s already registered') %
3038 raise error.Abort(_('compression engine %s already registered') %
3039 name)
3039 name)
3040
3040
3041 bundleinfo = engine.bundletype()
3041 bundleinfo = engine.bundletype()
3042 if bundleinfo:
3042 if bundleinfo:
3043 bundlename, bundletype = bundleinfo
3043 bundlename, bundletype = bundleinfo
3044
3044
3045 if bundlename in self._bundlenames:
3045 if bundlename in self._bundlenames:
3046 raise error.Abort(_('bundle name %s already registered') %
3046 raise error.Abort(_('bundle name %s already registered') %
3047 bundlename)
3047 bundlename)
3048 if bundletype in self._bundletypes:
3048 if bundletype in self._bundletypes:
3049 raise error.Abort(_('bundle type %s already registered by %s') %
3049 raise error.Abort(_('bundle type %s already registered by %s') %
3050 (bundletype, self._bundletypes[bundletype]))
3050 (bundletype, self._bundletypes[bundletype]))
3051
3051
3052 # No external facing name declared.
3052 # No external facing name declared.
3053 if bundlename:
3053 if bundlename:
3054 self._bundlenames[bundlename] = name
3054 self._bundlenames[bundlename] = name
3055
3055
3056 self._bundletypes[bundletype] = name
3056 self._bundletypes[bundletype] = name
3057
3057
3058 wiresupport = engine.wireprotosupport()
3058 wiresupport = engine.wireprotosupport()
3059 if wiresupport:
3059 if wiresupport:
3060 wiretype = wiresupport.name
3060 wiretype = wiresupport.name
3061 if wiretype in self._wiretypes:
3061 if wiretype in self._wiretypes:
3062 raise error.Abort(_('wire protocol compression %s already '
3062 raise error.Abort(_('wire protocol compression %s already '
3063 'registered by %s') %
3063 'registered by %s') %
3064 (wiretype, self._wiretypes[wiretype]))
3064 (wiretype, self._wiretypes[wiretype]))
3065
3065
3066 self._wiretypes[wiretype] = name
3066 self._wiretypes[wiretype] = name
3067
3067
3068 revlogheader = engine.revlogheader()
3068 revlogheader = engine.revlogheader()
3069 if revlogheader and revlogheader in self._revlogheaders:
3069 if revlogheader and revlogheader in self._revlogheaders:
3070 raise error.Abort(_('revlog header %s already registered by %s') %
3070 raise error.Abort(_('revlog header %s already registered by %s') %
3071 (revlogheader, self._revlogheaders[revlogheader]))
3071 (revlogheader, self._revlogheaders[revlogheader]))
3072
3072
3073 if revlogheader:
3073 if revlogheader:
3074 self._revlogheaders[revlogheader] = name
3074 self._revlogheaders[revlogheader] = name
3075
3075
3076 self._engines[name] = engine
3076 self._engines[name] = engine
3077
3077
3078 @property
3078 @property
3079 def supportedbundlenames(self):
3079 def supportedbundlenames(self):
3080 return set(self._bundlenames.keys())
3080 return set(self._bundlenames.keys())
3081
3081
3082 @property
3082 @property
3083 def supportedbundletypes(self):
3083 def supportedbundletypes(self):
3084 return set(self._bundletypes.keys())
3084 return set(self._bundletypes.keys())
3085
3085
3086 def forbundlename(self, bundlename):
3086 def forbundlename(self, bundlename):
3087 """Obtain a compression engine registered to a bundle name.
3087 """Obtain a compression engine registered to a bundle name.
3088
3088
3089 Will raise KeyError if the bundle type isn't registered.
3089 Will raise KeyError if the bundle type isn't registered.
3090
3090
3091 Will abort if the engine is known but not available.
3091 Will abort if the engine is known but not available.
3092 """
3092 """
3093 engine = self._engines[self._bundlenames[bundlename]]
3093 engine = self._engines[self._bundlenames[bundlename]]
3094 if not engine.available():
3094 if not engine.available():
3095 raise error.Abort(_('compression engine %s could not be loaded') %
3095 raise error.Abort(_('compression engine %s could not be loaded') %
3096 engine.name())
3096 engine.name())
3097 return engine
3097 return engine
3098
3098
3099 def forbundletype(self, bundletype):
3099 def forbundletype(self, bundletype):
3100 """Obtain a compression engine registered to a bundle type.
3100 """Obtain a compression engine registered to a bundle type.
3101
3101
3102 Will raise KeyError if the bundle type isn't registered.
3102 Will raise KeyError if the bundle type isn't registered.
3103
3103
3104 Will abort if the engine is known but not available.
3104 Will abort if the engine is known but not available.
3105 """
3105 """
3106 engine = self._engines[self._bundletypes[bundletype]]
3106 engine = self._engines[self._bundletypes[bundletype]]
3107 if not engine.available():
3107 if not engine.available():
3108 raise error.Abort(_('compression engine %s could not be loaded') %
3108 raise error.Abort(_('compression engine %s could not be loaded') %
3109 engine.name())
3109 engine.name())
3110 return engine
3110 return engine
3111
3111
3112 def supportedwireengines(self, role, onlyavailable=True):
3112 def supportedwireengines(self, role, onlyavailable=True):
3113 """Obtain compression engines that support the wire protocol.
3113 """Obtain compression engines that support the wire protocol.
3114
3114
3115 Returns a list of engines in prioritized order, most desired first.
3115 Returns a list of engines in prioritized order, most desired first.
3116
3116
3117 If ``onlyavailable`` is set, filter out engines that can't be
3117 If ``onlyavailable`` is set, filter out engines that can't be
3118 loaded.
3118 loaded.
3119 """
3119 """
3120 assert role in (SERVERROLE, CLIENTROLE)
3120 assert role in (SERVERROLE, CLIENTROLE)
3121
3121
3122 attr = 'serverpriority' if role == SERVERROLE else 'clientpriority'
3122 attr = 'serverpriority' if role == SERVERROLE else 'clientpriority'
3123
3123
3124 engines = [self._engines[e] for e in self._wiretypes.values()]
3124 engines = [self._engines[e] for e in self._wiretypes.values()]
3125 if onlyavailable:
3125 if onlyavailable:
3126 engines = [e for e in engines if e.available()]
3126 engines = [e for e in engines if e.available()]
3127
3127
3128 def getkey(e):
3128 def getkey(e):
3129 # Sort first by priority, highest first. In case of tie, sort
3129 # Sort first by priority, highest first. In case of tie, sort
3130 # alphabetically. This is arbitrary, but ensures output is
3130 # alphabetically. This is arbitrary, but ensures output is
3131 # stable.
3131 # stable.
3132 w = e.wireprotosupport()
3132 w = e.wireprotosupport()
3133 return -1 * getattr(w, attr), w.name
3133 return -1 * getattr(w, attr), w.name
3134
3134
3135 return list(sorted(engines, key=getkey))
3135 return list(sorted(engines, key=getkey))
3136
3136
3137 def forwiretype(self, wiretype):
3137 def forwiretype(self, wiretype):
3138 engine = self._engines[self._wiretypes[wiretype]]
3138 engine = self._engines[self._wiretypes[wiretype]]
3139 if not engine.available():
3139 if not engine.available():
3140 raise error.Abort(_('compression engine %s could not be loaded') %
3140 raise error.Abort(_('compression engine %s could not be loaded') %
3141 engine.name())
3141 engine.name())
3142 return engine
3142 return engine
3143
3143
3144 def forrevlogheader(self, header):
3144 def forrevlogheader(self, header):
3145 """Obtain a compression engine registered to a revlog header.
3145 """Obtain a compression engine registered to a revlog header.
3146
3146
3147 Will raise KeyError if the revlog header value isn't registered.
3147 Will raise KeyError if the revlog header value isn't registered.
3148 """
3148 """
3149 return self._engines[self._revlogheaders[header]]
3149 return self._engines[self._revlogheaders[header]]
3150
3150
3151 compengines = compressormanager()
3151 compengines = compressormanager()
3152
3152
3153 class compressionengine(object):
3153 class compressionengine(object):
3154 """Base class for compression engines.
3154 """Base class for compression engines.
3155
3155
3156 Compression engines must implement the interface defined by this class.
3156 Compression engines must implement the interface defined by this class.
3157 """
3157 """
3158 def name(self):
3158 def name(self):
3159 """Returns the name of the compression engine.
3159 """Returns the name of the compression engine.
3160
3160
3161 This is the key the engine is registered under.
3161 This is the key the engine is registered under.
3162
3162
3163 This method must be implemented.
3163 This method must be implemented.
3164 """
3164 """
3165 raise NotImplementedError()
3165 raise NotImplementedError()
3166
3166
3167 def available(self):
3167 def available(self):
3168 """Whether the compression engine is available.
3168 """Whether the compression engine is available.
3169
3169
3170 The intent of this method is to allow optional compression engines
3170 The intent of this method is to allow optional compression engines
3171 that may not be available in all installations (such as engines relying
3171 that may not be available in all installations (such as engines relying
3172 on C extensions that may not be present).
3172 on C extensions that may not be present).
3173 """
3173 """
3174 return True
3174 return True
3175
3175
3176 def bundletype(self):
3176 def bundletype(self):
3177 """Describes bundle identifiers for this engine.
3177 """Describes bundle identifiers for this engine.
3178
3178
3179 If this compression engine isn't supported for bundles, returns None.
3179 If this compression engine isn't supported for bundles, returns None.
3180
3180
3181 If this engine can be used for bundles, returns a 2-tuple of strings of
3181 If this engine can be used for bundles, returns a 2-tuple of strings of
3182 the user-facing "bundle spec" compression name and an internal
3182 the user-facing "bundle spec" compression name and an internal
3183 identifier used to denote the compression format within bundles. To
3183 identifier used to denote the compression format within bundles. To
3184 exclude the name from external usage, set the first element to ``None``.
3184 exclude the name from external usage, set the first element to ``None``.
3185
3185
3186 If bundle compression is supported, the class must also implement
3186 If bundle compression is supported, the class must also implement
3187 ``compressstream`` and `decompressorreader``.
3187 ``compressstream`` and `decompressorreader``.
3188 """
3188 """
3189 return None
3189 return None
3190
3190
3191 def wireprotosupport(self):
3191 def wireprotosupport(self):
3192 """Declare support for this compression format on the wire protocol.
3192 """Declare support for this compression format on the wire protocol.
3193
3193
3194 If this compression engine isn't supported for compressing wire
3194 If this compression engine isn't supported for compressing wire
3195 protocol payloads, returns None.
3195 protocol payloads, returns None.
3196
3196
3197 Otherwise, returns ``compenginewireprotosupport`` with the following
3197 Otherwise, returns ``compenginewireprotosupport`` with the following
3198 fields:
3198 fields:
3199
3199
3200 * String format identifier
3200 * String format identifier
3201 * Integer priority for the server
3201 * Integer priority for the server
3202 * Integer priority for the client
3202 * Integer priority for the client
3203
3203
3204 The integer priorities are used to order the advertisement of format
3204 The integer priorities are used to order the advertisement of format
3205 support by server and client. The highest integer is advertised
3205 support by server and client. The highest integer is advertised
3206 first. Integers with non-positive values aren't advertised.
3206 first. Integers with non-positive values aren't advertised.
3207
3207
3208 The priority values are somewhat arbitrary and only used for default
3208 The priority values are somewhat arbitrary and only used for default
3209 ordering. The relative order can be changed via config options.
3209 ordering. The relative order can be changed via config options.
3210
3210
3211 If wire protocol compression is supported, the class must also implement
3211 If wire protocol compression is supported, the class must also implement
3212 ``compressstream`` and ``decompressorreader``.
3212 ``compressstream`` and ``decompressorreader``.
3213 """
3213 """
3214 return None
3214 return None
3215
3215
3216 def revlogheader(self):
3216 def revlogheader(self):
3217 """Header added to revlog chunks that identifies this engine.
3217 """Header added to revlog chunks that identifies this engine.
3218
3218
3219 If this engine can be used to compress revlogs, this method should
3219 If this engine can be used to compress revlogs, this method should
3220 return the bytes used to identify chunks compressed with this engine.
3220 return the bytes used to identify chunks compressed with this engine.
3221 Else, the method should return ``None`` to indicate it does not
3221 Else, the method should return ``None`` to indicate it does not
3222 participate in revlog compression.
3222 participate in revlog compression.
3223 """
3223 """
3224 return None
3224 return None
3225
3225
3226 def compressstream(self, it, opts=None):
3226 def compressstream(self, it, opts=None):
3227 """Compress an iterator of chunks.
3227 """Compress an iterator of chunks.
3228
3228
3229 The method receives an iterator (ideally a generator) of chunks of
3229 The method receives an iterator (ideally a generator) of chunks of
3230 bytes to be compressed. It returns an iterator (ideally a generator)
3230 bytes to be compressed. It returns an iterator (ideally a generator)
3231 of bytes of chunks representing the compressed output.
3231 of bytes of chunks representing the compressed output.
3232
3232
3233 Optionally accepts an argument defining how to perform compression.
3233 Optionally accepts an argument defining how to perform compression.
3234 Each engine treats this argument differently.
3234 Each engine treats this argument differently.
3235 """
3235 """
3236 raise NotImplementedError()
3236 raise NotImplementedError()
3237
3237
3238 def decompressorreader(self, fh):
3238 def decompressorreader(self, fh):
3239 """Perform decompression on a file object.
3239 """Perform decompression on a file object.
3240
3240
3241 Argument is an object with a ``read(size)`` method that returns
3241 Argument is an object with a ``read(size)`` method that returns
3242 compressed data. Return value is an object with a ``read(size)`` that
3242 compressed data. Return value is an object with a ``read(size)`` that
3243 returns uncompressed data.
3243 returns uncompressed data.
3244 """
3244 """
3245 raise NotImplementedError()
3245 raise NotImplementedError()
3246
3246
3247 def revlogcompressor(self, opts=None):
3247 def revlogcompressor(self, opts=None):
3248 """Obtain an object that can be used to compress revlog entries.
3248 """Obtain an object that can be used to compress revlog entries.
3249
3249
3250 The object has a ``compress(data)`` method that compresses binary
3250 The object has a ``compress(data)`` method that compresses binary
3251 data. This method returns compressed binary data or ``None`` if
3251 data. This method returns compressed binary data or ``None`` if
3252 the data could not be compressed (too small, not compressible, etc).
3252 the data could not be compressed (too small, not compressible, etc).
3253 The returned data should have a header uniquely identifying this
3253 The returned data should have a header uniquely identifying this
3254 compression format so decompression can be routed to this engine.
3254 compression format so decompression can be routed to this engine.
3255 This header should be identified by the ``revlogheader()`` return
3255 This header should be identified by the ``revlogheader()`` return
3256 value.
3256 value.
3257
3257
3258 The object has a ``decompress(data)`` method that decompresses
3258 The object has a ``decompress(data)`` method that decompresses
3259 data. The method will only be called if ``data`` begins with
3259 data. The method will only be called if ``data`` begins with
3260 ``revlogheader()``. The method should return the raw, uncompressed
3260 ``revlogheader()``. The method should return the raw, uncompressed
3261 data or raise a ``RevlogError``.
3261 data or raise a ``RevlogError``.
3262
3262
3263 The object is reusable but is not thread safe.
3263 The object is reusable but is not thread safe.
3264 """
3264 """
3265 raise NotImplementedError()
3265 raise NotImplementedError()
3266
3266
3267 class _zlibengine(compressionengine):
3267 class _zlibengine(compressionengine):
3268 def name(self):
3268 def name(self):
3269 return 'zlib'
3269 return 'zlib'
3270
3270
3271 def bundletype(self):
3271 def bundletype(self):
3272 return 'gzip', 'GZ'
3272 return 'gzip', 'GZ'
3273
3273
3274 def wireprotosupport(self):
3274 def wireprotosupport(self):
3275 return compewireprotosupport('zlib', 20, 20)
3275 return compewireprotosupport('zlib', 20, 20)
3276
3276
3277 def revlogheader(self):
3277 def revlogheader(self):
3278 return 'x'
3278 return 'x'
3279
3279
3280 def compressstream(self, it, opts=None):
3280 def compressstream(self, it, opts=None):
3281 opts = opts or {}
3281 opts = opts or {}
3282
3282
3283 z = zlib.compressobj(opts.get('level', -1))
3283 z = zlib.compressobj(opts.get('level', -1))
3284 for chunk in it:
3284 for chunk in it:
3285 data = z.compress(chunk)
3285 data = z.compress(chunk)
3286 # Not all calls to compress emit data. It is cheaper to inspect
3286 # Not all calls to compress emit data. It is cheaper to inspect
3287 # here than to feed empty chunks through generator.
3287 # here than to feed empty chunks through generator.
3288 if data:
3288 if data:
3289 yield data
3289 yield data
3290
3290
3291 yield z.flush()
3291 yield z.flush()
3292
3292
3293 def decompressorreader(self, fh):
3293 def decompressorreader(self, fh):
3294 def gen():
3294 def gen():
3295 d = zlib.decompressobj()
3295 d = zlib.decompressobj()
3296 for chunk in filechunkiter(fh):
3296 for chunk in filechunkiter(fh):
3297 while chunk:
3297 while chunk:
3298 # Limit output size to limit memory.
3298 # Limit output size to limit memory.
3299 yield d.decompress(chunk, 2 ** 18)
3299 yield d.decompress(chunk, 2 ** 18)
3300 chunk = d.unconsumed_tail
3300 chunk = d.unconsumed_tail
3301
3301
3302 return chunkbuffer(gen())
3302 return chunkbuffer(gen())
3303
3303
3304 class zlibrevlogcompressor(object):
3304 class zlibrevlogcompressor(object):
3305 def compress(self, data):
3305 def compress(self, data):
3306 insize = len(data)
3306 insize = len(data)
3307 # Caller handles empty input case.
3307 # Caller handles empty input case.
3308 assert insize > 0
3308 assert insize > 0
3309
3309
3310 if insize < 44:
3310 if insize < 44:
3311 return None
3311 return None
3312
3312
3313 elif insize <= 1000000:
3313 elif insize <= 1000000:
3314 compressed = zlib.compress(data)
3314 compressed = zlib.compress(data)
3315 if len(compressed) < insize:
3315 if len(compressed) < insize:
3316 return compressed
3316 return compressed
3317 return None
3317 return None
3318
3318
3319 # zlib makes an internal copy of the input buffer, doubling
3319 # zlib makes an internal copy of the input buffer, doubling
3320 # memory usage for large inputs. So do streaming compression
3320 # memory usage for large inputs. So do streaming compression
3321 # on large inputs.
3321 # on large inputs.
3322 else:
3322 else:
3323 z = zlib.compressobj()
3323 z = zlib.compressobj()
3324 parts = []
3324 parts = []
3325 pos = 0
3325 pos = 0
3326 while pos < insize:
3326 while pos < insize:
3327 pos2 = pos + 2**20
3327 pos2 = pos + 2**20
3328 parts.append(z.compress(data[pos:pos2]))
3328 parts.append(z.compress(data[pos:pos2]))
3329 pos = pos2
3329 pos = pos2
3330 parts.append(z.flush())
3330 parts.append(z.flush())
3331
3331
3332 if sum(map(len, parts)) < insize:
3332 if sum(map(len, parts)) < insize:
3333 return ''.join(parts)
3333 return ''.join(parts)
3334 return None
3334 return None
3335
3335
3336 def decompress(self, data):
3336 def decompress(self, data):
3337 try:
3337 try:
3338 return zlib.decompress(data)
3338 return zlib.decompress(data)
3339 except zlib.error as e:
3339 except zlib.error as e:
3340 raise error.RevlogError(_('revlog decompress error: %s') %
3340 raise error.RevlogError(_('revlog decompress error: %s') %
3341 str(e))
3341 str(e))
3342
3342
3343 def revlogcompressor(self, opts=None):
3343 def revlogcompressor(self, opts=None):
3344 return self.zlibrevlogcompressor()
3344 return self.zlibrevlogcompressor()
3345
3345
3346 compengines.register(_zlibengine())
3346 compengines.register(_zlibengine())
3347
3347
3348 class _bz2engine(compressionengine):
3348 class _bz2engine(compressionengine):
3349 def name(self):
3349 def name(self):
3350 return 'bz2'
3350 return 'bz2'
3351
3351
3352 def bundletype(self):
3352 def bundletype(self):
3353 return 'bzip2', 'BZ'
3353 return 'bzip2', 'BZ'
3354
3354
3355 # We declare a protocol name but don't advertise by default because
3355 # We declare a protocol name but don't advertise by default because
3356 # it is slow.
3356 # it is slow.
3357 def wireprotosupport(self):
3357 def wireprotosupport(self):
3358 return compewireprotosupport('bzip2', 0, 0)
3358 return compewireprotosupport('bzip2', 0, 0)
3359
3359
3360 def compressstream(self, it, opts=None):
3360 def compressstream(self, it, opts=None):
3361 opts = opts or {}
3361 opts = opts or {}
3362 z = bz2.BZ2Compressor(opts.get('level', 9))
3362 z = bz2.BZ2Compressor(opts.get('level', 9))
3363 for chunk in it:
3363 for chunk in it:
3364 data = z.compress(chunk)
3364 data = z.compress(chunk)
3365 if data:
3365 if data:
3366 yield data
3366 yield data
3367
3367
3368 yield z.flush()
3368 yield z.flush()
3369
3369
3370 def decompressorreader(self, fh):
3370 def decompressorreader(self, fh):
3371 def gen():
3371 def gen():
3372 d = bz2.BZ2Decompressor()
3372 d = bz2.BZ2Decompressor()
3373 for chunk in filechunkiter(fh):
3373 for chunk in filechunkiter(fh):
3374 yield d.decompress(chunk)
3374 yield d.decompress(chunk)
3375
3375
3376 return chunkbuffer(gen())
3376 return chunkbuffer(gen())
3377
3377
3378 compengines.register(_bz2engine())
3378 compengines.register(_bz2engine())
3379
3379
3380 class _truncatedbz2engine(compressionengine):
3380 class _truncatedbz2engine(compressionengine):
3381 def name(self):
3381 def name(self):
3382 return 'bz2truncated'
3382 return 'bz2truncated'
3383
3383
3384 def bundletype(self):
3384 def bundletype(self):
3385 return None, '_truncatedBZ'
3385 return None, '_truncatedBZ'
3386
3386
3387 # We don't implement compressstream because it is hackily handled elsewhere.
3387 # We don't implement compressstream because it is hackily handled elsewhere.
3388
3388
3389 def decompressorreader(self, fh):
3389 def decompressorreader(self, fh):
3390 def gen():
3390 def gen():
3391 # The input stream doesn't have the 'BZ' header. So add it back.
3391 # The input stream doesn't have the 'BZ' header. So add it back.
3392 d = bz2.BZ2Decompressor()
3392 d = bz2.BZ2Decompressor()
3393 d.decompress('BZ')
3393 d.decompress('BZ')
3394 for chunk in filechunkiter(fh):
3394 for chunk in filechunkiter(fh):
3395 yield d.decompress(chunk)
3395 yield d.decompress(chunk)
3396
3396
3397 return chunkbuffer(gen())
3397 return chunkbuffer(gen())
3398
3398
3399 compengines.register(_truncatedbz2engine())
3399 compengines.register(_truncatedbz2engine())
3400
3400
3401 class _noopengine(compressionengine):
3401 class _noopengine(compressionengine):
3402 def name(self):
3402 def name(self):
3403 return 'none'
3403 return 'none'
3404
3404
3405 def bundletype(self):
3405 def bundletype(self):
3406 return 'none', 'UN'
3406 return 'none', 'UN'
3407
3407
3408 # Clients always support uncompressed payloads. Servers don't because
3408 # Clients always support uncompressed payloads. Servers don't because
3409 # unless you are on a fast network, uncompressed payloads can easily
3409 # unless you are on a fast network, uncompressed payloads can easily
3410 # saturate your network pipe.
3410 # saturate your network pipe.
3411 def wireprotosupport(self):
3411 def wireprotosupport(self):
3412 return compewireprotosupport('none', 0, 10)
3412 return compewireprotosupport('none', 0, 10)
3413
3413
3414 # We don't implement revlogheader because it is handled specially
3414 # We don't implement revlogheader because it is handled specially
3415 # in the revlog class.
3415 # in the revlog class.
3416
3416
3417 def compressstream(self, it, opts=None):
3417 def compressstream(self, it, opts=None):
3418 return it
3418 return it
3419
3419
3420 def decompressorreader(self, fh):
3420 def decompressorreader(self, fh):
3421 return fh
3421 return fh
3422
3422
3423 class nooprevlogcompressor(object):
3423 class nooprevlogcompressor(object):
3424 def compress(self, data):
3424 def compress(self, data):
3425 return None
3425 return None
3426
3426
3427 def revlogcompressor(self, opts=None):
3427 def revlogcompressor(self, opts=None):
3428 return self.nooprevlogcompressor()
3428 return self.nooprevlogcompressor()
3429
3429
3430 compengines.register(_noopengine())
3430 compengines.register(_noopengine())
3431
3431
3432 class _zstdengine(compressionengine):
3432 class _zstdengine(compressionengine):
3433 def name(self):
3433 def name(self):
3434 return 'zstd'
3434 return 'zstd'
3435
3435
3436 @propertycache
3436 @propertycache
3437 def _module(self):
3437 def _module(self):
3438 # Not all installs have the zstd module available. So defer importing
3438 # Not all installs have the zstd module available. So defer importing
3439 # until first access.
3439 # until first access.
3440 try:
3440 try:
3441 from . import zstd
3441 from . import zstd
3442 # Force delayed import.
3442 # Force delayed import.
3443 zstd.__version__
3443 zstd.__version__
3444 return zstd
3444 return zstd
3445 except ImportError:
3445 except ImportError:
3446 return None
3446 return None
3447
3447
3448 def available(self):
3448 def available(self):
3449 return bool(self._module)
3449 return bool(self._module)
3450
3450
3451 def bundletype(self):
3451 def bundletype(self):
3452 return 'zstd', 'ZS'
3452 return 'zstd', 'ZS'
3453
3453
3454 def wireprotosupport(self):
3454 def wireprotosupport(self):
3455 return compewireprotosupport('zstd', 50, 50)
3455 return compewireprotosupport('zstd', 50, 50)
3456
3456
3457 def revlogheader(self):
3457 def revlogheader(self):
3458 return '\x28'
3458 return '\x28'
3459
3459
3460 def compressstream(self, it, opts=None):
3460 def compressstream(self, it, opts=None):
3461 opts = opts or {}
3461 opts = opts or {}
3462 # zstd level 3 is almost always significantly faster than zlib
3462 # zstd level 3 is almost always significantly faster than zlib
3463 # while providing no worse compression. It strikes a good balance
3463 # while providing no worse compression. It strikes a good balance
3464 # between speed and compression.
3464 # between speed and compression.
3465 level = opts.get('level', 3)
3465 level = opts.get('level', 3)
3466
3466
3467 zstd = self._module
3467 zstd = self._module
3468 z = zstd.ZstdCompressor(level=level).compressobj()
3468 z = zstd.ZstdCompressor(level=level).compressobj()
3469 for chunk in it:
3469 for chunk in it:
3470 data = z.compress(chunk)
3470 data = z.compress(chunk)
3471 if data:
3471 if data:
3472 yield data
3472 yield data
3473
3473
3474 yield z.flush()
3474 yield z.flush()
3475
3475
3476 def decompressorreader(self, fh):
3476 def decompressorreader(self, fh):
3477 zstd = self._module
3477 zstd = self._module
3478 dctx = zstd.ZstdDecompressor()
3478 dctx = zstd.ZstdDecompressor()
3479 return chunkbuffer(dctx.read_from(fh))
3479 return chunkbuffer(dctx.read_from(fh))
3480
3480
3481 class zstdrevlogcompressor(object):
3481 class zstdrevlogcompressor(object):
3482 def __init__(self, zstd, level=3):
3482 def __init__(self, zstd, level=3):
3483 # Writing the content size adds a few bytes to the output. However,
3483 # Writing the content size adds a few bytes to the output. However,
3484 # it allows decompression to be more optimal since we can
3484 # it allows decompression to be more optimal since we can
3485 # pre-allocate a buffer to hold the result.
3485 # pre-allocate a buffer to hold the result.
3486 self._cctx = zstd.ZstdCompressor(level=level,
3486 self._cctx = zstd.ZstdCompressor(level=level,
3487 write_content_size=True)
3487 write_content_size=True)
3488 self._dctx = zstd.ZstdDecompressor()
3488 self._dctx = zstd.ZstdDecompressor()
3489 self._compinsize = zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE
3489 self._compinsize = zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE
3490 self._decompinsize = zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE
3490 self._decompinsize = zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE
3491
3491
3492 def compress(self, data):
3492 def compress(self, data):
3493 insize = len(data)
3493 insize = len(data)
3494 # Caller handles empty input case.
3494 # Caller handles empty input case.
3495 assert insize > 0
3495 assert insize > 0
3496
3496
3497 if insize < 50:
3497 if insize < 50:
3498 return None
3498 return None
3499
3499
3500 elif insize <= 1000000:
3500 elif insize <= 1000000:
3501 compressed = self._cctx.compress(data)
3501 compressed = self._cctx.compress(data)
3502 if len(compressed) < insize:
3502 if len(compressed) < insize:
3503 return compressed
3503 return compressed
3504 return None
3504 return None
3505 else:
3505 else:
3506 z = self._cctx.compressobj()
3506 z = self._cctx.compressobj()
3507 chunks = []
3507 chunks = []
3508 pos = 0
3508 pos = 0
3509 while pos < insize:
3509 while pos < insize:
3510 pos2 = pos + self._compinsize
3510 pos2 = pos + self._compinsize
3511 chunk = z.compress(data[pos:pos2])
3511 chunk = z.compress(data[pos:pos2])
3512 if chunk:
3512 if chunk:
3513 chunks.append(chunk)
3513 chunks.append(chunk)
3514 pos = pos2
3514 pos = pos2
3515 chunks.append(z.flush())
3515 chunks.append(z.flush())
3516
3516
3517 if sum(map(len, chunks)) < insize:
3517 if sum(map(len, chunks)) < insize:
3518 return ''.join(chunks)
3518 return ''.join(chunks)
3519 return None
3519 return None
3520
3520
3521 def decompress(self, data):
3521 def decompress(self, data):
3522 insize = len(data)
3522 insize = len(data)
3523
3523
3524 try:
3524 try:
3525 # This was measured to be faster than other streaming
3525 # This was measured to be faster than other streaming
3526 # decompressors.
3526 # decompressors.
3527 dobj = self._dctx.decompressobj()
3527 dobj = self._dctx.decompressobj()
3528 chunks = []
3528 chunks = []
3529 pos = 0
3529 pos = 0
3530 while pos < insize:
3530 while pos < insize:
3531 pos2 = pos + self._decompinsize
3531 pos2 = pos + self._decompinsize
3532 chunk = dobj.decompress(data[pos:pos2])
3532 chunk = dobj.decompress(data[pos:pos2])
3533 if chunk:
3533 if chunk:
3534 chunks.append(chunk)
3534 chunks.append(chunk)
3535 pos = pos2
3535 pos = pos2
3536 # Frame should be exhausted, so no finish() API.
3536 # Frame should be exhausted, so no finish() API.
3537
3537
3538 return ''.join(chunks)
3538 return ''.join(chunks)
3539 except Exception as e:
3539 except Exception as e:
3540 raise error.RevlogError(_('revlog decompress error: %s') %
3540 raise error.RevlogError(_('revlog decompress error: %s') %
3541 str(e))
3541 str(e))
3542
3542
3543 def revlogcompressor(self, opts=None):
3543 def revlogcompressor(self, opts=None):
3544 opts = opts or {}
3544 opts = opts or {}
3545 return self.zstdrevlogcompressor(self._module,
3545 return self.zstdrevlogcompressor(self._module,
3546 level=opts.get('level', 3))
3546 level=opts.get('level', 3))
3547
3547
3548 compengines.register(_zstdengine())
3548 compengines.register(_zstdengine())
3549
3549
3550 # convenient shortcut
3550 # convenient shortcut
3551 dst = debugstacktrace
3551 dst = debugstacktrace
General Comments 0
You need to be logged in to leave comments. Login now