Skip to content

Commit a8943c0

Browse files
committed
filter: proxy_stream is now git_filter_buffered_stream
The filter's proxy_stream is used to adapt filters that only provided an `apply` function into a `stream` function. Make this internal to the library instead of private to the filter file. This will allow the filters to use it directly, instead of relying on the filter functionality to do the proxying.
1 parent a9a7bfb commit a8943c0

File tree

2 files changed

+56
-43
lines changed

2 files changed

+56
-43
lines changed

src/filter.c

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,10 @@ int git_filter_list_apply_to_blob(
839839
return error;
840840
}
841841

842-
struct proxy_stream {
842+
struct buffered_stream {
843843
git_writestream parent;
844844
git_filter *filter;
845+
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *);
845846
const git_filter_source *source;
846847
void **payload;
847848
git_buf input;
@@ -850,89 +851,91 @@ struct proxy_stream {
850851
git_writestream *target;
851852
};
852853

853-
static int proxy_stream_write(
854+
static int buffered_stream_write(
854855
git_writestream *s, const char *buffer, size_t len)
855856
{
856-
struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
857-
GIT_ASSERT_ARG(proxy_stream);
857+
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
858+
GIT_ASSERT_ARG(buffered_stream);
858859

859-
return git_buf_put(&proxy_stream->input, buffer, len);
860+
return git_buf_put(&buffered_stream->input, buffer, len);
860861
}
861862

862-
static int proxy_stream_close(git_writestream *s)
863+
static int buffered_stream_close(git_writestream *s)
863864
{
864-
struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
865+
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
865866
git_buf *writebuf;
866867
git_error_state error_state = {0};
867868
int error;
868869

869-
GIT_ASSERT_ARG(proxy_stream);
870+
GIT_ASSERT_ARG(buffered_stream);
870871

871-
error = proxy_stream->filter->apply(
872-
proxy_stream->filter,
873-
proxy_stream->payload,
874-
proxy_stream->output,
875-
&proxy_stream->input,
876-
proxy_stream->source);
872+
error = buffered_stream->write_fn(
873+
buffered_stream->filter,
874+
buffered_stream->payload,
875+
buffered_stream->output,
876+
&buffered_stream->input,
877+
buffered_stream->source);
877878

878879
if (error == GIT_PASSTHROUGH) {
879-
writebuf = &proxy_stream->input;
880+
writebuf = &buffered_stream->input;
880881
} else if (error == 0) {
881-
if ((error = git_buf_sanitize(proxy_stream->output)) < 0)
882+
if ((error = git_buf_sanitize(buffered_stream->output)) < 0)
882883
return error;
883884

884-
writebuf = proxy_stream->output;
885+
writebuf = buffered_stream->output;
885886
} else {
886887
/* close stream before erroring out taking care
887888
* to preserve the original error */
888889
git_error_state_capture(&error_state, error);
889-
proxy_stream->target->close(proxy_stream->target);
890+
buffered_stream->target->close(buffered_stream->target);
890891
git_error_state_restore(&error_state);
891892
return error;
892893
}
893894

894-
if ((error = proxy_stream->target->write(
895-
proxy_stream->target, writebuf->ptr, writebuf->size)) == 0)
896-
error = proxy_stream->target->close(proxy_stream->target);
895+
if ((error = buffered_stream->target->write(
896+
buffered_stream->target, writebuf->ptr, writebuf->size)) == 0)
897+
error = buffered_stream->target->close(buffered_stream->target);
897898

898899
return error;
899900
}
900901

901-
static void proxy_stream_free(git_writestream *s)
902+
static void buffered_stream_free(git_writestream *s)
902903
{
903-
struct proxy_stream *proxy_stream = (struct proxy_stream *)s;
904+
struct buffered_stream *buffered_stream = (struct buffered_stream *)s;
904905

905-
if (proxy_stream) {
906-
git_buf_dispose(&proxy_stream->input);
907-
git_buf_dispose(&proxy_stream->temp_buf);
908-
git__free(proxy_stream);
906+
if (buffered_stream) {
907+
git_buf_dispose(&buffered_stream->input);
908+
git_buf_dispose(&buffered_stream->temp_buf);
909+
git__free(buffered_stream);
909910
}
910911
}
911912

912-
static int proxy_stream_init(
913+
int git_filter_buffered_stream_new(
913914
git_writestream **out,
914915
git_filter *filter,
916+
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *),
915917
git_buf *temp_buf,
916918
void **payload,
917919
const git_filter_source *source,
918920
git_writestream *target)
919921
{
920-
struct proxy_stream *proxy_stream = git__calloc(1, sizeof(struct proxy_stream));
921-
GIT_ERROR_CHECK_ALLOC(proxy_stream);
922-
923-
proxy_stream->parent.write = proxy_stream_write;
924-
proxy_stream->parent.close = proxy_stream_close;
925-
proxy_stream->parent.free = proxy_stream_free;
926-
proxy_stream->filter = filter;
927-
proxy_stream->payload = payload;
928-
proxy_stream->source = source;
929-
proxy_stream->target = target;
930-
proxy_stream->output = temp_buf ? temp_buf : &proxy_stream->temp_buf;
922+
struct buffered_stream *buffered_stream = git__calloc(1, sizeof(struct buffered_stream));
923+
GIT_ERROR_CHECK_ALLOC(buffered_stream);
924+
925+
buffered_stream->parent.write = buffered_stream_write;
926+
buffered_stream->parent.close = buffered_stream_close;
927+
buffered_stream->parent.free = buffered_stream_free;
928+
buffered_stream->filter = filter;
929+
buffered_stream->write_fn = write_fn;
930+
buffered_stream->output = temp_buf ? temp_buf : &buffered_stream->temp_buf;
931+
buffered_stream->payload = payload;
932+
buffered_stream->source = source;
933+
buffered_stream->target = target;
931934

932935
if (temp_buf)
933936
git_buf_clear(temp_buf);
934937

935-
*out = (git_writestream *)proxy_stream;
938+
*out = (git_writestream *)buffered_stream;
936939
return 0;
937940
}
938941

@@ -970,9 +973,9 @@ static int stream_list_init(
970973
&fe->payload, &filters->source, last_stream);
971974
else
972975
/* Create a stream that proxies the one-shot apply */
973-
error = proxy_stream_init(&filter_stream, fe->filter,
974-
filters->temp_buf, &fe->payload, &filters->source,
975-
last_stream);
976+
error = git_filter_buffered_stream_new(&filter_stream,
977+
fe->filter, fe->filter->apply, filters->temp_buf,
978+
&fe->payload, &filters->source, last_stream);
976979

977980
if (error < 0)
978981
goto out;

src/filter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "attr_file.h"
1313
#include "git2/filter.h"
14+
#include "git2/sys/filter.h"
1415

1516
/* Amount of file to examine for NUL byte when checking binary-ness */
1617
#define GIT_FILTER_BYTES_TO_CHECK_NUL 8000
@@ -51,4 +52,13 @@ extern int git_filter_list__convert_buf(
5152
extern git_filter *git_crlf_filter_new(void);
5253
extern git_filter *git_ident_filter_new(void);
5354

55+
extern int git_filter_buffered_stream_new(
56+
git_writestream **out,
57+
git_filter *filter,
58+
int (*write_fn)(git_filter *, void **, git_buf *, const git_buf *, const git_filter_source *),
59+
git_buf *temp_buf,
60+
void **payload,
61+
const git_filter_source *source,
62+
git_writestream *target);
63+
5464
#endif

0 commit comments

Comments
 (0)