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