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