Skip to content

Commit a32ab07

Browse files
committed
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 parent c0290e2 commit a32ab07

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

include/git2/blob.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
9696
*/
9797
GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
9898

99+
/**
100+
* Flags to control the functionality of `git_blob_filter`.
101+
*/
102+
typedef enum {
103+
/** When set, filters will not be applied to binary files. */
104+
GIT_BLOB_FILTER_CHECK_FOR_BINARY = (1 << 0),
105+
} git_blob_filter_flag_t;
106+
107+
/**
108+
* The options used when applying filter options to a file.
109+
*/
110+
typedef struct {
111+
int version;
112+
113+
/** Flags to control the filtering process */
114+
git_blob_filter_flag_t flags;
115+
} git_blob_filter_options;
116+
117+
#define GIT_BLOB_FILTER_OPTIONS_VERSION 1
118+
#define GIT_BLOB_FILTER_OPTIONS_INIT {GIT_BLOB_FILTER_OPTIONS_VERSION, GIT_BLOB_FILTER_CHECK_FOR_BINARY}
119+
99120
/**
100121
* Get a buffer with the filtered content of a blob.
101122
*
@@ -115,6 +136,24 @@ GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
115136
* @param out The git_buf to be filled in
116137
* @param blob Pointer to the blob
117138
* @param as_path Path used for file attribute lookups, etc.
139+
* @param opts Options to use for filtering the blob
140+
* @return 0 on success or an error code
141+
*/
142+
GIT_EXTERN(int) git_blob_filter(
143+
git_buf *out,
144+
git_blob *blob,
145+
const char *as_path,
146+
git_blob_filter_options *opts);
147+
148+
/**
149+
* Get a buffer with the filtered content of a blob. This is
150+
* equivalent to calling `git_blob_filter`, with the only possible
151+
* option being the binary check.
152+
*
153+
* @see git_blob_filter
154+
* @param out The git_buf to be filled in
155+
* @param blob Pointer to the blob
156+
* @param as_path Path used for file attribute lookups, etc.
118157
* @param check_for_binary_data Should this test if blob content contains
119158
* NUL bytes / looks like binary data before applying filters?
120159
* @return 0 on success or an error code

src/blob.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,25 +400,34 @@ int git_blob_is_binary(const git_blob *blob)
400400
return git_buf_text_is_binary(&content);
401401
}
402402

403-
int git_blob_filtered_content(
403+
int git_blob_filter(
404404
git_buf *out,
405405
git_blob *blob,
406406
const char *path,
407-
int check_for_binary_data)
407+
git_blob_filter_options *given_opts)
408408
{
409409
int error = 0;
410410
git_filter_list *fl = NULL;
411+
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
412+
git_filter_flag_t flags = GIT_FILTER_DEFAULT;
411413

412414
assert(blob && path && out);
413415

414416
git_buf_sanitize(out);
415417

416-
if (check_for_binary_data && git_blob_is_binary(blob))
418+
GIT_ERROR_CHECK_VERSION(
419+
given_opts, GIT_BLOB_FILTER_OPTIONS_VERSION, "git_blob_filter_options");
420+
421+
if (given_opts != NULL)
422+
memcpy(&opts, given_opts, sizeof(git_blob_filter_options));
423+
424+
if ((opts.flags & GIT_BLOB_FILTER_CHECK_FOR_BINARY) != 0 &&
425+
git_blob_is_binary(blob))
417426
return 0;
418427

419428
if (!(error = git_filter_list_load(
420429
&fl, git_blob_owner(blob), blob, path,
421-
GIT_FILTER_TO_WORKTREE, GIT_FILTER_DEFAULT))) {
430+
GIT_FILTER_TO_WORKTREE, flags))) {
422431

423432
error = git_filter_list_apply_to_blob(out, fl, blob);
424433

@@ -428,6 +437,22 @@ int git_blob_filtered_content(
428437
return error;
429438
}
430439

440+
int git_blob_filtered_content(
441+
git_buf *out,
442+
git_blob *blob,
443+
const char *path,
444+
int check_for_binary_data)
445+
{
446+
git_blob_filter_options opts = GIT_BLOB_FILTER_OPTIONS_INIT;
447+
448+
if (check_for_binary_data)
449+
opts.flags |= GIT_BLOB_FILTER_CHECK_FOR_BINARY;
450+
else
451+
opts.flags &= ~GIT_BLOB_FILTER_CHECK_FOR_BINARY;
452+
453+
return git_blob_filter(out, blob, path, &opts);
454+
}
455+
431456
/* Deprecated functions */
432457

433458
int git_blob_create_frombuffer(

0 commit comments

Comments
 (0)