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