| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2f5e258 commit 65863a2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | # This module is part of GitPython and is released under the | |
| 4 | 4 | # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ | |
| 5 | 5 | ||
| 6 | + import enum | ||
| 6 | 7 | import re | |
| 7 | 8 | ||
| 8 | 9 | from git.cmd import handle_process_output | |
@@ -22,13 +23,12 @@ | |||
| 22 | 23 | Match, | |
| 23 | 24 | Optional, | |
| 24 | 25 | Tuple, | |
| 25 | - Type, | ||
| 26 | 26 | TypeVar, | |
| 27 | 27 | Union, | |
| 28 | 28 | TYPE_CHECKING, | |
| 29 | 29 | cast, | |
| 30 | 30 | ) | |
| 31 | - from git.types import Literal, PathLike, final | ||
| 31 | + from git.types import Literal, PathLike | ||
| 32 | 32 | ||
| 33 | 33 | if TYPE_CHECKING: | |
| 34 | 34 | from .objects.tree import Tree | |
@@ -48,10 +48,55 @@ | |||
| 48 | 48 | # ------------------------------------------------------------------------ | |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | - __all__ = ("Diffable", "DiffIndex", "Diff", "NULL_TREE") | ||
| 51 | + __all__ = ("DiffConstants", "NULL_TREE", "INDEX", "Diffable", "DiffIndex", "Diff") | ||
| 52 | 52 | ||
| 53 | - NULL_TREE = object() | ||
| 54 | - """Special object to compare against the empty tree in diffs.""" | ||
| 53 | + | ||
| 54 | + @enum.unique | ||
| 55 | + class DiffConstants(enum.Enum): | ||
| 56 | + """Special objects for :meth:`Diffable.diff`. | ||
| 57 | + | ||
| 58 | + See the :meth:`Diffable.diff` method's ``other`` parameter, which accepts various | ||
| 59 | + values including these. | ||
| 60 | + | ||
| 61 | + :note: | ||
| 62 | + These constants are also available as attributes of the :mod:`git.diff` module, | ||
| 63 | + the :class:`Diffable` class and its subclasses and instances, and the top-level | ||
| 64 | + :mod:`git` module. | ||
| 65 | + """ | ||
| 66 | + | ||
| 67 | + NULL_TREE = enum.auto() | ||
| 68 | + """Stand-in indicating you want to compare against the empty tree in diffs. | ||
| 69 | + | ||
| 70 | + Also accessible as :const:`git.NULL_TREE`, :const:`git.diff.NULL_TREE`, and | ||
| 71 | + :const:`Diffable.NULL_TREE`. | ||
| 72 | + """ | ||
| 73 | + | ||
| 74 | + INDEX = enum.auto() | ||
| 75 | + """Stand-in indicating you want to diff against the index. | ||
| 76 | + | ||
| 77 | + Also accessible as :const:`git.INDEX`, :const:`git.diff.INDEX`, and | ||
| 78 | + :const:`Diffable.INDEX`, as well as :const:`Diffable.Index`. The latter has been | ||
| 79 | + kept for backward compatibility and made an alias of this, so it may still be used. | ||
| 80 | + """ | ||
| 81 | + | ||
| 82 | + | ||
| 83 | + NULL_TREE: Literal[DiffConstants.NULL_TREE] = DiffConstants.NULL_TREE | ||
| 84 | + """Stand-in indicating you want to compare against the empty tree in diffs. | ||
| 85 | + | ||
| 86 | + See :meth:`Diffable.diff`, which accepts this as a value of its ``other`` parameter. | ||
| 87 | + | ||
| 88 | + This is an alias of :const:`DiffConstants.NULL_TREE`, which may also be accessed as | ||
| 89 | + :const:`git.NULL_TREE` and :const:`Diffable.NULL_TREE`. | ||
| 90 | + """ | ||
| 91 | + | ||
| 92 | + INDEX: Literal[DiffConstants.INDEX] = DiffConstants.INDEX | ||
| 93 | + """Stand-in indicating you want to diff against the index. | ||
| 94 | + | ||
| 95 | + See :meth:`Diffable.diff`, which accepts this as a value of its ``other`` parameter. | ||
| 96 | + | ||
| 97 | + This is an alias of :const:`DiffConstants.INDEX`, which may also be accessed as | ||
| 98 | + :const:`git.INDEX` and :const:`Diffable.INDEX`, as well as :const:`Diffable.Index`. | ||
| 99 | + """ | ||
| 55 | 100 | ||
| 56 | 101 | _octal_byte_re = re.compile(rb"\\([0-9]{3})") | |
| 57 | 102 | ||
@@ -84,7 +129,7 @@ class Diffable: | |||
| 84 | 129 | compatible type. | |
| 85 | 130 | ||
| 86 | 131 | :note: | |
| 87 | - Subclasses require a repo member, as it is the case for | ||
| 132 | + Subclasses require a :attr:`repo` member, as it is the case for | ||
| 88 | 133 | :class:`~git.objects.base.Object` instances. For practical reasons we do not | |
| 89 | 134 | derive from :class:`~git.objects.base.Object`. | |
| 90 | 135 | """ | |
@@ -94,9 +139,25 @@ class Diffable: | |||
| 94 | 139 | repo: "Repo" | |
| 95 | 140 | """Repository to operate on. Must be provided by subclass or sibling class.""" | |
| 96 | 141 | ||
| 97 | - @final | ||
| 98 | - class Index: | ||
| 99 | - """Stand-in indicating you want to diff against the index.""" | ||
| 142 | + NULL_TREE = NULL_TREE | ||
| 143 | + """Stand-in indicating you want to compare against the empty tree in diffs. | ||
| 144 | + | ||
| 145 | + See the :meth:`diff` method, which accepts this as a value of its ``other`` | ||
| 146 | + parameter. | ||
| 147 | + | ||
| 148 | + This is the same as :const:`DiffConstants.NULL_TREE`, and may also be accessed as | ||
| 149 | + :const:`git.NULL_TREE` and :const:`git.diff.NULL_TREE`. | ||
| 150 | + """ | ||
| 151 | + | ||
| 152 | + INDEX = Index = INDEX | ||
| 153 | + """Stand-in indicating you want to diff against the index. | ||
| 154 | + | ||
| 155 | + See the :meth:`diff` method, which accepts this as a value of its ``other`` | ||
| 156 | + parameter. | ||
| 157 | + | ||
| 158 | + This is the same as :const:`DiffConstants.INDEX`, and may also be accessed as | ||
| 159 | + :const:`git.INDEX` and :const:`git.diff.INDEX`. | ||
| 160 | + """ | ||
| 100 | 161 | ||
| 101 | 162 | def _process_diff_args( | |
| 102 | 163 | self, | |
@@ -112,7 +173,7 @@ def _process_diff_args( | |||
| 112 | 173 | ||
| 113 | 174 | def diff( | |
| 114 | 175 | self, | |
| 115 | - other: Union[Type["Index"], "Tree", "Commit", str, None] = Index, | ||
| 176 | + other: Union[Literal[DiffConstants.INDEX], "Tree", "Commit", str, None] = INDEX, | ||
| 116 | 177 | paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, | |
| 117 | 178 | create_patch: bool = False, | |
| 118 | 179 | **kwargs: Any, | |
@@ -125,20 +186,15 @@ def diff( | |||
| 125 | 186 | ||
| 126 | 187 | * If ``None``, we will be compared to the working tree. | |
| 127 | 188 | ||
| 128 | - * If :class:`~git.types.Tree_ish`, it will be compared against the | ||
| 129 | - respective tree. (See https://git-scm.com/docs/gitglossary#def_tree-ish.) | ||
| 130 | - This can also be passed as a string. | ||
| 189 | + * If a :class:`~git.types.Tree_ish` or string, it will be compared against | ||
| 190 | + the respective tree. | ||
| 131 | 191 | ||
| 132 | - * If :class:`Diffable.Index`, it will be compared against the index. Use the | ||
| 133 | - type object :class:`Index` itself, without attempting to instantiate it. | ||
| 134 | - (That is, you should treat :class:`Index` as an opqaue constant. Don't | ||
| 135 | - rely on it being a class or even callable.) | ||
| 192 | + * If :const:`INDEX`, it will be compared against the index. | ||
| 136 | 193 | ||
| 137 | - * If :attr:`git.NULL_TREE <NULL_TREE>`, it will compare against the empty | ||
| 138 | - tree. | ||
| 194 | + * If :const:`NULL_TREE`, it will compare against the empty tree. | ||
| 139 | 195 | ||
| 140 | - This parameter defaults to :class:`Diffable.Index` (rather than ``None``) so | ||
| 141 | - that the method will not by default fail on bare repositories. | ||
| 196 | + This parameter defaults to :const:`INDEX` (rather than ``None``) so that the | ||
| 197 | + method will not by default fail on bare repositories. | ||
| 142 | 198 | ||
| 143 | 199 | :param paths: | |
| 144 | 200 | This a list of paths or a single path to limit the diff to. It will only | |
@@ -185,7 +241,7 @@ def diff( | |||
| 185 | 241 | paths = [paths] | |
| 186 | 242 | ||
| 187 | 243 | diff_cmd = self.repo.git.diff | |
| 188 | - if other is Diffable.Index: | ||
| 244 | + if other is INDEX: | ||
| 189 | 245 | args.insert(0, "--cached") | |
| 190 | 246 | elif other is NULL_TREE: | |
| 191 | 247 | args.insert(0, "-r") # Recursive diff-tree. | |
@@ -218,7 +274,7 @@ def diff( | |||
| 218 | 274 | ||
| 219 | 275 | ||
| 220 | 276 | class DiffIndex(List[T_Diff]): | |
| 221 | - R"""An Index for diffs, allowing a list of :class:`Diff`\s to be queried by the diff | ||
| 277 | + R"""An index for diffs, allowing a list of :class:`Diff`\s to be queried by the diff | ||
| 222 | 278 | properties. | |
| 223 | 279 | ||
| 224 | 280 | The class improves the diff handling convenience. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,11 +76,10 @@ | |||
| 76 | 76 | Sequence, | |
| 77 | 77 | TYPE_CHECKING, | |
| 78 | 78 | Tuple, | |
| 79 | - Type, | ||
| 80 | 79 | Union, | |
| 81 | 80 | ) | |
| 82 | 81 | ||
| 83 | - from git.types import Commit_ish, PathLike | ||
| 82 | + from git.types import Commit_ish, Literal, PathLike | ||
| 84 | 83 | ||
| 85 | 84 | if TYPE_CHECKING: | |
| 86 | 85 | from subprocess import Popen | |
@@ -1479,7 +1478,7 @@ def reset( | |||
| 1479 | 1478 | # @ default_index, breaks typing for some reason, copied into function | |
| 1480 | 1479 | def diff( | |
| 1481 | 1480 | self, | |
| 1482 | - other: Union[Type["git_diff.Diffable.Index"], "Tree", "Commit", str, None] = git_diff.Diffable.Index, | ||
| 1481 | + other: Union[Literal[git_diff.DiffConstants.INDEX], "Tree", "Commit", str, None] = git_diff.INDEX, | ||
| 1483 | 1482 | paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, | |
| 1484 | 1483 | create_patch: bool = False, | |
| 1485 | 1484 | **kwargs: Any, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,6 @@ | |||
| 22 | 22 | TypedDict, | |
| 23 | 23 | Protocol, | |
| 24 | 24 | SupportsIndex as SupportsIndex, | |
| 25 | - final, | ||
| 26 | 25 | runtime_checkable, | |
| 27 | 26 | ) | |
| 28 | 27 | else: | |
@@ -31,7 +30,6 @@ | |||
| 31 | 30 | SupportsIndex as SupportsIndex, | |
| 32 | 31 | TypedDict, | |
| 33 | 32 | Protocol, | |
| 34 | - final, | ||
| 35 | 33 | runtime_checkable, | |
| 36 | 34 | ) | |
| 37 | 35 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments