hgext/closehead.py
author Matt Harbison <matt_harbison@yahoo.com>
Wed, 13 Apr 2022 14:37:57 -0400
changeset 49074 4ba27acdea63
parent 48875 6000f5b25c9b
child 50873 259213382862
permissions -rw-r--r--
resourceutil: force filesystem access to resources when using py2exe I don't know why it doesn't work, but it avoids this fatal error on startup: > hg debugshell Traceback (most recent call last): File "hg", line 58, in <module> File "mercurial\dispatch.pyc", line 143, in run File "mercurial\dispatch.pyc", line 232, in dispatch File "mercurial\dispatch.pyc", line 254, in _rundispatch File "mercurial\ui.pyc", line 316, in load File "mercurial\rcutil.pyc", line 98, in rccomponents File "mercurial\rcutil.pyc", line 68, in default_rc_resources File "mercurial\utils\resourceutil.pyc", line 102, in contents File "<frozen zipimport>", line 775, in contents AssertionError I assume the py2 version of py2exe never hit this because `importlib.resources` failed to import. Differential Revision: https://phab.mercurial-scm.org/D12554
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     1
# closehead.py - Close arbitrary heads without checking them out first
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     2
#
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     3
# This software may be used and distributed according to the terms of the
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     4
# GNU General Public License version 2 or any later version.
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     5
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     6
'''close arbitrary heads without checking them out first'''
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     7
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     8
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     9
from mercurial.i18n import _
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    10
from mercurial import (
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    11
    bookmarks,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    12
    cmdutil,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    13
    context,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    14
    error,
48118
5105a9975407 errors: raise InputError from revsingle() iff revset provided by the user
Martin von Zweigbergk <martinvonz@google.com>
parents: 48116
diff changeset
    15
    logcmdutil,
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    16
    pycompat,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    17
    registrar,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    18
)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    19
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    20
cmdtable = {}
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    21
command = registrar.command(cmdtable)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    22
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    23
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    24
# be specifying the version(s) of Mercurial they are tested with, or
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    25
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    26
testedwith = b'ships-with-hg-core'
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    27
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    28
commitopts = cmdutil.commitopts
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    29
commitopts2 = cmdutil.commitopts2
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    30
commitopts3 = [(b'r', b'rev', [], _(b'revision to check'), _(b'REV'))]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    31
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    32
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    33
@command(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
    b'close-head|close-heads',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    35
    commitopts + commitopts2 + commitopts3,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
    _(b'[OPTION]... [REV]...'),
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 40228
diff changeset
    37
    helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    38
    inferrepo=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    39
)
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    40
def close_branch(ui, repo, *revs, **opts):
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    41
    """close the given head revisions
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    42
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    43
    This is equivalent to checking out each revision in a clean tree and running
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    44
    ``hg commit --close-branch``, except that it doesn't change the working
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    45
    directory.
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    46
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    47
    The commit message must be specified with -l or -m.
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    48
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    49
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    50
    def docommit(rev):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    51
        cctx = context.memctx(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    52
            repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    53
            parents=[rev, None],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    54
            text=message,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    55
            files=[],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    56
            filectxfn=None,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    57
            user=opts.get(b'user'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    58
            date=opts.get(b'date'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    59
            extra=extra,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40293
diff changeset
    60
        )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    61
        tr = repo.transaction(b'commit')
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    62
        ret = repo.commitctx(cctx, True)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    63
        bookmarks.update(repo, [rev, None], ret)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    64
        cctx.markcommitted(ret)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    65
        tr.close()
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    66
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    67
    opts = pycompat.byteskwargs(opts)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    68
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    69
    revs += tuple(opts.get(b'rev', []))
48116
5ced12cfa41b errors: raise InputError on bad revset to revrange() iff provided by the user
Martin von Zweigbergk <martinvonz@google.com>
parents: 44452
diff changeset
    70
    revs = logcmdutil.revrange(repo, revs)
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    71
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    72
    if not revs:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    73
        raise error.Abort(_(b'no revisions specified'))
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    74
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    75
    heads = []
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    76
    for branch in repo.branchmap():
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    77
        heads.extend(repo.branchheads(branch))
44452
9d2b2df2c2ba cleanup: run pyupgrade on our source tree to clean up varying things
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
    78
    heads = {repo[h].rev() for h in heads}
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    79
    for rev in revs:
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    80
        if rev not in heads:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
            raise error.Abort(_(b'revision is not an open head: %d') % rev)
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    82
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    83
    message = cmdutil.logmessage(ui, opts)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    84
    if not message:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    85
        raise error.Abort(_(b"no commit message specified with -l or -m"))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    86
    extra = {b'close': b'1'}
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    87
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    88
    with repo.wlock(), repo.lock():
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    89
        for rev in revs:
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    90
            r = repo[rev]
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    91
            branch = r.branch()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
            extra[b'branch'] = branch
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    93
            docommit(r)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    94
    return 0