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 list_objects_filter_init(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
);
146 list_objects_filter_init(&filter_options
->sub
[new_index
]);
148 decoded
= url_percent_decode(subspec
->buf
);
150 result
= has_reserved_character(subspec
, errbuf
) ||
151 gently_parse_list_objects_filter(
152 &filter_options
->sub
[new_index
], decoded
, errbuf
);
158 static int parse_combine_filter(
159 struct list_objects_filter_options
*filter_options
,
161 struct strbuf
*errbuf
)
163 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
168 strbuf_addstr(errbuf
, _("expected something after combine:"));
173 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
174 if (subspecs
[sub
+ 1]) {
176 * This is not the last subspec. Remove trailing "+" so
179 size_t last
= subspecs
[sub
]->len
- 1;
180 assert(subspecs
[sub
]->buf
[last
] == '+');
181 strbuf_remove(subspecs
[sub
], last
, 1);
183 result
= parse_combine_subfilter(
184 filter_options
, subspecs
[sub
], errbuf
);
187 filter_options
->choice
= LOFC_COMBINE
;
190 strbuf_list_free(subspecs
);
192 list_objects_filter_release(filter_options
);
196 static int allow_unencoded(char ch
)
198 if (ch
<= ' ' || ch
== '%' || ch
== '+')
200 return !strchr(RESERVED_NON_WS
, ch
);
203 static void filter_spec_append_urlencode(
204 struct list_objects_filter_options
*filter
, const char *raw
)
206 size_t orig_len
= filter
->filter_spec
.len
;
207 strbuf_addstr_urlencode(&filter
->filter_spec
, raw
, allow_unencoded
);
208 trace_printf("Add to combine filter-spec: %s\n",
209 filter
->filter_spec
.buf
+ orig_len
);
213 * Changes filter_options into an equivalent LOFC_COMBINE filter options
214 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
216 static void transform_to_combine_type(
217 struct list_objects_filter_options
*filter_options
)
219 assert(filter_options
->choice
);
220 if (filter_options
->choice
== LOFC_COMBINE
)
223 const int initial_sub_alloc
= 2;
224 struct list_objects_filter_options
*sub_array
=
225 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
226 sub_array
[0] = *filter_options
;
227 list_objects_filter_init(filter_options
);
228 filter_options
->sub
= sub_array
;
229 filter_options
->sub_alloc
= initial_sub_alloc
;
231 filter_options
->sub_nr
= 1;
232 filter_options
->choice
= LOFC_COMBINE
;
233 strbuf_addstr(&filter_options
->filter_spec
, "combine:");
234 filter_spec_append_urlencode(
236 list_objects_filter_spec(&filter_options
->sub
[0]));
238 * We don't need the filter_spec strings for subfilter specs, only the
241 strbuf_release(&filter_options
->sub
[0].filter_spec
);
244 void list_objects_filter_die_if_populated(
245 struct list_objects_filter_options
*filter_options
)
247 if (filter_options
->choice
)
248 die(_("multiple filter-specs cannot be combined"));
251 void parse_list_objects_filter(
252 struct list_objects_filter_options
*filter_options
,
255 struct strbuf errbuf
= STRBUF_INIT
;
258 if (!filter_options
->filter_spec
.buf
)
259 BUG("filter_options not properly initialized");
261 if (!filter_options
->choice
) {
262 strbuf_addstr(&filter_options
->filter_spec
, arg
);
264 parse_error
= gently_parse_list_objects_filter(
265 filter_options
, arg
, &errbuf
);
267 struct list_objects_filter_options
*sub
;
270 * Make filter_options an LOFC_COMBINE spec so we can trivially
271 * add subspecs to it.
273 transform_to_combine_type(filter_options
);
275 strbuf_addch(&filter_options
->filter_spec
, '+');
276 filter_spec_append_urlencode(filter_options
, arg
);
277 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
278 filter_options
->sub_alloc
);
279 sub
= &filter_options
->sub
[filter_options
->sub_nr
- 1];
281 list_objects_filter_init(sub
);
282 parse_error
= gently_parse_list_objects_filter(sub
, arg
,
286 die("%s", errbuf
.buf
);
289 int opt_parse_list_objects_filter(const struct option
*opt
,
290 const char *arg
, int unset
)
292 struct list_objects_filter_options
*filter_options
= opt
->value
;
293 opt_lof_init init
= (opt_lof_init
)opt
->defval
;
296 filter_options
= init(opt
->value
);
299 list_objects_filter_set_no_filter(filter_options
);
301 parse_list_objects_filter(filter_options
, arg
);
305 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
307 if (!filter
->filter_spec
.len
)
308 BUG("no filter_spec available for this filter");
309 return filter
->filter_spec
.buf
;
312 const char *expand_list_objects_filter_spec(
313 struct list_objects_filter_options
*filter
)
315 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
316 strbuf_release(&filter
->filter_spec
);
317 strbuf_addf(&filter
->filter_spec
, "blob:limit=%lu",
318 filter
->blob_limit_value
);
321 return list_objects_filter_spec(filter
);
324 void list_objects_filter_release(
325 struct list_objects_filter_options
*filter_options
)
331 strbuf_release(&filter_options
->filter_spec
);
332 free(filter_options
->sparse_oid_name
);
333 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
334 list_objects_filter_release(&filter_options
->sub
[sub
]);
335 free(filter_options
->sub
);
336 list_objects_filter_init(filter_options
);
339 void partial_clone_register(
341 struct list_objects_filter_options
*filter_options
)
343 struct promisor_remote
*promisor_remote
;
347 /* Check if it is already registered */
348 if ((promisor_remote
= promisor_remote_find(remote
))) {
349 if (promisor_remote
->partial_clone_filter
)
351 * Remote is already registered and a filter is already
352 * set, so we don't need to do anything here.
356 if (upgrade_repository_format(1) < 0)
357 die(_("unable to upgrade repository format to support partial clone"));
359 /* Add promisor config for the remote */
360 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
361 git_config_set(cfg_name
, "true");
366 * Record the initial filter-spec in the config as
367 * the default for subsequent fetches from this remote.
369 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
370 /* NEEDSWORK: 'expand' result leaking??? */
371 git_config_set(filter_name
,
372 expand_list_objects_filter_spec(filter_options
));
375 /* Make sure the config info are reset */
376 promisor_remote_reinit();
379 void partial_clone_get_default_filter_spec(
380 struct list_objects_filter_options
*filter_options
,
383 struct promisor_remote
*promisor
= promisor_remote_find(remote
);
384 struct strbuf errbuf
= STRBUF_INIT
;
387 * Parse default value, but silently ignore it if it is invalid.
389 if (!promisor
|| !promisor
->partial_clone_filter
)
392 strbuf_addstr(&filter_options
->filter_spec
,
393 promisor
->partial_clone_filter
);
394 gently_parse_list_objects_filter(filter_options
,
395 promisor
->partial_clone_filter
,
397 strbuf_release(&errbuf
);
400 void list_objects_filter_copy(
401 struct list_objects_filter_options
*dest
,
402 const struct list_objects_filter_options
*src
)
406 /* Copy everything. We will overwrite the pointers shortly. */
407 memcpy(dest
, src
, sizeof(struct list_objects_filter_options
));
409 strbuf_init(&dest
->filter_spec
, 0);
410 strbuf_addbuf(&dest
->filter_spec
, &src
->filter_spec
);
411 dest
->sparse_oid_name
= xstrdup_or_null(src
->sparse_oid_name
);
413 ALLOC_ARRAY(dest
->sub
, dest
->sub_alloc
);
414 for (i
= 0; i
< src
->sub_nr
; i
++)
415 list_objects_filter_copy(&dest
->sub
[i
], &src
->sub
[i
]);
418 void list_objects_filter_init(struct list_objects_filter_options
*filter_options
)
420 struct list_objects_filter_options blank
= LIST_OBJECTS_FILTER_INIT
;
421 memcpy(filter_options
, &blank
, sizeof(*filter_options
));