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

Appearance settings

Commit ed45d5b

Browse filesBrowse files
committed
Fix blob filter path shorter than filter path
1 parent 420d2af commit ed45d5b
Copy full SHA for ed45d5b

2 files changed

+37-2Lines changed: 37 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎git/index/typ.py‎

Copy file name to clipboardExpand all lines: git/index/typ.py
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# typing ----------------------------------------------------------------------
1111

12-
from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast
12+
from typing import NamedTuple, Sequence, TYPE_CHECKING, Tuple, Union, cast, List
1313

1414
from git.types import PathLike
1515

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

Collapse file

‎test/test_blob_filter.py‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Test the blob filter."""
2+
from pathlib import Path
3+
from typing import Sequence, Tuple
4+
from unittest.mock import MagicMock
5+
6+
import pytest
7+
8+
from git.index.typ import BlobFilter, StageType
9+
from git.objects import Blob
10+
from git.types import PathLike
11+
12+
13+
# fmt: off
14+
@pytest.mark.parametrize('paths, stage_type, path, expected_result', [
15+
((Path("foo"),), 0, Path("foo"), True),
16+
((Path("foo"),), 0, Path("foo/bar"), True),
17+
((Path("foo/bar"),), 0, Path("foo"), False),
18+
((Path("foo"), Path("bar")), 0, Path("foo"), True),
19+
])
20+
# fmt: on
21+
def test_blob_filter(paths: Sequence[PathLike], stage_type: StageType, path: PathLike, expected_result: bool) -> None:
22+
"""Test the blob filter."""
23+
blob_filter = BlobFilter(paths)
24+
25+
binsha = MagicMock(__len__=lambda self: 20)
26+
blob: Blob = Blob(repo=MagicMock(), binsha=binsha, path=path)
27+
stage_blob: Tuple[StageType, Blob] = (stage_type, blob)
28+
29+
result = blob_filter(stage_blob)
30+
31+
assert result == expected_result

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.