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