1 #include "clar_libgit2.h"
5 #include "git2/sys/filter.h"
6 #include "git2/sys/repository.h"
7 #include "custom_helpers.h"
9 static git_repository
*g_repo
= NULL
;
11 static git_filter
*create_wildcard_filter(void);
15 static unsigned char input
[] = {
16 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
17 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
18 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
19 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
22 static unsigned char reversed
[] = {
23 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
24 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
25 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
26 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
29 static unsigned char flipped
[] = {
30 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
31 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
32 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8,
33 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0,
36 void test_filter_wildcard__initialize(void)
38 cl_git_pass(git_filter_register(
39 "wildcard", create_wildcard_filter(), GIT_FILTER_DRIVER_PRIORITY
));
41 g_repo
= cl_git_sandbox_init("empty_standard_repo");
44 "empty_standard_repo/.gitattributes",
46 "hero-flip-* filter=wcflip\n"
47 "hero-reverse-* filter=wcreverse\n"
48 "none-* filter=unregistered\n");
51 void test_filter_wildcard__cleanup(void)
53 cl_git_pass(git_filter_unregister("wildcard"));
55 cl_git_sandbox_cleanup();
59 static int wildcard_filter_check(
62 const git_filter_source
*src
,
63 const char **attr_values
)
68 if (strcmp(attr_values
[0], "wcflip") == 0 ||
69 strcmp(attr_values
[0], "wcreverse") == 0) {
70 *payload
= git__strdup(attr_values
[0]);
71 GIT_ERROR_CHECK_ALLOC(*payload
);
75 return GIT_PASSTHROUGH
;
78 static int wildcard_filter_apply(
83 const git_filter_source
*source
)
85 const char *filtername
= *payload
;
87 if (filtername
&& strcmp(filtername
, "wcflip") == 0)
88 return bitflip_filter_apply(self
, payload
, to
, from
, source
);
89 else if (filtername
&& strcmp(filtername
, "wcreverse") == 0)
90 return reverse_filter_apply(self
, payload
, to
, from
, source
);
92 cl_fail("Unexpected attribute");
93 return GIT_PASSTHROUGH
;
96 static int wildcard_filter_stream(
97 git_writestream
**out
,
100 const git_filter_source
*src
,
101 git_writestream
*next
)
103 return git_filter_buffered_stream_new(out
,
104 self
, wildcard_filter_apply
, NULL
, payload
, src
, next
);
107 static void wildcard_filter_cleanup(git_filter
*self
, void *payload
)
113 static void wildcard_filter_free(git_filter
*f
)
118 static git_filter
*create_wildcard_filter(void)
120 git_filter
*filter
= git__calloc(1, sizeof(git_filter
));
123 filter
->version
= GIT_FILTER_VERSION
;
124 filter
->attributes
= "filter=*";
125 filter
->check
= wildcard_filter_check
;
126 filter
->stream
= wildcard_filter_stream
;
127 filter
->cleanup
= wildcard_filter_cleanup
;
128 filter
->shutdown
= wildcard_filter_free
;
133 void test_filter_wildcard__reverse(void)
136 git_buf out
= GIT_BUF_INIT
;
138 cl_git_pass(git_filter_list_load(
139 &fl
, g_repo
, NULL
, "hero-reverse-foo", GIT_FILTER_TO_ODB
, 0));
141 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, (char *)input
, DATA_LEN
));
143 cl_assert_equal_i(DATA_LEN
, out
.size
);
146 0, memcmp(reversed
, out
.ptr
, out
.size
));
148 git_filter_list_free(fl
);
149 git_buf_dispose(&out
);
152 void test_filter_wildcard__flip(void)
155 git_buf out
= GIT_BUF_INIT
;
157 cl_git_pass(git_filter_list_load(
158 &fl
, g_repo
, NULL
, "hero-flip-foo", GIT_FILTER_TO_ODB
, 0));
160 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, (char *)input
, DATA_LEN
));
162 cl_assert_equal_i(DATA_LEN
, out
.size
);
165 0, memcmp(flipped
, out
.ptr
, out
.size
));
167 git_filter_list_free(fl
);
168 git_buf_dispose(&out
);
171 void test_filter_wildcard__none(void)
174 git_buf out
= GIT_BUF_INIT
;
176 cl_git_pass(git_filter_list_load(
177 &fl
, g_repo
, NULL
, "none-foo", GIT_FILTER_TO_ODB
, 0));
179 cl_git_pass(git_filter_list_apply_to_buffer(&out
, fl
, (char *)input
, DATA_LEN
));
181 cl_assert_equal_i(DATA_LEN
, out
.size
);
184 0, memcmp(input
, out
.ptr
, out
.size
));
186 git_filter_list_free(fl
);
187 git_buf_dispose(&out
);