| 1 |
"""utilities to assist in working with pygit2""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from mercurial.node import bin, sha1nodeconstants |
| 6 |
|
| 7 |
pygit2_module = None |
| 8 |
|
| 9 |
|
| 10 |
def get_pygit2(): |
| 11 |
global pygit2_module |
| 12 |
if pygit2_module is None: |
| 13 |
try: |
| 14 |
import pygit2 as pygit2_module # pytype: disable=import-error |
| 15 |
|
| 16 |
pygit2_module.InvalidSpecError |
| 17 |
except (ImportError, AttributeError): |
| 18 |
pass |
| 19 |
return pygit2_module |
| 20 |
|
| 21 |
|
| 22 |
def pygit2_version(): |
| 23 |
mod = get_pygit2() |
| 24 |
v = "N/A" |
| 25 |
|
| 26 |
if mod: |
| 27 |
try: |
| 28 |
v = mod.__version__ |
| 29 |
except AttributeError: |
| 30 |
pass |
| 31 |
|
| 32 |
return b"(pygit2 %s)" % v.encode("utf-8") |
| 33 |
|
| 34 |
|
| 35 |
def togitnode(n): |
| 36 |
"""Wrapper to convert a Mercurial binary node to a unicode hexlified node. |
| 37 |
|
| 38 |
pygit2 and sqlite both need nodes as strings, not bytes. |
| 39 |
""" |
| 40 |
assert len(n) == 20 |
| 41 |
return n.hex() |
| 42 |
|
| 43 |
|
| 44 |
def fromgitnode(n): |
| 45 |
"""Opposite of togitnode.""" |
| 46 |
assert len(n) == 40 |
| 47 |
return bin(n) |
| 48 |
|
| 49 |
|
| 50 |
nullgit = togitnode(sha1nodeconstants.nullid) |