Skip to content

Fix blob filter types #1459

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions git/index/typ.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# typing ----------------------------------------------------------------------

from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast
from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast, List

from git.types import PathLike

Expand Down Expand Up @@ -57,7 +57,11 @@ def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool:
for pathlike in self.paths:
path: Path = pathlike if isinstance(pathlike, Path) else Path(pathlike)
# TODO: Change to use `PosixPath.is_relative_to` once Python 3.8 is no longer supported.
if all(i == j for i, j in zip(path.parts, blob_path.parts)):
filter_parts: List[str] = path.parts
blob_parts: List[str] = blob_path.parts
if len(filter_parts) > len(blob_parts):
continue
if all(i == j for i, j in zip(filter_parts, blob_parts)):
return True
return False

Expand Down
31 changes: 31 additions & 0 deletions test/test_blob_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Test the blob filter."""
from pathlib import Path
from typing import Sequence, Tuple
from unittest.mock import MagicMock

import pytest

from git.index.typ import BlobFilter, StageType
from git.objects import Blob
from git.types import PathLike


# fmt: off
@pytest.mark.parametrize('paths, stage_type, path, expected_result', [
((Path("foo"),), 0, Path("foo"), True),
((Path("foo"),), 0, Path("foo/bar"), True),
((Path("foo/bar"),), 0, Path("foo"), False),
((Path("foo"), Path("bar")), 0, Path("foo"), True),
])
# fmt: on
def test_blob_filter(paths: Sequence[PathLike], stage_type: StageType, path: PathLike, expected_result: bool) -> None:
"""Test the blob filter."""
blob_filter = BlobFilter(paths)

binsha = MagicMock(__len__=lambda self: 20)
blob: Blob = Blob(repo=MagicMock(), binsha=binsha, path=path)
stage_blob: Tuple[StageType, Blob] = (stage_type, blob)

result = blob_filter(stage_blob)

assert result == expected_result