Skip to content

Commit 31d9c24

Browse files
committed
filter: internal git_buf filter handling function
Introduce `git_filter_list__convert_buf` which behaves like the old implementation of `git_filter_list__apply_data`, where it might move the input data buffer over into the output data buffer space for efficiency. This new implementation will do so in a more predictible way, always freeing the given input buffer (either moving it to the output buffer or filtering it into the output buffer first). Convert internal users to it.
1 parent 68b9605 commit 31d9c24

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,28 @@ int git_filter_list_apply_to_buffer(
742742
return error;
743743
}
744744

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);
755+
return 0;
756+
}
757+
758+
error = git_filter_list_apply_to_buffer(out, filters,
759+
in->ptr, in->size);
760+
761+
if (!error)
762+
git_buf_dispose(in);
763+
764+
return error;
765+
}
766+
745767
int git_filter_list_apply_to_file(
746768
git_buf *out,
747769
git_filter_list *filters,

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)