blob.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  2. #
  3. # This module is part of GitPython and is released under the
  4. # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
  5. __all__ = ["Blob"]
  6. from mimetypes import guess_type
  7. import os
  8. import sys
  9. if sys.version_info >= (3, 8):
  10. from typing import Literal
  11. else:
  12. from typing_extensions import Literal
  13. from . import base
  14. class Blob(base.IndexObject):
  15. """A Blob encapsulates a git blob object.
  16. See :manpage:`gitglossary(7)` on "blob":
  17. https://git-scm.com/docs/gitglossary#def_blob_object
  18. """
  19. DEFAULT_MIME_TYPE = "text/plain"
  20. type: Literal["blob"] = "blob"
  21. # Valid blob modes
  22. executable_mode = 0o100755
  23. file_mode = 0o100644
  24. link_mode = 0o120000
  25. __slots__ = ()
  26. @property
  27. def mime_type(self) -> str:
  28. """
  29. :return:
  30. String describing the mime type of this file (based on the filename)
  31. :note:
  32. Defaults to ``text/plain`` in case the actual file type is unknown.
  33. """
  34. guesses = None
  35. if self.path:
  36. guesses = guess_type(os.fspath(self.path))
  37. return guesses and guesses[0] or self.DEFAULT_MIME_TYPE