repoview.py
436 lines
| 15.3 KiB
| text/x-python
|
PythonLexer
/ mercurial / repoview.py
|
r18100 | # repoview.py - Filtered view of a localrepo object | ||
# | ||||
# Copyright 2012 Pierre-Yves David <pierre-yves.david@ens-lyon.org> | ||||
# Logilab SA <contact@logilab.fr> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r25972 | from __future__ import absolute_import | ||
|
r18100 | import copy | ||
|
r35248 | import weakref | ||
|
r25972 | |||
|
r43754 | from .i18n import _ | ||
from .node import ( | ||||
hex, | ||||
nullrev, | ||||
) | ||||
|
r43359 | from .pycompat import ( | ||
|
r43360 | delattr, | ||
|
r43359 | getattr, | ||
setattr, | ||||
) | ||||
|
r25972 | from . import ( | ||
|
r43752 | error, | ||
|
r25972 | obsolete, | ||
phases, | ||||
|
r35249 | pycompat, | ||
|
r43750 | revlog, | ||
|
r25972 | tags as tagsmod, | ||
r42417 | util, | |||
) | ||||
|
r43346 | from .utils import repoviewutil | ||
|
r18242 | |||
|
r18293 | def hideablerevs(repo): | ||
|
r28780 | """Revision candidates to be hidden | ||
This is a standalone function to allow extensions to wrap it. | ||||
|
r18293 | |||
|
r28780 | Because we use the set of immutable changesets as a fallback subset in | ||
r42314 | branchmap (see mercurial.utils.repoviewutils.subsettable), you cannot set | |||
"public" changesets as "hideable". Doing so would break multiple code | ||||
assertions and lead to crashes.""" | ||||
|
r43347 | obsoletes = obsolete.getrevs(repo, b'obsolete') | ||
|
r39333 | internals = repo._phasecache.getrevset(repo, phases.localhiddenphases) | ||
internals = frozenset(internals) | ||||
return obsoletes | internals | ||||
|
r18293 | |||
|
r43346 | |||
|
r32580 | def pinnedrevs(repo): | ||
|
r32579 | """revisions blocking hidden changesets from being filtered | ||
r32479 | """ | |||
r32426 | ||||
cl = repo.changelog | ||||
|
r32580 | pinned = set() | ||
pinned.update([par.rev() for par in repo[None].parents()]) | ||||
pinned.update([cl.rev(bm) for bm in repo._bookmarks.values()]) | ||||
r32426 | ||||
tags = {} | ||||
tagsmod.readlocaltags(repo.ui, repo, tags, {}) | ||||
if tags: | ||||
rev, nodemap = cl.rev, cl.nodemap | ||||
|
r32580 | pinned.update(rev(t[0]) for t in tags.values() if t[0] in nodemap) | ||
return pinned | ||||
r32426 | ||||
r32476 | ||||
|
r32582 | def _revealancestors(pfunc, hidden, revs): | ||
"""reveals contiguous chains of hidden ancestors of 'revs' by removing them | ||||
from 'hidden' | ||||
r32474 | ||||
- pfunc(r): a funtion returning parent of 'r', | ||||
|
r32581 | - hidden: the (preliminary) hidden revisions, to be updated | ||
r32474 | - revs: iterable of revnum, | |||
|
r32585 | (Ancestors are revealed exclusively, i.e. the elements in 'revs' are | ||
*not* revealed) | ||||
r32474 | """ | |||
stack = list(revs) | ||||
while stack: | ||||
for p in pfunc(stack.pop()): | ||||
|
r32582 | if p != nullrev and p in hidden: | ||
|
r32581 | hidden.remove(p) | ||
r32474 | stack.append(p) | |||
|
r43346 | |||
|
r35509 | def computehidden(repo, visibilityexceptions=None): | ||
|
r18242 | """compute the set of hidden revision to filter | ||
During most operation hidden should be filtered.""" | ||||
assert not repo.changelog.filteredrevs | ||||
|
r22151 | |||
r32478 | hidden = hideablerevs(repo) | |||
if hidden: | ||||
|
r32586 | hidden = set(hidden - pinnedrevs(repo)) | ||
|
r35509 | if visibilityexceptions: | ||
hidden -= visibilityexceptions | ||||
r32478 | pfunc = repo.changelog.parentrevs | |||
|
r38174 | mutable = repo._phasecache.getrevset(repo, phases.mutablephases) | ||
r32478 | ||||
|
r32587 | visible = mutable - hidden | ||
_revealancestors(pfunc, hidden, visible) | ||||
r32478 | return frozenset(hidden) | |||
|
r18242 | |||
|
r43346 | |||
r42295 | def computesecret(repo, visibilityexceptions=None): | |||
"""compute the set of revision that can never be exposed through hgweb | ||||
Changeset in the secret phase (or above) should stay unaccessible.""" | ||||
assert not repo.changelog.filteredrevs | ||||
secrets = repo._phasecache.getrevset(repo, phases.remotehiddenphases) | ||||
return frozenset(secrets) | ||||
|
r43346 | |||
|
r35509 | def computeunserved(repo, visibilityexceptions=None): | ||
|
r18102 | """compute the set of revision that should be filtered when used a server | ||
Secret and hidden changeset should not pretend to be here.""" | ||||
assert not repo.changelog.filteredrevs | ||||
# fast path in simple case to avoid impact of non optimised code | ||||
|
r43347 | hiddens = filterrevs(repo, b'visible') | ||
secrets = filterrevs(repo, b'served.hidden') | ||||
r42294 | if secrets: | |||
r42295 | return frozenset(hiddens | secrets) | |||
|
r18273 | else: | ||
return hiddens | ||||
|
r18100 | |||
|
r43346 | |||
|
r35509 | def computemutable(repo, visibilityexceptions=None): | ||
|
r18245 | assert not repo.changelog.filteredrevs | ||
# fast check to avoid revset call on huge repo | ||||
|
r25149 | if any(repo._phasecache.phaseroots[1:]): | ||
|
r18274 | getphase = repo._phasecache.phase | ||
|
r43347 | maymutable = filterrevs(repo, b'base') | ||
|
r18274 | return frozenset(r for r in maymutable if getphase(repo, r)) | ||
|
r18245 | return frozenset() | ||
|
r43346 | |||
|
r35509 | def computeimpactable(repo, visibilityexceptions=None): | ||
|
r18246 | """Everything impactable by mutable revision | ||
|
r18462 | The immutable filter still have some chance to get invalidated. This will | ||
|
r18246 | happen when: | ||
- you garbage collect hidden changeset, | ||||
- public phase is moved backward, | ||||
- something is changed in the filtering (this could be fixed) | ||||
This filter out any mutable changeset and any public changeset that may be | ||||
impacted by something happening to a mutable revision. | ||||
This is achieved by filtered everything with a revision number egal or | ||||
higher than the first mutable changeset is filtered.""" | ||||
assert not repo.changelog.filteredrevs | ||||
cl = repo.changelog | ||||
firstmutable = len(cl) | ||||
for roots in repo._phasecache.phaseroots[1:]: | ||||
if roots: | ||||
firstmutable = min(firstmutable, min(cl.rev(r) for r in roots)) | ||||
|
r18443 | # protect from nullrev root | ||
firstmutable = max(0, firstmutable) | ||||
|
r38806 | return frozenset(pycompat.xrange(firstmutable, len(cl))) | ||
|
r18246 | |||
|
r43346 | |||
|
r18100 | # function to compute filtered set | ||
|
r20196 | # | ||
|
r20549 | # When adding a new filter you MUST update the table at: | ||
r42314 | # mercurial.utils.repoviewutil.subsettable | |||
|
r20196 | # Otherwise your filter will have to recompute all its branches cache | ||
# from scratch (very slow). | ||||
|
r43346 | filtertable = { | ||
|
r43347 | b'visible': computehidden, | ||
b'visible-hidden': computehidden, | ||||
b'served.hidden': computesecret, | ||||
b'served': computeunserved, | ||||
b'immutable': computemutable, | ||||
b'base': computeimpactable, | ||||
|
r43346 | } | ||
|
r18100 | |||
r42417 | _basefiltername = list(filtertable) | |||
|
r43346 | |||
r42417 | def extrafilter(ui): | |||
"""initialize extra filter and return its id | ||||
If extra filtering is configured, we make sure the associated filtered view | ||||
are declared and return the associated id. | ||||
""" | ||||
|
r43347 | frevs = ui.config(b'experimental', b'extra-filter-revs') | ||
r42417 | if frevs is None: | |||
return None | ||||
|
r43347 | fid = pycompat.sysbytes(util.DIGESTS[b'sha1'](frevs).hexdigest())[:12] | ||
r42417 | ||||
|
r43347 | combine = lambda fname: fname + b'%' + fid | ||
r42417 | ||||
subsettable = repoviewutil.subsettable | ||||
|
r43347 | if combine(b'base') not in filtertable: | ||
r42417 | for name in _basefiltername: | |||
|
r43346 | |||
r42417 | def extrafilteredrevs(repo, *args, **kwargs): | |||
baserevs = filtertable[name](repo, *args, **kwargs) | ||||
extrarevs = frozenset(repo.revs(frevs)) | ||||
return baserevs | extrarevs | ||||
|
r43346 | |||
r42417 | filtertable[combine(name)] = extrafilteredrevs | |||
if name in subsettable: | ||||
subsettable[combine(name)] = combine(subsettable[name]) | ||||
return fid | ||||
|
r43346 | |||
|
r35509 | def filterrevs(repo, filtername, visibilityexceptions=None): | ||
"""returns set of filtered revision for this filter name | ||||
visibilityexceptions is a set of revs which must are exceptions for | ||||
hidden-state and must be visible. They are dynamic and hence we should not | ||||
cache it's result""" | ||||
|
r18101 | if filtername not in repo.filteredrevcache: | ||
func = filtertable[filtername] | ||||
|
r35509 | if visibilityexceptions: | ||
return func(repo.unfiltered, visibilityexceptions) | ||||
|
r18101 | repo.filteredrevcache[filtername] = func(repo.unfiltered()) | ||
return repo.filteredrevcache[filtername] | ||||
|
r18100 | |||
|
r43346 | |||
|
r43746 | def wrapchangelog(unfichangelog, filteredrevs): | ||
cl = copy.copy(unfichangelog) | ||||
cl.filteredrevs = filteredrevs | ||||
|
r43747 | |||
class filteredchangelog(cl.__class__): | ||||
|
r43748 | def tiprev(self): | ||
"""filtered version of revlog.tiprev""" | ||||
for i in pycompat.xrange(len(self) - 1, -2, -1): | ||||
if i not in self.filteredrevs: | ||||
return i | ||||
|
r43747 | |||
|
r43749 | def __contains__(self, rev): | ||
"""filtered version of revlog.__contains__""" | ||||
return 0 <= rev < len(self) and rev not in self.filteredrevs | ||||
|
r43750 | def __iter__(self): | ||
"""filtered version of revlog.__iter__""" | ||||
if len(self.filteredrevs) == 0: | ||||
return revlog.revlog.__iter__(self) | ||||
def filterediter(): | ||||
for i in pycompat.xrange(len(self)): | ||||
if i not in self.filteredrevs: | ||||
yield i | ||||
return filterediter() | ||||
|
r43751 | def revs(self, start=0, stop=None): | ||
"""filtered version of revlog.revs""" | ||||
for i in super(filteredchangelog, self).revs(start, stop): | ||||
if i not in self.filteredrevs: | ||||
yield i | ||||
|
r43752 | def _checknofilteredinrevs(self, revs): | ||
"""raise the appropriate error if 'revs' contains a filtered revision | ||||
This returns a version of 'revs' to be used thereafter by the caller. | ||||
In particular, if revs is an iterator, it is converted into a set. | ||||
""" | ||||
safehasattr = util.safehasattr | ||||
if safehasattr(revs, '__next__'): | ||||
# Note that inspect.isgenerator() is not true for iterators, | ||||
revs = set(revs) | ||||
filteredrevs = self.filteredrevs | ||||
if safehasattr(revs, 'first'): # smartset | ||||
offenders = revs & filteredrevs | ||||
else: | ||||
offenders = filteredrevs.intersection(revs) | ||||
for rev in offenders: | ||||
raise error.FilteredIndexError(rev) | ||||
return revs | ||||
def headrevs(self, revs=None): | ||||
if revs is None and self.filteredrevs: | ||||
try: | ||||
return self.index.headrevsfiltered(self.filteredrevs) | ||||
# AttributeError covers non-c-extension environments and | ||||
# old c extensions without filter handling. | ||||
except AttributeError: | ||||
return self._headrevs() | ||||
if self.filteredrevs: | ||||
revs = self._checknofilteredinrevs(revs) | ||||
return super(filteredchangelog, self).headrevs(revs) | ||||
|
r43753 | def strip(self, *args, **kwargs): | ||
# XXX make something better than assert | ||||
# We can't expect proper strip behavior if we are filtered. | ||||
assert not self.filteredrevs | ||||
super(filteredchangelog, self).strip(*args, **kwargs) | ||||
|
r43754 | def rev(self, node): | ||
"""filtered version of revlog.rev""" | ||||
r = super(filteredchangelog, self).rev(node) | ||||
if r in self.filteredrevs: | ||||
raise error.FilteredLookupError( | ||||
hex(node), self.indexfile, _(b'filtered node') | ||||
) | ||||
return r | ||||
|
r43755 | def node(self, rev): | ||
"""filtered version of revlog.node""" | ||||
if rev in self.filteredrevs: | ||||
raise error.FilteredIndexError(rev) | ||||
return super(filteredchangelog, self).node(rev) | ||||
|
r43747 | cl.__class__ = filteredchangelog | ||
|
r43746 | return cl | ||
|
r18100 | class repoview(object): | ||
"""Provide a read/write view of a repo through a filtered changelog | ||||
This object is used to access a filtered version of a repository without | ||||
altering the original repository object itself. We can not alter the | ||||
original object for two main reasons: | ||||
- It prevents the use of a repo with multiple filters at the same time. In | ||||
particular when multiple threads are involved. | ||||
- It makes scope of the filtering harder to control. | ||||
This object behaves very closely to the original repository. All attribute | ||||
operations are done on the original repository: | ||||
- An access to `repoview.someattr` actually returns `repo.someattr`, | ||||
- A write to `repoview.someattr` actually sets value of `repo.someattr`, | ||||
- A deletion of `repoview.someattr` actually drops `someattr` | ||||
from `repo.__dict__`. | ||||
The only exception is the `changelog` property. It is overridden to return | ||||
a (surface) copy of `repo.changelog` with some revisions filtered. The | ||||
`filtername` attribute of the view control the revisions that need to be | ||||
filtered. (the fact the changelog is copied is an implementation detail). | ||||
Unlike attributes, this object intercepts all method calls. This means that | ||||
all methods are run on the `repoview` object with the filtered `changelog` | ||||
property. For this purpose the simple `repoview` class must be mixed with | ||||
the actual class of the repository. This ensures that the resulting | ||||
`repoview` object have the very same methods than the repo object. This | ||||
leads to the property below. | ||||
repoview.method() --> repo.__class__.method(repoview) | ||||
The inheritance has to be done dynamically because `repo` can be of any | ||||
|
r18644 | subclasses of `localrepo`. Eg: `bundlerepo` or `statichttprepo`. | ||
|
r18100 | """ | ||
|
r35508 | def __init__(self, repo, filtername, visibilityexceptions=None): | ||
|
r31221 | object.__setattr__(self, r'_unfilteredrepo', repo) | ||
object.__setattr__(self, r'filtername', filtername) | ||||
object.__setattr__(self, r'_clcachekey', None) | ||||
object.__setattr__(self, r'_clcache', None) | ||||
|
r35508 | # revs which are exceptions and must not be hidden | ||
|
r43346 | object.__setattr__(self, r'_visibilityexceptions', visibilityexceptions) | ||
|
r18100 | |||
|
r18644 | # not a propertycache on purpose we shall implement a proper cache later | ||
|
r18100 | @property | ||
def changelog(self): | ||||
"""return a filtered version of the changeset | ||||
this changelog must not be used for writing""" | ||||
# some cache may be implemented later | ||||
|
r18445 | unfi = self._unfilteredrepo | ||
unfichangelog = unfi.changelog | ||||
|
r27258 | # bypass call to changelog.method | ||
unfiindex = unfichangelog.index | ||||
|
r38887 | unfilen = len(unfiindex) | ||
|
r27258 | unfinode = unfiindex[unfilen - 1][7] | ||
|
r43534 | with util.timedcm('repo filter for %s', self.filtername): | ||
revs = filterrevs(unfi, self.filtername, self._visibilityexceptions) | ||||
|
r18445 | cl = self._clcache | ||
|
r27258 | newkey = (unfilen, unfinode, hash(revs), unfichangelog._delayed) | ||
|
r28265 | # if cl.index is not unfiindex, unfi.changelog would be | ||
# recreated, and our clcache refers to garbage object | ||||
|
r43346 | if cl is not None and ( | ||
cl.index is not unfiindex or newkey != self._clcachekey | ||||
): | ||||
|
r27258 | cl = None | ||
|
r18445 | # could have been made None by the previous if | ||
if cl is None: | ||||
|
r43746 | cl = wrapchangelog(unfichangelog, revs) | ||
|
r31358 | object.__setattr__(self, r'_clcache', cl) | ||
object.__setattr__(self, r'_clcachekey', newkey) | ||||
|
r18100 | return cl | ||
def unfiltered(self): | ||||
"""Return an unfiltered version of a repo""" | ||||
return self._unfilteredrepo | ||||
|
r35508 | def filtered(self, name, visibilityexceptions=None): | ||
|
r18100 | """Return a filtered version of a repository""" | ||
|
r35508 | if name == self.filtername and not visibilityexceptions: | ||
|
r18100 | return self | ||
|
r35508 | return self.unfiltered().filtered(name, visibilityexceptions) | ||
|
r18100 | |||
|
r35249 | def __repr__(self): | ||
|
r43346 | return r'<%s:%s %r>' % ( | ||
self.__class__.__name__, | ||||
pycompat.sysstr(self.filtername), | ||||
self.unfiltered(), | ||||
) | ||||
|
r35249 | |||
|
r18100 | # everything access are forwarded to the proxied repo | ||
def __getattr__(self, attr): | ||||
return getattr(self._unfilteredrepo, attr) | ||||
def __setattr__(self, attr, value): | ||||
return setattr(self._unfilteredrepo, attr, value) | ||||
def __delattr__(self, attr): | ||||
return delattr(self._unfilteredrepo, attr) | ||||
|
r35248 | |||
|
r43346 | |||
|
r35248 | # Python <3.4 easily leaks types via __mro__. See | ||
# https://bugs.python.org/issue17950. We cache dynamically created types | ||||
# so they won't be leaked on every invocation of repo.filtered(). | ||||
_filteredrepotypes = weakref.WeakKeyDictionary() | ||||
|
r43346 | |||
|
r35248 | def newtype(base): | ||
"""Create a new type with the repoview mixin and the given base class""" | ||||
if base not in _filteredrepotypes: | ||||
|
r43346 | |||
|
r35248 | class filteredrepo(repoview, base): | ||
pass | ||||
|
r43346 | |||
|
r35248 | _filteredrepotypes[base] = filteredrepo | ||
return _filteredrepotypes[base] | ||||