Skip to content

Commit 4b5a6c6

Browse files
committed
filter: add API for registering Python filters
1 parent 5dda191 commit 4b5a6c6

File tree

7 files changed

+689
-1
lines changed

7 files changed

+689
-1
lines changed

pygit2/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .credentials import *
3838
from .errors import check_error, Passthrough
3939
from .ffi import ffi, C
40+
from .filter import Filter, FilterSource, filter_register, filter_unregister
4041
from .index import Index, IndexEntry
4142
from .remote import Remote
4243
from .repository import Repository
@@ -117,6 +118,18 @@
117118
GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED : int = C.GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED
118119
GIT_STASH_APPLY_PROGRESS_DONE : int = C.GIT_STASH_APPLY_PROGRESS_DONE
119120

121+
# GIT_FILTER
122+
GIT_FILTER_TO_WORKTREE : int = C.GIT_FILTER_TO_WORKTREE
123+
GIT_FILTER_SMUDGE : int = C.GIT_FILTER_SMUDGE
124+
GIT_FILTER_TO_ODB : int = C.GIT_FILTER_TO_ODB
125+
GIT_FILTER_CLEAN : int = C.GIT_FILTER_CLEAN
126+
GIT_FILTER_DRIVER_PRIORITY : int = C.GIT_FILTER_DRIVER_PRIORITY
127+
GIT_FILTER_DEFAULT : int = C.GIT_FILTER_DEFAULT
128+
GIT_FILTER_ALLOW_UNSAFE : int = C.GIT_FILTER_ALLOW_UNSAFE
129+
GIT_FILTER_NO_SYSTEM_ATTRIBUTES : int = C.GIT_FILTER_NO_SYSTEM_ATTRIBUTES
130+
GIT_FILTER_ATTRIBUTES_FROM_HEAD : int = C.GIT_FILTER_ATTRIBUTES_FROM_HEAD
131+
GIT_FILTER_ATTRIBUTES_FROM_COMMIT : int = C.GIT_FILTER_ATTRIBUTES_FROM_COMMIT
132+
120133
# libgit version tuple
121134
LIBGIT2_VER = (LIBGIT2_VER_MAJOR, LIBGIT2_VER_MINOR, LIBGIT2_VER_REVISION)
122135

pygit2/_run.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
'revert.h',
8282
'stash.h',
8383
'submodule.h',
84+
'filter.h',
8485
'callbacks.h', # Bridge from libgit2 to Python
8586
]
8687
h_source = []
@@ -96,7 +97,7 @@
9697
ffi = FFI()
9798
ffi.set_source(
9899
"pygit2._libgit2",
99-
"#include <git2.h>", # preamble
100+
"#include <git2.h>\n#include <git2/sys/filter.h>", # preamble
100101
**libgit2_kw
101102
)
102103
ffi.cdef(C_HEADER_SRC)

pygit2/decl/callbacks.h

+28
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,31 @@ extern "Python" void _checkout_progress_cb(
6565
extern "Python" int _stash_apply_progress_cb(
6666
git_stash_apply_progress_t progress,
6767
void *payload);
68+
69+
extern "Python" void _filter_shutdown_cb(git_filter *self);
70+
71+
extern "Python" int _filter_check_cb(
72+
git_filter *self,
73+
void **payload,
74+
const git_filter_source *src,
75+
const char **attr_values);
76+
77+
extern "Python" int _filter_stream_cb(
78+
git_writestream **out,
79+
git_filter *self,
80+
void **payload,
81+
const git_filter_source *src,
82+
git_writestream *next);
83+
84+
extern "Python" void _filter_cleanup_cb(
85+
git_filter *self,
86+
void *payload);
87+
88+
extern "Python" int _writestream_write_cb(
89+
git_writestream *stream,
90+
const char *buffer,
91+
size_t len);
92+
93+
extern "Python" int _writestream_close_cb(git_writestream *stream);
94+
95+
extern "Python" void _writestream_free_cb(git_writestream *stream);

pygit2/decl/filter.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
typedef enum {
2+
GIT_FILTER_TO_WORKTREE = 0,
3+
GIT_FILTER_SMUDGE = GIT_FILTER_TO_WORKTREE,
4+
GIT_FILTER_TO_ODB = 1,
5+
GIT_FILTER_CLEAN = GIT_FILTER_TO_ODB
6+
} git_filter_mode_t;
7+
8+
typedef enum {
9+
GIT_FILTER_DEFAULT = 0u,
10+
GIT_FILTER_ALLOW_UNSAFE = (1u << 0),
11+
GIT_FILTER_NO_SYSTEM_ATTRIBUTES = (1u << 1),
12+
GIT_FILTER_ATTRIBUTES_FROM_HEAD = (1u << 2),
13+
GIT_FILTER_ATTRIBUTES_FROM_COMMIT = (1u << 3)
14+
} git_filter_flag_t;
15+
16+
17+
18+
#define GIT_FILTER_VERSION ...
19+
20+
#define GIT_FILTER_DRIVER_PRIORITY 200
21+
22+
typedef struct git_filter_source git_filter_source;
23+
typedef struct git_filter git_filter;
24+
25+
typedef int (*git_filter_init_fn)(git_filter *self);
26+
typedef void (*git_filter_shutdown_fn)(git_filter *self);
27+
typedef int (*git_filter_check_fn)(
28+
git_filter *self,
29+
void **payload, /* NULL on entry, may be set */
30+
const git_filter_source *src,
31+
const char **attr_values);
32+
typedef int (*git_filter_apply_fn)(
33+
git_filter *self,
34+
void **payload, /* may be read and/or set */
35+
git_buf *to,
36+
const git_buf *from,
37+
const git_filter_source *src);
38+
typedef int (*git_filter_stream_fn)(
39+
git_writestream **out,
40+
git_filter *self,
41+
void **payload,
42+
const git_filter_source *src,
43+
git_writestream *next);
44+
typedef void (*git_filter_cleanup_fn)(
45+
git_filter *self,
46+
void *payload);
47+
48+
struct git_filter {
49+
unsigned int version;
50+
const char *attributes;
51+
git_filter_init_fn initialize;
52+
git_filter_shutdown_fn shutdown;
53+
git_filter_check_fn check;
54+
git_filter_apply_fn apply; /* deprecated in favor of stream */
55+
git_filter_stream_fn stream;
56+
git_filter_cleanup_fn cleanup;
57+
};
58+
59+
int git_filter_init(git_filter *filter, unsigned int version);
60+
int git_filter_register(
61+
const char *name, git_filter *filter, int priority);
62+
int git_filter_unregister(const char *name);
63+
const char *git_filter_source_path(const git_filter_source *src);
64+
uint16_t git_filter_source_filemode(const git_filter_source *src);
65+
const git_oid *git_filter_source_id(const git_filter_source *src);
66+
git_filter_mode_t git_filter_source_mode(const git_filter_source *src);
67+
uint32_t git_filter_source_flags(const git_filter_source *src);

pygit2/decl/types.h

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ typedef struct git_submodule git_submodule;
1212
typedef struct git_transport git_transport;
1313
typedef struct git_tree git_tree;
1414
typedef struct git_packbuilder git_packbuilder;
15+
typedef struct git_writestream git_writestream;
16+
17+
struct git_writestream {
18+
int (*write)(git_writestream *stream, const char *buffer, size_t len);
19+
int (*close)(git_writestream *stream);
20+
void (*free)(git_writestream *stream);
21+
};
1522

1623
typedef int64_t git_off_t;
1724
typedef int64_t git_time_t;

0 commit comments

Comments
 (0)