hgext/commitextras.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 07 Mar 2024 10:55:22 +0100
changeset 51531 f85f23f1479b
parent 50928 d718eddf01d9
permissions -rw-r--r--
branchcache: skip entries that are topological heads in the on disk file In the majority of cases, topological heads are also branch heads. We have efficient way to get the topological heads and efficient way to retrieve their branch information. So there is little value in putting them in the branch cache file explicitly. On the contrary, writing them explicitly tend to create very large cache file that are inefficient to read and update. So the branch cache v3 format is no longer including them. This changeset focus on the format aspect and have no focus on the performance aspect. We will cover that later.

# commitextras.py
#
# Copyright 2013 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

'''adds a new flag extras to commit (ADVANCED)'''


import re

from mercurial.i18n import _
from mercurial import (
    commands,
    error,
    extensions,
    registrar,
)

cmdtable = {}
command = registrar.command(cmdtable)
testedwith = b'ships-with-hg-core'

usedinternally = {
    b'amend_source',
    b'branch',
    b'close',
    b'histedit_source',
    b'topic',
    b'rebase_source',
    b'intermediate-source',
    b'__touch-noise__',
    b'source',
    b'transplant_source',
}


def extsetup(ui):
    entry = extensions.wrapcommand(commands.table, b'commit', _commit)
    options = entry[1]
    options.append(
        (
            b'',
            b'extra',
            [],
            _(b'set a changeset\'s extra values'),
            _(b"KEY=VALUE"),
        )
    )


def _commit(orig, ui, repo, *pats, **opts):
    if hasattr(repo, 'unfiltered'):
        repo = repo.unfiltered()

    class repoextra(repo.__class__):
        def commit(self, *innerpats, **inneropts):
            extras = opts.get('extra')
            for raw in extras:
                if b'=' not in raw:
                    msg = _(
                        b"unable to parse '%s', should follow "
                        b"KEY=VALUE format"
                    )
                    raise error.InputError(msg % raw)
                k, v = raw.split(b'=', 1)
                if not k:
                    msg = _(b"unable to parse '%s', keys can't be empty")
                    raise error.InputError(msg % raw)
                if re.search(br'[^\w-]', k):
                    msg = _(
                        b"keys can only contain ascii letters, digits,"
                        b" '_' and '-'"
                    )
                    raise error.InputError(msg)
                if k in usedinternally:
                    msg = _(
                        b"key '%s' is used internally, can't be set "
                        b"manually"
                    )
                    raise error.InputError(msg % k)
                inneropts['extra'][k] = v
            return super(repoextra, self).commit(*innerpats, **inneropts)

    repo.__class__ = repoextra
    return orig(ui, repo, *pats, **opts)