list-objects-filter-options: allow mult. --filter
[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 "trace.h"
10 #include "url.h"
11
12 static int parse_combine_filter(
13         struct list_objects_filter_options *filter_options,
14         const char *arg,
15         struct strbuf *errbuf);
16
17 /*
18  * Parse value of the argument to the "filter" keyword.
19  * On the command line this looks like:
20  *       --filter=<arg>
21  * and in the pack protocol as:
22  *       "filter" SP <arg>
23  *
24  * The filter keyword will be used by many commands.
25  * See Documentation/rev-list-options.txt for allowed values for <arg>.
26  *
27  * Capture the given arg as the "filter_spec".  This can be forwarded to
28  * subordinate commands when necessary (although it's better to pass it through
29  * expand_list_objects_filter_spec() first).  We also "intern" the arg for the
30  * convenience of the current command.
31  */
32 static int gently_parse_list_objects_filter(
33         struct list_objects_filter_options *filter_options,
34         const char *arg,
35         struct strbuf *errbuf)
36 {
37         const char *v0;
38
39         if (filter_options->choice)
40                 BUG("filter_options already populated");
41
42         if (!strcmp(arg, "blob:none")) {
43                 filter_options->choice = LOFC_BLOB_NONE;
44                 return 0;
45
46         } else if (skip_prefix(arg, "blob:limit=", &v0)) {
47                 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
48                         filter_options->choice = LOFC_BLOB_LIMIT;
49                         return 0;
50                 }
51
52         } else if (skip_prefix(arg, "tree:", &v0)) {
53                 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
54                         strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
55                         return 1;
56                 }
57                 filter_options->choice = LOFC_TREE_DEPTH;
58                 return 0;
59
60         } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
61                 struct object_context oc;
62                 struct object_id sparse_oid;
63
64                 /*
65                  * Try to parse <oid-expression> into an OID for the current
66                  * command, but DO NOT complain if we don't have the blob or
67                  * ref locally.
68                  */
69                 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
70                                           &sparse_oid, &oc))
71                         filter_options->sparse_oid_value = oiddup(&sparse_oid);
72                 filter_options->choice = LOFC_SPARSE_OID;
73                 return 0;
74
75         } else if (skip_prefix(arg, "sparse:path=", &v0)) {
76                 if (errbuf) {
77                         strbuf_addstr(
78                                 errbuf,
79                                 _("sparse:path filters support has been dropped"));
80                 }
81                 return 1;
82
83         } else if (skip_prefix(arg, "combine:", &v0)) {
84                 return parse_combine_filter(filter_options, v0, errbuf);
85
86         }
87         /*
88          * Please update _git_fetch() in git-completion.bash when you
89          * add new filters
90          */
91
92         strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
93
94         memset(filter_options, 0, sizeof(*filter_options));
95         return 1;
96 }
97
98 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
99
100 static int has_reserved_character(
101         struct strbuf *sub_spec, struct strbuf *errbuf)
102 {
103         const char *c = sub_spec->buf;
104         while (*c) {
105                 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
106                         strbuf_addf(
107                                 errbuf,
108                                 _("must escape char in sub-filter-spec: '%c'"),
109                                 *c);
110                         return 1;
111                 }
112                 c++;
113         }
114
115         return 0;
116 }
117
118 static int parse_combine_subfilter(
119         struct list_objects_filter_options *filter_options,
120         struct strbuf *subspec,
121         struct strbuf *errbuf)
122 {
123         size_t new_index = filter_options->sub_nr++;
124         char *decoded;
125         int result;
126
127         ALLOC_GROW(filter_options->sub, filter_options->sub_nr,
128                    filter_options->sub_alloc);
129         memset(&filter_options->sub[new_index], 0,
130                sizeof(*filter_options->sub));
131
132         decoded = url_percent_decode(subspec->buf);
133
134         result = has_reserved_character(subspec, errbuf) ||
135                 gently_parse_list_objects_filter(
136                         &filter_options->sub[new_index], decoded, errbuf);
137
138         free(decoded);
139         return result;
140 }
141
142 static int parse_combine_filter(
143         struct list_objects_filter_options *filter_options,
144         const char *arg,
145         struct strbuf *errbuf)
146 {
147         struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
148         size_t sub;
149         int result = 0;
150
151         if (!subspecs[0]) {
152                 strbuf_addstr(errbuf, _("expected something after combine:"));
153                 result = 1;
154                 goto cleanup;
155         }
156
157         for (sub = 0; subspecs[sub] && !result; sub++) {
158                 if (subspecs[sub + 1]) {
159                         /*
160                          * This is not the last subspec. Remove trailing "+" so
161                          * we can parse it.
162                          */
163                         size_t last = subspecs[sub]->len - 1;
164                         assert(subspecs[sub]->buf[last] == '+');
165                         strbuf_remove(subspecs[sub], last, 1);
166                 }
167                 result = parse_combine_subfilter(
168                         filter_options, subspecs[sub], errbuf);
169         }
170
171         filter_options->choice = LOFC_COMBINE;
172
173 cleanup:
174         strbuf_list_free(subspecs);
175         if (result) {
176                 list_objects_filter_release(filter_options);
177                 memset(filter_options, 0, sizeof(*filter_options));
178         }
179         return result;
180 }
181
182 static int allow_unencoded(char ch)
183 {
184         if (ch <= ' ' || ch == '%' || ch == '+')
185                 return 0;
186         return !strchr(RESERVED_NON_WS, ch);
187 }
188
189 static void filter_spec_append_urlencode(
190         struct list_objects_filter_options *filter, const char *raw)
191 {
192         struct strbuf buf = STRBUF_INIT;
193         strbuf_addstr_urlencode(&buf, raw, allow_unencoded);
194         trace_printf("Add to combine filter-spec: %s\n", buf.buf);
195         string_list_append(&filter->filter_spec, strbuf_detach(&buf, NULL));
196 }
197
198 /*
199  * Changes filter_options into an equivalent LOFC_COMBINE filter options
200  * instance. Does not do anything if filter_options is already LOFC_COMBINE.
201  */
202 static void transform_to_combine_type(
203         struct list_objects_filter_options *filter_options)
204 {
205         assert(filter_options->choice);
206         if (filter_options->choice == LOFC_COMBINE)
207                 return;
208         {
209                 const int initial_sub_alloc = 2;
210                 struct list_objects_filter_options *sub_array =
211                         xcalloc(initial_sub_alloc, sizeof(*sub_array));
212                 sub_array[0] = *filter_options;
213                 memset(filter_options, 0, sizeof(*filter_options));
214                 filter_options->sub = sub_array;
215                 filter_options->sub_alloc = initial_sub_alloc;
216         }
217         filter_options->sub_nr = 1;
218         filter_options->choice = LOFC_COMBINE;
219         string_list_append(&filter_options->filter_spec, xstrdup("combine:"));
220         filter_spec_append_urlencode(
221                 filter_options,
222                 list_objects_filter_spec(&filter_options->sub[0]));
223         /*
224          * We don't need the filter_spec strings for subfilter specs, only the
225          * top level.
226          */
227         string_list_clear(&filter_options->sub[0].filter_spec, /*free_util=*/0);
228 }
229
230 void list_objects_filter_die_if_populated(
231         struct list_objects_filter_options *filter_options)
232 {
233         if (filter_options->choice)
234                 die(_("multiple filter-specs cannot be combined"));
235 }
236
237 int parse_list_objects_filter(
238         struct list_objects_filter_options *filter_options,
239         const char *arg)
240 {
241         struct strbuf errbuf = STRBUF_INIT;
242         int parse_error;
243
244         if (!filter_options->choice) {
245                 string_list_append(&filter_options->filter_spec, xstrdup(arg));
246
247                 parse_error = gently_parse_list_objects_filter(
248                         filter_options, arg, &errbuf);
249         } else {
250                 /*
251                  * Make filter_options an LOFC_COMBINE spec so we can trivially
252                  * add subspecs to it.
253                  */
254                 transform_to_combine_type(filter_options);
255
256                 string_list_append(&filter_options->filter_spec, xstrdup("+"));
257                 filter_spec_append_urlencode(filter_options, arg);
258                 ALLOC_GROW(filter_options->sub, filter_options->sub_nr + 1,
259                            filter_options->sub_alloc);
260                 filter_options = &filter_options->sub[filter_options->sub_nr++];
261                 memset(filter_options, 0, sizeof(*filter_options));
262
263                 parse_error = gently_parse_list_objects_filter(
264                         filter_options, arg, &errbuf);
265         }
266         if (parse_error)
267                 die("%s", errbuf.buf);
268         return 0;
269 }
270
271 int opt_parse_list_objects_filter(const struct option *opt,
272                                   const char *arg, int unset)
273 {
274         struct list_objects_filter_options *filter_options = opt->value;
275
276         if (unset || !arg) {
277                 list_objects_filter_set_no_filter(filter_options);
278                 return 0;
279         }
280
281         return parse_list_objects_filter(filter_options, arg);
282 }
283
284 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
285 {
286         if (!filter->filter_spec.nr)
287                 BUG("no filter_spec available for this filter");
288         if (filter->filter_spec.nr != 1) {
289                 struct strbuf concatted = STRBUF_INIT;
290                 strbuf_add_separated_string_list(
291                         &concatted, "", &filter->filter_spec);
292                 string_list_clear(&filter->filter_spec, /*free_util=*/0);
293                 string_list_append(
294                         &filter->filter_spec, strbuf_detach(&concatted, NULL));
295         }
296
297         return filter->filter_spec.items[0].string;
298 }
299
300 const char *expand_list_objects_filter_spec(
301         struct list_objects_filter_options *filter)
302 {
303         if (filter->choice == LOFC_BLOB_LIMIT) {
304                 struct strbuf expanded_spec = STRBUF_INIT;
305                 strbuf_addf(&expanded_spec, "blob:limit=%lu",
306                             filter->blob_limit_value);
307                 string_list_clear(&filter->filter_spec, /*free_util=*/0);
308                 string_list_append(
309                         &filter->filter_spec,
310                         strbuf_detach(&expanded_spec, NULL));
311         }
312
313         return list_objects_filter_spec(filter);
314 }
315
316 void list_objects_filter_release(
317         struct list_objects_filter_options *filter_options)
318 {
319         size_t sub;
320
321         if (!filter_options)
322                 return;
323         string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
324         free(filter_options->sparse_oid_value);
325         for (sub = 0; sub < filter_options->sub_nr; sub++)
326                 list_objects_filter_release(&filter_options->sub[sub]);
327         free(filter_options->sub);
328         memset(filter_options, 0, sizeof(*filter_options));
329 }
330
331 void partial_clone_register(
332         const char *remote,
333         struct list_objects_filter_options *filter_options)
334 {
335         /*
336          * Record the name of the partial clone remote in the
337          * config and in the global variable -- the latter is
338          * used throughout to indicate that partial clone is
339          * enabled and to expect missing objects.
340          */
341         if (repository_format_partial_clone &&
342             *repository_format_partial_clone &&
343             strcmp(remote, repository_format_partial_clone))
344                 die(_("cannot change partial clone promisor remote"));
345
346         git_config_set("core.repositoryformatversion", "1");
347         git_config_set("extensions.partialclone", remote);
348
349         repository_format_partial_clone = xstrdup(remote);
350
351         /*
352          * Record the initial filter-spec in the config as
353          * the default for subsequent fetches from this remote.
354          */
355         core_partial_clone_filter_default =
356                 xstrdup(expand_list_objects_filter_spec(filter_options));
357         git_config_set("core.partialclonefilter",
358                        core_partial_clone_filter_default);
359 }
360
361 void partial_clone_get_default_filter_spec(
362         struct list_objects_filter_options *filter_options)
363 {
364         struct strbuf errbuf = STRBUF_INIT;
365
366         /*
367          * Parse default value, but silently ignore it if it is invalid.
368          */
369         if (!core_partial_clone_filter_default)
370                 return;
371
372         string_list_append(&filter_options->filter_spec,
373                            core_partial_clone_filter_default);
374         gently_parse_list_objects_filter(filter_options,
375                                          core_partial_clone_filter_default,
376                                          &errbuf);
377         strbuf_release(&errbuf);
378 }