]> git.ipfire.org Git - thirdparty/git.git/blame - list-objects-filter-options.c
list_objects_filter_options: plug leak of filter_spec strings
[thirdparty/git.git] / list-objects-filter-options.c
CommitLineData
25ec7bca
JH
1#include "cache.h"
2#include "commit.h"
3#include "config.h"
4#include "revision.h"
dbbcd44f 5#include "strvec.h"
25ec7bca
JH
6#include "list-objects.h"
7#include "list-objects-filter.h"
8#include "list-objects-filter-options.h"
b14ed5ad 9#include "promisor-remote.h"
489fc9ee 10#include "trace.h"
e987df5f
MD
11#include "url.h"
12
13static int parse_combine_filter(
14 struct list_objects_filter_options *filter_options,
15 const char *arg,
16 struct strbuf *errbuf);
25ec7bca 17
b9ea2147
TB
18const char *list_object_filter_config_name(enum list_objects_filter_choice c)
19{
20 switch (c) {
21 case LOFC_DISABLED:
22 /* we have no name for "no filter at all" */
23 break;
24 case LOFC_BLOB_NONE:
25 return "blob:none";
26 case LOFC_BLOB_LIMIT:
27 return "blob:limit";
28 case LOFC_TREE_DEPTH:
29 return "tree";
30 case LOFC_SPARSE_OID:
31 return "sparse:oid";
b0c42a53
PS
32 case LOFC_OBJECT_TYPE:
33 return "object:type";
b9ea2147
TB
34 case LOFC_COMBINE:
35 return "combine";
36 case LOFC__COUNT:
37 /* not a real filter type; just the count of all filters */
38 break;
39 }
5a923bb1 40 BUG("list_object_filter_config_name: invalid argument '%d'", c);
b9ea2147
TB
41}
42
105c6f14 43int gently_parse_list_objects_filter(
1e1e39b3
JH
44 struct list_objects_filter_options *filter_options,
45 const char *arg,
46 struct strbuf *errbuf)
25ec7bca
JH
47{
48 const char *v0;
49
fa3d1b63
CC
50 if (!arg)
51 return 0;
52
f56f7642
MD
53 if (filter_options->choice)
54 BUG("filter_options already populated");
25ec7bca
JH
55
56 if (!strcmp(arg, "blob:none")) {
57 filter_options->choice = LOFC_BLOB_NONE;
58 return 0;
25ec7bca 59
1e1e39b3
JH
60 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
61 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
62 filter_options->choice = LOFC_BLOB_LIMIT;
63 return 0;
64 }
25ec7bca 65
bc5975d2 66 } else if (skip_prefix(arg, "tree:", &v0)) {
c813a7c3 67 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
842b0051 68 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
bc5975d2
MD
69 return 1;
70 }
c813a7c3 71 filter_options->choice = LOFC_TREE_DEPTH;
bc5975d2
MD
72 return 0;
73
1e1e39b3 74 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
4c96a775 75 filter_options->sparse_oid_name = xstrdup(v0);
25ec7bca
JH
76 filter_options->choice = LOFC_SPARSE_OID;
77 return 0;
25ec7bca 78
1e1e39b3 79 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
e693237e
CC
80 if (errbuf) {
81 strbuf_addstr(
82 errbuf,
83 _("sparse:path filters support has been dropped"));
84 }
85 return 1;
e987df5f 86
b0c42a53
PS
87 } else if (skip_prefix(arg, "object:type=", &v0)) {
88 int type = type_from_string_gently(v0, strlen(v0), 1);
89 if (type < 0) {
225f7fa8 90 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
b0c42a53
PS
91 "not a valid object type"), v0);
92 return 1;
93 }
94
95 filter_options->object_type = type;
96 filter_options->choice = LOFC_OBJECT_TYPE;
97
98 return 0;
99
e987df5f
MD
100 } else if (skip_prefix(arg, "combine:", &v0)) {
101 return parse_combine_filter(filter_options, v0, errbuf);
102
25ec7bca 103 }
5a59a230
NTND
104 /*
105 * Please update _git_fetch() in git-completion.bash when you
106 * add new filters
107 */
25ec7bca 108
842b0051 109 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
cc0b05a4 110
1e1e39b3
JH
111 memset(filter_options, 0, sizeof(*filter_options));
112 return 1;
113}
114
e987df5f
MD
115static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
116
117static int has_reserved_character(
118 struct strbuf *sub_spec, struct strbuf *errbuf)
1e1e39b3 119{
e987df5f
MD
120 const char *c = sub_spec->buf;
121 while (*c) {
122 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
123 strbuf_addf(
124 errbuf,
125 _("must escape char in sub-filter-spec: '%c'"),
126 *c);
127 return 1;
128 }
129 c++;
130 }
131
25ec7bca
JH
132 return 0;
133}
134
e987df5f
MD
135static int parse_combine_subfilter(
136 struct list_objects_filter_options *filter_options,
137 struct strbuf *subspec,
138 struct strbuf *errbuf)
139{
5a133e8a 140 size_t new_index = filter_options->sub_nr;
e987df5f
MD
141 char *decoded;
142 int result;
143
5a133e8a
MD
144 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
145 filter_options->sub_alloc);
e987df5f
MD
146
147 decoded = url_percent_decode(subspec->buf);
148
149 result = has_reserved_character(subspec, errbuf) ||
150 gently_parse_list_objects_filter(
151 &filter_options->sub[new_index], decoded, errbuf);
152
153 free(decoded);
154 return result;
155}
156
157static int parse_combine_filter(
158 struct list_objects_filter_options *filter_options,
159 const char *arg,
160 struct strbuf *errbuf)
161{
162 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
163 size_t sub;
164 int result = 0;
165
166 if (!subspecs[0]) {
167 strbuf_addstr(errbuf, _("expected something after combine:"));
168 result = 1;
169 goto cleanup;
170 }
171
172 for (sub = 0; subspecs[sub] && !result; sub++) {
173 if (subspecs[sub + 1]) {
174 /*
175 * This is not the last subspec. Remove trailing "+" so
176 * we can parse it.
177 */
178 size_t last = subspecs[sub]->len - 1;
179 assert(subspecs[sub]->buf[last] == '+');
180 strbuf_remove(subspecs[sub], last, 1);
181 }
182 result = parse_combine_subfilter(
183 filter_options, subspecs[sub], errbuf);
184 }
185
186 filter_options->choice = LOFC_COMBINE;
187
188cleanup:
189 strbuf_list_free(subspecs);
190 if (result) {
191 list_objects_filter_release(filter_options);
192 memset(filter_options, 0, sizeof(*filter_options));
193 }
194 return result;
195}
196
489fc9ee
MD
197static int allow_unencoded(char ch)
198{
199 if (ch <= ' ' || ch == '%' || ch == '+')
200 return 0;
201 return !strchr(RESERVED_NON_WS, ch);
202}
203
204static void filter_spec_append_urlencode(
205 struct list_objects_filter_options *filter, const char *raw)
1e1e39b3
JH
206{
207 struct strbuf buf = STRBUF_INIT;
489fc9ee
MD
208 strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
209 trace_printf("Add to combine filter-spec: %s\n", buf.buf);
7e2619d8 210 string_list_append_nodup(&filter->filter_spec, strbuf_detach(&buf, NULL));
489fc9ee
MD
211}
212
213/*
214 * Changes filter_options into an equivalent LOFC_COMBINE filter options
215 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
216 */
217static void transform_to_combine_type(
218 struct list_objects_filter_options *filter_options)
219{
220 assert(filter_options->choice);
221 if (filter_options->choice == LOFC_COMBINE)
222 return;
223 {
224 const int initial_sub_alloc = 2;
225 struct list_objects_filter_options *sub_array =
226 xcalloc(initial_sub_alloc, sizeof(*sub_array));
227 sub_array[0] = *filter_options;
228 memset(filter_options, 0, sizeof(*filter_options));
7e2619d8 229 string_list_init_dup(&filter_options->filter_spec);
489fc9ee
MD
230 filter_options->sub = sub_array;
231 filter_options->sub_alloc = initial_sub_alloc;
232 }
233 filter_options->sub_nr = 1;
234 filter_options->choice = LOFC_COMBINE;
7e2619d8 235 string_list_append(&filter_options->filter_spec, "combine:");
489fc9ee
MD
236 filter_spec_append_urlencode(
237 filter_options,
238 list_objects_filter_spec(&filter_options->sub[0]));
239 /*
240 * We don't need the filter_spec strings for subfilter specs, only the
241 * top level.
242 */
243 string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
244}
245
246void list_objects_filter_die_if_populated(
247 struct list_objects_filter_options *filter_options)
248{
f56f7642
MD
249 if (filter_options->choice)
250 die(_("multiple filter-specs cannot be combined"));
489fc9ee
MD
251}
252
90d21f9e 253void parse_list_objects_filter(
489fc9ee
MD
254 struct list_objects_filter_options *filter_options,
255 const char *arg)
256{
257 struct strbuf errbuf = STRBUF_INIT;
258 int parse_error;
259
7e2619d8
JK
260 if (!filter_options->filter_spec.strdup_strings) {
261 if (filter_options->filter_spec.nr)
262 BUG("unexpected non-allocated string in filter_spec");
263 filter_options->filter_spec.strdup_strings = 1;
264 }
265
489fc9ee 266 if (!filter_options->choice) {
7e2619d8 267 string_list_append(&filter_options->filter_spec, arg);
489fc9ee
MD
268
269 parse_error = gently_parse_list_objects_filter(
270 filter_options, arg, &errbuf);
271 } else {
272 /*
273 * Make filter_options an LOFC_COMBINE spec so we can trivially
274 * add subspecs to it.
275 */
276 transform_to_combine_type(filter_options);
277
7e2619d8 278 string_list_append(&filter_options->filter_spec, "+");
489fc9ee 279 filter_spec_append_urlencode(filter_options, arg);
5a133e8a
MD
280 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
281 filter_options->sub_alloc);
489fc9ee
MD
282
283 parse_error = gently_parse_list_objects_filter(
5a133e8a
MD
284 &filter_options->sub[filter_options->sub_nr - 1], arg,
285 &errbuf);
489fc9ee
MD
286 }
287 if (parse_error)
288 die("%s", errbuf.buf);
25ec7bca
JH
289}
290
291int opt_parse_list_objects_filter(const struct option *opt,
292 const char *arg, int unset)
293{
294 struct list_objects_filter_options *filter_options = opt->value;
5cb28270
ÆAB
295 opt_lof_init init = (opt_lof_init)opt->defval;
296
297 if (init)
298 filter_options = init(opt->value);
25ec7bca 299
90d21f9e 300 if (unset || !arg)
aa57b871 301 list_objects_filter_set_no_filter(filter_options);
90d21f9e
MD
302 else
303 parse_list_objects_filter(filter_options, arg);
304 return 0;
25ec7bca 305}
4875c979 306
cf9ceb5a 307const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
87c2d9d3 308{
cf9ceb5a
MD
309 if (!filter->filter_spec.nr)
310 BUG("no filter_spec available for this filter");
311 if (filter->filter_spec.nr != 1) {
312 struct strbuf concatted = STRBUF_INIT;
313 strbuf_add_separated_string_list(
314 &concatted, "", &filter->filter_spec);
315 string_list_clear(&filter->filter_spec, /*free_util=*/0);
7e2619d8 316 string_list_append_nodup(
cf9ceb5a 317 &filter->filter_spec, strbuf_detach(&concatted, NULL));
4875c979 318 }
25ec7bca 319
cf9ceb5a 320 return filter->filter_spec.items[0].string;
25ec7bca 321}
4875c979 322
cf9ceb5a
MD
323const char *expand_list_objects_filter_spec(
324 struct list_objects_filter_options *filter)
87c2d9d3 325{
cf9ceb5a
MD
326 if (filter->choice == LOFC_BLOB_LIMIT) {
327 struct strbuf expanded_spec = STRBUF_INIT;
328 strbuf_addf(&expanded_spec, "blob:limit=%lu",
87c2d9d3 329 filter->blob_limit_value);
cf9ceb5a 330 string_list_clear(&filter->filter_spec, /*free_util=*/0);
7e2619d8 331 string_list_append_nodup(
cf9ceb5a
MD
332 &filter->filter_spec,
333 strbuf_detach(&expanded_spec, NULL));
334 }
335
336 return list_objects_filter_spec(filter);
87c2d9d3
JS
337}
338
4875c979
JH
339void list_objects_filter_release(
340 struct list_objects_filter_options *filter_options)
341{
e987df5f
MD
342 size_t sub;
343
344 if (!filter_options)
345 return;
cf9ceb5a 346 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
4c96a775 347 free(filter_options->sparse_oid_name);
e987df5f
MD
348 for (sub = 0; sub < filter_options->sub_nr; sub++)
349 list_objects_filter_release(&filter_options->sub[sub]);
350 free(filter_options->sub);
4875c979
JH
351 memset(filter_options, 0, sizeof(*filter_options));
352}
1e1e39b3
JH
353
354void partial_clone_register(
355 const char *remote,
cf9ceb5a 356 struct list_objects_filter_options *filter_options)
1e1e39b3 357{
23547c40 358 struct promisor_remote *promisor_remote;
b14ed5ad 359 char *cfg_name;
fa3d1b63 360 char *filter_name;
1e1e39b3 361
b14ed5ad 362 /* Check if it is already registered */
23547c40
JT
363 if ((promisor_remote = promisor_remote_find(remote))) {
364 if (promisor_remote->partial_clone_filter)
365 /*
366 * Remote is already registered and a filter is already
367 * set, so we don't need to do anything here.
368 */
369 return;
370 } else {
16af5f1a
XL
371 if (upgrade_repository_format(1) < 0)
372 die(_("unable to upgrade repository format to support partial clone"));
1e1e39b3 373
b14ed5ad
CC
374 /* Add promisor config for the remote */
375 cfg_name = xstrfmt("remote.%s.promisor", remote);
376 git_config_set(cfg_name, "true");
377 free(cfg_name);
378 }
1e1e39b3
JH
379
380 /*
381 * Record the initial filter-spec in the config as
382 * the default for subsequent fetches from this remote.
383 */
fa3d1b63 384 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
627b8268
JH
385 /* NEEDSWORK: 'expand' result leaking??? */
386 git_config_set(filter_name,
387 expand_list_objects_filter_spec(filter_options));
fa3d1b63 388 free(filter_name);
b14ed5ad
CC
389
390 /* Make sure the config info are reset */
391 promisor_remote_reinit();
1e1e39b3
JH
392}
393
394void partial_clone_get_default_filter_spec(
fa3d1b63
CC
395 struct list_objects_filter_options *filter_options,
396 const char *remote)
1e1e39b3 397{
fa3d1b63 398 struct promisor_remote *promisor = promisor_remote_find(remote);
842b0051 399 struct strbuf errbuf = STRBUF_INIT;
fa3d1b63 400
1e1e39b3
JH
401 /*
402 * Parse default value, but silently ignore it if it is invalid.
403 */
627b8268 404 if (!promisor)
cac1137d 405 return;
e987df5f 406
cf9ceb5a 407 string_list_append(&filter_options->filter_spec,
627b8268 408 promisor->partial_clone_filter);
1e1e39b3 409 gently_parse_list_objects_filter(filter_options,
627b8268 410 promisor->partial_clone_filter,
842b0051
MD
411 &errbuf);
412 strbuf_release(&errbuf);
1e1e39b3 413}
4a4c3f9b
DS
414
415void list_objects_filter_copy(
416 struct list_objects_filter_options *dest,
417 const struct list_objects_filter_options *src)
418{
419 int i;
420 struct string_list_item *item;
421
422 /* Copy everything. We will overwrite the pointers shortly. */
423 memcpy(dest, src, sizeof(struct list_objects_filter_options));
424
425 string_list_init_dup(&dest->filter_spec);
426 for_each_string_list_item(item, &src->filter_spec)
427 string_list_append(&dest->filter_spec, item->string);
3fbfbbb7 428 dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);
4a4c3f9b
DS
429
430 ALLOC_ARRAY(dest->sub, dest->sub_alloc);
431 for (i = 0; i < src->sub_nr; i++)
432 list_objects_filter_copy(&dest->sub[i], &src->sub[i]);
433}