blob: fd8d59f653ab9b04cc709f9d0f4b00f407bf7ae9 [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
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000043/*
Christian Couder1dde5fa2017-12-05 16:50:12 +000044 * Parse value of the argument to the "filter" keyword.
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000045 * On the command line this looks like:
46 * --filter=<arg>
47 * and in the pack protocol as:
48 * "filter" SP <arg>
49 *
50 * The filter keyword will be used by many commands.
51 * See Documentation/rev-list-options.txt for allowed values for <arg>.
52 *
53 * Capture the given arg as the "filter_spec". This can be forwarded to
Josh Steadmon87c2d9d2019-01-07 16:17:09 -080054 * subordinate commands when necessary (although it's better to pass it through
55 * expand_list_objects_filter_spec() first). We also "intern" the arg for the
56 * convenience of the current command.
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000057 */
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000058static int gently_parse_list_objects_filter(
59 struct list_objects_filter_options *filter_options,
60 const char *arg,
61 struct strbuf *errbuf)
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000062{
63 const char *v0;
64
Christian Couderfa3d1b62019-06-25 15:40:32 +020065 if (!arg)
66 return 0;
67
Matthew DeVoref56f7642019-06-27 15:54:09 -070068 if (filter_options->choice)
69 BUG("filter_options already populated");
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000070
71 if (!strcmp(arg, "blob:none")) {
72 filter_options->choice = LOFC_BLOB_NONE;
73 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000074
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000075 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
76 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
77 filter_options->choice = LOFC_BLOB_LIMIT;
78 return 0;
79 }
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000080
Matthew DeVorebc5975d2018-10-05 14:31:27 -070081 } else if (skip_prefix(arg, "tree:", &v0)) {
Matthew DeVorec813a7c2019-01-08 18:59:13 -080082 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
Matthew DeVore842b0052019-06-27 15:54:07 -070083 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
Matthew DeVorebc5975d2018-10-05 14:31:27 -070084 return 1;
85 }
Matthew DeVorec813a7c2019-01-08 18:59:13 -080086 filter_options->choice = LOFC_TREE_DEPTH;
Matthew DeVorebc5975d2018-10-05 14:31:27 -070087 return 0;
88
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000089 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
Jeff King4c96a772019-09-15 12:12:44 -040090 filter_options->sparse_oid_name = xstrdup(v0);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000091 filter_options->choice = LOFC_SPARSE_OID;
92 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +000093
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +000094 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
Christian Coudere6932372019-05-29 14:44:32 +020095 if (errbuf) {
96 strbuf_addstr(
97 errbuf,
98 _("sparse:path filters support has been dropped"));
99 }
100 return 1;
Matthew DeVoree987df52019-06-27 15:54:08 -0700101
Patrick Steinhardtb0c42a52021-04-19 13:46:53 +0200102 } else if (skip_prefix(arg, "object:type=", &v0)) {
103 int type = type_from_string_gently(v0, strlen(v0), 1);
104 if (type < 0) {
Jean-Noël Avila225f7fa2021-05-20 09:42:14 +0200105 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
Patrick Steinhardtb0c42a52021-04-19 13:46:53 +0200106 "not a valid object type"), v0);
107 return 1;
108 }
109
110 filter_options->object_type = type;
111 filter_options->choice = LOFC_OBJECT_TYPE;
112
113 return 0;
114
Matthew DeVoree987df52019-06-27 15:54:08 -0700115 } else if (skip_prefix(arg, "combine:", &v0)) {
116 return parse_combine_filter(filter_options, v0, errbuf);
117
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000118 }
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700119 /*
120 * Please update _git_fetch() in git-completion.bash when you
121 * add new filters
122 */
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000123
Matthew DeVore842b0052019-06-27 15:54:07 -0700124 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
Matthew DeVorecc0b05a2018-10-05 14:31:26 -0700125
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000126 memset(filter_options, 0, sizeof(*filter_options));
127 return 1;
128}
129
Matthew DeVoree987df52019-06-27 15:54:08 -0700130static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
131
132static int has_reserved_character(
133 struct strbuf *sub_spec, struct strbuf *errbuf)
134{
135 const char *c = sub_spec->buf;
136 while (*c) {
137 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
138 strbuf_addf(
139 errbuf,
140 _("must escape char in sub-filter-spec: '%c'"),
141 *c);
142 return 1;
143 }
144 c++;
145 }
146
147 return 0;
148}
149
150static int parse_combine_subfilter(
151 struct list_objects_filter_options *filter_options,
152 struct strbuf *subspec,
153 struct strbuf *errbuf)
154{
Matthew DeVore5a133e82019-06-27 15:54:13 -0700155 size_t new_index = filter_options->sub_nr;
Matthew DeVoree987df52019-06-27 15:54:08 -0700156 char *decoded;
157 int result;
158
Matthew DeVore5a133e82019-06-27 15:54:13 -0700159 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
160 filter_options->sub_alloc);
Matthew DeVoree987df52019-06-27 15:54:08 -0700161
162 decoded = url_percent_decode(subspec->buf);
163
164 result = has_reserved_character(subspec, errbuf) ||
165 gently_parse_list_objects_filter(
166 &filter_options->sub[new_index], decoded, errbuf);
167
168 free(decoded);
169 return result;
170}
171
172static int parse_combine_filter(
173 struct list_objects_filter_options *filter_options,
174 const char *arg,
175 struct strbuf *errbuf)
176{
177 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
178 size_t sub;
179 int result = 0;
180
181 if (!subspecs[0]) {
182 strbuf_addstr(errbuf, _("expected something after combine:"));
183 result = 1;
184 goto cleanup;
185 }
186
187 for (sub = 0; subspecs[sub] && !result; sub++) {
188 if (subspecs[sub + 1]) {
189 /*
190 * This is not the last subspec. Remove trailing "+" so
191 * we can parse it.
192 */
193 size_t last = subspecs[sub]->len - 1;
194 assert(subspecs[sub]->buf[last] == '+');
195 strbuf_remove(subspecs[sub], last, 1);
196 }
197 result = parse_combine_subfilter(
198 filter_options, subspecs[sub], errbuf);
199 }
200
201 filter_options->choice = LOFC_COMBINE;
202
203cleanup:
204 strbuf_list_free(subspecs);
205 if (result) {
206 list_objects_filter_release(filter_options);
207 memset(filter_options, 0, sizeof(*filter_options));
208 }
209 return result;
210}
211
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700212static int allow_unencoded(char ch)
213{
214 if (ch <= ' ' || ch == '%' || ch == '+')
215 return 0;
216 return !strchr(RESERVED_NON_WS, ch);
217}
218
219static void filter_spec_append_urlencode(
220 struct list_objects_filter_options *filter, const char *raw)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000221{
222 struct strbuf buf = STRBUF_INIT;
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700223 strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
224 trace_printf("Add to combine filter-spec: %s\n", buf.buf);
225 string_list_append(&filter->filter_spec, strbuf_detach(&buf, NULL));
226}
227
228/*
229 * Changes filter_options into an equivalent LOFC_COMBINE filter options
230 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
231 */
232static void transform_to_combine_type(
233 struct list_objects_filter_options *filter_options)
234{
235 assert(filter_options->choice);
236 if (filter_options->choice == LOFC_COMBINE)
237 return;
238 {
239 const int initial_sub_alloc = 2;
240 struct list_objects_filter_options *sub_array =
241 xcalloc(initial_sub_alloc, sizeof(*sub_array));
242 sub_array[0] = *filter_options;
243 memset(filter_options, 0, sizeof(*filter_options));
244 filter_options->sub = sub_array;
245 filter_options->sub_alloc = initial_sub_alloc;
246 }
247 filter_options->sub_nr = 1;
248 filter_options->choice = LOFC_COMBINE;
249 string_list_append(&filter_options->filter_spec, xstrdup("combine:"));
250 filter_spec_append_urlencode(
251 filter_options,
252 list_objects_filter_spec(&filter_options->sub[0]));
253 /*
254 * We don't need the filter_spec strings for subfilter specs, only the
255 * top level.
256 */
257 string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
258}
259
260void list_objects_filter_die_if_populated(
261 struct list_objects_filter_options *filter_options)
262{
Matthew DeVoref56f7642019-06-27 15:54:09 -0700263 if (filter_options->choice)
264 die(_("multiple filter-specs cannot be combined"));
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700265}
266
Matthew DeVore90d21f92019-06-27 15:54:14 -0700267void parse_list_objects_filter(
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700268 struct list_objects_filter_options *filter_options,
269 const char *arg)
270{
271 struct strbuf errbuf = STRBUF_INIT;
272 int parse_error;
273
274 if (!filter_options->choice) {
275 string_list_append(&filter_options->filter_spec, xstrdup(arg));
276
277 parse_error = gently_parse_list_objects_filter(
278 filter_options, arg, &errbuf);
279 } else {
280 /*
281 * Make filter_options an LOFC_COMBINE spec so we can trivially
282 * add subspecs to it.
283 */
284 transform_to_combine_type(filter_options);
285
286 string_list_append(&filter_options->filter_spec, xstrdup("+"));
287 filter_spec_append_urlencode(filter_options, arg);
Matthew DeVore5a133e82019-06-27 15:54:13 -0700288 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
289 filter_options->sub_alloc);
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700290
291 parse_error = gently_parse_list_objects_filter(
Matthew DeVore5a133e82019-06-27 15:54:13 -0700292 &filter_options->sub[filter_options->sub_nr - 1], arg,
293 &errbuf);
Matthew DeVore489fc9e2019-06-27 15:54:12 -0700294 }
295 if (parse_error)
296 die("%s", errbuf.buf);
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000297}
298
299int opt_parse_list_objects_filter(const struct option *opt,
300 const char *arg, int unset)
301{
302 struct list_objects_filter_options *filter_options = opt->value;
303
Matthew DeVore90d21f92019-06-27 15:54:14 -0700304 if (unset || !arg)
Jeff Hostetleraa57b872017-12-08 15:58:50 +0000305 list_objects_filter_set_no_filter(filter_options);
Matthew DeVore90d21f92019-06-27 15:54:14 -0700306 else
307 parse_list_objects_filter(filter_options, arg);
308 return 0;
Jeff Hostetler25ec7bc2017-11-21 20:58:50 +0000309}
Jeff Hostetler4875c972017-12-05 16:50:13 +0000310
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700311const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800312{
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700313 if (!filter->filter_spec.nr)
314 BUG("no filter_spec available for this filter");
315 if (filter->filter_spec.nr != 1) {
316 struct strbuf concatted = STRBUF_INIT;
317 strbuf_add_separated_string_list(
318 &concatted, "", &filter->filter_spec);
319 string_list_clear(&filter->filter_spec, /*free_util=*/0);
320 string_list_append(
321 &filter->filter_spec, strbuf_detach(&concatted, NULL));
322 }
323
324 return filter->filter_spec.items[0].string;
325}
326
327const char *expand_list_objects_filter_spec(
328 struct list_objects_filter_options *filter)
329{
330 if (filter->choice == LOFC_BLOB_LIMIT) {
331 struct strbuf expanded_spec = STRBUF_INIT;
332 strbuf_addf(&expanded_spec, "blob:limit=%lu",
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800333 filter->blob_limit_value);
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700334 string_list_clear(&filter->filter_spec, /*free_util=*/0);
335 string_list_append(
336 &filter->filter_spec,
337 strbuf_detach(&expanded_spec, NULL));
338 }
339
340 return list_objects_filter_spec(filter);
Josh Steadmon87c2d9d2019-01-07 16:17:09 -0800341}
342
Jeff Hostetler4875c972017-12-05 16:50:13 +0000343void list_objects_filter_release(
344 struct list_objects_filter_options *filter_options)
345{
Matthew DeVoree987df52019-06-27 15:54:08 -0700346 size_t sub;
347
348 if (!filter_options)
349 return;
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700350 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
Jeff King4c96a772019-09-15 12:12:44 -0400351 free(filter_options->sparse_oid_name);
Matthew DeVoree987df52019-06-27 15:54:08 -0700352 for (sub = 0; sub < filter_options->sub_nr; sub++)
353 list_objects_filter_release(&filter_options->sub[sub]);
354 free(filter_options->sub);
Jeff Hostetler4875c972017-12-05 16:50:13 +0000355 memset(filter_options, 0, sizeof(*filter_options));
356}
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000357
358void partial_clone_register(
359 const char *remote,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700360 struct list_objects_filter_options *filter_options)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000361{
Jonathan Tan23547c42020-09-28 15:26:38 -0700362 struct promisor_remote *promisor_remote;
Christian Couderb14ed5a2019-06-25 15:40:31 +0200363 char *cfg_name;
Christian Couderfa3d1b62019-06-25 15:40:32 +0200364 char *filter_name;
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000365
Christian Couderb14ed5a2019-06-25 15:40:31 +0200366 /* Check if it is already registered */
Jonathan Tan23547c42020-09-28 15:26:38 -0700367 if ((promisor_remote = promisor_remote_find(remote))) {
368 if (promisor_remote->partial_clone_filter)
369 /*
370 * Remote is already registered and a filter is already
371 * set, so we don't need to do anything here.
372 */
373 return;
374 } else {
Xin Li16af5f12020-06-05 02:10:01 -0700375 if (upgrade_repository_format(1) < 0)
376 die(_("unable to upgrade repository format to support partial clone"));
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000377
Christian Couderb14ed5a2019-06-25 15:40:31 +0200378 /* Add promisor config for the remote */
379 cfg_name = xstrfmt("remote.%s.promisor", remote);
380 git_config_set(cfg_name, "true");
381 free(cfg_name);
382 }
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000383
384 /*
385 * Record the initial filter-spec in the config as
386 * the default for subsequent fetches from this remote.
387 */
Christian Couderfa3d1b62019-06-25 15:40:32 +0200388 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
Junio C Hamano627b8262019-09-18 11:50:09 -0700389 /* NEEDSWORK: 'expand' result leaking??? */
390 git_config_set(filter_name,
391 expand_list_objects_filter_spec(filter_options));
Christian Couderfa3d1b62019-06-25 15:40:32 +0200392 free(filter_name);
Christian Couderb14ed5a2019-06-25 15:40:31 +0200393
394 /* Make sure the config info are reset */
395 promisor_remote_reinit();
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000396}
397
398void partial_clone_get_default_filter_spec(
Christian Couderfa3d1b62019-06-25 15:40:32 +0200399 struct list_objects_filter_options *filter_options,
400 const char *remote)
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000401{
Christian Couderfa3d1b62019-06-25 15:40:32 +0200402 struct promisor_remote *promisor = promisor_remote_find(remote);
Matthew DeVore842b0052019-06-27 15:54:07 -0700403 struct strbuf errbuf = STRBUF_INIT;
Christian Couderfa3d1b62019-06-25 15:40:32 +0200404
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000405 /*
406 * Parse default value, but silently ignore it if it is invalid.
407 */
Junio C Hamano627b8262019-09-18 11:50:09 -0700408 if (!promisor)
Jonathan Tancac11372018-06-11 14:51:26 -0700409 return;
Matthew DeVoree987df52019-06-27 15:54:08 -0700410
Matthew DeVorecf9ceb52019-06-27 15:54:10 -0700411 string_list_append(&filter_options->filter_spec,
Junio C Hamano627b8262019-09-18 11:50:09 -0700412 promisor->partial_clone_filter);
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000413 gently_parse_list_objects_filter(filter_options,
Junio C Hamano627b8262019-09-18 11:50:09 -0700414 promisor->partial_clone_filter,
Matthew DeVore842b0052019-06-27 15:54:07 -0700415 &errbuf);
416 strbuf_release(&errbuf);
Jeff Hostetler1e1e39b2017-12-08 15:58:45 +0000417}