66from __future__ import annotations
77
88import os
9- import stat
9+ import os . path as osp
1010from pathlib import Path
11+ import stat
1112from string import digits
1213
14+ from git .cmd import Git
1315from git .exc import WorkTreeRepositoryUnsupported
1416from git .objects import Object
1517from git .refs import SymbolicReference
1618from 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
2420
2521# Typing ----------------------------------------------------------------------
2622
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
2926
3027if TYPE_CHECKING :
31- from git .types import PathLike
32- from .base import Repo
3328 from git .db import GitCmdObjectDB
34- from git .refs .reference import Reference
3529 from git .objects import Commit , TagObject , Blob , Tree
30+ from git .refs .reference import Reference
3631 from git .refs .tag import Tag
32+ from .base import Repo
3733
3834# ----------------------------------------------------------------------------
3935
@@ -56,7 +52,7 @@ def touch(filename: str) -> str:
5652 return filename
5753
5854
59- def is_git_dir (d : " PathLike" ) -> bool :
55+ def is_git_dir (d : PathLike ) -> bool :
6056 """This is taken from the git setup.c:is_git_directory function.
6157
6258 :raise git.exc.WorkTreeRepositoryUnsupported:
@@ -79,7 +75,7 @@ def is_git_dir(d: "PathLike") -> bool:
7975 return False
8076
8177
82- def find_worktree_git_dir (dotgit : " PathLike" ) -> Optional [str ]:
78+ def find_worktree_git_dir (dotgit : PathLike ) -> Optional [str ]:
8379 """Search for a gitdir for this worktree."""
8480 try :
8581 statbuf = os .stat (dotgit )
@@ -98,7 +94,7 @@ def find_worktree_git_dir(dotgit: "PathLike") -> Optional[str]:
9894 return None
9995
10096
101- def find_submodule_git_dir (d : " PathLike" ) -> Optional [" PathLike" ]:
97+ def find_submodule_git_dir (d : PathLike ) -> Optional [PathLike ]:
10298 """Search for a submodule repo."""
10399 if is_git_dir (d ):
104100 return d
@@ -141,18 +137,26 @@ def short_to_long(odb: "GitCmdObjectDB", hexsha: str) -> Optional[bytes]:
141137 # END exception handling
142138
143139
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 ]:
147151 """
148152 :return:
149153 Object specified by the given name - hexshas (short and long) as well as
150154 references are supported.
151155
152156 :param return_ref:
153157 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`.
156160 """
157161 hexsha : Union [None , str , bytes ] = None
158162
@@ -201,7 +205,7 @@ def name_to_object(
201205 return Object .new_from_sha (repo , hex_to_bin (hexsha ))
202206
203207
204- def deref_tag (tag : "Tag" ) -> "TagObject" :
208+ def deref_tag (tag : "Tag" ) -> AnyGitObject :
205209 """Recursively dereference a tag and return the resulting object."""
206210 while True :
207211 try :
@@ -212,7 +216,7 @@ def deref_tag(tag: "Tag") -> "TagObject":
212216 return tag
213217
214218
215- def to_commit (obj : Object ) -> Union [ "Commit" , "TagObject" ] :
219+ def to_commit (obj : Object ) -> "Commit" :
216220 """Convert the given object to a commit if possible and return it."""
217221 if obj .type == "tag" :
218222 obj = deref_tag (obj )
0 commit comments