Content-Length: 2654 | pFad | http://github.com/gitpython-developers/GitPython/pull/2012.patch
thub.com
From 11f7fafada8348c8e8f699c7ab621be6d26b00a5 Mon Sep 17 00:00:00 2001
From: Kamil Kozik
Date: Fri, 7 Mar 2025 18:39:06 +0100
Subject: [PATCH] `IndexFile._to_relative_path` - fix case where absolute path
gets stripped of trailing slash
---
git/index/base.py | 5 ++++-
test/test_index.py | 22 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/git/index/base.py b/git/index/base.py
index 39cc9143c..65b1f9308 100644
--- a/git/index/base.py
+++ b/git/index/base.py
@@ -655,7 +655,10 @@ def _to_relative_path(self, path: PathLike) -> PathLike:
raise InvalidGitRepositoryError("require non-bare repository")
if not osp.normpath(str(path)).startswith(str(self.repo.working_tree_dir)):
raise ValueError("Absolute path %r is not in git repository at %r" % (path, self.repo.working_tree_dir))
- return os.path.relpath(path, self.repo.working_tree_dir)
+ result = os.path.relpath(path, self.repo.working_tree_dir)
+ if str(path).endswith(os.sep) and not result.endswith(os.sep):
+ result += os.sep
+ return result
def _preprocess_add_items(
self, items: Union[PathLike, Sequence[Union[PathLike, Blob, BaseIndexEntry, "Submodule"]]]
diff --git a/test/test_index.py b/test/test_index.py
index c586a0b5a..c42032e70 100644
--- a/test/test_index.py
+++ b/test/test_index.py
@@ -16,6 +16,7 @@
import subprocess
import sys
import tempfile
+from unittest import mock
from gitdb.base import IStream
@@ -1015,6 +1016,27 @@ class Mocked:
rel = index._to_relative_path(path)
self.assertEqual(rel, os.path.relpath(path, root))
+ def test__to_relative_path_absolute_trailing_slash(self):
+ repo_root = os.path.join(osp.abspath(os.sep), "directory1", "repo_root")
+
+ class Mocked:
+ bare = False
+ git_dir = repo_root
+ working_tree_dir = repo_root
+
+ repo = Mocked()
+ path = os.path.join(repo_root, f"directory2{os.sep}")
+ index = IndexFile(repo)
+
+ expected_path = f"directory2{os.sep}"
+ actual_path = index._to_relative_path(path)
+ self.assertEqual(expected_path, actual_path)
+
+ with mock.patch("git.index.base.os.path") as ospath_mock:
+ ospath_mock.relpath.return_value = f"directory2{os.sep}"
+ actual_path = index._to_relative_path(path)
+ self.assertEqual(expected_path, actual_path)
+
@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.Absent,
reason="Can't run a hook on Windows without bash.exe.",
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/gitpython-developers/GitPython/pull/2012.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy