hgext/git/gitutil.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 49300 227124098e14
child 49849 de9ffb82ef4d
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:
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
"""utilities to assist in working with pygit2"""
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
49300
227124098e14 py3: use `x.hex()` instead of `pycompat.sysstr(node.hex(x))`
Manuel Jacob <me@manueljacob.de>
parents: 49290
diff changeset
     3
from mercurial.node import bin, sha1nodeconstants
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
44484
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     5
pygit2_module = None
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     6
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     7
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     8
def get_pygit2():
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     9
    global pygit2_module
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    10
    if pygit2_module is None:
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    11
        try:
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    12
            import pygit2 as pygit2_module
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    13
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    14
            pygit2_module.InvalidSpecError
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    15
        except (ImportError, AttributeError):
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    16
            pass
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    17
    return pygit2_module
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    18
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
45950
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    20
def pygit2_version():
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    21
    mod = get_pygit2()
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    22
    v = "N/A"
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    23
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    24
    if mod:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    25
        try:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    26
            v = mod.__version__
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    27
        except AttributeError:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    28
            pass
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    29
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    30
    return b"(pygit2 %s)" % v.encode("utf-8")
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    31
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    32
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
def togitnode(n):
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
    """Wrapper to convert a Mercurial binary node to a unicode hexlified node.
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
    pygit2 and sqlite both need nodes as strings, not bytes.
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    """
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
    assert len(n) == 20
49300
227124098e14 py3: use `x.hex()` instead of `pycompat.sysstr(node.hex(x))`
Manuel Jacob <me@manueljacob.de>
parents: 49290
diff changeset
    39
    return n.hex()
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
def fromgitnode(n):
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
    """Opposite of togitnode."""
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
    assert len(n) == 40
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
    return bin(n)
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45950
diff changeset
    48
nullgit = togitnode(sha1nodeconstants.nullid)