hgext3rd/topic/server.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 04 Mar 2020 16:36:32 +0100
branchstable
changeset 5148 366f6e4108d6
parent 5140 c705c4069fb1
child 5150 e0c091b199bc
permissions -rw-r--r--
compat: fix subsettable import for mercurial <= 4.8
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
    wireprototypes,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     9
    wireprotov1peer,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    10
    wireprotov1server,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
5148
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    13
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    14
try:
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    15
    from mercurial.utils import (
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    16
        repoviewutil,
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    17
    )
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    18
    repoviewutil.subsettable
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    19
except (AttributeError, ImportError):
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    20
    # hg <= 4.8
366f6e4108d6 compat: fix subsettable import for mercurial <= 4.8
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5140
diff changeset
    21
    from mercurial import branchmap as repoviewutil
5140
c705c4069fb1 fix: reinstall import from the right location
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5139
diff changeset
    22
5139
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
from . import (
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    24
    common,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    25
    constants,
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    26
)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    27
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    28
### Visibility restriction
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    29
#
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    30
# 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
    31
# 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
    32
# 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
    33
# 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
    34
#
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    35
# 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
    36
# 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
    37
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    38
# 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
    39
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    40
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
    41
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    42
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
    43
    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
    44
    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
    45
    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
    46
    consider = mutable - filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    47
    cl = repo.changelog
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    48
    extrafiltered = set()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    49
    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
    50
        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
    51
            extrafiltered.add(r)
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    52
    if extrafiltered:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    53
        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
    54
    return filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    55
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
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
    57
    try:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    58
        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
    59
    except (ImportError, AttributeError):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    60
        # 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
    61
        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
    62
    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
    63
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    64
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
    65
    """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
    66
    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
    67
    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
    68
        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
    69
        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
    70
    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
    71
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    72
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
    73
    """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
    74
    h = repo.heads()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    75
    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
    76
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    77
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
    78
    """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
    79
    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
    80
    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
    81
        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
    82
    return caps
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 setupserver(ui):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
    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
    86
    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
    87
    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
    88
    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
    89
    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
    90
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    91
    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
    92
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    93
        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
    94
            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
    95
                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
    96
                    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
    97
                    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
    98
                        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
    99
            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
   100
            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
   101
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   102
    wireprotov1peer.peerexecutor = topicpeerexecutor
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   104
    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
   105
        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
   106
        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
   107
        repoviewutil.subsettable[b'served'] = FILTERNAME