Show More
@@ -1,1353 +1,1353 b'' | |||
|
1 | 1 | # Subversion 1.4/1.5 Python API backend |
|
2 | 2 | # |
|
3 | 3 | # Copyright(C) 2007 Daniel Holth et al |
|
4 | 4 | from __future__ import absolute_import |
|
5 | 5 | |
|
6 | 6 | import os |
|
7 | 7 | import re |
|
8 | 8 | import tempfile |
|
9 | 9 | import xml.dom.minidom |
|
10 | 10 | |
|
11 | 11 | from mercurial.i18n import _ |
|
12 | 12 | from mercurial import ( |
|
13 | 13 | encoding, |
|
14 | 14 | error, |
|
15 | 15 | pycompat, |
|
16 | 16 | scmutil, |
|
17 | 17 | util, |
|
18 | 18 | ) |
|
19 | 19 | |
|
20 | 20 | from . import common |
|
21 | 21 | |
|
22 | 22 | pickle = util.pickle |
|
23 | 23 | stringio = util.stringio |
|
24 | 24 | propertycache = util.propertycache |
|
25 | 25 | urlerr = util.urlerr |
|
26 | 26 | urlreq = util.urlreq |
|
27 | 27 | |
|
28 | 28 | commandline = common.commandline |
|
29 | 29 | commit = common.commit |
|
30 | 30 | converter_sink = common.converter_sink |
|
31 | 31 | converter_source = common.converter_source |
|
32 | 32 | decodeargs = common.decodeargs |
|
33 | 33 | encodeargs = common.encodeargs |
|
34 | 34 | makedatetimestamp = common.makedatetimestamp |
|
35 | 35 | mapfile = common.mapfile |
|
36 | 36 | MissingTool = common.MissingTool |
|
37 | 37 | NoRepo = common.NoRepo |
|
38 | 38 | |
|
39 | 39 | # Subversion stuff. Works best with very recent Python SVN bindings |
|
40 | 40 | # e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing |
|
41 | 41 | # these bindings. |
|
42 | 42 | |
|
43 | 43 | try: |
|
44 | 44 | import svn |
|
45 | 45 | import svn.client |
|
46 | 46 | import svn.core |
|
47 | 47 | import svn.ra |
|
48 | 48 | import svn.delta |
|
49 | 49 | from . import transport |
|
50 | 50 | import warnings |
|
51 | 51 | warnings.filterwarnings('ignore', |
|
52 | 52 | module='svn.core', |
|
53 | 53 | category=DeprecationWarning) |
|
54 | 54 | svn.core.SubversionException # trigger import to catch error |
|
55 | 55 | |
|
56 | 56 | except ImportError: |
|
57 | 57 | svn = None |
|
58 | 58 | |
|
59 | 59 | class SvnPathNotFound(Exception): |
|
60 | 60 | pass |
|
61 | 61 | |
|
62 | 62 | def revsplit(rev): |
|
63 | 63 | """Parse a revision string and return (uuid, path, revnum). |
|
64 | 64 | >>> revsplit('svn:a2147622-4a9f-4db4-a8d3-13562ff547b2' |
|
65 | 65 | ... '/proj%20B/mytrunk/mytrunk@1') |
|
66 | 66 | ('a2147622-4a9f-4db4-a8d3-13562ff547b2', '/proj%20B/mytrunk/mytrunk', 1) |
|
67 | 67 | >>> revsplit('svn:8af66a51-67f5-4354-b62c-98d67cc7be1d@1') |
|
68 | 68 | ('', '', 1) |
|
69 | 69 | >>> revsplit('@7') |
|
70 | 70 | ('', '', 7) |
|
71 | 71 | >>> revsplit('7') |
|
72 | 72 | ('', '', 0) |
|
73 | 73 | >>> revsplit('bad') |
|
74 | 74 | ('', '', 0) |
|
75 | 75 | """ |
|
76 | 76 | parts = rev.rsplit('@', 1) |
|
77 | 77 | revnum = 0 |
|
78 | 78 | if len(parts) > 1: |
|
79 | 79 | revnum = int(parts[1]) |
|
80 | 80 | parts = parts[0].split('/', 1) |
|
81 | 81 | uuid = '' |
|
82 | 82 | mod = '' |
|
83 | 83 | if len(parts) > 1 and parts[0].startswith('svn:'): |
|
84 | 84 | uuid = parts[0][4:] |
|
85 | 85 | mod = '/' + parts[1] |
|
86 | 86 | return uuid, mod, revnum |
|
87 | 87 | |
|
88 | 88 | def quote(s): |
|
89 | 89 | # As of svn 1.7, many svn calls expect "canonical" paths. In |
|
90 | 90 | # theory, we should call svn.core.*canonicalize() on all paths |
|
91 | 91 | # before passing them to the API. Instead, we assume the base url |
|
92 | 92 | # is canonical and copy the behaviour of svn URL encoding function |
|
93 | 93 | # so we can extend it safely with new components. The "safe" |
|
94 | 94 | # characters were taken from the "svn_uri__char_validity" table in |
|
95 | 95 | # libsvn_subr/path.c. |
|
96 | 96 | return urlreq.quote(s, "!$&'()*+,-./:=@_~") |
|
97 | 97 | |
|
98 | 98 | def geturl(path): |
|
99 | 99 | try: |
|
100 | 100 | return svn.client.url_from_path(svn.core.svn_path_canonicalize(path)) |
|
101 | 101 | except svn.core.SubversionException: |
|
102 | 102 | # svn.client.url_from_path() fails with local repositories |
|
103 | 103 | pass |
|
104 | 104 | if os.path.isdir(path): |
|
105 | 105 | path = os.path.normpath(os.path.abspath(path)) |
|
106 | 106 | if pycompat.osname == 'nt': |
|
107 | 107 | path = '/' + util.normpath(path) |
|
108 | 108 | # Module URL is later compared with the repository URL returned |
|
109 | 109 | # by svn API, which is UTF-8. |
|
110 | 110 | path = encoding.tolocal(path) |
|
111 | 111 | path = 'file://%s' % quote(path) |
|
112 | 112 | return svn.core.svn_path_canonicalize(path) |
|
113 | 113 | |
|
114 | 114 | def optrev(number): |
|
115 | 115 | optrev = svn.core.svn_opt_revision_t() |
|
116 | 116 | optrev.kind = svn.core.svn_opt_revision_number |
|
117 | 117 | optrev.value.number = number |
|
118 | 118 | return optrev |
|
119 | 119 | |
|
120 | 120 | class changedpath(object): |
|
121 | 121 | def __init__(self, p): |
|
122 | 122 | self.copyfrom_path = p.copyfrom_path |
|
123 | 123 | self.copyfrom_rev = p.copyfrom_rev |
|
124 | 124 | self.action = p.action |
|
125 | 125 | |
|
126 | 126 | def get_log_child(fp, url, paths, start, end, limit=0, |
|
127 | 127 | discover_changed_paths=True, strict_node_history=False): |
|
128 | 128 | protocol = -1 |
|
129 | 129 | def receiver(orig_paths, revnum, author, date, message, pool): |
|
130 | 130 | paths = {} |
|
131 | 131 | if orig_paths is not None: |
|
132 | 132 | for k, v in orig_paths.iteritems(): |
|
133 | 133 | paths[k] = changedpath(v) |
|
134 | 134 | pickle.dump((paths, revnum, author, date, message), |
|
135 | 135 | fp, protocol) |
|
136 | 136 | |
|
137 | 137 | try: |
|
138 | 138 | # Use an ra of our own so that our parent can consume |
|
139 | 139 | # our results without confusing the server. |
|
140 | 140 | t = transport.SvnRaTransport(url=url) |
|
141 | 141 | svn.ra.get_log(t.ra, paths, start, end, limit, |
|
142 | 142 | discover_changed_paths, |
|
143 | 143 | strict_node_history, |
|
144 | 144 | receiver) |
|
145 | 145 | except IOError: |
|
146 | 146 | # Caller may interrupt the iteration |
|
147 | 147 | pickle.dump(None, fp, protocol) |
|
148 | 148 | except Exception as inst: |
|
149 | 149 | pickle.dump(str(inst), fp, protocol) |
|
150 | 150 | else: |
|
151 | 151 | pickle.dump(None, fp, protocol) |
|
152 | 152 | fp.close() |
|
153 | 153 | # With large history, cleanup process goes crazy and suddenly |
|
154 | 154 | # consumes *huge* amount of memory. The output file being closed, |
|
155 | 155 | # there is no need for clean termination. |
|
156 | 156 | os._exit(0) |
|
157 | 157 | |
|
158 | 158 | def debugsvnlog(ui, **opts): |
|
159 | 159 | """Fetch SVN log in a subprocess and channel them back to parent to |
|
160 | 160 | avoid memory collection issues. |
|
161 | 161 | """ |
|
162 | 162 | if svn is None: |
|
163 | 163 | raise error.Abort(_('debugsvnlog could not load Subversion python ' |
|
164 | 164 | 'bindings')) |
|
165 | 165 | |
|
166 | 166 | args = decodeargs(ui.fin.read()) |
|
167 | 167 | get_log_child(ui.fout, *args) |
|
168 | 168 | |
|
169 | 169 | class logstream(object): |
|
170 | 170 | """Interruptible revision log iterator.""" |
|
171 | 171 | def __init__(self, stdout): |
|
172 | 172 | self._stdout = stdout |
|
173 | 173 | |
|
174 | 174 | def __iter__(self): |
|
175 | 175 | while True: |
|
176 | 176 | try: |
|
177 | 177 | entry = pickle.load(self._stdout) |
|
178 | 178 | except EOFError: |
|
179 | 179 | raise error.Abort(_('Mercurial failed to run itself, check' |
|
180 | 180 | ' hg executable is in PATH')) |
|
181 | 181 | try: |
|
182 | 182 | orig_paths, revnum, author, date, message = entry |
|
183 | 183 | except (TypeError, ValueError): |
|
184 | 184 | if entry is None: |
|
185 | 185 | break |
|
186 | 186 | raise error.Abort(_("log stream exception '%s'") % entry) |
|
187 | 187 | yield entry |
|
188 | 188 | |
|
189 | 189 | def close(self): |
|
190 | 190 | if self._stdout: |
|
191 | 191 | self._stdout.close() |
|
192 | 192 | self._stdout = None |
|
193 | 193 | |
|
194 | 194 | class directlogstream(list): |
|
195 | 195 | """Direct revision log iterator. |
|
196 | 196 | This can be used for debugging and development but it will probably leak |
|
197 | 197 | memory and is not suitable for real conversions.""" |
|
198 | 198 | def __init__(self, url, paths, start, end, limit=0, |
|
199 | 199 | discover_changed_paths=True, strict_node_history=False): |
|
200 | 200 | |
|
201 | 201 | def receiver(orig_paths, revnum, author, date, message, pool): |
|
202 | 202 | paths = {} |
|
203 | 203 | if orig_paths is not None: |
|
204 | 204 | for k, v in orig_paths.iteritems(): |
|
205 | 205 | paths[k] = changedpath(v) |
|
206 | 206 | self.append((paths, revnum, author, date, message)) |
|
207 | 207 | |
|
208 | 208 | # Use an ra of our own so that our parent can consume |
|
209 | 209 | # our results without confusing the server. |
|
210 | 210 | t = transport.SvnRaTransport(url=url) |
|
211 | 211 | svn.ra.get_log(t.ra, paths, start, end, limit, |
|
212 | 212 | discover_changed_paths, |
|
213 | 213 | strict_node_history, |
|
214 | 214 | receiver) |
|
215 | 215 | |
|
216 | 216 | def close(self): |
|
217 | 217 | pass |
|
218 | 218 | |
|
219 | 219 | # Check to see if the given path is a local Subversion repo. Verify this by |
|
220 | 220 | # looking for several svn-specific files and directories in the given |
|
221 | 221 | # directory. |
|
222 | 222 | def filecheck(ui, path, proto): |
|
223 | 223 | for x in ('locks', 'hooks', 'format', 'db'): |
|
224 | 224 | if not os.path.exists(os.path.join(path, x)): |
|
225 | 225 | return False |
|
226 | 226 | return True |
|
227 | 227 | |
|
228 | 228 | # Check to see if a given path is the root of an svn repo over http. We verify |
|
229 | 229 | # this by requesting a version-controlled URL we know can't exist and looking |
|
230 | 230 | # for the svn-specific "not found" XML. |
|
231 | 231 | def httpcheck(ui, path, proto): |
|
232 | 232 | try: |
|
233 | 233 | opener = urlreq.buildopener() |
|
234 | 234 | rsp = opener.open('%s://%s/!svn/ver/0/.svn' % (proto, path)) |
|
235 | 235 | data = rsp.read() |
|
236 | 236 | except urlerr.httperror as inst: |
|
237 | 237 | if inst.code != 404: |
|
238 | 238 | # Except for 404 we cannot know for sure this is not an svn repo |
|
239 | 239 | ui.warn(_('svn: cannot probe remote repository, assume it could ' |
|
240 | 240 | 'be a subversion repository. Use --source-type if you ' |
|
241 | 241 | 'know better.\n')) |
|
242 | 242 | return True |
|
243 | 243 | data = inst.fp.read() |
|
244 | 244 | except Exception: |
|
245 | 245 | # Could be urlerr.urlerror if the URL is invalid or anything else. |
|
246 | 246 | return False |
|
247 | 247 | return '<m:human-readable errcode="160013">' in data |
|
248 | 248 | |
|
249 | 249 | protomap = {'http': httpcheck, |
|
250 | 250 | 'https': httpcheck, |
|
251 | 251 | 'file': filecheck, |
|
252 | 252 | } |
|
253 | 253 | def issvnurl(ui, url): |
|
254 | 254 | try: |
|
255 | 255 | proto, path = url.split('://', 1) |
|
256 | 256 | if proto == 'file': |
|
257 | 257 | if (pycompat.osname == 'nt' and path[:1] == '/' |
|
258 | 258 | and path[1:2].isalpha() and path[2:6].lower() == '%3a/'): |
|
259 | 259 | path = path[:2] + ':/' + path[6:] |
|
260 | 260 | path = urlreq.url2pathname(path) |
|
261 | 261 | except ValueError: |
|
262 | 262 | proto = 'file' |
|
263 | 263 | path = os.path.abspath(url) |
|
264 | 264 | if proto == 'file': |
|
265 | 265 | path = util.pconvert(path) |
|
266 | 266 | check = protomap.get(proto, lambda *args: False) |
|
267 | 267 | while '/' in path: |
|
268 | 268 | if check(ui, path, proto): |
|
269 | 269 | return True |
|
270 | 270 | path = path.rsplit('/', 1)[0] |
|
271 | 271 | return False |
|
272 | 272 | |
|
273 | 273 | # SVN conversion code stolen from bzr-svn and tailor |
|
274 | 274 | # |
|
275 | 275 | # Subversion looks like a versioned filesystem, branches structures |
|
276 | 276 | # are defined by conventions and not enforced by the tool. First, |
|
277 | 277 | # we define the potential branches (modules) as "trunk" and "branches" |
|
278 | 278 | # children directories. Revisions are then identified by their |
|
279 | 279 | # module and revision number (and a repository identifier). |
|
280 | 280 | # |
|
281 | 281 | # The revision graph is really a tree (or a forest). By default, a |
|
282 | 282 | # revision parent is the previous revision in the same module. If the |
|
283 | 283 | # module directory is copied/moved from another module then the |
|
284 | 284 | # revision is the module root and its parent the source revision in |
|
285 | 285 | # the parent module. A revision has at most one parent. |
|
286 | 286 | # |
|
287 | 287 | class svn_source(converter_source): |
|
288 | 288 | def __init__(self, ui, url, revs=None): |
|
289 | 289 | super(svn_source, self).__init__(ui, url, revs=revs) |
|
290 | 290 | |
|
291 | 291 | if not (url.startswith('svn://') or url.startswith('svn+ssh://') or |
|
292 | 292 | (os.path.exists(url) and |
|
293 | 293 | os.path.exists(os.path.join(url, '.svn'))) or |
|
294 | 294 | issvnurl(ui, url)): |
|
295 | 295 | raise NoRepo(_("%s does not look like a Subversion repository") |
|
296 | 296 | % url) |
|
297 | 297 | if svn is None: |
|
298 | 298 | raise MissingTool(_('could not load Subversion python bindings')) |
|
299 | 299 | |
|
300 | 300 | try: |
|
301 | 301 | version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR |
|
302 | 302 | if version < (1, 4): |
|
303 | 303 | raise MissingTool(_('Subversion python bindings %d.%d found, ' |
|
304 | 304 | '1.4 or later required') % version) |
|
305 | 305 | except AttributeError: |
|
306 | 306 | raise MissingTool(_('Subversion python bindings are too old, 1.4 ' |
|
307 | 307 | 'or later required')) |
|
308 | 308 | |
|
309 | 309 | self.lastrevs = {} |
|
310 | 310 | |
|
311 | 311 | latest = None |
|
312 | 312 | try: |
|
313 | 313 | # Support file://path@rev syntax. Useful e.g. to convert |
|
314 | 314 | # deleted branches. |
|
315 | 315 | at = url.rfind('@') |
|
316 | 316 | if at >= 0: |
|
317 | 317 | latest = int(url[at + 1:]) |
|
318 | 318 | url = url[:at] |
|
319 | 319 | except ValueError: |
|
320 | 320 | pass |
|
321 | 321 | self.url = geturl(url) |
|
322 | 322 | self.encoding = 'UTF-8' # Subversion is always nominal UTF-8 |
|
323 | 323 | try: |
|
324 | 324 | self.transport = transport.SvnRaTransport(url=self.url) |
|
325 | 325 | self.ra = self.transport.ra |
|
326 | 326 | self.ctx = self.transport.client |
|
327 | 327 | self.baseurl = svn.ra.get_repos_root(self.ra) |
|
328 | 328 | # Module is either empty or a repository path starting with |
|
329 | 329 | # a slash and not ending with a slash. |
|
330 | 330 | self.module = urlreq.unquote(self.url[len(self.baseurl):]) |
|
331 | 331 | self.prevmodule = None |
|
332 | 332 | self.rootmodule = self.module |
|
333 | 333 | self.commits = {} |
|
334 | 334 | self.paths = {} |
|
335 | 335 | self.uuid = svn.ra.get_uuid(self.ra) |
|
336 | 336 | except svn.core.SubversionException: |
|
337 | 337 | ui.traceback() |
|
338 | 338 | svnversion = '%d.%d.%d' % (svn.core.SVN_VER_MAJOR, |
|
339 | 339 | svn.core.SVN_VER_MINOR, |
|
340 | 340 | svn.core.SVN_VER_MICRO) |
|
341 | 341 | raise NoRepo(_("%s does not look like a Subversion repository " |
|
342 | 342 | "to libsvn version %s") |
|
343 | 343 | % (self.url, svnversion)) |
|
344 | 344 | |
|
345 | 345 | if revs: |
|
346 | 346 | if len(revs) > 1: |
|
347 | 347 | raise error.Abort(_('subversion source does not support ' |
|
348 | 348 | 'specifying multiple revisions')) |
|
349 | 349 | try: |
|
350 | 350 | latest = int(revs[0]) |
|
351 | 351 | except ValueError: |
|
352 | 352 | raise error.Abort(_('svn: revision %s is not an integer') % |
|
353 | 353 | revs[0]) |
|
354 | 354 | |
|
355 | 355 | self.trunkname = self.ui.config('convert', 'svn.trunk', |
|
356 | 356 | 'trunk').strip('/') |
|
357 | 357 | self.startrev = self.ui.config('convert', 'svn.startrev', default=0) |
|
358 | 358 | try: |
|
359 | 359 | self.startrev = int(self.startrev) |
|
360 | 360 | if self.startrev < 0: |
|
361 | 361 | self.startrev = 0 |
|
362 | 362 | except ValueError: |
|
363 | 363 | raise error.Abort(_('svn: start revision %s is not an integer') |
|
364 | 364 | % self.startrev) |
|
365 | 365 | |
|
366 | 366 | try: |
|
367 | 367 | self.head = self.latest(self.module, latest) |
|
368 | 368 | except SvnPathNotFound: |
|
369 | 369 | self.head = None |
|
370 | 370 | if not self.head: |
|
371 | 371 | raise error.Abort(_('no revision found in module %s') |
|
372 | 372 | % self.module) |
|
373 | 373 | self.last_changed = self.revnum(self.head) |
|
374 | 374 | |
|
375 | 375 | self._changescache = (None, None) |
|
376 | 376 | |
|
377 | 377 | if os.path.exists(os.path.join(url, '.svn/entries')): |
|
378 | 378 | self.wc = url |
|
379 | 379 | else: |
|
380 | 380 | self.wc = None |
|
381 | 381 | self.convertfp = None |
|
382 | 382 | |
|
383 | 383 | def setrevmap(self, revmap): |
|
384 | 384 | lastrevs = {} |
|
385 | 385 | for revid in revmap.iterkeys(): |
|
386 | 386 | uuid, module, revnum = revsplit(revid) |
|
387 | 387 | lastrevnum = lastrevs.setdefault(module, revnum) |
|
388 | 388 | if revnum > lastrevnum: |
|
389 | 389 | lastrevs[module] = revnum |
|
390 | 390 | self.lastrevs = lastrevs |
|
391 | 391 | |
|
392 | 392 | def exists(self, path, optrev): |
|
393 | 393 | try: |
|
394 | 394 | svn.client.ls(self.url.rstrip('/') + '/' + quote(path), |
|
395 | 395 | optrev, False, self.ctx) |
|
396 | 396 | return True |
|
397 | 397 | except svn.core.SubversionException: |
|
398 | 398 | return False |
|
399 | 399 | |
|
400 | 400 | def getheads(self): |
|
401 | 401 | |
|
402 | 402 | def isdir(path, revnum): |
|
403 | 403 | kind = self._checkpath(path, revnum) |
|
404 | 404 | return kind == svn.core.svn_node_dir |
|
405 | 405 | |
|
406 | 406 | def getcfgpath(name, rev): |
|
407 | 407 | cfgpath = self.ui.config('convert', 'svn.' + name) |
|
408 | 408 | if cfgpath is not None and cfgpath.strip() == '': |
|
409 | 409 | return None |
|
410 | 410 | path = (cfgpath or name).strip('/') |
|
411 | 411 | if not self.exists(path, rev): |
|
412 | 412 | if self.module.endswith(path) and name == 'trunk': |
|
413 | 413 | # we are converting from inside this directory |
|
414 | 414 | return None |
|
415 | 415 | if cfgpath: |
|
416 | 416 | raise error.Abort(_('expected %s to be at %r, but not found' |
|
417 | 417 | ) % (name, path)) |
|
418 | 418 | return None |
|
419 | 419 | self.ui.note(_('found %s at %r\n') % (name, path)) |
|
420 | 420 | return path |
|
421 | 421 | |
|
422 | 422 | rev = optrev(self.last_changed) |
|
423 | 423 | oldmodule = '' |
|
424 | 424 | trunk = getcfgpath('trunk', rev) |
|
425 | 425 | self.tags = getcfgpath('tags', rev) |
|
426 | 426 | branches = getcfgpath('branches', rev) |
|
427 | 427 | |
|
428 | 428 | # If the project has a trunk or branches, we will extract heads |
|
429 | 429 | # from them. We keep the project root otherwise. |
|
430 | 430 | if trunk: |
|
431 | 431 | oldmodule = self.module or '' |
|
432 | 432 | self.module += '/' + trunk |
|
433 | 433 | self.head = self.latest(self.module, self.last_changed) |
|
434 | 434 | if not self.head: |
|
435 | 435 | raise error.Abort(_('no revision found in module %s') |
|
436 | 436 | % self.module) |
|
437 | 437 | |
|
438 | 438 | # First head in the list is the module's head |
|
439 | 439 | self.heads = [self.head] |
|
440 | 440 | if self.tags is not None: |
|
441 | 441 | self.tags = '%s/%s' % (oldmodule , (self.tags or 'tags')) |
|
442 | 442 | |
|
443 | 443 | # Check if branches bring a few more heads to the list |
|
444 | 444 | if branches: |
|
445 | 445 | rpath = self.url.strip('/') |
|
446 | 446 | branchnames = svn.client.ls(rpath + '/' + quote(branches), |
|
447 | 447 | rev, False, self.ctx) |
|
448 | 448 | for branch in sorted(branchnames): |
|
449 | 449 | module = '%s/%s/%s' % (oldmodule, branches, branch) |
|
450 | 450 | if not isdir(module, self.last_changed): |
|
451 | 451 | continue |
|
452 | 452 | brevid = self.latest(module, self.last_changed) |
|
453 | 453 | if not brevid: |
|
454 | 454 | self.ui.note(_('ignoring empty branch %s\n') % branch) |
|
455 | 455 | continue |
|
456 | 456 | self.ui.note(_('found branch %s at %d\n') % |
|
457 | 457 | (branch, self.revnum(brevid))) |
|
458 | 458 | self.heads.append(brevid) |
|
459 | 459 | |
|
460 | 460 | if self.startrev and self.heads: |
|
461 | 461 | if len(self.heads) > 1: |
|
462 | 462 | raise error.Abort(_('svn: start revision is not supported ' |
|
463 | 463 | 'with more than one branch')) |
|
464 | 464 | revnum = self.revnum(self.heads[0]) |
|
465 | 465 | if revnum < self.startrev: |
|
466 | 466 | raise error.Abort( |
|
467 | 467 | _('svn: no revision found after start revision %d') |
|
468 | 468 | % self.startrev) |
|
469 | 469 | |
|
470 | 470 | return self.heads |
|
471 | 471 | |
|
472 | 472 | def _getchanges(self, rev, full): |
|
473 | 473 | (paths, parents) = self.paths[rev] |
|
474 | 474 | copies = {} |
|
475 | 475 | if parents: |
|
476 | 476 | files, self.removed, copies = self.expandpaths(rev, paths, parents) |
|
477 | 477 | if full or not parents: |
|
478 | 478 | # Perform a full checkout on roots |
|
479 | 479 | uuid, module, revnum = revsplit(rev) |
|
480 | 480 | entries = svn.client.ls(self.baseurl + quote(module), |
|
481 | 481 | optrev(revnum), True, self.ctx) |
|
482 | 482 | files = [n for n, e in entries.iteritems() |
|
483 | 483 | if e.kind == svn.core.svn_node_file] |
|
484 | 484 | self.removed = set() |
|
485 | 485 | |
|
486 | 486 | files.sort() |
|
487 | 487 | files = zip(files, [rev] * len(files)) |
|
488 | 488 | return (files, copies) |
|
489 | 489 | |
|
490 | 490 | def getchanges(self, rev, full): |
|
491 | 491 | # reuse cache from getchangedfiles |
|
492 | 492 | if self._changescache[0] == rev and not full: |
|
493 | 493 | (files, copies) = self._changescache[1] |
|
494 | 494 | else: |
|
495 | 495 | (files, copies) = self._getchanges(rev, full) |
|
496 | 496 | # caller caches the result, so free it here to release memory |
|
497 | 497 | del self.paths[rev] |
|
498 | 498 | return (files, copies, set()) |
|
499 | 499 | |
|
500 | 500 | def getchangedfiles(self, rev, i): |
|
501 | 501 | # called from filemap - cache computed values for reuse in getchanges |
|
502 | 502 | (files, copies) = self._getchanges(rev, False) |
|
503 | 503 | self._changescache = (rev, (files, copies)) |
|
504 | 504 | return [f[0] for f in files] |
|
505 | 505 | |
|
506 | 506 | def getcommit(self, rev): |
|
507 | 507 | if rev not in self.commits: |
|
508 | 508 | uuid, module, revnum = revsplit(rev) |
|
509 | 509 | self.module = module |
|
510 | 510 | self.reparent(module) |
|
511 | 511 | # We assume that: |
|
512 | 512 | # - requests for revisions after "stop" come from the |
|
513 | 513 | # revision graph backward traversal. Cache all of them |
|
514 | 514 | # down to stop, they will be used eventually. |
|
515 | 515 | # - requests for revisions before "stop" come to get |
|
516 | 516 | # isolated branches parents. Just fetch what is needed. |
|
517 | 517 | stop = self.lastrevs.get(module, 0) |
|
518 | 518 | if revnum < stop: |
|
519 | 519 | stop = revnum + 1 |
|
520 | 520 | self._fetch_revisions(revnum, stop) |
|
521 | 521 | if rev not in self.commits: |
|
522 | 522 | raise error.Abort(_('svn: revision %s not found') % revnum) |
|
523 | 523 | revcommit = self.commits[rev] |
|
524 | 524 | # caller caches the result, so free it here to release memory |
|
525 | 525 | del self.commits[rev] |
|
526 | 526 | return revcommit |
|
527 | 527 | |
|
528 | 528 | def checkrevformat(self, revstr, mapname='splicemap'): |
|
529 | 529 | """ fails if revision format does not match the correct format""" |
|
530 | 530 | if not re.match(r'svn:[0-9a-f]{8,8}-[0-9a-f]{4,4}-' |
|
531 | 531 | r'[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]' |
|
532 | 532 | r'{12,12}(.*)\@[0-9]+$',revstr): |
|
533 | 533 | raise error.Abort(_('%s entry %s is not a valid revision' |
|
534 | 534 | ' identifier') % (mapname, revstr)) |
|
535 | 535 | |
|
536 | 536 | def numcommits(self): |
|
537 | 537 | return int(self.head.rsplit('@', 1)[1]) - self.startrev |
|
538 | 538 | |
|
539 | 539 | def gettags(self): |
|
540 | 540 | tags = {} |
|
541 | 541 | if self.tags is None: |
|
542 | 542 | return tags |
|
543 | 543 | |
|
544 | 544 | # svn tags are just a convention, project branches left in a |
|
545 | 545 | # 'tags' directory. There is no other relationship than |
|
546 | 546 | # ancestry, which is expensive to discover and makes them hard |
|
547 | 547 | # to update incrementally. Worse, past revisions may be |
|
548 | 548 | # referenced by tags far away in the future, requiring a deep |
|
549 | 549 | # history traversal on every calculation. Current code |
|
550 | 550 | # performs a single backward traversal, tracking moves within |
|
551 | 551 | # the tags directory (tag renaming) and recording a new tag |
|
552 | 552 | # everytime a project is copied from outside the tags |
|
553 | 553 | # directory. It also lists deleted tags, this behaviour may |
|
554 | 554 | # change in the future. |
|
555 | 555 | pendings = [] |
|
556 | 556 | tagspath = self.tags |
|
557 | 557 | start = svn.ra.get_latest_revnum(self.ra) |
|
558 | 558 | stream = self._getlog([self.tags], start, self.startrev) |
|
559 | 559 | try: |
|
560 | 560 | for entry in stream: |
|
561 | 561 | origpaths, revnum, author, date, message = entry |
|
562 | 562 | if not origpaths: |
|
563 | 563 | origpaths = [] |
|
564 | 564 | copies = [(e.copyfrom_path, e.copyfrom_rev, p) for p, e |
|
565 | 565 | in origpaths.iteritems() if e.copyfrom_path] |
|
566 | 566 | # Apply moves/copies from more specific to general |
|
567 | 567 | copies.sort(reverse=True) |
|
568 | 568 | |
|
569 | 569 | srctagspath = tagspath |
|
570 | 570 | if copies and copies[-1][2] == tagspath: |
|
571 | 571 | # Track tags directory moves |
|
572 | 572 | srctagspath = copies.pop()[0] |
|
573 | 573 | |
|
574 | 574 | for source, sourcerev, dest in copies: |
|
575 | 575 | if not dest.startswith(tagspath + '/'): |
|
576 | 576 | continue |
|
577 | 577 | for tag in pendings: |
|
578 | 578 | if tag[0].startswith(dest): |
|
579 | 579 | tagpath = source + tag[0][len(dest):] |
|
580 | 580 | tag[:2] = [tagpath, sourcerev] |
|
581 | 581 | break |
|
582 | 582 | else: |
|
583 | 583 | pendings.append([source, sourcerev, dest]) |
|
584 | 584 | |
|
585 | 585 | # Filter out tags with children coming from different |
|
586 | 586 | # parts of the repository like: |
|
587 | 587 | # /tags/tag.1 (from /trunk:10) |
|
588 | 588 | # /tags/tag.1/foo (from /branches/foo:12) |
|
589 | 589 | # Here/tags/tag.1 discarded as well as its children. |
|
590 | 590 | # It happens with tools like cvs2svn. Such tags cannot |
|
591 | 591 | # be represented in mercurial. |
|
592 | 592 | addeds = dict((p, e.copyfrom_path) for p, e |
|
593 | 593 | in origpaths.iteritems() |
|
594 | 594 | if e.action == 'A' and e.copyfrom_path) |
|
595 | 595 | badroots = set() |
|
596 | 596 | for destroot in addeds: |
|
597 | 597 | for source, sourcerev, dest in pendings: |
|
598 | 598 | if (not dest.startswith(destroot + '/') |
|
599 | 599 | or source.startswith(addeds[destroot] + '/')): |
|
600 | 600 | continue |
|
601 | 601 | badroots.add(destroot) |
|
602 | 602 | break |
|
603 | 603 | |
|
604 | 604 | for badroot in badroots: |
|
605 | 605 | pendings = [p for p in pendings if p[2] != badroot |
|
606 | 606 | and not p[2].startswith(badroot + '/')] |
|
607 | 607 | |
|
608 | 608 | # Tell tag renamings from tag creations |
|
609 | 609 | renamings = [] |
|
610 | 610 | for source, sourcerev, dest in pendings: |
|
611 | 611 | tagname = dest.split('/')[-1] |
|
612 | 612 | if source.startswith(srctagspath): |
|
613 | 613 | renamings.append([source, sourcerev, tagname]) |
|
614 | 614 | continue |
|
615 | 615 | if tagname in tags: |
|
616 | 616 | # Keep the latest tag value |
|
617 | 617 | continue |
|
618 | 618 | # From revision may be fake, get one with changes |
|
619 | 619 | try: |
|
620 | 620 | tagid = self.latest(source, sourcerev) |
|
621 | 621 | if tagid and tagname not in tags: |
|
622 | 622 | tags[tagname] = tagid |
|
623 | 623 | except SvnPathNotFound: |
|
624 | 624 | # It happens when we are following directories |
|
625 | 625 | # we assumed were copied with their parents |
|
626 | 626 | # but were really created in the tag |
|
627 | 627 | # directory. |
|
628 | 628 | pass |
|
629 | 629 | pendings = renamings |
|
630 | 630 | tagspath = srctagspath |
|
631 | 631 | finally: |
|
632 | 632 | stream.close() |
|
633 | 633 | return tags |
|
634 | 634 | |
|
635 | 635 | def converted(self, rev, destrev): |
|
636 | 636 | if not self.wc: |
|
637 | 637 | return |
|
638 | 638 | if self.convertfp is None: |
|
639 | 639 | self.convertfp = open(os.path.join(self.wc, '.svn', 'hg-shamap'), |
|
640 | 640 | 'a') |
|
641 | 641 | self.convertfp.write('%s %d\n' % (destrev, self.revnum(rev))) |
|
642 | 642 | self.convertfp.flush() |
|
643 | 643 | |
|
644 | 644 | def revid(self, revnum, module=None): |
|
645 | 645 | return 'svn:%s%s@%s' % (self.uuid, module or self.module, revnum) |
|
646 | 646 | |
|
647 | 647 | def revnum(self, rev): |
|
648 | 648 | return int(rev.split('@')[-1]) |
|
649 | 649 | |
|
650 | 650 | def latest(self, path, stop=None): |
|
651 | 651 | """Find the latest revid affecting path, up to stop revision |
|
652 | 652 | number. If stop is None, default to repository latest |
|
653 | 653 | revision. It may return a revision in a different module, |
|
654 | 654 | since a branch may be moved without a change being |
|
655 | 655 | reported. Return None if computed module does not belong to |
|
656 | 656 | rootmodule subtree. |
|
657 | 657 | """ |
|
658 | 658 | def findchanges(path, start, stop=None): |
|
659 | 659 | stream = self._getlog([path], start, stop or 1) |
|
660 | 660 | try: |
|
661 | 661 | for entry in stream: |
|
662 | 662 | paths, revnum, author, date, message = entry |
|
663 | 663 | if stop is None and paths: |
|
664 | 664 | # We do not know the latest changed revision, |
|
665 | 665 | # keep the first one with changed paths. |
|
666 | 666 | break |
|
667 | 667 | if revnum <= stop: |
|
668 | 668 | break |
|
669 | 669 | |
|
670 | 670 | for p in paths: |
|
671 | 671 | if (not path.startswith(p) or |
|
672 | 672 | not paths[p].copyfrom_path): |
|
673 | 673 | continue |
|
674 | 674 | newpath = paths[p].copyfrom_path + path[len(p):] |
|
675 | 675 | self.ui.debug("branch renamed from %s to %s at %d\n" % |
|
676 | 676 | (path, newpath, revnum)) |
|
677 | 677 | path = newpath |
|
678 | 678 | break |
|
679 | 679 | if not paths: |
|
680 | 680 | revnum = None |
|
681 | 681 | return revnum, path |
|
682 | 682 | finally: |
|
683 | 683 | stream.close() |
|
684 | 684 | |
|
685 | 685 | if not path.startswith(self.rootmodule): |
|
686 | 686 | # Requests on foreign branches may be forbidden at server level |
|
687 | 687 | self.ui.debug('ignoring foreign branch %r\n' % path) |
|
688 | 688 | return None |
|
689 | 689 | |
|
690 | 690 | if stop is None: |
|
691 | 691 | stop = svn.ra.get_latest_revnum(self.ra) |
|
692 | 692 | try: |
|
693 | 693 | prevmodule = self.reparent('') |
|
694 | 694 | dirent = svn.ra.stat(self.ra, path.strip('/'), stop) |
|
695 | 695 | self.reparent(prevmodule) |
|
696 | 696 | except svn.core.SubversionException: |
|
697 | 697 | dirent = None |
|
698 | 698 | if not dirent: |
|
699 | 699 | raise SvnPathNotFound(_('%s not found up to revision %d') |
|
700 | 700 | % (path, stop)) |
|
701 | 701 | |
|
702 | 702 | # stat() gives us the previous revision on this line of |
|
703 | 703 | # development, but it might be in *another module*. Fetch the |
|
704 | 704 | # log and detect renames down to the latest revision. |
|
705 | 705 | revnum, realpath = findchanges(path, stop, dirent.created_rev) |
|
706 | 706 | if revnum is None: |
|
707 | 707 | # Tools like svnsync can create empty revision, when |
|
708 | 708 | # synchronizing only a subtree for instance. These empty |
|
709 | 709 | # revisions created_rev still have their original values |
|
710 | 710 | # despite all changes having disappeared and can be |
|
711 | 711 | # returned by ra.stat(), at least when stating the root |
|
712 | 712 | # module. In that case, do not trust created_rev and scan |
|
713 | 713 | # the whole history. |
|
714 | 714 | revnum, realpath = findchanges(path, stop) |
|
715 | 715 | if revnum is None: |
|
716 | 716 | self.ui.debug('ignoring empty branch %r\n' % realpath) |
|
717 | 717 | return None |
|
718 | 718 | |
|
719 | 719 | if not realpath.startswith(self.rootmodule): |
|
720 | 720 | self.ui.debug('ignoring foreign branch %r\n' % realpath) |
|
721 | 721 | return None |
|
722 | 722 | return self.revid(revnum, realpath) |
|
723 | 723 | |
|
724 | 724 | def reparent(self, module): |
|
725 | 725 | """Reparent the svn transport and return the previous parent.""" |
|
726 | 726 | if self.prevmodule == module: |
|
727 | 727 | return module |
|
728 | 728 | svnurl = self.baseurl + quote(module) |
|
729 | 729 | prevmodule = self.prevmodule |
|
730 | 730 | if prevmodule is None: |
|
731 | 731 | prevmodule = '' |
|
732 | 732 | self.ui.debug("reparent to %s\n" % svnurl) |
|
733 | 733 | svn.ra.reparent(self.ra, svnurl) |
|
734 | 734 | self.prevmodule = module |
|
735 | 735 | return prevmodule |
|
736 | 736 | |
|
737 | 737 | def expandpaths(self, rev, paths, parents): |
|
738 | 738 | changed, removed = set(), set() |
|
739 | 739 | copies = {} |
|
740 | 740 | |
|
741 | 741 | new_module, revnum = revsplit(rev)[1:] |
|
742 | 742 | if new_module != self.module: |
|
743 | 743 | self.module = new_module |
|
744 | 744 | self.reparent(self.module) |
|
745 | 745 | |
|
746 | 746 | for i, (path, ent) in enumerate(paths): |
|
747 | 747 | self.ui.progress(_('scanning paths'), i, item=path, |
|
748 | 748 | total=len(paths), unit=_('paths')) |
|
749 | 749 | entrypath = self.getrelpath(path) |
|
750 | 750 | |
|
751 | 751 | kind = self._checkpath(entrypath, revnum) |
|
752 | 752 | if kind == svn.core.svn_node_file: |
|
753 | 753 | changed.add(self.recode(entrypath)) |
|
754 | 754 | if not ent.copyfrom_path or not parents: |
|
755 | 755 | continue |
|
756 | 756 | # Copy sources not in parent revisions cannot be |
|
757 | 757 | # represented, ignore their origin for now |
|
758 | 758 | pmodule, prevnum = revsplit(parents[0])[1:] |
|
759 | 759 | if ent.copyfrom_rev < prevnum: |
|
760 | 760 | continue |
|
761 | 761 | copyfrom_path = self.getrelpath(ent.copyfrom_path, pmodule) |
|
762 | 762 | if not copyfrom_path: |
|
763 | 763 | continue |
|
764 | 764 | self.ui.debug("copied to %s from %s@%s\n" % |
|
765 | 765 | (entrypath, copyfrom_path, ent.copyfrom_rev)) |
|
766 | 766 | copies[self.recode(entrypath)] = self.recode(copyfrom_path) |
|
767 | 767 | elif kind == 0: # gone, but had better be a deleted *file* |
|
768 | 768 | self.ui.debug("gone from %s\n" % ent.copyfrom_rev) |
|
769 | 769 | pmodule, prevnum = revsplit(parents[0])[1:] |
|
770 | 770 | parentpath = pmodule + "/" + entrypath |
|
771 | 771 | fromkind = self._checkpath(entrypath, prevnum, pmodule) |
|
772 | 772 | |
|
773 | 773 | if fromkind == svn.core.svn_node_file: |
|
774 | 774 | removed.add(self.recode(entrypath)) |
|
775 | 775 | elif fromkind == svn.core.svn_node_dir: |
|
776 | 776 | oroot = parentpath.strip('/') |
|
777 | 777 | nroot = path.strip('/') |
|
778 | 778 | children = self._iterfiles(oroot, prevnum) |
|
779 | 779 | for childpath in children: |
|
780 | 780 | childpath = childpath.replace(oroot, nroot) |
|
781 | 781 | childpath = self.getrelpath("/" + childpath, pmodule) |
|
782 | 782 | if childpath: |
|
783 | 783 | removed.add(self.recode(childpath)) |
|
784 | 784 | else: |
|
785 | 785 | self.ui.debug('unknown path in revision %d: %s\n' % \ |
|
786 | 786 | (revnum, path)) |
|
787 | 787 | elif kind == svn.core.svn_node_dir: |
|
788 | 788 | if ent.action == 'M': |
|
789 | 789 | # If the directory just had a prop change, |
|
790 | 790 | # then we shouldn't need to look for its children. |
|
791 | 791 | continue |
|
792 | 792 | if ent.action == 'R' and parents: |
|
793 | 793 | # If a directory is replacing a file, mark the previous |
|
794 | 794 | # file as deleted |
|
795 | 795 | pmodule, prevnum = revsplit(parents[0])[1:] |
|
796 | 796 | pkind = self._checkpath(entrypath, prevnum, pmodule) |
|
797 | 797 | if pkind == svn.core.svn_node_file: |
|
798 | 798 | removed.add(self.recode(entrypath)) |
|
799 | 799 | elif pkind == svn.core.svn_node_dir: |
|
800 | 800 | # We do not know what files were kept or removed, |
|
801 | 801 | # mark them all as changed. |
|
802 | 802 | for childpath in self._iterfiles(pmodule, prevnum): |
|
803 | 803 | childpath = self.getrelpath("/" + childpath) |
|
804 | 804 | if childpath: |
|
805 | 805 | changed.add(self.recode(childpath)) |
|
806 | 806 | |
|
807 | 807 | for childpath in self._iterfiles(path, revnum): |
|
808 | 808 | childpath = self.getrelpath("/" + childpath) |
|
809 | 809 | if childpath: |
|
810 | 810 | changed.add(self.recode(childpath)) |
|
811 | 811 | |
|
812 | 812 | # Handle directory copies |
|
813 | 813 | if not ent.copyfrom_path or not parents: |
|
814 | 814 | continue |
|
815 | 815 | # Copy sources not in parent revisions cannot be |
|
816 | 816 | # represented, ignore their origin for now |
|
817 | 817 | pmodule, prevnum = revsplit(parents[0])[1:] |
|
818 | 818 | if ent.copyfrom_rev < prevnum: |
|
819 | 819 | continue |
|
820 | 820 | copyfrompath = self.getrelpath(ent.copyfrom_path, pmodule) |
|
821 | 821 | if not copyfrompath: |
|
822 | 822 | continue |
|
823 | 823 | self.ui.debug("mark %s came from %s:%d\n" |
|
824 | 824 | % (path, copyfrompath, ent.copyfrom_rev)) |
|
825 | 825 | children = self._iterfiles(ent.copyfrom_path, ent.copyfrom_rev) |
|
826 | 826 | for childpath in children: |
|
827 | 827 | childpath = self.getrelpath("/" + childpath, pmodule) |
|
828 | 828 | if not childpath: |
|
829 | 829 | continue |
|
830 | 830 | copytopath = path + childpath[len(copyfrompath):] |
|
831 | 831 | copytopath = self.getrelpath(copytopath) |
|
832 | 832 | copies[self.recode(copytopath)] = self.recode(childpath) |
|
833 | 833 | |
|
834 | 834 | self.ui.progress(_('scanning paths'), None) |
|
835 | 835 | changed.update(removed) |
|
836 | 836 | return (list(changed), removed, copies) |
|
837 | 837 | |
|
838 | 838 | def _fetch_revisions(self, from_revnum, to_revnum): |
|
839 | 839 | if from_revnum < to_revnum: |
|
840 | 840 | from_revnum, to_revnum = to_revnum, from_revnum |
|
841 | 841 | |
|
842 | 842 | self.child_cset = None |
|
843 | 843 | |
|
844 | 844 | def parselogentry(orig_paths, revnum, author, date, message): |
|
845 | 845 | """Return the parsed commit object or None, and True if |
|
846 | 846 | the revision is a branch root. |
|
847 | 847 | """ |
|
848 | 848 | self.ui.debug("parsing revision %d (%d changes)\n" % |
|
849 | 849 | (revnum, len(orig_paths))) |
|
850 | 850 | |
|
851 | 851 | branched = False |
|
852 | 852 | rev = self.revid(revnum) |
|
853 | 853 | # branch log might return entries for a parent we already have |
|
854 | 854 | |
|
855 | 855 | if rev in self.commits or revnum < to_revnum: |
|
856 | 856 | return None, branched |
|
857 | 857 | |
|
858 | 858 | parents = [] |
|
859 | 859 | # check whether this revision is the start of a branch or part |
|
860 | 860 | # of a branch renaming |
|
861 | 861 | orig_paths = sorted(orig_paths.iteritems()) |
|
862 | 862 | root_paths = [(p, e) for p, e in orig_paths |
|
863 | 863 | if self.module.startswith(p)] |
|
864 | 864 | if root_paths: |
|
865 | 865 | path, ent = root_paths[-1] |
|
866 | 866 | if ent.copyfrom_path: |
|
867 | 867 | branched = True |
|
868 | 868 | newpath = ent.copyfrom_path + self.module[len(path):] |
|
869 | 869 | # ent.copyfrom_rev may not be the actual last revision |
|
870 | 870 | previd = self.latest(newpath, ent.copyfrom_rev) |
|
871 | 871 | if previd is not None: |
|
872 | 872 | prevmodule, prevnum = revsplit(previd)[1:] |
|
873 | 873 | if prevnum >= self.startrev: |
|
874 | 874 | parents = [previd] |
|
875 | 875 | self.ui.note( |
|
876 | 876 | _('found parent of branch %s at %d: %s\n') % |
|
877 | 877 | (self.module, prevnum, prevmodule)) |
|
878 | 878 | else: |
|
879 | 879 | self.ui.debug("no copyfrom path, don't know what to do.\n") |
|
880 | 880 | |
|
881 | 881 | paths = [] |
|
882 | 882 | # filter out unrelated paths |
|
883 | 883 | for path, ent in orig_paths: |
|
884 | 884 | if self.getrelpath(path) is None: |
|
885 | 885 | continue |
|
886 | 886 | paths.append((path, ent)) |
|
887 | 887 | |
|
888 | 888 | # Example SVN datetime. Includes microseconds. |
|
889 | 889 | # ISO-8601 conformant |
|
890 | 890 | # '2007-01-04T17:35:00.902377Z' |
|
891 | 891 | date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"]) |
|
892 | 892 | if self.ui.configbool('convert', 'localtimezone'): |
|
893 | 893 | date = makedatetimestamp(date[0]) |
|
894 | 894 | |
|
895 | 895 | if message: |
|
896 | 896 | log = self.recode(message) |
|
897 | 897 | else: |
|
898 | 898 | log = '' |
|
899 | 899 | |
|
900 | 900 | if author: |
|
901 | 901 | author = self.recode(author) |
|
902 | 902 | else: |
|
903 | 903 | author = '' |
|
904 | 904 | |
|
905 | 905 | try: |
|
906 | 906 | branch = self.module.split("/")[-1] |
|
907 | 907 | if branch == self.trunkname: |
|
908 | 908 | branch = None |
|
909 | 909 | except IndexError: |
|
910 | 910 | branch = None |
|
911 | 911 | |
|
912 | 912 | cset = commit(author=author, |
|
913 | 913 | date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'), |
|
914 | 914 | desc=log, |
|
915 | 915 | parents=parents, |
|
916 | 916 | branch=branch, |
|
917 | 917 | rev=rev) |
|
918 | 918 | |
|
919 | 919 | self.commits[rev] = cset |
|
920 | 920 | # The parents list is *shared* among self.paths and the |
|
921 | 921 | # commit object. Both will be updated below. |
|
922 | 922 | self.paths[rev] = (paths, cset.parents) |
|
923 | 923 | if self.child_cset and not self.child_cset.parents: |
|
924 | 924 | self.child_cset.parents[:] = [rev] |
|
925 | 925 | self.child_cset = cset |
|
926 | 926 | return cset, branched |
|
927 | 927 | |
|
928 | 928 | self.ui.note(_('fetching revision log for "%s" from %d to %d\n') % |
|
929 | 929 | (self.module, from_revnum, to_revnum)) |
|
930 | 930 | |
|
931 | 931 | try: |
|
932 | 932 | firstcset = None |
|
933 | 933 | lastonbranch = False |
|
934 | 934 | stream = self._getlog([self.module], from_revnum, to_revnum) |
|
935 | 935 | try: |
|
936 | 936 | for entry in stream: |
|
937 | 937 | paths, revnum, author, date, message = entry |
|
938 | 938 | if revnum < self.startrev: |
|
939 | 939 | lastonbranch = True |
|
940 | 940 | break |
|
941 | 941 | if not paths: |
|
942 | 942 | self.ui.debug('revision %d has no entries\n' % revnum) |
|
943 | 943 | # If we ever leave the loop on an empty |
|
944 | 944 | # revision, do not try to get a parent branch |
|
945 | 945 | lastonbranch = lastonbranch or revnum == 0 |
|
946 | 946 | continue |
|
947 | 947 | cset, lastonbranch = parselogentry(paths, revnum, author, |
|
948 | 948 | date, message) |
|
949 | 949 | if cset: |
|
950 | 950 | firstcset = cset |
|
951 | 951 | if lastonbranch: |
|
952 | 952 | break |
|
953 | 953 | finally: |
|
954 | 954 | stream.close() |
|
955 | 955 | |
|
956 | 956 | if not lastonbranch and firstcset and not firstcset.parents: |
|
957 | 957 | # The first revision of the sequence (the last fetched one) |
|
958 | 958 | # has invalid parents if not a branch root. Find the parent |
|
959 | 959 | # revision now, if any. |
|
960 | 960 | try: |
|
961 | 961 | firstrevnum = self.revnum(firstcset.rev) |
|
962 | 962 | if firstrevnum > 1: |
|
963 | 963 | latest = self.latest(self.module, firstrevnum - 1) |
|
964 | 964 | if latest: |
|
965 | 965 | firstcset.parents.append(latest) |
|
966 | 966 | except SvnPathNotFound: |
|
967 | 967 | pass |
|
968 | 968 | except svn.core.SubversionException as xxx_todo_changeme: |
|
969 | 969 | (inst, num) = xxx_todo_changeme.args |
|
970 | 970 | if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION: |
|
971 | 971 | raise error.Abort(_('svn: branch has no revision %s') |
|
972 | 972 | % to_revnum) |
|
973 | 973 | raise |
|
974 | 974 | |
|
975 | 975 | def getfile(self, file, rev): |
|
976 | 976 | # TODO: ra.get_file transmits the whole file instead of diffs. |
|
977 | 977 | if file in self.removed: |
|
978 | 978 | return None, None |
|
979 | 979 | mode = '' |
|
980 | 980 | try: |
|
981 | 981 | new_module, revnum = revsplit(rev)[1:] |
|
982 | 982 | if self.module != new_module: |
|
983 | 983 | self.module = new_module |
|
984 | 984 | self.reparent(self.module) |
|
985 | 985 | io = stringio() |
|
986 | 986 | info = svn.ra.get_file(self.ra, file, revnum, io) |
|
987 | 987 | data = io.getvalue() |
|
988 | 988 | # ra.get_file() seems to keep a reference on the input buffer |
|
989 | 989 | # preventing collection. Release it explicitly. |
|
990 | 990 | io.close() |
|
991 | 991 | if isinstance(info, list): |
|
992 | 992 | info = info[-1] |
|
993 | 993 | mode = ("svn:executable" in info) and 'x' or '' |
|
994 | 994 | mode = ("svn:special" in info) and 'l' or mode |
|
995 | 995 | except svn.core.SubversionException as e: |
|
996 | 996 | notfound = (svn.core.SVN_ERR_FS_NOT_FOUND, |
|
997 | 997 | svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND) |
|
998 | 998 | if e.apr_err in notfound: # File not found |
|
999 | 999 | return None, None |
|
1000 | 1000 | raise |
|
1001 | 1001 | if mode == 'l': |
|
1002 | 1002 | link_prefix = "link " |
|
1003 | 1003 | if data.startswith(link_prefix): |
|
1004 | 1004 | data = data[len(link_prefix):] |
|
1005 | 1005 | return data, mode |
|
1006 | 1006 | |
|
1007 | 1007 | def _iterfiles(self, path, revnum): |
|
1008 | 1008 | """Enumerate all files in path at revnum, recursively.""" |
|
1009 | 1009 | path = path.strip('/') |
|
1010 | 1010 | pool = svn.core.Pool() |
|
1011 | 1011 | rpath = '/'.join([self.baseurl, quote(path)]).strip('/') |
|
1012 | 1012 | entries = svn.client.ls(rpath, optrev(revnum), True, self.ctx, pool) |
|
1013 | 1013 | if path: |
|
1014 | 1014 | path += '/' |
|
1015 | 1015 | return ((path + p) for p, e in entries.iteritems() |
|
1016 | 1016 | if e.kind == svn.core.svn_node_file) |
|
1017 | 1017 | |
|
1018 | 1018 | def getrelpath(self, path, module=None): |
|
1019 | 1019 | if module is None: |
|
1020 | 1020 | module = self.module |
|
1021 | 1021 | # Given the repository url of this wc, say |
|
1022 | 1022 | # "http://server/plone/CMFPlone/branches/Plone-2_0-branch" |
|
1023 | 1023 | # extract the "entry" portion (a relative path) from what |
|
1024 | 1024 | # svn log --xml says, i.e. |
|
1025 | 1025 | # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py" |
|
1026 | 1026 | # that is to say "tests/PloneTestCase.py" |
|
1027 | 1027 | if path.startswith(module): |
|
1028 | 1028 | relative = path.rstrip('/')[len(module):] |
|
1029 | 1029 | if relative.startswith('/'): |
|
1030 | 1030 | return relative[1:] |
|
1031 | 1031 | elif relative == '': |
|
1032 | 1032 | return relative |
|
1033 | 1033 | |
|
1034 | 1034 | # The path is outside our tracked tree... |
|
1035 | 1035 | self.ui.debug('%r is not under %r, ignoring\n' % (path, module)) |
|
1036 | 1036 | return None |
|
1037 | 1037 | |
|
1038 | 1038 | def _checkpath(self, path, revnum, module=None): |
|
1039 | 1039 | if module is not None: |
|
1040 | 1040 | prevmodule = self.reparent('') |
|
1041 | 1041 | path = module + '/' + path |
|
1042 | 1042 | try: |
|
1043 | 1043 | # ra.check_path does not like leading slashes very much, it leads |
|
1044 | 1044 | # to PROPFIND subversion errors |
|
1045 | 1045 | return svn.ra.check_path(self.ra, path.strip('/'), revnum) |
|
1046 | 1046 | finally: |
|
1047 | 1047 | if module is not None: |
|
1048 | 1048 | self.reparent(prevmodule) |
|
1049 | 1049 | |
|
1050 | 1050 | def _getlog(self, paths, start, end, limit=0, discover_changed_paths=True, |
|
1051 | 1051 | strict_node_history=False): |
|
1052 | 1052 | # Normalize path names, svn >= 1.5 only wants paths relative to |
|
1053 | 1053 | # supplied URL |
|
1054 | 1054 | relpaths = [] |
|
1055 | 1055 | for p in paths: |
|
1056 | 1056 | if not p.startswith('/'): |
|
1057 | 1057 | p = self.module + '/' + p |
|
1058 | 1058 | relpaths.append(p.strip('/')) |
|
1059 | 1059 | args = [self.baseurl, relpaths, start, end, limit, |
|
1060 | 1060 | discover_changed_paths, strict_node_history] |
|
1061 | 1061 | # developer config: convert.svn.debugsvnlog |
|
1062 | 1062 | if not self.ui.configbool('convert', 'svn.debugsvnlog', True): |
|
1063 | 1063 | return directlogstream(*args) |
|
1064 | 1064 | arg = encodeargs(args) |
|
1065 | 1065 | hgexe = util.hgexecutable() |
|
1066 | 1066 | cmd = '%s debugsvnlog' % util.shellquote(hgexe) |
|
1067 | 1067 | stdin, stdout = util.popen2(util.quotecommand(cmd)) |
|
1068 | 1068 | stdin.write(arg) |
|
1069 | 1069 | try: |
|
1070 | 1070 | stdin.close() |
|
1071 | 1071 | except IOError: |
|
1072 | 1072 | raise error.Abort(_('Mercurial failed to run itself, check' |
|
1073 | 1073 | ' hg executable is in PATH')) |
|
1074 | 1074 | return logstream(stdout) |
|
1075 | 1075 | |
|
1076 | 1076 | pre_revprop_change = '''#!/bin/sh |
|
1077 | 1077 | |
|
1078 | 1078 | REPOS="$1" |
|
1079 | 1079 | REV="$2" |
|
1080 | 1080 | USER="$3" |
|
1081 | 1081 | PROPNAME="$4" |
|
1082 | 1082 | ACTION="$5" |
|
1083 | 1083 | |
|
1084 | 1084 | if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi |
|
1085 | 1085 | if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-branch" ]; then exit 0; fi |
|
1086 | 1086 | if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-rev" ]; then exit 0; fi |
|
1087 | 1087 | |
|
1088 | 1088 | echo "Changing prohibited revision property" >&2 |
|
1089 | 1089 | exit 1 |
|
1090 | 1090 | ''' |
|
1091 | 1091 | |
|
1092 | 1092 | class svn_sink(converter_sink, commandline): |
|
1093 | 1093 | commit_re = re.compile(r'Committed revision (\d+).', re.M) |
|
1094 | 1094 | uuid_re = re.compile(r'Repository UUID:\s*(\S+)', re.M) |
|
1095 | 1095 | |
|
1096 | 1096 | def prerun(self): |
|
1097 | 1097 | if self.wc: |
|
1098 | 1098 | os.chdir(self.wc) |
|
1099 | 1099 | |
|
1100 | 1100 | def postrun(self): |
|
1101 | 1101 | if self.wc: |
|
1102 | 1102 | os.chdir(self.cwd) |
|
1103 | 1103 | |
|
1104 | 1104 | def join(self, name): |
|
1105 | 1105 | return os.path.join(self.wc, '.svn', name) |
|
1106 | 1106 | |
|
1107 | 1107 | def revmapfile(self): |
|
1108 | 1108 | return self.join('hg-shamap') |
|
1109 | 1109 | |
|
1110 | 1110 | def authorfile(self): |
|
1111 | 1111 | return self.join('hg-authormap') |
|
1112 | 1112 | |
|
1113 | 1113 | def __init__(self, ui, path): |
|
1114 | 1114 | |
|
1115 | 1115 | converter_sink.__init__(self, ui, path) |
|
1116 | 1116 | commandline.__init__(self, ui, 'svn') |
|
1117 | 1117 | self.delete = [] |
|
1118 | 1118 | self.setexec = [] |
|
1119 | 1119 | self.delexec = [] |
|
1120 | 1120 | self.copies = [] |
|
1121 | 1121 | self.wc = None |
|
1122 | 1122 | self.cwd = pycompat.getcwd() |
|
1123 | 1123 | |
|
1124 | 1124 | created = False |
|
1125 | 1125 | if os.path.isfile(os.path.join(path, '.svn', 'entries')): |
|
1126 | 1126 | self.wc = os.path.realpath(path) |
|
1127 | 1127 | self.run0('update') |
|
1128 | 1128 | else: |
|
1129 | 1129 | if not re.search(r'^(file|http|https|svn|svn\+ssh)\://', path): |
|
1130 | 1130 | path = os.path.realpath(path) |
|
1131 | 1131 | if os.path.isdir(os.path.dirname(path)): |
|
1132 | 1132 | if not os.path.exists(os.path.join(path, 'db', 'fs-type')): |
|
1133 | 1133 | ui.status(_('initializing svn repository %r\n') % |
|
1134 | 1134 | os.path.basename(path)) |
|
1135 | 1135 | commandline(ui, 'svnadmin').run0('create', path) |
|
1136 | 1136 | created = path |
|
1137 | 1137 | path = util.normpath(path) |
|
1138 | 1138 | if not path.startswith('/'): |
|
1139 | 1139 | path = '/' + path |
|
1140 | 1140 | path = 'file://' + path |
|
1141 | 1141 | |
|
1142 | 1142 | wcpath = os.path.join(pycompat.getcwd(), os.path.basename(path) + |
|
1143 | 1143 | '-wc') |
|
1144 | 1144 | ui.status(_('initializing svn working copy %r\n') |
|
1145 | 1145 | % os.path.basename(wcpath)) |
|
1146 | 1146 | self.run0('checkout', path, wcpath) |
|
1147 | 1147 | |
|
1148 | 1148 | self.wc = wcpath |
|
1149 | 1149 | self.opener = scmutil.opener(self.wc) |
|
1150 | 1150 | self.wopener = scmutil.opener(self.wc) |
|
1151 | 1151 | self.childmap = mapfile(ui, self.join('hg-childmap')) |
|
1152 | 1152 | if util.checkexec(self.wc): |
|
1153 | 1153 | self.is_exec = util.isexec |
|
1154 | 1154 | else: |
|
1155 | 1155 | self.is_exec = None |
|
1156 | 1156 | |
|
1157 | 1157 | if created: |
|
1158 | 1158 | hook = os.path.join(created, 'hooks', 'pre-revprop-change') |
|
1159 | 1159 | fp = open(hook, 'w') |
|
1160 | 1160 | fp.write(pre_revprop_change) |
|
1161 | 1161 | fp.close() |
|
1162 | 1162 | util.setflags(hook, False, True) |
|
1163 | 1163 | |
|
1164 | 1164 | output = self.run0('info') |
|
1165 | 1165 | self.uuid = self.uuid_re.search(output).group(1).strip() |
|
1166 | 1166 | |
|
1167 | 1167 | def wjoin(self, *names): |
|
1168 | 1168 | return os.path.join(self.wc, *names) |
|
1169 | 1169 | |
|
1170 | 1170 | @propertycache |
|
1171 | 1171 | def manifest(self): |
|
1172 | 1172 | # As of svn 1.7, the "add" command fails when receiving |
|
1173 | 1173 | # already tracked entries, so we have to track and filter them |
|
1174 | 1174 | # ourselves. |
|
1175 | 1175 | m = set() |
|
1176 | 1176 | output = self.run0('ls', recursive=True, xml=True) |
|
1177 | 1177 | doc = xml.dom.minidom.parseString(output) |
|
1178 | 1178 | for e in doc.getElementsByTagName('entry'): |
|
1179 | 1179 | for n in e.childNodes: |
|
1180 | 1180 | if n.nodeType != n.ELEMENT_NODE or n.tagName != 'name': |
|
1181 | 1181 | continue |
|
1182 | 1182 | name = ''.join(c.data for c in n.childNodes |
|
1183 | 1183 | if c.nodeType == c.TEXT_NODE) |
|
1184 | 1184 | # Entries are compared with names coming from |
|
1185 | 1185 | # mercurial, so bytes with undefined encoding. Our |
|
1186 | 1186 | # best bet is to assume they are in local |
|
1187 | 1187 | # encoding. They will be passed to command line calls |
|
1188 | 1188 | # later anyway, so they better be. |
|
1189 | 1189 | m.add(encoding.tolocal(name.encode('utf-8'))) |
|
1190 | 1190 | break |
|
1191 | 1191 | return m |
|
1192 | 1192 | |
|
1193 | 1193 | def putfile(self, filename, flags, data): |
|
1194 | 1194 | if 'l' in flags: |
|
1195 | 1195 | self.wopener.symlink(data, filename) |
|
1196 | 1196 | else: |
|
1197 | 1197 | try: |
|
1198 | 1198 | if os.path.islink(self.wjoin(filename)): |
|
1199 | 1199 | os.unlink(filename) |
|
1200 | 1200 | except OSError: |
|
1201 | 1201 | pass |
|
1202 | 1202 | self.wopener.write(filename, data) |
|
1203 | 1203 | |
|
1204 | 1204 | if self.is_exec: |
|
1205 | 1205 | if self.is_exec(self.wjoin(filename)): |
|
1206 | 1206 | if 'x' not in flags: |
|
1207 | 1207 | self.delexec.append(filename) |
|
1208 | 1208 | else: |
|
1209 | 1209 | if 'x' in flags: |
|
1210 | 1210 | self.setexec.append(filename) |
|
1211 | 1211 | util.setflags(self.wjoin(filename), False, 'x' in flags) |
|
1212 | 1212 | |
|
1213 | 1213 | def _copyfile(self, source, dest): |
|
1214 | 1214 | # SVN's copy command pukes if the destination file exists, but |
|
1215 | 1215 | # our copyfile method expects to record a copy that has |
|
1216 | 1216 | # already occurred. Cross the semantic gap. |
|
1217 | 1217 | wdest = self.wjoin(dest) |
|
1218 | 1218 | exists = os.path.lexists(wdest) |
|
1219 | 1219 | if exists: |
|
1220 | 1220 | fd, tempname = tempfile.mkstemp( |
|
1221 | 1221 | prefix='hg-copy-', dir=os.path.dirname(wdest)) |
|
1222 | 1222 | os.close(fd) |
|
1223 | 1223 | os.unlink(tempname) |
|
1224 | 1224 | os.rename(wdest, tempname) |
|
1225 | 1225 | try: |
|
1226 | 1226 | self.run0('copy', source, dest) |
|
1227 | 1227 | finally: |
|
1228 | 1228 | self.manifest.add(dest) |
|
1229 | 1229 | if exists: |
|
1230 | 1230 | try: |
|
1231 | 1231 | os.unlink(wdest) |
|
1232 | 1232 | except OSError: |
|
1233 | 1233 | pass |
|
1234 | 1234 | os.rename(tempname, wdest) |
|
1235 | 1235 | |
|
1236 | 1236 | def dirs_of(self, files): |
|
1237 | 1237 | dirs = set() |
|
1238 | 1238 | for f in files: |
|
1239 | 1239 | if os.path.isdir(self.wjoin(f)): |
|
1240 | 1240 | dirs.add(f) |
|
1241 | 1241 | i = len(f) |
|
1242 | 1242 | for i in iter(lambda: f.rfind('/', 0, i), -1): |
|
1243 | 1243 | dirs.add(f[:i]) |
|
1244 | 1244 | return dirs |
|
1245 | 1245 | |
|
1246 | 1246 | def add_dirs(self, files): |
|
1247 | 1247 | add_dirs = [d for d in sorted(self.dirs_of(files)) |
|
1248 | 1248 | if d not in self.manifest] |
|
1249 | 1249 | if add_dirs: |
|
1250 | 1250 | self.manifest.update(add_dirs) |
|
1251 | 1251 | self.xargs(add_dirs, 'add', non_recursive=True, quiet=True) |
|
1252 | 1252 | return add_dirs |
|
1253 | 1253 | |
|
1254 | 1254 | def add_files(self, files): |
|
1255 | 1255 | files = [f for f in files if f not in self.manifest] |
|
1256 | 1256 | if files: |
|
1257 | 1257 | self.manifest.update(files) |
|
1258 | 1258 | self.xargs(files, 'add', quiet=True) |
|
1259 | 1259 | return files |
|
1260 | 1260 | |
|
1261 | 1261 | def addchild(self, parent, child): |
|
1262 | 1262 | self.childmap[parent] = child |
|
1263 | 1263 | |
|
1264 | 1264 | def revid(self, rev): |
|
1265 | 1265 | return u"svn:%s@%s" % (self.uuid, rev) |
|
1266 | 1266 | |
|
1267 | 1267 | def putcommit(self, files, copies, parents, commit, source, revmap, full, |
|
1268 | 1268 | cleanp2): |
|
1269 | 1269 | for parent in parents: |
|
1270 | 1270 | try: |
|
1271 | 1271 | return self.revid(self.childmap[parent]) |
|
1272 | 1272 | except KeyError: |
|
1273 | 1273 | pass |
|
1274 | 1274 | |
|
1275 | 1275 | # Apply changes to working copy |
|
1276 | 1276 | for f, v in files: |
|
1277 | 1277 | data, mode = source.getfile(f, v) |
|
1278 | 1278 | if data is None: |
|
1279 | 1279 | self.delete.append(f) |
|
1280 | 1280 | else: |
|
1281 | 1281 | self.putfile(f, mode, data) |
|
1282 | 1282 | if f in copies: |
|
1283 | 1283 | self.copies.append([copies[f], f]) |
|
1284 | 1284 | if full: |
|
1285 | 1285 | self.delete.extend(sorted(self.manifest.difference(files))) |
|
1286 | 1286 | files = [f[0] for f in files] |
|
1287 | 1287 | |
|
1288 | 1288 | entries = set(self.delete) |
|
1289 | 1289 | files = frozenset(files) |
|
1290 | 1290 | entries.update(self.add_dirs(files.difference(entries))) |
|
1291 | 1291 | if self.copies: |
|
1292 | 1292 | for s, d in self.copies: |
|
1293 | 1293 | self._copyfile(s, d) |
|
1294 | 1294 | self.copies = [] |
|
1295 | 1295 | if self.delete: |
|
1296 | 1296 | self.xargs(self.delete, 'delete') |
|
1297 | 1297 | for f in self.delete: |
|
1298 | 1298 | self.manifest.remove(f) |
|
1299 | 1299 | self.delete = [] |
|
1300 | 1300 | entries.update(self.add_files(files.difference(entries))) |
|
1301 | 1301 | if self.delexec: |
|
1302 | 1302 | self.xargs(self.delexec, 'propdel', 'svn:executable') |
|
1303 | 1303 | self.delexec = [] |
|
1304 | 1304 | if self.setexec: |
|
1305 | 1305 | self.xargs(self.setexec, 'propset', 'svn:executable', '*') |
|
1306 | 1306 | self.setexec = [] |
|
1307 | 1307 | |
|
1308 | 1308 | fd, messagefile = tempfile.mkstemp(prefix='hg-convert-') |
|
1309 | fp = os.fdopen(fd, 'w') | |
|
1309 | fp = os.fdopen(fd, pycompat.sysstr('w')) | |
|
1310 | 1310 | fp.write(commit.desc) |
|
1311 | 1311 | fp.close() |
|
1312 | 1312 | try: |
|
1313 | 1313 | output = self.run0('commit', |
|
1314 | 1314 | username=util.shortuser(commit.author), |
|
1315 | 1315 | file=messagefile, |
|
1316 | 1316 | encoding='utf-8') |
|
1317 | 1317 | try: |
|
1318 | 1318 | rev = self.commit_re.search(output).group(1) |
|
1319 | 1319 | except AttributeError: |
|
1320 | 1320 | if parents and not files: |
|
1321 | 1321 | return parents[0] |
|
1322 | 1322 | self.ui.warn(_('unexpected svn output:\n')) |
|
1323 | 1323 | self.ui.warn(output) |
|
1324 | 1324 | raise error.Abort(_('unable to cope with svn output')) |
|
1325 | 1325 | if commit.rev: |
|
1326 | 1326 | self.run('propset', 'hg:convert-rev', commit.rev, |
|
1327 | 1327 | revprop=True, revision=rev) |
|
1328 | 1328 | if commit.branch and commit.branch != 'default': |
|
1329 | 1329 | self.run('propset', 'hg:convert-branch', commit.branch, |
|
1330 | 1330 | revprop=True, revision=rev) |
|
1331 | 1331 | for parent in parents: |
|
1332 | 1332 | self.addchild(parent, rev) |
|
1333 | 1333 | return self.revid(rev) |
|
1334 | 1334 | finally: |
|
1335 | 1335 | os.unlink(messagefile) |
|
1336 | 1336 | |
|
1337 | 1337 | def puttags(self, tags): |
|
1338 | 1338 | self.ui.warn(_('writing Subversion tags is not yet implemented\n')) |
|
1339 | 1339 | return None, None |
|
1340 | 1340 | |
|
1341 | 1341 | def hascommitfrommap(self, rev): |
|
1342 | 1342 | # We trust that revisions referenced in a map still is present |
|
1343 | 1343 | # TODO: implement something better if necessary and feasible |
|
1344 | 1344 | return True |
|
1345 | 1345 | |
|
1346 | 1346 | def hascommitforsplicemap(self, rev): |
|
1347 | 1347 | # This is not correct as one can convert to an existing subversion |
|
1348 | 1348 | # repository and childmap would not list all revisions. Too bad. |
|
1349 | 1349 | if rev in self.childmap: |
|
1350 | 1350 | return True |
|
1351 | 1351 | raise error.Abort(_('splice map revision %s not found in subversion ' |
|
1352 | 1352 | 'child map (revision lookups are not implemented)') |
|
1353 | 1353 | % rev) |
@@ -1,318 +1,319 b'' | |||
|
1 | 1 | # Copyright 2005, 2006 Benoit Boissinot <benoit.boissinot@ens-lyon.org> |
|
2 | 2 | # |
|
3 | 3 | # This software may be used and distributed according to the terms of the |
|
4 | 4 | # GNU General Public License version 2 or any later version. |
|
5 | 5 | |
|
6 | 6 | '''commands to sign and verify changesets''' |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import binascii |
|
11 | 11 | import os |
|
12 | 12 | import tempfile |
|
13 | 13 | |
|
14 | 14 | from mercurial.i18n import _ |
|
15 | 15 | from mercurial import ( |
|
16 | 16 | cmdutil, |
|
17 | 17 | commands, |
|
18 | 18 | error, |
|
19 | 19 | match, |
|
20 | 20 | node as hgnode, |
|
21 | pycompat, | |
|
21 | 22 | util, |
|
22 | 23 | ) |
|
23 | 24 | |
|
24 | 25 | cmdtable = {} |
|
25 | 26 | command = cmdutil.command(cmdtable) |
|
26 | 27 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
27 | 28 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
28 | 29 | # be specifying the version(s) of Mercurial they are tested with, or |
|
29 | 30 | # leave the attribute unspecified. |
|
30 | 31 | testedwith = 'ships-with-hg-core' |
|
31 | 32 | |
|
32 | 33 | class gpg(object): |
|
33 | 34 | def __init__(self, path, key=None): |
|
34 | 35 | self.path = path |
|
35 | 36 | self.key = (key and " --local-user \"%s\"" % key) or "" |
|
36 | 37 | |
|
37 | 38 | def sign(self, data): |
|
38 | 39 | gpgcmd = "%s --sign --detach-sign%s" % (self.path, self.key) |
|
39 | 40 | return util.filter(data, gpgcmd) |
|
40 | 41 | |
|
41 | 42 | def verify(self, data, sig): |
|
42 | 43 | """ returns of the good and bad signatures""" |
|
43 | 44 | sigfile = datafile = None |
|
44 | 45 | try: |
|
45 | 46 | # create temporary files |
|
46 | 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 | 49 | fp.write(sig) |
|
49 | 50 | fp.close() |
|
50 | 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 | 53 | fp.write(data) |
|
53 | 54 | fp.close() |
|
54 | 55 | gpgcmd = ("%s --logger-fd 1 --status-fd 1 --verify " |
|
55 | 56 | "\"%s\" \"%s\"" % (self.path, sigfile, datafile)) |
|
56 | 57 | ret = util.filter("", gpgcmd) |
|
57 | 58 | finally: |
|
58 | 59 | for f in (sigfile, datafile): |
|
59 | 60 | try: |
|
60 | 61 | if f: |
|
61 | 62 | os.unlink(f) |
|
62 | 63 | except OSError: |
|
63 | 64 | pass |
|
64 | 65 | keys = [] |
|
65 | 66 | key, fingerprint = None, None |
|
66 | 67 | for l in ret.splitlines(): |
|
67 | 68 | # see DETAILS in the gnupg documentation |
|
68 | 69 | # filter the logger output |
|
69 | 70 | if not l.startswith("[GNUPG:]"): |
|
70 | 71 | continue |
|
71 | 72 | l = l[9:] |
|
72 | 73 | if l.startswith("VALIDSIG"): |
|
73 | 74 | # fingerprint of the primary key |
|
74 | 75 | fingerprint = l.split()[10] |
|
75 | 76 | elif l.startswith("ERRSIG"): |
|
76 | 77 | key = l.split(" ", 3)[:2] |
|
77 | 78 | key.append("") |
|
78 | 79 | fingerprint = None |
|
79 | 80 | elif (l.startswith("GOODSIG") or |
|
80 | 81 | l.startswith("EXPSIG") or |
|
81 | 82 | l.startswith("EXPKEYSIG") or |
|
82 | 83 | l.startswith("BADSIG")): |
|
83 | 84 | if key is not None: |
|
84 | 85 | keys.append(key + [fingerprint]) |
|
85 | 86 | key = l.split(" ", 2) |
|
86 | 87 | fingerprint = None |
|
87 | 88 | if key is not None: |
|
88 | 89 | keys.append(key + [fingerprint]) |
|
89 | 90 | return keys |
|
90 | 91 | |
|
91 | 92 | def newgpg(ui, **opts): |
|
92 | 93 | """create a new gpg instance""" |
|
93 | 94 | gpgpath = ui.config("gpg", "cmd", "gpg") |
|
94 | 95 | gpgkey = opts.get('key') |
|
95 | 96 | if not gpgkey: |
|
96 | 97 | gpgkey = ui.config("gpg", "key", None) |
|
97 | 98 | return gpg(gpgpath, gpgkey) |
|
98 | 99 | |
|
99 | 100 | def sigwalk(repo): |
|
100 | 101 | """ |
|
101 | 102 | walk over every sigs, yields a couple |
|
102 | 103 | ((node, version, sig), (filename, linenumber)) |
|
103 | 104 | """ |
|
104 | 105 | def parsefile(fileiter, context): |
|
105 | 106 | ln = 1 |
|
106 | 107 | for l in fileiter: |
|
107 | 108 | if not l: |
|
108 | 109 | continue |
|
109 | 110 | yield (l.split(" ", 2), (context, ln)) |
|
110 | 111 | ln += 1 |
|
111 | 112 | |
|
112 | 113 | # read the heads |
|
113 | 114 | fl = repo.file(".hgsigs") |
|
114 | 115 | for r in reversed(fl.heads()): |
|
115 | 116 | fn = ".hgsigs|%s" % hgnode.short(r) |
|
116 | 117 | for item in parsefile(fl.read(r).splitlines(), fn): |
|
117 | 118 | yield item |
|
118 | 119 | try: |
|
119 | 120 | # read local signatures |
|
120 | 121 | fn = "localsigs" |
|
121 | 122 | for item in parsefile(repo.vfs(fn), fn): |
|
122 | 123 | yield item |
|
123 | 124 | except IOError: |
|
124 | 125 | pass |
|
125 | 126 | |
|
126 | 127 | def getkeys(ui, repo, mygpg, sigdata, context): |
|
127 | 128 | """get the keys who signed a data""" |
|
128 | 129 | fn, ln = context |
|
129 | 130 | node, version, sig = sigdata |
|
130 | 131 | prefix = "%s:%d" % (fn, ln) |
|
131 | 132 | node = hgnode.bin(node) |
|
132 | 133 | |
|
133 | 134 | data = node2txt(repo, node, version) |
|
134 | 135 | sig = binascii.a2b_base64(sig) |
|
135 | 136 | keys = mygpg.verify(data, sig) |
|
136 | 137 | |
|
137 | 138 | validkeys = [] |
|
138 | 139 | # warn for expired key and/or sigs |
|
139 | 140 | for key in keys: |
|
140 | 141 | if key[0] == "ERRSIG": |
|
141 | 142 | ui.write(_("%s Unknown key ID \"%s\"\n") |
|
142 | 143 | % (prefix, shortkey(ui, key[1][:15]))) |
|
143 | 144 | continue |
|
144 | 145 | if key[0] == "BADSIG": |
|
145 | 146 | ui.write(_("%s Bad signature from \"%s\"\n") % (prefix, key[2])) |
|
146 | 147 | continue |
|
147 | 148 | if key[0] == "EXPSIG": |
|
148 | 149 | ui.write(_("%s Note: Signature has expired" |
|
149 | 150 | " (signed by: \"%s\")\n") % (prefix, key[2])) |
|
150 | 151 | elif key[0] == "EXPKEYSIG": |
|
151 | 152 | ui.write(_("%s Note: This key has expired" |
|
152 | 153 | " (signed by: \"%s\")\n") % (prefix, key[2])) |
|
153 | 154 | validkeys.append((key[1], key[2], key[3])) |
|
154 | 155 | return validkeys |
|
155 | 156 | |
|
156 | 157 | @command("sigs", [], _('hg sigs')) |
|
157 | 158 | def sigs(ui, repo): |
|
158 | 159 | """list signed changesets""" |
|
159 | 160 | mygpg = newgpg(ui) |
|
160 | 161 | revs = {} |
|
161 | 162 | |
|
162 | 163 | for data, context in sigwalk(repo): |
|
163 | 164 | node, version, sig = data |
|
164 | 165 | fn, ln = context |
|
165 | 166 | try: |
|
166 | 167 | n = repo.lookup(node) |
|
167 | 168 | except KeyError: |
|
168 | 169 | ui.warn(_("%s:%d node does not exist\n") % (fn, ln)) |
|
169 | 170 | continue |
|
170 | 171 | r = repo.changelog.rev(n) |
|
171 | 172 | keys = getkeys(ui, repo, mygpg, data, context) |
|
172 | 173 | if not keys: |
|
173 | 174 | continue |
|
174 | 175 | revs.setdefault(r, []) |
|
175 | 176 | revs[r].extend(keys) |
|
176 | 177 | for rev in sorted(revs, reverse=True): |
|
177 | 178 | for k in revs[rev]: |
|
178 | 179 | r = "%5d:%s" % (rev, hgnode.hex(repo.changelog.node(rev))) |
|
179 | 180 | ui.write("%-30s %s\n" % (keystr(ui, k), r)) |
|
180 | 181 | |
|
181 | 182 | @command("sigcheck", [], _('hg sigcheck REV')) |
|
182 | 183 | def sigcheck(ui, repo, rev): |
|
183 | 184 | """verify all the signatures there may be for a particular revision""" |
|
184 | 185 | mygpg = newgpg(ui) |
|
185 | 186 | rev = repo.lookup(rev) |
|
186 | 187 | hexrev = hgnode.hex(rev) |
|
187 | 188 | keys = [] |
|
188 | 189 | |
|
189 | 190 | for data, context in sigwalk(repo): |
|
190 | 191 | node, version, sig = data |
|
191 | 192 | if node == hexrev: |
|
192 | 193 | k = getkeys(ui, repo, mygpg, data, context) |
|
193 | 194 | if k: |
|
194 | 195 | keys.extend(k) |
|
195 | 196 | |
|
196 | 197 | if not keys: |
|
197 | 198 | ui.write(_("no valid signature for %s\n") % hgnode.short(rev)) |
|
198 | 199 | return |
|
199 | 200 | |
|
200 | 201 | # print summary |
|
201 | 202 | ui.write(_("%s is signed by:\n") % hgnode.short(rev)) |
|
202 | 203 | for key in keys: |
|
203 | 204 | ui.write(" %s\n" % keystr(ui, key)) |
|
204 | 205 | |
|
205 | 206 | def keystr(ui, key): |
|
206 | 207 | """associate a string to a key (username, comment)""" |
|
207 | 208 | keyid, user, fingerprint = key |
|
208 | 209 | comment = ui.config("gpg", fingerprint, None) |
|
209 | 210 | if comment: |
|
210 | 211 | return "%s (%s)" % (user, comment) |
|
211 | 212 | else: |
|
212 | 213 | return user |
|
213 | 214 | |
|
214 | 215 | @command("sign", |
|
215 | 216 | [('l', 'local', None, _('make the signature local')), |
|
216 | 217 | ('f', 'force', None, _('sign even if the sigfile is modified')), |
|
217 | 218 | ('', 'no-commit', None, _('do not commit the sigfile after signing')), |
|
218 | 219 | ('k', 'key', '', |
|
219 | 220 | _('the key id to sign with'), _('ID')), |
|
220 | 221 | ('m', 'message', '', |
|
221 | 222 | _('use text as commit message'), _('TEXT')), |
|
222 | 223 | ('e', 'edit', False, _('invoke editor on commit messages')), |
|
223 | 224 | ] + commands.commitopts2, |
|
224 | 225 | _('hg sign [OPTION]... [REV]...')) |
|
225 | 226 | def sign(ui, repo, *revs, **opts): |
|
226 | 227 | """add a signature for the current or given revision |
|
227 | 228 | |
|
228 | 229 | If no revision is given, the parent of the working directory is used, |
|
229 | 230 | or tip if no revision is checked out. |
|
230 | 231 | |
|
231 | 232 | The ``gpg.cmd`` config setting can be used to specify the command |
|
232 | 233 | to run. A default key can be specified with ``gpg.key``. |
|
233 | 234 | |
|
234 | 235 | See :hg:`help dates` for a list of formats valid for -d/--date. |
|
235 | 236 | """ |
|
236 | 237 | with repo.wlock(): |
|
237 | 238 | return _dosign(ui, repo, *revs, **opts) |
|
238 | 239 | |
|
239 | 240 | def _dosign(ui, repo, *revs, **opts): |
|
240 | 241 | mygpg = newgpg(ui, **opts) |
|
241 | 242 | sigver = "0" |
|
242 | 243 | sigmessage = "" |
|
243 | 244 | |
|
244 | 245 | date = opts.get('date') |
|
245 | 246 | if date: |
|
246 | 247 | opts['date'] = util.parsedate(date) |
|
247 | 248 | |
|
248 | 249 | if revs: |
|
249 | 250 | nodes = [repo.lookup(n) for n in revs] |
|
250 | 251 | else: |
|
251 | 252 | nodes = [node for node in repo.dirstate.parents() |
|
252 | 253 | if node != hgnode.nullid] |
|
253 | 254 | if len(nodes) > 1: |
|
254 | 255 | raise error.Abort(_('uncommitted merge - please provide a ' |
|
255 | 256 | 'specific revision')) |
|
256 | 257 | if not nodes: |
|
257 | 258 | nodes = [repo.changelog.tip()] |
|
258 | 259 | |
|
259 | 260 | for n in nodes: |
|
260 | 261 | hexnode = hgnode.hex(n) |
|
261 | 262 | ui.write(_("signing %d:%s\n") % (repo.changelog.rev(n), |
|
262 | 263 | hgnode.short(n))) |
|
263 | 264 | # build data |
|
264 | 265 | data = node2txt(repo, n, sigver) |
|
265 | 266 | sig = mygpg.sign(data) |
|
266 | 267 | if not sig: |
|
267 | 268 | raise error.Abort(_("error while signing")) |
|
268 | 269 | sig = binascii.b2a_base64(sig) |
|
269 | 270 | sig = sig.replace("\n", "") |
|
270 | 271 | sigmessage += "%s %s %s\n" % (hexnode, sigver, sig) |
|
271 | 272 | |
|
272 | 273 | # write it |
|
273 | 274 | if opts['local']: |
|
274 | 275 | repo.vfs.append("localsigs", sigmessage) |
|
275 | 276 | return |
|
276 | 277 | |
|
277 | 278 | if not opts["force"]: |
|
278 | 279 | msigs = match.exact(repo.root, '', ['.hgsigs']) |
|
279 | 280 | if any(repo.status(match=msigs, unknown=True, ignored=True)): |
|
280 | 281 | raise error.Abort(_("working copy of .hgsigs is changed "), |
|
281 | 282 | hint=_("please commit .hgsigs manually")) |
|
282 | 283 | |
|
283 | 284 | sigsfile = repo.wfile(".hgsigs", "ab") |
|
284 | 285 | sigsfile.write(sigmessage) |
|
285 | 286 | sigsfile.close() |
|
286 | 287 | |
|
287 | 288 | if '.hgsigs' not in repo.dirstate: |
|
288 | 289 | repo[None].add([".hgsigs"]) |
|
289 | 290 | |
|
290 | 291 | if opts["no_commit"]: |
|
291 | 292 | return |
|
292 | 293 | |
|
293 | 294 | message = opts['message'] |
|
294 | 295 | if not message: |
|
295 | 296 | # we don't translate commit messages |
|
296 | 297 | message = "\n".join(["Added signature for changeset %s" |
|
297 | 298 | % hgnode.short(n) |
|
298 | 299 | for n in nodes]) |
|
299 | 300 | try: |
|
300 | 301 | editor = cmdutil.getcommiteditor(editform='gpg.sign', **opts) |
|
301 | 302 | repo.commit(message, opts['user'], opts['date'], match=msigs, |
|
302 | 303 | editor=editor) |
|
303 | 304 | except ValueError as inst: |
|
304 | 305 | raise error.Abort(str(inst)) |
|
305 | 306 | |
|
306 | 307 | def shortkey(ui, key): |
|
307 | 308 | if len(key) != 16: |
|
308 | 309 | ui.debug("key ID \"%s\" format error\n" % key) |
|
309 | 310 | return key |
|
310 | 311 | |
|
311 | 312 | return key[-8:] |
|
312 | 313 | |
|
313 | 314 | def node2txt(repo, node, ver): |
|
314 | 315 | """map a manifest into some text""" |
|
315 | 316 | if ver == "0": |
|
316 | 317 | return "%s\n" % hgnode.hex(node) |
|
317 | 318 | else: |
|
318 | 319 | raise error.Abort(_("unknown signature version")) |
@@ -1,743 +1,744 b'' | |||
|
1 | 1 | # Patch transplanting extension for Mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 2007 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | '''command to transplant changesets from another branch |
|
9 | 9 | |
|
10 | 10 | This extension allows you to transplant changes to another parent revision, |
|
11 | 11 | possibly in another repository. The transplant is done using 'diff' patches. |
|
12 | 12 | |
|
13 | 13 | Transplanted patches are recorded in .hg/transplant/transplants, as a |
|
14 | 14 | map from a changeset hash to its hash in the source repository. |
|
15 | 15 | ''' |
|
16 | 16 | from __future__ import absolute_import |
|
17 | 17 | |
|
18 | 18 | import os |
|
19 | 19 | import tempfile |
|
20 | 20 | from mercurial.i18n import _ |
|
21 | 21 | from mercurial import ( |
|
22 | 22 | bundlerepo, |
|
23 | 23 | cmdutil, |
|
24 | 24 | error, |
|
25 | 25 | exchange, |
|
26 | 26 | hg, |
|
27 | 27 | match, |
|
28 | 28 | merge, |
|
29 | 29 | node as nodemod, |
|
30 | 30 | patch, |
|
31 | pycompat, | |
|
31 | 32 | registrar, |
|
32 | 33 | revlog, |
|
33 | 34 | revset, |
|
34 | 35 | scmutil, |
|
35 | 36 | util, |
|
36 | 37 | ) |
|
37 | 38 | |
|
38 | 39 | class TransplantError(error.Abort): |
|
39 | 40 | pass |
|
40 | 41 | |
|
41 | 42 | cmdtable = {} |
|
42 | 43 | command = cmdutil.command(cmdtable) |
|
43 | 44 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
|
44 | 45 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
|
45 | 46 | # be specifying the version(s) of Mercurial they are tested with, or |
|
46 | 47 | # leave the attribute unspecified. |
|
47 | 48 | testedwith = 'ships-with-hg-core' |
|
48 | 49 | |
|
49 | 50 | class transplantentry(object): |
|
50 | 51 | def __init__(self, lnode, rnode): |
|
51 | 52 | self.lnode = lnode |
|
52 | 53 | self.rnode = rnode |
|
53 | 54 | |
|
54 | 55 | class transplants(object): |
|
55 | 56 | def __init__(self, path=None, transplantfile=None, opener=None): |
|
56 | 57 | self.path = path |
|
57 | 58 | self.transplantfile = transplantfile |
|
58 | 59 | self.opener = opener |
|
59 | 60 | |
|
60 | 61 | if not opener: |
|
61 | 62 | self.opener = scmutil.opener(self.path) |
|
62 | 63 | self.transplants = {} |
|
63 | 64 | self.dirty = False |
|
64 | 65 | self.read() |
|
65 | 66 | |
|
66 | 67 | def read(self): |
|
67 | 68 | abspath = os.path.join(self.path, self.transplantfile) |
|
68 | 69 | if self.transplantfile and os.path.exists(abspath): |
|
69 | 70 | for line in self.opener.read(self.transplantfile).splitlines(): |
|
70 | 71 | lnode, rnode = map(revlog.bin, line.split(':')) |
|
71 | 72 | list = self.transplants.setdefault(rnode, []) |
|
72 | 73 | list.append(transplantentry(lnode, rnode)) |
|
73 | 74 | |
|
74 | 75 | def write(self): |
|
75 | 76 | if self.dirty and self.transplantfile: |
|
76 | 77 | if not os.path.isdir(self.path): |
|
77 | 78 | os.mkdir(self.path) |
|
78 | 79 | fp = self.opener(self.transplantfile, 'w') |
|
79 | 80 | for list in self.transplants.itervalues(): |
|
80 | 81 | for t in list: |
|
81 | 82 | l, r = map(nodemod.hex, (t.lnode, t.rnode)) |
|
82 | 83 | fp.write(l + ':' + r + '\n') |
|
83 | 84 | fp.close() |
|
84 | 85 | self.dirty = False |
|
85 | 86 | |
|
86 | 87 | def get(self, rnode): |
|
87 | 88 | return self.transplants.get(rnode) or [] |
|
88 | 89 | |
|
89 | 90 | def set(self, lnode, rnode): |
|
90 | 91 | list = self.transplants.setdefault(rnode, []) |
|
91 | 92 | list.append(transplantentry(lnode, rnode)) |
|
92 | 93 | self.dirty = True |
|
93 | 94 | |
|
94 | 95 | def remove(self, transplant): |
|
95 | 96 | list = self.transplants.get(transplant.rnode) |
|
96 | 97 | if list: |
|
97 | 98 | del list[list.index(transplant)] |
|
98 | 99 | self.dirty = True |
|
99 | 100 | |
|
100 | 101 | class transplanter(object): |
|
101 | 102 | def __init__(self, ui, repo, opts): |
|
102 | 103 | self.ui = ui |
|
103 | 104 | self.path = repo.join('transplant') |
|
104 | 105 | self.opener = scmutil.opener(self.path) |
|
105 | 106 | self.transplants = transplants(self.path, 'transplants', |
|
106 | 107 | opener=self.opener) |
|
107 | 108 | def getcommiteditor(): |
|
108 | 109 | editform = cmdutil.mergeeditform(repo[None], 'transplant') |
|
109 | 110 | return cmdutil.getcommiteditor(editform=editform, **opts) |
|
110 | 111 | self.getcommiteditor = getcommiteditor |
|
111 | 112 | |
|
112 | 113 | def applied(self, repo, node, parent): |
|
113 | 114 | '''returns True if a node is already an ancestor of parent |
|
114 | 115 | or is parent or has already been transplanted''' |
|
115 | 116 | if hasnode(repo, parent): |
|
116 | 117 | parentrev = repo.changelog.rev(parent) |
|
117 | 118 | if hasnode(repo, node): |
|
118 | 119 | rev = repo.changelog.rev(node) |
|
119 | 120 | reachable = repo.changelog.ancestors([parentrev], rev, |
|
120 | 121 | inclusive=True) |
|
121 | 122 | if rev in reachable: |
|
122 | 123 | return True |
|
123 | 124 | for t in self.transplants.get(node): |
|
124 | 125 | # it might have been stripped |
|
125 | 126 | if not hasnode(repo, t.lnode): |
|
126 | 127 | self.transplants.remove(t) |
|
127 | 128 | return False |
|
128 | 129 | lnoderev = repo.changelog.rev(t.lnode) |
|
129 | 130 | if lnoderev in repo.changelog.ancestors([parentrev], lnoderev, |
|
130 | 131 | inclusive=True): |
|
131 | 132 | return True |
|
132 | 133 | return False |
|
133 | 134 | |
|
134 | 135 | def apply(self, repo, source, revmap, merges, opts=None): |
|
135 | 136 | '''apply the revisions in revmap one by one in revision order''' |
|
136 | 137 | if opts is None: |
|
137 | 138 | opts = {} |
|
138 | 139 | revs = sorted(revmap) |
|
139 | 140 | p1, p2 = repo.dirstate.parents() |
|
140 | 141 | pulls = [] |
|
141 | 142 | diffopts = patch.difffeatureopts(self.ui, opts) |
|
142 | 143 | diffopts.git = True |
|
143 | 144 | |
|
144 | 145 | lock = tr = None |
|
145 | 146 | try: |
|
146 | 147 | lock = repo.lock() |
|
147 | 148 | tr = repo.transaction('transplant') |
|
148 | 149 | for rev in revs: |
|
149 | 150 | node = revmap[rev] |
|
150 | 151 | revstr = '%s:%s' % (rev, nodemod.short(node)) |
|
151 | 152 | |
|
152 | 153 | if self.applied(repo, node, p1): |
|
153 | 154 | self.ui.warn(_('skipping already applied revision %s\n') % |
|
154 | 155 | revstr) |
|
155 | 156 | continue |
|
156 | 157 | |
|
157 | 158 | parents = source.changelog.parents(node) |
|
158 | 159 | if not (opts.get('filter') or opts.get('log')): |
|
159 | 160 | # If the changeset parent is the same as the |
|
160 | 161 | # wdir's parent, just pull it. |
|
161 | 162 | if parents[0] == p1: |
|
162 | 163 | pulls.append(node) |
|
163 | 164 | p1 = node |
|
164 | 165 | continue |
|
165 | 166 | if pulls: |
|
166 | 167 | if source != repo: |
|
167 | 168 | exchange.pull(repo, source.peer(), heads=pulls) |
|
168 | 169 | merge.update(repo, pulls[-1], False, False) |
|
169 | 170 | p1, p2 = repo.dirstate.parents() |
|
170 | 171 | pulls = [] |
|
171 | 172 | |
|
172 | 173 | domerge = False |
|
173 | 174 | if node in merges: |
|
174 | 175 | # pulling all the merge revs at once would mean we |
|
175 | 176 | # couldn't transplant after the latest even if |
|
176 | 177 | # transplants before them fail. |
|
177 | 178 | domerge = True |
|
178 | 179 | if not hasnode(repo, node): |
|
179 | 180 | exchange.pull(repo, source.peer(), heads=[node]) |
|
180 | 181 | |
|
181 | 182 | skipmerge = False |
|
182 | 183 | if parents[1] != revlog.nullid: |
|
183 | 184 | if not opts.get('parent'): |
|
184 | 185 | self.ui.note(_('skipping merge changeset %s:%s\n') |
|
185 | 186 | % (rev, nodemod.short(node))) |
|
186 | 187 | skipmerge = True |
|
187 | 188 | else: |
|
188 | 189 | parent = source.lookup(opts['parent']) |
|
189 | 190 | if parent not in parents: |
|
190 | 191 | raise error.Abort(_('%s is not a parent of %s') % |
|
191 | 192 | (nodemod.short(parent), |
|
192 | 193 | nodemod.short(node))) |
|
193 | 194 | else: |
|
194 | 195 | parent = parents[0] |
|
195 | 196 | |
|
196 | 197 | if skipmerge: |
|
197 | 198 | patchfile = None |
|
198 | 199 | else: |
|
199 | 200 | fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-') |
|
200 | fp = os.fdopen(fd, 'w') | |
|
201 | fp = os.fdopen(fd, pycompat.sysstr('w')) | |
|
201 | 202 | gen = patch.diff(source, parent, node, opts=diffopts) |
|
202 | 203 | for chunk in gen: |
|
203 | 204 | fp.write(chunk) |
|
204 | 205 | fp.close() |
|
205 | 206 | |
|
206 | 207 | del revmap[rev] |
|
207 | 208 | if patchfile or domerge: |
|
208 | 209 | try: |
|
209 | 210 | try: |
|
210 | 211 | n = self.applyone(repo, node, |
|
211 | 212 | source.changelog.read(node), |
|
212 | 213 | patchfile, merge=domerge, |
|
213 | 214 | log=opts.get('log'), |
|
214 | 215 | filter=opts.get('filter')) |
|
215 | 216 | except TransplantError: |
|
216 | 217 | # Do not rollback, it is up to the user to |
|
217 | 218 | # fix the merge or cancel everything |
|
218 | 219 | tr.close() |
|
219 | 220 | raise |
|
220 | 221 | if n and domerge: |
|
221 | 222 | self.ui.status(_('%s merged at %s\n') % (revstr, |
|
222 | 223 | nodemod.short(n))) |
|
223 | 224 | elif n: |
|
224 | 225 | self.ui.status(_('%s transplanted to %s\n') |
|
225 | 226 | % (nodemod.short(node), |
|
226 | 227 | nodemod.short(n))) |
|
227 | 228 | finally: |
|
228 | 229 | if patchfile: |
|
229 | 230 | os.unlink(patchfile) |
|
230 | 231 | tr.close() |
|
231 | 232 | if pulls: |
|
232 | 233 | exchange.pull(repo, source.peer(), heads=pulls) |
|
233 | 234 | merge.update(repo, pulls[-1], False, False) |
|
234 | 235 | finally: |
|
235 | 236 | self.saveseries(revmap, merges) |
|
236 | 237 | self.transplants.write() |
|
237 | 238 | if tr: |
|
238 | 239 | tr.release() |
|
239 | 240 | if lock: |
|
240 | 241 | lock.release() |
|
241 | 242 | |
|
242 | 243 | def filter(self, filter, node, changelog, patchfile): |
|
243 | 244 | '''arbitrarily rewrite changeset before applying it''' |
|
244 | 245 | |
|
245 | 246 | self.ui.status(_('filtering %s\n') % patchfile) |
|
246 | 247 | user, date, msg = (changelog[1], changelog[2], changelog[4]) |
|
247 | 248 | fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-') |
|
248 | fp = os.fdopen(fd, 'w') | |
|
249 | fp = os.fdopen(fd, pycompat.sysstr('w')) | |
|
249 | 250 | fp.write("# HG changeset patch\n") |
|
250 | 251 | fp.write("# User %s\n" % user) |
|
251 | 252 | fp.write("# Date %d %d\n" % date) |
|
252 | 253 | fp.write(msg + '\n') |
|
253 | 254 | fp.close() |
|
254 | 255 | |
|
255 | 256 | try: |
|
256 | 257 | self.ui.system('%s %s %s' % (filter, util.shellquote(headerfile), |
|
257 | 258 | util.shellquote(patchfile)), |
|
258 | 259 | environ={'HGUSER': changelog[1], |
|
259 | 260 | 'HGREVISION': nodemod.hex(node), |
|
260 | 261 | }, |
|
261 | 262 | onerr=error.Abort, errprefix=_('filter failed')) |
|
262 | 263 | user, date, msg = self.parselog(file(headerfile))[1:4] |
|
263 | 264 | finally: |
|
264 | 265 | os.unlink(headerfile) |
|
265 | 266 | |
|
266 | 267 | return (user, date, msg) |
|
267 | 268 | |
|
268 | 269 | def applyone(self, repo, node, cl, patchfile, merge=False, log=False, |
|
269 | 270 | filter=None): |
|
270 | 271 | '''apply the patch in patchfile to the repository as a transplant''' |
|
271 | 272 | (manifest, user, (time, timezone), files, message) = cl[:5] |
|
272 | 273 | date = "%d %d" % (time, timezone) |
|
273 | 274 | extra = {'transplant_source': node} |
|
274 | 275 | if filter: |
|
275 | 276 | (user, date, message) = self.filter(filter, node, cl, patchfile) |
|
276 | 277 | |
|
277 | 278 | if log: |
|
278 | 279 | # we don't translate messages inserted into commits |
|
279 | 280 | message += '\n(transplanted from %s)' % nodemod.hex(node) |
|
280 | 281 | |
|
281 | 282 | self.ui.status(_('applying %s\n') % nodemod.short(node)) |
|
282 | 283 | self.ui.note('%s %s\n%s\n' % (user, date, message)) |
|
283 | 284 | |
|
284 | 285 | if not patchfile and not merge: |
|
285 | 286 | raise error.Abort(_('can only omit patchfile if merging')) |
|
286 | 287 | if patchfile: |
|
287 | 288 | try: |
|
288 | 289 | files = set() |
|
289 | 290 | patch.patch(self.ui, repo, patchfile, files=files, eolmode=None) |
|
290 | 291 | files = list(files) |
|
291 | 292 | except Exception as inst: |
|
292 | 293 | seriespath = os.path.join(self.path, 'series') |
|
293 | 294 | if os.path.exists(seriespath): |
|
294 | 295 | os.unlink(seriespath) |
|
295 | 296 | p1 = repo.dirstate.p1() |
|
296 | 297 | p2 = node |
|
297 | 298 | self.log(user, date, message, p1, p2, merge=merge) |
|
298 | 299 | self.ui.write(str(inst) + '\n') |
|
299 | 300 | raise TransplantError(_('fix up the working directory and run ' |
|
300 | 301 | 'hg transplant --continue')) |
|
301 | 302 | else: |
|
302 | 303 | files = None |
|
303 | 304 | if merge: |
|
304 | 305 | p1, p2 = repo.dirstate.parents() |
|
305 | 306 | repo.setparents(p1, node) |
|
306 | 307 | m = match.always(repo.root, '') |
|
307 | 308 | else: |
|
308 | 309 | m = match.exact(repo.root, '', files) |
|
309 | 310 | |
|
310 | 311 | n = repo.commit(message, user, date, extra=extra, match=m, |
|
311 | 312 | editor=self.getcommiteditor()) |
|
312 | 313 | if not n: |
|
313 | 314 | self.ui.warn(_('skipping emptied changeset %s\n') % |
|
314 | 315 | nodemod.short(node)) |
|
315 | 316 | return None |
|
316 | 317 | if not merge: |
|
317 | 318 | self.transplants.set(n, node) |
|
318 | 319 | |
|
319 | 320 | return n |
|
320 | 321 | |
|
321 | 322 | def canresume(self): |
|
322 | 323 | return os.path.exists(os.path.join(self.path, 'journal')) |
|
323 | 324 | |
|
324 | 325 | def resume(self, repo, source, opts): |
|
325 | 326 | '''recover last transaction and apply remaining changesets''' |
|
326 | 327 | if os.path.exists(os.path.join(self.path, 'journal')): |
|
327 | 328 | n, node = self.recover(repo, source, opts) |
|
328 | 329 | if n: |
|
329 | 330 | self.ui.status(_('%s transplanted as %s\n') % |
|
330 | 331 | (nodemod.short(node), |
|
331 | 332 | nodemod.short(n))) |
|
332 | 333 | else: |
|
333 | 334 | self.ui.status(_('%s skipped due to empty diff\n') |
|
334 | 335 | % (nodemod.short(node),)) |
|
335 | 336 | seriespath = os.path.join(self.path, 'series') |
|
336 | 337 | if not os.path.exists(seriespath): |
|
337 | 338 | self.transplants.write() |
|
338 | 339 | return |
|
339 | 340 | nodes, merges = self.readseries() |
|
340 | 341 | revmap = {} |
|
341 | 342 | for n in nodes: |
|
342 | 343 | revmap[source.changelog.rev(n)] = n |
|
343 | 344 | os.unlink(seriespath) |
|
344 | 345 | |
|
345 | 346 | self.apply(repo, source, revmap, merges, opts) |
|
346 | 347 | |
|
347 | 348 | def recover(self, repo, source, opts): |
|
348 | 349 | '''commit working directory using journal metadata''' |
|
349 | 350 | node, user, date, message, parents = self.readlog() |
|
350 | 351 | merge = False |
|
351 | 352 | |
|
352 | 353 | if not user or not date or not message or not parents[0]: |
|
353 | 354 | raise error.Abort(_('transplant log file is corrupt')) |
|
354 | 355 | |
|
355 | 356 | parent = parents[0] |
|
356 | 357 | if len(parents) > 1: |
|
357 | 358 | if opts.get('parent'): |
|
358 | 359 | parent = source.lookup(opts['parent']) |
|
359 | 360 | if parent not in parents: |
|
360 | 361 | raise error.Abort(_('%s is not a parent of %s') % |
|
361 | 362 | (nodemod.short(parent), |
|
362 | 363 | nodemod.short(node))) |
|
363 | 364 | else: |
|
364 | 365 | merge = True |
|
365 | 366 | |
|
366 | 367 | extra = {'transplant_source': node} |
|
367 | 368 | try: |
|
368 | 369 | p1, p2 = repo.dirstate.parents() |
|
369 | 370 | if p1 != parent: |
|
370 | 371 | raise error.Abort(_('working directory not at transplant ' |
|
371 | 372 | 'parent %s') % nodemod.hex(parent)) |
|
372 | 373 | if merge: |
|
373 | 374 | repo.setparents(p1, parents[1]) |
|
374 | 375 | modified, added, removed, deleted = repo.status()[:4] |
|
375 | 376 | if merge or modified or added or removed or deleted: |
|
376 | 377 | n = repo.commit(message, user, date, extra=extra, |
|
377 | 378 | editor=self.getcommiteditor()) |
|
378 | 379 | if not n: |
|
379 | 380 | raise error.Abort(_('commit failed')) |
|
380 | 381 | if not merge: |
|
381 | 382 | self.transplants.set(n, node) |
|
382 | 383 | else: |
|
383 | 384 | n = None |
|
384 | 385 | self.unlog() |
|
385 | 386 | |
|
386 | 387 | return n, node |
|
387 | 388 | finally: |
|
388 | 389 | # TODO: get rid of this meaningless try/finally enclosing. |
|
389 | 390 | # this is kept only to reduce changes in a patch. |
|
390 | 391 | pass |
|
391 | 392 | |
|
392 | 393 | def readseries(self): |
|
393 | 394 | nodes = [] |
|
394 | 395 | merges = [] |
|
395 | 396 | cur = nodes |
|
396 | 397 | for line in self.opener.read('series').splitlines(): |
|
397 | 398 | if line.startswith('# Merges'): |
|
398 | 399 | cur = merges |
|
399 | 400 | continue |
|
400 | 401 | cur.append(revlog.bin(line)) |
|
401 | 402 | |
|
402 | 403 | return (nodes, merges) |
|
403 | 404 | |
|
404 | 405 | def saveseries(self, revmap, merges): |
|
405 | 406 | if not revmap: |
|
406 | 407 | return |
|
407 | 408 | |
|
408 | 409 | if not os.path.isdir(self.path): |
|
409 | 410 | os.mkdir(self.path) |
|
410 | 411 | series = self.opener('series', 'w') |
|
411 | 412 | for rev in sorted(revmap): |
|
412 | 413 | series.write(nodemod.hex(revmap[rev]) + '\n') |
|
413 | 414 | if merges: |
|
414 | 415 | series.write('# Merges\n') |
|
415 | 416 | for m in merges: |
|
416 | 417 | series.write(nodemod.hex(m) + '\n') |
|
417 | 418 | series.close() |
|
418 | 419 | |
|
419 | 420 | def parselog(self, fp): |
|
420 | 421 | parents = [] |
|
421 | 422 | message = [] |
|
422 | 423 | node = revlog.nullid |
|
423 | 424 | inmsg = False |
|
424 | 425 | user = None |
|
425 | 426 | date = None |
|
426 | 427 | for line in fp.read().splitlines(): |
|
427 | 428 | if inmsg: |
|
428 | 429 | message.append(line) |
|
429 | 430 | elif line.startswith('# User '): |
|
430 | 431 | user = line[7:] |
|
431 | 432 | elif line.startswith('# Date '): |
|
432 | 433 | date = line[7:] |
|
433 | 434 | elif line.startswith('# Node ID '): |
|
434 | 435 | node = revlog.bin(line[10:]) |
|
435 | 436 | elif line.startswith('# Parent '): |
|
436 | 437 | parents.append(revlog.bin(line[9:])) |
|
437 | 438 | elif not line.startswith('# '): |
|
438 | 439 | inmsg = True |
|
439 | 440 | message.append(line) |
|
440 | 441 | if None in (user, date): |
|
441 | 442 | raise error.Abort(_("filter corrupted changeset (no user or date)")) |
|
442 | 443 | return (node, user, date, '\n'.join(message), parents) |
|
443 | 444 | |
|
444 | 445 | def log(self, user, date, message, p1, p2, merge=False): |
|
445 | 446 | '''journal changelog metadata for later recover''' |
|
446 | 447 | |
|
447 | 448 | if not os.path.isdir(self.path): |
|
448 | 449 | os.mkdir(self.path) |
|
449 | 450 | fp = self.opener('journal', 'w') |
|
450 | 451 | fp.write('# User %s\n' % user) |
|
451 | 452 | fp.write('# Date %s\n' % date) |
|
452 | 453 | fp.write('# Node ID %s\n' % nodemod.hex(p2)) |
|
453 | 454 | fp.write('# Parent ' + nodemod.hex(p1) + '\n') |
|
454 | 455 | if merge: |
|
455 | 456 | fp.write('# Parent ' + nodemod.hex(p2) + '\n') |
|
456 | 457 | fp.write(message.rstrip() + '\n') |
|
457 | 458 | fp.close() |
|
458 | 459 | |
|
459 | 460 | def readlog(self): |
|
460 | 461 | return self.parselog(self.opener('journal')) |
|
461 | 462 | |
|
462 | 463 | def unlog(self): |
|
463 | 464 | '''remove changelog journal''' |
|
464 | 465 | absdst = os.path.join(self.path, 'journal') |
|
465 | 466 | if os.path.exists(absdst): |
|
466 | 467 | os.unlink(absdst) |
|
467 | 468 | |
|
468 | 469 | def transplantfilter(self, repo, source, root): |
|
469 | 470 | def matchfn(node): |
|
470 | 471 | if self.applied(repo, node, root): |
|
471 | 472 | return False |
|
472 | 473 | if source.changelog.parents(node)[1] != revlog.nullid: |
|
473 | 474 | return False |
|
474 | 475 | extra = source.changelog.read(node)[5] |
|
475 | 476 | cnode = extra.get('transplant_source') |
|
476 | 477 | if cnode and self.applied(repo, cnode, root): |
|
477 | 478 | return False |
|
478 | 479 | return True |
|
479 | 480 | |
|
480 | 481 | return matchfn |
|
481 | 482 | |
|
482 | 483 | def hasnode(repo, node): |
|
483 | 484 | try: |
|
484 | 485 | return repo.changelog.rev(node) is not None |
|
485 | 486 | except error.RevlogError: |
|
486 | 487 | return False |
|
487 | 488 | |
|
488 | 489 | def browserevs(ui, repo, nodes, opts): |
|
489 | 490 | '''interactively transplant changesets''' |
|
490 | 491 | displayer = cmdutil.show_changeset(ui, repo, opts) |
|
491 | 492 | transplants = [] |
|
492 | 493 | merges = [] |
|
493 | 494 | prompt = _('apply changeset? [ynmpcq?]:' |
|
494 | 495 | '$$ &yes, transplant this changeset' |
|
495 | 496 | '$$ &no, skip this changeset' |
|
496 | 497 | '$$ &merge at this changeset' |
|
497 | 498 | '$$ show &patch' |
|
498 | 499 | '$$ &commit selected changesets' |
|
499 | 500 | '$$ &quit and cancel transplant' |
|
500 | 501 | '$$ &? (show this help)') |
|
501 | 502 | for node in nodes: |
|
502 | 503 | displayer.show(repo[node]) |
|
503 | 504 | action = None |
|
504 | 505 | while not action: |
|
505 | 506 | action = 'ynmpcq?'[ui.promptchoice(prompt)] |
|
506 | 507 | if action == '?': |
|
507 | 508 | for c, t in ui.extractchoices(prompt)[1]: |
|
508 | 509 | ui.write('%s: %s\n' % (c, t)) |
|
509 | 510 | action = None |
|
510 | 511 | elif action == 'p': |
|
511 | 512 | parent = repo.changelog.parents(node)[0] |
|
512 | 513 | for chunk in patch.diff(repo, parent, node): |
|
513 | 514 | ui.write(chunk) |
|
514 | 515 | action = None |
|
515 | 516 | if action == 'y': |
|
516 | 517 | transplants.append(node) |
|
517 | 518 | elif action == 'm': |
|
518 | 519 | merges.append(node) |
|
519 | 520 | elif action == 'c': |
|
520 | 521 | break |
|
521 | 522 | elif action == 'q': |
|
522 | 523 | transplants = () |
|
523 | 524 | merges = () |
|
524 | 525 | break |
|
525 | 526 | displayer.close() |
|
526 | 527 | return (transplants, merges) |
|
527 | 528 | |
|
528 | 529 | @command('transplant', |
|
529 | 530 | [('s', 'source', '', _('transplant changesets from REPO'), _('REPO')), |
|
530 | 531 | ('b', 'branch', [], _('use this source changeset as head'), _('REV')), |
|
531 | 532 | ('a', 'all', None, _('pull all changesets up to the --branch revisions')), |
|
532 | 533 | ('p', 'prune', [], _('skip over REV'), _('REV')), |
|
533 | 534 | ('m', 'merge', [], _('merge at REV'), _('REV')), |
|
534 | 535 | ('', 'parent', '', |
|
535 | 536 | _('parent to choose when transplanting merge'), _('REV')), |
|
536 | 537 | ('e', 'edit', False, _('invoke editor on commit messages')), |
|
537 | 538 | ('', 'log', None, _('append transplant info to log message')), |
|
538 | 539 | ('c', 'continue', None, _('continue last transplant session ' |
|
539 | 540 | 'after fixing conflicts')), |
|
540 | 541 | ('', 'filter', '', |
|
541 | 542 | _('filter changesets through command'), _('CMD'))], |
|
542 | 543 | _('hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] ' |
|
543 | 544 | '[-m REV] [REV]...')) |
|
544 | 545 | def transplant(ui, repo, *revs, **opts): |
|
545 | 546 | '''transplant changesets from another branch |
|
546 | 547 | |
|
547 | 548 | Selected changesets will be applied on top of the current working |
|
548 | 549 | directory with the log of the original changeset. The changesets |
|
549 | 550 | are copied and will thus appear twice in the history with different |
|
550 | 551 | identities. |
|
551 | 552 | |
|
552 | 553 | Consider using the graft command if everything is inside the same |
|
553 | 554 | repository - it will use merges and will usually give a better result. |
|
554 | 555 | Use the rebase extension if the changesets are unpublished and you want |
|
555 | 556 | to move them instead of copying them. |
|
556 | 557 | |
|
557 | 558 | If --log is specified, log messages will have a comment appended |
|
558 | 559 | of the form:: |
|
559 | 560 | |
|
560 | 561 | (transplanted from CHANGESETHASH) |
|
561 | 562 | |
|
562 | 563 | You can rewrite the changelog message with the --filter option. |
|
563 | 564 | Its argument will be invoked with the current changelog message as |
|
564 | 565 | $1 and the patch as $2. |
|
565 | 566 | |
|
566 | 567 | --source/-s specifies another repository to use for selecting changesets, |
|
567 | 568 | just as if it temporarily had been pulled. |
|
568 | 569 | If --branch/-b is specified, these revisions will be used as |
|
569 | 570 | heads when deciding which changesets to transplant, just as if only |
|
570 | 571 | these revisions had been pulled. |
|
571 | 572 | If --all/-a is specified, all the revisions up to the heads specified |
|
572 | 573 | with --branch will be transplanted. |
|
573 | 574 | |
|
574 | 575 | Example: |
|
575 | 576 | |
|
576 | 577 | - transplant all changes up to REV on top of your current revision:: |
|
577 | 578 | |
|
578 | 579 | hg transplant --branch REV --all |
|
579 | 580 | |
|
580 | 581 | You can optionally mark selected transplanted changesets as merge |
|
581 | 582 | changesets. You will not be prompted to transplant any ancestors |
|
582 | 583 | of a merged transplant, and you can merge descendants of them |
|
583 | 584 | normally instead of transplanting them. |
|
584 | 585 | |
|
585 | 586 | Merge changesets may be transplanted directly by specifying the |
|
586 | 587 | proper parent changeset by calling :hg:`transplant --parent`. |
|
587 | 588 | |
|
588 | 589 | If no merges or revisions are provided, :hg:`transplant` will |
|
589 | 590 | start an interactive changeset browser. |
|
590 | 591 | |
|
591 | 592 | If a changeset application fails, you can fix the merge by hand |
|
592 | 593 | and then resume where you left off by calling :hg:`transplant |
|
593 | 594 | --continue/-c`. |
|
594 | 595 | ''' |
|
595 | 596 | with repo.wlock(): |
|
596 | 597 | return _dotransplant(ui, repo, *revs, **opts) |
|
597 | 598 | |
|
598 | 599 | def _dotransplant(ui, repo, *revs, **opts): |
|
599 | 600 | def incwalk(repo, csets, match=util.always): |
|
600 | 601 | for node in csets: |
|
601 | 602 | if match(node): |
|
602 | 603 | yield node |
|
603 | 604 | |
|
604 | 605 | def transplantwalk(repo, dest, heads, match=util.always): |
|
605 | 606 | '''Yield all nodes that are ancestors of a head but not ancestors |
|
606 | 607 | of dest. |
|
607 | 608 | If no heads are specified, the heads of repo will be used.''' |
|
608 | 609 | if not heads: |
|
609 | 610 | heads = repo.heads() |
|
610 | 611 | ancestors = [] |
|
611 | 612 | ctx = repo[dest] |
|
612 | 613 | for head in heads: |
|
613 | 614 | ancestors.append(ctx.ancestor(repo[head]).node()) |
|
614 | 615 | for node in repo.changelog.nodesbetween(ancestors, heads)[0]: |
|
615 | 616 | if match(node): |
|
616 | 617 | yield node |
|
617 | 618 | |
|
618 | 619 | def checkopts(opts, revs): |
|
619 | 620 | if opts.get('continue'): |
|
620 | 621 | if opts.get('branch') or opts.get('all') or opts.get('merge'): |
|
621 | 622 | raise error.Abort(_('--continue is incompatible with ' |
|
622 | 623 | '--branch, --all and --merge')) |
|
623 | 624 | return |
|
624 | 625 | if not (opts.get('source') or revs or |
|
625 | 626 | opts.get('merge') or opts.get('branch')): |
|
626 | 627 | raise error.Abort(_('no source URL, branch revision, or revision ' |
|
627 | 628 | 'list provided')) |
|
628 | 629 | if opts.get('all'): |
|
629 | 630 | if not opts.get('branch'): |
|
630 | 631 | raise error.Abort(_('--all requires a branch revision')) |
|
631 | 632 | if revs: |
|
632 | 633 | raise error.Abort(_('--all is incompatible with a ' |
|
633 | 634 | 'revision list')) |
|
634 | 635 | |
|
635 | 636 | checkopts(opts, revs) |
|
636 | 637 | |
|
637 | 638 | if not opts.get('log'): |
|
638 | 639 | # deprecated config: transplant.log |
|
639 | 640 | opts['log'] = ui.config('transplant', 'log') |
|
640 | 641 | if not opts.get('filter'): |
|
641 | 642 | # deprecated config: transplant.filter |
|
642 | 643 | opts['filter'] = ui.config('transplant', 'filter') |
|
643 | 644 | |
|
644 | 645 | tp = transplanter(ui, repo, opts) |
|
645 | 646 | |
|
646 | 647 | p1, p2 = repo.dirstate.parents() |
|
647 | 648 | if len(repo) > 0 and p1 == revlog.nullid: |
|
648 | 649 | raise error.Abort(_('no revision checked out')) |
|
649 | 650 | if opts.get('continue'): |
|
650 | 651 | if not tp.canresume(): |
|
651 | 652 | raise error.Abort(_('no transplant to continue')) |
|
652 | 653 | else: |
|
653 | 654 | cmdutil.checkunfinished(repo) |
|
654 | 655 | if p2 != revlog.nullid: |
|
655 | 656 | raise error.Abort(_('outstanding uncommitted merges')) |
|
656 | 657 | m, a, r, d = repo.status()[:4] |
|
657 | 658 | if m or a or r or d: |
|
658 | 659 | raise error.Abort(_('outstanding local changes')) |
|
659 | 660 | |
|
660 | 661 | sourcerepo = opts.get('source') |
|
661 | 662 | if sourcerepo: |
|
662 | 663 | peer = hg.peer(repo, opts, ui.expandpath(sourcerepo)) |
|
663 | 664 | heads = map(peer.lookup, opts.get('branch', ())) |
|
664 | 665 | target = set(heads) |
|
665 | 666 | for r in revs: |
|
666 | 667 | try: |
|
667 | 668 | target.add(peer.lookup(r)) |
|
668 | 669 | except error.RepoError: |
|
669 | 670 | pass |
|
670 | 671 | source, csets, cleanupfn = bundlerepo.getremotechanges(ui, repo, peer, |
|
671 | 672 | onlyheads=sorted(target), force=True) |
|
672 | 673 | else: |
|
673 | 674 | source = repo |
|
674 | 675 | heads = map(source.lookup, opts.get('branch', ())) |
|
675 | 676 | cleanupfn = None |
|
676 | 677 | |
|
677 | 678 | try: |
|
678 | 679 | if opts.get('continue'): |
|
679 | 680 | tp.resume(repo, source, opts) |
|
680 | 681 | return |
|
681 | 682 | |
|
682 | 683 | tf = tp.transplantfilter(repo, source, p1) |
|
683 | 684 | if opts.get('prune'): |
|
684 | 685 | prune = set(source.lookup(r) |
|
685 | 686 | for r in scmutil.revrange(source, opts.get('prune'))) |
|
686 | 687 | matchfn = lambda x: tf(x) and x not in prune |
|
687 | 688 | else: |
|
688 | 689 | matchfn = tf |
|
689 | 690 | merges = map(source.lookup, opts.get('merge', ())) |
|
690 | 691 | revmap = {} |
|
691 | 692 | if revs: |
|
692 | 693 | for r in scmutil.revrange(source, revs): |
|
693 | 694 | revmap[int(r)] = source.lookup(r) |
|
694 | 695 | elif opts.get('all') or not merges: |
|
695 | 696 | if source != repo: |
|
696 | 697 | alltransplants = incwalk(source, csets, match=matchfn) |
|
697 | 698 | else: |
|
698 | 699 | alltransplants = transplantwalk(source, p1, heads, |
|
699 | 700 | match=matchfn) |
|
700 | 701 | if opts.get('all'): |
|
701 | 702 | revs = alltransplants |
|
702 | 703 | else: |
|
703 | 704 | revs, newmerges = browserevs(ui, source, alltransplants, opts) |
|
704 | 705 | merges.extend(newmerges) |
|
705 | 706 | for r in revs: |
|
706 | 707 | revmap[source.changelog.rev(r)] = r |
|
707 | 708 | for r in merges: |
|
708 | 709 | revmap[source.changelog.rev(r)] = r |
|
709 | 710 | |
|
710 | 711 | tp.apply(repo, source, revmap, merges, opts) |
|
711 | 712 | finally: |
|
712 | 713 | if cleanupfn: |
|
713 | 714 | cleanupfn() |
|
714 | 715 | |
|
715 | 716 | revsetpredicate = registrar.revsetpredicate() |
|
716 | 717 | |
|
717 | 718 | @revsetpredicate('transplanted([set])') |
|
718 | 719 | def revsettransplanted(repo, subset, x): |
|
719 | 720 | """Transplanted changesets in set, or all transplanted changesets. |
|
720 | 721 | """ |
|
721 | 722 | if x: |
|
722 | 723 | s = revset.getset(repo, subset, x) |
|
723 | 724 | else: |
|
724 | 725 | s = subset |
|
725 | 726 | return revset.baseset([r for r in s if |
|
726 | 727 | repo[r].extra().get('transplant_source')]) |
|
727 | 728 | |
|
728 | 729 | templatekeyword = registrar.templatekeyword() |
|
729 | 730 | |
|
730 | 731 | @templatekeyword('transplanted') |
|
731 | 732 | def kwtransplanted(repo, ctx, **args): |
|
732 | 733 | """String. The node identifier of the transplanted |
|
733 | 734 | changeset if any.""" |
|
734 | 735 | n = ctx.extra().get('transplant_source') |
|
735 | 736 | return n and nodemod.hex(n) or '' |
|
736 | 737 | |
|
737 | 738 | def extsetup(ui): |
|
738 | 739 | cmdutil.unfinishedstates.append( |
|
739 | 740 | ['transplant/journal', True, False, _('transplant in progress'), |
|
740 | 741 | _("use 'hg transplant --continue' or 'hg update' to abort")]) |
|
741 | 742 | |
|
742 | 743 | # tell hggettext to extract docstrings from these functions: |
|
743 | 744 | i18nfunctions = [revsettransplanted, kwtransplanted] |
@@ -1,1044 +1,1045 b'' | |||
|
1 | 1 | # changegroup.py - Mercurial changegroup manipulation functions |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import struct |
|
12 | 12 | import tempfile |
|
13 | 13 | import weakref |
|
14 | 14 | |
|
15 | 15 | from .i18n import _ |
|
16 | 16 | from .node import ( |
|
17 | 17 | hex, |
|
18 | 18 | nullrev, |
|
19 | 19 | short, |
|
20 | 20 | ) |
|
21 | 21 | |
|
22 | 22 | from . import ( |
|
23 | 23 | branchmap, |
|
24 | 24 | dagutil, |
|
25 | 25 | discovery, |
|
26 | 26 | error, |
|
27 | 27 | mdiff, |
|
28 | 28 | phases, |
|
29 | pycompat, | |
|
29 | 30 | util, |
|
30 | 31 | ) |
|
31 | 32 | |
|
32 | 33 | _CHANGEGROUPV1_DELTA_HEADER = "20s20s20s20s" |
|
33 | 34 | _CHANGEGROUPV2_DELTA_HEADER = "20s20s20s20s20s" |
|
34 | 35 | _CHANGEGROUPV3_DELTA_HEADER = ">20s20s20s20s20sH" |
|
35 | 36 | |
|
36 | 37 | def readexactly(stream, n): |
|
37 | 38 | '''read n bytes from stream.read and abort if less was available''' |
|
38 | 39 | s = stream.read(n) |
|
39 | 40 | if len(s) < n: |
|
40 | 41 | raise error.Abort(_("stream ended unexpectedly" |
|
41 | 42 | " (got %d bytes, expected %d)") |
|
42 | 43 | % (len(s), n)) |
|
43 | 44 | return s |
|
44 | 45 | |
|
45 | 46 | def getchunk(stream): |
|
46 | 47 | """return the next chunk from stream as a string""" |
|
47 | 48 | d = readexactly(stream, 4) |
|
48 | 49 | l = struct.unpack(">l", d)[0] |
|
49 | 50 | if l <= 4: |
|
50 | 51 | if l: |
|
51 | 52 | raise error.Abort(_("invalid chunk length %d") % l) |
|
52 | 53 | return "" |
|
53 | 54 | return readexactly(stream, l - 4) |
|
54 | 55 | |
|
55 | 56 | def chunkheader(length): |
|
56 | 57 | """return a changegroup chunk header (string)""" |
|
57 | 58 | return struct.pack(">l", length + 4) |
|
58 | 59 | |
|
59 | 60 | def closechunk(): |
|
60 | 61 | """return a changegroup chunk header (string) for a zero-length chunk""" |
|
61 | 62 | return struct.pack(">l", 0) |
|
62 | 63 | |
|
63 | 64 | def combineresults(results): |
|
64 | 65 | """logic to combine 0 or more addchangegroup results into one""" |
|
65 | 66 | changedheads = 0 |
|
66 | 67 | result = 1 |
|
67 | 68 | for ret in results: |
|
68 | 69 | # If any changegroup result is 0, return 0 |
|
69 | 70 | if ret == 0: |
|
70 | 71 | result = 0 |
|
71 | 72 | break |
|
72 | 73 | if ret < -1: |
|
73 | 74 | changedheads += ret + 1 |
|
74 | 75 | elif ret > 1: |
|
75 | 76 | changedheads += ret - 1 |
|
76 | 77 | if changedheads > 0: |
|
77 | 78 | result = 1 + changedheads |
|
78 | 79 | elif changedheads < 0: |
|
79 | 80 | result = -1 + changedheads |
|
80 | 81 | return result |
|
81 | 82 | |
|
82 | 83 | def writechunks(ui, chunks, filename, vfs=None): |
|
83 | 84 | """Write chunks to a file and return its filename. |
|
84 | 85 | |
|
85 | 86 | The stream is assumed to be a bundle file. |
|
86 | 87 | Existing files will not be overwritten. |
|
87 | 88 | If no filename is specified, a temporary file is created. |
|
88 | 89 | """ |
|
89 | 90 | fh = None |
|
90 | 91 | cleanup = None |
|
91 | 92 | try: |
|
92 | 93 | if filename: |
|
93 | 94 | if vfs: |
|
94 | 95 | fh = vfs.open(filename, "wb") |
|
95 | 96 | else: |
|
96 | 97 | # Increase default buffer size because default is usually |
|
97 | 98 | # small (4k is common on Linux). |
|
98 | 99 | fh = open(filename, "wb", 131072) |
|
99 | 100 | else: |
|
100 | 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 | 103 | cleanup = filename |
|
103 | 104 | for c in chunks: |
|
104 | 105 | fh.write(c) |
|
105 | 106 | cleanup = None |
|
106 | 107 | return filename |
|
107 | 108 | finally: |
|
108 | 109 | if fh is not None: |
|
109 | 110 | fh.close() |
|
110 | 111 | if cleanup is not None: |
|
111 | 112 | if filename and vfs: |
|
112 | 113 | vfs.unlink(cleanup) |
|
113 | 114 | else: |
|
114 | 115 | os.unlink(cleanup) |
|
115 | 116 | |
|
116 | 117 | class cg1unpacker(object): |
|
117 | 118 | """Unpacker for cg1 changegroup streams. |
|
118 | 119 | |
|
119 | 120 | A changegroup unpacker handles the framing of the revision data in |
|
120 | 121 | the wire format. Most consumers will want to use the apply() |
|
121 | 122 | method to add the changes from the changegroup to a repository. |
|
122 | 123 | |
|
123 | 124 | If you're forwarding a changegroup unmodified to another consumer, |
|
124 | 125 | use getchunks(), which returns an iterator of changegroup |
|
125 | 126 | chunks. This is mostly useful for cases where you need to know the |
|
126 | 127 | data stream has ended by observing the end of the changegroup. |
|
127 | 128 | |
|
128 | 129 | deltachunk() is useful only if you're applying delta data. Most |
|
129 | 130 | consumers should prefer apply() instead. |
|
130 | 131 | |
|
131 | 132 | A few other public methods exist. Those are used only for |
|
132 | 133 | bundlerepo and some debug commands - their use is discouraged. |
|
133 | 134 | """ |
|
134 | 135 | deltaheader = _CHANGEGROUPV1_DELTA_HEADER |
|
135 | 136 | deltaheadersize = struct.calcsize(deltaheader) |
|
136 | 137 | version = '01' |
|
137 | 138 | _grouplistcount = 1 # One list of files after the manifests |
|
138 | 139 | |
|
139 | 140 | def __init__(self, fh, alg, extras=None): |
|
140 | 141 | if alg is None: |
|
141 | 142 | alg = 'UN' |
|
142 | 143 | if alg not in util.compengines.supportedbundletypes: |
|
143 | 144 | raise error.Abort(_('unknown stream compression type: %s') |
|
144 | 145 | % alg) |
|
145 | 146 | if alg == 'BZ': |
|
146 | 147 | alg = '_truncatedBZ' |
|
147 | 148 | |
|
148 | 149 | compengine = util.compengines.forbundletype(alg) |
|
149 | 150 | self._stream = compengine.decompressorreader(fh) |
|
150 | 151 | self._type = alg |
|
151 | 152 | self.extras = extras or {} |
|
152 | 153 | self.callback = None |
|
153 | 154 | |
|
154 | 155 | # These methods (compressed, read, seek, tell) all appear to only |
|
155 | 156 | # be used by bundlerepo, but it's a little hard to tell. |
|
156 | 157 | def compressed(self): |
|
157 | 158 | return self._type is not None and self._type != 'UN' |
|
158 | 159 | def read(self, l): |
|
159 | 160 | return self._stream.read(l) |
|
160 | 161 | def seek(self, pos): |
|
161 | 162 | return self._stream.seek(pos) |
|
162 | 163 | def tell(self): |
|
163 | 164 | return self._stream.tell() |
|
164 | 165 | def close(self): |
|
165 | 166 | return self._stream.close() |
|
166 | 167 | |
|
167 | 168 | def _chunklength(self): |
|
168 | 169 | d = readexactly(self._stream, 4) |
|
169 | 170 | l = struct.unpack(">l", d)[0] |
|
170 | 171 | if l <= 4: |
|
171 | 172 | if l: |
|
172 | 173 | raise error.Abort(_("invalid chunk length %d") % l) |
|
173 | 174 | return 0 |
|
174 | 175 | if self.callback: |
|
175 | 176 | self.callback() |
|
176 | 177 | return l - 4 |
|
177 | 178 | |
|
178 | 179 | def changelogheader(self): |
|
179 | 180 | """v10 does not have a changelog header chunk""" |
|
180 | 181 | return {} |
|
181 | 182 | |
|
182 | 183 | def manifestheader(self): |
|
183 | 184 | """v10 does not have a manifest header chunk""" |
|
184 | 185 | return {} |
|
185 | 186 | |
|
186 | 187 | def filelogheader(self): |
|
187 | 188 | """return the header of the filelogs chunk, v10 only has the filename""" |
|
188 | 189 | l = self._chunklength() |
|
189 | 190 | if not l: |
|
190 | 191 | return {} |
|
191 | 192 | fname = readexactly(self._stream, l) |
|
192 | 193 | return {'filename': fname} |
|
193 | 194 | |
|
194 | 195 | def _deltaheader(self, headertuple, prevnode): |
|
195 | 196 | node, p1, p2, cs = headertuple |
|
196 | 197 | if prevnode is None: |
|
197 | 198 | deltabase = p1 |
|
198 | 199 | else: |
|
199 | 200 | deltabase = prevnode |
|
200 | 201 | flags = 0 |
|
201 | 202 | return node, p1, p2, deltabase, cs, flags |
|
202 | 203 | |
|
203 | 204 | def deltachunk(self, prevnode): |
|
204 | 205 | l = self._chunklength() |
|
205 | 206 | if not l: |
|
206 | 207 | return {} |
|
207 | 208 | headerdata = readexactly(self._stream, self.deltaheadersize) |
|
208 | 209 | header = struct.unpack(self.deltaheader, headerdata) |
|
209 | 210 | delta = readexactly(self._stream, l - self.deltaheadersize) |
|
210 | 211 | node, p1, p2, deltabase, cs, flags = self._deltaheader(header, prevnode) |
|
211 | 212 | return {'node': node, 'p1': p1, 'p2': p2, 'cs': cs, |
|
212 | 213 | 'deltabase': deltabase, 'delta': delta, 'flags': flags} |
|
213 | 214 | |
|
214 | 215 | def getchunks(self): |
|
215 | 216 | """returns all the chunks contains in the bundle |
|
216 | 217 | |
|
217 | 218 | Used when you need to forward the binary stream to a file or another |
|
218 | 219 | network API. To do so, it parse the changegroup data, otherwise it will |
|
219 | 220 | block in case of sshrepo because it don't know the end of the stream. |
|
220 | 221 | """ |
|
221 | 222 | # an empty chunkgroup is the end of the changegroup |
|
222 | 223 | # a changegroup has at least 2 chunkgroups (changelog and manifest). |
|
223 | 224 | # after that, changegroup versions 1 and 2 have a series of groups |
|
224 | 225 | # with one group per file. changegroup 3 has a series of directory |
|
225 | 226 | # manifests before the files. |
|
226 | 227 | count = 0 |
|
227 | 228 | emptycount = 0 |
|
228 | 229 | while emptycount < self._grouplistcount: |
|
229 | 230 | empty = True |
|
230 | 231 | count += 1 |
|
231 | 232 | while True: |
|
232 | 233 | chunk = getchunk(self) |
|
233 | 234 | if not chunk: |
|
234 | 235 | if empty and count > 2: |
|
235 | 236 | emptycount += 1 |
|
236 | 237 | break |
|
237 | 238 | empty = False |
|
238 | 239 | yield chunkheader(len(chunk)) |
|
239 | 240 | pos = 0 |
|
240 | 241 | while pos < len(chunk): |
|
241 | 242 | next = pos + 2**20 |
|
242 | 243 | yield chunk[pos:next] |
|
243 | 244 | pos = next |
|
244 | 245 | yield closechunk() |
|
245 | 246 | |
|
246 | 247 | def _unpackmanifests(self, repo, revmap, trp, prog, numchanges): |
|
247 | 248 | # We know that we'll never have more manifests than we had |
|
248 | 249 | # changesets. |
|
249 | 250 | self.callback = prog(_('manifests'), numchanges) |
|
250 | 251 | # no need to check for empty manifest group here: |
|
251 | 252 | # if the result of the merge of 1 and 2 is the same in 3 and 4, |
|
252 | 253 | # no new manifest will be created and the manifest group will |
|
253 | 254 | # be empty during the pull |
|
254 | 255 | self.manifestheader() |
|
255 | 256 | repo.manifestlog._revlog.addgroup(self, revmap, trp) |
|
256 | 257 | repo.ui.progress(_('manifests'), None) |
|
257 | 258 | self.callback = None |
|
258 | 259 | |
|
259 | 260 | def apply(self, repo, srctype, url, emptyok=False, |
|
260 | 261 | targetphase=phases.draft, expectedtotal=None): |
|
261 | 262 | """Add the changegroup returned by source.read() to this repo. |
|
262 | 263 | srctype is a string like 'push', 'pull', or 'unbundle'. url is |
|
263 | 264 | the URL of the repo where this changegroup is coming from. |
|
264 | 265 | |
|
265 | 266 | Return an integer summarizing the change to this repo: |
|
266 | 267 | - nothing changed or no source: 0 |
|
267 | 268 | - more heads than before: 1+added heads (2..n) |
|
268 | 269 | - fewer heads than before: -1-removed heads (-2..-n) |
|
269 | 270 | - number of heads stays the same: 1 |
|
270 | 271 | """ |
|
271 | 272 | repo = repo.unfiltered() |
|
272 | 273 | def csmap(x): |
|
273 | 274 | repo.ui.debug("add changeset %s\n" % short(x)) |
|
274 | 275 | return len(cl) |
|
275 | 276 | |
|
276 | 277 | def revmap(x): |
|
277 | 278 | return cl.rev(x) |
|
278 | 279 | |
|
279 | 280 | changesets = files = revisions = 0 |
|
280 | 281 | |
|
281 | 282 | try: |
|
282 | 283 | with repo.transaction("\n".join([srctype, |
|
283 | 284 | util.hidepassword(url)])) as tr: |
|
284 | 285 | # The transaction could have been created before and already |
|
285 | 286 | # carries source information. In this case we use the top |
|
286 | 287 | # level data. We overwrite the argument because we need to use |
|
287 | 288 | # the top level value (if they exist) in this function. |
|
288 | 289 | srctype = tr.hookargs.setdefault('source', srctype) |
|
289 | 290 | url = tr.hookargs.setdefault('url', url) |
|
290 | 291 | repo.hook('prechangegroup', throw=True, **tr.hookargs) |
|
291 | 292 | |
|
292 | 293 | # write changelog data to temp files so concurrent readers |
|
293 | 294 | # will not see an inconsistent view |
|
294 | 295 | cl = repo.changelog |
|
295 | 296 | cl.delayupdate(tr) |
|
296 | 297 | oldheads = cl.heads() |
|
297 | 298 | |
|
298 | 299 | trp = weakref.proxy(tr) |
|
299 | 300 | # pull off the changeset group |
|
300 | 301 | repo.ui.status(_("adding changesets\n")) |
|
301 | 302 | clstart = len(cl) |
|
302 | 303 | class prog(object): |
|
303 | 304 | def __init__(self, step, total): |
|
304 | 305 | self._step = step |
|
305 | 306 | self._total = total |
|
306 | 307 | self._count = 1 |
|
307 | 308 | def __call__(self): |
|
308 | 309 | repo.ui.progress(self._step, self._count, |
|
309 | 310 | unit=_('chunks'), total=self._total) |
|
310 | 311 | self._count += 1 |
|
311 | 312 | self.callback = prog(_('changesets'), expectedtotal) |
|
312 | 313 | |
|
313 | 314 | efiles = set() |
|
314 | 315 | def onchangelog(cl, node): |
|
315 | 316 | efiles.update(cl.readfiles(node)) |
|
316 | 317 | |
|
317 | 318 | self.changelogheader() |
|
318 | 319 | srccontent = cl.addgroup(self, csmap, trp, |
|
319 | 320 | addrevisioncb=onchangelog) |
|
320 | 321 | efiles = len(efiles) |
|
321 | 322 | |
|
322 | 323 | if not (srccontent or emptyok): |
|
323 | 324 | raise error.Abort(_("received changelog group is empty")) |
|
324 | 325 | clend = len(cl) |
|
325 | 326 | changesets = clend - clstart |
|
326 | 327 | repo.ui.progress(_('changesets'), None) |
|
327 | 328 | self.callback = None |
|
328 | 329 | |
|
329 | 330 | # pull off the manifest group |
|
330 | 331 | repo.ui.status(_("adding manifests\n")) |
|
331 | 332 | self._unpackmanifests(repo, revmap, trp, prog, changesets) |
|
332 | 333 | |
|
333 | 334 | needfiles = {} |
|
334 | 335 | if repo.ui.configbool('server', 'validate', default=False): |
|
335 | 336 | cl = repo.changelog |
|
336 | 337 | ml = repo.manifestlog |
|
337 | 338 | # validate incoming csets have their manifests |
|
338 | 339 | for cset in xrange(clstart, clend): |
|
339 | 340 | mfnode = cl.changelogrevision(cset).manifest |
|
340 | 341 | mfest = ml[mfnode].readdelta() |
|
341 | 342 | # store file nodes we must see |
|
342 | 343 | for f, n in mfest.iteritems(): |
|
343 | 344 | needfiles.setdefault(f, set()).add(n) |
|
344 | 345 | |
|
345 | 346 | # process the files |
|
346 | 347 | repo.ui.status(_("adding file changes\n")) |
|
347 | 348 | newrevs, newfiles = _addchangegroupfiles( |
|
348 | 349 | repo, self, revmap, trp, efiles, needfiles) |
|
349 | 350 | revisions += newrevs |
|
350 | 351 | files += newfiles |
|
351 | 352 | |
|
352 | 353 | dh = 0 |
|
353 | 354 | if oldheads: |
|
354 | 355 | heads = cl.heads() |
|
355 | 356 | dh = len(heads) - len(oldheads) |
|
356 | 357 | for h in heads: |
|
357 | 358 | if h not in oldheads and repo[h].closesbranch(): |
|
358 | 359 | dh -= 1 |
|
359 | 360 | htext = "" |
|
360 | 361 | if dh: |
|
361 | 362 | htext = _(" (%+d heads)") % dh |
|
362 | 363 | |
|
363 | 364 | repo.ui.status(_("added %d changesets" |
|
364 | 365 | " with %d changes to %d files%s\n") |
|
365 | 366 | % (changesets, revisions, files, htext)) |
|
366 | 367 | repo.invalidatevolatilesets() |
|
367 | 368 | |
|
368 | 369 | if changesets > 0: |
|
369 | 370 | if 'node' not in tr.hookargs: |
|
370 | 371 | tr.hookargs['node'] = hex(cl.node(clstart)) |
|
371 | 372 | tr.hookargs['node_last'] = hex(cl.node(clend - 1)) |
|
372 | 373 | hookargs = dict(tr.hookargs) |
|
373 | 374 | else: |
|
374 | 375 | hookargs = dict(tr.hookargs) |
|
375 | 376 | hookargs['node'] = hex(cl.node(clstart)) |
|
376 | 377 | hookargs['node_last'] = hex(cl.node(clend - 1)) |
|
377 | 378 | repo.hook('pretxnchangegroup', throw=True, **hookargs) |
|
378 | 379 | |
|
379 | 380 | added = [cl.node(r) for r in xrange(clstart, clend)] |
|
380 | 381 | publishing = repo.publishing() |
|
381 | 382 | if srctype in ('push', 'serve'): |
|
382 | 383 | # Old servers can not push the boundary themselves. |
|
383 | 384 | # New servers won't push the boundary if changeset already |
|
384 | 385 | # exists locally as secret |
|
385 | 386 | # |
|
386 | 387 | # We should not use added here but the list of all change in |
|
387 | 388 | # the bundle |
|
388 | 389 | if publishing: |
|
389 | 390 | phases.advanceboundary(repo, tr, phases.public, |
|
390 | 391 | srccontent) |
|
391 | 392 | else: |
|
392 | 393 | # Those changesets have been pushed from the |
|
393 | 394 | # outside, their phases are going to be pushed |
|
394 | 395 | # alongside. Therefor `targetphase` is |
|
395 | 396 | # ignored. |
|
396 | 397 | phases.advanceboundary(repo, tr, phases.draft, |
|
397 | 398 | srccontent) |
|
398 | 399 | phases.retractboundary(repo, tr, phases.draft, added) |
|
399 | 400 | elif srctype != 'strip': |
|
400 | 401 | # publishing only alter behavior during push |
|
401 | 402 | # |
|
402 | 403 | # strip should not touch boundary at all |
|
403 | 404 | phases.retractboundary(repo, tr, targetphase, added) |
|
404 | 405 | |
|
405 | 406 | if changesets > 0: |
|
406 | 407 | if srctype != 'strip': |
|
407 | 408 | # During strip, branchcache is invalid but |
|
408 | 409 | # coming call to `destroyed` will repair it. |
|
409 | 410 | # In other case we can safely update cache on |
|
410 | 411 | # disk. |
|
411 | 412 | repo.ui.debug('updating the branch cache\n') |
|
412 | 413 | branchmap.updatecache(repo.filtered('served')) |
|
413 | 414 | |
|
414 | 415 | def runhooks(): |
|
415 | 416 | # These hooks run when the lock releases, not when the |
|
416 | 417 | # transaction closes. So it's possible for the changelog |
|
417 | 418 | # to have changed since we last saw it. |
|
418 | 419 | if clstart >= len(repo): |
|
419 | 420 | return |
|
420 | 421 | |
|
421 | 422 | repo.hook("changegroup", **hookargs) |
|
422 | 423 | |
|
423 | 424 | for n in added: |
|
424 | 425 | args = hookargs.copy() |
|
425 | 426 | args['node'] = hex(n) |
|
426 | 427 | del args['node_last'] |
|
427 | 428 | repo.hook("incoming", **args) |
|
428 | 429 | |
|
429 | 430 | newheads = [h for h in repo.heads() |
|
430 | 431 | if h not in oldheads] |
|
431 | 432 | repo.ui.log("incoming", |
|
432 | 433 | "%s incoming changes - new heads: %s\n", |
|
433 | 434 | len(added), |
|
434 | 435 | ', '.join([hex(c[:6]) for c in newheads])) |
|
435 | 436 | |
|
436 | 437 | tr.addpostclose('changegroup-runhooks-%020i' % clstart, |
|
437 | 438 | lambda tr: repo._afterlock(runhooks)) |
|
438 | 439 | finally: |
|
439 | 440 | repo.ui.flush() |
|
440 | 441 | # never return 0 here: |
|
441 | 442 | if dh < 0: |
|
442 | 443 | return dh - 1 |
|
443 | 444 | else: |
|
444 | 445 | return dh + 1 |
|
445 | 446 | |
|
446 | 447 | class cg2unpacker(cg1unpacker): |
|
447 | 448 | """Unpacker for cg2 streams. |
|
448 | 449 | |
|
449 | 450 | cg2 streams add support for generaldelta, so the delta header |
|
450 | 451 | format is slightly different. All other features about the data |
|
451 | 452 | remain the same. |
|
452 | 453 | """ |
|
453 | 454 | deltaheader = _CHANGEGROUPV2_DELTA_HEADER |
|
454 | 455 | deltaheadersize = struct.calcsize(deltaheader) |
|
455 | 456 | version = '02' |
|
456 | 457 | |
|
457 | 458 | def _deltaheader(self, headertuple, prevnode): |
|
458 | 459 | node, p1, p2, deltabase, cs = headertuple |
|
459 | 460 | flags = 0 |
|
460 | 461 | return node, p1, p2, deltabase, cs, flags |
|
461 | 462 | |
|
462 | 463 | class cg3unpacker(cg2unpacker): |
|
463 | 464 | """Unpacker for cg3 streams. |
|
464 | 465 | |
|
465 | 466 | cg3 streams add support for exchanging treemanifests and revlog |
|
466 | 467 | flags. It adds the revlog flags to the delta header and an empty chunk |
|
467 | 468 | separating manifests and files. |
|
468 | 469 | """ |
|
469 | 470 | deltaheader = _CHANGEGROUPV3_DELTA_HEADER |
|
470 | 471 | deltaheadersize = struct.calcsize(deltaheader) |
|
471 | 472 | version = '03' |
|
472 | 473 | _grouplistcount = 2 # One list of manifests and one list of files |
|
473 | 474 | |
|
474 | 475 | def _deltaheader(self, headertuple, prevnode): |
|
475 | 476 | node, p1, p2, deltabase, cs, flags = headertuple |
|
476 | 477 | return node, p1, p2, deltabase, cs, flags |
|
477 | 478 | |
|
478 | 479 | def _unpackmanifests(self, repo, revmap, trp, prog, numchanges): |
|
479 | 480 | super(cg3unpacker, self)._unpackmanifests(repo, revmap, trp, prog, |
|
480 | 481 | numchanges) |
|
481 | 482 | for chunkdata in iter(self.filelogheader, {}): |
|
482 | 483 | # If we get here, there are directory manifests in the changegroup |
|
483 | 484 | d = chunkdata["filename"] |
|
484 | 485 | repo.ui.debug("adding %s revisions\n" % d) |
|
485 | 486 | dirlog = repo.manifestlog._revlog.dirlog(d) |
|
486 | 487 | if not dirlog.addgroup(self, revmap, trp): |
|
487 | 488 | raise error.Abort(_("received dir revlog group is empty")) |
|
488 | 489 | |
|
489 | 490 | class headerlessfixup(object): |
|
490 | 491 | def __init__(self, fh, h): |
|
491 | 492 | self._h = h |
|
492 | 493 | self._fh = fh |
|
493 | 494 | def read(self, n): |
|
494 | 495 | if self._h: |
|
495 | 496 | d, self._h = self._h[:n], self._h[n:] |
|
496 | 497 | if len(d) < n: |
|
497 | 498 | d += readexactly(self._fh, n - len(d)) |
|
498 | 499 | return d |
|
499 | 500 | return readexactly(self._fh, n) |
|
500 | 501 | |
|
501 | 502 | class cg1packer(object): |
|
502 | 503 | deltaheader = _CHANGEGROUPV1_DELTA_HEADER |
|
503 | 504 | version = '01' |
|
504 | 505 | def __init__(self, repo, bundlecaps=None): |
|
505 | 506 | """Given a source repo, construct a bundler. |
|
506 | 507 | |
|
507 | 508 | bundlecaps is optional and can be used to specify the set of |
|
508 | 509 | capabilities which can be used to build the bundle. |
|
509 | 510 | """ |
|
510 | 511 | # Set of capabilities we can use to build the bundle. |
|
511 | 512 | if bundlecaps is None: |
|
512 | 513 | bundlecaps = set() |
|
513 | 514 | self._bundlecaps = bundlecaps |
|
514 | 515 | # experimental config: bundle.reorder |
|
515 | 516 | reorder = repo.ui.config('bundle', 'reorder', 'auto') |
|
516 | 517 | if reorder == 'auto': |
|
517 | 518 | reorder = None |
|
518 | 519 | else: |
|
519 | 520 | reorder = util.parsebool(reorder) |
|
520 | 521 | self._repo = repo |
|
521 | 522 | self._reorder = reorder |
|
522 | 523 | self._progress = repo.ui.progress |
|
523 | 524 | if self._repo.ui.verbose and not self._repo.ui.debugflag: |
|
524 | 525 | self._verbosenote = self._repo.ui.note |
|
525 | 526 | else: |
|
526 | 527 | self._verbosenote = lambda s: None |
|
527 | 528 | |
|
528 | 529 | def close(self): |
|
529 | 530 | return closechunk() |
|
530 | 531 | |
|
531 | 532 | def fileheader(self, fname): |
|
532 | 533 | return chunkheader(len(fname)) + fname |
|
533 | 534 | |
|
534 | 535 | # Extracted both for clarity and for overriding in extensions. |
|
535 | 536 | def _sortgroup(self, revlog, nodelist, lookup): |
|
536 | 537 | """Sort nodes for change group and turn them into revnums.""" |
|
537 | 538 | # for generaldelta revlogs, we linearize the revs; this will both be |
|
538 | 539 | # much quicker and generate a much smaller bundle |
|
539 | 540 | if (revlog._generaldelta and self._reorder is None) or self._reorder: |
|
540 | 541 | dag = dagutil.revlogdag(revlog) |
|
541 | 542 | return dag.linearize(set(revlog.rev(n) for n in nodelist)) |
|
542 | 543 | else: |
|
543 | 544 | return sorted([revlog.rev(n) for n in nodelist]) |
|
544 | 545 | |
|
545 | 546 | def group(self, nodelist, revlog, lookup, units=None): |
|
546 | 547 | """Calculate a delta group, yielding a sequence of changegroup chunks |
|
547 | 548 | (strings). |
|
548 | 549 | |
|
549 | 550 | Given a list of changeset revs, return a set of deltas and |
|
550 | 551 | metadata corresponding to nodes. The first delta is |
|
551 | 552 | first parent(nodelist[0]) -> nodelist[0], the receiver is |
|
552 | 553 | guaranteed to have this parent as it has all history before |
|
553 | 554 | these changesets. In the case firstparent is nullrev the |
|
554 | 555 | changegroup starts with a full revision. |
|
555 | 556 | |
|
556 | 557 | If units is not None, progress detail will be generated, units specifies |
|
557 | 558 | the type of revlog that is touched (changelog, manifest, etc.). |
|
558 | 559 | """ |
|
559 | 560 | # if we don't have any revisions touched by these changesets, bail |
|
560 | 561 | if len(nodelist) == 0: |
|
561 | 562 | yield self.close() |
|
562 | 563 | return |
|
563 | 564 | |
|
564 | 565 | revs = self._sortgroup(revlog, nodelist, lookup) |
|
565 | 566 | |
|
566 | 567 | # add the parent of the first rev |
|
567 | 568 | p = revlog.parentrevs(revs[0])[0] |
|
568 | 569 | revs.insert(0, p) |
|
569 | 570 | |
|
570 | 571 | # build deltas |
|
571 | 572 | total = len(revs) - 1 |
|
572 | 573 | msgbundling = _('bundling') |
|
573 | 574 | for r in xrange(len(revs) - 1): |
|
574 | 575 | if units is not None: |
|
575 | 576 | self._progress(msgbundling, r + 1, unit=units, total=total) |
|
576 | 577 | prev, curr = revs[r], revs[r + 1] |
|
577 | 578 | linknode = lookup(revlog.node(curr)) |
|
578 | 579 | for c in self.revchunk(revlog, curr, prev, linknode): |
|
579 | 580 | yield c |
|
580 | 581 | |
|
581 | 582 | if units is not None: |
|
582 | 583 | self._progress(msgbundling, None) |
|
583 | 584 | yield self.close() |
|
584 | 585 | |
|
585 | 586 | # filter any nodes that claim to be part of the known set |
|
586 | 587 | def prune(self, revlog, missing, commonrevs): |
|
587 | 588 | rr, rl = revlog.rev, revlog.linkrev |
|
588 | 589 | return [n for n in missing if rl(rr(n)) not in commonrevs] |
|
589 | 590 | |
|
590 | 591 | def _packmanifests(self, dir, mfnodes, lookuplinknode): |
|
591 | 592 | """Pack flat manifests into a changegroup stream.""" |
|
592 | 593 | assert not dir |
|
593 | 594 | for chunk in self.group(mfnodes, self._repo.manifestlog._revlog, |
|
594 | 595 | lookuplinknode, units=_('manifests')): |
|
595 | 596 | yield chunk |
|
596 | 597 | |
|
597 | 598 | def _manifestsdone(self): |
|
598 | 599 | return '' |
|
599 | 600 | |
|
600 | 601 | def generate(self, commonrevs, clnodes, fastpathlinkrev, source): |
|
601 | 602 | '''yield a sequence of changegroup chunks (strings)''' |
|
602 | 603 | repo = self._repo |
|
603 | 604 | cl = repo.changelog |
|
604 | 605 | |
|
605 | 606 | clrevorder = {} |
|
606 | 607 | mfs = {} # needed manifests |
|
607 | 608 | fnodes = {} # needed file nodes |
|
608 | 609 | changedfiles = set() |
|
609 | 610 | |
|
610 | 611 | # Callback for the changelog, used to collect changed files and manifest |
|
611 | 612 | # nodes. |
|
612 | 613 | # Returns the linkrev node (identity in the changelog case). |
|
613 | 614 | def lookupcl(x): |
|
614 | 615 | c = cl.read(x) |
|
615 | 616 | clrevorder[x] = len(clrevorder) |
|
616 | 617 | n = c[0] |
|
617 | 618 | # record the first changeset introducing this manifest version |
|
618 | 619 | mfs.setdefault(n, x) |
|
619 | 620 | # Record a complete list of potentially-changed files in |
|
620 | 621 | # this manifest. |
|
621 | 622 | changedfiles.update(c[3]) |
|
622 | 623 | return x |
|
623 | 624 | |
|
624 | 625 | self._verbosenote(_('uncompressed size of bundle content:\n')) |
|
625 | 626 | size = 0 |
|
626 | 627 | for chunk in self.group(clnodes, cl, lookupcl, units=_('changesets')): |
|
627 | 628 | size += len(chunk) |
|
628 | 629 | yield chunk |
|
629 | 630 | self._verbosenote(_('%8.i (changelog)\n') % size) |
|
630 | 631 | |
|
631 | 632 | # We need to make sure that the linkrev in the changegroup refers to |
|
632 | 633 | # the first changeset that introduced the manifest or file revision. |
|
633 | 634 | # The fastpath is usually safer than the slowpath, because the filelogs |
|
634 | 635 | # are walked in revlog order. |
|
635 | 636 | # |
|
636 | 637 | # When taking the slowpath with reorder=None and the manifest revlog |
|
637 | 638 | # uses generaldelta, the manifest may be walked in the "wrong" order. |
|
638 | 639 | # Without 'clrevorder', we would get an incorrect linkrev (see fix in |
|
639 | 640 | # cc0ff93d0c0c). |
|
640 | 641 | # |
|
641 | 642 | # When taking the fastpath, we are only vulnerable to reordering |
|
642 | 643 | # of the changelog itself. The changelog never uses generaldelta, so |
|
643 | 644 | # it is only reordered when reorder=True. To handle this case, we |
|
644 | 645 | # simply take the slowpath, which already has the 'clrevorder' logic. |
|
645 | 646 | # This was also fixed in cc0ff93d0c0c. |
|
646 | 647 | fastpathlinkrev = fastpathlinkrev and not self._reorder |
|
647 | 648 | # Treemanifests don't work correctly with fastpathlinkrev |
|
648 | 649 | # either, because we don't discover which directory nodes to |
|
649 | 650 | # send along with files. This could probably be fixed. |
|
650 | 651 | fastpathlinkrev = fastpathlinkrev and ( |
|
651 | 652 | 'treemanifest' not in repo.requirements) |
|
652 | 653 | |
|
653 | 654 | for chunk in self.generatemanifests(commonrevs, clrevorder, |
|
654 | 655 | fastpathlinkrev, mfs, fnodes): |
|
655 | 656 | yield chunk |
|
656 | 657 | mfs.clear() |
|
657 | 658 | clrevs = set(cl.rev(x) for x in clnodes) |
|
658 | 659 | |
|
659 | 660 | if not fastpathlinkrev: |
|
660 | 661 | def linknodes(unused, fname): |
|
661 | 662 | return fnodes.get(fname, {}) |
|
662 | 663 | else: |
|
663 | 664 | cln = cl.node |
|
664 | 665 | def linknodes(filerevlog, fname): |
|
665 | 666 | llr = filerevlog.linkrev |
|
666 | 667 | fln = filerevlog.node |
|
667 | 668 | revs = ((r, llr(r)) for r in filerevlog) |
|
668 | 669 | return dict((fln(r), cln(lr)) for r, lr in revs if lr in clrevs) |
|
669 | 670 | |
|
670 | 671 | for chunk in self.generatefiles(changedfiles, linknodes, commonrevs, |
|
671 | 672 | source): |
|
672 | 673 | yield chunk |
|
673 | 674 | |
|
674 | 675 | yield self.close() |
|
675 | 676 | |
|
676 | 677 | if clnodes: |
|
677 | 678 | repo.hook('outgoing', node=hex(clnodes[0]), source=source) |
|
678 | 679 | |
|
679 | 680 | def generatemanifests(self, commonrevs, clrevorder, fastpathlinkrev, mfs, |
|
680 | 681 | fnodes): |
|
681 | 682 | repo = self._repo |
|
682 | 683 | mfl = repo.manifestlog |
|
683 | 684 | dirlog = mfl._revlog.dirlog |
|
684 | 685 | tmfnodes = {'': mfs} |
|
685 | 686 | |
|
686 | 687 | # Callback for the manifest, used to collect linkrevs for filelog |
|
687 | 688 | # revisions. |
|
688 | 689 | # Returns the linkrev node (collected in lookupcl). |
|
689 | 690 | def makelookupmflinknode(dir): |
|
690 | 691 | if fastpathlinkrev: |
|
691 | 692 | assert not dir |
|
692 | 693 | return mfs.__getitem__ |
|
693 | 694 | |
|
694 | 695 | def lookupmflinknode(x): |
|
695 | 696 | """Callback for looking up the linknode for manifests. |
|
696 | 697 | |
|
697 | 698 | Returns the linkrev node for the specified manifest. |
|
698 | 699 | |
|
699 | 700 | SIDE EFFECT: |
|
700 | 701 | |
|
701 | 702 | 1) fclnodes gets populated with the list of relevant |
|
702 | 703 | file nodes if we're not using fastpathlinkrev |
|
703 | 704 | 2) When treemanifests are in use, collects treemanifest nodes |
|
704 | 705 | to send |
|
705 | 706 | |
|
706 | 707 | Note that this means manifests must be completely sent to |
|
707 | 708 | the client before you can trust the list of files and |
|
708 | 709 | treemanifests to send. |
|
709 | 710 | """ |
|
710 | 711 | clnode = tmfnodes[dir][x] |
|
711 | 712 | mdata = mfl.get(dir, x).readfast(shallow=True) |
|
712 | 713 | for p, n, fl in mdata.iterentries(): |
|
713 | 714 | if fl == 't': # subdirectory manifest |
|
714 | 715 | subdir = dir + p + '/' |
|
715 | 716 | tmfclnodes = tmfnodes.setdefault(subdir, {}) |
|
716 | 717 | tmfclnode = tmfclnodes.setdefault(n, clnode) |
|
717 | 718 | if clrevorder[clnode] < clrevorder[tmfclnode]: |
|
718 | 719 | tmfclnodes[n] = clnode |
|
719 | 720 | else: |
|
720 | 721 | f = dir + p |
|
721 | 722 | fclnodes = fnodes.setdefault(f, {}) |
|
722 | 723 | fclnode = fclnodes.setdefault(n, clnode) |
|
723 | 724 | if clrevorder[clnode] < clrevorder[fclnode]: |
|
724 | 725 | fclnodes[n] = clnode |
|
725 | 726 | return clnode |
|
726 | 727 | return lookupmflinknode |
|
727 | 728 | |
|
728 | 729 | size = 0 |
|
729 | 730 | while tmfnodes: |
|
730 | 731 | dir = min(tmfnodes) |
|
731 | 732 | nodes = tmfnodes[dir] |
|
732 | 733 | prunednodes = self.prune(dirlog(dir), nodes, commonrevs) |
|
733 | 734 | if not dir or prunednodes: |
|
734 | 735 | for x in self._packmanifests(dir, prunednodes, |
|
735 | 736 | makelookupmflinknode(dir)): |
|
736 | 737 | size += len(x) |
|
737 | 738 | yield x |
|
738 | 739 | del tmfnodes[dir] |
|
739 | 740 | self._verbosenote(_('%8.i (manifests)\n') % size) |
|
740 | 741 | yield self._manifestsdone() |
|
741 | 742 | |
|
742 | 743 | # The 'source' parameter is useful for extensions |
|
743 | 744 | def generatefiles(self, changedfiles, linknodes, commonrevs, source): |
|
744 | 745 | repo = self._repo |
|
745 | 746 | progress = self._progress |
|
746 | 747 | msgbundling = _('bundling') |
|
747 | 748 | |
|
748 | 749 | total = len(changedfiles) |
|
749 | 750 | # for progress output |
|
750 | 751 | msgfiles = _('files') |
|
751 | 752 | for i, fname in enumerate(sorted(changedfiles)): |
|
752 | 753 | filerevlog = repo.file(fname) |
|
753 | 754 | if not filerevlog: |
|
754 | 755 | raise error.Abort(_("empty or missing revlog for %s") % fname) |
|
755 | 756 | |
|
756 | 757 | linkrevnodes = linknodes(filerevlog, fname) |
|
757 | 758 | # Lookup for filenodes, we collected the linkrev nodes above in the |
|
758 | 759 | # fastpath case and with lookupmf in the slowpath case. |
|
759 | 760 | def lookupfilelog(x): |
|
760 | 761 | return linkrevnodes[x] |
|
761 | 762 | |
|
762 | 763 | filenodes = self.prune(filerevlog, linkrevnodes, commonrevs) |
|
763 | 764 | if filenodes: |
|
764 | 765 | progress(msgbundling, i + 1, item=fname, unit=msgfiles, |
|
765 | 766 | total=total) |
|
766 | 767 | h = self.fileheader(fname) |
|
767 | 768 | size = len(h) |
|
768 | 769 | yield h |
|
769 | 770 | for chunk in self.group(filenodes, filerevlog, lookupfilelog): |
|
770 | 771 | size += len(chunk) |
|
771 | 772 | yield chunk |
|
772 | 773 | self._verbosenote(_('%8.i %s\n') % (size, fname)) |
|
773 | 774 | progress(msgbundling, None) |
|
774 | 775 | |
|
775 | 776 | def deltaparent(self, revlog, rev, p1, p2, prev): |
|
776 | 777 | return prev |
|
777 | 778 | |
|
778 | 779 | def revchunk(self, revlog, rev, prev, linknode): |
|
779 | 780 | node = revlog.node(rev) |
|
780 | 781 | p1, p2 = revlog.parentrevs(rev) |
|
781 | 782 | base = self.deltaparent(revlog, rev, p1, p2, prev) |
|
782 | 783 | |
|
783 | 784 | prefix = '' |
|
784 | 785 | if revlog.iscensored(base) or revlog.iscensored(rev): |
|
785 | 786 | try: |
|
786 | 787 | delta = revlog.revision(node, raw=True) |
|
787 | 788 | except error.CensoredNodeError as e: |
|
788 | 789 | delta = e.tombstone |
|
789 | 790 | if base == nullrev: |
|
790 | 791 | prefix = mdiff.trivialdiffheader(len(delta)) |
|
791 | 792 | else: |
|
792 | 793 | baselen = revlog.rawsize(base) |
|
793 | 794 | prefix = mdiff.replacediffheader(baselen, len(delta)) |
|
794 | 795 | elif base == nullrev: |
|
795 | 796 | delta = revlog.revision(node, raw=True) |
|
796 | 797 | prefix = mdiff.trivialdiffheader(len(delta)) |
|
797 | 798 | else: |
|
798 | 799 | delta = revlog.revdiff(base, rev) |
|
799 | 800 | p1n, p2n = revlog.parents(node) |
|
800 | 801 | basenode = revlog.node(base) |
|
801 | 802 | flags = revlog.flags(rev) |
|
802 | 803 | meta = self.builddeltaheader(node, p1n, p2n, basenode, linknode, flags) |
|
803 | 804 | meta += prefix |
|
804 | 805 | l = len(meta) + len(delta) |
|
805 | 806 | yield chunkheader(l) |
|
806 | 807 | yield meta |
|
807 | 808 | yield delta |
|
808 | 809 | def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags): |
|
809 | 810 | # do nothing with basenode, it is implicitly the previous one in HG10 |
|
810 | 811 | # do nothing with flags, it is implicitly 0 for cg1 and cg2 |
|
811 | 812 | return struct.pack(self.deltaheader, node, p1n, p2n, linknode) |
|
812 | 813 | |
|
813 | 814 | class cg2packer(cg1packer): |
|
814 | 815 | version = '02' |
|
815 | 816 | deltaheader = _CHANGEGROUPV2_DELTA_HEADER |
|
816 | 817 | |
|
817 | 818 | def __init__(self, repo, bundlecaps=None): |
|
818 | 819 | super(cg2packer, self).__init__(repo, bundlecaps) |
|
819 | 820 | if self._reorder is None: |
|
820 | 821 | # Since generaldelta is directly supported by cg2, reordering |
|
821 | 822 | # generally doesn't help, so we disable it by default (treating |
|
822 | 823 | # bundle.reorder=auto just like bundle.reorder=False). |
|
823 | 824 | self._reorder = False |
|
824 | 825 | |
|
825 | 826 | def deltaparent(self, revlog, rev, p1, p2, prev): |
|
826 | 827 | dp = revlog.deltaparent(rev) |
|
827 | 828 | if dp == nullrev and revlog.storedeltachains: |
|
828 | 829 | # Avoid sending full revisions when delta parent is null. Pick prev |
|
829 | 830 | # in that case. It's tempting to pick p1 in this case, as p1 will |
|
830 | 831 | # be smaller in the common case. However, computing a delta against |
|
831 | 832 | # p1 may require resolving the raw text of p1, which could be |
|
832 | 833 | # expensive. The revlog caches should have prev cached, meaning |
|
833 | 834 | # less CPU for changegroup generation. There is likely room to add |
|
834 | 835 | # a flag and/or config option to control this behavior. |
|
835 | 836 | return prev |
|
836 | 837 | elif dp == nullrev: |
|
837 | 838 | # revlog is configured to use full snapshot for a reason, |
|
838 | 839 | # stick to full snapshot. |
|
839 | 840 | return nullrev |
|
840 | 841 | elif dp not in (p1, p2, prev): |
|
841 | 842 | # Pick prev when we can't be sure remote has the base revision. |
|
842 | 843 | return prev |
|
843 | 844 | else: |
|
844 | 845 | return dp |
|
845 | 846 | |
|
846 | 847 | def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags): |
|
847 | 848 | # Do nothing with flags, it is implicitly 0 in cg1 and cg2 |
|
848 | 849 | return struct.pack(self.deltaheader, node, p1n, p2n, basenode, linknode) |
|
849 | 850 | |
|
850 | 851 | class cg3packer(cg2packer): |
|
851 | 852 | version = '03' |
|
852 | 853 | deltaheader = _CHANGEGROUPV3_DELTA_HEADER |
|
853 | 854 | |
|
854 | 855 | def _packmanifests(self, dir, mfnodes, lookuplinknode): |
|
855 | 856 | if dir: |
|
856 | 857 | yield self.fileheader(dir) |
|
857 | 858 | |
|
858 | 859 | dirlog = self._repo.manifestlog._revlog.dirlog(dir) |
|
859 | 860 | for chunk in self.group(mfnodes, dirlog, lookuplinknode, |
|
860 | 861 | units=_('manifests')): |
|
861 | 862 | yield chunk |
|
862 | 863 | |
|
863 | 864 | def _manifestsdone(self): |
|
864 | 865 | return self.close() |
|
865 | 866 | |
|
866 | 867 | def builddeltaheader(self, node, p1n, p2n, basenode, linknode, flags): |
|
867 | 868 | return struct.pack( |
|
868 | 869 | self.deltaheader, node, p1n, p2n, basenode, linknode, flags) |
|
869 | 870 | |
|
870 | 871 | _packermap = {'01': (cg1packer, cg1unpacker), |
|
871 | 872 | # cg2 adds support for exchanging generaldelta |
|
872 | 873 | '02': (cg2packer, cg2unpacker), |
|
873 | 874 | # cg3 adds support for exchanging revlog flags and treemanifests |
|
874 | 875 | '03': (cg3packer, cg3unpacker), |
|
875 | 876 | } |
|
876 | 877 | |
|
877 | 878 | def allsupportedversions(repo): |
|
878 | 879 | versions = set(_packermap.keys()) |
|
879 | 880 | if not (repo.ui.configbool('experimental', 'changegroup3') or |
|
880 | 881 | repo.ui.configbool('experimental', 'treemanifest') or |
|
881 | 882 | 'treemanifest' in repo.requirements): |
|
882 | 883 | versions.discard('03') |
|
883 | 884 | return versions |
|
884 | 885 | |
|
885 | 886 | # Changegroup versions that can be applied to the repo |
|
886 | 887 | def supportedincomingversions(repo): |
|
887 | 888 | return allsupportedversions(repo) |
|
888 | 889 | |
|
889 | 890 | # Changegroup versions that can be created from the repo |
|
890 | 891 | def supportedoutgoingversions(repo): |
|
891 | 892 | versions = allsupportedversions(repo) |
|
892 | 893 | if 'treemanifest' in repo.requirements: |
|
893 | 894 | # Versions 01 and 02 support only flat manifests and it's just too |
|
894 | 895 | # expensive to convert between the flat manifest and tree manifest on |
|
895 | 896 | # the fly. Since tree manifests are hashed differently, all of history |
|
896 | 897 | # would have to be converted. Instead, we simply don't even pretend to |
|
897 | 898 | # support versions 01 and 02. |
|
898 | 899 | versions.discard('01') |
|
899 | 900 | versions.discard('02') |
|
900 | 901 | return versions |
|
901 | 902 | |
|
902 | 903 | def safeversion(repo): |
|
903 | 904 | # Finds the smallest version that it's safe to assume clients of the repo |
|
904 | 905 | # will support. For example, all hg versions that support generaldelta also |
|
905 | 906 | # support changegroup 02. |
|
906 | 907 | versions = supportedoutgoingversions(repo) |
|
907 | 908 | if 'generaldelta' in repo.requirements: |
|
908 | 909 | versions.discard('01') |
|
909 | 910 | assert versions |
|
910 | 911 | return min(versions) |
|
911 | 912 | |
|
912 | 913 | def getbundler(version, repo, bundlecaps=None): |
|
913 | 914 | assert version in supportedoutgoingversions(repo) |
|
914 | 915 | return _packermap[version][0](repo, bundlecaps) |
|
915 | 916 | |
|
916 | 917 | def getunbundler(version, fh, alg, extras=None): |
|
917 | 918 | return _packermap[version][1](fh, alg, extras=extras) |
|
918 | 919 | |
|
919 | 920 | def _changegroupinfo(repo, nodes, source): |
|
920 | 921 | if repo.ui.verbose or source == 'bundle': |
|
921 | 922 | repo.ui.status(_("%d changesets found\n") % len(nodes)) |
|
922 | 923 | if repo.ui.debugflag: |
|
923 | 924 | repo.ui.debug("list of changesets:\n") |
|
924 | 925 | for node in nodes: |
|
925 | 926 | repo.ui.debug("%s\n" % hex(node)) |
|
926 | 927 | |
|
927 | 928 | def getsubsetraw(repo, outgoing, bundler, source, fastpath=False): |
|
928 | 929 | repo = repo.unfiltered() |
|
929 | 930 | commonrevs = outgoing.common |
|
930 | 931 | csets = outgoing.missing |
|
931 | 932 | heads = outgoing.missingheads |
|
932 | 933 | # We go through the fast path if we get told to, or if all (unfiltered |
|
933 | 934 | # heads have been requested (since we then know there all linkrevs will |
|
934 | 935 | # be pulled by the client). |
|
935 | 936 | heads.sort() |
|
936 | 937 | fastpathlinkrev = fastpath or ( |
|
937 | 938 | repo.filtername is None and heads == sorted(repo.heads())) |
|
938 | 939 | |
|
939 | 940 | repo.hook('preoutgoing', throw=True, source=source) |
|
940 | 941 | _changegroupinfo(repo, csets, source) |
|
941 | 942 | return bundler.generate(commonrevs, csets, fastpathlinkrev, source) |
|
942 | 943 | |
|
943 | 944 | def getsubset(repo, outgoing, bundler, source, fastpath=False): |
|
944 | 945 | gengroup = getsubsetraw(repo, outgoing, bundler, source, fastpath) |
|
945 | 946 | return getunbundler(bundler.version, util.chunkbuffer(gengroup), None, |
|
946 | 947 | {'clcount': len(outgoing.missing)}) |
|
947 | 948 | |
|
948 | 949 | def changegroupsubset(repo, roots, heads, source, version='01'): |
|
949 | 950 | """Compute a changegroup consisting of all the nodes that are |
|
950 | 951 | descendants of any of the roots and ancestors of any of the heads. |
|
951 | 952 | Return a chunkbuffer object whose read() method will return |
|
952 | 953 | successive changegroup chunks. |
|
953 | 954 | |
|
954 | 955 | It is fairly complex as determining which filenodes and which |
|
955 | 956 | manifest nodes need to be included for the changeset to be complete |
|
956 | 957 | is non-trivial. |
|
957 | 958 | |
|
958 | 959 | Another wrinkle is doing the reverse, figuring out which changeset in |
|
959 | 960 | the changegroup a particular filenode or manifestnode belongs to. |
|
960 | 961 | """ |
|
961 | 962 | outgoing = discovery.outgoing(repo, missingroots=roots, missingheads=heads) |
|
962 | 963 | bundler = getbundler(version, repo) |
|
963 | 964 | return getsubset(repo, outgoing, bundler, source) |
|
964 | 965 | |
|
965 | 966 | def getlocalchangegroupraw(repo, source, outgoing, bundlecaps=None, |
|
966 | 967 | version='01'): |
|
967 | 968 | """Like getbundle, but taking a discovery.outgoing as an argument. |
|
968 | 969 | |
|
969 | 970 | This is only implemented for local repos and reuses potentially |
|
970 | 971 | precomputed sets in outgoing. Returns a raw changegroup generator.""" |
|
971 | 972 | if not outgoing.missing: |
|
972 | 973 | return None |
|
973 | 974 | bundler = getbundler(version, repo, bundlecaps) |
|
974 | 975 | return getsubsetraw(repo, outgoing, bundler, source) |
|
975 | 976 | |
|
976 | 977 | def getlocalchangegroup(repo, source, outgoing, bundlecaps=None, |
|
977 | 978 | version='01'): |
|
978 | 979 | """Like getbundle, but taking a discovery.outgoing as an argument. |
|
979 | 980 | |
|
980 | 981 | This is only implemented for local repos and reuses potentially |
|
981 | 982 | precomputed sets in outgoing.""" |
|
982 | 983 | if not outgoing.missing: |
|
983 | 984 | return None |
|
984 | 985 | bundler = getbundler(version, repo, bundlecaps) |
|
985 | 986 | return getsubset(repo, outgoing, bundler, source) |
|
986 | 987 | |
|
987 | 988 | def getchangegroup(repo, source, outgoing, bundlecaps=None, |
|
988 | 989 | version='01'): |
|
989 | 990 | """Like changegroupsubset, but returns the set difference between the |
|
990 | 991 | ancestors of heads and the ancestors common. |
|
991 | 992 | |
|
992 | 993 | If heads is None, use the local heads. If common is None, use [nullid]. |
|
993 | 994 | |
|
994 | 995 | The nodes in common might not all be known locally due to the way the |
|
995 | 996 | current discovery protocol works. |
|
996 | 997 | """ |
|
997 | 998 | return getlocalchangegroup(repo, source, outgoing, bundlecaps=bundlecaps, |
|
998 | 999 | version=version) |
|
999 | 1000 | |
|
1000 | 1001 | def changegroup(repo, basenodes, source): |
|
1001 | 1002 | # to avoid a race we use changegroupsubset() (issue1320) |
|
1002 | 1003 | return changegroupsubset(repo, basenodes, repo.heads(), source) |
|
1003 | 1004 | |
|
1004 | 1005 | def _addchangegroupfiles(repo, source, revmap, trp, expectedfiles, needfiles): |
|
1005 | 1006 | revisions = 0 |
|
1006 | 1007 | files = 0 |
|
1007 | 1008 | for chunkdata in iter(source.filelogheader, {}): |
|
1008 | 1009 | files += 1 |
|
1009 | 1010 | f = chunkdata["filename"] |
|
1010 | 1011 | repo.ui.debug("adding %s revisions\n" % f) |
|
1011 | 1012 | repo.ui.progress(_('files'), files, unit=_('files'), |
|
1012 | 1013 | total=expectedfiles) |
|
1013 | 1014 | fl = repo.file(f) |
|
1014 | 1015 | o = len(fl) |
|
1015 | 1016 | try: |
|
1016 | 1017 | if not fl.addgroup(source, revmap, trp): |
|
1017 | 1018 | raise error.Abort(_("received file revlog group is empty")) |
|
1018 | 1019 | except error.CensoredBaseError as e: |
|
1019 | 1020 | raise error.Abort(_("received delta base is censored: %s") % e) |
|
1020 | 1021 | revisions += len(fl) - o |
|
1021 | 1022 | if f in needfiles: |
|
1022 | 1023 | needs = needfiles[f] |
|
1023 | 1024 | for new in xrange(o, len(fl)): |
|
1024 | 1025 | n = fl.node(new) |
|
1025 | 1026 | if n in needs: |
|
1026 | 1027 | needs.remove(n) |
|
1027 | 1028 | else: |
|
1028 | 1029 | raise error.Abort( |
|
1029 | 1030 | _("received spurious file revlog entry")) |
|
1030 | 1031 | if not needs: |
|
1031 | 1032 | del needfiles[f] |
|
1032 | 1033 | repo.ui.progress(_('files'), None) |
|
1033 | 1034 | |
|
1034 | 1035 | for f, needs in needfiles.iteritems(): |
|
1035 | 1036 | fl = repo.file(f) |
|
1036 | 1037 | for n in needs: |
|
1037 | 1038 | try: |
|
1038 | 1039 | fl.rev(n) |
|
1039 | 1040 | except error.LookupError: |
|
1040 | 1041 | raise error.Abort( |
|
1041 | 1042 | _('missing file data for %s:%s - run hg verify') % |
|
1042 | 1043 | (f, hex(n))) |
|
1043 | 1044 | |
|
1044 | 1045 | return revisions, files |
@@ -1,365 +1,365 b'' | |||
|
1 | 1 | # osutil.py - pure Python version of osutil.c |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import ctypes |
|
11 | 11 | import ctypes.util |
|
12 | 12 | import os |
|
13 | 13 | import socket |
|
14 | 14 | import stat as statmod |
|
15 | 15 | |
|
16 | 16 | from . import ( |
|
17 | 17 | policy, |
|
18 | 18 | pycompat, |
|
19 | 19 | ) |
|
20 | 20 | |
|
21 | 21 | modulepolicy = policy.policy |
|
22 | 22 | policynocffi = policy.policynocffi |
|
23 | 23 | |
|
24 | 24 | def _mode_to_kind(mode): |
|
25 | 25 | if statmod.S_ISREG(mode): |
|
26 | 26 | return statmod.S_IFREG |
|
27 | 27 | if statmod.S_ISDIR(mode): |
|
28 | 28 | return statmod.S_IFDIR |
|
29 | 29 | if statmod.S_ISLNK(mode): |
|
30 | 30 | return statmod.S_IFLNK |
|
31 | 31 | if statmod.S_ISBLK(mode): |
|
32 | 32 | return statmod.S_IFBLK |
|
33 | 33 | if statmod.S_ISCHR(mode): |
|
34 | 34 | return statmod.S_IFCHR |
|
35 | 35 | if statmod.S_ISFIFO(mode): |
|
36 | 36 | return statmod.S_IFIFO |
|
37 | 37 | if statmod.S_ISSOCK(mode): |
|
38 | 38 | return statmod.S_IFSOCK |
|
39 | 39 | return mode |
|
40 | 40 | |
|
41 | 41 | def listdirpure(path, stat=False, skip=None): |
|
42 | 42 | '''listdir(path, stat=False) -> list_of_tuples |
|
43 | 43 | |
|
44 | 44 | Return a sorted list containing information about the entries |
|
45 | 45 | in the directory. |
|
46 | 46 | |
|
47 | 47 | If stat is True, each element is a 3-tuple: |
|
48 | 48 | |
|
49 | 49 | (name, type, stat object) |
|
50 | 50 | |
|
51 | 51 | Otherwise, each element is a 2-tuple: |
|
52 | 52 | |
|
53 | 53 | (name, type) |
|
54 | 54 | ''' |
|
55 | 55 | result = [] |
|
56 | 56 | prefix = path |
|
57 | 57 | if not prefix.endswith(pycompat.ossep): |
|
58 | 58 | prefix += pycompat.ossep |
|
59 | 59 | names = os.listdir(path) |
|
60 | 60 | names.sort() |
|
61 | 61 | for fn in names: |
|
62 | 62 | st = os.lstat(prefix + fn) |
|
63 | 63 | if fn == skip and statmod.S_ISDIR(st.st_mode): |
|
64 | 64 | return [] |
|
65 | 65 | if stat: |
|
66 | 66 | result.append((fn, _mode_to_kind(st.st_mode), st)) |
|
67 | 67 | else: |
|
68 | 68 | result.append((fn, _mode_to_kind(st.st_mode))) |
|
69 | 69 | return result |
|
70 | 70 | |
|
71 | 71 | ffi = None |
|
72 | 72 | if modulepolicy not in policynocffi and pycompat.sysplatform == 'darwin': |
|
73 | 73 | try: |
|
74 | 74 | from _osutil_cffi import ffi, lib |
|
75 | 75 | except ImportError: |
|
76 | 76 | if modulepolicy == 'cffi': # strict cffi import |
|
77 | 77 | raise |
|
78 | 78 | |
|
79 | 79 | if pycompat.sysplatform == 'darwin' and ffi is not None: |
|
80 | 80 | listdir_batch_size = 4096 |
|
81 | 81 | # tweakable number, only affects performance, which chunks |
|
82 | 82 | # of bytes do we get back from getattrlistbulk |
|
83 | 83 | |
|
84 | 84 | attrkinds = [None] * 20 # we need the max no for enum VXXX, 20 is plenty |
|
85 | 85 | |
|
86 | 86 | attrkinds[lib.VREG] = statmod.S_IFREG |
|
87 | 87 | attrkinds[lib.VDIR] = statmod.S_IFDIR |
|
88 | 88 | attrkinds[lib.VLNK] = statmod.S_IFLNK |
|
89 | 89 | attrkinds[lib.VBLK] = statmod.S_IFBLK |
|
90 | 90 | attrkinds[lib.VCHR] = statmod.S_IFCHR |
|
91 | 91 | attrkinds[lib.VFIFO] = statmod.S_IFIFO |
|
92 | 92 | attrkinds[lib.VSOCK] = statmod.S_IFSOCK |
|
93 | 93 | |
|
94 | 94 | class stat_res(object): |
|
95 | 95 | def __init__(self, st_mode, st_mtime, st_size): |
|
96 | 96 | self.st_mode = st_mode |
|
97 | 97 | self.st_mtime = st_mtime |
|
98 | 98 | self.st_size = st_size |
|
99 | 99 | |
|
100 | 100 | tv_sec_ofs = ffi.offsetof("struct timespec", "tv_sec") |
|
101 | 101 | buf = ffi.new("char[]", listdir_batch_size) |
|
102 | 102 | |
|
103 | 103 | def listdirinternal(dfd, req, stat, skip): |
|
104 | 104 | ret = [] |
|
105 | 105 | while True: |
|
106 | 106 | r = lib.getattrlistbulk(dfd, req, buf, listdir_batch_size, 0) |
|
107 | 107 | if r == 0: |
|
108 | 108 | break |
|
109 | 109 | if r == -1: |
|
110 | 110 | raise OSError(ffi.errno, os.strerror(ffi.errno)) |
|
111 | 111 | cur = ffi.cast("val_attrs_t*", buf) |
|
112 | 112 | for i in range(r): |
|
113 | 113 | lgt = cur.length |
|
114 | 114 | assert lgt == ffi.cast('uint32_t*', cur)[0] |
|
115 | 115 | ofs = cur.name_info.attr_dataoffset |
|
116 | 116 | str_lgt = cur.name_info.attr_length |
|
117 | 117 | base_ofs = ffi.offsetof('val_attrs_t', 'name_info') |
|
118 | 118 | name = str(ffi.buffer(ffi.cast("char*", cur) + base_ofs + ofs, |
|
119 | 119 | str_lgt - 1)) |
|
120 | 120 | tp = attrkinds[cur.obj_type] |
|
121 | 121 | if name == "." or name == "..": |
|
122 | 122 | continue |
|
123 | 123 | if skip == name and tp == statmod.S_ISDIR: |
|
124 | 124 | return [] |
|
125 | 125 | if stat: |
|
126 | 126 | mtime = cur.mtime.tv_sec |
|
127 | 127 | mode = (cur.accessmask & ~lib.S_IFMT)| tp |
|
128 | 128 | ret.append((name, tp, stat_res(st_mode=mode, st_mtime=mtime, |
|
129 | 129 | st_size=cur.datalength))) |
|
130 | 130 | else: |
|
131 | 131 | ret.append((name, tp)) |
|
132 | 132 | cur = ffi.cast("val_attrs_t*", int(ffi.cast("intptr_t", cur)) |
|
133 | 133 | + lgt) |
|
134 | 134 | return ret |
|
135 | 135 | |
|
136 | 136 | def listdir(path, stat=False, skip=None): |
|
137 | 137 | req = ffi.new("struct attrlist*") |
|
138 | 138 | req.bitmapcount = lib.ATTR_BIT_MAP_COUNT |
|
139 | 139 | req.commonattr = (lib.ATTR_CMN_RETURNED_ATTRS | |
|
140 | 140 | lib.ATTR_CMN_NAME | |
|
141 | 141 | lib.ATTR_CMN_OBJTYPE | |
|
142 | 142 | lib.ATTR_CMN_ACCESSMASK | |
|
143 | 143 | lib.ATTR_CMN_MODTIME) |
|
144 | 144 | req.fileattr = lib.ATTR_FILE_DATALENGTH |
|
145 | 145 | dfd = lib.open(path, lib.O_RDONLY, 0) |
|
146 | 146 | if dfd == -1: |
|
147 | 147 | raise OSError(ffi.errno, os.strerror(ffi.errno)) |
|
148 | 148 | |
|
149 | 149 | try: |
|
150 | 150 | ret = listdirinternal(dfd, req, stat, skip) |
|
151 | 151 | finally: |
|
152 | 152 | try: |
|
153 | 153 | lib.close(dfd) |
|
154 | 154 | except BaseException: |
|
155 | 155 | pass # we ignore all the errors from closing, not |
|
156 | 156 | # much we can do about that |
|
157 | 157 | return ret |
|
158 | 158 | else: |
|
159 | 159 | listdir = listdirpure |
|
160 | 160 | |
|
161 | 161 | if pycompat.osname != 'nt': |
|
162 | 162 | posixfile = open |
|
163 | 163 | |
|
164 | 164 | _SCM_RIGHTS = 0x01 |
|
165 | 165 | _socklen_t = ctypes.c_uint |
|
166 | 166 | |
|
167 | 167 | if pycompat.sysplatform.startswith('linux'): |
|
168 | 168 | # socket.h says "the type should be socklen_t but the definition of |
|
169 | 169 | # the kernel is incompatible with this." |
|
170 | 170 | _cmsg_len_t = ctypes.c_size_t |
|
171 | 171 | _msg_controllen_t = ctypes.c_size_t |
|
172 | 172 | _msg_iovlen_t = ctypes.c_size_t |
|
173 | 173 | else: |
|
174 | 174 | _cmsg_len_t = _socklen_t |
|
175 | 175 | _msg_controllen_t = _socklen_t |
|
176 | 176 | _msg_iovlen_t = ctypes.c_int |
|
177 | 177 | |
|
178 | 178 | class _iovec(ctypes.Structure): |
|
179 | 179 | _fields_ = [ |
|
180 | 180 | (u'iov_base', ctypes.c_void_p), |
|
181 | 181 | (u'iov_len', ctypes.c_size_t), |
|
182 | 182 | ] |
|
183 | 183 | |
|
184 | 184 | class _msghdr(ctypes.Structure): |
|
185 | 185 | _fields_ = [ |
|
186 | 186 | (u'msg_name', ctypes.c_void_p), |
|
187 | 187 | (u'msg_namelen', _socklen_t), |
|
188 | 188 | (u'msg_iov', ctypes.POINTER(_iovec)), |
|
189 | 189 | (u'msg_iovlen', _msg_iovlen_t), |
|
190 | 190 | (u'msg_control', ctypes.c_void_p), |
|
191 | 191 | (u'msg_controllen', _msg_controllen_t), |
|
192 | 192 | (u'msg_flags', ctypes.c_int), |
|
193 | 193 | ] |
|
194 | 194 | |
|
195 | 195 | class _cmsghdr(ctypes.Structure): |
|
196 | 196 | _fields_ = [ |
|
197 | 197 | (u'cmsg_len', _cmsg_len_t), |
|
198 | 198 | (u'cmsg_level', ctypes.c_int), |
|
199 | 199 | (u'cmsg_type', ctypes.c_int), |
|
200 | 200 | (u'cmsg_data', ctypes.c_ubyte * 0), |
|
201 | 201 | ] |
|
202 | 202 | |
|
203 | 203 | _libc = ctypes.CDLL(ctypes.util.find_library(u'c'), use_errno=True) |
|
204 | 204 | _recvmsg = getattr(_libc, 'recvmsg', None) |
|
205 | 205 | if _recvmsg: |
|
206 | 206 | _recvmsg.restype = getattr(ctypes, 'c_ssize_t', ctypes.c_long) |
|
207 | 207 | _recvmsg.argtypes = (ctypes.c_int, ctypes.POINTER(_msghdr), |
|
208 | 208 | ctypes.c_int) |
|
209 | 209 | else: |
|
210 | 210 | # recvmsg isn't always provided by libc; such systems are unsupported |
|
211 | 211 | def _recvmsg(sockfd, msg, flags): |
|
212 | 212 | raise NotImplementedError('unsupported platform') |
|
213 | 213 | |
|
214 | 214 | def _CMSG_FIRSTHDR(msgh): |
|
215 | 215 | if msgh.msg_controllen < ctypes.sizeof(_cmsghdr): |
|
216 | 216 | return |
|
217 | 217 | cmsgptr = ctypes.cast(msgh.msg_control, ctypes.POINTER(_cmsghdr)) |
|
218 | 218 | return cmsgptr.contents |
|
219 | 219 | |
|
220 | 220 | # The pure version is less portable than the native version because the |
|
221 | 221 | # handling of socket ancillary data heavily depends on C preprocessor. |
|
222 | 222 | # Also, some length fields are wrongly typed in Linux kernel. |
|
223 | 223 | def recvfds(sockfd): |
|
224 | 224 | """receive list of file descriptors via socket""" |
|
225 | 225 | dummy = (ctypes.c_ubyte * 1)() |
|
226 | 226 | iov = _iovec(ctypes.cast(dummy, ctypes.c_void_p), ctypes.sizeof(dummy)) |
|
227 | 227 | cbuf = ctypes.create_string_buffer(256) |
|
228 | 228 | msgh = _msghdr(None, 0, |
|
229 | 229 | ctypes.pointer(iov), 1, |
|
230 | 230 | ctypes.cast(cbuf, ctypes.c_void_p), ctypes.sizeof(cbuf), |
|
231 | 231 | 0) |
|
232 | 232 | r = _recvmsg(sockfd, ctypes.byref(msgh), 0) |
|
233 | 233 | if r < 0: |
|
234 | 234 | e = ctypes.get_errno() |
|
235 | 235 | raise OSError(e, os.strerror(e)) |
|
236 | 236 | # assumes that the first cmsg has fds because it isn't easy to write |
|
237 | 237 | # portable CMSG_NXTHDR() with ctypes. |
|
238 | 238 | cmsg = _CMSG_FIRSTHDR(msgh) |
|
239 | 239 | if not cmsg: |
|
240 | 240 | return [] |
|
241 | 241 | if (cmsg.cmsg_level != socket.SOL_SOCKET or |
|
242 | 242 | cmsg.cmsg_type != _SCM_RIGHTS): |
|
243 | 243 | return [] |
|
244 | 244 | rfds = ctypes.cast(cmsg.cmsg_data, ctypes.POINTER(ctypes.c_int)) |
|
245 | 245 | rfdscount = ((cmsg.cmsg_len - _cmsghdr.cmsg_data.offset) / |
|
246 | 246 | ctypes.sizeof(ctypes.c_int)) |
|
247 | 247 | return [rfds[i] for i in xrange(rfdscount)] |
|
248 | 248 | |
|
249 | 249 | else: |
|
250 | 250 | import msvcrt |
|
251 | 251 | |
|
252 | 252 | _kernel32 = ctypes.windll.kernel32 |
|
253 | 253 | |
|
254 | 254 | _DWORD = ctypes.c_ulong |
|
255 | 255 | _LPCSTR = _LPSTR = ctypes.c_char_p |
|
256 | 256 | _HANDLE = ctypes.c_void_p |
|
257 | 257 | |
|
258 | 258 | _INVALID_HANDLE_VALUE = _HANDLE(-1).value |
|
259 | 259 | |
|
260 | 260 | # CreateFile |
|
261 | 261 | _FILE_SHARE_READ = 0x00000001 |
|
262 | 262 | _FILE_SHARE_WRITE = 0x00000002 |
|
263 | 263 | _FILE_SHARE_DELETE = 0x00000004 |
|
264 | 264 | |
|
265 | 265 | _CREATE_ALWAYS = 2 |
|
266 | 266 | _OPEN_EXISTING = 3 |
|
267 | 267 | _OPEN_ALWAYS = 4 |
|
268 | 268 | |
|
269 | 269 | _GENERIC_READ = 0x80000000 |
|
270 | 270 | _GENERIC_WRITE = 0x40000000 |
|
271 | 271 | |
|
272 | 272 | _FILE_ATTRIBUTE_NORMAL = 0x80 |
|
273 | 273 | |
|
274 | 274 | # open_osfhandle flags |
|
275 | 275 | _O_RDONLY = 0x0000 |
|
276 | 276 | _O_RDWR = 0x0002 |
|
277 | 277 | _O_APPEND = 0x0008 |
|
278 | 278 | |
|
279 | 279 | _O_TEXT = 0x4000 |
|
280 | 280 | _O_BINARY = 0x8000 |
|
281 | 281 | |
|
282 | 282 | # types of parameters of C functions used (required by pypy) |
|
283 | 283 | |
|
284 | 284 | _kernel32.CreateFileA.argtypes = [_LPCSTR, _DWORD, _DWORD, ctypes.c_void_p, |
|
285 | 285 | _DWORD, _DWORD, _HANDLE] |
|
286 | 286 | _kernel32.CreateFileA.restype = _HANDLE |
|
287 | 287 | |
|
288 | 288 | def _raiseioerror(name): |
|
289 | 289 | err = ctypes.WinError() |
|
290 | 290 | raise IOError(err.errno, '%s: %s' % (name, err.strerror)) |
|
291 | 291 | |
|
292 | 292 | class posixfile(object): |
|
293 | 293 | '''a file object aiming for POSIX-like semantics |
|
294 | 294 | |
|
295 | 295 | CPython's open() returns a file that was opened *without* setting the |
|
296 | 296 | _FILE_SHARE_DELETE flag, which causes rename and unlink to abort. |
|
297 | 297 | This even happens if any hardlinked copy of the file is in open state. |
|
298 | 298 | We set _FILE_SHARE_DELETE here, so files opened with posixfile can be |
|
299 | 299 | renamed and deleted while they are held open. |
|
300 | 300 | Note that if a file opened with posixfile is unlinked, the file |
|
301 | 301 | remains but cannot be opened again or be recreated under the same name, |
|
302 | 302 | until all reading processes have closed the file.''' |
|
303 | 303 | |
|
304 | 304 | def __init__(self, name, mode='r', bufsize=-1): |
|
305 | 305 | if 'b' in mode: |
|
306 | 306 | flags = _O_BINARY |
|
307 | 307 | else: |
|
308 | 308 | flags = _O_TEXT |
|
309 | 309 | |
|
310 | 310 | m0 = mode[0] |
|
311 | 311 | if m0 == 'r' and '+' not in mode: |
|
312 | 312 | flags |= _O_RDONLY |
|
313 | 313 | access = _GENERIC_READ |
|
314 | 314 | else: |
|
315 | 315 | # work around http://support.microsoft.com/kb/899149 and |
|
316 | 316 | # set _O_RDWR for 'w' and 'a', even if mode has no '+' |
|
317 | 317 | flags |= _O_RDWR |
|
318 | 318 | access = _GENERIC_READ | _GENERIC_WRITE |
|
319 | 319 | |
|
320 | 320 | if m0 == 'r': |
|
321 | 321 | creation = _OPEN_EXISTING |
|
322 | 322 | elif m0 == 'w': |
|
323 | 323 | creation = _CREATE_ALWAYS |
|
324 | 324 | elif m0 == 'a': |
|
325 | 325 | creation = _OPEN_ALWAYS |
|
326 | 326 | flags |= _O_APPEND |
|
327 | 327 | else: |
|
328 | 328 | raise ValueError("invalid mode: %s" % mode) |
|
329 | 329 | |
|
330 | 330 | fh = _kernel32.CreateFileA(name, access, |
|
331 | 331 | _FILE_SHARE_READ | _FILE_SHARE_WRITE | _FILE_SHARE_DELETE, |
|
332 | 332 | None, creation, _FILE_ATTRIBUTE_NORMAL, None) |
|
333 | 333 | if fh == _INVALID_HANDLE_VALUE: |
|
334 | 334 | _raiseioerror(name) |
|
335 | 335 | |
|
336 | 336 | fd = msvcrt.open_osfhandle(fh, flags) |
|
337 | 337 | if fd == -1: |
|
338 | 338 | _kernel32.CloseHandle(fh) |
|
339 | 339 | _raiseioerror(name) |
|
340 | 340 | |
|
341 | f = os.fdopen(fd, mode, bufsize) | |
|
341 | f = os.fdopen(fd, pycompat.sysstr(mode), bufsize) | |
|
342 | 342 | # unfortunately, f.name is '<fdopen>' at this point -- so we store |
|
343 | 343 | # the name on this wrapper. We cannot just assign to f.name, |
|
344 | 344 | # because that attribute is read-only. |
|
345 | 345 | object.__setattr__(self, 'name', name) |
|
346 | 346 | object.__setattr__(self, '_file', f) |
|
347 | 347 | |
|
348 | 348 | def __iter__(self): |
|
349 | 349 | return self._file |
|
350 | 350 | |
|
351 | 351 | def __getattr__(self, name): |
|
352 | 352 | return getattr(self._file, name) |
|
353 | 353 | |
|
354 | 354 | def __setattr__(self, name, value): |
|
355 | 355 | '''mimics the read-only attributes of Python file objects |
|
356 | 356 | by raising 'TypeError: readonly attribute' if someone tries: |
|
357 | 357 | f = posixfile('foo.txt') |
|
358 | 358 | f.name = 'bla' ''' |
|
359 | 359 | return self._file.__setattr__(name, value) |
|
360 | 360 | |
|
361 | 361 | def __enter__(self): |
|
362 | 362 | return self._file.__enter__() |
|
363 | 363 | |
|
364 | 364 | def __exit__(self, exc_type, exc_value, exc_tb): |
|
365 | 365 | return self._file.__exit__(exc_type, exc_value, exc_tb) |
@@ -1,1431 +1,1431 b'' | |||
|
1 | 1 | # ui.py - user interface bits for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import contextlib |
|
11 | 11 | import errno |
|
12 | 12 | import getpass |
|
13 | 13 | import inspect |
|
14 | 14 | import os |
|
15 | 15 | import re |
|
16 | 16 | import socket |
|
17 | 17 | import sys |
|
18 | 18 | import tempfile |
|
19 | 19 | import traceback |
|
20 | 20 | |
|
21 | 21 | from .i18n import _ |
|
22 | 22 | from .node import hex |
|
23 | 23 | |
|
24 | 24 | from . import ( |
|
25 | 25 | config, |
|
26 | 26 | encoding, |
|
27 | 27 | error, |
|
28 | 28 | formatter, |
|
29 | 29 | progress, |
|
30 | 30 | pycompat, |
|
31 | 31 | scmutil, |
|
32 | 32 | util, |
|
33 | 33 | ) |
|
34 | 34 | |
|
35 | 35 | urlreq = util.urlreq |
|
36 | 36 | |
|
37 | 37 | samplehgrcs = { |
|
38 | 38 | 'user': |
|
39 | 39 | """# example user config (see 'hg help config' for more info) |
|
40 | 40 | [ui] |
|
41 | 41 | # name and email, e.g. |
|
42 | 42 | # username = Jane Doe <jdoe@example.com> |
|
43 | 43 | username = |
|
44 | 44 | |
|
45 | 45 | [extensions] |
|
46 | 46 | # uncomment these lines to enable some popular extensions |
|
47 | 47 | # (see 'hg help extensions' for more info) |
|
48 | 48 | # |
|
49 | 49 | # pager = |
|
50 | 50 | # color =""", |
|
51 | 51 | |
|
52 | 52 | 'cloned': |
|
53 | 53 | """# example repository config (see 'hg help config' for more info) |
|
54 | 54 | [paths] |
|
55 | 55 | default = %s |
|
56 | 56 | |
|
57 | 57 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
58 | 58 | # (see 'hg help config.paths' for more info) |
|
59 | 59 | # |
|
60 | 60 | # default-push = ssh://jdoe@example.net/hg/jdoes-fork |
|
61 | 61 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
62 | 62 | # my-clone = /home/jdoe/jdoes-clone |
|
63 | 63 | |
|
64 | 64 | [ui] |
|
65 | 65 | # name and email (local to this repository, optional), e.g. |
|
66 | 66 | # username = Jane Doe <jdoe@example.com> |
|
67 | 67 | """, |
|
68 | 68 | |
|
69 | 69 | 'local': |
|
70 | 70 | """# example repository config (see 'hg help config' for more info) |
|
71 | 71 | [paths] |
|
72 | 72 | # path aliases to other clones of this repo in URLs or filesystem paths |
|
73 | 73 | # (see 'hg help config.paths' for more info) |
|
74 | 74 | # |
|
75 | 75 | # default = http://example.com/hg/example-repo |
|
76 | 76 | # default-push = ssh://jdoe@example.net/hg/jdoes-fork |
|
77 | 77 | # my-fork = ssh://jdoe@example.net/hg/jdoes-fork |
|
78 | 78 | # my-clone = /home/jdoe/jdoes-clone |
|
79 | 79 | |
|
80 | 80 | [ui] |
|
81 | 81 | # name and email (local to this repository, optional), e.g. |
|
82 | 82 | # username = Jane Doe <jdoe@example.com> |
|
83 | 83 | """, |
|
84 | 84 | |
|
85 | 85 | 'global': |
|
86 | 86 | """# example system-wide hg config (see 'hg help config' for more info) |
|
87 | 87 | |
|
88 | 88 | [extensions] |
|
89 | 89 | # uncomment these lines to enable some popular extensions |
|
90 | 90 | # (see 'hg help extensions' for more info) |
|
91 | 91 | # |
|
92 | 92 | # blackbox = |
|
93 | 93 | # color = |
|
94 | 94 | # pager =""", |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | class ui(object): |
|
98 | 98 | def __init__(self, src=None): |
|
99 | 99 | """Create a fresh new ui object if no src given |
|
100 | 100 | |
|
101 | 101 | Use uimod.ui.load() to create a ui which knows global and user configs. |
|
102 | 102 | In most cases, you should use ui.copy() to create a copy of an existing |
|
103 | 103 | ui object. |
|
104 | 104 | """ |
|
105 | 105 | # _buffers: used for temporary capture of output |
|
106 | 106 | self._buffers = [] |
|
107 | 107 | # 3-tuple describing how each buffer in the stack behaves. |
|
108 | 108 | # Values are (capture stderr, capture subprocesses, apply labels). |
|
109 | 109 | self._bufferstates = [] |
|
110 | 110 | # When a buffer is active, defines whether we are expanding labels. |
|
111 | 111 | # This exists to prevent an extra list lookup. |
|
112 | 112 | self._bufferapplylabels = None |
|
113 | 113 | self.quiet = self.verbose = self.debugflag = self.tracebackflag = False |
|
114 | 114 | self._reportuntrusted = True |
|
115 | 115 | self._ocfg = config.config() # overlay |
|
116 | 116 | self._tcfg = config.config() # trusted |
|
117 | 117 | self._ucfg = config.config() # untrusted |
|
118 | 118 | self._trustusers = set() |
|
119 | 119 | self._trustgroups = set() |
|
120 | 120 | self.callhooks = True |
|
121 | 121 | # Insecure server connections requested. |
|
122 | 122 | self.insecureconnections = False |
|
123 | 123 | |
|
124 | 124 | if src: |
|
125 | 125 | self.fout = src.fout |
|
126 | 126 | self.ferr = src.ferr |
|
127 | 127 | self.fin = src.fin |
|
128 | 128 | |
|
129 | 129 | self._tcfg = src._tcfg.copy() |
|
130 | 130 | self._ucfg = src._ucfg.copy() |
|
131 | 131 | self._ocfg = src._ocfg.copy() |
|
132 | 132 | self._trustusers = src._trustusers.copy() |
|
133 | 133 | self._trustgroups = src._trustgroups.copy() |
|
134 | 134 | self.environ = src.environ |
|
135 | 135 | self.callhooks = src.callhooks |
|
136 | 136 | self.insecureconnections = src.insecureconnections |
|
137 | 137 | self.fixconfig() |
|
138 | 138 | |
|
139 | 139 | self.httppasswordmgrdb = src.httppasswordmgrdb |
|
140 | 140 | else: |
|
141 | 141 | self.fout = util.stdout |
|
142 | 142 | self.ferr = util.stderr |
|
143 | 143 | self.fin = util.stdin |
|
144 | 144 | |
|
145 | 145 | # shared read-only environment |
|
146 | 146 | self.environ = encoding.environ |
|
147 | 147 | |
|
148 | 148 | self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm() |
|
149 | 149 | |
|
150 | 150 | allowed = self.configlist('experimental', 'exportableenviron') |
|
151 | 151 | if '*' in allowed: |
|
152 | 152 | self._exportableenviron = self.environ |
|
153 | 153 | else: |
|
154 | 154 | self._exportableenviron = {} |
|
155 | 155 | for k in allowed: |
|
156 | 156 | if k in self.environ: |
|
157 | 157 | self._exportableenviron[k] = self.environ[k] |
|
158 | 158 | |
|
159 | 159 | @classmethod |
|
160 | 160 | def load(cls): |
|
161 | 161 | """Create a ui and load global and user configs""" |
|
162 | 162 | u = cls() |
|
163 | 163 | # we always trust global config files |
|
164 | 164 | for f in scmutil.rcpath(): |
|
165 | 165 | u.readconfig(f, trust=True) |
|
166 | 166 | return u |
|
167 | 167 | |
|
168 | 168 | def copy(self): |
|
169 | 169 | return self.__class__(self) |
|
170 | 170 | |
|
171 | 171 | def resetstate(self): |
|
172 | 172 | """Clear internal state that shouldn't persist across commands""" |
|
173 | 173 | if self._progbar: |
|
174 | 174 | self._progbar.resetstate() # reset last-print time of progress bar |
|
175 | 175 | self.httppasswordmgrdb = urlreq.httppasswordmgrwithdefaultrealm() |
|
176 | 176 | |
|
177 | 177 | def formatter(self, topic, opts): |
|
178 | 178 | return formatter.formatter(self, topic, opts) |
|
179 | 179 | |
|
180 | 180 | def _trusted(self, fp, f): |
|
181 | 181 | st = util.fstat(fp) |
|
182 | 182 | if util.isowner(st): |
|
183 | 183 | return True |
|
184 | 184 | |
|
185 | 185 | tusers, tgroups = self._trustusers, self._trustgroups |
|
186 | 186 | if '*' in tusers or '*' in tgroups: |
|
187 | 187 | return True |
|
188 | 188 | |
|
189 | 189 | user = util.username(st.st_uid) |
|
190 | 190 | group = util.groupname(st.st_gid) |
|
191 | 191 | if user in tusers or group in tgroups or user == util.username(): |
|
192 | 192 | return True |
|
193 | 193 | |
|
194 | 194 | if self._reportuntrusted: |
|
195 | 195 | self.warn(_('not trusting file %s from untrusted ' |
|
196 | 196 | 'user %s, group %s\n') % (f, user, group)) |
|
197 | 197 | return False |
|
198 | 198 | |
|
199 | 199 | def readconfig(self, filename, root=None, trust=False, |
|
200 | 200 | sections=None, remap=None): |
|
201 | 201 | try: |
|
202 | 202 | fp = open(filename, u'rb') |
|
203 | 203 | except IOError: |
|
204 | 204 | if not sections: # ignore unless we were looking for something |
|
205 | 205 | return |
|
206 | 206 | raise |
|
207 | 207 | |
|
208 | 208 | cfg = config.config() |
|
209 | 209 | trusted = sections or trust or self._trusted(fp, filename) |
|
210 | 210 | |
|
211 | 211 | try: |
|
212 | 212 | cfg.read(filename, fp, sections=sections, remap=remap) |
|
213 | 213 | fp.close() |
|
214 | 214 | except error.ConfigError as inst: |
|
215 | 215 | if trusted: |
|
216 | 216 | raise |
|
217 | 217 | self.warn(_("ignored: %s\n") % str(inst)) |
|
218 | 218 | |
|
219 | 219 | if self.plain(): |
|
220 | 220 | for k in ('debug', 'fallbackencoding', 'quiet', 'slash', |
|
221 | 221 | 'logtemplate', 'statuscopies', 'style', |
|
222 | 222 | 'traceback', 'verbose'): |
|
223 | 223 | if k in cfg['ui']: |
|
224 | 224 | del cfg['ui'][k] |
|
225 | 225 | for k, v in cfg.items('defaults'): |
|
226 | 226 | del cfg['defaults'][k] |
|
227 | 227 | # Don't remove aliases from the configuration if in the exceptionlist |
|
228 | 228 | if self.plain('alias'): |
|
229 | 229 | for k, v in cfg.items('alias'): |
|
230 | 230 | del cfg['alias'][k] |
|
231 | 231 | if self.plain('revsetalias'): |
|
232 | 232 | for k, v in cfg.items('revsetalias'): |
|
233 | 233 | del cfg['revsetalias'][k] |
|
234 | 234 | if self.plain('templatealias'): |
|
235 | 235 | for k, v in cfg.items('templatealias'): |
|
236 | 236 | del cfg['templatealias'][k] |
|
237 | 237 | |
|
238 | 238 | if trusted: |
|
239 | 239 | self._tcfg.update(cfg) |
|
240 | 240 | self._tcfg.update(self._ocfg) |
|
241 | 241 | self._ucfg.update(cfg) |
|
242 | 242 | self._ucfg.update(self._ocfg) |
|
243 | 243 | |
|
244 | 244 | if root is None: |
|
245 | 245 | root = os.path.expanduser('~') |
|
246 | 246 | self.fixconfig(root=root) |
|
247 | 247 | |
|
248 | 248 | def fixconfig(self, root=None, section=None): |
|
249 | 249 | if section in (None, 'paths'): |
|
250 | 250 | # expand vars and ~ |
|
251 | 251 | # translate paths relative to root (or home) into absolute paths |
|
252 | 252 | root = root or pycompat.getcwd() |
|
253 | 253 | for c in self._tcfg, self._ucfg, self._ocfg: |
|
254 | 254 | for n, p in c.items('paths'): |
|
255 | 255 | # Ignore sub-options. |
|
256 | 256 | if ':' in n: |
|
257 | 257 | continue |
|
258 | 258 | if not p: |
|
259 | 259 | continue |
|
260 | 260 | if '%%' in p: |
|
261 | 261 | s = self.configsource('paths', n) or 'none' |
|
262 | 262 | self.warn(_("(deprecated '%%' in path %s=%s from %s)\n") |
|
263 | 263 | % (n, p, s)) |
|
264 | 264 | p = p.replace('%%', '%') |
|
265 | 265 | p = util.expandpath(p) |
|
266 | 266 | if not util.hasscheme(p) and not os.path.isabs(p): |
|
267 | 267 | p = os.path.normpath(os.path.join(root, p)) |
|
268 | 268 | c.set("paths", n, p) |
|
269 | 269 | |
|
270 | 270 | if section in (None, 'ui'): |
|
271 | 271 | # update ui options |
|
272 | 272 | self.debugflag = self.configbool('ui', 'debug') |
|
273 | 273 | self.verbose = self.debugflag or self.configbool('ui', 'verbose') |
|
274 | 274 | self.quiet = not self.debugflag and self.configbool('ui', 'quiet') |
|
275 | 275 | if self.verbose and self.quiet: |
|
276 | 276 | self.quiet = self.verbose = False |
|
277 | 277 | self._reportuntrusted = self.debugflag or self.configbool("ui", |
|
278 | 278 | "report_untrusted", True) |
|
279 | 279 | self.tracebackflag = self.configbool('ui', 'traceback', False) |
|
280 | 280 | |
|
281 | 281 | if section in (None, 'trusted'): |
|
282 | 282 | # update trust information |
|
283 | 283 | self._trustusers.update(self.configlist('trusted', 'users')) |
|
284 | 284 | self._trustgroups.update(self.configlist('trusted', 'groups')) |
|
285 | 285 | |
|
286 | 286 | def backupconfig(self, section, item): |
|
287 | 287 | return (self._ocfg.backup(section, item), |
|
288 | 288 | self._tcfg.backup(section, item), |
|
289 | 289 | self._ucfg.backup(section, item),) |
|
290 | 290 | def restoreconfig(self, data): |
|
291 | 291 | self._ocfg.restore(data[0]) |
|
292 | 292 | self._tcfg.restore(data[1]) |
|
293 | 293 | self._ucfg.restore(data[2]) |
|
294 | 294 | |
|
295 | 295 | def setconfig(self, section, name, value, source=''): |
|
296 | 296 | for cfg in (self._ocfg, self._tcfg, self._ucfg): |
|
297 | 297 | cfg.set(section, name, value, source) |
|
298 | 298 | self.fixconfig(section=section) |
|
299 | 299 | |
|
300 | 300 | def _data(self, untrusted): |
|
301 | 301 | return untrusted and self._ucfg or self._tcfg |
|
302 | 302 | |
|
303 | 303 | def configsource(self, section, name, untrusted=False): |
|
304 | 304 | return self._data(untrusted).source(section, name) |
|
305 | 305 | |
|
306 | 306 | def config(self, section, name, default=None, untrusted=False): |
|
307 | 307 | if isinstance(name, list): |
|
308 | 308 | alternates = name |
|
309 | 309 | else: |
|
310 | 310 | alternates = [name] |
|
311 | 311 | |
|
312 | 312 | for n in alternates: |
|
313 | 313 | value = self._data(untrusted).get(section, n, None) |
|
314 | 314 | if value is not None: |
|
315 | 315 | name = n |
|
316 | 316 | break |
|
317 | 317 | else: |
|
318 | 318 | value = default |
|
319 | 319 | |
|
320 | 320 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
321 | 321 | for n in alternates: |
|
322 | 322 | uvalue = self._ucfg.get(section, n) |
|
323 | 323 | if uvalue is not None and uvalue != value: |
|
324 | 324 | self.debug("ignoring untrusted configuration option " |
|
325 | 325 | "%s.%s = %s\n" % (section, n, uvalue)) |
|
326 | 326 | return value |
|
327 | 327 | |
|
328 | 328 | def configsuboptions(self, section, name, default=None, untrusted=False): |
|
329 | 329 | """Get a config option and all sub-options. |
|
330 | 330 | |
|
331 | 331 | Some config options have sub-options that are declared with the |
|
332 | 332 | format "key:opt = value". This method is used to return the main |
|
333 | 333 | option and all its declared sub-options. |
|
334 | 334 | |
|
335 | 335 | Returns a 2-tuple of ``(option, sub-options)``, where `sub-options`` |
|
336 | 336 | is a dict of defined sub-options where keys and values are strings. |
|
337 | 337 | """ |
|
338 | 338 | data = self._data(untrusted) |
|
339 | 339 | main = data.get(section, name, default) |
|
340 | 340 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
341 | 341 | uvalue = self._ucfg.get(section, name) |
|
342 | 342 | if uvalue is not None and uvalue != main: |
|
343 | 343 | self.debug('ignoring untrusted configuration option ' |
|
344 | 344 | '%s.%s = %s\n' % (section, name, uvalue)) |
|
345 | 345 | |
|
346 | 346 | sub = {} |
|
347 | 347 | prefix = '%s:' % name |
|
348 | 348 | for k, v in data.items(section): |
|
349 | 349 | if k.startswith(prefix): |
|
350 | 350 | sub[k[len(prefix):]] = v |
|
351 | 351 | |
|
352 | 352 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
353 | 353 | for k, v in sub.items(): |
|
354 | 354 | uvalue = self._ucfg.get(section, '%s:%s' % (name, k)) |
|
355 | 355 | if uvalue is not None and uvalue != v: |
|
356 | 356 | self.debug('ignoring untrusted configuration option ' |
|
357 | 357 | '%s:%s.%s = %s\n' % (section, name, k, uvalue)) |
|
358 | 358 | |
|
359 | 359 | return main, sub |
|
360 | 360 | |
|
361 | 361 | def configpath(self, section, name, default=None, untrusted=False): |
|
362 | 362 | 'get a path config item, expanded relative to repo root or config file' |
|
363 | 363 | v = self.config(section, name, default, untrusted) |
|
364 | 364 | if v is None: |
|
365 | 365 | return None |
|
366 | 366 | if not os.path.isabs(v) or "://" not in v: |
|
367 | 367 | src = self.configsource(section, name, untrusted) |
|
368 | 368 | if ':' in src: |
|
369 | 369 | base = os.path.dirname(src.rsplit(':')[0]) |
|
370 | 370 | v = os.path.join(base, os.path.expanduser(v)) |
|
371 | 371 | return v |
|
372 | 372 | |
|
373 | 373 | def configbool(self, section, name, default=False, untrusted=False): |
|
374 | 374 | """parse a configuration element as a boolean |
|
375 | 375 | |
|
376 | 376 | >>> u = ui(); s = 'foo' |
|
377 | 377 | >>> u.setconfig(s, 'true', 'yes') |
|
378 | 378 | >>> u.configbool(s, 'true') |
|
379 | 379 | True |
|
380 | 380 | >>> u.setconfig(s, 'false', 'no') |
|
381 | 381 | >>> u.configbool(s, 'false') |
|
382 | 382 | False |
|
383 | 383 | >>> u.configbool(s, 'unknown') |
|
384 | 384 | False |
|
385 | 385 | >>> u.configbool(s, 'unknown', True) |
|
386 | 386 | True |
|
387 | 387 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
388 | 388 | >>> u.configbool(s, 'invalid') |
|
389 | 389 | Traceback (most recent call last): |
|
390 | 390 | ... |
|
391 | 391 | ConfigError: foo.invalid is not a boolean ('somevalue') |
|
392 | 392 | """ |
|
393 | 393 | |
|
394 | 394 | v = self.config(section, name, None, untrusted) |
|
395 | 395 | if v is None: |
|
396 | 396 | return default |
|
397 | 397 | if isinstance(v, bool): |
|
398 | 398 | return v |
|
399 | 399 | b = util.parsebool(v) |
|
400 | 400 | if b is None: |
|
401 | 401 | raise error.ConfigError(_("%s.%s is not a boolean ('%s')") |
|
402 | 402 | % (section, name, v)) |
|
403 | 403 | return b |
|
404 | 404 | |
|
405 | 405 | def configint(self, section, name, default=None, untrusted=False): |
|
406 | 406 | """parse a configuration element as an integer |
|
407 | 407 | |
|
408 | 408 | >>> u = ui(); s = 'foo' |
|
409 | 409 | >>> u.setconfig(s, 'int1', '42') |
|
410 | 410 | >>> u.configint(s, 'int1') |
|
411 | 411 | 42 |
|
412 | 412 | >>> u.setconfig(s, 'int2', '-42') |
|
413 | 413 | >>> u.configint(s, 'int2') |
|
414 | 414 | -42 |
|
415 | 415 | >>> u.configint(s, 'unknown', 7) |
|
416 | 416 | 7 |
|
417 | 417 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
418 | 418 | >>> u.configint(s, 'invalid') |
|
419 | 419 | Traceback (most recent call last): |
|
420 | 420 | ... |
|
421 | 421 | ConfigError: foo.invalid is not an integer ('somevalue') |
|
422 | 422 | """ |
|
423 | 423 | |
|
424 | 424 | v = self.config(section, name, None, untrusted) |
|
425 | 425 | if v is None: |
|
426 | 426 | return default |
|
427 | 427 | try: |
|
428 | 428 | return int(v) |
|
429 | 429 | except ValueError: |
|
430 | 430 | raise error.ConfigError(_("%s.%s is not an integer ('%s')") |
|
431 | 431 | % (section, name, v)) |
|
432 | 432 | |
|
433 | 433 | def configbytes(self, section, name, default=0, untrusted=False): |
|
434 | 434 | """parse a configuration element as a quantity in bytes |
|
435 | 435 | |
|
436 | 436 | Units can be specified as b (bytes), k or kb (kilobytes), m or |
|
437 | 437 | mb (megabytes), g or gb (gigabytes). |
|
438 | 438 | |
|
439 | 439 | >>> u = ui(); s = 'foo' |
|
440 | 440 | >>> u.setconfig(s, 'val1', '42') |
|
441 | 441 | >>> u.configbytes(s, 'val1') |
|
442 | 442 | 42 |
|
443 | 443 | >>> u.setconfig(s, 'val2', '42.5 kb') |
|
444 | 444 | >>> u.configbytes(s, 'val2') |
|
445 | 445 | 43520 |
|
446 | 446 | >>> u.configbytes(s, 'unknown', '7 MB') |
|
447 | 447 | 7340032 |
|
448 | 448 | >>> u.setconfig(s, 'invalid', 'somevalue') |
|
449 | 449 | >>> u.configbytes(s, 'invalid') |
|
450 | 450 | Traceback (most recent call last): |
|
451 | 451 | ... |
|
452 | 452 | ConfigError: foo.invalid is not a byte quantity ('somevalue') |
|
453 | 453 | """ |
|
454 | 454 | |
|
455 | 455 | value = self.config(section, name) |
|
456 | 456 | if value is None: |
|
457 | 457 | if not isinstance(default, str): |
|
458 | 458 | return default |
|
459 | 459 | value = default |
|
460 | 460 | try: |
|
461 | 461 | return util.sizetoint(value) |
|
462 | 462 | except error.ParseError: |
|
463 | 463 | raise error.ConfigError(_("%s.%s is not a byte quantity ('%s')") |
|
464 | 464 | % (section, name, value)) |
|
465 | 465 | |
|
466 | 466 | def configlist(self, section, name, default=None, untrusted=False): |
|
467 | 467 | """parse a configuration element as a list of comma/space separated |
|
468 | 468 | strings |
|
469 | 469 | |
|
470 | 470 | >>> u = ui(); s = 'foo' |
|
471 | 471 | >>> u.setconfig(s, 'list1', 'this,is "a small" ,test') |
|
472 | 472 | >>> u.configlist(s, 'list1') |
|
473 | 473 | ['this', 'is', 'a small', 'test'] |
|
474 | 474 | """ |
|
475 | 475 | |
|
476 | 476 | def _parse_plain(parts, s, offset): |
|
477 | 477 | whitespace = False |
|
478 | 478 | while offset < len(s) and (s[offset].isspace() or s[offset] == ','): |
|
479 | 479 | whitespace = True |
|
480 | 480 | offset += 1 |
|
481 | 481 | if offset >= len(s): |
|
482 | 482 | return None, parts, offset |
|
483 | 483 | if whitespace: |
|
484 | 484 | parts.append('') |
|
485 | 485 | if s[offset] == '"' and not parts[-1]: |
|
486 | 486 | return _parse_quote, parts, offset + 1 |
|
487 | 487 | elif s[offset] == '"' and parts[-1][-1] == '\\': |
|
488 | 488 | parts[-1] = parts[-1][:-1] + s[offset] |
|
489 | 489 | return _parse_plain, parts, offset + 1 |
|
490 | 490 | parts[-1] += s[offset] |
|
491 | 491 | return _parse_plain, parts, offset + 1 |
|
492 | 492 | |
|
493 | 493 | def _parse_quote(parts, s, offset): |
|
494 | 494 | if offset < len(s) and s[offset] == '"': # "" |
|
495 | 495 | parts.append('') |
|
496 | 496 | offset += 1 |
|
497 | 497 | while offset < len(s) and (s[offset].isspace() or |
|
498 | 498 | s[offset] == ','): |
|
499 | 499 | offset += 1 |
|
500 | 500 | return _parse_plain, parts, offset |
|
501 | 501 | |
|
502 | 502 | while offset < len(s) and s[offset] != '"': |
|
503 | 503 | if (s[offset] == '\\' and offset + 1 < len(s) |
|
504 | 504 | and s[offset + 1] == '"'): |
|
505 | 505 | offset += 1 |
|
506 | 506 | parts[-1] += '"' |
|
507 | 507 | else: |
|
508 | 508 | parts[-1] += s[offset] |
|
509 | 509 | offset += 1 |
|
510 | 510 | |
|
511 | 511 | if offset >= len(s): |
|
512 | 512 | real_parts = _configlist(parts[-1]) |
|
513 | 513 | if not real_parts: |
|
514 | 514 | parts[-1] = '"' |
|
515 | 515 | else: |
|
516 | 516 | real_parts[0] = '"' + real_parts[0] |
|
517 | 517 | parts = parts[:-1] |
|
518 | 518 | parts.extend(real_parts) |
|
519 | 519 | return None, parts, offset |
|
520 | 520 | |
|
521 | 521 | offset += 1 |
|
522 | 522 | while offset < len(s) and s[offset] in [' ', ',']: |
|
523 | 523 | offset += 1 |
|
524 | 524 | |
|
525 | 525 | if offset < len(s): |
|
526 | 526 | if offset + 1 == len(s) and s[offset] == '"': |
|
527 | 527 | parts[-1] += '"' |
|
528 | 528 | offset += 1 |
|
529 | 529 | else: |
|
530 | 530 | parts.append('') |
|
531 | 531 | else: |
|
532 | 532 | return None, parts, offset |
|
533 | 533 | |
|
534 | 534 | return _parse_plain, parts, offset |
|
535 | 535 | |
|
536 | 536 | def _configlist(s): |
|
537 | 537 | s = s.rstrip(' ,') |
|
538 | 538 | if not s: |
|
539 | 539 | return [] |
|
540 | 540 | parser, parts, offset = _parse_plain, [''], 0 |
|
541 | 541 | while parser: |
|
542 | 542 | parser, parts, offset = parser(parts, s, offset) |
|
543 | 543 | return parts |
|
544 | 544 | |
|
545 | 545 | result = self.config(section, name, untrusted=untrusted) |
|
546 | 546 | if result is None: |
|
547 | 547 | result = default or [] |
|
548 | 548 | if isinstance(result, bytes): |
|
549 | 549 | result = _configlist(result.lstrip(' ,\n')) |
|
550 | 550 | if result is None: |
|
551 | 551 | result = default or [] |
|
552 | 552 | return result |
|
553 | 553 | |
|
554 | 554 | def hasconfig(self, section, name, untrusted=False): |
|
555 | 555 | return self._data(untrusted).hasitem(section, name) |
|
556 | 556 | |
|
557 | 557 | def has_section(self, section, untrusted=False): |
|
558 | 558 | '''tell whether section exists in config.''' |
|
559 | 559 | return section in self._data(untrusted) |
|
560 | 560 | |
|
561 | 561 | def configitems(self, section, untrusted=False, ignoresub=False): |
|
562 | 562 | items = self._data(untrusted).items(section) |
|
563 | 563 | if ignoresub: |
|
564 | 564 | newitems = {} |
|
565 | 565 | for k, v in items: |
|
566 | 566 | if ':' not in k: |
|
567 | 567 | newitems[k] = v |
|
568 | 568 | items = newitems.items() |
|
569 | 569 | if self.debugflag and not untrusted and self._reportuntrusted: |
|
570 | 570 | for k, v in self._ucfg.items(section): |
|
571 | 571 | if self._tcfg.get(section, k) != v: |
|
572 | 572 | self.debug("ignoring untrusted configuration option " |
|
573 | 573 | "%s.%s = %s\n" % (section, k, v)) |
|
574 | 574 | return items |
|
575 | 575 | |
|
576 | 576 | def walkconfig(self, untrusted=False): |
|
577 | 577 | cfg = self._data(untrusted) |
|
578 | 578 | for section in cfg.sections(): |
|
579 | 579 | for name, value in self.configitems(section, untrusted): |
|
580 | 580 | yield section, name, value |
|
581 | 581 | |
|
582 | 582 | def plain(self, feature=None): |
|
583 | 583 | '''is plain mode active? |
|
584 | 584 | |
|
585 | 585 | Plain mode means that all configuration variables which affect |
|
586 | 586 | the behavior and output of Mercurial should be |
|
587 | 587 | ignored. Additionally, the output should be stable, |
|
588 | 588 | reproducible and suitable for use in scripts or applications. |
|
589 | 589 | |
|
590 | 590 | The only way to trigger plain mode is by setting either the |
|
591 | 591 | `HGPLAIN' or `HGPLAINEXCEPT' environment variables. |
|
592 | 592 | |
|
593 | 593 | The return value can either be |
|
594 | 594 | - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT |
|
595 | 595 | - True otherwise |
|
596 | 596 | ''' |
|
597 | 597 | if ('HGPLAIN' not in encoding.environ and |
|
598 | 598 | 'HGPLAINEXCEPT' not in encoding.environ): |
|
599 | 599 | return False |
|
600 | 600 | exceptions = encoding.environ.get('HGPLAINEXCEPT', |
|
601 | 601 | '').strip().split(',') |
|
602 | 602 | if feature and exceptions: |
|
603 | 603 | return feature not in exceptions |
|
604 | 604 | return True |
|
605 | 605 | |
|
606 | 606 | def username(self): |
|
607 | 607 | """Return default username to be used in commits. |
|
608 | 608 | |
|
609 | 609 | Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL |
|
610 | 610 | and stop searching if one of these is set. |
|
611 | 611 | If not found and ui.askusername is True, ask the user, else use |
|
612 | 612 | ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". |
|
613 | 613 | """ |
|
614 | 614 | user = encoding.environ.get("HGUSER") |
|
615 | 615 | if user is None: |
|
616 | 616 | user = self.config("ui", ["username", "user"]) |
|
617 | 617 | if user is not None: |
|
618 | 618 | user = os.path.expandvars(user) |
|
619 | 619 | if user is None: |
|
620 | 620 | user = encoding.environ.get("EMAIL") |
|
621 | 621 | if user is None and self.configbool("ui", "askusername"): |
|
622 | 622 | user = self.prompt(_("enter a commit username:"), default=None) |
|
623 | 623 | if user is None and not self.interactive(): |
|
624 | 624 | try: |
|
625 | 625 | user = '%s@%s' % (util.getuser(), socket.getfqdn()) |
|
626 | 626 | self.warn(_("no username found, using '%s' instead\n") % user) |
|
627 | 627 | except KeyError: |
|
628 | 628 | pass |
|
629 | 629 | if not user: |
|
630 | 630 | raise error.Abort(_('no username supplied'), |
|
631 | 631 | hint=_("use 'hg config --edit' " |
|
632 | 632 | 'to set your username')) |
|
633 | 633 | if "\n" in user: |
|
634 | 634 | raise error.Abort(_("username %s contains a newline\n") |
|
635 | 635 | % repr(user)) |
|
636 | 636 | return user |
|
637 | 637 | |
|
638 | 638 | def shortuser(self, user): |
|
639 | 639 | """Return a short representation of a user name or email address.""" |
|
640 | 640 | if not self.verbose: |
|
641 | 641 | user = util.shortuser(user) |
|
642 | 642 | return user |
|
643 | 643 | |
|
644 | 644 | def expandpath(self, loc, default=None): |
|
645 | 645 | """Return repository location relative to cwd or from [paths]""" |
|
646 | 646 | try: |
|
647 | 647 | p = self.paths.getpath(loc) |
|
648 | 648 | if p: |
|
649 | 649 | return p.rawloc |
|
650 | 650 | except error.RepoError: |
|
651 | 651 | pass |
|
652 | 652 | |
|
653 | 653 | if default: |
|
654 | 654 | try: |
|
655 | 655 | p = self.paths.getpath(default) |
|
656 | 656 | if p: |
|
657 | 657 | return p.rawloc |
|
658 | 658 | except error.RepoError: |
|
659 | 659 | pass |
|
660 | 660 | |
|
661 | 661 | return loc |
|
662 | 662 | |
|
663 | 663 | @util.propertycache |
|
664 | 664 | def paths(self): |
|
665 | 665 | return paths(self) |
|
666 | 666 | |
|
667 | 667 | def pushbuffer(self, error=False, subproc=False, labeled=False): |
|
668 | 668 | """install a buffer to capture standard output of the ui object |
|
669 | 669 | |
|
670 | 670 | If error is True, the error output will be captured too. |
|
671 | 671 | |
|
672 | 672 | If subproc is True, output from subprocesses (typically hooks) will be |
|
673 | 673 | captured too. |
|
674 | 674 | |
|
675 | 675 | If labeled is True, any labels associated with buffered |
|
676 | 676 | output will be handled. By default, this has no effect |
|
677 | 677 | on the output returned, but extensions and GUI tools may |
|
678 | 678 | handle this argument and returned styled output. If output |
|
679 | 679 | is being buffered so it can be captured and parsed or |
|
680 | 680 | processed, labeled should not be set to True. |
|
681 | 681 | """ |
|
682 | 682 | self._buffers.append([]) |
|
683 | 683 | self._bufferstates.append((error, subproc, labeled)) |
|
684 | 684 | self._bufferapplylabels = labeled |
|
685 | 685 | |
|
686 | 686 | def popbuffer(self): |
|
687 | 687 | '''pop the last buffer and return the buffered output''' |
|
688 | 688 | self._bufferstates.pop() |
|
689 | 689 | if self._bufferstates: |
|
690 | 690 | self._bufferapplylabels = self._bufferstates[-1][2] |
|
691 | 691 | else: |
|
692 | 692 | self._bufferapplylabels = None |
|
693 | 693 | |
|
694 | 694 | return "".join(self._buffers.pop()) |
|
695 | 695 | |
|
696 | 696 | def write(self, *args, **opts): |
|
697 | 697 | '''write args to output |
|
698 | 698 | |
|
699 | 699 | By default, this method simply writes to the buffer or stdout, |
|
700 | 700 | but extensions or GUI tools may override this method, |
|
701 | 701 | write_err(), popbuffer(), and label() to style output from |
|
702 | 702 | various parts of hg. |
|
703 | 703 | |
|
704 | 704 | An optional keyword argument, "label", can be passed in. |
|
705 | 705 | This should be a string containing label names separated by |
|
706 | 706 | space. Label names take the form of "topic.type". For example, |
|
707 | 707 | ui.debug() issues a label of "ui.debug". |
|
708 | 708 | |
|
709 | 709 | When labeling output for a specific command, a label of |
|
710 | 710 | "cmdname.type" is recommended. For example, status issues |
|
711 | 711 | a label of "status.modified" for modified files. |
|
712 | 712 | ''' |
|
713 | 713 | if self._buffers and not opts.get('prompt', False): |
|
714 | 714 | self._buffers[-1].extend(a for a in args) |
|
715 | 715 | else: |
|
716 | 716 | self._progclear() |
|
717 | 717 | for a in args: |
|
718 | 718 | self.fout.write(a) |
|
719 | 719 | |
|
720 | 720 | def write_err(self, *args, **opts): |
|
721 | 721 | self._progclear() |
|
722 | 722 | try: |
|
723 | 723 | if self._bufferstates and self._bufferstates[-1][0]: |
|
724 | 724 | return self.write(*args, **opts) |
|
725 | 725 | if not getattr(self.fout, 'closed', False): |
|
726 | 726 | self.fout.flush() |
|
727 | 727 | for a in args: |
|
728 | 728 | self.ferr.write(a) |
|
729 | 729 | # stderr may be buffered under win32 when redirected to files, |
|
730 | 730 | # including stdout. |
|
731 | 731 | if not getattr(self.ferr, 'closed', False): |
|
732 | 732 | self.ferr.flush() |
|
733 | 733 | except IOError as inst: |
|
734 | 734 | if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF): |
|
735 | 735 | raise |
|
736 | 736 | |
|
737 | 737 | def flush(self): |
|
738 | 738 | try: self.fout.flush() |
|
739 | 739 | except (IOError, ValueError): pass |
|
740 | 740 | try: self.ferr.flush() |
|
741 | 741 | except (IOError, ValueError): pass |
|
742 | 742 | |
|
743 | 743 | def _isatty(self, fh): |
|
744 | 744 | if self.configbool('ui', 'nontty', False): |
|
745 | 745 | return False |
|
746 | 746 | return util.isatty(fh) |
|
747 | 747 | |
|
748 | 748 | def interface(self, feature): |
|
749 | 749 | """what interface to use for interactive console features? |
|
750 | 750 | |
|
751 | 751 | The interface is controlled by the value of `ui.interface` but also by |
|
752 | 752 | the value of feature-specific configuration. For example: |
|
753 | 753 | |
|
754 | 754 | ui.interface.histedit = text |
|
755 | 755 | ui.interface.chunkselector = curses |
|
756 | 756 | |
|
757 | 757 | Here the features are "histedit" and "chunkselector". |
|
758 | 758 | |
|
759 | 759 | The configuration above means that the default interfaces for commands |
|
760 | 760 | is curses, the interface for histedit is text and the interface for |
|
761 | 761 | selecting chunk is crecord (the best curses interface available). |
|
762 | 762 | |
|
763 | 763 | Consider the following example: |
|
764 | 764 | ui.interface = curses |
|
765 | 765 | ui.interface.histedit = text |
|
766 | 766 | |
|
767 | 767 | Then histedit will use the text interface and chunkselector will use |
|
768 | 768 | the default curses interface (crecord at the moment). |
|
769 | 769 | """ |
|
770 | 770 | alldefaults = frozenset(["text", "curses"]) |
|
771 | 771 | |
|
772 | 772 | featureinterfaces = { |
|
773 | 773 | "chunkselector": [ |
|
774 | 774 | "text", |
|
775 | 775 | "curses", |
|
776 | 776 | ] |
|
777 | 777 | } |
|
778 | 778 | |
|
779 | 779 | # Feature-specific interface |
|
780 | 780 | if feature not in featureinterfaces.keys(): |
|
781 | 781 | # Programming error, not user error |
|
782 | 782 | raise ValueError("Unknown feature requested %s" % feature) |
|
783 | 783 | |
|
784 | 784 | availableinterfaces = frozenset(featureinterfaces[feature]) |
|
785 | 785 | if alldefaults > availableinterfaces: |
|
786 | 786 | # Programming error, not user error. We need a use case to |
|
787 | 787 | # define the right thing to do here. |
|
788 | 788 | raise ValueError( |
|
789 | 789 | "Feature %s does not handle all default interfaces" % |
|
790 | 790 | feature) |
|
791 | 791 | |
|
792 | 792 | if self.plain(): |
|
793 | 793 | return "text" |
|
794 | 794 | |
|
795 | 795 | # Default interface for all the features |
|
796 | 796 | defaultinterface = "text" |
|
797 | 797 | i = self.config("ui", "interface", None) |
|
798 | 798 | if i in alldefaults: |
|
799 | 799 | defaultinterface = i |
|
800 | 800 | |
|
801 | 801 | choseninterface = defaultinterface |
|
802 | 802 | f = self.config("ui", "interface.%s" % feature, None) |
|
803 | 803 | if f in availableinterfaces: |
|
804 | 804 | choseninterface = f |
|
805 | 805 | |
|
806 | 806 | if i is not None and defaultinterface != i: |
|
807 | 807 | if f is not None: |
|
808 | 808 | self.warn(_("invalid value for ui.interface: %s\n") % |
|
809 | 809 | (i,)) |
|
810 | 810 | else: |
|
811 | 811 | self.warn(_("invalid value for ui.interface: %s (using %s)\n") % |
|
812 | 812 | (i, choseninterface)) |
|
813 | 813 | if f is not None and choseninterface != f: |
|
814 | 814 | self.warn(_("invalid value for ui.interface.%s: %s (using %s)\n") % |
|
815 | 815 | (feature, f, choseninterface)) |
|
816 | 816 | |
|
817 | 817 | return choseninterface |
|
818 | 818 | |
|
819 | 819 | def interactive(self): |
|
820 | 820 | '''is interactive input allowed? |
|
821 | 821 | |
|
822 | 822 | An interactive session is a session where input can be reasonably read |
|
823 | 823 | from `sys.stdin'. If this function returns false, any attempt to read |
|
824 | 824 | from stdin should fail with an error, unless a sensible default has been |
|
825 | 825 | specified. |
|
826 | 826 | |
|
827 | 827 | Interactiveness is triggered by the value of the `ui.interactive' |
|
828 | 828 | configuration variable or - if it is unset - when `sys.stdin' points |
|
829 | 829 | to a terminal device. |
|
830 | 830 | |
|
831 | 831 | This function refers to input only; for output, see `ui.formatted()'. |
|
832 | 832 | ''' |
|
833 | 833 | i = self.configbool("ui", "interactive", None) |
|
834 | 834 | if i is None: |
|
835 | 835 | # some environments replace stdin without implementing isatty |
|
836 | 836 | # usually those are non-interactive |
|
837 | 837 | return self._isatty(self.fin) |
|
838 | 838 | |
|
839 | 839 | return i |
|
840 | 840 | |
|
841 | 841 | def termwidth(self): |
|
842 | 842 | '''how wide is the terminal in columns? |
|
843 | 843 | ''' |
|
844 | 844 | if 'COLUMNS' in encoding.environ: |
|
845 | 845 | try: |
|
846 | 846 | return int(encoding.environ['COLUMNS']) |
|
847 | 847 | except ValueError: |
|
848 | 848 | pass |
|
849 | 849 | return scmutil.termsize(self)[0] |
|
850 | 850 | |
|
851 | 851 | def formatted(self): |
|
852 | 852 | '''should formatted output be used? |
|
853 | 853 | |
|
854 | 854 | It is often desirable to format the output to suite the output medium. |
|
855 | 855 | Examples of this are truncating long lines or colorizing messages. |
|
856 | 856 | However, this is not often not desirable when piping output into other |
|
857 | 857 | utilities, e.g. `grep'. |
|
858 | 858 | |
|
859 | 859 | Formatted output is triggered by the value of the `ui.formatted' |
|
860 | 860 | configuration variable or - if it is unset - when `sys.stdout' points |
|
861 | 861 | to a terminal device. Please note that `ui.formatted' should be |
|
862 | 862 | considered an implementation detail; it is not intended for use outside |
|
863 | 863 | Mercurial or its extensions. |
|
864 | 864 | |
|
865 | 865 | This function refers to output only; for input, see `ui.interactive()'. |
|
866 | 866 | This function always returns false when in plain mode, see `ui.plain()'. |
|
867 | 867 | ''' |
|
868 | 868 | if self.plain(): |
|
869 | 869 | return False |
|
870 | 870 | |
|
871 | 871 | i = self.configbool("ui", "formatted", None) |
|
872 | 872 | if i is None: |
|
873 | 873 | # some environments replace stdout without implementing isatty |
|
874 | 874 | # usually those are non-interactive |
|
875 | 875 | return self._isatty(self.fout) |
|
876 | 876 | |
|
877 | 877 | return i |
|
878 | 878 | |
|
879 | 879 | def _readline(self, prompt=''): |
|
880 | 880 | if self._isatty(self.fin): |
|
881 | 881 | try: |
|
882 | 882 | # magically add command line editing support, where |
|
883 | 883 | # available |
|
884 | 884 | import readline |
|
885 | 885 | # force demandimport to really load the module |
|
886 | 886 | readline.read_history_file |
|
887 | 887 | # windows sometimes raises something other than ImportError |
|
888 | 888 | except Exception: |
|
889 | 889 | pass |
|
890 | 890 | |
|
891 | 891 | # call write() so output goes through subclassed implementation |
|
892 | 892 | # e.g. color extension on Windows |
|
893 | 893 | self.write(prompt, prompt=True) |
|
894 | 894 | |
|
895 | 895 | # instead of trying to emulate raw_input, swap (self.fin, |
|
896 | 896 | # self.fout) with (sys.stdin, sys.stdout) |
|
897 | 897 | oldin = sys.stdin |
|
898 | 898 | oldout = sys.stdout |
|
899 | 899 | sys.stdin = self.fin |
|
900 | 900 | sys.stdout = self.fout |
|
901 | 901 | # prompt ' ' must exist; otherwise readline may delete entire line |
|
902 | 902 | # - http://bugs.python.org/issue12833 |
|
903 | 903 | line = raw_input(' ') |
|
904 | 904 | sys.stdin = oldin |
|
905 | 905 | sys.stdout = oldout |
|
906 | 906 | |
|
907 | 907 | # When stdin is in binary mode on Windows, it can cause |
|
908 | 908 | # raw_input() to emit an extra trailing carriage return |
|
909 | 909 | if os.linesep == '\r\n' and line and line[-1] == '\r': |
|
910 | 910 | line = line[:-1] |
|
911 | 911 | return line |
|
912 | 912 | |
|
913 | 913 | def prompt(self, msg, default="y"): |
|
914 | 914 | """Prompt user with msg, read response. |
|
915 | 915 | If ui is not interactive, the default is returned. |
|
916 | 916 | """ |
|
917 | 917 | if not self.interactive(): |
|
918 | 918 | self.write(msg, ' ', default or '', "\n") |
|
919 | 919 | return default |
|
920 | 920 | try: |
|
921 | 921 | r = self._readline(self.label(msg, 'ui.prompt')) |
|
922 | 922 | if not r: |
|
923 | 923 | r = default |
|
924 | 924 | if self.configbool('ui', 'promptecho'): |
|
925 | 925 | self.write(r, "\n") |
|
926 | 926 | return r |
|
927 | 927 | except EOFError: |
|
928 | 928 | raise error.ResponseExpected() |
|
929 | 929 | |
|
930 | 930 | @staticmethod |
|
931 | 931 | def extractchoices(prompt): |
|
932 | 932 | """Extract prompt message and list of choices from specified prompt. |
|
933 | 933 | |
|
934 | 934 | This returns tuple "(message, choices)", and "choices" is the |
|
935 | 935 | list of tuple "(response character, text without &)". |
|
936 | 936 | |
|
937 | 937 | >>> ui.extractchoices("awake? $$ &Yes $$ &No") |
|
938 | 938 | ('awake? ', [('y', 'Yes'), ('n', 'No')]) |
|
939 | 939 | >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No") |
|
940 | 940 | ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')]) |
|
941 | 941 | >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o") |
|
942 | 942 | ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')]) |
|
943 | 943 | """ |
|
944 | 944 | |
|
945 | 945 | # Sadly, the prompt string may have been built with a filename |
|
946 | 946 | # containing "$$" so let's try to find the first valid-looking |
|
947 | 947 | # prompt to start parsing. Sadly, we also can't rely on |
|
948 | 948 | # choices containing spaces, ASCII, or basically anything |
|
949 | 949 | # except an ampersand followed by a character. |
|
950 | 950 | m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt) |
|
951 | 951 | msg = m.group(1) |
|
952 | 952 | choices = [p.strip(' ') for p in m.group(2).split('$$')] |
|
953 | 953 | return (msg, |
|
954 | 954 | [(s[s.index('&') + 1].lower(), s.replace('&', '', 1)) |
|
955 | 955 | for s in choices]) |
|
956 | 956 | |
|
957 | 957 | def promptchoice(self, prompt, default=0): |
|
958 | 958 | """Prompt user with a message, read response, and ensure it matches |
|
959 | 959 | one of the provided choices. The prompt is formatted as follows: |
|
960 | 960 | |
|
961 | 961 | "would you like fries with that (Yn)? $$ &Yes $$ &No" |
|
962 | 962 | |
|
963 | 963 | The index of the choice is returned. Responses are case |
|
964 | 964 | insensitive. If ui is not interactive, the default is |
|
965 | 965 | returned. |
|
966 | 966 | """ |
|
967 | 967 | |
|
968 | 968 | msg, choices = self.extractchoices(prompt) |
|
969 | 969 | resps = [r for r, t in choices] |
|
970 | 970 | while True: |
|
971 | 971 | r = self.prompt(msg, resps[default]) |
|
972 | 972 | if r.lower() in resps: |
|
973 | 973 | return resps.index(r.lower()) |
|
974 | 974 | self.write(_("unrecognized response\n")) |
|
975 | 975 | |
|
976 | 976 | def getpass(self, prompt=None, default=None): |
|
977 | 977 | if not self.interactive(): |
|
978 | 978 | return default |
|
979 | 979 | try: |
|
980 | 980 | self.write_err(self.label(prompt or _('password: '), 'ui.prompt')) |
|
981 | 981 | # disable getpass() only if explicitly specified. it's still valid |
|
982 | 982 | # to interact with tty even if fin is not a tty. |
|
983 | 983 | if self.configbool('ui', 'nontty'): |
|
984 | 984 | l = self.fin.readline() |
|
985 | 985 | if not l: |
|
986 | 986 | raise EOFError |
|
987 | 987 | return l.rstrip('\n') |
|
988 | 988 | else: |
|
989 | 989 | return getpass.getpass('') |
|
990 | 990 | except EOFError: |
|
991 | 991 | raise error.ResponseExpected() |
|
992 | 992 | def status(self, *msg, **opts): |
|
993 | 993 | '''write status message to output (if ui.quiet is False) |
|
994 | 994 | |
|
995 | 995 | This adds an output label of "ui.status". |
|
996 | 996 | ''' |
|
997 | 997 | if not self.quiet: |
|
998 | 998 | opts['label'] = opts.get('label', '') + ' ui.status' |
|
999 | 999 | self.write(*msg, **opts) |
|
1000 | 1000 | def warn(self, *msg, **opts): |
|
1001 | 1001 | '''write warning message to output (stderr) |
|
1002 | 1002 | |
|
1003 | 1003 | This adds an output label of "ui.warning". |
|
1004 | 1004 | ''' |
|
1005 | 1005 | opts['label'] = opts.get('label', '') + ' ui.warning' |
|
1006 | 1006 | self.write_err(*msg, **opts) |
|
1007 | 1007 | def note(self, *msg, **opts): |
|
1008 | 1008 | '''write note to output (if ui.verbose is True) |
|
1009 | 1009 | |
|
1010 | 1010 | This adds an output label of "ui.note". |
|
1011 | 1011 | ''' |
|
1012 | 1012 | if self.verbose: |
|
1013 | 1013 | opts['label'] = opts.get('label', '') + ' ui.note' |
|
1014 | 1014 | self.write(*msg, **opts) |
|
1015 | 1015 | def debug(self, *msg, **opts): |
|
1016 | 1016 | '''write debug message to output (if ui.debugflag is True) |
|
1017 | 1017 | |
|
1018 | 1018 | This adds an output label of "ui.debug". |
|
1019 | 1019 | ''' |
|
1020 | 1020 | if self.debugflag: |
|
1021 | 1021 | opts['label'] = opts.get('label', '') + ' ui.debug' |
|
1022 | 1022 | self.write(*msg, **opts) |
|
1023 | 1023 | |
|
1024 | 1024 | def edit(self, text, user, extra=None, editform=None, pending=None, |
|
1025 | 1025 | repopath=None): |
|
1026 | 1026 | extra_defaults = { |
|
1027 | 1027 | 'prefix': 'editor', |
|
1028 | 1028 | 'suffix': '.txt', |
|
1029 | 1029 | } |
|
1030 | 1030 | if extra is not None: |
|
1031 | 1031 | extra_defaults.update(extra) |
|
1032 | 1032 | extra = extra_defaults |
|
1033 | 1033 | |
|
1034 | 1034 | rdir = None |
|
1035 | 1035 | if self.configbool('experimental', 'editortmpinhg'): |
|
1036 | 1036 | rdir = repopath |
|
1037 | 1037 | (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-', |
|
1038 | 1038 | suffix=extra['suffix'], text=True, |
|
1039 | 1039 | dir=rdir) |
|
1040 | 1040 | try: |
|
1041 | f = os.fdopen(fd, "w") | |
|
1041 | f = os.fdopen(fd, pycompat.sysstr("w")) | |
|
1042 | 1042 | f.write(text) |
|
1043 | 1043 | f.close() |
|
1044 | 1044 | |
|
1045 | 1045 | environ = {'HGUSER': user} |
|
1046 | 1046 | if 'transplant_source' in extra: |
|
1047 | 1047 | environ.update({'HGREVISION': hex(extra['transplant_source'])}) |
|
1048 | 1048 | for label in ('intermediate-source', 'source', 'rebase_source'): |
|
1049 | 1049 | if label in extra: |
|
1050 | 1050 | environ.update({'HGREVISION': extra[label]}) |
|
1051 | 1051 | break |
|
1052 | 1052 | if editform: |
|
1053 | 1053 | environ.update({'HGEDITFORM': editform}) |
|
1054 | 1054 | if pending: |
|
1055 | 1055 | environ.update({'HG_PENDING': pending}) |
|
1056 | 1056 | |
|
1057 | 1057 | editor = self.geteditor() |
|
1058 | 1058 | |
|
1059 | 1059 | self.system("%s \"%s\"" % (editor, name), |
|
1060 | 1060 | environ=environ, |
|
1061 | 1061 | onerr=error.Abort, errprefix=_("edit failed")) |
|
1062 | 1062 | |
|
1063 | 1063 | f = open(name) |
|
1064 | 1064 | t = f.read() |
|
1065 | 1065 | f.close() |
|
1066 | 1066 | finally: |
|
1067 | 1067 | os.unlink(name) |
|
1068 | 1068 | |
|
1069 | 1069 | return t |
|
1070 | 1070 | |
|
1071 | 1071 | def system(self, cmd, environ=None, cwd=None, onerr=None, errprefix=None): |
|
1072 | 1072 | '''execute shell command with appropriate output stream. command |
|
1073 | 1073 | output will be redirected if fout is not stdout. |
|
1074 | 1074 | ''' |
|
1075 | 1075 | out = self.fout |
|
1076 | 1076 | if any(s[1] for s in self._bufferstates): |
|
1077 | 1077 | out = self |
|
1078 | 1078 | return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr, |
|
1079 | 1079 | errprefix=errprefix, out=out) |
|
1080 | 1080 | |
|
1081 | 1081 | def traceback(self, exc=None, force=False): |
|
1082 | 1082 | '''print exception traceback if traceback printing enabled or forced. |
|
1083 | 1083 | only to call in exception handler. returns true if traceback |
|
1084 | 1084 | printed.''' |
|
1085 | 1085 | if self.tracebackflag or force: |
|
1086 | 1086 | if exc is None: |
|
1087 | 1087 | exc = sys.exc_info() |
|
1088 | 1088 | cause = getattr(exc[1], 'cause', None) |
|
1089 | 1089 | |
|
1090 | 1090 | if cause is not None: |
|
1091 | 1091 | causetb = traceback.format_tb(cause[2]) |
|
1092 | 1092 | exctb = traceback.format_tb(exc[2]) |
|
1093 | 1093 | exconly = traceback.format_exception_only(cause[0], cause[1]) |
|
1094 | 1094 | |
|
1095 | 1095 | # exclude frame where 'exc' was chained and rethrown from exctb |
|
1096 | 1096 | self.write_err('Traceback (most recent call last):\n', |
|
1097 | 1097 | ''.join(exctb[:-1]), |
|
1098 | 1098 | ''.join(causetb), |
|
1099 | 1099 | ''.join(exconly)) |
|
1100 | 1100 | else: |
|
1101 | 1101 | output = traceback.format_exception(exc[0], exc[1], exc[2]) |
|
1102 | 1102 | self.write_err(''.join(output)) |
|
1103 | 1103 | return self.tracebackflag or force |
|
1104 | 1104 | |
|
1105 | 1105 | def geteditor(self): |
|
1106 | 1106 | '''return editor to use''' |
|
1107 | 1107 | if pycompat.sysplatform == 'plan9': |
|
1108 | 1108 | # vi is the MIPS instruction simulator on Plan 9. We |
|
1109 | 1109 | # instead default to E to plumb commit messages to |
|
1110 | 1110 | # avoid confusion. |
|
1111 | 1111 | editor = 'E' |
|
1112 | 1112 | else: |
|
1113 | 1113 | editor = 'vi' |
|
1114 | 1114 | return (encoding.environ.get("HGEDITOR") or |
|
1115 | 1115 | self.config("ui", "editor") or |
|
1116 | 1116 | encoding.environ.get("VISUAL") or |
|
1117 | 1117 | encoding.environ.get("EDITOR", editor)) |
|
1118 | 1118 | |
|
1119 | 1119 | @util.propertycache |
|
1120 | 1120 | def _progbar(self): |
|
1121 | 1121 | """setup the progbar singleton to the ui object""" |
|
1122 | 1122 | if (self.quiet or self.debugflag |
|
1123 | 1123 | or self.configbool('progress', 'disable', False) |
|
1124 | 1124 | or not progress.shouldprint(self)): |
|
1125 | 1125 | return None |
|
1126 | 1126 | return getprogbar(self) |
|
1127 | 1127 | |
|
1128 | 1128 | def _progclear(self): |
|
1129 | 1129 | """clear progress bar output if any. use it before any output""" |
|
1130 | 1130 | if '_progbar' not in vars(self): # nothing loaded yet |
|
1131 | 1131 | return |
|
1132 | 1132 | if self._progbar is not None and self._progbar.printed: |
|
1133 | 1133 | self._progbar.clear() |
|
1134 | 1134 | |
|
1135 | 1135 | def progress(self, topic, pos, item="", unit="", total=None): |
|
1136 | 1136 | '''show a progress message |
|
1137 | 1137 | |
|
1138 | 1138 | By default a textual progress bar will be displayed if an operation |
|
1139 | 1139 | takes too long. 'topic' is the current operation, 'item' is a |
|
1140 | 1140 | non-numeric marker of the current position (i.e. the currently |
|
1141 | 1141 | in-process file), 'pos' is the current numeric position (i.e. |
|
1142 | 1142 | revision, bytes, etc.), unit is a corresponding unit label, |
|
1143 | 1143 | and total is the highest expected pos. |
|
1144 | 1144 | |
|
1145 | 1145 | Multiple nested topics may be active at a time. |
|
1146 | 1146 | |
|
1147 | 1147 | All topics should be marked closed by setting pos to None at |
|
1148 | 1148 | termination. |
|
1149 | 1149 | ''' |
|
1150 | 1150 | if self._progbar is not None: |
|
1151 | 1151 | self._progbar.progress(topic, pos, item=item, unit=unit, |
|
1152 | 1152 | total=total) |
|
1153 | 1153 | if pos is None or not self.configbool('progress', 'debug'): |
|
1154 | 1154 | return |
|
1155 | 1155 | |
|
1156 | 1156 | if unit: |
|
1157 | 1157 | unit = ' ' + unit |
|
1158 | 1158 | if item: |
|
1159 | 1159 | item = ' ' + item |
|
1160 | 1160 | |
|
1161 | 1161 | if total: |
|
1162 | 1162 | pct = 100.0 * pos / total |
|
1163 | 1163 | self.debug('%s:%s %s/%s%s (%4.2f%%)\n' |
|
1164 | 1164 | % (topic, item, pos, total, unit, pct)) |
|
1165 | 1165 | else: |
|
1166 | 1166 | self.debug('%s:%s %s%s\n' % (topic, item, pos, unit)) |
|
1167 | 1167 | |
|
1168 | 1168 | def log(self, service, *msg, **opts): |
|
1169 | 1169 | '''hook for logging facility extensions |
|
1170 | 1170 | |
|
1171 | 1171 | service should be a readily-identifiable subsystem, which will |
|
1172 | 1172 | allow filtering. |
|
1173 | 1173 | |
|
1174 | 1174 | *msg should be a newline-terminated format string to log, and |
|
1175 | 1175 | then any values to %-format into that format string. |
|
1176 | 1176 | |
|
1177 | 1177 | **opts currently has no defined meanings. |
|
1178 | 1178 | ''' |
|
1179 | 1179 | |
|
1180 | 1180 | def label(self, msg, label): |
|
1181 | 1181 | '''style msg based on supplied label |
|
1182 | 1182 | |
|
1183 | 1183 | Like ui.write(), this just returns msg unchanged, but extensions |
|
1184 | 1184 | and GUI tools can override it to allow styling output without |
|
1185 | 1185 | writing it. |
|
1186 | 1186 | |
|
1187 | 1187 | ui.write(s, 'label') is equivalent to |
|
1188 | 1188 | ui.write(ui.label(s, 'label')). |
|
1189 | 1189 | ''' |
|
1190 | 1190 | return msg |
|
1191 | 1191 | |
|
1192 | 1192 | def develwarn(self, msg, stacklevel=1, config=None): |
|
1193 | 1193 | """issue a developer warning message |
|
1194 | 1194 | |
|
1195 | 1195 | Use 'stacklevel' to report the offender some layers further up in the |
|
1196 | 1196 | stack. |
|
1197 | 1197 | """ |
|
1198 | 1198 | if not self.configbool('devel', 'all-warnings'): |
|
1199 | 1199 | if config is not None and not self.configbool('devel', config): |
|
1200 | 1200 | return |
|
1201 | 1201 | msg = 'devel-warn: ' + msg |
|
1202 | 1202 | stacklevel += 1 # get in develwarn |
|
1203 | 1203 | if self.tracebackflag: |
|
1204 | 1204 | util.debugstacktrace(msg, stacklevel, self.ferr, self.fout) |
|
1205 | 1205 | self.log('develwarn', '%s at:\n%s' % |
|
1206 | 1206 | (msg, ''.join(util.getstackframes(stacklevel)))) |
|
1207 | 1207 | else: |
|
1208 | 1208 | curframe = inspect.currentframe() |
|
1209 | 1209 | calframe = inspect.getouterframes(curframe, 2) |
|
1210 | 1210 | self.write_err('%s at: %s:%s (%s)\n' |
|
1211 | 1211 | % ((msg,) + calframe[stacklevel][1:4])) |
|
1212 | 1212 | self.log('develwarn', '%s at: %s:%s (%s)\n', |
|
1213 | 1213 | msg, *calframe[stacklevel][1:4]) |
|
1214 | 1214 | curframe = calframe = None # avoid cycles |
|
1215 | 1215 | |
|
1216 | 1216 | def deprecwarn(self, msg, version): |
|
1217 | 1217 | """issue a deprecation warning |
|
1218 | 1218 | |
|
1219 | 1219 | - msg: message explaining what is deprecated and how to upgrade, |
|
1220 | 1220 | - version: last version where the API will be supported, |
|
1221 | 1221 | """ |
|
1222 | 1222 | if not (self.configbool('devel', 'all-warnings') |
|
1223 | 1223 | or self.configbool('devel', 'deprec-warn')): |
|
1224 | 1224 | return |
|
1225 | 1225 | msg += ("\n(compatibility will be dropped after Mercurial-%s," |
|
1226 | 1226 | " update your code.)") % version |
|
1227 | 1227 | self.develwarn(msg, stacklevel=2, config='deprec-warn') |
|
1228 | 1228 | |
|
1229 | 1229 | def exportableenviron(self): |
|
1230 | 1230 | """The environment variables that are safe to export, e.g. through |
|
1231 | 1231 | hgweb. |
|
1232 | 1232 | """ |
|
1233 | 1233 | return self._exportableenviron |
|
1234 | 1234 | |
|
1235 | 1235 | @contextlib.contextmanager |
|
1236 | 1236 | def configoverride(self, overrides, source=""): |
|
1237 | 1237 | """Context manager for temporary config overrides |
|
1238 | 1238 | `overrides` must be a dict of the following structure: |
|
1239 | 1239 | {(section, name) : value}""" |
|
1240 | 1240 | backups = {} |
|
1241 | 1241 | try: |
|
1242 | 1242 | for (section, name), value in overrides.items(): |
|
1243 | 1243 | backups[(section, name)] = self.backupconfig(section, name) |
|
1244 | 1244 | self.setconfig(section, name, value, source) |
|
1245 | 1245 | yield |
|
1246 | 1246 | finally: |
|
1247 | 1247 | for __, backup in backups.items(): |
|
1248 | 1248 | self.restoreconfig(backup) |
|
1249 | 1249 | # just restoring ui.quiet config to the previous value is not enough |
|
1250 | 1250 | # as it does not update ui.quiet class member |
|
1251 | 1251 | if ('ui', 'quiet') in overrides: |
|
1252 | 1252 | self.fixconfig(section='ui') |
|
1253 | 1253 | |
|
1254 | 1254 | class paths(dict): |
|
1255 | 1255 | """Represents a collection of paths and their configs. |
|
1256 | 1256 | |
|
1257 | 1257 | Data is initially derived from ui instances and the config files they have |
|
1258 | 1258 | loaded. |
|
1259 | 1259 | """ |
|
1260 | 1260 | def __init__(self, ui): |
|
1261 | 1261 | dict.__init__(self) |
|
1262 | 1262 | |
|
1263 | 1263 | for name, loc in ui.configitems('paths', ignoresub=True): |
|
1264 | 1264 | # No location is the same as not existing. |
|
1265 | 1265 | if not loc: |
|
1266 | 1266 | continue |
|
1267 | 1267 | loc, sub = ui.configsuboptions('paths', name) |
|
1268 | 1268 | self[name] = path(ui, name, rawloc=loc, suboptions=sub) |
|
1269 | 1269 | |
|
1270 | 1270 | def getpath(self, name, default=None): |
|
1271 | 1271 | """Return a ``path`` from a string, falling back to default. |
|
1272 | 1272 | |
|
1273 | 1273 | ``name`` can be a named path or locations. Locations are filesystem |
|
1274 | 1274 | paths or URIs. |
|
1275 | 1275 | |
|
1276 | 1276 | Returns None if ``name`` is not a registered path, a URI, or a local |
|
1277 | 1277 | path to a repo. |
|
1278 | 1278 | """ |
|
1279 | 1279 | # Only fall back to default if no path was requested. |
|
1280 | 1280 | if name is None: |
|
1281 | 1281 | if not default: |
|
1282 | 1282 | default = () |
|
1283 | 1283 | elif not isinstance(default, (tuple, list)): |
|
1284 | 1284 | default = (default,) |
|
1285 | 1285 | for k in default: |
|
1286 | 1286 | try: |
|
1287 | 1287 | return self[k] |
|
1288 | 1288 | except KeyError: |
|
1289 | 1289 | continue |
|
1290 | 1290 | return None |
|
1291 | 1291 | |
|
1292 | 1292 | # Most likely empty string. |
|
1293 | 1293 | # This may need to raise in the future. |
|
1294 | 1294 | if not name: |
|
1295 | 1295 | return None |
|
1296 | 1296 | |
|
1297 | 1297 | try: |
|
1298 | 1298 | return self[name] |
|
1299 | 1299 | except KeyError: |
|
1300 | 1300 | # Try to resolve as a local path or URI. |
|
1301 | 1301 | try: |
|
1302 | 1302 | # We don't pass sub-options in, so no need to pass ui instance. |
|
1303 | 1303 | return path(None, None, rawloc=name) |
|
1304 | 1304 | except ValueError: |
|
1305 | 1305 | raise error.RepoError(_('repository %s does not exist') % |
|
1306 | 1306 | name) |
|
1307 | 1307 | |
|
1308 | 1308 | _pathsuboptions = {} |
|
1309 | 1309 | |
|
1310 | 1310 | def pathsuboption(option, attr): |
|
1311 | 1311 | """Decorator used to declare a path sub-option. |
|
1312 | 1312 | |
|
1313 | 1313 | Arguments are the sub-option name and the attribute it should set on |
|
1314 | 1314 | ``path`` instances. |
|
1315 | 1315 | |
|
1316 | 1316 | The decorated function will receive as arguments a ``ui`` instance, |
|
1317 | 1317 | ``path`` instance, and the string value of this option from the config. |
|
1318 | 1318 | The function should return the value that will be set on the ``path`` |
|
1319 | 1319 | instance. |
|
1320 | 1320 | |
|
1321 | 1321 | This decorator can be used to perform additional verification of |
|
1322 | 1322 | sub-options and to change the type of sub-options. |
|
1323 | 1323 | """ |
|
1324 | 1324 | def register(func): |
|
1325 | 1325 | _pathsuboptions[option] = (attr, func) |
|
1326 | 1326 | return func |
|
1327 | 1327 | return register |
|
1328 | 1328 | |
|
1329 | 1329 | @pathsuboption('pushurl', 'pushloc') |
|
1330 | 1330 | def pushurlpathoption(ui, path, value): |
|
1331 | 1331 | u = util.url(value) |
|
1332 | 1332 | # Actually require a URL. |
|
1333 | 1333 | if not u.scheme: |
|
1334 | 1334 | ui.warn(_('(paths.%s:pushurl not a URL; ignoring)\n') % path.name) |
|
1335 | 1335 | return None |
|
1336 | 1336 | |
|
1337 | 1337 | # Don't support the #foo syntax in the push URL to declare branch to |
|
1338 | 1338 | # push. |
|
1339 | 1339 | if u.fragment: |
|
1340 | 1340 | ui.warn(_('("#fragment" in paths.%s:pushurl not supported; ' |
|
1341 | 1341 | 'ignoring)\n') % path.name) |
|
1342 | 1342 | u.fragment = None |
|
1343 | 1343 | |
|
1344 | 1344 | return str(u) |
|
1345 | 1345 | |
|
1346 | 1346 | @pathsuboption('pushrev', 'pushrev') |
|
1347 | 1347 | def pushrevpathoption(ui, path, value): |
|
1348 | 1348 | return value |
|
1349 | 1349 | |
|
1350 | 1350 | class path(object): |
|
1351 | 1351 | """Represents an individual path and its configuration.""" |
|
1352 | 1352 | |
|
1353 | 1353 | def __init__(self, ui, name, rawloc=None, suboptions=None): |
|
1354 | 1354 | """Construct a path from its config options. |
|
1355 | 1355 | |
|
1356 | 1356 | ``ui`` is the ``ui`` instance the path is coming from. |
|
1357 | 1357 | ``name`` is the symbolic name of the path. |
|
1358 | 1358 | ``rawloc`` is the raw location, as defined in the config. |
|
1359 | 1359 | ``pushloc`` is the raw locations pushes should be made to. |
|
1360 | 1360 | |
|
1361 | 1361 | If ``name`` is not defined, we require that the location be a) a local |
|
1362 | 1362 | filesystem path with a .hg directory or b) a URL. If not, |
|
1363 | 1363 | ``ValueError`` is raised. |
|
1364 | 1364 | """ |
|
1365 | 1365 | if not rawloc: |
|
1366 | 1366 | raise ValueError('rawloc must be defined') |
|
1367 | 1367 | |
|
1368 | 1368 | # Locations may define branches via syntax <base>#<branch>. |
|
1369 | 1369 | u = util.url(rawloc) |
|
1370 | 1370 | branch = None |
|
1371 | 1371 | if u.fragment: |
|
1372 | 1372 | branch = u.fragment |
|
1373 | 1373 | u.fragment = None |
|
1374 | 1374 | |
|
1375 | 1375 | self.url = u |
|
1376 | 1376 | self.branch = branch |
|
1377 | 1377 | |
|
1378 | 1378 | self.name = name |
|
1379 | 1379 | self.rawloc = rawloc |
|
1380 | 1380 | self.loc = str(u) |
|
1381 | 1381 | |
|
1382 | 1382 | # When given a raw location but not a symbolic name, validate the |
|
1383 | 1383 | # location is valid. |
|
1384 | 1384 | if not name and not u.scheme and not self._isvalidlocalpath(self.loc): |
|
1385 | 1385 | raise ValueError('location is not a URL or path to a local ' |
|
1386 | 1386 | 'repo: %s' % rawloc) |
|
1387 | 1387 | |
|
1388 | 1388 | suboptions = suboptions or {} |
|
1389 | 1389 | |
|
1390 | 1390 | # Now process the sub-options. If a sub-option is registered, its |
|
1391 | 1391 | # attribute will always be present. The value will be None if there |
|
1392 | 1392 | # was no valid sub-option. |
|
1393 | 1393 | for suboption, (attr, func) in _pathsuboptions.iteritems(): |
|
1394 | 1394 | if suboption not in suboptions: |
|
1395 | 1395 | setattr(self, attr, None) |
|
1396 | 1396 | continue |
|
1397 | 1397 | |
|
1398 | 1398 | value = func(ui, self, suboptions[suboption]) |
|
1399 | 1399 | setattr(self, attr, value) |
|
1400 | 1400 | |
|
1401 | 1401 | def _isvalidlocalpath(self, path): |
|
1402 | 1402 | """Returns True if the given path is a potentially valid repository. |
|
1403 | 1403 | This is its own function so that extensions can change the definition of |
|
1404 | 1404 | 'valid' in this case (like when pulling from a git repo into a hg |
|
1405 | 1405 | one).""" |
|
1406 | 1406 | return os.path.isdir(os.path.join(path, '.hg')) |
|
1407 | 1407 | |
|
1408 | 1408 | @property |
|
1409 | 1409 | def suboptions(self): |
|
1410 | 1410 | """Return sub-options and their values for this path. |
|
1411 | 1411 | |
|
1412 | 1412 | This is intended to be used for presentation purposes. |
|
1413 | 1413 | """ |
|
1414 | 1414 | d = {} |
|
1415 | 1415 | for subopt, (attr, _func) in _pathsuboptions.iteritems(): |
|
1416 | 1416 | value = getattr(self, attr) |
|
1417 | 1417 | if value is not None: |
|
1418 | 1418 | d[subopt] = value |
|
1419 | 1419 | return d |
|
1420 | 1420 | |
|
1421 | 1421 | # we instantiate one globally shared progress bar to avoid |
|
1422 | 1422 | # competing progress bars when multiple UI objects get created |
|
1423 | 1423 | _progresssingleton = None |
|
1424 | 1424 | |
|
1425 | 1425 | def getprogbar(ui): |
|
1426 | 1426 | global _progresssingleton |
|
1427 | 1427 | if _progresssingleton is None: |
|
1428 | 1428 | # passing 'ui' object to the singleton is fishy, |
|
1429 | 1429 | # this is how the extension used to work but feel free to rework it. |
|
1430 | 1430 | _progresssingleton = progress.progbar(ui) |
|
1431 | 1431 | return _progresssingleton |
@@ -1,3551 +1,3551 b'' | |||
|
1 | 1 | # util.py - Mercurial utility functions and platform specific implementations |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005 K. Thananchayan <thananck@yahoo.com> |
|
4 | 4 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> |
|
6 | 6 | # |
|
7 | 7 | # This software may be used and distributed according to the terms of the |
|
8 | 8 | # GNU General Public License version 2 or any later version. |
|
9 | 9 | |
|
10 | 10 | """Mercurial utility functions and platform specific implementations. |
|
11 | 11 | |
|
12 | 12 | This contains helper routines that are independent of the SCM core and |
|
13 | 13 | hide platform-specific details from the core. |
|
14 | 14 | """ |
|
15 | 15 | |
|
16 | 16 | from __future__ import absolute_import |
|
17 | 17 | |
|
18 | 18 | import bz2 |
|
19 | 19 | import calendar |
|
20 | 20 | import collections |
|
21 | 21 | import datetime |
|
22 | 22 | import errno |
|
23 | 23 | import gc |
|
24 | 24 | import hashlib |
|
25 | 25 | import imp |
|
26 | 26 | import os |
|
27 | 27 | import platform as pyplatform |
|
28 | 28 | import re as remod |
|
29 | 29 | import shutil |
|
30 | 30 | import signal |
|
31 | 31 | import socket |
|
32 | 32 | import stat |
|
33 | 33 | import string |
|
34 | 34 | import subprocess |
|
35 | 35 | import sys |
|
36 | 36 | import tempfile |
|
37 | 37 | import textwrap |
|
38 | 38 | import time |
|
39 | 39 | import traceback |
|
40 | 40 | import zlib |
|
41 | 41 | |
|
42 | 42 | from . import ( |
|
43 | 43 | encoding, |
|
44 | 44 | error, |
|
45 | 45 | i18n, |
|
46 | 46 | osutil, |
|
47 | 47 | parsers, |
|
48 | 48 | pycompat, |
|
49 | 49 | ) |
|
50 | 50 | |
|
51 | 51 | empty = pycompat.empty |
|
52 | 52 | httplib = pycompat.httplib |
|
53 | 53 | httpserver = pycompat.httpserver |
|
54 | 54 | pickle = pycompat.pickle |
|
55 | 55 | queue = pycompat.queue |
|
56 | 56 | socketserver = pycompat.socketserver |
|
57 | 57 | stderr = pycompat.stderr |
|
58 | 58 | stdin = pycompat.stdin |
|
59 | 59 | stdout = pycompat.stdout |
|
60 | 60 | stringio = pycompat.stringio |
|
61 | 61 | urlerr = pycompat.urlerr |
|
62 | 62 | urlparse = pycompat.urlparse |
|
63 | 63 | urlreq = pycompat.urlreq |
|
64 | 64 | xmlrpclib = pycompat.xmlrpclib |
|
65 | 65 | |
|
66 | 66 | def isatty(fp): |
|
67 | 67 | try: |
|
68 | 68 | return fp.isatty() |
|
69 | 69 | except AttributeError: |
|
70 | 70 | return False |
|
71 | 71 | |
|
72 | 72 | # glibc determines buffering on first write to stdout - if we replace a TTY |
|
73 | 73 | # destined stdout with a pipe destined stdout (e.g. pager), we want line |
|
74 | 74 | # buffering |
|
75 | 75 | if isatty(stdout): |
|
76 | 76 | stdout = os.fdopen(stdout.fileno(), 'wb', 1) |
|
77 | 77 | |
|
78 | 78 | if pycompat.osname == 'nt': |
|
79 | 79 | from . import windows as platform |
|
80 | 80 | stdout = platform.winstdout(stdout) |
|
81 | 81 | else: |
|
82 | 82 | from . import posix as platform |
|
83 | 83 | |
|
84 | 84 | _ = i18n._ |
|
85 | 85 | |
|
86 | 86 | bindunixsocket = platform.bindunixsocket |
|
87 | 87 | cachestat = platform.cachestat |
|
88 | 88 | checkexec = platform.checkexec |
|
89 | 89 | checklink = platform.checklink |
|
90 | 90 | copymode = platform.copymode |
|
91 | 91 | executablepath = platform.executablepath |
|
92 | 92 | expandglobs = platform.expandglobs |
|
93 | 93 | explainexit = platform.explainexit |
|
94 | 94 | findexe = platform.findexe |
|
95 | 95 | gethgcmd = platform.gethgcmd |
|
96 | 96 | getuser = platform.getuser |
|
97 | 97 | getpid = os.getpid |
|
98 | 98 | groupmembers = platform.groupmembers |
|
99 | 99 | groupname = platform.groupname |
|
100 | 100 | hidewindow = platform.hidewindow |
|
101 | 101 | isexec = platform.isexec |
|
102 | 102 | isowner = platform.isowner |
|
103 | 103 | localpath = platform.localpath |
|
104 | 104 | lookupreg = platform.lookupreg |
|
105 | 105 | makedir = platform.makedir |
|
106 | 106 | nlinks = platform.nlinks |
|
107 | 107 | normpath = platform.normpath |
|
108 | 108 | normcase = platform.normcase |
|
109 | 109 | normcasespec = platform.normcasespec |
|
110 | 110 | normcasefallback = platform.normcasefallback |
|
111 | 111 | openhardlinks = platform.openhardlinks |
|
112 | 112 | oslink = platform.oslink |
|
113 | 113 | parsepatchoutput = platform.parsepatchoutput |
|
114 | 114 | pconvert = platform.pconvert |
|
115 | 115 | poll = platform.poll |
|
116 | 116 | popen = platform.popen |
|
117 | 117 | posixfile = platform.posixfile |
|
118 | 118 | quotecommand = platform.quotecommand |
|
119 | 119 | readpipe = platform.readpipe |
|
120 | 120 | rename = platform.rename |
|
121 | 121 | removedirs = platform.removedirs |
|
122 | 122 | samedevice = platform.samedevice |
|
123 | 123 | samefile = platform.samefile |
|
124 | 124 | samestat = platform.samestat |
|
125 | 125 | setbinary = platform.setbinary |
|
126 | 126 | setflags = platform.setflags |
|
127 | 127 | setsignalhandler = platform.setsignalhandler |
|
128 | 128 | shellquote = platform.shellquote |
|
129 | 129 | spawndetached = platform.spawndetached |
|
130 | 130 | split = platform.split |
|
131 | 131 | sshargs = platform.sshargs |
|
132 | 132 | statfiles = getattr(osutil, 'statfiles', platform.statfiles) |
|
133 | 133 | statisexec = platform.statisexec |
|
134 | 134 | statislink = platform.statislink |
|
135 | 135 | testpid = platform.testpid |
|
136 | 136 | umask = platform.umask |
|
137 | 137 | unlink = platform.unlink |
|
138 | 138 | unlinkpath = platform.unlinkpath |
|
139 | 139 | username = platform.username |
|
140 | 140 | |
|
141 | 141 | # Python compatibility |
|
142 | 142 | |
|
143 | 143 | _notset = object() |
|
144 | 144 | |
|
145 | 145 | # disable Python's problematic floating point timestamps (issue4836) |
|
146 | 146 | # (Python hypocritically says you shouldn't change this behavior in |
|
147 | 147 | # libraries, and sure enough Mercurial is not a library.) |
|
148 | 148 | os.stat_float_times(False) |
|
149 | 149 | |
|
150 | 150 | def safehasattr(thing, attr): |
|
151 | 151 | return getattr(thing, attr, _notset) is not _notset |
|
152 | 152 | |
|
153 | 153 | def bitsfrom(container): |
|
154 | 154 | bits = 0 |
|
155 | 155 | for bit in container: |
|
156 | 156 | bits |= bit |
|
157 | 157 | return bits |
|
158 | 158 | |
|
159 | 159 | DIGESTS = { |
|
160 | 160 | 'md5': hashlib.md5, |
|
161 | 161 | 'sha1': hashlib.sha1, |
|
162 | 162 | 'sha512': hashlib.sha512, |
|
163 | 163 | } |
|
164 | 164 | # List of digest types from strongest to weakest |
|
165 | 165 | DIGESTS_BY_STRENGTH = ['sha512', 'sha1', 'md5'] |
|
166 | 166 | |
|
167 | 167 | for k in DIGESTS_BY_STRENGTH: |
|
168 | 168 | assert k in DIGESTS |
|
169 | 169 | |
|
170 | 170 | class digester(object): |
|
171 | 171 | """helper to compute digests. |
|
172 | 172 | |
|
173 | 173 | This helper can be used to compute one or more digests given their name. |
|
174 | 174 | |
|
175 | 175 | >>> d = digester(['md5', 'sha1']) |
|
176 | 176 | >>> d.update('foo') |
|
177 | 177 | >>> [k for k in sorted(d)] |
|
178 | 178 | ['md5', 'sha1'] |
|
179 | 179 | >>> d['md5'] |
|
180 | 180 | 'acbd18db4cc2f85cedef654fccc4a4d8' |
|
181 | 181 | >>> d['sha1'] |
|
182 | 182 | '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33' |
|
183 | 183 | >>> digester.preferred(['md5', 'sha1']) |
|
184 | 184 | 'sha1' |
|
185 | 185 | """ |
|
186 | 186 | |
|
187 | 187 | def __init__(self, digests, s=''): |
|
188 | 188 | self._hashes = {} |
|
189 | 189 | for k in digests: |
|
190 | 190 | if k not in DIGESTS: |
|
191 | 191 | raise Abort(_('unknown digest type: %s') % k) |
|
192 | 192 | self._hashes[k] = DIGESTS[k]() |
|
193 | 193 | if s: |
|
194 | 194 | self.update(s) |
|
195 | 195 | |
|
196 | 196 | def update(self, data): |
|
197 | 197 | for h in self._hashes.values(): |
|
198 | 198 | h.update(data) |
|
199 | 199 | |
|
200 | 200 | def __getitem__(self, key): |
|
201 | 201 | if key not in DIGESTS: |
|
202 | 202 | raise Abort(_('unknown digest type: %s') % k) |
|
203 | 203 | return self._hashes[key].hexdigest() |
|
204 | 204 | |
|
205 | 205 | def __iter__(self): |
|
206 | 206 | return iter(self._hashes) |
|
207 | 207 | |
|
208 | 208 | @staticmethod |
|
209 | 209 | def preferred(supported): |
|
210 | 210 | """returns the strongest digest type in both supported and DIGESTS.""" |
|
211 | 211 | |
|
212 | 212 | for k in DIGESTS_BY_STRENGTH: |
|
213 | 213 | if k in supported: |
|
214 | 214 | return k |
|
215 | 215 | return None |
|
216 | 216 | |
|
217 | 217 | class digestchecker(object): |
|
218 | 218 | """file handle wrapper that additionally checks content against a given |
|
219 | 219 | size and digests. |
|
220 | 220 | |
|
221 | 221 | d = digestchecker(fh, size, {'md5': '...'}) |
|
222 | 222 | |
|
223 | 223 | When multiple digests are given, all of them are validated. |
|
224 | 224 | """ |
|
225 | 225 | |
|
226 | 226 | def __init__(self, fh, size, digests): |
|
227 | 227 | self._fh = fh |
|
228 | 228 | self._size = size |
|
229 | 229 | self._got = 0 |
|
230 | 230 | self._digests = dict(digests) |
|
231 | 231 | self._digester = digester(self._digests.keys()) |
|
232 | 232 | |
|
233 | 233 | def read(self, length=-1): |
|
234 | 234 | content = self._fh.read(length) |
|
235 | 235 | self._digester.update(content) |
|
236 | 236 | self._got += len(content) |
|
237 | 237 | return content |
|
238 | 238 | |
|
239 | 239 | def validate(self): |
|
240 | 240 | if self._size != self._got: |
|
241 | 241 | raise Abort(_('size mismatch: expected %d, got %d') % |
|
242 | 242 | (self._size, self._got)) |
|
243 | 243 | for k, v in self._digests.items(): |
|
244 | 244 | if v != self._digester[k]: |
|
245 | 245 | # i18n: first parameter is a digest name |
|
246 | 246 | raise Abort(_('%s mismatch: expected %s, got %s') % |
|
247 | 247 | (k, v, self._digester[k])) |
|
248 | 248 | |
|
249 | 249 | try: |
|
250 | 250 | buffer = buffer |
|
251 | 251 | except NameError: |
|
252 | 252 | if not pycompat.ispy3: |
|
253 | 253 | def buffer(sliceable, offset=0, length=None): |
|
254 | 254 | if length is not None: |
|
255 | 255 | return sliceable[offset:offset + length] |
|
256 | 256 | return sliceable[offset:] |
|
257 | 257 | else: |
|
258 | 258 | def buffer(sliceable, offset=0, length=None): |
|
259 | 259 | if length is not None: |
|
260 | 260 | return memoryview(sliceable)[offset:offset + length] |
|
261 | 261 | return memoryview(sliceable)[offset:] |
|
262 | 262 | |
|
263 | 263 | closefds = pycompat.osname == 'posix' |
|
264 | 264 | |
|
265 | 265 | _chunksize = 4096 |
|
266 | 266 | |
|
267 | 267 | class bufferedinputpipe(object): |
|
268 | 268 | """a manually buffered input pipe |
|
269 | 269 | |
|
270 | 270 | Python will not let us use buffered IO and lazy reading with 'polling' at |
|
271 | 271 | the same time. We cannot probe the buffer state and select will not detect |
|
272 | 272 | that data are ready to read if they are already buffered. |
|
273 | 273 | |
|
274 | 274 | This class let us work around that by implementing its own buffering |
|
275 | 275 | (allowing efficient readline) while offering a way to know if the buffer is |
|
276 | 276 | empty from the output (allowing collaboration of the buffer with polling). |
|
277 | 277 | |
|
278 | 278 | This class lives in the 'util' module because it makes use of the 'os' |
|
279 | 279 | module from the python stdlib. |
|
280 | 280 | """ |
|
281 | 281 | |
|
282 | 282 | def __init__(self, input): |
|
283 | 283 | self._input = input |
|
284 | 284 | self._buffer = [] |
|
285 | 285 | self._eof = False |
|
286 | 286 | self._lenbuf = 0 |
|
287 | 287 | |
|
288 | 288 | @property |
|
289 | 289 | def hasbuffer(self): |
|
290 | 290 | """True is any data is currently buffered |
|
291 | 291 | |
|
292 | 292 | This will be used externally a pre-step for polling IO. If there is |
|
293 | 293 | already data then no polling should be set in place.""" |
|
294 | 294 | return bool(self._buffer) |
|
295 | 295 | |
|
296 | 296 | @property |
|
297 | 297 | def closed(self): |
|
298 | 298 | return self._input.closed |
|
299 | 299 | |
|
300 | 300 | def fileno(self): |
|
301 | 301 | return self._input.fileno() |
|
302 | 302 | |
|
303 | 303 | def close(self): |
|
304 | 304 | return self._input.close() |
|
305 | 305 | |
|
306 | 306 | def read(self, size): |
|
307 | 307 | while (not self._eof) and (self._lenbuf < size): |
|
308 | 308 | self._fillbuffer() |
|
309 | 309 | return self._frombuffer(size) |
|
310 | 310 | |
|
311 | 311 | def readline(self, *args, **kwargs): |
|
312 | 312 | if 1 < len(self._buffer): |
|
313 | 313 | # this should not happen because both read and readline end with a |
|
314 | 314 | # _frombuffer call that collapse it. |
|
315 | 315 | self._buffer = [''.join(self._buffer)] |
|
316 | 316 | self._lenbuf = len(self._buffer[0]) |
|
317 | 317 | lfi = -1 |
|
318 | 318 | if self._buffer: |
|
319 | 319 | lfi = self._buffer[-1].find('\n') |
|
320 | 320 | while (not self._eof) and lfi < 0: |
|
321 | 321 | self._fillbuffer() |
|
322 | 322 | if self._buffer: |
|
323 | 323 | lfi = self._buffer[-1].find('\n') |
|
324 | 324 | size = lfi + 1 |
|
325 | 325 | if lfi < 0: # end of file |
|
326 | 326 | size = self._lenbuf |
|
327 | 327 | elif 1 < len(self._buffer): |
|
328 | 328 | # we need to take previous chunks into account |
|
329 | 329 | size += self._lenbuf - len(self._buffer[-1]) |
|
330 | 330 | return self._frombuffer(size) |
|
331 | 331 | |
|
332 | 332 | def _frombuffer(self, size): |
|
333 | 333 | """return at most 'size' data from the buffer |
|
334 | 334 | |
|
335 | 335 | The data are removed from the buffer.""" |
|
336 | 336 | if size == 0 or not self._buffer: |
|
337 | 337 | return '' |
|
338 | 338 | buf = self._buffer[0] |
|
339 | 339 | if 1 < len(self._buffer): |
|
340 | 340 | buf = ''.join(self._buffer) |
|
341 | 341 | |
|
342 | 342 | data = buf[:size] |
|
343 | 343 | buf = buf[len(data):] |
|
344 | 344 | if buf: |
|
345 | 345 | self._buffer = [buf] |
|
346 | 346 | self._lenbuf = len(buf) |
|
347 | 347 | else: |
|
348 | 348 | self._buffer = [] |
|
349 | 349 | self._lenbuf = 0 |
|
350 | 350 | return data |
|
351 | 351 | |
|
352 | 352 | def _fillbuffer(self): |
|
353 | 353 | """read data to the buffer""" |
|
354 | 354 | data = os.read(self._input.fileno(), _chunksize) |
|
355 | 355 | if not data: |
|
356 | 356 | self._eof = True |
|
357 | 357 | else: |
|
358 | 358 | self._lenbuf += len(data) |
|
359 | 359 | self._buffer.append(data) |
|
360 | 360 | |
|
361 | 361 | def popen2(cmd, env=None, newlines=False): |
|
362 | 362 | # Setting bufsize to -1 lets the system decide the buffer size. |
|
363 | 363 | # The default for bufsize is 0, meaning unbuffered. This leads to |
|
364 | 364 | # poor performance on Mac OS X: http://bugs.python.org/issue4194 |
|
365 | 365 | p = subprocess.Popen(cmd, shell=True, bufsize=-1, |
|
366 | 366 | close_fds=closefds, |
|
367 | 367 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
368 | 368 | universal_newlines=newlines, |
|
369 | 369 | env=env) |
|
370 | 370 | return p.stdin, p.stdout |
|
371 | 371 | |
|
372 | 372 | def popen3(cmd, env=None, newlines=False): |
|
373 | 373 | stdin, stdout, stderr, p = popen4(cmd, env, newlines) |
|
374 | 374 | return stdin, stdout, stderr |
|
375 | 375 | |
|
376 | 376 | def popen4(cmd, env=None, newlines=False, bufsize=-1): |
|
377 | 377 | p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, |
|
378 | 378 | close_fds=closefds, |
|
379 | 379 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
|
380 | 380 | stderr=subprocess.PIPE, |
|
381 | 381 | universal_newlines=newlines, |
|
382 | 382 | env=env) |
|
383 | 383 | return p.stdin, p.stdout, p.stderr, p |
|
384 | 384 | |
|
385 | 385 | def version(): |
|
386 | 386 | """Return version information if available.""" |
|
387 | 387 | try: |
|
388 | 388 | from . import __version__ |
|
389 | 389 | return __version__.version |
|
390 | 390 | except ImportError: |
|
391 | 391 | return 'unknown' |
|
392 | 392 | |
|
393 | 393 | def versiontuple(v=None, n=4): |
|
394 | 394 | """Parses a Mercurial version string into an N-tuple. |
|
395 | 395 | |
|
396 | 396 | The version string to be parsed is specified with the ``v`` argument. |
|
397 | 397 | If it isn't defined, the current Mercurial version string will be parsed. |
|
398 | 398 | |
|
399 | 399 | ``n`` can be 2, 3, or 4. Here is how some version strings map to |
|
400 | 400 | returned values: |
|
401 | 401 | |
|
402 | 402 | >>> v = '3.6.1+190-df9b73d2d444' |
|
403 | 403 | >>> versiontuple(v, 2) |
|
404 | 404 | (3, 6) |
|
405 | 405 | >>> versiontuple(v, 3) |
|
406 | 406 | (3, 6, 1) |
|
407 | 407 | >>> versiontuple(v, 4) |
|
408 | 408 | (3, 6, 1, '190-df9b73d2d444') |
|
409 | 409 | |
|
410 | 410 | >>> versiontuple('3.6.1+190-df9b73d2d444+20151118') |
|
411 | 411 | (3, 6, 1, '190-df9b73d2d444+20151118') |
|
412 | 412 | |
|
413 | 413 | >>> v = '3.6' |
|
414 | 414 | >>> versiontuple(v, 2) |
|
415 | 415 | (3, 6) |
|
416 | 416 | >>> versiontuple(v, 3) |
|
417 | 417 | (3, 6, None) |
|
418 | 418 | >>> versiontuple(v, 4) |
|
419 | 419 | (3, 6, None, None) |
|
420 | 420 | |
|
421 | 421 | >>> v = '3.9-rc' |
|
422 | 422 | >>> versiontuple(v, 2) |
|
423 | 423 | (3, 9) |
|
424 | 424 | >>> versiontuple(v, 3) |
|
425 | 425 | (3, 9, None) |
|
426 | 426 | >>> versiontuple(v, 4) |
|
427 | 427 | (3, 9, None, 'rc') |
|
428 | 428 | |
|
429 | 429 | >>> v = '3.9-rc+2-02a8fea4289b' |
|
430 | 430 | >>> versiontuple(v, 2) |
|
431 | 431 | (3, 9) |
|
432 | 432 | >>> versiontuple(v, 3) |
|
433 | 433 | (3, 9, None) |
|
434 | 434 | >>> versiontuple(v, 4) |
|
435 | 435 | (3, 9, None, 'rc+2-02a8fea4289b') |
|
436 | 436 | """ |
|
437 | 437 | if not v: |
|
438 | 438 | v = version() |
|
439 | 439 | parts = remod.split('[\+-]', v, 1) |
|
440 | 440 | if len(parts) == 1: |
|
441 | 441 | vparts, extra = parts[0], None |
|
442 | 442 | else: |
|
443 | 443 | vparts, extra = parts |
|
444 | 444 | |
|
445 | 445 | vints = [] |
|
446 | 446 | for i in vparts.split('.'): |
|
447 | 447 | try: |
|
448 | 448 | vints.append(int(i)) |
|
449 | 449 | except ValueError: |
|
450 | 450 | break |
|
451 | 451 | # (3, 6) -> (3, 6, None) |
|
452 | 452 | while len(vints) < 3: |
|
453 | 453 | vints.append(None) |
|
454 | 454 | |
|
455 | 455 | if n == 2: |
|
456 | 456 | return (vints[0], vints[1]) |
|
457 | 457 | if n == 3: |
|
458 | 458 | return (vints[0], vints[1], vints[2]) |
|
459 | 459 | if n == 4: |
|
460 | 460 | return (vints[0], vints[1], vints[2], extra) |
|
461 | 461 | |
|
462 | 462 | # used by parsedate |
|
463 | 463 | defaultdateformats = ( |
|
464 | 464 | '%Y-%m-%dT%H:%M:%S', # the 'real' ISO8601 |
|
465 | 465 | '%Y-%m-%dT%H:%M', # without seconds |
|
466 | 466 | '%Y-%m-%dT%H%M%S', # another awful but legal variant without : |
|
467 | 467 | '%Y-%m-%dT%H%M', # without seconds |
|
468 | 468 | '%Y-%m-%d %H:%M:%S', # our common legal variant |
|
469 | 469 | '%Y-%m-%d %H:%M', # without seconds |
|
470 | 470 | '%Y-%m-%d %H%M%S', # without : |
|
471 | 471 | '%Y-%m-%d %H%M', # without seconds |
|
472 | 472 | '%Y-%m-%d %I:%M:%S%p', |
|
473 | 473 | '%Y-%m-%d %H:%M', |
|
474 | 474 | '%Y-%m-%d %I:%M%p', |
|
475 | 475 | '%Y-%m-%d', |
|
476 | 476 | '%m-%d', |
|
477 | 477 | '%m/%d', |
|
478 | 478 | '%m/%d/%y', |
|
479 | 479 | '%m/%d/%Y', |
|
480 | 480 | '%a %b %d %H:%M:%S %Y', |
|
481 | 481 | '%a %b %d %I:%M:%S%p %Y', |
|
482 | 482 | '%a, %d %b %Y %H:%M:%S', # GNU coreutils "/bin/date --rfc-2822" |
|
483 | 483 | '%b %d %H:%M:%S %Y', |
|
484 | 484 | '%b %d %I:%M:%S%p %Y', |
|
485 | 485 | '%b %d %H:%M:%S', |
|
486 | 486 | '%b %d %I:%M:%S%p', |
|
487 | 487 | '%b %d %H:%M', |
|
488 | 488 | '%b %d %I:%M%p', |
|
489 | 489 | '%b %d %Y', |
|
490 | 490 | '%b %d', |
|
491 | 491 | '%H:%M:%S', |
|
492 | 492 | '%I:%M:%S%p', |
|
493 | 493 | '%H:%M', |
|
494 | 494 | '%I:%M%p', |
|
495 | 495 | ) |
|
496 | 496 | |
|
497 | 497 | extendeddateformats = defaultdateformats + ( |
|
498 | 498 | "%Y", |
|
499 | 499 | "%Y-%m", |
|
500 | 500 | "%b", |
|
501 | 501 | "%b %Y", |
|
502 | 502 | ) |
|
503 | 503 | |
|
504 | 504 | def cachefunc(func): |
|
505 | 505 | '''cache the result of function calls''' |
|
506 | 506 | # XXX doesn't handle keywords args |
|
507 | 507 | if func.__code__.co_argcount == 0: |
|
508 | 508 | cache = [] |
|
509 | 509 | def f(): |
|
510 | 510 | if len(cache) == 0: |
|
511 | 511 | cache.append(func()) |
|
512 | 512 | return cache[0] |
|
513 | 513 | return f |
|
514 | 514 | cache = {} |
|
515 | 515 | if func.__code__.co_argcount == 1: |
|
516 | 516 | # we gain a small amount of time because |
|
517 | 517 | # we don't need to pack/unpack the list |
|
518 | 518 | def f(arg): |
|
519 | 519 | if arg not in cache: |
|
520 | 520 | cache[arg] = func(arg) |
|
521 | 521 | return cache[arg] |
|
522 | 522 | else: |
|
523 | 523 | def f(*args): |
|
524 | 524 | if args not in cache: |
|
525 | 525 | cache[args] = func(*args) |
|
526 | 526 | return cache[args] |
|
527 | 527 | |
|
528 | 528 | return f |
|
529 | 529 | |
|
530 | 530 | class sortdict(dict): |
|
531 | 531 | '''a simple sorted dictionary''' |
|
532 | 532 | def __init__(self, data=None): |
|
533 | 533 | self._list = [] |
|
534 | 534 | if data: |
|
535 | 535 | self.update(data) |
|
536 | 536 | def copy(self): |
|
537 | 537 | return sortdict(self) |
|
538 | 538 | def __setitem__(self, key, val): |
|
539 | 539 | if key in self: |
|
540 | 540 | self._list.remove(key) |
|
541 | 541 | self._list.append(key) |
|
542 | 542 | dict.__setitem__(self, key, val) |
|
543 | 543 | def __iter__(self): |
|
544 | 544 | return self._list.__iter__() |
|
545 | 545 | def update(self, src): |
|
546 | 546 | if isinstance(src, dict): |
|
547 | 547 | src = src.iteritems() |
|
548 | 548 | for k, v in src: |
|
549 | 549 | self[k] = v |
|
550 | 550 | def clear(self): |
|
551 | 551 | dict.clear(self) |
|
552 | 552 | self._list = [] |
|
553 | 553 | def items(self): |
|
554 | 554 | return [(k, self[k]) for k in self._list] |
|
555 | 555 | def __delitem__(self, key): |
|
556 | 556 | dict.__delitem__(self, key) |
|
557 | 557 | self._list.remove(key) |
|
558 | 558 | def pop(self, key, *args, **kwargs): |
|
559 | 559 | dict.pop(self, key, *args, **kwargs) |
|
560 | 560 | try: |
|
561 | 561 | self._list.remove(key) |
|
562 | 562 | except ValueError: |
|
563 | 563 | pass |
|
564 | 564 | def keys(self): |
|
565 | 565 | return self._list[:] |
|
566 | 566 | def iterkeys(self): |
|
567 | 567 | return self._list.__iter__() |
|
568 | 568 | def iteritems(self): |
|
569 | 569 | for k in self._list: |
|
570 | 570 | yield k, self[k] |
|
571 | 571 | def insert(self, index, key, val): |
|
572 | 572 | self._list.insert(index, key) |
|
573 | 573 | dict.__setitem__(self, key, val) |
|
574 | 574 | def __repr__(self): |
|
575 | 575 | if not self: |
|
576 | 576 | return '%s()' % self.__class__.__name__ |
|
577 | 577 | return '%s(%r)' % (self.__class__.__name__, self.items()) |
|
578 | 578 | |
|
579 | 579 | class _lrucachenode(object): |
|
580 | 580 | """A node in a doubly linked list. |
|
581 | 581 | |
|
582 | 582 | Holds a reference to nodes on either side as well as a key-value |
|
583 | 583 | pair for the dictionary entry. |
|
584 | 584 | """ |
|
585 | 585 | __slots__ = (u'next', u'prev', u'key', u'value') |
|
586 | 586 | |
|
587 | 587 | def __init__(self): |
|
588 | 588 | self.next = None |
|
589 | 589 | self.prev = None |
|
590 | 590 | |
|
591 | 591 | self.key = _notset |
|
592 | 592 | self.value = None |
|
593 | 593 | |
|
594 | 594 | def markempty(self): |
|
595 | 595 | """Mark the node as emptied.""" |
|
596 | 596 | self.key = _notset |
|
597 | 597 | |
|
598 | 598 | class lrucachedict(object): |
|
599 | 599 | """Dict that caches most recent accesses and sets. |
|
600 | 600 | |
|
601 | 601 | The dict consists of an actual backing dict - indexed by original |
|
602 | 602 | key - and a doubly linked circular list defining the order of entries in |
|
603 | 603 | the cache. |
|
604 | 604 | |
|
605 | 605 | The head node is the newest entry in the cache. If the cache is full, |
|
606 | 606 | we recycle head.prev and make it the new head. Cache accesses result in |
|
607 | 607 | the node being moved to before the existing head and being marked as the |
|
608 | 608 | new head node. |
|
609 | 609 | """ |
|
610 | 610 | def __init__(self, max): |
|
611 | 611 | self._cache = {} |
|
612 | 612 | |
|
613 | 613 | self._head = head = _lrucachenode() |
|
614 | 614 | head.prev = head |
|
615 | 615 | head.next = head |
|
616 | 616 | self._size = 1 |
|
617 | 617 | self._capacity = max |
|
618 | 618 | |
|
619 | 619 | def __len__(self): |
|
620 | 620 | return len(self._cache) |
|
621 | 621 | |
|
622 | 622 | def __contains__(self, k): |
|
623 | 623 | return k in self._cache |
|
624 | 624 | |
|
625 | 625 | def __iter__(self): |
|
626 | 626 | # We don't have to iterate in cache order, but why not. |
|
627 | 627 | n = self._head |
|
628 | 628 | for i in range(len(self._cache)): |
|
629 | 629 | yield n.key |
|
630 | 630 | n = n.next |
|
631 | 631 | |
|
632 | 632 | def __getitem__(self, k): |
|
633 | 633 | node = self._cache[k] |
|
634 | 634 | self._movetohead(node) |
|
635 | 635 | return node.value |
|
636 | 636 | |
|
637 | 637 | def __setitem__(self, k, v): |
|
638 | 638 | node = self._cache.get(k) |
|
639 | 639 | # Replace existing value and mark as newest. |
|
640 | 640 | if node is not None: |
|
641 | 641 | node.value = v |
|
642 | 642 | self._movetohead(node) |
|
643 | 643 | return |
|
644 | 644 | |
|
645 | 645 | if self._size < self._capacity: |
|
646 | 646 | node = self._addcapacity() |
|
647 | 647 | else: |
|
648 | 648 | # Grab the last/oldest item. |
|
649 | 649 | node = self._head.prev |
|
650 | 650 | |
|
651 | 651 | # At capacity. Kill the old entry. |
|
652 | 652 | if node.key is not _notset: |
|
653 | 653 | del self._cache[node.key] |
|
654 | 654 | |
|
655 | 655 | node.key = k |
|
656 | 656 | node.value = v |
|
657 | 657 | self._cache[k] = node |
|
658 | 658 | # And mark it as newest entry. No need to adjust order since it |
|
659 | 659 | # is already self._head.prev. |
|
660 | 660 | self._head = node |
|
661 | 661 | |
|
662 | 662 | def __delitem__(self, k): |
|
663 | 663 | node = self._cache.pop(k) |
|
664 | 664 | node.markempty() |
|
665 | 665 | |
|
666 | 666 | # Temporarily mark as newest item before re-adjusting head to make |
|
667 | 667 | # this node the oldest item. |
|
668 | 668 | self._movetohead(node) |
|
669 | 669 | self._head = node.next |
|
670 | 670 | |
|
671 | 671 | # Additional dict methods. |
|
672 | 672 | |
|
673 | 673 | def get(self, k, default=None): |
|
674 | 674 | try: |
|
675 | 675 | return self._cache[k].value |
|
676 | 676 | except KeyError: |
|
677 | 677 | return default |
|
678 | 678 | |
|
679 | 679 | def clear(self): |
|
680 | 680 | n = self._head |
|
681 | 681 | while n.key is not _notset: |
|
682 | 682 | n.markempty() |
|
683 | 683 | n = n.next |
|
684 | 684 | |
|
685 | 685 | self._cache.clear() |
|
686 | 686 | |
|
687 | 687 | def copy(self): |
|
688 | 688 | result = lrucachedict(self._capacity) |
|
689 | 689 | n = self._head.prev |
|
690 | 690 | # Iterate in oldest-to-newest order, so the copy has the right ordering |
|
691 | 691 | for i in range(len(self._cache)): |
|
692 | 692 | result[n.key] = n.value |
|
693 | 693 | n = n.prev |
|
694 | 694 | return result |
|
695 | 695 | |
|
696 | 696 | def _movetohead(self, node): |
|
697 | 697 | """Mark a node as the newest, making it the new head. |
|
698 | 698 | |
|
699 | 699 | When a node is accessed, it becomes the freshest entry in the LRU |
|
700 | 700 | list, which is denoted by self._head. |
|
701 | 701 | |
|
702 | 702 | Visually, let's make ``N`` the new head node (* denotes head): |
|
703 | 703 | |
|
704 | 704 | previous/oldest <-> head <-> next/next newest |
|
705 | 705 | |
|
706 | 706 | ----<->--- A* ---<->----- |
|
707 | 707 | | | |
|
708 | 708 | E <-> D <-> N <-> C <-> B |
|
709 | 709 | |
|
710 | 710 | To: |
|
711 | 711 | |
|
712 | 712 | ----<->--- N* ---<->----- |
|
713 | 713 | | | |
|
714 | 714 | E <-> D <-> C <-> B <-> A |
|
715 | 715 | |
|
716 | 716 | This requires the following moves: |
|
717 | 717 | |
|
718 | 718 | C.next = D (node.prev.next = node.next) |
|
719 | 719 | D.prev = C (node.next.prev = node.prev) |
|
720 | 720 | E.next = N (head.prev.next = node) |
|
721 | 721 | N.prev = E (node.prev = head.prev) |
|
722 | 722 | N.next = A (node.next = head) |
|
723 | 723 | A.prev = N (head.prev = node) |
|
724 | 724 | """ |
|
725 | 725 | head = self._head |
|
726 | 726 | # C.next = D |
|
727 | 727 | node.prev.next = node.next |
|
728 | 728 | # D.prev = C |
|
729 | 729 | node.next.prev = node.prev |
|
730 | 730 | # N.prev = E |
|
731 | 731 | node.prev = head.prev |
|
732 | 732 | # N.next = A |
|
733 | 733 | # It is tempting to do just "head" here, however if node is |
|
734 | 734 | # adjacent to head, this will do bad things. |
|
735 | 735 | node.next = head.prev.next |
|
736 | 736 | # E.next = N |
|
737 | 737 | node.next.prev = node |
|
738 | 738 | # A.prev = N |
|
739 | 739 | node.prev.next = node |
|
740 | 740 | |
|
741 | 741 | self._head = node |
|
742 | 742 | |
|
743 | 743 | def _addcapacity(self): |
|
744 | 744 | """Add a node to the circular linked list. |
|
745 | 745 | |
|
746 | 746 | The new node is inserted before the head node. |
|
747 | 747 | """ |
|
748 | 748 | head = self._head |
|
749 | 749 | node = _lrucachenode() |
|
750 | 750 | head.prev.next = node |
|
751 | 751 | node.prev = head.prev |
|
752 | 752 | node.next = head |
|
753 | 753 | head.prev = node |
|
754 | 754 | self._size += 1 |
|
755 | 755 | return node |
|
756 | 756 | |
|
757 | 757 | def lrucachefunc(func): |
|
758 | 758 | '''cache most recent results of function calls''' |
|
759 | 759 | cache = {} |
|
760 | 760 | order = collections.deque() |
|
761 | 761 | if func.__code__.co_argcount == 1: |
|
762 | 762 | def f(arg): |
|
763 | 763 | if arg not in cache: |
|
764 | 764 | if len(cache) > 20: |
|
765 | 765 | del cache[order.popleft()] |
|
766 | 766 | cache[arg] = func(arg) |
|
767 | 767 | else: |
|
768 | 768 | order.remove(arg) |
|
769 | 769 | order.append(arg) |
|
770 | 770 | return cache[arg] |
|
771 | 771 | else: |
|
772 | 772 | def f(*args): |
|
773 | 773 | if args not in cache: |
|
774 | 774 | if len(cache) > 20: |
|
775 | 775 | del cache[order.popleft()] |
|
776 | 776 | cache[args] = func(*args) |
|
777 | 777 | else: |
|
778 | 778 | order.remove(args) |
|
779 | 779 | order.append(args) |
|
780 | 780 | return cache[args] |
|
781 | 781 | |
|
782 | 782 | return f |
|
783 | 783 | |
|
784 | 784 | class propertycache(object): |
|
785 | 785 | def __init__(self, func): |
|
786 | 786 | self.func = func |
|
787 | 787 | self.name = func.__name__ |
|
788 | 788 | def __get__(self, obj, type=None): |
|
789 | 789 | result = self.func(obj) |
|
790 | 790 | self.cachevalue(obj, result) |
|
791 | 791 | return result |
|
792 | 792 | |
|
793 | 793 | def cachevalue(self, obj, value): |
|
794 | 794 | # __dict__ assignment required to bypass __setattr__ (eg: repoview) |
|
795 | 795 | obj.__dict__[self.name] = value |
|
796 | 796 | |
|
797 | 797 | def pipefilter(s, cmd): |
|
798 | 798 | '''filter string S through command CMD, returning its output''' |
|
799 | 799 | p = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
800 | 800 | stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
|
801 | 801 | pout, perr = p.communicate(s) |
|
802 | 802 | return pout |
|
803 | 803 | |
|
804 | 804 | def tempfilter(s, cmd): |
|
805 | 805 | '''filter string S through a pair of temporary files with CMD. |
|
806 | 806 | CMD is used as a template to create the real command to be run, |
|
807 | 807 | with the strings INFILE and OUTFILE replaced by the real names of |
|
808 | 808 | the temporary files generated.''' |
|
809 | 809 | inname, outname = None, None |
|
810 | 810 | try: |
|
811 | 811 | infd, inname = tempfile.mkstemp(prefix='hg-filter-in-') |
|
812 | fp = os.fdopen(infd, 'wb') | |
|
812 | fp = os.fdopen(infd, pycompat.sysstr('wb')) | |
|
813 | 813 | fp.write(s) |
|
814 | 814 | fp.close() |
|
815 | 815 | outfd, outname = tempfile.mkstemp(prefix='hg-filter-out-') |
|
816 | 816 | os.close(outfd) |
|
817 | 817 | cmd = cmd.replace('INFILE', inname) |
|
818 | 818 | cmd = cmd.replace('OUTFILE', outname) |
|
819 | 819 | code = os.system(cmd) |
|
820 | 820 | if pycompat.sysplatform == 'OpenVMS' and code & 1: |
|
821 | 821 | code = 0 |
|
822 | 822 | if code: |
|
823 | 823 | raise Abort(_("command '%s' failed: %s") % |
|
824 | 824 | (cmd, explainexit(code))) |
|
825 | 825 | return readfile(outname) |
|
826 | 826 | finally: |
|
827 | 827 | try: |
|
828 | 828 | if inname: |
|
829 | 829 | os.unlink(inname) |
|
830 | 830 | except OSError: |
|
831 | 831 | pass |
|
832 | 832 | try: |
|
833 | 833 | if outname: |
|
834 | 834 | os.unlink(outname) |
|
835 | 835 | except OSError: |
|
836 | 836 | pass |
|
837 | 837 | |
|
838 | 838 | filtertable = { |
|
839 | 839 | 'tempfile:': tempfilter, |
|
840 | 840 | 'pipe:': pipefilter, |
|
841 | 841 | } |
|
842 | 842 | |
|
843 | 843 | def filter(s, cmd): |
|
844 | 844 | "filter a string through a command that transforms its input to its output" |
|
845 | 845 | for name, fn in filtertable.iteritems(): |
|
846 | 846 | if cmd.startswith(name): |
|
847 | 847 | return fn(s, cmd[len(name):].lstrip()) |
|
848 | 848 | return pipefilter(s, cmd) |
|
849 | 849 | |
|
850 | 850 | def binary(s): |
|
851 | 851 | """return true if a string is binary data""" |
|
852 | 852 | return bool(s and '\0' in s) |
|
853 | 853 | |
|
854 | 854 | def increasingchunks(source, min=1024, max=65536): |
|
855 | 855 | '''return no less than min bytes per chunk while data remains, |
|
856 | 856 | doubling min after each chunk until it reaches max''' |
|
857 | 857 | def log2(x): |
|
858 | 858 | if not x: |
|
859 | 859 | return 0 |
|
860 | 860 | i = 0 |
|
861 | 861 | while x: |
|
862 | 862 | x >>= 1 |
|
863 | 863 | i += 1 |
|
864 | 864 | return i - 1 |
|
865 | 865 | |
|
866 | 866 | buf = [] |
|
867 | 867 | blen = 0 |
|
868 | 868 | for chunk in source: |
|
869 | 869 | buf.append(chunk) |
|
870 | 870 | blen += len(chunk) |
|
871 | 871 | if blen >= min: |
|
872 | 872 | if min < max: |
|
873 | 873 | min = min << 1 |
|
874 | 874 | nmin = 1 << log2(blen) |
|
875 | 875 | if nmin > min: |
|
876 | 876 | min = nmin |
|
877 | 877 | if min > max: |
|
878 | 878 | min = max |
|
879 | 879 | yield ''.join(buf) |
|
880 | 880 | blen = 0 |
|
881 | 881 | buf = [] |
|
882 | 882 | if buf: |
|
883 | 883 | yield ''.join(buf) |
|
884 | 884 | |
|
885 | 885 | Abort = error.Abort |
|
886 | 886 | |
|
887 | 887 | def always(fn): |
|
888 | 888 | return True |
|
889 | 889 | |
|
890 | 890 | def never(fn): |
|
891 | 891 | return False |
|
892 | 892 | |
|
893 | 893 | def nogc(func): |
|
894 | 894 | """disable garbage collector |
|
895 | 895 | |
|
896 | 896 | Python's garbage collector triggers a GC each time a certain number of |
|
897 | 897 | container objects (the number being defined by gc.get_threshold()) are |
|
898 | 898 | allocated even when marked not to be tracked by the collector. Tracking has |
|
899 | 899 | no effect on when GCs are triggered, only on what objects the GC looks |
|
900 | 900 | into. As a workaround, disable GC while building complex (huge) |
|
901 | 901 | containers. |
|
902 | 902 | |
|
903 | 903 | This garbage collector issue have been fixed in 2.7. |
|
904 | 904 | """ |
|
905 | 905 | if sys.version_info >= (2, 7): |
|
906 | 906 | return func |
|
907 | 907 | def wrapper(*args, **kwargs): |
|
908 | 908 | gcenabled = gc.isenabled() |
|
909 | 909 | gc.disable() |
|
910 | 910 | try: |
|
911 | 911 | return func(*args, **kwargs) |
|
912 | 912 | finally: |
|
913 | 913 | if gcenabled: |
|
914 | 914 | gc.enable() |
|
915 | 915 | return wrapper |
|
916 | 916 | |
|
917 | 917 | def pathto(root, n1, n2): |
|
918 | 918 | '''return the relative path from one place to another. |
|
919 | 919 | root should use os.sep to separate directories |
|
920 | 920 | n1 should use os.sep to separate directories |
|
921 | 921 | n2 should use "/" to separate directories |
|
922 | 922 | returns an os.sep-separated path. |
|
923 | 923 | |
|
924 | 924 | If n1 is a relative path, it's assumed it's |
|
925 | 925 | relative to root. |
|
926 | 926 | n2 should always be relative to root. |
|
927 | 927 | ''' |
|
928 | 928 | if not n1: |
|
929 | 929 | return localpath(n2) |
|
930 | 930 | if os.path.isabs(n1): |
|
931 | 931 | if os.path.splitdrive(root)[0] != os.path.splitdrive(n1)[0]: |
|
932 | 932 | return os.path.join(root, localpath(n2)) |
|
933 | 933 | n2 = '/'.join((pconvert(root), n2)) |
|
934 | 934 | a, b = splitpath(n1), n2.split('/') |
|
935 | 935 | a.reverse() |
|
936 | 936 | b.reverse() |
|
937 | 937 | while a and b and a[-1] == b[-1]: |
|
938 | 938 | a.pop() |
|
939 | 939 | b.pop() |
|
940 | 940 | b.reverse() |
|
941 | 941 | return pycompat.ossep.join((['..'] * len(a)) + b) or '.' |
|
942 | 942 | |
|
943 | 943 | def mainfrozen(): |
|
944 | 944 | """return True if we are a frozen executable. |
|
945 | 945 | |
|
946 | 946 | The code supports py2exe (most common, Windows only) and tools/freeze |
|
947 | 947 | (portable, not much used). |
|
948 | 948 | """ |
|
949 | 949 | return (safehasattr(sys, "frozen") or # new py2exe |
|
950 | 950 | safehasattr(sys, "importers") or # old py2exe |
|
951 | 951 | imp.is_frozen(u"__main__")) # tools/freeze |
|
952 | 952 | |
|
953 | 953 | # the location of data files matching the source code |
|
954 | 954 | if mainfrozen() and getattr(sys, 'frozen', None) != 'macosx_app': |
|
955 | 955 | # executable version (py2exe) doesn't support __file__ |
|
956 | 956 | datapath = os.path.dirname(pycompat.sysexecutable) |
|
957 | 957 | else: |
|
958 | 958 | datapath = os.path.dirname(__file__) |
|
959 | 959 | |
|
960 | 960 | if not isinstance(datapath, bytes): |
|
961 | 961 | datapath = pycompat.fsencode(datapath) |
|
962 | 962 | |
|
963 | 963 | i18n.setdatapath(datapath) |
|
964 | 964 | |
|
965 | 965 | _hgexecutable = None |
|
966 | 966 | |
|
967 | 967 | def hgexecutable(): |
|
968 | 968 | """return location of the 'hg' executable. |
|
969 | 969 | |
|
970 | 970 | Defaults to $HG or 'hg' in the search path. |
|
971 | 971 | """ |
|
972 | 972 | if _hgexecutable is None: |
|
973 | 973 | hg = encoding.environ.get('HG') |
|
974 | 974 | mainmod = sys.modules['__main__'] |
|
975 | 975 | if hg: |
|
976 | 976 | _sethgexecutable(hg) |
|
977 | 977 | elif mainfrozen(): |
|
978 | 978 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
979 | 979 | # Env variable set by py2app |
|
980 | 980 | _sethgexecutable(encoding.environ['EXECUTABLEPATH']) |
|
981 | 981 | else: |
|
982 | 982 | _sethgexecutable(pycompat.sysexecutable) |
|
983 | 983 | elif os.path.basename(getattr(mainmod, '__file__', '')) == 'hg': |
|
984 | 984 | _sethgexecutable(mainmod.__file__) |
|
985 | 985 | else: |
|
986 | 986 | exe = findexe('hg') or os.path.basename(sys.argv[0]) |
|
987 | 987 | _sethgexecutable(exe) |
|
988 | 988 | return _hgexecutable |
|
989 | 989 | |
|
990 | 990 | def _sethgexecutable(path): |
|
991 | 991 | """set location of the 'hg' executable""" |
|
992 | 992 | global _hgexecutable |
|
993 | 993 | _hgexecutable = path |
|
994 | 994 | |
|
995 | 995 | def _isstdout(f): |
|
996 | 996 | fileno = getattr(f, 'fileno', None) |
|
997 | 997 | return fileno and fileno() == sys.__stdout__.fileno() |
|
998 | 998 | |
|
999 | 999 | def shellenviron(environ=None): |
|
1000 | 1000 | """return environ with optional override, useful for shelling out""" |
|
1001 | 1001 | def py2shell(val): |
|
1002 | 1002 | 'convert python object into string that is useful to shell' |
|
1003 | 1003 | if val is None or val is False: |
|
1004 | 1004 | return '0' |
|
1005 | 1005 | if val is True: |
|
1006 | 1006 | return '1' |
|
1007 | 1007 | return str(val) |
|
1008 | 1008 | env = dict(encoding.environ) |
|
1009 | 1009 | if environ: |
|
1010 | 1010 | env.update((k, py2shell(v)) for k, v in environ.iteritems()) |
|
1011 | 1011 | env['HG'] = hgexecutable() |
|
1012 | 1012 | return env |
|
1013 | 1013 | |
|
1014 | 1014 | def system(cmd, environ=None, cwd=None, onerr=None, errprefix=None, out=None): |
|
1015 | 1015 | '''enhanced shell command execution. |
|
1016 | 1016 | run with environment maybe modified, maybe in different dir. |
|
1017 | 1017 | |
|
1018 | 1018 | if command fails and onerr is None, return status, else raise onerr |
|
1019 | 1019 | object as exception. |
|
1020 | 1020 | |
|
1021 | 1021 | if out is specified, it is assumed to be a file-like object that has a |
|
1022 | 1022 | write() method. stdout and stderr will be redirected to out.''' |
|
1023 | 1023 | try: |
|
1024 | 1024 | stdout.flush() |
|
1025 | 1025 | except Exception: |
|
1026 | 1026 | pass |
|
1027 | 1027 | origcmd = cmd |
|
1028 | 1028 | cmd = quotecommand(cmd) |
|
1029 | 1029 | if pycompat.sysplatform == 'plan9' and (sys.version_info[0] == 2 |
|
1030 | 1030 | and sys.version_info[1] < 7): |
|
1031 | 1031 | # subprocess kludge to work around issues in half-baked Python |
|
1032 | 1032 | # ports, notably bichued/python: |
|
1033 | 1033 | if not cwd is None: |
|
1034 | 1034 | os.chdir(cwd) |
|
1035 | 1035 | rc = os.system(cmd) |
|
1036 | 1036 | else: |
|
1037 | 1037 | env = shellenviron(environ) |
|
1038 | 1038 | if out is None or _isstdout(out): |
|
1039 | 1039 | rc = subprocess.call(cmd, shell=True, close_fds=closefds, |
|
1040 | 1040 | env=env, cwd=cwd) |
|
1041 | 1041 | else: |
|
1042 | 1042 | proc = subprocess.Popen(cmd, shell=True, close_fds=closefds, |
|
1043 | 1043 | env=env, cwd=cwd, stdout=subprocess.PIPE, |
|
1044 | 1044 | stderr=subprocess.STDOUT) |
|
1045 | 1045 | for line in iter(proc.stdout.readline, ''): |
|
1046 | 1046 | out.write(line) |
|
1047 | 1047 | proc.wait() |
|
1048 | 1048 | rc = proc.returncode |
|
1049 | 1049 | if pycompat.sysplatform == 'OpenVMS' and rc & 1: |
|
1050 | 1050 | rc = 0 |
|
1051 | 1051 | if rc and onerr: |
|
1052 | 1052 | errmsg = '%s %s' % (os.path.basename(origcmd.split(None, 1)[0]), |
|
1053 | 1053 | explainexit(rc)[0]) |
|
1054 | 1054 | if errprefix: |
|
1055 | 1055 | errmsg = '%s: %s' % (errprefix, errmsg) |
|
1056 | 1056 | raise onerr(errmsg) |
|
1057 | 1057 | return rc |
|
1058 | 1058 | |
|
1059 | 1059 | def checksignature(func): |
|
1060 | 1060 | '''wrap a function with code to check for calling errors''' |
|
1061 | 1061 | def check(*args, **kwargs): |
|
1062 | 1062 | try: |
|
1063 | 1063 | return func(*args, **kwargs) |
|
1064 | 1064 | except TypeError: |
|
1065 | 1065 | if len(traceback.extract_tb(sys.exc_info()[2])) == 1: |
|
1066 | 1066 | raise error.SignatureError |
|
1067 | 1067 | raise |
|
1068 | 1068 | |
|
1069 | 1069 | return check |
|
1070 | 1070 | |
|
1071 | 1071 | def copyfile(src, dest, hardlink=False, copystat=False, checkambig=False): |
|
1072 | 1072 | '''copy a file, preserving mode and optionally other stat info like |
|
1073 | 1073 | atime/mtime |
|
1074 | 1074 | |
|
1075 | 1075 | checkambig argument is used with filestat, and is useful only if |
|
1076 | 1076 | destination file is guarded by any lock (e.g. repo.lock or |
|
1077 | 1077 | repo.wlock). |
|
1078 | 1078 | |
|
1079 | 1079 | copystat and checkambig should be exclusive. |
|
1080 | 1080 | ''' |
|
1081 | 1081 | assert not (copystat and checkambig) |
|
1082 | 1082 | oldstat = None |
|
1083 | 1083 | if os.path.lexists(dest): |
|
1084 | 1084 | if checkambig: |
|
1085 | 1085 | oldstat = checkambig and filestat(dest) |
|
1086 | 1086 | unlink(dest) |
|
1087 | 1087 | # hardlinks are problematic on CIFS, quietly ignore this flag |
|
1088 | 1088 | # until we find a way to work around it cleanly (issue4546) |
|
1089 | 1089 | if False and hardlink: |
|
1090 | 1090 | try: |
|
1091 | 1091 | oslink(src, dest) |
|
1092 | 1092 | return |
|
1093 | 1093 | except (IOError, OSError): |
|
1094 | 1094 | pass # fall back to normal copy |
|
1095 | 1095 | if os.path.islink(src): |
|
1096 | 1096 | os.symlink(os.readlink(src), dest) |
|
1097 | 1097 | # copytime is ignored for symlinks, but in general copytime isn't needed |
|
1098 | 1098 | # for them anyway |
|
1099 | 1099 | else: |
|
1100 | 1100 | try: |
|
1101 | 1101 | shutil.copyfile(src, dest) |
|
1102 | 1102 | if copystat: |
|
1103 | 1103 | # copystat also copies mode |
|
1104 | 1104 | shutil.copystat(src, dest) |
|
1105 | 1105 | else: |
|
1106 | 1106 | shutil.copymode(src, dest) |
|
1107 | 1107 | if oldstat and oldstat.stat: |
|
1108 | 1108 | newstat = filestat(dest) |
|
1109 | 1109 | if newstat.isambig(oldstat): |
|
1110 | 1110 | # stat of copied file is ambiguous to original one |
|
1111 | 1111 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
|
1112 | 1112 | os.utime(dest, (advanced, advanced)) |
|
1113 | 1113 | except shutil.Error as inst: |
|
1114 | 1114 | raise Abort(str(inst)) |
|
1115 | 1115 | |
|
1116 | 1116 | def copyfiles(src, dst, hardlink=None, progress=lambda t, pos: None): |
|
1117 | 1117 | """Copy a directory tree using hardlinks if possible.""" |
|
1118 | 1118 | num = 0 |
|
1119 | 1119 | |
|
1120 | 1120 | if hardlink is None: |
|
1121 | 1121 | hardlink = (os.stat(src).st_dev == |
|
1122 | 1122 | os.stat(os.path.dirname(dst)).st_dev) |
|
1123 | 1123 | if hardlink: |
|
1124 | 1124 | topic = _('linking') |
|
1125 | 1125 | else: |
|
1126 | 1126 | topic = _('copying') |
|
1127 | 1127 | |
|
1128 | 1128 | if os.path.isdir(src): |
|
1129 | 1129 | os.mkdir(dst) |
|
1130 | 1130 | for name, kind in osutil.listdir(src): |
|
1131 | 1131 | srcname = os.path.join(src, name) |
|
1132 | 1132 | dstname = os.path.join(dst, name) |
|
1133 | 1133 | def nprog(t, pos): |
|
1134 | 1134 | if pos is not None: |
|
1135 | 1135 | return progress(t, pos + num) |
|
1136 | 1136 | hardlink, n = copyfiles(srcname, dstname, hardlink, progress=nprog) |
|
1137 | 1137 | num += n |
|
1138 | 1138 | else: |
|
1139 | 1139 | if hardlink: |
|
1140 | 1140 | try: |
|
1141 | 1141 | oslink(src, dst) |
|
1142 | 1142 | except (IOError, OSError): |
|
1143 | 1143 | hardlink = False |
|
1144 | 1144 | shutil.copy(src, dst) |
|
1145 | 1145 | else: |
|
1146 | 1146 | shutil.copy(src, dst) |
|
1147 | 1147 | num += 1 |
|
1148 | 1148 | progress(topic, num) |
|
1149 | 1149 | progress(topic, None) |
|
1150 | 1150 | |
|
1151 | 1151 | return hardlink, num |
|
1152 | 1152 | |
|
1153 | 1153 | _winreservednames = '''con prn aux nul |
|
1154 | 1154 | com1 com2 com3 com4 com5 com6 com7 com8 com9 |
|
1155 | 1155 | lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9'''.split() |
|
1156 | 1156 | _winreservedchars = ':*?"<>|' |
|
1157 | 1157 | def checkwinfilename(path): |
|
1158 | 1158 | r'''Check that the base-relative path is a valid filename on Windows. |
|
1159 | 1159 | Returns None if the path is ok, or a UI string describing the problem. |
|
1160 | 1160 | |
|
1161 | 1161 | >>> checkwinfilename("just/a/normal/path") |
|
1162 | 1162 | >>> checkwinfilename("foo/bar/con.xml") |
|
1163 | 1163 | "filename contains 'con', which is reserved on Windows" |
|
1164 | 1164 | >>> checkwinfilename("foo/con.xml/bar") |
|
1165 | 1165 | "filename contains 'con', which is reserved on Windows" |
|
1166 | 1166 | >>> checkwinfilename("foo/bar/xml.con") |
|
1167 | 1167 | >>> checkwinfilename("foo/bar/AUX/bla.txt") |
|
1168 | 1168 | "filename contains 'AUX', which is reserved on Windows" |
|
1169 | 1169 | >>> checkwinfilename("foo/bar/bla:.txt") |
|
1170 | 1170 | "filename contains ':', which is reserved on Windows" |
|
1171 | 1171 | >>> checkwinfilename("foo/bar/b\07la.txt") |
|
1172 | 1172 | "filename contains '\\x07', which is invalid on Windows" |
|
1173 | 1173 | >>> checkwinfilename("foo/bar/bla ") |
|
1174 | 1174 | "filename ends with ' ', which is not allowed on Windows" |
|
1175 | 1175 | >>> checkwinfilename("../bar") |
|
1176 | 1176 | >>> checkwinfilename("foo\\") |
|
1177 | 1177 | "filename ends with '\\', which is invalid on Windows" |
|
1178 | 1178 | >>> checkwinfilename("foo\\/bar") |
|
1179 | 1179 | "directory name ends with '\\', which is invalid on Windows" |
|
1180 | 1180 | ''' |
|
1181 | 1181 | if path.endswith('\\'): |
|
1182 | 1182 | return _("filename ends with '\\', which is invalid on Windows") |
|
1183 | 1183 | if '\\/' in path: |
|
1184 | 1184 | return _("directory name ends with '\\', which is invalid on Windows") |
|
1185 | 1185 | for n in path.replace('\\', '/').split('/'): |
|
1186 | 1186 | if not n: |
|
1187 | 1187 | continue |
|
1188 | 1188 | for c in n: |
|
1189 | 1189 | if c in _winreservedchars: |
|
1190 | 1190 | return _("filename contains '%s', which is reserved " |
|
1191 | 1191 | "on Windows") % c |
|
1192 | 1192 | if ord(c) <= 31: |
|
1193 | 1193 | return _("filename contains %r, which is invalid " |
|
1194 | 1194 | "on Windows") % c |
|
1195 | 1195 | base = n.split('.')[0] |
|
1196 | 1196 | if base and base.lower() in _winreservednames: |
|
1197 | 1197 | return _("filename contains '%s', which is reserved " |
|
1198 | 1198 | "on Windows") % base |
|
1199 | 1199 | t = n[-1] |
|
1200 | 1200 | if t in '. ' and n not in '..': |
|
1201 | 1201 | return _("filename ends with '%s', which is not allowed " |
|
1202 | 1202 | "on Windows") % t |
|
1203 | 1203 | |
|
1204 | 1204 | if pycompat.osname == 'nt': |
|
1205 | 1205 | checkosfilename = checkwinfilename |
|
1206 | 1206 | else: |
|
1207 | 1207 | checkosfilename = platform.checkosfilename |
|
1208 | 1208 | |
|
1209 | 1209 | def makelock(info, pathname): |
|
1210 | 1210 | try: |
|
1211 | 1211 | return os.symlink(info, pathname) |
|
1212 | 1212 | except OSError as why: |
|
1213 | 1213 | if why.errno == errno.EEXIST: |
|
1214 | 1214 | raise |
|
1215 | 1215 | except AttributeError: # no symlink in os |
|
1216 | 1216 | pass |
|
1217 | 1217 | |
|
1218 | 1218 | ld = os.open(pathname, os.O_CREAT | os.O_WRONLY | os.O_EXCL) |
|
1219 | 1219 | os.write(ld, info) |
|
1220 | 1220 | os.close(ld) |
|
1221 | 1221 | |
|
1222 | 1222 | def readlock(pathname): |
|
1223 | 1223 | try: |
|
1224 | 1224 | return os.readlink(pathname) |
|
1225 | 1225 | except OSError as why: |
|
1226 | 1226 | if why.errno not in (errno.EINVAL, errno.ENOSYS): |
|
1227 | 1227 | raise |
|
1228 | 1228 | except AttributeError: # no symlink in os |
|
1229 | 1229 | pass |
|
1230 | 1230 | fp = posixfile(pathname) |
|
1231 | 1231 | r = fp.read() |
|
1232 | 1232 | fp.close() |
|
1233 | 1233 | return r |
|
1234 | 1234 | |
|
1235 | 1235 | def fstat(fp): |
|
1236 | 1236 | '''stat file object that may not have fileno method.''' |
|
1237 | 1237 | try: |
|
1238 | 1238 | return os.fstat(fp.fileno()) |
|
1239 | 1239 | except AttributeError: |
|
1240 | 1240 | return os.stat(fp.name) |
|
1241 | 1241 | |
|
1242 | 1242 | # File system features |
|
1243 | 1243 | |
|
1244 | 1244 | def fscasesensitive(path): |
|
1245 | 1245 | """ |
|
1246 | 1246 | Return true if the given path is on a case-sensitive filesystem |
|
1247 | 1247 | |
|
1248 | 1248 | Requires a path (like /foo/.hg) ending with a foldable final |
|
1249 | 1249 | directory component. |
|
1250 | 1250 | """ |
|
1251 | 1251 | s1 = os.lstat(path) |
|
1252 | 1252 | d, b = os.path.split(path) |
|
1253 | 1253 | b2 = b.upper() |
|
1254 | 1254 | if b == b2: |
|
1255 | 1255 | b2 = b.lower() |
|
1256 | 1256 | if b == b2: |
|
1257 | 1257 | return True # no evidence against case sensitivity |
|
1258 | 1258 | p2 = os.path.join(d, b2) |
|
1259 | 1259 | try: |
|
1260 | 1260 | s2 = os.lstat(p2) |
|
1261 | 1261 | if s2 == s1: |
|
1262 | 1262 | return False |
|
1263 | 1263 | return True |
|
1264 | 1264 | except OSError: |
|
1265 | 1265 | return True |
|
1266 | 1266 | |
|
1267 | 1267 | try: |
|
1268 | 1268 | import re2 |
|
1269 | 1269 | _re2 = None |
|
1270 | 1270 | except ImportError: |
|
1271 | 1271 | _re2 = False |
|
1272 | 1272 | |
|
1273 | 1273 | class _re(object): |
|
1274 | 1274 | def _checkre2(self): |
|
1275 | 1275 | global _re2 |
|
1276 | 1276 | try: |
|
1277 | 1277 | # check if match works, see issue3964 |
|
1278 | 1278 | _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]')) |
|
1279 | 1279 | except ImportError: |
|
1280 | 1280 | _re2 = False |
|
1281 | 1281 | |
|
1282 | 1282 | def compile(self, pat, flags=0): |
|
1283 | 1283 | '''Compile a regular expression, using re2 if possible |
|
1284 | 1284 | |
|
1285 | 1285 | For best performance, use only re2-compatible regexp features. The |
|
1286 | 1286 | only flags from the re module that are re2-compatible are |
|
1287 | 1287 | IGNORECASE and MULTILINE.''' |
|
1288 | 1288 | if _re2 is None: |
|
1289 | 1289 | self._checkre2() |
|
1290 | 1290 | if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0: |
|
1291 | 1291 | if flags & remod.IGNORECASE: |
|
1292 | 1292 | pat = '(?i)' + pat |
|
1293 | 1293 | if flags & remod.MULTILINE: |
|
1294 | 1294 | pat = '(?m)' + pat |
|
1295 | 1295 | try: |
|
1296 | 1296 | return re2.compile(pat) |
|
1297 | 1297 | except re2.error: |
|
1298 | 1298 | pass |
|
1299 | 1299 | return remod.compile(pat, flags) |
|
1300 | 1300 | |
|
1301 | 1301 | @propertycache |
|
1302 | 1302 | def escape(self): |
|
1303 | 1303 | '''Return the version of escape corresponding to self.compile. |
|
1304 | 1304 | |
|
1305 | 1305 | This is imperfect because whether re2 or re is used for a particular |
|
1306 | 1306 | function depends on the flags, etc, but it's the best we can do. |
|
1307 | 1307 | ''' |
|
1308 | 1308 | global _re2 |
|
1309 | 1309 | if _re2 is None: |
|
1310 | 1310 | self._checkre2() |
|
1311 | 1311 | if _re2: |
|
1312 | 1312 | return re2.escape |
|
1313 | 1313 | else: |
|
1314 | 1314 | return remod.escape |
|
1315 | 1315 | |
|
1316 | 1316 | re = _re() |
|
1317 | 1317 | |
|
1318 | 1318 | _fspathcache = {} |
|
1319 | 1319 | def fspath(name, root): |
|
1320 | 1320 | '''Get name in the case stored in the filesystem |
|
1321 | 1321 | |
|
1322 | 1322 | The name should be relative to root, and be normcase-ed for efficiency. |
|
1323 | 1323 | |
|
1324 | 1324 | Note that this function is unnecessary, and should not be |
|
1325 | 1325 | called, for case-sensitive filesystems (simply because it's expensive). |
|
1326 | 1326 | |
|
1327 | 1327 | The root should be normcase-ed, too. |
|
1328 | 1328 | ''' |
|
1329 | 1329 | def _makefspathcacheentry(dir): |
|
1330 | 1330 | return dict((normcase(n), n) for n in os.listdir(dir)) |
|
1331 | 1331 | |
|
1332 | 1332 | seps = pycompat.ossep |
|
1333 | 1333 | if pycompat.osaltsep: |
|
1334 | 1334 | seps = seps + pycompat.osaltsep |
|
1335 | 1335 | # Protect backslashes. This gets silly very quickly. |
|
1336 | 1336 | seps.replace('\\','\\\\') |
|
1337 | 1337 | pattern = remod.compile(r'([^%s]+)|([%s]+)' % (seps, seps)) |
|
1338 | 1338 | dir = os.path.normpath(root) |
|
1339 | 1339 | result = [] |
|
1340 | 1340 | for part, sep in pattern.findall(name): |
|
1341 | 1341 | if sep: |
|
1342 | 1342 | result.append(sep) |
|
1343 | 1343 | continue |
|
1344 | 1344 | |
|
1345 | 1345 | if dir not in _fspathcache: |
|
1346 | 1346 | _fspathcache[dir] = _makefspathcacheentry(dir) |
|
1347 | 1347 | contents = _fspathcache[dir] |
|
1348 | 1348 | |
|
1349 | 1349 | found = contents.get(part) |
|
1350 | 1350 | if not found: |
|
1351 | 1351 | # retry "once per directory" per "dirstate.walk" which |
|
1352 | 1352 | # may take place for each patches of "hg qpush", for example |
|
1353 | 1353 | _fspathcache[dir] = contents = _makefspathcacheentry(dir) |
|
1354 | 1354 | found = contents.get(part) |
|
1355 | 1355 | |
|
1356 | 1356 | result.append(found or part) |
|
1357 | 1357 | dir = os.path.join(dir, part) |
|
1358 | 1358 | |
|
1359 | 1359 | return ''.join(result) |
|
1360 | 1360 | |
|
1361 | 1361 | def checknlink(testfile): |
|
1362 | 1362 | '''check whether hardlink count reporting works properly''' |
|
1363 | 1363 | |
|
1364 | 1364 | # testfile may be open, so we need a separate file for checking to |
|
1365 | 1365 | # work around issue2543 (or testfile may get lost on Samba shares) |
|
1366 | 1366 | f1 = testfile + ".hgtmp1" |
|
1367 | 1367 | if os.path.lexists(f1): |
|
1368 | 1368 | return False |
|
1369 | 1369 | try: |
|
1370 | 1370 | posixfile(f1, 'w').close() |
|
1371 | 1371 | except IOError: |
|
1372 | 1372 | try: |
|
1373 | 1373 | os.unlink(f1) |
|
1374 | 1374 | except OSError: |
|
1375 | 1375 | pass |
|
1376 | 1376 | return False |
|
1377 | 1377 | |
|
1378 | 1378 | f2 = testfile + ".hgtmp2" |
|
1379 | 1379 | fd = None |
|
1380 | 1380 | try: |
|
1381 | 1381 | oslink(f1, f2) |
|
1382 | 1382 | # nlinks() may behave differently for files on Windows shares if |
|
1383 | 1383 | # the file is open. |
|
1384 | 1384 | fd = posixfile(f2) |
|
1385 | 1385 | return nlinks(f2) > 1 |
|
1386 | 1386 | except OSError: |
|
1387 | 1387 | return False |
|
1388 | 1388 | finally: |
|
1389 | 1389 | if fd is not None: |
|
1390 | 1390 | fd.close() |
|
1391 | 1391 | for f in (f1, f2): |
|
1392 | 1392 | try: |
|
1393 | 1393 | os.unlink(f) |
|
1394 | 1394 | except OSError: |
|
1395 | 1395 | pass |
|
1396 | 1396 | |
|
1397 | 1397 | def endswithsep(path): |
|
1398 | 1398 | '''Check path ends with os.sep or os.altsep.''' |
|
1399 | 1399 | return (path.endswith(pycompat.ossep) |
|
1400 | 1400 | or pycompat.osaltsep and path.endswith(pycompat.osaltsep)) |
|
1401 | 1401 | |
|
1402 | 1402 | def splitpath(path): |
|
1403 | 1403 | '''Split path by os.sep. |
|
1404 | 1404 | Note that this function does not use os.altsep because this is |
|
1405 | 1405 | an alternative of simple "xxx.split(os.sep)". |
|
1406 | 1406 | It is recommended to use os.path.normpath() before using this |
|
1407 | 1407 | function if need.''' |
|
1408 | 1408 | return path.split(pycompat.ossep) |
|
1409 | 1409 | |
|
1410 | 1410 | def gui(): |
|
1411 | 1411 | '''Are we running in a GUI?''' |
|
1412 | 1412 | if pycompat.sysplatform == 'darwin': |
|
1413 | 1413 | if 'SSH_CONNECTION' in encoding.environ: |
|
1414 | 1414 | # handle SSH access to a box where the user is logged in |
|
1415 | 1415 | return False |
|
1416 | 1416 | elif getattr(osutil, 'isgui', None): |
|
1417 | 1417 | # check if a CoreGraphics session is available |
|
1418 | 1418 | return osutil.isgui() |
|
1419 | 1419 | else: |
|
1420 | 1420 | # pure build; use a safe default |
|
1421 | 1421 | return True |
|
1422 | 1422 | else: |
|
1423 | 1423 | return pycompat.osname == "nt" or encoding.environ.get("DISPLAY") |
|
1424 | 1424 | |
|
1425 | 1425 | def mktempcopy(name, emptyok=False, createmode=None): |
|
1426 | 1426 | """Create a temporary file with the same contents from name |
|
1427 | 1427 | |
|
1428 | 1428 | The permission bits are copied from the original file. |
|
1429 | 1429 | |
|
1430 | 1430 | If the temporary file is going to be truncated immediately, you |
|
1431 | 1431 | can use emptyok=True as an optimization. |
|
1432 | 1432 | |
|
1433 | 1433 | Returns the name of the temporary file. |
|
1434 | 1434 | """ |
|
1435 | 1435 | d, fn = os.path.split(name) |
|
1436 | 1436 | fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d) |
|
1437 | 1437 | os.close(fd) |
|
1438 | 1438 | # Temporary files are created with mode 0600, which is usually not |
|
1439 | 1439 | # what we want. If the original file already exists, just copy |
|
1440 | 1440 | # its mode. Otherwise, manually obey umask. |
|
1441 | 1441 | copymode(name, temp, createmode) |
|
1442 | 1442 | if emptyok: |
|
1443 | 1443 | return temp |
|
1444 | 1444 | try: |
|
1445 | 1445 | try: |
|
1446 | 1446 | ifp = posixfile(name, "rb") |
|
1447 | 1447 | except IOError as inst: |
|
1448 | 1448 | if inst.errno == errno.ENOENT: |
|
1449 | 1449 | return temp |
|
1450 | 1450 | if not getattr(inst, 'filename', None): |
|
1451 | 1451 | inst.filename = name |
|
1452 | 1452 | raise |
|
1453 | 1453 | ofp = posixfile(temp, "wb") |
|
1454 | 1454 | for chunk in filechunkiter(ifp): |
|
1455 | 1455 | ofp.write(chunk) |
|
1456 | 1456 | ifp.close() |
|
1457 | 1457 | ofp.close() |
|
1458 | 1458 | except: # re-raises |
|
1459 | 1459 | try: os.unlink(temp) |
|
1460 | 1460 | except OSError: pass |
|
1461 | 1461 | raise |
|
1462 | 1462 | return temp |
|
1463 | 1463 | |
|
1464 | 1464 | class filestat(object): |
|
1465 | 1465 | """help to exactly detect change of a file |
|
1466 | 1466 | |
|
1467 | 1467 | 'stat' attribute is result of 'os.stat()' if specified 'path' |
|
1468 | 1468 | exists. Otherwise, it is None. This can avoid preparative |
|
1469 | 1469 | 'exists()' examination on client side of this class. |
|
1470 | 1470 | """ |
|
1471 | 1471 | def __init__(self, path): |
|
1472 | 1472 | try: |
|
1473 | 1473 | self.stat = os.stat(path) |
|
1474 | 1474 | except OSError as err: |
|
1475 | 1475 | if err.errno != errno.ENOENT: |
|
1476 | 1476 | raise |
|
1477 | 1477 | self.stat = None |
|
1478 | 1478 | |
|
1479 | 1479 | __hash__ = object.__hash__ |
|
1480 | 1480 | |
|
1481 | 1481 | def __eq__(self, old): |
|
1482 | 1482 | try: |
|
1483 | 1483 | # if ambiguity between stat of new and old file is |
|
1484 | 1484 | # avoided, comparison of size, ctime and mtime is enough |
|
1485 | 1485 | # to exactly detect change of a file regardless of platform |
|
1486 | 1486 | return (self.stat.st_size == old.stat.st_size and |
|
1487 | 1487 | self.stat.st_ctime == old.stat.st_ctime and |
|
1488 | 1488 | self.stat.st_mtime == old.stat.st_mtime) |
|
1489 | 1489 | except AttributeError: |
|
1490 | 1490 | return False |
|
1491 | 1491 | |
|
1492 | 1492 | def isambig(self, old): |
|
1493 | 1493 | """Examine whether new (= self) stat is ambiguous against old one |
|
1494 | 1494 | |
|
1495 | 1495 | "S[N]" below means stat of a file at N-th change: |
|
1496 | 1496 | |
|
1497 | 1497 | - S[n-1].ctime < S[n].ctime: can detect change of a file |
|
1498 | 1498 | - S[n-1].ctime == S[n].ctime |
|
1499 | 1499 | - S[n-1].ctime < S[n].mtime: means natural advancing (*1) |
|
1500 | 1500 | - S[n-1].ctime == S[n].mtime: is ambiguous (*2) |
|
1501 | 1501 | - S[n-1].ctime > S[n].mtime: never occurs naturally (don't care) |
|
1502 | 1502 | - S[n-1].ctime > S[n].ctime: never occurs naturally (don't care) |
|
1503 | 1503 | |
|
1504 | 1504 | Case (*2) above means that a file was changed twice or more at |
|
1505 | 1505 | same time in sec (= S[n-1].ctime), and comparison of timestamp |
|
1506 | 1506 | is ambiguous. |
|
1507 | 1507 | |
|
1508 | 1508 | Base idea to avoid such ambiguity is "advance mtime 1 sec, if |
|
1509 | 1509 | timestamp is ambiguous". |
|
1510 | 1510 | |
|
1511 | 1511 | But advancing mtime only in case (*2) doesn't work as |
|
1512 | 1512 | expected, because naturally advanced S[n].mtime in case (*1) |
|
1513 | 1513 | might be equal to manually advanced S[n-1 or earlier].mtime. |
|
1514 | 1514 | |
|
1515 | 1515 | Therefore, all "S[n-1].ctime == S[n].ctime" cases should be |
|
1516 | 1516 | treated as ambiguous regardless of mtime, to avoid overlooking |
|
1517 | 1517 | by confliction between such mtime. |
|
1518 | 1518 | |
|
1519 | 1519 | Advancing mtime "if isambig(oldstat)" ensures "S[n-1].mtime != |
|
1520 | 1520 | S[n].mtime", even if size of a file isn't changed. |
|
1521 | 1521 | """ |
|
1522 | 1522 | try: |
|
1523 | 1523 | return (self.stat.st_ctime == old.stat.st_ctime) |
|
1524 | 1524 | except AttributeError: |
|
1525 | 1525 | return False |
|
1526 | 1526 | |
|
1527 | 1527 | def avoidambig(self, path, old): |
|
1528 | 1528 | """Change file stat of specified path to avoid ambiguity |
|
1529 | 1529 | |
|
1530 | 1530 | 'old' should be previous filestat of 'path'. |
|
1531 | 1531 | |
|
1532 | 1532 | This skips avoiding ambiguity, if a process doesn't have |
|
1533 | 1533 | appropriate privileges for 'path'. |
|
1534 | 1534 | """ |
|
1535 | 1535 | advanced = (old.stat.st_mtime + 1) & 0x7fffffff |
|
1536 | 1536 | try: |
|
1537 | 1537 | os.utime(path, (advanced, advanced)) |
|
1538 | 1538 | except OSError as inst: |
|
1539 | 1539 | if inst.errno == errno.EPERM: |
|
1540 | 1540 | # utime() on the file created by another user causes EPERM, |
|
1541 | 1541 | # if a process doesn't have appropriate privileges |
|
1542 | 1542 | return |
|
1543 | 1543 | raise |
|
1544 | 1544 | |
|
1545 | 1545 | def __ne__(self, other): |
|
1546 | 1546 | return not self == other |
|
1547 | 1547 | |
|
1548 | 1548 | class atomictempfile(object): |
|
1549 | 1549 | '''writable file object that atomically updates a file |
|
1550 | 1550 | |
|
1551 | 1551 | All writes will go to a temporary copy of the original file. Call |
|
1552 | 1552 | close() when you are done writing, and atomictempfile will rename |
|
1553 | 1553 | the temporary copy to the original name, making the changes |
|
1554 | 1554 | visible. If the object is destroyed without being closed, all your |
|
1555 | 1555 | writes are discarded. |
|
1556 | 1556 | |
|
1557 | 1557 | checkambig argument of constructor is used with filestat, and is |
|
1558 | 1558 | useful only if target file is guarded by any lock (e.g. repo.lock |
|
1559 | 1559 | or repo.wlock). |
|
1560 | 1560 | ''' |
|
1561 | 1561 | def __init__(self, name, mode='w+b', createmode=None, checkambig=False): |
|
1562 | 1562 | self.__name = name # permanent name |
|
1563 | 1563 | self._tempname = mktempcopy(name, emptyok=('w' in mode), |
|
1564 | 1564 | createmode=createmode) |
|
1565 | 1565 | self._fp = posixfile(self._tempname, mode) |
|
1566 | 1566 | self._checkambig = checkambig |
|
1567 | 1567 | |
|
1568 | 1568 | # delegated methods |
|
1569 | 1569 | self.read = self._fp.read |
|
1570 | 1570 | self.write = self._fp.write |
|
1571 | 1571 | self.seek = self._fp.seek |
|
1572 | 1572 | self.tell = self._fp.tell |
|
1573 | 1573 | self.fileno = self._fp.fileno |
|
1574 | 1574 | |
|
1575 | 1575 | def close(self): |
|
1576 | 1576 | if not self._fp.closed: |
|
1577 | 1577 | self._fp.close() |
|
1578 | 1578 | filename = localpath(self.__name) |
|
1579 | 1579 | oldstat = self._checkambig and filestat(filename) |
|
1580 | 1580 | if oldstat and oldstat.stat: |
|
1581 | 1581 | rename(self._tempname, filename) |
|
1582 | 1582 | newstat = filestat(filename) |
|
1583 | 1583 | if newstat.isambig(oldstat): |
|
1584 | 1584 | # stat of changed file is ambiguous to original one |
|
1585 | 1585 | advanced = (oldstat.stat.st_mtime + 1) & 0x7fffffff |
|
1586 | 1586 | os.utime(filename, (advanced, advanced)) |
|
1587 | 1587 | else: |
|
1588 | 1588 | rename(self._tempname, filename) |
|
1589 | 1589 | |
|
1590 | 1590 | def discard(self): |
|
1591 | 1591 | if not self._fp.closed: |
|
1592 | 1592 | try: |
|
1593 | 1593 | os.unlink(self._tempname) |
|
1594 | 1594 | except OSError: |
|
1595 | 1595 | pass |
|
1596 | 1596 | self._fp.close() |
|
1597 | 1597 | |
|
1598 | 1598 | def __del__(self): |
|
1599 | 1599 | if safehasattr(self, '_fp'): # constructor actually did something |
|
1600 | 1600 | self.discard() |
|
1601 | 1601 | |
|
1602 | 1602 | def __enter__(self): |
|
1603 | 1603 | return self |
|
1604 | 1604 | |
|
1605 | 1605 | def __exit__(self, exctype, excvalue, traceback): |
|
1606 | 1606 | if exctype is not None: |
|
1607 | 1607 | self.discard() |
|
1608 | 1608 | else: |
|
1609 | 1609 | self.close() |
|
1610 | 1610 | |
|
1611 | 1611 | def makedirs(name, mode=None, notindexed=False): |
|
1612 | 1612 | """recursive directory creation with parent mode inheritance |
|
1613 | 1613 | |
|
1614 | 1614 | Newly created directories are marked as "not to be indexed by |
|
1615 | 1615 | the content indexing service", if ``notindexed`` is specified |
|
1616 | 1616 | for "write" mode access. |
|
1617 | 1617 | """ |
|
1618 | 1618 | try: |
|
1619 | 1619 | makedir(name, notindexed) |
|
1620 | 1620 | except OSError as err: |
|
1621 | 1621 | if err.errno == errno.EEXIST: |
|
1622 | 1622 | return |
|
1623 | 1623 | if err.errno != errno.ENOENT or not name: |
|
1624 | 1624 | raise |
|
1625 | 1625 | parent = os.path.dirname(os.path.abspath(name)) |
|
1626 | 1626 | if parent == name: |
|
1627 | 1627 | raise |
|
1628 | 1628 | makedirs(parent, mode, notindexed) |
|
1629 | 1629 | try: |
|
1630 | 1630 | makedir(name, notindexed) |
|
1631 | 1631 | except OSError as err: |
|
1632 | 1632 | # Catch EEXIST to handle races |
|
1633 | 1633 | if err.errno == errno.EEXIST: |
|
1634 | 1634 | return |
|
1635 | 1635 | raise |
|
1636 | 1636 | if mode is not None: |
|
1637 | 1637 | os.chmod(name, mode) |
|
1638 | 1638 | |
|
1639 | 1639 | def readfile(path): |
|
1640 | 1640 | with open(path, 'rb') as fp: |
|
1641 | 1641 | return fp.read() |
|
1642 | 1642 | |
|
1643 | 1643 | def writefile(path, text): |
|
1644 | 1644 | with open(path, 'wb') as fp: |
|
1645 | 1645 | fp.write(text) |
|
1646 | 1646 | |
|
1647 | 1647 | def appendfile(path, text): |
|
1648 | 1648 | with open(path, 'ab') as fp: |
|
1649 | 1649 | fp.write(text) |
|
1650 | 1650 | |
|
1651 | 1651 | class chunkbuffer(object): |
|
1652 | 1652 | """Allow arbitrary sized chunks of data to be efficiently read from an |
|
1653 | 1653 | iterator over chunks of arbitrary size.""" |
|
1654 | 1654 | |
|
1655 | 1655 | def __init__(self, in_iter): |
|
1656 | 1656 | """in_iter is the iterator that's iterating over the input chunks. |
|
1657 | 1657 | targetsize is how big a buffer to try to maintain.""" |
|
1658 | 1658 | def splitbig(chunks): |
|
1659 | 1659 | for chunk in chunks: |
|
1660 | 1660 | if len(chunk) > 2**20: |
|
1661 | 1661 | pos = 0 |
|
1662 | 1662 | while pos < len(chunk): |
|
1663 | 1663 | end = pos + 2 ** 18 |
|
1664 | 1664 | yield chunk[pos:end] |
|
1665 | 1665 | pos = end |
|
1666 | 1666 | else: |
|
1667 | 1667 | yield chunk |
|
1668 | 1668 | self.iter = splitbig(in_iter) |
|
1669 | 1669 | self._queue = collections.deque() |
|
1670 | 1670 | self._chunkoffset = 0 |
|
1671 | 1671 | |
|
1672 | 1672 | def read(self, l=None): |
|
1673 | 1673 | """Read L bytes of data from the iterator of chunks of data. |
|
1674 | 1674 | Returns less than L bytes if the iterator runs dry. |
|
1675 | 1675 | |
|
1676 | 1676 | If size parameter is omitted, read everything""" |
|
1677 | 1677 | if l is None: |
|
1678 | 1678 | return ''.join(self.iter) |
|
1679 | 1679 | |
|
1680 | 1680 | left = l |
|
1681 | 1681 | buf = [] |
|
1682 | 1682 | queue = self._queue |
|
1683 | 1683 | while left > 0: |
|
1684 | 1684 | # refill the queue |
|
1685 | 1685 | if not queue: |
|
1686 | 1686 | target = 2**18 |
|
1687 | 1687 | for chunk in self.iter: |
|
1688 | 1688 | queue.append(chunk) |
|
1689 | 1689 | target -= len(chunk) |
|
1690 | 1690 | if target <= 0: |
|
1691 | 1691 | break |
|
1692 | 1692 | if not queue: |
|
1693 | 1693 | break |
|
1694 | 1694 | |
|
1695 | 1695 | # The easy way to do this would be to queue.popleft(), modify the |
|
1696 | 1696 | # chunk (if necessary), then queue.appendleft(). However, for cases |
|
1697 | 1697 | # where we read partial chunk content, this incurs 2 dequeue |
|
1698 | 1698 | # mutations and creates a new str for the remaining chunk in the |
|
1699 | 1699 | # queue. Our code below avoids this overhead. |
|
1700 | 1700 | |
|
1701 | 1701 | chunk = queue[0] |
|
1702 | 1702 | chunkl = len(chunk) |
|
1703 | 1703 | offset = self._chunkoffset |
|
1704 | 1704 | |
|
1705 | 1705 | # Use full chunk. |
|
1706 | 1706 | if offset == 0 and left >= chunkl: |
|
1707 | 1707 | left -= chunkl |
|
1708 | 1708 | queue.popleft() |
|
1709 | 1709 | buf.append(chunk) |
|
1710 | 1710 | # self._chunkoffset remains at 0. |
|
1711 | 1711 | continue |
|
1712 | 1712 | |
|
1713 | 1713 | chunkremaining = chunkl - offset |
|
1714 | 1714 | |
|
1715 | 1715 | # Use all of unconsumed part of chunk. |
|
1716 | 1716 | if left >= chunkremaining: |
|
1717 | 1717 | left -= chunkremaining |
|
1718 | 1718 | queue.popleft() |
|
1719 | 1719 | # offset == 0 is enabled by block above, so this won't merely |
|
1720 | 1720 | # copy via ``chunk[0:]``. |
|
1721 | 1721 | buf.append(chunk[offset:]) |
|
1722 | 1722 | self._chunkoffset = 0 |
|
1723 | 1723 | |
|
1724 | 1724 | # Partial chunk needed. |
|
1725 | 1725 | else: |
|
1726 | 1726 | buf.append(chunk[offset:offset + left]) |
|
1727 | 1727 | self._chunkoffset += left |
|
1728 | 1728 | left -= chunkremaining |
|
1729 | 1729 | |
|
1730 | 1730 | return ''.join(buf) |
|
1731 | 1731 | |
|
1732 | 1732 | def filechunkiter(f, size=131072, limit=None): |
|
1733 | 1733 | """Create a generator that produces the data in the file size |
|
1734 | 1734 | (default 131072) bytes at a time, up to optional limit (default is |
|
1735 | 1735 | to read all data). Chunks may be less than size bytes if the |
|
1736 | 1736 | chunk is the last chunk in the file, or the file is a socket or |
|
1737 | 1737 | some other type of file that sometimes reads less data than is |
|
1738 | 1738 | requested.""" |
|
1739 | 1739 | assert size >= 0 |
|
1740 | 1740 | assert limit is None or limit >= 0 |
|
1741 | 1741 | while True: |
|
1742 | 1742 | if limit is None: |
|
1743 | 1743 | nbytes = size |
|
1744 | 1744 | else: |
|
1745 | 1745 | nbytes = min(limit, size) |
|
1746 | 1746 | s = nbytes and f.read(nbytes) |
|
1747 | 1747 | if not s: |
|
1748 | 1748 | break |
|
1749 | 1749 | if limit: |
|
1750 | 1750 | limit -= len(s) |
|
1751 | 1751 | yield s |
|
1752 | 1752 | |
|
1753 | 1753 | def makedate(timestamp=None): |
|
1754 | 1754 | '''Return a unix timestamp (or the current time) as a (unixtime, |
|
1755 | 1755 | offset) tuple based off the local timezone.''' |
|
1756 | 1756 | if timestamp is None: |
|
1757 | 1757 | timestamp = time.time() |
|
1758 | 1758 | if timestamp < 0: |
|
1759 | 1759 | hint = _("check your clock") |
|
1760 | 1760 | raise Abort(_("negative timestamp: %d") % timestamp, hint=hint) |
|
1761 | 1761 | delta = (datetime.datetime.utcfromtimestamp(timestamp) - |
|
1762 | 1762 | datetime.datetime.fromtimestamp(timestamp)) |
|
1763 | 1763 | tz = delta.days * 86400 + delta.seconds |
|
1764 | 1764 | return timestamp, tz |
|
1765 | 1765 | |
|
1766 | 1766 | def datestr(date=None, format='%a %b %d %H:%M:%S %Y %1%2'): |
|
1767 | 1767 | """represent a (unixtime, offset) tuple as a localized time. |
|
1768 | 1768 | unixtime is seconds since the epoch, and offset is the time zone's |
|
1769 | 1769 | number of seconds away from UTC. |
|
1770 | 1770 | |
|
1771 | 1771 | >>> datestr((0, 0)) |
|
1772 | 1772 | 'Thu Jan 01 00:00:00 1970 +0000' |
|
1773 | 1773 | >>> datestr((42, 0)) |
|
1774 | 1774 | 'Thu Jan 01 00:00:42 1970 +0000' |
|
1775 | 1775 | >>> datestr((-42, 0)) |
|
1776 | 1776 | 'Wed Dec 31 23:59:18 1969 +0000' |
|
1777 | 1777 | >>> datestr((0x7fffffff, 0)) |
|
1778 | 1778 | 'Tue Jan 19 03:14:07 2038 +0000' |
|
1779 | 1779 | >>> datestr((-0x80000000, 0)) |
|
1780 | 1780 | 'Fri Dec 13 20:45:52 1901 +0000' |
|
1781 | 1781 | """ |
|
1782 | 1782 | t, tz = date or makedate() |
|
1783 | 1783 | if "%1" in format or "%2" in format or "%z" in format: |
|
1784 | 1784 | sign = (tz > 0) and "-" or "+" |
|
1785 | 1785 | minutes = abs(tz) // 60 |
|
1786 | 1786 | q, r = divmod(minutes, 60) |
|
1787 | 1787 | format = format.replace("%z", "%1%2") |
|
1788 | 1788 | format = format.replace("%1", "%c%02d" % (sign, q)) |
|
1789 | 1789 | format = format.replace("%2", "%02d" % r) |
|
1790 | 1790 | d = t - tz |
|
1791 | 1791 | if d > 0x7fffffff: |
|
1792 | 1792 | d = 0x7fffffff |
|
1793 | 1793 | elif d < -0x80000000: |
|
1794 | 1794 | d = -0x80000000 |
|
1795 | 1795 | # Never use time.gmtime() and datetime.datetime.fromtimestamp() |
|
1796 | 1796 | # because they use the gmtime() system call which is buggy on Windows |
|
1797 | 1797 | # for negative values. |
|
1798 | 1798 | t = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=d) |
|
1799 | 1799 | s = t.strftime(format) |
|
1800 | 1800 | return s |
|
1801 | 1801 | |
|
1802 | 1802 | def shortdate(date=None): |
|
1803 | 1803 | """turn (timestamp, tzoff) tuple into iso 8631 date.""" |
|
1804 | 1804 | return datestr(date, format='%Y-%m-%d') |
|
1805 | 1805 | |
|
1806 | 1806 | def parsetimezone(s): |
|
1807 | 1807 | """find a trailing timezone, if any, in string, and return a |
|
1808 | 1808 | (offset, remainder) pair""" |
|
1809 | 1809 | |
|
1810 | 1810 | if s.endswith("GMT") or s.endswith("UTC"): |
|
1811 | 1811 | return 0, s[:-3].rstrip() |
|
1812 | 1812 | |
|
1813 | 1813 | # Unix-style timezones [+-]hhmm |
|
1814 | 1814 | if len(s) >= 5 and s[-5] in "+-" and s[-4:].isdigit(): |
|
1815 | 1815 | sign = (s[-5] == "+") and 1 or -1 |
|
1816 | 1816 | hours = int(s[-4:-2]) |
|
1817 | 1817 | minutes = int(s[-2:]) |
|
1818 | 1818 | return -sign * (hours * 60 + minutes) * 60, s[:-5].rstrip() |
|
1819 | 1819 | |
|
1820 | 1820 | # ISO8601 trailing Z |
|
1821 | 1821 | if s.endswith("Z") and s[-2:-1].isdigit(): |
|
1822 | 1822 | return 0, s[:-1] |
|
1823 | 1823 | |
|
1824 | 1824 | # ISO8601-style [+-]hh:mm |
|
1825 | 1825 | if (len(s) >= 6 and s[-6] in "+-" and s[-3] == ":" and |
|
1826 | 1826 | s[-5:-3].isdigit() and s[-2:].isdigit()): |
|
1827 | 1827 | sign = (s[-6] == "+") and 1 or -1 |
|
1828 | 1828 | hours = int(s[-5:-3]) |
|
1829 | 1829 | minutes = int(s[-2:]) |
|
1830 | 1830 | return -sign * (hours * 60 + minutes) * 60, s[:-6] |
|
1831 | 1831 | |
|
1832 | 1832 | return None, s |
|
1833 | 1833 | |
|
1834 | 1834 | def strdate(string, format, defaults=[]): |
|
1835 | 1835 | """parse a localized time string and return a (unixtime, offset) tuple. |
|
1836 | 1836 | if the string cannot be parsed, ValueError is raised.""" |
|
1837 | 1837 | # NOTE: unixtime = localunixtime + offset |
|
1838 | 1838 | offset, date = parsetimezone(string) |
|
1839 | 1839 | |
|
1840 | 1840 | # add missing elements from defaults |
|
1841 | 1841 | usenow = False # default to using biased defaults |
|
1842 | 1842 | for part in ("S", "M", "HI", "d", "mb", "yY"): # decreasing specificity |
|
1843 | 1843 | found = [True for p in part if ("%"+p) in format] |
|
1844 | 1844 | if not found: |
|
1845 | 1845 | date += "@" + defaults[part][usenow] |
|
1846 | 1846 | format += "@%" + part[0] |
|
1847 | 1847 | else: |
|
1848 | 1848 | # We've found a specific time element, less specific time |
|
1849 | 1849 | # elements are relative to today |
|
1850 | 1850 | usenow = True |
|
1851 | 1851 | |
|
1852 | 1852 | timetuple = time.strptime(date, format) |
|
1853 | 1853 | localunixtime = int(calendar.timegm(timetuple)) |
|
1854 | 1854 | if offset is None: |
|
1855 | 1855 | # local timezone |
|
1856 | 1856 | unixtime = int(time.mktime(timetuple)) |
|
1857 | 1857 | offset = unixtime - localunixtime |
|
1858 | 1858 | else: |
|
1859 | 1859 | unixtime = localunixtime + offset |
|
1860 | 1860 | return unixtime, offset |
|
1861 | 1861 | |
|
1862 | 1862 | def parsedate(date, formats=None, bias=None): |
|
1863 | 1863 | """parse a localized date/time and return a (unixtime, offset) tuple. |
|
1864 | 1864 | |
|
1865 | 1865 | The date may be a "unixtime offset" string or in one of the specified |
|
1866 | 1866 | formats. If the date already is a (unixtime, offset) tuple, it is returned. |
|
1867 | 1867 | |
|
1868 | 1868 | >>> parsedate(' today ') == parsedate(\ |
|
1869 | 1869 | datetime.date.today().strftime('%b %d')) |
|
1870 | 1870 | True |
|
1871 | 1871 | >>> parsedate( 'yesterday ') == parsedate((datetime.date.today() -\ |
|
1872 | 1872 | datetime.timedelta(days=1)\ |
|
1873 | 1873 | ).strftime('%b %d')) |
|
1874 | 1874 | True |
|
1875 | 1875 | >>> now, tz = makedate() |
|
1876 | 1876 | >>> strnow, strtz = parsedate('now') |
|
1877 | 1877 | >>> (strnow - now) < 1 |
|
1878 | 1878 | True |
|
1879 | 1879 | >>> tz == strtz |
|
1880 | 1880 | True |
|
1881 | 1881 | """ |
|
1882 | 1882 | if bias is None: |
|
1883 | 1883 | bias = {} |
|
1884 | 1884 | if not date: |
|
1885 | 1885 | return 0, 0 |
|
1886 | 1886 | if isinstance(date, tuple) and len(date) == 2: |
|
1887 | 1887 | return date |
|
1888 | 1888 | if not formats: |
|
1889 | 1889 | formats = defaultdateformats |
|
1890 | 1890 | date = date.strip() |
|
1891 | 1891 | |
|
1892 | 1892 | if date == 'now' or date == _('now'): |
|
1893 | 1893 | return makedate() |
|
1894 | 1894 | if date == 'today' or date == _('today'): |
|
1895 | 1895 | date = datetime.date.today().strftime('%b %d') |
|
1896 | 1896 | elif date == 'yesterday' or date == _('yesterday'): |
|
1897 | 1897 | date = (datetime.date.today() - |
|
1898 | 1898 | datetime.timedelta(days=1)).strftime('%b %d') |
|
1899 | 1899 | |
|
1900 | 1900 | try: |
|
1901 | 1901 | when, offset = map(int, date.split(' ')) |
|
1902 | 1902 | except ValueError: |
|
1903 | 1903 | # fill out defaults |
|
1904 | 1904 | now = makedate() |
|
1905 | 1905 | defaults = {} |
|
1906 | 1906 | for part in ("d", "mb", "yY", "HI", "M", "S"): |
|
1907 | 1907 | # this piece is for rounding the specific end of unknowns |
|
1908 | 1908 | b = bias.get(part) |
|
1909 | 1909 | if b is None: |
|
1910 | 1910 | if part[0] in "HMS": |
|
1911 | 1911 | b = "00" |
|
1912 | 1912 | else: |
|
1913 | 1913 | b = "0" |
|
1914 | 1914 | |
|
1915 | 1915 | # this piece is for matching the generic end to today's date |
|
1916 | 1916 | n = datestr(now, "%" + part[0]) |
|
1917 | 1917 | |
|
1918 | 1918 | defaults[part] = (b, n) |
|
1919 | 1919 | |
|
1920 | 1920 | for format in formats: |
|
1921 | 1921 | try: |
|
1922 | 1922 | when, offset = strdate(date, format, defaults) |
|
1923 | 1923 | except (ValueError, OverflowError): |
|
1924 | 1924 | pass |
|
1925 | 1925 | else: |
|
1926 | 1926 | break |
|
1927 | 1927 | else: |
|
1928 | 1928 | raise Abort(_('invalid date: %r') % date) |
|
1929 | 1929 | # validate explicit (probably user-specified) date and |
|
1930 | 1930 | # time zone offset. values must fit in signed 32 bits for |
|
1931 | 1931 | # current 32-bit linux runtimes. timezones go from UTC-12 |
|
1932 | 1932 | # to UTC+14 |
|
1933 | 1933 | if when < -0x80000000 or when > 0x7fffffff: |
|
1934 | 1934 | raise Abort(_('date exceeds 32 bits: %d') % when) |
|
1935 | 1935 | if offset < -50400 or offset > 43200: |
|
1936 | 1936 | raise Abort(_('impossible time zone offset: %d') % offset) |
|
1937 | 1937 | return when, offset |
|
1938 | 1938 | |
|
1939 | 1939 | def matchdate(date): |
|
1940 | 1940 | """Return a function that matches a given date match specifier |
|
1941 | 1941 | |
|
1942 | 1942 | Formats include: |
|
1943 | 1943 | |
|
1944 | 1944 | '{date}' match a given date to the accuracy provided |
|
1945 | 1945 | |
|
1946 | 1946 | '<{date}' on or before a given date |
|
1947 | 1947 | |
|
1948 | 1948 | '>{date}' on or after a given date |
|
1949 | 1949 | |
|
1950 | 1950 | >>> p1 = parsedate("10:29:59") |
|
1951 | 1951 | >>> p2 = parsedate("10:30:00") |
|
1952 | 1952 | >>> p3 = parsedate("10:30:59") |
|
1953 | 1953 | >>> p4 = parsedate("10:31:00") |
|
1954 | 1954 | >>> p5 = parsedate("Sep 15 10:30:00 1999") |
|
1955 | 1955 | >>> f = matchdate("10:30") |
|
1956 | 1956 | >>> f(p1[0]) |
|
1957 | 1957 | False |
|
1958 | 1958 | >>> f(p2[0]) |
|
1959 | 1959 | True |
|
1960 | 1960 | >>> f(p3[0]) |
|
1961 | 1961 | True |
|
1962 | 1962 | >>> f(p4[0]) |
|
1963 | 1963 | False |
|
1964 | 1964 | >>> f(p5[0]) |
|
1965 | 1965 | False |
|
1966 | 1966 | """ |
|
1967 | 1967 | |
|
1968 | 1968 | def lower(date): |
|
1969 | 1969 | d = {'mb': "1", 'd': "1"} |
|
1970 | 1970 | return parsedate(date, extendeddateformats, d)[0] |
|
1971 | 1971 | |
|
1972 | 1972 | def upper(date): |
|
1973 | 1973 | d = {'mb': "12", 'HI': "23", 'M': "59", 'S': "59"} |
|
1974 | 1974 | for days in ("31", "30", "29"): |
|
1975 | 1975 | try: |
|
1976 | 1976 | d["d"] = days |
|
1977 | 1977 | return parsedate(date, extendeddateformats, d)[0] |
|
1978 | 1978 | except Abort: |
|
1979 | 1979 | pass |
|
1980 | 1980 | d["d"] = "28" |
|
1981 | 1981 | return parsedate(date, extendeddateformats, d)[0] |
|
1982 | 1982 | |
|
1983 | 1983 | date = date.strip() |
|
1984 | 1984 | |
|
1985 | 1985 | if not date: |
|
1986 | 1986 | raise Abort(_("dates cannot consist entirely of whitespace")) |
|
1987 | 1987 | elif date[0] == "<": |
|
1988 | 1988 | if not date[1:]: |
|
1989 | 1989 | raise Abort(_("invalid day spec, use '<DATE'")) |
|
1990 | 1990 | when = upper(date[1:]) |
|
1991 | 1991 | return lambda x: x <= when |
|
1992 | 1992 | elif date[0] == ">": |
|
1993 | 1993 | if not date[1:]: |
|
1994 | 1994 | raise Abort(_("invalid day spec, use '>DATE'")) |
|
1995 | 1995 | when = lower(date[1:]) |
|
1996 | 1996 | return lambda x: x >= when |
|
1997 | 1997 | elif date[0] == "-": |
|
1998 | 1998 | try: |
|
1999 | 1999 | days = int(date[1:]) |
|
2000 | 2000 | except ValueError: |
|
2001 | 2001 | raise Abort(_("invalid day spec: %s") % date[1:]) |
|
2002 | 2002 | if days < 0: |
|
2003 | 2003 | raise Abort(_("%s must be nonnegative (see 'hg help dates')") |
|
2004 | 2004 | % date[1:]) |
|
2005 | 2005 | when = makedate()[0] - days * 3600 * 24 |
|
2006 | 2006 | return lambda x: x >= when |
|
2007 | 2007 | elif " to " in date: |
|
2008 | 2008 | a, b = date.split(" to ") |
|
2009 | 2009 | start, stop = lower(a), upper(b) |
|
2010 | 2010 | return lambda x: x >= start and x <= stop |
|
2011 | 2011 | else: |
|
2012 | 2012 | start, stop = lower(date), upper(date) |
|
2013 | 2013 | return lambda x: x >= start and x <= stop |
|
2014 | 2014 | |
|
2015 | 2015 | def stringmatcher(pattern, casesensitive=True): |
|
2016 | 2016 | """ |
|
2017 | 2017 | accepts a string, possibly starting with 're:' or 'literal:' prefix. |
|
2018 | 2018 | returns the matcher name, pattern, and matcher function. |
|
2019 | 2019 | missing or unknown prefixes are treated as literal matches. |
|
2020 | 2020 | |
|
2021 | 2021 | helper for tests: |
|
2022 | 2022 | >>> def test(pattern, *tests): |
|
2023 | 2023 | ... kind, pattern, matcher = stringmatcher(pattern) |
|
2024 | 2024 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) |
|
2025 | 2025 | >>> def itest(pattern, *tests): |
|
2026 | 2026 | ... kind, pattern, matcher = stringmatcher(pattern, casesensitive=False) |
|
2027 | 2027 | ... return (kind, pattern, [bool(matcher(t)) for t in tests]) |
|
2028 | 2028 | |
|
2029 | 2029 | exact matching (no prefix): |
|
2030 | 2030 | >>> test('abcdefg', 'abc', 'def', 'abcdefg') |
|
2031 | 2031 | ('literal', 'abcdefg', [False, False, True]) |
|
2032 | 2032 | |
|
2033 | 2033 | regex matching ('re:' prefix) |
|
2034 | 2034 | >>> test('re:a.+b', 'nomatch', 'fooadef', 'fooadefbar') |
|
2035 | 2035 | ('re', 'a.+b', [False, False, True]) |
|
2036 | 2036 | |
|
2037 | 2037 | force exact matches ('literal:' prefix) |
|
2038 | 2038 | >>> test('literal:re:foobar', 'foobar', 're:foobar') |
|
2039 | 2039 | ('literal', 're:foobar', [False, True]) |
|
2040 | 2040 | |
|
2041 | 2041 | unknown prefixes are ignored and treated as literals |
|
2042 | 2042 | >>> test('foo:bar', 'foo', 'bar', 'foo:bar') |
|
2043 | 2043 | ('literal', 'foo:bar', [False, False, True]) |
|
2044 | 2044 | |
|
2045 | 2045 | case insensitive regex matches |
|
2046 | 2046 | >>> itest('re:A.+b', 'nomatch', 'fooadef', 'fooadefBar') |
|
2047 | 2047 | ('re', 'A.+b', [False, False, True]) |
|
2048 | 2048 | |
|
2049 | 2049 | case insensitive literal matches |
|
2050 | 2050 | >>> itest('ABCDEFG', 'abc', 'def', 'abcdefg') |
|
2051 | 2051 | ('literal', 'ABCDEFG', [False, False, True]) |
|
2052 | 2052 | """ |
|
2053 | 2053 | if pattern.startswith('re:'): |
|
2054 | 2054 | pattern = pattern[3:] |
|
2055 | 2055 | try: |
|
2056 | 2056 | flags = 0 |
|
2057 | 2057 | if not casesensitive: |
|
2058 | 2058 | flags = remod.I |
|
2059 | 2059 | regex = remod.compile(pattern, flags) |
|
2060 | 2060 | except remod.error as e: |
|
2061 | 2061 | raise error.ParseError(_('invalid regular expression: %s') |
|
2062 | 2062 | % e) |
|
2063 | 2063 | return 're', pattern, regex.search |
|
2064 | 2064 | elif pattern.startswith('literal:'): |
|
2065 | 2065 | pattern = pattern[8:] |
|
2066 | 2066 | |
|
2067 | 2067 | match = pattern.__eq__ |
|
2068 | 2068 | |
|
2069 | 2069 | if not casesensitive: |
|
2070 | 2070 | ipat = encoding.lower(pattern) |
|
2071 | 2071 | match = lambda s: ipat == encoding.lower(s) |
|
2072 | 2072 | return 'literal', pattern, match |
|
2073 | 2073 | |
|
2074 | 2074 | def shortuser(user): |
|
2075 | 2075 | """Return a short representation of a user name or email address.""" |
|
2076 | 2076 | f = user.find('@') |
|
2077 | 2077 | if f >= 0: |
|
2078 | 2078 | user = user[:f] |
|
2079 | 2079 | f = user.find('<') |
|
2080 | 2080 | if f >= 0: |
|
2081 | 2081 | user = user[f + 1:] |
|
2082 | 2082 | f = user.find(' ') |
|
2083 | 2083 | if f >= 0: |
|
2084 | 2084 | user = user[:f] |
|
2085 | 2085 | f = user.find('.') |
|
2086 | 2086 | if f >= 0: |
|
2087 | 2087 | user = user[:f] |
|
2088 | 2088 | return user |
|
2089 | 2089 | |
|
2090 | 2090 | def emailuser(user): |
|
2091 | 2091 | """Return the user portion of an email address.""" |
|
2092 | 2092 | f = user.find('@') |
|
2093 | 2093 | if f >= 0: |
|
2094 | 2094 | user = user[:f] |
|
2095 | 2095 | f = user.find('<') |
|
2096 | 2096 | if f >= 0: |
|
2097 | 2097 | user = user[f + 1:] |
|
2098 | 2098 | return user |
|
2099 | 2099 | |
|
2100 | 2100 | def email(author): |
|
2101 | 2101 | '''get email of author.''' |
|
2102 | 2102 | r = author.find('>') |
|
2103 | 2103 | if r == -1: |
|
2104 | 2104 | r = None |
|
2105 | 2105 | return author[author.find('<') + 1:r] |
|
2106 | 2106 | |
|
2107 | 2107 | def ellipsis(text, maxlength=400): |
|
2108 | 2108 | """Trim string to at most maxlength (default: 400) columns in display.""" |
|
2109 | 2109 | return encoding.trim(text, maxlength, ellipsis='...') |
|
2110 | 2110 | |
|
2111 | 2111 | def unitcountfn(*unittable): |
|
2112 | 2112 | '''return a function that renders a readable count of some quantity''' |
|
2113 | 2113 | |
|
2114 | 2114 | def go(count): |
|
2115 | 2115 | for multiplier, divisor, format in unittable: |
|
2116 | 2116 | if count >= divisor * multiplier: |
|
2117 | 2117 | return format % (count / float(divisor)) |
|
2118 | 2118 | return unittable[-1][2] % count |
|
2119 | 2119 | |
|
2120 | 2120 | return go |
|
2121 | 2121 | |
|
2122 | 2122 | bytecount = unitcountfn( |
|
2123 | 2123 | (100, 1 << 30, _('%.0f GB')), |
|
2124 | 2124 | (10, 1 << 30, _('%.1f GB')), |
|
2125 | 2125 | (1, 1 << 30, _('%.2f GB')), |
|
2126 | 2126 | (100, 1 << 20, _('%.0f MB')), |
|
2127 | 2127 | (10, 1 << 20, _('%.1f MB')), |
|
2128 | 2128 | (1, 1 << 20, _('%.2f MB')), |
|
2129 | 2129 | (100, 1 << 10, _('%.0f KB')), |
|
2130 | 2130 | (10, 1 << 10, _('%.1f KB')), |
|
2131 | 2131 | (1, 1 << 10, _('%.2f KB')), |
|
2132 | 2132 | (1, 1, _('%.0f bytes')), |
|
2133 | 2133 | ) |
|
2134 | 2134 | |
|
2135 | 2135 | def uirepr(s): |
|
2136 | 2136 | # Avoid double backslash in Windows path repr() |
|
2137 | 2137 | return repr(s).replace('\\\\', '\\') |
|
2138 | 2138 | |
|
2139 | 2139 | # delay import of textwrap |
|
2140 | 2140 | def MBTextWrapper(**kwargs): |
|
2141 | 2141 | class tw(textwrap.TextWrapper): |
|
2142 | 2142 | """ |
|
2143 | 2143 | Extend TextWrapper for width-awareness. |
|
2144 | 2144 | |
|
2145 | 2145 | Neither number of 'bytes' in any encoding nor 'characters' is |
|
2146 | 2146 | appropriate to calculate terminal columns for specified string. |
|
2147 | 2147 | |
|
2148 | 2148 | Original TextWrapper implementation uses built-in 'len()' directly, |
|
2149 | 2149 | so overriding is needed to use width information of each characters. |
|
2150 | 2150 | |
|
2151 | 2151 | In addition, characters classified into 'ambiguous' width are |
|
2152 | 2152 | treated as wide in East Asian area, but as narrow in other. |
|
2153 | 2153 | |
|
2154 | 2154 | This requires use decision to determine width of such characters. |
|
2155 | 2155 | """ |
|
2156 | 2156 | def _cutdown(self, ucstr, space_left): |
|
2157 | 2157 | l = 0 |
|
2158 | 2158 | colwidth = encoding.ucolwidth |
|
2159 | 2159 | for i in xrange(len(ucstr)): |
|
2160 | 2160 | l += colwidth(ucstr[i]) |
|
2161 | 2161 | if space_left < l: |
|
2162 | 2162 | return (ucstr[:i], ucstr[i:]) |
|
2163 | 2163 | return ucstr, '' |
|
2164 | 2164 | |
|
2165 | 2165 | # overriding of base class |
|
2166 | 2166 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
|
2167 | 2167 | space_left = max(width - cur_len, 1) |
|
2168 | 2168 | |
|
2169 | 2169 | if self.break_long_words: |
|
2170 | 2170 | cut, res = self._cutdown(reversed_chunks[-1], space_left) |
|
2171 | 2171 | cur_line.append(cut) |
|
2172 | 2172 | reversed_chunks[-1] = res |
|
2173 | 2173 | elif not cur_line: |
|
2174 | 2174 | cur_line.append(reversed_chunks.pop()) |
|
2175 | 2175 | |
|
2176 | 2176 | # this overriding code is imported from TextWrapper of Python 2.6 |
|
2177 | 2177 | # to calculate columns of string by 'encoding.ucolwidth()' |
|
2178 | 2178 | def _wrap_chunks(self, chunks): |
|
2179 | 2179 | colwidth = encoding.ucolwidth |
|
2180 | 2180 | |
|
2181 | 2181 | lines = [] |
|
2182 | 2182 | if self.width <= 0: |
|
2183 | 2183 | raise ValueError("invalid width %r (must be > 0)" % self.width) |
|
2184 | 2184 | |
|
2185 | 2185 | # Arrange in reverse order so items can be efficiently popped |
|
2186 | 2186 | # from a stack of chucks. |
|
2187 | 2187 | chunks.reverse() |
|
2188 | 2188 | |
|
2189 | 2189 | while chunks: |
|
2190 | 2190 | |
|
2191 | 2191 | # Start the list of chunks that will make up the current line. |
|
2192 | 2192 | # cur_len is just the length of all the chunks in cur_line. |
|
2193 | 2193 | cur_line = [] |
|
2194 | 2194 | cur_len = 0 |
|
2195 | 2195 | |
|
2196 | 2196 | # Figure out which static string will prefix this line. |
|
2197 | 2197 | if lines: |
|
2198 | 2198 | indent = self.subsequent_indent |
|
2199 | 2199 | else: |
|
2200 | 2200 | indent = self.initial_indent |
|
2201 | 2201 | |
|
2202 | 2202 | # Maximum width for this line. |
|
2203 | 2203 | width = self.width - len(indent) |
|
2204 | 2204 | |
|
2205 | 2205 | # First chunk on line is whitespace -- drop it, unless this |
|
2206 | 2206 | # is the very beginning of the text (i.e. no lines started yet). |
|
2207 | 2207 | if self.drop_whitespace and chunks[-1].strip() == '' and lines: |
|
2208 | 2208 | del chunks[-1] |
|
2209 | 2209 | |
|
2210 | 2210 | while chunks: |
|
2211 | 2211 | l = colwidth(chunks[-1]) |
|
2212 | 2212 | |
|
2213 | 2213 | # Can at least squeeze this chunk onto the current line. |
|
2214 | 2214 | if cur_len + l <= width: |
|
2215 | 2215 | cur_line.append(chunks.pop()) |
|
2216 | 2216 | cur_len += l |
|
2217 | 2217 | |
|
2218 | 2218 | # Nope, this line is full. |
|
2219 | 2219 | else: |
|
2220 | 2220 | break |
|
2221 | 2221 | |
|
2222 | 2222 | # The current line is full, and the next chunk is too big to |
|
2223 | 2223 | # fit on *any* line (not just this one). |
|
2224 | 2224 | if chunks and colwidth(chunks[-1]) > width: |
|
2225 | 2225 | self._handle_long_word(chunks, cur_line, cur_len, width) |
|
2226 | 2226 | |
|
2227 | 2227 | # If the last chunk on this line is all whitespace, drop it. |
|
2228 | 2228 | if (self.drop_whitespace and |
|
2229 | 2229 | cur_line and cur_line[-1].strip() == ''): |
|
2230 | 2230 | del cur_line[-1] |
|
2231 | 2231 | |
|
2232 | 2232 | # Convert current line back to a string and store it in list |
|
2233 | 2233 | # of all lines (return value). |
|
2234 | 2234 | if cur_line: |
|
2235 | 2235 | lines.append(indent + ''.join(cur_line)) |
|
2236 | 2236 | |
|
2237 | 2237 | return lines |
|
2238 | 2238 | |
|
2239 | 2239 | global MBTextWrapper |
|
2240 | 2240 | MBTextWrapper = tw |
|
2241 | 2241 | return tw(**kwargs) |
|
2242 | 2242 | |
|
2243 | 2243 | def wrap(line, width, initindent='', hangindent=''): |
|
2244 | 2244 | maxindent = max(len(hangindent), len(initindent)) |
|
2245 | 2245 | if width <= maxindent: |
|
2246 | 2246 | # adjust for weird terminal size |
|
2247 | 2247 | width = max(78, maxindent + 1) |
|
2248 | 2248 | line = line.decode(encoding.encoding, encoding.encodingmode) |
|
2249 | 2249 | initindent = initindent.decode(encoding.encoding, encoding.encodingmode) |
|
2250 | 2250 | hangindent = hangindent.decode(encoding.encoding, encoding.encodingmode) |
|
2251 | 2251 | wrapper = MBTextWrapper(width=width, |
|
2252 | 2252 | initial_indent=initindent, |
|
2253 | 2253 | subsequent_indent=hangindent) |
|
2254 | 2254 | return wrapper.fill(line).encode(encoding.encoding) |
|
2255 | 2255 | |
|
2256 | 2256 | if (pyplatform.python_implementation() == 'CPython' and |
|
2257 | 2257 | sys.version_info < (3, 0)): |
|
2258 | 2258 | # There is an issue in CPython that some IO methods do not handle EINTR |
|
2259 | 2259 | # correctly. The following table shows what CPython version (and functions) |
|
2260 | 2260 | # are affected (buggy: has the EINTR bug, okay: otherwise): |
|
2261 | 2261 | # |
|
2262 | 2262 | # | < 2.7.4 | 2.7.4 to 2.7.12 | >= 3.0 |
|
2263 | 2263 | # -------------------------------------------------- |
|
2264 | 2264 | # fp.__iter__ | buggy | buggy | okay |
|
2265 | 2265 | # fp.read* | buggy | okay [1] | okay |
|
2266 | 2266 | # |
|
2267 | 2267 | # [1]: fixed by changeset 67dc99a989cd in the cpython hg repo. |
|
2268 | 2268 | # |
|
2269 | 2269 | # Here we workaround the EINTR issue for fileobj.__iter__. Other methods |
|
2270 | 2270 | # like "read*" are ignored for now, as Python < 2.7.4 is a minority. |
|
2271 | 2271 | # |
|
2272 | 2272 | # Although we can workaround the EINTR issue for fp.__iter__, it is slower: |
|
2273 | 2273 | # "for x in fp" is 4x faster than "for x in iter(fp.readline, '')" in |
|
2274 | 2274 | # CPython 2, because CPython 2 maintains an internal readahead buffer for |
|
2275 | 2275 | # fp.__iter__ but not other fp.read* methods. |
|
2276 | 2276 | # |
|
2277 | 2277 | # On modern systems like Linux, the "read" syscall cannot be interrupted |
|
2278 | 2278 | # when reading "fast" files like on-disk files. So the EINTR issue only |
|
2279 | 2279 | # affects things like pipes, sockets, ttys etc. We treat "normal" (S_ISREG) |
|
2280 | 2280 | # files approximately as "fast" files and use the fast (unsafe) code path, |
|
2281 | 2281 | # to minimize the performance impact. |
|
2282 | 2282 | if sys.version_info >= (2, 7, 4): |
|
2283 | 2283 | # fp.readline deals with EINTR correctly, use it as a workaround. |
|
2284 | 2284 | def _safeiterfile(fp): |
|
2285 | 2285 | return iter(fp.readline, '') |
|
2286 | 2286 | else: |
|
2287 | 2287 | # fp.read* are broken too, manually deal with EINTR in a stupid way. |
|
2288 | 2288 | # note: this may block longer than necessary because of bufsize. |
|
2289 | 2289 | def _safeiterfile(fp, bufsize=4096): |
|
2290 | 2290 | fd = fp.fileno() |
|
2291 | 2291 | line = '' |
|
2292 | 2292 | while True: |
|
2293 | 2293 | try: |
|
2294 | 2294 | buf = os.read(fd, bufsize) |
|
2295 | 2295 | except OSError as ex: |
|
2296 | 2296 | # os.read only raises EINTR before any data is read |
|
2297 | 2297 | if ex.errno == errno.EINTR: |
|
2298 | 2298 | continue |
|
2299 | 2299 | else: |
|
2300 | 2300 | raise |
|
2301 | 2301 | line += buf |
|
2302 | 2302 | if '\n' in buf: |
|
2303 | 2303 | splitted = line.splitlines(True) |
|
2304 | 2304 | line = '' |
|
2305 | 2305 | for l in splitted: |
|
2306 | 2306 | if l[-1] == '\n': |
|
2307 | 2307 | yield l |
|
2308 | 2308 | else: |
|
2309 | 2309 | line = l |
|
2310 | 2310 | if not buf: |
|
2311 | 2311 | break |
|
2312 | 2312 | if line: |
|
2313 | 2313 | yield line |
|
2314 | 2314 | |
|
2315 | 2315 | def iterfile(fp): |
|
2316 | 2316 | fastpath = True |
|
2317 | 2317 | if type(fp) is file: |
|
2318 | 2318 | fastpath = stat.S_ISREG(os.fstat(fp.fileno()).st_mode) |
|
2319 | 2319 | if fastpath: |
|
2320 | 2320 | return fp |
|
2321 | 2321 | else: |
|
2322 | 2322 | return _safeiterfile(fp) |
|
2323 | 2323 | else: |
|
2324 | 2324 | # PyPy and CPython 3 do not have the EINTR issue thus no workaround needed. |
|
2325 | 2325 | def iterfile(fp): |
|
2326 | 2326 | return fp |
|
2327 | 2327 | |
|
2328 | 2328 | def iterlines(iterator): |
|
2329 | 2329 | for chunk in iterator: |
|
2330 | 2330 | for line in chunk.splitlines(): |
|
2331 | 2331 | yield line |
|
2332 | 2332 | |
|
2333 | 2333 | def expandpath(path): |
|
2334 | 2334 | return os.path.expanduser(os.path.expandvars(path)) |
|
2335 | 2335 | |
|
2336 | 2336 | def hgcmd(): |
|
2337 | 2337 | """Return the command used to execute current hg |
|
2338 | 2338 | |
|
2339 | 2339 | This is different from hgexecutable() because on Windows we want |
|
2340 | 2340 | to avoid things opening new shell windows like batch files, so we |
|
2341 | 2341 | get either the python call or current executable. |
|
2342 | 2342 | """ |
|
2343 | 2343 | if mainfrozen(): |
|
2344 | 2344 | if getattr(sys, 'frozen', None) == 'macosx_app': |
|
2345 | 2345 | # Env variable set by py2app |
|
2346 | 2346 | return [encoding.environ['EXECUTABLEPATH']] |
|
2347 | 2347 | else: |
|
2348 | 2348 | return [pycompat.sysexecutable] |
|
2349 | 2349 | return gethgcmd() |
|
2350 | 2350 | |
|
2351 | 2351 | def rundetached(args, condfn): |
|
2352 | 2352 | """Execute the argument list in a detached process. |
|
2353 | 2353 | |
|
2354 | 2354 | condfn is a callable which is called repeatedly and should return |
|
2355 | 2355 | True once the child process is known to have started successfully. |
|
2356 | 2356 | At this point, the child process PID is returned. If the child |
|
2357 | 2357 | process fails to start or finishes before condfn() evaluates to |
|
2358 | 2358 | True, return -1. |
|
2359 | 2359 | """ |
|
2360 | 2360 | # Windows case is easier because the child process is either |
|
2361 | 2361 | # successfully starting and validating the condition or exiting |
|
2362 | 2362 | # on failure. We just poll on its PID. On Unix, if the child |
|
2363 | 2363 | # process fails to start, it will be left in a zombie state until |
|
2364 | 2364 | # the parent wait on it, which we cannot do since we expect a long |
|
2365 | 2365 | # running process on success. Instead we listen for SIGCHLD telling |
|
2366 | 2366 | # us our child process terminated. |
|
2367 | 2367 | terminated = set() |
|
2368 | 2368 | def handler(signum, frame): |
|
2369 | 2369 | terminated.add(os.wait()) |
|
2370 | 2370 | prevhandler = None |
|
2371 | 2371 | SIGCHLD = getattr(signal, 'SIGCHLD', None) |
|
2372 | 2372 | if SIGCHLD is not None: |
|
2373 | 2373 | prevhandler = signal.signal(SIGCHLD, handler) |
|
2374 | 2374 | try: |
|
2375 | 2375 | pid = spawndetached(args) |
|
2376 | 2376 | while not condfn(): |
|
2377 | 2377 | if ((pid in terminated or not testpid(pid)) |
|
2378 | 2378 | and not condfn()): |
|
2379 | 2379 | return -1 |
|
2380 | 2380 | time.sleep(0.1) |
|
2381 | 2381 | return pid |
|
2382 | 2382 | finally: |
|
2383 | 2383 | if prevhandler is not None: |
|
2384 | 2384 | signal.signal(signal.SIGCHLD, prevhandler) |
|
2385 | 2385 | |
|
2386 | 2386 | def interpolate(prefix, mapping, s, fn=None, escape_prefix=False): |
|
2387 | 2387 | """Return the result of interpolating items in the mapping into string s. |
|
2388 | 2388 | |
|
2389 | 2389 | prefix is a single character string, or a two character string with |
|
2390 | 2390 | a backslash as the first character if the prefix needs to be escaped in |
|
2391 | 2391 | a regular expression. |
|
2392 | 2392 | |
|
2393 | 2393 | fn is an optional function that will be applied to the replacement text |
|
2394 | 2394 | just before replacement. |
|
2395 | 2395 | |
|
2396 | 2396 | escape_prefix is an optional flag that allows using doubled prefix for |
|
2397 | 2397 | its escaping. |
|
2398 | 2398 | """ |
|
2399 | 2399 | fn = fn or (lambda s: s) |
|
2400 | 2400 | patterns = '|'.join(mapping.keys()) |
|
2401 | 2401 | if escape_prefix: |
|
2402 | 2402 | patterns += '|' + prefix |
|
2403 | 2403 | if len(prefix) > 1: |
|
2404 | 2404 | prefix_char = prefix[1:] |
|
2405 | 2405 | else: |
|
2406 | 2406 | prefix_char = prefix |
|
2407 | 2407 | mapping[prefix_char] = prefix_char |
|
2408 | 2408 | r = remod.compile(r'%s(%s)' % (prefix, patterns)) |
|
2409 | 2409 | return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) |
|
2410 | 2410 | |
|
2411 | 2411 | def getport(port): |
|
2412 | 2412 | """Return the port for a given network service. |
|
2413 | 2413 | |
|
2414 | 2414 | If port is an integer, it's returned as is. If it's a string, it's |
|
2415 | 2415 | looked up using socket.getservbyname(). If there's no matching |
|
2416 | 2416 | service, error.Abort is raised. |
|
2417 | 2417 | """ |
|
2418 | 2418 | try: |
|
2419 | 2419 | return int(port) |
|
2420 | 2420 | except ValueError: |
|
2421 | 2421 | pass |
|
2422 | 2422 | |
|
2423 | 2423 | try: |
|
2424 | 2424 | return socket.getservbyname(port) |
|
2425 | 2425 | except socket.error: |
|
2426 | 2426 | raise Abort(_("no port number associated with service '%s'") % port) |
|
2427 | 2427 | |
|
2428 | 2428 | _booleans = {'1': True, 'yes': True, 'true': True, 'on': True, 'always': True, |
|
2429 | 2429 | '0': False, 'no': False, 'false': False, 'off': False, |
|
2430 | 2430 | 'never': False} |
|
2431 | 2431 | |
|
2432 | 2432 | def parsebool(s): |
|
2433 | 2433 | """Parse s into a boolean. |
|
2434 | 2434 | |
|
2435 | 2435 | If s is not a valid boolean, returns None. |
|
2436 | 2436 | """ |
|
2437 | 2437 | return _booleans.get(s.lower(), None) |
|
2438 | 2438 | |
|
2439 | 2439 | _hextochr = dict((a + b, chr(int(a + b, 16))) |
|
2440 | 2440 | for a in string.hexdigits for b in string.hexdigits) |
|
2441 | 2441 | |
|
2442 | 2442 | class url(object): |
|
2443 | 2443 | r"""Reliable URL parser. |
|
2444 | 2444 | |
|
2445 | 2445 | This parses URLs and provides attributes for the following |
|
2446 | 2446 | components: |
|
2447 | 2447 | |
|
2448 | 2448 | <scheme>://<user>:<passwd>@<host>:<port>/<path>?<query>#<fragment> |
|
2449 | 2449 | |
|
2450 | 2450 | Missing components are set to None. The only exception is |
|
2451 | 2451 | fragment, which is set to '' if present but empty. |
|
2452 | 2452 | |
|
2453 | 2453 | If parsefragment is False, fragment is included in query. If |
|
2454 | 2454 | parsequery is False, query is included in path. If both are |
|
2455 | 2455 | False, both fragment and query are included in path. |
|
2456 | 2456 | |
|
2457 | 2457 | See http://www.ietf.org/rfc/rfc2396.txt for more information. |
|
2458 | 2458 | |
|
2459 | 2459 | Note that for backward compatibility reasons, bundle URLs do not |
|
2460 | 2460 | take host names. That means 'bundle://../' has a path of '../'. |
|
2461 | 2461 | |
|
2462 | 2462 | Examples: |
|
2463 | 2463 | |
|
2464 | 2464 | >>> url('http://www.ietf.org/rfc/rfc2396.txt') |
|
2465 | 2465 | <url scheme: 'http', host: 'www.ietf.org', path: 'rfc/rfc2396.txt'> |
|
2466 | 2466 | >>> url('ssh://[::1]:2200//home/joe/repo') |
|
2467 | 2467 | <url scheme: 'ssh', host: '[::1]', port: '2200', path: '/home/joe/repo'> |
|
2468 | 2468 | >>> url('file:///home/joe/repo') |
|
2469 | 2469 | <url scheme: 'file', path: '/home/joe/repo'> |
|
2470 | 2470 | >>> url('file:///c:/temp/foo/') |
|
2471 | 2471 | <url scheme: 'file', path: 'c:/temp/foo/'> |
|
2472 | 2472 | >>> url('bundle:foo') |
|
2473 | 2473 | <url scheme: 'bundle', path: 'foo'> |
|
2474 | 2474 | >>> url('bundle://../foo') |
|
2475 | 2475 | <url scheme: 'bundle', path: '../foo'> |
|
2476 | 2476 | >>> url(r'c:\foo\bar') |
|
2477 | 2477 | <url path: 'c:\\foo\\bar'> |
|
2478 | 2478 | >>> url(r'\\blah\blah\blah') |
|
2479 | 2479 | <url path: '\\\\blah\\blah\\blah'> |
|
2480 | 2480 | >>> url(r'\\blah\blah\blah#baz') |
|
2481 | 2481 | <url path: '\\\\blah\\blah\\blah', fragment: 'baz'> |
|
2482 | 2482 | >>> url(r'file:///C:\users\me') |
|
2483 | 2483 | <url scheme: 'file', path: 'C:\\users\\me'> |
|
2484 | 2484 | |
|
2485 | 2485 | Authentication credentials: |
|
2486 | 2486 | |
|
2487 | 2487 | >>> url('ssh://joe:xyz@x/repo') |
|
2488 | 2488 | <url scheme: 'ssh', user: 'joe', passwd: 'xyz', host: 'x', path: 'repo'> |
|
2489 | 2489 | >>> url('ssh://joe@x/repo') |
|
2490 | 2490 | <url scheme: 'ssh', user: 'joe', host: 'x', path: 'repo'> |
|
2491 | 2491 | |
|
2492 | 2492 | Query strings and fragments: |
|
2493 | 2493 | |
|
2494 | 2494 | >>> url('http://host/a?b#c') |
|
2495 | 2495 | <url scheme: 'http', host: 'host', path: 'a', query: 'b', fragment: 'c'> |
|
2496 | 2496 | >>> url('http://host/a?b#c', parsequery=False, parsefragment=False) |
|
2497 | 2497 | <url scheme: 'http', host: 'host', path: 'a?b#c'> |
|
2498 | 2498 | |
|
2499 | 2499 | Empty path: |
|
2500 | 2500 | |
|
2501 | 2501 | >>> url('') |
|
2502 | 2502 | <url path: ''> |
|
2503 | 2503 | >>> url('#a') |
|
2504 | 2504 | <url path: '', fragment: 'a'> |
|
2505 | 2505 | >>> url('http://host/') |
|
2506 | 2506 | <url scheme: 'http', host: 'host', path: ''> |
|
2507 | 2507 | >>> url('http://host/#a') |
|
2508 | 2508 | <url scheme: 'http', host: 'host', path: '', fragment: 'a'> |
|
2509 | 2509 | |
|
2510 | 2510 | Only scheme: |
|
2511 | 2511 | |
|
2512 | 2512 | >>> url('http:') |
|
2513 | 2513 | <url scheme: 'http'> |
|
2514 | 2514 | """ |
|
2515 | 2515 | |
|
2516 | 2516 | _safechars = "!~*'()+" |
|
2517 | 2517 | _safepchars = "/!~*'()+:\\" |
|
2518 | 2518 | _matchscheme = remod.compile('^[a-zA-Z0-9+.\\-]+:').match |
|
2519 | 2519 | |
|
2520 | 2520 | def __init__(self, path, parsequery=True, parsefragment=True): |
|
2521 | 2521 | # We slowly chomp away at path until we have only the path left |
|
2522 | 2522 | self.scheme = self.user = self.passwd = self.host = None |
|
2523 | 2523 | self.port = self.path = self.query = self.fragment = None |
|
2524 | 2524 | self._localpath = True |
|
2525 | 2525 | self._hostport = '' |
|
2526 | 2526 | self._origpath = path |
|
2527 | 2527 | |
|
2528 | 2528 | if parsefragment and '#' in path: |
|
2529 | 2529 | path, self.fragment = path.split('#', 1) |
|
2530 | 2530 | |
|
2531 | 2531 | # special case for Windows drive letters and UNC paths |
|
2532 | 2532 | if hasdriveletter(path) or path.startswith('\\\\'): |
|
2533 | 2533 | self.path = path |
|
2534 | 2534 | return |
|
2535 | 2535 | |
|
2536 | 2536 | # For compatibility reasons, we can't handle bundle paths as |
|
2537 | 2537 | # normal URLS |
|
2538 | 2538 | if path.startswith('bundle:'): |
|
2539 | 2539 | self.scheme = 'bundle' |
|
2540 | 2540 | path = path[7:] |
|
2541 | 2541 | if path.startswith('//'): |
|
2542 | 2542 | path = path[2:] |
|
2543 | 2543 | self.path = path |
|
2544 | 2544 | return |
|
2545 | 2545 | |
|
2546 | 2546 | if self._matchscheme(path): |
|
2547 | 2547 | parts = path.split(':', 1) |
|
2548 | 2548 | if parts[0]: |
|
2549 | 2549 | self.scheme, path = parts |
|
2550 | 2550 | self._localpath = False |
|
2551 | 2551 | |
|
2552 | 2552 | if not path: |
|
2553 | 2553 | path = None |
|
2554 | 2554 | if self._localpath: |
|
2555 | 2555 | self.path = '' |
|
2556 | 2556 | return |
|
2557 | 2557 | else: |
|
2558 | 2558 | if self._localpath: |
|
2559 | 2559 | self.path = path |
|
2560 | 2560 | return |
|
2561 | 2561 | |
|
2562 | 2562 | if parsequery and '?' in path: |
|
2563 | 2563 | path, self.query = path.split('?', 1) |
|
2564 | 2564 | if not path: |
|
2565 | 2565 | path = None |
|
2566 | 2566 | if not self.query: |
|
2567 | 2567 | self.query = None |
|
2568 | 2568 | |
|
2569 | 2569 | # // is required to specify a host/authority |
|
2570 | 2570 | if path and path.startswith('//'): |
|
2571 | 2571 | parts = path[2:].split('/', 1) |
|
2572 | 2572 | if len(parts) > 1: |
|
2573 | 2573 | self.host, path = parts |
|
2574 | 2574 | else: |
|
2575 | 2575 | self.host = parts[0] |
|
2576 | 2576 | path = None |
|
2577 | 2577 | if not self.host: |
|
2578 | 2578 | self.host = None |
|
2579 | 2579 | # path of file:///d is /d |
|
2580 | 2580 | # path of file:///d:/ is d:/, not /d:/ |
|
2581 | 2581 | if path and not hasdriveletter(path): |
|
2582 | 2582 | path = '/' + path |
|
2583 | 2583 | |
|
2584 | 2584 | if self.host and '@' in self.host: |
|
2585 | 2585 | self.user, self.host = self.host.rsplit('@', 1) |
|
2586 | 2586 | if ':' in self.user: |
|
2587 | 2587 | self.user, self.passwd = self.user.split(':', 1) |
|
2588 | 2588 | if not self.host: |
|
2589 | 2589 | self.host = None |
|
2590 | 2590 | |
|
2591 | 2591 | # Don't split on colons in IPv6 addresses without ports |
|
2592 | 2592 | if (self.host and ':' in self.host and |
|
2593 | 2593 | not (self.host.startswith('[') and self.host.endswith(']'))): |
|
2594 | 2594 | self._hostport = self.host |
|
2595 | 2595 | self.host, self.port = self.host.rsplit(':', 1) |
|
2596 | 2596 | if not self.host: |
|
2597 | 2597 | self.host = None |
|
2598 | 2598 | |
|
2599 | 2599 | if (self.host and self.scheme == 'file' and |
|
2600 | 2600 | self.host not in ('localhost', '127.0.0.1', '[::1]')): |
|
2601 | 2601 | raise Abort(_('file:// URLs can only refer to localhost')) |
|
2602 | 2602 | |
|
2603 | 2603 | self.path = path |
|
2604 | 2604 | |
|
2605 | 2605 | # leave the query string escaped |
|
2606 | 2606 | for a in ('user', 'passwd', 'host', 'port', |
|
2607 | 2607 | 'path', 'fragment'): |
|
2608 | 2608 | v = getattr(self, a) |
|
2609 | 2609 | if v is not None: |
|
2610 | 2610 | setattr(self, a, pycompat.urlunquote(v)) |
|
2611 | 2611 | |
|
2612 | 2612 | def __repr__(self): |
|
2613 | 2613 | attrs = [] |
|
2614 | 2614 | for a in ('scheme', 'user', 'passwd', 'host', 'port', 'path', |
|
2615 | 2615 | 'query', 'fragment'): |
|
2616 | 2616 | v = getattr(self, a) |
|
2617 | 2617 | if v is not None: |
|
2618 | 2618 | attrs.append('%s: %r' % (a, v)) |
|
2619 | 2619 | return '<url %s>' % ', '.join(attrs) |
|
2620 | 2620 | |
|
2621 | 2621 | def __str__(self): |
|
2622 | 2622 | r"""Join the URL's components back into a URL string. |
|
2623 | 2623 | |
|
2624 | 2624 | Examples: |
|
2625 | 2625 | |
|
2626 | 2626 | >>> str(url('http://user:pw@host:80/c:/bob?fo:oo#ba:ar')) |
|
2627 | 2627 | 'http://user:pw@host:80/c:/bob?fo:oo#ba:ar' |
|
2628 | 2628 | >>> str(url('http://user:pw@host:80/?foo=bar&baz=42')) |
|
2629 | 2629 | 'http://user:pw@host:80/?foo=bar&baz=42' |
|
2630 | 2630 | >>> str(url('http://user:pw@host:80/?foo=bar%3dbaz')) |
|
2631 | 2631 | 'http://user:pw@host:80/?foo=bar%3dbaz' |
|
2632 | 2632 | >>> str(url('ssh://user:pw@[::1]:2200//home/joe#')) |
|
2633 | 2633 | 'ssh://user:pw@[::1]:2200//home/joe#' |
|
2634 | 2634 | >>> str(url('http://localhost:80//')) |
|
2635 | 2635 | 'http://localhost:80//' |
|
2636 | 2636 | >>> str(url('http://localhost:80/')) |
|
2637 | 2637 | 'http://localhost:80/' |
|
2638 | 2638 | >>> str(url('http://localhost:80')) |
|
2639 | 2639 | 'http://localhost:80/' |
|
2640 | 2640 | >>> str(url('bundle:foo')) |
|
2641 | 2641 | 'bundle:foo' |
|
2642 | 2642 | >>> str(url('bundle://../foo')) |
|
2643 | 2643 | 'bundle:../foo' |
|
2644 | 2644 | >>> str(url('path')) |
|
2645 | 2645 | 'path' |
|
2646 | 2646 | >>> str(url('file:///tmp/foo/bar')) |
|
2647 | 2647 | 'file:///tmp/foo/bar' |
|
2648 | 2648 | >>> str(url('file:///c:/tmp/foo/bar')) |
|
2649 | 2649 | 'file:///c:/tmp/foo/bar' |
|
2650 | 2650 | >>> print url(r'bundle:foo\bar') |
|
2651 | 2651 | bundle:foo\bar |
|
2652 | 2652 | >>> print url(r'file:///D:\data\hg') |
|
2653 | 2653 | file:///D:\data\hg |
|
2654 | 2654 | """ |
|
2655 | 2655 | if self._localpath: |
|
2656 | 2656 | s = self.path |
|
2657 | 2657 | if self.scheme == 'bundle': |
|
2658 | 2658 | s = 'bundle:' + s |
|
2659 | 2659 | if self.fragment: |
|
2660 | 2660 | s += '#' + self.fragment |
|
2661 | 2661 | return s |
|
2662 | 2662 | |
|
2663 | 2663 | s = self.scheme + ':' |
|
2664 | 2664 | if self.user or self.passwd or self.host: |
|
2665 | 2665 | s += '//' |
|
2666 | 2666 | elif self.scheme and (not self.path or self.path.startswith('/') |
|
2667 | 2667 | or hasdriveletter(self.path)): |
|
2668 | 2668 | s += '//' |
|
2669 | 2669 | if hasdriveletter(self.path): |
|
2670 | 2670 | s += '/' |
|
2671 | 2671 | if self.user: |
|
2672 | 2672 | s += urlreq.quote(self.user, safe=self._safechars) |
|
2673 | 2673 | if self.passwd: |
|
2674 | 2674 | s += ':' + urlreq.quote(self.passwd, safe=self._safechars) |
|
2675 | 2675 | if self.user or self.passwd: |
|
2676 | 2676 | s += '@' |
|
2677 | 2677 | if self.host: |
|
2678 | 2678 | if not (self.host.startswith('[') and self.host.endswith(']')): |
|
2679 | 2679 | s += urlreq.quote(self.host) |
|
2680 | 2680 | else: |
|
2681 | 2681 | s += self.host |
|
2682 | 2682 | if self.port: |
|
2683 | 2683 | s += ':' + urlreq.quote(self.port) |
|
2684 | 2684 | if self.host: |
|
2685 | 2685 | s += '/' |
|
2686 | 2686 | if self.path: |
|
2687 | 2687 | # TODO: similar to the query string, we should not unescape the |
|
2688 | 2688 | # path when we store it, the path might contain '%2f' = '/', |
|
2689 | 2689 | # which we should *not* escape. |
|
2690 | 2690 | s += urlreq.quote(self.path, safe=self._safepchars) |
|
2691 | 2691 | if self.query: |
|
2692 | 2692 | # we store the query in escaped form. |
|
2693 | 2693 | s += '?' + self.query |
|
2694 | 2694 | if self.fragment is not None: |
|
2695 | 2695 | s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) |
|
2696 | 2696 | return s |
|
2697 | 2697 | |
|
2698 | 2698 | def authinfo(self): |
|
2699 | 2699 | user, passwd = self.user, self.passwd |
|
2700 | 2700 | try: |
|
2701 | 2701 | self.user, self.passwd = None, None |
|
2702 | 2702 | s = str(self) |
|
2703 | 2703 | finally: |
|
2704 | 2704 | self.user, self.passwd = user, passwd |
|
2705 | 2705 | if not self.user: |
|
2706 | 2706 | return (s, None) |
|
2707 | 2707 | # authinfo[1] is passed to urllib2 password manager, and its |
|
2708 | 2708 | # URIs must not contain credentials. The host is passed in the |
|
2709 | 2709 | # URIs list because Python < 2.4.3 uses only that to search for |
|
2710 | 2710 | # a password. |
|
2711 | 2711 | return (s, (None, (s, self.host), |
|
2712 | 2712 | self.user, self.passwd or '')) |
|
2713 | 2713 | |
|
2714 | 2714 | def isabs(self): |
|
2715 | 2715 | if self.scheme and self.scheme != 'file': |
|
2716 | 2716 | return True # remote URL |
|
2717 | 2717 | if hasdriveletter(self.path): |
|
2718 | 2718 | return True # absolute for our purposes - can't be joined() |
|
2719 | 2719 | if self.path.startswith(r'\\'): |
|
2720 | 2720 | return True # Windows UNC path |
|
2721 | 2721 | if self.path.startswith('/'): |
|
2722 | 2722 | return True # POSIX-style |
|
2723 | 2723 | return False |
|
2724 | 2724 | |
|
2725 | 2725 | def localpath(self): |
|
2726 | 2726 | if self.scheme == 'file' or self.scheme == 'bundle': |
|
2727 | 2727 | path = self.path or '/' |
|
2728 | 2728 | # For Windows, we need to promote hosts containing drive |
|
2729 | 2729 | # letters to paths with drive letters. |
|
2730 | 2730 | if hasdriveletter(self._hostport): |
|
2731 | 2731 | path = self._hostport + '/' + self.path |
|
2732 | 2732 | elif (self.host is not None and self.path |
|
2733 | 2733 | and not hasdriveletter(path)): |
|
2734 | 2734 | path = '/' + path |
|
2735 | 2735 | return path |
|
2736 | 2736 | return self._origpath |
|
2737 | 2737 | |
|
2738 | 2738 | def islocal(self): |
|
2739 | 2739 | '''whether localpath will return something that posixfile can open''' |
|
2740 | 2740 | return (not self.scheme or self.scheme == 'file' |
|
2741 | 2741 | or self.scheme == 'bundle') |
|
2742 | 2742 | |
|
2743 | 2743 | def hasscheme(path): |
|
2744 | 2744 | return bool(url(path).scheme) |
|
2745 | 2745 | |
|
2746 | 2746 | def hasdriveletter(path): |
|
2747 | 2747 | return path and path[1:2] == ':' and path[0:1].isalpha() |
|
2748 | 2748 | |
|
2749 | 2749 | def urllocalpath(path): |
|
2750 | 2750 | return url(path, parsequery=False, parsefragment=False).localpath() |
|
2751 | 2751 | |
|
2752 | 2752 | def hidepassword(u): |
|
2753 | 2753 | '''hide user credential in a url string''' |
|
2754 | 2754 | u = url(u) |
|
2755 | 2755 | if u.passwd: |
|
2756 | 2756 | u.passwd = '***' |
|
2757 | 2757 | return str(u) |
|
2758 | 2758 | |
|
2759 | 2759 | def removeauth(u): |
|
2760 | 2760 | '''remove all authentication information from a url string''' |
|
2761 | 2761 | u = url(u) |
|
2762 | 2762 | u.user = u.passwd = None |
|
2763 | 2763 | return str(u) |
|
2764 | 2764 | |
|
2765 | 2765 | timecount = unitcountfn( |
|
2766 | 2766 | (1, 1e3, _('%.0f s')), |
|
2767 | 2767 | (100, 1, _('%.1f s')), |
|
2768 | 2768 | (10, 1, _('%.2f s')), |
|
2769 | 2769 | (1, 1, _('%.3f s')), |
|
2770 | 2770 | (100, 0.001, _('%.1f ms')), |
|
2771 | 2771 | (10, 0.001, _('%.2f ms')), |
|
2772 | 2772 | (1, 0.001, _('%.3f ms')), |
|
2773 | 2773 | (100, 0.000001, _('%.1f us')), |
|
2774 | 2774 | (10, 0.000001, _('%.2f us')), |
|
2775 | 2775 | (1, 0.000001, _('%.3f us')), |
|
2776 | 2776 | (100, 0.000000001, _('%.1f ns')), |
|
2777 | 2777 | (10, 0.000000001, _('%.2f ns')), |
|
2778 | 2778 | (1, 0.000000001, _('%.3f ns')), |
|
2779 | 2779 | ) |
|
2780 | 2780 | |
|
2781 | 2781 | _timenesting = [0] |
|
2782 | 2782 | |
|
2783 | 2783 | def timed(func): |
|
2784 | 2784 | '''Report the execution time of a function call to stderr. |
|
2785 | 2785 | |
|
2786 | 2786 | During development, use as a decorator when you need to measure |
|
2787 | 2787 | the cost of a function, e.g. as follows: |
|
2788 | 2788 | |
|
2789 | 2789 | @util.timed |
|
2790 | 2790 | def foo(a, b, c): |
|
2791 | 2791 | pass |
|
2792 | 2792 | ''' |
|
2793 | 2793 | |
|
2794 | 2794 | def wrapper(*args, **kwargs): |
|
2795 | 2795 | start = time.time() |
|
2796 | 2796 | indent = 2 |
|
2797 | 2797 | _timenesting[0] += indent |
|
2798 | 2798 | try: |
|
2799 | 2799 | return func(*args, **kwargs) |
|
2800 | 2800 | finally: |
|
2801 | 2801 | elapsed = time.time() - start |
|
2802 | 2802 | _timenesting[0] -= indent |
|
2803 | 2803 | stderr.write('%s%s: %s\n' % |
|
2804 | 2804 | (' ' * _timenesting[0], func.__name__, |
|
2805 | 2805 | timecount(elapsed))) |
|
2806 | 2806 | return wrapper |
|
2807 | 2807 | |
|
2808 | 2808 | _sizeunits = (('m', 2**20), ('k', 2**10), ('g', 2**30), |
|
2809 | 2809 | ('kb', 2**10), ('mb', 2**20), ('gb', 2**30), ('b', 1)) |
|
2810 | 2810 | |
|
2811 | 2811 | def sizetoint(s): |
|
2812 | 2812 | '''Convert a space specifier to a byte count. |
|
2813 | 2813 | |
|
2814 | 2814 | >>> sizetoint('30') |
|
2815 | 2815 | 30 |
|
2816 | 2816 | >>> sizetoint('2.2kb') |
|
2817 | 2817 | 2252 |
|
2818 | 2818 | >>> sizetoint('6M') |
|
2819 | 2819 | 6291456 |
|
2820 | 2820 | ''' |
|
2821 | 2821 | t = s.strip().lower() |
|
2822 | 2822 | try: |
|
2823 | 2823 | for k, u in _sizeunits: |
|
2824 | 2824 | if t.endswith(k): |
|
2825 | 2825 | return int(float(t[:-len(k)]) * u) |
|
2826 | 2826 | return int(t) |
|
2827 | 2827 | except ValueError: |
|
2828 | 2828 | raise error.ParseError(_("couldn't parse size: %s") % s) |
|
2829 | 2829 | |
|
2830 | 2830 | class hooks(object): |
|
2831 | 2831 | '''A collection of hook functions that can be used to extend a |
|
2832 | 2832 | function's behavior. Hooks are called in lexicographic order, |
|
2833 | 2833 | based on the names of their sources.''' |
|
2834 | 2834 | |
|
2835 | 2835 | def __init__(self): |
|
2836 | 2836 | self._hooks = [] |
|
2837 | 2837 | |
|
2838 | 2838 | def add(self, source, hook): |
|
2839 | 2839 | self._hooks.append((source, hook)) |
|
2840 | 2840 | |
|
2841 | 2841 | def __call__(self, *args): |
|
2842 | 2842 | self._hooks.sort(key=lambda x: x[0]) |
|
2843 | 2843 | results = [] |
|
2844 | 2844 | for source, hook in self._hooks: |
|
2845 | 2845 | results.append(hook(*args)) |
|
2846 | 2846 | return results |
|
2847 | 2847 | |
|
2848 | 2848 | def getstackframes(skip=0, line=' %-*s in %s\n', fileline='%s:%s'): |
|
2849 | 2849 | '''Yields lines for a nicely formatted stacktrace. |
|
2850 | 2850 | Skips the 'skip' last entries. |
|
2851 | 2851 | Each file+linenumber is formatted according to fileline. |
|
2852 | 2852 | Each line is formatted according to line. |
|
2853 | 2853 | If line is None, it yields: |
|
2854 | 2854 | length of longest filepath+line number, |
|
2855 | 2855 | filepath+linenumber, |
|
2856 | 2856 | function |
|
2857 | 2857 | |
|
2858 | 2858 | Not be used in production code but very convenient while developing. |
|
2859 | 2859 | ''' |
|
2860 | 2860 | entries = [(fileline % (fn, ln), func) |
|
2861 | 2861 | for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]] |
|
2862 | 2862 | if entries: |
|
2863 | 2863 | fnmax = max(len(entry[0]) for entry in entries) |
|
2864 | 2864 | for fnln, func in entries: |
|
2865 | 2865 | if line is None: |
|
2866 | 2866 | yield (fnmax, fnln, func) |
|
2867 | 2867 | else: |
|
2868 | 2868 | yield line % (fnmax, fnln, func) |
|
2869 | 2869 | |
|
2870 | 2870 | def debugstacktrace(msg='stacktrace', skip=0, f=stderr, otherf=stdout): |
|
2871 | 2871 | '''Writes a message to f (stderr) with a nicely formatted stacktrace. |
|
2872 | 2872 | Skips the 'skip' last entries. By default it will flush stdout first. |
|
2873 | 2873 | It can be used everywhere and intentionally does not require an ui object. |
|
2874 | 2874 | Not be used in production code but very convenient while developing. |
|
2875 | 2875 | ''' |
|
2876 | 2876 | if otherf: |
|
2877 | 2877 | otherf.flush() |
|
2878 | 2878 | f.write('%s at:\n' % msg) |
|
2879 | 2879 | for line in getstackframes(skip + 1): |
|
2880 | 2880 | f.write(line) |
|
2881 | 2881 | f.flush() |
|
2882 | 2882 | |
|
2883 | 2883 | class dirs(object): |
|
2884 | 2884 | '''a multiset of directory names from a dirstate or manifest''' |
|
2885 | 2885 | |
|
2886 | 2886 | def __init__(self, map, skip=None): |
|
2887 | 2887 | self._dirs = {} |
|
2888 | 2888 | addpath = self.addpath |
|
2889 | 2889 | if safehasattr(map, 'iteritems') and skip is not None: |
|
2890 | 2890 | for f, s in map.iteritems(): |
|
2891 | 2891 | if s[0] != skip: |
|
2892 | 2892 | addpath(f) |
|
2893 | 2893 | else: |
|
2894 | 2894 | for f in map: |
|
2895 | 2895 | addpath(f) |
|
2896 | 2896 | |
|
2897 | 2897 | def addpath(self, path): |
|
2898 | 2898 | dirs = self._dirs |
|
2899 | 2899 | for base in finddirs(path): |
|
2900 | 2900 | if base in dirs: |
|
2901 | 2901 | dirs[base] += 1 |
|
2902 | 2902 | return |
|
2903 | 2903 | dirs[base] = 1 |
|
2904 | 2904 | |
|
2905 | 2905 | def delpath(self, path): |
|
2906 | 2906 | dirs = self._dirs |
|
2907 | 2907 | for base in finddirs(path): |
|
2908 | 2908 | if dirs[base] > 1: |
|
2909 | 2909 | dirs[base] -= 1 |
|
2910 | 2910 | return |
|
2911 | 2911 | del dirs[base] |
|
2912 | 2912 | |
|
2913 | 2913 | def __iter__(self): |
|
2914 | 2914 | return self._dirs.iterkeys() |
|
2915 | 2915 | |
|
2916 | 2916 | def __contains__(self, d): |
|
2917 | 2917 | return d in self._dirs |
|
2918 | 2918 | |
|
2919 | 2919 | if safehasattr(parsers, 'dirs'): |
|
2920 | 2920 | dirs = parsers.dirs |
|
2921 | 2921 | |
|
2922 | 2922 | def finddirs(path): |
|
2923 | 2923 | pos = path.rfind('/') |
|
2924 | 2924 | while pos != -1: |
|
2925 | 2925 | yield path[:pos] |
|
2926 | 2926 | pos = path.rfind('/', 0, pos) |
|
2927 | 2927 | |
|
2928 | 2928 | class ctxmanager(object): |
|
2929 | 2929 | '''A context manager for use in 'with' blocks to allow multiple |
|
2930 | 2930 | contexts to be entered at once. This is both safer and more |
|
2931 | 2931 | flexible than contextlib.nested. |
|
2932 | 2932 | |
|
2933 | 2933 | Once Mercurial supports Python 2.7+, this will become mostly |
|
2934 | 2934 | unnecessary. |
|
2935 | 2935 | ''' |
|
2936 | 2936 | |
|
2937 | 2937 | def __init__(self, *args): |
|
2938 | 2938 | '''Accepts a list of no-argument functions that return context |
|
2939 | 2939 | managers. These will be invoked at __call__ time.''' |
|
2940 | 2940 | self._pending = args |
|
2941 | 2941 | self._atexit = [] |
|
2942 | 2942 | |
|
2943 | 2943 | def __enter__(self): |
|
2944 | 2944 | return self |
|
2945 | 2945 | |
|
2946 | 2946 | def enter(self): |
|
2947 | 2947 | '''Create and enter context managers in the order in which they were |
|
2948 | 2948 | passed to the constructor.''' |
|
2949 | 2949 | values = [] |
|
2950 | 2950 | for func in self._pending: |
|
2951 | 2951 | obj = func() |
|
2952 | 2952 | values.append(obj.__enter__()) |
|
2953 | 2953 | self._atexit.append(obj.__exit__) |
|
2954 | 2954 | del self._pending |
|
2955 | 2955 | return values |
|
2956 | 2956 | |
|
2957 | 2957 | def atexit(self, func, *args, **kwargs): |
|
2958 | 2958 | '''Add a function to call when this context manager exits. The |
|
2959 | 2959 | ordering of multiple atexit calls is unspecified, save that |
|
2960 | 2960 | they will happen before any __exit__ functions.''' |
|
2961 | 2961 | def wrapper(exc_type, exc_val, exc_tb): |
|
2962 | 2962 | func(*args, **kwargs) |
|
2963 | 2963 | self._atexit.append(wrapper) |
|
2964 | 2964 | return func |
|
2965 | 2965 | |
|
2966 | 2966 | def __exit__(self, exc_type, exc_val, exc_tb): |
|
2967 | 2967 | '''Context managers are exited in the reverse order from which |
|
2968 | 2968 | they were created.''' |
|
2969 | 2969 | received = exc_type is not None |
|
2970 | 2970 | suppressed = False |
|
2971 | 2971 | pending = None |
|
2972 | 2972 | self._atexit.reverse() |
|
2973 | 2973 | for exitfunc in self._atexit: |
|
2974 | 2974 | try: |
|
2975 | 2975 | if exitfunc(exc_type, exc_val, exc_tb): |
|
2976 | 2976 | suppressed = True |
|
2977 | 2977 | exc_type = None |
|
2978 | 2978 | exc_val = None |
|
2979 | 2979 | exc_tb = None |
|
2980 | 2980 | except BaseException: |
|
2981 | 2981 | pending = sys.exc_info() |
|
2982 | 2982 | exc_type, exc_val, exc_tb = pending = sys.exc_info() |
|
2983 | 2983 | del self._atexit |
|
2984 | 2984 | if pending: |
|
2985 | 2985 | raise exc_val |
|
2986 | 2986 | return received and suppressed |
|
2987 | 2987 | |
|
2988 | 2988 | # compression code |
|
2989 | 2989 | |
|
2990 | 2990 | SERVERROLE = 'server' |
|
2991 | 2991 | CLIENTROLE = 'client' |
|
2992 | 2992 | |
|
2993 | 2993 | compewireprotosupport = collections.namedtuple(u'compenginewireprotosupport', |
|
2994 | 2994 | (u'name', u'serverpriority', |
|
2995 | 2995 | u'clientpriority')) |
|
2996 | 2996 | |
|
2997 | 2997 | class compressormanager(object): |
|
2998 | 2998 | """Holds registrations of various compression engines. |
|
2999 | 2999 | |
|
3000 | 3000 | This class essentially abstracts the differences between compression |
|
3001 | 3001 | engines to allow new compression formats to be added easily, possibly from |
|
3002 | 3002 | extensions. |
|
3003 | 3003 | |
|
3004 | 3004 | Compressors are registered against the global instance by calling its |
|
3005 | 3005 | ``register()`` method. |
|
3006 | 3006 | """ |
|
3007 | 3007 | def __init__(self): |
|
3008 | 3008 | self._engines = {} |
|
3009 | 3009 | # Bundle spec human name to engine name. |
|
3010 | 3010 | self._bundlenames = {} |
|
3011 | 3011 | # Internal bundle identifier to engine name. |
|
3012 | 3012 | self._bundletypes = {} |
|
3013 | 3013 | # Revlog header to engine name. |
|
3014 | 3014 | self._revlogheaders = {} |
|
3015 | 3015 | # Wire proto identifier to engine name. |
|
3016 | 3016 | self._wiretypes = {} |
|
3017 | 3017 | |
|
3018 | 3018 | def __getitem__(self, key): |
|
3019 | 3019 | return self._engines[key] |
|
3020 | 3020 | |
|
3021 | 3021 | def __contains__(self, key): |
|
3022 | 3022 | return key in self._engines |
|
3023 | 3023 | |
|
3024 | 3024 | def __iter__(self): |
|
3025 | 3025 | return iter(self._engines.keys()) |
|
3026 | 3026 | |
|
3027 | 3027 | def register(self, engine): |
|
3028 | 3028 | """Register a compression engine with the manager. |
|
3029 | 3029 | |
|
3030 | 3030 | The argument must be a ``compressionengine`` instance. |
|
3031 | 3031 | """ |
|
3032 | 3032 | if not isinstance(engine, compressionengine): |
|
3033 | 3033 | raise ValueError(_('argument must be a compressionengine')) |
|
3034 | 3034 | |
|
3035 | 3035 | name = engine.name() |
|
3036 | 3036 | |
|
3037 | 3037 | if name in self._engines: |
|
3038 | 3038 | raise error.Abort(_('compression engine %s already registered') % |
|
3039 | 3039 | name) |
|
3040 | 3040 | |
|
3041 | 3041 | bundleinfo = engine.bundletype() |
|
3042 | 3042 | if bundleinfo: |
|
3043 | 3043 | bundlename, bundletype = bundleinfo |
|
3044 | 3044 | |
|
3045 | 3045 | if bundlename in self._bundlenames: |
|
3046 | 3046 | raise error.Abort(_('bundle name %s already registered') % |
|
3047 | 3047 | bundlename) |
|
3048 | 3048 | if bundletype in self._bundletypes: |
|
3049 | 3049 | raise error.Abort(_('bundle type %s already registered by %s') % |
|
3050 | 3050 | (bundletype, self._bundletypes[bundletype])) |
|
3051 | 3051 | |
|
3052 | 3052 | # No external facing name declared. |
|
3053 | 3053 | if bundlename: |
|
3054 | 3054 | self._bundlenames[bundlename] = name |
|
3055 | 3055 | |
|
3056 | 3056 | self._bundletypes[bundletype] = name |
|
3057 | 3057 | |
|
3058 | 3058 | wiresupport = engine.wireprotosupport() |
|
3059 | 3059 | if wiresupport: |
|
3060 | 3060 | wiretype = wiresupport.name |
|
3061 | 3061 | if wiretype in self._wiretypes: |
|
3062 | 3062 | raise error.Abort(_('wire protocol compression %s already ' |
|
3063 | 3063 | 'registered by %s') % |
|
3064 | 3064 | (wiretype, self._wiretypes[wiretype])) |
|
3065 | 3065 | |
|
3066 | 3066 | self._wiretypes[wiretype] = name |
|
3067 | 3067 | |
|
3068 | 3068 | revlogheader = engine.revlogheader() |
|
3069 | 3069 | if revlogheader and revlogheader in self._revlogheaders: |
|
3070 | 3070 | raise error.Abort(_('revlog header %s already registered by %s') % |
|
3071 | 3071 | (revlogheader, self._revlogheaders[revlogheader])) |
|
3072 | 3072 | |
|
3073 | 3073 | if revlogheader: |
|
3074 | 3074 | self._revlogheaders[revlogheader] = name |
|
3075 | 3075 | |
|
3076 | 3076 | self._engines[name] = engine |
|
3077 | 3077 | |
|
3078 | 3078 | @property |
|
3079 | 3079 | def supportedbundlenames(self): |
|
3080 | 3080 | return set(self._bundlenames.keys()) |
|
3081 | 3081 | |
|
3082 | 3082 | @property |
|
3083 | 3083 | def supportedbundletypes(self): |
|
3084 | 3084 | return set(self._bundletypes.keys()) |
|
3085 | 3085 | |
|
3086 | 3086 | def forbundlename(self, bundlename): |
|
3087 | 3087 | """Obtain a compression engine registered to a bundle name. |
|
3088 | 3088 | |
|
3089 | 3089 | Will raise KeyError if the bundle type isn't registered. |
|
3090 | 3090 | |
|
3091 | 3091 | Will abort if the engine is known but not available. |
|
3092 | 3092 | """ |
|
3093 | 3093 | engine = self._engines[self._bundlenames[bundlename]] |
|
3094 | 3094 | if not engine.available(): |
|
3095 | 3095 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3096 | 3096 | engine.name()) |
|
3097 | 3097 | return engine |
|
3098 | 3098 | |
|
3099 | 3099 | def forbundletype(self, bundletype): |
|
3100 | 3100 | """Obtain a compression engine registered to a bundle type. |
|
3101 | 3101 | |
|
3102 | 3102 | Will raise KeyError if the bundle type isn't registered. |
|
3103 | 3103 | |
|
3104 | 3104 | Will abort if the engine is known but not available. |
|
3105 | 3105 | """ |
|
3106 | 3106 | engine = self._engines[self._bundletypes[bundletype]] |
|
3107 | 3107 | if not engine.available(): |
|
3108 | 3108 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3109 | 3109 | engine.name()) |
|
3110 | 3110 | return engine |
|
3111 | 3111 | |
|
3112 | 3112 | def supportedwireengines(self, role, onlyavailable=True): |
|
3113 | 3113 | """Obtain compression engines that support the wire protocol. |
|
3114 | 3114 | |
|
3115 | 3115 | Returns a list of engines in prioritized order, most desired first. |
|
3116 | 3116 | |
|
3117 | 3117 | If ``onlyavailable`` is set, filter out engines that can't be |
|
3118 | 3118 | loaded. |
|
3119 | 3119 | """ |
|
3120 | 3120 | assert role in (SERVERROLE, CLIENTROLE) |
|
3121 | 3121 | |
|
3122 | 3122 | attr = 'serverpriority' if role == SERVERROLE else 'clientpriority' |
|
3123 | 3123 | |
|
3124 | 3124 | engines = [self._engines[e] for e in self._wiretypes.values()] |
|
3125 | 3125 | if onlyavailable: |
|
3126 | 3126 | engines = [e for e in engines if e.available()] |
|
3127 | 3127 | |
|
3128 | 3128 | def getkey(e): |
|
3129 | 3129 | # Sort first by priority, highest first. In case of tie, sort |
|
3130 | 3130 | # alphabetically. This is arbitrary, but ensures output is |
|
3131 | 3131 | # stable. |
|
3132 | 3132 | w = e.wireprotosupport() |
|
3133 | 3133 | return -1 * getattr(w, attr), w.name |
|
3134 | 3134 | |
|
3135 | 3135 | return list(sorted(engines, key=getkey)) |
|
3136 | 3136 | |
|
3137 | 3137 | def forwiretype(self, wiretype): |
|
3138 | 3138 | engine = self._engines[self._wiretypes[wiretype]] |
|
3139 | 3139 | if not engine.available(): |
|
3140 | 3140 | raise error.Abort(_('compression engine %s could not be loaded') % |
|
3141 | 3141 | engine.name()) |
|
3142 | 3142 | return engine |
|
3143 | 3143 | |
|
3144 | 3144 | def forrevlogheader(self, header): |
|
3145 | 3145 | """Obtain a compression engine registered to a revlog header. |
|
3146 | 3146 | |
|
3147 | 3147 | Will raise KeyError if the revlog header value isn't registered. |
|
3148 | 3148 | """ |
|
3149 | 3149 | return self._engines[self._revlogheaders[header]] |
|
3150 | 3150 | |
|
3151 | 3151 | compengines = compressormanager() |
|
3152 | 3152 | |
|
3153 | 3153 | class compressionengine(object): |
|
3154 | 3154 | """Base class for compression engines. |
|
3155 | 3155 | |
|
3156 | 3156 | Compression engines must implement the interface defined by this class. |
|
3157 | 3157 | """ |
|
3158 | 3158 | def name(self): |
|
3159 | 3159 | """Returns the name of the compression engine. |
|
3160 | 3160 | |
|
3161 | 3161 | This is the key the engine is registered under. |
|
3162 | 3162 | |
|
3163 | 3163 | This method must be implemented. |
|
3164 | 3164 | """ |
|
3165 | 3165 | raise NotImplementedError() |
|
3166 | 3166 | |
|
3167 | 3167 | def available(self): |
|
3168 | 3168 | """Whether the compression engine is available. |
|
3169 | 3169 | |
|
3170 | 3170 | The intent of this method is to allow optional compression engines |
|
3171 | 3171 | that may not be available in all installations (such as engines relying |
|
3172 | 3172 | on C extensions that may not be present). |
|
3173 | 3173 | """ |
|
3174 | 3174 | return True |
|
3175 | 3175 | |
|
3176 | 3176 | def bundletype(self): |
|
3177 | 3177 | """Describes bundle identifiers for this engine. |
|
3178 | 3178 | |
|
3179 | 3179 | If this compression engine isn't supported for bundles, returns None. |
|
3180 | 3180 | |
|
3181 | 3181 | If this engine can be used for bundles, returns a 2-tuple of strings of |
|
3182 | 3182 | the user-facing "bundle spec" compression name and an internal |
|
3183 | 3183 | identifier used to denote the compression format within bundles. To |
|
3184 | 3184 | exclude the name from external usage, set the first element to ``None``. |
|
3185 | 3185 | |
|
3186 | 3186 | If bundle compression is supported, the class must also implement |
|
3187 | 3187 | ``compressstream`` and `decompressorreader``. |
|
3188 | 3188 | """ |
|
3189 | 3189 | return None |
|
3190 | 3190 | |
|
3191 | 3191 | def wireprotosupport(self): |
|
3192 | 3192 | """Declare support for this compression format on the wire protocol. |
|
3193 | 3193 | |
|
3194 | 3194 | If this compression engine isn't supported for compressing wire |
|
3195 | 3195 | protocol payloads, returns None. |
|
3196 | 3196 | |
|
3197 | 3197 | Otherwise, returns ``compenginewireprotosupport`` with the following |
|
3198 | 3198 | fields: |
|
3199 | 3199 | |
|
3200 | 3200 | * String format identifier |
|
3201 | 3201 | * Integer priority for the server |
|
3202 | 3202 | * Integer priority for the client |
|
3203 | 3203 | |
|
3204 | 3204 | The integer priorities are used to order the advertisement of format |
|
3205 | 3205 | support by server and client. The highest integer is advertised |
|
3206 | 3206 | first. Integers with non-positive values aren't advertised. |
|
3207 | 3207 | |
|
3208 | 3208 | The priority values are somewhat arbitrary and only used for default |
|
3209 | 3209 | ordering. The relative order can be changed via config options. |
|
3210 | 3210 | |
|
3211 | 3211 | If wire protocol compression is supported, the class must also implement |
|
3212 | 3212 | ``compressstream`` and ``decompressorreader``. |
|
3213 | 3213 | """ |
|
3214 | 3214 | return None |
|
3215 | 3215 | |
|
3216 | 3216 | def revlogheader(self): |
|
3217 | 3217 | """Header added to revlog chunks that identifies this engine. |
|
3218 | 3218 | |
|
3219 | 3219 | If this engine can be used to compress revlogs, this method should |
|
3220 | 3220 | return the bytes used to identify chunks compressed with this engine. |
|
3221 | 3221 | Else, the method should return ``None`` to indicate it does not |
|
3222 | 3222 | participate in revlog compression. |
|
3223 | 3223 | """ |
|
3224 | 3224 | return None |
|
3225 | 3225 | |
|
3226 | 3226 | def compressstream(self, it, opts=None): |
|
3227 | 3227 | """Compress an iterator of chunks. |
|
3228 | 3228 | |
|
3229 | 3229 | The method receives an iterator (ideally a generator) of chunks of |
|
3230 | 3230 | bytes to be compressed. It returns an iterator (ideally a generator) |
|
3231 | 3231 | of bytes of chunks representing the compressed output. |
|
3232 | 3232 | |
|
3233 | 3233 | Optionally accepts an argument defining how to perform compression. |
|
3234 | 3234 | Each engine treats this argument differently. |
|
3235 | 3235 | """ |
|
3236 | 3236 | raise NotImplementedError() |
|
3237 | 3237 | |
|
3238 | 3238 | def decompressorreader(self, fh): |
|
3239 | 3239 | """Perform decompression on a file object. |
|
3240 | 3240 | |
|
3241 | 3241 | Argument is an object with a ``read(size)`` method that returns |
|
3242 | 3242 | compressed data. Return value is an object with a ``read(size)`` that |
|
3243 | 3243 | returns uncompressed data. |
|
3244 | 3244 | """ |
|
3245 | 3245 | raise NotImplementedError() |
|
3246 | 3246 | |
|
3247 | 3247 | def revlogcompressor(self, opts=None): |
|
3248 | 3248 | """Obtain an object that can be used to compress revlog entries. |
|
3249 | 3249 | |
|
3250 | 3250 | The object has a ``compress(data)`` method that compresses binary |
|
3251 | 3251 | data. This method returns compressed binary data or ``None`` if |
|
3252 | 3252 | the data could not be compressed (too small, not compressible, etc). |
|
3253 | 3253 | The returned data should have a header uniquely identifying this |
|
3254 | 3254 | compression format so decompression can be routed to this engine. |
|
3255 | 3255 | This header should be identified by the ``revlogheader()`` return |
|
3256 | 3256 | value. |
|
3257 | 3257 | |
|
3258 | 3258 | The object has a ``decompress(data)`` method that decompresses |
|
3259 | 3259 | data. The method will only be called if ``data`` begins with |
|
3260 | 3260 | ``revlogheader()``. The method should return the raw, uncompressed |
|
3261 | 3261 | data or raise a ``RevlogError``. |
|
3262 | 3262 | |
|
3263 | 3263 | The object is reusable but is not thread safe. |
|
3264 | 3264 | """ |
|
3265 | 3265 | raise NotImplementedError() |
|
3266 | 3266 | |
|
3267 | 3267 | class _zlibengine(compressionengine): |
|
3268 | 3268 | def name(self): |
|
3269 | 3269 | return 'zlib' |
|
3270 | 3270 | |
|
3271 | 3271 | def bundletype(self): |
|
3272 | 3272 | return 'gzip', 'GZ' |
|
3273 | 3273 | |
|
3274 | 3274 | def wireprotosupport(self): |
|
3275 | 3275 | return compewireprotosupport('zlib', 20, 20) |
|
3276 | 3276 | |
|
3277 | 3277 | def revlogheader(self): |
|
3278 | 3278 | return 'x' |
|
3279 | 3279 | |
|
3280 | 3280 | def compressstream(self, it, opts=None): |
|
3281 | 3281 | opts = opts or {} |
|
3282 | 3282 | |
|
3283 | 3283 | z = zlib.compressobj(opts.get('level', -1)) |
|
3284 | 3284 | for chunk in it: |
|
3285 | 3285 | data = z.compress(chunk) |
|
3286 | 3286 | # Not all calls to compress emit data. It is cheaper to inspect |
|
3287 | 3287 | # here than to feed empty chunks through generator. |
|
3288 | 3288 | if data: |
|
3289 | 3289 | yield data |
|
3290 | 3290 | |
|
3291 | 3291 | yield z.flush() |
|
3292 | 3292 | |
|
3293 | 3293 | def decompressorreader(self, fh): |
|
3294 | 3294 | def gen(): |
|
3295 | 3295 | d = zlib.decompressobj() |
|
3296 | 3296 | for chunk in filechunkiter(fh): |
|
3297 | 3297 | while chunk: |
|
3298 | 3298 | # Limit output size to limit memory. |
|
3299 | 3299 | yield d.decompress(chunk, 2 ** 18) |
|
3300 | 3300 | chunk = d.unconsumed_tail |
|
3301 | 3301 | |
|
3302 | 3302 | return chunkbuffer(gen()) |
|
3303 | 3303 | |
|
3304 | 3304 | class zlibrevlogcompressor(object): |
|
3305 | 3305 | def compress(self, data): |
|
3306 | 3306 | insize = len(data) |
|
3307 | 3307 | # Caller handles empty input case. |
|
3308 | 3308 | assert insize > 0 |
|
3309 | 3309 | |
|
3310 | 3310 | if insize < 44: |
|
3311 | 3311 | return None |
|
3312 | 3312 | |
|
3313 | 3313 | elif insize <= 1000000: |
|
3314 | 3314 | compressed = zlib.compress(data) |
|
3315 | 3315 | if len(compressed) < insize: |
|
3316 | 3316 | return compressed |
|
3317 | 3317 | return None |
|
3318 | 3318 | |
|
3319 | 3319 | # zlib makes an internal copy of the input buffer, doubling |
|
3320 | 3320 | # memory usage for large inputs. So do streaming compression |
|
3321 | 3321 | # on large inputs. |
|
3322 | 3322 | else: |
|
3323 | 3323 | z = zlib.compressobj() |
|
3324 | 3324 | parts = [] |
|
3325 | 3325 | pos = 0 |
|
3326 | 3326 | while pos < insize: |
|
3327 | 3327 | pos2 = pos + 2**20 |
|
3328 | 3328 | parts.append(z.compress(data[pos:pos2])) |
|
3329 | 3329 | pos = pos2 |
|
3330 | 3330 | parts.append(z.flush()) |
|
3331 | 3331 | |
|
3332 | 3332 | if sum(map(len, parts)) < insize: |
|
3333 | 3333 | return ''.join(parts) |
|
3334 | 3334 | return None |
|
3335 | 3335 | |
|
3336 | 3336 | def decompress(self, data): |
|
3337 | 3337 | try: |
|
3338 | 3338 | return zlib.decompress(data) |
|
3339 | 3339 | except zlib.error as e: |
|
3340 | 3340 | raise error.RevlogError(_('revlog decompress error: %s') % |
|
3341 | 3341 | str(e)) |
|
3342 | 3342 | |
|
3343 | 3343 | def revlogcompressor(self, opts=None): |
|
3344 | 3344 | return self.zlibrevlogcompressor() |
|
3345 | 3345 | |
|
3346 | 3346 | compengines.register(_zlibengine()) |
|
3347 | 3347 | |
|
3348 | 3348 | class _bz2engine(compressionengine): |
|
3349 | 3349 | def name(self): |
|
3350 | 3350 | return 'bz2' |
|
3351 | 3351 | |
|
3352 | 3352 | def bundletype(self): |
|
3353 | 3353 | return 'bzip2', 'BZ' |
|
3354 | 3354 | |
|
3355 | 3355 | # We declare a protocol name but don't advertise by default because |
|
3356 | 3356 | # it is slow. |
|
3357 | 3357 | def wireprotosupport(self): |
|
3358 | 3358 | return compewireprotosupport('bzip2', 0, 0) |
|
3359 | 3359 | |
|
3360 | 3360 | def compressstream(self, it, opts=None): |
|
3361 | 3361 | opts = opts or {} |
|
3362 | 3362 | z = bz2.BZ2Compressor(opts.get('level', 9)) |
|
3363 | 3363 | for chunk in it: |
|
3364 | 3364 | data = z.compress(chunk) |
|
3365 | 3365 | if data: |
|
3366 | 3366 | yield data |
|
3367 | 3367 | |
|
3368 | 3368 | yield z.flush() |
|
3369 | 3369 | |
|
3370 | 3370 | def decompressorreader(self, fh): |
|
3371 | 3371 | def gen(): |
|
3372 | 3372 | d = bz2.BZ2Decompressor() |
|
3373 | 3373 | for chunk in filechunkiter(fh): |
|
3374 | 3374 | yield d.decompress(chunk) |
|
3375 | 3375 | |
|
3376 | 3376 | return chunkbuffer(gen()) |
|
3377 | 3377 | |
|
3378 | 3378 | compengines.register(_bz2engine()) |
|
3379 | 3379 | |
|
3380 | 3380 | class _truncatedbz2engine(compressionengine): |
|
3381 | 3381 | def name(self): |
|
3382 | 3382 | return 'bz2truncated' |
|
3383 | 3383 | |
|
3384 | 3384 | def bundletype(self): |
|
3385 | 3385 | return None, '_truncatedBZ' |
|
3386 | 3386 | |
|
3387 | 3387 | # We don't implement compressstream because it is hackily handled elsewhere. |
|
3388 | 3388 | |
|
3389 | 3389 | def decompressorreader(self, fh): |
|
3390 | 3390 | def gen(): |
|
3391 | 3391 | # The input stream doesn't have the 'BZ' header. So add it back. |
|
3392 | 3392 | d = bz2.BZ2Decompressor() |
|
3393 | 3393 | d.decompress('BZ') |
|
3394 | 3394 | for chunk in filechunkiter(fh): |
|
3395 | 3395 | yield d.decompress(chunk) |
|
3396 | 3396 | |
|
3397 | 3397 | return chunkbuffer(gen()) |
|
3398 | 3398 | |
|
3399 | 3399 | compengines.register(_truncatedbz2engine()) |
|
3400 | 3400 | |
|
3401 | 3401 | class _noopengine(compressionengine): |
|
3402 | 3402 | def name(self): |
|
3403 | 3403 | return 'none' |
|
3404 | 3404 | |
|
3405 | 3405 | def bundletype(self): |
|
3406 | 3406 | return 'none', 'UN' |
|
3407 | 3407 | |
|
3408 | 3408 | # Clients always support uncompressed payloads. Servers don't because |
|
3409 | 3409 | # unless you are on a fast network, uncompressed payloads can easily |
|
3410 | 3410 | # saturate your network pipe. |
|
3411 | 3411 | def wireprotosupport(self): |
|
3412 | 3412 | return compewireprotosupport('none', 0, 10) |
|
3413 | 3413 | |
|
3414 | 3414 | # We don't implement revlogheader because it is handled specially |
|
3415 | 3415 | # in the revlog class. |
|
3416 | 3416 | |
|
3417 | 3417 | def compressstream(self, it, opts=None): |
|
3418 | 3418 | return it |
|
3419 | 3419 | |
|
3420 | 3420 | def decompressorreader(self, fh): |
|
3421 | 3421 | return fh |
|
3422 | 3422 | |
|
3423 | 3423 | class nooprevlogcompressor(object): |
|
3424 | 3424 | def compress(self, data): |
|
3425 | 3425 | return None |
|
3426 | 3426 | |
|
3427 | 3427 | def revlogcompressor(self, opts=None): |
|
3428 | 3428 | return self.nooprevlogcompressor() |
|
3429 | 3429 | |
|
3430 | 3430 | compengines.register(_noopengine()) |
|
3431 | 3431 | |
|
3432 | 3432 | class _zstdengine(compressionengine): |
|
3433 | 3433 | def name(self): |
|
3434 | 3434 | return 'zstd' |
|
3435 | 3435 | |
|
3436 | 3436 | @propertycache |
|
3437 | 3437 | def _module(self): |
|
3438 | 3438 | # Not all installs have the zstd module available. So defer importing |
|
3439 | 3439 | # until first access. |
|
3440 | 3440 | try: |
|
3441 | 3441 | from . import zstd |
|
3442 | 3442 | # Force delayed import. |
|
3443 | 3443 | zstd.__version__ |
|
3444 | 3444 | return zstd |
|
3445 | 3445 | except ImportError: |
|
3446 | 3446 | return None |
|
3447 | 3447 | |
|
3448 | 3448 | def available(self): |
|
3449 | 3449 | return bool(self._module) |
|
3450 | 3450 | |
|
3451 | 3451 | def bundletype(self): |
|
3452 | 3452 | return 'zstd', 'ZS' |
|
3453 | 3453 | |
|
3454 | 3454 | def wireprotosupport(self): |
|
3455 | 3455 | return compewireprotosupport('zstd', 50, 50) |
|
3456 | 3456 | |
|
3457 | 3457 | def revlogheader(self): |
|
3458 | 3458 | return '\x28' |
|
3459 | 3459 | |
|
3460 | 3460 | def compressstream(self, it, opts=None): |
|
3461 | 3461 | opts = opts or {} |
|
3462 | 3462 | # zstd level 3 is almost always significantly faster than zlib |
|
3463 | 3463 | # while providing no worse compression. It strikes a good balance |
|
3464 | 3464 | # between speed and compression. |
|
3465 | 3465 | level = opts.get('level', 3) |
|
3466 | 3466 | |
|
3467 | 3467 | zstd = self._module |
|
3468 | 3468 | z = zstd.ZstdCompressor(level=level).compressobj() |
|
3469 | 3469 | for chunk in it: |
|
3470 | 3470 | data = z.compress(chunk) |
|
3471 | 3471 | if data: |
|
3472 | 3472 | yield data |
|
3473 | 3473 | |
|
3474 | 3474 | yield z.flush() |
|
3475 | 3475 | |
|
3476 | 3476 | def decompressorreader(self, fh): |
|
3477 | 3477 | zstd = self._module |
|
3478 | 3478 | dctx = zstd.ZstdDecompressor() |
|
3479 | 3479 | return chunkbuffer(dctx.read_from(fh)) |
|
3480 | 3480 | |
|
3481 | 3481 | class zstdrevlogcompressor(object): |
|
3482 | 3482 | def __init__(self, zstd, level=3): |
|
3483 | 3483 | # Writing the content size adds a few bytes to the output. However, |
|
3484 | 3484 | # it allows decompression to be more optimal since we can |
|
3485 | 3485 | # pre-allocate a buffer to hold the result. |
|
3486 | 3486 | self._cctx = zstd.ZstdCompressor(level=level, |
|
3487 | 3487 | write_content_size=True) |
|
3488 | 3488 | self._dctx = zstd.ZstdDecompressor() |
|
3489 | 3489 | self._compinsize = zstd.COMPRESSION_RECOMMENDED_INPUT_SIZE |
|
3490 | 3490 | self._decompinsize = zstd.DECOMPRESSION_RECOMMENDED_INPUT_SIZE |
|
3491 | 3491 | |
|
3492 | 3492 | def compress(self, data): |
|
3493 | 3493 | insize = len(data) |
|
3494 | 3494 | # Caller handles empty input case. |
|
3495 | 3495 | assert insize > 0 |
|
3496 | 3496 | |
|
3497 | 3497 | if insize < 50: |
|
3498 | 3498 | return None |
|
3499 | 3499 | |
|
3500 | 3500 | elif insize <= 1000000: |
|
3501 | 3501 | compressed = self._cctx.compress(data) |
|
3502 | 3502 | if len(compressed) < insize: |
|
3503 | 3503 | return compressed |
|
3504 | 3504 | return None |
|
3505 | 3505 | else: |
|
3506 | 3506 | z = self._cctx.compressobj() |
|
3507 | 3507 | chunks = [] |
|
3508 | 3508 | pos = 0 |
|
3509 | 3509 | while pos < insize: |
|
3510 | 3510 | pos2 = pos + self._compinsize |
|
3511 | 3511 | chunk = z.compress(data[pos:pos2]) |
|
3512 | 3512 | if chunk: |
|
3513 | 3513 | chunks.append(chunk) |
|
3514 | 3514 | pos = pos2 |
|
3515 | 3515 | chunks.append(z.flush()) |
|
3516 | 3516 | |
|
3517 | 3517 | if sum(map(len, chunks)) < insize: |
|
3518 | 3518 | return ''.join(chunks) |
|
3519 | 3519 | return None |
|
3520 | 3520 | |
|
3521 | 3521 | def decompress(self, data): |
|
3522 | 3522 | insize = len(data) |
|
3523 | 3523 | |
|
3524 | 3524 | try: |
|
3525 | 3525 | # This was measured to be faster than other streaming |
|
3526 | 3526 | # decompressors. |
|
3527 | 3527 | dobj = self._dctx.decompressobj() |
|
3528 | 3528 | chunks = [] |
|
3529 | 3529 | pos = 0 |
|
3530 | 3530 | while pos < insize: |
|
3531 | 3531 | pos2 = pos + self._decompinsize |
|
3532 | 3532 | chunk = dobj.decompress(data[pos:pos2]) |
|
3533 | 3533 | if chunk: |
|
3534 | 3534 | chunks.append(chunk) |
|
3535 | 3535 | pos = pos2 |
|
3536 | 3536 | # Frame should be exhausted, so no finish() API. |
|
3537 | 3537 | |
|
3538 | 3538 | return ''.join(chunks) |
|
3539 | 3539 | except Exception as e: |
|
3540 | 3540 | raise error.RevlogError(_('revlog decompress error: %s') % |
|
3541 | 3541 | str(e)) |
|
3542 | 3542 | |
|
3543 | 3543 | def revlogcompressor(self, opts=None): |
|
3544 | 3544 | opts = opts or {} |
|
3545 | 3545 | return self.zstdrevlogcompressor(self._module, |
|
3546 | 3546 | level=opts.get('level', 3)) |
|
3547 | 3547 | |
|
3548 | 3548 | compengines.register(_zstdengine()) |
|
3549 | 3549 | |
|
3550 | 3550 | # convenient shortcut |
|
3551 | 3551 | dst = debugstacktrace |
General Comments 0
You need to be logged in to leave comments.
Login now