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 filter_options->filter_spec = strdup(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 void expand_list_objects_filter_spec(
207 const struct list_objects_filter_options *filter,
208 struct strbuf *expanded_spec)
210 strbuf_init(expanded_spec, strlen(filter->filter_spec));
211 if (filter->choice == LOFC_BLOB_LIMIT)
212 strbuf_addf(expanded_spec, "blob:limit=%lu",
213 filter->blob_limit_value);
214 else if (filter->choice == LOFC_TREE_DEPTH)
215 strbuf_addf(expanded_spec, "tree:%lu",
216 filter->tree_exclude_depth);
218 strbuf_addstr(expanded_spec, filter->filter_spec);
221 void list_objects_filter_release(
222 struct list_objects_filter_options *filter_options)
228 free(filter_options->filter_spec);
229 free(filter_options->sparse_oid_value);
230 for (sub = 0; sub < filter_options->sub_nr; sub++)
231 list_objects_filter_release(&filter_options->sub[sub]);
232 free(filter_options->sub);
233 memset(filter_options, 0, sizeof(*filter_options));
236 void partial_clone_register(
238 const struct list_objects_filter_options *filter_options)
241 * Record the name of the partial clone remote in the
242 * config and in the global variable -- the latter is
243 * used throughout to indicate that partial clone is
244 * enabled and to expect missing objects.
246 if (repository_format_partial_clone &&
247 *repository_format_partial_clone &&
248 strcmp(remote, repository_format_partial_clone))
249 die(_("cannot change partial clone promisor remote"));
251 git_config_set("core.repositoryformatversion", "1");
252 git_config_set("extensions.partialclone", remote);
254 repository_format_partial_clone = xstrdup(remote);
257 * Record the initial filter-spec in the config as
258 * the default for subsequent fetches from this remote.
260 core_partial_clone_filter_default =
261 xstrdup(filter_options->filter_spec);
262 git_config_set("core.partialclonefilter",
263 core_partial_clone_filter_default);
266 void partial_clone_get_default_filter_spec(
267 struct list_objects_filter_options *filter_options)
269 struct strbuf errbuf = STRBUF_INIT;
272 * Parse default value, but silently ignore it if it is invalid.
274 if (!core_partial_clone_filter_default)
277 filter_options->filter_spec = strdup(core_partial_clone_filter_default);
278 gently_parse_list_objects_filter(filter_options,
279 core_partial_clone_filter_default,
281 strbuf_release(&errbuf);