hgext/closehead.py
author Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
Mon, 22 Jul 2019 06:33:11 -0400
branchstable
changeset 42643 ce52377102db
parent 40293 c303d65d2e34
child 43076 2372284d9457
permissions -rw-r--r--
amend: stop committing unrequested file reverts (issue6157) Differential Revision: https://phab.mercurial-scm.org/D6667
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
from __future__ import absolute_import
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
     9
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    10
from mercurial.i18n import _
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    11
from mercurial import (
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    12
    bookmarks,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    13
    cmdutil,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    14
    context,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    15
    error,
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
    scmutil,
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
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    21
cmdtable = {}
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    22
command = registrar.command(cmdtable)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    23
# 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
    24
# 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
    25
# 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
    26
# leave the attribute unspecified.
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    27
testedwith = 'ships-with-hg-core'
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    28
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    29
commitopts = cmdutil.commitopts
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    30
commitopts2 = cmdutil.commitopts2
40096
12a72729678e closehead: fix close-head -r listification
Joerg Sonnenberger <joerg@bec.de>
parents: 39993
diff changeset
    31
commitopts3 = [('r', 'rev', [],
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    32
               _('revision to check'), _('REV'))]
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    33
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    34
@command('close-head|close-heads', commitopts + commitopts2 + commitopts3,
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 40228
diff changeset
    35
    _('[OPTION]... [REV]...'),
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 40228
diff changeset
    36
    helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 40228
diff changeset
    37
    inferrepo=True)
39993
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    38
def close_branch(ui, repo, *revs, **opts):
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    39
    """close the given head revisions
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    40
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    41
    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
    42
    ``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
    43
    directory.
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    44
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    45
    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
    46
    """
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    47
    def docommit(rev):
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    48
        cctx = context.memctx(repo, parents=[rev, None], text=message,
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    49
                              files=[], filectxfn=None, user=opts.get('user'),
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    50
                              date=opts.get('date'), extra=extra)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    51
        tr = repo.transaction('commit')
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    52
        ret = repo.commitctx(cctx, True)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    53
        bookmarks.update(repo, [rev, None], ret)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    54
        cctx.markcommitted(ret)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    55
        tr.close()
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    56
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    57
    opts = pycompat.byteskwargs(opts)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    58
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    59
    revs += tuple(opts.get('rev', []))
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    60
    revs = scmutil.revrange(repo, revs)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    61
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    62
    if not revs:
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    63
        raise error.Abort(_('no revisions specified'))
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    64
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    65
    heads = []
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    66
    for branch in repo.branchmap():
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    67
        heads.extend(repo.branchheads(branch))
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    68
    heads = set(repo[h].rev() for h in heads)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    69
    for rev in revs:
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    70
        if rev not in heads:
40228
a17c07793dcd py3: use '%d' for rev nums instead of '%s'
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40096
diff changeset
    71
            raise error.Abort(_('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
    72
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    73
    message = cmdutil.logmessage(ui, opts)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    74
    if not message:
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    75
        raise error.Abort(_("no commit message specified with -l or -m"))
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    76
    extra = { 'close': '1' }
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    77
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    78
    with repo.wlock(), repo.lock():
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
            r = repo[rev]
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    81
            branch = r.branch()
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    82
            extra['branch'] = branch
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    83
            docommit(r)
cd5f2e615262 extensions: new closehead module for closing arbitrary heads
Joerg Sonnenberger <joerg@bec.de>
parents:
diff changeset
    84
    return 0