hgext3rd/topic/server.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 06 Apr 2020 07:26:40 +0200
branchstable
changeset 5273 f560d23639e1
parent 5150 e0c091b199bc
child 5180 515d425c0a05
permissions -rw-r--r--
head-checking: ignore obsoleted section when checking head creation locally Same as for the server side check, these do not need to be taken in account.
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:
5150
e0c091b199bc topic: extend topic gating to descendant
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 5148
diff changeset
    53
        extrafiltered = set(repo.revs('%ld::%ld', extrafiltered, consider))
5139
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    54
        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
    55
    return filteredrevs
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    56
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    57
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
    58
    try:
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    59
        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
    60
    except (ImportError, AttributeError):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    61
        # 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
    62
        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
    63
    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
    64
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    65
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
    66
    """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
    67
    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
    68
    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
    69
        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
    70
        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
    71
    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
    72
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    73
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
    74
    """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
    75
    h = repo.heads()
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    76
    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
    77
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    78
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
    79
    """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
    80
    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
    81
    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
    82
        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
    83
    return caps
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    84
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    85
def setupserver(ui):
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    86
    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
    87
    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
    88
    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
    89
    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
    90
    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
    91
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    92
    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
    93
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    94
        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
    95
            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
    96
                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
    97
                    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
    98
                    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
    99
                        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
   100
            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
   101
            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
   102
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   103
    wireprotov1peer.peerexecutor = topicpeerexecutor
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   104
19b8ffd23795 topic: option to hide topic changesets to plain client
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
   105
    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
   106
        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
   107
        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
   108
        repoviewutil.subsettable[b'served'] = FILTERNAME