hgext3rd/topic/discovery.py
author Sean Farley <sean@farley.io>
Mon, 21 Mar 2016 21:26:45 -0700
changeset 1921 4898296d7d25
parent 1920 2a28bd7eab31
child 1934 9d6d30e36cdd
permissions -rw-r--r--
discovery: whitespace
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
     1
import weakref
1886
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     2
from mercurial import branchmap
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
     3
from mercurial import error
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
     4
from mercurial import exchange
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
     5
from mercurial.i18n import _
1886
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     6
from . import topicmap
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     7
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     8
def _headssummary(orig, repo, remote, outgoing):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
     9
    publishing = ('phases' not in remote.listkeys('namespaces')
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    10
                  or bool(remote.listkeys('phases').get('publishing', False)))
1903
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
    11
    if publishing or not remote.capable('topics'):
1886
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    12
        return orig(repo, remote, outgoing)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    13
    oldrepo = repo.__class__
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    14
    oldbranchcache = branchmap.branchcache
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    15
    oldfilename = branchmap._filename
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    16
    try:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    17
        class repocls(repo.__class__):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    18
            def __getitem__(self, key):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    19
                ctx = super(repocls, self).__getitem__(key)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    20
                oldbranch = ctx.branch
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    21
                def branch():
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    22
                    branch = oldbranch()
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    23
                    topic = ctx.topic()
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    24
                    if topic:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    25
                        branch = "%s:%s" % (branch, topic)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    26
                    return branch
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    27
                ctx.branch = branch
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    28
                return ctx
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    29
        repo.__class__ = repocls
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    30
        branchmap.branchcache = topicmap.topiccache
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    31
        branchmap._filename = topicmap._filename
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    32
        summary = orig(repo, remote, outgoing)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    33
        for key, value in summary.iteritems():
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    34
            if ':' in key: # This is a topic
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    35
                if value[0] is None and value[1]:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    36
                    summary[key] = ([value[1].pop(0)], ) + value[1:]
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    37
        return summary
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    38
    finally:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    39
        repo.__class__ = oldrepo
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    40
        branchmap.branchcache = oldbranchcache
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    41
        branchmap._filename = oldfilename
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    42
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    43
def wireprotobranchmap(orig, repo, proto):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    44
    oldrepo = repo.__class__
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    45
    try:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    46
        class repocls(repo.__class__):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    47
            def branchmap(self):
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    48
                usetopic = not self.publishing()
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    49
                return super(repocls, self).branchmap(topic=usetopic)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    50
        repo.__class__ = repocls
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    51
        return orig(repo, proto)
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    52
    finally:
0504e76bfbd9 push: allow pushing new topic to non-publishing server by default
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
diff changeset
    53
        repo.__class__ = oldrepo
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    54
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    55
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    56
# Discovery have deficiency around phases, branch can get new heads with pure
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    57
# phases change. This happened with a changeset was allowed to be pushed
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    58
# because it had a topic, but it later become public and create a new branch
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    59
# head.
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    60
#
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    61
# Handle this by doing an extra check for new head creation server side
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    62
def _nbheads(repo):
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    63
    data = {}
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    64
    for b in repo.branchmap().iterbranches():
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    65
        if ':' in b[0]:
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    66
            continue
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    67
        data[b[0]] = len(b[1])
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    68
    return data
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    69
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    70
def handlecheckheads(orig, op, inpart):
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    71
    orig(op, inpart)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    72
    if op.repo.publishing():
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    73
        return
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    74
    tr = op.gettransaction()
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    75
    if tr.hookargs['source'] not in ('push', 'serve'): # not a push
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    76
        return
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    77
    tr._prepushheads = _nbheads(op.repo)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    78
    reporef = weakref.ref(op.repo)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    79
    oldvalidator = tr.validator
1921
4898296d7d25 discovery: whitespace
Sean Farley <sean@farley.io>
parents: 1920
diff changeset
    80
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    81
    def validator(tr):
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    82
        repo = reporef()
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    83
        if repo is not None:
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    84
            repo.invalidatecaches()
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    85
            finalheads = _nbheads(repo)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    86
            for branch, oldnb in tr._prepushheads.iteritems():
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    87
                newnb = finalheads.pop(branch, 0)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    88
                if oldnb < newnb:
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    89
                    msg = _('push create a new head on branch "%s"' % branch)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    90
                    raise error.Abort(msg)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    91
            for branch, newnb in finalheads.iteritems():
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    92
                if 1 < newnb:
1921
4898296d7d25 discovery: whitespace
Sean Farley <sean@farley.io>
parents: 1920
diff changeset
    93
                    msg = _('push create more than 1 head on new branch "%s"'
4898296d7d25 discovery: whitespace
Sean Farley <sean@farley.io>
parents: 1920
diff changeset
    94
                            % branch)
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    95
                    raise error.Abort(msg)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    96
        return oldvalidator(tr)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    97
    tr.validator = validator
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    98
handlecheckheads.params = frozenset()
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
    99
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
   100
def _pushb2phases(orig, pushop, bundler):
1921
4898296d7d25 discovery: whitespace
Sean Farley <sean@farley.io>
parents: 1920
diff changeset
   101
    hascheck = any(p.type == 'check:heads' for p in bundler._parts)
1887
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
   102
    if pushop.outdatedphases and not hascheck:
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
   103
        exchange._pushb2ctxcheckheads(pushop, bundler)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
   104
    return orig(pushop, bundler)
68125d026b07 push: hackish handeling of new branch head from phase move
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1886
diff changeset
   105
1903
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   106
def wireprotocaps(orig, repo, proto):
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   107
    caps = orig(repo, proto)
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   108
    if repo.peer().capable('topics'):
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   109
        caps.append('topics')
58cdf061d49a topic: don't take topic into account when pushing to non-topic repo
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 1901
diff changeset
   110
    return caps