hgext/git/gitutil.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Thu, 17 Feb 2022 07:34:49 +0100
changeset 48793 6e559391f96e
parent 47012 d55b71393907
child 48875 6000f5b25c9b
permissions -rw-r--r--
tracked-key: remove the dual write and rename to tracked-hint The dual-write approach was mostly useless. As explained in the previous version of the help, the key had to be read twice before we could cache a value. However this "read twice" limitation actually also apply to any usage of the key. If some operation wants to rely of the "same value == same tracked set" property it would need to read the value before, and after running that operation (or at least, after, in all cases). So it cannot be sure the operation it did is "valid" until checking the key after the operation. As a resultat such operation can only be read-only or rollbackable. This reduce the utility of the "same value == same tracked set" a lot. So it seems simpler to drop the double write and to update the documentation to highlight that this file does not garantee race-free operation. As a result the "key" is demoted to a "hint". Documentation is updated accordingly. Differential Revision: https://phab.mercurial-scm.org/D12201
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
from __future__ import absolute_import
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45950
diff changeset
     4
from mercurial.node import bin, hex, sha1nodeconstants
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
from mercurial import pycompat
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
44484
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     8
pygit2_module = None
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
     9
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    10
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    11
def get_pygit2():
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    12
    global pygit2_module
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    13
    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
    14
        try:
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    15
            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
    16
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    17
            pygit2_module.InvalidSpecError
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    18
        except (ImportError, AttributeError):
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    19
            pass
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    20
    return pygit2_module
ec54b3d2af0b git: don't fail import when pygit2 is not install
Martin von Zweigbergk <martinvonz@google.com>
parents: 44477
diff changeset
    21
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
45950
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    23
def pygit2_version():
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    24
    mod = get_pygit2()
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    25
    v = "N/A"
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    26
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    27
    if mod:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    28
        try:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    29
            v = mod.__version__
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    30
        except AttributeError:
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    31
            pass
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    32
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    33
    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
    34
c7c1efdfd4de git: show the version of `pygit2` with verbose version output
Matt Harbison <matt_harbison@yahoo.com>
parents: 44484
diff changeset
    35
44477
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
def togitnode(n):
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
    """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
    38
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
    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
    40
    """
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
    assert len(n) == 20
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
    return pycompat.sysstr(hex(n))
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
def fromgitnode(n):
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
    """Opposite of togitnode."""
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    47
    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
    48
    if pycompat.ispy3:
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    49
        return bin(n.encode('ascii'))
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    50
    return bin(n)
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    51
ad718271a9eb git: skeleton of a new extension to _directly_ operate on git repos
Augie Fackler <augie@google.com>
parents:
diff changeset
    52
47012
d55b71393907 node: replace nullid and friends with nodeconstants class
Joerg Sonnenberger <joerg@bec.de>
parents: 45950
diff changeset
    53
nullgit = togitnode(sha1nodeconstants.nullid)