Skip to content

Commit ee7252f

Browse files
committed
Add enums BlobFilter, FilterFlag, FilterMode
1 parent 78cab58 commit ee7252f

File tree

7 files changed

+73
-35
lines changed

7 files changed

+73
-35
lines changed

docs/filters.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ setting.
3838
self.buffer = io.BytesIO()
3939
4040
def check(self, src, attr_values):
41-
if src.mode == GIT_FILTER_SMUDGE:
41+
if src.mode == pygit2.enums.FilterMode.SMUDGE:
4242
# attr_values contains the values of the 'text' and 'eol'
4343
# attributes in that order (as they are defined in
4444
# CRLFFilter.attributes
@@ -48,7 +48,7 @@ setting.
4848
self.linesep = b'\r\n'
4949
elif eol == 'lf':
5050
self.linesep = b'\n'
51-
else: # src.mode == GIT_FILTER_CLEAN
51+
else: # src.mode == pygit2.enums.FilterMode.CLEAN
5252
# always use LF line-endings when writing to the ODB
5353
self.linesep = b'\n'
5454

pygit2/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
AttrCheck,
4444
ApplyLocation,
4545
BlameFlag,
46+
BlobFilter,
4647
BranchType,
4748
CheckoutNotify,
4849
CheckoutStrategy,
@@ -55,6 +56,8 @@
5556
Feature,
5657
FetchPrune,
5758
FileMode,
59+
FilterFlag,
60+
FilterMode,
5861
ObjectType,
5962
Option,
6063
MergeAnalysis,

pygit2/blob.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from typing import Optional
66
from queue import Queue
77

8-
from ._pygit2 import GIT_BLOB_FILTER_CHECK_FOR_BINARY, Blob, Oid
8+
from ._pygit2 import Blob, Oid
9+
from .enums import BlobFilter
910

1011

1112
class _BlobIO(io.RawIOBase):
@@ -20,7 +21,7 @@ def __init__(
2021
self,
2122
blob: Blob,
2223
as_path: Optional[str] = None,
23-
flags: int = GIT_BLOB_FILTER_CHECK_FOR_BINARY,
24+
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
2425
commit_id: Optional[Oid] = None,
2526
):
2627
super().__init__()
@@ -34,7 +35,7 @@ def __init__(
3435
args=(self._queue, self._ready, self._writer_closed),
3536
kwargs={
3637
"as_path": as_path,
37-
"flags": flags,
38+
"flags": int(flags),
3839
"commit_id": commit_id,
3940
},
4041
daemon=True,
@@ -127,7 +128,7 @@ def __init__(
127128
self,
128129
blob: Blob,
129130
as_path: Optional[str] = None,
130-
flags: int = GIT_BLOB_FILTER_CHECK_FOR_BINARY,
131+
flags: BlobFilter = BlobFilter.CHECK_FOR_BINARY,
131132
commit_id: Optional[Oid] = None,
132133
):
133134
"""Wrap the specified blob.
@@ -137,10 +138,10 @@ def __init__(
137138
as_path: Filter the contents of the blob as if it had the specified
138139
path. If `as_path` is None, the raw contents of the blob will
139140
be read.
140-
flags: GIT_BLOB_FILTER_* bitflags (only applicable when `as_path`
141-
is set).
142-
commit_oid: Commit to load attributes from when
143-
GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT is specified in `flags`
141+
flags: A combination of enums.BlobFilter constants
142+
(only applicable when `as_path` is set).
143+
commit_id: Commit to load attributes from when
144+
ATTRIBUTES_FROM_COMMIT is specified in `flags`
144145
(only applicable when `as_path` is set).
145146
"""
146147
raw = _BlobIO(blob, as_path=as_path, flags=flags, commit_id=commit_id)

pygit2/enums.py

+46
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ class BlameFlag(IntFlag):
9090
"Ignore whitespace differences"
9191

9292

93+
class BlobFilter(IntFlag):
94+
CHECK_FOR_BINARY = _pygit2.GIT_BLOB_FILTER_CHECK_FOR_BINARY
95+
"Do not apply filters to binary files."
96+
97+
NO_SYSTEM_ATTRIBUTES = _pygit2.GIT_BLOB_FILTER_NO_SYSTEM_ATTRIBUTES
98+
"Filters will not load configuration from the system-wide `gitattributes` in `/etc` (or system equivalent)."
99+
100+
ATTRIBUTES_FROM_HEAD = _pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD
101+
"Load filters from a `.gitattributes` file in the HEAD commit."
102+
103+
ATTRIBUTES_FROM_COMMIT = _pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT
104+
"Load filters from a `.gitattributes` file in the specified commit."
105+
106+
93107
class BranchType(IntFlag):
94108
LOCAL = _pygit2.GIT_BRANCH_LOCAL
95109
REMOTE = _pygit2.GIT_BRANCH_REMOTE
@@ -656,6 +670,38 @@ class FileStatus(IntFlag):
656670
CONFLICTED = _pygit2.GIT_STATUS_CONFLICTED
657671

658672

673+
class FilterFlag(IntFlag):
674+
""" Filter option flags. """
675+
676+
DEFAULT = _pygit2.GIT_FILTER_DEFAULT
677+
678+
ALLOW_UNSAFE = _pygit2.GIT_FILTER_ALLOW_UNSAFE
679+
"Don't error for `safecrlf` violations, allow them to continue."
680+
681+
NO_SYSTEM_ATTRIBUTES = _pygit2.GIT_FILTER_NO_SYSTEM_ATTRIBUTES
682+
"Don't load `/etc/gitattributes` (or the system equivalent)"
683+
684+
ATTRIBUTES_FROM_HEAD = _pygit2.GIT_FILTER_ATTRIBUTES_FROM_HEAD
685+
"Load attributes from `.gitattributes` in the root of HEAD"
686+
687+
ATTRIBUTES_FROM_COMMIT = _pygit2.GIT_FILTER_ATTRIBUTES_FROM_COMMIT
688+
"Load attributes from `.gitattributes` in a given commit. This can only be specified in a `git_filter_options`."
689+
690+
691+
class FilterMode(IntEnum):
692+
"""
693+
Filters are applied in one of two directions: smudging - which is
694+
exporting a file from the Git object database to the working directory,
695+
and cleaning - which is importing a file from the working directory to
696+
the Git object database. These values control which direction of
697+
change is being applied.
698+
"""
699+
TO_WORKTREE = _pygit2.GIT_FILTER_TO_WORKTREE
700+
SMUDGE = _pygit2.GIT_FILTER_SMUDGE
701+
TO_ODB = _pygit2.GIT_FILTER_TO_ODB
702+
CLEAN = _pygit2.GIT_FILTER_CLEAN
703+
704+
659705
class MergeAnalysis(IntFlag):
660706
""" The results of `Repository.merge_analysis` indicate the merge opportunities. """
661707

src/blob.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static void blob_filter_stream_free(git_writestream *s)
226226

227227

228228
PyDoc_STRVAR(Blob__write_to_queue__doc__,
229-
"_write_to_queue(queue: queue.Queue, closed: threading.Event, chunk_size: int = io.DEFAULT_BUFFER_SIZE, [as_path: str = None, flags: int = GIT_BLOB_FILTER_CHECK_FOR_BINARY, commit_id: oid = None]) -> None\n"
229+
"_write_to_queue(queue: queue.Queue, closed: threading.Event, chunk_size: int = io.DEFAULT_BUFFER_SIZE, [as_path: str = None, flags: enums.BlobFilter = enums.BlobFilter.CHECK_FOR_BINARY, commit_id: oid = None]) -> None\n"
230230
"\n"
231231
"Write the contents of the blob in chunks to `queue`.\n"
232232
"If `as_path` is None, the raw contents of blob will be written to the queue,\n"
@@ -261,13 +261,12 @@ PyDoc_STRVAR(Blob__write_to_queue__doc__,
261261
" When set, the blob contents will be filtered as if it had this\n"
262262
" filename (used for attribute lookups).\n"
263263
"\n"
264-
"flags : int\n"
265-
" GIT_BLOB_FILTER_* bitflags (only applicable when `as_path` is set).\n"
264+
"flags : enums.BlobFilter\n"
265+
" A combination of BlobFilter constants (only applicable when `as_path` is set).\n"
266266
"\n"
267267
"commit_id : oid\n"
268-
" Commit to load attributes from when\n"
269-
" GIT_BLOB_FILTER_ATTRIBUTES_FROM_COMMIT is specified in `flags`\n"
270-
" (only applicable when `as_path` is set).\n");
268+
" Commit to load attributes from when ATTRIBUTES_FROM_COMMIT is\n"
269+
" specified in `flags` (only applicable when `as_path` is set).\n");
271270

272271
PyObject *
273272
Blob__write_to_queue(Blob *self, PyObject *args, PyObject *kwds)

src/filter.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ FilterSource_filemode__get__(FilterSource *self)
8585

8686
PyDoc_STRVAR(FilterSource_oid__doc__,
8787
"Oid of the source object. If the oid is unknown "
88-
"(often the case with GIT_FILTER_CLEAN) then `oid` will be None.\n");
88+
"(often the case with FilterMode.CLEAN) then `oid` will be None.\n");
8989
PyObject *
9090
FilterSource_oid__get__(FilterSource *self)
9191
{
@@ -96,7 +96,7 @@ FilterSource_oid__get__(FilterSource *self)
9696
}
9797

9898
PyDoc_STRVAR(FilterSource_mode__doc__,
99-
"Filter mode (either GIT_FILTER_CLEAN or GIT_FILTER_SMUDGE).\n");
99+
"Filter mode (either FilterMode.CLEAN or FilterMode.SMUDGE).\n");
100100

101101
PyObject *
102102
FilterSource_mode__get__(FilterSource *self)
@@ -105,7 +105,7 @@ FilterSource_mode__get__(FilterSource *self)
105105
}
106106

107107
PyDoc_STRVAR(FilterSource_flags__doc__,
108-
"GIT_FILTER_* flags to be applied to the data.\n");
108+
"A combination of filter flags (enums.FilterFlag) to be applied to the data.\n");
109109

110110
PyObject *
111111
FilterSource_flags__get__(FilterSource *self)

test/test_filter.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import pygit2
6+
from pygit2 import BlobFilter
67
from pygit2.errors import Passthrough
78

89

@@ -77,10 +78,7 @@ def unmatched_filter():
7778
def test_filter(testrepo, rot13_filter):
7879
blob_oid = testrepo.create_blob_fromworkdir("bye.txt")
7980
blob = testrepo[blob_oid]
80-
flags = (
81-
pygit2.GIT_BLOB_FILTER_CHECK_FOR_BINARY
82-
| pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD
83-
)
81+
flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
8482
assert b'olr jbeyq\n' == blob.data
8583
with pygit2.BlobIO(blob) as reader:
8684
assert b'olr jbeyq\n' == reader.read()
@@ -91,10 +89,7 @@ def test_filter(testrepo, rot13_filter):
9189
def test_filter_buffered(testrepo, buffered_filter):
9290
blob_oid = testrepo.create_blob_fromworkdir("bye.txt")
9391
blob = testrepo[blob_oid]
94-
flags = (
95-
pygit2.GIT_BLOB_FILTER_CHECK_FOR_BINARY
96-
| pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD
97-
)
92+
flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
9893
assert b'olr jbeyq\n' == blob.data
9994
with pygit2.BlobIO(blob) as reader:
10095
assert b'olr jbeyq\n' == reader.read()
@@ -105,10 +100,7 @@ def test_filter_buffered(testrepo, buffered_filter):
105100
def test_filter_passthrough(testrepo, passthrough_filter):
106101
blob_oid = testrepo.create_blob_fromworkdir("bye.txt")
107102
blob = testrepo[blob_oid]
108-
flags = (
109-
pygit2.GIT_BLOB_FILTER_CHECK_FOR_BINARY
110-
| pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD
111-
)
103+
flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
112104
assert b'bye world\n' == blob.data
113105
with pygit2.BlobIO(blob) as reader:
114106
assert b'bye world\n' == reader.read()
@@ -119,10 +111,7 @@ def test_filter_passthrough(testrepo, passthrough_filter):
119111
def test_filter_unmatched(testrepo, unmatched_filter):
120112
blob_oid = testrepo.create_blob_fromworkdir("bye.txt")
121113
blob = testrepo[blob_oid]
122-
flags = (
123-
pygit2.GIT_BLOB_FILTER_CHECK_FOR_BINARY
124-
| pygit2.GIT_BLOB_FILTER_ATTRIBUTES_FROM_HEAD
125-
)
114+
flags = BlobFilter.CHECK_FOR_BINARY | BlobFilter.ATTRIBUTES_FROM_HEAD
126115
assert b'bye world\n' == blob.data
127116
with pygit2.BlobIO(blob) as reader:
128117
assert b'bye world\n' == reader.read()

0 commit comments

Comments
 (0)