view hgext/directaccess.py @ 1363:2eaa2943f9f3

directaccess: use cached filteredrevs Before this patch we were calling directly repoview.computehidden(repo) to compute the revisions visible with direct access, without going through the caching mechanism for the filtered revisions. There was two issues with that: (1) Performance: We were not leverating the cached values of the 'visible' revs (2) Stability: If there were to be a cache inconsistency with the computation of 'visible' we would crash in the branchmap consistency check partial.validfor. Consider the scenario of rebase with bookmarks: - when we delete a bookmark on an obsolete changeset (like what rebase does when moving the bookmark after rebasing the changesets) - then this changes the value returned by repoview.computehidden(repo) as bookmarks are used as dynamic blockers in repoview.computehidden(repo) - as of now, we don't invalidate the cache in the case of bookmark change - if we have a cached value from before the bookmark change, repoview.filterrevs(repo, 'visible') considers the cached value correct and returns something different than repoview.computehidden(repo) - in turn, if we use repoview.computehidden(repo) in directaccess, the subset relationship is broken and the cache consistency assertion (parial.validfor) fails if branchmap.updatecache is called in this time window This patch leverages the caching infrastructure in place to speed up the computation of the filteredrevs for visible-directaccess-nowarn and visible-directaccess-warn. Incidentally it prevents the bug discussed in (2) from crashing when running a rebase with a bookmark. Note that there still needs to be a fix in core for the case discussed in (2). The test for this side of the fix (not core's fix for (2) is very hard to implement without introducing a lot of dependencies and does not belong here. It is much easier to have the test of the fix for the scenario (2) in core along with the fix.
author Laurent Charignon <lcharignon@fb.com>
date Sat, 13 Jun 2015 11:14:07 -0700
parents 5c13945b32fc
children 0c134ca37567
line wrap: on
line source

""" This extension provides direct access
It is the ability to refer and access hidden sha in commands provided that you 
know their value.
For example hg log -r xxx where xxx is a commit has should work whether xxx is
hidden or not as we assume that the user knows what he is doing when referring
to xxx.
"""
from mercurial import extensions
from mercurial import cmdutil
from mercurial import repoview
from mercurial import branchmap
from mercurial import revset
from mercurial import error
from mercurial import commands
from mercurial.i18n import _

cmdtable = {}
command = cmdutil.command(cmdtable)

# List of commands where no warning is shown for direct access
directaccesslevel = [
    # warning or not, extension (None if core), command name
    (False, None, 'update'), 
    (False, None, 'export'),
    (True,  'rebase', 'rebase'),
    (False,  'evolve', 'prune'),
]

def reposetup(ui, repo):
    repo._explicitaccess = set()

def _computehidden(repo):
    hidden = repoview.filterrevs(repo, 'visible')
    cl = repo.changelog
    dynamic = hidden & repo._explicitaccess
    if dynamic:
        blocked = cl.ancestors(dynamic, inclusive=True)
        hidden = frozenset(r for r in hidden if r not in blocked)
    return hidden

def setupdirectaccess():
    """ Add two new filtername that behave like visible to provide direct access
    and direct access with warning. Wraps the commands to setup direct access """
    repoview.filtertable.update({'visible-directaccess-nowarn': _computehidden})
    repoview.filtertable.update({'visible-directaccess-warn': _computehidden})
    branchmap.subsettable['visible-directaccess-nowarn'] = 'visible'
    branchmap.subsettable['visible-directaccess-warn'] = 'visible'

    for warn, ext, cmd in directaccesslevel:
        try:
            cmdtable = extensions.find(ext).cmdtable if ext else commands.table
            wrapper = wrapwithwarning if warn else wrapwithoutwarning
            extensions.wrapcommand(cmdtable, cmd, wrapper)
        except (error.UnknownCommand, KeyError):
            pass

def wrapwithoutwarning(orig, ui, repo, *args, **kwargs):
    if repo and repo.filtername == 'visible':
        repo = repo.filtered("visible-directaccess-nowarn")
    return orig(ui, repo, *args, **kwargs)

def wrapwithwarning(orig, ui, repo, *args, **kwargs):
    if repo and repo.filtername == 'visible':
        repo = repo.filtered("visible-directaccess-warn")
    return orig(ui, repo, *args, **kwargs)

def uisetup(ui):
    """ Change ordering of extensions to ensure that directaccess extsetup comes
    after the one of the extensions in the loadsafter list """
    loadsafter = ui.configlist('directaccess','loadsafter')
    order = list(extensions._order)
    directaccesidx = order.index('directaccess')

    # The min idx for directaccess to load after all the extensions in loadafter
    minidxdirectaccess = directaccesidx

    for ext in loadsafter:
        try:
            minidxdirectaccess = max(minidxdirectaccess, order.index(ext))
        except ValueError:
            pass # extension not loaded

    if minidxdirectaccess > directaccesidx:
        order.insert(minidxdirectaccess + 1, 'directaccess')
        order.remove('directaccess')
        extensions._order = order

def extsetup(ui):
    extensions.wrapfunction(revset, 'posttreebuilthook', _posttreebuilthook)
    setupdirectaccess()

def gethashsymbols(tree):
    # Returns the list of symbols of the tree that look like hashes
    # for example for the revset 3::abe3ff it will return ('abe3ff')
    if not tree:
        return []

    if len(tree) == 2 and tree[0] == "symbol":
        try:
            int(tree[1])
            return []
        except ValueError as e:
            return [tree[1]]
    elif len(tree) == 3:
        return gethashsymbols(tree[1]) + gethashsymbols(tree[2])
    else:
        return []

def _posttreebuilthook(orig, tree, repo):
    # This is use to enabled direct hash access
    # We extract the symbols that look like hashes and add them to the
    # explicitaccess set
    orig(tree, repo)
    filternm = ""
    if repo is not None:
        filternm = repo.filtername
    if filternm is not None and filternm.startswith('visible-directaccess'):
        prelength = len(repo._explicitaccess)
        accessbefore = set(repo._explicitaccess)
        repo.symbols = gethashsymbols(tree)
        cl = repo.unfiltered().changelog
        for node in repo.symbols:
            try:
                node = cl._partialmatch(node)
            except error.LookupError:
                node = None
            if node is not None:
                rev = cl.rev(node)
                if rev not in repo.changelog:
                    repo._explicitaccess.add(rev)
        if prelength != len(repo._explicitaccess):
            if repo.filtername != 'visible-directaccess-nowarn':
                unhiddencommits = repo._explicitaccess - accessbefore
                repo.ui.warn( _("Warning: accessing hidden changesets %s " 
                                "for write operation\n") % 
                                (",".join([str(repo.unfiltered()[l]) 
                                    for l in unhiddencommits])))
            repo.invalidatevolatilesets()