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+
import os
89
import sys
910

1011
import git.diff as git_diff
@@ -230,7 +231,7 @@ def _iter_convert_to_object(self, iterable: Iterable[TreeCacheTup]) -> Iterator[
230231
raise TypeError("Unknown mode %o found in tree data for path '%s'" % (mode, path)) from e
231232
# END for each item
232233

233-
def join(self, file: str) -> IndexObjUnion:
234+
def join(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 "/" in file:
245247
tree = self
246248
item = self
@@ -269,7 +271,7 @@ def join(self, file: str) -> IndexObjUnion:
269271
raise KeyError(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
from pathlib import Path
99
import subprocess
1010

11+
import pytest
12+
1113
from git.objects import Blob, Tree
14+
from git.repo import Repo
1215
from git.util import cwd
1316

1417
from test.lib import TestBase, with_rw_directory
18+
from .lib.helper import PathLikeMock, with_rw_repo
1519

1620

1721
class TestTree(TestBase):
@@ -161,3 +165,57 @@ def lib_folder(t, _d):
161165
assert root[item.path] == item == root / item.path
162166
# END for each item
163167
assert found_slash
168+
169+
@with_rw_repo("0.3.2.1")
170+
def test_repo_lookup_string_path(self, rw_repo):
171+
repo = Repo(rw_repo.git_dir)
172+
blob = repo.tree() / ".gitignore"
173+
assert isinstance(blob, Blob)
174+
assert blob.hexsha == "787b3d442a113b78e343deb585ab5531eb7187fa"
175+
176+
@with_rw_repo("0.3.2.1")
177+
def test_repo_lookup_pathlike_path(self, rw_repo):
178+
repo = Repo(rw_repo.git_dir)
179+
blob = repo.tree() / PathLikeMock(".gitignore")
180+
assert isinstance(blob, Blob)
181+
assert blob.hexsha == "787b3d442a113b78e343deb585ab5531eb7187fa"
182+
183+
@with_rw_repo("0.3.2.1")
184+
def test_repo_lookup_invalid_string_path(self, rw_repo):
185+
repo = Repo(rw_repo.git_dir)
186+
with pytest.raises(KeyError):
187+
repo.tree() / "doesnotexist"
188+
189+
@with_rw_repo("0.3.2.1")
190+
def test_repo_lookup_invalid_pathlike_path(self, rw_repo):
191+
repo = Repo(rw_repo.git_dir)
192+
with pytest.raises(KeyError):
193+
repo.tree() / PathLikeMock("doesnotexist")
194+
195+
@with_rw_repo("0.3.2.1")
196+
def test_repo_lookup_nested_string_path(self, rw_repo):
197+
repo = Repo(rw_repo.git_dir)
198+
blob = repo.tree() / "git/__init__.py"
199+
assert isinstance(blob, Blob)
200+
assert blob.hexsha == "d87dcbdbb65d2782e14eea27e7f833a209c052f3"
201+
202+
@with_rw_repo("0.3.2.1")
203+
def test_repo_lookup_nested_pathlike_path(self, rw_repo):
204+
repo = Repo(rw_repo.git_dir)
205+
blob = repo.tree() / PathLikeMock("git/__init__.py")
206+
assert isinstance(blob, Blob)
207+
assert blob.hexsha == "d87dcbdbb65d2782e14eea27e7f833a209c052f3"
208+
209+
@with_rw_repo("0.3.2.1")
210+
def test_repo_lookup_folder_string_path(self, rw_repo):
211+
repo = Repo(rw_repo.git_dir)
212+
tree = repo.tree() / "git"
213+
assert isinstance(tree, Tree)
214+
assert tree.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977"
215+
216+
@with_rw_repo("0.3.2.1")
217+
def test_repo_lookup_folder_pathlike_path(self, rw_repo):
218+
repo = Repo(rw_repo.git_dir)
219+
tree = repo.tree() / PathLikeMock("git")
220+
assert isinstance(tree, Tree)
221+
assert tree.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977"

0 commit comments

Comments
 (0)