treewide: include parse-options.h in source files
[git/debian.git] / list-objects-filter-options.c
blob33134159d7dc822bea00237337182d6ed3b71409
1 #include "cache.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "revision.h"
5 #include "strvec.h"
6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
9 #include "promisor-remote.h"
10 #include "trace.h"
11 #include "url.h"
12 #include "parse-options.h"
14 static int parse_combine_filter(
15 struct list_objects_filter_options *filter_options,
16 const char *arg,
17 struct strbuf *errbuf);
19 const char *list_object_filter_config_name(enum list_objects_filter_choice c)
21 switch (c) {
22 case LOFC_DISABLED:
23 /* we have no name for "no filter at all" */
24 break;
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;
41 BUG("list_object_filter_config_name: invalid argument '%d'", c);
44 int gently_parse_list_objects_filter(
45 struct list_objects_filter_options *filter_options,
46 const char *arg,
47 struct strbuf *errbuf)
49 const char *v0;
51 if (!arg)
52 return 0;
54 if (filter_options->choice)
55 BUG("filter_options already populated");
57 if (!strcmp(arg, "blob:none")) {
58 filter_options->choice = LOFC_BLOB_NONE;
59 return 0;
61 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
62 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
63 filter_options->choice = LOFC_BLOB_LIMIT;
64 return 0;
67 } else if (skip_prefix(arg, "tree:", &v0)) {
68 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
69 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
70 return 1;
72 filter_options->choice = LOFC_TREE_DEPTH;
73 return 0;
75 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
76 filter_options->sparse_oid_name = xstrdup(v0);
77 filter_options->choice = LOFC_SPARSE_OID;
78 return 0;
80 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
81 if (errbuf) {
82 strbuf_addstr(
83 errbuf,
84 _("sparse:path filters support has been dropped"));
86 return 1;
88 } else if (skip_prefix(arg, "object:type=", &v0)) {
89 int type = type_from_string_gently(v0, strlen(v0), 1);
90 if (type < 0) {
91 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
92 "not a valid object type"), v0);
93 return 1;
96 filter_options->object_type = type;
97 filter_options->choice = LOFC_OBJECT_TYPE;
99 return 0;
101 } else if (skip_prefix(arg, "combine:", &v0)) {
102 return parse_combine_filter(filter_options, v0, errbuf);
106 * Please update _git_fetch() in git-completion.bash when you
107 * add new filters
110 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
112 list_objects_filter_init(filter_options);
113 return 1;
116 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
118 static int has_reserved_character(
119 struct strbuf *sub_spec, struct strbuf *errbuf)
121 const char *c = sub_spec->buf;
122 while (*c) {
123 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
124 strbuf_addf(
125 errbuf,
126 _("must escape char in sub-filter-spec: '%c'"),
127 *c);
128 return 1;
130 c++;
133 return 0;
136 static int parse_combine_subfilter(
137 struct list_objects_filter_options *filter_options,
138 struct strbuf *subspec,
139 struct strbuf *errbuf)
141 size_t new_index = filter_options->sub_nr;
142 char *decoded;
143 int result;
145 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
146 filter_options->sub_alloc);
147 list_objects_filter_init(&filter_options->sub[new_index]);
149 decoded = url_percent_decode(subspec->buf);
151 result = has_reserved_character(subspec, errbuf) ||
152 gently_parse_list_objects_filter(
153 &filter_options->sub[new_index], decoded, errbuf);
155 free(decoded);
156 return result;
159 static int parse_combine_filter(
160 struct list_objects_filter_options *filter_options,
161 const char *arg,
162 struct strbuf *errbuf)
164 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
165 size_t sub;
166 int result = 0;
168 if (!subspecs[0]) {
169 strbuf_addstr(errbuf, _("expected something after combine:"));
170 result = 1;
171 goto cleanup;
174 for (sub = 0; subspecs[sub] && !result; sub++) {
175 if (subspecs[sub + 1]) {
177 * This is not the last subspec. Remove trailing "+" so
178 * we can parse it.
180 size_t last = subspecs[sub]->len - 1;
181 assert(subspecs[sub]->buf[last] == '+');
182 strbuf_remove(subspecs[sub], last, 1);
184 result = parse_combine_subfilter(
185 filter_options, subspecs[sub], errbuf);
188 filter_options->choice = LOFC_COMBINE;
190 cleanup:
191 strbuf_list_free(subspecs);
192 if (result)
193 list_objects_filter_release(filter_options);
194 return result;
197 static int allow_unencoded(char ch)
199 if (ch <= ' ' || ch == '%' || ch == '+')
200 return 0;
201 return !strchr(RESERVED_NON_WS, ch);
204 static void filter_spec_append_urlencode(
205 struct list_objects_filter_options *filter, const char *raw)
207 size_t orig_len = filter->filter_spec.len;
208 strbuf_addstr_urlencode(&filter->filter_spec, raw, allow_unencoded);
209 trace_printf("Add to combine filter-spec: %s\n",
210 filter->filter_spec.buf + orig_len);
214 * Changes filter_options into an equivalent LOFC_COMBINE filter options
215 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
217 static void transform_to_combine_type(
218 struct list_objects_filter_options *filter_options)
220 assert(filter_options->choice);
221 if (filter_options->choice == LOFC_COMBINE)
222 return;
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 list_objects_filter_init(filter_options);
229 filter_options->sub = sub_array;
230 filter_options->sub_alloc = initial_sub_alloc;
232 filter_options->sub_nr = 1;
233 filter_options->choice = LOFC_COMBINE;
234 strbuf_addstr(&filter_options->filter_spec, "combine:");
235 filter_spec_append_urlencode(
236 filter_options,
237 list_objects_filter_spec(&filter_options->sub[0]));
239 * We don't need the filter_spec strings for subfilter specs, only the
240 * top level.
242 strbuf_release(&filter_options->sub[0].filter_spec);
245 void list_objects_filter_die_if_populated(
246 struct list_objects_filter_options *filter_options)
248 if (filter_options->choice)
249 die(_("multiple filter-specs cannot be combined"));
252 void parse_list_objects_filter(
253 struct list_objects_filter_options *filter_options,
254 const char *arg)
256 struct strbuf errbuf = STRBUF_INIT;
257 int parse_error;
259 if (!filter_options->filter_spec.buf)
260 BUG("filter_options not properly initialized");
262 if (!filter_options->choice) {
263 strbuf_addstr(&filter_options->filter_spec, arg);
265 parse_error = gently_parse_list_objects_filter(
266 filter_options, arg, &errbuf);
267 } else {
268 struct list_objects_filter_options *sub;
271 * Make filter_options an LOFC_COMBINE spec so we can trivially
272 * add subspecs to it.
274 transform_to_combine_type(filter_options);
276 strbuf_addch(&filter_options->filter_spec, '+');
277 filter_spec_append_urlencode(filter_options, arg);
278 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
279 filter_options->sub_alloc);
280 sub = &filter_options->sub[filter_options->sub_nr - 1];
282 list_objects_filter_init(sub);
283 parse_error = gently_parse_list_objects_filter(sub, arg,
284 &errbuf);
286 if (parse_error)
287 die("%s", errbuf.buf);
290 int opt_parse_list_objects_filter(const struct option *opt,
291 const char *arg, int unset)
293 struct list_objects_filter_options *filter_options = opt->value;
295 if (unset || !arg)
296 list_objects_filter_set_no_filter(filter_options);
297 else
298 parse_list_objects_filter(filter_options, arg);
299 return 0;
302 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
304 if (!filter->filter_spec.len)
305 BUG("no filter_spec available for this filter");
306 return filter->filter_spec.buf;
309 const char *expand_list_objects_filter_spec(
310 struct list_objects_filter_options *filter)
312 if (filter->choice == LOFC_BLOB_LIMIT) {
313 strbuf_release(&filter->filter_spec);
314 strbuf_addf(&filter->filter_spec, "blob:limit=%lu",
315 filter->blob_limit_value);
318 return list_objects_filter_spec(filter);
321 void list_objects_filter_release(
322 struct list_objects_filter_options *filter_options)
324 size_t sub;
326 if (!filter_options)
327 return;
328 strbuf_release(&filter_options->filter_spec);
329 free(filter_options->sparse_oid_name);
330 for (sub = 0; sub < filter_options->sub_nr; sub++)
331 list_objects_filter_release(&filter_options->sub[sub]);
332 free(filter_options->sub);
333 list_objects_filter_init(filter_options);
336 void partial_clone_register(
337 const char *remote,
338 struct list_objects_filter_options *filter_options)
340 struct promisor_remote *promisor_remote;
341 char *cfg_name;
342 char *filter_name;
344 /* Check if it is already registered */
345 if ((promisor_remote = promisor_remote_find(remote))) {
346 if (promisor_remote->partial_clone_filter)
348 * Remote is already registered and a filter is already
349 * set, so we don't need to do anything here.
351 return;
352 } else {
353 if (upgrade_repository_format(1) < 0)
354 die(_("unable to upgrade repository format to support partial clone"));
356 /* Add promisor config for the remote */
357 cfg_name = xstrfmt("remote.%s.promisor", remote);
358 git_config_set(cfg_name, "true");
359 free(cfg_name);
363 * Record the initial filter-spec in the config as
364 * the default for subsequent fetches from this remote.
366 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
367 /* NEEDSWORK: 'expand' result leaking??? */
368 git_config_set(filter_name,
369 expand_list_objects_filter_spec(filter_options));
370 free(filter_name);
372 /* Make sure the config info are reset */
373 promisor_remote_reinit();
376 void partial_clone_get_default_filter_spec(
377 struct list_objects_filter_options *filter_options,
378 const char *remote)
380 struct promisor_remote *promisor = promisor_remote_find(remote);
381 struct strbuf errbuf = STRBUF_INIT;
384 * Parse default value, but silently ignore it if it is invalid.
386 if (!promisor || !promisor->partial_clone_filter)
387 return;
389 strbuf_addstr(&filter_options->filter_spec,
390 promisor->partial_clone_filter);
391 gently_parse_list_objects_filter(filter_options,
392 promisor->partial_clone_filter,
393 &errbuf);
394 strbuf_release(&errbuf);
397 void list_objects_filter_copy(
398 struct list_objects_filter_options *dest,
399 const struct list_objects_filter_options *src)
401 int i;
403 /* Copy everything. We will overwrite the pointers shortly. */
404 memcpy(dest, src, sizeof(struct list_objects_filter_options));
406 strbuf_init(&dest->filter_spec, 0);
407 strbuf_addbuf(&dest->filter_spec, &src->filter_spec);
408 dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);
410 ALLOC_ARRAY(dest->sub, dest->sub_alloc);
411 for (i = 0; i < src->sub_nr; i++)
412 list_objects_filter_copy(&dest->sub[i], &src->sub[i]);
415 void list_objects_filter_init(struct list_objects_filter_options *filter_options)
417 struct list_objects_filter_options blank = LIST_OBJECTS_FILTER_INIT;
418 memcpy(filter_options, &blank, sizeof(*filter_options));