Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/git/db.py: 78%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This module is part of GitPython and is released under the
2# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
4"""Module with our own gitdb implementation - it uses the git command."""
6__all__ = ["GitCmdObjectDB", "GitDB"]
8from gitdb.base import OInfo, OStream
9from gitdb.db import GitDB, LooseObjectDB
10from gitdb.exc import BadObject
12from git.util import bin_to_hex, hex_to_bin
13from git.exc import GitCommandError
15# typing-------------------------------------------------
17from typing import TYPE_CHECKING
19from git.types import PathLike
21if TYPE_CHECKING:
22 from git.cmd import Git
24# --------------------------------------------------------
27class GitCmdObjectDB(LooseObjectDB):
28 """A database representing the default git object store, which includes loose
29 objects, pack files and an alternates file.
31 It will create objects only in the loose object database.
32 """
34 def __init__(self, root_path: PathLike, git: "Git") -> None:
35 """Initialize this instance with the root and a git command."""
36 super().__init__(root_path)
37 self._git = git
39 def info(self, binsha: bytes) -> OInfo:
40 """Get a git object header (using git itself)."""
41 hexsha, typename, size = self._git.get_object_header(bin_to_hex(binsha))
42 return OInfo(hex_to_bin(hexsha), typename, size)
44 def stream(self, binsha: bytes) -> OStream:
45 """Get git object data as a stream supporting ``read()`` (using git itself)."""
46 hexsha, typename, size, stream = self._git.stream_object_data(bin_to_hex(binsha))
47 return OStream(hex_to_bin(hexsha), typename, size, stream)
49 # { Interface
51 def partial_to_complete_sha_hex(self, partial_hexsha: str) -> bytes:
52 """
53 :return:
54 Full binary 20 byte sha from the given partial hexsha
56 :raise gitdb.exc.AmbiguousObjectName:
58 :raise gitdb.exc.BadObject:
60 :note:
61 Currently we only raise :exc:`~gitdb.exc.BadObject` as git does not
62 communicate ambiguous objects separately.
63 """
64 try:
65 hexsha, _typename, _size = self._git.get_object_header(partial_hexsha)
66 return hex_to_bin(hexsha)
67 except (GitCommandError, ValueError) as e:
68 raise BadObject(partial_hexsha) from e
69 # END handle exceptions
71 # } END interface