Skip to content

Conversation

@George-Ogden
Copy link
Contributor

This extends the Tree.join and Tree.__truediv__ API to allow a pathlike object.
For example:

repo = Repo(my_local_repo)
interesting_filepath: Path = Path(folder) / file
blob = repo.tree() / interesting_filepath

This did not work before without explicitly converting the Path to a string, but works now.

@Byron
Copy link
Member

Byron commented Dec 14, 2025

Thanks a lot, this looks very good to me!

It's a bit sad that we have some test duplication to validate various types, but I guess it's better than not having them.
I will merge unless Copilot comes up with something more than nits that I can just apply.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the Tree.join and Tree.__truediv__ API to support pathlike objects (such as pathlib.Path), enabling more Pythonic path handling when traversing Git tree objects. Previously, users had to explicitly convert Path objects to strings; now they can use them directly with the / operator.

  • Type annotations updated from str to PathLike for both methods
  • Implementation uses os.fspath() to convert PathLike objects to strings
  • Comprehensive test coverage added for PathLike support with various scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
git/objects/tree.py Updated join() and __truediv__() methods to accept PathLike parameters; added os.fspath() conversion; imported os module
test/test_tree.py Added comprehensive test cases for string and PathLike path lookups covering simple, nested, folder, and invalid path scenarios; imported pytest, Repo, and PathLikeMock

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 212 to 221
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"
Copy link

Copilot AI Dec 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable is named blob but is actually a Tree object (as confirmed by the assertion isinstance(blob, Tree)). Consider renaming it to tree or result for clarity.

Suggested change
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"
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"

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Rename blob to tree

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants