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