22
22
23
23
from typing import Optional , TYPE_CHECKING , Union , cast , overload
24
24
25
- from git .types import AnyGitObject , Literal , Old_commit_ish , PathLike
25
+ from git .types import AnyGitObject , Literal , PathLike
26
26
27
27
if TYPE_CHECKING :
28
28
from git .db import GitCmdObjectDB
29
- from git .objects import Commit , TagObject , Blob , Tree
29
+ from git .objects import Commit , TagObject
30
30
from git .refs .reference import Reference
31
31
from git .refs .tag import Tag
32
32
from .base import Repo
@@ -227,12 +227,18 @@ def to_commit(obj: Object) -> "Commit":
227
227
return obj
228
228
229
229
230
- def rev_parse (repo : "Repo" , rev : str ) -> Union ["Commit" , "Tag" , "Tree" , "Blob" ]:
231
- """
230
+ def rev_parse (repo : "Repo" , rev : str ) -> AnyGitObject :
231
+ """Parse a revision string. Like ``git rev-parse``.
232
+
232
233
:return:
233
- `~git.objects.base.Object` at the given revision, either
234
- `~git.objects.commit.Commit`, `~git.refs.tag.Tag`, `~git.objects.tree.Tree` or
235
- `~git.objects.blob.Blob`.
234
+ `~git.objects.base.Object` at the given revision.
235
+
236
+ This may be any type of git object:
237
+
238
+ * :class:`Commit <git.objects.commit.Commit>`
239
+ * :class:`TagObject <git.objects.tag.TagObject>`
240
+ * :class:`Tree <git.objects.tree.Tree>`
241
+ * :class:`Blob <git.objects.blob.Blob>`
236
242
237
243
:param rev:
238
244
``git rev-parse``-compatible revision specification as string. Please see
@@ -253,7 +259,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
253
259
raise NotImplementedError ("commit by message search (regex)" )
254
260
# END handle search
255
261
256
- obj : Union [ Old_commit_ish , "Reference" , None ] = None
262
+ obj : Optional [ AnyGitObject ] = None
257
263
ref = None
258
264
output_type = "commit"
259
265
start = 0
@@ -275,12 +281,10 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
275
281
if token == "@" :
276
282
ref = cast ("Reference" , name_to_object (repo , rev [:start ], return_ref = True ))
277
283
else :
278
- obj = cast ( Old_commit_ish , name_to_object (repo , rev [:start ]) )
284
+ obj = name_to_object (repo , rev [:start ])
279
285
# END handle token
280
286
# END handle refname
281
287
else :
282
- assert obj is not None
283
-
284
288
if ref is not None :
285
289
obj = cast ("Commit" , ref .commit )
286
290
# END handle ref
@@ -300,7 +304,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
300
304
pass # Default.
301
305
elif output_type == "tree" :
302
306
try :
303
- obj = cast (Old_commit_ish , obj )
307
+ obj = cast (AnyGitObject , obj )
304
308
obj = to_commit (obj ).tree
305
309
except (AttributeError , ValueError ):
306
310
pass # Error raised later.
@@ -373,7 +377,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
373
377
parsed_to = start
374
378
# Handle hierarchy walk.
375
379
try :
376
- obj = cast (Old_commit_ish , obj )
380
+ obj = cast (AnyGitObject , obj )
377
381
if token == "~" :
378
382
obj = to_commit (obj )
379
383
for _ in range (num ):
@@ -402,7 +406,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
402
406
403
407
# Still no obj? It's probably a simple name.
404
408
if obj is None :
405
- obj = cast ( Old_commit_ish , name_to_object (repo , rev ) )
409
+ obj = name_to_object (repo , rev )
406
410
parsed_to = lr
407
411
# END handle simple name
408
412
0 commit comments