##// END OF EJS Templates
localrepo: delete obsolete comment about `prefix in repo` raising exception...
Martin von Zweigbergk -
r46730:c6ae1982 default
parent child Browse files
Show More
@@ -1,3596 +1,3592
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 functools
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 bin,
21 21 hex,
22 22 nullid,
23 23 nullrev,
24 24 short,
25 25 )
26 26 from .pycompat import (
27 27 delattr,
28 28 getattr,
29 29 )
30 30 from . import (
31 31 bookmarks,
32 32 branchmap,
33 33 bundle2,
34 34 bundlecaches,
35 35 changegroup,
36 36 color,
37 37 commit,
38 38 context,
39 39 dirstate,
40 40 dirstateguard,
41 41 discovery,
42 42 encoding,
43 43 error,
44 44 exchange,
45 45 extensions,
46 46 filelog,
47 47 hook,
48 48 lock as lockmod,
49 49 match as matchmod,
50 50 mergestate as mergestatemod,
51 51 mergeutil,
52 52 namespaces,
53 53 narrowspec,
54 54 obsolete,
55 55 pathutil,
56 56 phases,
57 57 pushkey,
58 58 pycompat,
59 59 rcutil,
60 60 repoview,
61 61 requirements as requirementsmod,
62 62 revset,
63 63 revsetlang,
64 64 scmutil,
65 65 sparse,
66 66 store as storemod,
67 67 subrepoutil,
68 68 tags as tagsmod,
69 69 transaction,
70 70 txnutil,
71 71 util,
72 72 vfs as vfsmod,
73 73 )
74 74
75 75 from .interfaces import (
76 76 repository,
77 77 util as interfaceutil,
78 78 )
79 79
80 80 from .utils import (
81 81 hashutil,
82 82 procutil,
83 83 stringutil,
84 84 )
85 85
86 86 from .revlogutils import constants as revlogconst
87 87
88 88 release = lockmod.release
89 89 urlerr = util.urlerr
90 90 urlreq = util.urlreq
91 91
92 92 # set of (path, vfs-location) tuples. vfs-location is:
93 93 # - 'plain for vfs relative paths
94 94 # - '' for svfs relative paths
95 95 _cachedfiles = set()
96 96
97 97
98 98 class _basefilecache(scmutil.filecache):
99 99 """All filecache usage on repo are done for logic that should be unfiltered"""
100 100
101 101 def __get__(self, repo, type=None):
102 102 if repo is None:
103 103 return self
104 104 # proxy to unfiltered __dict__ since filtered repo has no entry
105 105 unfi = repo.unfiltered()
106 106 try:
107 107 return unfi.__dict__[self.sname]
108 108 except KeyError:
109 109 pass
110 110 return super(_basefilecache, self).__get__(unfi, type)
111 111
112 112 def set(self, repo, value):
113 113 return super(_basefilecache, self).set(repo.unfiltered(), value)
114 114
115 115
116 116 class repofilecache(_basefilecache):
117 117 """filecache for files in .hg but outside of .hg/store"""
118 118
119 119 def __init__(self, *paths):
120 120 super(repofilecache, self).__init__(*paths)
121 121 for path in paths:
122 122 _cachedfiles.add((path, b'plain'))
123 123
124 124 def join(self, obj, fname):
125 125 return obj.vfs.join(fname)
126 126
127 127
128 128 class storecache(_basefilecache):
129 129 """filecache for files in the store"""
130 130
131 131 def __init__(self, *paths):
132 132 super(storecache, self).__init__(*paths)
133 133 for path in paths:
134 134 _cachedfiles.add((path, b''))
135 135
136 136 def join(self, obj, fname):
137 137 return obj.sjoin(fname)
138 138
139 139
140 140 class mixedrepostorecache(_basefilecache):
141 141 """filecache for a mix files in .hg/store and outside"""
142 142
143 143 def __init__(self, *pathsandlocations):
144 144 # scmutil.filecache only uses the path for passing back into our
145 145 # join(), so we can safely pass a list of paths and locations
146 146 super(mixedrepostorecache, self).__init__(*pathsandlocations)
147 147 _cachedfiles.update(pathsandlocations)
148 148
149 149 def join(self, obj, fnameandlocation):
150 150 fname, location = fnameandlocation
151 151 if location == b'plain':
152 152 return obj.vfs.join(fname)
153 153 else:
154 154 if location != b'':
155 155 raise error.ProgrammingError(
156 156 b'unexpected location: %s' % location
157 157 )
158 158 return obj.sjoin(fname)
159 159
160 160
161 161 def isfilecached(repo, name):
162 162 """check if a repo has already cached "name" filecache-ed property
163 163
164 164 This returns (cachedobj-or-None, iscached) tuple.
165 165 """
166 166 cacheentry = repo.unfiltered()._filecache.get(name, None)
167 167 if not cacheentry:
168 168 return None, False
169 169 return cacheentry.obj, True
170 170
171 171
172 172 class unfilteredpropertycache(util.propertycache):
173 173 """propertycache that apply to unfiltered repo only"""
174 174
175 175 def __get__(self, repo, type=None):
176 176 unfi = repo.unfiltered()
177 177 if unfi is repo:
178 178 return super(unfilteredpropertycache, self).__get__(unfi)
179 179 return getattr(unfi, self.name)
180 180
181 181
182 182 class filteredpropertycache(util.propertycache):
183 183 """propertycache that must take filtering in account"""
184 184
185 185 def cachevalue(self, obj, value):
186 186 object.__setattr__(obj, self.name, value)
187 187
188 188
189 189 def hasunfilteredcache(repo, name):
190 190 """check if a repo has an unfilteredpropertycache value for <name>"""
191 191 return name in vars(repo.unfiltered())
192 192
193 193
194 194 def unfilteredmethod(orig):
195 195 """decorate method that always need to be run on unfiltered version"""
196 196
197 197 @functools.wraps(orig)
198 198 def wrapper(repo, *args, **kwargs):
199 199 return orig(repo.unfiltered(), *args, **kwargs)
200 200
201 201 return wrapper
202 202
203 203
204 204 moderncaps = {
205 205 b'lookup',
206 206 b'branchmap',
207 207 b'pushkey',
208 208 b'known',
209 209 b'getbundle',
210 210 b'unbundle',
211 211 }
212 212 legacycaps = moderncaps.union({b'changegroupsubset'})
213 213
214 214
215 215 @interfaceutil.implementer(repository.ipeercommandexecutor)
216 216 class localcommandexecutor(object):
217 217 def __init__(self, peer):
218 218 self._peer = peer
219 219 self._sent = False
220 220 self._closed = False
221 221
222 222 def __enter__(self):
223 223 return self
224 224
225 225 def __exit__(self, exctype, excvalue, exctb):
226 226 self.close()
227 227
228 228 def callcommand(self, command, args):
229 229 if self._sent:
230 230 raise error.ProgrammingError(
231 231 b'callcommand() cannot be used after sendcommands()'
232 232 )
233 233
234 234 if self._closed:
235 235 raise error.ProgrammingError(
236 236 b'callcommand() cannot be used after close()'
237 237 )
238 238
239 239 # We don't need to support anything fancy. Just call the named
240 240 # method on the peer and return a resolved future.
241 241 fn = getattr(self._peer, pycompat.sysstr(command))
242 242
243 243 f = pycompat.futures.Future()
244 244
245 245 try:
246 246 result = fn(**pycompat.strkwargs(args))
247 247 except Exception:
248 248 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
249 249 else:
250 250 f.set_result(result)
251 251
252 252 return f
253 253
254 254 def sendcommands(self):
255 255 self._sent = True
256 256
257 257 def close(self):
258 258 self._closed = True
259 259
260 260
261 261 @interfaceutil.implementer(repository.ipeercommands)
262 262 class localpeer(repository.peer):
263 263 '''peer for a local repo; reflects only the most recent API'''
264 264
265 265 def __init__(self, repo, caps=None):
266 266 super(localpeer, self).__init__()
267 267
268 268 if caps is None:
269 269 caps = moderncaps.copy()
270 270 self._repo = repo.filtered(b'served')
271 271 self.ui = repo.ui
272 272 self._caps = repo._restrictcapabilities(caps)
273 273
274 274 # Begin of _basepeer interface.
275 275
276 276 def url(self):
277 277 return self._repo.url()
278 278
279 279 def local(self):
280 280 return self._repo
281 281
282 282 def peer(self):
283 283 return self
284 284
285 285 def canpush(self):
286 286 return True
287 287
288 288 def close(self):
289 289 self._repo.close()
290 290
291 291 # End of _basepeer interface.
292 292
293 293 # Begin of _basewirecommands interface.
294 294
295 295 def branchmap(self):
296 296 return self._repo.branchmap()
297 297
298 298 def capabilities(self):
299 299 return self._caps
300 300
301 301 def clonebundles(self):
302 302 return self._repo.tryread(bundlecaches.CB_MANIFEST_FILE)
303 303
304 304 def debugwireargs(self, one, two, three=None, four=None, five=None):
305 305 """Used to test argument passing over the wire"""
306 306 return b"%s %s %s %s %s" % (
307 307 one,
308 308 two,
309 309 pycompat.bytestr(three),
310 310 pycompat.bytestr(four),
311 311 pycompat.bytestr(five),
312 312 )
313 313
314 314 def getbundle(
315 315 self, source, heads=None, common=None, bundlecaps=None, **kwargs
316 316 ):
317 317 chunks = exchange.getbundlechunks(
318 318 self._repo,
319 319 source,
320 320 heads=heads,
321 321 common=common,
322 322 bundlecaps=bundlecaps,
323 323 **kwargs
324 324 )[1]
325 325 cb = util.chunkbuffer(chunks)
326 326
327 327 if exchange.bundle2requested(bundlecaps):
328 328 # When requesting a bundle2, getbundle returns a stream to make the
329 329 # wire level function happier. We need to build a proper object
330 330 # from it in local peer.
331 331 return bundle2.getunbundler(self.ui, cb)
332 332 else:
333 333 return changegroup.getunbundler(b'01', cb, None)
334 334
335 335 def heads(self):
336 336 return self._repo.heads()
337 337
338 338 def known(self, nodes):
339 339 return self._repo.known(nodes)
340 340
341 341 def listkeys(self, namespace):
342 342 return self._repo.listkeys(namespace)
343 343
344 344 def lookup(self, key):
345 345 return self._repo.lookup(key)
346 346
347 347 def pushkey(self, namespace, key, old, new):
348 348 return self._repo.pushkey(namespace, key, old, new)
349 349
350 350 def stream_out(self):
351 351 raise error.Abort(_(b'cannot perform stream clone against local peer'))
352 352
353 353 def unbundle(self, bundle, heads, url):
354 354 """apply a bundle on a repo
355 355
356 356 This function handles the repo locking itself."""
357 357 try:
358 358 try:
359 359 bundle = exchange.readbundle(self.ui, bundle, None)
360 360 ret = exchange.unbundle(self._repo, bundle, heads, b'push', url)
361 361 if util.safehasattr(ret, b'getchunks'):
362 362 # This is a bundle20 object, turn it into an unbundler.
363 363 # This little dance should be dropped eventually when the
364 364 # API is finally improved.
365 365 stream = util.chunkbuffer(ret.getchunks())
366 366 ret = bundle2.getunbundler(self.ui, stream)
367 367 return ret
368 368 except Exception as exc:
369 369 # If the exception contains output salvaged from a bundle2
370 370 # reply, we need to make sure it is printed before continuing
371 371 # to fail. So we build a bundle2 with such output and consume
372 372 # it directly.
373 373 #
374 374 # This is not very elegant but allows a "simple" solution for
375 375 # issue4594
376 376 output = getattr(exc, '_bundle2salvagedoutput', ())
377 377 if output:
378 378 bundler = bundle2.bundle20(self._repo.ui)
379 379 for out in output:
380 380 bundler.addpart(out)
381 381 stream = util.chunkbuffer(bundler.getchunks())
382 382 b = bundle2.getunbundler(self.ui, stream)
383 383 bundle2.processbundle(self._repo, b)
384 384 raise
385 385 except error.PushRaced as exc:
386 386 raise error.ResponseError(
387 387 _(b'push failed:'), stringutil.forcebytestr(exc)
388 388 )
389 389
390 390 # End of _basewirecommands interface.
391 391
392 392 # Begin of peer interface.
393 393
394 394 def commandexecutor(self):
395 395 return localcommandexecutor(self)
396 396
397 397 # End of peer interface.
398 398
399 399
400 400 @interfaceutil.implementer(repository.ipeerlegacycommands)
401 401 class locallegacypeer(localpeer):
402 402 """peer extension which implements legacy methods too; used for tests with
403 403 restricted capabilities"""
404 404
405 405 def __init__(self, repo):
406 406 super(locallegacypeer, self).__init__(repo, caps=legacycaps)
407 407
408 408 # Begin of baselegacywirecommands interface.
409 409
410 410 def between(self, pairs):
411 411 return self._repo.between(pairs)
412 412
413 413 def branches(self, nodes):
414 414 return self._repo.branches(nodes)
415 415
416 416 def changegroup(self, nodes, source):
417 417 outgoing = discovery.outgoing(
418 418 self._repo, missingroots=nodes, ancestorsof=self._repo.heads()
419 419 )
420 420 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
421 421
422 422 def changegroupsubset(self, bases, heads, source):
423 423 outgoing = discovery.outgoing(
424 424 self._repo, missingroots=bases, ancestorsof=heads
425 425 )
426 426 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
427 427
428 428 # End of baselegacywirecommands interface.
429 429
430 430
431 431 # Functions receiving (ui, features) that extensions can register to impact
432 432 # the ability to load repositories with custom requirements. Only
433 433 # functions defined in loaded extensions are called.
434 434 #
435 435 # The function receives a set of requirement strings that the repository
436 436 # is capable of opening. Functions will typically add elements to the
437 437 # set to reflect that the extension knows how to handle that requirements.
438 438 featuresetupfuncs = set()
439 439
440 440
441 441 def _getsharedvfs(hgvfs, requirements):
442 442 """returns the vfs object pointing to root of shared source
443 443 repo for a shared repository
444 444
445 445 hgvfs is vfs pointing at .hg/ of current repo (shared one)
446 446 requirements is a set of requirements of current repo (shared one)
447 447 """
448 448 # The ``shared`` or ``relshared`` requirements indicate the
449 449 # store lives in the path contained in the ``.hg/sharedpath`` file.
450 450 # This is an absolute path for ``shared`` and relative to
451 451 # ``.hg/`` for ``relshared``.
452 452 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
453 453 if requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements:
454 454 sharedpath = hgvfs.join(sharedpath)
455 455
456 456 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
457 457
458 458 if not sharedvfs.exists():
459 459 raise error.RepoError(
460 460 _(b'.hg/sharedpath points to nonexistent directory %s')
461 461 % sharedvfs.base
462 462 )
463 463 return sharedvfs
464 464
465 465
466 466 def _readrequires(vfs, allowmissing):
467 467 """reads the require file present at root of this vfs
468 468 and return a set of requirements
469 469
470 470 If allowmissing is True, we suppress ENOENT if raised"""
471 471 # requires file contains a newline-delimited list of
472 472 # features/capabilities the opener (us) must have in order to use
473 473 # the repository. This file was introduced in Mercurial 0.9.2,
474 474 # which means very old repositories may not have one. We assume
475 475 # a missing file translates to no requirements.
476 476 try:
477 477 requirements = set(vfs.read(b'requires').splitlines())
478 478 except IOError as e:
479 479 if not (allowmissing and e.errno == errno.ENOENT):
480 480 raise
481 481 requirements = set()
482 482 return requirements
483 483
484 484
485 485 def makelocalrepository(baseui, path, intents=None):
486 486 """Create a local repository object.
487 487
488 488 Given arguments needed to construct a local repository, this function
489 489 performs various early repository loading functionality (such as
490 490 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
491 491 the repository can be opened, derives a type suitable for representing
492 492 that repository, and returns an instance of it.
493 493
494 494 The returned object conforms to the ``repository.completelocalrepository``
495 495 interface.
496 496
497 497 The repository type is derived by calling a series of factory functions
498 498 for each aspect/interface of the final repository. These are defined by
499 499 ``REPO_INTERFACES``.
500 500
501 501 Each factory function is called to produce a type implementing a specific
502 502 interface. The cumulative list of returned types will be combined into a
503 503 new type and that type will be instantiated to represent the local
504 504 repository.
505 505
506 506 The factory functions each receive various state that may be consulted
507 507 as part of deriving a type.
508 508
509 509 Extensions should wrap these factory functions to customize repository type
510 510 creation. Note that an extension's wrapped function may be called even if
511 511 that extension is not loaded for the repo being constructed. Extensions
512 512 should check if their ``__name__`` appears in the
513 513 ``extensionmodulenames`` set passed to the factory function and no-op if
514 514 not.
515 515 """
516 516 ui = baseui.copy()
517 517 # Prevent copying repo configuration.
518 518 ui.copy = baseui.copy
519 519
520 520 # Working directory VFS rooted at repository root.
521 521 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
522 522
523 523 # Main VFS for .hg/ directory.
524 524 hgpath = wdirvfs.join(b'.hg')
525 525 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
526 526 # Whether this repository is shared one or not
527 527 shared = False
528 528 # If this repository is shared, vfs pointing to shared repo
529 529 sharedvfs = None
530 530
531 531 # The .hg/ path should exist and should be a directory. All other
532 532 # cases are errors.
533 533 if not hgvfs.isdir():
534 534 try:
535 535 hgvfs.stat()
536 536 except OSError as e:
537 537 if e.errno != errno.ENOENT:
538 538 raise
539 539 except ValueError as e:
540 540 # Can be raised on Python 3.8 when path is invalid.
541 541 raise error.Abort(
542 542 _(b'invalid path %s: %s') % (path, pycompat.bytestr(e))
543 543 )
544 544
545 545 raise error.RepoError(_(b'repository %s not found') % path)
546 546
547 547 requirements = _readrequires(hgvfs, True)
548 548 shared = (
549 549 requirementsmod.SHARED_REQUIREMENT in requirements
550 550 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
551 551 )
552 552 if shared:
553 553 sharedvfs = _getsharedvfs(hgvfs, requirements)
554 554
555 555 # if .hg/requires contains the sharesafe requirement, it means
556 556 # there exists a `.hg/store/requires` too and we should read it
557 557 # NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
558 558 # is present. We never write SHARESAFE_REQUIREMENT for a repo if store
559 559 # is not present, refer checkrequirementscompat() for that
560 560 #
561 561 # However, if SHARESAFE_REQUIREMENT is not present, it means that the
562 562 # repository was shared the old way. We check the share source .hg/requires
563 563 # for SHARESAFE_REQUIREMENT to detect whether the current repository needs
564 564 # to be reshared
565 565 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
566 566
567 567 if (
568 568 shared
569 569 and requirementsmod.SHARESAFE_REQUIREMENT
570 570 not in _readrequires(sharedvfs, True)
571 571 ):
572 572 raise error.Abort(
573 573 _(b"share source does not support exp-sharesafe requirement")
574 574 )
575 575
576 576 if shared:
577 577 # This is a shared repo
578 578 storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
579 579 else:
580 580 storevfs = vfsmod.vfs(hgvfs.join(b'store'))
581 581
582 582 requirements |= _readrequires(storevfs, False)
583 583 elif shared:
584 584 sourcerequires = _readrequires(sharedvfs, False)
585 585 if requirementsmod.SHARESAFE_REQUIREMENT in sourcerequires:
586 586 ui.warn(
587 587 _(
588 588 b'warning: source repository supports share-safe functionality.'
589 589 b' Reshare to upgrade.\n'
590 590 )
591 591 )
592 592
593 593 # The .hg/hgrc file may load extensions or contain config options
594 594 # that influence repository construction. Attempt to load it and
595 595 # process any new extensions that it may have pulled in.
596 596 if loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs):
597 597 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
598 598 extensions.loadall(ui)
599 599 extensions.populateui(ui)
600 600
601 601 # Set of module names of extensions loaded for this repository.
602 602 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
603 603
604 604 supportedrequirements = gathersupportedrequirements(ui)
605 605
606 606 # We first validate the requirements are known.
607 607 ensurerequirementsrecognized(requirements, supportedrequirements)
608 608
609 609 # Then we validate that the known set is reasonable to use together.
610 610 ensurerequirementscompatible(ui, requirements)
611 611
612 612 # TODO there are unhandled edge cases related to opening repositories with
613 613 # shared storage. If storage is shared, we should also test for requirements
614 614 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
615 615 # that repo, as that repo may load extensions needed to open it. This is a
616 616 # bit complicated because we don't want the other hgrc to overwrite settings
617 617 # in this hgrc.
618 618 #
619 619 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
620 620 # file when sharing repos. But if a requirement is added after the share is
621 621 # performed, thereby introducing a new requirement for the opener, we may
622 622 # will not see that and could encounter a run-time error interacting with
623 623 # that shared store since it has an unknown-to-us requirement.
624 624
625 625 # At this point, we know we should be capable of opening the repository.
626 626 # Now get on with doing that.
627 627
628 628 features = set()
629 629
630 630 # The "store" part of the repository holds versioned data. How it is
631 631 # accessed is determined by various requirements. If `shared` or
632 632 # `relshared` requirements are present, this indicates current repository
633 633 # is a share and store exists in path mentioned in `.hg/sharedpath`
634 634 if shared:
635 635 storebasepath = sharedvfs.base
636 636 cachepath = sharedvfs.join(b'cache')
637 637 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
638 638 else:
639 639 storebasepath = hgvfs.base
640 640 cachepath = hgvfs.join(b'cache')
641 641 wcachepath = hgvfs.join(b'wcache')
642 642
643 643 # The store has changed over time and the exact layout is dictated by
644 644 # requirements. The store interface abstracts differences across all
645 645 # of them.
646 646 store = makestore(
647 647 requirements,
648 648 storebasepath,
649 649 lambda base: vfsmod.vfs(base, cacheaudited=True),
650 650 )
651 651 hgvfs.createmode = store.createmode
652 652
653 653 storevfs = store.vfs
654 654 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
655 655
656 656 # The cache vfs is used to manage cache files.
657 657 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
658 658 cachevfs.createmode = store.createmode
659 659 # The cache vfs is used to manage cache files related to the working copy
660 660 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
661 661 wcachevfs.createmode = store.createmode
662 662
663 663 # Now resolve the type for the repository object. We do this by repeatedly
664 664 # calling a factory function to produces types for specific aspects of the
665 665 # repo's operation. The aggregate returned types are used as base classes
666 666 # for a dynamically-derived type, which will represent our new repository.
667 667
668 668 bases = []
669 669 extrastate = {}
670 670
671 671 for iface, fn in REPO_INTERFACES:
672 672 # We pass all potentially useful state to give extensions tons of
673 673 # flexibility.
674 674 typ = fn()(
675 675 ui=ui,
676 676 intents=intents,
677 677 requirements=requirements,
678 678 features=features,
679 679 wdirvfs=wdirvfs,
680 680 hgvfs=hgvfs,
681 681 store=store,
682 682 storevfs=storevfs,
683 683 storeoptions=storevfs.options,
684 684 cachevfs=cachevfs,
685 685 wcachevfs=wcachevfs,
686 686 extensionmodulenames=extensionmodulenames,
687 687 extrastate=extrastate,
688 688 baseclasses=bases,
689 689 )
690 690
691 691 if not isinstance(typ, type):
692 692 raise error.ProgrammingError(
693 693 b'unable to construct type for %s' % iface
694 694 )
695 695
696 696 bases.append(typ)
697 697
698 698 # type() allows you to use characters in type names that wouldn't be
699 699 # recognized as Python symbols in source code. We abuse that to add
700 700 # rich information about our constructed repo.
701 701 name = pycompat.sysstr(
702 702 b'derivedrepo:%s<%s>' % (wdirvfs.base, b','.join(sorted(requirements)))
703 703 )
704 704
705 705 cls = type(name, tuple(bases), {})
706 706
707 707 return cls(
708 708 baseui=baseui,
709 709 ui=ui,
710 710 origroot=path,
711 711 wdirvfs=wdirvfs,
712 712 hgvfs=hgvfs,
713 713 requirements=requirements,
714 714 supportedrequirements=supportedrequirements,
715 715 sharedpath=storebasepath,
716 716 store=store,
717 717 cachevfs=cachevfs,
718 718 wcachevfs=wcachevfs,
719 719 features=features,
720 720 intents=intents,
721 721 )
722 722
723 723
724 724 def loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs=None):
725 725 """Load hgrc files/content into a ui instance.
726 726
727 727 This is called during repository opening to load any additional
728 728 config files or settings relevant to the current repository.
729 729
730 730 Returns a bool indicating whether any additional configs were loaded.
731 731
732 732 Extensions should monkeypatch this function to modify how per-repo
733 733 configs are loaded. For example, an extension may wish to pull in
734 734 configs from alternate files or sources.
735 735
736 736 sharedvfs is vfs object pointing to source repo if the current one is a
737 737 shared one
738 738 """
739 739 if not rcutil.use_repo_hgrc():
740 740 return False
741 741
742 742 ret = False
743 743 # first load config from shared source if we has to
744 744 if requirementsmod.SHARESAFE_REQUIREMENT in requirements and sharedvfs:
745 745 try:
746 746 ui.readconfig(sharedvfs.join(b'hgrc'), root=sharedvfs.base)
747 747 ret = True
748 748 except IOError:
749 749 pass
750 750
751 751 try:
752 752 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
753 753 ret = True
754 754 except IOError:
755 755 pass
756 756
757 757 try:
758 758 ui.readconfig(hgvfs.join(b'hgrc-not-shared'), root=wdirvfs.base)
759 759 ret = True
760 760 except IOError:
761 761 pass
762 762
763 763 return ret
764 764
765 765
766 766 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
767 767 """Perform additional actions after .hg/hgrc is loaded.
768 768
769 769 This function is called during repository loading immediately after
770 770 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
771 771
772 772 The function can be used to validate configs, automatically add
773 773 options (including extensions) based on requirements, etc.
774 774 """
775 775
776 776 # Map of requirements to list of extensions to load automatically when
777 777 # requirement is present.
778 778 autoextensions = {
779 779 b'git': [b'git'],
780 780 b'largefiles': [b'largefiles'],
781 781 b'lfs': [b'lfs'],
782 782 }
783 783
784 784 for requirement, names in sorted(autoextensions.items()):
785 785 if requirement not in requirements:
786 786 continue
787 787
788 788 for name in names:
789 789 if not ui.hasconfig(b'extensions', name):
790 790 ui.setconfig(b'extensions', name, b'', source=b'autoload')
791 791
792 792
793 793 def gathersupportedrequirements(ui):
794 794 """Determine the complete set of recognized requirements."""
795 795 # Start with all requirements supported by this file.
796 796 supported = set(localrepository._basesupported)
797 797
798 798 # Execute ``featuresetupfuncs`` entries if they belong to an extension
799 799 # relevant to this ui instance.
800 800 modules = {m.__name__ for n, m in extensions.extensions(ui)}
801 801
802 802 for fn in featuresetupfuncs:
803 803 if fn.__module__ in modules:
804 804 fn(ui, supported)
805 805
806 806 # Add derived requirements from registered compression engines.
807 807 for name in util.compengines:
808 808 engine = util.compengines[name]
809 809 if engine.available() and engine.revlogheader():
810 810 supported.add(b'exp-compression-%s' % name)
811 811 if engine.name() == b'zstd':
812 812 supported.add(b'revlog-compression-zstd')
813 813
814 814 return supported
815 815
816 816
817 817 def ensurerequirementsrecognized(requirements, supported):
818 818 """Validate that a set of local requirements is recognized.
819 819
820 820 Receives a set of requirements. Raises an ``error.RepoError`` if there
821 821 exists any requirement in that set that currently loaded code doesn't
822 822 recognize.
823 823
824 824 Returns a set of supported requirements.
825 825 """
826 826 missing = set()
827 827
828 828 for requirement in requirements:
829 829 if requirement in supported:
830 830 continue
831 831
832 832 if not requirement or not requirement[0:1].isalnum():
833 833 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
834 834
835 835 missing.add(requirement)
836 836
837 837 if missing:
838 838 raise error.RequirementError(
839 839 _(b'repository requires features unknown to this Mercurial: %s')
840 840 % b' '.join(sorted(missing)),
841 841 hint=_(
842 842 b'see https://mercurial-scm.org/wiki/MissingRequirement '
843 843 b'for more information'
844 844 ),
845 845 )
846 846
847 847
848 848 def ensurerequirementscompatible(ui, requirements):
849 849 """Validates that a set of recognized requirements is mutually compatible.
850 850
851 851 Some requirements may not be compatible with others or require
852 852 config options that aren't enabled. This function is called during
853 853 repository opening to ensure that the set of requirements needed
854 854 to open a repository is sane and compatible with config options.
855 855
856 856 Extensions can monkeypatch this function to perform additional
857 857 checking.
858 858
859 859 ``error.RepoError`` should be raised on failure.
860 860 """
861 861 if (
862 862 requirementsmod.SPARSE_REQUIREMENT in requirements
863 863 and not sparse.enabled
864 864 ):
865 865 raise error.RepoError(
866 866 _(
867 867 b'repository is using sparse feature but '
868 868 b'sparse is not enabled; enable the '
869 869 b'"sparse" extensions to access'
870 870 )
871 871 )
872 872
873 873
874 874 def makestore(requirements, path, vfstype):
875 875 """Construct a storage object for a repository."""
876 876 if b'store' in requirements:
877 877 if b'fncache' in requirements:
878 878 return storemod.fncachestore(
879 879 path, vfstype, b'dotencode' in requirements
880 880 )
881 881
882 882 return storemod.encodedstore(path, vfstype)
883 883
884 884 return storemod.basicstore(path, vfstype)
885 885
886 886
887 887 def resolvestorevfsoptions(ui, requirements, features):
888 888 """Resolve the options to pass to the store vfs opener.
889 889
890 890 The returned dict is used to influence behavior of the storage layer.
891 891 """
892 892 options = {}
893 893
894 894 if requirementsmod.TREEMANIFEST_REQUIREMENT in requirements:
895 895 options[b'treemanifest'] = True
896 896
897 897 # experimental config: format.manifestcachesize
898 898 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
899 899 if manifestcachesize is not None:
900 900 options[b'manifestcachesize'] = manifestcachesize
901 901
902 902 # In the absence of another requirement superseding a revlog-related
903 903 # requirement, we have to assume the repo is using revlog version 0.
904 904 # This revlog format is super old and we don't bother trying to parse
905 905 # opener options for it because those options wouldn't do anything
906 906 # meaningful on such old repos.
907 907 if (
908 908 b'revlogv1' in requirements
909 909 or requirementsmod.REVLOGV2_REQUIREMENT in requirements
910 910 ):
911 911 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
912 912 else: # explicitly mark repo as using revlogv0
913 913 options[b'revlogv0'] = True
914 914
915 915 if requirementsmod.COPIESSDC_REQUIREMENT in requirements:
916 916 options[b'copies-storage'] = b'changeset-sidedata'
917 917 else:
918 918 writecopiesto = ui.config(b'experimental', b'copies.write-to')
919 919 copiesextramode = (b'changeset-only', b'compatibility')
920 920 if writecopiesto in copiesextramode:
921 921 options[b'copies-storage'] = b'extra'
922 922
923 923 return options
924 924
925 925
926 926 def resolverevlogstorevfsoptions(ui, requirements, features):
927 927 """Resolve opener options specific to revlogs."""
928 928
929 929 options = {}
930 930 options[b'flagprocessors'] = {}
931 931
932 932 if b'revlogv1' in requirements:
933 933 options[b'revlogv1'] = True
934 934 if requirementsmod.REVLOGV2_REQUIREMENT in requirements:
935 935 options[b'revlogv2'] = True
936 936
937 937 if b'generaldelta' in requirements:
938 938 options[b'generaldelta'] = True
939 939
940 940 # experimental config: format.chunkcachesize
941 941 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
942 942 if chunkcachesize is not None:
943 943 options[b'chunkcachesize'] = chunkcachesize
944 944
945 945 deltabothparents = ui.configbool(
946 946 b'storage', b'revlog.optimize-delta-parent-choice'
947 947 )
948 948 options[b'deltabothparents'] = deltabothparents
949 949
950 950 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
951 951 lazydeltabase = False
952 952 if lazydelta:
953 953 lazydeltabase = ui.configbool(
954 954 b'storage', b'revlog.reuse-external-delta-parent'
955 955 )
956 956 if lazydeltabase is None:
957 957 lazydeltabase = not scmutil.gddeltaconfig(ui)
958 958 options[b'lazydelta'] = lazydelta
959 959 options[b'lazydeltabase'] = lazydeltabase
960 960
961 961 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
962 962 if 0 <= chainspan:
963 963 options[b'maxdeltachainspan'] = chainspan
964 964
965 965 mmapindexthreshold = ui.configbytes(b'experimental', b'mmapindexthreshold')
966 966 if mmapindexthreshold is not None:
967 967 options[b'mmapindexthreshold'] = mmapindexthreshold
968 968
969 969 withsparseread = ui.configbool(b'experimental', b'sparse-read')
970 970 srdensitythres = float(
971 971 ui.config(b'experimental', b'sparse-read.density-threshold')
972 972 )
973 973 srmingapsize = ui.configbytes(b'experimental', b'sparse-read.min-gap-size')
974 974 options[b'with-sparse-read'] = withsparseread
975 975 options[b'sparse-read-density-threshold'] = srdensitythres
976 976 options[b'sparse-read-min-gap-size'] = srmingapsize
977 977
978 978 sparserevlog = requirementsmod.SPARSEREVLOG_REQUIREMENT in requirements
979 979 options[b'sparse-revlog'] = sparserevlog
980 980 if sparserevlog:
981 981 options[b'generaldelta'] = True
982 982
983 983 sidedata = requirementsmod.SIDEDATA_REQUIREMENT in requirements
984 984 options[b'side-data'] = sidedata
985 985
986 986 maxchainlen = None
987 987 if sparserevlog:
988 988 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
989 989 # experimental config: format.maxchainlen
990 990 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
991 991 if maxchainlen is not None:
992 992 options[b'maxchainlen'] = maxchainlen
993 993
994 994 for r in requirements:
995 995 # we allow multiple compression engine requirement to co-exist because
996 996 # strickly speaking, revlog seems to support mixed compression style.
997 997 #
998 998 # The compression used for new entries will be "the last one"
999 999 prefix = r.startswith
1000 1000 if prefix(b'revlog-compression-') or prefix(b'exp-compression-'):
1001 1001 options[b'compengine'] = r.split(b'-', 2)[2]
1002 1002
1003 1003 options[b'zlib.level'] = ui.configint(b'storage', b'revlog.zlib.level')
1004 1004 if options[b'zlib.level'] is not None:
1005 1005 if not (0 <= options[b'zlib.level'] <= 9):
1006 1006 msg = _(b'invalid value for `storage.revlog.zlib.level` config: %d')
1007 1007 raise error.Abort(msg % options[b'zlib.level'])
1008 1008 options[b'zstd.level'] = ui.configint(b'storage', b'revlog.zstd.level')
1009 1009 if options[b'zstd.level'] is not None:
1010 1010 if not (0 <= options[b'zstd.level'] <= 22):
1011 1011 msg = _(b'invalid value for `storage.revlog.zstd.level` config: %d')
1012 1012 raise error.Abort(msg % options[b'zstd.level'])
1013 1013
1014 1014 if requirementsmod.NARROW_REQUIREMENT in requirements:
1015 1015 options[b'enableellipsis'] = True
1016 1016
1017 1017 if ui.configbool(b'experimental', b'rust.index'):
1018 1018 options[b'rust.index'] = True
1019 1019 if requirementsmod.NODEMAP_REQUIREMENT in requirements:
1020 1020 options[b'persistent-nodemap'] = True
1021 1021 if ui.configbool(b'storage', b'revlog.nodemap.mmap'):
1022 1022 options[b'persistent-nodemap.mmap'] = True
1023 1023 epnm = ui.config(b'storage', b'revlog.nodemap.mode')
1024 1024 options[b'persistent-nodemap.mode'] = epnm
1025 1025 if ui.configbool(b'devel', b'persistent-nodemap'):
1026 1026 options[b'devel-force-nodemap'] = True
1027 1027
1028 1028 return options
1029 1029
1030 1030
1031 1031 def makemain(**kwargs):
1032 1032 """Produce a type conforming to ``ilocalrepositorymain``."""
1033 1033 return localrepository
1034 1034
1035 1035
1036 1036 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1037 1037 class revlogfilestorage(object):
1038 1038 """File storage when using revlogs."""
1039 1039
1040 1040 def file(self, path):
1041 1041 if path[0] == b'/':
1042 1042 path = path[1:]
1043 1043
1044 1044 return filelog.filelog(self.svfs, path)
1045 1045
1046 1046
1047 1047 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1048 1048 class revlognarrowfilestorage(object):
1049 1049 """File storage when using revlogs and narrow files."""
1050 1050
1051 1051 def file(self, path):
1052 1052 if path[0] == b'/':
1053 1053 path = path[1:]
1054 1054
1055 1055 return filelog.narrowfilelog(self.svfs, path, self._storenarrowmatch)
1056 1056
1057 1057
1058 1058 def makefilestorage(requirements, features, **kwargs):
1059 1059 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
1060 1060 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
1061 1061 features.add(repository.REPO_FEATURE_STREAM_CLONE)
1062 1062
1063 1063 if requirementsmod.NARROW_REQUIREMENT in requirements:
1064 1064 return revlognarrowfilestorage
1065 1065 else:
1066 1066 return revlogfilestorage
1067 1067
1068 1068
1069 1069 # List of repository interfaces and factory functions for them. Each
1070 1070 # will be called in order during ``makelocalrepository()`` to iteratively
1071 1071 # derive the final type for a local repository instance. We capture the
1072 1072 # function as a lambda so we don't hold a reference and the module-level
1073 1073 # functions can be wrapped.
1074 1074 REPO_INTERFACES = [
1075 1075 (repository.ilocalrepositorymain, lambda: makemain),
1076 1076 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
1077 1077 ]
1078 1078
1079 1079
1080 1080 @interfaceutil.implementer(repository.ilocalrepositorymain)
1081 1081 class localrepository(object):
1082 1082 """Main class for representing local repositories.
1083 1083
1084 1084 All local repositories are instances of this class.
1085 1085
1086 1086 Constructed on its own, instances of this class are not usable as
1087 1087 repository objects. To obtain a usable repository object, call
1088 1088 ``hg.repository()``, ``localrepo.instance()``, or
1089 1089 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
1090 1090 ``instance()`` adds support for creating new repositories.
1091 1091 ``hg.repository()`` adds more extension integration, including calling
1092 1092 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
1093 1093 used.
1094 1094 """
1095 1095
1096 1096 # obsolete experimental requirements:
1097 1097 # - manifestv2: An experimental new manifest format that allowed
1098 1098 # for stem compression of long paths. Experiment ended up not
1099 1099 # being successful (repository sizes went up due to worse delta
1100 1100 # chains), and the code was deleted in 4.6.
1101 1101 supportedformats = {
1102 1102 b'revlogv1',
1103 1103 b'generaldelta',
1104 1104 requirementsmod.TREEMANIFEST_REQUIREMENT,
1105 1105 requirementsmod.COPIESSDC_REQUIREMENT,
1106 1106 requirementsmod.REVLOGV2_REQUIREMENT,
1107 1107 requirementsmod.SIDEDATA_REQUIREMENT,
1108 1108 requirementsmod.SPARSEREVLOG_REQUIREMENT,
1109 1109 requirementsmod.NODEMAP_REQUIREMENT,
1110 1110 bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT,
1111 1111 requirementsmod.SHARESAFE_REQUIREMENT,
1112 1112 }
1113 1113 _basesupported = supportedformats | {
1114 1114 b'store',
1115 1115 b'fncache',
1116 1116 requirementsmod.SHARED_REQUIREMENT,
1117 1117 requirementsmod.RELATIVE_SHARED_REQUIREMENT,
1118 1118 b'dotencode',
1119 1119 requirementsmod.SPARSE_REQUIREMENT,
1120 1120 requirementsmod.INTERNAL_PHASE_REQUIREMENT,
1121 1121 }
1122 1122
1123 1123 # list of prefix for file which can be written without 'wlock'
1124 1124 # Extensions should extend this list when needed
1125 1125 _wlockfreeprefix = {
1126 1126 # We migh consider requiring 'wlock' for the next
1127 1127 # two, but pretty much all the existing code assume
1128 1128 # wlock is not needed so we keep them excluded for
1129 1129 # now.
1130 1130 b'hgrc',
1131 1131 b'requires',
1132 1132 # XXX cache is a complicatged business someone
1133 1133 # should investigate this in depth at some point
1134 1134 b'cache/',
1135 1135 # XXX shouldn't be dirstate covered by the wlock?
1136 1136 b'dirstate',
1137 1137 # XXX bisect was still a bit too messy at the time
1138 1138 # this changeset was introduced. Someone should fix
1139 1139 # the remainig bit and drop this line
1140 1140 b'bisect.state',
1141 1141 }
1142 1142
1143 1143 def __init__(
1144 1144 self,
1145 1145 baseui,
1146 1146 ui,
1147 1147 origroot,
1148 1148 wdirvfs,
1149 1149 hgvfs,
1150 1150 requirements,
1151 1151 supportedrequirements,
1152 1152 sharedpath,
1153 1153 store,
1154 1154 cachevfs,
1155 1155 wcachevfs,
1156 1156 features,
1157 1157 intents=None,
1158 1158 ):
1159 1159 """Create a new local repository instance.
1160 1160
1161 1161 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
1162 1162 or ``localrepo.makelocalrepository()`` for obtaining a new repository
1163 1163 object.
1164 1164
1165 1165 Arguments:
1166 1166
1167 1167 baseui
1168 1168 ``ui.ui`` instance that ``ui`` argument was based off of.
1169 1169
1170 1170 ui
1171 1171 ``ui.ui`` instance for use by the repository.
1172 1172
1173 1173 origroot
1174 1174 ``bytes`` path to working directory root of this repository.
1175 1175
1176 1176 wdirvfs
1177 1177 ``vfs.vfs`` rooted at the working directory.
1178 1178
1179 1179 hgvfs
1180 1180 ``vfs.vfs`` rooted at .hg/
1181 1181
1182 1182 requirements
1183 1183 ``set`` of bytestrings representing repository opening requirements.
1184 1184
1185 1185 supportedrequirements
1186 1186 ``set`` of bytestrings representing repository requirements that we
1187 1187 know how to open. May be a supetset of ``requirements``.
1188 1188
1189 1189 sharedpath
1190 1190 ``bytes`` Defining path to storage base directory. Points to a
1191 1191 ``.hg/`` directory somewhere.
1192 1192
1193 1193 store
1194 1194 ``store.basicstore`` (or derived) instance providing access to
1195 1195 versioned storage.
1196 1196
1197 1197 cachevfs
1198 1198 ``vfs.vfs`` used for cache files.
1199 1199
1200 1200 wcachevfs
1201 1201 ``vfs.vfs`` used for cache files related to the working copy.
1202 1202
1203 1203 features
1204 1204 ``set`` of bytestrings defining features/capabilities of this
1205 1205 instance.
1206 1206
1207 1207 intents
1208 1208 ``set`` of system strings indicating what this repo will be used
1209 1209 for.
1210 1210 """
1211 1211 self.baseui = baseui
1212 1212 self.ui = ui
1213 1213 self.origroot = origroot
1214 1214 # vfs rooted at working directory.
1215 1215 self.wvfs = wdirvfs
1216 1216 self.root = wdirvfs.base
1217 1217 # vfs rooted at .hg/. Used to access most non-store paths.
1218 1218 self.vfs = hgvfs
1219 1219 self.path = hgvfs.base
1220 1220 self.requirements = requirements
1221 1221 self.supported = supportedrequirements
1222 1222 self.sharedpath = sharedpath
1223 1223 self.store = store
1224 1224 self.cachevfs = cachevfs
1225 1225 self.wcachevfs = wcachevfs
1226 1226 self.features = features
1227 1227
1228 1228 self.filtername = None
1229 1229
1230 1230 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1231 1231 b'devel', b'check-locks'
1232 1232 ):
1233 1233 self.vfs.audit = self._getvfsward(self.vfs.audit)
1234 1234 # A list of callback to shape the phase if no data were found.
1235 1235 # Callback are in the form: func(repo, roots) --> processed root.
1236 1236 # This list it to be filled by extension during repo setup
1237 1237 self._phasedefaults = []
1238 1238
1239 1239 color.setup(self.ui)
1240 1240
1241 1241 self.spath = self.store.path
1242 1242 self.svfs = self.store.vfs
1243 1243 self.sjoin = self.store.join
1244 1244 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1245 1245 b'devel', b'check-locks'
1246 1246 ):
1247 1247 if util.safehasattr(self.svfs, b'vfs'): # this is filtervfs
1248 1248 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1249 1249 else: # standard vfs
1250 1250 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1251 1251
1252 1252 self._dirstatevalidatewarned = False
1253 1253
1254 1254 self._branchcaches = branchmap.BranchMapCache()
1255 1255 self._revbranchcache = None
1256 1256 self._filterpats = {}
1257 1257 self._datafilters = {}
1258 1258 self._transref = self._lockref = self._wlockref = None
1259 1259
1260 1260 # A cache for various files under .hg/ that tracks file changes,
1261 1261 # (used by the filecache decorator)
1262 1262 #
1263 1263 # Maps a property name to its util.filecacheentry
1264 1264 self._filecache = {}
1265 1265
1266 1266 # hold sets of revision to be filtered
1267 1267 # should be cleared when something might have changed the filter value:
1268 1268 # - new changesets,
1269 1269 # - phase change,
1270 1270 # - new obsolescence marker,
1271 1271 # - working directory parent change,
1272 1272 # - bookmark changes
1273 1273 self.filteredrevcache = {}
1274 1274
1275 1275 # post-dirstate-status hooks
1276 1276 self._postdsstatus = []
1277 1277
1278 1278 # generic mapping between names and nodes
1279 1279 self.names = namespaces.namespaces()
1280 1280
1281 1281 # Key to signature value.
1282 1282 self._sparsesignaturecache = {}
1283 1283 # Signature to cached matcher instance.
1284 1284 self._sparsematchercache = {}
1285 1285
1286 1286 self._extrafilterid = repoview.extrafilter(ui)
1287 1287
1288 1288 self.filecopiesmode = None
1289 1289 if requirementsmod.COPIESSDC_REQUIREMENT in self.requirements:
1290 1290 self.filecopiesmode = b'changeset-sidedata'
1291 1291
1292 1292 def _getvfsward(self, origfunc):
1293 1293 """build a ward for self.vfs"""
1294 1294 rref = weakref.ref(self)
1295 1295
1296 1296 def checkvfs(path, mode=None):
1297 1297 ret = origfunc(path, mode=mode)
1298 1298 repo = rref()
1299 1299 if (
1300 1300 repo is None
1301 1301 or not util.safehasattr(repo, b'_wlockref')
1302 1302 or not util.safehasattr(repo, b'_lockref')
1303 1303 ):
1304 1304 return
1305 1305 if mode in (None, b'r', b'rb'):
1306 1306 return
1307 1307 if path.startswith(repo.path):
1308 1308 # truncate name relative to the repository (.hg)
1309 1309 path = path[len(repo.path) + 1 :]
1310 1310 if path.startswith(b'cache/'):
1311 1311 msg = b'accessing cache with vfs instead of cachevfs: "%s"'
1312 1312 repo.ui.develwarn(msg % path, stacklevel=3, config=b"cache-vfs")
1313 1313 # path prefixes covered by 'lock'
1314 1314 vfs_path_prefixes = (
1315 1315 b'journal.',
1316 1316 b'undo.',
1317 1317 b'strip-backup/',
1318 1318 b'cache/',
1319 1319 )
1320 1320 if any(path.startswith(prefix) for prefix in vfs_path_prefixes):
1321 1321 if repo._currentlock(repo._lockref) is None:
1322 1322 repo.ui.develwarn(
1323 1323 b'write with no lock: "%s"' % path,
1324 1324 stacklevel=3,
1325 1325 config=b'check-locks',
1326 1326 )
1327 1327 elif repo._currentlock(repo._wlockref) is None:
1328 1328 # rest of vfs files are covered by 'wlock'
1329 1329 #
1330 1330 # exclude special files
1331 1331 for prefix in self._wlockfreeprefix:
1332 1332 if path.startswith(prefix):
1333 1333 return
1334 1334 repo.ui.develwarn(
1335 1335 b'write with no wlock: "%s"' % path,
1336 1336 stacklevel=3,
1337 1337 config=b'check-locks',
1338 1338 )
1339 1339 return ret
1340 1340
1341 1341 return checkvfs
1342 1342
1343 1343 def _getsvfsward(self, origfunc):
1344 1344 """build a ward for self.svfs"""
1345 1345 rref = weakref.ref(self)
1346 1346
1347 1347 def checksvfs(path, mode=None):
1348 1348 ret = origfunc(path, mode=mode)
1349 1349 repo = rref()
1350 1350 if repo is None or not util.safehasattr(repo, b'_lockref'):
1351 1351 return
1352 1352 if mode in (None, b'r', b'rb'):
1353 1353 return
1354 1354 if path.startswith(repo.sharedpath):
1355 1355 # truncate name relative to the repository (.hg)
1356 1356 path = path[len(repo.sharedpath) + 1 :]
1357 1357 if repo._currentlock(repo._lockref) is None:
1358 1358 repo.ui.develwarn(
1359 1359 b'write with no lock: "%s"' % path, stacklevel=4
1360 1360 )
1361 1361 return ret
1362 1362
1363 1363 return checksvfs
1364 1364
1365 1365 def close(self):
1366 1366 self._writecaches()
1367 1367
1368 1368 def _writecaches(self):
1369 1369 if self._revbranchcache:
1370 1370 self._revbranchcache.write()
1371 1371
1372 1372 def _restrictcapabilities(self, caps):
1373 1373 if self.ui.configbool(b'experimental', b'bundle2-advertise'):
1374 1374 caps = set(caps)
1375 1375 capsblob = bundle2.encodecaps(
1376 1376 bundle2.getrepocaps(self, role=b'client')
1377 1377 )
1378 1378 caps.add(b'bundle2=' + urlreq.quote(capsblob))
1379 1379 return caps
1380 1380
1381 1381 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1382 1382 # self -> auditor -> self._checknested -> self
1383 1383
1384 1384 @property
1385 1385 def auditor(self):
1386 1386 # This is only used by context.workingctx.match in order to
1387 1387 # detect files in subrepos.
1388 1388 return pathutil.pathauditor(self.root, callback=self._checknested)
1389 1389
1390 1390 @property
1391 1391 def nofsauditor(self):
1392 1392 # This is only used by context.basectx.match in order to detect
1393 1393 # files in subrepos.
1394 1394 return pathutil.pathauditor(
1395 1395 self.root, callback=self._checknested, realfs=False, cached=True
1396 1396 )
1397 1397
1398 1398 def _checknested(self, path):
1399 1399 """Determine if path is a legal nested repository."""
1400 1400 if not path.startswith(self.root):
1401 1401 return False
1402 1402 subpath = path[len(self.root) + 1 :]
1403 1403 normsubpath = util.pconvert(subpath)
1404 1404
1405 1405 # XXX: Checking against the current working copy is wrong in
1406 1406 # the sense that it can reject things like
1407 1407 #
1408 1408 # $ hg cat -r 10 sub/x.txt
1409 1409 #
1410 1410 # if sub/ is no longer a subrepository in the working copy
1411 1411 # parent revision.
1412 1412 #
1413 1413 # However, it can of course also allow things that would have
1414 1414 # been rejected before, such as the above cat command if sub/
1415 1415 # is a subrepository now, but was a normal directory before.
1416 1416 # The old path auditor would have rejected by mistake since it
1417 1417 # panics when it sees sub/.hg/.
1418 1418 #
1419 1419 # All in all, checking against the working copy seems sensible
1420 1420 # since we want to prevent access to nested repositories on
1421 1421 # the filesystem *now*.
1422 1422 ctx = self[None]
1423 1423 parts = util.splitpath(subpath)
1424 1424 while parts:
1425 1425 prefix = b'/'.join(parts)
1426 1426 if prefix in ctx.substate:
1427 1427 if prefix == normsubpath:
1428 1428 return True
1429 1429 else:
1430 1430 sub = ctx.sub(prefix)
1431 1431 return sub.checknested(subpath[len(prefix) + 1 :])
1432 1432 else:
1433 1433 parts.pop()
1434 1434 return False
1435 1435
1436 1436 def peer(self):
1437 1437 return localpeer(self) # not cached to avoid reference cycle
1438 1438
1439 1439 def unfiltered(self):
1440 1440 """Return unfiltered version of the repository
1441 1441
1442 1442 Intended to be overwritten by filtered repo."""
1443 1443 return self
1444 1444
1445 1445 def filtered(self, name, visibilityexceptions=None):
1446 1446 """Return a filtered version of a repository
1447 1447
1448 1448 The `name` parameter is the identifier of the requested view. This
1449 1449 will return a repoview object set "exactly" to the specified view.
1450 1450
1451 1451 This function does not apply recursive filtering to a repository. For
1452 1452 example calling `repo.filtered("served")` will return a repoview using
1453 1453 the "served" view, regardless of the initial view used by `repo`.
1454 1454
1455 1455 In other word, there is always only one level of `repoview` "filtering".
1456 1456 """
1457 1457 if self._extrafilterid is not None and b'%' not in name:
1458 1458 name = name + b'%' + self._extrafilterid
1459 1459
1460 1460 cls = repoview.newtype(self.unfiltered().__class__)
1461 1461 return cls(self, name, visibilityexceptions)
1462 1462
1463 1463 @mixedrepostorecache(
1464 1464 (b'bookmarks', b'plain'),
1465 1465 (b'bookmarks.current', b'plain'),
1466 1466 (b'bookmarks', b''),
1467 1467 (b'00changelog.i', b''),
1468 1468 )
1469 1469 def _bookmarks(self):
1470 1470 # Since the multiple files involved in the transaction cannot be
1471 1471 # written atomically (with current repository format), there is a race
1472 1472 # condition here.
1473 1473 #
1474 1474 # 1) changelog content A is read
1475 1475 # 2) outside transaction update changelog to content B
1476 1476 # 3) outside transaction update bookmark file referring to content B
1477 1477 # 4) bookmarks file content is read and filtered against changelog-A
1478 1478 #
1479 1479 # When this happens, bookmarks against nodes missing from A are dropped.
1480 1480 #
1481 1481 # Having this happening during read is not great, but it become worse
1482 1482 # when this happen during write because the bookmarks to the "unknown"
1483 1483 # nodes will be dropped for good. However, writes happen within locks.
1484 1484 # This locking makes it possible to have a race free consistent read.
1485 1485 # For this purpose data read from disc before locking are
1486 1486 # "invalidated" right after the locks are taken. This invalidations are
1487 1487 # "light", the `filecache` mechanism keep the data in memory and will
1488 1488 # reuse them if the underlying files did not changed. Not parsing the
1489 1489 # same data multiple times helps performances.
1490 1490 #
1491 1491 # Unfortunately in the case describe above, the files tracked by the
1492 1492 # bookmarks file cache might not have changed, but the in-memory
1493 1493 # content is still "wrong" because we used an older changelog content
1494 1494 # to process the on-disk data. So after locking, the changelog would be
1495 1495 # refreshed but `_bookmarks` would be preserved.
1496 1496 # Adding `00changelog.i` to the list of tracked file is not
1497 1497 # enough, because at the time we build the content for `_bookmarks` in
1498 1498 # (4), the changelog file has already diverged from the content used
1499 1499 # for loading `changelog` in (1)
1500 1500 #
1501 1501 # To prevent the issue, we force the changelog to be explicitly
1502 1502 # reloaded while computing `_bookmarks`. The data race can still happen
1503 1503 # without the lock (with a narrower window), but it would no longer go
1504 1504 # undetected during the lock time refresh.
1505 1505 #
1506 1506 # The new schedule is as follow
1507 1507 #
1508 1508 # 1) filecache logic detect that `_bookmarks` needs to be computed
1509 1509 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1510 1510 # 3) We force `changelog` filecache to be tested
1511 1511 # 4) cachestat for `changelog` are captured (for changelog)
1512 1512 # 5) `_bookmarks` is computed and cached
1513 1513 #
1514 1514 # The step in (3) ensure we have a changelog at least as recent as the
1515 1515 # cache stat computed in (1). As a result at locking time:
1516 1516 # * if the changelog did not changed since (1) -> we can reuse the data
1517 1517 # * otherwise -> the bookmarks get refreshed.
1518 1518 self._refreshchangelog()
1519 1519 return bookmarks.bmstore(self)
1520 1520
1521 1521 def _refreshchangelog(self):
1522 1522 """make sure the in memory changelog match the on-disk one"""
1523 1523 if 'changelog' in vars(self) and self.currenttransaction() is None:
1524 1524 del self.changelog
1525 1525
1526 1526 @property
1527 1527 def _activebookmark(self):
1528 1528 return self._bookmarks.active
1529 1529
1530 1530 # _phasesets depend on changelog. what we need is to call
1531 1531 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1532 1532 # can't be easily expressed in filecache mechanism.
1533 1533 @storecache(b'phaseroots', b'00changelog.i')
1534 1534 def _phasecache(self):
1535 1535 return phases.phasecache(self, self._phasedefaults)
1536 1536
1537 1537 @storecache(b'obsstore')
1538 1538 def obsstore(self):
1539 1539 return obsolete.makestore(self.ui, self)
1540 1540
1541 1541 @storecache(b'00changelog.i')
1542 1542 def changelog(self):
1543 1543 # load dirstate before changelog to avoid race see issue6303
1544 1544 self.dirstate.prefetch_parents()
1545 1545 return self.store.changelog(txnutil.mayhavepending(self.root))
1546 1546
1547 1547 @storecache(b'00manifest.i')
1548 1548 def manifestlog(self):
1549 1549 return self.store.manifestlog(self, self._storenarrowmatch)
1550 1550
1551 1551 @repofilecache(b'dirstate')
1552 1552 def dirstate(self):
1553 1553 return self._makedirstate()
1554 1554
1555 1555 def _makedirstate(self):
1556 1556 """Extension point for wrapping the dirstate per-repo."""
1557 1557 sparsematchfn = lambda: sparse.matcher(self)
1558 1558
1559 1559 return dirstate.dirstate(
1560 1560 self.vfs, self.ui, self.root, self._dirstatevalidate, sparsematchfn
1561 1561 )
1562 1562
1563 1563 def _dirstatevalidate(self, node):
1564 1564 try:
1565 1565 self.changelog.rev(node)
1566 1566 return node
1567 1567 except error.LookupError:
1568 1568 if not self._dirstatevalidatewarned:
1569 1569 self._dirstatevalidatewarned = True
1570 1570 self.ui.warn(
1571 1571 _(b"warning: ignoring unknown working parent %s!\n")
1572 1572 % short(node)
1573 1573 )
1574 1574 return nullid
1575 1575
1576 1576 @storecache(narrowspec.FILENAME)
1577 1577 def narrowpats(self):
1578 1578 """matcher patterns for this repository's narrowspec
1579 1579
1580 1580 A tuple of (includes, excludes).
1581 1581 """
1582 1582 return narrowspec.load(self)
1583 1583
1584 1584 @storecache(narrowspec.FILENAME)
1585 1585 def _storenarrowmatch(self):
1586 1586 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1587 1587 return matchmod.always()
1588 1588 include, exclude = self.narrowpats
1589 1589 return narrowspec.match(self.root, include=include, exclude=exclude)
1590 1590
1591 1591 @storecache(narrowspec.FILENAME)
1592 1592 def _narrowmatch(self):
1593 1593 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1594 1594 return matchmod.always()
1595 1595 narrowspec.checkworkingcopynarrowspec(self)
1596 1596 include, exclude = self.narrowpats
1597 1597 return narrowspec.match(self.root, include=include, exclude=exclude)
1598 1598
1599 1599 def narrowmatch(self, match=None, includeexact=False):
1600 1600 """matcher corresponding the the repo's narrowspec
1601 1601
1602 1602 If `match` is given, then that will be intersected with the narrow
1603 1603 matcher.
1604 1604
1605 1605 If `includeexact` is True, then any exact matches from `match` will
1606 1606 be included even if they're outside the narrowspec.
1607 1607 """
1608 1608 if match:
1609 1609 if includeexact and not self._narrowmatch.always():
1610 1610 # do not exclude explicitly-specified paths so that they can
1611 1611 # be warned later on
1612 1612 em = matchmod.exact(match.files())
1613 1613 nm = matchmod.unionmatcher([self._narrowmatch, em])
1614 1614 return matchmod.intersectmatchers(match, nm)
1615 1615 return matchmod.intersectmatchers(match, self._narrowmatch)
1616 1616 return self._narrowmatch
1617 1617
1618 1618 def setnarrowpats(self, newincludes, newexcludes):
1619 1619 narrowspec.save(self, newincludes, newexcludes)
1620 1620 self.invalidate(clearfilecache=True)
1621 1621
1622 1622 @unfilteredpropertycache
1623 1623 def _quick_access_changeid_null(self):
1624 1624 return {
1625 1625 b'null': (nullrev, nullid),
1626 1626 nullrev: (nullrev, nullid),
1627 1627 nullid: (nullrev, nullid),
1628 1628 }
1629 1629
1630 1630 @unfilteredpropertycache
1631 1631 def _quick_access_changeid_wc(self):
1632 1632 # also fast path access to the working copy parents
1633 1633 # however, only do it for filter that ensure wc is visible.
1634 1634 quick = self._quick_access_changeid_null.copy()
1635 1635 cl = self.unfiltered().changelog
1636 1636 for node in self.dirstate.parents():
1637 1637 if node == nullid:
1638 1638 continue
1639 1639 rev = cl.index.get_rev(node)
1640 1640 if rev is None:
1641 1641 # unknown working copy parent case:
1642 1642 #
1643 1643 # skip the fast path and let higher code deal with it
1644 1644 continue
1645 1645 pair = (rev, node)
1646 1646 quick[rev] = pair
1647 1647 quick[node] = pair
1648 1648 # also add the parents of the parents
1649 1649 for r in cl.parentrevs(rev):
1650 1650 if r == nullrev:
1651 1651 continue
1652 1652 n = cl.node(r)
1653 1653 pair = (r, n)
1654 1654 quick[r] = pair
1655 1655 quick[n] = pair
1656 1656 p1node = self.dirstate.p1()
1657 1657 if p1node != nullid:
1658 1658 quick[b'.'] = quick[p1node]
1659 1659 return quick
1660 1660
1661 1661 @unfilteredmethod
1662 1662 def _quick_access_changeid_invalidate(self):
1663 1663 if '_quick_access_changeid_wc' in vars(self):
1664 1664 del self.__dict__['_quick_access_changeid_wc']
1665 1665
1666 1666 @property
1667 1667 def _quick_access_changeid(self):
1668 1668 """an helper dictionnary for __getitem__ calls
1669 1669
1670 1670 This contains a list of symbol we can recognise right away without
1671 1671 further processing.
1672 1672 """
1673 1673 if self.filtername in repoview.filter_has_wc:
1674 1674 return self._quick_access_changeid_wc
1675 1675 return self._quick_access_changeid_null
1676 1676
1677 1677 def __getitem__(self, changeid):
1678 1678 # dealing with special cases
1679 1679 if changeid is None:
1680 1680 return context.workingctx(self)
1681 1681 if isinstance(changeid, context.basectx):
1682 1682 return changeid
1683 1683
1684 1684 # dealing with multiple revisions
1685 1685 if isinstance(changeid, slice):
1686 1686 # wdirrev isn't contiguous so the slice shouldn't include it
1687 1687 return [
1688 1688 self[i]
1689 1689 for i in pycompat.xrange(*changeid.indices(len(self)))
1690 1690 if i not in self.changelog.filteredrevs
1691 1691 ]
1692 1692
1693 1693 # dealing with some special values
1694 1694 quick_access = self._quick_access_changeid.get(changeid)
1695 1695 if quick_access is not None:
1696 1696 rev, node = quick_access
1697 1697 return context.changectx(self, rev, node, maybe_filtered=False)
1698 1698 if changeid == b'tip':
1699 1699 node = self.changelog.tip()
1700 1700 rev = self.changelog.rev(node)
1701 1701 return context.changectx(self, rev, node)
1702 1702
1703 1703 # dealing with arbitrary values
1704 1704 try:
1705 1705 if isinstance(changeid, int):
1706 1706 node = self.changelog.node(changeid)
1707 1707 rev = changeid
1708 1708 elif changeid == b'.':
1709 1709 # this is a hack to delay/avoid loading obsmarkers
1710 1710 # when we know that '.' won't be hidden
1711 1711 node = self.dirstate.p1()
1712 1712 rev = self.unfiltered().changelog.rev(node)
1713 1713 elif len(changeid) == 20:
1714 1714 try:
1715 1715 node = changeid
1716 1716 rev = self.changelog.rev(changeid)
1717 1717 except error.FilteredLookupError:
1718 1718 changeid = hex(changeid) # for the error message
1719 1719 raise
1720 1720 except LookupError:
1721 1721 # check if it might have come from damaged dirstate
1722 1722 #
1723 1723 # XXX we could avoid the unfiltered if we had a recognizable
1724 1724 # exception for filtered changeset access
1725 1725 if (
1726 1726 self.local()
1727 1727 and changeid in self.unfiltered().dirstate.parents()
1728 1728 ):
1729 1729 msg = _(b"working directory has unknown parent '%s'!")
1730 1730 raise error.Abort(msg % short(changeid))
1731 1731 changeid = hex(changeid) # for the error message
1732 1732 raise
1733 1733
1734 1734 elif len(changeid) == 40:
1735 1735 node = bin(changeid)
1736 1736 rev = self.changelog.rev(node)
1737 1737 else:
1738 1738 raise error.ProgrammingError(
1739 1739 b"unsupported changeid '%s' of type %s"
1740 1740 % (changeid, pycompat.bytestr(type(changeid)))
1741 1741 )
1742 1742
1743 1743 return context.changectx(self, rev, node)
1744 1744
1745 1745 except (error.FilteredIndexError, error.FilteredLookupError):
1746 1746 raise error.FilteredRepoLookupError(
1747 1747 _(b"filtered revision '%s'") % pycompat.bytestr(changeid)
1748 1748 )
1749 1749 except (IndexError, LookupError):
1750 1750 raise error.RepoLookupError(
1751 1751 _(b"unknown revision '%s'") % pycompat.bytestr(changeid)
1752 1752 )
1753 1753 except error.WdirUnsupported:
1754 1754 return context.workingctx(self)
1755 1755
1756 1756 def __contains__(self, changeid):
1757 """True if the given changeid exists
1758
1759 error.AmbiguousPrefixLookupError is raised if an ambiguous node
1760 specified.
1761 """
1757 """True if the given changeid exists"""
1762 1758 try:
1763 1759 self[changeid]
1764 1760 return True
1765 1761 except error.RepoLookupError:
1766 1762 return False
1767 1763
1768 1764 def __nonzero__(self):
1769 1765 return True
1770 1766
1771 1767 __bool__ = __nonzero__
1772 1768
1773 1769 def __len__(self):
1774 1770 # no need to pay the cost of repoview.changelog
1775 1771 unfi = self.unfiltered()
1776 1772 return len(unfi.changelog)
1777 1773
1778 1774 def __iter__(self):
1779 1775 return iter(self.changelog)
1780 1776
1781 1777 def revs(self, expr, *args):
1782 1778 """Find revisions matching a revset.
1783 1779
1784 1780 The revset is specified as a string ``expr`` that may contain
1785 1781 %-formatting to escape certain types. See ``revsetlang.formatspec``.
1786 1782
1787 1783 Revset aliases from the configuration are not expanded. To expand
1788 1784 user aliases, consider calling ``scmutil.revrange()`` or
1789 1785 ``repo.anyrevs([expr], user=True)``.
1790 1786
1791 1787 Returns a smartset.abstractsmartset, which is a list-like interface
1792 1788 that contains integer revisions.
1793 1789 """
1794 1790 tree = revsetlang.spectree(expr, *args)
1795 1791 return revset.makematcher(tree)(self)
1796 1792
1797 1793 def set(self, expr, *args):
1798 1794 """Find revisions matching a revset and emit changectx instances.
1799 1795
1800 1796 This is a convenience wrapper around ``revs()`` that iterates the
1801 1797 result and is a generator of changectx instances.
1802 1798
1803 1799 Revset aliases from the configuration are not expanded. To expand
1804 1800 user aliases, consider calling ``scmutil.revrange()``.
1805 1801 """
1806 1802 for r in self.revs(expr, *args):
1807 1803 yield self[r]
1808 1804
1809 1805 def anyrevs(self, specs, user=False, localalias=None):
1810 1806 """Find revisions matching one of the given revsets.
1811 1807
1812 1808 Revset aliases from the configuration are not expanded by default. To
1813 1809 expand user aliases, specify ``user=True``. To provide some local
1814 1810 definitions overriding user aliases, set ``localalias`` to
1815 1811 ``{name: definitionstring}``.
1816 1812 """
1817 1813 if specs == [b'null']:
1818 1814 return revset.baseset([nullrev])
1819 1815 if specs == [b'.']:
1820 1816 quick_data = self._quick_access_changeid.get(b'.')
1821 1817 if quick_data is not None:
1822 1818 return revset.baseset([quick_data[0]])
1823 1819 if user:
1824 1820 m = revset.matchany(
1825 1821 self.ui,
1826 1822 specs,
1827 1823 lookup=revset.lookupfn(self),
1828 1824 localalias=localalias,
1829 1825 )
1830 1826 else:
1831 1827 m = revset.matchany(None, specs, localalias=localalias)
1832 1828 return m(self)
1833 1829
1834 1830 def url(self):
1835 1831 return b'file:' + self.root
1836 1832
1837 1833 def hook(self, name, throw=False, **args):
1838 1834 """Call a hook, passing this repo instance.
1839 1835
1840 1836 This a convenience method to aid invoking hooks. Extensions likely
1841 1837 won't call this unless they have registered a custom hook or are
1842 1838 replacing code that is expected to call a hook.
1843 1839 """
1844 1840 return hook.hook(self.ui, self, name, throw, **args)
1845 1841
1846 1842 @filteredpropertycache
1847 1843 def _tagscache(self):
1848 1844 """Returns a tagscache object that contains various tags related
1849 1845 caches."""
1850 1846
1851 1847 # This simplifies its cache management by having one decorated
1852 1848 # function (this one) and the rest simply fetch things from it.
1853 1849 class tagscache(object):
1854 1850 def __init__(self):
1855 1851 # These two define the set of tags for this repository. tags
1856 1852 # maps tag name to node; tagtypes maps tag name to 'global' or
1857 1853 # 'local'. (Global tags are defined by .hgtags across all
1858 1854 # heads, and local tags are defined in .hg/localtags.)
1859 1855 # They constitute the in-memory cache of tags.
1860 1856 self.tags = self.tagtypes = None
1861 1857
1862 1858 self.nodetagscache = self.tagslist = None
1863 1859
1864 1860 cache = tagscache()
1865 1861 cache.tags, cache.tagtypes = self._findtags()
1866 1862
1867 1863 return cache
1868 1864
1869 1865 def tags(self):
1870 1866 '''return a mapping of tag to node'''
1871 1867 t = {}
1872 1868 if self.changelog.filteredrevs:
1873 1869 tags, tt = self._findtags()
1874 1870 else:
1875 1871 tags = self._tagscache.tags
1876 1872 rev = self.changelog.rev
1877 1873 for k, v in pycompat.iteritems(tags):
1878 1874 try:
1879 1875 # ignore tags to unknown nodes
1880 1876 rev(v)
1881 1877 t[k] = v
1882 1878 except (error.LookupError, ValueError):
1883 1879 pass
1884 1880 return t
1885 1881
1886 1882 def _findtags(self):
1887 1883 """Do the hard work of finding tags. Return a pair of dicts
1888 1884 (tags, tagtypes) where tags maps tag name to node, and tagtypes
1889 1885 maps tag name to a string like \'global\' or \'local\'.
1890 1886 Subclasses or extensions are free to add their own tags, but
1891 1887 should be aware that the returned dicts will be retained for the
1892 1888 duration of the localrepo object."""
1893 1889
1894 1890 # XXX what tagtype should subclasses/extensions use? Currently
1895 1891 # mq and bookmarks add tags, but do not set the tagtype at all.
1896 1892 # Should each extension invent its own tag type? Should there
1897 1893 # be one tagtype for all such "virtual" tags? Or is the status
1898 1894 # quo fine?
1899 1895
1900 1896 # map tag name to (node, hist)
1901 1897 alltags = tagsmod.findglobaltags(self.ui, self)
1902 1898 # map tag name to tag type
1903 1899 tagtypes = {tag: b'global' for tag in alltags}
1904 1900
1905 1901 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
1906 1902
1907 1903 # Build the return dicts. Have to re-encode tag names because
1908 1904 # the tags module always uses UTF-8 (in order not to lose info
1909 1905 # writing to the cache), but the rest of Mercurial wants them in
1910 1906 # local encoding.
1911 1907 tags = {}
1912 1908 for (name, (node, hist)) in pycompat.iteritems(alltags):
1913 1909 if node != nullid:
1914 1910 tags[encoding.tolocal(name)] = node
1915 1911 tags[b'tip'] = self.changelog.tip()
1916 1912 tagtypes = {
1917 1913 encoding.tolocal(name): value
1918 1914 for (name, value) in pycompat.iteritems(tagtypes)
1919 1915 }
1920 1916 return (tags, tagtypes)
1921 1917
1922 1918 def tagtype(self, tagname):
1923 1919 """
1924 1920 return the type of the given tag. result can be:
1925 1921
1926 1922 'local' : a local tag
1927 1923 'global' : a global tag
1928 1924 None : tag does not exist
1929 1925 """
1930 1926
1931 1927 return self._tagscache.tagtypes.get(tagname)
1932 1928
1933 1929 def tagslist(self):
1934 1930 '''return a list of tags ordered by revision'''
1935 1931 if not self._tagscache.tagslist:
1936 1932 l = []
1937 1933 for t, n in pycompat.iteritems(self.tags()):
1938 1934 l.append((self.changelog.rev(n), t, n))
1939 1935 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
1940 1936
1941 1937 return self._tagscache.tagslist
1942 1938
1943 1939 def nodetags(self, node):
1944 1940 '''return the tags associated with a node'''
1945 1941 if not self._tagscache.nodetagscache:
1946 1942 nodetagscache = {}
1947 1943 for t, n in pycompat.iteritems(self._tagscache.tags):
1948 1944 nodetagscache.setdefault(n, []).append(t)
1949 1945 for tags in pycompat.itervalues(nodetagscache):
1950 1946 tags.sort()
1951 1947 self._tagscache.nodetagscache = nodetagscache
1952 1948 return self._tagscache.nodetagscache.get(node, [])
1953 1949
1954 1950 def nodebookmarks(self, node):
1955 1951 """return the list of bookmarks pointing to the specified node"""
1956 1952 return self._bookmarks.names(node)
1957 1953
1958 1954 def branchmap(self):
1959 1955 """returns a dictionary {branch: [branchheads]} with branchheads
1960 1956 ordered by increasing revision number"""
1961 1957 return self._branchcaches[self]
1962 1958
1963 1959 @unfilteredmethod
1964 1960 def revbranchcache(self):
1965 1961 if not self._revbranchcache:
1966 1962 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
1967 1963 return self._revbranchcache
1968 1964
1969 1965 def branchtip(self, branch, ignoremissing=False):
1970 1966 """return the tip node for a given branch
1971 1967
1972 1968 If ignoremissing is True, then this method will not raise an error.
1973 1969 This is helpful for callers that only expect None for a missing branch
1974 1970 (e.g. namespace).
1975 1971
1976 1972 """
1977 1973 try:
1978 1974 return self.branchmap().branchtip(branch)
1979 1975 except KeyError:
1980 1976 if not ignoremissing:
1981 1977 raise error.RepoLookupError(_(b"unknown branch '%s'") % branch)
1982 1978 else:
1983 1979 pass
1984 1980
1985 1981 def lookup(self, key):
1986 1982 node = scmutil.revsymbol(self, key).node()
1987 1983 if node is None:
1988 1984 raise error.RepoLookupError(_(b"unknown revision '%s'") % key)
1989 1985 return node
1990 1986
1991 1987 def lookupbranch(self, key):
1992 1988 if self.branchmap().hasbranch(key):
1993 1989 return key
1994 1990
1995 1991 return scmutil.revsymbol(self, key).branch()
1996 1992
1997 1993 def known(self, nodes):
1998 1994 cl = self.changelog
1999 1995 get_rev = cl.index.get_rev
2000 1996 filtered = cl.filteredrevs
2001 1997 result = []
2002 1998 for n in nodes:
2003 1999 r = get_rev(n)
2004 2000 resp = not (r is None or r in filtered)
2005 2001 result.append(resp)
2006 2002 return result
2007 2003
2008 2004 def local(self):
2009 2005 return self
2010 2006
2011 2007 def publishing(self):
2012 2008 # it's safe (and desirable) to trust the publish flag unconditionally
2013 2009 # so that we don't finalize changes shared between users via ssh or nfs
2014 2010 return self.ui.configbool(b'phases', b'publish', untrusted=True)
2015 2011
2016 2012 def cancopy(self):
2017 2013 # so statichttprepo's override of local() works
2018 2014 if not self.local():
2019 2015 return False
2020 2016 if not self.publishing():
2021 2017 return True
2022 2018 # if publishing we can't copy if there is filtered content
2023 2019 return not self.filtered(b'visible').changelog.filteredrevs
2024 2020
2025 2021 def shared(self):
2026 2022 '''the type of shared repository (None if not shared)'''
2027 2023 if self.sharedpath != self.path:
2028 2024 return b'store'
2029 2025 return None
2030 2026
2031 2027 def wjoin(self, f, *insidef):
2032 2028 return self.vfs.reljoin(self.root, f, *insidef)
2033 2029
2034 2030 def setparents(self, p1, p2=nullid):
2035 2031 self[None].setparents(p1, p2)
2036 2032 self._quick_access_changeid_invalidate()
2037 2033
2038 2034 def filectx(self, path, changeid=None, fileid=None, changectx=None):
2039 2035 """changeid must be a changeset revision, if specified.
2040 2036 fileid can be a file revision or node."""
2041 2037 return context.filectx(
2042 2038 self, path, changeid, fileid, changectx=changectx
2043 2039 )
2044 2040
2045 2041 def getcwd(self):
2046 2042 return self.dirstate.getcwd()
2047 2043
2048 2044 def pathto(self, f, cwd=None):
2049 2045 return self.dirstate.pathto(f, cwd)
2050 2046
2051 2047 def _loadfilter(self, filter):
2052 2048 if filter not in self._filterpats:
2053 2049 l = []
2054 2050 for pat, cmd in self.ui.configitems(filter):
2055 2051 if cmd == b'!':
2056 2052 continue
2057 2053 mf = matchmod.match(self.root, b'', [pat])
2058 2054 fn = None
2059 2055 params = cmd
2060 2056 for name, filterfn in pycompat.iteritems(self._datafilters):
2061 2057 if cmd.startswith(name):
2062 2058 fn = filterfn
2063 2059 params = cmd[len(name) :].lstrip()
2064 2060 break
2065 2061 if not fn:
2066 2062 fn = lambda s, c, **kwargs: procutil.filter(s, c)
2067 2063 fn.__name__ = 'commandfilter'
2068 2064 # Wrap old filters not supporting keyword arguments
2069 2065 if not pycompat.getargspec(fn)[2]:
2070 2066 oldfn = fn
2071 2067 fn = lambda s, c, oldfn=oldfn, **kwargs: oldfn(s, c)
2072 2068 fn.__name__ = 'compat-' + oldfn.__name__
2073 2069 l.append((mf, fn, params))
2074 2070 self._filterpats[filter] = l
2075 2071 return self._filterpats[filter]
2076 2072
2077 2073 def _filter(self, filterpats, filename, data):
2078 2074 for mf, fn, cmd in filterpats:
2079 2075 if mf(filename):
2080 2076 self.ui.debug(
2081 2077 b"filtering %s through %s\n"
2082 2078 % (filename, cmd or pycompat.sysbytes(fn.__name__))
2083 2079 )
2084 2080 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
2085 2081 break
2086 2082
2087 2083 return data
2088 2084
2089 2085 @unfilteredpropertycache
2090 2086 def _encodefilterpats(self):
2091 2087 return self._loadfilter(b'encode')
2092 2088
2093 2089 @unfilteredpropertycache
2094 2090 def _decodefilterpats(self):
2095 2091 return self._loadfilter(b'decode')
2096 2092
2097 2093 def adddatafilter(self, name, filter):
2098 2094 self._datafilters[name] = filter
2099 2095
2100 2096 def wread(self, filename):
2101 2097 if self.wvfs.islink(filename):
2102 2098 data = self.wvfs.readlink(filename)
2103 2099 else:
2104 2100 data = self.wvfs.read(filename)
2105 2101 return self._filter(self._encodefilterpats, filename, data)
2106 2102
2107 2103 def wwrite(self, filename, data, flags, backgroundclose=False, **kwargs):
2108 2104 """write ``data`` into ``filename`` in the working directory
2109 2105
2110 2106 This returns length of written (maybe decoded) data.
2111 2107 """
2112 2108 data = self._filter(self._decodefilterpats, filename, data)
2113 2109 if b'l' in flags:
2114 2110 self.wvfs.symlink(data, filename)
2115 2111 else:
2116 2112 self.wvfs.write(
2117 2113 filename, data, backgroundclose=backgroundclose, **kwargs
2118 2114 )
2119 2115 if b'x' in flags:
2120 2116 self.wvfs.setflags(filename, False, True)
2121 2117 else:
2122 2118 self.wvfs.setflags(filename, False, False)
2123 2119 return len(data)
2124 2120
2125 2121 def wwritedata(self, filename, data):
2126 2122 return self._filter(self._decodefilterpats, filename, data)
2127 2123
2128 2124 def currenttransaction(self):
2129 2125 """return the current transaction or None if non exists"""
2130 2126 if self._transref:
2131 2127 tr = self._transref()
2132 2128 else:
2133 2129 tr = None
2134 2130
2135 2131 if tr and tr.running():
2136 2132 return tr
2137 2133 return None
2138 2134
2139 2135 def transaction(self, desc, report=None):
2140 2136 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
2141 2137 b'devel', b'check-locks'
2142 2138 ):
2143 2139 if self._currentlock(self._lockref) is None:
2144 2140 raise error.ProgrammingError(b'transaction requires locking')
2145 2141 tr = self.currenttransaction()
2146 2142 if tr is not None:
2147 2143 return tr.nest(name=desc)
2148 2144
2149 2145 # abort here if the journal already exists
2150 2146 if self.svfs.exists(b"journal"):
2151 2147 raise error.RepoError(
2152 2148 _(b"abandoned transaction found"),
2153 2149 hint=_(b"run 'hg recover' to clean up transaction"),
2154 2150 )
2155 2151
2156 2152 idbase = b"%.40f#%f" % (random.random(), time.time())
2157 2153 ha = hex(hashutil.sha1(idbase).digest())
2158 2154 txnid = b'TXN:' + ha
2159 2155 self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
2160 2156
2161 2157 self._writejournal(desc)
2162 2158 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
2163 2159 if report:
2164 2160 rp = report
2165 2161 else:
2166 2162 rp = self.ui.warn
2167 2163 vfsmap = {b'plain': self.vfs, b'store': self.svfs} # root of .hg/
2168 2164 # we must avoid cyclic reference between repo and transaction.
2169 2165 reporef = weakref.ref(self)
2170 2166 # Code to track tag movement
2171 2167 #
2172 2168 # Since tags are all handled as file content, it is actually quite hard
2173 2169 # to track these movement from a code perspective. So we fallback to a
2174 2170 # tracking at the repository level. One could envision to track changes
2175 2171 # to the '.hgtags' file through changegroup apply but that fails to
2176 2172 # cope with case where transaction expose new heads without changegroup
2177 2173 # being involved (eg: phase movement).
2178 2174 #
2179 2175 # For now, We gate the feature behind a flag since this likely comes
2180 2176 # with performance impacts. The current code run more often than needed
2181 2177 # and do not use caches as much as it could. The current focus is on
2182 2178 # the behavior of the feature so we disable it by default. The flag
2183 2179 # will be removed when we are happy with the performance impact.
2184 2180 #
2185 2181 # Once this feature is no longer experimental move the following
2186 2182 # documentation to the appropriate help section:
2187 2183 #
2188 2184 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
2189 2185 # tags (new or changed or deleted tags). In addition the details of
2190 2186 # these changes are made available in a file at:
2191 2187 # ``REPOROOT/.hg/changes/tags.changes``.
2192 2188 # Make sure you check for HG_TAG_MOVED before reading that file as it
2193 2189 # might exist from a previous transaction even if no tag were touched
2194 2190 # in this one. Changes are recorded in a line base format::
2195 2191 #
2196 2192 # <action> <hex-node> <tag-name>\n
2197 2193 #
2198 2194 # Actions are defined as follow:
2199 2195 # "-R": tag is removed,
2200 2196 # "+A": tag is added,
2201 2197 # "-M": tag is moved (old value),
2202 2198 # "+M": tag is moved (new value),
2203 2199 tracktags = lambda x: None
2204 2200 # experimental config: experimental.hook-track-tags
2205 2201 shouldtracktags = self.ui.configbool(
2206 2202 b'experimental', b'hook-track-tags'
2207 2203 )
2208 2204 if desc != b'strip' and shouldtracktags:
2209 2205 oldheads = self.changelog.headrevs()
2210 2206
2211 2207 def tracktags(tr2):
2212 2208 repo = reporef()
2213 2209 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
2214 2210 newheads = repo.changelog.headrevs()
2215 2211 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
2216 2212 # notes: we compare lists here.
2217 2213 # As we do it only once buiding set would not be cheaper
2218 2214 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
2219 2215 if changes:
2220 2216 tr2.hookargs[b'tag_moved'] = b'1'
2221 2217 with repo.vfs(
2222 2218 b'changes/tags.changes', b'w', atomictemp=True
2223 2219 ) as changesfile:
2224 2220 # note: we do not register the file to the transaction
2225 2221 # because we needs it to still exist on the transaction
2226 2222 # is close (for txnclose hooks)
2227 2223 tagsmod.writediff(changesfile, changes)
2228 2224
2229 2225 def validate(tr2):
2230 2226 """will run pre-closing hooks"""
2231 2227 # XXX the transaction API is a bit lacking here so we take a hacky
2232 2228 # path for now
2233 2229 #
2234 2230 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
2235 2231 # dict is copied before these run. In addition we needs the data
2236 2232 # available to in memory hooks too.
2237 2233 #
2238 2234 # Moreover, we also need to make sure this runs before txnclose
2239 2235 # hooks and there is no "pending" mechanism that would execute
2240 2236 # logic only if hooks are about to run.
2241 2237 #
2242 2238 # Fixing this limitation of the transaction is also needed to track
2243 2239 # other families of changes (bookmarks, phases, obsolescence).
2244 2240 #
2245 2241 # This will have to be fixed before we remove the experimental
2246 2242 # gating.
2247 2243 tracktags(tr2)
2248 2244 repo = reporef()
2249 2245
2250 2246 singleheadopt = (b'experimental', b'single-head-per-branch')
2251 2247 singlehead = repo.ui.configbool(*singleheadopt)
2252 2248 if singlehead:
2253 2249 singleheadsub = repo.ui.configsuboptions(*singleheadopt)[1]
2254 2250 accountclosed = singleheadsub.get(
2255 2251 b"account-closed-heads", False
2256 2252 )
2257 2253 if singleheadsub.get(b"public-changes-only", False):
2258 2254 filtername = b"immutable"
2259 2255 else:
2260 2256 filtername = b"visible"
2261 2257 scmutil.enforcesinglehead(
2262 2258 repo, tr2, desc, accountclosed, filtername
2263 2259 )
2264 2260 if hook.hashook(repo.ui, b'pretxnclose-bookmark'):
2265 2261 for name, (old, new) in sorted(
2266 2262 tr.changes[b'bookmarks'].items()
2267 2263 ):
2268 2264 args = tr.hookargs.copy()
2269 2265 args.update(bookmarks.preparehookargs(name, old, new))
2270 2266 repo.hook(
2271 2267 b'pretxnclose-bookmark',
2272 2268 throw=True,
2273 2269 **pycompat.strkwargs(args)
2274 2270 )
2275 2271 if hook.hashook(repo.ui, b'pretxnclose-phase'):
2276 2272 cl = repo.unfiltered().changelog
2277 2273 for revs, (old, new) in tr.changes[b'phases']:
2278 2274 for rev in revs:
2279 2275 args = tr.hookargs.copy()
2280 2276 node = hex(cl.node(rev))
2281 2277 args.update(phases.preparehookargs(node, old, new))
2282 2278 repo.hook(
2283 2279 b'pretxnclose-phase',
2284 2280 throw=True,
2285 2281 **pycompat.strkwargs(args)
2286 2282 )
2287 2283
2288 2284 repo.hook(
2289 2285 b'pretxnclose', throw=True, **pycompat.strkwargs(tr.hookargs)
2290 2286 )
2291 2287
2292 2288 def releasefn(tr, success):
2293 2289 repo = reporef()
2294 2290 if repo is None:
2295 2291 # If the repo has been GC'd (and this release function is being
2296 2292 # called from transaction.__del__), there's not much we can do,
2297 2293 # so just leave the unfinished transaction there and let the
2298 2294 # user run `hg recover`.
2299 2295 return
2300 2296 if success:
2301 2297 # this should be explicitly invoked here, because
2302 2298 # in-memory changes aren't written out at closing
2303 2299 # transaction, if tr.addfilegenerator (via
2304 2300 # dirstate.write or so) isn't invoked while
2305 2301 # transaction running
2306 2302 repo.dirstate.write(None)
2307 2303 else:
2308 2304 # discard all changes (including ones already written
2309 2305 # out) in this transaction
2310 2306 narrowspec.restorebackup(self, b'journal.narrowspec')
2311 2307 narrowspec.restorewcbackup(self, b'journal.narrowspec.dirstate')
2312 2308 repo.dirstate.restorebackup(None, b'journal.dirstate')
2313 2309
2314 2310 repo.invalidate(clearfilecache=True)
2315 2311
2316 2312 tr = transaction.transaction(
2317 2313 rp,
2318 2314 self.svfs,
2319 2315 vfsmap,
2320 2316 b"journal",
2321 2317 b"undo",
2322 2318 aftertrans(renames),
2323 2319 self.store.createmode,
2324 2320 validator=validate,
2325 2321 releasefn=releasefn,
2326 2322 checkambigfiles=_cachedfiles,
2327 2323 name=desc,
2328 2324 )
2329 2325 tr.changes[b'origrepolen'] = len(self)
2330 2326 tr.changes[b'obsmarkers'] = set()
2331 2327 tr.changes[b'phases'] = []
2332 2328 tr.changes[b'bookmarks'] = {}
2333 2329
2334 2330 tr.hookargs[b'txnid'] = txnid
2335 2331 tr.hookargs[b'txnname'] = desc
2336 2332 tr.hookargs[b'changes'] = tr.changes
2337 2333 # note: writing the fncache only during finalize mean that the file is
2338 2334 # outdated when running hooks. As fncache is used for streaming clone,
2339 2335 # this is not expected to break anything that happen during the hooks.
2340 2336 tr.addfinalize(b'flush-fncache', self.store.write)
2341 2337
2342 2338 def txnclosehook(tr2):
2343 2339 """To be run if transaction is successful, will schedule a hook run"""
2344 2340 # Don't reference tr2 in hook() so we don't hold a reference.
2345 2341 # This reduces memory consumption when there are multiple
2346 2342 # transactions per lock. This can likely go away if issue5045
2347 2343 # fixes the function accumulation.
2348 2344 hookargs = tr2.hookargs
2349 2345
2350 2346 def hookfunc(unused_success):
2351 2347 repo = reporef()
2352 2348 if hook.hashook(repo.ui, b'txnclose-bookmark'):
2353 2349 bmchanges = sorted(tr.changes[b'bookmarks'].items())
2354 2350 for name, (old, new) in bmchanges:
2355 2351 args = tr.hookargs.copy()
2356 2352 args.update(bookmarks.preparehookargs(name, old, new))
2357 2353 repo.hook(
2358 2354 b'txnclose-bookmark',
2359 2355 throw=False,
2360 2356 **pycompat.strkwargs(args)
2361 2357 )
2362 2358
2363 2359 if hook.hashook(repo.ui, b'txnclose-phase'):
2364 2360 cl = repo.unfiltered().changelog
2365 2361 phasemv = sorted(
2366 2362 tr.changes[b'phases'], key=lambda r: r[0][0]
2367 2363 )
2368 2364 for revs, (old, new) in phasemv:
2369 2365 for rev in revs:
2370 2366 args = tr.hookargs.copy()
2371 2367 node = hex(cl.node(rev))
2372 2368 args.update(phases.preparehookargs(node, old, new))
2373 2369 repo.hook(
2374 2370 b'txnclose-phase',
2375 2371 throw=False,
2376 2372 **pycompat.strkwargs(args)
2377 2373 )
2378 2374
2379 2375 repo.hook(
2380 2376 b'txnclose', throw=False, **pycompat.strkwargs(hookargs)
2381 2377 )
2382 2378
2383 2379 reporef()._afterlock(hookfunc)
2384 2380
2385 2381 tr.addfinalize(b'txnclose-hook', txnclosehook)
2386 2382 # Include a leading "-" to make it happen before the transaction summary
2387 2383 # reports registered via scmutil.registersummarycallback() whose names
2388 2384 # are 00-txnreport etc. That way, the caches will be warm when the
2389 2385 # callbacks run.
2390 2386 tr.addpostclose(b'-warm-cache', self._buildcacheupdater(tr))
2391 2387
2392 2388 def txnaborthook(tr2):
2393 2389 """To be run if transaction is aborted"""
2394 2390 reporef().hook(
2395 2391 b'txnabort', throw=False, **pycompat.strkwargs(tr2.hookargs)
2396 2392 )
2397 2393
2398 2394 tr.addabort(b'txnabort-hook', txnaborthook)
2399 2395 # avoid eager cache invalidation. in-memory data should be identical
2400 2396 # to stored data if transaction has no error.
2401 2397 tr.addpostclose(b'refresh-filecachestats', self._refreshfilecachestats)
2402 2398 self._transref = weakref.ref(tr)
2403 2399 scmutil.registersummarycallback(self, tr, desc)
2404 2400 return tr
2405 2401
2406 2402 def _journalfiles(self):
2407 2403 return (
2408 2404 (self.svfs, b'journal'),
2409 2405 (self.svfs, b'journal.narrowspec'),
2410 2406 (self.vfs, b'journal.narrowspec.dirstate'),
2411 2407 (self.vfs, b'journal.dirstate'),
2412 2408 (self.vfs, b'journal.branch'),
2413 2409 (self.vfs, b'journal.desc'),
2414 2410 (bookmarks.bookmarksvfs(self), b'journal.bookmarks'),
2415 2411 (self.svfs, b'journal.phaseroots'),
2416 2412 )
2417 2413
2418 2414 def undofiles(self):
2419 2415 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2420 2416
2421 2417 @unfilteredmethod
2422 2418 def _writejournal(self, desc):
2423 2419 self.dirstate.savebackup(None, b'journal.dirstate')
2424 2420 narrowspec.savewcbackup(self, b'journal.narrowspec.dirstate')
2425 2421 narrowspec.savebackup(self, b'journal.narrowspec')
2426 2422 self.vfs.write(
2427 2423 b"journal.branch", encoding.fromlocal(self.dirstate.branch())
2428 2424 )
2429 2425 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
2430 2426 bookmarksvfs = bookmarks.bookmarksvfs(self)
2431 2427 bookmarksvfs.write(
2432 2428 b"journal.bookmarks", bookmarksvfs.tryread(b"bookmarks")
2433 2429 )
2434 2430 self.svfs.write(b"journal.phaseroots", self.svfs.tryread(b"phaseroots"))
2435 2431
2436 2432 def recover(self):
2437 2433 with self.lock():
2438 2434 if self.svfs.exists(b"journal"):
2439 2435 self.ui.status(_(b"rolling back interrupted transaction\n"))
2440 2436 vfsmap = {
2441 2437 b'': self.svfs,
2442 2438 b'plain': self.vfs,
2443 2439 }
2444 2440 transaction.rollback(
2445 2441 self.svfs,
2446 2442 vfsmap,
2447 2443 b"journal",
2448 2444 self.ui.warn,
2449 2445 checkambigfiles=_cachedfiles,
2450 2446 )
2451 2447 self.invalidate()
2452 2448 return True
2453 2449 else:
2454 2450 self.ui.warn(_(b"no interrupted transaction available\n"))
2455 2451 return False
2456 2452
2457 2453 def rollback(self, dryrun=False, force=False):
2458 2454 wlock = lock = dsguard = None
2459 2455 try:
2460 2456 wlock = self.wlock()
2461 2457 lock = self.lock()
2462 2458 if self.svfs.exists(b"undo"):
2463 2459 dsguard = dirstateguard.dirstateguard(self, b'rollback')
2464 2460
2465 2461 return self._rollback(dryrun, force, dsguard)
2466 2462 else:
2467 2463 self.ui.warn(_(b"no rollback information available\n"))
2468 2464 return 1
2469 2465 finally:
2470 2466 release(dsguard, lock, wlock)
2471 2467
2472 2468 @unfilteredmethod # Until we get smarter cache management
2473 2469 def _rollback(self, dryrun, force, dsguard):
2474 2470 ui = self.ui
2475 2471 try:
2476 2472 args = self.vfs.read(b'undo.desc').splitlines()
2477 2473 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2478 2474 if len(args) >= 3:
2479 2475 detail = args[2]
2480 2476 oldtip = oldlen - 1
2481 2477
2482 2478 if detail and ui.verbose:
2483 2479 msg = _(
2484 2480 b'repository tip rolled back to revision %d'
2485 2481 b' (undo %s: %s)\n'
2486 2482 ) % (oldtip, desc, detail)
2487 2483 else:
2488 2484 msg = _(
2489 2485 b'repository tip rolled back to revision %d (undo %s)\n'
2490 2486 ) % (oldtip, desc)
2491 2487 except IOError:
2492 2488 msg = _(b'rolling back unknown transaction\n')
2493 2489 desc = None
2494 2490
2495 2491 if not force and self[b'.'] != self[b'tip'] and desc == b'commit':
2496 2492 raise error.Abort(
2497 2493 _(
2498 2494 b'rollback of last commit while not checked out '
2499 2495 b'may lose data'
2500 2496 ),
2501 2497 hint=_(b'use -f to force'),
2502 2498 )
2503 2499
2504 2500 ui.status(msg)
2505 2501 if dryrun:
2506 2502 return 0
2507 2503
2508 2504 parents = self.dirstate.parents()
2509 2505 self.destroying()
2510 2506 vfsmap = {b'plain': self.vfs, b'': self.svfs}
2511 2507 transaction.rollback(
2512 2508 self.svfs, vfsmap, b'undo', ui.warn, checkambigfiles=_cachedfiles
2513 2509 )
2514 2510 bookmarksvfs = bookmarks.bookmarksvfs(self)
2515 2511 if bookmarksvfs.exists(b'undo.bookmarks'):
2516 2512 bookmarksvfs.rename(
2517 2513 b'undo.bookmarks', b'bookmarks', checkambig=True
2518 2514 )
2519 2515 if self.svfs.exists(b'undo.phaseroots'):
2520 2516 self.svfs.rename(b'undo.phaseroots', b'phaseroots', checkambig=True)
2521 2517 self.invalidate()
2522 2518
2523 2519 has_node = self.changelog.index.has_node
2524 2520 parentgone = any(not has_node(p) for p in parents)
2525 2521 if parentgone:
2526 2522 # prevent dirstateguard from overwriting already restored one
2527 2523 dsguard.close()
2528 2524
2529 2525 narrowspec.restorebackup(self, b'undo.narrowspec')
2530 2526 narrowspec.restorewcbackup(self, b'undo.narrowspec.dirstate')
2531 2527 self.dirstate.restorebackup(None, b'undo.dirstate')
2532 2528 try:
2533 2529 branch = self.vfs.read(b'undo.branch')
2534 2530 self.dirstate.setbranch(encoding.tolocal(branch))
2535 2531 except IOError:
2536 2532 ui.warn(
2537 2533 _(
2538 2534 b'named branch could not be reset: '
2539 2535 b'current branch is still \'%s\'\n'
2540 2536 )
2541 2537 % self.dirstate.branch()
2542 2538 )
2543 2539
2544 2540 parents = tuple([p.rev() for p in self[None].parents()])
2545 2541 if len(parents) > 1:
2546 2542 ui.status(
2547 2543 _(
2548 2544 b'working directory now based on '
2549 2545 b'revisions %d and %d\n'
2550 2546 )
2551 2547 % parents
2552 2548 )
2553 2549 else:
2554 2550 ui.status(
2555 2551 _(b'working directory now based on revision %d\n') % parents
2556 2552 )
2557 2553 mergestatemod.mergestate.clean(self)
2558 2554
2559 2555 # TODO: if we know which new heads may result from this rollback, pass
2560 2556 # them to destroy(), which will prevent the branchhead cache from being
2561 2557 # invalidated.
2562 2558 self.destroyed()
2563 2559 return 0
2564 2560
2565 2561 def _buildcacheupdater(self, newtransaction):
2566 2562 """called during transaction to build the callback updating cache
2567 2563
2568 2564 Lives on the repository to help extension who might want to augment
2569 2565 this logic. For this purpose, the created transaction is passed to the
2570 2566 method.
2571 2567 """
2572 2568 # we must avoid cyclic reference between repo and transaction.
2573 2569 reporef = weakref.ref(self)
2574 2570
2575 2571 def updater(tr):
2576 2572 repo = reporef()
2577 2573 repo.updatecaches(tr)
2578 2574
2579 2575 return updater
2580 2576
2581 2577 @unfilteredmethod
2582 2578 def updatecaches(self, tr=None, full=False):
2583 2579 """warm appropriate caches
2584 2580
2585 2581 If this function is called after a transaction closed. The transaction
2586 2582 will be available in the 'tr' argument. This can be used to selectively
2587 2583 update caches relevant to the changes in that transaction.
2588 2584
2589 2585 If 'full' is set, make sure all caches the function knows about have
2590 2586 up-to-date data. Even the ones usually loaded more lazily.
2591 2587 """
2592 2588 if tr is not None and tr.hookargs.get(b'source') == b'strip':
2593 2589 # During strip, many caches are invalid but
2594 2590 # later call to `destroyed` will refresh them.
2595 2591 return
2596 2592
2597 2593 if tr is None or tr.changes[b'origrepolen'] < len(self):
2598 2594 # accessing the 'ser ved' branchmap should refresh all the others,
2599 2595 self.ui.debug(b'updating the branch cache\n')
2600 2596 self.filtered(b'served').branchmap()
2601 2597 self.filtered(b'served.hidden').branchmap()
2602 2598
2603 2599 if full:
2604 2600 unfi = self.unfiltered()
2605 2601
2606 2602 self.changelog.update_caches(transaction=tr)
2607 2603 self.manifestlog.update_caches(transaction=tr)
2608 2604
2609 2605 rbc = unfi.revbranchcache()
2610 2606 for r in unfi.changelog:
2611 2607 rbc.branchinfo(r)
2612 2608 rbc.write()
2613 2609
2614 2610 # ensure the working copy parents are in the manifestfulltextcache
2615 2611 for ctx in self[b'.'].parents():
2616 2612 ctx.manifest() # accessing the manifest is enough
2617 2613
2618 2614 # accessing fnode cache warms the cache
2619 2615 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2620 2616 # accessing tags warm the cache
2621 2617 self.tags()
2622 2618 self.filtered(b'served').tags()
2623 2619
2624 2620 # The `full` arg is documented as updating even the lazily-loaded
2625 2621 # caches immediately, so we're forcing a write to cause these caches
2626 2622 # to be warmed up even if they haven't explicitly been requested
2627 2623 # yet (if they've never been used by hg, they won't ever have been
2628 2624 # written, even if they're a subset of another kind of cache that
2629 2625 # *has* been used).
2630 2626 for filt in repoview.filtertable.keys():
2631 2627 filtered = self.filtered(filt)
2632 2628 filtered.branchmap().write(filtered)
2633 2629
2634 2630 def invalidatecaches(self):
2635 2631
2636 2632 if '_tagscache' in vars(self):
2637 2633 # can't use delattr on proxy
2638 2634 del self.__dict__['_tagscache']
2639 2635
2640 2636 self._branchcaches.clear()
2641 2637 self.invalidatevolatilesets()
2642 2638 self._sparsesignaturecache.clear()
2643 2639
2644 2640 def invalidatevolatilesets(self):
2645 2641 self.filteredrevcache.clear()
2646 2642 obsolete.clearobscaches(self)
2647 2643 self._quick_access_changeid_invalidate()
2648 2644
2649 2645 def invalidatedirstate(self):
2650 2646 """Invalidates the dirstate, causing the next call to dirstate
2651 2647 to check if it was modified since the last time it was read,
2652 2648 rereading it if it has.
2653 2649
2654 2650 This is different to dirstate.invalidate() that it doesn't always
2655 2651 rereads the dirstate. Use dirstate.invalidate() if you want to
2656 2652 explicitly read the dirstate again (i.e. restoring it to a previous
2657 2653 known good state)."""
2658 2654 if hasunfilteredcache(self, 'dirstate'):
2659 2655 for k in self.dirstate._filecache:
2660 2656 try:
2661 2657 delattr(self.dirstate, k)
2662 2658 except AttributeError:
2663 2659 pass
2664 2660 delattr(self.unfiltered(), 'dirstate')
2665 2661
2666 2662 def invalidate(self, clearfilecache=False):
2667 2663 """Invalidates both store and non-store parts other than dirstate
2668 2664
2669 2665 If a transaction is running, invalidation of store is omitted,
2670 2666 because discarding in-memory changes might cause inconsistency
2671 2667 (e.g. incomplete fncache causes unintentional failure, but
2672 2668 redundant one doesn't).
2673 2669 """
2674 2670 unfiltered = self.unfiltered() # all file caches are stored unfiltered
2675 2671 for k in list(self._filecache.keys()):
2676 2672 # dirstate is invalidated separately in invalidatedirstate()
2677 2673 if k == b'dirstate':
2678 2674 continue
2679 2675 if (
2680 2676 k == b'changelog'
2681 2677 and self.currenttransaction()
2682 2678 and self.changelog._delayed
2683 2679 ):
2684 2680 # The changelog object may store unwritten revisions. We don't
2685 2681 # want to lose them.
2686 2682 # TODO: Solve the problem instead of working around it.
2687 2683 continue
2688 2684
2689 2685 if clearfilecache:
2690 2686 del self._filecache[k]
2691 2687 try:
2692 2688 delattr(unfiltered, k)
2693 2689 except AttributeError:
2694 2690 pass
2695 2691 self.invalidatecaches()
2696 2692 if not self.currenttransaction():
2697 2693 # TODO: Changing contents of store outside transaction
2698 2694 # causes inconsistency. We should make in-memory store
2699 2695 # changes detectable, and abort if changed.
2700 2696 self.store.invalidatecaches()
2701 2697
2702 2698 def invalidateall(self):
2703 2699 """Fully invalidates both store and non-store parts, causing the
2704 2700 subsequent operation to reread any outside changes."""
2705 2701 # extension should hook this to invalidate its caches
2706 2702 self.invalidate()
2707 2703 self.invalidatedirstate()
2708 2704
2709 2705 @unfilteredmethod
2710 2706 def _refreshfilecachestats(self, tr):
2711 2707 """Reload stats of cached files so that they are flagged as valid"""
2712 2708 for k, ce in self._filecache.items():
2713 2709 k = pycompat.sysstr(k)
2714 2710 if k == 'dirstate' or k not in self.__dict__:
2715 2711 continue
2716 2712 ce.refresh()
2717 2713
2718 2714 def _lock(
2719 2715 self,
2720 2716 vfs,
2721 2717 lockname,
2722 2718 wait,
2723 2719 releasefn,
2724 2720 acquirefn,
2725 2721 desc,
2726 2722 ):
2727 2723 timeout = 0
2728 2724 warntimeout = 0
2729 2725 if wait:
2730 2726 timeout = self.ui.configint(b"ui", b"timeout")
2731 2727 warntimeout = self.ui.configint(b"ui", b"timeout.warn")
2732 2728 # internal config: ui.signal-safe-lock
2733 2729 signalsafe = self.ui.configbool(b'ui', b'signal-safe-lock')
2734 2730
2735 2731 l = lockmod.trylock(
2736 2732 self.ui,
2737 2733 vfs,
2738 2734 lockname,
2739 2735 timeout,
2740 2736 warntimeout,
2741 2737 releasefn=releasefn,
2742 2738 acquirefn=acquirefn,
2743 2739 desc=desc,
2744 2740 signalsafe=signalsafe,
2745 2741 )
2746 2742 return l
2747 2743
2748 2744 def _afterlock(self, callback):
2749 2745 """add a callback to be run when the repository is fully unlocked
2750 2746
2751 2747 The callback will be executed when the outermost lock is released
2752 2748 (with wlock being higher level than 'lock')."""
2753 2749 for ref in (self._wlockref, self._lockref):
2754 2750 l = ref and ref()
2755 2751 if l and l.held:
2756 2752 l.postrelease.append(callback)
2757 2753 break
2758 2754 else: # no lock have been found.
2759 2755 callback(True)
2760 2756
2761 2757 def lock(self, wait=True):
2762 2758 """Lock the repository store (.hg/store) and return a weak reference
2763 2759 to the lock. Use this before modifying the store (e.g. committing or
2764 2760 stripping). If you are opening a transaction, get a lock as well.)
2765 2761
2766 2762 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2767 2763 'wlock' first to avoid a dead-lock hazard."""
2768 2764 l = self._currentlock(self._lockref)
2769 2765 if l is not None:
2770 2766 l.lock()
2771 2767 return l
2772 2768
2773 2769 l = self._lock(
2774 2770 vfs=self.svfs,
2775 2771 lockname=b"lock",
2776 2772 wait=wait,
2777 2773 releasefn=None,
2778 2774 acquirefn=self.invalidate,
2779 2775 desc=_(b'repository %s') % self.origroot,
2780 2776 )
2781 2777 self._lockref = weakref.ref(l)
2782 2778 return l
2783 2779
2784 2780 def wlock(self, wait=True):
2785 2781 """Lock the non-store parts of the repository (everything under
2786 2782 .hg except .hg/store) and return a weak reference to the lock.
2787 2783
2788 2784 Use this before modifying files in .hg.
2789 2785
2790 2786 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2791 2787 'wlock' first to avoid a dead-lock hazard."""
2792 2788 l = self._wlockref and self._wlockref()
2793 2789 if l is not None and l.held:
2794 2790 l.lock()
2795 2791 return l
2796 2792
2797 2793 # We do not need to check for non-waiting lock acquisition. Such
2798 2794 # acquisition would not cause dead-lock as they would just fail.
2799 2795 if wait and (
2800 2796 self.ui.configbool(b'devel', b'all-warnings')
2801 2797 or self.ui.configbool(b'devel', b'check-locks')
2802 2798 ):
2803 2799 if self._currentlock(self._lockref) is not None:
2804 2800 self.ui.develwarn(b'"wlock" acquired after "lock"')
2805 2801
2806 2802 def unlock():
2807 2803 if self.dirstate.pendingparentchange():
2808 2804 self.dirstate.invalidate()
2809 2805 else:
2810 2806 self.dirstate.write(None)
2811 2807
2812 2808 self._filecache[b'dirstate'].refresh()
2813 2809
2814 2810 l = self._lock(
2815 2811 self.vfs,
2816 2812 b"wlock",
2817 2813 wait,
2818 2814 unlock,
2819 2815 self.invalidatedirstate,
2820 2816 _(b'working directory of %s') % self.origroot,
2821 2817 )
2822 2818 self._wlockref = weakref.ref(l)
2823 2819 return l
2824 2820
2825 2821 def _currentlock(self, lockref):
2826 2822 """Returns the lock if it's held, or None if it's not."""
2827 2823 if lockref is None:
2828 2824 return None
2829 2825 l = lockref()
2830 2826 if l is None or not l.held:
2831 2827 return None
2832 2828 return l
2833 2829
2834 2830 def currentwlock(self):
2835 2831 """Returns the wlock if it's held, or None if it's not."""
2836 2832 return self._currentlock(self._wlockref)
2837 2833
2838 2834 def checkcommitpatterns(self, wctx, match, status, fail):
2839 2835 """check for commit arguments that aren't committable"""
2840 2836 if match.isexact() or match.prefix():
2841 2837 matched = set(status.modified + status.added + status.removed)
2842 2838
2843 2839 for f in match.files():
2844 2840 f = self.dirstate.normalize(f)
2845 2841 if f == b'.' or f in matched or f in wctx.substate:
2846 2842 continue
2847 2843 if f in status.deleted:
2848 2844 fail(f, _(b'file not found!'))
2849 2845 # Is it a directory that exists or used to exist?
2850 2846 if self.wvfs.isdir(f) or wctx.p1().hasdir(f):
2851 2847 d = f + b'/'
2852 2848 for mf in matched:
2853 2849 if mf.startswith(d):
2854 2850 break
2855 2851 else:
2856 2852 fail(f, _(b"no match under directory!"))
2857 2853 elif f not in self.dirstate:
2858 2854 fail(f, _(b"file not tracked!"))
2859 2855
2860 2856 @unfilteredmethod
2861 2857 def commit(
2862 2858 self,
2863 2859 text=b"",
2864 2860 user=None,
2865 2861 date=None,
2866 2862 match=None,
2867 2863 force=False,
2868 2864 editor=None,
2869 2865 extra=None,
2870 2866 ):
2871 2867 """Add a new revision to current repository.
2872 2868
2873 2869 Revision information is gathered from the working directory,
2874 2870 match can be used to filter the committed files. If editor is
2875 2871 supplied, it is called to get a commit message.
2876 2872 """
2877 2873 if extra is None:
2878 2874 extra = {}
2879 2875
2880 2876 def fail(f, msg):
2881 2877 raise error.InputError(b'%s: %s' % (f, msg))
2882 2878
2883 2879 if not match:
2884 2880 match = matchmod.always()
2885 2881
2886 2882 if not force:
2887 2883 match.bad = fail
2888 2884
2889 2885 # lock() for recent changelog (see issue4368)
2890 2886 with self.wlock(), self.lock():
2891 2887 wctx = self[None]
2892 2888 merge = len(wctx.parents()) > 1
2893 2889
2894 2890 if not force and merge and not match.always():
2895 2891 raise error.Abort(
2896 2892 _(
2897 2893 b'cannot partially commit a merge '
2898 2894 b'(do not specify files or patterns)'
2899 2895 )
2900 2896 )
2901 2897
2902 2898 status = self.status(match=match, clean=force)
2903 2899 if force:
2904 2900 status.modified.extend(
2905 2901 status.clean
2906 2902 ) # mq may commit clean files
2907 2903
2908 2904 # check subrepos
2909 2905 subs, commitsubs, newstate = subrepoutil.precommit(
2910 2906 self.ui, wctx, status, match, force=force
2911 2907 )
2912 2908
2913 2909 # make sure all explicit patterns are matched
2914 2910 if not force:
2915 2911 self.checkcommitpatterns(wctx, match, status, fail)
2916 2912
2917 2913 cctx = context.workingcommitctx(
2918 2914 self, status, text, user, date, extra
2919 2915 )
2920 2916
2921 2917 ms = mergestatemod.mergestate.read(self)
2922 2918 mergeutil.checkunresolved(ms)
2923 2919
2924 2920 # internal config: ui.allowemptycommit
2925 2921 if cctx.isempty() and not self.ui.configbool(
2926 2922 b'ui', b'allowemptycommit'
2927 2923 ):
2928 2924 self.ui.debug(b'nothing to commit, clearing merge state\n')
2929 2925 ms.reset()
2930 2926 return None
2931 2927
2932 2928 if merge and cctx.deleted():
2933 2929 raise error.Abort(_(b"cannot commit merge with missing files"))
2934 2930
2935 2931 if editor:
2936 2932 cctx._text = editor(self, cctx, subs)
2937 2933 edited = text != cctx._text
2938 2934
2939 2935 # Save commit message in case this transaction gets rolled back
2940 2936 # (e.g. by a pretxncommit hook). Leave the content alone on
2941 2937 # the assumption that the user will use the same editor again.
2942 2938 msgfn = self.savecommitmessage(cctx._text)
2943 2939
2944 2940 # commit subs and write new state
2945 2941 if subs:
2946 2942 uipathfn = scmutil.getuipathfn(self)
2947 2943 for s in sorted(commitsubs):
2948 2944 sub = wctx.sub(s)
2949 2945 self.ui.status(
2950 2946 _(b'committing subrepository %s\n')
2951 2947 % uipathfn(subrepoutil.subrelpath(sub))
2952 2948 )
2953 2949 sr = sub.commit(cctx._text, user, date)
2954 2950 newstate[s] = (newstate[s][0], sr)
2955 2951 subrepoutil.writestate(self, newstate)
2956 2952
2957 2953 p1, p2 = self.dirstate.parents()
2958 2954 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or b'')
2959 2955 try:
2960 2956 self.hook(
2961 2957 b"precommit", throw=True, parent1=hookp1, parent2=hookp2
2962 2958 )
2963 2959 with self.transaction(b'commit'):
2964 2960 ret = self.commitctx(cctx, True)
2965 2961 # update bookmarks, dirstate and mergestate
2966 2962 bookmarks.update(self, [p1, p2], ret)
2967 2963 cctx.markcommitted(ret)
2968 2964 ms.reset()
2969 2965 except: # re-raises
2970 2966 if edited:
2971 2967 self.ui.write(
2972 2968 _(b'note: commit message saved in %s\n') % msgfn
2973 2969 )
2974 2970 self.ui.write(
2975 2971 _(
2976 2972 b"note: use 'hg commit --logfile "
2977 2973 b".hg/last-message.txt --edit' to reuse it\n"
2978 2974 )
2979 2975 )
2980 2976 raise
2981 2977
2982 2978 def commithook(unused_success):
2983 2979 # hack for command that use a temporary commit (eg: histedit)
2984 2980 # temporary commit got stripped before hook release
2985 2981 if self.changelog.hasnode(ret):
2986 2982 self.hook(
2987 2983 b"commit", node=hex(ret), parent1=hookp1, parent2=hookp2
2988 2984 )
2989 2985
2990 2986 self._afterlock(commithook)
2991 2987 return ret
2992 2988
2993 2989 @unfilteredmethod
2994 2990 def commitctx(self, ctx, error=False, origctx=None):
2995 2991 return commit.commitctx(self, ctx, error=error, origctx=origctx)
2996 2992
2997 2993 @unfilteredmethod
2998 2994 def destroying(self):
2999 2995 """Inform the repository that nodes are about to be destroyed.
3000 2996 Intended for use by strip and rollback, so there's a common
3001 2997 place for anything that has to be done before destroying history.
3002 2998
3003 2999 This is mostly useful for saving state that is in memory and waiting
3004 3000 to be flushed when the current lock is released. Because a call to
3005 3001 destroyed is imminent, the repo will be invalidated causing those
3006 3002 changes to stay in memory (waiting for the next unlock), or vanish
3007 3003 completely.
3008 3004 """
3009 3005 # When using the same lock to commit and strip, the phasecache is left
3010 3006 # dirty after committing. Then when we strip, the repo is invalidated,
3011 3007 # causing those changes to disappear.
3012 3008 if '_phasecache' in vars(self):
3013 3009 self._phasecache.write()
3014 3010
3015 3011 @unfilteredmethod
3016 3012 def destroyed(self):
3017 3013 """Inform the repository that nodes have been destroyed.
3018 3014 Intended for use by strip and rollback, so there's a common
3019 3015 place for anything that has to be done after destroying history.
3020 3016 """
3021 3017 # When one tries to:
3022 3018 # 1) destroy nodes thus calling this method (e.g. strip)
3023 3019 # 2) use phasecache somewhere (e.g. commit)
3024 3020 #
3025 3021 # then 2) will fail because the phasecache contains nodes that were
3026 3022 # removed. We can either remove phasecache from the filecache,
3027 3023 # causing it to reload next time it is accessed, or simply filter
3028 3024 # the removed nodes now and write the updated cache.
3029 3025 self._phasecache.filterunknown(self)
3030 3026 self._phasecache.write()
3031 3027
3032 3028 # refresh all repository caches
3033 3029 self.updatecaches()
3034 3030
3035 3031 # Ensure the persistent tag cache is updated. Doing it now
3036 3032 # means that the tag cache only has to worry about destroyed
3037 3033 # heads immediately after a strip/rollback. That in turn
3038 3034 # guarantees that "cachetip == currenttip" (comparing both rev
3039 3035 # and node) always means no nodes have been added or destroyed.
3040 3036
3041 3037 # XXX this is suboptimal when qrefresh'ing: we strip the current
3042 3038 # head, refresh the tag cache, then immediately add a new head.
3043 3039 # But I think doing it this way is necessary for the "instant
3044 3040 # tag cache retrieval" case to work.
3045 3041 self.invalidate()
3046 3042
3047 3043 def status(
3048 3044 self,
3049 3045 node1=b'.',
3050 3046 node2=None,
3051 3047 match=None,
3052 3048 ignored=False,
3053 3049 clean=False,
3054 3050 unknown=False,
3055 3051 listsubrepos=False,
3056 3052 ):
3057 3053 '''a convenience method that calls node1.status(node2)'''
3058 3054 return self[node1].status(
3059 3055 node2, match, ignored, clean, unknown, listsubrepos
3060 3056 )
3061 3057
3062 3058 def addpostdsstatus(self, ps):
3063 3059 """Add a callback to run within the wlock, at the point at which status
3064 3060 fixups happen.
3065 3061
3066 3062 On status completion, callback(wctx, status) will be called with the
3067 3063 wlock held, unless the dirstate has changed from underneath or the wlock
3068 3064 couldn't be grabbed.
3069 3065
3070 3066 Callbacks should not capture and use a cached copy of the dirstate --
3071 3067 it might change in the meanwhile. Instead, they should access the
3072 3068 dirstate via wctx.repo().dirstate.
3073 3069
3074 3070 This list is emptied out after each status run -- extensions should
3075 3071 make sure it adds to this list each time dirstate.status is called.
3076 3072 Extensions should also make sure they don't call this for statuses
3077 3073 that don't involve the dirstate.
3078 3074 """
3079 3075
3080 3076 # The list is located here for uniqueness reasons -- it is actually
3081 3077 # managed by the workingctx, but that isn't unique per-repo.
3082 3078 self._postdsstatus.append(ps)
3083 3079
3084 3080 def postdsstatus(self):
3085 3081 """Used by workingctx to get the list of post-dirstate-status hooks."""
3086 3082 return self._postdsstatus
3087 3083
3088 3084 def clearpostdsstatus(self):
3089 3085 """Used by workingctx to clear post-dirstate-status hooks."""
3090 3086 del self._postdsstatus[:]
3091 3087
3092 3088 def heads(self, start=None):
3093 3089 if start is None:
3094 3090 cl = self.changelog
3095 3091 headrevs = reversed(cl.headrevs())
3096 3092 return [cl.node(rev) for rev in headrevs]
3097 3093
3098 3094 heads = self.changelog.heads(start)
3099 3095 # sort the output in rev descending order
3100 3096 return sorted(heads, key=self.changelog.rev, reverse=True)
3101 3097
3102 3098 def branchheads(self, branch=None, start=None, closed=False):
3103 3099 """return a (possibly filtered) list of heads for the given branch
3104 3100
3105 3101 Heads are returned in topological order, from newest to oldest.
3106 3102 If branch is None, use the dirstate branch.
3107 3103 If start is not None, return only heads reachable from start.
3108 3104 If closed is True, return heads that are marked as closed as well.
3109 3105 """
3110 3106 if branch is None:
3111 3107 branch = self[None].branch()
3112 3108 branches = self.branchmap()
3113 3109 if not branches.hasbranch(branch):
3114 3110 return []
3115 3111 # the cache returns heads ordered lowest to highest
3116 3112 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
3117 3113 if start is not None:
3118 3114 # filter out the heads that cannot be reached from startrev
3119 3115 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
3120 3116 bheads = [h for h in bheads if h in fbheads]
3121 3117 return bheads
3122 3118
3123 3119 def branches(self, nodes):
3124 3120 if not nodes:
3125 3121 nodes = [self.changelog.tip()]
3126 3122 b = []
3127 3123 for n in nodes:
3128 3124 t = n
3129 3125 while True:
3130 3126 p = self.changelog.parents(n)
3131 3127 if p[1] != nullid or p[0] == nullid:
3132 3128 b.append((t, n, p[0], p[1]))
3133 3129 break
3134 3130 n = p[0]
3135 3131 return b
3136 3132
3137 3133 def between(self, pairs):
3138 3134 r = []
3139 3135
3140 3136 for top, bottom in pairs:
3141 3137 n, l, i = top, [], 0
3142 3138 f = 1
3143 3139
3144 3140 while n != bottom and n != nullid:
3145 3141 p = self.changelog.parents(n)[0]
3146 3142 if i == f:
3147 3143 l.append(n)
3148 3144 f = f * 2
3149 3145 n = p
3150 3146 i += 1
3151 3147
3152 3148 r.append(l)
3153 3149
3154 3150 return r
3155 3151
3156 3152 def checkpush(self, pushop):
3157 3153 """Extensions can override this function if additional checks have
3158 3154 to be performed before pushing, or call it if they override push
3159 3155 command.
3160 3156 """
3161 3157
3162 3158 @unfilteredpropertycache
3163 3159 def prepushoutgoinghooks(self):
3164 3160 """Return util.hooks consists of a pushop with repo, remote, outgoing
3165 3161 methods, which are called before pushing changesets.
3166 3162 """
3167 3163 return util.hooks()
3168 3164
3169 3165 def pushkey(self, namespace, key, old, new):
3170 3166 try:
3171 3167 tr = self.currenttransaction()
3172 3168 hookargs = {}
3173 3169 if tr is not None:
3174 3170 hookargs.update(tr.hookargs)
3175 3171 hookargs = pycompat.strkwargs(hookargs)
3176 3172 hookargs['namespace'] = namespace
3177 3173 hookargs['key'] = key
3178 3174 hookargs['old'] = old
3179 3175 hookargs['new'] = new
3180 3176 self.hook(b'prepushkey', throw=True, **hookargs)
3181 3177 except error.HookAbort as exc:
3182 3178 self.ui.write_err(_(b"pushkey-abort: %s\n") % exc)
3183 3179 if exc.hint:
3184 3180 self.ui.write_err(_(b"(%s)\n") % exc.hint)
3185 3181 return False
3186 3182 self.ui.debug(b'pushing key for "%s:%s"\n' % (namespace, key))
3187 3183 ret = pushkey.push(self, namespace, key, old, new)
3188 3184
3189 3185 def runhook(unused_success):
3190 3186 self.hook(
3191 3187 b'pushkey',
3192 3188 namespace=namespace,
3193 3189 key=key,
3194 3190 old=old,
3195 3191 new=new,
3196 3192 ret=ret,
3197 3193 )
3198 3194
3199 3195 self._afterlock(runhook)
3200 3196 return ret
3201 3197
3202 3198 def listkeys(self, namespace):
3203 3199 self.hook(b'prelistkeys', throw=True, namespace=namespace)
3204 3200 self.ui.debug(b'listing keys for "%s"\n' % namespace)
3205 3201 values = pushkey.list(self, namespace)
3206 3202 self.hook(b'listkeys', namespace=namespace, values=values)
3207 3203 return values
3208 3204
3209 3205 def debugwireargs(self, one, two, three=None, four=None, five=None):
3210 3206 '''used to test argument passing over the wire'''
3211 3207 return b"%s %s %s %s %s" % (
3212 3208 one,
3213 3209 two,
3214 3210 pycompat.bytestr(three),
3215 3211 pycompat.bytestr(four),
3216 3212 pycompat.bytestr(five),
3217 3213 )
3218 3214
3219 3215 def savecommitmessage(self, text):
3220 3216 fp = self.vfs(b'last-message.txt', b'wb')
3221 3217 try:
3222 3218 fp.write(text)
3223 3219 finally:
3224 3220 fp.close()
3225 3221 return self.pathto(fp.name[len(self.root) + 1 :])
3226 3222
3227 3223
3228 3224 # used to avoid circular references so destructors work
3229 3225 def aftertrans(files):
3230 3226 renamefiles = [tuple(t) for t in files]
3231 3227
3232 3228 def a():
3233 3229 for vfs, src, dest in renamefiles:
3234 3230 # if src and dest refer to a same file, vfs.rename is a no-op,
3235 3231 # leaving both src and dest on disk. delete dest to make sure
3236 3232 # the rename couldn't be such a no-op.
3237 3233 vfs.tryunlink(dest)
3238 3234 try:
3239 3235 vfs.rename(src, dest)
3240 3236 except OSError: # journal file does not yet exist
3241 3237 pass
3242 3238
3243 3239 return a
3244 3240
3245 3241
3246 3242 def undoname(fn):
3247 3243 base, name = os.path.split(fn)
3248 3244 assert name.startswith(b'journal')
3249 3245 return os.path.join(base, name.replace(b'journal', b'undo', 1))
3250 3246
3251 3247
3252 3248 def instance(ui, path, create, intents=None, createopts=None):
3253 3249 localpath = util.urllocalpath(path)
3254 3250 if create:
3255 3251 createrepository(ui, localpath, createopts=createopts)
3256 3252
3257 3253 return makelocalrepository(ui, localpath, intents=intents)
3258 3254
3259 3255
3260 3256 def islocal(path):
3261 3257 return True
3262 3258
3263 3259
3264 3260 def defaultcreateopts(ui, createopts=None):
3265 3261 """Populate the default creation options for a repository.
3266 3262
3267 3263 A dictionary of explicitly requested creation options can be passed
3268 3264 in. Missing keys will be populated.
3269 3265 """
3270 3266 createopts = dict(createopts or {})
3271 3267
3272 3268 if b'backend' not in createopts:
3273 3269 # experimental config: storage.new-repo-backend
3274 3270 createopts[b'backend'] = ui.config(b'storage', b'new-repo-backend')
3275 3271
3276 3272 return createopts
3277 3273
3278 3274
3279 3275 def newreporequirements(ui, createopts):
3280 3276 """Determine the set of requirements for a new local repository.
3281 3277
3282 3278 Extensions can wrap this function to specify custom requirements for
3283 3279 new repositories.
3284 3280 """
3285 3281 # If the repo is being created from a shared repository, we copy
3286 3282 # its requirements.
3287 3283 if b'sharedrepo' in createopts:
3288 3284 requirements = set(createopts[b'sharedrepo'].requirements)
3289 3285 if createopts.get(b'sharedrelative'):
3290 3286 requirements.add(requirementsmod.RELATIVE_SHARED_REQUIREMENT)
3291 3287 else:
3292 3288 requirements.add(requirementsmod.SHARED_REQUIREMENT)
3293 3289
3294 3290 return requirements
3295 3291
3296 3292 if b'backend' not in createopts:
3297 3293 raise error.ProgrammingError(
3298 3294 b'backend key not present in createopts; '
3299 3295 b'was defaultcreateopts() called?'
3300 3296 )
3301 3297
3302 3298 if createopts[b'backend'] != b'revlogv1':
3303 3299 raise error.Abort(
3304 3300 _(
3305 3301 b'unable to determine repository requirements for '
3306 3302 b'storage backend: %s'
3307 3303 )
3308 3304 % createopts[b'backend']
3309 3305 )
3310 3306
3311 3307 requirements = {b'revlogv1'}
3312 3308 if ui.configbool(b'format', b'usestore'):
3313 3309 requirements.add(b'store')
3314 3310 if ui.configbool(b'format', b'usefncache'):
3315 3311 requirements.add(b'fncache')
3316 3312 if ui.configbool(b'format', b'dotencode'):
3317 3313 requirements.add(b'dotencode')
3318 3314
3319 3315 compengines = ui.configlist(b'format', b'revlog-compression')
3320 3316 for compengine in compengines:
3321 3317 if compengine in util.compengines:
3322 3318 break
3323 3319 else:
3324 3320 raise error.Abort(
3325 3321 _(
3326 3322 b'compression engines %s defined by '
3327 3323 b'format.revlog-compression not available'
3328 3324 )
3329 3325 % b', '.join(b'"%s"' % e for e in compengines),
3330 3326 hint=_(
3331 3327 b'run "hg debuginstall" to list available '
3332 3328 b'compression engines'
3333 3329 ),
3334 3330 )
3335 3331
3336 3332 # zlib is the historical default and doesn't need an explicit requirement.
3337 3333 if compengine == b'zstd':
3338 3334 requirements.add(b'revlog-compression-zstd')
3339 3335 elif compengine != b'zlib':
3340 3336 requirements.add(b'exp-compression-%s' % compengine)
3341 3337
3342 3338 if scmutil.gdinitconfig(ui):
3343 3339 requirements.add(b'generaldelta')
3344 3340 if ui.configbool(b'format', b'sparse-revlog'):
3345 3341 requirements.add(requirementsmod.SPARSEREVLOG_REQUIREMENT)
3346 3342
3347 3343 # experimental config: format.exp-use-side-data
3348 3344 if ui.configbool(b'format', b'exp-use-side-data'):
3349 3345 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3350 3346 # experimental config: format.exp-use-copies-side-data-changeset
3351 3347 if ui.configbool(b'format', b'exp-use-copies-side-data-changeset'):
3352 3348 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3353 3349 requirements.add(requirementsmod.COPIESSDC_REQUIREMENT)
3354 3350 if ui.configbool(b'experimental', b'treemanifest'):
3355 3351 requirements.add(requirementsmod.TREEMANIFEST_REQUIREMENT)
3356 3352
3357 3353 revlogv2 = ui.config(b'experimental', b'revlogv2')
3358 3354 if revlogv2 == b'enable-unstable-format-and-corrupt-my-data':
3359 3355 requirements.remove(b'revlogv1')
3360 3356 # generaldelta is implied by revlogv2.
3361 3357 requirements.discard(b'generaldelta')
3362 3358 requirements.add(requirementsmod.REVLOGV2_REQUIREMENT)
3363 3359 # experimental config: format.internal-phase
3364 3360 if ui.configbool(b'format', b'internal-phase'):
3365 3361 requirements.add(requirementsmod.INTERNAL_PHASE_REQUIREMENT)
3366 3362
3367 3363 if createopts.get(b'narrowfiles'):
3368 3364 requirements.add(requirementsmod.NARROW_REQUIREMENT)
3369 3365
3370 3366 if createopts.get(b'lfs'):
3371 3367 requirements.add(b'lfs')
3372 3368
3373 3369 if ui.configbool(b'format', b'bookmarks-in-store'):
3374 3370 requirements.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3375 3371
3376 3372 if ui.configbool(b'format', b'use-persistent-nodemap'):
3377 3373 requirements.add(requirementsmod.NODEMAP_REQUIREMENT)
3378 3374
3379 3375 # if share-safe is enabled, let's create the new repository with the new
3380 3376 # requirement
3381 3377 if ui.configbool(b'format', b'exp-share-safe'):
3382 3378 requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
3383 3379
3384 3380 return requirements
3385 3381
3386 3382
3387 3383 def checkrequirementscompat(ui, requirements):
3388 3384 """Checks compatibility of repository requirements enabled and disabled.
3389 3385
3390 3386 Returns a set of requirements which needs to be dropped because dependend
3391 3387 requirements are not enabled. Also warns users about it"""
3392 3388
3393 3389 dropped = set()
3394 3390
3395 3391 if b'store' not in requirements:
3396 3392 if bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT in requirements:
3397 3393 ui.warn(
3398 3394 _(
3399 3395 b'ignoring enabled \'format.bookmarks-in-store\' config '
3400 3396 b'beacuse it is incompatible with disabled '
3401 3397 b'\'format.usestore\' config\n'
3402 3398 )
3403 3399 )
3404 3400 dropped.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3405 3401
3406 3402 if (
3407 3403 requirementsmod.SHARED_REQUIREMENT in requirements
3408 3404 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
3409 3405 ):
3410 3406 raise error.Abort(
3411 3407 _(
3412 3408 b"cannot create shared repository as source was created"
3413 3409 b" with 'format.usestore' config disabled"
3414 3410 )
3415 3411 )
3416 3412
3417 3413 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
3418 3414 ui.warn(
3419 3415 _(
3420 3416 b"ignoring enabled 'format.exp-share-safe' config because "
3421 3417 b"it is incompatible with disabled 'format.usestore'"
3422 3418 b" config\n"
3423 3419 )
3424 3420 )
3425 3421 dropped.add(requirementsmod.SHARESAFE_REQUIREMENT)
3426 3422
3427 3423 return dropped
3428 3424
3429 3425
3430 3426 def filterknowncreateopts(ui, createopts):
3431 3427 """Filters a dict of repo creation options against options that are known.
3432 3428
3433 3429 Receives a dict of repo creation options and returns a dict of those
3434 3430 options that we don't know how to handle.
3435 3431
3436 3432 This function is called as part of repository creation. If the
3437 3433 returned dict contains any items, repository creation will not
3438 3434 be allowed, as it means there was a request to create a repository
3439 3435 with options not recognized by loaded code.
3440 3436
3441 3437 Extensions can wrap this function to filter out creation options
3442 3438 they know how to handle.
3443 3439 """
3444 3440 known = {
3445 3441 b'backend',
3446 3442 b'lfs',
3447 3443 b'narrowfiles',
3448 3444 b'sharedrepo',
3449 3445 b'sharedrelative',
3450 3446 b'shareditems',
3451 3447 b'shallowfilestore',
3452 3448 }
3453 3449
3454 3450 return {k: v for k, v in createopts.items() if k not in known}
3455 3451
3456 3452
3457 3453 def createrepository(ui, path, createopts=None):
3458 3454 """Create a new repository in a vfs.
3459 3455
3460 3456 ``path`` path to the new repo's working directory.
3461 3457 ``createopts`` options for the new repository.
3462 3458
3463 3459 The following keys for ``createopts`` are recognized:
3464 3460
3465 3461 backend
3466 3462 The storage backend to use.
3467 3463 lfs
3468 3464 Repository will be created with ``lfs`` requirement. The lfs extension
3469 3465 will automatically be loaded when the repository is accessed.
3470 3466 narrowfiles
3471 3467 Set up repository to support narrow file storage.
3472 3468 sharedrepo
3473 3469 Repository object from which storage should be shared.
3474 3470 sharedrelative
3475 3471 Boolean indicating if the path to the shared repo should be
3476 3472 stored as relative. By default, the pointer to the "parent" repo
3477 3473 is stored as an absolute path.
3478 3474 shareditems
3479 3475 Set of items to share to the new repository (in addition to storage).
3480 3476 shallowfilestore
3481 3477 Indicates that storage for files should be shallow (not all ancestor
3482 3478 revisions are known).
3483 3479 """
3484 3480 createopts = defaultcreateopts(ui, createopts=createopts)
3485 3481
3486 3482 unknownopts = filterknowncreateopts(ui, createopts)
3487 3483
3488 3484 if not isinstance(unknownopts, dict):
3489 3485 raise error.ProgrammingError(
3490 3486 b'filterknowncreateopts() did not return a dict'
3491 3487 )
3492 3488
3493 3489 if unknownopts:
3494 3490 raise error.Abort(
3495 3491 _(
3496 3492 b'unable to create repository because of unknown '
3497 3493 b'creation option: %s'
3498 3494 )
3499 3495 % b', '.join(sorted(unknownopts)),
3500 3496 hint=_(b'is a required extension not loaded?'),
3501 3497 )
3502 3498
3503 3499 requirements = newreporequirements(ui, createopts=createopts)
3504 3500 requirements -= checkrequirementscompat(ui, requirements)
3505 3501
3506 3502 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3507 3503
3508 3504 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3509 3505 if hgvfs.exists():
3510 3506 raise error.RepoError(_(b'repository %s already exists') % path)
3511 3507
3512 3508 if b'sharedrepo' in createopts:
3513 3509 sharedpath = createopts[b'sharedrepo'].sharedpath
3514 3510
3515 3511 if createopts.get(b'sharedrelative'):
3516 3512 try:
3517 3513 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3518 3514 except (IOError, ValueError) as e:
3519 3515 # ValueError is raised on Windows if the drive letters differ
3520 3516 # on each path.
3521 3517 raise error.Abort(
3522 3518 _(b'cannot calculate relative path'),
3523 3519 hint=stringutil.forcebytestr(e),
3524 3520 )
3525 3521
3526 3522 if not wdirvfs.exists():
3527 3523 wdirvfs.makedirs()
3528 3524
3529 3525 hgvfs.makedir(notindexed=True)
3530 3526 if b'sharedrepo' not in createopts:
3531 3527 hgvfs.mkdir(b'cache')
3532 3528 hgvfs.mkdir(b'wcache')
3533 3529
3534 3530 if b'store' in requirements and b'sharedrepo' not in createopts:
3535 3531 hgvfs.mkdir(b'store')
3536 3532
3537 3533 # We create an invalid changelog outside the store so very old
3538 3534 # Mercurial versions (which didn't know about the requirements
3539 3535 # file) encounter an error on reading the changelog. This
3540 3536 # effectively locks out old clients and prevents them from
3541 3537 # mucking with a repo in an unknown format.
3542 3538 #
3543 3539 # The revlog header has version 2, which won't be recognized by
3544 3540 # such old clients.
3545 3541 hgvfs.append(
3546 3542 b'00changelog.i',
3547 3543 b'\0\0\0\2 dummy changelog to prevent using the old repo '
3548 3544 b'layout',
3549 3545 )
3550 3546
3551 3547 # Filter the requirements into working copy and store ones
3552 3548 wcreq, storereq = scmutil.filterrequirements(requirements)
3553 3549 # write working copy ones
3554 3550 scmutil.writerequires(hgvfs, wcreq)
3555 3551 # If there are store requirements and the current repository
3556 3552 # is not a shared one, write stored requirements
3557 3553 # For new shared repository, we don't need to write the store
3558 3554 # requirements as they are already present in store requires
3559 3555 if storereq and b'sharedrepo' not in createopts:
3560 3556 storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
3561 3557 scmutil.writerequires(storevfs, storereq)
3562 3558
3563 3559 # Write out file telling readers where to find the shared store.
3564 3560 if b'sharedrepo' in createopts:
3565 3561 hgvfs.write(b'sharedpath', sharedpath)
3566 3562
3567 3563 if createopts.get(b'shareditems'):
3568 3564 shared = b'\n'.join(sorted(createopts[b'shareditems'])) + b'\n'
3569 3565 hgvfs.write(b'shared', shared)
3570 3566
3571 3567
3572 3568 def poisonrepository(repo):
3573 3569 """Poison a repository instance so it can no longer be used."""
3574 3570 # Perform any cleanup on the instance.
3575 3571 repo.close()
3576 3572
3577 3573 # Our strategy is to replace the type of the object with one that
3578 3574 # has all attribute lookups result in error.
3579 3575 #
3580 3576 # But we have to allow the close() method because some constructors
3581 3577 # of repos call close() on repo references.
3582 3578 class poisonedrepository(object):
3583 3579 def __getattribute__(self, item):
3584 3580 if item == 'close':
3585 3581 return object.__getattribute__(self, item)
3586 3582
3587 3583 raise error.ProgrammingError(
3588 3584 b'repo instances should not be used after unshare'
3589 3585 )
3590 3586
3591 3587 def close(self):
3592 3588 pass
3593 3589
3594 3590 # We may have a repoview, which intercepts __setattr__. So be sure
3595 3591 # we operate at the lowest level possible.
3596 3592 object.__setattr__(repo, '__class__', poisonedrepository)
General Comments 0
You need to be logged in to leave comments. Login now