]> git.ipfire.org Git - thirdparty/git.git/blob - list-objects-filter-options.c
l10n: fr v2.24.0 rnd2
[thirdparty/git.git] / list-objects-filter-options.c
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"
12
13 static int parse_combine_filter(
14 struct list_objects_filter_options *filter_options,
15 const char *arg,
16 struct strbuf *errbuf);
17
18 /*
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>
24 *
25 * The filter keyword will be used by many commands.
26 * See Documentation/rev-list-options.txt for allowed values for <arg>.
27 *
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.
32 */
33 static int gently_parse_list_objects_filter(
34 struct list_objects_filter_options *filter_options,
35 const char *arg,
36 struct strbuf *errbuf)
37 {
38 const char *v0;
39
40 if (!arg)
41 return 0;
42
43 if (filter_options->choice)
44 BUG("filter_options already populated");
45
46 if (!strcmp(arg, "blob:none")) {
47 filter_options->choice = LOFC_BLOB_NONE;
48 return 0;
49
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;
54 }
55
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;
60 }
61 filter_options->choice = LOFC_TREE_DEPTH;
62 return 0;
63
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;
68
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"));
74 }
75 return 1;
76
77 } else if (skip_prefix(arg, "combine:", &v0)) {
78 return parse_combine_filter(filter_options, v0, errbuf);
79
80 }
81 /*
82 * Please update _git_fetch() in git-completion.bash when you
83 * add new filters
84 */
85
86 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
87
88 memset(filter_options, 0, sizeof(*filter_options));
89 return 1;
90 }
91
92 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
93
94 static int has_reserved_character(
95 struct strbuf *sub_spec, struct strbuf *errbuf)
96 {
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;
105 }
106 c++;
107 }
108
109 return 0;
110 }
111
112 static int parse_combine_subfilter(
113 struct list_objects_filter_options *filter_options,
114 struct strbuf *subspec,
115 struct strbuf *errbuf)
116 {
117 size_t new_index = filter_options->sub_nr;
118 char *decoded;
119 int result;
120
121 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
122 filter_options->sub_alloc);
123
124 decoded = url_percent_decode(subspec->buf);
125
126 result = has_reserved_character(subspec, errbuf) ||
127 gently_parse_list_objects_filter(
128 &filter_options->sub[new_index], decoded, errbuf);
129
130 free(decoded);
131 return result;
132 }
133
134 static int parse_combine_filter(
135 struct list_objects_filter_options *filter_options,
136 const char *arg,
137 struct strbuf *errbuf)
138 {
139 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
140 size_t sub;
141 int result = 0;
142
143 if (!subspecs[0]) {
144 strbuf_addstr(errbuf, _("expected something after combine:"));
145 result = 1;
146 goto cleanup;
147 }
148
149 for (sub = 0; subspecs[sub] && !result; sub++) {
150 if (subspecs[sub + 1]) {
151 /*
152 * This is not the last subspec. Remove trailing "+" so
153 * we can parse it.
154 */
155 size_t last = subspecs[sub]->len - 1;
156 assert(subspecs[sub]->buf[last] == '+');
157 strbuf_remove(subspecs[sub], last, 1);
158 }
159 result = parse_combine_subfilter(
160 filter_options, subspecs[sub], errbuf);
161 }
162
163 filter_options->choice = LOFC_COMBINE;
164
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));
170 }
171 return result;
172 }
173
174 static int allow_unencoded(char ch)
175 {
176 if (ch <= ' ' || ch == '%' || ch == '+')
177 return 0;
178 return !strchr(RESERVED_NON_WS, ch);
179 }
180
181 static void filter_spec_append_urlencode(
182 struct list_objects_filter_options *filter, const char *raw)
183 {
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));
188 }
189
190 /*
191 * Changes filter_options into an equivalent LOFC_COMBINE filter options
192 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
193 */
194 static void transform_to_combine_type(
195 struct list_objects_filter_options *filter_options)
196 {
197 assert(filter_options->choice);
198 if (filter_options->choice == LOFC_COMBINE)
199 return;
200 {
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;
208 }
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]));
215 /*
216 * We don't need the filter_spec strings for subfilter specs, only the
217 * top level.
218 */
219 string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
220 }
221
222 void list_objects_filter_die_if_populated(
223 struct list_objects_filter_options *filter_options)
224 {
225 if (filter_options->choice)
226 die(_("multiple filter-specs cannot be combined"));
227 }
228
229 void parse_list_objects_filter(
230 struct list_objects_filter_options *filter_options,
231 const char *arg)
232 {
233 struct strbuf errbuf = STRBUF_INIT;
234 int parse_error;
235
236 if (!filter_options->choice) {
237 string_list_append(&filter_options->filter_spec, xstrdup(arg));
238
239 parse_error = gently_parse_list_objects_filter(
240 filter_options, arg, &errbuf);
241 } else {
242 /*
243 * Make filter_options an LOFC_COMBINE spec so we can trivially
244 * add subspecs to it.
245 */
246 transform_to_combine_type(filter_options);
247
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);
252
253 parse_error = gently_parse_list_objects_filter(
254 &filter_options->sub[filter_options->sub_nr - 1], arg,
255 &errbuf);
256 }
257 if (parse_error)
258 die("%s", errbuf.buf);
259 }
260
261 int opt_parse_list_objects_filter(const struct option *opt,
262 const char *arg, int unset)
263 {
264 struct list_objects_filter_options *filter_options = opt->value;
265
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;
271 }
272
273 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
274 {
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));
284 }
285
286 return filter->filter_spec.items[0].string;
287 }
288
289 const char *expand_list_objects_filter_spec(
290 struct list_objects_filter_options *filter)
291 {
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));
300 }
301
302 return list_objects_filter_spec(filter);
303 }
304
305 void list_objects_filter_release(
306 struct list_objects_filter_options *filter_options)
307 {
308 size_t sub;
309
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));
318 }
319
320 void partial_clone_register(
321 const char *remote,
322 struct list_objects_filter_options *filter_options)
323 {
324 char *cfg_name;
325 char *filter_name;
326
327 /* Check if it is already registered */
328 if (!promisor_remote_find(remote)) {
329 git_config_set("core.repositoryformatversion", "1");
330
331 /* Add promisor config for the remote */
332 cfg_name = xstrfmt("remote.%s.promisor", remote);
333 git_config_set(cfg_name, "true");
334 free(cfg_name);
335 }
336
337 /*
338 * Record the initial filter-spec in the config as
339 * the default for subsequent fetches from this remote.
340 */
341 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
342 /* NEEDSWORK: 'expand' result leaking??? */
343 git_config_set(filter_name,
344 expand_list_objects_filter_spec(filter_options));
345 free(filter_name);
346
347 /* Make sure the config info are reset */
348 promisor_remote_reinit();
349 }
350
351 void partial_clone_get_default_filter_spec(
352 struct list_objects_filter_options *filter_options,
353 const char *remote)
354 {
355 struct promisor_remote *promisor = promisor_remote_find(remote);
356 struct strbuf errbuf = STRBUF_INIT;
357
358 /*
359 * Parse default value, but silently ignore it if it is invalid.
360 */
361 if (!promisor)
362 return;
363
364 string_list_append(&filter_options->filter_spec,
365 promisor->partial_clone_filter);
366 gently_parse_list_objects_filter(filter_options,
367 promisor->partial_clone_filter,
368 &errbuf);
369 strbuf_release(&errbuf);
370 }