##// END OF EJS Templates
wireproto: properly call clonebundles command...
Gregory Szorc -
r37667:a1687996 default
parent child Browse files
Show More
@@ -1,2409 +1,2410 b''
1 1 # exchange.py - utility to exchange data between repos.
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 collections
11 11 import errno
12 12 import hashlib
13 13
14 14 from .i18n import _
15 15 from .node import (
16 16 bin,
17 17 hex,
18 18 nullid,
19 19 )
20 20 from .thirdparty import (
21 21 attr,
22 22 )
23 23 from . import (
24 24 bookmarks as bookmod,
25 25 bundle2,
26 26 changegroup,
27 27 discovery,
28 28 error,
29 29 lock as lockmod,
30 30 logexchange,
31 31 obsolete,
32 32 phases,
33 33 pushkey,
34 34 pycompat,
35 35 scmutil,
36 36 sslutil,
37 37 streamclone,
38 38 url as urlmod,
39 39 util,
40 40 )
41 41 from .utils import (
42 42 stringutil,
43 43 )
44 44
45 45 urlerr = util.urlerr
46 46 urlreq = util.urlreq
47 47
48 48 # Maps bundle version human names to changegroup versions.
49 49 _bundlespeccgversions = {'v1': '01',
50 50 'v2': '02',
51 51 'packed1': 's1',
52 52 'bundle2': '02', #legacy
53 53 }
54 54
55 55 # Maps bundle version with content opts to choose which part to bundle
56 56 _bundlespeccontentopts = {
57 57 'v1': {
58 58 'changegroup': True,
59 59 'cg.version': '01',
60 60 'obsolescence': False,
61 61 'phases': False,
62 62 'tagsfnodescache': False,
63 63 'revbranchcache': False
64 64 },
65 65 'v2': {
66 66 'changegroup': True,
67 67 'cg.version': '02',
68 68 'obsolescence': False,
69 69 'phases': False,
70 70 'tagsfnodescache': True,
71 71 'revbranchcache': True
72 72 },
73 73 'packed1' : {
74 74 'cg.version': 's1'
75 75 }
76 76 }
77 77 _bundlespeccontentopts['bundle2'] = _bundlespeccontentopts['v2']
78 78
79 79 _bundlespecvariants = {"streamv2": {"changegroup": False, "streamv2": True,
80 80 "tagsfnodescache": False,
81 81 "revbranchcache": False}}
82 82
83 83 # Compression engines allowed in version 1. THIS SHOULD NEVER CHANGE.
84 84 _bundlespecv1compengines = {'gzip', 'bzip2', 'none'}
85 85
86 86 @attr.s
87 87 class bundlespec(object):
88 88 compression = attr.ib()
89 89 version = attr.ib()
90 90 params = attr.ib()
91 91 contentopts = attr.ib()
92 92
93 93 def parsebundlespec(repo, spec, strict=True, externalnames=False):
94 94 """Parse a bundle string specification into parts.
95 95
96 96 Bundle specifications denote a well-defined bundle/exchange format.
97 97 The content of a given specification should not change over time in
98 98 order to ensure that bundles produced by a newer version of Mercurial are
99 99 readable from an older version.
100 100
101 101 The string currently has the form:
102 102
103 103 <compression>-<type>[;<parameter0>[;<parameter1>]]
104 104
105 105 Where <compression> is one of the supported compression formats
106 106 and <type> is (currently) a version string. A ";" can follow the type and
107 107 all text afterwards is interpreted as URI encoded, ";" delimited key=value
108 108 pairs.
109 109
110 110 If ``strict`` is True (the default) <compression> is required. Otherwise,
111 111 it is optional.
112 112
113 113 If ``externalnames`` is False (the default), the human-centric names will
114 114 be converted to their internal representation.
115 115
116 116 Returns a bundlespec object of (compression, version, parameters).
117 117 Compression will be ``None`` if not in strict mode and a compression isn't
118 118 defined.
119 119
120 120 An ``InvalidBundleSpecification`` is raised when the specification is
121 121 not syntactically well formed.
122 122
123 123 An ``UnsupportedBundleSpecification`` is raised when the compression or
124 124 bundle type/version is not recognized.
125 125
126 126 Note: this function will likely eventually return a more complex data
127 127 structure, including bundle2 part information.
128 128 """
129 129 def parseparams(s):
130 130 if ';' not in s:
131 131 return s, {}
132 132
133 133 params = {}
134 134 version, paramstr = s.split(';', 1)
135 135
136 136 for p in paramstr.split(';'):
137 137 if '=' not in p:
138 138 raise error.InvalidBundleSpecification(
139 139 _('invalid bundle specification: '
140 140 'missing "=" in parameter: %s') % p)
141 141
142 142 key, value = p.split('=', 1)
143 143 key = urlreq.unquote(key)
144 144 value = urlreq.unquote(value)
145 145 params[key] = value
146 146
147 147 return version, params
148 148
149 149
150 150 if strict and '-' not in spec:
151 151 raise error.InvalidBundleSpecification(
152 152 _('invalid bundle specification; '
153 153 'must be prefixed with compression: %s') % spec)
154 154
155 155 if '-' in spec:
156 156 compression, version = spec.split('-', 1)
157 157
158 158 if compression not in util.compengines.supportedbundlenames:
159 159 raise error.UnsupportedBundleSpecification(
160 160 _('%s compression is not supported') % compression)
161 161
162 162 version, params = parseparams(version)
163 163
164 164 if version not in _bundlespeccgversions:
165 165 raise error.UnsupportedBundleSpecification(
166 166 _('%s is not a recognized bundle version') % version)
167 167 else:
168 168 # Value could be just the compression or just the version, in which
169 169 # case some defaults are assumed (but only when not in strict mode).
170 170 assert not strict
171 171
172 172 spec, params = parseparams(spec)
173 173
174 174 if spec in util.compengines.supportedbundlenames:
175 175 compression = spec
176 176 version = 'v1'
177 177 # Generaldelta repos require v2.
178 178 if 'generaldelta' in repo.requirements:
179 179 version = 'v2'
180 180 # Modern compression engines require v2.
181 181 if compression not in _bundlespecv1compengines:
182 182 version = 'v2'
183 183 elif spec in _bundlespeccgversions:
184 184 if spec == 'packed1':
185 185 compression = 'none'
186 186 else:
187 187 compression = 'bzip2'
188 188 version = spec
189 189 else:
190 190 raise error.UnsupportedBundleSpecification(
191 191 _('%s is not a recognized bundle specification') % spec)
192 192
193 193 # Bundle version 1 only supports a known set of compression engines.
194 194 if version == 'v1' and compression not in _bundlespecv1compengines:
195 195 raise error.UnsupportedBundleSpecification(
196 196 _('compression engine %s is not supported on v1 bundles') %
197 197 compression)
198 198
199 199 # The specification for packed1 can optionally declare the data formats
200 200 # required to apply it. If we see this metadata, compare against what the
201 201 # repo supports and error if the bundle isn't compatible.
202 202 if version == 'packed1' and 'requirements' in params:
203 203 requirements = set(params['requirements'].split(','))
204 204 missingreqs = requirements - repo.supportedformats
205 205 if missingreqs:
206 206 raise error.UnsupportedBundleSpecification(
207 207 _('missing support for repository features: %s') %
208 208 ', '.join(sorted(missingreqs)))
209 209
210 210 # Compute contentopts based on the version
211 211 contentopts = _bundlespeccontentopts.get(version, {}).copy()
212 212
213 213 # Process the variants
214 214 if "stream" in params and params["stream"] == "v2":
215 215 variant = _bundlespecvariants["streamv2"]
216 216 contentopts.update(variant)
217 217
218 218 if not externalnames:
219 219 engine = util.compengines.forbundlename(compression)
220 220 compression = engine.bundletype()[1]
221 221 version = _bundlespeccgversions[version]
222 222
223 223 return bundlespec(compression, version, params, contentopts)
224 224
225 225 def readbundle(ui, fh, fname, vfs=None):
226 226 header = changegroup.readexactly(fh, 4)
227 227
228 228 alg = None
229 229 if not fname:
230 230 fname = "stream"
231 231 if not header.startswith('HG') and header.startswith('\0'):
232 232 fh = changegroup.headerlessfixup(fh, header)
233 233 header = "HG10"
234 234 alg = 'UN'
235 235 elif vfs:
236 236 fname = vfs.join(fname)
237 237
238 238 magic, version = header[0:2], header[2:4]
239 239
240 240 if magic != 'HG':
241 241 raise error.Abort(_('%s: not a Mercurial bundle') % fname)
242 242 if version == '10':
243 243 if alg is None:
244 244 alg = changegroup.readexactly(fh, 2)
245 245 return changegroup.cg1unpacker(fh, alg)
246 246 elif version.startswith('2'):
247 247 return bundle2.getunbundler(ui, fh, magicstring=magic + version)
248 248 elif version == 'S1':
249 249 return streamclone.streamcloneapplier(fh)
250 250 else:
251 251 raise error.Abort(_('%s: unknown bundle version %s') % (fname, version))
252 252
253 253 def getbundlespec(ui, fh):
254 254 """Infer the bundlespec from a bundle file handle.
255 255
256 256 The input file handle is seeked and the original seek position is not
257 257 restored.
258 258 """
259 259 def speccompression(alg):
260 260 try:
261 261 return util.compengines.forbundletype(alg).bundletype()[0]
262 262 except KeyError:
263 263 return None
264 264
265 265 b = readbundle(ui, fh, None)
266 266 if isinstance(b, changegroup.cg1unpacker):
267 267 alg = b._type
268 268 if alg == '_truncatedBZ':
269 269 alg = 'BZ'
270 270 comp = speccompression(alg)
271 271 if not comp:
272 272 raise error.Abort(_('unknown compression algorithm: %s') % alg)
273 273 return '%s-v1' % comp
274 274 elif isinstance(b, bundle2.unbundle20):
275 275 if 'Compression' in b.params:
276 276 comp = speccompression(b.params['Compression'])
277 277 if not comp:
278 278 raise error.Abort(_('unknown compression algorithm: %s') % comp)
279 279 else:
280 280 comp = 'none'
281 281
282 282 version = None
283 283 for part in b.iterparts():
284 284 if part.type == 'changegroup':
285 285 version = part.params['version']
286 286 if version in ('01', '02'):
287 287 version = 'v2'
288 288 else:
289 289 raise error.Abort(_('changegroup version %s does not have '
290 290 'a known bundlespec') % version,
291 291 hint=_('try upgrading your Mercurial '
292 292 'client'))
293 293 elif part.type == 'stream2' and version is None:
294 294 # A stream2 part requires to be part of a v2 bundle
295 295 version = "v2"
296 296 requirements = urlreq.unquote(part.params['requirements'])
297 297 splitted = requirements.split()
298 298 params = bundle2._formatrequirementsparams(splitted)
299 299 return 'none-v2;stream=v2;%s' % params
300 300
301 301 if not version:
302 302 raise error.Abort(_('could not identify changegroup version in '
303 303 'bundle'))
304 304
305 305 return '%s-%s' % (comp, version)
306 306 elif isinstance(b, streamclone.streamcloneapplier):
307 307 requirements = streamclone.readbundle1header(fh)[2]
308 308 formatted = bundle2._formatrequirementsparams(requirements)
309 309 return 'none-packed1;%s' % formatted
310 310 else:
311 311 raise error.Abort(_('unknown bundle type: %s') % b)
312 312
313 313 def _computeoutgoing(repo, heads, common):
314 314 """Computes which revs are outgoing given a set of common
315 315 and a set of heads.
316 316
317 317 This is a separate function so extensions can have access to
318 318 the logic.
319 319
320 320 Returns a discovery.outgoing object.
321 321 """
322 322 cl = repo.changelog
323 323 if common:
324 324 hasnode = cl.hasnode
325 325 common = [n for n in common if hasnode(n)]
326 326 else:
327 327 common = [nullid]
328 328 if not heads:
329 329 heads = cl.heads()
330 330 return discovery.outgoing(repo, common, heads)
331 331
332 332 def _forcebundle1(op):
333 333 """return true if a pull/push must use bundle1
334 334
335 335 This function is used to allow testing of the older bundle version"""
336 336 ui = op.repo.ui
337 337 # The goal is this config is to allow developer to choose the bundle
338 338 # version used during exchanged. This is especially handy during test.
339 339 # Value is a list of bundle version to be picked from, highest version
340 340 # should be used.
341 341 #
342 342 # developer config: devel.legacy.exchange
343 343 exchange = ui.configlist('devel', 'legacy.exchange')
344 344 forcebundle1 = 'bundle2' not in exchange and 'bundle1' in exchange
345 345 return forcebundle1 or not op.remote.capable('bundle2')
346 346
347 347 class pushoperation(object):
348 348 """A object that represent a single push operation
349 349
350 350 Its purpose is to carry push related state and very common operations.
351 351
352 352 A new pushoperation should be created at the beginning of each push and
353 353 discarded afterward.
354 354 """
355 355
356 356 def __init__(self, repo, remote, force=False, revs=None, newbranch=False,
357 357 bookmarks=(), pushvars=None):
358 358 # repo we push from
359 359 self.repo = repo
360 360 self.ui = repo.ui
361 361 # repo we push to
362 362 self.remote = remote
363 363 # force option provided
364 364 self.force = force
365 365 # revs to be pushed (None is "all")
366 366 self.revs = revs
367 367 # bookmark explicitly pushed
368 368 self.bookmarks = bookmarks
369 369 # allow push of new branch
370 370 self.newbranch = newbranch
371 371 # step already performed
372 372 # (used to check what steps have been already performed through bundle2)
373 373 self.stepsdone = set()
374 374 # Integer version of the changegroup push result
375 375 # - None means nothing to push
376 376 # - 0 means HTTP error
377 377 # - 1 means we pushed and remote head count is unchanged *or*
378 378 # we have outgoing changesets but refused to push
379 379 # - other values as described by addchangegroup()
380 380 self.cgresult = None
381 381 # Boolean value for the bookmark push
382 382 self.bkresult = None
383 383 # discover.outgoing object (contains common and outgoing data)
384 384 self.outgoing = None
385 385 # all remote topological heads before the push
386 386 self.remoteheads = None
387 387 # Details of the remote branch pre and post push
388 388 #
389 389 # mapping: {'branch': ([remoteheads],
390 390 # [newheads],
391 391 # [unsyncedheads],
392 392 # [discardedheads])}
393 393 # - branch: the branch name
394 394 # - remoteheads: the list of remote heads known locally
395 395 # None if the branch is new
396 396 # - newheads: the new remote heads (known locally) with outgoing pushed
397 397 # - unsyncedheads: the list of remote heads unknown locally.
398 398 # - discardedheads: the list of remote heads made obsolete by the push
399 399 self.pushbranchmap = None
400 400 # testable as a boolean indicating if any nodes are missing locally.
401 401 self.incoming = None
402 402 # summary of the remote phase situation
403 403 self.remotephases = None
404 404 # phases changes that must be pushed along side the changesets
405 405 self.outdatedphases = None
406 406 # phases changes that must be pushed if changeset push fails
407 407 self.fallbackoutdatedphases = None
408 408 # outgoing obsmarkers
409 409 self.outobsmarkers = set()
410 410 # outgoing bookmarks
411 411 self.outbookmarks = []
412 412 # transaction manager
413 413 self.trmanager = None
414 414 # map { pushkey partid -> callback handling failure}
415 415 # used to handle exception from mandatory pushkey part failure
416 416 self.pkfailcb = {}
417 417 # an iterable of pushvars or None
418 418 self.pushvars = pushvars
419 419
420 420 @util.propertycache
421 421 def futureheads(self):
422 422 """future remote heads if the changeset push succeeds"""
423 423 return self.outgoing.missingheads
424 424
425 425 @util.propertycache
426 426 def fallbackheads(self):
427 427 """future remote heads if the changeset push fails"""
428 428 if self.revs is None:
429 429 # not target to push, all common are relevant
430 430 return self.outgoing.commonheads
431 431 unfi = self.repo.unfiltered()
432 432 # I want cheads = heads(::missingheads and ::commonheads)
433 433 # (missingheads is revs with secret changeset filtered out)
434 434 #
435 435 # This can be expressed as:
436 436 # cheads = ( (missingheads and ::commonheads)
437 437 # + (commonheads and ::missingheads))"
438 438 # )
439 439 #
440 440 # while trying to push we already computed the following:
441 441 # common = (::commonheads)
442 442 # missing = ((commonheads::missingheads) - commonheads)
443 443 #
444 444 # We can pick:
445 445 # * missingheads part of common (::commonheads)
446 446 common = self.outgoing.common
447 447 nm = self.repo.changelog.nodemap
448 448 cheads = [node for node in self.revs if nm[node] in common]
449 449 # and
450 450 # * commonheads parents on missing
451 451 revset = unfi.set('%ln and parents(roots(%ln))',
452 452 self.outgoing.commonheads,
453 453 self.outgoing.missing)
454 454 cheads.extend(c.node() for c in revset)
455 455 return cheads
456 456
457 457 @property
458 458 def commonheads(self):
459 459 """set of all common heads after changeset bundle push"""
460 460 if self.cgresult:
461 461 return self.futureheads
462 462 else:
463 463 return self.fallbackheads
464 464
465 465 # mapping of message used when pushing bookmark
466 466 bookmsgmap = {'update': (_("updating bookmark %s\n"),
467 467 _('updating bookmark %s failed!\n')),
468 468 'export': (_("exporting bookmark %s\n"),
469 469 _('exporting bookmark %s failed!\n')),
470 470 'delete': (_("deleting remote bookmark %s\n"),
471 471 _('deleting remote bookmark %s failed!\n')),
472 472 }
473 473
474 474
475 475 def push(repo, remote, force=False, revs=None, newbranch=False, bookmarks=(),
476 476 opargs=None):
477 477 '''Push outgoing changesets (limited by revs) from a local
478 478 repository to remote. Return an integer:
479 479 - None means nothing to push
480 480 - 0 means HTTP error
481 481 - 1 means we pushed and remote head count is unchanged *or*
482 482 we have outgoing changesets but refused to push
483 483 - other values as described by addchangegroup()
484 484 '''
485 485 if opargs is None:
486 486 opargs = {}
487 487 pushop = pushoperation(repo, remote, force, revs, newbranch, bookmarks,
488 488 **pycompat.strkwargs(opargs))
489 489 if pushop.remote.local():
490 490 missing = (set(pushop.repo.requirements)
491 491 - pushop.remote.local().supported)
492 492 if missing:
493 493 msg = _("required features are not"
494 494 " supported in the destination:"
495 495 " %s") % (', '.join(sorted(missing)))
496 496 raise error.Abort(msg)
497 497
498 498 if not pushop.remote.canpush():
499 499 raise error.Abort(_("destination does not support push"))
500 500
501 501 if not pushop.remote.capable('unbundle'):
502 502 raise error.Abort(_('cannot push: destination does not support the '
503 503 'unbundle wire protocol command'))
504 504
505 505 # get lock as we might write phase data
506 506 wlock = lock = None
507 507 try:
508 508 # bundle2 push may receive a reply bundle touching bookmarks or other
509 509 # things requiring the wlock. Take it now to ensure proper ordering.
510 510 maypushback = pushop.ui.configbool('experimental', 'bundle2.pushback')
511 511 if (not _forcebundle1(pushop)) and maypushback:
512 512 wlock = pushop.repo.wlock()
513 513 lock = pushop.repo.lock()
514 514 pushop.trmanager = transactionmanager(pushop.repo,
515 515 'push-response',
516 516 pushop.remote.url())
517 517 except IOError as err:
518 518 if err.errno != errno.EACCES:
519 519 raise
520 520 # source repo cannot be locked.
521 521 # We do not abort the push, but just disable the local phase
522 522 # synchronisation.
523 523 msg = 'cannot lock source repository: %s\n' % err
524 524 pushop.ui.debug(msg)
525 525
526 526 with wlock or util.nullcontextmanager(), \
527 527 lock or util.nullcontextmanager(), \
528 528 pushop.trmanager or util.nullcontextmanager():
529 529 pushop.repo.checkpush(pushop)
530 530 _pushdiscovery(pushop)
531 531 if not _forcebundle1(pushop):
532 532 _pushbundle2(pushop)
533 533 _pushchangeset(pushop)
534 534 _pushsyncphase(pushop)
535 535 _pushobsolete(pushop)
536 536 _pushbookmark(pushop)
537 537
538 538 return pushop
539 539
540 540 # list of steps to perform discovery before push
541 541 pushdiscoveryorder = []
542 542
543 543 # Mapping between step name and function
544 544 #
545 545 # This exists to help extensions wrap steps if necessary
546 546 pushdiscoverymapping = {}
547 547
548 548 def pushdiscovery(stepname):
549 549 """decorator for function performing discovery before push
550 550
551 551 The function is added to the step -> function mapping and appended to the
552 552 list of steps. Beware that decorated function will be added in order (this
553 553 may matter).
554 554
555 555 You can only use this decorator for a new step, if you want to wrap a step
556 556 from an extension, change the pushdiscovery dictionary directly."""
557 557 def dec(func):
558 558 assert stepname not in pushdiscoverymapping
559 559 pushdiscoverymapping[stepname] = func
560 560 pushdiscoveryorder.append(stepname)
561 561 return func
562 562 return dec
563 563
564 564 def _pushdiscovery(pushop):
565 565 """Run all discovery steps"""
566 566 for stepname in pushdiscoveryorder:
567 567 step = pushdiscoverymapping[stepname]
568 568 step(pushop)
569 569
570 570 @pushdiscovery('changeset')
571 571 def _pushdiscoverychangeset(pushop):
572 572 """discover the changeset that need to be pushed"""
573 573 fci = discovery.findcommonincoming
574 574 if pushop.revs:
575 575 commoninc = fci(pushop.repo, pushop.remote, force=pushop.force,
576 576 ancestorsof=pushop.revs)
577 577 else:
578 578 commoninc = fci(pushop.repo, pushop.remote, force=pushop.force)
579 579 common, inc, remoteheads = commoninc
580 580 fco = discovery.findcommonoutgoing
581 581 outgoing = fco(pushop.repo, pushop.remote, onlyheads=pushop.revs,
582 582 commoninc=commoninc, force=pushop.force)
583 583 pushop.outgoing = outgoing
584 584 pushop.remoteheads = remoteheads
585 585 pushop.incoming = inc
586 586
587 587 @pushdiscovery('phase')
588 588 def _pushdiscoveryphase(pushop):
589 589 """discover the phase that needs to be pushed
590 590
591 591 (computed for both success and failure case for changesets push)"""
592 592 outgoing = pushop.outgoing
593 593 unfi = pushop.repo.unfiltered()
594 594 remotephases = pushop.remote.listkeys('phases')
595 595 if (pushop.ui.configbool('ui', '_usedassubrepo')
596 596 and remotephases # server supports phases
597 597 and not pushop.outgoing.missing # no changesets to be pushed
598 598 and remotephases.get('publishing', False)):
599 599 # When:
600 600 # - this is a subrepo push
601 601 # - and remote support phase
602 602 # - and no changeset are to be pushed
603 603 # - and remote is publishing
604 604 # We may be in issue 3781 case!
605 605 # We drop the possible phase synchronisation done by
606 606 # courtesy to publish changesets possibly locally draft
607 607 # on the remote.
608 608 pushop.outdatedphases = []
609 609 pushop.fallbackoutdatedphases = []
610 610 return
611 611
612 612 pushop.remotephases = phases.remotephasessummary(pushop.repo,
613 613 pushop.fallbackheads,
614 614 remotephases)
615 615 droots = pushop.remotephases.draftroots
616 616
617 617 extracond = ''
618 618 if not pushop.remotephases.publishing:
619 619 extracond = ' and public()'
620 620 revset = 'heads((%%ln::%%ln) %s)' % extracond
621 621 # Get the list of all revs draft on remote by public here.
622 622 # XXX Beware that revset break if droots is not strictly
623 623 # XXX root we may want to ensure it is but it is costly
624 624 fallback = list(unfi.set(revset, droots, pushop.fallbackheads))
625 625 if not outgoing.missing:
626 626 future = fallback
627 627 else:
628 628 # adds changeset we are going to push as draft
629 629 #
630 630 # should not be necessary for publishing server, but because of an
631 631 # issue fixed in xxxxx we have to do it anyway.
632 632 fdroots = list(unfi.set('roots(%ln + %ln::)',
633 633 outgoing.missing, droots))
634 634 fdroots = [f.node() for f in fdroots]
635 635 future = list(unfi.set(revset, fdroots, pushop.futureheads))
636 636 pushop.outdatedphases = future
637 637 pushop.fallbackoutdatedphases = fallback
638 638
639 639 @pushdiscovery('obsmarker')
640 640 def _pushdiscoveryobsmarkers(pushop):
641 641 if (obsolete.isenabled(pushop.repo, obsolete.exchangeopt)
642 642 and pushop.repo.obsstore
643 643 and 'obsolete' in pushop.remote.listkeys('namespaces')):
644 644 repo = pushop.repo
645 645 # very naive computation, that can be quite expensive on big repo.
646 646 # However: evolution is currently slow on them anyway.
647 647 nodes = (c.node() for c in repo.set('::%ln', pushop.futureheads))
648 648 pushop.outobsmarkers = pushop.repo.obsstore.relevantmarkers(nodes)
649 649
650 650 @pushdiscovery('bookmarks')
651 651 def _pushdiscoverybookmarks(pushop):
652 652 ui = pushop.ui
653 653 repo = pushop.repo.unfiltered()
654 654 remote = pushop.remote
655 655 ui.debug("checking for updated bookmarks\n")
656 656 ancestors = ()
657 657 if pushop.revs:
658 658 revnums = map(repo.changelog.rev, pushop.revs)
659 659 ancestors = repo.changelog.ancestors(revnums, inclusive=True)
660 660 remotebookmark = remote.listkeys('bookmarks')
661 661
662 662 explicit = set([repo._bookmarks.expandname(bookmark)
663 663 for bookmark in pushop.bookmarks])
664 664
665 665 remotebookmark = bookmod.unhexlifybookmarks(remotebookmark)
666 666 comp = bookmod.comparebookmarks(repo, repo._bookmarks, remotebookmark)
667 667
668 668 def safehex(x):
669 669 if x is None:
670 670 return x
671 671 return hex(x)
672 672
673 673 def hexifycompbookmarks(bookmarks):
674 674 return [(b, safehex(scid), safehex(dcid))
675 675 for (b, scid, dcid) in bookmarks]
676 676
677 677 comp = [hexifycompbookmarks(marks) for marks in comp]
678 678 return _processcompared(pushop, ancestors, explicit, remotebookmark, comp)
679 679
680 680 def _processcompared(pushop, pushed, explicit, remotebms, comp):
681 681 """take decision on bookmark to pull from the remote bookmark
682 682
683 683 Exist to help extensions who want to alter this behavior.
684 684 """
685 685 addsrc, adddst, advsrc, advdst, diverge, differ, invalid, same = comp
686 686
687 687 repo = pushop.repo
688 688
689 689 for b, scid, dcid in advsrc:
690 690 if b in explicit:
691 691 explicit.remove(b)
692 692 if not pushed or repo[scid].rev() in pushed:
693 693 pushop.outbookmarks.append((b, dcid, scid))
694 694 # search added bookmark
695 695 for b, scid, dcid in addsrc:
696 696 if b in explicit:
697 697 explicit.remove(b)
698 698 pushop.outbookmarks.append((b, '', scid))
699 699 # search for overwritten bookmark
700 700 for b, scid, dcid in list(advdst) + list(diverge) + list(differ):
701 701 if b in explicit:
702 702 explicit.remove(b)
703 703 pushop.outbookmarks.append((b, dcid, scid))
704 704 # search for bookmark to delete
705 705 for b, scid, dcid in adddst:
706 706 if b in explicit:
707 707 explicit.remove(b)
708 708 # treat as "deleted locally"
709 709 pushop.outbookmarks.append((b, dcid, ''))
710 710 # identical bookmarks shouldn't get reported
711 711 for b, scid, dcid in same:
712 712 if b in explicit:
713 713 explicit.remove(b)
714 714
715 715 if explicit:
716 716 explicit = sorted(explicit)
717 717 # we should probably list all of them
718 718 pushop.ui.warn(_('bookmark %s does not exist on the local '
719 719 'or remote repository!\n') % explicit[0])
720 720 pushop.bkresult = 2
721 721
722 722 pushop.outbookmarks.sort()
723 723
724 724 def _pushcheckoutgoing(pushop):
725 725 outgoing = pushop.outgoing
726 726 unfi = pushop.repo.unfiltered()
727 727 if not outgoing.missing:
728 728 # nothing to push
729 729 scmutil.nochangesfound(unfi.ui, unfi, outgoing.excluded)
730 730 return False
731 731 # something to push
732 732 if not pushop.force:
733 733 # if repo.obsstore == False --> no obsolete
734 734 # then, save the iteration
735 735 if unfi.obsstore:
736 736 # this message are here for 80 char limit reason
737 737 mso = _("push includes obsolete changeset: %s!")
738 738 mspd = _("push includes phase-divergent changeset: %s!")
739 739 mscd = _("push includes content-divergent changeset: %s!")
740 740 mst = {"orphan": _("push includes orphan changeset: %s!"),
741 741 "phase-divergent": mspd,
742 742 "content-divergent": mscd}
743 743 # If we are to push if there is at least one
744 744 # obsolete or unstable changeset in missing, at
745 745 # least one of the missinghead will be obsolete or
746 746 # unstable. So checking heads only is ok
747 747 for node in outgoing.missingheads:
748 748 ctx = unfi[node]
749 749 if ctx.obsolete():
750 750 raise error.Abort(mso % ctx)
751 751 elif ctx.isunstable():
752 752 # TODO print more than one instability in the abort
753 753 # message
754 754 raise error.Abort(mst[ctx.instabilities()[0]] % ctx)
755 755
756 756 discovery.checkheads(pushop)
757 757 return True
758 758
759 759 # List of names of steps to perform for an outgoing bundle2, order matters.
760 760 b2partsgenorder = []
761 761
762 762 # Mapping between step name and function
763 763 #
764 764 # This exists to help extensions wrap steps if necessary
765 765 b2partsgenmapping = {}
766 766
767 767 def b2partsgenerator(stepname, idx=None):
768 768 """decorator for function generating bundle2 part
769 769
770 770 The function is added to the step -> function mapping and appended to the
771 771 list of steps. Beware that decorated functions will be added in order
772 772 (this may matter).
773 773
774 774 You can only use this decorator for new steps, if you want to wrap a step
775 775 from an extension, attack the b2partsgenmapping dictionary directly."""
776 776 def dec(func):
777 777 assert stepname not in b2partsgenmapping
778 778 b2partsgenmapping[stepname] = func
779 779 if idx is None:
780 780 b2partsgenorder.append(stepname)
781 781 else:
782 782 b2partsgenorder.insert(idx, stepname)
783 783 return func
784 784 return dec
785 785
786 786 def _pushb2ctxcheckheads(pushop, bundler):
787 787 """Generate race condition checking parts
788 788
789 789 Exists as an independent function to aid extensions
790 790 """
791 791 # * 'force' do not check for push race,
792 792 # * if we don't push anything, there are nothing to check.
793 793 if not pushop.force and pushop.outgoing.missingheads:
794 794 allowunrelated = 'related' in bundler.capabilities.get('checkheads', ())
795 795 emptyremote = pushop.pushbranchmap is None
796 796 if not allowunrelated or emptyremote:
797 797 bundler.newpart('check:heads', data=iter(pushop.remoteheads))
798 798 else:
799 799 affected = set()
800 800 for branch, heads in pushop.pushbranchmap.iteritems():
801 801 remoteheads, newheads, unsyncedheads, discardedheads = heads
802 802 if remoteheads is not None:
803 803 remote = set(remoteheads)
804 804 affected |= set(discardedheads) & remote
805 805 affected |= remote - set(newheads)
806 806 if affected:
807 807 data = iter(sorted(affected))
808 808 bundler.newpart('check:updated-heads', data=data)
809 809
810 810 def _pushing(pushop):
811 811 """return True if we are pushing anything"""
812 812 return bool(pushop.outgoing.missing
813 813 or pushop.outdatedphases
814 814 or pushop.outobsmarkers
815 815 or pushop.outbookmarks)
816 816
817 817 @b2partsgenerator('check-bookmarks')
818 818 def _pushb2checkbookmarks(pushop, bundler):
819 819 """insert bookmark move checking"""
820 820 if not _pushing(pushop) or pushop.force:
821 821 return
822 822 b2caps = bundle2.bundle2caps(pushop.remote)
823 823 hasbookmarkcheck = 'bookmarks' in b2caps
824 824 if not (pushop.outbookmarks and hasbookmarkcheck):
825 825 return
826 826 data = []
827 827 for book, old, new in pushop.outbookmarks:
828 828 old = bin(old)
829 829 data.append((book, old))
830 830 checkdata = bookmod.binaryencode(data)
831 831 bundler.newpart('check:bookmarks', data=checkdata)
832 832
833 833 @b2partsgenerator('check-phases')
834 834 def _pushb2checkphases(pushop, bundler):
835 835 """insert phase move checking"""
836 836 if not _pushing(pushop) or pushop.force:
837 837 return
838 838 b2caps = bundle2.bundle2caps(pushop.remote)
839 839 hasphaseheads = 'heads' in b2caps.get('phases', ())
840 840 if pushop.remotephases is not None and hasphaseheads:
841 841 # check that the remote phase has not changed
842 842 checks = [[] for p in phases.allphases]
843 843 checks[phases.public].extend(pushop.remotephases.publicheads)
844 844 checks[phases.draft].extend(pushop.remotephases.draftroots)
845 845 if any(checks):
846 846 for nodes in checks:
847 847 nodes.sort()
848 848 checkdata = phases.binaryencode(checks)
849 849 bundler.newpart('check:phases', data=checkdata)
850 850
851 851 @b2partsgenerator('changeset')
852 852 def _pushb2ctx(pushop, bundler):
853 853 """handle changegroup push through bundle2
854 854
855 855 addchangegroup result is stored in the ``pushop.cgresult`` attribute.
856 856 """
857 857 if 'changesets' in pushop.stepsdone:
858 858 return
859 859 pushop.stepsdone.add('changesets')
860 860 # Send known heads to the server for race detection.
861 861 if not _pushcheckoutgoing(pushop):
862 862 return
863 863 pushop.repo.prepushoutgoinghooks(pushop)
864 864
865 865 _pushb2ctxcheckheads(pushop, bundler)
866 866
867 867 b2caps = bundle2.bundle2caps(pushop.remote)
868 868 version = '01'
869 869 cgversions = b2caps.get('changegroup')
870 870 if cgversions: # 3.1 and 3.2 ship with an empty value
871 871 cgversions = [v for v in cgversions
872 872 if v in changegroup.supportedoutgoingversions(
873 873 pushop.repo)]
874 874 if not cgversions:
875 875 raise ValueError(_('no common changegroup version'))
876 876 version = max(cgversions)
877 877 cgstream = changegroup.makestream(pushop.repo, pushop.outgoing, version,
878 878 'push')
879 879 cgpart = bundler.newpart('changegroup', data=cgstream)
880 880 if cgversions:
881 881 cgpart.addparam('version', version)
882 882 if 'treemanifest' in pushop.repo.requirements:
883 883 cgpart.addparam('treemanifest', '1')
884 884 def handlereply(op):
885 885 """extract addchangegroup returns from server reply"""
886 886 cgreplies = op.records.getreplies(cgpart.id)
887 887 assert len(cgreplies['changegroup']) == 1
888 888 pushop.cgresult = cgreplies['changegroup'][0]['return']
889 889 return handlereply
890 890
891 891 @b2partsgenerator('phase')
892 892 def _pushb2phases(pushop, bundler):
893 893 """handle phase push through bundle2"""
894 894 if 'phases' in pushop.stepsdone:
895 895 return
896 896 b2caps = bundle2.bundle2caps(pushop.remote)
897 897 ui = pushop.repo.ui
898 898
899 899 legacyphase = 'phases' in ui.configlist('devel', 'legacy.exchange')
900 900 haspushkey = 'pushkey' in b2caps
901 901 hasphaseheads = 'heads' in b2caps.get('phases', ())
902 902
903 903 if hasphaseheads and not legacyphase:
904 904 return _pushb2phaseheads(pushop, bundler)
905 905 elif haspushkey:
906 906 return _pushb2phasespushkey(pushop, bundler)
907 907
908 908 def _pushb2phaseheads(pushop, bundler):
909 909 """push phase information through a bundle2 - binary part"""
910 910 pushop.stepsdone.add('phases')
911 911 if pushop.outdatedphases:
912 912 updates = [[] for p in phases.allphases]
913 913 updates[0].extend(h.node() for h in pushop.outdatedphases)
914 914 phasedata = phases.binaryencode(updates)
915 915 bundler.newpart('phase-heads', data=phasedata)
916 916
917 917 def _pushb2phasespushkey(pushop, bundler):
918 918 """push phase information through a bundle2 - pushkey part"""
919 919 pushop.stepsdone.add('phases')
920 920 part2node = []
921 921
922 922 def handlefailure(pushop, exc):
923 923 targetid = int(exc.partid)
924 924 for partid, node in part2node:
925 925 if partid == targetid:
926 926 raise error.Abort(_('updating %s to public failed') % node)
927 927
928 928 enc = pushkey.encode
929 929 for newremotehead in pushop.outdatedphases:
930 930 part = bundler.newpart('pushkey')
931 931 part.addparam('namespace', enc('phases'))
932 932 part.addparam('key', enc(newremotehead.hex()))
933 933 part.addparam('old', enc('%d' % phases.draft))
934 934 part.addparam('new', enc('%d' % phases.public))
935 935 part2node.append((part.id, newremotehead))
936 936 pushop.pkfailcb[part.id] = handlefailure
937 937
938 938 def handlereply(op):
939 939 for partid, node in part2node:
940 940 partrep = op.records.getreplies(partid)
941 941 results = partrep['pushkey']
942 942 assert len(results) <= 1
943 943 msg = None
944 944 if not results:
945 945 msg = _('server ignored update of %s to public!\n') % node
946 946 elif not int(results[0]['return']):
947 947 msg = _('updating %s to public failed!\n') % node
948 948 if msg is not None:
949 949 pushop.ui.warn(msg)
950 950 return handlereply
951 951
952 952 @b2partsgenerator('obsmarkers')
953 953 def _pushb2obsmarkers(pushop, bundler):
954 954 if 'obsmarkers' in pushop.stepsdone:
955 955 return
956 956 remoteversions = bundle2.obsmarkersversion(bundler.capabilities)
957 957 if obsolete.commonversion(remoteversions) is None:
958 958 return
959 959 pushop.stepsdone.add('obsmarkers')
960 960 if pushop.outobsmarkers:
961 961 markers = sorted(pushop.outobsmarkers)
962 962 bundle2.buildobsmarkerspart(bundler, markers)
963 963
964 964 @b2partsgenerator('bookmarks')
965 965 def _pushb2bookmarks(pushop, bundler):
966 966 """handle bookmark push through bundle2"""
967 967 if 'bookmarks' in pushop.stepsdone:
968 968 return
969 969 b2caps = bundle2.bundle2caps(pushop.remote)
970 970
971 971 legacy = pushop.repo.ui.configlist('devel', 'legacy.exchange')
972 972 legacybooks = 'bookmarks' in legacy
973 973
974 974 if not legacybooks and 'bookmarks' in b2caps:
975 975 return _pushb2bookmarkspart(pushop, bundler)
976 976 elif 'pushkey' in b2caps:
977 977 return _pushb2bookmarkspushkey(pushop, bundler)
978 978
979 979 def _bmaction(old, new):
980 980 """small utility for bookmark pushing"""
981 981 if not old:
982 982 return 'export'
983 983 elif not new:
984 984 return 'delete'
985 985 return 'update'
986 986
987 987 def _pushb2bookmarkspart(pushop, bundler):
988 988 pushop.stepsdone.add('bookmarks')
989 989 if not pushop.outbookmarks:
990 990 return
991 991
992 992 allactions = []
993 993 data = []
994 994 for book, old, new in pushop.outbookmarks:
995 995 new = bin(new)
996 996 data.append((book, new))
997 997 allactions.append((book, _bmaction(old, new)))
998 998 checkdata = bookmod.binaryencode(data)
999 999 bundler.newpart('bookmarks', data=checkdata)
1000 1000
1001 1001 def handlereply(op):
1002 1002 ui = pushop.ui
1003 1003 # if success
1004 1004 for book, action in allactions:
1005 1005 ui.status(bookmsgmap[action][0] % book)
1006 1006
1007 1007 return handlereply
1008 1008
1009 1009 def _pushb2bookmarkspushkey(pushop, bundler):
1010 1010 pushop.stepsdone.add('bookmarks')
1011 1011 part2book = []
1012 1012 enc = pushkey.encode
1013 1013
1014 1014 def handlefailure(pushop, exc):
1015 1015 targetid = int(exc.partid)
1016 1016 for partid, book, action in part2book:
1017 1017 if partid == targetid:
1018 1018 raise error.Abort(bookmsgmap[action][1].rstrip() % book)
1019 1019 # we should not be called for part we did not generated
1020 1020 assert False
1021 1021
1022 1022 for book, old, new in pushop.outbookmarks:
1023 1023 part = bundler.newpart('pushkey')
1024 1024 part.addparam('namespace', enc('bookmarks'))
1025 1025 part.addparam('key', enc(book))
1026 1026 part.addparam('old', enc(old))
1027 1027 part.addparam('new', enc(new))
1028 1028 action = 'update'
1029 1029 if not old:
1030 1030 action = 'export'
1031 1031 elif not new:
1032 1032 action = 'delete'
1033 1033 part2book.append((part.id, book, action))
1034 1034 pushop.pkfailcb[part.id] = handlefailure
1035 1035
1036 1036 def handlereply(op):
1037 1037 ui = pushop.ui
1038 1038 for partid, book, action in part2book:
1039 1039 partrep = op.records.getreplies(partid)
1040 1040 results = partrep['pushkey']
1041 1041 assert len(results) <= 1
1042 1042 if not results:
1043 1043 pushop.ui.warn(_('server ignored bookmark %s update\n') % book)
1044 1044 else:
1045 1045 ret = int(results[0]['return'])
1046 1046 if ret:
1047 1047 ui.status(bookmsgmap[action][0] % book)
1048 1048 else:
1049 1049 ui.warn(bookmsgmap[action][1] % book)
1050 1050 if pushop.bkresult is not None:
1051 1051 pushop.bkresult = 1
1052 1052 return handlereply
1053 1053
1054 1054 @b2partsgenerator('pushvars', idx=0)
1055 1055 def _getbundlesendvars(pushop, bundler):
1056 1056 '''send shellvars via bundle2'''
1057 1057 pushvars = pushop.pushvars
1058 1058 if pushvars:
1059 1059 shellvars = {}
1060 1060 for raw in pushvars:
1061 1061 if '=' not in raw:
1062 1062 msg = ("unable to parse variable '%s', should follow "
1063 1063 "'KEY=VALUE' or 'KEY=' format")
1064 1064 raise error.Abort(msg % raw)
1065 1065 k, v = raw.split('=', 1)
1066 1066 shellvars[k] = v
1067 1067
1068 1068 part = bundler.newpart('pushvars')
1069 1069
1070 1070 for key, value in shellvars.iteritems():
1071 1071 part.addparam(key, value, mandatory=False)
1072 1072
1073 1073 def _pushbundle2(pushop):
1074 1074 """push data to the remote using bundle2
1075 1075
1076 1076 The only currently supported type of data is changegroup but this will
1077 1077 evolve in the future."""
1078 1078 bundler = bundle2.bundle20(pushop.ui, bundle2.bundle2caps(pushop.remote))
1079 1079 pushback = (pushop.trmanager
1080 1080 and pushop.ui.configbool('experimental', 'bundle2.pushback'))
1081 1081
1082 1082 # create reply capability
1083 1083 capsblob = bundle2.encodecaps(bundle2.getrepocaps(pushop.repo,
1084 1084 allowpushback=pushback,
1085 1085 role='client'))
1086 1086 bundler.newpart('replycaps', data=capsblob)
1087 1087 replyhandlers = []
1088 1088 for partgenname in b2partsgenorder:
1089 1089 partgen = b2partsgenmapping[partgenname]
1090 1090 ret = partgen(pushop, bundler)
1091 1091 if callable(ret):
1092 1092 replyhandlers.append(ret)
1093 1093 # do not push if nothing to push
1094 1094 if bundler.nbparts <= 1:
1095 1095 return
1096 1096 stream = util.chunkbuffer(bundler.getchunks())
1097 1097 try:
1098 1098 try:
1099 1099 with pushop.remote.commandexecutor() as e:
1100 1100 reply = e.callcommand('unbundle', {
1101 1101 'bundle': stream,
1102 1102 'heads': ['force'],
1103 1103 'url': pushop.remote.url(),
1104 1104 }).result()
1105 1105 except error.BundleValueError as exc:
1106 1106 raise error.Abort(_('missing support for %s') % exc)
1107 1107 try:
1108 1108 trgetter = None
1109 1109 if pushback:
1110 1110 trgetter = pushop.trmanager.transaction
1111 1111 op = bundle2.processbundle(pushop.repo, reply, trgetter)
1112 1112 except error.BundleValueError as exc:
1113 1113 raise error.Abort(_('missing support for %s') % exc)
1114 1114 except bundle2.AbortFromPart as exc:
1115 1115 pushop.ui.status(_('remote: %s\n') % exc)
1116 1116 if exc.hint is not None:
1117 1117 pushop.ui.status(_('remote: %s\n') % ('(%s)' % exc.hint))
1118 1118 raise error.Abort(_('push failed on remote'))
1119 1119 except error.PushkeyFailed as exc:
1120 1120 partid = int(exc.partid)
1121 1121 if partid not in pushop.pkfailcb:
1122 1122 raise
1123 1123 pushop.pkfailcb[partid](pushop, exc)
1124 1124 for rephand in replyhandlers:
1125 1125 rephand(op)
1126 1126
1127 1127 def _pushchangeset(pushop):
1128 1128 """Make the actual push of changeset bundle to remote repo"""
1129 1129 if 'changesets' in pushop.stepsdone:
1130 1130 return
1131 1131 pushop.stepsdone.add('changesets')
1132 1132 if not _pushcheckoutgoing(pushop):
1133 1133 return
1134 1134
1135 1135 # Should have verified this in push().
1136 1136 assert pushop.remote.capable('unbundle')
1137 1137
1138 1138 pushop.repo.prepushoutgoinghooks(pushop)
1139 1139 outgoing = pushop.outgoing
1140 1140 # TODO: get bundlecaps from remote
1141 1141 bundlecaps = None
1142 1142 # create a changegroup from local
1143 1143 if pushop.revs is None and not (outgoing.excluded
1144 1144 or pushop.repo.changelog.filteredrevs):
1145 1145 # push everything,
1146 1146 # use the fast path, no race possible on push
1147 1147 cg = changegroup.makechangegroup(pushop.repo, outgoing, '01', 'push',
1148 1148 fastpath=True, bundlecaps=bundlecaps)
1149 1149 else:
1150 1150 cg = changegroup.makechangegroup(pushop.repo, outgoing, '01',
1151 1151 'push', bundlecaps=bundlecaps)
1152 1152
1153 1153 # apply changegroup to remote
1154 1154 # local repo finds heads on server, finds out what
1155 1155 # revs it must push. once revs transferred, if server
1156 1156 # finds it has different heads (someone else won
1157 1157 # commit/push race), server aborts.
1158 1158 if pushop.force:
1159 1159 remoteheads = ['force']
1160 1160 else:
1161 1161 remoteheads = pushop.remoteheads
1162 1162 # ssh: return remote's addchangegroup()
1163 1163 # http: return remote's addchangegroup() or 0 for error
1164 1164 pushop.cgresult = pushop.remote.unbundle(cg, remoteheads,
1165 1165 pushop.repo.url())
1166 1166
1167 1167 def _pushsyncphase(pushop):
1168 1168 """synchronise phase information locally and remotely"""
1169 1169 cheads = pushop.commonheads
1170 1170 # even when we don't push, exchanging phase data is useful
1171 1171 remotephases = pushop.remote.listkeys('phases')
1172 1172 if (pushop.ui.configbool('ui', '_usedassubrepo')
1173 1173 and remotephases # server supports phases
1174 1174 and pushop.cgresult is None # nothing was pushed
1175 1175 and remotephases.get('publishing', False)):
1176 1176 # When:
1177 1177 # - this is a subrepo push
1178 1178 # - and remote support phase
1179 1179 # - and no changeset was pushed
1180 1180 # - and remote is publishing
1181 1181 # We may be in issue 3871 case!
1182 1182 # We drop the possible phase synchronisation done by
1183 1183 # courtesy to publish changesets possibly locally draft
1184 1184 # on the remote.
1185 1185 remotephases = {'publishing': 'True'}
1186 1186 if not remotephases: # old server or public only reply from non-publishing
1187 1187 _localphasemove(pushop, cheads)
1188 1188 # don't push any phase data as there is nothing to push
1189 1189 else:
1190 1190 ana = phases.analyzeremotephases(pushop.repo, cheads,
1191 1191 remotephases)
1192 1192 pheads, droots = ana
1193 1193 ### Apply remote phase on local
1194 1194 if remotephases.get('publishing', False):
1195 1195 _localphasemove(pushop, cheads)
1196 1196 else: # publish = False
1197 1197 _localphasemove(pushop, pheads)
1198 1198 _localphasemove(pushop, cheads, phases.draft)
1199 1199 ### Apply local phase on remote
1200 1200
1201 1201 if pushop.cgresult:
1202 1202 if 'phases' in pushop.stepsdone:
1203 1203 # phases already pushed though bundle2
1204 1204 return
1205 1205 outdated = pushop.outdatedphases
1206 1206 else:
1207 1207 outdated = pushop.fallbackoutdatedphases
1208 1208
1209 1209 pushop.stepsdone.add('phases')
1210 1210
1211 1211 # filter heads already turned public by the push
1212 1212 outdated = [c for c in outdated if c.node() not in pheads]
1213 1213 # fallback to independent pushkey command
1214 1214 for newremotehead in outdated:
1215 1215 with pushop.remote.commandexecutor() as e:
1216 1216 r = e.callcommand('pushkey', {
1217 1217 'namespace': 'phases',
1218 1218 'key': newremotehead.hex(),
1219 1219 'old': '%d' % phases.draft,
1220 1220 'new': '%d' % phases.public
1221 1221 }).result()
1222 1222
1223 1223 if not r:
1224 1224 pushop.ui.warn(_('updating %s to public failed!\n')
1225 1225 % newremotehead)
1226 1226
1227 1227 def _localphasemove(pushop, nodes, phase=phases.public):
1228 1228 """move <nodes> to <phase> in the local source repo"""
1229 1229 if pushop.trmanager:
1230 1230 phases.advanceboundary(pushop.repo,
1231 1231 pushop.trmanager.transaction(),
1232 1232 phase,
1233 1233 nodes)
1234 1234 else:
1235 1235 # repo is not locked, do not change any phases!
1236 1236 # Informs the user that phases should have been moved when
1237 1237 # applicable.
1238 1238 actualmoves = [n for n in nodes if phase < pushop.repo[n].phase()]
1239 1239 phasestr = phases.phasenames[phase]
1240 1240 if actualmoves:
1241 1241 pushop.ui.status(_('cannot lock source repo, skipping '
1242 1242 'local %s phase update\n') % phasestr)
1243 1243
1244 1244 def _pushobsolete(pushop):
1245 1245 """utility function to push obsolete markers to a remote"""
1246 1246 if 'obsmarkers' in pushop.stepsdone:
1247 1247 return
1248 1248 repo = pushop.repo
1249 1249 remote = pushop.remote
1250 1250 pushop.stepsdone.add('obsmarkers')
1251 1251 if pushop.outobsmarkers:
1252 1252 pushop.ui.debug('try to push obsolete markers to remote\n')
1253 1253 rslts = []
1254 1254 remotedata = obsolete._pushkeyescape(sorted(pushop.outobsmarkers))
1255 1255 for key in sorted(remotedata, reverse=True):
1256 1256 # reverse sort to ensure we end with dump0
1257 1257 data = remotedata[key]
1258 1258 rslts.append(remote.pushkey('obsolete', key, '', data))
1259 1259 if [r for r in rslts if not r]:
1260 1260 msg = _('failed to push some obsolete markers!\n')
1261 1261 repo.ui.warn(msg)
1262 1262
1263 1263 def _pushbookmark(pushop):
1264 1264 """Update bookmark position on remote"""
1265 1265 if pushop.cgresult == 0 or 'bookmarks' in pushop.stepsdone:
1266 1266 return
1267 1267 pushop.stepsdone.add('bookmarks')
1268 1268 ui = pushop.ui
1269 1269 remote = pushop.remote
1270 1270
1271 1271 for b, old, new in pushop.outbookmarks:
1272 1272 action = 'update'
1273 1273 if not old:
1274 1274 action = 'export'
1275 1275 elif not new:
1276 1276 action = 'delete'
1277 1277
1278 1278 with remote.commandexecutor() as e:
1279 1279 r = e.callcommand('pushkey', {
1280 1280 'namespace': 'bookmarks',
1281 1281 'key': b,
1282 1282 'old': old,
1283 1283 'new': new,
1284 1284 }).result()
1285 1285
1286 1286 if r:
1287 1287 ui.status(bookmsgmap[action][0] % b)
1288 1288 else:
1289 1289 ui.warn(bookmsgmap[action][1] % b)
1290 1290 # discovery can have set the value form invalid entry
1291 1291 if pushop.bkresult is not None:
1292 1292 pushop.bkresult = 1
1293 1293
1294 1294 class pulloperation(object):
1295 1295 """A object that represent a single pull operation
1296 1296
1297 1297 It purpose is to carry pull related state and very common operation.
1298 1298
1299 1299 A new should be created at the beginning of each pull and discarded
1300 1300 afterward.
1301 1301 """
1302 1302
1303 1303 def __init__(self, repo, remote, heads=None, force=False, bookmarks=(),
1304 1304 remotebookmarks=None, streamclonerequested=None):
1305 1305 # repo we pull into
1306 1306 self.repo = repo
1307 1307 # repo we pull from
1308 1308 self.remote = remote
1309 1309 # revision we try to pull (None is "all")
1310 1310 self.heads = heads
1311 1311 # bookmark pulled explicitly
1312 1312 self.explicitbookmarks = [repo._bookmarks.expandname(bookmark)
1313 1313 for bookmark in bookmarks]
1314 1314 # do we force pull?
1315 1315 self.force = force
1316 1316 # whether a streaming clone was requested
1317 1317 self.streamclonerequested = streamclonerequested
1318 1318 # transaction manager
1319 1319 self.trmanager = None
1320 1320 # set of common changeset between local and remote before pull
1321 1321 self.common = None
1322 1322 # set of pulled head
1323 1323 self.rheads = None
1324 1324 # list of missing changeset to fetch remotely
1325 1325 self.fetch = None
1326 1326 # remote bookmarks data
1327 1327 self.remotebookmarks = remotebookmarks
1328 1328 # result of changegroup pulling (used as return code by pull)
1329 1329 self.cgresult = None
1330 1330 # list of step already done
1331 1331 self.stepsdone = set()
1332 1332 # Whether we attempted a clone from pre-generated bundles.
1333 1333 self.clonebundleattempted = False
1334 1334
1335 1335 @util.propertycache
1336 1336 def pulledsubset(self):
1337 1337 """heads of the set of changeset target by the pull"""
1338 1338 # compute target subset
1339 1339 if self.heads is None:
1340 1340 # We pulled every thing possible
1341 1341 # sync on everything common
1342 1342 c = set(self.common)
1343 1343 ret = list(self.common)
1344 1344 for n in self.rheads:
1345 1345 if n not in c:
1346 1346 ret.append(n)
1347 1347 return ret
1348 1348 else:
1349 1349 # We pulled a specific subset
1350 1350 # sync on this subset
1351 1351 return self.heads
1352 1352
1353 1353 @util.propertycache
1354 1354 def canusebundle2(self):
1355 1355 return not _forcebundle1(self)
1356 1356
1357 1357 @util.propertycache
1358 1358 def remotebundle2caps(self):
1359 1359 return bundle2.bundle2caps(self.remote)
1360 1360
1361 1361 def gettransaction(self):
1362 1362 # deprecated; talk to trmanager directly
1363 1363 return self.trmanager.transaction()
1364 1364
1365 1365 class transactionmanager(util.transactional):
1366 1366 """An object to manage the life cycle of a transaction
1367 1367
1368 1368 It creates the transaction on demand and calls the appropriate hooks when
1369 1369 closing the transaction."""
1370 1370 def __init__(self, repo, source, url):
1371 1371 self.repo = repo
1372 1372 self.source = source
1373 1373 self.url = url
1374 1374 self._tr = None
1375 1375
1376 1376 def transaction(self):
1377 1377 """Return an open transaction object, constructing if necessary"""
1378 1378 if not self._tr:
1379 1379 trname = '%s\n%s' % (self.source, util.hidepassword(self.url))
1380 1380 self._tr = self.repo.transaction(trname)
1381 1381 self._tr.hookargs['source'] = self.source
1382 1382 self._tr.hookargs['url'] = self.url
1383 1383 return self._tr
1384 1384
1385 1385 def close(self):
1386 1386 """close transaction if created"""
1387 1387 if self._tr is not None:
1388 1388 self._tr.close()
1389 1389
1390 1390 def release(self):
1391 1391 """release transaction if created"""
1392 1392 if self._tr is not None:
1393 1393 self._tr.release()
1394 1394
1395 1395 def _fullpullbundle2(repo, pullop):
1396 1396 # The server may send a partial reply, i.e. when inlining
1397 1397 # pre-computed bundles. In that case, update the common
1398 1398 # set based on the results and pull another bundle.
1399 1399 #
1400 1400 # There are two indicators that the process is finished:
1401 1401 # - no changeset has been added, or
1402 1402 # - all remote heads are known locally.
1403 1403 # The head check must use the unfiltered view as obsoletion
1404 1404 # markers can hide heads.
1405 1405 unfi = repo.unfiltered()
1406 1406 unficl = unfi.changelog
1407 1407 def headsofdiff(h1, h2):
1408 1408 """Returns heads(h1 % h2)"""
1409 1409 res = unfi.set('heads(%ln %% %ln)', h1, h2)
1410 1410 return set(ctx.node() for ctx in res)
1411 1411 def headsofunion(h1, h2):
1412 1412 """Returns heads((h1 + h2) - null)"""
1413 1413 res = unfi.set('heads((%ln + %ln - null))', h1, h2)
1414 1414 return set(ctx.node() for ctx in res)
1415 1415 while True:
1416 1416 old_heads = unficl.heads()
1417 1417 clstart = len(unficl)
1418 1418 _pullbundle2(pullop)
1419 1419 if changegroup.NARROW_REQUIREMENT in repo.requirements:
1420 1420 # XXX narrow clones filter the heads on the server side during
1421 1421 # XXX getbundle and result in partial replies as well.
1422 1422 # XXX Disable pull bundles in this case as band aid to avoid
1423 1423 # XXX extra round trips.
1424 1424 break
1425 1425 if clstart == len(unficl):
1426 1426 break
1427 1427 if all(unficl.hasnode(n) for n in pullop.rheads):
1428 1428 break
1429 1429 new_heads = headsofdiff(unficl.heads(), old_heads)
1430 1430 pullop.common = headsofunion(new_heads, pullop.common)
1431 1431 pullop.rheads = set(pullop.rheads) - pullop.common
1432 1432
1433 1433 def pull(repo, remote, heads=None, force=False, bookmarks=(), opargs=None,
1434 1434 streamclonerequested=None):
1435 1435 """Fetch repository data from a remote.
1436 1436
1437 1437 This is the main function used to retrieve data from a remote repository.
1438 1438
1439 1439 ``repo`` is the local repository to clone into.
1440 1440 ``remote`` is a peer instance.
1441 1441 ``heads`` is an iterable of revisions we want to pull. ``None`` (the
1442 1442 default) means to pull everything from the remote.
1443 1443 ``bookmarks`` is an iterable of bookmarks requesting to be pulled. By
1444 1444 default, all remote bookmarks are pulled.
1445 1445 ``opargs`` are additional keyword arguments to pass to ``pulloperation``
1446 1446 initialization.
1447 1447 ``streamclonerequested`` is a boolean indicating whether a "streaming
1448 1448 clone" is requested. A "streaming clone" is essentially a raw file copy
1449 1449 of revlogs from the server. This only works when the local repository is
1450 1450 empty. The default value of ``None`` means to respect the server
1451 1451 configuration for preferring stream clones.
1452 1452
1453 1453 Returns the ``pulloperation`` created for this pull.
1454 1454 """
1455 1455 if opargs is None:
1456 1456 opargs = {}
1457 1457 pullop = pulloperation(repo, remote, heads, force, bookmarks=bookmarks,
1458 1458 streamclonerequested=streamclonerequested,
1459 1459 **pycompat.strkwargs(opargs))
1460 1460
1461 1461 peerlocal = pullop.remote.local()
1462 1462 if peerlocal:
1463 1463 missing = set(peerlocal.requirements) - pullop.repo.supported
1464 1464 if missing:
1465 1465 msg = _("required features are not"
1466 1466 " supported in the destination:"
1467 1467 " %s") % (', '.join(sorted(missing)))
1468 1468 raise error.Abort(msg)
1469 1469
1470 1470 pullop.trmanager = transactionmanager(repo, 'pull', remote.url())
1471 1471 with repo.wlock(), repo.lock(), pullop.trmanager:
1472 1472 # This should ideally be in _pullbundle2(). However, it needs to run
1473 1473 # before discovery to avoid extra work.
1474 1474 _maybeapplyclonebundle(pullop)
1475 1475 streamclone.maybeperformlegacystreamclone(pullop)
1476 1476 _pulldiscovery(pullop)
1477 1477 if pullop.canusebundle2:
1478 1478 _fullpullbundle2(repo, pullop)
1479 1479 _pullchangeset(pullop)
1480 1480 _pullphase(pullop)
1481 1481 _pullbookmarks(pullop)
1482 1482 _pullobsolete(pullop)
1483 1483
1484 1484 # storing remotenames
1485 1485 if repo.ui.configbool('experimental', 'remotenames'):
1486 1486 logexchange.pullremotenames(repo, remote)
1487 1487
1488 1488 return pullop
1489 1489
1490 1490 # list of steps to perform discovery before pull
1491 1491 pulldiscoveryorder = []
1492 1492
1493 1493 # Mapping between step name and function
1494 1494 #
1495 1495 # This exists to help extensions wrap steps if necessary
1496 1496 pulldiscoverymapping = {}
1497 1497
1498 1498 def pulldiscovery(stepname):
1499 1499 """decorator for function performing discovery before pull
1500 1500
1501 1501 The function is added to the step -> function mapping and appended to the
1502 1502 list of steps. Beware that decorated function will be added in order (this
1503 1503 may matter).
1504 1504
1505 1505 You can only use this decorator for a new step, if you want to wrap a step
1506 1506 from an extension, change the pulldiscovery dictionary directly."""
1507 1507 def dec(func):
1508 1508 assert stepname not in pulldiscoverymapping
1509 1509 pulldiscoverymapping[stepname] = func
1510 1510 pulldiscoveryorder.append(stepname)
1511 1511 return func
1512 1512 return dec
1513 1513
1514 1514 def _pulldiscovery(pullop):
1515 1515 """Run all discovery steps"""
1516 1516 for stepname in pulldiscoveryorder:
1517 1517 step = pulldiscoverymapping[stepname]
1518 1518 step(pullop)
1519 1519
1520 1520 @pulldiscovery('b1:bookmarks')
1521 1521 def _pullbookmarkbundle1(pullop):
1522 1522 """fetch bookmark data in bundle1 case
1523 1523
1524 1524 If not using bundle2, we have to fetch bookmarks before changeset
1525 1525 discovery to reduce the chance and impact of race conditions."""
1526 1526 if pullop.remotebookmarks is not None:
1527 1527 return
1528 1528 if pullop.canusebundle2 and 'listkeys' in pullop.remotebundle2caps:
1529 1529 # all known bundle2 servers now support listkeys, but lets be nice with
1530 1530 # new implementation.
1531 1531 return
1532 1532 books = pullop.remote.listkeys('bookmarks')
1533 1533 pullop.remotebookmarks = bookmod.unhexlifybookmarks(books)
1534 1534
1535 1535
1536 1536 @pulldiscovery('changegroup')
1537 1537 def _pulldiscoverychangegroup(pullop):
1538 1538 """discovery phase for the pull
1539 1539
1540 1540 Current handle changeset discovery only, will change handle all discovery
1541 1541 at some point."""
1542 1542 tmp = discovery.findcommonincoming(pullop.repo,
1543 1543 pullop.remote,
1544 1544 heads=pullop.heads,
1545 1545 force=pullop.force)
1546 1546 common, fetch, rheads = tmp
1547 1547 nm = pullop.repo.unfiltered().changelog.nodemap
1548 1548 if fetch and rheads:
1549 1549 # If a remote heads is filtered locally, put in back in common.
1550 1550 #
1551 1551 # This is a hackish solution to catch most of "common but locally
1552 1552 # hidden situation". We do not performs discovery on unfiltered
1553 1553 # repository because it end up doing a pathological amount of round
1554 1554 # trip for w huge amount of changeset we do not care about.
1555 1555 #
1556 1556 # If a set of such "common but filtered" changeset exist on the server
1557 1557 # but are not including a remote heads, we'll not be able to detect it,
1558 1558 scommon = set(common)
1559 1559 for n in rheads:
1560 1560 if n in nm:
1561 1561 if n not in scommon:
1562 1562 common.append(n)
1563 1563 if set(rheads).issubset(set(common)):
1564 1564 fetch = []
1565 1565 pullop.common = common
1566 1566 pullop.fetch = fetch
1567 1567 pullop.rheads = rheads
1568 1568
1569 1569 def _pullbundle2(pullop):
1570 1570 """pull data using bundle2
1571 1571
1572 1572 For now, the only supported data are changegroup."""
1573 1573 kwargs = {'bundlecaps': caps20to10(pullop.repo, role='client')}
1574 1574
1575 1575 # make ui easier to access
1576 1576 ui = pullop.repo.ui
1577 1577
1578 1578 # At the moment we don't do stream clones over bundle2. If that is
1579 1579 # implemented then here's where the check for that will go.
1580 1580 streaming = streamclone.canperformstreamclone(pullop, bundle2=True)[0]
1581 1581
1582 1582 # declare pull perimeters
1583 1583 kwargs['common'] = pullop.common
1584 1584 kwargs['heads'] = pullop.heads or pullop.rheads
1585 1585
1586 1586 if streaming:
1587 1587 kwargs['cg'] = False
1588 1588 kwargs['stream'] = True
1589 1589 pullop.stepsdone.add('changegroup')
1590 1590 pullop.stepsdone.add('phases')
1591 1591
1592 1592 else:
1593 1593 # pulling changegroup
1594 1594 pullop.stepsdone.add('changegroup')
1595 1595
1596 1596 kwargs['cg'] = pullop.fetch
1597 1597
1598 1598 legacyphase = 'phases' in ui.configlist('devel', 'legacy.exchange')
1599 1599 hasbinaryphase = 'heads' in pullop.remotebundle2caps.get('phases', ())
1600 1600 if (not legacyphase and hasbinaryphase):
1601 1601 kwargs['phases'] = True
1602 1602 pullop.stepsdone.add('phases')
1603 1603
1604 1604 if 'listkeys' in pullop.remotebundle2caps:
1605 1605 if 'phases' not in pullop.stepsdone:
1606 1606 kwargs['listkeys'] = ['phases']
1607 1607
1608 1608 bookmarksrequested = False
1609 1609 legacybookmark = 'bookmarks' in ui.configlist('devel', 'legacy.exchange')
1610 1610 hasbinarybook = 'bookmarks' in pullop.remotebundle2caps
1611 1611
1612 1612 if pullop.remotebookmarks is not None:
1613 1613 pullop.stepsdone.add('request-bookmarks')
1614 1614
1615 1615 if ('request-bookmarks' not in pullop.stepsdone
1616 1616 and pullop.remotebookmarks is None
1617 1617 and not legacybookmark and hasbinarybook):
1618 1618 kwargs['bookmarks'] = True
1619 1619 bookmarksrequested = True
1620 1620
1621 1621 if 'listkeys' in pullop.remotebundle2caps:
1622 1622 if 'request-bookmarks' not in pullop.stepsdone:
1623 1623 # make sure to always includes bookmark data when migrating
1624 1624 # `hg incoming --bundle` to using this function.
1625 1625 pullop.stepsdone.add('request-bookmarks')
1626 1626 kwargs.setdefault('listkeys', []).append('bookmarks')
1627 1627
1628 1628 # If this is a full pull / clone and the server supports the clone bundles
1629 1629 # feature, tell the server whether we attempted a clone bundle. The
1630 1630 # presence of this flag indicates the client supports clone bundles. This
1631 1631 # will enable the server to treat clients that support clone bundles
1632 1632 # differently from those that don't.
1633 1633 if (pullop.remote.capable('clonebundles')
1634 1634 and pullop.heads is None and list(pullop.common) == [nullid]):
1635 1635 kwargs['cbattempted'] = pullop.clonebundleattempted
1636 1636
1637 1637 if streaming:
1638 1638 pullop.repo.ui.status(_('streaming all changes\n'))
1639 1639 elif not pullop.fetch:
1640 1640 pullop.repo.ui.status(_("no changes found\n"))
1641 1641 pullop.cgresult = 0
1642 1642 else:
1643 1643 if pullop.heads is None and list(pullop.common) == [nullid]:
1644 1644 pullop.repo.ui.status(_("requesting all changes\n"))
1645 1645 if obsolete.isenabled(pullop.repo, obsolete.exchangeopt):
1646 1646 remoteversions = bundle2.obsmarkersversion(pullop.remotebundle2caps)
1647 1647 if obsolete.commonversion(remoteversions) is not None:
1648 1648 kwargs['obsmarkers'] = True
1649 1649 pullop.stepsdone.add('obsmarkers')
1650 1650 _pullbundle2extraprepare(pullop, kwargs)
1651 1651
1652 1652 with pullop.remote.commandexecutor() as e:
1653 1653 args = dict(kwargs)
1654 1654 args['source'] = 'pull'
1655 1655 bundle = e.callcommand('getbundle', args).result()
1656 1656
1657 1657 try:
1658 1658 op = bundle2.bundleoperation(pullop.repo, pullop.gettransaction,
1659 1659 source='pull')
1660 1660 op.modes['bookmarks'] = 'records'
1661 1661 bundle2.processbundle(pullop.repo, bundle, op=op)
1662 1662 except bundle2.AbortFromPart as exc:
1663 1663 pullop.repo.ui.status(_('remote: abort: %s\n') % exc)
1664 1664 raise error.Abort(_('pull failed on remote'), hint=exc.hint)
1665 1665 except error.BundleValueError as exc:
1666 1666 raise error.Abort(_('missing support for %s') % exc)
1667 1667
1668 1668 if pullop.fetch:
1669 1669 pullop.cgresult = bundle2.combinechangegroupresults(op)
1670 1670
1671 1671 # processing phases change
1672 1672 for namespace, value in op.records['listkeys']:
1673 1673 if namespace == 'phases':
1674 1674 _pullapplyphases(pullop, value)
1675 1675
1676 1676 # processing bookmark update
1677 1677 if bookmarksrequested:
1678 1678 books = {}
1679 1679 for record in op.records['bookmarks']:
1680 1680 books[record['bookmark']] = record["node"]
1681 1681 pullop.remotebookmarks = books
1682 1682 else:
1683 1683 for namespace, value in op.records['listkeys']:
1684 1684 if namespace == 'bookmarks':
1685 1685 pullop.remotebookmarks = bookmod.unhexlifybookmarks(value)
1686 1686
1687 1687 # bookmark data were either already there or pulled in the bundle
1688 1688 if pullop.remotebookmarks is not None:
1689 1689 _pullbookmarks(pullop)
1690 1690
1691 1691 def _pullbundle2extraprepare(pullop, kwargs):
1692 1692 """hook function so that extensions can extend the getbundle call"""
1693 1693
1694 1694 def _pullchangeset(pullop):
1695 1695 """pull changeset from unbundle into the local repo"""
1696 1696 # We delay the open of the transaction as late as possible so we
1697 1697 # don't open transaction for nothing or you break future useful
1698 1698 # rollback call
1699 1699 if 'changegroup' in pullop.stepsdone:
1700 1700 return
1701 1701 pullop.stepsdone.add('changegroup')
1702 1702 if not pullop.fetch:
1703 1703 pullop.repo.ui.status(_("no changes found\n"))
1704 1704 pullop.cgresult = 0
1705 1705 return
1706 1706 tr = pullop.gettransaction()
1707 1707 if pullop.heads is None and list(pullop.common) == [nullid]:
1708 1708 pullop.repo.ui.status(_("requesting all changes\n"))
1709 1709 elif pullop.heads is None and pullop.remote.capable('changegroupsubset'):
1710 1710 # issue1320, avoid a race if remote changed after discovery
1711 1711 pullop.heads = pullop.rheads
1712 1712
1713 1713 if pullop.remote.capable('getbundle'):
1714 1714 # TODO: get bundlecaps from remote
1715 1715 cg = pullop.remote.getbundle('pull', common=pullop.common,
1716 1716 heads=pullop.heads or pullop.rheads)
1717 1717 elif pullop.heads is None:
1718 1718 with pullop.remote.commandexecutor() as e:
1719 1719 cg = e.callcommand('changegroup', {
1720 1720 'nodes': pullop.fetch,
1721 1721 'source': 'pull',
1722 1722 }).result()
1723 1723
1724 1724 elif not pullop.remote.capable('changegroupsubset'):
1725 1725 raise error.Abort(_("partial pull cannot be done because "
1726 1726 "other repository doesn't support "
1727 1727 "changegroupsubset."))
1728 1728 else:
1729 1729 with pullop.remote.commandexecutor() as e:
1730 1730 cg = e.callcommand('changegroupsubset', {
1731 1731 'bases': pullop.fetch,
1732 1732 'heads': pullop.heads,
1733 1733 'source': 'pull',
1734 1734 }).result()
1735 1735
1736 1736 bundleop = bundle2.applybundle(pullop.repo, cg, tr, 'pull',
1737 1737 pullop.remote.url())
1738 1738 pullop.cgresult = bundle2.combinechangegroupresults(bundleop)
1739 1739
1740 1740 def _pullphase(pullop):
1741 1741 # Get remote phases data from remote
1742 1742 if 'phases' in pullop.stepsdone:
1743 1743 return
1744 1744 remotephases = pullop.remote.listkeys('phases')
1745 1745 _pullapplyphases(pullop, remotephases)
1746 1746
1747 1747 def _pullapplyphases(pullop, remotephases):
1748 1748 """apply phase movement from observed remote state"""
1749 1749 if 'phases' in pullop.stepsdone:
1750 1750 return
1751 1751 pullop.stepsdone.add('phases')
1752 1752 publishing = bool(remotephases.get('publishing', False))
1753 1753 if remotephases and not publishing:
1754 1754 # remote is new and non-publishing
1755 1755 pheads, _dr = phases.analyzeremotephases(pullop.repo,
1756 1756 pullop.pulledsubset,
1757 1757 remotephases)
1758 1758 dheads = pullop.pulledsubset
1759 1759 else:
1760 1760 # Remote is old or publishing all common changesets
1761 1761 # should be seen as public
1762 1762 pheads = pullop.pulledsubset
1763 1763 dheads = []
1764 1764 unfi = pullop.repo.unfiltered()
1765 1765 phase = unfi._phasecache.phase
1766 1766 rev = unfi.changelog.nodemap.get
1767 1767 public = phases.public
1768 1768 draft = phases.draft
1769 1769
1770 1770 # exclude changesets already public locally and update the others
1771 1771 pheads = [pn for pn in pheads if phase(unfi, rev(pn)) > public]
1772 1772 if pheads:
1773 1773 tr = pullop.gettransaction()
1774 1774 phases.advanceboundary(pullop.repo, tr, public, pheads)
1775 1775
1776 1776 # exclude changesets already draft locally and update the others
1777 1777 dheads = [pn for pn in dheads if phase(unfi, rev(pn)) > draft]
1778 1778 if dheads:
1779 1779 tr = pullop.gettransaction()
1780 1780 phases.advanceboundary(pullop.repo, tr, draft, dheads)
1781 1781
1782 1782 def _pullbookmarks(pullop):
1783 1783 """process the remote bookmark information to update the local one"""
1784 1784 if 'bookmarks' in pullop.stepsdone:
1785 1785 return
1786 1786 pullop.stepsdone.add('bookmarks')
1787 1787 repo = pullop.repo
1788 1788 remotebookmarks = pullop.remotebookmarks
1789 1789 bookmod.updatefromremote(repo.ui, repo, remotebookmarks,
1790 1790 pullop.remote.url(),
1791 1791 pullop.gettransaction,
1792 1792 explicit=pullop.explicitbookmarks)
1793 1793
1794 1794 def _pullobsolete(pullop):
1795 1795 """utility function to pull obsolete markers from a remote
1796 1796
1797 1797 The `gettransaction` is function that return the pull transaction, creating
1798 1798 one if necessary. We return the transaction to inform the calling code that
1799 1799 a new transaction have been created (when applicable).
1800 1800
1801 1801 Exists mostly to allow overriding for experimentation purpose"""
1802 1802 if 'obsmarkers' in pullop.stepsdone:
1803 1803 return
1804 1804 pullop.stepsdone.add('obsmarkers')
1805 1805 tr = None
1806 1806 if obsolete.isenabled(pullop.repo, obsolete.exchangeopt):
1807 1807 pullop.repo.ui.debug('fetching remote obsolete markers\n')
1808 1808 remoteobs = pullop.remote.listkeys('obsolete')
1809 1809 if 'dump0' in remoteobs:
1810 1810 tr = pullop.gettransaction()
1811 1811 markers = []
1812 1812 for key in sorted(remoteobs, reverse=True):
1813 1813 if key.startswith('dump'):
1814 1814 data = util.b85decode(remoteobs[key])
1815 1815 version, newmarks = obsolete._readmarkers(data)
1816 1816 markers += newmarks
1817 1817 if markers:
1818 1818 pullop.repo.obsstore.add(tr, markers)
1819 1819 pullop.repo.invalidatevolatilesets()
1820 1820 return tr
1821 1821
1822 1822 def caps20to10(repo, role):
1823 1823 """return a set with appropriate options to use bundle20 during getbundle"""
1824 1824 caps = {'HG20'}
1825 1825 capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo, role=role))
1826 1826 caps.add('bundle2=' + urlreq.quote(capsblob))
1827 1827 return caps
1828 1828
1829 1829 # List of names of steps to perform for a bundle2 for getbundle, order matters.
1830 1830 getbundle2partsorder = []
1831 1831
1832 1832 # Mapping between step name and function
1833 1833 #
1834 1834 # This exists to help extensions wrap steps if necessary
1835 1835 getbundle2partsmapping = {}
1836 1836
1837 1837 def getbundle2partsgenerator(stepname, idx=None):
1838 1838 """decorator for function generating bundle2 part for getbundle
1839 1839
1840 1840 The function is added to the step -> function mapping and appended to the
1841 1841 list of steps. Beware that decorated functions will be added in order
1842 1842 (this may matter).
1843 1843
1844 1844 You can only use this decorator for new steps, if you want to wrap a step
1845 1845 from an extension, attack the getbundle2partsmapping dictionary directly."""
1846 1846 def dec(func):
1847 1847 assert stepname not in getbundle2partsmapping
1848 1848 getbundle2partsmapping[stepname] = func
1849 1849 if idx is None:
1850 1850 getbundle2partsorder.append(stepname)
1851 1851 else:
1852 1852 getbundle2partsorder.insert(idx, stepname)
1853 1853 return func
1854 1854 return dec
1855 1855
1856 1856 def bundle2requested(bundlecaps):
1857 1857 if bundlecaps is not None:
1858 1858 return any(cap.startswith('HG2') for cap in bundlecaps)
1859 1859 return False
1860 1860
1861 1861 def getbundlechunks(repo, source, heads=None, common=None, bundlecaps=None,
1862 1862 **kwargs):
1863 1863 """Return chunks constituting a bundle's raw data.
1864 1864
1865 1865 Could be a bundle HG10 or a bundle HG20 depending on bundlecaps
1866 1866 passed.
1867 1867
1868 1868 Returns a 2-tuple of a dict with metadata about the generated bundle
1869 1869 and an iterator over raw chunks (of varying sizes).
1870 1870 """
1871 1871 kwargs = pycompat.byteskwargs(kwargs)
1872 1872 info = {}
1873 1873 usebundle2 = bundle2requested(bundlecaps)
1874 1874 # bundle10 case
1875 1875 if not usebundle2:
1876 1876 if bundlecaps and not kwargs.get('cg', True):
1877 1877 raise ValueError(_('request for bundle10 must include changegroup'))
1878 1878
1879 1879 if kwargs:
1880 1880 raise ValueError(_('unsupported getbundle arguments: %s')
1881 1881 % ', '.join(sorted(kwargs.keys())))
1882 1882 outgoing = _computeoutgoing(repo, heads, common)
1883 1883 info['bundleversion'] = 1
1884 1884 return info, changegroup.makestream(repo, outgoing, '01', source,
1885 1885 bundlecaps=bundlecaps)
1886 1886
1887 1887 # bundle20 case
1888 1888 info['bundleversion'] = 2
1889 1889 b2caps = {}
1890 1890 for bcaps in bundlecaps:
1891 1891 if bcaps.startswith('bundle2='):
1892 1892 blob = urlreq.unquote(bcaps[len('bundle2='):])
1893 1893 b2caps.update(bundle2.decodecaps(blob))
1894 1894 bundler = bundle2.bundle20(repo.ui, b2caps)
1895 1895
1896 1896 kwargs['heads'] = heads
1897 1897 kwargs['common'] = common
1898 1898
1899 1899 for name in getbundle2partsorder:
1900 1900 func = getbundle2partsmapping[name]
1901 1901 func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps,
1902 1902 **pycompat.strkwargs(kwargs))
1903 1903
1904 1904 info['prefercompressed'] = bundler.prefercompressed
1905 1905
1906 1906 return info, bundler.getchunks()
1907 1907
1908 1908 @getbundle2partsgenerator('stream2')
1909 1909 def _getbundlestream2(bundler, repo, *args, **kwargs):
1910 1910 return bundle2.addpartbundlestream2(bundler, repo, **kwargs)
1911 1911
1912 1912 @getbundle2partsgenerator('changegroup')
1913 1913 def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None,
1914 1914 b2caps=None, heads=None, common=None, **kwargs):
1915 1915 """add a changegroup part to the requested bundle"""
1916 1916 cgstream = None
1917 1917 if kwargs.get(r'cg', True):
1918 1918 # build changegroup bundle here.
1919 1919 version = '01'
1920 1920 cgversions = b2caps.get('changegroup')
1921 1921 if cgversions: # 3.1 and 3.2 ship with an empty value
1922 1922 cgversions = [v for v in cgversions
1923 1923 if v in changegroup.supportedoutgoingversions(repo)]
1924 1924 if not cgversions:
1925 1925 raise ValueError(_('no common changegroup version'))
1926 1926 version = max(cgversions)
1927 1927 outgoing = _computeoutgoing(repo, heads, common)
1928 1928 if outgoing.missing:
1929 1929 cgstream = changegroup.makestream(repo, outgoing, version, source,
1930 1930 bundlecaps=bundlecaps)
1931 1931
1932 1932 if cgstream:
1933 1933 part = bundler.newpart('changegroup', data=cgstream)
1934 1934 if cgversions:
1935 1935 part.addparam('version', version)
1936 1936 part.addparam('nbchanges', '%d' % len(outgoing.missing),
1937 1937 mandatory=False)
1938 1938 if 'treemanifest' in repo.requirements:
1939 1939 part.addparam('treemanifest', '1')
1940 1940
1941 1941 @getbundle2partsgenerator('bookmarks')
1942 1942 def _getbundlebookmarkpart(bundler, repo, source, bundlecaps=None,
1943 1943 b2caps=None, **kwargs):
1944 1944 """add a bookmark part to the requested bundle"""
1945 1945 if not kwargs.get(r'bookmarks', False):
1946 1946 return
1947 1947 if 'bookmarks' not in b2caps:
1948 1948 raise ValueError(_('no common bookmarks exchange method'))
1949 1949 books = bookmod.listbinbookmarks(repo)
1950 1950 data = bookmod.binaryencode(books)
1951 1951 if data:
1952 1952 bundler.newpart('bookmarks', data=data)
1953 1953
1954 1954 @getbundle2partsgenerator('listkeys')
1955 1955 def _getbundlelistkeysparts(bundler, repo, source, bundlecaps=None,
1956 1956 b2caps=None, **kwargs):
1957 1957 """add parts containing listkeys namespaces to the requested bundle"""
1958 1958 listkeys = kwargs.get(r'listkeys', ())
1959 1959 for namespace in listkeys:
1960 1960 part = bundler.newpart('listkeys')
1961 1961 part.addparam('namespace', namespace)
1962 1962 keys = repo.listkeys(namespace).items()
1963 1963 part.data = pushkey.encodekeys(keys)
1964 1964
1965 1965 @getbundle2partsgenerator('obsmarkers')
1966 1966 def _getbundleobsmarkerpart(bundler, repo, source, bundlecaps=None,
1967 1967 b2caps=None, heads=None, **kwargs):
1968 1968 """add an obsolescence markers part to the requested bundle"""
1969 1969 if kwargs.get(r'obsmarkers', False):
1970 1970 if heads is None:
1971 1971 heads = repo.heads()
1972 1972 subset = [c.node() for c in repo.set('::%ln', heads)]
1973 1973 markers = repo.obsstore.relevantmarkers(subset)
1974 1974 markers = sorted(markers)
1975 1975 bundle2.buildobsmarkerspart(bundler, markers)
1976 1976
1977 1977 @getbundle2partsgenerator('phases')
1978 1978 def _getbundlephasespart(bundler, repo, source, bundlecaps=None,
1979 1979 b2caps=None, heads=None, **kwargs):
1980 1980 """add phase heads part to the requested bundle"""
1981 1981 if kwargs.get(r'phases', False):
1982 1982 if not 'heads' in b2caps.get('phases'):
1983 1983 raise ValueError(_('no common phases exchange method'))
1984 1984 if heads is None:
1985 1985 heads = repo.heads()
1986 1986
1987 1987 headsbyphase = collections.defaultdict(set)
1988 1988 if repo.publishing():
1989 1989 headsbyphase[phases.public] = heads
1990 1990 else:
1991 1991 # find the appropriate heads to move
1992 1992
1993 1993 phase = repo._phasecache.phase
1994 1994 node = repo.changelog.node
1995 1995 rev = repo.changelog.rev
1996 1996 for h in heads:
1997 1997 headsbyphase[phase(repo, rev(h))].add(h)
1998 1998 seenphases = list(headsbyphase.keys())
1999 1999
2000 2000 # We do not handle anything but public and draft phase for now)
2001 2001 if seenphases:
2002 2002 assert max(seenphases) <= phases.draft
2003 2003
2004 2004 # if client is pulling non-public changesets, we need to find
2005 2005 # intermediate public heads.
2006 2006 draftheads = headsbyphase.get(phases.draft, set())
2007 2007 if draftheads:
2008 2008 publicheads = headsbyphase.get(phases.public, set())
2009 2009
2010 2010 revset = 'heads(only(%ln, %ln) and public())'
2011 2011 extraheads = repo.revs(revset, draftheads, publicheads)
2012 2012 for r in extraheads:
2013 2013 headsbyphase[phases.public].add(node(r))
2014 2014
2015 2015 # transform data in a format used by the encoding function
2016 2016 phasemapping = []
2017 2017 for phase in phases.allphases:
2018 2018 phasemapping.append(sorted(headsbyphase[phase]))
2019 2019
2020 2020 # generate the actual part
2021 2021 phasedata = phases.binaryencode(phasemapping)
2022 2022 bundler.newpart('phase-heads', data=phasedata)
2023 2023
2024 2024 @getbundle2partsgenerator('hgtagsfnodes')
2025 2025 def _getbundletagsfnodes(bundler, repo, source, bundlecaps=None,
2026 2026 b2caps=None, heads=None, common=None,
2027 2027 **kwargs):
2028 2028 """Transfer the .hgtags filenodes mapping.
2029 2029
2030 2030 Only values for heads in this bundle will be transferred.
2031 2031
2032 2032 The part data consists of pairs of 20 byte changeset node and .hgtags
2033 2033 filenodes raw values.
2034 2034 """
2035 2035 # Don't send unless:
2036 2036 # - changeset are being exchanged,
2037 2037 # - the client supports it.
2038 2038 if not (kwargs.get(r'cg', True) and 'hgtagsfnodes' in b2caps):
2039 2039 return
2040 2040
2041 2041 outgoing = _computeoutgoing(repo, heads, common)
2042 2042 bundle2.addparttagsfnodescache(repo, bundler, outgoing)
2043 2043
2044 2044 @getbundle2partsgenerator('cache:rev-branch-cache')
2045 2045 def _getbundlerevbranchcache(bundler, repo, source, bundlecaps=None,
2046 2046 b2caps=None, heads=None, common=None,
2047 2047 **kwargs):
2048 2048 """Transfer the rev-branch-cache mapping
2049 2049
2050 2050 The payload is a series of data related to each branch
2051 2051
2052 2052 1) branch name length
2053 2053 2) number of open heads
2054 2054 3) number of closed heads
2055 2055 4) open heads nodes
2056 2056 5) closed heads nodes
2057 2057 """
2058 2058 # Don't send unless:
2059 2059 # - changeset are being exchanged,
2060 2060 # - the client supports it.
2061 2061 if not (kwargs.get(r'cg', True)) or 'rev-branch-cache' not in b2caps:
2062 2062 return
2063 2063 outgoing = _computeoutgoing(repo, heads, common)
2064 2064 bundle2.addpartrevbranchcache(repo, bundler, outgoing)
2065 2065
2066 2066 def check_heads(repo, their_heads, context):
2067 2067 """check if the heads of a repo have been modified
2068 2068
2069 2069 Used by peer for unbundling.
2070 2070 """
2071 2071 heads = repo.heads()
2072 2072 heads_hash = hashlib.sha1(''.join(sorted(heads))).digest()
2073 2073 if not (their_heads == ['force'] or their_heads == heads or
2074 2074 their_heads == ['hashed', heads_hash]):
2075 2075 # someone else committed/pushed/unbundled while we
2076 2076 # were transferring data
2077 2077 raise error.PushRaced('repository changed while %s - '
2078 2078 'please try again' % context)
2079 2079
2080 2080 def unbundle(repo, cg, heads, source, url):
2081 2081 """Apply a bundle to a repo.
2082 2082
2083 2083 this function makes sure the repo is locked during the application and have
2084 2084 mechanism to check that no push race occurred between the creation of the
2085 2085 bundle and its application.
2086 2086
2087 2087 If the push was raced as PushRaced exception is raised."""
2088 2088 r = 0
2089 2089 # need a transaction when processing a bundle2 stream
2090 2090 # [wlock, lock, tr] - needs to be an array so nested functions can modify it
2091 2091 lockandtr = [None, None, None]
2092 2092 recordout = None
2093 2093 # quick fix for output mismatch with bundle2 in 3.4
2094 2094 captureoutput = repo.ui.configbool('experimental', 'bundle2-output-capture')
2095 2095 if url.startswith('remote:http:') or url.startswith('remote:https:'):
2096 2096 captureoutput = True
2097 2097 try:
2098 2098 # note: outside bundle1, 'heads' is expected to be empty and this
2099 2099 # 'check_heads' call wil be a no-op
2100 2100 check_heads(repo, heads, 'uploading changes')
2101 2101 # push can proceed
2102 2102 if not isinstance(cg, bundle2.unbundle20):
2103 2103 # legacy case: bundle1 (changegroup 01)
2104 2104 txnname = "\n".join([source, util.hidepassword(url)])
2105 2105 with repo.lock(), repo.transaction(txnname) as tr:
2106 2106 op = bundle2.applybundle(repo, cg, tr, source, url)
2107 2107 r = bundle2.combinechangegroupresults(op)
2108 2108 else:
2109 2109 r = None
2110 2110 try:
2111 2111 def gettransaction():
2112 2112 if not lockandtr[2]:
2113 2113 lockandtr[0] = repo.wlock()
2114 2114 lockandtr[1] = repo.lock()
2115 2115 lockandtr[2] = repo.transaction(source)
2116 2116 lockandtr[2].hookargs['source'] = source
2117 2117 lockandtr[2].hookargs['url'] = url
2118 2118 lockandtr[2].hookargs['bundle2'] = '1'
2119 2119 return lockandtr[2]
2120 2120
2121 2121 # Do greedy locking by default until we're satisfied with lazy
2122 2122 # locking.
2123 2123 if not repo.ui.configbool('experimental', 'bundle2lazylocking'):
2124 2124 gettransaction()
2125 2125
2126 2126 op = bundle2.bundleoperation(repo, gettransaction,
2127 2127 captureoutput=captureoutput,
2128 2128 source='push')
2129 2129 try:
2130 2130 op = bundle2.processbundle(repo, cg, op=op)
2131 2131 finally:
2132 2132 r = op.reply
2133 2133 if captureoutput and r is not None:
2134 2134 repo.ui.pushbuffer(error=True, subproc=True)
2135 2135 def recordout(output):
2136 2136 r.newpart('output', data=output, mandatory=False)
2137 2137 if lockandtr[2] is not None:
2138 2138 lockandtr[2].close()
2139 2139 except BaseException as exc:
2140 2140 exc.duringunbundle2 = True
2141 2141 if captureoutput and r is not None:
2142 2142 parts = exc._bundle2salvagedoutput = r.salvageoutput()
2143 2143 def recordout(output):
2144 2144 part = bundle2.bundlepart('output', data=output,
2145 2145 mandatory=False)
2146 2146 parts.append(part)
2147 2147 raise
2148 2148 finally:
2149 2149 lockmod.release(lockandtr[2], lockandtr[1], lockandtr[0])
2150 2150 if recordout is not None:
2151 2151 recordout(repo.ui.popbuffer())
2152 2152 return r
2153 2153
2154 2154 def _maybeapplyclonebundle(pullop):
2155 2155 """Apply a clone bundle from a remote, if possible."""
2156 2156
2157 2157 repo = pullop.repo
2158 2158 remote = pullop.remote
2159 2159
2160 2160 if not repo.ui.configbool('ui', 'clonebundles'):
2161 2161 return
2162 2162
2163 2163 # Only run if local repo is empty.
2164 2164 if len(repo):
2165 2165 return
2166 2166
2167 2167 if pullop.heads:
2168 2168 return
2169 2169
2170 2170 if not remote.capable('clonebundles'):
2171 2171 return
2172 2172
2173 res = remote._call('clonebundles')
2173 with remote.commandexecutor() as e:
2174 res = e.callcommand('clonebundles', {}).result()
2174 2175
2175 2176 # If we call the wire protocol command, that's good enough to record the
2176 2177 # attempt.
2177 2178 pullop.clonebundleattempted = True
2178 2179
2179 2180 entries = parseclonebundlesmanifest(repo, res)
2180 2181 if not entries:
2181 2182 repo.ui.note(_('no clone bundles available on remote; '
2182 2183 'falling back to regular clone\n'))
2183 2184 return
2184 2185
2185 2186 entries = filterclonebundleentries(
2186 2187 repo, entries, streamclonerequested=pullop.streamclonerequested)
2187 2188
2188 2189 if not entries:
2189 2190 # There is a thundering herd concern here. However, if a server
2190 2191 # operator doesn't advertise bundles appropriate for its clients,
2191 2192 # they deserve what's coming. Furthermore, from a client's
2192 2193 # perspective, no automatic fallback would mean not being able to
2193 2194 # clone!
2194 2195 repo.ui.warn(_('no compatible clone bundles available on server; '
2195 2196 'falling back to regular clone\n'))
2196 2197 repo.ui.warn(_('(you may want to report this to the server '
2197 2198 'operator)\n'))
2198 2199 return
2199 2200
2200 2201 entries = sortclonebundleentries(repo.ui, entries)
2201 2202
2202 2203 url = entries[0]['URL']
2203 2204 repo.ui.status(_('applying clone bundle from %s\n') % url)
2204 2205 if trypullbundlefromurl(repo.ui, repo, url):
2205 2206 repo.ui.status(_('finished applying clone bundle\n'))
2206 2207 # Bundle failed.
2207 2208 #
2208 2209 # We abort by default to avoid the thundering herd of
2209 2210 # clients flooding a server that was expecting expensive
2210 2211 # clone load to be offloaded.
2211 2212 elif repo.ui.configbool('ui', 'clonebundlefallback'):
2212 2213 repo.ui.warn(_('falling back to normal clone\n'))
2213 2214 else:
2214 2215 raise error.Abort(_('error applying bundle'),
2215 2216 hint=_('if this error persists, consider contacting '
2216 2217 'the server operator or disable clone '
2217 2218 'bundles via '
2218 2219 '"--config ui.clonebundles=false"'))
2219 2220
2220 2221 def parseclonebundlesmanifest(repo, s):
2221 2222 """Parses the raw text of a clone bundles manifest.
2222 2223
2223 2224 Returns a list of dicts. The dicts have a ``URL`` key corresponding
2224 2225 to the URL and other keys are the attributes for the entry.
2225 2226 """
2226 2227 m = []
2227 2228 for line in s.splitlines():
2228 2229 fields = line.split()
2229 2230 if not fields:
2230 2231 continue
2231 2232 attrs = {'URL': fields[0]}
2232 2233 for rawattr in fields[1:]:
2233 2234 key, value = rawattr.split('=', 1)
2234 2235 key = urlreq.unquote(key)
2235 2236 value = urlreq.unquote(value)
2236 2237 attrs[key] = value
2237 2238
2238 2239 # Parse BUNDLESPEC into components. This makes client-side
2239 2240 # preferences easier to specify since you can prefer a single
2240 2241 # component of the BUNDLESPEC.
2241 2242 if key == 'BUNDLESPEC':
2242 2243 try:
2243 2244 bundlespec = parsebundlespec(repo, value,
2244 2245 externalnames=True)
2245 2246 attrs['COMPRESSION'] = bundlespec.compression
2246 2247 attrs['VERSION'] = bundlespec.version
2247 2248 except error.InvalidBundleSpecification:
2248 2249 pass
2249 2250 except error.UnsupportedBundleSpecification:
2250 2251 pass
2251 2252
2252 2253 m.append(attrs)
2253 2254
2254 2255 return m
2255 2256
2256 2257 def isstreamclonespec(bundlespec):
2257 2258 # Stream clone v1
2258 2259 if (bundlespec.compression == 'UN' and bundlespec.version == 's1'):
2259 2260 return True
2260 2261
2261 2262 # Stream clone v2
2262 2263 if (bundlespec.compression == 'UN' and bundlespec.version == '02' and \
2263 2264 bundlespec.contentopts.get('streamv2')):
2264 2265 return True
2265 2266
2266 2267 return False
2267 2268
2268 2269 def filterclonebundleentries(repo, entries, streamclonerequested=False):
2269 2270 """Remove incompatible clone bundle manifest entries.
2270 2271
2271 2272 Accepts a list of entries parsed with ``parseclonebundlesmanifest``
2272 2273 and returns a new list consisting of only the entries that this client
2273 2274 should be able to apply.
2274 2275
2275 2276 There is no guarantee we'll be able to apply all returned entries because
2276 2277 the metadata we use to filter on may be missing or wrong.
2277 2278 """
2278 2279 newentries = []
2279 2280 for entry in entries:
2280 2281 spec = entry.get('BUNDLESPEC')
2281 2282 if spec:
2282 2283 try:
2283 2284 bundlespec = parsebundlespec(repo, spec, strict=True)
2284 2285
2285 2286 # If a stream clone was requested, filter out non-streamclone
2286 2287 # entries.
2287 2288 if streamclonerequested and not isstreamclonespec(bundlespec):
2288 2289 repo.ui.debug('filtering %s because not a stream clone\n' %
2289 2290 entry['URL'])
2290 2291 continue
2291 2292
2292 2293 except error.InvalidBundleSpecification as e:
2293 2294 repo.ui.debug(str(e) + '\n')
2294 2295 continue
2295 2296 except error.UnsupportedBundleSpecification as e:
2296 2297 repo.ui.debug('filtering %s because unsupported bundle '
2297 2298 'spec: %s\n' % (
2298 2299 entry['URL'], stringutil.forcebytestr(e)))
2299 2300 continue
2300 2301 # If we don't have a spec and requested a stream clone, we don't know
2301 2302 # what the entry is so don't attempt to apply it.
2302 2303 elif streamclonerequested:
2303 2304 repo.ui.debug('filtering %s because cannot determine if a stream '
2304 2305 'clone bundle\n' % entry['URL'])
2305 2306 continue
2306 2307
2307 2308 if 'REQUIRESNI' in entry and not sslutil.hassni:
2308 2309 repo.ui.debug('filtering %s because SNI not supported\n' %
2309 2310 entry['URL'])
2310 2311 continue
2311 2312
2312 2313 newentries.append(entry)
2313 2314
2314 2315 return newentries
2315 2316
2316 2317 class clonebundleentry(object):
2317 2318 """Represents an item in a clone bundles manifest.
2318 2319
2319 2320 This rich class is needed to support sorting since sorted() in Python 3
2320 2321 doesn't support ``cmp`` and our comparison is complex enough that ``key=``
2321 2322 won't work.
2322 2323 """
2323 2324
2324 2325 def __init__(self, value, prefers):
2325 2326 self.value = value
2326 2327 self.prefers = prefers
2327 2328
2328 2329 def _cmp(self, other):
2329 2330 for prefkey, prefvalue in self.prefers:
2330 2331 avalue = self.value.get(prefkey)
2331 2332 bvalue = other.value.get(prefkey)
2332 2333
2333 2334 # Special case for b missing attribute and a matches exactly.
2334 2335 if avalue is not None and bvalue is None and avalue == prefvalue:
2335 2336 return -1
2336 2337
2337 2338 # Special case for a missing attribute and b matches exactly.
2338 2339 if bvalue is not None and avalue is None and bvalue == prefvalue:
2339 2340 return 1
2340 2341
2341 2342 # We can't compare unless attribute present on both.
2342 2343 if avalue is None or bvalue is None:
2343 2344 continue
2344 2345
2345 2346 # Same values should fall back to next attribute.
2346 2347 if avalue == bvalue:
2347 2348 continue
2348 2349
2349 2350 # Exact matches come first.
2350 2351 if avalue == prefvalue:
2351 2352 return -1
2352 2353 if bvalue == prefvalue:
2353 2354 return 1
2354 2355
2355 2356 # Fall back to next attribute.
2356 2357 continue
2357 2358
2358 2359 # If we got here we couldn't sort by attributes and prefers. Fall
2359 2360 # back to index order.
2360 2361 return 0
2361 2362
2362 2363 def __lt__(self, other):
2363 2364 return self._cmp(other) < 0
2364 2365
2365 2366 def __gt__(self, other):
2366 2367 return self._cmp(other) > 0
2367 2368
2368 2369 def __eq__(self, other):
2369 2370 return self._cmp(other) == 0
2370 2371
2371 2372 def __le__(self, other):
2372 2373 return self._cmp(other) <= 0
2373 2374
2374 2375 def __ge__(self, other):
2375 2376 return self._cmp(other) >= 0
2376 2377
2377 2378 def __ne__(self, other):
2378 2379 return self._cmp(other) != 0
2379 2380
2380 2381 def sortclonebundleentries(ui, entries):
2381 2382 prefers = ui.configlist('ui', 'clonebundleprefers')
2382 2383 if not prefers:
2383 2384 return list(entries)
2384 2385
2385 2386 prefers = [p.split('=', 1) for p in prefers]
2386 2387
2387 2388 items = sorted(clonebundleentry(v, prefers) for v in entries)
2388 2389 return [i.value for i in items]
2389 2390
2390 2391 def trypullbundlefromurl(ui, repo, url):
2391 2392 """Attempt to apply a bundle from a URL."""
2392 2393 with repo.lock(), repo.transaction('bundleurl') as tr:
2393 2394 try:
2394 2395 fh = urlmod.open(ui, url)
2395 2396 cg = readbundle(ui, fh, 'stream')
2396 2397
2397 2398 if isinstance(cg, streamclone.streamcloneapplier):
2398 2399 cg.apply(repo)
2399 2400 else:
2400 2401 bundle2.applybundle(repo, cg, tr, 'clonebundles', url)
2401 2402 return True
2402 2403 except urlerr.httperror as e:
2403 2404 ui.warn(_('HTTP error fetching bundle: %s\n') %
2404 2405 stringutil.forcebytestr(e))
2405 2406 except urlerr.urlerror as e:
2406 2407 ui.warn(_('error fetching bundle: %s\n') %
2407 2408 stringutil.forcebytestr(e.reason))
2408 2409
2409 2410 return False
@@ -1,2375 +1,2378 b''
1 1 # localrepo.py - read/write repository class 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 errno
11 11 import hashlib
12 12 import os
13 13 import random
14 14 import sys
15 15 import time
16 16 import weakref
17 17
18 18 from .i18n import _
19 19 from .node import (
20 20 hex,
21 21 nullid,
22 22 short,
23 23 )
24 24 from .thirdparty.zope import (
25 25 interface as zi,
26 26 )
27 27 from . import (
28 28 bookmarks,
29 29 branchmap,
30 30 bundle2,
31 31 changegroup,
32 32 changelog,
33 33 color,
34 34 context,
35 35 dirstate,
36 36 dirstateguard,
37 37 discovery,
38 38 encoding,
39 39 error,
40 40 exchange,
41 41 extensions,
42 42 filelog,
43 43 hook,
44 44 lock as lockmod,
45 45 manifest,
46 46 match as matchmod,
47 47 merge as mergemod,
48 48 mergeutil,
49 49 namespaces,
50 50 narrowspec,
51 51 obsolete,
52 52 pathutil,
53 53 phases,
54 54 pushkey,
55 55 pycompat,
56 56 repository,
57 57 repoview,
58 58 revset,
59 59 revsetlang,
60 60 scmutil,
61 61 sparse,
62 62 store,
63 63 subrepoutil,
64 64 tags as tagsmod,
65 65 transaction,
66 66 txnutil,
67 67 util,
68 68 vfs as vfsmod,
69 69 )
70 70 from .utils import (
71 71 procutil,
72 72 stringutil,
73 73 )
74 74
75 75 release = lockmod.release
76 76 urlerr = util.urlerr
77 77 urlreq = util.urlreq
78 78
79 79 # set of (path, vfs-location) tuples. vfs-location is:
80 80 # - 'plain for vfs relative paths
81 81 # - '' for svfs relative paths
82 82 _cachedfiles = set()
83 83
84 84 class _basefilecache(scmutil.filecache):
85 85 """All filecache usage on repo are done for logic that should be unfiltered
86 86 """
87 87 def __get__(self, repo, type=None):
88 88 if repo is None:
89 89 return self
90 90 return super(_basefilecache, self).__get__(repo.unfiltered(), type)
91 91 def __set__(self, repo, value):
92 92 return super(_basefilecache, self).__set__(repo.unfiltered(), value)
93 93 def __delete__(self, repo):
94 94 return super(_basefilecache, self).__delete__(repo.unfiltered())
95 95
96 96 class repofilecache(_basefilecache):
97 97 """filecache for files in .hg but outside of .hg/store"""
98 98 def __init__(self, *paths):
99 99 super(repofilecache, self).__init__(*paths)
100 100 for path in paths:
101 101 _cachedfiles.add((path, 'plain'))
102 102
103 103 def join(self, obj, fname):
104 104 return obj.vfs.join(fname)
105 105
106 106 class storecache(_basefilecache):
107 107 """filecache for files in the store"""
108 108 def __init__(self, *paths):
109 109 super(storecache, self).__init__(*paths)
110 110 for path in paths:
111 111 _cachedfiles.add((path, ''))
112 112
113 113 def join(self, obj, fname):
114 114 return obj.sjoin(fname)
115 115
116 116 def isfilecached(repo, name):
117 117 """check if a repo has already cached "name" filecache-ed property
118 118
119 119 This returns (cachedobj-or-None, iscached) tuple.
120 120 """
121 121 cacheentry = repo.unfiltered()._filecache.get(name, None)
122 122 if not cacheentry:
123 123 return None, False
124 124 return cacheentry.obj, True
125 125
126 126 class unfilteredpropertycache(util.propertycache):
127 127 """propertycache that apply to unfiltered repo only"""
128 128
129 129 def __get__(self, repo, type=None):
130 130 unfi = repo.unfiltered()
131 131 if unfi is repo:
132 132 return super(unfilteredpropertycache, self).__get__(unfi)
133 133 return getattr(unfi, self.name)
134 134
135 135 class filteredpropertycache(util.propertycache):
136 136 """propertycache that must take filtering in account"""
137 137
138 138 def cachevalue(self, obj, value):
139 139 object.__setattr__(obj, self.name, value)
140 140
141 141
142 142 def hasunfilteredcache(repo, name):
143 143 """check if a repo has an unfilteredpropertycache value for <name>"""
144 144 return name in vars(repo.unfiltered())
145 145
146 146 def unfilteredmethod(orig):
147 147 """decorate method that always need to be run on unfiltered version"""
148 148 def wrapper(repo, *args, **kwargs):
149 149 return orig(repo.unfiltered(), *args, **kwargs)
150 150 return wrapper
151 151
152 152 moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle',
153 153 'unbundle'}
154 154 legacycaps = moderncaps.union({'changegroupsubset'})
155 155
156 156 @zi.implementer(repository.ipeercommandexecutor)
157 157 class localcommandexecutor(object):
158 158 def __init__(self, peer):
159 159 self._peer = peer
160 160 self._sent = False
161 161 self._closed = False
162 162
163 163 def __enter__(self):
164 164 return self
165 165
166 166 def __exit__(self, exctype, excvalue, exctb):
167 167 self.close()
168 168
169 169 def callcommand(self, command, args):
170 170 if self._sent:
171 171 raise error.ProgrammingError('callcommand() cannot be used after '
172 172 'sendcommands()')
173 173
174 174 if self._closed:
175 175 raise error.ProgrammingError('callcommand() cannot be used after '
176 176 'close()')
177 177
178 178 # We don't need to support anything fancy. Just call the named
179 179 # method on the peer and return a resolved future.
180 180 fn = getattr(self._peer, pycompat.sysstr(command))
181 181
182 182 f = pycompat.futures.Future()
183 183
184 184 try:
185 185 result = fn(**args)
186 186 except Exception:
187 187 f.set_exception_info(*sys.exc_info()[1:])
188 188 else:
189 189 f.set_result(result)
190 190
191 191 return f
192 192
193 193 def sendcommands(self):
194 194 self._sent = True
195 195
196 196 def close(self):
197 197 self._closed = True
198 198
199 199 class localpeer(repository.peer):
200 200 '''peer for a local repo; reflects only the most recent API'''
201 201
202 202 def __init__(self, repo, caps=None):
203 203 super(localpeer, self).__init__()
204 204
205 205 if caps is None:
206 206 caps = moderncaps.copy()
207 207 self._repo = repo.filtered('served')
208 208 self.ui = repo.ui
209 209 self._caps = repo._restrictcapabilities(caps)
210 210
211 211 # Begin of _basepeer interface.
212 212
213 213 def url(self):
214 214 return self._repo.url()
215 215
216 216 def local(self):
217 217 return self._repo
218 218
219 219 def peer(self):
220 220 return self
221 221
222 222 def canpush(self):
223 223 return True
224 224
225 225 def close(self):
226 226 self._repo.close()
227 227
228 228 # End of _basepeer interface.
229 229
230 230 # Begin of _basewirecommands interface.
231 231
232 232 def branchmap(self):
233 233 return self._repo.branchmap()
234 234
235 235 def capabilities(self):
236 236 return self._caps
237 237
238 def clonebundles(self):
239 return self._repo.tryread('clonebundles.manifest')
240
238 241 def debugwireargs(self, one, two, three=None, four=None, five=None):
239 242 """Used to test argument passing over the wire"""
240 243 return "%s %s %s %s %s" % (one, two, pycompat.bytestr(three),
241 244 pycompat.bytestr(four),
242 245 pycompat.bytestr(five))
243 246
244 247 def getbundle(self, source, heads=None, common=None, bundlecaps=None,
245 248 **kwargs):
246 249 chunks = exchange.getbundlechunks(self._repo, source, heads=heads,
247 250 common=common, bundlecaps=bundlecaps,
248 251 **kwargs)[1]
249 252 cb = util.chunkbuffer(chunks)
250 253
251 254 if exchange.bundle2requested(bundlecaps):
252 255 # When requesting a bundle2, getbundle returns a stream to make the
253 256 # wire level function happier. We need to build a proper object
254 257 # from it in local peer.
255 258 return bundle2.getunbundler(self.ui, cb)
256 259 else:
257 260 return changegroup.getunbundler('01', cb, None)
258 261
259 262 def heads(self):
260 263 return self._repo.heads()
261 264
262 265 def known(self, nodes):
263 266 return self._repo.known(nodes)
264 267
265 268 def listkeys(self, namespace):
266 269 return self._repo.listkeys(namespace)
267 270
268 271 def lookup(self, key):
269 272 return self._repo.lookup(key)
270 273
271 274 def pushkey(self, namespace, key, old, new):
272 275 return self._repo.pushkey(namespace, key, old, new)
273 276
274 277 def stream_out(self):
275 278 raise error.Abort(_('cannot perform stream clone against local '
276 279 'peer'))
277 280
278 281 def unbundle(self, bundle, heads, url):
279 282 """apply a bundle on a repo
280 283
281 284 This function handles the repo locking itself."""
282 285 try:
283 286 try:
284 287 bundle = exchange.readbundle(self.ui, bundle, None)
285 288 ret = exchange.unbundle(self._repo, bundle, heads, 'push', url)
286 289 if util.safehasattr(ret, 'getchunks'):
287 290 # This is a bundle20 object, turn it into an unbundler.
288 291 # This little dance should be dropped eventually when the
289 292 # API is finally improved.
290 293 stream = util.chunkbuffer(ret.getchunks())
291 294 ret = bundle2.getunbundler(self.ui, stream)
292 295 return ret
293 296 except Exception as exc:
294 297 # If the exception contains output salvaged from a bundle2
295 298 # reply, we need to make sure it is printed before continuing
296 299 # to fail. So we build a bundle2 with such output and consume
297 300 # it directly.
298 301 #
299 302 # This is not very elegant but allows a "simple" solution for
300 303 # issue4594
301 304 output = getattr(exc, '_bundle2salvagedoutput', ())
302 305 if output:
303 306 bundler = bundle2.bundle20(self._repo.ui)
304 307 for out in output:
305 308 bundler.addpart(out)
306 309 stream = util.chunkbuffer(bundler.getchunks())
307 310 b = bundle2.getunbundler(self.ui, stream)
308 311 bundle2.processbundle(self._repo, b)
309 312 raise
310 313 except error.PushRaced as exc:
311 314 raise error.ResponseError(_('push failed:'),
312 315 stringutil.forcebytestr(exc))
313 316
314 317 # End of _basewirecommands interface.
315 318
316 319 # Begin of peer interface.
317 320
318 321 def commandexecutor(self):
319 322 return localcommandexecutor(self)
320 323
321 324 # End of peer interface.
322 325
323 326 @zi.implementer(repository.ipeerlegacycommands)
324 327 class locallegacypeer(localpeer):
325 328 '''peer extension which implements legacy methods too; used for tests with
326 329 restricted capabilities'''
327 330
328 331 def __init__(self, repo):
329 332 super(locallegacypeer, self).__init__(repo, caps=legacycaps)
330 333
331 334 # Begin of baselegacywirecommands interface.
332 335
333 336 def between(self, pairs):
334 337 return self._repo.between(pairs)
335 338
336 339 def branches(self, nodes):
337 340 return self._repo.branches(nodes)
338 341
339 342 def changegroup(self, nodes, source):
340 343 outgoing = discovery.outgoing(self._repo, missingroots=nodes,
341 344 missingheads=self._repo.heads())
342 345 return changegroup.makechangegroup(self._repo, outgoing, '01', source)
343 346
344 347 def changegroupsubset(self, bases, heads, source):
345 348 outgoing = discovery.outgoing(self._repo, missingroots=bases,
346 349 missingheads=heads)
347 350 return changegroup.makechangegroup(self._repo, outgoing, '01', source)
348 351
349 352 # End of baselegacywirecommands interface.
350 353
351 354 # Increment the sub-version when the revlog v2 format changes to lock out old
352 355 # clients.
353 356 REVLOGV2_REQUIREMENT = 'exp-revlogv2.0'
354 357
355 358 # Functions receiving (ui, features) that extensions can register to impact
356 359 # the ability to load repositories with custom requirements. Only
357 360 # functions defined in loaded extensions are called.
358 361 #
359 362 # The function receives a set of requirement strings that the repository
360 363 # is capable of opening. Functions will typically add elements to the
361 364 # set to reflect that the extension knows how to handle that requirements.
362 365 featuresetupfuncs = set()
363 366
364 367 @zi.implementer(repository.completelocalrepository)
365 368 class localrepository(object):
366 369
367 370 # obsolete experimental requirements:
368 371 # - manifestv2: An experimental new manifest format that allowed
369 372 # for stem compression of long paths. Experiment ended up not
370 373 # being successful (repository sizes went up due to worse delta
371 374 # chains), and the code was deleted in 4.6.
372 375 supportedformats = {
373 376 'revlogv1',
374 377 'generaldelta',
375 378 'treemanifest',
376 379 REVLOGV2_REQUIREMENT,
377 380 }
378 381 _basesupported = supportedformats | {
379 382 'store',
380 383 'fncache',
381 384 'shared',
382 385 'relshared',
383 386 'dotencode',
384 387 'exp-sparse',
385 388 }
386 389 openerreqs = {
387 390 'revlogv1',
388 391 'generaldelta',
389 392 'treemanifest',
390 393 }
391 394
392 395 # list of prefix for file which can be written without 'wlock'
393 396 # Extensions should extend this list when needed
394 397 _wlockfreeprefix = {
395 398 # We migh consider requiring 'wlock' for the next
396 399 # two, but pretty much all the existing code assume
397 400 # wlock is not needed so we keep them excluded for
398 401 # now.
399 402 'hgrc',
400 403 'requires',
401 404 # XXX cache is a complicatged business someone
402 405 # should investigate this in depth at some point
403 406 'cache/',
404 407 # XXX shouldn't be dirstate covered by the wlock?
405 408 'dirstate',
406 409 # XXX bisect was still a bit too messy at the time
407 410 # this changeset was introduced. Someone should fix
408 411 # the remainig bit and drop this line
409 412 'bisect.state',
410 413 }
411 414
412 415 def __init__(self, baseui, path, create=False):
413 416 self.requirements = set()
414 417 self.filtername = None
415 418 # wvfs: rooted at the repository root, used to access the working copy
416 419 self.wvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
417 420 # vfs: rooted at .hg, used to access repo files outside of .hg/store
418 421 self.vfs = None
419 422 # svfs: usually rooted at .hg/store, used to access repository history
420 423 # If this is a shared repository, this vfs may point to another
421 424 # repository's .hg/store directory.
422 425 self.svfs = None
423 426 self.root = self.wvfs.base
424 427 self.path = self.wvfs.join(".hg")
425 428 self.origroot = path
426 429 # This is only used by context.workingctx.match in order to
427 430 # detect files in subrepos.
428 431 self.auditor = pathutil.pathauditor(
429 432 self.root, callback=self._checknested)
430 433 # This is only used by context.basectx.match in order to detect
431 434 # files in subrepos.
432 435 self.nofsauditor = pathutil.pathauditor(
433 436 self.root, callback=self._checknested, realfs=False, cached=True)
434 437 self.baseui = baseui
435 438 self.ui = baseui.copy()
436 439 self.ui.copy = baseui.copy # prevent copying repo configuration
437 440 self.vfs = vfsmod.vfs(self.path, cacheaudited=True)
438 441 if (self.ui.configbool('devel', 'all-warnings') or
439 442 self.ui.configbool('devel', 'check-locks')):
440 443 self.vfs.audit = self._getvfsward(self.vfs.audit)
441 444 # A list of callback to shape the phase if no data were found.
442 445 # Callback are in the form: func(repo, roots) --> processed root.
443 446 # This list it to be filled by extension during repo setup
444 447 self._phasedefaults = []
445 448 try:
446 449 self.ui.readconfig(self.vfs.join("hgrc"), self.root)
447 450 self._loadextensions()
448 451 except IOError:
449 452 pass
450 453
451 454 if featuresetupfuncs:
452 455 self.supported = set(self._basesupported) # use private copy
453 456 extmods = set(m.__name__ for n, m
454 457 in extensions.extensions(self.ui))
455 458 for setupfunc in featuresetupfuncs:
456 459 if setupfunc.__module__ in extmods:
457 460 setupfunc(self.ui, self.supported)
458 461 else:
459 462 self.supported = self._basesupported
460 463 color.setup(self.ui)
461 464
462 465 # Add compression engines.
463 466 for name in util.compengines:
464 467 engine = util.compengines[name]
465 468 if engine.revlogheader():
466 469 self.supported.add('exp-compression-%s' % name)
467 470
468 471 if not self.vfs.isdir():
469 472 if create:
470 473 self.requirements = newreporequirements(self)
471 474
472 475 if not self.wvfs.exists():
473 476 self.wvfs.makedirs()
474 477 self.vfs.makedir(notindexed=True)
475 478
476 479 if 'store' in self.requirements:
477 480 self.vfs.mkdir("store")
478 481
479 482 # create an invalid changelog
480 483 self.vfs.append(
481 484 "00changelog.i",
482 485 '\0\0\0\2' # represents revlogv2
483 486 ' dummy changelog to prevent using the old repo layout'
484 487 )
485 488 else:
486 489 raise error.RepoError(_("repository %s not found") % path)
487 490 elif create:
488 491 raise error.RepoError(_("repository %s already exists") % path)
489 492 else:
490 493 try:
491 494 self.requirements = scmutil.readrequires(
492 495 self.vfs, self.supported)
493 496 except IOError as inst:
494 497 if inst.errno != errno.ENOENT:
495 498 raise
496 499
497 500 cachepath = self.vfs.join('cache')
498 501 self.sharedpath = self.path
499 502 try:
500 503 sharedpath = self.vfs.read("sharedpath").rstrip('\n')
501 504 if 'relshared' in self.requirements:
502 505 sharedpath = self.vfs.join(sharedpath)
503 506 vfs = vfsmod.vfs(sharedpath, realpath=True)
504 507 cachepath = vfs.join('cache')
505 508 s = vfs.base
506 509 if not vfs.exists():
507 510 raise error.RepoError(
508 511 _('.hg/sharedpath points to nonexistent directory %s') % s)
509 512 self.sharedpath = s
510 513 except IOError as inst:
511 514 if inst.errno != errno.ENOENT:
512 515 raise
513 516
514 517 if 'exp-sparse' in self.requirements and not sparse.enabled:
515 518 raise error.RepoError(_('repository is using sparse feature but '
516 519 'sparse is not enabled; enable the '
517 520 '"sparse" extensions to access'))
518 521
519 522 self.store = store.store(
520 523 self.requirements, self.sharedpath,
521 524 lambda base: vfsmod.vfs(base, cacheaudited=True))
522 525 self.spath = self.store.path
523 526 self.svfs = self.store.vfs
524 527 self.sjoin = self.store.join
525 528 self.vfs.createmode = self.store.createmode
526 529 self.cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
527 530 self.cachevfs.createmode = self.store.createmode
528 531 if (self.ui.configbool('devel', 'all-warnings') or
529 532 self.ui.configbool('devel', 'check-locks')):
530 533 if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs
531 534 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
532 535 else: # standard vfs
533 536 self.svfs.audit = self._getsvfsward(self.svfs.audit)
534 537 self._applyopenerreqs()
535 538 if create:
536 539 self._writerequirements()
537 540
538 541 self._dirstatevalidatewarned = False
539 542
540 543 self._branchcaches = {}
541 544 self._revbranchcache = None
542 545 self._filterpats = {}
543 546 self._datafilters = {}
544 547 self._transref = self._lockref = self._wlockref = None
545 548
546 549 # A cache for various files under .hg/ that tracks file changes,
547 550 # (used by the filecache decorator)
548 551 #
549 552 # Maps a property name to its util.filecacheentry
550 553 self._filecache = {}
551 554
552 555 # hold sets of revision to be filtered
553 556 # should be cleared when something might have changed the filter value:
554 557 # - new changesets,
555 558 # - phase change,
556 559 # - new obsolescence marker,
557 560 # - working directory parent change,
558 561 # - bookmark changes
559 562 self.filteredrevcache = {}
560 563
561 564 # post-dirstate-status hooks
562 565 self._postdsstatus = []
563 566
564 567 # generic mapping between names and nodes
565 568 self.names = namespaces.namespaces()
566 569
567 570 # Key to signature value.
568 571 self._sparsesignaturecache = {}
569 572 # Signature to cached matcher instance.
570 573 self._sparsematchercache = {}
571 574
572 575 def _getvfsward(self, origfunc):
573 576 """build a ward for self.vfs"""
574 577 rref = weakref.ref(self)
575 578 def checkvfs(path, mode=None):
576 579 ret = origfunc(path, mode=mode)
577 580 repo = rref()
578 581 if (repo is None
579 582 or not util.safehasattr(repo, '_wlockref')
580 583 or not util.safehasattr(repo, '_lockref')):
581 584 return
582 585 if mode in (None, 'r', 'rb'):
583 586 return
584 587 if path.startswith(repo.path):
585 588 # truncate name relative to the repository (.hg)
586 589 path = path[len(repo.path) + 1:]
587 590 if path.startswith('cache/'):
588 591 msg = 'accessing cache with vfs instead of cachevfs: "%s"'
589 592 repo.ui.develwarn(msg % path, stacklevel=2, config="cache-vfs")
590 593 if path.startswith('journal.'):
591 594 # journal is covered by 'lock'
592 595 if repo._currentlock(repo._lockref) is None:
593 596 repo.ui.develwarn('write with no lock: "%s"' % path,
594 597 stacklevel=2, config='check-locks')
595 598 elif repo._currentlock(repo._wlockref) is None:
596 599 # rest of vfs files are covered by 'wlock'
597 600 #
598 601 # exclude special files
599 602 for prefix in self._wlockfreeprefix:
600 603 if path.startswith(prefix):
601 604 return
602 605 repo.ui.develwarn('write with no wlock: "%s"' % path,
603 606 stacklevel=2, config='check-locks')
604 607 return ret
605 608 return checkvfs
606 609
607 610 def _getsvfsward(self, origfunc):
608 611 """build a ward for self.svfs"""
609 612 rref = weakref.ref(self)
610 613 def checksvfs(path, mode=None):
611 614 ret = origfunc(path, mode=mode)
612 615 repo = rref()
613 616 if repo is None or not util.safehasattr(repo, '_lockref'):
614 617 return
615 618 if mode in (None, 'r', 'rb'):
616 619 return
617 620 if path.startswith(repo.sharedpath):
618 621 # truncate name relative to the repository (.hg)
619 622 path = path[len(repo.sharedpath) + 1:]
620 623 if repo._currentlock(repo._lockref) is None:
621 624 repo.ui.develwarn('write with no lock: "%s"' % path,
622 625 stacklevel=3)
623 626 return ret
624 627 return checksvfs
625 628
626 629 def close(self):
627 630 self._writecaches()
628 631
629 632 def _loadextensions(self):
630 633 extensions.loadall(self.ui)
631 634
632 635 def _writecaches(self):
633 636 if self._revbranchcache:
634 637 self._revbranchcache.write()
635 638
636 639 def _restrictcapabilities(self, caps):
637 640 if self.ui.configbool('experimental', 'bundle2-advertise'):
638 641 caps = set(caps)
639 642 capsblob = bundle2.encodecaps(bundle2.getrepocaps(self,
640 643 role='client'))
641 644 caps.add('bundle2=' + urlreq.quote(capsblob))
642 645 return caps
643 646
644 647 def _applyopenerreqs(self):
645 648 self.svfs.options = dict((r, 1) for r in self.requirements
646 649 if r in self.openerreqs)
647 650 # experimental config: format.chunkcachesize
648 651 chunkcachesize = self.ui.configint('format', 'chunkcachesize')
649 652 if chunkcachesize is not None:
650 653 self.svfs.options['chunkcachesize'] = chunkcachesize
651 654 # experimental config: format.maxchainlen
652 655 maxchainlen = self.ui.configint('format', 'maxchainlen')
653 656 if maxchainlen is not None:
654 657 self.svfs.options['maxchainlen'] = maxchainlen
655 658 # experimental config: format.manifestcachesize
656 659 manifestcachesize = self.ui.configint('format', 'manifestcachesize')
657 660 if manifestcachesize is not None:
658 661 self.svfs.options['manifestcachesize'] = manifestcachesize
659 662 # experimental config: format.aggressivemergedeltas
660 663 aggressivemergedeltas = self.ui.configbool('format',
661 664 'aggressivemergedeltas')
662 665 self.svfs.options['aggressivemergedeltas'] = aggressivemergedeltas
663 666 self.svfs.options['lazydeltabase'] = not scmutil.gddeltaconfig(self.ui)
664 667 chainspan = self.ui.configbytes('experimental', 'maxdeltachainspan')
665 668 if 0 <= chainspan:
666 669 self.svfs.options['maxdeltachainspan'] = chainspan
667 670 mmapindexthreshold = self.ui.configbytes('experimental',
668 671 'mmapindexthreshold')
669 672 if mmapindexthreshold is not None:
670 673 self.svfs.options['mmapindexthreshold'] = mmapindexthreshold
671 674 withsparseread = self.ui.configbool('experimental', 'sparse-read')
672 675 srdensitythres = float(self.ui.config('experimental',
673 676 'sparse-read.density-threshold'))
674 677 srmingapsize = self.ui.configbytes('experimental',
675 678 'sparse-read.min-gap-size')
676 679 self.svfs.options['with-sparse-read'] = withsparseread
677 680 self.svfs.options['sparse-read-density-threshold'] = srdensitythres
678 681 self.svfs.options['sparse-read-min-gap-size'] = srmingapsize
679 682
680 683 for r in self.requirements:
681 684 if r.startswith('exp-compression-'):
682 685 self.svfs.options['compengine'] = r[len('exp-compression-'):]
683 686
684 687 # TODO move "revlogv2" to openerreqs once finalized.
685 688 if REVLOGV2_REQUIREMENT in self.requirements:
686 689 self.svfs.options['revlogv2'] = True
687 690
688 691 def _writerequirements(self):
689 692 scmutil.writerequires(self.vfs, self.requirements)
690 693
691 694 def _checknested(self, path):
692 695 """Determine if path is a legal nested repository."""
693 696 if not path.startswith(self.root):
694 697 return False
695 698 subpath = path[len(self.root) + 1:]
696 699 normsubpath = util.pconvert(subpath)
697 700
698 701 # XXX: Checking against the current working copy is wrong in
699 702 # the sense that it can reject things like
700 703 #
701 704 # $ hg cat -r 10 sub/x.txt
702 705 #
703 706 # if sub/ is no longer a subrepository in the working copy
704 707 # parent revision.
705 708 #
706 709 # However, it can of course also allow things that would have
707 710 # been rejected before, such as the above cat command if sub/
708 711 # is a subrepository now, but was a normal directory before.
709 712 # The old path auditor would have rejected by mistake since it
710 713 # panics when it sees sub/.hg/.
711 714 #
712 715 # All in all, checking against the working copy seems sensible
713 716 # since we want to prevent access to nested repositories on
714 717 # the filesystem *now*.
715 718 ctx = self[None]
716 719 parts = util.splitpath(subpath)
717 720 while parts:
718 721 prefix = '/'.join(parts)
719 722 if prefix in ctx.substate:
720 723 if prefix == normsubpath:
721 724 return True
722 725 else:
723 726 sub = ctx.sub(prefix)
724 727 return sub.checknested(subpath[len(prefix) + 1:])
725 728 else:
726 729 parts.pop()
727 730 return False
728 731
729 732 def peer(self):
730 733 return localpeer(self) # not cached to avoid reference cycle
731 734
732 735 def unfiltered(self):
733 736 """Return unfiltered version of the repository
734 737
735 738 Intended to be overwritten by filtered repo."""
736 739 return self
737 740
738 741 def filtered(self, name, visibilityexceptions=None):
739 742 """Return a filtered version of a repository"""
740 743 cls = repoview.newtype(self.unfiltered().__class__)
741 744 return cls(self, name, visibilityexceptions)
742 745
743 746 @repofilecache('bookmarks', 'bookmarks.current')
744 747 def _bookmarks(self):
745 748 return bookmarks.bmstore(self)
746 749
747 750 @property
748 751 def _activebookmark(self):
749 752 return self._bookmarks.active
750 753
751 754 # _phasesets depend on changelog. what we need is to call
752 755 # _phasecache.invalidate() if '00changelog.i' was changed, but it
753 756 # can't be easily expressed in filecache mechanism.
754 757 @storecache('phaseroots', '00changelog.i')
755 758 def _phasecache(self):
756 759 return phases.phasecache(self, self._phasedefaults)
757 760
758 761 @storecache('obsstore')
759 762 def obsstore(self):
760 763 return obsolete.makestore(self.ui, self)
761 764
762 765 @storecache('00changelog.i')
763 766 def changelog(self):
764 767 return changelog.changelog(self.svfs,
765 768 trypending=txnutil.mayhavepending(self.root))
766 769
767 770 def _constructmanifest(self):
768 771 # This is a temporary function while we migrate from manifest to
769 772 # manifestlog. It allows bundlerepo and unionrepo to intercept the
770 773 # manifest creation.
771 774 return manifest.manifestrevlog(self.svfs)
772 775
773 776 @storecache('00manifest.i')
774 777 def manifestlog(self):
775 778 return manifest.manifestlog(self.svfs, self)
776 779
777 780 @repofilecache('dirstate')
778 781 def dirstate(self):
779 782 sparsematchfn = lambda: sparse.matcher(self)
780 783
781 784 return dirstate.dirstate(self.vfs, self.ui, self.root,
782 785 self._dirstatevalidate, sparsematchfn)
783 786
784 787 def _dirstatevalidate(self, node):
785 788 try:
786 789 self.changelog.rev(node)
787 790 return node
788 791 except error.LookupError:
789 792 if not self._dirstatevalidatewarned:
790 793 self._dirstatevalidatewarned = True
791 794 self.ui.warn(_("warning: ignoring unknown"
792 795 " working parent %s!\n") % short(node))
793 796 return nullid
794 797
795 798 @repofilecache(narrowspec.FILENAME)
796 799 def narrowpats(self):
797 800 """matcher patterns for this repository's narrowspec
798 801
799 802 A tuple of (includes, excludes).
800 803 """
801 804 source = self
802 805 if self.shared():
803 806 from . import hg
804 807 source = hg.sharedreposource(self)
805 808 return narrowspec.load(source)
806 809
807 810 @repofilecache(narrowspec.FILENAME)
808 811 def _narrowmatch(self):
809 812 if changegroup.NARROW_REQUIREMENT not in self.requirements:
810 813 return matchmod.always(self.root, '')
811 814 include, exclude = self.narrowpats
812 815 return narrowspec.match(self.root, include=include, exclude=exclude)
813 816
814 817 # TODO(martinvonz): make this property-like instead?
815 818 def narrowmatch(self):
816 819 return self._narrowmatch
817 820
818 821 def setnarrowpats(self, newincludes, newexcludes):
819 822 target = self
820 823 if self.shared():
821 824 from . import hg
822 825 target = hg.sharedreposource(self)
823 826 narrowspec.save(target, newincludes, newexcludes)
824 827 self.invalidate(clearfilecache=True)
825 828
826 829 def __getitem__(self, changeid):
827 830 if changeid is None:
828 831 return context.workingctx(self)
829 832 if isinstance(changeid, context.basectx):
830 833 return changeid
831 834 if isinstance(changeid, slice):
832 835 # wdirrev isn't contiguous so the slice shouldn't include it
833 836 return [context.changectx(self, i)
834 837 for i in xrange(*changeid.indices(len(self)))
835 838 if i not in self.changelog.filteredrevs]
836 839 try:
837 840 return context.changectx(self, changeid)
838 841 except error.WdirUnsupported:
839 842 return context.workingctx(self)
840 843
841 844 def __contains__(self, changeid):
842 845 """True if the given changeid exists
843 846
844 847 error.LookupError is raised if an ambiguous node specified.
845 848 """
846 849 try:
847 850 self[changeid]
848 851 return True
849 852 except (error.RepoLookupError, error.FilteredIndexError,
850 853 error.FilteredLookupError):
851 854 return False
852 855
853 856 def __nonzero__(self):
854 857 return True
855 858
856 859 __bool__ = __nonzero__
857 860
858 861 def __len__(self):
859 862 # no need to pay the cost of repoview.changelog
860 863 unfi = self.unfiltered()
861 864 return len(unfi.changelog)
862 865
863 866 def __iter__(self):
864 867 return iter(self.changelog)
865 868
866 869 def revs(self, expr, *args):
867 870 '''Find revisions matching a revset.
868 871
869 872 The revset is specified as a string ``expr`` that may contain
870 873 %-formatting to escape certain types. See ``revsetlang.formatspec``.
871 874
872 875 Revset aliases from the configuration are not expanded. To expand
873 876 user aliases, consider calling ``scmutil.revrange()`` or
874 877 ``repo.anyrevs([expr], user=True)``.
875 878
876 879 Returns a revset.abstractsmartset, which is a list-like interface
877 880 that contains integer revisions.
878 881 '''
879 882 expr = revsetlang.formatspec(expr, *args)
880 883 m = revset.match(None, expr)
881 884 return m(self)
882 885
883 886 def set(self, expr, *args):
884 887 '''Find revisions matching a revset and emit changectx instances.
885 888
886 889 This is a convenience wrapper around ``revs()`` that iterates the
887 890 result and is a generator of changectx instances.
888 891
889 892 Revset aliases from the configuration are not expanded. To expand
890 893 user aliases, consider calling ``scmutil.revrange()``.
891 894 '''
892 895 for r in self.revs(expr, *args):
893 896 yield self[r]
894 897
895 898 def anyrevs(self, specs, user=False, localalias=None):
896 899 '''Find revisions matching one of the given revsets.
897 900
898 901 Revset aliases from the configuration are not expanded by default. To
899 902 expand user aliases, specify ``user=True``. To provide some local
900 903 definitions overriding user aliases, set ``localalias`` to
901 904 ``{name: definitionstring}``.
902 905 '''
903 906 if user:
904 907 m = revset.matchany(self.ui, specs, repo=self,
905 908 localalias=localalias)
906 909 else:
907 910 m = revset.matchany(None, specs, localalias=localalias)
908 911 return m(self)
909 912
910 913 def url(self):
911 914 return 'file:' + self.root
912 915
913 916 def hook(self, name, throw=False, **args):
914 917 """Call a hook, passing this repo instance.
915 918
916 919 This a convenience method to aid invoking hooks. Extensions likely
917 920 won't call this unless they have registered a custom hook or are
918 921 replacing code that is expected to call a hook.
919 922 """
920 923 return hook.hook(self.ui, self, name, throw, **args)
921 924
922 925 @filteredpropertycache
923 926 def _tagscache(self):
924 927 '''Returns a tagscache object that contains various tags related
925 928 caches.'''
926 929
927 930 # This simplifies its cache management by having one decorated
928 931 # function (this one) and the rest simply fetch things from it.
929 932 class tagscache(object):
930 933 def __init__(self):
931 934 # These two define the set of tags for this repository. tags
932 935 # maps tag name to node; tagtypes maps tag name to 'global' or
933 936 # 'local'. (Global tags are defined by .hgtags across all
934 937 # heads, and local tags are defined in .hg/localtags.)
935 938 # They constitute the in-memory cache of tags.
936 939 self.tags = self.tagtypes = None
937 940
938 941 self.nodetagscache = self.tagslist = None
939 942
940 943 cache = tagscache()
941 944 cache.tags, cache.tagtypes = self._findtags()
942 945
943 946 return cache
944 947
945 948 def tags(self):
946 949 '''return a mapping of tag to node'''
947 950 t = {}
948 951 if self.changelog.filteredrevs:
949 952 tags, tt = self._findtags()
950 953 else:
951 954 tags = self._tagscache.tags
952 955 for k, v in tags.iteritems():
953 956 try:
954 957 # ignore tags to unknown nodes
955 958 self.changelog.rev(v)
956 959 t[k] = v
957 960 except (error.LookupError, ValueError):
958 961 pass
959 962 return t
960 963
961 964 def _findtags(self):
962 965 '''Do the hard work of finding tags. Return a pair of dicts
963 966 (tags, tagtypes) where tags maps tag name to node, and tagtypes
964 967 maps tag name to a string like \'global\' or \'local\'.
965 968 Subclasses or extensions are free to add their own tags, but
966 969 should be aware that the returned dicts will be retained for the
967 970 duration of the localrepo object.'''
968 971
969 972 # XXX what tagtype should subclasses/extensions use? Currently
970 973 # mq and bookmarks add tags, but do not set the tagtype at all.
971 974 # Should each extension invent its own tag type? Should there
972 975 # be one tagtype for all such "virtual" tags? Or is the status
973 976 # quo fine?
974 977
975 978
976 979 # map tag name to (node, hist)
977 980 alltags = tagsmod.findglobaltags(self.ui, self)
978 981 # map tag name to tag type
979 982 tagtypes = dict((tag, 'global') for tag in alltags)
980 983
981 984 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
982 985
983 986 # Build the return dicts. Have to re-encode tag names because
984 987 # the tags module always uses UTF-8 (in order not to lose info
985 988 # writing to the cache), but the rest of Mercurial wants them in
986 989 # local encoding.
987 990 tags = {}
988 991 for (name, (node, hist)) in alltags.iteritems():
989 992 if node != nullid:
990 993 tags[encoding.tolocal(name)] = node
991 994 tags['tip'] = self.changelog.tip()
992 995 tagtypes = dict([(encoding.tolocal(name), value)
993 996 for (name, value) in tagtypes.iteritems()])
994 997 return (tags, tagtypes)
995 998
996 999 def tagtype(self, tagname):
997 1000 '''
998 1001 return the type of the given tag. result can be:
999 1002
1000 1003 'local' : a local tag
1001 1004 'global' : a global tag
1002 1005 None : tag does not exist
1003 1006 '''
1004 1007
1005 1008 return self._tagscache.tagtypes.get(tagname)
1006 1009
1007 1010 def tagslist(self):
1008 1011 '''return a list of tags ordered by revision'''
1009 1012 if not self._tagscache.tagslist:
1010 1013 l = []
1011 1014 for t, n in self.tags().iteritems():
1012 1015 l.append((self.changelog.rev(n), t, n))
1013 1016 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
1014 1017
1015 1018 return self._tagscache.tagslist
1016 1019
1017 1020 def nodetags(self, node):
1018 1021 '''return the tags associated with a node'''
1019 1022 if not self._tagscache.nodetagscache:
1020 1023 nodetagscache = {}
1021 1024 for t, n in self._tagscache.tags.iteritems():
1022 1025 nodetagscache.setdefault(n, []).append(t)
1023 1026 for tags in nodetagscache.itervalues():
1024 1027 tags.sort()
1025 1028 self._tagscache.nodetagscache = nodetagscache
1026 1029 return self._tagscache.nodetagscache.get(node, [])
1027 1030
1028 1031 def nodebookmarks(self, node):
1029 1032 """return the list of bookmarks pointing to the specified node"""
1030 1033 marks = []
1031 1034 for bookmark, n in self._bookmarks.iteritems():
1032 1035 if n == node:
1033 1036 marks.append(bookmark)
1034 1037 return sorted(marks)
1035 1038
1036 1039 def branchmap(self):
1037 1040 '''returns a dictionary {branch: [branchheads]} with branchheads
1038 1041 ordered by increasing revision number'''
1039 1042 branchmap.updatecache(self)
1040 1043 return self._branchcaches[self.filtername]
1041 1044
1042 1045 @unfilteredmethod
1043 1046 def revbranchcache(self):
1044 1047 if not self._revbranchcache:
1045 1048 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
1046 1049 return self._revbranchcache
1047 1050
1048 1051 def branchtip(self, branch, ignoremissing=False):
1049 1052 '''return the tip node for a given branch
1050 1053
1051 1054 If ignoremissing is True, then this method will not raise an error.
1052 1055 This is helpful for callers that only expect None for a missing branch
1053 1056 (e.g. namespace).
1054 1057
1055 1058 '''
1056 1059 try:
1057 1060 return self.branchmap().branchtip(branch)
1058 1061 except KeyError:
1059 1062 if not ignoremissing:
1060 1063 raise error.RepoLookupError(_("unknown branch '%s'") % branch)
1061 1064 else:
1062 1065 pass
1063 1066
1064 1067 def lookup(self, key):
1065 1068 return scmutil.revsymbol(self, key).node()
1066 1069
1067 1070 def lookupbranch(self, key):
1068 1071 if key in self.branchmap():
1069 1072 return key
1070 1073
1071 1074 return scmutil.revsymbol(self, key).branch()
1072 1075
1073 1076 def known(self, nodes):
1074 1077 cl = self.changelog
1075 1078 nm = cl.nodemap
1076 1079 filtered = cl.filteredrevs
1077 1080 result = []
1078 1081 for n in nodes:
1079 1082 r = nm.get(n)
1080 1083 resp = not (r is None or r in filtered)
1081 1084 result.append(resp)
1082 1085 return result
1083 1086
1084 1087 def local(self):
1085 1088 return self
1086 1089
1087 1090 def publishing(self):
1088 1091 # it's safe (and desirable) to trust the publish flag unconditionally
1089 1092 # so that we don't finalize changes shared between users via ssh or nfs
1090 1093 return self.ui.configbool('phases', 'publish', untrusted=True)
1091 1094
1092 1095 def cancopy(self):
1093 1096 # so statichttprepo's override of local() works
1094 1097 if not self.local():
1095 1098 return False
1096 1099 if not self.publishing():
1097 1100 return True
1098 1101 # if publishing we can't copy if there is filtered content
1099 1102 return not self.filtered('visible').changelog.filteredrevs
1100 1103
1101 1104 def shared(self):
1102 1105 '''the type of shared repository (None if not shared)'''
1103 1106 if self.sharedpath != self.path:
1104 1107 return 'store'
1105 1108 return None
1106 1109
1107 1110 def wjoin(self, f, *insidef):
1108 1111 return self.vfs.reljoin(self.root, f, *insidef)
1109 1112
1110 1113 def file(self, f):
1111 1114 if f[0] == '/':
1112 1115 f = f[1:]
1113 1116 return filelog.filelog(self.svfs, f)
1114 1117
1115 1118 def setparents(self, p1, p2=nullid):
1116 1119 with self.dirstate.parentchange():
1117 1120 copies = self.dirstate.setparents(p1, p2)
1118 1121 pctx = self[p1]
1119 1122 if copies:
1120 1123 # Adjust copy records, the dirstate cannot do it, it
1121 1124 # requires access to parents manifests. Preserve them
1122 1125 # only for entries added to first parent.
1123 1126 for f in copies:
1124 1127 if f not in pctx and copies[f] in pctx:
1125 1128 self.dirstate.copy(copies[f], f)
1126 1129 if p2 == nullid:
1127 1130 for f, s in sorted(self.dirstate.copies().items()):
1128 1131 if f not in pctx and s not in pctx:
1129 1132 self.dirstate.copy(None, f)
1130 1133
1131 1134 def filectx(self, path, changeid=None, fileid=None, changectx=None):
1132 1135 """changeid can be a changeset revision, node, or tag.
1133 1136 fileid can be a file revision or node."""
1134 1137 return context.filectx(self, path, changeid, fileid,
1135 1138 changectx=changectx)
1136 1139
1137 1140 def getcwd(self):
1138 1141 return self.dirstate.getcwd()
1139 1142
1140 1143 def pathto(self, f, cwd=None):
1141 1144 return self.dirstate.pathto(f, cwd)
1142 1145
1143 1146 def _loadfilter(self, filter):
1144 1147 if filter not in self._filterpats:
1145 1148 l = []
1146 1149 for pat, cmd in self.ui.configitems(filter):
1147 1150 if cmd == '!':
1148 1151 continue
1149 1152 mf = matchmod.match(self.root, '', [pat])
1150 1153 fn = None
1151 1154 params = cmd
1152 1155 for name, filterfn in self._datafilters.iteritems():
1153 1156 if cmd.startswith(name):
1154 1157 fn = filterfn
1155 1158 params = cmd[len(name):].lstrip()
1156 1159 break
1157 1160 if not fn:
1158 1161 fn = lambda s, c, **kwargs: procutil.filter(s, c)
1159 1162 # Wrap old filters not supporting keyword arguments
1160 1163 if not pycompat.getargspec(fn)[2]:
1161 1164 oldfn = fn
1162 1165 fn = lambda s, c, **kwargs: oldfn(s, c)
1163 1166 l.append((mf, fn, params))
1164 1167 self._filterpats[filter] = l
1165 1168 return self._filterpats[filter]
1166 1169
1167 1170 def _filter(self, filterpats, filename, data):
1168 1171 for mf, fn, cmd in filterpats:
1169 1172 if mf(filename):
1170 1173 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
1171 1174 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
1172 1175 break
1173 1176
1174 1177 return data
1175 1178
1176 1179 @unfilteredpropertycache
1177 1180 def _encodefilterpats(self):
1178 1181 return self._loadfilter('encode')
1179 1182
1180 1183 @unfilteredpropertycache
1181 1184 def _decodefilterpats(self):
1182 1185 return self._loadfilter('decode')
1183 1186
1184 1187 def adddatafilter(self, name, filter):
1185 1188 self._datafilters[name] = filter
1186 1189
1187 1190 def wread(self, filename):
1188 1191 if self.wvfs.islink(filename):
1189 1192 data = self.wvfs.readlink(filename)
1190 1193 else:
1191 1194 data = self.wvfs.read(filename)
1192 1195 return self._filter(self._encodefilterpats, filename, data)
1193 1196
1194 1197 def wwrite(self, filename, data, flags, backgroundclose=False, **kwargs):
1195 1198 """write ``data`` into ``filename`` in the working directory
1196 1199
1197 1200 This returns length of written (maybe decoded) data.
1198 1201 """
1199 1202 data = self._filter(self._decodefilterpats, filename, data)
1200 1203 if 'l' in flags:
1201 1204 self.wvfs.symlink(data, filename)
1202 1205 else:
1203 1206 self.wvfs.write(filename, data, backgroundclose=backgroundclose,
1204 1207 **kwargs)
1205 1208 if 'x' in flags:
1206 1209 self.wvfs.setflags(filename, False, True)
1207 1210 else:
1208 1211 self.wvfs.setflags(filename, False, False)
1209 1212 return len(data)
1210 1213
1211 1214 def wwritedata(self, filename, data):
1212 1215 return self._filter(self._decodefilterpats, filename, data)
1213 1216
1214 1217 def currenttransaction(self):
1215 1218 """return the current transaction or None if non exists"""
1216 1219 if self._transref:
1217 1220 tr = self._transref()
1218 1221 else:
1219 1222 tr = None
1220 1223
1221 1224 if tr and tr.running():
1222 1225 return tr
1223 1226 return None
1224 1227
1225 1228 def transaction(self, desc, report=None):
1226 1229 if (self.ui.configbool('devel', 'all-warnings')
1227 1230 or self.ui.configbool('devel', 'check-locks')):
1228 1231 if self._currentlock(self._lockref) is None:
1229 1232 raise error.ProgrammingError('transaction requires locking')
1230 1233 tr = self.currenttransaction()
1231 1234 if tr is not None:
1232 1235 return tr.nest(name=desc)
1233 1236
1234 1237 # abort here if the journal already exists
1235 1238 if self.svfs.exists("journal"):
1236 1239 raise error.RepoError(
1237 1240 _("abandoned transaction found"),
1238 1241 hint=_("run 'hg recover' to clean up transaction"))
1239 1242
1240 1243 idbase = "%.40f#%f" % (random.random(), time.time())
1241 1244 ha = hex(hashlib.sha1(idbase).digest())
1242 1245 txnid = 'TXN:' + ha
1243 1246 self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid)
1244 1247
1245 1248 self._writejournal(desc)
1246 1249 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
1247 1250 if report:
1248 1251 rp = report
1249 1252 else:
1250 1253 rp = self.ui.warn
1251 1254 vfsmap = {'plain': self.vfs} # root of .hg/
1252 1255 # we must avoid cyclic reference between repo and transaction.
1253 1256 reporef = weakref.ref(self)
1254 1257 # Code to track tag movement
1255 1258 #
1256 1259 # Since tags are all handled as file content, it is actually quite hard
1257 1260 # to track these movement from a code perspective. So we fallback to a
1258 1261 # tracking at the repository level. One could envision to track changes
1259 1262 # to the '.hgtags' file through changegroup apply but that fails to
1260 1263 # cope with case where transaction expose new heads without changegroup
1261 1264 # being involved (eg: phase movement).
1262 1265 #
1263 1266 # For now, We gate the feature behind a flag since this likely comes
1264 1267 # with performance impacts. The current code run more often than needed
1265 1268 # and do not use caches as much as it could. The current focus is on
1266 1269 # the behavior of the feature so we disable it by default. The flag
1267 1270 # will be removed when we are happy with the performance impact.
1268 1271 #
1269 1272 # Once this feature is no longer experimental move the following
1270 1273 # documentation to the appropriate help section:
1271 1274 #
1272 1275 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
1273 1276 # tags (new or changed or deleted tags). In addition the details of
1274 1277 # these changes are made available in a file at:
1275 1278 # ``REPOROOT/.hg/changes/tags.changes``.
1276 1279 # Make sure you check for HG_TAG_MOVED before reading that file as it
1277 1280 # might exist from a previous transaction even if no tag were touched
1278 1281 # in this one. Changes are recorded in a line base format::
1279 1282 #
1280 1283 # <action> <hex-node> <tag-name>\n
1281 1284 #
1282 1285 # Actions are defined as follow:
1283 1286 # "-R": tag is removed,
1284 1287 # "+A": tag is added,
1285 1288 # "-M": tag is moved (old value),
1286 1289 # "+M": tag is moved (new value),
1287 1290 tracktags = lambda x: None
1288 1291 # experimental config: experimental.hook-track-tags
1289 1292 shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags')
1290 1293 if desc != 'strip' and shouldtracktags:
1291 1294 oldheads = self.changelog.headrevs()
1292 1295 def tracktags(tr2):
1293 1296 repo = reporef()
1294 1297 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
1295 1298 newheads = repo.changelog.headrevs()
1296 1299 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
1297 1300 # notes: we compare lists here.
1298 1301 # As we do it only once buiding set would not be cheaper
1299 1302 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
1300 1303 if changes:
1301 1304 tr2.hookargs['tag_moved'] = '1'
1302 1305 with repo.vfs('changes/tags.changes', 'w',
1303 1306 atomictemp=True) as changesfile:
1304 1307 # note: we do not register the file to the transaction
1305 1308 # because we needs it to still exist on the transaction
1306 1309 # is close (for txnclose hooks)
1307 1310 tagsmod.writediff(changesfile, changes)
1308 1311 def validate(tr2):
1309 1312 """will run pre-closing hooks"""
1310 1313 # XXX the transaction API is a bit lacking here so we take a hacky
1311 1314 # path for now
1312 1315 #
1313 1316 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
1314 1317 # dict is copied before these run. In addition we needs the data
1315 1318 # available to in memory hooks too.
1316 1319 #
1317 1320 # Moreover, we also need to make sure this runs before txnclose
1318 1321 # hooks and there is no "pending" mechanism that would execute
1319 1322 # logic only if hooks are about to run.
1320 1323 #
1321 1324 # Fixing this limitation of the transaction is also needed to track
1322 1325 # other families of changes (bookmarks, phases, obsolescence).
1323 1326 #
1324 1327 # This will have to be fixed before we remove the experimental
1325 1328 # gating.
1326 1329 tracktags(tr2)
1327 1330 repo = reporef()
1328 1331 if repo.ui.configbool('experimental', 'single-head-per-branch'):
1329 1332 scmutil.enforcesinglehead(repo, tr2, desc)
1330 1333 if hook.hashook(repo.ui, 'pretxnclose-bookmark'):
1331 1334 for name, (old, new) in sorted(tr.changes['bookmarks'].items()):
1332 1335 args = tr.hookargs.copy()
1333 1336 args.update(bookmarks.preparehookargs(name, old, new))
1334 1337 repo.hook('pretxnclose-bookmark', throw=True,
1335 1338 txnname=desc,
1336 1339 **pycompat.strkwargs(args))
1337 1340 if hook.hashook(repo.ui, 'pretxnclose-phase'):
1338 1341 cl = repo.unfiltered().changelog
1339 1342 for rev, (old, new) in tr.changes['phases'].items():
1340 1343 args = tr.hookargs.copy()
1341 1344 node = hex(cl.node(rev))
1342 1345 args.update(phases.preparehookargs(node, old, new))
1343 1346 repo.hook('pretxnclose-phase', throw=True, txnname=desc,
1344 1347 **pycompat.strkwargs(args))
1345 1348
1346 1349 repo.hook('pretxnclose', throw=True,
1347 1350 txnname=desc, **pycompat.strkwargs(tr.hookargs))
1348 1351 def releasefn(tr, success):
1349 1352 repo = reporef()
1350 1353 if success:
1351 1354 # this should be explicitly invoked here, because
1352 1355 # in-memory changes aren't written out at closing
1353 1356 # transaction, if tr.addfilegenerator (via
1354 1357 # dirstate.write or so) isn't invoked while
1355 1358 # transaction running
1356 1359 repo.dirstate.write(None)
1357 1360 else:
1358 1361 # discard all changes (including ones already written
1359 1362 # out) in this transaction
1360 1363 repo.dirstate.restorebackup(None, 'journal.dirstate')
1361 1364
1362 1365 repo.invalidate(clearfilecache=True)
1363 1366
1364 1367 tr = transaction.transaction(rp, self.svfs, vfsmap,
1365 1368 "journal",
1366 1369 "undo",
1367 1370 aftertrans(renames),
1368 1371 self.store.createmode,
1369 1372 validator=validate,
1370 1373 releasefn=releasefn,
1371 1374 checkambigfiles=_cachedfiles,
1372 1375 name=desc)
1373 1376 tr.changes['revs'] = xrange(0, 0)
1374 1377 tr.changes['obsmarkers'] = set()
1375 1378 tr.changes['phases'] = {}
1376 1379 tr.changes['bookmarks'] = {}
1377 1380
1378 1381 tr.hookargs['txnid'] = txnid
1379 1382 # note: writing the fncache only during finalize mean that the file is
1380 1383 # outdated when running hooks. As fncache is used for streaming clone,
1381 1384 # this is not expected to break anything that happen during the hooks.
1382 1385 tr.addfinalize('flush-fncache', self.store.write)
1383 1386 def txnclosehook(tr2):
1384 1387 """To be run if transaction is successful, will schedule a hook run
1385 1388 """
1386 1389 # Don't reference tr2 in hook() so we don't hold a reference.
1387 1390 # This reduces memory consumption when there are multiple
1388 1391 # transactions per lock. This can likely go away if issue5045
1389 1392 # fixes the function accumulation.
1390 1393 hookargs = tr2.hookargs
1391 1394
1392 1395 def hookfunc():
1393 1396 repo = reporef()
1394 1397 if hook.hashook(repo.ui, 'txnclose-bookmark'):
1395 1398 bmchanges = sorted(tr.changes['bookmarks'].items())
1396 1399 for name, (old, new) in bmchanges:
1397 1400 args = tr.hookargs.copy()
1398 1401 args.update(bookmarks.preparehookargs(name, old, new))
1399 1402 repo.hook('txnclose-bookmark', throw=False,
1400 1403 txnname=desc, **pycompat.strkwargs(args))
1401 1404
1402 1405 if hook.hashook(repo.ui, 'txnclose-phase'):
1403 1406 cl = repo.unfiltered().changelog
1404 1407 phasemv = sorted(tr.changes['phases'].items())
1405 1408 for rev, (old, new) in phasemv:
1406 1409 args = tr.hookargs.copy()
1407 1410 node = hex(cl.node(rev))
1408 1411 args.update(phases.preparehookargs(node, old, new))
1409 1412 repo.hook('txnclose-phase', throw=False, txnname=desc,
1410 1413 **pycompat.strkwargs(args))
1411 1414
1412 1415 repo.hook('txnclose', throw=False, txnname=desc,
1413 1416 **pycompat.strkwargs(hookargs))
1414 1417 reporef()._afterlock(hookfunc)
1415 1418 tr.addfinalize('txnclose-hook', txnclosehook)
1416 1419 # Include a leading "-" to make it happen before the transaction summary
1417 1420 # reports registered via scmutil.registersummarycallback() whose names
1418 1421 # are 00-txnreport etc. That way, the caches will be warm when the
1419 1422 # callbacks run.
1420 1423 tr.addpostclose('-warm-cache', self._buildcacheupdater(tr))
1421 1424 def txnaborthook(tr2):
1422 1425 """To be run if transaction is aborted
1423 1426 """
1424 1427 reporef().hook('txnabort', throw=False, txnname=desc,
1425 1428 **pycompat.strkwargs(tr2.hookargs))
1426 1429 tr.addabort('txnabort-hook', txnaborthook)
1427 1430 # avoid eager cache invalidation. in-memory data should be identical
1428 1431 # to stored data if transaction has no error.
1429 1432 tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats)
1430 1433 self._transref = weakref.ref(tr)
1431 1434 scmutil.registersummarycallback(self, tr, desc)
1432 1435 return tr
1433 1436
1434 1437 def _journalfiles(self):
1435 1438 return ((self.svfs, 'journal'),
1436 1439 (self.vfs, 'journal.dirstate'),
1437 1440 (self.vfs, 'journal.branch'),
1438 1441 (self.vfs, 'journal.desc'),
1439 1442 (self.vfs, 'journal.bookmarks'),
1440 1443 (self.svfs, 'journal.phaseroots'))
1441 1444
1442 1445 def undofiles(self):
1443 1446 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
1444 1447
1445 1448 @unfilteredmethod
1446 1449 def _writejournal(self, desc):
1447 1450 self.dirstate.savebackup(None, 'journal.dirstate')
1448 1451 self.vfs.write("journal.branch",
1449 1452 encoding.fromlocal(self.dirstate.branch()))
1450 1453 self.vfs.write("journal.desc",
1451 1454 "%d\n%s\n" % (len(self), desc))
1452 1455 self.vfs.write("journal.bookmarks",
1453 1456 self.vfs.tryread("bookmarks"))
1454 1457 self.svfs.write("journal.phaseroots",
1455 1458 self.svfs.tryread("phaseroots"))
1456 1459
1457 1460 def recover(self):
1458 1461 with self.lock():
1459 1462 if self.svfs.exists("journal"):
1460 1463 self.ui.status(_("rolling back interrupted transaction\n"))
1461 1464 vfsmap = {'': self.svfs,
1462 1465 'plain': self.vfs,}
1463 1466 transaction.rollback(self.svfs, vfsmap, "journal",
1464 1467 self.ui.warn,
1465 1468 checkambigfiles=_cachedfiles)
1466 1469 self.invalidate()
1467 1470 return True
1468 1471 else:
1469 1472 self.ui.warn(_("no interrupted transaction available\n"))
1470 1473 return False
1471 1474
1472 1475 def rollback(self, dryrun=False, force=False):
1473 1476 wlock = lock = dsguard = None
1474 1477 try:
1475 1478 wlock = self.wlock()
1476 1479 lock = self.lock()
1477 1480 if self.svfs.exists("undo"):
1478 1481 dsguard = dirstateguard.dirstateguard(self, 'rollback')
1479 1482
1480 1483 return self._rollback(dryrun, force, dsguard)
1481 1484 else:
1482 1485 self.ui.warn(_("no rollback information available\n"))
1483 1486 return 1
1484 1487 finally:
1485 1488 release(dsguard, lock, wlock)
1486 1489
1487 1490 @unfilteredmethod # Until we get smarter cache management
1488 1491 def _rollback(self, dryrun, force, dsguard):
1489 1492 ui = self.ui
1490 1493 try:
1491 1494 args = self.vfs.read('undo.desc').splitlines()
1492 1495 (oldlen, desc, detail) = (int(args[0]), args[1], None)
1493 1496 if len(args) >= 3:
1494 1497 detail = args[2]
1495 1498 oldtip = oldlen - 1
1496 1499
1497 1500 if detail and ui.verbose:
1498 1501 msg = (_('repository tip rolled back to revision %d'
1499 1502 ' (undo %s: %s)\n')
1500 1503 % (oldtip, desc, detail))
1501 1504 else:
1502 1505 msg = (_('repository tip rolled back to revision %d'
1503 1506 ' (undo %s)\n')
1504 1507 % (oldtip, desc))
1505 1508 except IOError:
1506 1509 msg = _('rolling back unknown transaction\n')
1507 1510 desc = None
1508 1511
1509 1512 if not force and self['.'] != self['tip'] and desc == 'commit':
1510 1513 raise error.Abort(
1511 1514 _('rollback of last commit while not checked out '
1512 1515 'may lose data'), hint=_('use -f to force'))
1513 1516
1514 1517 ui.status(msg)
1515 1518 if dryrun:
1516 1519 return 0
1517 1520
1518 1521 parents = self.dirstate.parents()
1519 1522 self.destroying()
1520 1523 vfsmap = {'plain': self.vfs, '': self.svfs}
1521 1524 transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
1522 1525 checkambigfiles=_cachedfiles)
1523 1526 if self.vfs.exists('undo.bookmarks'):
1524 1527 self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
1525 1528 if self.svfs.exists('undo.phaseroots'):
1526 1529 self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True)
1527 1530 self.invalidate()
1528 1531
1529 1532 parentgone = (parents[0] not in self.changelog.nodemap or
1530 1533 parents[1] not in self.changelog.nodemap)
1531 1534 if parentgone:
1532 1535 # prevent dirstateguard from overwriting already restored one
1533 1536 dsguard.close()
1534 1537
1535 1538 self.dirstate.restorebackup(None, 'undo.dirstate')
1536 1539 try:
1537 1540 branch = self.vfs.read('undo.branch')
1538 1541 self.dirstate.setbranch(encoding.tolocal(branch))
1539 1542 except IOError:
1540 1543 ui.warn(_('named branch could not be reset: '
1541 1544 'current branch is still \'%s\'\n')
1542 1545 % self.dirstate.branch())
1543 1546
1544 1547 parents = tuple([p.rev() for p in self[None].parents()])
1545 1548 if len(parents) > 1:
1546 1549 ui.status(_('working directory now based on '
1547 1550 'revisions %d and %d\n') % parents)
1548 1551 else:
1549 1552 ui.status(_('working directory now based on '
1550 1553 'revision %d\n') % parents)
1551 1554 mergemod.mergestate.clean(self, self['.'].node())
1552 1555
1553 1556 # TODO: if we know which new heads may result from this rollback, pass
1554 1557 # them to destroy(), which will prevent the branchhead cache from being
1555 1558 # invalidated.
1556 1559 self.destroyed()
1557 1560 return 0
1558 1561
1559 1562 def _buildcacheupdater(self, newtransaction):
1560 1563 """called during transaction to build the callback updating cache
1561 1564
1562 1565 Lives on the repository to help extension who might want to augment
1563 1566 this logic. For this purpose, the created transaction is passed to the
1564 1567 method.
1565 1568 """
1566 1569 # we must avoid cyclic reference between repo and transaction.
1567 1570 reporef = weakref.ref(self)
1568 1571 def updater(tr):
1569 1572 repo = reporef()
1570 1573 repo.updatecaches(tr)
1571 1574 return updater
1572 1575
1573 1576 @unfilteredmethod
1574 1577 def updatecaches(self, tr=None, full=False):
1575 1578 """warm appropriate caches
1576 1579
1577 1580 If this function is called after a transaction closed. The transaction
1578 1581 will be available in the 'tr' argument. This can be used to selectively
1579 1582 update caches relevant to the changes in that transaction.
1580 1583
1581 1584 If 'full' is set, make sure all caches the function knows about have
1582 1585 up-to-date data. Even the ones usually loaded more lazily.
1583 1586 """
1584 1587 if tr is not None and tr.hookargs.get('source') == 'strip':
1585 1588 # During strip, many caches are invalid but
1586 1589 # later call to `destroyed` will refresh them.
1587 1590 return
1588 1591
1589 1592 if tr is None or tr.changes['revs']:
1590 1593 # updating the unfiltered branchmap should refresh all the others,
1591 1594 self.ui.debug('updating the branch cache\n')
1592 1595 branchmap.updatecache(self.filtered('served'))
1593 1596
1594 1597 if full:
1595 1598 rbc = self.revbranchcache()
1596 1599 for r in self.changelog:
1597 1600 rbc.branchinfo(r)
1598 1601 rbc.write()
1599 1602
1600 1603 def invalidatecaches(self):
1601 1604
1602 1605 if '_tagscache' in vars(self):
1603 1606 # can't use delattr on proxy
1604 1607 del self.__dict__['_tagscache']
1605 1608
1606 1609 self.unfiltered()._branchcaches.clear()
1607 1610 self.invalidatevolatilesets()
1608 1611 self._sparsesignaturecache.clear()
1609 1612
1610 1613 def invalidatevolatilesets(self):
1611 1614 self.filteredrevcache.clear()
1612 1615 obsolete.clearobscaches(self)
1613 1616
1614 1617 def invalidatedirstate(self):
1615 1618 '''Invalidates the dirstate, causing the next call to dirstate
1616 1619 to check if it was modified since the last time it was read,
1617 1620 rereading it if it has.
1618 1621
1619 1622 This is different to dirstate.invalidate() that it doesn't always
1620 1623 rereads the dirstate. Use dirstate.invalidate() if you want to
1621 1624 explicitly read the dirstate again (i.e. restoring it to a previous
1622 1625 known good state).'''
1623 1626 if hasunfilteredcache(self, 'dirstate'):
1624 1627 for k in self.dirstate._filecache:
1625 1628 try:
1626 1629 delattr(self.dirstate, k)
1627 1630 except AttributeError:
1628 1631 pass
1629 1632 delattr(self.unfiltered(), 'dirstate')
1630 1633
1631 1634 def invalidate(self, clearfilecache=False):
1632 1635 '''Invalidates both store and non-store parts other than dirstate
1633 1636
1634 1637 If a transaction is running, invalidation of store is omitted,
1635 1638 because discarding in-memory changes might cause inconsistency
1636 1639 (e.g. incomplete fncache causes unintentional failure, but
1637 1640 redundant one doesn't).
1638 1641 '''
1639 1642 unfiltered = self.unfiltered() # all file caches are stored unfiltered
1640 1643 for k in list(self._filecache.keys()):
1641 1644 # dirstate is invalidated separately in invalidatedirstate()
1642 1645 if k == 'dirstate':
1643 1646 continue
1644 1647 if (k == 'changelog' and
1645 1648 self.currenttransaction() and
1646 1649 self.changelog._delayed):
1647 1650 # The changelog object may store unwritten revisions. We don't
1648 1651 # want to lose them.
1649 1652 # TODO: Solve the problem instead of working around it.
1650 1653 continue
1651 1654
1652 1655 if clearfilecache:
1653 1656 del self._filecache[k]
1654 1657 try:
1655 1658 delattr(unfiltered, k)
1656 1659 except AttributeError:
1657 1660 pass
1658 1661 self.invalidatecaches()
1659 1662 if not self.currenttransaction():
1660 1663 # TODO: Changing contents of store outside transaction
1661 1664 # causes inconsistency. We should make in-memory store
1662 1665 # changes detectable, and abort if changed.
1663 1666 self.store.invalidatecaches()
1664 1667
1665 1668 def invalidateall(self):
1666 1669 '''Fully invalidates both store and non-store parts, causing the
1667 1670 subsequent operation to reread any outside changes.'''
1668 1671 # extension should hook this to invalidate its caches
1669 1672 self.invalidate()
1670 1673 self.invalidatedirstate()
1671 1674
1672 1675 @unfilteredmethod
1673 1676 def _refreshfilecachestats(self, tr):
1674 1677 """Reload stats of cached files so that they are flagged as valid"""
1675 1678 for k, ce in self._filecache.items():
1676 1679 k = pycompat.sysstr(k)
1677 1680 if k == r'dirstate' or k not in self.__dict__:
1678 1681 continue
1679 1682 ce.refresh()
1680 1683
1681 1684 def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc,
1682 1685 inheritchecker=None, parentenvvar=None):
1683 1686 parentlock = None
1684 1687 # the contents of parentenvvar are used by the underlying lock to
1685 1688 # determine whether it can be inherited
1686 1689 if parentenvvar is not None:
1687 1690 parentlock = encoding.environ.get(parentenvvar)
1688 1691
1689 1692 timeout = 0
1690 1693 warntimeout = 0
1691 1694 if wait:
1692 1695 timeout = self.ui.configint("ui", "timeout")
1693 1696 warntimeout = self.ui.configint("ui", "timeout.warn")
1694 1697
1695 1698 l = lockmod.trylock(self.ui, vfs, lockname, timeout, warntimeout,
1696 1699 releasefn=releasefn,
1697 1700 acquirefn=acquirefn, desc=desc,
1698 1701 inheritchecker=inheritchecker,
1699 1702 parentlock=parentlock)
1700 1703 return l
1701 1704
1702 1705 def _afterlock(self, callback):
1703 1706 """add a callback to be run when the repository is fully unlocked
1704 1707
1705 1708 The callback will be executed when the outermost lock is released
1706 1709 (with wlock being higher level than 'lock')."""
1707 1710 for ref in (self._wlockref, self._lockref):
1708 1711 l = ref and ref()
1709 1712 if l and l.held:
1710 1713 l.postrelease.append(callback)
1711 1714 break
1712 1715 else: # no lock have been found.
1713 1716 callback()
1714 1717
1715 1718 def lock(self, wait=True):
1716 1719 '''Lock the repository store (.hg/store) and return a weak reference
1717 1720 to the lock. Use this before modifying the store (e.g. committing or
1718 1721 stripping). If you are opening a transaction, get a lock as well.)
1719 1722
1720 1723 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1721 1724 'wlock' first to avoid a dead-lock hazard.'''
1722 1725 l = self._currentlock(self._lockref)
1723 1726 if l is not None:
1724 1727 l.lock()
1725 1728 return l
1726 1729
1727 1730 l = self._lock(self.svfs, "lock", wait, None,
1728 1731 self.invalidate, _('repository %s') % self.origroot)
1729 1732 self._lockref = weakref.ref(l)
1730 1733 return l
1731 1734
1732 1735 def _wlockchecktransaction(self):
1733 1736 if self.currenttransaction() is not None:
1734 1737 raise error.LockInheritanceContractViolation(
1735 1738 'wlock cannot be inherited in the middle of a transaction')
1736 1739
1737 1740 def wlock(self, wait=True):
1738 1741 '''Lock the non-store parts of the repository (everything under
1739 1742 .hg except .hg/store) and return a weak reference to the lock.
1740 1743
1741 1744 Use this before modifying files in .hg.
1742 1745
1743 1746 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1744 1747 'wlock' first to avoid a dead-lock hazard.'''
1745 1748 l = self._wlockref and self._wlockref()
1746 1749 if l is not None and l.held:
1747 1750 l.lock()
1748 1751 return l
1749 1752
1750 1753 # We do not need to check for non-waiting lock acquisition. Such
1751 1754 # acquisition would not cause dead-lock as they would just fail.
1752 1755 if wait and (self.ui.configbool('devel', 'all-warnings')
1753 1756 or self.ui.configbool('devel', 'check-locks')):
1754 1757 if self._currentlock(self._lockref) is not None:
1755 1758 self.ui.develwarn('"wlock" acquired after "lock"')
1756 1759
1757 1760 def unlock():
1758 1761 if self.dirstate.pendingparentchange():
1759 1762 self.dirstate.invalidate()
1760 1763 else:
1761 1764 self.dirstate.write(None)
1762 1765
1763 1766 self._filecache['dirstate'].refresh()
1764 1767
1765 1768 l = self._lock(self.vfs, "wlock", wait, unlock,
1766 1769 self.invalidatedirstate, _('working directory of %s') %
1767 1770 self.origroot,
1768 1771 inheritchecker=self._wlockchecktransaction,
1769 1772 parentenvvar='HG_WLOCK_LOCKER')
1770 1773 self._wlockref = weakref.ref(l)
1771 1774 return l
1772 1775
1773 1776 def _currentlock(self, lockref):
1774 1777 """Returns the lock if it's held, or None if it's not."""
1775 1778 if lockref is None:
1776 1779 return None
1777 1780 l = lockref()
1778 1781 if l is None or not l.held:
1779 1782 return None
1780 1783 return l
1781 1784
1782 1785 def currentwlock(self):
1783 1786 """Returns the wlock if it's held, or None if it's not."""
1784 1787 return self._currentlock(self._wlockref)
1785 1788
1786 1789 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
1787 1790 """
1788 1791 commit an individual file as part of a larger transaction
1789 1792 """
1790 1793
1791 1794 fname = fctx.path()
1792 1795 fparent1 = manifest1.get(fname, nullid)
1793 1796 fparent2 = manifest2.get(fname, nullid)
1794 1797 if isinstance(fctx, context.filectx):
1795 1798 node = fctx.filenode()
1796 1799 if node in [fparent1, fparent2]:
1797 1800 self.ui.debug('reusing %s filelog entry\n' % fname)
1798 1801 if manifest1.flags(fname) != fctx.flags():
1799 1802 changelist.append(fname)
1800 1803 return node
1801 1804
1802 1805 flog = self.file(fname)
1803 1806 meta = {}
1804 1807 copy = fctx.renamed()
1805 1808 if copy and copy[0] != fname:
1806 1809 # Mark the new revision of this file as a copy of another
1807 1810 # file. This copy data will effectively act as a parent
1808 1811 # of this new revision. If this is a merge, the first
1809 1812 # parent will be the nullid (meaning "look up the copy data")
1810 1813 # and the second one will be the other parent. For example:
1811 1814 #
1812 1815 # 0 --- 1 --- 3 rev1 changes file foo
1813 1816 # \ / rev2 renames foo to bar and changes it
1814 1817 # \- 2 -/ rev3 should have bar with all changes and
1815 1818 # should record that bar descends from
1816 1819 # bar in rev2 and foo in rev1
1817 1820 #
1818 1821 # this allows this merge to succeed:
1819 1822 #
1820 1823 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1821 1824 # \ / merging rev3 and rev4 should use bar@rev2
1822 1825 # \- 2 --- 4 as the merge base
1823 1826 #
1824 1827
1825 1828 cfname = copy[0]
1826 1829 crev = manifest1.get(cfname)
1827 1830 newfparent = fparent2
1828 1831
1829 1832 if manifest2: # branch merge
1830 1833 if fparent2 == nullid or crev is None: # copied on remote side
1831 1834 if cfname in manifest2:
1832 1835 crev = manifest2[cfname]
1833 1836 newfparent = fparent1
1834 1837
1835 1838 # Here, we used to search backwards through history to try to find
1836 1839 # where the file copy came from if the source of a copy was not in
1837 1840 # the parent directory. However, this doesn't actually make sense to
1838 1841 # do (what does a copy from something not in your working copy even
1839 1842 # mean?) and it causes bugs (eg, issue4476). Instead, we will warn
1840 1843 # the user that copy information was dropped, so if they didn't
1841 1844 # expect this outcome it can be fixed, but this is the correct
1842 1845 # behavior in this circumstance.
1843 1846
1844 1847 if crev:
1845 1848 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1846 1849 meta["copy"] = cfname
1847 1850 meta["copyrev"] = hex(crev)
1848 1851 fparent1, fparent2 = nullid, newfparent
1849 1852 else:
1850 1853 self.ui.warn(_("warning: can't find ancestor for '%s' "
1851 1854 "copied from '%s'!\n") % (fname, cfname))
1852 1855
1853 1856 elif fparent1 == nullid:
1854 1857 fparent1, fparent2 = fparent2, nullid
1855 1858 elif fparent2 != nullid:
1856 1859 # is one parent an ancestor of the other?
1857 1860 fparentancestors = flog.commonancestorsheads(fparent1, fparent2)
1858 1861 if fparent1 in fparentancestors:
1859 1862 fparent1, fparent2 = fparent2, nullid
1860 1863 elif fparent2 in fparentancestors:
1861 1864 fparent2 = nullid
1862 1865
1863 1866 # is the file changed?
1864 1867 text = fctx.data()
1865 1868 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1866 1869 changelist.append(fname)
1867 1870 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1868 1871 # are just the flags changed during merge?
1869 1872 elif fname in manifest1 and manifest1.flags(fname) != fctx.flags():
1870 1873 changelist.append(fname)
1871 1874
1872 1875 return fparent1
1873 1876
1874 1877 def checkcommitpatterns(self, wctx, vdirs, match, status, fail):
1875 1878 """check for commit arguments that aren't committable"""
1876 1879 if match.isexact() or match.prefix():
1877 1880 matched = set(status.modified + status.added + status.removed)
1878 1881
1879 1882 for f in match.files():
1880 1883 f = self.dirstate.normalize(f)
1881 1884 if f == '.' or f in matched or f in wctx.substate:
1882 1885 continue
1883 1886 if f in status.deleted:
1884 1887 fail(f, _('file not found!'))
1885 1888 if f in vdirs: # visited directory
1886 1889 d = f + '/'
1887 1890 for mf in matched:
1888 1891 if mf.startswith(d):
1889 1892 break
1890 1893 else:
1891 1894 fail(f, _("no match under directory!"))
1892 1895 elif f not in self.dirstate:
1893 1896 fail(f, _("file not tracked!"))
1894 1897
1895 1898 @unfilteredmethod
1896 1899 def commit(self, text="", user=None, date=None, match=None, force=False,
1897 1900 editor=False, extra=None):
1898 1901 """Add a new revision to current repository.
1899 1902
1900 1903 Revision information is gathered from the working directory,
1901 1904 match can be used to filter the committed files. If editor is
1902 1905 supplied, it is called to get a commit message.
1903 1906 """
1904 1907 if extra is None:
1905 1908 extra = {}
1906 1909
1907 1910 def fail(f, msg):
1908 1911 raise error.Abort('%s: %s' % (f, msg))
1909 1912
1910 1913 if not match:
1911 1914 match = matchmod.always(self.root, '')
1912 1915
1913 1916 if not force:
1914 1917 vdirs = []
1915 1918 match.explicitdir = vdirs.append
1916 1919 match.bad = fail
1917 1920
1918 1921 wlock = lock = tr = None
1919 1922 try:
1920 1923 wlock = self.wlock()
1921 1924 lock = self.lock() # for recent changelog (see issue4368)
1922 1925
1923 1926 wctx = self[None]
1924 1927 merge = len(wctx.parents()) > 1
1925 1928
1926 1929 if not force and merge and not match.always():
1927 1930 raise error.Abort(_('cannot partially commit a merge '
1928 1931 '(do not specify files or patterns)'))
1929 1932
1930 1933 status = self.status(match=match, clean=force)
1931 1934 if force:
1932 1935 status.modified.extend(status.clean) # mq may commit clean files
1933 1936
1934 1937 # check subrepos
1935 1938 subs, commitsubs, newstate = subrepoutil.precommit(
1936 1939 self.ui, wctx, status, match, force=force)
1937 1940
1938 1941 # make sure all explicit patterns are matched
1939 1942 if not force:
1940 1943 self.checkcommitpatterns(wctx, vdirs, match, status, fail)
1941 1944
1942 1945 cctx = context.workingcommitctx(self, status,
1943 1946 text, user, date, extra)
1944 1947
1945 1948 # internal config: ui.allowemptycommit
1946 1949 allowemptycommit = (wctx.branch() != wctx.p1().branch()
1947 1950 or extra.get('close') or merge or cctx.files()
1948 1951 or self.ui.configbool('ui', 'allowemptycommit'))
1949 1952 if not allowemptycommit:
1950 1953 return None
1951 1954
1952 1955 if merge and cctx.deleted():
1953 1956 raise error.Abort(_("cannot commit merge with missing files"))
1954 1957
1955 1958 ms = mergemod.mergestate.read(self)
1956 1959 mergeutil.checkunresolved(ms)
1957 1960
1958 1961 if editor:
1959 1962 cctx._text = editor(self, cctx, subs)
1960 1963 edited = (text != cctx._text)
1961 1964
1962 1965 # Save commit message in case this transaction gets rolled back
1963 1966 # (e.g. by a pretxncommit hook). Leave the content alone on
1964 1967 # the assumption that the user will use the same editor again.
1965 1968 msgfn = self.savecommitmessage(cctx._text)
1966 1969
1967 1970 # commit subs and write new state
1968 1971 if subs:
1969 1972 for s in sorted(commitsubs):
1970 1973 sub = wctx.sub(s)
1971 1974 self.ui.status(_('committing subrepository %s\n') %
1972 1975 subrepoutil.subrelpath(sub))
1973 1976 sr = sub.commit(cctx._text, user, date)
1974 1977 newstate[s] = (newstate[s][0], sr)
1975 1978 subrepoutil.writestate(self, newstate)
1976 1979
1977 1980 p1, p2 = self.dirstate.parents()
1978 1981 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1979 1982 try:
1980 1983 self.hook("precommit", throw=True, parent1=hookp1,
1981 1984 parent2=hookp2)
1982 1985 tr = self.transaction('commit')
1983 1986 ret = self.commitctx(cctx, True)
1984 1987 except: # re-raises
1985 1988 if edited:
1986 1989 self.ui.write(
1987 1990 _('note: commit message saved in %s\n') % msgfn)
1988 1991 raise
1989 1992 # update bookmarks, dirstate and mergestate
1990 1993 bookmarks.update(self, [p1, p2], ret)
1991 1994 cctx.markcommitted(ret)
1992 1995 ms.reset()
1993 1996 tr.close()
1994 1997
1995 1998 finally:
1996 1999 lockmod.release(tr, lock, wlock)
1997 2000
1998 2001 def commithook(node=hex(ret), parent1=hookp1, parent2=hookp2):
1999 2002 # hack for command that use a temporary commit (eg: histedit)
2000 2003 # temporary commit got stripped before hook release
2001 2004 if self.changelog.hasnode(ret):
2002 2005 self.hook("commit", node=node, parent1=parent1,
2003 2006 parent2=parent2)
2004 2007 self._afterlock(commithook)
2005 2008 return ret
2006 2009
2007 2010 @unfilteredmethod
2008 2011 def commitctx(self, ctx, error=False):
2009 2012 """Add a new revision to current repository.
2010 2013 Revision information is passed via the context argument.
2011 2014 """
2012 2015
2013 2016 tr = None
2014 2017 p1, p2 = ctx.p1(), ctx.p2()
2015 2018 user = ctx.user()
2016 2019
2017 2020 lock = self.lock()
2018 2021 try:
2019 2022 tr = self.transaction("commit")
2020 2023 trp = weakref.proxy(tr)
2021 2024
2022 2025 if ctx.manifestnode():
2023 2026 # reuse an existing manifest revision
2024 2027 mn = ctx.manifestnode()
2025 2028 files = ctx.files()
2026 2029 elif ctx.files():
2027 2030 m1ctx = p1.manifestctx()
2028 2031 m2ctx = p2.manifestctx()
2029 2032 mctx = m1ctx.copy()
2030 2033
2031 2034 m = mctx.read()
2032 2035 m1 = m1ctx.read()
2033 2036 m2 = m2ctx.read()
2034 2037
2035 2038 # check in files
2036 2039 added = []
2037 2040 changed = []
2038 2041 removed = list(ctx.removed())
2039 2042 linkrev = len(self)
2040 2043 self.ui.note(_("committing files:\n"))
2041 2044 for f in sorted(ctx.modified() + ctx.added()):
2042 2045 self.ui.note(f + "\n")
2043 2046 try:
2044 2047 fctx = ctx[f]
2045 2048 if fctx is None:
2046 2049 removed.append(f)
2047 2050 else:
2048 2051 added.append(f)
2049 2052 m[f] = self._filecommit(fctx, m1, m2, linkrev,
2050 2053 trp, changed)
2051 2054 m.setflag(f, fctx.flags())
2052 2055 except OSError as inst:
2053 2056 self.ui.warn(_("trouble committing %s!\n") % f)
2054 2057 raise
2055 2058 except IOError as inst:
2056 2059 errcode = getattr(inst, 'errno', errno.ENOENT)
2057 2060 if error or errcode and errcode != errno.ENOENT:
2058 2061 self.ui.warn(_("trouble committing %s!\n") % f)
2059 2062 raise
2060 2063
2061 2064 # update manifest
2062 2065 self.ui.note(_("committing manifest\n"))
2063 2066 removed = [f for f in sorted(removed) if f in m1 or f in m2]
2064 2067 drop = [f for f in removed if f in m]
2065 2068 for f in drop:
2066 2069 del m[f]
2067 2070 mn = mctx.write(trp, linkrev,
2068 2071 p1.manifestnode(), p2.manifestnode(),
2069 2072 added, drop)
2070 2073 files = changed + removed
2071 2074 else:
2072 2075 mn = p1.manifestnode()
2073 2076 files = []
2074 2077
2075 2078 # update changelog
2076 2079 self.ui.note(_("committing changelog\n"))
2077 2080 self.changelog.delayupdate(tr)
2078 2081 n = self.changelog.add(mn, files, ctx.description(),
2079 2082 trp, p1.node(), p2.node(),
2080 2083 user, ctx.date(), ctx.extra().copy())
2081 2084 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
2082 2085 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
2083 2086 parent2=xp2)
2084 2087 # set the new commit is proper phase
2085 2088 targetphase = subrepoutil.newcommitphase(self.ui, ctx)
2086 2089 if targetphase:
2087 2090 # retract boundary do not alter parent changeset.
2088 2091 # if a parent have higher the resulting phase will
2089 2092 # be compliant anyway
2090 2093 #
2091 2094 # if minimal phase was 0 we don't need to retract anything
2092 2095 phases.registernew(self, tr, targetphase, [n])
2093 2096 tr.close()
2094 2097 return n
2095 2098 finally:
2096 2099 if tr:
2097 2100 tr.release()
2098 2101 lock.release()
2099 2102
2100 2103 @unfilteredmethod
2101 2104 def destroying(self):
2102 2105 '''Inform the repository that nodes are about to be destroyed.
2103 2106 Intended for use by strip and rollback, so there's a common
2104 2107 place for anything that has to be done before destroying history.
2105 2108
2106 2109 This is mostly useful for saving state that is in memory and waiting
2107 2110 to be flushed when the current lock is released. Because a call to
2108 2111 destroyed is imminent, the repo will be invalidated causing those
2109 2112 changes to stay in memory (waiting for the next unlock), or vanish
2110 2113 completely.
2111 2114 '''
2112 2115 # When using the same lock to commit and strip, the phasecache is left
2113 2116 # dirty after committing. Then when we strip, the repo is invalidated,
2114 2117 # causing those changes to disappear.
2115 2118 if '_phasecache' in vars(self):
2116 2119 self._phasecache.write()
2117 2120
2118 2121 @unfilteredmethod
2119 2122 def destroyed(self):
2120 2123 '''Inform the repository that nodes have been destroyed.
2121 2124 Intended for use by strip and rollback, so there's a common
2122 2125 place for anything that has to be done after destroying history.
2123 2126 '''
2124 2127 # When one tries to:
2125 2128 # 1) destroy nodes thus calling this method (e.g. strip)
2126 2129 # 2) use phasecache somewhere (e.g. commit)
2127 2130 #
2128 2131 # then 2) will fail because the phasecache contains nodes that were
2129 2132 # removed. We can either remove phasecache from the filecache,
2130 2133 # causing it to reload next time it is accessed, or simply filter
2131 2134 # the removed nodes now and write the updated cache.
2132 2135 self._phasecache.filterunknown(self)
2133 2136 self._phasecache.write()
2134 2137
2135 2138 # refresh all repository caches
2136 2139 self.updatecaches()
2137 2140
2138 2141 # Ensure the persistent tag cache is updated. Doing it now
2139 2142 # means that the tag cache only has to worry about destroyed
2140 2143 # heads immediately after a strip/rollback. That in turn
2141 2144 # guarantees that "cachetip == currenttip" (comparing both rev
2142 2145 # and node) always means no nodes have been added or destroyed.
2143 2146
2144 2147 # XXX this is suboptimal when qrefresh'ing: we strip the current
2145 2148 # head, refresh the tag cache, then immediately add a new head.
2146 2149 # But I think doing it this way is necessary for the "instant
2147 2150 # tag cache retrieval" case to work.
2148 2151 self.invalidate()
2149 2152
2150 2153 def status(self, node1='.', node2=None, match=None,
2151 2154 ignored=False, clean=False, unknown=False,
2152 2155 listsubrepos=False):
2153 2156 '''a convenience method that calls node1.status(node2)'''
2154 2157 return self[node1].status(node2, match, ignored, clean, unknown,
2155 2158 listsubrepos)
2156 2159
2157 2160 def addpostdsstatus(self, ps):
2158 2161 """Add a callback to run within the wlock, at the point at which status
2159 2162 fixups happen.
2160 2163
2161 2164 On status completion, callback(wctx, status) will be called with the
2162 2165 wlock held, unless the dirstate has changed from underneath or the wlock
2163 2166 couldn't be grabbed.
2164 2167
2165 2168 Callbacks should not capture and use a cached copy of the dirstate --
2166 2169 it might change in the meanwhile. Instead, they should access the
2167 2170 dirstate via wctx.repo().dirstate.
2168 2171
2169 2172 This list is emptied out after each status run -- extensions should
2170 2173 make sure it adds to this list each time dirstate.status is called.
2171 2174 Extensions should also make sure they don't call this for statuses
2172 2175 that don't involve the dirstate.
2173 2176 """
2174 2177
2175 2178 # The list is located here for uniqueness reasons -- it is actually
2176 2179 # managed by the workingctx, but that isn't unique per-repo.
2177 2180 self._postdsstatus.append(ps)
2178 2181
2179 2182 def postdsstatus(self):
2180 2183 """Used by workingctx to get the list of post-dirstate-status hooks."""
2181 2184 return self._postdsstatus
2182 2185
2183 2186 def clearpostdsstatus(self):
2184 2187 """Used by workingctx to clear post-dirstate-status hooks."""
2185 2188 del self._postdsstatus[:]
2186 2189
2187 2190 def heads(self, start=None):
2188 2191 if start is None:
2189 2192 cl = self.changelog
2190 2193 headrevs = reversed(cl.headrevs())
2191 2194 return [cl.node(rev) for rev in headrevs]
2192 2195
2193 2196 heads = self.changelog.heads(start)
2194 2197 # sort the output in rev descending order
2195 2198 return sorted(heads, key=self.changelog.rev, reverse=True)
2196 2199
2197 2200 def branchheads(self, branch=None, start=None, closed=False):
2198 2201 '''return a (possibly filtered) list of heads for the given branch
2199 2202
2200 2203 Heads are returned in topological order, from newest to oldest.
2201 2204 If branch is None, use the dirstate branch.
2202 2205 If start is not None, return only heads reachable from start.
2203 2206 If closed is True, return heads that are marked as closed as well.
2204 2207 '''
2205 2208 if branch is None:
2206 2209 branch = self[None].branch()
2207 2210 branches = self.branchmap()
2208 2211 if branch not in branches:
2209 2212 return []
2210 2213 # the cache returns heads ordered lowest to highest
2211 2214 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
2212 2215 if start is not None:
2213 2216 # filter out the heads that cannot be reached from startrev
2214 2217 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
2215 2218 bheads = [h for h in bheads if h in fbheads]
2216 2219 return bheads
2217 2220
2218 2221 def branches(self, nodes):
2219 2222 if not nodes:
2220 2223 nodes = [self.changelog.tip()]
2221 2224 b = []
2222 2225 for n in nodes:
2223 2226 t = n
2224 2227 while True:
2225 2228 p = self.changelog.parents(n)
2226 2229 if p[1] != nullid or p[0] == nullid:
2227 2230 b.append((t, n, p[0], p[1]))
2228 2231 break
2229 2232 n = p[0]
2230 2233 return b
2231 2234
2232 2235 def between(self, pairs):
2233 2236 r = []
2234 2237
2235 2238 for top, bottom in pairs:
2236 2239 n, l, i = top, [], 0
2237 2240 f = 1
2238 2241
2239 2242 while n != bottom and n != nullid:
2240 2243 p = self.changelog.parents(n)[0]
2241 2244 if i == f:
2242 2245 l.append(n)
2243 2246 f = f * 2
2244 2247 n = p
2245 2248 i += 1
2246 2249
2247 2250 r.append(l)
2248 2251
2249 2252 return r
2250 2253
2251 2254 def checkpush(self, pushop):
2252 2255 """Extensions can override this function if additional checks have
2253 2256 to be performed before pushing, or call it if they override push
2254 2257 command.
2255 2258 """
2256 2259
2257 2260 @unfilteredpropertycache
2258 2261 def prepushoutgoinghooks(self):
2259 2262 """Return util.hooks consists of a pushop with repo, remote, outgoing
2260 2263 methods, which are called before pushing changesets.
2261 2264 """
2262 2265 return util.hooks()
2263 2266
2264 2267 def pushkey(self, namespace, key, old, new):
2265 2268 try:
2266 2269 tr = self.currenttransaction()
2267 2270 hookargs = {}
2268 2271 if tr is not None:
2269 2272 hookargs.update(tr.hookargs)
2270 2273 hookargs = pycompat.strkwargs(hookargs)
2271 2274 hookargs[r'namespace'] = namespace
2272 2275 hookargs[r'key'] = key
2273 2276 hookargs[r'old'] = old
2274 2277 hookargs[r'new'] = new
2275 2278 self.hook('prepushkey', throw=True, **hookargs)
2276 2279 except error.HookAbort as exc:
2277 2280 self.ui.write_err(_("pushkey-abort: %s\n") % exc)
2278 2281 if exc.hint:
2279 2282 self.ui.write_err(_("(%s)\n") % exc.hint)
2280 2283 return False
2281 2284 self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key))
2282 2285 ret = pushkey.push(self, namespace, key, old, new)
2283 2286 def runhook():
2284 2287 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2285 2288 ret=ret)
2286 2289 self._afterlock(runhook)
2287 2290 return ret
2288 2291
2289 2292 def listkeys(self, namespace):
2290 2293 self.hook('prelistkeys', throw=True, namespace=namespace)
2291 2294 self.ui.debug('listing keys for "%s"\n' % namespace)
2292 2295 values = pushkey.list(self, namespace)
2293 2296 self.hook('listkeys', namespace=namespace, values=values)
2294 2297 return values
2295 2298
2296 2299 def debugwireargs(self, one, two, three=None, four=None, five=None):
2297 2300 '''used to test argument passing over the wire'''
2298 2301 return "%s %s %s %s %s" % (one, two, pycompat.bytestr(three),
2299 2302 pycompat.bytestr(four),
2300 2303 pycompat.bytestr(five))
2301 2304
2302 2305 def savecommitmessage(self, text):
2303 2306 fp = self.vfs('last-message.txt', 'wb')
2304 2307 try:
2305 2308 fp.write(text)
2306 2309 finally:
2307 2310 fp.close()
2308 2311 return self.pathto(fp.name[len(self.root) + 1:])
2309 2312
2310 2313 # used to avoid circular references so destructors work
2311 2314 def aftertrans(files):
2312 2315 renamefiles = [tuple(t) for t in files]
2313 2316 def a():
2314 2317 for vfs, src, dest in renamefiles:
2315 2318 # if src and dest refer to a same file, vfs.rename is a no-op,
2316 2319 # leaving both src and dest on disk. delete dest to make sure
2317 2320 # the rename couldn't be such a no-op.
2318 2321 vfs.tryunlink(dest)
2319 2322 try:
2320 2323 vfs.rename(src, dest)
2321 2324 except OSError: # journal file does not yet exist
2322 2325 pass
2323 2326 return a
2324 2327
2325 2328 def undoname(fn):
2326 2329 base, name = os.path.split(fn)
2327 2330 assert name.startswith('journal')
2328 2331 return os.path.join(base, name.replace('journal', 'undo', 1))
2329 2332
2330 2333 def instance(ui, path, create):
2331 2334 return localrepository(ui, util.urllocalpath(path), create)
2332 2335
2333 2336 def islocal(path):
2334 2337 return True
2335 2338
2336 2339 def newreporequirements(repo):
2337 2340 """Determine the set of requirements for a new local repository.
2338 2341
2339 2342 Extensions can wrap this function to specify custom requirements for
2340 2343 new repositories.
2341 2344 """
2342 2345 ui = repo.ui
2343 2346 requirements = {'revlogv1'}
2344 2347 if ui.configbool('format', 'usestore'):
2345 2348 requirements.add('store')
2346 2349 if ui.configbool('format', 'usefncache'):
2347 2350 requirements.add('fncache')
2348 2351 if ui.configbool('format', 'dotencode'):
2349 2352 requirements.add('dotencode')
2350 2353
2351 2354 compengine = ui.config('experimental', 'format.compression')
2352 2355 if compengine not in util.compengines:
2353 2356 raise error.Abort(_('compression engine %s defined by '
2354 2357 'experimental.format.compression not available') %
2355 2358 compengine,
2356 2359 hint=_('run "hg debuginstall" to list available '
2357 2360 'compression engines'))
2358 2361
2359 2362 # zlib is the historical default and doesn't need an explicit requirement.
2360 2363 if compengine != 'zlib':
2361 2364 requirements.add('exp-compression-%s' % compengine)
2362 2365
2363 2366 if scmutil.gdinitconfig(ui):
2364 2367 requirements.add('generaldelta')
2365 2368 if ui.configbool('experimental', 'treemanifest'):
2366 2369 requirements.add('treemanifest')
2367 2370
2368 2371 revlogv2 = ui.config('experimental', 'revlogv2')
2369 2372 if revlogv2 == 'enable-unstable-format-and-corrupt-my-data':
2370 2373 requirements.remove('revlogv1')
2371 2374 # generaldelta is implied by revlogv2.
2372 2375 requirements.discard('generaldelta')
2373 2376 requirements.add(REVLOGV2_REQUIREMENT)
2374 2377
2375 2378 return requirements
@@ -1,989 +1,995 b''
1 1 # repository.py - Interfaces and base classes for repositories and peers.
2 2 #
3 3 # Copyright 2017 Gregory Szorc <gregory.szorc@gmail.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 from .i18n import _
11 11 from .thirdparty.zope import (
12 12 interface as zi,
13 13 )
14 14 from . import (
15 15 error,
16 16 )
17 17
18 18 class ipeerconnection(zi.Interface):
19 19 """Represents a "connection" to a repository.
20 20
21 21 This is the base interface for representing a connection to a repository.
22 22 It holds basic properties and methods applicable to all peer types.
23 23
24 24 This is not a complete interface definition and should not be used
25 25 outside of this module.
26 26 """
27 27 ui = zi.Attribute("""ui.ui instance""")
28 28
29 29 def url():
30 30 """Returns a URL string representing this peer.
31 31
32 32 Currently, implementations expose the raw URL used to construct the
33 33 instance. It may contain credentials as part of the URL. The
34 34 expectations of the value aren't well-defined and this could lead to
35 35 data leakage.
36 36
37 37 TODO audit/clean consumers and more clearly define the contents of this
38 38 value.
39 39 """
40 40
41 41 def local():
42 42 """Returns a local repository instance.
43 43
44 44 If the peer represents a local repository, returns an object that
45 45 can be used to interface with it. Otherwise returns ``None``.
46 46 """
47 47
48 48 def peer():
49 49 """Returns an object conforming to this interface.
50 50
51 51 Most implementations will ``return self``.
52 52 """
53 53
54 54 def canpush():
55 55 """Returns a boolean indicating if this peer can be pushed to."""
56 56
57 57 def close():
58 58 """Close the connection to this peer.
59 59
60 60 This is called when the peer will no longer be used. Resources
61 61 associated with the peer should be cleaned up.
62 62 """
63 63
64 64 class ipeercapabilities(zi.Interface):
65 65 """Peer sub-interface related to capabilities."""
66 66
67 67 def capable(name):
68 68 """Determine support for a named capability.
69 69
70 70 Returns ``False`` if capability not supported.
71 71
72 72 Returns ``True`` if boolean capability is supported. Returns a string
73 73 if capability support is non-boolean.
74 74
75 75 Capability strings may or may not map to wire protocol capabilities.
76 76 """
77 77
78 78 def requirecap(name, purpose):
79 79 """Require a capability to be present.
80 80
81 81 Raises a ``CapabilityError`` if the capability isn't present.
82 82 """
83 83
84 84 class ipeercommands(zi.Interface):
85 85 """Client-side interface for communicating over the wire protocol.
86 86
87 87 This interface is used as a gateway to the Mercurial wire protocol.
88 88 methods commonly call wire protocol commands of the same name.
89 89 """
90 90
91 91 def branchmap():
92 92 """Obtain heads in named branches.
93 93
94 94 Returns a dict mapping branch name to an iterable of nodes that are
95 95 heads on that branch.
96 96 """
97 97
98 98 def capabilities():
99 99 """Obtain capabilities of the peer.
100 100
101 101 Returns a set of string capabilities.
102 102 """
103 103
104 def clonebundles():
105 """Obtains the clone bundles manifest for the repo.
106
107 Returns the manifest as unparsed bytes.
108 """
109
104 110 def debugwireargs(one, two, three=None, four=None, five=None):
105 111 """Used to facilitate debugging of arguments passed over the wire."""
106 112
107 113 def getbundle(source, **kwargs):
108 114 """Obtain remote repository data as a bundle.
109 115
110 116 This command is how the bulk of repository data is transferred from
111 117 the peer to the local repository
112 118
113 119 Returns a generator of bundle data.
114 120 """
115 121
116 122 def heads():
117 123 """Determine all known head revisions in the peer.
118 124
119 125 Returns an iterable of binary nodes.
120 126 """
121 127
122 128 def known(nodes):
123 129 """Determine whether multiple nodes are known.
124 130
125 131 Accepts an iterable of nodes whose presence to check for.
126 132
127 133 Returns an iterable of booleans indicating of the corresponding node
128 134 at that index is known to the peer.
129 135 """
130 136
131 137 def listkeys(namespace):
132 138 """Obtain all keys in a pushkey namespace.
133 139
134 140 Returns an iterable of key names.
135 141 """
136 142
137 143 def lookup(key):
138 144 """Resolve a value to a known revision.
139 145
140 146 Returns a binary node of the resolved revision on success.
141 147 """
142 148
143 149 def pushkey(namespace, key, old, new):
144 150 """Set a value using the ``pushkey`` protocol.
145 151
146 152 Arguments correspond to the pushkey namespace and key to operate on and
147 153 the old and new values for that key.
148 154
149 155 Returns a string with the peer result. The value inside varies by the
150 156 namespace.
151 157 """
152 158
153 159 def stream_out():
154 160 """Obtain streaming clone data.
155 161
156 162 Successful result should be a generator of data chunks.
157 163 """
158 164
159 165 def unbundle(bundle, heads, url):
160 166 """Transfer repository data to the peer.
161 167
162 168 This is how the bulk of data during a push is transferred.
163 169
164 170 Returns the integer number of heads added to the peer.
165 171 """
166 172
167 173 class ipeerlegacycommands(zi.Interface):
168 174 """Interface for implementing support for legacy wire protocol commands.
169 175
170 176 Wire protocol commands transition to legacy status when they are no longer
171 177 used by modern clients. To facilitate identifying which commands are
172 178 legacy, the interfaces are split.
173 179 """
174 180
175 181 def between(pairs):
176 182 """Obtain nodes between pairs of nodes.
177 183
178 184 ``pairs`` is an iterable of node pairs.
179 185
180 186 Returns an iterable of iterables of nodes corresponding to each
181 187 requested pair.
182 188 """
183 189
184 190 def branches(nodes):
185 191 """Obtain ancestor changesets of specific nodes back to a branch point.
186 192
187 193 For each requested node, the peer finds the first ancestor node that is
188 194 a DAG root or is a merge.
189 195
190 196 Returns an iterable of iterables with the resolved values for each node.
191 197 """
192 198
193 199 def changegroup(nodes, source):
194 200 """Obtain a changegroup with data for descendants of specified nodes."""
195 201
196 202 def changegroupsubset(bases, heads, source):
197 203 pass
198 204
199 205 class ipeercommandexecutor(zi.Interface):
200 206 """Represents a mechanism to execute remote commands.
201 207
202 208 This is the primary interface for requesting that wire protocol commands
203 209 be executed. Instances of this interface are active in a context manager
204 210 and have a well-defined lifetime. When the context manager exits, all
205 211 outstanding requests are waited on.
206 212 """
207 213
208 214 def callcommand(name, args):
209 215 """Request that a named command be executed.
210 216
211 217 Receives the command name and a dictionary of command arguments.
212 218
213 219 Returns a ``concurrent.futures.Future`` that will resolve to the
214 220 result of that command request. That exact value is left up to
215 221 the implementation and possibly varies by command.
216 222
217 223 Not all commands can coexist with other commands in an executor
218 224 instance: it depends on the underlying wire protocol transport being
219 225 used and the command itself.
220 226
221 227 Implementations MAY call ``sendcommands()`` automatically if the
222 228 requested command can not coexist with other commands in this executor.
223 229
224 230 Implementations MAY call ``sendcommands()`` automatically when the
225 231 future's ``result()`` is called. So, consumers using multiple
226 232 commands with an executor MUST ensure that ``result()`` is not called
227 233 until all command requests have been issued.
228 234 """
229 235
230 236 def sendcommands():
231 237 """Trigger submission of queued command requests.
232 238
233 239 Not all transports submit commands as soon as they are requested to
234 240 run. When called, this method forces queued command requests to be
235 241 issued. It will no-op if all commands have already been sent.
236 242
237 243 When called, no more new commands may be issued with this executor.
238 244 """
239 245
240 246 def close():
241 247 """Signal that this command request is finished.
242 248
243 249 When called, no more new commands may be issued. All outstanding
244 250 commands that have previously been issued are waited on before
245 251 returning. This not only includes waiting for the futures to resolve,
246 252 but also waiting for all response data to arrive. In other words,
247 253 calling this waits for all on-wire state for issued command requests
248 254 to finish.
249 255
250 256 When used as a context manager, this method is called when exiting the
251 257 context manager.
252 258
253 259 This method may call ``sendcommands()`` if there are buffered commands.
254 260 """
255 261
256 262 class ipeerrequests(zi.Interface):
257 263 """Interface for executing commands on a peer."""
258 264
259 265 def commandexecutor():
260 266 """A context manager that resolves to an ipeercommandexecutor.
261 267
262 268 The object this resolves to can be used to issue command requests
263 269 to the peer.
264 270
265 271 Callers should call its ``callcommand`` method to issue command
266 272 requests.
267 273
268 274 A new executor should be obtained for each distinct set of commands
269 275 (possibly just a single command) that the consumer wants to execute
270 276 as part of a single operation or round trip. This is because some
271 277 peers are half-duplex and/or don't support persistent connections.
272 278 e.g. in the case of HTTP peers, commands sent to an executor represent
273 279 a single HTTP request. While some peers may support multiple command
274 280 sends over the wire per executor, consumers need to code to the least
275 281 capable peer. So it should be assumed that command executors buffer
276 282 called commands until they are told to send them and that each
277 283 command executor could result in a new connection or wire-level request
278 284 being issued.
279 285 """
280 286
281 287 class ipeerbase(ipeerconnection, ipeercapabilities, ipeercommands,
282 288 ipeerrequests):
283 289 """Unified interface for peer repositories.
284 290
285 291 All peer instances must conform to this interface.
286 292 """
287 293
288 294 @zi.implementer(ipeerbase)
289 295 class peer(object):
290 296 """Base class for peer repositories."""
291 297
292 298 def capable(self, name):
293 299 caps = self.capabilities()
294 300 if name in caps:
295 301 return True
296 302
297 303 name = '%s=' % name
298 304 for cap in caps:
299 305 if cap.startswith(name):
300 306 return cap[len(name):]
301 307
302 308 return False
303 309
304 310 def requirecap(self, name, purpose):
305 311 if self.capable(name):
306 312 return
307 313
308 314 raise error.CapabilityError(
309 315 _('cannot %s; remote repository does not support the %r '
310 316 'capability') % (purpose, name))
311 317
312 318 class ifilerevisionssequence(zi.Interface):
313 319 """Contains index data for all revisions of a file.
314 320
315 321 Types implementing this behave like lists of tuples. The index
316 322 in the list corresponds to the revision number. The values contain
317 323 index metadata.
318 324
319 325 The *null* revision (revision number -1) is always the last item
320 326 in the index.
321 327 """
322 328
323 329 def __len__():
324 330 """The total number of revisions."""
325 331
326 332 def __getitem__(rev):
327 333 """Returns the object having a specific revision number.
328 334
329 335 Returns an 8-tuple with the following fields:
330 336
331 337 offset+flags
332 338 Contains the offset and flags for the revision. 64-bit unsigned
333 339 integer where first 6 bytes are the offset and the next 2 bytes
334 340 are flags. The offset can be 0 if it is not used by the store.
335 341 compressed size
336 342 Size of the revision data in the store. It can be 0 if it isn't
337 343 needed by the store.
338 344 uncompressed size
339 345 Fulltext size. It can be 0 if it isn't needed by the store.
340 346 base revision
341 347 Revision number of revision the delta for storage is encoded
342 348 against. -1 indicates not encoded against a base revision.
343 349 link revision
344 350 Revision number of changelog revision this entry is related to.
345 351 p1 revision
346 352 Revision number of 1st parent. -1 if no 1st parent.
347 353 p2 revision
348 354 Revision number of 2nd parent. -1 if no 1st parent.
349 355 node
350 356 Binary node value for this revision number.
351 357
352 358 Negative values should index off the end of the sequence. ``-1``
353 359 should return the null revision. ``-2`` should return the most
354 360 recent revision.
355 361 """
356 362
357 363 def __contains__(rev):
358 364 """Whether a revision number exists."""
359 365
360 366 def insert(self, i, entry):
361 367 """Add an item to the index at specific revision."""
362 368
363 369 class ifileindex(zi.Interface):
364 370 """Storage interface for index data of a single file.
365 371
366 372 File storage data is divided into index metadata and data storage.
367 373 This interface defines the index portion of the interface.
368 374
369 375 The index logically consists of:
370 376
371 377 * A mapping between revision numbers and nodes.
372 378 * DAG data (storing and querying the relationship between nodes).
373 379 * Metadata to facilitate storage.
374 380 """
375 381 index = zi.Attribute(
376 382 """An ``ifilerevisionssequence`` instance.""")
377 383
378 384 def __len__():
379 385 """Obtain the number of revisions stored for this file."""
380 386
381 387 def __iter__():
382 388 """Iterate over revision numbers for this file."""
383 389
384 390 def revs(start=0, stop=None):
385 391 """Iterate over revision numbers for this file, with control."""
386 392
387 393 def parents(node):
388 394 """Returns a 2-tuple of parent nodes for a revision.
389 395
390 396 Values will be ``nullid`` if the parent is empty.
391 397 """
392 398
393 399 def parentrevs(rev):
394 400 """Like parents() but operates on revision numbers."""
395 401
396 402 def rev(node):
397 403 """Obtain the revision number given a node.
398 404
399 405 Raises ``error.LookupError`` if the node is not known.
400 406 """
401 407
402 408 def node(rev):
403 409 """Obtain the node value given a revision number.
404 410
405 411 Raises ``IndexError`` if the node is not known.
406 412 """
407 413
408 414 def lookup(node):
409 415 """Attempt to resolve a value to a node.
410 416
411 417 Value can be a binary node, hex node, revision number, or a string
412 418 that can be converted to an integer.
413 419
414 420 Raises ``error.LookupError`` if a node could not be resolved.
415 421 """
416 422
417 423 def linkrev(rev):
418 424 """Obtain the changeset revision number a revision is linked to."""
419 425
420 426 def flags(rev):
421 427 """Obtain flags used to affect storage of a revision."""
422 428
423 429 def iscensored(rev):
424 430 """Return whether a revision's content has been censored."""
425 431
426 432 def commonancestorsheads(node1, node2):
427 433 """Obtain an iterable of nodes containing heads of common ancestors.
428 434
429 435 See ``ancestor.commonancestorsheads()``.
430 436 """
431 437
432 438 def descendants(revs):
433 439 """Obtain descendant revision numbers for a set of revision numbers.
434 440
435 441 If ``nullrev`` is in the set, this is equivalent to ``revs()``.
436 442 """
437 443
438 444 def headrevs():
439 445 """Obtain a list of revision numbers that are DAG heads.
440 446
441 447 The list is sorted oldest to newest.
442 448
443 449 TODO determine if sorting is required.
444 450 """
445 451
446 452 def heads(start=None, stop=None):
447 453 """Obtain a list of nodes that are DAG heads, with control.
448 454
449 455 The set of revisions examined can be limited by specifying
450 456 ``start`` and ``stop``. ``start`` is a node. ``stop`` is an
451 457 iterable of nodes. DAG traversal starts at earlier revision
452 458 ``start`` and iterates forward until any node in ``stop`` is
453 459 encountered.
454 460 """
455 461
456 462 def children(node):
457 463 """Obtain nodes that are children of a node.
458 464
459 465 Returns a list of nodes.
460 466 """
461 467
462 468 def deltaparent(rev):
463 469 """"Return the revision that is a suitable parent to delta against."""
464 470
465 471 def candelta(baserev, rev):
466 472 """"Whether a delta can be generated between two revisions."""
467 473
468 474 class ifiledata(zi.Interface):
469 475 """Storage interface for data storage of a specific file.
470 476
471 477 This complements ``ifileindex`` and provides an interface for accessing
472 478 data for a tracked file.
473 479 """
474 480 def rawsize(rev):
475 481 """The size of the fulltext data for a revision as stored."""
476 482
477 483 def size(rev):
478 484 """Obtain the fulltext size of file data.
479 485
480 486 Any metadata is excluded from size measurements. Use ``rawsize()`` if
481 487 metadata size is important.
482 488 """
483 489
484 490 def checkhash(fulltext, node, p1=None, p2=None, rev=None):
485 491 """Validate the stored hash of a given fulltext and node.
486 492
487 493 Raises ``error.RevlogError`` is hash validation fails.
488 494 """
489 495
490 496 def revision(node, raw=False):
491 497 """"Obtain fulltext data for a node.
492 498
493 499 By default, any storage transformations are applied before the data
494 500 is returned. If ``raw`` is True, non-raw storage transformations
495 501 are not applied.
496 502
497 503 The fulltext data may contain a header containing metadata. Most
498 504 consumers should use ``read()`` to obtain the actual file data.
499 505 """
500 506
501 507 def read(node):
502 508 """Resolve file fulltext data.
503 509
504 510 This is similar to ``revision()`` except any metadata in the data
505 511 headers is stripped.
506 512 """
507 513
508 514 def renamed(node):
509 515 """Obtain copy metadata for a node.
510 516
511 517 Returns ``False`` if no copy metadata is stored or a 2-tuple of
512 518 (path, node) from which this revision was copied.
513 519 """
514 520
515 521 def cmp(node, fulltext):
516 522 """Compare fulltext to another revision.
517 523
518 524 Returns True if the fulltext is different from what is stored.
519 525
520 526 This takes copy metadata into account.
521 527
522 528 TODO better document the copy metadata and censoring logic.
523 529 """
524 530
525 531 def revdiff(rev1, rev2):
526 532 """Obtain a delta between two revision numbers.
527 533
528 534 Operates on raw data in the store (``revision(node, raw=True)``).
529 535
530 536 The returned data is the result of ``bdiff.bdiff`` on the raw
531 537 revision data.
532 538 """
533 539
534 540 class ifilemutation(zi.Interface):
535 541 """Storage interface for mutation events of a tracked file."""
536 542
537 543 def add(filedata, meta, transaction, linkrev, p1, p2):
538 544 """Add a new revision to the store.
539 545
540 546 Takes file data, dictionary of metadata, a transaction, linkrev,
541 547 and parent nodes.
542 548
543 549 Returns the node that was added.
544 550
545 551 May no-op if a revision matching the supplied data is already stored.
546 552 """
547 553
548 554 def addrevision(revisiondata, transaction, linkrev, p1, p2, node=None,
549 555 flags=0, cachedelta=None):
550 556 """Add a new revision to the store.
551 557
552 558 This is similar to ``add()`` except it operates at a lower level.
553 559
554 560 The data passed in already contains a metadata header, if any.
555 561
556 562 ``node`` and ``flags`` can be used to define the expected node and
557 563 the flags to use with storage.
558 564
559 565 ``add()`` is usually called when adding files from e.g. the working
560 566 directory. ``addrevision()`` is often called by ``add()`` and for
561 567 scenarios where revision data has already been computed, such as when
562 568 applying raw data from a peer repo.
563 569 """
564 570
565 571 def addgroup(deltas, linkmapper, transaction, addrevisioncb=None):
566 572 """Process a series of deltas for storage.
567 573
568 574 ``deltas`` is an iterable of 7-tuples of
569 575 (node, p1, p2, linknode, deltabase, delta, flags) defining revisions
570 576 to add.
571 577
572 578 The ``delta`` field contains ``mpatch`` data to apply to a base
573 579 revision, identified by ``deltabase``. The base node can be
574 580 ``nullid``, in which case the header from the delta can be ignored
575 581 and the delta used as the fulltext.
576 582
577 583 ``addrevisioncb`` should be called for each node as it is committed.
578 584
579 585 Returns a list of nodes that were processed. A node will be in the list
580 586 even if it existed in the store previously.
581 587 """
582 588
583 589 def getstrippoint(minlink):
584 590 """Find the minimum revision that must be stripped to strip a linkrev.
585 591
586 592 Returns a 2-tuple containing the minimum revision number and a set
587 593 of all revisions numbers that would be broken by this strip.
588 594
589 595 TODO this is highly revlog centric and should be abstracted into
590 596 a higher-level deletion API. ``repair.strip()`` relies on this.
591 597 """
592 598
593 599 def strip(minlink, transaction):
594 600 """Remove storage of items starting at a linkrev.
595 601
596 602 This uses ``getstrippoint()`` to determine the first node to remove.
597 603 Then it effectively truncates storage for all revisions after that.
598 604
599 605 TODO this is highly revlog centric and should be abstracted into a
600 606 higher-level deletion API.
601 607 """
602 608
603 609 class ifilestorage(ifileindex, ifiledata, ifilemutation):
604 610 """Complete storage interface for a single tracked file."""
605 611
606 612 version = zi.Attribute(
607 613 """Version number of storage.
608 614
609 615 TODO this feels revlog centric and could likely be removed.
610 616 """)
611 617
612 618 storedeltachains = zi.Attribute(
613 619 """Whether the store stores deltas.
614 620
615 621 TODO deltachains are revlog centric. This can probably removed
616 622 once there are better abstractions for obtaining/writing
617 623 data.
618 624 """)
619 625
620 626 _generaldelta = zi.Attribute(
621 627 """Whether deltas can be against any parent revision.
622 628
623 629 TODO this is used by changegroup code and it could probably be
624 630 folded into another API.
625 631 """)
626 632
627 633 def files():
628 634 """Obtain paths that are backing storage for this file.
629 635
630 636 TODO this is used heavily by verify code and there should probably
631 637 be a better API for that.
632 638 """
633 639
634 640 def checksize():
635 641 """Obtain the expected sizes of backing files.
636 642
637 643 TODO this is used by verify and it should not be part of the interface.
638 644 """
639 645
640 646 class completelocalrepository(zi.Interface):
641 647 """Monolithic interface for local repositories.
642 648
643 649 This currently captures the reality of things - not how things should be.
644 650 """
645 651
646 652 supportedformats = zi.Attribute(
647 653 """Set of requirements that apply to stream clone.
648 654
649 655 This is actually a class attribute and is shared among all instances.
650 656 """)
651 657
652 658 openerreqs = zi.Attribute(
653 659 """Set of requirements that are passed to the opener.
654 660
655 661 This is actually a class attribute and is shared among all instances.
656 662 """)
657 663
658 664 supported = zi.Attribute(
659 665 """Set of requirements that this repo is capable of opening.""")
660 666
661 667 requirements = zi.Attribute(
662 668 """Set of requirements this repo uses.""")
663 669
664 670 filtername = zi.Attribute(
665 671 """Name of the repoview that is active on this repo.""")
666 672
667 673 wvfs = zi.Attribute(
668 674 """VFS used to access the working directory.""")
669 675
670 676 vfs = zi.Attribute(
671 677 """VFS rooted at the .hg directory.
672 678
673 679 Used to access repository data not in the store.
674 680 """)
675 681
676 682 svfs = zi.Attribute(
677 683 """VFS rooted at the store.
678 684
679 685 Used to access repository data in the store. Typically .hg/store.
680 686 But can point elsewhere if the store is shared.
681 687 """)
682 688
683 689 root = zi.Attribute(
684 690 """Path to the root of the working directory.""")
685 691
686 692 path = zi.Attribute(
687 693 """Path to the .hg directory.""")
688 694
689 695 origroot = zi.Attribute(
690 696 """The filesystem path that was used to construct the repo.""")
691 697
692 698 auditor = zi.Attribute(
693 699 """A pathauditor for the working directory.
694 700
695 701 This checks if a path refers to a nested repository.
696 702
697 703 Operates on the filesystem.
698 704 """)
699 705
700 706 nofsauditor = zi.Attribute(
701 707 """A pathauditor for the working directory.
702 708
703 709 This is like ``auditor`` except it doesn't do filesystem checks.
704 710 """)
705 711
706 712 baseui = zi.Attribute(
707 713 """Original ui instance passed into constructor.""")
708 714
709 715 ui = zi.Attribute(
710 716 """Main ui instance for this instance.""")
711 717
712 718 sharedpath = zi.Attribute(
713 719 """Path to the .hg directory of the repo this repo was shared from.""")
714 720
715 721 store = zi.Attribute(
716 722 """A store instance.""")
717 723
718 724 spath = zi.Attribute(
719 725 """Path to the store.""")
720 726
721 727 sjoin = zi.Attribute(
722 728 """Alias to self.store.join.""")
723 729
724 730 cachevfs = zi.Attribute(
725 731 """A VFS used to access the cache directory.
726 732
727 733 Typically .hg/cache.
728 734 """)
729 735
730 736 filteredrevcache = zi.Attribute(
731 737 """Holds sets of revisions to be filtered.""")
732 738
733 739 names = zi.Attribute(
734 740 """A ``namespaces`` instance.""")
735 741
736 742 def close():
737 743 """Close the handle on this repository."""
738 744
739 745 def peer():
740 746 """Obtain an object conforming to the ``peer`` interface."""
741 747
742 748 def unfiltered():
743 749 """Obtain an unfiltered/raw view of this repo."""
744 750
745 751 def filtered(name, visibilityexceptions=None):
746 752 """Obtain a named view of this repository."""
747 753
748 754 obsstore = zi.Attribute(
749 755 """A store of obsolescence data.""")
750 756
751 757 changelog = zi.Attribute(
752 758 """A handle on the changelog revlog.""")
753 759
754 760 manifestlog = zi.Attribute(
755 761 """A handle on the root manifest revlog.""")
756 762
757 763 dirstate = zi.Attribute(
758 764 """Working directory state.""")
759 765
760 766 narrowpats = zi.Attribute(
761 767 """Matcher patterns for this repository's narrowspec.""")
762 768
763 769 def narrowmatch():
764 770 """Obtain a matcher for the narrowspec."""
765 771
766 772 def setnarrowpats(newincludes, newexcludes):
767 773 """Define the narrowspec for this repository."""
768 774
769 775 def __getitem__(changeid):
770 776 """Try to resolve a changectx."""
771 777
772 778 def __contains__(changeid):
773 779 """Whether a changeset exists."""
774 780
775 781 def __nonzero__():
776 782 """Always returns True."""
777 783 return True
778 784
779 785 __bool__ = __nonzero__
780 786
781 787 def __len__():
782 788 """Returns the number of changesets in the repo."""
783 789
784 790 def __iter__():
785 791 """Iterate over revisions in the changelog."""
786 792
787 793 def revs(expr, *args):
788 794 """Evaluate a revset.
789 795
790 796 Emits revisions.
791 797 """
792 798
793 799 def set(expr, *args):
794 800 """Evaluate a revset.
795 801
796 802 Emits changectx instances.
797 803 """
798 804
799 805 def anyrevs(specs, user=False, localalias=None):
800 806 """Find revisions matching one of the given revsets."""
801 807
802 808 def url():
803 809 """Returns a string representing the location of this repo."""
804 810
805 811 def hook(name, throw=False, **args):
806 812 """Call a hook."""
807 813
808 814 def tags():
809 815 """Return a mapping of tag to node."""
810 816
811 817 def tagtype(tagname):
812 818 """Return the type of a given tag."""
813 819
814 820 def tagslist():
815 821 """Return a list of tags ordered by revision."""
816 822
817 823 def nodetags(node):
818 824 """Return the tags associated with a node."""
819 825
820 826 def nodebookmarks(node):
821 827 """Return the list of bookmarks pointing to the specified node."""
822 828
823 829 def branchmap():
824 830 """Return a mapping of branch to heads in that branch."""
825 831
826 832 def revbranchcache():
827 833 pass
828 834
829 835 def branchtip(branchtip, ignoremissing=False):
830 836 """Return the tip node for a given branch."""
831 837
832 838 def lookup(key):
833 839 """Resolve the node for a revision."""
834 840
835 841 def lookupbranch(key):
836 842 """Look up the branch name of the given revision or branch name."""
837 843
838 844 def known(nodes):
839 845 """Determine whether a series of nodes is known.
840 846
841 847 Returns a list of bools.
842 848 """
843 849
844 850 def local():
845 851 """Whether the repository is local."""
846 852 return True
847 853
848 854 def publishing():
849 855 """Whether the repository is a publishing repository."""
850 856
851 857 def cancopy():
852 858 pass
853 859
854 860 def shared():
855 861 """The type of shared repository or None."""
856 862
857 863 def wjoin(f, *insidef):
858 864 """Calls self.vfs.reljoin(self.root, f, *insidef)"""
859 865
860 866 def file(f):
861 867 """Obtain a filelog for a tracked path."""
862 868
863 869 def setparents(p1, p2):
864 870 """Set the parent nodes of the working directory."""
865 871
866 872 def filectx(path, changeid=None, fileid=None):
867 873 """Obtain a filectx for the given file revision."""
868 874
869 875 def getcwd():
870 876 """Obtain the current working directory from the dirstate."""
871 877
872 878 def pathto(f, cwd=None):
873 879 """Obtain the relative path to a file."""
874 880
875 881 def adddatafilter(name, fltr):
876 882 pass
877 883
878 884 def wread(filename):
879 885 """Read a file from wvfs, using data filters."""
880 886
881 887 def wwrite(filename, data, flags, backgroundclose=False, **kwargs):
882 888 """Write data to a file in the wvfs, using data filters."""
883 889
884 890 def wwritedata(filename, data):
885 891 """Resolve data for writing to the wvfs, using data filters."""
886 892
887 893 def currenttransaction():
888 894 """Obtain the current transaction instance or None."""
889 895
890 896 def transaction(desc, report=None):
891 897 """Open a new transaction to write to the repository."""
892 898
893 899 def undofiles():
894 900 """Returns a list of (vfs, path) for files to undo transactions."""
895 901
896 902 def recover():
897 903 """Roll back an interrupted transaction."""
898 904
899 905 def rollback(dryrun=False, force=False):
900 906 """Undo the last transaction.
901 907
902 908 DANGEROUS.
903 909 """
904 910
905 911 def updatecaches(tr=None, full=False):
906 912 """Warm repo caches."""
907 913
908 914 def invalidatecaches():
909 915 """Invalidate cached data due to the repository mutating."""
910 916
911 917 def invalidatevolatilesets():
912 918 pass
913 919
914 920 def invalidatedirstate():
915 921 """Invalidate the dirstate."""
916 922
917 923 def invalidate(clearfilecache=False):
918 924 pass
919 925
920 926 def invalidateall():
921 927 pass
922 928
923 929 def lock(wait=True):
924 930 """Lock the repository store and return a lock instance."""
925 931
926 932 def wlock(wait=True):
927 933 """Lock the non-store parts of the repository."""
928 934
929 935 def currentwlock():
930 936 """Return the wlock if it's held or None."""
931 937
932 938 def checkcommitpatterns(wctx, vdirs, match, status, fail):
933 939 pass
934 940
935 941 def commit(text='', user=None, date=None, match=None, force=False,
936 942 editor=False, extra=None):
937 943 """Add a new revision to the repository."""
938 944
939 945 def commitctx(ctx, error=False):
940 946 """Commit a commitctx instance to the repository."""
941 947
942 948 def destroying():
943 949 """Inform the repository that nodes are about to be destroyed."""
944 950
945 951 def destroyed():
946 952 """Inform the repository that nodes have been destroyed."""
947 953
948 954 def status(node1='.', node2=None, match=None, ignored=False,
949 955 clean=False, unknown=False, listsubrepos=False):
950 956 """Convenience method to call repo[x].status()."""
951 957
952 958 def addpostdsstatus(ps):
953 959 pass
954 960
955 961 def postdsstatus():
956 962 pass
957 963
958 964 def clearpostdsstatus():
959 965 pass
960 966
961 967 def heads(start=None):
962 968 """Obtain list of nodes that are DAG heads."""
963 969
964 970 def branchheads(branch=None, start=None, closed=False):
965 971 pass
966 972
967 973 def branches(nodes):
968 974 pass
969 975
970 976 def between(pairs):
971 977 pass
972 978
973 979 def checkpush(pushop):
974 980 pass
975 981
976 982 prepushoutgoinghooks = zi.Attribute(
977 983 """util.hooks instance.""")
978 984
979 985 def pushkey(namespace, key, old, new):
980 986 pass
981 987
982 988 def listkeys(namespace):
983 989 pass
984 990
985 991 def debugwireargs(one, two, three=None, four=None, five=None):
986 992 pass
987 993
988 994 def savecommitmessage(text):
989 995 pass
@@ -1,615 +1,619 b''
1 1 # wireprotov1peer.py - Client-side functionality for wire protocol version 1.
2 2 #
3 3 # Copyright 2005-2010 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 hashlib
11 11 import sys
12 12 import weakref
13 13
14 14 from .i18n import _
15 15 from .node import (
16 16 bin,
17 17 )
18 18 from .thirdparty.zope import (
19 19 interface as zi,
20 20 )
21 21 from . import (
22 22 bundle2,
23 23 changegroup as changegroupmod,
24 24 encoding,
25 25 error,
26 26 pushkey as pushkeymod,
27 27 pycompat,
28 28 repository,
29 29 util,
30 30 wireprototypes,
31 31 )
32 32
33 33 urlreq = util.urlreq
34 34
35 35 def batchable(f):
36 36 '''annotation for batchable methods
37 37
38 38 Such methods must implement a coroutine as follows:
39 39
40 40 @batchable
41 41 def sample(self, one, two=None):
42 42 # Build list of encoded arguments suitable for your wire protocol:
43 43 encargs = [('one', encode(one),), ('two', encode(two),)]
44 44 # Create future for injection of encoded result:
45 45 encresref = future()
46 46 # Return encoded arguments and future:
47 47 yield encargs, encresref
48 48 # Assuming the future to be filled with the result from the batched
49 49 # request now. Decode it:
50 50 yield decode(encresref.value)
51 51
52 52 The decorator returns a function which wraps this coroutine as a plain
53 53 method, but adds the original method as an attribute called "batchable",
54 54 which is used by remotebatch to split the call into separate encoding and
55 55 decoding phases.
56 56 '''
57 57 def plain(*args, **opts):
58 58 batchable = f(*args, **opts)
59 59 encargsorres, encresref = next(batchable)
60 60 if not encresref:
61 61 return encargsorres # a local result in this case
62 62 self = args[0]
63 63 cmd = pycompat.bytesurl(f.__name__) # ensure cmd is ascii bytestr
64 64 encresref.set(self._submitone(cmd, encargsorres))
65 65 return next(batchable)
66 66 setattr(plain, 'batchable', f)
67 67 return plain
68 68
69 69 class future(object):
70 70 '''placeholder for a value to be set later'''
71 71 def set(self, value):
72 72 if util.safehasattr(self, 'value'):
73 73 raise error.RepoError("future is already set")
74 74 self.value = value
75 75
76 76 def encodebatchcmds(req):
77 77 """Return a ``cmds`` argument value for the ``batch`` command."""
78 78 escapearg = wireprototypes.escapebatcharg
79 79
80 80 cmds = []
81 81 for op, argsdict in req:
82 82 # Old servers didn't properly unescape argument names. So prevent
83 83 # the sending of argument names that may not be decoded properly by
84 84 # servers.
85 85 assert all(escapearg(k) == k for k in argsdict)
86 86
87 87 args = ','.join('%s=%s' % (escapearg(k), escapearg(v))
88 88 for k, v in argsdict.iteritems())
89 89 cmds.append('%s %s' % (op, args))
90 90
91 91 return ';'.join(cmds)
92 92
93 93 class unsentfuture(pycompat.futures.Future):
94 94 """A Future variation to represent an unsent command.
95 95
96 96 Because we buffer commands and don't submit them immediately, calling
97 97 ``result()`` on an unsent future could deadlock. Futures for buffered
98 98 commands are represented by this type, which wraps ``result()`` to
99 99 call ``sendcommands()``.
100 100 """
101 101
102 102 def result(self, timeout=None):
103 103 if self.done():
104 104 return pycompat.futures.Future.result(self, timeout)
105 105
106 106 self._peerexecutor.sendcommands()
107 107
108 108 # This looks like it will infinitely recurse. However,
109 109 # sendcommands() should modify __class__. This call serves as a check
110 110 # on that.
111 111 return self.result(timeout)
112 112
113 113 @zi.implementer(repository.ipeercommandexecutor)
114 114 class peerexecutor(object):
115 115 def __init__(self, peer):
116 116 self._peer = peer
117 117 self._sent = False
118 118 self._closed = False
119 119 self._calls = []
120 120 self._futures = weakref.WeakSet()
121 121 self._responseexecutor = None
122 122 self._responsef = None
123 123
124 124 def __enter__(self):
125 125 return self
126 126
127 127 def __exit__(self, exctype, excvalee, exctb):
128 128 self.close()
129 129
130 130 def callcommand(self, command, args):
131 131 if self._sent:
132 132 raise error.ProgrammingError('callcommand() cannot be used '
133 133 'after commands are sent')
134 134
135 135 if self._closed:
136 136 raise error.ProgrammingError('callcommand() cannot be used '
137 137 'after close()')
138 138
139 139 # Commands are dispatched through methods on the peer.
140 140 fn = getattr(self._peer, pycompat.sysstr(command), None)
141 141
142 142 if not fn:
143 143 raise error.ProgrammingError(
144 144 'cannot call command %s: method of same name not available '
145 145 'on peer' % command)
146 146
147 147 # Commands are either batchable or they aren't. If a command
148 148 # isn't batchable, we send it immediately because the executor
149 149 # can no longer accept new commands after a non-batchable command.
150 150 # If a command is batchable, we queue it for later. But we have
151 151 # to account for the case of a non-batchable command arriving after
152 152 # a batchable one and refuse to service it.
153 153
154 154 def addcall():
155 155 f = pycompat.futures.Future()
156 156 self._futures.add(f)
157 157 self._calls.append((command, args, fn, f))
158 158 return f
159 159
160 160 if getattr(fn, 'batchable', False):
161 161 f = addcall()
162 162
163 163 # But since we don't issue it immediately, we wrap its result()
164 164 # to trigger sending so we avoid deadlocks.
165 165 f.__class__ = unsentfuture
166 166 f._peerexecutor = self
167 167 else:
168 168 if self._calls:
169 169 raise error.ProgrammingError(
170 170 '%s is not batchable and cannot be called on a command '
171 171 'executor along with other commands' % command)
172 172
173 173 f = addcall()
174 174
175 175 # Non-batchable commands can never coexist with another command
176 176 # in this executor. So send the command immediately.
177 177 self.sendcommands()
178 178
179 179 return f
180 180
181 181 def sendcommands(self):
182 182 if self._sent:
183 183 return
184 184
185 185 if not self._calls:
186 186 return
187 187
188 188 self._sent = True
189 189
190 190 # Unhack any future types so caller seens a clean type and to break
191 191 # cycle between us and futures.
192 192 for f in self._futures:
193 193 if isinstance(f, unsentfuture):
194 194 f.__class__ = pycompat.futures.Future
195 195 f._peerexecutor = None
196 196
197 197 calls = self._calls
198 198 # Mainly to destroy references to futures.
199 199 self._calls = None
200 200
201 201 # Simple case of a single command. We call it synchronously.
202 202 if len(calls) == 1:
203 203 command, args, fn, f = calls[0]
204 204
205 205 # Future was cancelled. Ignore it.
206 206 if not f.set_running_or_notify_cancel():
207 207 return
208 208
209 209 try:
210 210 result = fn(**pycompat.strkwargs(args))
211 211 except Exception:
212 212 f.set_exception_info(*sys.exc_info()[1:])
213 213 else:
214 214 f.set_result(result)
215 215
216 216 return
217 217
218 218 # Batch commands are a bit harder. First, we have to deal with the
219 219 # @batchable coroutine. That's a bit annoying. Furthermore, we also
220 220 # need to preserve streaming. i.e. it should be possible for the
221 221 # futures to resolve as data is coming in off the wire without having
222 222 # to wait for the final byte of the final response. We do this by
223 223 # spinning up a thread to read the responses.
224 224
225 225 requests = []
226 226 states = []
227 227
228 228 for command, args, fn, f in calls:
229 229 # Future was cancelled. Ignore it.
230 230 if not f.set_running_or_notify_cancel():
231 231 continue
232 232
233 233 try:
234 234 batchable = fn.batchable(fn.__self__,
235 235 **pycompat.strkwargs(args))
236 236 except Exception:
237 237 f.set_exception_info(*sys.exc_info()[1:])
238 238 return
239 239
240 240 # Encoded arguments and future holding remote result.
241 241 try:
242 242 encodedargs, fremote = next(batchable)
243 243 except Exception:
244 244 f.set_exception_info(*sys.exc_info()[1:])
245 245 return
246 246
247 247 requests.append((command, encodedargs))
248 248 states.append((command, f, batchable, fremote))
249 249
250 250 if not requests:
251 251 return
252 252
253 253 # This will emit responses in order they were executed.
254 254 wireresults = self._peer._submitbatch(requests)
255 255
256 256 # The use of a thread pool executor here is a bit weird for something
257 257 # that only spins up a single thread. However, thread management is
258 258 # hard and it is easy to encounter race conditions, deadlocks, etc.
259 259 # concurrent.futures already solves these problems and its thread pool
260 260 # executor has minimal overhead. So we use it.
261 261 self._responseexecutor = pycompat.futures.ThreadPoolExecutor(1)
262 262 self._responsef = self._responseexecutor.submit(self._readbatchresponse,
263 263 states, wireresults)
264 264
265 265 def close(self):
266 266 self.sendcommands()
267 267
268 268 if self._closed:
269 269 return
270 270
271 271 self._closed = True
272 272
273 273 if not self._responsef:
274 274 return
275 275
276 276 # We need to wait on our in-flight response and then shut down the
277 277 # executor once we have a result.
278 278 try:
279 279 self._responsef.result()
280 280 finally:
281 281 self._responseexecutor.shutdown(wait=True)
282 282 self._responsef = None
283 283 self._responseexecutor = None
284 284
285 285 # If any of our futures are still in progress, mark them as
286 286 # errored. Otherwise a result() could wait indefinitely.
287 287 for f in self._futures:
288 288 if not f.done():
289 289 f.set_exception(error.ResponseError(
290 290 _('unfulfilled batch command response')))
291 291
292 292 self._futures = None
293 293
294 294 def _readbatchresponse(self, states, wireresults):
295 295 # Executes in a thread to read data off the wire.
296 296
297 297 for command, f, batchable, fremote in states:
298 298 # Grab raw result off the wire and teach the internal future
299 299 # about it.
300 300 remoteresult = next(wireresults)
301 301 fremote.set(remoteresult)
302 302
303 303 # And ask the coroutine to decode that value.
304 304 try:
305 305 result = next(batchable)
306 306 except Exception:
307 307 f.set_exception_info(*sys.exc_info()[1:])
308 308 else:
309 309 f.set_result(result)
310 310
311 311 @zi.implementer(repository.ipeerlegacycommands)
312 312 class wirepeer(repository.peer):
313 313 """Client-side interface for communicating with a peer repository.
314 314
315 315 Methods commonly call wire protocol commands of the same name.
316 316
317 317 See also httppeer.py and sshpeer.py for protocol-specific
318 318 implementations of this interface.
319 319 """
320 320 def commandexecutor(self):
321 321 return peerexecutor(self)
322 322
323 323 # Begin of ipeercommands interface.
324 324
325 def clonebundles(self):
326 self.requirecap('clonebundles', _('clone bundles'))
327 return self._call('clonebundles')
328
325 329 @batchable
326 330 def lookup(self, key):
327 331 self.requirecap('lookup', _('look up remote revision'))
328 332 f = future()
329 333 yield {'key': encoding.fromlocal(key)}, f
330 334 d = f.value
331 335 success, data = d[:-1].split(" ", 1)
332 336 if int(success):
333 337 yield bin(data)
334 338 else:
335 339 self._abort(error.RepoError(data))
336 340
337 341 @batchable
338 342 def heads(self):
339 343 f = future()
340 344 yield {}, f
341 345 d = f.value
342 346 try:
343 347 yield wireprototypes.decodelist(d[:-1])
344 348 except ValueError:
345 349 self._abort(error.ResponseError(_("unexpected response:"), d))
346 350
347 351 @batchable
348 352 def known(self, nodes):
349 353 f = future()
350 354 yield {'nodes': wireprototypes.encodelist(nodes)}, f
351 355 d = f.value
352 356 try:
353 357 yield [bool(int(b)) for b in d]
354 358 except ValueError:
355 359 self._abort(error.ResponseError(_("unexpected response:"), d))
356 360
357 361 @batchable
358 362 def branchmap(self):
359 363 f = future()
360 364 yield {}, f
361 365 d = f.value
362 366 try:
363 367 branchmap = {}
364 368 for branchpart in d.splitlines():
365 369 branchname, branchheads = branchpart.split(' ', 1)
366 370 branchname = encoding.tolocal(urlreq.unquote(branchname))
367 371 branchheads = wireprototypes.decodelist(branchheads)
368 372 branchmap[branchname] = branchheads
369 373 yield branchmap
370 374 except TypeError:
371 375 self._abort(error.ResponseError(_("unexpected response:"), d))
372 376
373 377 @batchable
374 378 def listkeys(self, namespace):
375 379 if not self.capable('pushkey'):
376 380 yield {}, None
377 381 f = future()
378 382 self.ui.debug('preparing listkeys for "%s"\n' % namespace)
379 383 yield {'namespace': encoding.fromlocal(namespace)}, f
380 384 d = f.value
381 385 self.ui.debug('received listkey for "%s": %i bytes\n'
382 386 % (namespace, len(d)))
383 387 yield pushkeymod.decodekeys(d)
384 388
385 389 @batchable
386 390 def pushkey(self, namespace, key, old, new):
387 391 if not self.capable('pushkey'):
388 392 yield False, None
389 393 f = future()
390 394 self.ui.debug('preparing pushkey for "%s:%s"\n' % (namespace, key))
391 395 yield {'namespace': encoding.fromlocal(namespace),
392 396 'key': encoding.fromlocal(key),
393 397 'old': encoding.fromlocal(old),
394 398 'new': encoding.fromlocal(new)}, f
395 399 d = f.value
396 400 d, output = d.split('\n', 1)
397 401 try:
398 402 d = bool(int(d))
399 403 except ValueError:
400 404 raise error.ResponseError(
401 405 _('push failed (unexpected response):'), d)
402 406 for l in output.splitlines(True):
403 407 self.ui.status(_('remote: '), l)
404 408 yield d
405 409
406 410 def stream_out(self):
407 411 return self._callstream('stream_out')
408 412
409 413 def getbundle(self, source, **kwargs):
410 414 kwargs = pycompat.byteskwargs(kwargs)
411 415 self.requirecap('getbundle', _('look up remote changes'))
412 416 opts = {}
413 417 bundlecaps = kwargs.get('bundlecaps') or set()
414 418 for key, value in kwargs.iteritems():
415 419 if value is None:
416 420 continue
417 421 keytype = wireprototypes.GETBUNDLE_ARGUMENTS.get(key)
418 422 if keytype is None:
419 423 raise error.ProgrammingError(
420 424 'Unexpectedly None keytype for key %s' % key)
421 425 elif keytype == 'nodes':
422 426 value = wireprototypes.encodelist(value)
423 427 elif keytype == 'csv':
424 428 value = ','.join(value)
425 429 elif keytype == 'scsv':
426 430 value = ','.join(sorted(value))
427 431 elif keytype == 'boolean':
428 432 value = '%i' % bool(value)
429 433 elif keytype != 'plain':
430 434 raise KeyError('unknown getbundle option type %s'
431 435 % keytype)
432 436 opts[key] = value
433 437 f = self._callcompressable("getbundle", **pycompat.strkwargs(opts))
434 438 if any((cap.startswith('HG2') for cap in bundlecaps)):
435 439 return bundle2.getunbundler(self.ui, f)
436 440 else:
437 441 return changegroupmod.cg1unpacker(f, 'UN')
438 442
439 443 def unbundle(self, bundle, heads, url):
440 444 '''Send cg (a readable file-like object representing the
441 445 changegroup to push, typically a chunkbuffer object) to the
442 446 remote server as a bundle.
443 447
444 448 When pushing a bundle10 stream, return an integer indicating the
445 449 result of the push (see changegroup.apply()).
446 450
447 451 When pushing a bundle20 stream, return a bundle20 stream.
448 452
449 453 `url` is the url the client thinks it's pushing to, which is
450 454 visible to hooks.
451 455 '''
452 456
453 457 if heads != ['force'] and self.capable('unbundlehash'):
454 458 heads = wireprototypes.encodelist(
455 459 ['hashed', hashlib.sha1(''.join(sorted(heads))).digest()])
456 460 else:
457 461 heads = wireprototypes.encodelist(heads)
458 462
459 463 if util.safehasattr(bundle, 'deltaheader'):
460 464 # this a bundle10, do the old style call sequence
461 465 ret, output = self._callpush("unbundle", bundle, heads=heads)
462 466 if ret == "":
463 467 raise error.ResponseError(
464 468 _('push failed:'), output)
465 469 try:
466 470 ret = int(ret)
467 471 except ValueError:
468 472 raise error.ResponseError(
469 473 _('push failed (unexpected response):'), ret)
470 474
471 475 for l in output.splitlines(True):
472 476 self.ui.status(_('remote: '), l)
473 477 else:
474 478 # bundle2 push. Send a stream, fetch a stream.
475 479 stream = self._calltwowaystream('unbundle', bundle, heads=heads)
476 480 ret = bundle2.getunbundler(self.ui, stream)
477 481 return ret
478 482
479 483 # End of ipeercommands interface.
480 484
481 485 # Begin of ipeerlegacycommands interface.
482 486
483 487 def branches(self, nodes):
484 488 n = wireprototypes.encodelist(nodes)
485 489 d = self._call("branches", nodes=n)
486 490 try:
487 491 br = [tuple(wireprototypes.decodelist(b)) for b in d.splitlines()]
488 492 return br
489 493 except ValueError:
490 494 self._abort(error.ResponseError(_("unexpected response:"), d))
491 495
492 496 def between(self, pairs):
493 497 batch = 8 # avoid giant requests
494 498 r = []
495 499 for i in xrange(0, len(pairs), batch):
496 500 n = " ".join([wireprototypes.encodelist(p, '-')
497 501 for p in pairs[i:i + batch]])
498 502 d = self._call("between", pairs=n)
499 503 try:
500 504 r.extend(l and wireprototypes.decodelist(l) or []
501 505 for l in d.splitlines())
502 506 except ValueError:
503 507 self._abort(error.ResponseError(_("unexpected response:"), d))
504 508 return r
505 509
506 510 def changegroup(self, nodes, source):
507 511 n = wireprototypes.encodelist(nodes)
508 512 f = self._callcompressable("changegroup", roots=n)
509 513 return changegroupmod.cg1unpacker(f, 'UN')
510 514
511 515 def changegroupsubset(self, bases, heads, source):
512 516 self.requirecap('changegroupsubset', _('look up remote changes'))
513 517 bases = wireprototypes.encodelist(bases)
514 518 heads = wireprototypes.encodelist(heads)
515 519 f = self._callcompressable("changegroupsubset",
516 520 bases=bases, heads=heads)
517 521 return changegroupmod.cg1unpacker(f, 'UN')
518 522
519 523 # End of ipeerlegacycommands interface.
520 524
521 525 def _submitbatch(self, req):
522 526 """run batch request <req> on the server
523 527
524 528 Returns an iterator of the raw responses from the server.
525 529 """
526 530 ui = self.ui
527 531 if ui.debugflag and ui.configbool('devel', 'debug.peer-request'):
528 532 ui.debug('devel-peer-request: batched-content\n')
529 533 for op, args in req:
530 534 msg = 'devel-peer-request: - %s (%d arguments)\n'
531 535 ui.debug(msg % (op, len(args)))
532 536
533 537 unescapearg = wireprototypes.unescapebatcharg
534 538
535 539 rsp = self._callstream("batch", cmds=encodebatchcmds(req))
536 540 chunk = rsp.read(1024)
537 541 work = [chunk]
538 542 while chunk:
539 543 while ';' not in chunk and chunk:
540 544 chunk = rsp.read(1024)
541 545 work.append(chunk)
542 546 merged = ''.join(work)
543 547 while ';' in merged:
544 548 one, merged = merged.split(';', 1)
545 549 yield unescapearg(one)
546 550 chunk = rsp.read(1024)
547 551 work = [merged, chunk]
548 552 yield unescapearg(''.join(work))
549 553
550 554 def _submitone(self, op, args):
551 555 return self._call(op, **pycompat.strkwargs(args))
552 556
553 557 def debugwireargs(self, one, two, three=None, four=None, five=None):
554 558 # don't pass optional arguments left at their default value
555 559 opts = {}
556 560 if three is not None:
557 561 opts[r'three'] = three
558 562 if four is not None:
559 563 opts[r'four'] = four
560 564 return self._call('debugwireargs', one=one, two=two, **opts)
561 565
562 566 def _call(self, cmd, **args):
563 567 """execute <cmd> on the server
564 568
565 569 The command is expected to return a simple string.
566 570
567 571 returns the server reply as a string."""
568 572 raise NotImplementedError()
569 573
570 574 def _callstream(self, cmd, **args):
571 575 """execute <cmd> on the server
572 576
573 577 The command is expected to return a stream. Note that if the
574 578 command doesn't return a stream, _callstream behaves
575 579 differently for ssh and http peers.
576 580
577 581 returns the server reply as a file like object.
578 582 """
579 583 raise NotImplementedError()
580 584
581 585 def _callcompressable(self, cmd, **args):
582 586 """execute <cmd> on the server
583 587
584 588 The command is expected to return a stream.
585 589
586 590 The stream may have been compressed in some implementations. This
587 591 function takes care of the decompression. This is the only difference
588 592 with _callstream.
589 593
590 594 returns the server reply as a file like object.
591 595 """
592 596 raise NotImplementedError()
593 597
594 598 def _callpush(self, cmd, fp, **args):
595 599 """execute a <cmd> on server
596 600
597 601 The command is expected to be related to a push. Push has a special
598 602 return method.
599 603
600 604 returns the server reply as a (ret, output) tuple. ret is either
601 605 empty (error) or a stringified int.
602 606 """
603 607 raise NotImplementedError()
604 608
605 609 def _calltwowaystream(self, cmd, fp, **args):
606 610 """execute <cmd> on server
607 611
608 612 The command will send a stream to the server and get a stream in reply.
609 613 """
610 614 raise NotImplementedError()
611 615
612 616 def _abort(self, exception):
613 617 """clearly abort the wire protocol connection and raise the exception
614 618 """
615 619 raise NotImplementedError()
General Comments 0
You need to be logged in to leave comments. Login now