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
6__all__ = ["Blob"]
7
8from mimetypes import guess_type
9import sys
10
11if sys.version_info >= (3, 8):
12 from typing import Literal
13else:
14 from typing_extensions import Literal
15
16from . import base
17
18
19class Blob(base.IndexObject):
20 """A Blob encapsulates a git blob object.
21
22 See :manpage:`gitglossary(7)` on "blob":
23 https://git-scm.com/docs/gitglossary#def_blob_object
24 """
25
26 DEFAULT_MIME_TYPE = "text/plain"
27 type: Literal["blob"] = "blob"
28
29 # Valid blob modes
30 executable_mode = 0o100755
31 file_mode = 0o100644
32 link_mode = 0o120000
33
34 __slots__ = ()
35
36 @property
37 def mime_type(self) -> str:
38 """
39 :return:
40 String describing the mime type of this file (based on the filename)
41
42 :note:
43 Defaults to ``text/plain`` in case the actual file type is unknown.
44 """
45 guesses = None
46 if self.path:
47 guesses = guess_type(str(self.path))
48 return guesses and guesses[0] or self.DEFAULT_MIME_TYPE