From 9fa28ae108dc39cfb13282cd18d4251d0118dd52 Mon Sep 17 00:00:00 2001 From: George Ogden Date: Fri, 12 Dec 2025 21:41:25 +0000 Subject: [PATCH 1/3] Add failing tests for joining paths --- test/test_tree.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/test_tree.py b/test/test_tree.py index 7ba93bd36..dafd32847 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -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): @@ -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) + blob = repo.tree() / "git" + assert isinstance(blob, Tree) + assert blob.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) + blob = repo.tree() / PathLikeMock("git") + assert isinstance(blob, Tree) + assert blob.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977" From 88e26141c738f6ac3beb1a433039611f88c2c30d Mon Sep 17 00:00:00 2001 From: George Ogden Date: Fri, 12 Dec 2025 21:41:50 +0000 Subject: [PATCH 2/3] Allow joining path to tree --- git/objects/tree.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/git/objects/tree.py b/git/objects/tree.py index 1845d0d0d..a3d611c80 100644 --- a/git/objects/tree.py +++ b/git/objects/tree.py @@ -5,6 +5,7 @@ __all__ = ["TreeModifier", "Tree"] +import os import sys import git.diff as git_diff @@ -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: @@ -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 @@ -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. From c8b58c09904dabe67222165e4d3eecf4c8f07490 Mon Sep 17 00:00:00 2001 From: George Ogden <38294960+George-Ogden@users.noreply.github.com> Date: Sun, 14 Dec 2025 17:42:21 +0000 Subject: [PATCH 3/3] Update test/test_tree.py Rename blob to tree Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- test/test_tree.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_tree.py b/test/test_tree.py index dafd32847..629fd4d32 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -209,13 +209,13 @@ def test_repo_lookup_nested_pathlike_path(self, rw_repo): @with_rw_repo("0.3.2.1") def test_repo_lookup_folder_string_path(self, rw_repo): repo = Repo(rw_repo.git_dir) - blob = repo.tree() / "git" - assert isinstance(blob, Tree) - assert blob.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977" + 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) - blob = repo.tree() / PathLikeMock("git") - assert isinstance(blob, Tree) - assert blob.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977" + tree = repo.tree() / PathLikeMock("git") + assert isinstance(tree, Tree) + assert tree.hexsha == "ec8ae429156d65afde4bbb3455570193b56f0977"