Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions git/objects/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

__all__ = ["TreeModifier", "Tree"]

import os
import sys

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

def join(self, file: str) -> IndexObjUnion:
def join(self, file: PathLike) -> IndexObjUnion:
"""Find the named object in this tree's contents.

:return:
Expand All @@ -241,6 +242,7 @@ def join(self, file: str) -> IndexObjUnion:
If the given file or tree does not exist in this tree.
"""
msg = "Blob or Tree named %r not found"
file = os.fspath(file)
if "/" in file:
tree = self
item = self
Expand Down Expand Up @@ -269,7 +271,7 @@ def join(self, file: str) -> IndexObjUnion:
raise KeyError(msg % file)
# END handle long paths

def __truediv__(self, file: str) -> IndexObjUnion:
def __truediv__(self, file: PathLike) -> IndexObjUnion:
"""The ``/`` operator is another syntax for joining.

See :meth:`join` for details.
Expand Down
58 changes: 58 additions & 0 deletions test/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
from pathlib import Path
import subprocess

import pytest

from git.objects import Blob, Tree
from git.repo import Repo
from git.util import cwd

from test.lib import TestBase, with_rw_directory
from .lib.helper import PathLikeMock, with_rw_repo


class TestTree(TestBase):
Expand Down Expand Up @@ -161,3 +165,57 @@ def lib_folder(t, _d):
assert root[item.path] == item == root / item.path
# END for each item
assert found_slash

@with_rw_repo("0.3.2.1")
def test_repo_lookup_string_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
blob = repo.tree() / ".gitignore"
assert isinstance(blob, Blob)
assert blob.hexsha == "787b3d442a113b78e343deb585ab5531eb7187fa"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_pathlike_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
blob = repo.tree() / PathLikeMock(".gitignore")
assert isinstance(blob, Blob)
assert blob.hexsha == "787b3d442a113b78e343deb585ab5531eb7187fa"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_invalid_string_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
with pytest.raises(KeyError):
repo.tree() / "doesnotexist"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_invalid_pathlike_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
with pytest.raises(KeyError):
repo.tree() / PathLikeMock("doesnotexist")

@with_rw_repo("0.3.2.1")
def test_repo_lookup_nested_string_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
blob = repo.tree() / "git/__init__.py"
assert isinstance(blob, Blob)
assert blob.hexsha == "d87dcbdbb65d2782e14eea27e7f833a209c052f3"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_nested_pathlike_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
blob = repo.tree() / PathLikeMock("git/__init__.py")
assert isinstance(blob, Blob)
assert blob.hexsha == "d87dcbdbb65d2782e14eea27e7f833a209c052f3"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_folder_string_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
tree = repo.tree() / "git"
assert isinstance(tree, Tree)
assert tree.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977"

@with_rw_repo("0.3.2.1")
def test_repo_lookup_folder_pathlike_path(self, rw_repo):
repo = Repo(rw_repo.git_dir)
tree = repo.tree() / PathLikeMock("git")
assert isinstance(tree, Tree)
assert tree.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977"
Loading