Mercurial > hg
comparison mercurial/util.py @ 43523:c21aca51b392
utils: move the `dirs` definition in pathutil (API)
Before this change, the `dirs` class was accessible through the `mercurial.util`
module. That module is expected to stay free of scm specific content.
The `pathutil` destination has been selection by Martin von Zweigbergk.
This work is part of a refactoring to unify the revlog index and the nodemap.
This unification prepare the use of a persistent nodemap.
Differential Revision: https://phab.mercurial-scm.org/D7311
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Wed, 06 Nov 2019 14:13:19 +0100 |
parents | 9f70512ae2cf |
children | 0b7733719d21 |
comparison
equal
deleted
inserted
replaced
43522:ce96be208ea4 | 43523:c21aca51b392 |
---|---|
55 compression, | 55 compression, |
56 procutil, | 56 procutil, |
57 stringutil, | 57 stringutil, |
58 ) | 58 ) |
59 | 59 |
60 rustdirs = policy.importrust('dirstate', 'Dirs') | |
61 | |
62 base85 = policy.importmod('base85') | 60 base85 = policy.importmod('base85') |
63 osutil = policy.importmod('osutil') | 61 osutil = policy.importmod('osutil') |
64 parsers = policy.importmod('parsers') | |
65 | 62 |
66 b85decode = base85.b85decode | 63 b85decode = base85.b85decode |
67 b85encode = base85.b85encode | 64 b85encode = base85.b85encode |
68 | 65 |
69 cookielib = pycompat.cookielib | 66 cookielib = pycompat.cookielib |
3492 for line in getstackframes(skip + 1, depth=depth): | 3489 for line in getstackframes(skip + 1, depth=depth): |
3493 f.write(line) | 3490 f.write(line) |
3494 f.flush() | 3491 f.flush() |
3495 | 3492 |
3496 | 3493 |
3497 class dirs(object): | |
3498 '''a multiset of directory names from a dirstate or manifest''' | |
3499 | |
3500 def __init__(self, map, skip=None): | |
3501 self._dirs = {} | |
3502 addpath = self.addpath | |
3503 if isinstance(map, dict) and skip is not None: | |
3504 for f, s in pycompat.iteritems(map): | |
3505 if s[0] != skip: | |
3506 addpath(f) | |
3507 elif skip is not None: | |
3508 raise error.ProgrammingError( | |
3509 b"skip character is only supported with a dict source" | |
3510 ) | |
3511 else: | |
3512 for f in map: | |
3513 addpath(f) | |
3514 | |
3515 def addpath(self, path): | |
3516 dirs = self._dirs | |
3517 for base in finddirs(path): | |
3518 if base.endswith(b'/'): | |
3519 raise ValueError( | |
3520 "found invalid consecutive slashes in path: %r" % base | |
3521 ) | |
3522 if base in dirs: | |
3523 dirs[base] += 1 | |
3524 return | |
3525 dirs[base] = 1 | |
3526 | |
3527 def delpath(self, path): | |
3528 dirs = self._dirs | |
3529 for base in finddirs(path): | |
3530 if dirs[base] > 1: | |
3531 dirs[base] -= 1 | |
3532 return | |
3533 del dirs[base] | |
3534 | |
3535 def __iter__(self): | |
3536 return iter(self._dirs) | |
3537 | |
3538 def __contains__(self, d): | |
3539 return d in self._dirs | |
3540 | |
3541 | |
3542 if safehasattr(parsers, 'dirs'): | |
3543 dirs = parsers.dirs | |
3544 | |
3545 if rustdirs is not None: | |
3546 dirs = rustdirs | |
3547 | |
3548 | |
3549 def finddirs(path): | 3494 def finddirs(path): |
3550 pos = path.rfind(b'/') | 3495 pos = path.rfind(b'/') |
3551 while pos != -1: | 3496 while pos != -1: |
3552 yield path[:pos] | 3497 yield path[:pos] |
3553 pos = path.rfind(b'/', 0, pos) | 3498 pos = path.rfind(b'/', 0, pos) |