Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JoinPathlike Object to Tree#2094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Byron merged 3 commits intogitpython-developers:mainfromGeorge-Ogden:join-pathlike
Dec 15, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsgit/objects/tree.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,6 +5,7 @@

__all__ = ["TreeModifier", "Tree"]

import os
import sys

import git.diff as git_diff
Expand DownExpand 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 DownExpand 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 deletionstest/test_tree.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2026 Movatter.jp