Mercurial > hg
diff hgext/sparse.py @ 33373:fb320398a21c
dirstate: expose a sparse matcher on dirstate (API)
The sparse extension performs a lot of monkeypatching of dirstate
to make it sparse aware. Essentially, various operations need to
take the active sparse config into account. They do this by obtaining
a matcher representing the sparse config and filtering paths through
it.
The monkeypatching is done by stuffing a reference to a repo on
dirstate and calling sparse.matcher() (which takes a repo instance)
during each function call. The reason this function takes a repo
instance is because resolving the sparse config may require resolving
file contents from filelogs, and that requires a repo. (If the
current sparse config references "profile" files, the contents of
those files from the dirstate's parent revisions is resolved.)
I seem to recall people having strong opinions that the dirstate
object not have a reference to a repo. So copying what the sparse
extension does probably won't fly in core. Plus, the dirstate
modifications shouldn't require a full repo: they only need a matcher.
So there's no good reason to stuff a reference to the repo in
dirstate.
This commit exposes a sparse matcher to dirstate via a property that
when looked up will call a function that eventually calls
sparse.matcher(). The repo instance is bound in a closure, so it
isn't exposed to dirstate.
This approach is functionally similar to what the sparse extension does
today, except it hides the repo instance from dirstate. The approach
is not optimal because we have to call a proxy function and
sparse.matcher() on every property lookup. There is room to cache
the matcher instance in dirstate. After all, the matcher only changes
if the dirstate's parents change or if the sparse config changes. It
feels like we should be able to detect both events and update the
matcher when this occurs. But for now we preserve the existing
semantics so we can move the dirstate sparseness bits into core. Once
in core, refactoring becomes a bit easier since it will be clearer how
all these components interact.
The sparse extension has been updated to use the new property.
Because all references to the repo on dirstate have been removed,
the code for setting it has been removed.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 08 Jul 2017 16:18:04 -0700 |
parents | 4481f1fd27b1 |
children | 4dc04cdf2520 |
line wrap: on
line diff
--- a/hgext/sparse.py Sat Jul 08 15:42:11 2017 -0700 +++ b/hgext/sparse.py Sat Jul 08 16:18:04 2017 -0700 @@ -82,7 +82,6 @@ error, extensions, hg, - localrepo, match as matchmod, registrar, sparse, @@ -106,13 +105,6 @@ _setupadd(ui) _setupdirstate(ui) -def reposetup(ui, repo): - if not util.safehasattr(repo, 'dirstate'): - return - - if 'dirstate' in repo._filecache: - repo.dirstate.repo = repo - def replacefilecache(cls, propname, replacement): """Replace a filecache property with a new class. This allows changing the cache invalidation condition.""" @@ -200,13 +192,6 @@ and to prevent modifications to files outside the checkout. """ - def _dirstate(orig, repo): - dirstate = orig(repo) - dirstate.repo = repo - return dirstate - extensions.wrapfunction( - localrepo.localrepository.dirstate, 'func', _dirstate) - # The atrocity below is needed to wrap dirstate._ignore. It is a cached # property, which means normal function wrapping doesn't work. class ignorewrapper(object): @@ -217,10 +202,9 @@ self.sparsematch = None def __get__(self, obj, type=None): - repo = obj.repo origignore = self.orig.__get__(obj) - sparsematch = sparse.matcher(repo) + sparsematch = obj._sparsematcher if sparsematch.always(): return origignore @@ -241,7 +225,7 @@ # dirstate.rebuild should not add non-matching files def _rebuild(orig, self, parent, allfiles, changedfiles=None): - matcher = sparse.matcher(self.repo) + matcher = self._sparsematcher if not matcher.always(): allfiles = allfiles.matches(matcher) if changedfiles: @@ -262,8 +246,7 @@ '`hg add -s <file>` to include file directory while adding') for func in editfuncs: def _wrapper(orig, self, *args): - repo = self.repo - sparsematch = sparse.matcher(repo) + sparsematch = self._sparsematcher if not sparsematch.always(): for f in args: if (f is not None and not sparsematch(f) and