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