| 1 |
# commitextras.py |
| 2 |
# |
| 3 |
# Copyright 2013 Facebook, Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
'''adds a new flag extras to commit (ADVANCED)''' |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import re |
| 13 |
|
| 14 |
from mercurial.i18n import _ |
| 15 |
from mercurial import ( |
| 16 |
commands, |
| 17 |
error, |
| 18 |
extensions, |
| 19 |
registrar, |
| 20 |
) |
| 21 |
|
| 22 |
cmdtable = {} |
| 23 |
command = registrar.command(cmdtable) |
| 24 |
testedwith = b'ships-with-hg-core' |
| 25 |
|
| 26 |
usedinternally = { |
| 27 |
b'amend_source', |
| 28 |
b'branch', |
| 29 |
b'close', |
| 30 |
b'histedit_source', |
| 31 |
b'topic', |
| 32 |
b'rebase_source', |
| 33 |
b'intermediate-source', |
| 34 |
b'__touch-noise__', |
| 35 |
b'source', |
| 36 |
b'transplant_source', |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
def extsetup(ui): |
| 41 |
entry = extensions.wrapcommand(commands.table, b'commit', _commit) |
| 42 |
options = entry[1] |
| 43 |
options.append( |
| 44 |
( |
| 45 |
b'', |
| 46 |
b'extra', |
| 47 |
[], |
| 48 |
_(b'set a changeset\'s extra values'), |
| 49 |
_(b"KEY=VALUE"), |
| 50 |
) |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def _parse_extras(extras): |
| 55 |
res = {} |
| 56 |
for raw in extras: |
| 57 |
if b'=' not in raw: |
| 58 |
msg = _(b"unable to parse '%s', should follow KEY=VALUE format") |
| 59 |
raise error.InputError(msg % raw) |
| 60 |
k, v = raw.split(b'=', 1) |
| 61 |
if not k: |
| 62 |
msg = _(b"unable to parse '%s', keys can't be empty") |
| 63 |
raise error.InputError(msg % raw) |
| 64 |
if re.search(br'[^\w-]', k): |
| 65 |
msg = _(b"keys can only contain ascii letters, digits, '_' and '-'") |
| 66 |
raise error.InputError(msg) |
| 67 |
if k in usedinternally: |
| 68 |
msg = _(b"key '%s' is used internally, can't be set manually") |
| 69 |
raise error.InputError(msg % k) |
| 70 |
res[k] = v |
| 71 |
return res |
| 72 |
|
| 73 |
|
| 74 |
def _commit(orig, ui, repo, *pats, **opts): |
| 75 |
if hasattr(repo, 'unfiltered'): |
| 76 |
repo = repo.unfiltered() |
| 77 |
|
| 78 |
class repoextra(repo.__class__): |
| 79 |
def commit(self, *innerpats, **inneropts): |
| 80 |
extras = opts.get('extra') |
| 81 |
parsed = _parse_extras(extras) |
| 82 |
for k in parsed: |
| 83 |
inneropts['extra'][k] = parsed[k] |
| 84 |
return super().commit(*innerpats, **inneropts) |
| 85 |
|
| 86 |
repo.__class__ = repoextra |
| 87 |
return orig(ui, repo, *pats, **opts) |