Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bbb6ee

Browse files
IndexFile._to_relative_path - fix case where absolute path gets stripped of trailing slash
1 parentfe7533e commit2bbb6ee

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

‎git/index/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ def _to_relative_path(self, path: PathLike) -> PathLike:
655655
raiseInvalidGitRepositoryError("require non-bare repository")
656656
ifnotosp.normpath(str(path)).startswith(str(self.repo.working_tree_dir)):
657657
raiseValueError("Absolute path %r is not in git repository at %r"% (path,self.repo.working_tree_dir))
658-
returnos.path.relpath(path,self.repo.working_tree_dir)
658+
result=os.path.relpath(path,self.repo.working_tree_dir)
659+
ifstr(path).endswith(os.sep)andnotresult.endswith(os.sep):
660+
result+=os.sep
661+
returnresult
659662

660663
def_preprocess_add_items(
661664
self,items:Union[PathLike,Sequence[Union[PathLike,Blob,BaseIndexEntry,"Submodule"]]]

‎test/test_index.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
importsubprocess
1717
importsys
1818
importtempfile
19+
fromunittestimportmock
1920

2021
fromgitdb.baseimportIStream
2122

@@ -1015,6 +1016,27 @@ class Mocked:
10151016
rel=index._to_relative_path(path)
10161017
self.assertEqual(rel,os.path.relpath(path,root))
10171018

1019+
deftest__to_relative_path_absolute_trailing_slash(self):
1020+
repo_root=os.path.join(osp.abspath(os.sep),"directory1","repo_root")
1021+
1022+
classMocked:
1023+
bare=False
1024+
git_dir=repo_root
1025+
working_tree_dir=repo_root
1026+
1027+
repo=Mocked()
1028+
path=os.path.join(repo_root,"directory2/")
1029+
index=IndexFile(repo)
1030+
1031+
expected_path="directory2/"
1032+
actual_path=index._to_relative_path(path)
1033+
self.assertEqual(expected_path,actual_path)
1034+
1035+
withmock.patch("git.index.base.os.path")asospath_mock:
1036+
ospath_mock.relpath.return_value="directory2/"
1037+
actual_path=index._to_relative_path(path)
1038+
self.assertEqual(expected_path,actual_path)
1039+
10181040
@pytest.mark.xfail(
10191041
type(_win_bash_status)isWinBashStatus.Absent,
10201042
reason="Can't run a hook on Windows without bash.exe.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp