hgext3rd/serverminitopic.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Fri, 12 Oct 2018 14:56:00 +0200
branchstable
changeset 4166 7091f7215651
parent 3679 b12c5d107187
child 4187 49d442a2207f
permissions -rw-r--r--
test: backed out output changes from changeset 6f391c2a773b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
"""enable a minimal verison of topic for server
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
Non publishing repository will see topic as "branch:topic" in the branch field.
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
In addition to adding the extensions, the feature must be manually enabled in the config:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
    [experimental]
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
    server-mini-topic = yes
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
import hashlib
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
import contextlib
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
from mercurial import (
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
    branchmap,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
    context,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
    encoding,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
    extensions,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
    node,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    registrar,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
    util,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
3679
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    23
try:
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    24
    from mercurial import wireproto
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    25
    wireproto.branchmap
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    26
except ImportError: # <= hg-4.5
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    27
    from mercurial import wireprotov1server as wireproto
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    28
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
if util.safehasattr(registrar, 'configitem'):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
    configtable = {}
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
    configitem = registrar.configitem(configtable)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
    configitem('experimental', 'server-mini-topic',
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
               default=False,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
    )
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
def hasminitopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
    """true if minitopic is enabled on the repository
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
    (The value is cached on the repository)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
    """
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
    enabled = getattr(repo, '_hasminitopic', None)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
    if enabled is None:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
        enabled = (repo.ui.configbool('experimental', 'server-mini-topic')
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    45
                   and not repo.publishing())
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
        repo._hasminitopic = enabled
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
    return enabled
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
### make topic visible though "ctx.branch()"
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    51
def topicbranch(orig, self):
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    52
    branch = orig(self)
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    53
    if hasminitopic(self._repo) and self.phase():
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    54
        topic = self._changeset.extra.get('topic')
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    55
        if topic is not None:
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    56
            topic = encoding.tolocal(topic)
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    57
            branch = '%s:%s' % (branch, topic)
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    58
    return branch
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
### avoid caching topic data in rev-branch-cache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    62
class revbranchcacheoverlay(object):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    63
    """revbranch mixin that don't use the cache for non public changeset"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    65
    def _init__(self, *args, **kwargs):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    66
        super(revbranchcacheoverlay, self).__init__(*args, **kwargs)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    67
        if 'branchinfo' in vars(self):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    68
            del self.branchinfo
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    69
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    70
    def branchinfo(self, rev):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    71
        """return branch name and close flag for rev, using and updating
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
        persistent cache."""
3511
768f752b5364 serverminitopic: fix wrong object being passed to phase
Sean Farley <sean@farley.io>
parents: 3207
diff changeset
    73
        phase = self._repo._phasecache.phase(self._repo, rev)
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    74
        if phase:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
            ctx = self._repo[rev]
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
            return ctx.branch(), ctx.closesbranch()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
        return super(revbranchcacheoverlay, self).branchinfo(rev)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
def reposetup(ui, repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
    """install a repo class with a special revbranchcache"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
    if hasminitopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    83
        repo = repo.unfiltered()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
        class minitopicrepo(repo.__class__):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
            """repository subclass that install the modified cache"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
            def revbranchcache(self):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
                if self._revbranchcache is None:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
                    cache = super(minitopicrepo, self).revbranchcache()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
                    class topicawarerbc(revbranchcacheoverlay, cache.__class__):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
                        pass
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
                    cache.__class__ = topicawarerbc
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
                    if 'branchinfo' in vars(cache):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    96
                        del cache.branchinfo
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    97
                    self._revbranchcache = cache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    98
                return self._revbranchcache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    99
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   100
        repo.__class__ = minitopicrepo
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   101
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   102
### topic aware branch head cache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   104
def _phaseshash(repo, maxrev):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   105
    """uniq ID for a phase matching a set of rev"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   106
    revs = set()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   107
    cl = repo.changelog
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   108
    fr = cl.filteredrevs
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   109
    nm = cl.nodemap
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   110
    for roots in repo._phasecache.phaseroots[1:]:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   111
        for n in roots:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   112
            r = nm.get(n)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   113
            if r not in fr and r < maxrev:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   114
                revs.add(r)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   115
    key = node.nullid
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   116
    revs = sorted(revs)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   117
    if revs:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   118
        s = hashlib.sha1()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   119
        for rev in revs:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   120
            s.update('%s;' % rev)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   121
        key = s.digest()
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   122
    return key
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   123
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   124
# needed to prevent reference used for 'super()' call using in branchmap.py to
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   125
# no go into cycle. (yes, URG)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   126
_oldbranchmap = branchmap.branchcache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   127
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   128
@contextlib.contextmanager
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   129
def oldbranchmap():
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   130
    previous = branchmap.branchcache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   131
    try:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   132
        branchmap.branchcache = _oldbranchmap
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   133
        yield
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   134
    finally:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   135
        branchmap.branchcache = previous
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   136
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   137
_publiconly = set([
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   138
    'base',
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   139
    'immutable',
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   140
])
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   141
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   142
def mighttopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   143
    return hasminitopic(repo) and repo.filtername not in _publiconly
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   144
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   145
class _topiccache(branchmap.branchcache): # combine me with branchmap.branchcache
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   146
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   147
    def __init__(self, *args, **kwargs):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   148
        # super() call may fail otherwise
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   149
        with oldbranchmap():
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   150
            super(_topiccache, self).__init__(*args, **kwargs)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   151
        self.phaseshash = None
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   152
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   153
    def copy(self):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   154
        """return an deep copy of the branchcache object"""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   155
        new = self.__class__(self, self.tipnode, self.tiprev, self.filteredhash,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   156
                             self._closednodes)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   157
        new.phaseshash = self.phaseshash
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   158
        return new
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   159
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   160
    def validfor(self, repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   161
        """Is the cache content valid regarding a repo
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   162
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   163
        - False when cached tipnode is unknown or if we detect a strip.
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   164
        - True when cache is up to date or a subset of current repo."""
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   165
        valid = super(_topiccache, self).validfor(repo)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   166
        if not valid:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   167
            return False
3518
d938808e31bc serverminitopic: attempt to fix assertion for repoview in branchmap
Sean Farley <sean@farley.io>
parents: 3511
diff changeset
   168
        elif self.phaseshash is None:
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   169
            # phasehash at None means this is a branchmap
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   170
            # coming from a public only set
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   171
            return True
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   172
        else:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   173
            try:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   174
                valid = self.phaseshash == _phaseshash(repo, self.tiprev)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   175
                return valid
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   176
            except IndexError:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   177
                return False
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   178
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   179
    def write(self, repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   180
        # we expect (hope) mutable set to be small enough to be that computing
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   181
        # it all the time will be fast enough
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   182
        if not mighttopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   183
            super(_topiccache, self).write(repo)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   184
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   185
    def update(self, repo, revgen):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   186
        """Given a branchhead cache, self, that may have extra nodes or be
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   187
        missing heads, and a generator of nodes that are strictly a superset of
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   188
        heads missing, this function updates self to be correct.
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   189
        """
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   190
        super(_topiccache, self).update(repo, revgen)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   191
        if mighttopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   192
            self.phaseshash = _phaseshash(repo, self.tiprev)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   193
3207
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   194
def wrapread(orig, repo):
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   195
    # Avoiding to write cache for filter where topic applies is a good step,
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   196
    # but we need to also avoid reading it. Existing branchmap cache might
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   197
    # exists before the turned the feature on.
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   198
    if mighttopic(repo):
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   199
        return None
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   200
    return orig(repo)
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   201
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   202
# advertise topic capabilities
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   203
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   204
def wireprotocaps(orig, repo, proto):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   205
    caps = orig(repo, proto)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   206
    if hasminitopic(repo):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   207
        caps.append('topics')
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   208
    return caps
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   209
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   210
# wrap the necessary bit
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   211
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   212
def wrapclass(container, oldname, new):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   213
    old = getattr(container, oldname)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   214
    if not issubclass(old, new):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   215
        targetclass = new
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   216
        # check if someone else already wrapped the class and handle that
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   217
        if not issubclass(new, old):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   218
            class targetclass(new, old):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   219
                pass
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   220
        setattr(container, oldname, targetclass)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   221
    current = getattr(container, oldname)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   222
    assert issubclass(current, new), (current, new, targetclass)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   223
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   224
def uisetup(ui):
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   225
    wrapclass(branchmap, 'branchcache', _topiccache)
3207
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   226
    extensions.wrapfunction(branchmap, 'read', wrapread)
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   227
    extensions.wrapfunction(wireproto, '_capabilities', wireprotocaps)
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
   228
    extensions.wrapfunction(context.changectx, 'branch', topicbranch)