1 #include "git-compat-util.h"
8 #include "list-objects.h"
9 #include "list-objects-filter.h"
10 #include "list-objects-filter-options.h"
11 #include "promisor-remote.h"
14 #include "parse-options.h"
16 static int parse_combine_filter(
17 struct list_objects_filter_options
*filter_options
,
19 struct strbuf
*errbuf
);
21 const char *list_object_filter_config_name(enum list_objects_filter_choice c
)
25 /* we have no name for "no filter at all" */
35 case LOFC_OBJECT_TYPE
:
40 /* not a real filter type; just the count of all filters */
43 BUG("list_object_filter_config_name: invalid argument '%d'", c
);
46 int gently_parse_list_objects_filter(
47 struct list_objects_filter_options
*filter_options
,
49 struct strbuf
*errbuf
)
56 if (filter_options
->choice
)
57 BUG("filter_options already populated");
59 if (!strcmp(arg
, "blob:none")) {
60 filter_options
->choice
= LOFC_BLOB_NONE
;
63 } else if (skip_prefix(arg
, "blob:limit=", &v0
)) {
64 if (git_parse_ulong(v0
, &filter_options
->blob_limit_value
)) {
65 filter_options
->choice
= LOFC_BLOB_LIMIT
;
69 } else if (skip_prefix(arg
, "tree:", &v0
)) {
70 if (!git_parse_ulong(v0
, &filter_options
->tree_exclude_depth
)) {
71 strbuf_addstr(errbuf
, _("expected 'tree:<depth>'"));
74 filter_options
->choice
= LOFC_TREE_DEPTH
;
77 } else if (skip_prefix(arg
, "sparse:oid=", &v0
)) {
78 filter_options
->sparse_oid_name
= xstrdup(v0
);
79 filter_options
->choice
= LOFC_SPARSE_OID
;
82 } else if (skip_prefix(arg
, "sparse:path=", &v0
)) {
86 _("sparse:path filters support has been dropped"));
90 } else if (skip_prefix(arg
, "object:type=", &v0
)) {
91 int type
= type_from_string_gently(v0
, strlen(v0
), 1);
93 strbuf_addf(errbuf
, _("'%s' for 'object:type=<type>' is "
94 "not a valid object type"), v0
);
98 filter_options
->object_type
= type
;
99 filter_options
->choice
= LOFC_OBJECT_TYPE
;
103 } else if (skip_prefix(arg
, "combine:", &v0
)) {
104 return parse_combine_filter(filter_options
, v0
, errbuf
);
108 * Please update _git_fetch() in git-completion.bash when you
112 strbuf_addf(errbuf
, _("invalid filter-spec '%s'"), arg
);
114 list_objects_filter_init(filter_options
);
118 static const char *RESERVED_NON_WS
= "~`!@#$^&*()[]{}\\;'\",<>?";
120 static int has_reserved_character(
121 struct strbuf
*sub_spec
, struct strbuf
*errbuf
)
123 const char *c
= sub_spec
->buf
;
125 if (*c
<= ' ' || strchr(RESERVED_NON_WS
, *c
)) {
128 _("must escape char in sub-filter-spec: '%c'"),
138 static int parse_combine_subfilter(
139 struct list_objects_filter_options
*filter_options
,
140 struct strbuf
*subspec
,
141 struct strbuf
*errbuf
)
143 size_t new_index
= filter_options
->sub_nr
;
147 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
148 filter_options
->sub_alloc
);
149 list_objects_filter_init(&filter_options
->sub
[new_index
]);
151 decoded
= url_percent_decode(subspec
->buf
);
153 result
= has_reserved_character(subspec
, errbuf
) ||
154 gently_parse_list_objects_filter(
155 &filter_options
->sub
[new_index
], decoded
, errbuf
);
161 static int parse_combine_filter(
162 struct list_objects_filter_options
*filter_options
,
164 struct strbuf
*errbuf
)
166 struct strbuf
**subspecs
= strbuf_split_str(arg
, '+', 0);
171 strbuf_addstr(errbuf
, _("expected something after combine:"));
176 for (sub
= 0; subspecs
[sub
] && !result
; sub
++) {
177 if (subspecs
[sub
+ 1]) {
179 * This is not the last subspec. Remove trailing "+" so
182 size_t last
= subspecs
[sub
]->len
- 1;
183 assert(subspecs
[sub
]->buf
[last
] == '+');
184 strbuf_remove(subspecs
[sub
], last
, 1);
186 result
= parse_combine_subfilter(
187 filter_options
, subspecs
[sub
], errbuf
);
190 filter_options
->choice
= LOFC_COMBINE
;
193 strbuf_list_free(subspecs
);
195 list_objects_filter_release(filter_options
);
199 static int allow_unencoded(char ch
)
201 if (ch
<= ' ' || ch
== '%' || ch
== '+')
203 return !strchr(RESERVED_NON_WS
, ch
);
206 static void filter_spec_append_urlencode(
207 struct list_objects_filter_options
*filter
, const char *raw
)
209 size_t orig_len
= filter
->filter_spec
.len
;
210 strbuf_addstr_urlencode(&filter
->filter_spec
, raw
, allow_unencoded
);
211 trace_printf("Add to combine filter-spec: %s\n",
212 filter
->filter_spec
.buf
+ orig_len
);
216 * Changes filter_options into an equivalent LOFC_COMBINE filter options
217 * instance. Does not do anything if filter_options is already LOFC_COMBINE.
219 static void transform_to_combine_type(
220 struct list_objects_filter_options
*filter_options
)
222 assert(filter_options
->choice
);
223 if (filter_options
->choice
== LOFC_COMBINE
)
226 const int initial_sub_alloc
= 2;
227 struct list_objects_filter_options
*sub_array
=
228 xcalloc(initial_sub_alloc
, sizeof(*sub_array
));
229 sub_array
[0] = *filter_options
;
230 list_objects_filter_init(filter_options
);
231 filter_options
->sub
= sub_array
;
232 filter_options
->sub_alloc
= initial_sub_alloc
;
234 filter_options
->sub_nr
= 1;
235 filter_options
->choice
= LOFC_COMBINE
;
236 strbuf_addstr(&filter_options
->filter_spec
, "combine:");
237 filter_spec_append_urlencode(
239 list_objects_filter_spec(&filter_options
->sub
[0]));
241 * We don't need the filter_spec strings for subfilter specs, only the
244 strbuf_release(&filter_options
->sub
[0].filter_spec
);
247 void list_objects_filter_die_if_populated(
248 struct list_objects_filter_options
*filter_options
)
250 if (filter_options
->choice
)
251 die(_("multiple filter-specs cannot be combined"));
254 void parse_list_objects_filter(
255 struct list_objects_filter_options
*filter_options
,
258 struct strbuf errbuf
= STRBUF_INIT
;
261 if (!filter_options
->filter_spec
.buf
)
262 BUG("filter_options not properly initialized");
264 if (!filter_options
->choice
) {
265 strbuf_addstr(&filter_options
->filter_spec
, arg
);
267 parse_error
= gently_parse_list_objects_filter(
268 filter_options
, arg
, &errbuf
);
270 struct list_objects_filter_options
*sub
;
273 * Make filter_options an LOFC_COMBINE spec so we can trivially
274 * add subspecs to it.
276 transform_to_combine_type(filter_options
);
278 strbuf_addch(&filter_options
->filter_spec
, '+');
279 filter_spec_append_urlencode(filter_options
, arg
);
280 ALLOC_GROW_BY(filter_options
->sub
, filter_options
->sub_nr
, 1,
281 filter_options
->sub_alloc
);
282 sub
= &filter_options
->sub
[filter_options
->sub_nr
- 1];
284 list_objects_filter_init(sub
);
285 parse_error
= gently_parse_list_objects_filter(sub
, arg
,
289 die("%s", errbuf
.buf
);
292 int opt_parse_list_objects_filter(const struct option
*opt
,
293 const char *arg
, int unset
)
295 struct list_objects_filter_options
*filter_options
= opt
->value
;
298 list_objects_filter_set_no_filter(filter_options
);
300 parse_list_objects_filter(filter_options
, arg
);
304 const char *list_objects_filter_spec(struct list_objects_filter_options
*filter
)
306 if (!filter
->filter_spec
.len
)
307 BUG("no filter_spec available for this filter");
308 return filter
->filter_spec
.buf
;
311 const char *expand_list_objects_filter_spec(
312 struct list_objects_filter_options
*filter
)
314 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
315 strbuf_release(&filter
->filter_spec
);
316 strbuf_addf(&filter
->filter_spec
, "blob:limit=%lu",
317 filter
->blob_limit_value
);
320 return list_objects_filter_spec(filter
);
323 void list_objects_filter_release(
324 struct list_objects_filter_options
*filter_options
)
330 strbuf_release(&filter_options
->filter_spec
);
331 free(filter_options
->sparse_oid_name
);
332 for (sub
= 0; sub
< filter_options
->sub_nr
; sub
++)
333 list_objects_filter_release(&filter_options
->sub
[sub
]);
334 free(filter_options
->sub
);
335 list_objects_filter_init(filter_options
);
338 void partial_clone_register(
340 struct list_objects_filter_options
*filter_options
)
342 struct promisor_remote
*promisor_remote
;
346 /* Check if it is already registered */
347 if ((promisor_remote
= repo_promisor_remote_find(the_repository
, remote
))) {
348 if (promisor_remote
->partial_clone_filter
)
350 * Remote is already registered and a filter is already
351 * set, so we don't need to do anything here.
355 if (upgrade_repository_format(1) < 0)
356 die(_("unable to upgrade repository format to support partial clone"));
358 /* Add promisor config for the remote */
359 cfg_name
= xstrfmt("remote.%s.promisor", remote
);
360 git_config_set(cfg_name
, "true");
365 * Record the initial filter-spec in the config as
366 * the default for subsequent fetches from this remote.
368 filter_name
= xstrfmt("remote.%s.partialclonefilter", remote
);
369 /* NEEDSWORK: 'expand' result leaking??? */
370 git_config_set(filter_name
,
371 expand_list_objects_filter_spec(filter_options
));
374 /* Make sure the config info are reset */
375 repo_promisor_remote_reinit(the_repository
);
378 void partial_clone_get_default_filter_spec(
379 struct list_objects_filter_options
*filter_options
,
382 struct promisor_remote
*promisor
= repo_promisor_remote_find(the_repository
,
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
));