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
);
43 int gently_parse_list_objects_filter(
44 struct list_objects_filter_options
*filter_options
,
46 struct strbuf
*errbuf
)
53 if (filter_options
->choice
)
54 BUG("filter_options already populated");
56 if (!strcmp(arg
, "blob:none")) {
57 filter_options
->choice
= LOFC_BLOB_NONE
;
60 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
61 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
62 filter_options
->choice
= LOFC_BLOB_LIMIT
;
66 } else if (skip_prefix(arg
, "tree:", &v0
)) {
67 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
68 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
71 filter_options
->choice
= LOFC_TREE_DEPTH
;
74 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
75 filter_options
->sparse_oid_name
= xstrdup(v0
);
76 filter_options
->choice
= LOFC_SPARSE_OID
;
79 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
83 _("sparse:path filters support has been dropped"));
87 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
88 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
90 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
91 "not a valid object type"), v0
);
95 filter_options
->object_type
= type
;
96 filter_options
->choice
= LOFC_OBJECT_TYPE
;
100 } else if (skip_prefix(arg
, "combine:", &v0
)) {
101 return parse_combine_filter(filter_options
, v0
, errbuf
);
105 * Please update _git_fetch() in git-completion.bash when you
109 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
111 memset(filter_options
, 0, sizeof(*filter_options
));
115 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
117 static int has_reserved_character(
118 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
120 const char *c
= sub_spec
->buf
;
122 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
125 _("must escape char in sub-filter-spec: '%c'"),
135 static int parse_combine_subfilter(
136 struct list_objects_filter_options
*filter_options
,
137 struct strbuf
*subspec
,
138 struct strbuf
*errbuf
)
140 size_t new_index
= filter_options
->sub_nr
;
144 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
145 filter_options
->sub_alloc
);
147 decoded
= url_percent_decode(subspec
->buf
);
149 result
= has_reserved_character(subspec
, errbuf
) ||
150 gently_parse_list_objects_filter(
151 &filter_options
->sub
[new_index
], decoded
, errbuf
);
157 static int parse_combine_filter(
158 struct list_objects_filter_options
*filter_options
,
160 struct strbuf
*errbuf
)
162 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
167 strbuf_addstr(errbuf
, _("expected something after combine:"));
172 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
173 if (subspecs
[sub
+ 1]) {
175 * This is not the last subspec. Remove trailing "+" so
178 size_t last
= subspecs
[sub
]->len
- 1;
179 assert(subspecs
[sub
]->buf
[last
] == '+');
180 strbuf_remove(subspecs
[sub
], last
, 1);
182 result
= parse_combine_subfilter(
183 filter_options
, subspecs
[sub
], errbuf
);
186 filter_options
->choice
= LOFC_COMBINE
;
189 strbuf_list_free(subspecs
);
191 list_objects_filter_release(filter_options
);
192 memset(filter_options
, 0, sizeof(*filter_options
));
197 static int allow_unencoded(char ch
)
199 if (ch
<= ' ' || ch
== '%' || ch
== '+')
201 return !strchr(RESERVED_NON_WS
, ch
);
204 static void filter_spec_append_urlencode(
205 struct list_objects_filter_options
*filter
, const char *raw
)
207 struct strbuf buf
= STRBUF_INIT
;
208 strbuf_addstr_urlencode(&buf
, raw
, allow_unencoded
);
209 trace_printf("Add to combine filter-spec: %s\n", buf
.buf
);
210 string_list_append(&filter
->filter_spec
, strbuf_detach(&buf
, NULL
));
214 * Changes filter_options into an equivalent LOFC_COMBINE filter options
215 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
217 static void transform_to_combine_type(
218 struct list_objects_filter_options
*filter_options
)
220 assert(filter_options
->choice
);
221 if (filter_options
->choice
== LOFC_COMBINE
)
224 const int initial_sub_alloc
= 2;
225 struct list_objects_filter_options
*sub_array
=
226 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
227 sub_array
[0] = *filter_options
;
228 memset(filter_options
, 0, sizeof(*filter_options
));
229 filter_options
->sub
= sub_array
;
230 filter_options
->sub_alloc
= initial_sub_alloc
;
232 filter_options
->sub_nr
= 1;
233 filter_options
->choice
= LOFC_COMBINE
;
234 string_list_append(&filter_options
->filter_spec
, xstrdup("combine:"));
235 filter_spec_append_urlencode(
237 list_objects_filter_spec(&filter_options
->sub
[0]));
239 * We don't need the filter_spec strings for subfilter specs, only the
242 string_list_clear(&filter_options
->sub
[0].filter_spec
, /*free_util=*/0);
245 void list_objects_filter_die_if_populated(
246 struct list_objects_filter_options
*filter_options
)
248 if (filter_options
->choice
)
249 die(_("multiple filter-specs cannot be combined"));
252 void parse_list_objects_filter(
253 struct list_objects_filter_options
*filter_options
,
256 struct strbuf errbuf
= STRBUF_INIT
;
259 if (!filter_options
->choice
) {
260 string_list_append(&filter_options
->filter_spec
, xstrdup(arg
));
262 parse_error
= gently_parse_list_objects_filter(
263 filter_options
, arg
, &errbuf
);
266 * Make filter_options an LOFC_COMBINE spec so we can trivially
267 * add subspecs to it.
269 transform_to_combine_type(filter_options
);
271 string_list_append(&filter_options
->filter_spec
, xstrdup("+"));
272 filter_spec_append_urlencode(filter_options
, arg
);
273 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
274 filter_options
->sub_alloc
);
276 parse_error
= gently_parse_list_objects_filter(
277 &filter_options
->sub
[filter_options
->sub_nr
- 1], arg
,
281 die("%s", errbuf
.buf
);
284 int opt_parse_list_objects_filter(const struct option
*opt
,
285 const char *arg
, int unset
)
287 struct list_objects_filter_options
*filter_options
= opt
->value
;
288 opt_lof_init init
= (opt_lof_init
)opt
->defval
;
291 filter_options
= init(opt
->value
);
294 list_objects_filter_set_no_filter(filter_options
);
296 parse_list_objects_filter(filter_options
, arg
);
300 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
302 if (!filter
->filter_spec
.nr
)
303 BUG("no filter_spec available for this filter");
304 if (filter
->filter_spec
.nr
!= 1) {
305 struct strbuf concatted
= STRBUF_INIT
;
306 strbuf_add_separated_string_list(
307 &concatted
, "", &filter
->filter_spec
);
308 string_list_clear(&filter
->filter_spec
, /*free_util=*/0);
310 &filter
->filter_spec
, strbuf_detach(&concatted
, NULL
));
313 return filter
->filter_spec
.items
[0].string
;
316 const char *expand_list_objects_filter_spec(
317 struct list_objects_filter_options
*filter
)
319 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
320 struct strbuf expanded_spec
= STRBUF_INIT
;
321 strbuf_addf(&expanded_spec
, "blob:limit=%lu",
322 filter
->blob_limit_value
);
323 string_list_clear(&filter
->filter_spec
, /*free_util=*/0);
325 &filter
->filter_spec
,
326 strbuf_detach(&expanded_spec
, NULL
));
329 return list_objects_filter_spec(filter
);
332 void list_objects_filter_release(
333 struct list_objects_filter_options
*filter_options
)
339 string_list_clear(&filter_options
->filter_spec
, /*free_util=*/0);
340 free(filter_options
->sparse_oid_name
);
341 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
342 list_objects_filter_release(&filter_options
->sub
[sub
]);
343 free(filter_options
->sub
);
344 memset(filter_options
, 0, sizeof(*filter_options
));
347 void partial_clone_register(
349 struct list_objects_filter_options
*filter_options
)
351 struct promisor_remote
*promisor_remote
;
355 /* Check if it is already registered */
356 if ((promisor_remote
= promisor_remote_find(remote
))) {
357 if (promisor_remote
->partial_clone_filter
)
359 * Remote is already registered and a filter is already
360 * set, so we don't need to do anything here.
364 if (upgrade_repository_format(1) < 0)
365 die(_("unable to upgrade repository format to support partial clone"));
367 /* Add promisor config for the remote */
368 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
369 git_config_set(cfg_name
, "true");
374 * Record the initial filter-spec in the config as
375 * the default for subsequent fetches from this remote.
377 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
378 /* NEEDSWORK: 'expand' result leaking??? */
379 git_config_set(filter_name
,
380 expand_list_objects_filter_spec(filter_options
));
383 /* Make sure the config info are reset */
384 promisor_remote_reinit();
387 void partial_clone_get_default_filter_spec(
388 struct list_objects_filter_options
*filter_options
,
391 struct promisor_remote
*promisor
= promisor_remote_find(remote
);
392 struct strbuf errbuf
= STRBUF_INIT
;
395 * Parse default value, but silently ignore it if it is invalid.
400 string_list_append(&filter_options
->filter_spec
,
401 promisor
->partial_clone_filter
);
402 gently_parse_list_objects_filter(filter_options
,
403 promisor
->partial_clone_filter
,
405 strbuf_release(&errbuf
);
408 void list_objects_filter_copy(
409 struct list_objects_filter_options
*dest
,
410 const struct list_objects_filter_options
*src
)
413 struct string_list_item
*item
;
415 /* Copy everything. We will overwrite the pointers shortly. */
416 memcpy(dest
, src
, sizeof(struct list_objects_filter_options
));
418 string_list_init_dup(&dest
->filter_spec
);
419 for_each_string_list_item(item
, &src
->filter_spec
)
420 string_list_append(&dest
->filter_spec
, item
->string
);
422 ALLOC_ARRAY(dest
->sub
, dest
->sub_alloc
);
423 for (i
= 0; i
< src
->sub_nr
; i
++)
424 list_objects_filter_copy(&dest
->sub
[i
], &src
->sub
[i
]);