hgext/narrow/narrowtemplates.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48875 6000f5b25c9b
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:
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# narrowtemplates.py - added template keywords for narrow clones
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
# Copyright 2017 Google, Inc.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
#
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
from mercurial import (
36091
ea02be8665ef narrowtemplates: update to use registrar mechanism
Augie Fackler <augie@google.com>
parents: 36090
diff changeset
    10
    registrar,
36090
9445a3141501 narrow: move from ELLIPSIS_NODE_FLAG to revlog.REVIDX_ELLIPSIS
Augie Fackler <augie@google.com>
parents: 36079
diff changeset
    11
    revlog,
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
)
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
36091
ea02be8665ef narrowtemplates: update to use registrar mechanism
Augie Fackler <augie@google.com>
parents: 36090
diff changeset
    14
keywords = {}
ea02be8665ef narrowtemplates: update to use registrar mechanism
Augie Fackler <augie@google.com>
parents: 36090
diff changeset
    15
templatekeyword = registrar.templatekeyword(keywords)
ea02be8665ef narrowtemplates: update to use registrar mechanism
Augie Fackler <augie@google.com>
parents: 36090
diff changeset
    16
revsetpredicate = registrar.revsetpredicate()
ea02be8665ef narrowtemplates: update to use registrar mechanism
Augie Fackler <augie@google.com>
parents: 36090
diff changeset
    17
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42358
diff changeset
    18
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
def _isellipsis(repo, rev):
36090
9445a3141501 narrow: move from ELLIPSIS_NODE_FLAG to revlog.REVIDX_ELLIPSIS
Augie Fackler <augie@google.com>
parents: 36079
diff changeset
    20
    if repo.changelog.flags(rev) & revlog.REVIDX_ELLIPSIS:
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
        return True
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
    return False
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42358
diff changeset
    24
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    25
@templatekeyword(b'ellipsis', requires={b'repo', b'ctx'})
36514
7b74afec6772 templatekw: switch non-showlist template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36472
diff changeset
    26
def ellipsis(context, mapping):
36439
02cd2fb6de72 narrow: drop redundant templatekw/revset names from help text
Yuya Nishihara <yuya@tcha.org>
parents: 36091
diff changeset
    27
    """String. 'ellipsis' if the change is an ellipsis node, else ''."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    28
    repo = context.resource(mapping, b'repo')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
    ctx = context.resource(mapping, b'ctx')
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
    if _isellipsis(repo, ctx.rev()):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    31
        return b'ellipsis'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
    return b''
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42358
diff changeset
    34
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
@templatekeyword(b'outsidenarrow', requires={b'repo', b'ctx'})
36514
7b74afec6772 templatekw: switch non-showlist template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 36472
diff changeset
    36
def outsidenarrow(context, mapping):
36439
02cd2fb6de72 narrow: drop redundant templatekw/revset names from help text
Yuya Nishihara <yuya@tcha.org>
parents: 36091
diff changeset
    37
    """String. 'outsidenarrow' if the change affects no tracked files,
02cd2fb6de72 narrow: drop redundant templatekw/revset names from help text
Yuya Nishihara <yuya@tcha.org>
parents: 36091
diff changeset
    38
    else ''."""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    39
    repo = context.resource(mapping, b'repo')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
    ctx = context.resource(mapping, b'ctx')
36472
d0d5eef57fb0 narrow: drop safehasattr() checks for always-present repo.narrowmatch
Martin von Zweigbergk <martinvonz@google.com>
parents: 36439
diff changeset
    41
    m = repo.narrowmatch()
42358
45c18f7345c1 narrow: consider empty commits to be "inside the narrow spec" for templates
Danny Hooper <hooper@google.com>
parents: 38964
diff changeset
    42
    if ctx.files() and not m.always():
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
        if not any(m(f) for f in ctx.files()):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    44
            return b'outsidenarrow'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    45
    return b''
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42358
diff changeset
    47
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
@revsetpredicate(b'ellipsis()')
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
def ellipsisrevset(repo, subset, x):
36439
02cd2fb6de72 narrow: drop redundant templatekw/revset names from help text
Yuya Nishihara <yuya@tcha.org>
parents: 36091
diff changeset
    50
    """Changesets that are ellipsis nodes."""
36079
a2a6e724d61a narrow: import experimental extension from narrowhg revision cb51d673e9c5
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
    return subset.filter(lambda r: _isellipsis(repo, r))