db.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """Module with our own gitdb implementation - it uses the git command"""
  2. from git.util import bin_to_hex, hex_to_bin
  3. from gitdb.base import OInfo, OStream
  4. from gitdb.db import GitDB # @UnusedImport
  5. from gitdb.db import LooseObjectDB
  6. from gitdb.exc import BadObject
  7. from git.exc import GitCommandError
  8. # typing-------------------------------------------------
  9. from typing import TYPE_CHECKING
  10. from git.types import PathLike
  11. if TYPE_CHECKING:
  12. from git.cmd import Git
  13. # --------------------------------------------------------
  14. __all__ = ("GitCmdObjectDB", "GitDB")
  15. class GitCmdObjectDB(LooseObjectDB):
  16. """A database representing the default git object store, which includes loose
  17. objects, pack files and an alternates file
  18. It will create objects only in the loose object database.
  19. :note: for now, we use the git command to do all the lookup, just until he
  20. have packs and the other implementations
  21. """
  22. def __init__(self, root_path: PathLike, git: "Git") -> None:
  23. """Initialize this instance with the root and a git command"""
  24. super(GitCmdObjectDB, self).__init__(root_path)
  25. self._git = git
  26. def info(self, binsha: bytes) -> OInfo:
  27. hexsha, typename, size = self._git.get_object_header(bin_to_hex(binsha))
  28. return OInfo(hex_to_bin(hexsha), typename, size)
  29. def stream(self, binsha: bytes) -> OStream:
  30. """For now, all lookup is done by git itself"""
  31. hexsha, typename, size, stream = self._git.stream_object_data(bin_to_hex(binsha))
  32. return OStream(hex_to_bin(hexsha), typename, size, stream)
  33. # { Interface
  34. def partial_to_complete_sha_hex(self, partial_hexsha: str) -> bytes:
  35. """:return: Full binary 20 byte sha from the given partial hexsha
  36. :raise AmbiguousObjectName:
  37. :raise BadObject:
  38. :note: currently we only raise BadObject as git does not communicate
  39. AmbiguousObjects separately"""
  40. try:
  41. hexsha, _typename, _size = self._git.get_object_header(partial_hexsha)
  42. return hex_to_bin(hexsha)
  43. except (GitCommandError, ValueError) as e:
  44. raise BadObject(partial_hexsha) from e
  45. # END handle exceptions
  46. # } END interface