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