Adding git_filter_init for initializing `git_filter` struct + unit test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index 8c6d7a5..6d575d4 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -272,6 +272,17 @@ struct git_filter {
#define GIT_FILTER_VERSION 1
#define GIT_FILTER_INIT {GIT_FILTER_VERSION}
+
+/**
+ * Initializes a `git_filter` with default values. Equivalent to
+ * creating an instance with GIT_FILTER_INIT.
+ *
+ * @param filter the `git_filter` struct to initialize.
+ * @param version Version the struct; pass `GIT_FILTER_VERSION`
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_filter_init(git_filter *filter, unsigned int version);
+
/**
* Register a filter under a given name with a given priority.
*
diff --git a/src/filter.c b/src/filter.c
index e74cc10..361e085 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -895,7 +895,7 @@ static int stream_list_init(
git_array_size(filters->filters) - 1 - i : i;
git_filter_entry *fe = git_array_get(filters->filters, filter_idx);
git_writestream *filter_stream;
-
+
assert(fe->filter->stream || fe->filter->apply);
/* If necessary, create a stream that proxies the traditional
@@ -1022,3 +1022,9 @@ int git_filter_list_stream_blob(
return git_filter_list_stream_data(filters, &in, target);
}
+
+int git_filter_init(git_filter *filter, unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT);
+ return 0;
+}
diff --git a/tests/core/structinit.c b/tests/core/structinit.c
index e9f7b4a..75c3ef6 100644
--- a/tests/core/structinit.c
+++ b/tests/core/structinit.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include <git2/sys/config.h>
+#include <git2/sys/filter.h>
#include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h>
#include <git2/sys/transport.h>
@@ -96,6 +97,11 @@ void test_core_structinit__compare(void)
git_diff_find_options, GIT_DIFF_FIND_OPTIONS_VERSION, \
GIT_DIFF_FIND_OPTIONS_INIT, git_diff_find_init_options);
+ /* filter */
+ CHECK_MACRO_FUNC_INIT_EQUAL( \
+ git_filter, GIT_FILTER_VERSION, \
+ GIT_FILTER_INIT, git_filter_init);
+
/* merge_file_input */
CHECK_MACRO_FUNC_INIT_EQUAL( \
git_merge_file_input, GIT_MERGE_FILE_INPUT_VERSION, \