1 #include "git-compat-util.h"
4 #include "list-objects-filter-options.h"
5 #include "promisor-remote.h"
8 #include "parse-options.h"
10 static int parse_combine_filter(
11 struct list_objects_filter_options
*filter_options
,
13 struct strbuf
*errbuf
);
15 const char *list_object_filter_config_name(enum list_objects_filter_choice c
)
19 /* we have no name for "no filter at all" */
29 case LOFC_OBJECT_TYPE
:
34 /* not a real filter type; just the count of all filters */
37 BUG("list_object_filter_config_name: invalid argument '%d'", c
);
40 int gently_parse_list_objects_filter(
41 struct list_objects_filter_options
*filter_options
,
43 struct strbuf
*errbuf
)
50 if (filter_options
->choice
)
51 BUG("filter_options already populated");
53 if (!strcmp(arg
, "blob:none")) {
54 filter_options
->choice
= LOFC_BLOB_NONE
;
57 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
58 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
59 filter_options
->choice
= LOFC_BLOB_LIMIT
;
63 } else if (skip_prefix(arg
, "tree:", &v0
)) {
64 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
65 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
68 filter_options
->choice
= LOFC_TREE_DEPTH
;
71 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
72 filter_options
->sparse_oid_name
= xstrdup(v0
);
73 filter_options
->choice
= LOFC_SPARSE_OID
;
76 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
80 _("sparse:path filters support has been dropped"));
84 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
85 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
87 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
88 "not a valid object type"), v0
);
92 filter_options
->object_type
= type
;
93 filter_options
->choice
= LOFC_OBJECT_TYPE
;
97 } else if (skip_prefix(arg
, "combine:", &v0
)) {
98 return parse_combine_filter(filter_options
, v0
, errbuf
);
102 * Please update _git_fetch() in git-completion.bash when you
106 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
108 list_objects_filter_init(filter_options
);
112 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
114 static int has_reserved_character(
115 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
117 const char *c
= sub_spec
->buf
;
119 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
122 _("must escape char in sub-filter-spec: '%c'"),
132 static int parse_combine_subfilter(
133 struct list_objects_filter_options
*filter_options
,
134 struct strbuf
*subspec
,
135 struct strbuf
*errbuf
)
137 size_t new_index
= filter_options
->sub_nr
;
141 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
142 filter_options
->sub_alloc
);
143 list_objects_filter_init(&filter_options
->sub
[new_index
]);
145 decoded
= url_percent_decode(subspec
->buf
);
147 result
= has_reserved_character(subspec
, errbuf
) ||
148 gently_parse_list_objects_filter(
149 &filter_options
->sub
[new_index
], decoded
, errbuf
);
155 static int parse_combine_filter(
156 struct list_objects_filter_options
*filter_options
,
158 struct strbuf
*errbuf
)
160 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
165 strbuf_addstr(errbuf
, _("expected something after combine:"));
170 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
171 if (subspecs
[sub
+ 1]) {
173 * This is not the last subspec. Remove trailing "+" so
176 size_t last
= subspecs
[sub
]->len
- 1;
177 assert(subspecs
[sub
]->buf
[last
] == '+');
178 strbuf_remove(subspecs
[sub
], last
, 1);
180 result
= parse_combine_subfilter(
181 filter_options
, subspecs
[sub
], errbuf
);
184 filter_options
->choice
= LOFC_COMBINE
;
187 strbuf_list_free(subspecs
);
189 list_objects_filter_release(filter_options
);
193 static int allow_unencoded(char ch
)
195 if (ch
<= ' ' || ch
== '%' || ch
== '+')
197 return !strchr(RESERVED_NON_WS
, ch
);
200 static void filter_spec_append_urlencode(
201 struct list_objects_filter_options
*filter
, const char *raw
)
203 size_t orig_len
= filter
->filter_spec
.len
;
204 strbuf_addstr_urlencode(&filter
->filter_spec
, raw
, allow_unencoded
);
205 trace_printf("Add to combine filter-spec: %s\n",
206 filter
->filter_spec
.buf
+ orig_len
);
210 * Changes filter_options into an equivalent LOFC_COMBINE filter options
211 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
213 static void transform_to_combine_type(
214 struct list_objects_filter_options
*filter_options
)
216 assert(filter_options
->choice
);
217 if (filter_options
->choice
== LOFC_COMBINE
)
220 const int initial_sub_alloc
= 2;
221 struct list_objects_filter_options
*sub_array
=
222 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
223 sub_array
[0] = *filter_options
;
224 list_objects_filter_init(filter_options
);
225 filter_options
->sub
= sub_array
;
226 filter_options
->sub_alloc
= initial_sub_alloc
;
228 filter_options
->sub_nr
= 1;
229 filter_options
->choice
= LOFC_COMBINE
;
230 strbuf_addstr(&filter_options
->filter_spec
, "combine:");
231 filter_spec_append_urlencode(
233 list_objects_filter_spec(&filter_options
->sub
[0]));
235 * We don't need the filter_spec strings for subfilter specs, only the
238 strbuf_release(&filter_options
->sub
[0].filter_spec
);
241 void list_objects_filter_die_if_populated(
242 struct list_objects_filter_options
*filter_options
)
244 if (filter_options
->choice
)
245 die(_("multiple filter-specs cannot be combined"));
248 void parse_list_objects_filter(
249 struct list_objects_filter_options
*filter_options
,
252 struct strbuf errbuf
= STRBUF_INIT
;
255 if (!filter_options
->filter_spec
.buf
)
256 BUG("filter_options not properly initialized");
258 if (!filter_options
->choice
) {
259 strbuf_addstr(&filter_options
->filter_spec
, arg
);
261 parse_error
= gently_parse_list_objects_filter(
262 filter_options
, arg
, &errbuf
);
264 struct list_objects_filter_options
*sub
;
267 * Make filter_options an LOFC_COMBINE spec so we can trivially
268 * add subspecs to it.
270 transform_to_combine_type(filter_options
);
272 strbuf_addch(&filter_options
->filter_spec
, '+');
273 filter_spec_append_urlencode(filter_options
, arg
);
274 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
275 filter_options
->sub_alloc
);
276 sub
= &filter_options
->sub
[filter_options
->sub_nr
- 1];
278 list_objects_filter_init(sub
);
279 parse_error
= gently_parse_list_objects_filter(sub
, arg
,
283 die("%s", errbuf
.buf
);
286 int opt_parse_list_objects_filter(const struct option
*opt
,
287 const char *arg
, int unset
)
289 struct list_objects_filter_options
*filter_options
= opt
->value
;
292 list_objects_filter_set_no_filter(filter_options
);
294 parse_list_objects_filter(filter_options
, arg
);
298 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
300 if (!filter
->filter_spec
.len
)
301 BUG("no filter_spec available for this filter");
302 return filter
->filter_spec
.buf
;
305 const char *expand_list_objects_filter_spec(
306 struct list_objects_filter_options
*filter
)
308 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
309 strbuf_release(&filter
->filter_spec
);
310 strbuf_addf(&filter
->filter_spec
, "blob:limit=%lu",
311 filter
->blob_limit_value
);
314 return list_objects_filter_spec(filter
);
317 void list_objects_filter_release(
318 struct list_objects_filter_options
*filter_options
)
324 strbuf_release(&filter_options
->filter_spec
);
325 free(filter_options
->sparse_oid_name
);
326 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
327 list_objects_filter_release(&filter_options
->sub
[sub
]);
328 free(filter_options
->sub
);
329 list_objects_filter_init(filter_options
);
332 void partial_clone_register(
334 struct list_objects_filter_options
*filter_options
)
336 struct promisor_remote
*promisor_remote
;
340 /* Check if it is already registered */
341 if ((promisor_remote
= repo_promisor_remote_find(the_repository
, remote
))) {
342 if (promisor_remote
->partial_clone_filter
)
344 * Remote is already registered and a filter is already
345 * set, so we don't need to do anything here.
349 if (upgrade_repository_format(1) < 0)
350 die(_("unable to upgrade repository format to support partial clone"));
352 /* Add promisor config for the remote */
353 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
354 git_config_set(cfg_name
, "true");
359 * Record the initial filter-spec in the config as
360 * the default for subsequent fetches from this remote.
362 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
363 /* NEEDSWORK: 'expand' result leaking??? */
364 git_config_set(filter_name
,
365 expand_list_objects_filter_spec(filter_options
));
368 /* Make sure the config info are reset */
369 repo_promisor_remote_reinit(the_repository
);
372 void partial_clone_get_default_filter_spec(
373 struct list_objects_filter_options
*filter_options
,
376 struct promisor_remote
*promisor
= repo_promisor_remote_find(the_repository
,
378 struct strbuf errbuf
= STRBUF_INIT
;
381 * Parse default value, but silently ignore it if it is invalid.
383 if (!promisor
|| !promisor
->partial_clone_filter
)
386 strbuf_addstr(&filter_options
->filter_spec
,
387 promisor
->partial_clone_filter
);
388 gently_parse_list_objects_filter(filter_options
,
389 promisor
->partial_clone_filter
,
391 strbuf_release(&errbuf
);
394 void list_objects_filter_copy(
395 struct list_objects_filter_options
*dest
,
396 const struct list_objects_filter_options
*src
)
400 /* Copy everything. We will overwrite the pointers shortly. */
401 memcpy(dest
, src
, sizeof(struct list_objects_filter_options
));
403 strbuf_init(&dest
->filter_spec
, 0);
404 strbuf_addbuf(&dest
->filter_spec
, &src
->filter_spec
);
405 dest
->sparse_oid_name
= xstrdup_or_null(src
->sparse_oid_name
);
407 ALLOC_ARRAY(dest
->sub
, dest
->sub_alloc
);
408 for (i
= 0; i
< src
->sub_nr
; i
++)
409 list_objects_filter_copy(&dest
->sub
[i
], &src
->sub
[i
]);
412 void list_objects_filter_init(struct list_objects_filter_options
*filter_options
)
414 struct list_objects_filter_options blank
= LIST_OBJECTS_FILTER_INIT
;
415 memcpy(filter_options
, &blank
, sizeof(*filter_options
));