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 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
4 changes: 2 additions & 2 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
from .typ import (
BaseIndexEntry,
IndexEntry,
StageType,
)
from .util import TemporaryFileSwap, post_clear_cache, default_index, git_working_dir

Expand Down Expand Up @@ -83,13 +84,12 @@
from git.util import Actor


StageType = int
Treeish = Union[Tree, Commit, str, bytes]

# ------------------------------------------------------------------------------------


__all__ = ("IndexFile", "CheckoutError")
__all__ = ("IndexFile", "CheckoutError", "StageType")


class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
Expand Down
23 changes: 16 additions & 7 deletions git/index/typ.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
"""Module with additional types used by the index"""

from binascii import b2a_hex
from pathlib import Path

from .util import pack, unpack
from git.objects import Blob


# 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

if TYPE_CHECKING:
from git.repo import Repo

StageType = int

# ---------------------------------------------------------------------------------

__all__ = ("BlobFilter", "BaseIndexEntry", "IndexEntry")
__all__ = ("BlobFilter", "BaseIndexEntry", "IndexEntry", "StageType")

# { Invariants
CE_NAMEMASK = 0x0FFF
Expand Down Expand Up @@ -48,12 +51,18 @@ def __init__(self, paths: Sequence[PathLike]) -> None:
"""
self.paths = paths

def __call__(self, stage_blob: Blob) -> bool:
path = stage_blob[1].path
for p in self.paths:
if path.startswith(p):
def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool:
blob_pathlike: PathLike = stage_blob[1].path
blob_path: Path = blob_pathlike if isinstance(blob_pathlike, Path) else Path(blob_pathlike)
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.
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
# END for each path in filter paths
return False


Expand Down
32 changes: 32 additions & 0 deletions test/test_blob_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""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, path, expected_result', [
((Path("foo"),), Path("foo"), True),
((Path("foo"),), Path("foo/bar"), True),
((Path("foo/bar"),), Path("foo"), False),
((Path("foo"), Path("bar")), Path("foo"), True),
])
# fmt: on
def test_blob_filter(paths: Sequence[PathLike], path: PathLike, expected_result: bool) -> None:
"""Test the blob filter."""
blob_filter = BlobFilter(paths)

binsha = MagicMock(__len__=lambda self: 20)
stage_type: StageType = 0
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