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