status: fix branch shown when not only bisecting
[alt-git.git] / list-objects-filter-options.c
blob2a3b7881af2bf5960dfbd41934aa3c66ad2a90eb
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "config.h"
5 #include "gettext.h"
6 #include "revision.h"
7 #include "strvec.h"
8 #include "list-objects.h"
9 #include "list-objects-filter.h"
10 #include "list-objects-filter-options.h"
11 #include "promisor-remote.h"
12 #include "trace.h"
13 #include "url.h"
14 #include "parse-options.h"
16 static int parse_combine_filter(
17 struct list_objects_filter_options *filter_options,
18 const char *arg,
19 struct strbuf *errbuf);
21 const char *list_object_filter_config_name(enum list_objects_filter_choice c)
23 switch (c) {
24 case LOFC_DISABLED:
25 /* we have no name for "no filter at all" */
26 break;
27 case LOFC_BLOB_NONE:
28 return "blob:none";
29 case LOFC_BLOB_LIMIT:
30 return "blob:limit";
31 case LOFC_TREE_DEPTH:
32 return "tree";
33 case LOFC_SPARSE_OID:
34 return "sparse:oid";
35 case LOFC_OBJECT_TYPE:
36 return "object:type";
37 case LOFC_COMBINE:
38 return "combine";
39 case LOFC__COUNT:
40 /* not a real filter type; just the count of all filters */
41 break;
43 BUG("list_object_filter_config_name: invalid argument '%d'", c);
46 int gently_parse_list_objects_filter(
47 struct list_objects_filter_options *filter_options,
48 const char *arg,
49 struct strbuf *errbuf)
51 const char *v0;
53 if (!arg)
54 return 0;
56 if (filter_options->choice)
57 BUG("filter_options already populated");
59 if (!strcmp(arg, "blob:none")) {
60 filter_options->choice = LOFC_BLOB_NONE;
61 return 0;
63 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
64 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
65 filter_options->choice = LOFC_BLOB_LIMIT;
66 return 0;
69 } else if (skip_prefix(arg, "tree:", &v0)) {
70 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
71 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
72 return 1;
74 filter_options->choice = LOFC_TREE_DEPTH;
75 return 0;
77 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
78 filter_options->sparse_oid_name = xstrdup(v0);
79 filter_options->choice = LOFC_SPARSE_OID;
80 return 0;
82 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
83 if (errbuf) {
84 strbuf_addstr(
85 errbuf,
86 _("sparse:path filters support has been dropped"));
88 return 1;
90 } else if (skip_prefix(arg, "object:type=", &v0)) {
91 int type = type_from_string_gently(v0, strlen(v0), 1);
92 if (type < 0) {
93 strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "
94 "not a valid object type"), v0);
95 return 1;
98 filter_options->object_type = type;
99 filter_options->choice = LOFC_OBJECT_TYPE;
101 return 0;
103 } else if (skip_prefix(arg, "combine:", &v0)) {
104 return parse_combine_filter(filter_options, v0, errbuf);
108 * Please update _git_fetch() in git-completion.bash when you
109 * add new filters
112 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
114 list_objects_filter_init(filter_options);
115 return 1;
118 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
120 static int has_reserved_character(
121 struct strbuf *sub_spec, struct strbuf *errbuf)
123 const char *c = sub_spec->buf;
124 while (*c) {
125 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
126 strbuf_addf(
127 errbuf,
128 _("must escape char in sub-filter-spec: '%c'"),
129 *c);
130 return 1;
132 c++;
135 return 0;
138 static int parse_combine_subfilter(
139 struct list_objects_filter_options *filter_options,
140 struct strbuf *subspec,
141 struct strbuf *errbuf)
143 size_t new_index = filter_options->sub_nr;
144 char *decoded;
145 int result;
147 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
148 filter_options->sub_alloc);
149 list_objects_filter_init(&filter_options->sub[new_index]);
151 decoded = url_percent_decode(subspec->buf);
153 result = has_reserved_character(subspec, errbuf) ||
154 gently_parse_list_objects_filter(
155 &filter_options->sub[new_index], decoded, errbuf);
157 free(decoded);
158 return result;
161 static int parse_combine_filter(
162 struct list_objects_filter_options *filter_options,
163 const char *arg,
164 struct strbuf *errbuf)
166 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
167 size_t sub;
168 int result = 0;
170 if (!subspecs[0]) {
171 strbuf_addstr(errbuf, _("expected something after combine:"));
172 result = 1;
173 goto cleanup;
176 for (sub = 0; subspecs[sub] && !result; sub++) {
177 if (subspecs[sub + 1]) {
179 * This is not the last subspec. Remove trailing "+" so
180 * we can parse it.
182 size_t last = subspecs[sub]->len - 1;
183 assert(subspecs[sub]->buf[last] == '+');
184 strbuf_remove(subspecs[sub], last, 1);
186 result = parse_combine_subfilter(
187 filter_options, subspecs[sub], errbuf);
190 filter_options->choice = LOFC_COMBINE;
192 cleanup:
193 strbuf_list_free(subspecs);
194 if (result)
195 list_objects_filter_release(filter_options);
196 return result;
199 static int allow_unencoded(char ch)
201 if (ch <= ' ' || ch == '%' || ch == '+')
202 return 0;
203 return !strchr(RESERVED_NON_WS, ch);
206 static void filter_spec_append_urlencode(
207 struct list_objects_filter_options *filter, const char *raw)
209 size_t orig_len = filter->filter_spec.len;
210 strbuf_addstr_urlencode(&filter->filter_spec, raw, allow_unencoded);
211 trace_printf("Add to combine filter-spec: %s\n",
212 filter->filter_spec.buf + orig_len);
216 * Changes filter_options into an equivalent LOFC_COMBINE filter options
217 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
219 static void transform_to_combine_type(
220 struct list_objects_filter_options *filter_options)
222 assert(filter_options->choice);
223 if (filter_options->choice == LOFC_COMBINE)
224 return;
226 const int initial_sub_alloc = 2;
227 struct list_objects_filter_options *sub_array =
228 xcalloc(initial_sub_alloc, sizeof(*sub_array));
229 sub_array[0] = *filter_options;
230 list_objects_filter_init(filter_options);
231 filter_options->sub = sub_array;
232 filter_options->sub_alloc = initial_sub_alloc;
234 filter_options->sub_nr = 1;
235 filter_options->choice = LOFC_COMBINE;
236 strbuf_addstr(&filter_options->filter_spec, "combine:");
237 filter_spec_append_urlencode(
238 filter_options,
239 list_objects_filter_spec(&filter_options->sub[0]));
241 * We don't need the filter_spec strings for subfilter specs, only the
242 * top level.
244 strbuf_release(&filter_options->sub[0].filter_spec);
247 void list_objects_filter_die_if_populated(
248 struct list_objects_filter_options *filter_options)
250 if (filter_options->choice)
251 die(_("multiple filter-specs cannot be combined"));
254 void parse_list_objects_filter(
255 struct list_objects_filter_options *filter_options,
256 const char *arg)
258 struct strbuf errbuf = STRBUF_INIT;
259 int parse_error;
261 if (!filter_options->filter_spec.buf)
262 BUG("filter_options not properly initialized");
264 if (!filter_options->choice) {
265 strbuf_addstr(&filter_options->filter_spec, arg);
267 parse_error = gently_parse_list_objects_filter(
268 filter_options, arg, &errbuf);
269 } else {
270 struct list_objects_filter_options *sub;
273 * Make filter_options an LOFC_COMBINE spec so we can trivially
274 * add subspecs to it.
276 transform_to_combine_type(filter_options);
278 strbuf_addch(&filter_options->filter_spec, '+');
279 filter_spec_append_urlencode(filter_options, arg);
280 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
281 filter_options->sub_alloc);
282 sub = &filter_options->sub[filter_options->sub_nr - 1];
284 list_objects_filter_init(sub);
285 parse_error = gently_parse_list_objects_filter(sub, arg,
286 &errbuf);
288 if (parse_error)
289 die("%s", errbuf.buf);
292 int opt_parse_list_objects_filter(const struct option *opt,
293 const char *arg, int unset)
295 struct list_objects_filter_options *filter_options = opt->value;
297 if (unset || !arg)
298 list_objects_filter_set_no_filter(filter_options);
299 else
300 parse_list_objects_filter(filter_options, arg);
301 return 0;
304 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
306 if (!filter->filter_spec.len)
307 BUG("no filter_spec available for this filter");
308 return filter->filter_spec.buf;
311 const char *expand_list_objects_filter_spec(
312 struct list_objects_filter_options *filter)
314 if (filter->choice == LOFC_BLOB_LIMIT) {
315 strbuf_release(&filter->filter_spec);
316 strbuf_addf(&filter->filter_spec, "blob:limit=%lu",
317 filter->blob_limit_value);
320 return list_objects_filter_spec(filter);
323 void list_objects_filter_release(
324 struct list_objects_filter_options *filter_options)
326 size_t sub;
328 if (!filter_options)
329 return;
330 strbuf_release(&filter_options->filter_spec);
331 free(filter_options->sparse_oid_name);
332 for (sub = 0; sub < filter_options->sub_nr; sub++)
333 list_objects_filter_release(&filter_options->sub[sub]);
334 free(filter_options->sub);
335 list_objects_filter_init(filter_options);
338 void partial_clone_register(
339 const char *remote,
340 struct list_objects_filter_options *filter_options)
342 struct promisor_remote *promisor_remote;
343 char *cfg_name;
344 char *filter_name;
346 /* Check if it is already registered */
347 if ((promisor_remote = repo_promisor_remote_find(the_repository, remote))) {
348 if (promisor_remote->partial_clone_filter)
350 * Remote is already registered and a filter is already
351 * set, so we don't need to do anything here.
353 return;
354 } else {
355 if (upgrade_repository_format(1) < 0)
356 die(_("unable to upgrade repository format to support partial clone"));
358 /* Add promisor config for the remote */
359 cfg_name = xstrfmt("remote.%s.promisor", remote);
360 git_config_set(cfg_name, "true");
361 free(cfg_name);
365 * Record the initial filter-spec in the config as
366 * the default for subsequent fetches from this remote.
368 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
369 /* NEEDSWORK: 'expand' result leaking??? */
370 git_config_set(filter_name,
371 expand_list_objects_filter_spec(filter_options));
372 free(filter_name);
374 /* Make sure the config info are reset */
375 repo_promisor_remote_reinit(the_repository);
378 void partial_clone_get_default_filter_spec(
379 struct list_objects_filter_options *filter_options,
380 const char *remote)
382 struct promisor_remote *promisor = repo_promisor_remote_find(the_repository,
383 remote);
384 struct strbuf errbuf = STRBUF_INIT;
387 * Parse default value, but silently ignore it if it is invalid.
389 if (!promisor || !promisor->partial_clone_filter)
390 return;
392 strbuf_addstr(&filter_options->filter_spec,
393 promisor->partial_clone_filter);
394 gently_parse_list_objects_filter(filter_options,
395 promisor->partial_clone_filter,
396 &errbuf);
397 strbuf_release(&errbuf);
400 void list_objects_filter_copy(
401 struct list_objects_filter_options *dest,
402 const struct list_objects_filter_options *src)
404 int i;
406 /* Copy everything. We will overwrite the pointers shortly. */
407 memcpy(dest, src, sizeof(struct list_objects_filter_options));
409 strbuf_init(&dest->filter_spec, 0);
410 strbuf_addbuf(&dest->filter_spec, &src->filter_spec);
411 dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);
413 ALLOC_ARRAY(dest->sub, dest->sub_alloc);
414 for (i = 0; i < src->sub_nr; i++)
415 list_objects_filter_copy(&dest->sub[i], &src->sub[i]);
418 void list_objects_filter_init(struct list_objects_filter_options *filter_options)
420 struct list_objects_filter_options blank = LIST_OBJECTS_FILTER_INIT;
421 memcpy(filter_options, &blank, sizeof(*filter_options));