Debian Codegraph @

hgext/narrow/narrowdirstate.py

raw
1 # narrowdirstate.py - extensions to mercurial dirstate to support narrow clones
2 #
3 # Copyright 2017 Google, Inc.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8 from __future__ import annotations
9
10 from mercurial.i18n import _
11 from mercurial import error
12
13
14 def wrapdirstate(repo, dirstate):
15 """Add narrow spec dirstate ignore, block changes outside narrow spec."""
16
17 def _editfunc(fn):
18 def _wrapper(self, filename, *args, **kwargs):
19 narrowmatch = repo.narrowmatch()
20 if (
21 filename is not None
22 and not narrowmatch(filename)
23 and filename not in self
24 ):
25 msg = _(b"cannot track '%s' - it is outside the narrow clone")
26 msg %= filename
27 raise error.Abort(msg)
28 return fn(self, filename, *args, **kwargs)
29
30 meth_name = fn.__name__
31 if not hasattr(dirstate, meth_name):
32 # Keep this in sync with the dirstate in case it changes
33 msg = "narrow overrides a method that does not exist: %s"
34 msg %= meth_name
35 raise error.ProgrammingError(msg)
36 return _wrapper
37
38 class narrowdirstate(dirstate.__class__):
39 # Prevent adding/editing/copying/deleting files that are outside the
40 # sparse checkout
41 @_editfunc
42 def set_tracked(self, *args, **kwargs):
43 return super().set_tracked(*args, **kwargs)
44
45 @_editfunc
46 def set_untracked(self, *args, **kwargs):
47 return super().set_untracked(*args, **kwargs)
48
49 def rebuild(self, parent, allfiles, changedfiles=None):
50 if changedfiles is None:
51 # Rebuilding entire dirstate, let's filter allfiles to match the
52 # narrowspec.
53 allfiles = [f for f in allfiles if repo.narrowmatch()(f)]
54 super().rebuild(parent, allfiles, changedfiles)
55
56 dirstate.__class__ = narrowdirstate
57 return dirstate