1 #include "git-compat-util.h"
7 #include "list-objects.h"
8 #include "list-objects-filter.h"
9 #include "list-objects-filter-options.h"
10 #include "promisor-remote.h"
13 #include "parse-options.h"
15 static int parse_combine_filter(
16 struct list_objects_filter_options
*filter_options
,
18 struct strbuf
*errbuf
);
20 const char *list_object_filter_config_name(enum list_objects_filter_choice c
)
24 /* we have no name for "no filter at all" */
34 case LOFC_OBJECT_TYPE
:
39 /* not a real filter type; just the count of all filters */
42 BUG("list_object_filter_config_name: invalid argument '%d'", c
);
45 int gently_parse_list_objects_filter(
46 struct list_objects_filter_options
*filter_options
,
48 struct strbuf
*errbuf
)
55 if (filter_options
->choice
)
56 BUG("filter_options already populated");
58 if (!strcmp(arg
, "blob:none")) {
59 filter_options
->choice
= LOFC_BLOB_NONE
;
62 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
63 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
64 filter_options
->choice
= LOFC_BLOB_LIMIT
;
68 } else if (skip_prefix(arg
, "tree:", &v0
)) {
69 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
70 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
73 filter_options
->choice
= LOFC_TREE_DEPTH
;
76 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
77 filter_options
->sparse_oid_name
= xstrdup(v0
);
78 filter_options
->choice
= LOFC_SPARSE_OID
;
81 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
85 _("sparse:path filters support has been dropped"));
89 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
90 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
92 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
93 "not a valid object type"), v0
);
97 filter_options
->object_type
= type
;
98 filter_options
->choice
= LOFC_OBJECT_TYPE
;
102 } else if (skip_prefix(arg
, "combine:", &v0
)) {
103 return parse_combine_filter(filter_options
, v0
, errbuf
);
107 * Please update _git_fetch() in git-completion.bash when you
111 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
113 list_objects_filter_init(filter_options
);
117 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
119 static int has_reserved_character(
120 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
122 const char *c
= sub_spec
->buf
;
124 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
127 _("must escape char in sub-filter-spec: '%c'"),
137 static int parse_combine_subfilter(
138 struct list_objects_filter_options
*filter_options
,
139 struct strbuf
*subspec
,
140 struct strbuf
*errbuf
)
142 size_t new_index
= filter_options
->sub_nr
;
146 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
147 filter_options
->sub_alloc
);
148 list_objects_filter_init(&filter_options
->sub
[new_index
]);
150 decoded
= url_percent_decode(subspec
->buf
);
152 result
= has_reserved_character(subspec
, errbuf
) ||
153 gently_parse_list_objects_filter(
154 &filter_options
->sub
[new_index
], decoded
, errbuf
);
160 static int parse_combine_filter(
161 struct list_objects_filter_options
*filter_options
,
163 struct strbuf
*errbuf
)
165 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
170 strbuf_addstr(errbuf
, _("expected something after combine:"));
175 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
176 if (subspecs
[sub
+ 1]) {
178 * This is not the last subspec. Remove trailing "+" so
181 size_t last
= subspecs
[sub
]->len
- 1;
182 assert(subspecs
[sub
]->buf
[last
] == '+');
183 strbuf_remove(subspecs
[sub
], last
, 1);
185 result
= parse_combine_subfilter(
186 filter_options
, subspecs
[sub
], errbuf
);
189 filter_options
->choice
= LOFC_COMBINE
;
192 strbuf_list_free(subspecs
);
194 list_objects_filter_release(filter_options
);
198 static int allow_unencoded(char ch
)
200 if (ch
<= ' ' || ch
== '%' || ch
== '+')
202 return !strchr(RESERVED_NON_WS
, ch
);
205 static void filter_spec_append_urlencode(
206 struct list_objects_filter_options
*filter
, const char *raw
)
208 size_t orig_len
= filter
->filter_spec
.len
;
209 strbuf_addstr_urlencode(&filter
->filter_spec
, raw
, allow_unencoded
);
210 trace_printf("Add to combine filter-spec: %s\n",
211 filter
->filter_spec
.buf
+ orig_len
);
215 * Changes filter_options into an equivalent LOFC_COMBINE filter options
216 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
218 static void transform_to_combine_type(
219 struct list_objects_filter_options
*filter_options
)
221 assert(filter_options
->choice
);
222 if (filter_options
->choice
== LOFC_COMBINE
)
225 const int initial_sub_alloc
= 2;
226 struct list_objects_filter_options
*sub_array
=
227 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
228 sub_array
[0] = *filter_options
;
229 list_objects_filter_init(filter_options
);
230 filter_options
->sub
= sub_array
;
231 filter_options
->sub_alloc
= initial_sub_alloc
;
233 filter_options
->sub_nr
= 1;
234 filter_options
->choice
= LOFC_COMBINE
;
235 strbuf_addstr(&filter_options
->filter_spec
, "combine:");
236 filter_spec_append_urlencode(
238 list_objects_filter_spec(&filter_options
->sub
[0]));
240 * We don't need the filter_spec strings for subfilter specs, only the
243 strbuf_release(&filter_options
->sub
[0].filter_spec
);
246 void list_objects_filter_die_if_populated(
247 struct list_objects_filter_options
*filter_options
)
249 if (filter_options
->choice
)
250 die(_("multiple filter-specs cannot be combined"));
253 void parse_list_objects_filter(
254 struct list_objects_filter_options
*filter_options
,
257 struct strbuf errbuf
= STRBUF_INIT
;
260 if (!filter_options
->filter_spec
.buf
)
261 BUG("filter_options not properly initialized");
263 if (!filter_options
->choice
) {
264 strbuf_addstr(&filter_options
->filter_spec
, arg
);
266 parse_error
= gently_parse_list_objects_filter(
267 filter_options
, arg
, &errbuf
);
269 struct list_objects_filter_options
*sub
;
272 * Make filter_options an LOFC_COMBINE spec so we can trivially
273 * add subspecs to it.
275 transform_to_combine_type(filter_options
);
277 strbuf_addch(&filter_options
->filter_spec
, '+');
278 filter_spec_append_urlencode(filter_options
, arg
);
279 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
280 filter_options
->sub_alloc
);
281 sub
= &filter_options
->sub
[filter_options
->sub_nr
- 1];
283 list_objects_filter_init(sub
);
284 parse_error
= gently_parse_list_objects_filter(sub
, arg
,
288 die("%s", errbuf
.buf
);
291 int opt_parse_list_objects_filter(const struct option
*opt
,
292 const char *arg
, int unset
)
294 struct list_objects_filter_options
*filter_options
= opt
->value
;
297 list_objects_filter_set_no_filter(filter_options
);
299 parse_list_objects_filter(filter_options
, arg
);
303 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
305 if (!filter
->filter_spec
.len
)
306 BUG("no filter_spec available for this filter");
307 return filter
->filter_spec
.buf
;
310 const char *expand_list_objects_filter_spec(
311 struct list_objects_filter_options
*filter
)
313 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
314 strbuf_release(&filter
->filter_spec
);
315 strbuf_addf(&filter
->filter_spec
, "blob:limit=%lu",
316 filter
->blob_limit_value
);
319 return list_objects_filter_spec(filter
);
322 void list_objects_filter_release(
323 struct list_objects_filter_options
*filter_options
)
329 strbuf_release(&filter_options
->filter_spec
);
330 free(filter_options
->sparse_oid_name
);
331 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
332 list_objects_filter_release(&filter_options
->sub
[sub
]);
333 free(filter_options
->sub
);
334 list_objects_filter_init(filter_options
);
337 void partial_clone_register(
339 struct list_objects_filter_options
*filter_options
)
341 struct promisor_remote
*promisor_remote
;
345 /* Check if it is already registered */
346 if ((promisor_remote
= repo_promisor_remote_find(the_repository
, remote
))) {
347 if (promisor_remote
->partial_clone_filter
)
349 * Remote is already registered and a filter is already
350 * set, so we don't need to do anything here.
354 if (upgrade_repository_format(1) < 0)
355 die(_("unable to upgrade repository format to support partial clone"));
357 /* Add promisor config for the remote */
358 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
359 git_config_set(cfg_name
, "true");
364 * Record the initial filter-spec in the config as
365 * the default for subsequent fetches from this remote.
367 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
368 /* NEEDSWORK: 'expand' result leaking??? */
369 git_config_set(filter_name
,
370 expand_list_objects_filter_spec(filter_options
));
373 /* Make sure the config info are reset */
374 repo_promisor_remote_reinit(the_repository
);
377 void partial_clone_get_default_filter_spec(
378 struct list_objects_filter_options
*filter_options
,
381 struct promisor_remote
*promisor
= repo_promisor_remote_find(the_repository
,
383 struct strbuf errbuf
= STRBUF_INIT
;
386 * Parse default value, but silently ignore it if it is invalid.
388 if (!promisor
|| !promisor
->partial_clone_filter
)
391 strbuf_addstr(&filter_options
->filter_spec
,
392 promisor
->partial_clone_filter
);
393 gently_parse_list_objects_filter(filter_options
,
394 promisor
->partial_clone_filter
,
396 strbuf_release(&errbuf
);
399 void list_objects_filter_copy(
400 struct list_objects_filter_options
*dest
,
401 const struct list_objects_filter_options
*src
)
405 /* Copy everything. We will overwrite the pointers shortly. */
406 memcpy(dest
, src
, sizeof(struct list_objects_filter_options
));
408 strbuf_init(&dest
->filter_spec
, 0);
409 strbuf_addbuf(&dest
->filter_spec
, &src
->filter_spec
);
410 dest
->sparse_oid_name
= xstrdup_or_null(src
->sparse_oid_name
);
412 ALLOC_ARRAY(dest
->sub
, dest
->sub_alloc
);
413 for (i
= 0; i
< src
->sub_nr
; i
++)
414 list_objects_filter_copy(&dest
->sub
[i
], &src
->sub
[i
]);
417 void list_objects_filter_init(struct list_objects_filter_options
*filter_options
)
419 struct list_objects_filter_options blank
= LIST_OBJECTS_FILTER_INIT
;
420 memcpy(filter_options
, &blank
, sizeof(*filter_options
));