Mercurial > hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
33372:4481f1fd27b1 | 33373:fb320398a21c |
---|---|
80 commands, | 80 commands, |
81 dirstate, | 81 dirstate, |
82 error, | 82 error, |
83 extensions, | 83 extensions, |
84 hg, | 84 hg, |
85 localrepo, | |
86 match as matchmod, | 85 match as matchmod, |
87 registrar, | 86 registrar, |
88 sparse, | 87 sparse, |
89 util, | 88 util, |
90 ) | 89 ) |
103 | 102 |
104 _setupclone(ui) | 103 _setupclone(ui) |
105 _setuplog(ui) | 104 _setuplog(ui) |
106 _setupadd(ui) | 105 _setupadd(ui) |
107 _setupdirstate(ui) | 106 _setupdirstate(ui) |
108 | |
109 def reposetup(ui, repo): | |
110 if not util.safehasattr(repo, 'dirstate'): | |
111 return | |
112 | |
113 if 'dirstate' in repo._filecache: | |
114 repo.dirstate.repo = repo | |
115 | 107 |
116 def replacefilecache(cls, propname, replacement): | 108 def replacefilecache(cls, propname, replacement): |
117 """Replace a filecache property with a new class. This allows changing the | 109 """Replace a filecache property with a new class. This allows changing the |
118 cache invalidation condition.""" | 110 cache invalidation condition.""" |
119 origcls = cls | 111 origcls = cls |
198 def _setupdirstate(ui): | 190 def _setupdirstate(ui): |
199 """Modify the dirstate to prevent stat'ing excluded files, | 191 """Modify the dirstate to prevent stat'ing excluded files, |
200 and to prevent modifications to files outside the checkout. | 192 and to prevent modifications to files outside the checkout. |
201 """ | 193 """ |
202 | 194 |
203 def _dirstate(orig, repo): | |
204 dirstate = orig(repo) | |
205 dirstate.repo = repo | |
206 return dirstate | |
207 extensions.wrapfunction( | |
208 localrepo.localrepository.dirstate, 'func', _dirstate) | |
209 | |
210 # The atrocity below is needed to wrap dirstate._ignore. It is a cached | 195 # The atrocity below is needed to wrap dirstate._ignore. It is a cached |
211 # property, which means normal function wrapping doesn't work. | 196 # property, which means normal function wrapping doesn't work. |
212 class ignorewrapper(object): | 197 class ignorewrapper(object): |
213 def __init__(self, orig): | 198 def __init__(self, orig): |
214 self.orig = orig | 199 self.orig = orig |
215 self.origignore = None | 200 self.origignore = None |
216 self.func = None | 201 self.func = None |
217 self.sparsematch = None | 202 self.sparsematch = None |
218 | 203 |
219 def __get__(self, obj, type=None): | 204 def __get__(self, obj, type=None): |
220 repo = obj.repo | |
221 origignore = self.orig.__get__(obj) | 205 origignore = self.orig.__get__(obj) |
222 | 206 |
223 sparsematch = sparse.matcher(repo) | 207 sparsematch = obj._sparsematcher |
224 if sparsematch.always(): | 208 if sparsematch.always(): |
225 return origignore | 209 return origignore |
226 | 210 |
227 if self.sparsematch != sparsematch or self.origignore != origignore: | 211 if self.sparsematch != sparsematch or self.origignore != origignore: |
228 self.func = matchmod.unionmatcher([ | 212 self.func = matchmod.unionmatcher([ |
239 | 223 |
240 replacefilecache(dirstate.dirstate, '_ignore', ignorewrapper) | 224 replacefilecache(dirstate.dirstate, '_ignore', ignorewrapper) |
241 | 225 |
242 # dirstate.rebuild should not add non-matching files | 226 # dirstate.rebuild should not add non-matching files |
243 def _rebuild(orig, self, parent, allfiles, changedfiles=None): | 227 def _rebuild(orig, self, parent, allfiles, changedfiles=None): |
244 matcher = sparse.matcher(self.repo) | 228 matcher = self._sparsematcher |
245 if not matcher.always(): | 229 if not matcher.always(): |
246 allfiles = allfiles.matches(matcher) | 230 allfiles = allfiles.matches(matcher) |
247 if changedfiles: | 231 if changedfiles: |
248 changedfiles = [f for f in changedfiles if matcher(f)] | 232 changedfiles = [f for f in changedfiles if matcher(f)] |
249 | 233 |
260 editfuncs = ['normal', 'add', 'normallookup', 'copy', 'remove', 'merge'] | 244 editfuncs = ['normal', 'add', 'normallookup', 'copy', 'remove', 'merge'] |
261 hint = _('include file with `hg debugsparse --include <pattern>` or use ' + | 245 hint = _('include file with `hg debugsparse --include <pattern>` or use ' + |
262 '`hg add -s <file>` to include file directory while adding') | 246 '`hg add -s <file>` to include file directory while adding') |
263 for func in editfuncs: | 247 for func in editfuncs: |
264 def _wrapper(orig, self, *args): | 248 def _wrapper(orig, self, *args): |
265 repo = self.repo | 249 sparsematch = self._sparsematcher |
266 sparsematch = sparse.matcher(repo) | |
267 if not sparsematch.always(): | 250 if not sparsematch.always(): |
268 for f in args: | 251 for f in args: |
269 if (f is not None and not sparsematch(f) and | 252 if (f is not None and not sparsematch(f) and |
270 f not in self): | 253 f not in self): |
271 raise error.Abort(_("cannot add '%s' - it is outside " | 254 raise error.Abort(_("cannot add '%s' - it is outside " |