hgext3rd/topic/server.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 19 Feb 2020 01:35:23 +0100
changeset 5139 19b8ffd23795
child 5140 c705c4069fb1
permissions -rw-r--r--
topic: option to hide topic changesets to plain client This is the first version of an option that make topic changeset hidden to client without the extension. It might become the default in the future.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5139
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
# topic/server.py - server specific behavior with topic
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
#
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
from mercurial import (
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     6
    extensions,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     7
    repoview,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
    repoviewutil,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
    wireprototypes,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
    wireprotov1peer,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
    wireprotov1server,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
from . import (
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    15
    common,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    16
    constants,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    17
)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    18
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
### Visibility restriction
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    20
#
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    21
# Serving draft changesets with topics to clients without topic extension can
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
# confuse them, because they won't see the topic label and will consider them
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
# normal anonymous heads. Instead we have the option to not serve changesets
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
# with topics to clients without topic support.
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
#
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    26
# To achieve this, we alter the behavior of the standard `heads` commands and
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
# introduce a new `heads` command that only clients with topic will know about.
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
# compat version of the wireprotocommand decorator, taken from evolve compat
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    31
FILTERNAME = b'served-no-topic'
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    32
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    33
def computeunservedtopic(repo, visibilityexceptions=None):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    34
    assert not repo.changelog.filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
    filteredrevs = repoview.filtertable[b'served'](repo, visibilityexceptions).copy()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    36
    mutable = repoview.filtertable[b'immutable'](repo, visibilityexceptions)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    37
    consider = mutable - filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
    cl = repo.changelog
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    39
    extrafiltered = set()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
    for r in consider:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    41
        if cl.changelogrevision(r).extra.get(constants.extrakey, b''):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
            extrafiltered.add(r)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    43
    if extrafiltered:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    44
        filteredrevs = frozenset(filteredrevs | extrafiltered)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    45
    return filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    46
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
def wireprotocommand(name, args=b'', permission=b'pull'):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
    try:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
        from mercurial.wireprotov1server import wireprotocommand
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    50
    except (ImportError, AttributeError):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    51
        # hg <= 4.6 (b4d85bc122bd)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
        from mercurial.wireproto import wireprotocommand
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
    return wireprotocommand(name, args, permission=permission)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
def wrapheads(orig, repo, proto):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
    """wrap head to hide topic^W draft changeset to old client"""
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
    hidetopics = repo.ui.configbool(b'experimental', b'topic.server-gate-topic-changesets')
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
    if common.hastopicext(repo) and hidetopics:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
        h = repo.filtered(FILTERNAME).heads()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
        return wireprototypes.bytesresponse(wireprototypes.encodelist(h) + b'\n')
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
    return orig(repo, proto)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    62
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    63
def topicheads(repo, proto):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
    """Same as the normal wireprotocol command, but accessing with a different end point."""
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    65
    h = repo.heads()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    66
    return wireprototypes.bytesresponse(wireprototypes.encodelist(h) + b'\n')
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    67
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    68
def wireprotocaps(orig, repo, proto):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    69
    """advertise the new topic specific `head` command for client with topic"""
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    70
    caps = orig(repo, proto)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    71
    if common.hastopicext(repo) and repo.peer().capable(b'topics'):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
        caps.append(b'_exttopics_heads')
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    73
    return caps
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    74
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
def setupserver(ui):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
    extensions.wrapfunction(wireprotov1server, 'heads', wrapheads)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
    wireprotov1server.commands.pop(b'heads')
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
    wireprotocommand(b'heads', permission=b'pull')(wireprotov1server.heads)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    79
    wireprotocommand(b'_exttopics_heads', permission=b'pull')(topicheads)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    80
    extensions.wrapfunction(wireprotov1server, '_capabilities', wireprotocaps)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    81
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    82
    class topicpeerexecutor(wireprotov1peer.peerexecutor):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    83
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
        def callcommand(self, command, args):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
            if command == b'heads':
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
                if self._peer.capable(b'_exttopics_heads'):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    87
                    command = b'_exttopics_heads'
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    88
                    if getattr(self._peer, '_exttopics_heads', None) is None:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    89
                        self._peer._exttopics_heads = self._peer.heads
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    90
            s = super(topicpeerexecutor, self)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
            return s.callcommand(command, args)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
    wireprotov1peer.peerexecutor = topicpeerexecutor
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    95
    if FILTERNAME not in repoview.filtertable:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    96
        repoview.filtertable[FILTERNAME] = computeunservedtopic
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    97
        repoviewutil.subsettable[FILTERNAME] = b'immutable'
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    98
        repoviewutil.subsettable[b'served'] = FILTERNAME