6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
9 #include "promisor-remote.h"
12 #include "parse-options.h"
14 static int parse_combine_filter(
15 struct list_objects_filter_options
*filter_options
,
17 struct strbuf
*errbuf
);
19 const char *list_object_filter_config_name(enum list_objects_filter_choice c
)
23 /* we have no name for "no filter at all" */
33 case LOFC_OBJECT_TYPE
:
38 /* not a real filter type; just the count of all filters */
41 BUG("list_object_filter_config_name: invalid argument '%d'", c
);
44 int gently_parse_list_objects_filter(
45 struct list_objects_filter_options
*filter_options
,
47 struct strbuf
*errbuf
)
54 if (filter_options
->choice
)
55 BUG("filter_options already populated");
57 if (!strcmp(arg
, "blob:none")) {
58 filter_options
->choice
= LOFC_BLOB_NONE
;
61 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
62 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
63 filter_options
->choice
= LOFC_BLOB_LIMIT
;
67 } else if (skip_prefix(arg
, "tree:", &v0
)) {
68 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
69 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
72 filter_options
->choice
= LOFC_TREE_DEPTH
;
75 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
76 filter_options
->sparse_oid_name
= xstrdup(v0
);
77 filter_options
->choice
= LOFC_SPARSE_OID
;
80 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
84 _("sparse:path filters support has been dropped"));
88 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
89 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
91 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
92 "not a valid object type"), v0
);
96 filter_options
->object_type
= type
;
97 filter_options
->choice
= LOFC_OBJECT_TYPE
;
101 } else if (skip_prefix(arg
, "combine:", &v0
)) {
102 return parse_combine_filter(filter_options
, v0
, errbuf
);
106 * Please update _git_fetch() in git-completion.bash when you
110 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
112 list_objects_filter_init(filter_options
);
116 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
118 static int has_reserved_character(
119 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
121 const char *c
= sub_spec
->buf
;
123 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
126 _("must escape char in sub-filter-spec: '%c'"),
136 static int parse_combine_subfilter(
137 struct list_objects_filter_options
*filter_options
,
138 struct strbuf
*subspec
,
139 struct strbuf
*errbuf
)
141 size_t new_index
= filter_options
->sub_nr
;
145 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
146 filter_options
->sub_alloc
);
147 list_objects_filter_init(&filter_options
->sub
[new_index
]);
149 decoded
= url_percent_decode(subspec
->buf
);
151 result
= has_reserved_character(subspec
, errbuf
) ||
152 gently_parse_list_objects_filter(
153 &filter_options
->sub
[new_index
], decoded
, errbuf
);
159 static int parse_combine_filter(
160 struct list_objects_filter_options
*filter_options
,
162 struct strbuf
*errbuf
)
164 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
169 strbuf_addstr(errbuf
, _("expected something after combine:"));
174 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
175 if (subspecs
[sub
+ 1]) {
177 * This is not the last subspec. Remove trailing "+" so
180 size_t last
= subspecs
[sub
]->len
- 1;
181 assert(subspecs
[sub
]->buf
[last
] == '+');
182 strbuf_remove(subspecs
[sub
], last
, 1);
184 result
= parse_combine_subfilter(
185 filter_options
, subspecs
[sub
], errbuf
);
188 filter_options
->choice
= LOFC_COMBINE
;
191 strbuf_list_free(subspecs
);
193 list_objects_filter_release(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 size_t orig_len
= filter
->filter_spec
.len
;
208 strbuf_addstr_urlencode(&filter
->filter_spec
, raw
, allow_unencoded
);
209 trace_printf("Add to combine filter-spec: %s\n",
210 filter
->filter_spec
.buf
+ orig_len
);
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 list_objects_filter_init(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 strbuf_addstr(&filter_options
->filter_spec
, "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 strbuf_release(&filter_options
->sub
[0].filter_spec
);
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
->filter_spec
.buf
)
260 BUG("filter_options not properly initialized");
262 if (!filter_options
->choice
) {
263 strbuf_addstr(&filter_options
->filter_spec
, arg
);
265 parse_error
= gently_parse_list_objects_filter(
266 filter_options
, arg
, &errbuf
);
268 struct list_objects_filter_options
*sub
;
271 * Make filter_options an LOFC_COMBINE spec so we can trivially
272 * add subspecs to it.
274 transform_to_combine_type(filter_options
);
276 strbuf_addch(&filter_options
->filter_spec
, '+');
277 filter_spec_append_urlencode(filter_options
, arg
);
278 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
279 filter_options
->sub_alloc
);
280 sub
= &filter_options
->sub
[filter_options
->sub_nr
- 1];
282 list_objects_filter_init(sub
);
283 parse_error
= gently_parse_list_objects_filter(sub
, arg
,
287 die("%s", errbuf
.buf
);
290 int opt_parse_list_objects_filter(const struct option
*opt
,
291 const char *arg
, int unset
)
293 struct list_objects_filter_options
*filter_options
= opt
->value
;
296 list_objects_filter_set_no_filter(filter_options
);
298 parse_list_objects_filter(filter_options
, arg
);
302 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
304 if (!filter
->filter_spec
.len
)
305 BUG("no filter_spec available for this filter");
306 return filter
->filter_spec
.buf
;
309 const char *expand_list_objects_filter_spec(
310 struct list_objects_filter_options
*filter
)
312 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
313 strbuf_release(&filter
->filter_spec
);
314 strbuf_addf(&filter
->filter_spec
, "blob:limit=%lu",
315 filter
->blob_limit_value
);
318 return list_objects_filter_spec(filter
);
321 void list_objects_filter_release(
322 struct list_objects_filter_options
*filter_options
)
328 strbuf_release(&filter_options
->filter_spec
);
329 free(filter_options
->sparse_oid_name
);
330 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
331 list_objects_filter_release(&filter_options
->sub
[sub
]);
332 free(filter_options
->sub
);
333 list_objects_filter_init(filter_options
);
336 void partial_clone_register(
338 struct list_objects_filter_options
*filter_options
)
340 struct promisor_remote
*promisor_remote
;
344 /* Check if it is already registered */
345 if ((promisor_remote
= promisor_remote_find(remote
))) {
346 if (promisor_remote
->partial_clone_filter
)
348 * Remote is already registered and a filter is already
349 * set, so we don't need to do anything here.
353 if (upgrade_repository_format(1) < 0)
354 die(_("unable to upgrade repository format to support partial clone"));
356 /* Add promisor config for the remote */
357 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
358 git_config_set(cfg_name
, "true");
363 * Record the initial filter-spec in the config as
364 * the default for subsequent fetches from this remote.
366 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
367 /* NEEDSWORK: 'expand' result leaking??? */
368 git_config_set(filter_name
,
369 expand_list_objects_filter_spec(filter_options
));
372 /* Make sure the config info are reset */
373 promisor_remote_reinit();
376 void partial_clone_get_default_filter_spec(
377 struct list_objects_filter_options
*filter_options
,
380 struct promisor_remote
*promisor
= promisor_remote_find(remote
);
381 struct strbuf errbuf
= STRBUF_INIT
;
384 * Parse default value, but silently ignore it if it is invalid.
386 if (!promisor
|| !promisor
->partial_clone_filter
)
389 strbuf_addstr(&filter_options
->filter_spec
,
390 promisor
->partial_clone_filter
);
391 gently_parse_list_objects_filter(filter_options
,
392 promisor
->partial_clone_filter
,
394 strbuf_release(&errbuf
);
397 void list_objects_filter_copy(
398 struct list_objects_filter_options
*dest
,
399 const struct list_objects_filter_options
*src
)
403 /* Copy everything. We will overwrite the pointers shortly. */
404 memcpy(dest
, src
, sizeof(struct list_objects_filter_options
));
406 strbuf_init(&dest
->filter_spec
, 0);
407 strbuf_addbuf(&dest
->filter_spec
, &src
->filter_spec
);
408 dest
->sparse_oid_name
= xstrdup_or_null(src
->sparse_oid_name
);
410 ALLOC_ARRAY(dest
->sub
, dest
->sub_alloc
);
411 for (i
= 0; i
< src
->sub_nr
; i
++)
412 list_objects_filter_copy(&dest
->sub
[i
], &src
->sub
[i
]);
415 void list_objects_filter_init(struct list_objects_filter_options
*filter_options
)
417 struct list_objects_filter_options blank
= LIST_OBJECTS_FILTER_INIT
;
418 memcpy(filter_options
, &blank
, sizeof(*filter_options
));