upload-pack: remove superfluous sigchain_pop() call
[alt-git.git] / list-objects-filter-options.c
blob3553ad7b0a759e13258a267960caf2ee15b6f019
1 #include "cache.h"
2 #include "commit.h"
3 #include "config.h"
4 #include "revision.h"
5 #include "argv-array.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"
13 static int parse_combine_filter(
14 struct list_objects_filter_options *filter_options,
15 const char *arg,
16 struct strbuf *errbuf);
19 * Parse value of the argument to the "filter" keyword.
20 * On the command line this looks like:
21 * --filter=<arg>
22 * and in the pack protocol as:
23 * "filter" SP <arg>
25 * The filter keyword will be used by many commands.
26 * See Documentation/rev-list-options.txt for allowed values for <arg>.
28 * Capture the given arg as the "filter_spec". This can be forwarded to
29 * subordinate commands when necessary (although it's better to pass it through
30 * expand_list_objects_filter_spec() first). We also "intern" the arg for the
31 * convenience of the current command.
33 static int gently_parse_list_objects_filter(
34 struct list_objects_filter_options *filter_options,
35 const char *arg,
36 struct strbuf *errbuf)
38 const char *v0;
40 if (!arg)
41 return 0;
43 if (filter_options->choice)
44 BUG("filter_options already populated");
46 if (!strcmp(arg, "blob:none")) {
47 filter_options->choice = LOFC_BLOB_NONE;
48 return 0;
50 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
51 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
52 filter_options->choice = LOFC_BLOB_LIMIT;
53 return 0;
56 } else if (skip_prefix(arg, "tree:", &v0)) {
57 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
58 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
59 return 1;
61 filter_options->choice = LOFC_TREE_DEPTH;
62 return 0;
64 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
65 filter_options->sparse_oid_name = xstrdup(v0);
66 filter_options->choice = LOFC_SPARSE_OID;
67 return 0;
69 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
70 if (errbuf) {
71 strbuf_addstr(
72 errbuf,
73 _("sparse:path filters support has been dropped"));
75 return 1;
77 } else if (skip_prefix(arg, "combine:", &v0)) {
78 return parse_combine_filter(filter_options, v0, errbuf);
82 * Please update _git_fetch() in git-completion.bash when you
83 * add new filters
86 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
88 memset(filter_options, 0, sizeof(*filter_options));
89 return 1;
92 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
94 static int has_reserved_character(
95 struct strbuf *sub_spec, struct strbuf *errbuf)
97 const char *c = sub_spec->buf;
98 while (*c) {
99 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
100 strbuf_addf(
101 errbuf,
102 _("must escape char in sub-filter-spec: '%c'"),
103 *c);
104 return 1;
106 c++;
109 return 0;
112 static int parse_combine_subfilter(
113 struct list_objects_filter_options *filter_options,
114 struct strbuf *subspec,
115 struct strbuf *errbuf)
117 size_t new_index = filter_options->sub_nr;
118 char *decoded;
119 int result;
121 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
122 filter_options->sub_alloc);
124 decoded = url_percent_decode(subspec->buf);
126 result = has_reserved_character(subspec, errbuf) ||
127 gently_parse_list_objects_filter(
128 &filter_options->sub[new_index], decoded, errbuf);
130 free(decoded);
131 return result;
134 static int parse_combine_filter(
135 struct list_objects_filter_options *filter_options,
136 const char *arg,
137 struct strbuf *errbuf)
139 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
140 size_t sub;
141 int result = 0;
143 if (!subspecs[0]) {
144 strbuf_addstr(errbuf, _("expected something after combine:"));
145 result = 1;
146 goto cleanup;
149 for (sub = 0; subspecs[sub] && !result; sub++) {
150 if (subspecs[sub + 1]) {
152 * This is not the last subspec. Remove trailing "+" so
153 * we can parse it.
155 size_t last = subspecs[sub]->len - 1;
156 assert(subspecs[sub]->buf[last] == '+');
157 strbuf_remove(subspecs[sub], last, 1);
159 result = parse_combine_subfilter(
160 filter_options, subspecs[sub], errbuf);
163 filter_options->choice = LOFC_COMBINE;
165 cleanup:
166 strbuf_list_free(subspecs);
167 if (result) {
168 list_objects_filter_release(filter_options);
169 memset(filter_options, 0, sizeof(*filter_options));
171 return result;
174 static int allow_unencoded(char ch)
176 if (ch <= ' ' || ch == '%' || ch == '+')
177 return 0;
178 return !strchr(RESERVED_NON_WS, ch);
181 static void filter_spec_append_urlencode(
182 struct list_objects_filter_options *filter, const char *raw)
184 struct strbuf buf = STRBUF_INIT;
185 strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
186 trace_printf("Add to combine filter-spec: %s\n", buf.buf);
187 string_list_append(&filter->filter_spec, strbuf_detach(&buf, NULL));
191 * Changes filter_options into an equivalent LOFC_COMBINE filter options
192 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
194 static void transform_to_combine_type(
195 struct list_objects_filter_options *filter_options)
197 assert(filter_options->choice);
198 if (filter_options->choice == LOFC_COMBINE)
199 return;
201 const int initial_sub_alloc = 2;
202 struct list_objects_filter_options *sub_array =
203 xcalloc(initial_sub_alloc, sizeof(*sub_array));
204 sub_array[0] = *filter_options;
205 memset(filter_options, 0, sizeof(*filter_options));
206 filter_options->sub = sub_array;
207 filter_options->sub_alloc = initial_sub_alloc;
209 filter_options->sub_nr = 1;
210 filter_options->choice = LOFC_COMBINE;
211 string_list_append(&filter_options->filter_spec, xstrdup("combine:"));
212 filter_spec_append_urlencode(
213 filter_options,
214 list_objects_filter_spec(&filter_options->sub[0]));
216 * We don't need the filter_spec strings for subfilter specs, only the
217 * top level.
219 string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
222 void list_objects_filter_die_if_populated(
223 struct list_objects_filter_options *filter_options)
225 if (filter_options->choice)
226 die(_("multiple filter-specs cannot be combined"));
229 void parse_list_objects_filter(
230 struct list_objects_filter_options *filter_options,
231 const char *arg)
233 struct strbuf errbuf = STRBUF_INIT;
234 int parse_error;
236 if (!filter_options->choice) {
237 string_list_append(&filter_options->filter_spec, xstrdup(arg));
239 parse_error = gently_parse_list_objects_filter(
240 filter_options, arg, &errbuf);
241 } else {
243 * Make filter_options an LOFC_COMBINE spec so we can trivially
244 * add subspecs to it.
246 transform_to_combine_type(filter_options);
248 string_list_append(&filter_options->filter_spec, xstrdup("+"));
249 filter_spec_append_urlencode(filter_options, arg);
250 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
251 filter_options->sub_alloc);
253 parse_error = gently_parse_list_objects_filter(
254 &filter_options->sub[filter_options->sub_nr - 1], arg,
255 &errbuf);
257 if (parse_error)
258 die("%s", errbuf.buf);
261 int opt_parse_list_objects_filter(const struct option *opt,
262 const char *arg, int unset)
264 struct list_objects_filter_options *filter_options = opt->value;
266 if (unset || !arg)
267 list_objects_filter_set_no_filter(filter_options);
268 else
269 parse_list_objects_filter(filter_options, arg);
270 return 0;
273 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
275 if (!filter->filter_spec.nr)
276 BUG("no filter_spec available for this filter");
277 if (filter->filter_spec.nr != 1) {
278 struct strbuf concatted = STRBUF_INIT;
279 strbuf_add_separated_string_list(
280 &concatted, "", &filter->filter_spec);
281 string_list_clear(&filter->filter_spec, /*free_util=*/0);
282 string_list_append(
283 &filter->filter_spec, strbuf_detach(&concatted, NULL));
286 return filter->filter_spec.items[0].string;
289 const char *expand_list_objects_filter_spec(
290 struct list_objects_filter_options *filter)
292 if (filter->choice == LOFC_BLOB_LIMIT) {
293 struct strbuf expanded_spec = STRBUF_INIT;
294 strbuf_addf(&expanded_spec, "blob:limit=%lu",
295 filter->blob_limit_value);
296 string_list_clear(&filter->filter_spec, /*free_util=*/0);
297 string_list_append(
298 &filter->filter_spec,
299 strbuf_detach(&expanded_spec, NULL));
302 return list_objects_filter_spec(filter);
305 void list_objects_filter_release(
306 struct list_objects_filter_options *filter_options)
308 size_t sub;
310 if (!filter_options)
311 return;
312 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
313 free(filter_options->sparse_oid_name);
314 for (sub = 0; sub < filter_options->sub_nr; sub++)
315 list_objects_filter_release(&filter_options->sub[sub]);
316 free(filter_options->sub);
317 memset(filter_options, 0, sizeof(*filter_options));
320 void partial_clone_register(
321 const char *remote,
322 struct list_objects_filter_options *filter_options)
324 char *cfg_name;
325 char *filter_name;
327 /* Check if it is already registered */
328 if (!promisor_remote_find(remote)) {
329 if (upgrade_repository_format(1) < 0)
330 die(_("unable to upgrade repository format to support partial clone"));
332 /* Add promisor config for the remote */
333 cfg_name = xstrfmt("remote.%s.promisor", remote);
334 git_config_set(cfg_name, "true");
335 free(cfg_name);
339 * Record the initial filter-spec in the config as
340 * the default for subsequent fetches from this remote.
342 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
343 /* NEEDSWORK: 'expand' result leaking??? */
344 git_config_set(filter_name,
345 expand_list_objects_filter_spec(filter_options));
346 free(filter_name);
348 /* Make sure the config info are reset */
349 promisor_remote_reinit();
352 void partial_clone_get_default_filter_spec(
353 struct list_objects_filter_options *filter_options,
354 const char *remote)
356 struct promisor_remote *promisor = promisor_remote_find(remote);
357 struct strbuf errbuf = STRBUF_INIT;
360 * Parse default value, but silently ignore it if it is invalid.
362 if (!promisor)
363 return;
365 string_list_append(&filter_options->filter_spec,
366 promisor->partial_clone_filter);
367 gently_parse_list_objects_filter(filter_options,
368 promisor->partial_clone_filter,
369 &errbuf);
370 strbuf_release(&errbuf);