blob.py 986 B

123456789101112131415161718192021222324252627282930313233343536
  1. # blob.py
  2. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  3. #
  4. # This module is part of GitPython and is released under
  5. # the BSD License: http://www.opensource.org/licenses/bsd-license.php
  6. from mimetypes import guess_type
  7. from . import base
  8. from git.types import Literal
  9. __all__ = ("Blob",)
  10. class Blob(base.IndexObject):
  11. """A Blob encapsulates a git blob object"""
  12. DEFAULT_MIME_TYPE = "text/plain"
  13. type: Literal["blob"] = "blob"
  14. # valid blob modes
  15. executable_mode = 0o100755
  16. file_mode = 0o100644
  17. link_mode = 0o120000
  18. __slots__ = ()
  19. @property
  20. def mime_type(self) -> str:
  21. """
  22. :return: String describing the mime type of this file (based on the filename)
  23. :note: Defaults to 'text/plain' in case the actual file type is unknown."""
  24. guesses = None
  25. if self.path:
  26. guesses = guess_type(str(self.path))
  27. return guesses and guesses[0] or self.DEFAULT_MIME_TYPE