Skip to content

Commit 26846f4

Browse files
committed
filter: remove git_buf sharing in git_filter_list_apply_to_data
The API `git_filter_list_apply_to_data` shares data between its out and in parameters to avoid unnecessarily copying it when there are no filters to apply. However, it does so in a manner that is potentially confusing, leaving both `git_buf`s populated with data. This is risky for end-users who have to know how to deal with this. Instead, we remove this optimization - users who want to avoid unnecessary copies can use the longstanding streaming API or check the filter status before invoking the filters.
1 parent 9869f1e commit 26846f4

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

include/git2/filter.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,6 @@ GIT_EXTERN(int) git_filter_list_contains(
122122
/**
123123
* Apply filter list to a data buffer.
124124
*
125-
* See `git2/buffer.h` for background on `git_buf` objects.
126-
*
127-
* If the `in` buffer holds data allocated by libgit2 (i.e. `in->asize` is
128-
* not zero), then it will be overwritten when applying the filters. If
129-
* not, then it will be left untouched.
130-
*
131-
* If there are no filters to apply (or `filters` is NULL), then the `out`
132-
* buffer will reference the `in` buffer data (with `asize` set to zero)
133-
* instead of allocating data. This keeps allocations to a minimum, but
134-
* it means you have to be careful about freeing the `in` data since `out`
135-
* may be pointing to it!
136-
*
137125
* @param out Buffer to store the result of the filtering
138126
* @param filters A loaded git_filter_list (or NULL)
139127
* @param in Buffer containing the data to filter

src/filter.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -720,31 +720,39 @@ static void buf_stream_init(struct buf_stream *writer, git_buf *target)
720720
git_buf_clear(target);
721721
}
722722

723-
int git_filter_list_apply_to_data(
724-
git_buf *tgt, git_filter_list *filters, git_buf *src)
723+
static int git_filter_list_apply_to_buffer(
724+
git_buf *out,
725+
git_filter_list *filters,
726+
const char *in,
727+
size_t in_len)
725728
{
726729
struct buf_stream writer;
727730
int error;
728731

729-
if ((error = git_buf_sanitize(tgt)) < 0 ||
730-
(error = git_buf_sanitize(src)) < 0)
731-
return error;
732-
733-
if (!filters) {
734-
git_buf_attach_notowned(tgt, src->ptr, src->size);
735-
return 0;
736-
}
732+
if ((error = git_buf_sanitize(out)) < 0)
733+
return error;
737734

738-
buf_stream_init(&writer, tgt);
735+
buf_stream_init(&writer, out);
739736

740737
if ((error = git_filter_list_stream_buffer(filters,
741-
src->ptr, src->size, &writer.parent)) < 0)
738+
in, in_len, &writer.parent)) < 0)
742739
return error;
743740

744741
GIT_ASSERT(writer.complete);
745742
return error;
746743
}
747744

745+
int git_filter_list_apply_to_data(
746+
git_buf *tgt, git_filter_list *filters, git_buf *src)
747+
{
748+
int error;
749+
750+
if ((error = git_buf_sanitize(src)) < 0)
751+
return error;
752+
753+
return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size);
754+
}
755+
748756
int git_filter_list_apply_to_file(
749757
git_buf *out,
750758
git_filter_list *filters,

0 commit comments

Comments
 (0)