hgext3rd/topic/flow.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 20 Mar 2020 12:37:44 +0700
changeset 5204 d7f87c7cb1f0
parent 5193 a4d081923c81
child 5276 8431bb224862
permissions -rw-r--r--
topic: compat with tr.changes[b'phases'], it's now a list
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     1
from __future__ import absolute_import
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     2
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     3
from mercurial import (
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
     4
    commands,
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     5
    error,
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
     6
    exchange,
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
     7
    extensions,
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
     8
    node,
3158
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
     9
    phases,
5204
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    10
    util,
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    11
)
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    12
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    13
from mercurial.i18n import _
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    14
4743
92e3db149d7d py3: call branchmap.items() on py3 and continue to call iteritems() on py2
Martin von Zweigbergk <martinvonz@google.com>
parents: 4647
diff changeset
    15
from . import (
92e3db149d7d py3: call branchmap.items() on py3 and continue to call iteritems() on py2
Martin von Zweigbergk <martinvonz@google.com>
parents: 4647
diff changeset
    16
    compat,
92e3db149d7d py3: call branchmap.items() on py3 and continue to call iteritems() on py2
Martin von Zweigbergk <martinvonz@google.com>
parents: 4647
diff changeset
    17
)
92e3db149d7d py3: call branchmap.items() on py3 and continue to call iteritems() on py2
Martin von Zweigbergk <martinvonz@google.com>
parents: 4647
diff changeset
    18
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    19
def enforcesinglehead(repo, tr):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    20
    branchmap = repo.filtered(b'visible').branchmap()
4743
92e3db149d7d py3: call branchmap.items() on py3 and continue to call iteritems() on py2
Martin von Zweigbergk <martinvonz@google.com>
parents: 4647
diff changeset
    21
    for name, heads in compat.branchmapitems(branchmap):
3157
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    22
        if len(heads) > 1:
f286eefbd20d topic: add an option to enforce a single head per name in a repository
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
diff changeset
    23
            hexs = [node.short(n) for n in heads]
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    24
            raise error.Abort(_(b'%d heads on "%s"') % (len(heads), name),
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    25
                              hint=(b', '.join(hexs)))
3158
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    26
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    27
def publishbarebranch(repo, tr):
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    28
    """Publish changeset without topic"""
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    29
    if b'node' not in tr.hookargs: # no new node
3158
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    30
        return
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    31
    startnode = node.bin(tr.hookargs[b'node'])
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    32
    topublish = repo.revs(b'not public() and (%n:) - hidden() - topic()', startnode)
3158
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    33
    if topublish:
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    34
        cl = repo.changelog
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    35
        nodes = [cl.node(r) for r in topublish]
678a9802c56b topic: add an option to automatically publish topic-less changeset
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3157
diff changeset
    36
        repo._phasecache.advanceboundary(repo, tr, phases.public, nodes)
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    37
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    38
def rejectuntopicedchangeset(repo, tr):
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    39
    """Reject the push if there are changeset without topic"""
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    40
    if b'node' not in tr.hookargs: # no new revs
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    41
        return
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    42
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    43
    startnode = node.bin(tr.hookargs[b'node'])
3282
3675fe74521d topic: use 'hookargs' over 'tr.changes' for flow control
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3235
diff changeset
    44
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    45
    mode = repo.ui.config(b'experimental', b'topic-mode.server', b'ignore')
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    46
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    47
    untopiced = repo.revs(b'not public() and (%n:) - hidden() - topic()', startnode)
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    48
    if untopiced:
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    49
        num = len(untopiced)
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    50
        fnode = repo[untopiced.first()].hex()[:10]
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    51
        if num == 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    52
            msg = _(b"%s") % fnode
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    53
        else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    54
            msg = _(b"%s and %d more") % (fnode, num - 1)
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    55
        if mode == b'warning':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    56
            fullmsg = _(b"pushed draft changeset without topic: %s\n")
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    57
            repo.ui.warn(fullmsg % msg)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    58
        elif mode == b'enforce':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    59
            fullmsg = _(b"rejecting draft changesets: %s")
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    60
            raise error.Abort(fullmsg % msg)
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    61
        else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    62
            repo.ui.warn(_(b"unknown 'topic-mode.server': %s\n" % mode))
3235
8a772f0c54d9 topics: add a config to reject draft changeset without topic on a server
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3226
diff changeset
    63
4647
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    64
def reject_publish(repo, tr):
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    65
    """prevent a transaction to be publish anything"""
5204
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    66
    if util.safehasattr(tr.changes[b'phases'], 'items'):
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    67
        # hg <= 5.3 (fdc802f29b2c)
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    68
        published = {
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    69
            r for r, (o, n) in tr.changes[b'phases'].items()
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    70
            if n == phases.public
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    71
        }
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    72
    else:
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    73
        revranges = [
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    74
            r for r, (o, n) in tr.changes[b'phases']
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    75
            if n == phases.public
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    76
        ]
d7f87c7cb1f0 topic: compat with tr.changes[b'phases'], it's now a list
Anton Shestakov <av6@dwimlabs.net>
parents: 5193
diff changeset
    77
        published = {r for revrange in revranges for r in revrange}
4647
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    78
    if published:
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    79
        r = min(published)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    80
        msg = b"rejecting publishing of changeset %s" % repo[r]
4647
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    81
        if len(published) > 1:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    82
            msg += b' and %d others' % (len(published) - 1)
4647
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    83
        raise error.Abort(msg)
228caeb8b7af topic: add a simple option to reject publishing
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 4263
diff changeset
    84
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    85
def wrappush(orig, repo, remote, *args, **kwargs):
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    86
    """interpret the --publish flag and pass it to the push operation"""
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    87
    newargs = kwargs.copy()
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    88
    if kwargs.pop('publish', False):
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    89
        opargs = kwargs.get('opargs')
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    90
        if opargs is None:
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    91
            opargs = {}
4808
14c12df16ab5 python3: add raw prefix to edge-cases kwargs-like objects
Raphaël Gomès <rgomes@octobus.net>
parents: 4743
diff changeset
    92
        newargs[r'opargs'] = opargs.copy()
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
    93
        newargs[r'opargs'][b'publish'] = True
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    94
    return orig(repo, remote, *args, **newargs)
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    95
3204
a342c454ccf3 pusoperation: wrap pushoperation __init__ directly
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3159
diff changeset
    96
def extendpushoperation(orig, self, *args, **kwargs):
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
    97
    publish = kwargs.pop('publish', False)
3204
a342c454ccf3 pusoperation: wrap pushoperation __init__ directly
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3159
diff changeset
    98
    orig(self, *args, **kwargs)
a342c454ccf3 pusoperation: wrap pushoperation __init__ directly
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3159
diff changeset
    99
    self.publish = publish
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   100
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   101
def wrapphasediscovery(orig, pushop):
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   102
    orig(pushop)
3226
5dfe4e5cf9e4 topic: use more protective code to access publishing code
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3204
diff changeset
   103
    if getattr(pushop, 'publish', False):
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   104
        if not pushop.remotephases.publishing:
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   105
            unfi = pushop.repo.unfiltered()
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   106
            droots = pushop.remotephases.draftroots
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
   107
            revset = b'%ln and (not public() or %ln::)'
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   108
            future = list(unfi.set(revset, pushop.futureheads, droots))
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   109
            pushop.outdatedphases = future
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   110
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   111
def installpushflag(ui):
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
   112
    entry = extensions.wrapcommand(commands.table, b'push', wrappush)
5193
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 4814
diff changeset
   113
    if not any(opt for opt in entry[1] if opt[1] == b'publish'):
a4d081923c81 compat: update hg-X.Y compat comments and test them
Anton Shestakov <av6@dwimlabs.net>
parents: 4814
diff changeset
   114
        # hg <= 4.8 (9b8d1ad851f8)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
   115
        entry[1].append((b'', b'publish', False,
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
   116
                         _(b'push the changeset as public')))
3204
a342c454ccf3 pusoperation: wrap pushoperation __init__ directly
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3159
diff changeset
   117
    extensions.wrapfunction(exchange.pushoperation, '__init__',
a342c454ccf3 pusoperation: wrap pushoperation __init__ directly
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3159
diff changeset
   118
                            extendpushoperation)
3159
90515d0bfb08 push: add a --publish flag
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 3158
diff changeset
   119
    extensions.wrapfunction(exchange, '_pushdiscoveryphase', wrapphasediscovery)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4808
diff changeset
   120
    exchange.pushdiscoverymapping[b'phase'] = exchange._pushdiscoveryphase