filters: stream internally Migrate the `git_filter_list_apply_*` functions over to using the new filter streams.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
diff --git a/src/filter.c b/src/filter.c
index 1af17d8..700afba 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -617,67 +617,70 @@ static int filter_list_out_buffer_from_raw(
return 0;
}
-int git_filter_list_apply_to_data(
- git_buf *tgt, git_filter_list *fl, git_buf *src)
+struct buf_stream {
+ 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;
- uint32_t i;
- git_buf *dbuffer[2], local = GIT_BUF_INIT;
- unsigned int si = 0;
+ struct buf_stream *buf_stream = (struct buf_stream *)s;
+ assert(buf_stream);
- git_buf_sanitize(tgt);
- git_buf_sanitize(src);
+ assert(buf_stream->complete == 0);
- if (!fl)
- return filter_list_out_buffer_from_raw(tgt, src->ptr, src->size);
+ return git_buf_put(buf_stream->target, buffer, len);
+}
- dbuffer[0] = src;
- dbuffer[1] = tgt;
+static int buf_stream_close(git_filter_stream *s)
+{
+ struct buf_stream *buf_stream = (struct buf_stream *)s;
+ assert(buf_stream);
- /* if `src` buffer is reallocable, then use it, otherwise copy it */
- if (!git_buf_is_allocated(src)) {
- if (git_buf_set(&local, src->ptr, src->size) < 0)
- return -1;
- dbuffer[0] = &local;
- }
+ assert(buf_stream->complete == 0);
+ buf_stream->complete = 1;
- 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).
- */
+ return 0;
+}
- error = fe->filter->apply(
- fe->filter, &fe->payload, dbuffer[di], dbuffer[si], &fl->source);
+static void buf_stream_free(git_filter_stream *s)
+{
+ GIT_UNUSED(s);
+}
- 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;
- }
- }
+static void buf_stream_init(struct buf_stream *writer, git_buf *target)
+{
+ memset(writer, 0, sizeof(struct buf_stream));
- /* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
- if (si != 1)
- git_buf_swap(dbuffer[0], dbuffer[1]);
+ writer->base.write = buf_stream_write;
+ writer->base.close = buf_stream_close;
+ writer->base.free = buf_stream_free;
+ writer->target = target;
-cleanup:
- git_buf_free(&local); /* don't leak if we allocated locally */
+ 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(src);
+
+ if (!filters)
+ return filter_list_out_buffer_from_raw(tgt, src->ptr, src->size);
+
+ buf_stream_init(&writer, tgt);
+
+ if ((error = git_filter_list_stream_data(filters, src,
+ (git_filter_stream *)&writer)) < 0)
+ return error;
+
+ assert(writer.complete);
return error;
}
@@ -687,19 +690,16 @@ int git_filter_list_apply_to_file(
git_repository *repo,
const char *path)
{
+ struct buf_stream writer;
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)) &&
- !(error = git_futils_readbuffer(&raw, abspath.ptr)))
- {
- error = git_filter_list_apply_to_data(out, filters, &raw);
+ buf_stream_init(&writer, out);
- 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;
}
@@ -724,15 +724,17 @@ int git_filter_list_apply_to_blob(
git_filter_list *filters,
git_blob *blob)
{
- git_buf in = GIT_BUF_INIT;
+ struct buf_stream writer;
+ int error;
- if (buf_from_blob(&in, blob) < 0)
- return -1;
+ buf_stream_init(&writer, out);
- if (filters)
- git_oid_cpy(&filters->source.oid, git_blob_id(blob));
+ if ((error = git_filter_list_stream_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 {
@@ -823,7 +825,6 @@ static int stream_list_init(
git_filter_list *filters,
git_filter_stream *target)
{
- git_vector filter_streams = GIT_VECTOR_INIT;
git_filter_stream *last_stream = target;
size_t i;
int error = 0;
@@ -845,14 +846,19 @@ static int stream_list_init(
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 ?
fe->filter->stream : proxy_stream_init;
- if ((error = stream_init(&filter_stream, fe->filter, &fe->payload, &filters->source, last_stream)) < 0)
- return error;
+ error = stream_init(&filter_stream, fe->filter,
+ &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;
}
@@ -867,6 +873,7 @@ void stream_list_free(git_vector *streams)
git_vector_foreach(streams, i, stream)
stream->free(stream);
+ git_vector_free(streams);
}
#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 */
int git_filter_list_stream_file(
git_filter_list *filters,
- git_buf *data,
git_repository *repo,
const char *path,
git_filter_stream *target)
{
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;
git_vector filter_streams = GIT_VECTOR_INIT;
git_filter_stream *stream_start;
@@ -898,7 +904,7 @@ int git_filter_list_stream_file(
}
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;
}
@@ -924,6 +930,8 @@ int git_filter_list_stream_data(
git_filter_stream *stream_start;
int error = 0;
+ git_buf_sanitize(data);
+
if ((error = stream_list_init(
&stream_start, &filter_streams, filters, target)) == 0 &&
(error =