promisor-remote: allow a server to advertise more fields
For now the "promisor-remote" protocol capability can only pass "name"
and "url" information from a server to a client in the form
"name=<remote_name>,url=<remote_url>".
To allow clients to make more informed decisions about which promisor
remotes they accept, let's make it possible to pass more information
by introducing a new "promisor.sendFields" configuration variable.
On the server side, information about a remote `foo` is stored in
configuration variables named `remote.foo.<variable-name>`. To make
it clearer and simpler, we use `field` and `field name` like this:
* `field name` refers to the <variable-name> part of such a
configuration variable, and
* `field` refers to both the `field name` and the value of such a
configuration variable.
The "promisor.sendFields" configuration variable should contain a
comma or space separated list of field names that will be looked up
in the configuration of the remote on the server to find the values
that will be passed to the client.
Only a set of predefined field names are allowed. The only field
names in this set are "partialCloneFilter" and "token". The
"partialCloneFilter" field name specifies the filter definition used
by the promisor remote, and the "token" field name can provide an
authentication credential for accessing it.
For example, if "promisor.sendFields" is set to "partialCloneFilter",
and the server has the "remote.foo.partialCloneFilter" config
variable set to a value, then that value will be passed in the
"partialCloneFilter" field in the form "partialCloneFilter=<value>"
after the "name" and "url" fields.
A following commit will allow the client to use the information to
decide if it accepts the remote or not. For now the client doesn't do
anything with the additional information it receives.
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
57af9cc2e6
commit
4bf7ae3123
@@ -314,6 +314,75 @@ static int allow_unsanitized(char ch)
|
||||
return ch > 32 && ch < 127;
|
||||
}
|
||||
|
||||
static const char promisor_field_filter[] = "partialCloneFilter";
|
||||
static const char promisor_field_token[] = "token";
|
||||
|
||||
/*
|
||||
* List of optional field names that can be used in the
|
||||
* "promisor-remote" protocol capability (others must be
|
||||
* ignored). Each field should correspond to a configurable property
|
||||
* of a remote that can be relevant for the client.
|
||||
*/
|
||||
static const char *known_fields[] = {
|
||||
promisor_field_filter, /* Filter used for partial clone */
|
||||
promisor_field_token, /* Authentication token for the remote */
|
||||
NULL
|
||||
};
|
||||
|
||||
/*
|
||||
* Check if 'field' is in the list of the known field names for the
|
||||
* "promisor-remote" protocol capability.
|
||||
*/
|
||||
static int is_known_field(const char *field)
|
||||
{
|
||||
const char **p;
|
||||
|
||||
for (p = known_fields; *p; p++)
|
||||
if (!strcasecmp(*p, field))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_valid_field(struct string_list_item *item, void *cb_data)
|
||||
{
|
||||
const char *field = item->string;
|
||||
const char *config_key = (const char *)cb_data;
|
||||
|
||||
if (!is_known_field(field)) {
|
||||
warning(_("unsupported field '%s' in '%s' config"), field, config_key);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char *fields_from_config(struct string_list *fields_list, const char *config_key)
|
||||
{
|
||||
char *fields = NULL;
|
||||
|
||||
if (!repo_config_get_string(the_repository, config_key, &fields) && *fields) {
|
||||
string_list_split_in_place_f(fields_list, fields, ",", -1,
|
||||
STRING_LIST_SPLIT_TRIM |
|
||||
STRING_LIST_SPLIT_NONEMPTY);
|
||||
filter_string_list(fields_list, 0, is_valid_field, (void *)config_key);
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
static struct string_list *fields_sent(void)
|
||||
{
|
||||
static struct string_list fields_list = STRING_LIST_INIT_NODUP;
|
||||
static int initialized;
|
||||
|
||||
if (!initialized) {
|
||||
fields_list.cmp = strcasecmp;
|
||||
fields_from_config(&fields_list, "promisor.sendFields");
|
||||
initialized = 1;
|
||||
}
|
||||
|
||||
return &fields_list;
|
||||
}
|
||||
|
||||
/*
|
||||
* Struct for promisor remotes involved in the "promisor-remote"
|
||||
* protocol capability.
|
||||
@@ -326,6 +395,8 @@ static int allow_unsanitized(char ch)
|
||||
struct promisor_info {
|
||||
const char *name;
|
||||
const char *url;
|
||||
const char *filter;
|
||||
const char *token;
|
||||
};
|
||||
|
||||
static void promisor_info_list_clear(struct string_list *list)
|
||||
@@ -334,15 +405,47 @@ static void promisor_info_list_clear(struct string_list *list)
|
||||
struct promisor_info *p = list->items[i].util;
|
||||
free((char *)p->name);
|
||||
free((char *)p->url);
|
||||
free((char *)p->filter);
|
||||
free((char *)p->token);
|
||||
}
|
||||
string_list_clear(list, 1);
|
||||
}
|
||||
|
||||
static void set_one_field(struct promisor_info *p,
|
||||
const char *field, const char *value)
|
||||
{
|
||||
if (!strcasecmp(field, promisor_field_filter))
|
||||
p->filter = xstrdup(value);
|
||||
else if (!strcasecmp(field, promisor_field_token))
|
||||
p->token = xstrdup(value);
|
||||
else
|
||||
BUG("invalid field '%s'", field);
|
||||
}
|
||||
|
||||
static void set_fields(struct promisor_info *p,
|
||||
struct string_list *field_names)
|
||||
{
|
||||
struct string_list_item *item;
|
||||
|
||||
for_each_string_list_item(item, field_names) {
|
||||
char *key = xstrfmt("remote.%s.%s", p->name, item->string);
|
||||
const char *val;
|
||||
if (!repo_config_get_string_tmp(the_repository, key, &val) && *val)
|
||||
set_one_field(p, item->string, val);
|
||||
free(key);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Populate 'list' with promisor remote information from the config.
|
||||
* The 'util' pointer of each list item will hold a 'struct promisor_info'.
|
||||
* The 'util' pointer of each list item will hold a 'struct
|
||||
* promisor_info'. Except "name" and "url", only members of that
|
||||
* struct specified by the 'field_names' list are set (using values
|
||||
* from the configuration).
|
||||
*/
|
||||
static void promisor_config_info_list(struct repository *repo, struct string_list *list)
|
||||
static void promisor_config_info_list(struct repository *repo,
|
||||
struct string_list *list,
|
||||
struct string_list *field_names)
|
||||
{
|
||||
struct promisor_remote *r;
|
||||
|
||||
@@ -360,6 +463,9 @@ static void promisor_config_info_list(struct repository *repo, struct string_lis
|
||||
new_info->name = xstrdup(r->name);
|
||||
new_info->url = xstrdup(url);
|
||||
|
||||
if (field_names)
|
||||
set_fields(new_info, field_names);
|
||||
|
||||
item = string_list_append(list, new_info->name);
|
||||
item->util = new_info;
|
||||
}
|
||||
@@ -380,7 +486,7 @@ char *promisor_remote_info(struct repository *repo)
|
||||
if (!advertise_promisors)
|
||||
return NULL;
|
||||
|
||||
promisor_config_info_list(repo, &config_info);
|
||||
promisor_config_info_list(repo, &config_info, fields_sent());
|
||||
|
||||
if (!config_info.nr)
|
||||
return NULL;
|
||||
@@ -395,6 +501,15 @@ char *promisor_remote_info(struct repository *repo)
|
||||
strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized);
|
||||
strbuf_addstr(&sb, ",url=");
|
||||
strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized);
|
||||
|
||||
if (p->filter) {
|
||||
strbuf_addf(&sb, ",%s=", promisor_field_filter);
|
||||
strbuf_addstr_urlencode(&sb, p->filter, allow_unsanitized);
|
||||
}
|
||||
if (p->token) {
|
||||
strbuf_addf(&sb, ",%s=", promisor_field_token);
|
||||
strbuf_addstr_urlencode(&sb, p->token, allow_unsanitized);
|
||||
}
|
||||
}
|
||||
|
||||
promisor_info_list_clear(&config_info);
|
||||
@@ -475,7 +590,7 @@ static void filter_promisor_remote(struct repository *repo,
|
||||
return;
|
||||
|
||||
if (accept != ACCEPT_ALL) {
|
||||
promisor_config_info_list(repo, &config_info);
|
||||
promisor_config_info_list(repo, &config_info, NULL);
|
||||
string_list_sort(&config_info);
|
||||
}
|
||||
|
||||
@@ -494,13 +609,9 @@ static void filter_promisor_remote(struct repository *repo,
|
||||
elems = strbuf_split(remotes[i], ',');
|
||||
|
||||
for (size_t j = 0; elems[j]; j++) {
|
||||
int res;
|
||||
strbuf_strip_suffix(elems[j], ",");
|
||||
res = skip_prefix(elems[j]->buf, "name=", &remote_name) ||
|
||||
if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
|
||||
skip_prefix(elems[j]->buf, "url=", &remote_url);
|
||||
if (!res)
|
||||
warning(_("unknown element '%s' from remote info"),
|
||||
elems[j]->buf);
|
||||
}
|
||||
|
||||
if (remote_name)
|
||||
|
||||
Reference in New Issue
Block a user