1- from git .types import PathLike
21import os
32
43from git .compat import defenc
1716 BadName
1817)
1918
20- import os .path as osp
21-
2219from .log import RefLog
2320
2421# typing ------------------------------------------------------------------
2522
26- from typing import Any , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING # NOQA
23+ from typing import Any , Iterator , List , Match , Optional , Tuple , Type , TypeVar , Union , TYPE_CHECKING , cast # NOQA
2724from git .types import Commit_ish , PathLike , TBD , Literal # NOQA
2825
2926if TYPE_CHECKING :
3835__all__ = ["SymbolicReference" ]
3936
4037
41- def _git_dir (repo , path ) :
38+ def _git_dir (repo : 'Repo' , path : PathLike ) -> PathLike :
4239 """ Find the git dir that's appropriate for the path"""
43- name = "%s" % ( path ,)
40+ name = f" { path } "
4441 if name in ['HEAD' , 'ORIG_HEAD' , 'FETCH_HEAD' , 'index' , 'logs' ]:
4542 return repo .git_dir
4643 return repo .common_dir
@@ -60,46 +57,46 @@ class SymbolicReference(object):
6057 _remote_common_path_default = "refs/remotes"
6158 _id_attribute_ = "name"
6259
63- def __init__ (self , repo : 'Repo' , path : PathLike , check_path : bool = False ):
60+ def __init__ (self , repo : 'Repo' , path : PathLike , check_path : bool = False ) -> None :
6461 self .repo = repo
6562 self .path = str (path )
6663
6764 def __str__ (self ) -> str :
6865 return self .path
6966
70- def __repr__ (self ):
67+ def __repr__ (self ) -> str :
7168 return '<git.%s "%s">' % (self .__class__ .__name__ , self .path )
7269
73- def __eq__ (self , other ):
70+ def __eq__ (self , other ) -> bool :
7471 if hasattr (other , 'path' ):
7572 return self .path == other .path
7673 return False
7774
78- def __ne__ (self , other ):
75+ def __ne__ (self , other ) -> bool :
7976 return not (self == other )
8077
8178 def __hash__ (self ):
8279 return hash (self .path )
8380
8481 @property
85- def name (self ):
82+ def name (self ) -> str :
8683 """
8784 :return:
8885 In case of symbolic references, the shortest assumable name
8986 is the path itself."""
90- return self .path
87+ return str ( self .path )
9188
9289 @property
93- def abspath (self ):
90+ def abspath (self ) -> PathLike :
9491 return join_path_native (_git_dir (self .repo , self .path ), self .path )
9592
9693 @classmethod
97- def _get_packed_refs_path (cls , repo ) :
98- return osp .join (repo .common_dir , 'packed-refs' )
94+ def _get_packed_refs_path (cls , repo : 'Repo' ) -> str :
95+ return os . path .join (repo .common_dir , 'packed-refs' )
9996
10097 @classmethod
101- def _iter_packed_refs (cls , repo ) :
102- """Returns an iterator yielding pairs of sha1/path pairs (as bytes) for the corresponding refs.
98+ def _iter_packed_refs (cls , repo : 'Repo' ) -> Iterator [ Tuple [ str , str ]] :
99+ """Returns an iterator yielding pairs of sha1/path pairs for the corresponding refs.
103100 :note: The packed refs file will be kept open as long as we iterate"""
104101 try :
105102 with open (cls ._get_packed_refs_path (repo ), 'rt' , encoding = 'UTF-8' ) as fp :
@@ -127,7 +124,7 @@ def _iter_packed_refs(cls, repo):
127124 if line [0 ] == '^' :
128125 continue
129126
130- yield tuple (line .split (' ' , 1 ))
127+ yield cast ( Tuple [ str , str ], tuple (line .split (' ' , 1 ) ))
131128 # END for each line
132129 except OSError :
133130 return None
@@ -138,7 +135,7 @@ def _iter_packed_refs(cls, repo):
138135 # alright.
139136
140137 @classmethod
141- def dereference_recursive (cls , repo , ref_path ) :
138+ def dereference_recursive (cls , repo : 'Repo' , ref_path : PathLike ) -> str :
142139 """
143140 :return: hexsha stored in the reference at the given ref_path, recursively dereferencing all
144141 intermediate references as required
@@ -150,14 +147,14 @@ def dereference_recursive(cls, repo, ref_path):
150147 # END recursive dereferencing
151148
152149 @classmethod
153- def _get_ref_info_helper (cls , repo , ref_path ):
150+ def _get_ref_info_helper (cls , repo : Repo , ref_path : PathLike ):
154151 """Return: (str(sha), str(target_ref_path)) if available, the sha the file at
155152 rela_path points to, or None. target_ref_path is the reference we
156153 point to, or None"""
157- tokens = None
154+ tokens : Union [ List [ str ], Tuple [ str , str ], None ] = None
158155 repodir = _git_dir (repo , ref_path )
159156 try :
160- with open (osp .join (repodir , ref_path ), 'rt' , encoding = 'UTF-8' ) as fp :
157+ with open (os . path .join (repodir , ref_path ), 'rt' , encoding = 'UTF-8' ) as fp :
161158 value = fp .read ().rstrip ()
162159 # Don't only split on spaces, but on whitespace, which allows to parse lines like
163160 # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
@@ -189,7 +186,7 @@ def _get_ref_info_helper(cls, repo, ref_path):
189186 raise ValueError ("Failed to parse reference information from %r" % ref_path )
190187
191188 @classmethod
192- def _get_ref_info (cls , repo , ref_path ):
189+ def _get_ref_info (cls , repo : 'Repo' , ref_path : PathLike ):
193190 """Return: (str(sha), str(target_ref_path)) if available, the sha the file at
194191 rela_path points to, or None. target_ref_path is the reference we
195192 point to, or None"""
@@ -424,21 +421,22 @@ def log_entry(self, index):
424421 return RefLog .entry_at (RefLog .path (self ), index )
425422
426423 @classmethod
427- def to_full_path (cls , path ) -> PathLike :
424+ def to_full_path (cls , path : Union [ PathLike , 'SymbolicReference' ] ) -> str :
428425 """
429426 :return: string with a full repository-relative path which can be used to initialize
430427 a Reference instance, for instance by using ``Reference.from_path``"""
431428 if isinstance (path , SymbolicReference ):
432429 path = path .path
433- full_ref_path = path
430+ full_ref_path = str ( path )
434431 if not cls ._common_path_default :
435432 return full_ref_path
436- if not path .startswith (cls ._common_path_default + "/" ):
433+
434+ if not str (path ).startswith (cls ._common_path_default + "/" ):
437435 full_ref_path = '%s/%s' % (cls ._common_path_default , path )
438436 return full_ref_path
439437
440438 @classmethod
441- def delete (cls , repo , path ) :
439+ def delete (cls , repo : 'Repo' , path : PathLike ) -> None :
442440 """Delete the reference at the given path
443441
444442 :param repo:
@@ -449,8 +447,8 @@ def delete(cls, repo, path):
449447 or just "myreference", hence 'refs/' is implied.
450448 Alternatively the symbolic reference to be deleted"""
451449 full_ref_path = cls .to_full_path (path )
452- abs_path = osp .join (repo .common_dir , full_ref_path )
453- if osp .exists (abs_path ):
450+ abs_path = os . path .join (repo .common_dir , full_ref_path )
451+ if os . path .exists (abs_path ):
454452 os .remove (abs_path )
455453 else :
456454 # check packed refs
@@ -460,8 +458,8 @@ def delete(cls, repo, path):
460458 new_lines = []
461459 made_change = False
462460 dropped_last_line = False
463- for line in reader :
464- line = line .decode (defenc )
461+ for line_bytes in reader :
462+ line = line_bytes .decode (defenc )
465463 _ , _ , line_ref = line .partition (' ' )
466464 line_ref = line_ref .strip ()
467465 # keep line if it is a comment or if the ref to delete is not
@@ -491,7 +489,7 @@ def delete(cls, repo, path):
491489
492490 # delete the reflog
493491 reflog_path = RefLog .path (cls (repo , full_ref_path ))
494- if osp .isfile (reflog_path ):
492+ if os . path .isfile (reflog_path ):
495493 os .remove (reflog_path )
496494 # END remove reflog
497495
@@ -504,14 +502,14 @@ def _create(cls, repo, path, resolve, reference, force, logmsg=None):
504502 instead"""
505503 git_dir = _git_dir (repo , path )
506504 full_ref_path = cls .to_full_path (path )
507- abs_ref_path = osp .join (git_dir , full_ref_path )
505+ abs_ref_path = os . path .join (git_dir , full_ref_path )
508506
509507 # figure out target data
510508 target = reference
511509 if resolve :
512510 target = repo .rev_parse (str (reference ))
513511
514- if not force and osp .isfile (abs_ref_path ):
512+ if not force and os . path .isfile (abs_ref_path ):
515513 target_data = str (target )
516514 if isinstance (target , SymbolicReference ):
517515 target_data = target .path
@@ -561,7 +559,7 @@ def create(cls, repo: 'Repo', path: PathLike, reference: Union[Commit_ish, str]
561559 :note: This does not alter the current HEAD, index or Working Tree"""
562560 return cls ._create (repo , path , cls ._resolve_ref_on_create , reference , force , logmsg )
563561
564- def rename (self , new_path , force = False ):
562+ def rename (self , new_path : str , force : bool = False ) -> 'SymbolicReference' :
565563 """Rename self to a new path
566564
567565 :param new_path:
@@ -579,9 +577,9 @@ def rename(self, new_path, force=False):
579577 if self .path == new_path :
580578 return self
581579
582- new_abs_path = osp .join (_git_dir (self .repo , new_path ), new_path )
583- cur_abs_path = osp .join (_git_dir (self .repo , self .path ), self .path )
584- if osp .isfile (new_abs_path ):
580+ new_abs_path = os . path .join (_git_dir (self .repo , new_path ), new_path )
581+ cur_abs_path = os . path .join (_git_dir (self .repo , self .path ), self .path )
582+ if os . path .isfile (new_abs_path ):
585583 if not force :
586584 # if they point to the same file, its not an error
587585 with open (new_abs_path , 'rb' ) as fd1 :
@@ -596,8 +594,8 @@ def rename(self, new_path, force=False):
596594 os .remove (new_abs_path )
597595 # END handle existing target file
598596
599- dname = osp .dirname (new_abs_path )
600- if not osp .isdir (dname ):
597+ dname = os . path .dirname (new_abs_path )
598+ if not os . path .isdir (dname ):
601599 os .makedirs (dname )
602600 # END create directory
603601
@@ -632,7 +630,7 @@ def _iter_items(cls: Type[T_References], repo: 'Repo', common_path: Union[PathLi
632630
633631 # read packed refs
634632 for _sha , rela_path in cls ._iter_packed_refs (repo ):
635- if rela_path .startswith (common_path ):
633+ if rela_path .startswith (str ( common_path ) ):
636634 rela_paths .add (rela_path )
637635 # END relative path matches common path
638636 # END packed refs reading
0 commit comments