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