commit-graph: retain commit slab when closing NULL commit_graph
[git.git] / list-objects-filter-options.c
blob8a08b7af49c1ff7750296508102bf67807027bea
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "revision.h"
6 #include "strvec.h"
7 #include "list-objects.h"
8 #include "list-objects-filter.h"
9 #include "list-objects-filter-options.h"
10 #include "promisor-remote.h"
11 #include "trace.h"
12 #include "url.h"
13 #include "parse-options.h"
15 static int parse_combine_filter(
16 struct list_objects_filter_options *filter_options,
17 const char *arg,
18 struct strbuf *errbuf);
20 const char *list_object_filter_config_name(enum list_objects_filter_choice c)
22 switch (c) {
23 case LOFC_DISABLED:
24 /* we have no name for "no filter at all" */
25 break;
26 case LOFC_BLOB_NONE:
27 return "blob:none";
28 case LOFC_BLOB_LIMIT:
29 return "blob:limit";
30 case LOFC_TREE_DEPTH:
31 return "tree";
32 case LOFC_SPARSE_OID:
33 return "sparse:oid";
34 case LOFC_OBJECT_TYPE:
35 return "object:type";
36 case LOFC_COMBINE:
37 return "combine";
38 case LOFC__COUNT:
39 /* not a real filter type; just the count of all filters */
40 break;
42 BUG("list_object_filter_config_name: invalid argument '%d'", c);
45 int gently_parse_list_objects_filter(
46 struct list_objects_filter_options *filter_options,
47 const char *arg,
48 struct strbuf *errbuf)
50 const char *v0;
52 if (!arg)
53 return 0;
55 if (filter_options->choice)
56 BUG("filter_options already populated");
58 if (!strcmp(arg, "blob:none")) {
59 filter_options->choice = LOFC_BLOB_NONE;
60 return 0;
62 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
63 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
64 filter_options->choice = LOFC_BLOB_LIMIT;
65 return 0;
68 } else if (skip_prefix(arg, "tree:", &v0)) {
69 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
70 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
71 return 1;
73 filter_options->choice = LOFC_TREE_DEPTH;
74 return 0;
76 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
77 filter_options->sparse_oid_name = xstrdup(v0);
78 filter_options->choice = LOFC_SPARSE_OID;
79 return 0;
81 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
82 if (errbuf) {
83 strbuf_addstr(
84 errbuf,
85 _("sparse:path filters support has been dropped"));
87 return 1;
89 } else if (skip_prefix(arg, "object:type=", &v0)) {
90 int type = type_from_string_gently(v0, strlen(v0), 1);
91 if (type < 0) {
92 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
93 "not a valid object type"), v0);
94 return 1;
97 filter_options->object_type = type;
98 filter_options->choice = LOFC_OBJECT_TYPE;
100 return 0;
102 } else if (skip_prefix(arg, "combine:", &v0)) {
103 return parse_combine_filter(filter_options, v0, errbuf);
107 * Please update _git_fetch() in git-completion.bash when you
108 * add new filters
111 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
113 list_objects_filter_init(filter_options);
114 return 1;
117 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
119 static int has_reserved_character(
120 struct strbuf *sub_spec, struct strbuf *errbuf)
122 const char *c = sub_spec->buf;
123 while (*c) {
124 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
125 strbuf_addf(
126 errbuf,
127 _("must escape char in sub-filter-spec: '%c'"),
128 *c);
129 return 1;
131 c++;
134 return 0;
137 static int parse_combine_subfilter(
138 struct list_objects_filter_options *filter_options,
139 struct strbuf *subspec,
140 struct strbuf *errbuf)
142 size_t new_index = filter_options->sub_nr;
143 char *decoded;
144 int result;
146 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
147 filter_options->sub_alloc);
148 list_objects_filter_init(&filter_options->sub[new_index]);
150 decoded = url_percent_decode(subspec->buf);
152 result = has_reserved_character(subspec, errbuf) ||
153 gently_parse_list_objects_filter(
154 &filter_options->sub[new_index], decoded, errbuf);
156 free(decoded);
157 return result;
160 static int parse_combine_filter(
161 struct list_objects_filter_options *filter_options,
162 const char *arg,
163 struct strbuf *errbuf)
165 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
166 size_t sub;
167 int result = 0;
169 if (!subspecs[0]) {
170 strbuf_addstr(errbuf, _("expected something after combine:"));
171 result = 1;
172 goto cleanup;
175 for (sub = 0; subspecs[sub] && !result; sub++) {
176 if (subspecs[sub + 1]) {
178 * This is not the last subspec. Remove trailing "+" so
179 * we can parse it.
181 size_t last = subspecs[sub]->len - 1;
182 assert(subspecs[sub]->buf[last] == '+');
183 strbuf_remove(subspecs[sub], last, 1);
185 result = parse_combine_subfilter(
186 filter_options, subspecs[sub], errbuf);
189 filter_options->choice = LOFC_COMBINE;
191 cleanup:
192 strbuf_list_free(subspecs);
193 if (result)
194 list_objects_filter_release(filter_options);
195 return result;
198 static int allow_unencoded(char ch)
200 if (ch <= ' ' || ch == '%' || ch == '+')
201 return 0;
202 return !strchr(RESERVED_NON_WS, ch);
205 static void filter_spec_append_urlencode(
206 struct list_objects_filter_options *filter, const char *raw)
208 size_t orig_len = filter->filter_spec.len;
209 strbuf_addstr_urlencode(&filter->filter_spec, raw, allow_unencoded);
210 trace_printf("Add to combine filter-spec: %s\n",
211 filter->filter_spec.buf + orig_len);
215 * Changes filter_options into an equivalent LOFC_COMBINE filter options
216 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
218 static void transform_to_combine_type(
219 struct list_objects_filter_options *filter_options)
221 assert(filter_options->choice);
222 if (filter_options->choice == LOFC_COMBINE)
223 return;
225 const int initial_sub_alloc = 2;
226 struct list_objects_filter_options *sub_array =
227 xcalloc(initial_sub_alloc, sizeof(*sub_array));
228 sub_array[0] = *filter_options;
229 list_objects_filter_init(filter_options);
230 filter_options->sub = sub_array;
231 filter_options->sub_alloc = initial_sub_alloc;
233 filter_options->sub_nr = 1;
234 filter_options->choice = LOFC_COMBINE;
235 strbuf_addstr(&filter_options->filter_spec, "combine:");
236 filter_spec_append_urlencode(
237 filter_options,
238 list_objects_filter_spec(&filter_options->sub[0]));
240 * We don't need the filter_spec strings for subfilter specs, only the
241 * top level.
243 strbuf_release(&filter_options->sub[0].filter_spec);
246 void list_objects_filter_die_if_populated(
247 struct list_objects_filter_options *filter_options)
249 if (filter_options->choice)
250 die(_("multiple filter-specs cannot be combined"));
253 void parse_list_objects_filter(
254 struct list_objects_filter_options *filter_options,
255 const char *arg)
257 struct strbuf errbuf = STRBUF_INIT;
258 int parse_error;
260 if (!filter_options->filter_spec.buf)
261 BUG("filter_options not properly initialized");
263 if (!filter_options->choice) {
264 strbuf_addstr(&filter_options->filter_spec, arg);
266 parse_error = gently_parse_list_objects_filter(
267 filter_options, arg, &errbuf);
268 } else {
269 struct list_objects_filter_options *sub;
272 * Make filter_options an LOFC_COMBINE spec so we can trivially
273 * add subspecs to it.
275 transform_to_combine_type(filter_options);
277 strbuf_addch(&filter_options->filter_spec, '+');
278 filter_spec_append_urlencode(filter_options, arg);
279 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
280 filter_options->sub_alloc);
281 sub = &filter_options->sub[filter_options->sub_nr - 1];
283 list_objects_filter_init(sub);
284 parse_error = gently_parse_list_objects_filter(sub, arg,
285 &errbuf);
287 if (parse_error)
288 die("%s", errbuf.buf);
291 int opt_parse_list_objects_filter(const struct option *opt,
292 const char *arg, int unset)
294 struct list_objects_filter_options *filter_options = opt->value;
296 if (unset || !arg)
297 list_objects_filter_set_no_filter(filter_options);
298 else
299 parse_list_objects_filter(filter_options, arg);
300 return 0;
303 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
305 if (!filter->filter_spec.len)
306 BUG("no filter_spec available for this filter");
307 return filter->filter_spec.buf;
310 const char *expand_list_objects_filter_spec(
311 struct list_objects_filter_options *filter)
313 if (filter->choice == LOFC_BLOB_LIMIT) {
314 strbuf_release(&filter->filter_spec);
315 strbuf_addf(&filter->filter_spec, "blob:limit=%lu",
316 filter->blob_limit_value);
319 return list_objects_filter_spec(filter);
322 void list_objects_filter_release(
323 struct list_objects_filter_options *filter_options)
325 size_t sub;
327 if (!filter_options)
328 return;
329 strbuf_release(&filter_options->filter_spec);
330 free(filter_options->sparse_oid_name);
331 for (sub = 0; sub < filter_options->sub_nr; sub++)
332 list_objects_filter_release(&filter_options->sub[sub]);
333 free(filter_options->sub);
334 list_objects_filter_init(filter_options);
337 void partial_clone_register(
338 const char *remote,
339 struct list_objects_filter_options *filter_options)
341 struct promisor_remote *promisor_remote;
342 char *cfg_name;
343 char *filter_name;
345 /* Check if it is already registered */
346 if ((promisor_remote = repo_promisor_remote_find(the_repository, remote))) {
347 if (promisor_remote->partial_clone_filter)
349 * Remote is already registered and a filter is already
350 * set, so we don't need to do anything here.
352 return;
353 } else {
354 if (upgrade_repository_format(1) < 0)
355 die(_("unable to upgrade repository format to support partial clone"));
357 /* Add promisor config for the remote */
358 cfg_name = xstrfmt("remote.%s.promisor", remote);
359 git_config_set(cfg_name, "true");
360 free(cfg_name);
364 * Record the initial filter-spec in the config as
365 * the default for subsequent fetches from this remote.
367 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
368 /* NEEDSWORK: 'expand' result leaking??? */
369 git_config_set(filter_name,
370 expand_list_objects_filter_spec(filter_options));
371 free(filter_name);
373 /* Make sure the config info are reset */
374 repo_promisor_remote_reinit(the_repository);
377 void partial_clone_get_default_filter_spec(
378 struct list_objects_filter_options *filter_options,
379 const char *remote)
381 struct promisor_remote *promisor = repo_promisor_remote_find(the_repository,
382 remote);
383 struct strbuf errbuf = STRBUF_INIT;
386 * Parse default value, but silently ignore it if it is invalid.
388 if (!promisor || !promisor->partial_clone_filter)
389 return;
391 strbuf_addstr(&filter_options->filter_spec,
392 promisor->partial_clone_filter);
393 gently_parse_list_objects_filter(filter_options,
394 promisor->partial_clone_filter,
395 &errbuf);
396 strbuf_release(&errbuf);
399 void list_objects_filter_copy(
400 struct list_objects_filter_options *dest,
401 const struct list_objects_filter_options *src)
403 int i;
405 /* Copy everything. We will overwrite the pointers shortly. */
406 memcpy(dest, src, sizeof(struct list_objects_filter_options));
408 strbuf_init(&dest->filter_spec, 0);
409 strbuf_addbuf(&dest->filter_spec, &src->filter_spec);
410 dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);
412 ALLOC_ARRAY(dest->sub, dest->sub_alloc);
413 for (i = 0; i < src->sub_nr; i++)
414 list_objects_filter_copy(&dest->sub[i], &src->sub[i]);
417 void list_objects_filter_init(struct list_objects_filter_options *filter_options)
419 struct list_objects_filter_options blank = LIST_OBJECTS_FILTER_INIT;
420 memcpy(filter_options, &blank, sizeof(*filter_options));