From da88a6da599c8bc598abfc00c6802d08be67be39 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Tue, 21 Jun 2022 08:40:09 -0400 Subject: Fix blob filter types Fix the types and type annotations of some of the blob filter code. --- git/index/typ.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index 6371953b..d9040e10 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -4,6 +4,7 @@ from binascii import b2a_hex from .util import pack, unpack from git.objects import Blob +from git.index.base import StageType # typing ---------------------------------------------------------------------- @@ -48,10 +49,10 @@ class BlobFilter(object): """ self.paths = paths - def __call__(self, stage_blob: Blob) -> bool: - path = stage_blob[1].path + def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool: + path: str = str(stage_blob[1].path) for p in self.paths: - if path.startswith(p): + if path.startswith(str(p)): return True # END for each path in filter paths return False -- cgit v1.2.1 From d17136574ef23050fe3271e51a8b811f96544b17 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Tue, 21 Jun 2022 09:19:54 -0400 Subject: Move stage type def --- git/index/typ.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index d9040e10..5264f370 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -4,7 +4,6 @@ from binascii import b2a_hex from .util import pack, unpack from git.objects import Blob -from git.index.base import StageType # typing ---------------------------------------------------------------------- @@ -16,9 +15,11 @@ 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 -- cgit v1.2.1 From 1314d6356d598d53175b62351d732f5e1fbf8665 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Sun, 26 Jun 2022 16:45:22 -0400 Subject: Change to not stringify paths --- git/index/typ.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index 5264f370..ad1a6973 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -1,6 +1,7 @@ """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 @@ -51,11 +52,12 @@ class BlobFilter(object): self.paths = paths def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool: - path: str = str(stage_blob[1].path) - for p in self.paths: - if path.startswith(str(p)): + 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) + if path.is_relative_to(blob_path): return True - # END for each path in filter paths return False -- cgit v1.2.1 From c6a018bdf802326f89e8c93ccac79fae78d94974 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Sun, 26 Jun 2022 16:47:41 -0400 Subject: Fix pathlike type annotation typo --- git/index/typ.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index ad1a6973..7f5dcc10 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -52,7 +52,7 @@ class BlobFilter(object): self.paths = paths def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool: - blob_pathlike: Pathlike = stage_blob[1].path + 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) -- cgit v1.2.1 From cc80e6bbb0b98d71f44846d66a8e9439b60efa42 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Sun, 26 Jun 2022 17:01:32 -0400 Subject: Remove usage of `PosixPath.is_relative_to` Remove usage of `PosixPath.is_relative_to` because it was added in Python 3.9 and earlier versions of Python are supported by `GitPython`. --- git/index/typ.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index 7f5dcc10..11077454 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -56,7 +56,8 @@ class BlobFilter(object): 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) - if path.is_relative_to(blob_path): + # TODO: Change to use `PosixPath.is_relative_to` once Python 3.8 is no longer supported. + if all(map(lambda t: t[0] == t[1], zip(path.parts, blob_path.parts))): return True return False -- cgit v1.2.1 From 420d2afa0ebfa976a07dc0ff902c75547ab228c6 Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Sun, 26 Jun 2022 17:04:34 -0400 Subject: Use generator instead of map --- git/index/typ.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index 11077454..ec7699e2 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -57,7 +57,7 @@ class BlobFilter(object): 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(map(lambda t: t[0] == t[1], zip(path.parts, blob_path.parts))): + if all(i == j for i, j in zip(path.parts, blob_path.parts)): return True return False -- cgit v1.2.1 From ed45d5b3c3d4e2d4520221037b40288dc85e428c Mon Sep 17 00:00:00 2001 From: Austin Scola Date: Tue, 28 Jun 2022 08:59:26 -0400 Subject: Fix blob filter path shorter than filter path --- git/index/typ.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'git/index/typ.py') diff --git a/git/index/typ.py b/git/index/typ.py index ec7699e2..b2c6c371 100644 --- a/git/index/typ.py +++ b/git/index/typ.py @@ -9,7 +9,7 @@ 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 @@ -57,7 +57,11 @@ class BlobFilter(object): 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 -- cgit v1.2.1