Skip to content

Commit 05548e6

Browse files
authored
Merge pull request libgit2#5859 from libgit2/ethomson/filter_buf
filter: stop taking git_buf as user input
2 parents 4bd1720 + 31d9c24 commit 05548e6

File tree

10 files changed

+209
-118
lines changed

10 files changed

+209
-118
lines changed

include/git2/deprecated.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "describe.h"
1919
#include "diff.h"
2020
#include "errors.h"
21+
#include "filter.h"
2122
#include "index.h"
2223
#include "indexer.h"
2324
#include "merge.h"
@@ -119,6 +120,39 @@ GIT_EXTERN(int) git_blob_filtered_content(
119120

120121
/**@}*/
121122

123+
/** @name Deprecated Filter Functions
124+
*
125+
* These functions are retained for backward compatibility. The
126+
* newer versions of these functions should be preferred in all
127+
* new code.
128+
*
129+
* There is no plan to remove these backward compatibility values at
130+
* this time.
131+
*/
132+
/**@{*/
133+
134+
/** Deprecated in favor of `git_filter_list_stream_buffer`.
135+
*
136+
* @deprecated Use git_filter_list_stream_buffer
137+
* @see Use git_filter_list_stream_buffer
138+
*/
139+
GIT_EXTERN(int) git_filter_list_stream_data(
140+
git_filter_list *filters,
141+
git_buf *data,
142+
git_writestream *target);
143+
144+
/** Deprecated in favor of `git_filter_list_apply_to_buffer`.
145+
*
146+
* @deprecated Use git_filter_list_apply_to_buffer
147+
* @see Use git_filter_list_apply_to_buffer
148+
*/
149+
GIT_EXTERN(int) git_filter_list_apply_to_data(
150+
git_buf *out,
151+
git_filter_list *filters,
152+
git_buf *in);
153+
154+
/**@}*/
155+
122156
/** @name Deprecated Tree Functions
123157
*
124158
* These functions are retained for backward compatibility. The

include/git2/filter.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,17 @@ 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
128+
* @param in_len The length of the input buffer
140129
* @return 0 on success, an error code otherwise
141130
*/
142-
GIT_EXTERN(int) git_filter_list_apply_to_data(
131+
GIT_EXTERN(int) git_filter_list_apply_to_buffer(
143132
git_buf *out,
144133
git_filter_list *filters,
145-
git_buf *in);
134+
const char *in,
135+
size_t in_len);
146136

147137
/**
148138
* Apply a filter list to the contents of a file on disk
@@ -175,12 +165,14 @@ GIT_EXTERN(int) git_filter_list_apply_to_blob(
175165
* Apply a filter list to an arbitrary buffer as a stream
176166
*
177167
* @param filters the list of filters to apply
178-
* @param data the buffer to filter
168+
* @param buffer the buffer to filter
169+
* @param len the size of the buffer
179170
* @param target the stream into which the data will be written
180171
*/
181-
GIT_EXTERN(int) git_filter_list_stream_data(
172+
GIT_EXTERN(int) git_filter_list_stream_buffer(
182173
git_filter_list *filters,
183-
git_buf *data,
174+
const char *buffer,
175+
size_t len,
184176
git_writestream *target);
185177

186178
/**

src/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ static int checkout_write_merge(
21212121
if ((error = git_filter_list__load_ext(
21222122
&fl, data->repo, NULL, git_buf_cstr(&path_workdir),
21232123
GIT_FILTER_TO_WORKTREE, &filter_opts)) < 0 ||
2124-
(error = git_filter_list_apply_to_data(&out_data, fl, &in_data)) < 0)
2124+
(error = git_filter_list__convert_buf(&out_data, fl, &in_data)) < 0)
21252125
goto done;
21262126
} else {
21272127
out_data.ptr = (char *)result.ptr;

src/diff_file.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,7 @@ static int diff_file_content_load_workdir_file(
362362
if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
363363
git_buf out = GIT_BUF_INIT;
364364

365-
error = git_filter_list_apply_to_data(&out, fl, &raw);
366-
367-
if (out.ptr != raw.ptr)
368-
git_buf_dispose(&raw);
365+
error = git_filter_list__convert_buf(&out, fl, &raw);
369366

370367
if (!error) {
371368
fc->map.len = out.size;

src/filter.c

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -720,28 +720,47 @@ 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+
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+
if ((error = git_buf_sanitize(out)) < 0)
733+
return error;
732734

733-
if (!filters) {
734-
git_buf_attach_notowned(tgt, src->ptr, src->size);
735+
buf_stream_init(&writer, out);
736+
737+
if ((error = git_filter_list_stream_buffer(filters,
738+
in, in_len, &writer.parent)) < 0)
739+
return error;
740+
741+
GIT_ASSERT(writer.complete);
742+
return error;
743+
}
744+
745+
int git_filter_list__convert_buf(
746+
git_buf *out,
747+
git_filter_list *filters,
748+
git_buf *in)
749+
{
750+
int error;
751+
752+
if (!filters || git_filter_list_length(filters) == 0) {
753+
git_buf_swap(out, in);
754+
git_buf_dispose(in);
735755
return 0;
736756
}
737757

738-
buf_stream_init(&writer, tgt);
758+
error = git_filter_list_apply_to_buffer(out, filters,
759+
in->ptr, in->size);
739760

740-
if ((error = git_filter_list_stream_data(filters, src,
741-
&writer.parent)) < 0)
742-
return error;
761+
if (!error)
762+
git_buf_dispose(in);
743763

744-
GIT_ASSERT(writer.complete);
745764
return error;
746765
}
747766

@@ -1002,24 +1021,21 @@ int git_filter_list_stream_file(
10021021
return error;
10031022
}
10041023

1005-
int git_filter_list_stream_data(
1024+
int git_filter_list_stream_buffer(
10061025
git_filter_list *filters,
1007-
git_buf *data,
1026+
const char *buffer,
1027+
size_t len,
10081028
git_writestream *target)
10091029
{
10101030
git_vector filter_streams = GIT_VECTOR_INIT;
10111031
git_writestream *stream_start;
10121032
int error, initialized = 0;
10131033

1014-
if ((error = git_buf_sanitize(data)) < 0)
1015-
return error;
1016-
10171034
if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0)
10181035
goto out;
10191036
initialized = 1;
10201037

1021-
if ((error = stream_start->write(
1022-
stream_start, data->ptr, data->size)) < 0)
1038+
if ((error = stream_start->write(stream_start, buffer, len)) < 0)
10231039
goto out;
10241040

10251041
out:
@@ -1043,11 +1059,39 @@ int git_filter_list_stream_blob(
10431059
if (filters)
10441060
git_oid_cpy(&filters->source.oid, git_blob_id(blob));
10451061

1046-
return git_filter_list_stream_data(filters, &in, target);
1062+
return git_filter_list_stream_buffer(filters, in.ptr, in.size, target);
10471063
}
10481064

10491065
int git_filter_init(git_filter *filter, unsigned int version)
10501066
{
10511067
GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT);
10521068
return 0;
10531069
}
1070+
1071+
#ifndef GIT_DEPRECATE_HARD
1072+
1073+
int git_filter_list_stream_data(
1074+
git_filter_list *filters,
1075+
git_buf *data,
1076+
git_writestream *target)
1077+
{
1078+
int error;
1079+
1080+
if ((error = git_buf_sanitize(data)) < 0)
1081+
return error;
1082+
1083+
return git_filter_list_stream_buffer(filters, data->ptr, data->size, target);
1084+
}
1085+
1086+
int git_filter_list_apply_to_data(
1087+
git_buf *tgt, git_filter_list *filters, git_buf *src)
1088+
{
1089+
int error;
1090+
1091+
if ((error = git_buf_sanitize(src)) < 0)
1092+
return error;
1093+
1094+
return git_filter_list_apply_to_buffer(tgt, filters, src->ptr, src->size);
1095+
}
1096+
1097+
#endif

src/filter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ extern int git_filter_list__load_ext(
3535
git_filter_mode_t mode,
3636
git_filter_options *filter_opts);
3737

38+
/*
39+
* The given input buffer will be converted to the given output buffer.
40+
* The input buffer will be freed (_if_ it was allocated).
41+
*/
42+
extern int git_filter_list__convert_buf(
43+
git_buf *out,
44+
git_filter_list *filters,
45+
git_buf *in);
46+
3847
/*
3948
* Available filters
4049
*/

src/odb.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ int git_odb__hashfd_filtered(
260260
if (!(error = git_futils_readbuffer_fd(&raw, fd, size))) {
261261
git_buf post = GIT_BUF_INIT;
262262

263-
error = git_filter_list_apply_to_data(&post, fl, &raw);
264-
265-
git_buf_dispose(&raw);
263+
error = git_filter_list__convert_buf(&post, fl, &raw);
266264

267265
if (!error)
268266
error = git_odb_hash(out, post.ptr, post.size, type);

0 commit comments

Comments
 (0)