mirror of
https://git.proxmox.com/git/libgit2
synced 2025-08-06 07:00:27 +00:00
filters: stream internally
Migrate the `git_filter_list_apply_*` functions over to using the new filter streams.
This commit is contained in:
parent
fbdc9db364
commit
5555696f8a
158
src/filter.c
158
src/filter.c
@ -617,67 +617,70 @@ static int filter_list_out_buffer_from_raw(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_filter_list_apply_to_data(
|
struct buf_stream {
|
||||||
git_buf *tgt, git_filter_list *fl, git_buf *src)
|
git_filter_stream base;
|
||||||
|
git_buf *target;
|
||||||
|
bool complete;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int buf_stream_write(
|
||||||
|
git_filter_stream *s, const char *buffer, size_t len)
|
||||||
{
|
{
|
||||||
int error = 0;
|
struct buf_stream *buf_stream = (struct buf_stream *)s;
|
||||||
uint32_t i;
|
assert(buf_stream);
|
||||||
git_buf *dbuffer[2], local = GIT_BUF_INIT;
|
|
||||||
unsigned int si = 0;
|
assert(buf_stream->complete == 0);
|
||||||
|
|
||||||
|
return git_buf_put(buf_stream->target, buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int buf_stream_close(git_filter_stream *s)
|
||||||
|
{
|
||||||
|
struct buf_stream *buf_stream = (struct buf_stream *)s;
|
||||||
|
assert(buf_stream);
|
||||||
|
|
||||||
|
assert(buf_stream->complete == 0);
|
||||||
|
buf_stream->complete = 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void buf_stream_free(git_filter_stream *s)
|
||||||
|
{
|
||||||
|
GIT_UNUSED(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void buf_stream_init(struct buf_stream *writer, git_buf *target)
|
||||||
|
{
|
||||||
|
memset(writer, 0, sizeof(struct buf_stream));
|
||||||
|
|
||||||
|
writer->base.write = buf_stream_write;
|
||||||
|
writer->base.close = buf_stream_close;
|
||||||
|
writer->base.free = buf_stream_free;
|
||||||
|
writer->target = target;
|
||||||
|
|
||||||
|
git_buf_clear(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_filter_list_apply_to_data(
|
||||||
|
git_buf *tgt, git_filter_list *filters, git_buf *src)
|
||||||
|
{
|
||||||
|
struct buf_stream writer;
|
||||||
|
int error;
|
||||||
|
|
||||||
git_buf_sanitize(tgt);
|
git_buf_sanitize(tgt);
|
||||||
git_buf_sanitize(src);
|
git_buf_sanitize(src);
|
||||||
|
|
||||||
if (!fl)
|
if (!filters)
|
||||||
return filter_list_out_buffer_from_raw(tgt, src->ptr, src->size);
|
return filter_list_out_buffer_from_raw(tgt, src->ptr, src->size);
|
||||||
|
|
||||||
dbuffer[0] = src;
|
buf_stream_init(&writer, tgt);
|
||||||
dbuffer[1] = tgt;
|
|
||||||
|
|
||||||
/* if `src` buffer is reallocable, then use it, otherwise copy it */
|
if ((error = git_filter_list_stream_data(filters, src,
|
||||||
if (!git_buf_is_allocated(src)) {
|
(git_filter_stream *)&writer)) < 0)
|
||||||
if (git_buf_set(&local, src->ptr, src->size) < 0)
|
return error;
|
||||||
return -1;
|
|
||||||
dbuffer[0] = &local;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < git_array_size(fl->filters); ++i) {
|
|
||||||
unsigned int di = 1 - si;
|
|
||||||
uint32_t fidx = (fl->source.mode == GIT_FILTER_TO_WORKTREE) ?
|
|
||||||
i : git_array_size(fl->filters) - 1 - i;
|
|
||||||
git_filter_entry *fe = git_array_get(fl->filters, fidx);
|
|
||||||
|
|
||||||
dbuffer[di]->size = 0;
|
|
||||||
|
|
||||||
/* Apply the filter from dbuffer[src] to the other buffer;
|
|
||||||
* if the filtering is canceled by the user mid-filter,
|
|
||||||
* we skip to the next filter without changing the source
|
|
||||||
* of the double buffering (so that the text goes through
|
|
||||||
* cleanly).
|
|
||||||
*/
|
|
||||||
|
|
||||||
error = fe->filter->apply(
|
|
||||||
fe->filter, &fe->payload, dbuffer[di], dbuffer[si], &fl->source);
|
|
||||||
|
|
||||||
if (error == GIT_PASSTHROUGH) {
|
|
||||||
/* PASSTHROUGH means filter decided not to process the buffer */
|
|
||||||
error = 0;
|
|
||||||
} else if (!error) {
|
|
||||||
git_buf_sanitize(dbuffer[di]); /* force NUL termination */
|
|
||||||
si = di; /* swap buffers */
|
|
||||||
} else {
|
|
||||||
tgt->size = 0;
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
|
|
||||||
if (si != 1)
|
|
||||||
git_buf_swap(dbuffer[0], dbuffer[1]);
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
git_buf_free(&local); /* don't leak if we allocated locally */
|
|
||||||
|
|
||||||
|
assert(writer.complete);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -687,19 +690,16 @@ int git_filter_list_apply_to_file(
|
|||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
const char *path)
|
const char *path)
|
||||||
{
|
{
|
||||||
|
struct buf_stream writer;
|
||||||
int error;
|
int error;
|
||||||
const char *base = repo ? git_repository_workdir(repo) : NULL;
|
|
||||||
git_buf abspath = GIT_BUF_INIT, raw = GIT_BUF_INIT;
|
|
||||||
|
|
||||||
if (!(error = git_path_join_unrooted(&abspath, path, base, NULL)) &&
|
buf_stream_init(&writer, out);
|
||||||
!(error = git_futils_readbuffer(&raw, abspath.ptr)))
|
|
||||||
{
|
|
||||||
error = git_filter_list_apply_to_data(out, filters, &raw);
|
|
||||||
|
|
||||||
git_buf_free(&raw);
|
if ((error = git_filter_list_stream_file(
|
||||||
}
|
filters, repo, path, (git_filter_stream *)&writer)) < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
git_buf_free(&abspath);
|
assert(writer.complete);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -724,15 +724,17 @@ int git_filter_list_apply_to_blob(
|
|||||||
git_filter_list *filters,
|
git_filter_list *filters,
|
||||||
git_blob *blob)
|
git_blob *blob)
|
||||||
{
|
{
|
||||||
git_buf in = GIT_BUF_INIT;
|
struct buf_stream writer;
|
||||||
|
int error;
|
||||||
|
|
||||||
if (buf_from_blob(&in, blob) < 0)
|
buf_stream_init(&writer, out);
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (filters)
|
if ((error = git_filter_list_stream_blob(
|
||||||
git_oid_cpy(&filters->source.oid, git_blob_id(blob));
|
filters, blob, (git_filter_stream *)&writer)) < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
return git_filter_list_apply_to_data(out, filters, &in);
|
assert(writer.complete);
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct proxy_stream {
|
struct proxy_stream {
|
||||||
@ -823,7 +825,6 @@ static int stream_list_init(
|
|||||||
git_filter_list *filters,
|
git_filter_list *filters,
|
||||||
git_filter_stream *target)
|
git_filter_stream *target)
|
||||||
{
|
{
|
||||||
git_vector filter_streams = GIT_VECTOR_INIT;
|
|
||||||
git_filter_stream *last_stream = target;
|
git_filter_stream *last_stream = target;
|
||||||
size_t i;
|
size_t i;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
@ -845,14 +846,19 @@ static int stream_list_init(
|
|||||||
|
|
||||||
assert(fe->filter->stream || fe->filter->apply);
|
assert(fe->filter->stream || fe->filter->apply);
|
||||||
|
|
||||||
/* If necessary, create a stream that proxies the one-shot apply */
|
/* If necessary, create a stream that proxies the traditional
|
||||||
|
* application.
|
||||||
|
*/
|
||||||
stream_init = fe->filter->stream ?
|
stream_init = fe->filter->stream ?
|
||||||
fe->filter->stream : proxy_stream_init;
|
fe->filter->stream : proxy_stream_init;
|
||||||
|
|
||||||
if ((error = stream_init(&filter_stream, fe->filter, &fe->payload, &filters->source, last_stream)) < 0)
|
error = stream_init(&filter_stream, fe->filter,
|
||||||
return error;
|
&fe->payload, &filters->source, last_stream);
|
||||||
|
|
||||||
git_vector_insert(&filter_streams, filter_stream);
|
if (error < 0)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
git_vector_insert(streams, filter_stream);
|
||||||
last_stream = filter_stream;
|
last_stream = filter_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -867,6 +873,7 @@ void stream_list_free(git_vector *streams)
|
|||||||
|
|
||||||
git_vector_foreach(streams, i, stream)
|
git_vector_foreach(streams, i, stream)
|
||||||
stream->free(stream);
|
stream->free(stream);
|
||||||
|
git_vector_free(streams);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define STREAM_BUFSIZE 10240
|
#define STREAM_BUFSIZE 10240
|
||||||
@ -874,13 +881,12 @@ void stream_list_free(git_vector *streams)
|
|||||||
/* TODO: maybe not use filter_stream as a target but create one */
|
/* TODO: maybe not use filter_stream as a target but create one */
|
||||||
int git_filter_list_stream_file(
|
int git_filter_list_stream_file(
|
||||||
git_filter_list *filters,
|
git_filter_list *filters,
|
||||||
git_buf *data,
|
|
||||||
git_repository *repo,
|
git_repository *repo,
|
||||||
const char *path,
|
const char *path,
|
||||||
git_filter_stream *target)
|
git_filter_stream *target)
|
||||||
{
|
{
|
||||||
char buf[STREAM_BUFSIZE];
|
char buf[STREAM_BUFSIZE];
|
||||||
git_buf abspath = GIT_BUF_INIT, raw = GIT_BUF_INIT;
|
git_buf abspath = GIT_BUF_INIT;
|
||||||
const char *base = repo ? git_repository_workdir(repo) : NULL;
|
const char *base = repo ? git_repository_workdir(repo) : NULL;
|
||||||
git_vector filter_streams = GIT_VECTOR_INIT;
|
git_vector filter_streams = GIT_VECTOR_INIT;
|
||||||
git_filter_stream *stream_start;
|
git_filter_stream *stream_start;
|
||||||
@ -898,7 +904,7 @@ int git_filter_list_stream_file(
|
|||||||
}
|
}
|
||||||
|
|
||||||
while ((readlen = p_read(fd, buf, STREAM_BUFSIZE)) > 0) {
|
while ((readlen = p_read(fd, buf, STREAM_BUFSIZE)) > 0) {
|
||||||
if ((error = stream_start->write(stream_start, data->ptr, data->size)) < 0)
|
if ((error = stream_start->write(stream_start, buf, readlen)) < 0)
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -924,6 +930,8 @@ int git_filter_list_stream_data(
|
|||||||
git_filter_stream *stream_start;
|
git_filter_stream *stream_start;
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|
||||||
|
git_buf_sanitize(data);
|
||||||
|
|
||||||
if ((error = stream_list_init(
|
if ((error = stream_list_init(
|
||||||
&stream_start, &filter_streams, filters, target)) == 0 &&
|
&stream_start, &filter_streams, filters, target)) == 0 &&
|
||||||
(error =
|
(error =
|
||||||
|
Loading…
Reference in New Issue
Block a user