Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert paths in constructors and large function calls
  • Loading branch information
George-Ogden committed Nov 28, 2025
commit 3801505d1218242e853dda17e981e2a2fa795b0e
5 changes: 3 additions & 2 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,15 @@ def _to_relative_path(self, path: PathLike) -> PathLike:

:raise ValueError:
"""
path = os.fspath(path)
if not osp.isabs(path):
return path
if self.repo.bare:
raise InvalidGitRepositoryError("require non-bare repository")
if not osp.normpath(os.fspath(path)).startswith(os.fspath(self.repo.working_tree_dir)):
if not osp.normpath(path).startswith(os.fspath(self.repo.working_tree_dir)):
raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir))
result = os.path.relpath(path, self.repo.working_tree_dir)
if os.fspath(path).endswith(os.sep) and not result.endswith(os.sep):
if path.endswith(os.sep) and not result.endswith(os.sep):
result += os.sep
return result

Expand Down
2 changes: 1 addition & 1 deletion git/index/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TemporaryFileSwap:
__slots__ = ("file_path", "tmp_file_path")

def __init__(self, file_path: PathLike) -> None:
self.file_path = file_path
self.file_path = os.fspath(file_path)
dirname, basename = osp.split(file_path)
fd, self.tmp_file_path = tempfile.mkstemp(prefix=basename, dir=dirname)
os.close(fd)
Expand Down
2 changes: 2 additions & 0 deletions git/refs/head.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__all__ = ["HEAD", "Head"]

import os
from git.config import GitConfigParser, SectionConstraint
from git.exc import GitCommandError
from git.util import join_path
Expand Down Expand Up @@ -48,6 +49,7 @@ class HEAD(SymbolicReference):
commit: "Commit"

def __init__(self, repo: "Repo", path: PathLike = _HEAD_NAME) -> None:
path = os.fspath(path)
if path != self._HEAD_NAME:
raise ValueError("HEAD instance must point to %r, got %r" % (self._HEAD_NAME, path))
super().__init__(repo, path)
Expand Down
3 changes: 2 additions & 1 deletion git/refs/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__all__ = ["RefLog", "RefLogEntry"]

from mmap import mmap
import os
import os.path as osp
import re
import time as _time
Expand Down Expand Up @@ -167,7 +168,7 @@ def __init__(self, filepath: Union[PathLike, None] = None) -> None:
"""Initialize this instance with an optional filepath, from which we will
initialize our data. The path is also used to write changes back using the
:meth:`write` method."""
self._path = filepath
self._path = os.fspath(filepath)
if filepath is not None:
self._read_from_file()
# END handle filepath
Expand Down
2 changes: 1 addition & 1 deletion git/refs/symbolic.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SymbolicReference:

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

def __str__(self) -> str:
return os.fspath(self.path)
Expand Down
1 change: 1 addition & 0 deletions git/repo/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def is_git_dir(d: PathLike) -> bool:
clearly indicates that we don't support it. There is the unlikely danger to
throw if we see directories which just look like a worktree dir, but are none.
"""
d = os.fspath(d)
if osp.isdir(d):
if (osp.isdir(osp.join(d, "objects")) or "GIT_OBJECT_DIRECTORY" in os.environ) and osp.isdir(
osp.join(d, "refs")
Expand Down
2 changes: 1 addition & 1 deletion git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ class LockFile:
__slots__ = ("_file_path", "_owns_lock")

def __init__(self, file_path: PathLike) -> None:
self._file_path = file_path
self._file_path = os.fspath(file_path)
self._owns_lock = False

def __del__(self) -> None:
Expand Down
Loading