blob: 4b25287886dfe7368582dd615d424e66e6f320f9 [file] [log] [blame]
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +00001#include "cache.h"
2#include "commit.h"
3#include "config.h"
4#include "revision.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04005#include "strvec.h"
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +00006#include "list-objects.h"
7#include "list-objects-filter.h"
8#include "list-objects-filter-options.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +02009#include "promisor-remote.h"
Matthew DeVore489fc9e2019-06-27 15:54:12 -070010#include "trace.h"
Matthew DeVoree987df52019-06-27 15:54:08 -070011#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);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000017
Taylor Blaub9ea2142020-07-31 16:26:26 -040018const 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";
Patrick Steinhardtb0c42a52021-04-19 13:46:53 +020032 case LOFC_OBJECT_TYPE:
33 return "object:type";
Taylor Blaub9ea2142020-07-31 16:26:26 -040034 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 }
Martin Ågren5a923bb2020-11-14 09:43:26 +010040 BUG("list_object_filter_config_name: invalid argument '%d'", c);
Taylor Blaub9ea2142020-07-31 16:26:26 -040041}
42
Derrick Stolee105c6f12022-03-09 16:01:39 +000043int gently_parse_list_objects_filter(
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000044 struct list_objects_filter_options *filter_options,
45 const char *arg,
46 struct strbuf *errbuf)
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000047{
48 const char *v0;
49
Christian Couderfa3d1b62019-06-25 15:40:32 +020050 if (!arg)
51 return 0;
52
Matthew DeVoref56f7642019-06-27 15:54:09 -070053 if (filter_options->choice)
54 BUG("filter_options already populated");
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000055
56 if (!strcmp(arg, "blob:none")) {
57 filter_options->choice = LOFC_BLOB_NONE;
58 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000059
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000060 } 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 }
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000065
Matthew DeVorebc5975d2018-10-05 14:31:27 -070066 } else if (skip_prefix(arg, "tree:", &v0)) {
Matthew DeVorec813a7c2019-01-08 18:59:13 -080067 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
Matthew DeVore842b0052019-06-27 15:54:07 -070068 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
Matthew DeVorebc5975d2018-10-05 14:31:27 -070069 return 1;
70 }
Matthew DeVorec813a7c2019-01-08 18:59:13 -080071 filter_options->choice = LOFC_TREE_DEPTH;
Matthew DeVorebc5975d2018-10-05 14:31:27 -070072 return 0;
73
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000074 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
Jeff King4c96a772019-09-15 12:12:44 -040075 filter_options->sparse_oid_name = xstrdup(v0);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000076 filter_options->choice = LOFC_SPARSE_OID;
77 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000078
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000079 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
Christian Coudere6932372019-05-29 14:44:32 +020080 if (errbuf) {
81 strbuf_addstr(
82 errbuf,
83 _("sparse:path filters support has been dropped"));
84 }
85 return 1;
Matthew DeVoree987df52019-06-27 15:54:08 -070086
Patrick Steinhardtb0c42a52021-04-19 13:46:53 +020087 } else if (skip_prefix(arg, "object:type=", &v0)) {
88 int type = type_from_string_gently(v0, strlen(v0), 1);
89 if (type < 0) {
Jean-Noël Avila225f7fa2021-05-20 09:42:14 +020090 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
Patrick Steinhardtb0c42a52021-04-19 13:46:53 +020091 "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
Matthew DeVoree987df52019-06-27 15:54:08 -0700100 } else if (skip_prefix(arg, "combine:", &v0)) {
101 return parse_combine_filter(filter_options, v0, errbuf);
102
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000103 }
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700104 /*
105 * Please update _git_fetch() in git-completion.bash when you
106 * add new filters
107 */
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000108
Matthew DeVore842b0052019-06-27 15:54:07 -0700109 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
Matthew DeVorecc0b05a2018-10-05 14:31:26 -0700110
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000111 memset(filter_options, 0, sizeof(*filter_options));
112 return 1;
113}
114
Matthew DeVoree987df52019-06-27 15:54:08 -0700115static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
116
117static int has_reserved_character(
118 struct strbuf *sub_spec, struct strbuf *errbuf)
119{
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
132 return 0;
133}
134
135static int parse_combine_subfilter(
136 struct list_objects_filter_options *filter_options,
137 struct strbuf *subspec,
138 struct strbuf *errbuf)
139{
Matthew DeVore5a133e82019-06-27 15:54:13 -0700140 size_t new_index = filter_options->sub_nr;
Matthew DeVoree987df52019-06-27 15:54:08 -0700141 char *decoded;
142 int result;
143
Matthew DeVore5a133e82019-06-27 15:54:13 -0700144 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
145 filter_options->sub_alloc);
Matthew DeVoree987df52019-06-27 15:54:08 -0700146
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
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700197static 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)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000206{
207 struct strbuf buf = STRBUF_INIT;
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700208 strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
209 trace_printf("Add to combine filter-spec: %s\n", buf.buf);
210 string_list_append(&filter->filter_spec, strbuf_detach(&buf, NULL));
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));
229 filter_options->sub = sub_array;
230 filter_options->sub_alloc = initial_sub_alloc;
231 }
232 filter_options->sub_nr = 1;
233 filter_options->choice = LOFC_COMBINE;
234 string_list_append(&filter_options->filter_spec, xstrdup("combine:"));
235 filter_spec_append_urlencode(
236 filter_options,
237 list_objects_filter_spec(&filter_options->sub[0]));
238 /*
239 * We don't need the filter_spec strings for subfilter specs, only the
240 * top level.
241 */
242 string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
243}
244
245void list_objects_filter_die_if_populated(
246 struct list_objects_filter_options *filter_options)
247{
Matthew DeVoref56f7642019-06-27 15:54:09 -0700248 if (filter_options->choice)
249 die(_("multiple filter-specs cannot be combined"));
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700250}
251
Matthew DeVore90d21f92019-06-27 15:54:14 -0700252void parse_list_objects_filter(
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700253 struct list_objects_filter_options *filter_options,
254 const char *arg)
255{
256 struct strbuf errbuf = STRBUF_INIT;
257 int parse_error;
258
259 if (!filter_options->choice) {
260 string_list_append(&filter_options->filter_spec, xstrdup(arg));
261
262 parse_error = gently_parse_list_objects_filter(
263 filter_options, arg, &errbuf);
264 } else {
265 /*
266 * Make filter_options an LOFC_COMBINE spec so we can trivially
267 * add subspecs to it.
268 */
269 transform_to_combine_type(filter_options);
270
271 string_list_append(&filter_options->filter_spec, xstrdup("+"));
272 filter_spec_append_urlencode(filter_options, arg);
Matthew DeVore5a133e82019-06-27 15:54:13 -0700273 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
274 filter_options->sub_alloc);
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700275
276 parse_error = gently_parse_list_objects_filter(
Matthew DeVore5a133e82019-06-27 15:54:13 -0700277 &filter_options->sub[filter_options->sub_nr - 1], arg,
278 &errbuf);
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700279 }
280 if (parse_error)
281 die("%s", errbuf.buf);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000282}
283
284int opt_parse_list_objects_filter(const struct option *opt,
285 const char *arg, int unset)
286{
287 struct list_objects_filter_options *filter_options = opt->value;
Ævar Arnfjörð Bjarmason5cb28272022-03-28 17:43:18 +0200288 opt_lof_init init = (opt_lof_init)opt->defval;
289
290 if (init)
291 filter_options = init(opt->value);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000292
Matthew DeVore90d21f92019-06-27 15:54:14 -0700293 if (unset || !arg)
Jeff Hostetleraa57b872017-12-08 15:58:50 +0000294 list_objects_filter_set_no_filter(filter_options);
Matthew DeVore90d21f92019-06-27 15:54:14 -0700295 else
296 parse_list_objects_filter(filter_options, arg);
297 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000298}
Jeff Hostetler4875c972017-12-05 16:50:13 +0000299
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700300const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800301{
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700302 if (!filter->filter_spec.nr)
303 BUG("no filter_spec available for this filter");
304 if (filter->filter_spec.nr != 1) {
305 struct strbuf concatted = STRBUF_INIT;
306 strbuf_add_separated_string_list(
307 &concatted, "", &filter->filter_spec);
308 string_list_clear(&filter->filter_spec, /*free_util=*/0);
309 string_list_append(
310 &filter->filter_spec, strbuf_detach(&concatted, NULL));
311 }
312
313 return filter->filter_spec.items[0].string;
314}
315
316const char *expand_list_objects_filter_spec(
317 struct list_objects_filter_options *filter)
318{
319 if (filter->choice == LOFC_BLOB_LIMIT) {
320 struct strbuf expanded_spec = STRBUF_INIT;
321 strbuf_addf(&expanded_spec, "blob:limit=%lu",
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800322 filter->blob_limit_value);
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700323 string_list_clear(&filter->filter_spec, /*free_util=*/0);
324 string_list_append(
325 &filter->filter_spec,
326 strbuf_detach(&expanded_spec, NULL));
327 }
328
329 return list_objects_filter_spec(filter);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800330}
331
Jeff Hostetler4875c972017-12-05 16:50:13 +0000332void list_objects_filter_release(
333 struct list_objects_filter_options *filter_options)
334{
Matthew DeVoree987df52019-06-27 15:54:08 -0700335 size_t sub;
336
337 if (!filter_options)
338 return;
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700339 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
Jeff King4c96a772019-09-15 12:12:44 -0400340 free(filter_options->sparse_oid_name);
Matthew DeVoree987df52019-06-27 15:54:08 -0700341 for (sub = 0; sub < filter_options->sub_nr; sub++)
342 list_objects_filter_release(&filter_options->sub[sub]);
343 free(filter_options->sub);
Jeff Hostetler4875c972017-12-05 16:50:13 +0000344 memset(filter_options, 0, sizeof(*filter_options));
345}
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000346
347void partial_clone_register(
348 const char *remote,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700349 struct list_objects_filter_options *filter_options)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000350{
Jonathan Tan23547c42020-09-28 15:26:38 -0700351 struct promisor_remote *promisor_remote;
Christian Couderb14ed5a2019-06-25 15:40:31 +0200352 char *cfg_name;
Christian Couderfa3d1b62019-06-25 15:40:32 +0200353 char *filter_name;
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000354
Christian Couderb14ed5a2019-06-25 15:40:31 +0200355 /* Check if it is already registered */
Jonathan Tan23547c42020-09-28 15:26:38 -0700356 if ((promisor_remote = promisor_remote_find(remote))) {
357 if (promisor_remote->partial_clone_filter)
358 /*
359 * Remote is already registered and a filter is already
360 * set, so we don't need to do anything here.
361 */
362 return;
363 } else {
Xin Li16af5f12020-06-05 02:10:01 -0700364 if (upgrade_repository_format(1) < 0)
365 die(_("unable to upgrade repository format to support partial clone"));
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000366
Christian Couderb14ed5a2019-06-25 15:40:31 +0200367 /* Add promisor config for the remote */
368 cfg_name = xstrfmt("remote.%s.promisor", remote);
369 git_config_set(cfg_name, "true");
370 free(cfg_name);
371 }
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000372
373 /*
374 * Record the initial filter-spec in the config as
375 * the default for subsequent fetches from this remote.
376 */
Christian Couderfa3d1b62019-06-25 15:40:32 +0200377 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
Junio C Hamano627b8262019-09-18 11:50:09 -0700378 /* NEEDSWORK: 'expand' result leaking??? */
379 git_config_set(filter_name,
380 expand_list_objects_filter_spec(filter_options));
Christian Couderfa3d1b62019-06-25 15:40:32 +0200381 free(filter_name);
Christian Couderb14ed5a2019-06-25 15:40:31 +0200382
383 /* Make sure the config info are reset */
384 promisor_remote_reinit();
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000385}
386
387void partial_clone_get_default_filter_spec(
Christian Couderfa3d1b62019-06-25 15:40:32 +0200388 struct list_objects_filter_options *filter_options,
389 const char *remote)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000390{
Christian Couderfa3d1b62019-06-25 15:40:32 +0200391 struct promisor_remote *promisor = promisor_remote_find(remote);
Matthew DeVore842b0052019-06-27 15:54:07 -0700392 struct strbuf errbuf = STRBUF_INIT;
Christian Couderfa3d1b62019-06-25 15:40:32 +0200393
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000394 /*
395 * Parse default value, but silently ignore it if it is invalid.
396 */
Junio C Hamano627b8262019-09-18 11:50:09 -0700397 if (!promisor)
Jonathan Tancac11372018-06-11 14:51:26 -0700398 return;
Matthew DeVoree987df52019-06-27 15:54:08 -0700399
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700400 string_list_append(&filter_options->filter_spec,
Junio C Hamano627b8262019-09-18 11:50:09 -0700401 promisor->partial_clone_filter);
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000402 gently_parse_list_objects_filter(filter_options,
Junio C Hamano627b8262019-09-18 11:50:09 -0700403 promisor->partial_clone_filter,
Matthew DeVore842b0052019-06-27 15:54:07 -0700404 &errbuf);
405 strbuf_release(&errbuf);
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000406}
Derrick Stolee4a4c3f92022-03-09 16:01:32 +0000407
408void list_objects_filter_copy(
409 struct list_objects_filter_options *dest,
410 const struct list_objects_filter_options *src)
411{
412 int i;
413 struct string_list_item *item;
414
415 /* Copy everything. We will overwrite the pointers shortly. */
416 memcpy(dest, src, sizeof(struct list_objects_filter_options));
417
418 string_list_init_dup(&dest->filter_spec);
419 for_each_string_list_item(item, &src->filter_spec)
420 string_list_append(&dest->filter_spec, item->string);
421
422 ALLOC_ARRAY(dest->sub, dest->sub_alloc);
423 for (i = 0; i < src->sub_nr; i++)
424 list_objects_filter_copy(&dest->sub[i], &src->sub[i]);
425}