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
);
18 const char *list_object_filter_config_name(enum list_objects_filter_choice c
)
22 /* we have no name for "no filter at all" */
32 case LOFC_OBJECT_TYPE
:
37 /* not a real filter type; just the count of all filters */
40 BUG("list_object_filter_config_name: invalid argument '%d'", c
);
44 * Parse value of the argument to the "filter" keyword.
45 * On the command line this looks like:
47 * and in the pack protocol as:
50 * The filter keyword will be used by many commands.
51 * See Documentation/rev-list-options.txt for allowed values for <arg>.
53 * Capture the given arg as the "filter_spec". This can be forwarded to
54 * subordinate commands when necessary (although it's better to pass it through
55 * expand_list_objects_filter_spec() first). We also "intern" the arg for the
56 * convenience of the current command.
58 static int gently_parse_list_objects_filter(
59 struct list_objects_filter_options
*filter_options
,
61 struct strbuf
*errbuf
)
68 if (filter_options
->choice
)
69 BUG("filter_options already populated");
71 if (!strcmp(arg
, "blob:none")) {
72 filter_options
->choice
= LOFC_BLOB_NONE
;
75 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
76 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
77 filter_options
->choice
= LOFC_BLOB_LIMIT
;
81 } else if (skip_prefix(arg
, "tree:", &v0
)) {
82 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
83 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
86 filter_options
->choice
= LOFC_TREE_DEPTH
;
89 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
90 filter_options
->sparse_oid_name
= xstrdup(v0
);
91 filter_options
->choice
= LOFC_SPARSE_OID
;
94 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
98 _("sparse:path filters support has been dropped"));
102 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
103 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
105 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
106 "not a valid object type"), v0
);
110 filter_options
->object_type
= type
;
111 filter_options
->choice
= LOFC_OBJECT_TYPE
;
115 } else if (skip_prefix(arg
, "combine:", &v0
)) {
116 return parse_combine_filter(filter_options
, v0
, errbuf
);
120 * Please update _git_fetch() in git-completion.bash when you
124 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
126 memset(filter_options
, 0, sizeof(*filter_options
));
130 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
132 static int has_reserved_character(
133 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
135 const char *c
= sub_spec
->buf
;
137 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
140 _("must escape char in sub-filter-spec: '%c'"),
150 static int parse_combine_subfilter(
151 struct list_objects_filter_options
*filter_options
,
152 struct strbuf
*subspec
,
153 struct strbuf
*errbuf
)
155 size_t new_index
= filter_options
->sub_nr
;
159 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
160 filter_options
->sub_alloc
);
162 decoded
= url_percent_decode(subspec
->buf
);
164 result
= has_reserved_character(subspec
, errbuf
) ||
165 gently_parse_list_objects_filter(
166 &filter_options
->sub
[new_index
], decoded
, errbuf
);
172 static int parse_combine_filter(
173 struct list_objects_filter_options
*filter_options
,
175 struct strbuf
*errbuf
)
177 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
182 strbuf_addstr(errbuf
, _("expected something after combine:"));
187 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
188 if (subspecs
[sub
+ 1]) {
190 * This is not the last subspec. Remove trailing "+" so
193 size_t last
= subspecs
[sub
]->len
- 1;
194 assert(subspecs
[sub
]->buf
[last
] == '+');
195 strbuf_remove(subspecs
[sub
], last
, 1);
197 result
= parse_combine_subfilter(
198 filter_options
, subspecs
[sub
], errbuf
);
201 filter_options
->choice
= LOFC_COMBINE
;
204 strbuf_list_free(subspecs
);
206 list_objects_filter_release(filter_options
);
207 memset(filter_options
, 0, sizeof(*filter_options
));
212 static int allow_unencoded(char ch
)
214 if (ch
<= ' ' || ch
== '%' || ch
== '+')
216 return !strchr(RESERVED_NON_WS
, ch
);
219 static void filter_spec_append_urlencode(
220 struct list_objects_filter_options
*filter
, const char *raw
)
222 struct strbuf buf
= STRBUF_INIT
;
223 strbuf_addstr_urlencode(&buf
, raw
, allow_unencoded
);
224 trace_printf("Add to combine filter-spec: %s\n", buf
.buf
);
225 string_list_append(&filter
->filter_spec
, strbuf_detach(&buf
, NULL
));
229 * Changes filter_options into an equivalent LOFC_COMBINE filter options
230 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
232 static void transform_to_combine_type(
233 struct list_objects_filter_options
*filter_options
)
235 assert(filter_options
->choice
);
236 if (filter_options
->choice
== LOFC_COMBINE
)
239 const int initial_sub_alloc
= 2;
240 struct list_objects_filter_options
*sub_array
=
241 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
242 sub_array
[0] = *filter_options
;
243 memset(filter_options
, 0, sizeof(*filter_options
));
244 filter_options
->sub
= sub_array
;
245 filter_options
->sub_alloc
= initial_sub_alloc
;
247 filter_options
->sub_nr
= 1;
248 filter_options
->choice
= LOFC_COMBINE
;
249 string_list_append(&filter_options
->filter_spec
, xstrdup("combine:"));
250 filter_spec_append_urlencode(
252 list_objects_filter_spec(&filter_options
->sub
[0]));
254 * We don't need the filter_spec strings for subfilter specs, only the
257 string_list_clear(&filter_options
->sub
[0].filter_spec
, /*free_util=*/0);
260 void list_objects_filter_die_if_populated(
261 struct list_objects_filter_options
*filter_options
)
263 if (filter_options
->choice
)
264 die(_("multiple filter-specs cannot be combined"));
267 void parse_list_objects_filter(
268 struct list_objects_filter_options
*filter_options
,
271 struct strbuf errbuf
= STRBUF_INIT
;
274 if (!filter_options
->choice
) {
275 string_list_append(&filter_options
->filter_spec
, xstrdup(arg
));
277 parse_error
= gently_parse_list_objects_filter(
278 filter_options
, arg
, &errbuf
);
281 * Make filter_options an LOFC_COMBINE spec so we can trivially
282 * add subspecs to it.
284 transform_to_combine_type(filter_options
);
286 string_list_append(&filter_options
->filter_spec
, xstrdup("+"));
287 filter_spec_append_urlencode(filter_options
, arg
);
288 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
289 filter_options
->sub_alloc
);
291 parse_error
= gently_parse_list_objects_filter(
292 &filter_options
->sub
[filter_options
->sub_nr
- 1], arg
,
296 die("%s", errbuf
.buf
);
299 int opt_parse_list_objects_filter(const struct option
*opt
,
300 const char *arg
, int unset
)
302 struct list_objects_filter_options
*filter_options
= opt
->value
;
305 list_objects_filter_set_no_filter(filter_options
);
307 parse_list_objects_filter(filter_options
, arg
);
311 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
313 if (!filter
->filter_spec
.nr
)
314 BUG("no filter_spec available for this filter");
315 if (filter
->filter_spec
.nr
!= 1) {
316 struct strbuf concatted
= STRBUF_INIT
;
317 strbuf_add_separated_string_list(
318 &concatted
, "", &filter
->filter_spec
);
319 string_list_clear(&filter
->filter_spec
, /*free_util=*/0);
321 &filter
->filter_spec
, strbuf_detach(&concatted
, NULL
));
324 return filter
->filter_spec
.items
[0].string
;
327 const char *expand_list_objects_filter_spec(
328 struct list_objects_filter_options
*filter
)
330 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
331 struct strbuf expanded_spec
= STRBUF_INIT
;
332 strbuf_addf(&expanded_spec
, "blob:limit=%lu",
333 filter
->blob_limit_value
);
334 string_list_clear(&filter
->filter_spec
, /*free_util=*/0);
336 &filter
->filter_spec
,
337 strbuf_detach(&expanded_spec
, NULL
));
340 return list_objects_filter_spec(filter
);
343 void list_objects_filter_release(
344 struct list_objects_filter_options
*filter_options
)
350 string_list_clear(&filter_options
->filter_spec
, /*free_util=*/0);
351 free(filter_options
->sparse_oid_name
);
352 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
353 list_objects_filter_release(&filter_options
->sub
[sub
]);
354 free(filter_options
->sub
);
355 memset(filter_options
, 0, sizeof(*filter_options
));
358 void partial_clone_register(
360 struct list_objects_filter_options
*filter_options
)
362 struct promisor_remote
*promisor_remote
;
366 /* Check if it is already registered */
367 if ((promisor_remote
= promisor_remote_find(remote
))) {
368 if (promisor_remote
->partial_clone_filter
)
370 * Remote is already registered and a filter is already
371 * set, so we don't need to do anything here.
375 if (upgrade_repository_format(1) < 0)
376 die(_("unable to upgrade repository format to support partial clone"));
378 /* Add promisor config for the remote */
379 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
380 git_config_set(cfg_name
, "true");
385 * Record the initial filter-spec in the config as
386 * the default for subsequent fetches from this remote.
388 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
389 /* NEEDSWORK: 'expand' result leaking??? */
390 git_config_set(filter_name
,
391 expand_list_objects_filter_spec(filter_options
));
394 /* Make sure the config info are reset */
395 promisor_remote_reinit();
398 void partial_clone_get_default_filter_spec(
399 struct list_objects_filter_options
*filter_options
,
402 struct promisor_remote
*promisor
= promisor_remote_find(remote
);
403 struct strbuf errbuf
= STRBUF_INIT
;
406 * Parse default value, but silently ignore it if it is invalid.
411 string_list_append(&filter_options
->filter_spec
,
412 promisor
->partial_clone_filter
);
413 gently_parse_list_objects_filter(filter_options
,
414 promisor
->partial_clone_filter
,
416 strbuf_release(&errbuf
);