Merge pull request #2086 from George-Ogden/true-pathlike · gitpython-developers/GitPython@eecc28d

@@ -4,6 +4,7 @@

44

__all__ = ["SymbolicReference"]

5566

import os

7+

from pathlib import Path

7889

from gitdb.exc import BadName, BadObject

910

@@ -76,10 +77,10 @@ class SymbolicReference:

76777778

def __init__(self, repo: "Repo", path: PathLike, check_path: bool = False) -> None:

7879

self.repo = repo

79-

self.path = path

80+

self.path: PathLike = path

80818182

def __str__(self) -> str:

82-

return str(self.path)

83+

return os.fspath(self.path)

83848485

def __repr__(self) -> str:

8586

return '<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

109110

def abspath(self) -> PathLike:

@@ -178,7 +179,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None:

178179

"""

179180

previous: Union[str, None] = None

180181

one_before_previous: Union[str, None] = None

181-

for c in str(ref_path):

182+

for c in os.fspath(ref_path):

182183

if c in " ~^:?*[\\":

183184

raise ValueError(

184185

f"Invalid reference '{ref_path}': references cannot contain spaces, tildes (~), carets (^),"

@@ -212,7 +213,7 @@ def _check_ref_name_valid(ref_path: PathLike) -> None:

212213

raise ValueError(f"Invalid reference '{ref_path}': references cannot end with a forward slash (/)")

213214

elif previous == "@" and one_before_previous is None:

214215

raise 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):

216217

raise ValueError(

217218

f"Invalid reference '{ref_path}': references cannot have slash-separated components that end with"

218219

" '.lock'"

@@ -235,7 +236,7 @@ def _get_ref_info_helper(

235236

tokens: Union[None, List[str], Tuple[str, str]] = None

236237

repodir = _git_dir(repo, ref_path)

237238

try:

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]

239240

value = 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:

614615

full_ref_path = path

615616

if not cls._common_path_default:

616617

return full_ref_path

617-

if not str(path).startswith(cls._common_path_default + "/"):

618+

if not os.fspath(path).startswith(cls._common_path_default + "/"):

618619

full_ref_path = "%s/%s" % (cls._common_path_default, path)

619620

return full_ref_path

620621

@@ -706,7 +707,7 @@ def _create(

706707

if not force and os.path.isfile(abs_ref_path):

707708

target_data = str(target)

708709

if isinstance(target, SymbolicReference):

709-

target_data = str(target.path)

710+

target_data = os.fspath(target.path)

710711

if not resolve:

711712

target_data = "ref: " + target_data

712713

with open(abs_ref_path, "rb") as fd:

@@ -842,7 +843,7 @@ def _iter_items(

842843843844

# Read packed refs.

844845

for _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)):

846847

rela_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

930931931932

def 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 + "/")