]> git.ipfire.org Git - thirdparty/git.git/blame - list-objects-filter-options.c
Merge branch 'jt/fetch-pack-record-refs-in-the-dot-promisor'
[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"
5#include "argv-array.h"
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
JH
17
18/*
1dde5fa2 19 * Parse value of the argument to the "filter" keyword.
25ec7bca
JH
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
87c2d9d3
JS
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.
25ec7bca 32 */
1e1e39b3
JH
33static int gently_parse_list_objects_filter(
34 struct list_objects_filter_options *filter_options,
35 const char *arg,
36 struct strbuf *errbuf)
25ec7bca
JH
37{
38 const char *v0;
39
fa3d1b63
CC
40 if (!arg)
41 return 0;
42
f56f7642
MD
43 if (filter_options->choice)
44 BUG("filter_options already populated");
25ec7bca
JH
45
46 if (!strcmp(arg, "blob:none")) {
47 filter_options->choice = LOFC_BLOB_NONE;
48 return 0;
25ec7bca 49
1e1e39b3
JH
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 }
25ec7bca 55
bc5975d2 56 } else if (skip_prefix(arg, "tree:", &v0)) {
c813a7c3 57 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
842b0051 58 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
bc5975d2
MD
59 return 1;
60 }
c813a7c3 61 filter_options->choice = LOFC_TREE_DEPTH;
bc5975d2
MD
62 return 0;
63
1e1e39b3 64 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
4c96a775 65 filter_options->sparse_oid_name = xstrdup(v0);
25ec7bca
JH
66 filter_options->choice = LOFC_SPARSE_OID;
67 return 0;
25ec7bca 68
1e1e39b3 69 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
e693237e
CC
70 if (errbuf) {
71 strbuf_addstr(
72 errbuf,
73 _("sparse:path filters support has been dropped"));
74 }
75 return 1;
e987df5f
MD
76
77 } else if (skip_prefix(arg, "combine:", &v0)) {
78 return parse_combine_filter(filter_options, v0, errbuf);
79
25ec7bca 80 }
5a59a230
NTND
81 /*
82 * Please update _git_fetch() in git-completion.bash when you
83 * add new filters
84 */
25ec7bca 85
842b0051 86 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
cc0b05a4 87
1e1e39b3
JH
88 memset(filter_options, 0, sizeof(*filter_options));
89 return 1;
90}
91
e987df5f
MD
92static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
93
94static int has_reserved_character(
95 struct strbuf *sub_spec, struct strbuf *errbuf)
1e1e39b3 96{
e987df5f
MD
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
25ec7bca
JH
109 return 0;
110}
111
e987df5f
MD
112static int parse_combine_subfilter(
113 struct list_objects_filter_options *filter_options,
114 struct strbuf *subspec,
115 struct strbuf *errbuf)
116{
5a133e8a 117 size_t new_index = filter_options->sub_nr;
e987df5f
MD
118 char *decoded;
119 int result;
120
5a133e8a
MD
121 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
122 filter_options->sub_alloc);
e987df5f
MD
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
134static 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
165cleanup:
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
489fc9ee
MD
174static int allow_unencoded(char ch)
175{
176 if (ch <= ' ' || ch == '%' || ch == '+')
177 return 0;
178 return !strchr(RESERVED_NON_WS, ch);
179}
180
181static void filter_spec_append_urlencode(
182 struct list_objects_filter_options *filter, const char *raw)
1e1e39b3
JH
183{
184 struct strbuf buf = STRBUF_INIT;
489fc9ee
MD
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 */
194static 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
222void list_objects_filter_die_if_populated(
223 struct list_objects_filter_options *filter_options)
224{
f56f7642
MD
225 if (filter_options->choice)
226 die(_("multiple filter-specs cannot be combined"));
489fc9ee
MD
227}
228
90d21f9e 229void parse_list_objects_filter(
489fc9ee
MD
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);
5a133e8a
MD
250 ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
251 filter_options->sub_alloc);
489fc9ee
MD
252
253 parse_error = gently_parse_list_objects_filter(
5a133e8a
MD
254 &filter_options->sub[filter_options->sub_nr - 1], arg,
255 &errbuf);
489fc9ee
MD
256 }
257 if (parse_error)
258 die("%s", errbuf.buf);
25ec7bca
JH
259}
260
261int 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
90d21f9e 266 if (unset || !arg)
aa57b871 267 list_objects_filter_set_no_filter(filter_options);
90d21f9e
MD
268 else
269 parse_list_objects_filter(filter_options, arg);
270 return 0;
25ec7bca 271}
4875c979 272
cf9ceb5a 273const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
87c2d9d3 274{
cf9ceb5a
MD
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));
4875c979 284 }
25ec7bca 285
cf9ceb5a 286 return filter->filter_spec.items[0].string;
25ec7bca 287}
4875c979 288
cf9ceb5a
MD
289const char *expand_list_objects_filter_spec(
290 struct list_objects_filter_options *filter)
87c2d9d3 291{
cf9ceb5a
MD
292 if (filter->choice == LOFC_BLOB_LIMIT) {
293 struct strbuf expanded_spec = STRBUF_INIT;
294 strbuf_addf(&expanded_spec, "blob:limit=%lu",
87c2d9d3 295 filter->blob_limit_value);
cf9ceb5a
MD
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);
87c2d9d3
JS
303}
304
4875c979
JH
305void list_objects_filter_release(
306 struct list_objects_filter_options *filter_options)
307{
e987df5f
MD
308 size_t sub;
309
310 if (!filter_options)
311 return;
cf9ceb5a 312 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
4c96a775 313 free(filter_options->sparse_oid_name);
e987df5f
MD
314 for (sub = 0; sub < filter_options->sub_nr; sub++)
315 list_objects_filter_release(&filter_options->sub[sub]);
316 free(filter_options->sub);
4875c979
JH
317 memset(filter_options, 0, sizeof(*filter_options));
318}
1e1e39b3
JH
319
320void partial_clone_register(
321 const char *remote,
cf9ceb5a 322 struct list_objects_filter_options *filter_options)
1e1e39b3 323{
b14ed5ad 324 char *cfg_name;
fa3d1b63 325 char *filter_name;
1e1e39b3 326
b14ed5ad
CC
327 /* Check if it is already registered */
328 if (!promisor_remote_find(remote)) {
329 git_config_set("core.repositoryformatversion", "1");
1e1e39b3 330
b14ed5ad
CC
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 }
1e1e39b3
JH
336
337 /*
338 * Record the initial filter-spec in the config as
339 * the default for subsequent fetches from this remote.
340 */
fa3d1b63 341 filter_name = xstrfmt("remote.%s.partialclonefilter", remote);
627b8268
JH
342 /* NEEDSWORK: 'expand' result leaking??? */
343 git_config_set(filter_name,
344 expand_list_objects_filter_spec(filter_options));
fa3d1b63 345 free(filter_name);
b14ed5ad
CC
346
347 /* Make sure the config info are reset */
348 promisor_remote_reinit();
1e1e39b3
JH
349}
350
351void partial_clone_get_default_filter_spec(
fa3d1b63
CC
352 struct list_objects_filter_options *filter_options,
353 const char *remote)
1e1e39b3 354{
fa3d1b63 355 struct promisor_remote *promisor = promisor_remote_find(remote);
842b0051 356 struct strbuf errbuf = STRBUF_INIT;
fa3d1b63 357
1e1e39b3
JH
358 /*
359 * Parse default value, but silently ignore it if it is invalid.
360 */
627b8268 361 if (!promisor)
cac1137d 362 return;
e987df5f 363
cf9ceb5a 364 string_list_append(&filter_options->filter_spec,
627b8268 365 promisor->partial_clone_filter);
1e1e39b3 366 gently_parse_list_objects_filter(filter_options,
627b8268 367 promisor->partial_clone_filter,
842b0051
MD
368 &errbuf);
369 strbuf_release(&errbuf);
1e1e39b3 370}