5 #include "argv-array.h"
6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
11 static int parse_combine_filter(
12 struct list_objects_filter_options *filter_options,
14 struct strbuf *errbuf);
17 * Parse value of the argument to the "filter" keyword.
18 * On the command line this looks like:
20 * and in the pack protocol as:
23 * The filter keyword will be used by many commands.
24 * See Documentation/rev-list-options.txt for allowed values for <arg>.
26 * Capture the given arg as the "filter_spec". This can be forwarded to
27 * subordinate commands when necessary (although it's better to pass it through
28 * expand_list_objects_filter_spec() first). We also "intern" the arg for the
29 * convenience of the current command.
31 static int gently_parse_list_objects_filter(
32 struct list_objects_filter_options *filter_options,
34 struct strbuf *errbuf)
38 if (filter_options->choice)
39 BUG("filter_options already populated");
41 if (!strcmp(arg, "blob:none")) {
42 filter_options->choice = LOFC_BLOB_NONE;
45 } else if (skip_prefix(arg, "blob:limit=", &v0)) {
46 if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {
47 filter_options->choice = LOFC_BLOB_LIMIT;
51 } else if (skip_prefix(arg, "tree:", &v0)) {
52 if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {
53 strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));
56 filter_options->choice = LOFC_TREE_DEPTH;
59 } else if (skip_prefix(arg, "sparse:oid=", &v0)) {
60 struct object_context oc;
61 struct object_id sparse_oid;
64 * Try to parse <oid-expression> into an OID for the current
65 * command, but DO NOT complain if we don't have the blob or
68 if (!get_oid_with_context(the_repository, v0, GET_OID_BLOB,
70 filter_options->sparse_oid_value = oiddup(&sparse_oid);
71 filter_options->choice = LOFC_SPARSE_OID;
74 } else if (skip_prefix(arg, "sparse:path=", &v0)) {
78 _("sparse:path filters support has been dropped"));
82 } else if (skip_prefix(arg, "combine:", &v0)) {
83 return parse_combine_filter(filter_options, v0, errbuf);
87 * Please update _git_fetch() in git-completion.bash when you
91 strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);
93 memset(filter_options, 0, sizeof(*filter_options));
97 static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";
99 static int has_reserved_character(
100 struct strbuf *sub_spec, struct strbuf *errbuf)
102 const char *c = sub_spec->buf;
104 if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {
107 _("must escape char in sub-filter-spec: '%c'"),
117 static int parse_combine_subfilter(
118 struct list_objects_filter_options *filter_options,
119 struct strbuf *subspec,
120 struct strbuf *errbuf)
122 size_t new_index = filter_options->sub_nr++;
126 ALLOC_GROW(filter_options->sub, filter_options->sub_nr,
127 filter_options->sub_alloc);
128 memset(&filter_options->sub[new_index], 0,
129 sizeof(*filter_options->sub));
131 decoded = url_percent_decode(subspec->buf);
133 result = has_reserved_character(subspec, errbuf) ||
134 gently_parse_list_objects_filter(
135 &filter_options->sub[new_index], decoded, errbuf);
141 static int parse_combine_filter(
142 struct list_objects_filter_options *filter_options,
144 struct strbuf *errbuf)
146 struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);
151 strbuf_addstr(errbuf, _("expected something after combine:"));
156 for (sub = 0; subspecs[sub] && !result; sub++) {
157 if (subspecs[sub + 1]) {
159 * This is not the last subspec. Remove trailing "+" so
162 size_t last = subspecs[sub]->len - 1;
163 assert(subspecs[sub]->buf[last] == '+');
164 strbuf_remove(subspecs[sub], last, 1);
166 result = parse_combine_subfilter(
167 filter_options, subspecs[sub], errbuf);
170 filter_options->choice = LOFC_COMBINE;
173 strbuf_list_free(subspecs);
175 list_objects_filter_release(filter_options);
176 memset(filter_options, 0, sizeof(*filter_options));
181 int parse_list_objects_filter(struct list_objects_filter_options *filter_options,
184 struct strbuf buf = STRBUF_INIT;
185 if (filter_options->choice)
186 die(_("multiple filter-specs cannot be combined"));
187 string_list_append(&filter_options->filter_spec, xstrdup(arg));
188 if (gently_parse_list_objects_filter(filter_options, arg, &buf))
193 int opt_parse_list_objects_filter(const struct option *opt,
194 const char *arg, int unset)
196 struct list_objects_filter_options *filter_options = opt->value;
199 list_objects_filter_set_no_filter(filter_options);
203 return parse_list_objects_filter(filter_options, arg);
206 const char *list_objects_filter_spec(struct list_objects_filter_options *filter)
208 if (!filter->filter_spec.nr)
209 BUG("no filter_spec available for this filter");
210 if (filter->filter_spec.nr != 1) {
211 struct strbuf concatted = STRBUF_INIT;
212 strbuf_add_separated_string_list(
213 &concatted, "", &filter->filter_spec);
214 string_list_clear(&filter->filter_spec, /*free_util=*/0);
216 &filter->filter_spec, strbuf_detach(&concatted, NULL));
219 return filter->filter_spec.items[0].string;
222 const char *expand_list_objects_filter_spec(
223 struct list_objects_filter_options *filter)
225 if (filter->choice == LOFC_BLOB_LIMIT) {
226 struct strbuf expanded_spec = STRBUF_INIT;
227 strbuf_addf(&expanded_spec, "blob:limit=%lu",
228 filter->blob_limit_value);
229 string_list_clear(&filter->filter_spec, /*free_util=*/0);
231 &filter->filter_spec,
232 strbuf_detach(&expanded_spec, NULL));
235 return list_objects_filter_spec(filter);
238 void list_objects_filter_release(
239 struct list_objects_filter_options *filter_options)
245 string_list_clear(&filter_options->filter_spec, /*free_util=*/0);
246 free(filter_options->sparse_oid_value);
247 for (sub = 0; sub < filter_options->sub_nr; sub++)
248 list_objects_filter_release(&filter_options->sub[sub]);
249 free(filter_options->sub);
250 memset(filter_options, 0, sizeof(*filter_options));
253 void partial_clone_register(
255 struct list_objects_filter_options *filter_options)
258 * Record the name of the partial clone remote in the
259 * config and in the global variable -- the latter is
260 * used throughout to indicate that partial clone is
261 * enabled and to expect missing objects.
263 if (repository_format_partial_clone &&
264 *repository_format_partial_clone &&
265 strcmp(remote, repository_format_partial_clone))
266 die(_("cannot change partial clone promisor remote"));
268 git_config_set("core.repositoryformatversion", "1");
269 git_config_set("extensions.partialclone", remote);
271 repository_format_partial_clone = xstrdup(remote);
274 * Record the initial filter-spec in the config as
275 * the default for subsequent fetches from this remote.
277 core_partial_clone_filter_default =
278 xstrdup(expand_list_objects_filter_spec(filter_options));
279 git_config_set("core.partialclonefilter",
280 core_partial_clone_filter_default);
283 void partial_clone_get_default_filter_spec(
284 struct list_objects_filter_options *filter_options)
286 struct strbuf errbuf = STRBUF_INIT;
289 * Parse default value, but silently ignore it if it is invalid.
291 if (!core_partial_clone_filter_default)
294 string_list_append(&filter_options->filter_spec,
295 core_partial_clone_filter_default);
296 gently_parse_list_objects_filter(filter_options,
297 core_partial_clone_filter_default,
299 strbuf_release(&errbuf);