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