hgext3rd/serverminitopic.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 08 May 2020 22:50:09 +0800
branchmercurial-4.6
changeset 5368 844b1ad5b34b
parent 5193 a4d081923c81
permissions -rw-r--r--
test-compat: merge mercurial-4.7 into mercurial-4.6
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
4364
b74a31c69ad5 minitopic: add warning about extensions state and testing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4187
diff changeset
     3
! This extensions is not actively maintained
b74a31c69ad5 minitopic: add warning about extensions state and testing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4187
diff changeset
     4
! We recommand using the main topic extension instead
b74a31c69ad5 minitopic: add warning about extensions state and testing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4187
diff changeset
     5
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
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
     7
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
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
     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
    [experimental]
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
    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
    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
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
    14
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
    15
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
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
    17
    branchmap,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
    context,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
    encoding,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
    extensions,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
    node,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
    registrar,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
    util,
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
)
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
5180
515d425c0a05 compat: drop 4.5 related compatibility around wireprotocol module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5042
diff changeset
    26
from mercurial import wireprotov1server
3679
b12c5d107187 minitopic: handle wireproto module change from b4d85bc122bd
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3651
diff changeset
    27
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
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
    29
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
    configtable = {}
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
    configitem = registrar.configitem(configtable)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    32
    configitem(b'experimental', b'server-mini-topic',
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
               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
    34
    )
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
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
    37
    """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
    38
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
    (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
    40
    """
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
    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
    42
    if enabled is None:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    43
        enabled = (repo.ui.configbool(b'experimental', b'server-mini-topic')
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
                   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
    45
        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
    46
    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
    47
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
### 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
    49
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    50
def topicbranch(orig, self):
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    51
    branch = orig(self)
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    52
    if hasminitopic(self._repo) and self.phase():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    53
        topic = self._changeset.extra.get(b'topic')
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    54
        if topic is not None:
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    55
            topic = encoding.tolocal(topic)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
    56
            branch = b'%s:%s' % (branch, topic)
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
    57
    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
    58
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
### 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
    60
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
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
    62
    """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
    63
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
    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
    65
        super(revbranchcacheoverlay, self).__init__(*args, **kwargs)
4804
079dbf36e884 python3: add raw prefix in cases harder to analyze at the token level
Raphaël Gomès <rgomes@octobus.net>
parents: 4758
diff changeset
    66
        if r'branchinfo' in vars(self):
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    67
            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
    68
4187
49d442a2207f topic: add the changelog argument to branchinfo()
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 3679
diff changeset
    69
    def branchinfo(self, rev, changelog=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
    70
        """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
    71
        persistent cache."""
3511
768f752b5364 serverminitopic: fix wrong object being passed to phase
Sean Farley <sean@farley.io>
parents: 3207
diff changeset
    72
        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
    73
        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
    74
            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
    75
            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
    76
        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
    77
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
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
    79
    """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
    80
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
    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
    82
        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
    83
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
        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
    85
            """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
    86
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
            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
    88
                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
    89
                    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
    90
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
                    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
    92
                        pass
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
                    cache.__class__ = topicawarerbc
4804
079dbf36e884 python3: add raw prefix in cases harder to analyze at the token level
Raphaël Gomès <rgomes@octobus.net>
parents: 4758
diff changeset
    94
                    if r'branchinfo' in vars(cache):
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
                        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
    96
                    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
    97
                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
    98
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    99
        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
   100
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   101
### 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
   102
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
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
   104
    """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
   105
    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
   106
    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
   107
    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
   108
    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
   109
    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
   110
        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
   111
            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
   112
            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
   113
                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
   114
    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
   115
    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
   116
    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
   117
        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
   118
        for rev in revs:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   119
            s.update(b'%d;' % 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
   120
        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
   121
    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
   122
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   123
# 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
   124
# 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
   125
_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
   126
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   127
@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
   128
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
   129
    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
   130
    try:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   131
        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
   132
        yield
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   133
    finally:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   134
        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
   135
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   136
_publiconly = set([
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   137
    b'base',
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   138
    b'immutable',
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   139
])
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
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
   142
    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
   143
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   144
class _topiccache(branchmap.branchcache): # combine me with branchmap.branchcache
4394
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   145
    @classmethod
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   146
    def fromfile(cls, repo):
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   147
        orig = super(_topiccache, cls).fromfile
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   148
        return wrapread(orig, repo)
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   149
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   150
    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
   151
        # 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
   152
        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
   153
            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
   154
        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
   155
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   156
    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
   157
        """return an deep copy of the branchcache object"""
4521
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   158
        if util.safehasattr(self, '_entries'):
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   159
            _entries = self._entries
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   160
        else:
5193
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 5180
diff changeset
   161
            # hg <= 4.9 (624d6683c705+b137a6793c51)
4521
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   162
            _entries = self
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   163
        new = self.__class__(_entries, self.tipnode, self.tiprev,
5303b9128714 serverminitopic: add compatibility for branchcache that now uses self._entries
Anton Shestakov <av6@dwimlabs.net>
parents: 4394
diff changeset
   164
                             self.filteredhash, self._closednodes)
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   165
        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
   166
        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
   167
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   168
    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
   169
        """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
   170
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   171
        - 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
   172
        - 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
   173
        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
   174
        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
   175
            return False
3518
d938808e31bc serverminitopic: attempt to fix assertion for repoview in branchmap
Sean Farley <sean@farley.io>
parents: 3511
diff changeset
   176
        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
   177
            # 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
   178
            # 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
   179
            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
   180
        else:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   181
            try:
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   182
                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
   183
                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
   184
            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
   185
                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
   186
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   187
    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
   188
        # 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
   189
        # 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
   190
        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
   191
            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
   192
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   193
    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
   194
        """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
   195
        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
   196
        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
   197
        """
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   198
        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
   199
        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
   200
            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
   201
3207
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   202
def wrapread(orig, repo):
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   203
    # 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
   204
    # 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
   205
    # exists before the turned the feature on.
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   206
    if mighttopic(repo):
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   207
        return None
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   208
    return orig(repo)
35c79686a635 serverminitopic: also avoid reading
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3206
diff changeset
   209
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   210
# 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
   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 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
   213
    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
   214
    if hasminitopic(repo):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4804
diff changeset
   215
        caps.append(b'topics')
3206
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   216
    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
   217
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   218
# 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
   219
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   220
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
   221
    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
   222
    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
   223
        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
   224
        # 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
   225
        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
   226
            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
   227
                pass
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   228
        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
   229
    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
   230
    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
   231
3ccde4699cf0 topic: introduce a minimal extensions to enable topic on the server
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   232
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
   233
    wrapclass(branchmap, 'branchcache', _topiccache)
4394
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   234
    try:
5193
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 5180
diff changeset
   235
        # hg <= 4.9 (3461814417f3)
4394
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   236
        extensions.wrapfunction(branchmap, 'read', wrapread)
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   237
    except AttributeError:
5193
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 5180
diff changeset
   238
        # Mercurial 5.0; branchcache.fromfile now takes care of this
4394
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   239
        # which is alredy defined on _topiccache
c6d1b0a6babe topic: make topics compatible with branchmap refactor
Martijn Pieters <mj@octobus.net>
parents: 4364
diff changeset
   240
        pass
5180
515d425c0a05 compat: drop 4.5 related compatibility around wireprotocol module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5042
diff changeset
   241
    extensions.wrapfunction(wireprotov1server, '_capabilities', wireprotocaps)
3651
fa15068a9945 serverminitopic: wrap context.changectx.branch instead of context.changectx
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3518
diff changeset
   242
    extensions.wrapfunction(context.changectx, 'branch', topicbranch)