Merge pull request #2086 from George-Ogden/true-pathlike · gitpython-developers/GitPython@eecc28d
@@ -4,6 +4,7 @@
44__all__ = ["SymbolicReference"]
5566import os
7+from pathlib import Path
7889from gitdb.exc import BadName, BadObject
910@@ -76,10 +77,10 @@ class SymbolicReference:
76777778def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None:
7879self.repo = repo
79-self.path = path
80+self.path: PathLike = path
80818182def __str__(self) -> str:
82-return str(self.path)
83+return os.fspath(self.path)
83848485def __repr__(self) -> str:
8586return '<git.%s "%s">' % (self.__class__.__name__, self.path)
@@ -103,7 +104,7 @@ def name(self) -> str:
103104 In case of symbolic references, the shortest assumable name is the path
104105 itself.
105106 """
106-return str(self.path)
107+return os.fspath(self.path)
107108108109@property
109110def abspath(self) -> PathLike:
@@ -178,7 +179,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None:
178179 """
179180previous: Union[str, None] = None
180181one_before_previous: Union[str, None] = None
181-for c in str(ref_path):
182+for c in os.fspath(ref_path):
182183if c in " ~^:?*[\\":
183184raise ValueError(
184185f"Invalid reference '{ref_path}': references cannot contain spaces, tildes (~), carets (^),"
@@ -212,7 +213,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None:
212213raise ValueError(f"Invalid reference '{ref_path}': references cannot end with a forward slash (/)")
213214elif previous == "@" and one_before_previous is None:
214215raise ValueError(f"Invalid reference '{ref_path}': references cannot be '@'")
215-elif any(component.endswith(".lock") for component in str(ref_path).split("/")):
216+elif any(component.endswith(".lock") for component in Path(ref_path).parts):
216217raise ValueError(
217218f"Invalid reference '{ref_path}': references cannot have slash-separated components that end with"
218219" '.lock'"
@@ -235,7 +236,7 @@ def _get_ref_info_helper(
235236tokens: Union[None, List[str], Tuple[str, str]] = None
236237repodir = _git_dir(repo, ref_path)
237238try:
238-with open(os.path.join(repodir, str(ref_path)), "rt", encoding="UTF-8") as fp:
239+with open(os.path.join(repodir, ref_path), "rt", encoding="UTF-8") as fp: # type: ignore[arg-type]
239240value = fp.read().rstrip()
240241# Don't only split on spaces, but on whitespace, which allows to parse lines like:
241242# 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo
@@ -614,7 +615,7 @@ def to_full_path(cls, path: Union[PathLike, "SymbolicReference"]) -> PathLike:
614615full_ref_path = path
615616if not cls._common_path_default:
616617return full_ref_path
617-if not str(path).startswith(cls._common_path_default + "/"):
618+if not os.fspath(path).startswith(cls._common_path_default + "/"):
618619full_ref_path = "%s/%s" % (cls._common_path_default, path)
619620return full_ref_path
620621@@ -706,7 +707,7 @@ def _create(
706707if not force and os.path.isfile(abs_ref_path):
707708target_data = str(target)
708709if isinstance(target, SymbolicReference):
709-target_data = str(target.path)
710+target_data = os.fspath(target.path)
710711if not resolve:
711712target_data = "ref: " + target_data
712713with open(abs_ref_path, "rb") as fd:
@@ -842,7 +843,7 @@ def _iter_items(
842843843844# Read packed refs.
844845for _sha, rela_path in cls._iter_packed_refs(repo):
845-if rela_path.startswith(str(common_path)):
846+if rela_path.startswith(os.fspath(common_path)):
846847rela_paths.add(rela_path)
847848# END relative path matches common path
848849# END packed refs reading
@@ -930,4 +931,4 @@ def from_path(cls: Type[T_References], repo: "Repo", path: PathLike) -> T_Refere
930931931932def is_remote(self) -> bool:
932933""":return: True if this symbolic reference points to a remote branch"""
933-return str(self.path).startswith(self._remote_common_path_default + "/")
934+return os.fspath(self.path).startswith(self._remote_common_path_default + "/")