2012-12-20 18:32:42 +04:00
|
|
|
# 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.
|
|
|
|
|
2015-08-09 05:58:05 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-20 18:32:42 +04:00
|
|
|
import copy
|
2017-12-05 15:50:33 +03:00
|
|
|
import weakref
|
2015-08-09 05:58:05 +03:00
|
|
|
|
|
|
|
from .node import nullrev
|
|
|
|
from . import (
|
|
|
|
obsolete,
|
|
|
|
phases,
|
2017-12-05 15:56:48 +03:00
|
|
|
pycompat,
|
2015-08-09 05:58:05 +03:00
|
|
|
tags as tagsmod,
|
|
|
|
)
|
2013-01-07 22:24:06 +04:00
|
|
|
|
flake8: enable F821 check
Summary:
This check is useful and detects real errors (ex. fbconduit). Unfortunately
`arc lint` will run it with both py2 and py3 so a lot of py2 builtins will
still be warned.
I didn't find a clean way to disable py3 check. So this diff tries to fix them.
For `xrange`, the change was done by a script:
```
import sys
import redbaron
headertypes = {'comment', 'endl', 'from_import', 'import', 'string',
'assignment', 'atomtrailers'}
xrangefix = '''try:
xrange(0)
except NameError:
xrange = range
'''
def isxrange(x):
try:
return x[0].value == 'xrange'
except Exception:
return False
def main(argv):
for i, path in enumerate(argv):
print('(%d/%d) scanning %s' % (i + 1, len(argv), path))
content = open(path).read()
try:
red = redbaron.RedBaron(content)
except Exception:
print(' warning: failed to parse')
continue
hasxrange = red.find('atomtrailersnode', value=isxrange)
hasxrangefix = 'xrange = range' in content
if hasxrangefix or not hasxrange:
print(' no need to change')
continue
# find a place to insert the compatibility statement
changed = False
for node in red:
if node.type in headertypes:
continue
# node.insert_before is an easier API, but it has bugs changing
# other "finally" and "except" positions. So do the insert
# manually.
# # node.insert_before(xrangefix)
line = node.absolute_bounding_box.top_left.line - 1
lines = content.splitlines(1)
content = ''.join(lines[:line]) + xrangefix + ''.join(lines[line:])
changed = True
break
if changed:
# "content" is faster than "red.dumps()"
open(path, 'w').write(content)
print(' updated')
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
```
For other py2 builtins that do not have a py3 equivalent, some `# noqa`
were added as a workaround for now.
Reviewed By: DurhamG
Differential Revision: D6934535
fbshipit-source-id: 546b62830af144bc8b46788d2e0fd00496838939
2018-02-10 04:31:44 +03:00
|
|
|
try:
|
|
|
|
xrange(0)
|
|
|
|
except NameError:
|
|
|
|
xrange = range
|
|
|
|
|
2013-01-10 13:25:02 +04:00
|
|
|
def hideablerevs(repo):
|
2016-04-03 01:56:47 +03:00
|
|
|
"""Revision candidates to be hidden
|
2013-01-10 13:25:02 +04:00
|
|
|
|
2016-04-03 01:56:47 +03:00
|
|
|
This is a standalone function to allow extensions to wrap it.
|
|
|
|
|
|
|
|
Because we use the set of immutable changesets as a fallback subset in
|
|
|
|
branchmap (see mercurial.branchmap.subsettable), you cannot set "public"
|
|
|
|
changesets as "hideable". Doing so would break multiple code assertions and
|
|
|
|
lead to crashes."""
|
2013-01-10 13:25:02 +04:00
|
|
|
return obsolete.getrevs(repo, 'obsolete')
|
|
|
|
|
2017-05-28 07:08:51 +03:00
|
|
|
def pinnedrevs(repo):
|
2017-05-28 07:02:17 +03:00
|
|
|
"""revisions blocking hidden changesets from being filtered
|
2017-05-21 16:56:02 +03:00
|
|
|
"""
|
2017-05-20 20:43:29 +03:00
|
|
|
|
|
|
|
cl = repo.changelog
|
2017-05-28 07:08:51 +03:00
|
|
|
pinned = set()
|
|
|
|
pinned.update([par.rev() for par in repo[None].parents()])
|
|
|
|
pinned.update([cl.rev(bm) for bm in repo._bookmarks.values()])
|
2017-05-20 20:43:29 +03:00
|
|
|
|
|
|
|
tags = {}
|
|
|
|
tagsmod.readlocaltags(repo.ui, repo, tags, {})
|
|
|
|
if tags:
|
|
|
|
rev, nodemap = cl.rev, cl.nodemap
|
2017-05-28 07:08:51 +03:00
|
|
|
pinned.update(rev(t[0]) for t in tags.values() if t[0] in nodemap)
|
|
|
|
return pinned
|
2017-05-20 20:43:29 +03:00
|
|
|
|
2017-05-28 08:10:20 +03:00
|
|
|
def _revealancestors(pfunc, hidden, revs):
|
|
|
|
"""reveals contiguous chains of hidden ancestors of 'revs' by removing them
|
|
|
|
from 'hidden'
|
2017-05-21 16:21:46 +03:00
|
|
|
|
|
|
|
- pfunc(r): a funtion returning parent of 'r',
|
2017-05-28 07:17:06 +03:00
|
|
|
- hidden: the (preliminary) hidden revisions, to be updated
|
2017-05-21 16:21:46 +03:00
|
|
|
- revs: iterable of revnum,
|
|
|
|
|
2017-05-28 09:05:10 +03:00
|
|
|
(Ancestors are revealed exclusively, i.e. the elements in 'revs' are
|
|
|
|
*not* revealed)
|
2017-05-21 16:21:46 +03:00
|
|
|
"""
|
|
|
|
stack = list(revs)
|
|
|
|
while stack:
|
|
|
|
for p in pfunc(stack.pop()):
|
2017-05-28 08:10:20 +03:00
|
|
|
if p != nullrev and p in hidden:
|
2017-05-28 07:17:06 +03:00
|
|
|
hidden.remove(p)
|
2017-05-21 16:21:46 +03:00
|
|
|
stack.append(p)
|
|
|
|
|
2013-01-07 22:24:06 +04:00
|
|
|
def computehidden(repo):
|
|
|
|
"""compute the set of hidden revision to filter
|
|
|
|
|
|
|
|
During most operation hidden should be filtered."""
|
|
|
|
assert not repo.changelog.filteredrevs
|
2014-08-12 20:39:14 +04:00
|
|
|
|
2017-05-21 16:47:06 +03:00
|
|
|
hidden = hideablerevs(repo)
|
|
|
|
if hidden:
|
2017-05-30 20:27:20 +03:00
|
|
|
hidden = set(hidden - pinnedrevs(repo))
|
2017-05-21 16:47:06 +03:00
|
|
|
pfunc = repo.changelog.parentrevs
|
|
|
|
mutablephases = (phases.draft, phases.secret)
|
|
|
|
mutable = repo._phasecache.getrevset(repo, mutablephases)
|
|
|
|
|
2017-05-30 23:16:32 +03:00
|
|
|
visible = mutable - hidden
|
|
|
|
_revealancestors(pfunc, hidden, visible)
|
2017-05-21 16:47:06 +03:00
|
|
|
return frozenset(hidden)
|
2013-01-07 22:24:06 +04:00
|
|
|
|
2012-12-17 20:12:02 +04:00
|
|
|
def computeunserved(repo):
|
|
|
|
"""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
|
2013-01-13 11:39:16 +04:00
|
|
|
hiddens = filterrevs(repo, 'visible')
|
2013-01-04 23:19:05 +04:00
|
|
|
if phases.hassecret(repo):
|
|
|
|
cl = repo.changelog
|
|
|
|
secret = phases.secret
|
|
|
|
getphase = repo._phasecache.phase
|
|
|
|
first = min(cl.rev(n) for n in repo._phasecache.phaseroots[secret])
|
|
|
|
revs = cl.revs(start=first)
|
|
|
|
secrets = set(r for r in revs if getphase(repo, r) >= secret)
|
|
|
|
return frozenset(hiddens | secrets)
|
|
|
|
else:
|
|
|
|
return hiddens
|
2012-12-20 18:32:42 +04:00
|
|
|
|
2013-01-02 04:57:46 +04:00
|
|
|
def computemutable(repo):
|
|
|
|
assert not repo.changelog.filteredrevs
|
|
|
|
# fast check to avoid revset call on huge repo
|
2015-05-16 21:30:07 +03:00
|
|
|
if any(repo._phasecache.phaseroots[1:]):
|
2013-01-07 18:50:25 +04:00
|
|
|
getphase = repo._phasecache.phase
|
2013-01-13 11:39:16 +04:00
|
|
|
maymutable = filterrevs(repo, 'base')
|
2013-01-07 18:50:25 +04:00
|
|
|
return frozenset(r for r in maymutable if getphase(repo, r))
|
2013-01-02 04:57:46 +04:00
|
|
|
return frozenset()
|
|
|
|
|
2013-01-02 05:02:41 +04:00
|
|
|
def computeimpactable(repo):
|
|
|
|
"""Everything impactable by mutable revision
|
|
|
|
|
2013-01-21 22:40:15 +04:00
|
|
|
The immutable filter still have some chance to get invalidated. This will
|
2013-01-02 05:02:41 +04:00
|
|
|
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))
|
2013-01-17 20:51:30 +04:00
|
|
|
# protect from nullrev root
|
|
|
|
firstmutable = max(0, firstmutable)
|
2013-01-02 05:02:41 +04:00
|
|
|
return frozenset(xrange(firstmutable, len(cl)))
|
|
|
|
|
2012-12-20 18:32:42 +04:00
|
|
|
# function to compute filtered set
|
2013-12-25 02:44:23 +04:00
|
|
|
#
|
2014-02-20 05:39:01 +04:00
|
|
|
# When adding a new filter you MUST update the table at:
|
2013-12-25 02:44:23 +04:00
|
|
|
# mercurial.branchmap.subsettable
|
|
|
|
# Otherwise your filter will have to recompute all its branches cache
|
|
|
|
# from scratch (very slow).
|
2013-01-13 11:39:16 +04:00
|
|
|
filtertable = {'visible': computehidden,
|
|
|
|
'served': computeunserved,
|
|
|
|
'immutable': computemutable,
|
|
|
|
'base': computeimpactable}
|
2012-12-20 18:32:42 +04:00
|
|
|
|
2013-01-13 11:39:16 +04:00
|
|
|
def filterrevs(repo, filtername):
|
2012-12-20 18:32:42 +04:00
|
|
|
"""returns set of filtered revision for this filter name"""
|
2012-12-20 20:14:07 +04:00
|
|
|
if filtername not in repo.filteredrevcache:
|
|
|
|
func = filtertable[filtername]
|
|
|
|
repo.filteredrevcache[filtername] = func(repo.unfiltered())
|
|
|
|
return repo.filteredrevcache[filtername]
|
2012-12-20 18:32:42 +04:00
|
|
|
|
|
|
|
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
|
2013-02-10 21:24:29 +04:00
|
|
|
subclasses of `localrepo`. Eg: `bundlerepo` or `statichttprepo`.
|
2012-12-20 18:32:42 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, repo, filtername):
|
2017-03-07 22:19:15 +03:00
|
|
|
object.__setattr__(self, r'_unfilteredrepo', repo)
|
|
|
|
object.__setattr__(self, r'filtername', filtername)
|
|
|
|
object.__setattr__(self, r'_clcachekey', None)
|
|
|
|
object.__setattr__(self, r'_clcache', None)
|
2012-12-20 18:32:42 +04:00
|
|
|
|
2013-02-10 21:24:29 +04:00
|
|
|
# not a propertycache on purpose we shall implement a proper cache later
|
2012-12-20 18:32:42 +04:00
|
|
|
@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
|
2013-01-19 02:43:32 +04:00
|
|
|
unfi = self._unfilteredrepo
|
|
|
|
unfichangelog = unfi.changelog
|
2015-12-05 01:22:15 +03:00
|
|
|
# bypass call to changelog.method
|
|
|
|
unfiindex = unfichangelog.index
|
|
|
|
unfilen = len(unfiindex) - 1
|
|
|
|
unfinode = unfiindex[unfilen - 1][7]
|
|
|
|
|
2013-01-19 02:43:32 +04:00
|
|
|
revs = filterrevs(unfi, self.filtername)
|
|
|
|
cl = self._clcache
|
2015-12-05 01:22:15 +03:00
|
|
|
newkey = (unfilen, unfinode, hash(revs), unfichangelog._delayed)
|
repoview: discard filtered changelog if index isn't shared with unfiltered
Before this patch, revisions rollbacked at failure of previous
transaction might be visible at subsequent operations unintentionally,
if repoview object is reused even after failure of transaction:
e.g. command server and HTTP server are typical cases.
'repoview' uses the tuple of values below of unfiltered changelog as
"the key" to examine validity of filtered changelog cache.
- length
- tip node
- filtered revisions (as hashed value)
- '_delayed' field
'repoview' compares between "the key" of unfiltered changelog at
previous caching and now, and reuses filtered changelog cache if no
change is detected.
But this comparison indicates only that there is no change between
unfiltered 'repo.changelog' at last caching and now, but not that
filtered changelog cache is valid for current unfiltered one.
'repoview' uses "shallow copy" of unfiltered changelog to create
filtered changelog cache. In this case, 'index' buffer of unfiltered
changelog is also referred by filtered changelog.
At failure of transaction, unfiltered changelog itself is invalidated
(= un-referred) on the 'repo' side (see b7829fc79508 also). But
'index' of it still contains revisions to be rollbacked at this
failure, and is referred by filtered changelog.
Therefore, even if there is no change between unfiltered
'repo.changelog' at last caching and now, steps below makes rollbacked
revisions visible via filtered changelog unintentionally.
1. instantiate unfiltered changelog as 'repo.changelog'
(call it CL1)
2. make filtered (= shallow copy of) CL1
(call it FCL1)
3. cache FCL1 with "the key" of CL1
4. revisions are appended to 'index', which is shared by CL1 and FCL1
5. invalidate 'repo.changelog' (= CL1) at failure of transaction
6. instantiate 'repo.changelog' again at next operation
(call it CL2)
CL2 doesn't have revisions added at (4), because it is
instantiated from '00changelog.i', which isn't changed while
failed transaction.
7. compare between "the key" of CL1 and CL2
8. FCL1 cached at (3) is reused, because comparison at (7) doesn't
detect change between CL1 at (1) and CL2
9. revisions rollbacked at (5) are visible via FCL1 unintentionally,
because FCL1 still refers 'index' changed at (4)
The root cause of this issue is that there is no examination about
validity of filtered changelog cache against current unfiltered one.
This patch discards filtered changelog cache, if its 'index' object
isn't shared with unfiltered one.
BTW, at the time of this patch, redundant truncation of
'00changelog.i' at failure of transaction (see b7829fc79508 for
detail) often prevents "hg serve" from making already rollbacked
revisions visible, because updating timestamps of '00changelog.i' by
truncation makes "hg serve" discard old repoview object with invalid
filtered changelog cache.
This is reason why this issue is overlooked before this patch, even
though test-bundle2-exchange.t has tests in similar situation: failure
of "hg push" via HTTP by pretxnclose hook on server side doesn't
prevent subsequent commands from looking up outgoing revisions
correctly.
But timestamp on the filesystem doesn't have enough resolution for
recent computation power, and it can't be assumed that this avoidance
always works as expected.
Therefore, without this patch, this issue might appear occasionally.
2016-02-24 00:10:46 +03:00
|
|
|
# if cl.index is not unfiindex, unfi.changelog would be
|
|
|
|
# recreated, and our clcache refers to garbage object
|
|
|
|
if (cl is not None and
|
|
|
|
(cl.index is not unfiindex or newkey != self._clcachekey)):
|
2015-12-05 01:22:15 +03:00
|
|
|
cl = None
|
2013-01-19 02:43:32 +04:00
|
|
|
# could have been made None by the previous if
|
|
|
|
if cl is None:
|
|
|
|
cl = copy.copy(unfichangelog)
|
|
|
|
cl.filteredrevs = revs
|
2017-03-12 08:48:06 +03:00
|
|
|
object.__setattr__(self, r'_clcache', cl)
|
|
|
|
object.__setattr__(self, r'_clcachekey', newkey)
|
2012-12-20 18:32:42 +04:00
|
|
|
return cl
|
|
|
|
|
|
|
|
def unfiltered(self):
|
|
|
|
"""Return an unfiltered version of a repo"""
|
|
|
|
return self._unfilteredrepo
|
|
|
|
|
|
|
|
def filtered(self, name):
|
|
|
|
"""Return a filtered version of a repository"""
|
|
|
|
if name == self.filtername:
|
|
|
|
return self
|
|
|
|
return self.unfiltered().filtered(name)
|
|
|
|
|
2017-12-05 15:56:48 +03:00
|
|
|
def __repr__(self):
|
|
|
|
return r'<%s:%s %r>' % (self.__class__.__name__,
|
|
|
|
pycompat.sysstr(self.filtername),
|
|
|
|
self.unfiltered())
|
|
|
|
|
2012-12-20 18:32:42 +04:00
|
|
|
# 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)
|
2017-12-05 15:50:33 +03:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
def newtype(base):
|
|
|
|
"""Create a new type with the repoview mixin and the given base class"""
|
|
|
|
if base not in _filteredrepotypes:
|
|
|
|
class filteredrepo(repoview, base):
|
|
|
|
pass
|
|
|
|
_filteredrepotypes[base] = filteredrepo
|
|
|
|
return _filteredrepotypes[base]
|