◐ Shell
clean mode source ↗

Respect `os.Pathlike` by George-Ogden · Pull Request #2086 · gitpython-developers/GitPython

Expand Up @@ -4,6 +4,7 @@ __all__ = ["SymbolicReference"]
import os from pathlib import Path
from gitdb.exc import BadName, BadObject
Expand Down Expand Up @@ -76,10 +77,10 @@ class SymbolicReference:
def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None: self.repo = repo self.path = path self.path: PathLike = path
def __str__(self) -> str: return str(self.path) return os.fspath(self.path)
def __repr__(self) -> str: return '<git.%s "%s">' % (self.__class__.__name__, self.path) Expand All @@ -103,7 +104,7 @@ def name(self) -> str: In case of symbolic references, the shortest assumable name is the path itself. """ return str(self.path) return os.fspath(self.path)
@property def abspath(self) -> PathLike: Expand Down Expand Up @@ -178,7 +179,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None: """ previous: Union[str, None] = None one_before_previous: Union[str, None] = None for c in str(ref_path): for c in os.fspath(ref_path): if c in " ~^:?*[\\": raise ValueError( f"Invalid reference '{ref_path}': references cannot contain spaces, tildes (~), carets (^)," Expand Down Expand Up @@ -212,7 +213,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None: raise ValueError(f"Invalid reference '{ref_path}': references cannot end with a forward slash (/)") elif previous == "@" and one_before_previous is None: raise ValueError(f"Invalid reference '{ref_path}': references cannot be '@'") elif any(component.endswith(".lock") for component in str(ref_path).split("/")): elif any(component.endswith(".lock") for component in Path(ref_path).parts): raise ValueError( f"Invalid reference '{ref_path}': references cannot have slash-separated components that end with" " '.lock'" Expand All @@ -235,7 +236,7 @@ def _get_ref_info_helper( tokens: Union[None, List[str], Tuple[str, str]] = None repodir = _git_dir(repo, ref_path) try: with open(os.path.join(repodir, str(ref_path)), "rt", encoding="UTF-8") as fp: with open(os.path.join(repodir, ref_path), "rt", encoding="UTF-8") as fp: # type: ignore[arg-type] value = fp.read().rstrip() # Don't only split on spaces, but on whitespace, which allows to parse lines like: # 60b64ef992065e2600bfef6187a97f92398a9144 branch 'master' of git-server:/path/to/repo Expand Down Expand Up @@ -614,7 +615,7 @@ def to_full_path(cls, path: Union[PathLike, "SymbolicReference"]) -> PathLike: full_ref_path = path if not cls._common_path_default: return full_ref_path if not str(path).startswith(cls._common_path_default + "/"): if not os.fspath(path).startswith(cls._common_path_default + "/"): full_ref_path = "%s/%s" % (cls._common_path_default, path) return full_ref_path
Expand Down Expand Up @@ -706,7 +707,7 @@ def _create( if not force and os.path.isfile(abs_ref_path): target_data = str(target) if isinstance(target, SymbolicReference): target_data = str(target.path) target_data = os.fspath(target.path) if not resolve: target_data = "ref: " + target_data with open(abs_ref_path, "rb") as fd: Expand Down Expand Up @@ -842,7 +843,7 @@ def _iter_items(
# Read packed refs. for _sha, rela_path in cls._iter_packed_refs(repo): if rela_path.startswith(str(common_path)): if rela_path.startswith(os.fspath(common_path)): rela_paths.add(rela_path) # END relative path matches common path # END packed refs reading Expand Down Expand Up @@ -930,4 +931,4 @@ def from_path(cls: Type[T_References], repo: "Repo", path: PathLike) -> T_Refere
def is_remote(self) -> bool: """:return: True if this symbolic reference points to a remote branch""" return str(self.path).startswith(self._remote_common_path_default + "/") return os.fspath(self.path).startswith(self._remote_common_path_default + "/")