1717 BadName
1818)
1919
20- import os .path as osp
21-
2220from .log import RefLog
2321
2422# typing ------------------------------------------------------------------
2523
26- from typing import Any , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING # NOQA
24+ from typing import Any , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING , cast # NOQA
2725from git .types import Commit_ish , PathLike , TBD , Literal # NOQA
2826
2927if TYPE_CHECKING :
3028 from git .repo import Repo
29+ from git .refs import Head , TagReference , Reference
3130
3231T_References = TypeVar ('T_References' , bound = 'SymbolicReference' )
3332
3736__all__ = ["SymbolicReference" ]
3837
3938
40- def _git_dir (repo , path ) :
39+ def _git_dir (repo : 'Repo' , path : PathLike ) -> PathLike :
4140 """ Find the git dir that's appropriate for the path"""
42- name = "%s" % ( path ,)
41+ name = f" { path } "
4342 if name in ['HEAD' , 'ORIG_HEAD' , 'FETCH_HEAD' , 'index' , 'logs' ]:
4443 return repo .git_dir
4544 return repo .common_dir
@@ -61,44 +60,44 @@ class SymbolicReference(object):
6160
6261 def __init__ (self , repo : 'Repo' , path : PathLike , check_path : bool = False ):
6362 self .repo = repo
64- self .path = str ( path )
63+ self .path = path
6564
6665 def __str__ (self ) -> str :
67- return self .path
66+ return str ( self .path )
6867
69- def __repr__ (self ):
68+ def __repr__ (self ) -> str :
7069 return '<git.%s "%s">' % (self .__class__ .__name__ , self .path )
7170
72- def __eq__ (self , other ) :
71+ def __eq__ (self , other : Any ) -> bool :
7372 if hasattr (other , 'path' ):
7473 return self .path == other .path
7574 return False
7675
77- def __ne__ (self , other ) :
76+ def __ne__ (self , other : Any ) -> bool :
7877 return not (self == other )
7978
80- def __hash__ (self ):
79+ def __hash__ (self ) -> int :
8180 return hash (self .path )
8281
8382 @property
84- def name (self ):
83+ def name (self ) -> str :
8584 """
8685 :return:
8786 In case of symbolic references, the shortest assumable name
8887 is the path itself."""
89- return self .path
88+ return str ( self .path )
9089
9190 @property
92- def abspath (self ):
91+ def abspath (self ) -> PathLike :
9392 return join_path_native (_git_dir (self .repo , self .path ), self .path )
9493
9594 @classmethod
96- def _get_packed_refs_path (cls , repo ) :
97- return osp .join (repo .common_dir , 'packed-refs' )
95+ def _get_packed_refs_path (cls , repo : 'Repo' ) -> str :
96+ return os . path .join (repo .common_dir , 'packed-refs' )
9897
9998 @classmethod
100- def _iter_packed_refs (cls , repo ) :
101- """Returns an iterator yielding pairs of sha1/path pairs (as bytes ) for the corresponding refs.
99+ def _iter_packed_refs (cls , repo : 'Repo' ) -> Iterator [ Tuple [ str , str ]] :
100+ """Returns an iterator yielding pairs of sha1/path pairs (as strings ) for the corresponding refs.
102101 :note: The packed refs file will be kept open as long as we iterate"""
103102 try :
104103 with open (cls ._get_packed_refs_path (repo ), 'rt' , encoding = 'UTF-8' ) as fp :
@@ -126,7 +125,7 @@ def _iter_packed_refs(cls, repo):
126125 if line [0 ] == '^' :
127126 continue
128127
129- yield tuple (line .split (' ' , 1 ))
128+ yield cast ( Tuple [ str , str ], tuple (line .split (' ' , 1 ) ))
130129 # END for each line
131130 except OSError :
132131 return None
@@ -137,7 +136,7 @@ def _iter_packed_refs(cls, repo):
137136 # alright.
138137
139138 @classmethod
140- def dereference_recursive (cls , repo , ref_path ) :
139+ def dereference_recursive (cls , repo : 'Repo' , ref_path : PathLike ) -> str :
141140 """
142141 :return: hexsha stored in the reference at the given ref_path, recursively dereferencing all
143142 intermediate references as required
@@ -149,14 +148,14 @@ def dereference_recursive(cls, repo, ref_path):
149148 # END recursive dereferencing
150149
151150 @classmethod
152- def _get_ref_info_helper (cls , repo , ref_path ):
151+ def _get_ref_info_helper (cls , repo : 'Repo' , ref_path : PathLike ):
153152 """Return: (str(sha), str(target_ref_path)) if available, the sha the file at
154153 rela_path points to, or None. target_ref_path is the reference we
155154 point to, or None"""
156- tokens = None
155+ tokens : Union [ None , List [ str ], Tuple [ str , str ]] = None
157156 repodir = _git_dir (repo , ref_path )
158157 try :
159- with open (osp .join (repodir , ref_path ), 'rt' , encoding = 'UTF-8' ) as fp :
158+ with open (os . path .join (repodir , ref_path ), 'rt' , encoding = 'UTF-8' ) as fp :
160159 value = fp .read ().rstrip ()
161160 # Don't only split on spaces, but on whitespace, which allows to parse lines like
162161 # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
@@ -447,8 +446,8 @@ def delete(cls, repo, path):
447446 or just "myreference", hence 'refs/' is implied.
448447 Alternatively the symbolic reference to be deleted"""
449448 full_ref_path = cls .to_full_path (path )
450- abs_path = osp .join (repo .common_dir , full_ref_path )
451- if osp .exists (abs_path ):
449+ abs_path = os . path .join (repo .common_dir , full_ref_path )
450+ if os . path .exists (abs_path ):
452451 os .remove (abs_path )
453452 else :
454453 # check packed refs
@@ -489,7 +488,7 @@ def delete(cls, repo, path):
489488
490489 # delete the reflog
491490 reflog_path = RefLog .path (cls (repo , full_ref_path ))
492- if osp .isfile (reflog_path ):
491+ if os . path .isfile (reflog_path ):
493492 os .remove (reflog_path )
494493 # END remove reflog
495494
@@ -502,14 +501,14 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
502501 instead"""
503502 git_dir = _git_dir (repo , path )
504503 full_ref_path = cls .to_full_path (path )
505- abs_ref_path = osp .join (git_dir , full_ref_path )
504+ abs_ref_path = os . path .join (git_dir , full_ref_path )
506505
507506 # figure out target data
508507 target = reference
509508 if resolve :
510509 target = repo .rev_parse (str (reference ))
511510
512- if not force and osp .isfile (abs_ref_path ):
511+ if not force and os . path .isfile (abs_ref_path ):
513512 target_data = str (target )
514513 if isinstance (target , SymbolicReference ):
515514 target_data = target .path
@@ -559,7 +558,7 @@ def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str]
559558 :note: This does not alter the current HEAD, index or Working Tree"""
560559 return cls ._create (repo , path , cls ._resolve_ref_on_create , reference , force , logmsg )
561560
562- def rename (self , new_path , force = False ):
561+ def rename (self , new_path : PathLike , force : bool = False ) -> 'SymbolicReference' :
563562 """Rename self to a new path
564563
565564 :param new_path:
@@ -577,9 +576,9 @@ def rename(self, new_path, force=False):
577576 if self .path == new_path :
578577 return self
579578
580- new_abs_path = osp .join (_git_dir (self .repo , new_path ), new_path )
581- cur_abs_path = osp .join (_git_dir (self .repo , self .path ), self .path )
582- if osp .isfile (new_abs_path ):
579+ new_abs_path = os . path .join (_git_dir (self .repo , new_path ), new_path )
580+ cur_abs_path = os . path .join (_git_dir (self .repo , self .path ), self .path )
581+ if os . path .isfile (new_abs_path ):
583582 if not force :
584583 # if they point to the same file, its not an error
585584 with open (new_abs_path , 'rb' ) as fd1 :
@@ -594,8 +593,8 @@ def rename(self, new_path, force=False):
594593 os .remove (new_abs_path )
595594 # END handle existing target file
596595
597- dname = osp .dirname (new_abs_path )
598- if not osp .isdir (dname ):
596+ dname = os . path .dirname (new_abs_path )
597+ if not os . path .isdir (dname ):
599598 os .makedirs (dname )
600599 # END create directory
601600
@@ -630,7 +629,7 @@ def _iter_items(cls: Type[T_References], repo: 'Repo', common_path: Union[PathLi
630629
631630 # read packed refs
632631 for _sha , rela_path in cls ._iter_packed_refs (repo ):
633- if rela_path .startswith (common_path ):
632+ if rela_path .startswith (str ( common_path ) ):
634633 rela_paths .add (rela_path )
635634 # END relative path matches common path
636635 # END packed refs reading
@@ -644,8 +643,8 @@ def _iter_items(cls: Type[T_References], repo: 'Repo', common_path: Union[PathLi
644643 # END for each sorted relative refpath
645644
646645 @classmethod
647- # type: ignore[override]
648- def iter_items ( cls , repo : 'Repo' , common_path : Union [ PathLike , None ] = None , * args , ** kwargs ) :
646+ def iter_items ( cls : Type [ T_References ], repo : 'Repo' , common_path : Union [ PathLike , None ] = None ,
647+ * args : Any , ** kwargs : Any ) -> Iterator [ T_References ] :
649648 """Find all refs in the repository
650649
651650 :param repo: is the Repo
@@ -665,7 +664,7 @@ def iter_items(cls, repo: 'Repo', common_path: Union[PathLike, None] = None, *ar
665664 return (r for r in cls ._iter_items (repo , common_path ) if r .__class__ == SymbolicReference or not r .is_detached )
666665
667666 @classmethod
668- def from_path (cls , repo , path ) :
667+ def from_path (cls , repo : 'Repo' , path : PathLike ) -> Union [ 'Head' , 'TagReference' , 'Reference' ] :
669668 """
670669 :param path: full .git-directory-relative path name to the Reference to instantiate
671670 :note: use to_full_path() if you only have a partial path of a known Reference Type
0 commit comments