blob: introduce git_blob_filter Provide a function to filter blobs that allows for more functionality than the existing `git_blob_filtered_content` function.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
diff --git a/include/git2/blob.h b/include/git2/blob.h
index 072522d..be721ed 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -97,6 +97,27 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
/**
+ * Flags to control the functionality of `git_blob_filter`.
+ */
+typedef enum {
+ /** When set, filters will not be applied to binary files. */
+ GIT_BLOB_FILTER_CHECK_FOR_BINARY = (1 << 0),
+} git_blob_filter_flag_t;
+
+/**
+ * The options used when applying filter options to a file.
+ */
+typedef struct {
+ int version;
+
+ /** Flags to control the filtering process */
+ git_blob_filter_flag_t flags;
+} git_blob_filter_options;
+
+#define GIT_BLOB_FILTER_OPTIONS_VERSION 1
+#define GIT_BLOB_FILTER_OPTIONS_INIT {GIT_BLOB_FILTER_OPTIONS_VERSION, GIT_BLOB_FILTER_CHECK_FOR_BINARY}
+
+/**
* Get a buffer with the filtered content of a blob.
*
* This applies filters as if the blob was being checked out to the
@@ -115,6 +136,24 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
* @param out The git_buf to be filled in
* @param blob Pointer to the blob
* @param as_path Path used for file attribute lookups, etc.
+ * @param opts Options to use for filtering the blob
+ * @return 0 on success or an error code
+ */
+GIT_EXTERN(int) git_blob_filter(
+ git_buf *out,
+ git_blob *blob,
+ const char *as_path,
+ git_blob_filter_options *opts);
+
+/**
+ * Get a buffer with the filtered content of a blob. This is
+ * equivalent to calling `git_blob_filter`, with the only possible
+ * option being the binary check.
+ *
+ * @see git_blob_filter
+ * @param out The git_buf to be filled in
+ * @param blob Pointer to the blob
+ * @param as_path Path used for file attribute lookups, etc.
* @param check_for_binary_data Should this test if blob content contains
* NUL bytes / looks like binary data before applying filters?
* @return 0 on success or an error code
diff --git a/src/blob.c b/src/blob.c
index 31e6ccf..efc9fc8 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -400,25 +400,34 @@ int git_blob_is_binary(const git_blob *blob)
return git_buf_text_is_binary(&content);
}
-int git_blob_filtered_content(
+int git_blob_filter(
git_buf *out,
git_blob *blob,
const char *path,
- int check_for_binary_data)
+ git_blob_filter_options *given_opts)
{
int error = 0;
git_filter_list *fl = NULL;
+ git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
+ git_filter_flag_t flags = GIT_FILTER_DEFAULT;
assert(blob && path && out);
git_buf_sanitize(out);
- if (check_for_binary_data && git_blob_is_binary(blob))
+ GIT_ERROR_CHECK_VERSION(
+ given_opts, GIT_BLOB_FILTER_OPTIONS_VERSION, "git_blob_filter_options");
+
+ if (given_opts != NULL)
+ memcpy(&opts, given_opts, sizeof(git_blob_filter_options));
+
+ if ((opts.flags & GIT_BLOB_FILTER_CHECK_FOR_BINARY) != 0 &&
+ git_blob_is_binary(blob))
return 0;
if (!(error = git_filter_list_load(
&fl, git_blob_owner(blob), blob, path,
- GIT_FILTER_TO_WORKTREE, GIT_FILTER_DEFAULT))) {
+ GIT_FILTER_TO_WORKTREE, flags))) {
error = git_filter_list_apply_to_blob(out, fl, blob);
@@ -428,6 +437,22 @@ int git_blob_filtered_content(
return error;
}
+int git_blob_filtered_content(
+ git_buf *out,
+ git_blob *blob,
+ const char *path,
+ int check_for_binary_data)
+{
+ git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
+
+ if (check_for_binary_data)
+ opts.flags |= GIT_BLOB_FILTER_CHECK_FOR_BINARY;
+ else
+ opts.flags &= ~GIT_BLOB_FILTER_CHECK_FOR_BINARY;
+
+ return git_blob_filter(out, blob, path, &opts);
+}
+
/* Deprecated functions */
int git_blob_create_frombuffer(