Skip to content

Commit b8bb60e

Browse files
authored
Merge pull request #2094 from George-Ogden/join-pathlike
Join `Pathlike` Object to Tree
2 parents 6d66a02 + c8b58c0 commit b8bb60e

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

‎git/objects/tree.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
__all__= ["TreeModifier", "Tree"]
77

8+
importos
89
importsys
910

1011
importgit.diffasgit_diff
@@ -230,7 +231,7 @@ def _iter_convert_to_object(self, iterable: Iterable[TreeCacheTup]) -> Iterator[
230231
raiseTypeError("Unknown mode %o found in tree data for path '%s'"% (mode, path)) frome
231232
# END for each item
232233

233-
defjoin(self, file: str) ->IndexObjUnion:
234+
defjoin(self, file: PathLike) ->IndexObjUnion:
234235
"""Find the named object in this tree's contents.
235236
236237
:return:
@@ -241,6 +242,7 @@ def join(self, file: str) -> IndexObjUnion:
241242
If the given file or tree does not exist in this tree.
242243
"""
243244
msg="Blob or Tree named %r not found"
245+
file=os.fspath(file)
244246
if"/"infile:
245247
tree=self
246248
item=self
@@ -269,7 +271,7 @@ def join(self, file: str) -> IndexObjUnion:
269271
raiseKeyError(msg%file)
270272
# END handle long paths
271273

272-
def__truediv__(self, file: str) ->IndexObjUnion:
274+
def__truediv__(self, file: PathLike) ->IndexObjUnion:
273275
"""The ``/`` operator is another syntax for joining.
274276
275277
See :meth:`join` for details.

‎test/test_tree.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
frompathlibimportPath
99
importsubprocess
1010

11+
importpytest
12+
1113
fromgit.objectsimportBlob, Tree
14+
fromgit.repoimportRepo
1215
fromgit.utilimportcwd
1316

1417
fromtest.libimportTestBase, with_rw_directory
18+
from .lib.helperimportPathLikeMock, with_rw_repo
1519

1620

1721
classTestTree(TestBase):
@@ -161,3 +165,57 @@ def lib_folder(t, _d):
161165
assertroot[item.path] ==item==root/item.path
162166
# END for each item
163167
assertfound_slash
168+
169+
@with_rw_repo("0.3.2.1")
170+
deftest_repo_lookup_string_path(self, rw_repo):
171+
repo=Repo(rw_repo.git_dir)
172+
blob=repo.tree() /".gitignore"
173+
assertisinstance(blob, Blob)
174+
assertblob.hexsha=="787b3d442a113b78e343deb585ab5531eb7187fa"
175+
176+
@with_rw_repo("0.3.2.1")
177+
deftest_repo_lookup_pathlike_path(self, rw_repo):
178+
repo=Repo(rw_repo.git_dir)
179+
blob=repo.tree() /PathLikeMock(".gitignore")
180+
assertisinstance(blob, Blob)
181+
assertblob.hexsha=="787b3d442a113b78e343deb585ab5531eb7187fa"
182+
183+
@with_rw_repo("0.3.2.1")
184+
deftest_repo_lookup_invalid_string_path(self, rw_repo):
185+
repo=Repo(rw_repo.git_dir)
186+
withpytest.raises(KeyError):
187+
repo.tree() /"doesnotexist"
188+
189+
@with_rw_repo("0.3.2.1")
190+
deftest_repo_lookup_invalid_pathlike_path(self, rw_repo):
191+
repo=Repo(rw_repo.git_dir)
192+
withpytest.raises(KeyError):
193+
repo.tree() /PathLikeMock("doesnotexist")
194+
195+
@with_rw_repo("0.3.2.1")
196+
deftest_repo_lookup_nested_string_path(self, rw_repo):
197+
repo=Repo(rw_repo.git_dir)
198+
blob=repo.tree() /"git/__init__.py"
199+
assertisinstance(blob, Blob)
200+
assertblob.hexsha=="d87dcbdbb65d2782e14eea27e7f833a209c052f3"
201+
202+
@with_rw_repo("0.3.2.1")
203+
deftest_repo_lookup_nested_pathlike_path(self, rw_repo):
204+
repo=Repo(rw_repo.git_dir)
205+
blob=repo.tree() /PathLikeMock("git/__init__.py")
206+
assertisinstance(blob, Blob)
207+
assertblob.hexsha=="d87dcbdbb65d2782e14eea27e7f833a209c052f3"
208+
209+
@with_rw_repo("0.3.2.1")
210+
deftest_repo_lookup_folder_string_path(self, rw_repo):
211+
repo=Repo(rw_repo.git_dir)
212+
tree=repo.tree() /"git"
213+
assertisinstance(tree, Tree)
214+
asserttree.hexsha=="ec8ae429156d65afde4bbb3455570193b56f0977"
215+
216+
@with_rw_repo("0.3.2.1")
217+
deftest_repo_lookup_folder_pathlike_path(self, rw_repo):
218+
repo=Repo(rw_repo.git_dir)
219+
tree=repo.tree() /PathLikeMock("git")
220+
assertisinstance(tree, Tree)
221+
asserttree.hexsha=="ec8ae429156d65afde4bbb3455570193b56f0977"

0 commit comments

Comments
(0)