git
/
list-objects-filter-options.c
418 строк · 10.8 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "config.h"5#include "gettext.h"6#include "list-objects-filter-options.h"7#include "promisor-remote.h"8#include "trace.h"9#include "url.h"10#include "parse-options.h"11
12static int parse_combine_filter(13struct list_objects_filter_options *filter_options,14const char *arg,15struct strbuf *errbuf);16
17const char *list_object_filter_config_name(enum list_objects_filter_choice c)18{
19switch (c) {20case LOFC_DISABLED:21/* we have no name for "no filter at all" */22break;23case LOFC_BLOB_NONE:24return "blob:none";25case LOFC_BLOB_LIMIT:26return "blob:limit";27case LOFC_TREE_DEPTH:28return "tree";29case LOFC_SPARSE_OID:30return "sparse:oid";31case LOFC_OBJECT_TYPE:32return "object:type";33case LOFC_COMBINE:34return "combine";35case LOFC__COUNT:36/* not a real filter type; just the count of all filters */37break;38}39BUG("list_object_filter_config_name: invalid argument '%d'", c);40}
41
42int gently_parse_list_objects_filter(43struct list_objects_filter_options *filter_options,44const char *arg,45struct strbuf *errbuf)46{
47const char *v0;48
49if (!arg)50return 0;51
52if (filter_options->choice)53BUG("filter_options already populated");54
55if (!strcmp(arg, "blob:none")) {56filter_options->choice = LOFC_BLOB_NONE;57return 0;58
59} else if (skip_prefix(arg, "blob:limit=", &v0)) {60if (git_parse_ulong(v0, &filter_options->blob_limit_value)) {61filter_options->choice = LOFC_BLOB_LIMIT;62return 0;63}64
65} else if (skip_prefix(arg, "tree:", &v0)) {66if (!git_parse_ulong(v0, &filter_options->tree_exclude_depth)) {67strbuf_addstr(errbuf, _("expected 'tree:<depth>'"));68return 1;69}70filter_options->choice = LOFC_TREE_DEPTH;71return 0;72
73} else if (skip_prefix(arg, "sparse:oid=", &v0)) {74filter_options->sparse_oid_name = xstrdup(v0);75filter_options->choice = LOFC_SPARSE_OID;76return 0;77
78} else if (skip_prefix(arg, "sparse:path=", &v0)) {79if (errbuf) {80strbuf_addstr(81errbuf,82_("sparse:path filters support has been dropped"));83}84return 1;85
86} else if (skip_prefix(arg, "object:type=", &v0)) {87int type = type_from_string_gently(v0, strlen(v0), 1);88if (type < 0) {89strbuf_addf(errbuf, _("'%s' for 'object:type=<type>' is "90"not a valid object type"), v0);91return 1;92}93
94filter_options->object_type = type;95filter_options->choice = LOFC_OBJECT_TYPE;96
97return 0;98
99} else if (skip_prefix(arg, "combine:", &v0)) {100return parse_combine_filter(filter_options, v0, errbuf);101
102}103/*104* Please update _git_fetch() in git-completion.bash when you
105* add new filters
106*/
107
108strbuf_addf(errbuf, _("invalid filter-spec '%s'"), arg);109
110list_objects_filter_init(filter_options);111return 1;112}
113
114static const char *RESERVED_NON_WS = "~`!@#$^&*()[]{}\\;'\",<>?";115
116static int has_reserved_character(117struct strbuf *sub_spec, struct strbuf *errbuf)118{
119const char *c = sub_spec->buf;120while (*c) {121if (*c <= ' ' || strchr(RESERVED_NON_WS, *c)) {122strbuf_addf(123errbuf,124_("must escape char in sub-filter-spec: '%c'"),125*c);126return 1;127}128c++;129}130
131return 0;132}
133
134static int parse_combine_subfilter(135struct list_objects_filter_options *filter_options,136struct strbuf *subspec,137struct strbuf *errbuf)138{
139size_t new_index = filter_options->sub_nr;140char *decoded;141int result;142
143ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,144filter_options->sub_alloc);145list_objects_filter_init(&filter_options->sub[new_index]);146
147decoded = url_percent_decode(subspec->buf);148
149result = has_reserved_character(subspec, errbuf) ||150gently_parse_list_objects_filter(151&filter_options->sub[new_index], decoded, errbuf);152
153free(decoded);154return result;155}
156
157static int parse_combine_filter(158struct list_objects_filter_options *filter_options,159const char *arg,160struct strbuf *errbuf)161{
162struct strbuf **subspecs = strbuf_split_str(arg, '+', 0);163size_t sub;164int result = 0;165
166if (!subspecs[0]) {167strbuf_addstr(errbuf, _("expected something after combine:"));168result = 1;169goto cleanup;170}171
172for (sub = 0; subspecs[sub] && !result; sub++) {173if (subspecs[sub + 1]) {174/*175* This is not the last subspec. Remove trailing "+" so
176* we can parse it.
177*/
178size_t last = subspecs[sub]->len - 1;179assert(subspecs[sub]->buf[last] == '+');180strbuf_remove(subspecs[sub], last, 1);181}182result = parse_combine_subfilter(183filter_options, subspecs[sub], errbuf);184}185
186filter_options->choice = LOFC_COMBINE;187
188cleanup:189strbuf_list_free(subspecs);190if (result)191list_objects_filter_release(filter_options);192return result;193}
194
195static int allow_unencoded(char ch)196{
197if (ch <= ' ' || ch == '%' || ch == '+')198return 0;199return !strchr(RESERVED_NON_WS, ch);200}
201
202static void filter_spec_append_urlencode(203struct list_objects_filter_options *filter, const char *raw)204{
205size_t orig_len = filter->filter_spec.len;206strbuf_addstr_urlencode(&filter->filter_spec, raw, allow_unencoded);207trace_printf("Add to combine filter-spec: %s\n",208filter->filter_spec.buf + orig_len);209}
210
211/*
212* Changes filter_options into an equivalent LOFC_COMBINE filter options
213* instance. Does not do anything if filter_options is already LOFC_COMBINE.
214*/
215static void transform_to_combine_type(216struct list_objects_filter_options *filter_options)217{
218assert(filter_options->choice);219if (filter_options->choice == LOFC_COMBINE)220return;221{222const int initial_sub_alloc = 2;223struct list_objects_filter_options *sub_array =224xcalloc(initial_sub_alloc, sizeof(*sub_array));225sub_array[0] = *filter_options;226list_objects_filter_init(filter_options);227filter_options->sub = sub_array;228filter_options->sub_alloc = initial_sub_alloc;229}230filter_options->sub_nr = 1;231filter_options->choice = LOFC_COMBINE;232strbuf_addstr(&filter_options->filter_spec, "combine:");233filter_spec_append_urlencode(234filter_options,235list_objects_filter_spec(&filter_options->sub[0]));236/*237* We don't need the filter_spec strings for subfilter specs, only the
238* top level.
239*/
240strbuf_release(&filter_options->sub[0].filter_spec);241}
242
243void list_objects_filter_die_if_populated(244struct list_objects_filter_options *filter_options)245{
246if (filter_options->choice)247die(_("multiple filter-specs cannot be combined"));248}
249
250void parse_list_objects_filter(251struct list_objects_filter_options *filter_options,252const char *arg)253{
254struct strbuf errbuf = STRBUF_INIT;255int parse_error;256
257if (!filter_options->filter_spec.buf)258BUG("filter_options not properly initialized");259
260if (!filter_options->choice) {261strbuf_addstr(&filter_options->filter_spec, arg);262
263parse_error = gently_parse_list_objects_filter(264filter_options, arg, &errbuf);265} else {266struct list_objects_filter_options *sub;267
268/*269* Make filter_options an LOFC_COMBINE spec so we can trivially
270* add subspecs to it.
271*/
272transform_to_combine_type(filter_options);273
274strbuf_addch(&filter_options->filter_spec, '+');275filter_spec_append_urlencode(filter_options, arg);276ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,277filter_options->sub_alloc);278sub = &filter_options->sub[filter_options->sub_nr - 1];279
280list_objects_filter_init(sub);281parse_error = gently_parse_list_objects_filter(sub, arg,282&errbuf);283}284if (parse_error)285die("%s", errbuf.buf);286}
287
288int opt_parse_list_objects_filter(const struct option *opt,289const char *arg, int unset)290{
291struct list_objects_filter_options *filter_options = opt->value;292
293if (unset || !arg)294list_objects_filter_set_no_filter(filter_options);295else296parse_list_objects_filter(filter_options, arg);297return 0;298}
299
300const char *list_objects_filter_spec(struct list_objects_filter_options *filter)301{
302if (!filter->filter_spec.len)303BUG("no filter_spec available for this filter");304return filter->filter_spec.buf;305}
306
307const char *expand_list_objects_filter_spec(308struct list_objects_filter_options *filter)309{
310if (filter->choice == LOFC_BLOB_LIMIT) {311strbuf_release(&filter->filter_spec);312strbuf_addf(&filter->filter_spec, "blob:limit=%lu",313filter->blob_limit_value);314}315
316return list_objects_filter_spec(filter);317}
318
319void list_objects_filter_release(320struct list_objects_filter_options *filter_options)321{
322size_t sub;323
324if (!filter_options)325return;326strbuf_release(&filter_options->filter_spec);327free(filter_options->sparse_oid_name);328for (sub = 0; sub < filter_options->sub_nr; sub++)329list_objects_filter_release(&filter_options->sub[sub]);330free(filter_options->sub);331list_objects_filter_init(filter_options);332}
333
334void partial_clone_register(335const char *remote,336struct list_objects_filter_options *filter_options)337{
338struct promisor_remote *promisor_remote;339char *cfg_name;340char *filter_name;341
342/* Check if it is already registered */343if ((promisor_remote = repo_promisor_remote_find(the_repository, remote))) {344if (promisor_remote->partial_clone_filter)345/*346* Remote is already registered and a filter is already
347* set, so we don't need to do anything here.
348*/
349return;350} else {351if (upgrade_repository_format(1) < 0)352die(_("unable to upgrade repository format to support partial clone"));353
354/* Add promisor config for the remote */355cfg_name = xstrfmt("remote.%s.promisor", remote);356git_config_set(cfg_name, "true");357free(cfg_name);358}359
360/*361* Record the initial filter-spec in the config as
362* the default for subsequent fetches from this remote.
363*/
364filter_name = xstrfmt("remote.%s.partialclonefilter", remote);365/* NEEDSWORK: 'expand' result leaking??? */366git_config_set(filter_name,367expand_list_objects_filter_spec(filter_options));368free(filter_name);369
370/* Make sure the config info are reset */371repo_promisor_remote_reinit(the_repository);372}
373
374void partial_clone_get_default_filter_spec(375struct list_objects_filter_options *filter_options,376const char *remote)377{
378struct promisor_remote *promisor = repo_promisor_remote_find(the_repository,379remote);380struct strbuf errbuf = STRBUF_INIT;381
382/*383* Parse default value, but silently ignore it if it is invalid.
384*/
385if (!promisor || !promisor->partial_clone_filter)386return;387
388strbuf_addstr(&filter_options->filter_spec,389promisor->partial_clone_filter);390gently_parse_list_objects_filter(filter_options,391promisor->partial_clone_filter,392&errbuf);393strbuf_release(&errbuf);394}
395
396void list_objects_filter_copy(397struct list_objects_filter_options *dest,398const struct list_objects_filter_options *src)399{
400int i;401
402/* Copy everything. We will overwrite the pointers shortly. */403memcpy(dest, src, sizeof(struct list_objects_filter_options));404
405strbuf_init(&dest->filter_spec, 0);406strbuf_addbuf(&dest->filter_spec, &src->filter_spec);407dest->sparse_oid_name = xstrdup_or_null(src->sparse_oid_name);408
409ALLOC_ARRAY(dest->sub, dest->sub_alloc);410for (i = 0; i < src->sub_nr; i++)411list_objects_filter_copy(&dest->sub[i], &src->sub[i]);412}
413
414void list_objects_filter_init(struct list_objects_filter_options *filter_options)415{
416struct list_objects_filter_options blank = LIST_OBJECTS_FILTER_INIT;417memcpy(filter_options, &blank, sizeof(*filter_options));418}
419