hgext/commitextras.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
child 50928 d718eddf01d9
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
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
33602
27fbca750b4d commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33562
diff changeset
    11
import re
27fbca750b4d commitextras: make sure keys contains ascii letters, numbers, '_' and '-' only
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33562
diff changeset
    12
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    13
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
    14
from mercurial import (
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    15
    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
    16
    error,
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    17
    extensions,
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    18
    registrar,
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    19
    util,
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    20
)
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
cmdtable = {}
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
command = registrar.command(cmdtable)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    24
testedwith = b'ships-with-hg-core'
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
33547
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    26
usedinternally = {
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    27
    b'amend_source',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
    b'branch',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
    b'close',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    30
    b'histedit_source',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    31
    b'topic',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
    b'rebase_source',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
    b'intermediate-source',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
    b'__touch-noise__',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
    b'source',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
    b'transplant_source',
33547
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    37
}
a6af8560494e commitextras: check the format of the arguments and no internal key is used
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33546
diff changeset
    38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    39
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
def extsetup(ui):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    41
    entry = extensions.wrapcommand(commands.table, b'commit', _commit)
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
    options = entry[1]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    43
    options.append(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
        (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
            b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
            b'extra',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    47
            [],
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
            _(b'set a changeset\'s extra values'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    49
            _(b"KEY=VALUE"),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
        )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    51
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    52
33546
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    53
77c0c36654c8 commitextras: move fb extension to core which add extras to a commit
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    54
def _commit(orig, ui, repo, *pats, **opts):
43115
4aa72cdf616f py3: delete b'' prefix from safehasattr arguments
Martin von Zweigbergk <martinvonz@google.com>
parents: 43077
diff changeset
    55
    if util.safehasattr(repo, 'unfiltered'):
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    56
        repo = repo.unfiltered()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    57
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    58
    class repoextra(repo.__class__):
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    59
        def commit(self, *innerpats, **inneropts):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43115
diff changeset
    60
            extras = opts.get('extra')
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    61
            for raw in extras:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    62
                if b'=' not in raw:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    63
                    msg = _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    64
                        b"unable to parse '%s', should follow "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    65
                        b"KEY=VALUE format"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    66
                    )
48370
45a073af50a2 errors: use detailed error for invalid commit-extras argument
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    67
                    raise error.InputError(msg % raw)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    68
                k, v = raw.split(b'=', 1)
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    69
                if not k:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    70
                    msg = _(b"unable to parse '%s', keys can't be empty")
48370
45a073af50a2 errors: use detailed error for invalid commit-extras argument
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    71
                    raise error.InputError(msg % raw)
41532
bd3f03d8cc9f global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39295
diff changeset
    72
                if re.search(br'[^\w-]', k):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    73
                    msg = _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    74
                        b"keys can only contain ascii letters, digits,"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
                        b" '_' and '-'"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    76
                    )
48370
45a073af50a2 errors: use detailed error for invalid commit-extras argument
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    77
                    raise error.InputError(msg)
39295
3a60416c4fd8 commitextras: no need to special case extras=[]
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 39294
diff changeset
    78
                if k in usedinternally:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    79
                    msg = _(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    80
                        b"key '%s' is used internally, can't be set "
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    81
                        b"manually"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    82
                    )
48370
45a073af50a2 errors: use detailed error for invalid commit-extras argument
Martin von Zweigbergk <martinvonz@google.com>
parents: 43506
diff changeset
    83
                    raise error.InputError(msg % k)
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43115
diff changeset
    84
                inneropts['extra'][k] = v
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    85
            return super(repoextra, self).commit(*innerpats, **inneropts)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41532
diff changeset
    86
39294
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    87
    repo.__class__ = repoextra
1cb7c9777852 commitextras: work nicely with other extensions
Valentin Gatien-Baron <vgatien-baron@janestreet.com>
parents: 36419
diff changeset
    88
    return orig(ui, repo, *pats, **opts)