6
6
from __future__ import annotations
7
7
8
8
import os
9
- import stat
9
+ import os . path as osp
10
10
from pathlib import Path
11
+ import stat
11
12
from string import digits
12
13
14
+ from git .cmd import Git
13
15
from git .exc import WorkTreeRepositoryUnsupported
14
16
from git .objects import Object
15
17
from git .refs import SymbolicReference
16
18
from git .util import hex_to_bin , bin_to_hex , cygpath
17
- from gitdb .exc import (
18
- BadObject ,
19
- BadName ,
20
- )
21
-
22
- import os .path as osp
23
- from git .cmd import Git
19
+ from gitdb .exc import BadName , BadObject
24
20
25
21
# Typing ----------------------------------------------------------------------
26
22
27
- from typing import Union , Optional , cast , TYPE_CHECKING
28
- from git .types import Old_commit_ish
23
+ from typing import Optional , TYPE_CHECKING , Union , cast , overload
24
+
25
+ from git .types import AnyGitObject , Literal , Old_commit_ish , PathLike
29
26
30
27
if TYPE_CHECKING :
31
- from git .types import PathLike
32
- from .base import Repo
33
28
from git .db import GitCmdObjectDB
34
- from git .refs .reference import Reference
35
29
from git .objects import Commit , TagObject , Blob , Tree
30
+ from git .refs .reference import Reference
36
31
from git .refs .tag import Tag
32
+ from .base import Repo
37
33
38
34
# ----------------------------------------------------------------------------
39
35
@@ -56,7 +52,7 @@ def touch(filename: str) -> str:
56
52
return filename
57
53
58
54
59
- def is_git_dir (d : " PathLike" ) -> bool :
55
+ def is_git_dir (d : PathLike ) -> bool :
60
56
"""This is taken from the git setup.c:is_git_directory function.
61
57
62
58
:raise git.exc.WorkTreeRepositoryUnsupported:
@@ -79,7 +75,7 @@ def is_git_dir(d: "PathLike") -> bool:
79
75
return False
80
76
81
77
82
- def find_worktree_git_dir (dotgit : " PathLike" ) -> Optional [str ]:
78
+ def find_worktree_git_dir (dotgit : PathLike ) -> Optional [str ]:
83
79
"""Search for a gitdir for this worktree."""
84
80
try :
85
81
statbuf = os .stat (dotgit )
@@ -98,7 +94,7 @@ def find_worktree_git_dir(dotgit: "PathLike") -> Optional[str]:
98
94
return None
99
95
100
96
101
- def find_submodule_git_dir (d : " PathLike" ) -> Optional [" PathLike" ]:
97
+ def find_submodule_git_dir (d : PathLike ) -> Optional [PathLike ]:
102
98
"""Search for a submodule repo."""
103
99
if is_git_dir (d ):
104
100
return d
@@ -141,18 +137,26 @@ def short_to_long(odb: "GitCmdObjectDB", hexsha: str) -> Optional[bytes]:
141
137
# END exception handling
142
138
143
139
144
- def name_to_object (
145
- repo : "Repo" , name : str , return_ref : bool = False
146
- ) -> Union [SymbolicReference , "Commit" , "TagObject" , "Blob" , "Tree" ]:
140
+ @overload
141
+ def name_to_object (repo : "Repo" , name : str , return_ref : Literal [False ] = ...) -> AnyGitObject :
142
+ ...
143
+
144
+
145
+ @overload
146
+ def name_to_object (repo : "Repo" , name : str , return_ref : Literal [True ]) -> Union [AnyGitObject , SymbolicReference ]:
147
+ ...
148
+
149
+
150
+ def name_to_object (repo : "Repo" , name : str , return_ref : bool = False ) -> Union [AnyGitObject , SymbolicReference ]:
147
151
"""
148
152
:return:
149
153
Object specified by the given name - hexshas (short and long) as well as
150
154
references are supported.
151
155
152
156
:param return_ref:
153
157
If ``True``, and name specifies a reference, we will return the reference
154
- instead of the object. Otherwise it will raise `~gitdb.exc.BadObject` or
155
- `~gitdb.exc.BadName`.
158
+ instead of the object. Otherwise it will raise :class: `~gitdb.exc.BadObject` or
159
+ :class: `~gitdb.exc.BadName`.
156
160
"""
157
161
hexsha : Union [None , str , bytes ] = None
158
162
@@ -201,7 +205,7 @@ def name_to_object(
201
205
return Object .new_from_sha (repo , hex_to_bin (hexsha ))
202
206
203
207
204
- def deref_tag (tag : "Tag" ) -> "TagObject" :
208
+ def deref_tag (tag : "Tag" ) -> AnyGitObject :
205
209
"""Recursively dereference a tag and return the resulting object."""
206
210
while True :
207
211
try :
@@ -212,7 +216,7 @@ def deref_tag(tag: "Tag") -> "TagObject":
212
216
return tag
213
217
214
218
215
- def to_commit (obj : Object ) -> Union [ "Commit" , "TagObject" ] :
219
+ def to_commit (obj : Object ) -> "Commit" :
216
220
"""Convert the given object to a commit if possible and return it."""
217
221
if obj .type == "tag" :
218
222
obj = deref_tag (obj )
0 commit comments