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"
13 static int parse_combine_filter(
14 struct list_objects_filter_options
*filter_options
,
16 struct strbuf
*errbuf
);
19 * Parse value of the argument to the "filter" keyword.
20 * On the command line this looks like:
22 * and in the pack protocol as:
25 * The filter keyword will be used by many commands.
26 * See Documentation/rev-list-options.txt for allowed values for <arg>.
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.
33 static int gently_parse_list_objects_filter(
34 struct list_objects_filter_options
*filter_options
,
36 struct strbuf
*errbuf
)
43 if (filter_options
->choice
)
44 BUG("filter_options already populated");
46 if (!strcmp(arg
, "blob:none")) {
47 filter_options
->choice
= LOFC_BLOB_NONE
;
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
;
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>'"));
61 filter_options
->choice
= LOFC_TREE_DEPTH
;
64 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
65 filter_options
->sparse_oid_name
= xstrdup(v0
);
66 filter_options
->choice
= LOFC_SPARSE_OID
;
69 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
73 _("sparse:path filters support has been dropped"));
77 } else if (skip_prefix(arg
, "combine:", &v0
)) {
78 return parse_combine_filter(filter_options
, v0
, errbuf
);
82 * Please update _git_fetch() in git-completion.bash when you
86 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
88 memset(filter_options
, 0, sizeof(*filter_options
));
92 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
94 static int has_reserved_character(
95 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
97 const char *c
= sub_spec
->buf
;
99 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
102 _("must escape char in sub-filter-spec: '%c'"),
112 static int parse_combine_subfilter(
113 struct list_objects_filter_options
*filter_options
,
114 struct strbuf
*subspec
,
115 struct strbuf
*errbuf
)
117 size_t new_index
= filter_options
->sub_nr
;
121 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
122 filter_options
->sub_alloc
);
124 decoded
= url_percent_decode(subspec
->buf
);
126 result
= has_reserved_character(subspec
, errbuf
) ||
127 gently_parse_list_objects_filter(
128 &filter_options
->sub
[new_index
], decoded
, errbuf
);
134 static int parse_combine_filter(
135 struct list_objects_filter_options
*filter_options
,
137 struct strbuf
*errbuf
)
139 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
144 strbuf_addstr(errbuf
, _("expected something after combine:"));
149 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
150 if (subspecs
[sub
+ 1]) {
152 * This is not the last subspec. Remove trailing "+" so
155 size_t last
= subspecs
[sub
]->len
- 1;
156 assert(subspecs
[sub
]->buf
[last
] == '+');
157 strbuf_remove(subspecs
[sub
], last
, 1);
159 result
= parse_combine_subfilter(
160 filter_options
, subspecs
[sub
], errbuf
);
163 filter_options
->choice
= LOFC_COMBINE
;
166 strbuf_list_free(subspecs
);
168 list_objects_filter_release(filter_options
);
169 memset(filter_options
, 0, sizeof(*filter_options
));
174 static int allow_unencoded(char ch
)
176 if (ch
<= ' ' || ch
== '%' || ch
== '+')
178 return !strchr(RESERVED_NON_WS
, ch
);
181 static void filter_spec_append_urlencode(
182 struct list_objects_filter_options
*filter
, const char *raw
)
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
));
191 * Changes filter_options into an equivalent LOFC_COMBINE filter options
192 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
194 static void transform_to_combine_type(
195 struct list_objects_filter_options
*filter_options
)
197 assert(filter_options
->choice
);
198 if (filter_options
->choice
== LOFC_COMBINE
)
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
;
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(
214 list_objects_filter_spec(&filter_options
->sub
[0]));
216 * We don't need the filter_spec strings for subfilter specs, only the
219 string_list_clear(&filter_options
->sub
[0].filter_spec
, /*free_util=*/0);
222 void list_objects_filter_die_if_populated(
223 struct list_objects_filter_options
*filter_options
)
225 if (filter_options
->choice
)
226 die(_("multiple filter-specs cannot be combined"));
229 void parse_list_objects_filter(
230 struct list_objects_filter_options
*filter_options
,
233 struct strbuf errbuf
= STRBUF_INIT
;
236 if (!filter_options
->choice
) {
237 string_list_append(&filter_options
->filter_spec
, xstrdup(arg
));
239 parse_error
= gently_parse_list_objects_filter(
240 filter_options
, arg
, &errbuf
);
243 * Make filter_options an LOFC_COMBINE spec so we can trivially
244 * add subspecs to it.
246 transform_to_combine_type(filter_options
);
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
);
253 parse_error
= gently_parse_list_objects_filter(
254 &filter_options
->sub
[filter_options
->sub_nr
- 1], arg
,
258 die("%s", errbuf
.buf
);
261 int opt_parse_list_objects_filter(const struct option
*opt
,
262 const char *arg
, int unset
)
264 struct list_objects_filter_options
*filter_options
= opt
->value
;
267 list_objects_filter_set_no_filter(filter_options
);
269 parse_list_objects_filter(filter_options
, arg
);
273 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
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);
283 &filter
->filter_spec
, strbuf_detach(&concatted
, NULL
));
286 return filter
->filter_spec
.items
[0].string
;
289 const char *expand_list_objects_filter_spec(
290 struct list_objects_filter_options
*filter
)
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);
298 &filter
->filter_spec
,
299 strbuf_detach(&expanded_spec
, NULL
));
302 return list_objects_filter_spec(filter
);
305 void list_objects_filter_release(
306 struct list_objects_filter_options
*filter_options
)
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
));
320 void partial_clone_register(
322 struct list_objects_filter_options
*filter_options
)
327 /* Check if it is already registered */
328 if (!promisor_remote_find(remote
)) {
329 if (upgrade_repository_format(1) < 0)
330 die(_("unable to upgrade repository format to support partial clone"));
332 /* Add promisor config for the remote */
333 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
334 git_config_set(cfg_name
, "true");
339 * Record the initial filter-spec in the config as
340 * the default for subsequent fetches from this remote.
342 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
343 /* NEEDSWORK: 'expand' result leaking??? */
344 git_config_set(filter_name
,
345 expand_list_objects_filter_spec(filter_options
));
348 /* Make sure the config info are reset */
349 promisor_remote_reinit();
352 void partial_clone_get_default_filter_spec(
353 struct list_objects_filter_options
*filter_options
,
356 struct promisor_remote
*promisor
= promisor_remote_find(remote
);
357 struct strbuf errbuf
= STRBUF_INIT
;
360 * Parse default value, but silently ignore it if it is invalid.
365 string_list_append(&filter_options
->filter_spec
,
366 promisor
->partial_clone_filter
);
367 gently_parse_list_objects_filter(filter_options
,
368 promisor
->partial_clone_filter
,
370 strbuf_release(&errbuf
);