hgext/commitextras.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 41532 bd3f03d8cc9f
child 43077 687b865b95ad
permissions -rw-r--r--
formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
# commitextras.py
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
#
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
# Copyright 2013 Facebook, Inc.
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
#
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
33562
3cfabb6cfd51 commitextras: mark the extension as ADVANCED
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33547
diff changeset
     8
'''adds a new flag extras to commit (ADVANCED)'''
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     9
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    10
from __future__ import absolute_import
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
33602
27fbca750b4d commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33562
diff changeset
    12
import re
27fbca750b4d commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33562
diff changeset
    13
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    14
from mercurial.i18n import _
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    15
from mercurial import (
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    16
    commands,
33547
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    17
    error,
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    18
    extensions,
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    19
    registrar,
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    20
    util,
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    21
)
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
cmdtable = {}
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    24
command = registrar.command(cmdtable)
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
testedwith = 'ships-with-hg-core'
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
33547
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    27
usedinternally = {
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    28
    'amend_source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    29
    'branch',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    30
    'close',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    31
    'histedit_source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    32
    'topic',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    33
    'rebase_source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    34
    'intermediate-source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    35
    '__touch-noise__',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    36
    'source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    37
    'transplant_source',
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    38
}
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    39
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    40
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    41
def extsetup(ui):
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
    entry = extensions.wrapcommand(commands.table, 'commit', _commit)
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
    options = entry[1]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    44
    options.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    45
        ('', 'extra', [], _('set a changeset\'s extra values'), _("KEY=VALUE"))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    46
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    47
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    49
def _commit(orig, ui, repo, *pats, **opts):
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    50
    if util.safehasattr(repo, 'unfiltered'):
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    51
        repo = repo.unfiltered()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    52
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    53
    class repoextra(repo.__class__):
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    54
        def commit(self, *innerpats, **inneropts):
34975
901a18b03e00 py3: handle keyword arguments in hgext/commitextras.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33606
diff changeset
    55
            extras = opts.get(r'extra')
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    56
            for raw in extras:
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    57
                if '=' not in raw:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    58
                    msg = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    59
                        "unable to parse '%s', should follow "
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    60
                        "KEY=VALUE format"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    61
                    )
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    62
                    raise error.Abort(msg % raw)
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    63
                k, v = raw.split('=', 1)
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    64
                if not k:
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    65
                    msg = _("unable to parse '%s', keys can't be empty")
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    66
                    raise error.Abort(msg % raw)
41532
bd3f03d8cc9f global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39295
diff changeset
    67
                if re.search(br'[^\w-]', k):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    68
                    msg = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    69
                        "keys can only contain ascii letters, digits,"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    70
                        " '_' and '-'"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    71
                    )
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    72
                    raise error.Abort(msg)
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    73
                if k in usedinternally:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    74
                    msg = _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    75
                        "key '%s' is used internally, can't be set " "manually"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    76
                    )
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    77
                    raise error.Abort(msg % k)
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    78
                inneropts[r'extra'][k] = v
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    79
            return super(repoextra, self).commit(*innerpats, **inneropts)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    80
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    81
    repo.__class__ = repoextra
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    82
    return orig(ui, repo, *pats, **opts)