Skip to content

Commit 0bece2a

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f79a515 commit 0bece2a

File tree

4 files changed

+226
-25
lines changed

4 files changed

+226
-25
lines changed

Documentation/config/promisor.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ promisor.advertise::
99
"false", which means the "promisor-remote" capability is not
1010
advertised.
1111

12+
promisor.sendFields::
13+
A comma or space separated list of additional remote related
14+
field names. A server sends these field names and the
15+
associated field values from its configuration when
16+
advertising its promisor remotes using the "promisor-remote"
17+
capability, see linkgit:gitprotocol-v2[5]. Currently, only the
18+
"partialCloneFilter" and "token" field names are supported.
19+
+
20+
`partialCloneFilter`:: contains the partial clone filter
21+
used for the remote.
22+
+
23+
`token`:: contains an authentication token for the remote.
24+
+
25+
When a field name is part of this list and a corresponding
26+
"remote.foo.<field-name>" config variable is set on the server to a
27+
non-empty value, then the field name and value are sent when
28+
advertising the promisor remote "foo".
29+
+
30+
This list has no effect unless the "promisor.advertise" config
31+
variable is set to "true", and the "name" and "url" fields are always
32+
advertised regardless of this setting.
33+
1234
promisor.acceptFromServer::
1335
If set to "all", a client will accept all the promisor remotes
1436
a server might advertise using the "promisor-remote"

Documentation/gitprotocol-v2.adoc

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -785,33 +785,64 @@ retrieving the header from a bundle at the indicated URI, and thus
785785
save themselves and the server(s) the request(s) needed to inspect the
786786
headers of that bundle or bundles.
787787
788-
promisor-remote=<pr-infos>
789-
~~~~~~~~~~~~~~~~~~~~~~~~~~
788+
promisor-remote=<pr-info>
789+
~~~~~~~~~~~~~~~~~~~~~~~~~
790790
791791
The server may advertise some promisor remotes it is using or knows
792792
about to a client which may want to use them as its promisor remotes,
793-
instead of this repository. In this case <pr-infos> should be of the
793+
instead of this repository. In this case <pr-info> should be of the
794794
form:
795795
796-
pr-infos = pr-info | pr-infos ";" pr-info
796+
pr-info = pr-fields | pr-info ";" pr-fields
797797
798-
pr-info = "name=" pr-name | "name=" pr-name "," "url=" pr-url
798+
pr-fields = pr-field | pr-fields "," pr-field
799799
800-
where `pr-name` is the urlencoded name of a promisor remote, and
801-
`pr-url` the urlencoded URL of that promisor remote.
800+
pr-field = field-name "=" field-value
802801
803-
In this case, if the client decides to use one or more promisor
804-
remotes the server advertised, it can reply with
805-
"promisor-remote=<pr-names>" where <pr-names> should be of the form:
802+
where all the `field-name` and `field-value` in a given `pr-fields`
803+
are field names and values related to a single promisor remote. A
804+
given `field-name` MUST NOT appear more than once in given
805+
`pr-fields`.
806+
807+
The server MUST advertise at least the "name" and "url" field names
808+
along with the associated field values, which are the name of a valid
809+
remote and its URL, in each `pr-fields`. The "name" and "url" fields
810+
MUST appear first in each pr-fields, in that order.
811+
812+
After these mandatory fields, the server MAY advertise the following
813+
optional fields in any order:
814+
815+
`partialCloneFilter`:: The filter specification used by the remote.
816+
Clients can use this to determine if the remote's filtering strategy
817+
is compatible with their needs (e.g., checking if both use "blob:none").
818+
It corresponds to the "remote.<name>.partialCloneFilter" config setting.
819+
820+
`token`:: An authentication token that clients can use when
821+
connecting to the remote. It corresponds to the "remote.<name>.token"
822+
config setting.
823+
824+
No other fields are defined by the protocol at this time. Field names
825+
are case-sensitive and MUST be transmitted exactly as specified
826+
above. Clients MUST ignore fields they don't recognize to allow for
827+
future protocol extensions.
828+
829+
For now, the client can only use information transmitted through these
830+
fields to decide if it accepts the advertised promisor remote. In the
831+
future that information might be used for other purposes though.
832+
833+
Field values MUST be urlencoded.
834+
835+
If the client decides to use one or more promisor remotes the server
836+
advertised, it can reply with "promisor-remote=<pr-names>" where
837+
<pr-names> should be of the form:
806838
807839
pr-names = pr-name | pr-names ";" pr-name
808840
809841
where `pr-name` is the urlencoded name of a promisor remote the server
810842
advertised and the client accepts.
811843
812-
Note that, everywhere in this document, `pr-name` MUST be a valid
813-
remote name, and the ';' and ',' characters MUST be encoded if they
814-
appear in `pr-name` or `pr-url`.
844+
Note that, everywhere in this document, the ';' and ',' characters
845+
MUST be encoded if they appear in `pr-name` or `field-value`.
815846
816847
If the server doesn't know any promisor remote that could be good for
817848
a client to use, or prefers a client not to use any promisor remote it
@@ -822,9 +853,10 @@ In this case, or if the client doesn't want to use any promisor remote
822853
the server advertised, the client shouldn't advertise the
823854
"promisor-remote" capability at all in its reply.
824855
825-
The "promisor.advertise" and "promisor.acceptFromServer" configuration
826-
options can be used on the server and client side to control what they
827-
advertise or accept respectively. See the documentation of these
856+
On the server side, the "promisor.advertise" and "promisor.sendFields"
857+
configuration options can be used to control what it advertises. On
858+
the client side, the "promisor.acceptFromServer" configuration option
859+
can be used to control what it accepts. See the documentation of these
828860
configuration options for more information.
829861
830862
Note that in the future it would be nice if the "promisor-remote"

promisor-remote.c

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,80 @@ static int allow_unsanitized(char ch)
314314
return ch > 32 && ch < 127;
315315
}
316316

317+
static const char promisor_field_filter[] = "partialCloneFilter";
318+
static const char promisor_field_token[] = "token";
319+
320+
/*
321+
* List of optional field names that can be used in the
322+
* "promisor-remote" protocol capability (others must be
323+
* ignored). Each field should correspond to a configurable property
324+
* of a remote that can be relevant for the client.
325+
*/
326+
static const char *known_fields[] = {
327+
promisor_field_filter, /* Filter used for partial clone */
328+
promisor_field_token, /* Authentication token for the remote */
329+
NULL
330+
};
331+
332+
/*
333+
* Check if 'field' is in the list of the known field names for the
334+
* "promisor-remote" protocol capability.
335+
*/
336+
static int is_known_field(const char *field)
337+
{
338+
const char **p;
339+
340+
for (p = known_fields; *p; p++)
341+
if (!strcasecmp(*p, field))
342+
return 1;
343+
return 0;
344+
}
345+
346+
static int is_valid_field(struct string_list_item *item, void *cb_data)
347+
{
348+
const char *field = item->string;
349+
const char *config_key = (const char *)cb_data;
350+
351+
if (!is_known_field(field)) {
352+
warning(_("unsupported field '%s' in '%s' config"), field, config_key);
353+
return 0;
354+
}
355+
return 1;
356+
}
357+
358+
static char *fields_from_config(struct string_list *fields_list, const char *config_key)
359+
{
360+
char *fields = NULL;
361+
362+
if (!git_config_get_string(config_key, &fields) && *fields) {
363+
/* Split on any comma or space character */
364+
string_list_split_in_place(fields_list, fields, ", ", -1);
365+
/*
366+
* Remove empty items that might result from trailing
367+
* commas, or from items being separated by both
368+
* commas and spaces.
369+
*/
370+
string_list_remove_empty_items(fields_list, 0);
371+
filter_string_list(fields_list, 0, is_valid_field, (void *)config_key);
372+
}
373+
374+
return fields;
375+
}
376+
377+
static struct string_list *fields_sent(void)
378+
{
379+
static struct string_list fields_list = STRING_LIST_INIT_NODUP;
380+
static int initialized;
381+
382+
if (!initialized) {
383+
fields_list.cmp = strcasecmp;
384+
fields_from_config(&fields_list, "promisor.sendFields");
385+
initialized = 1;
386+
}
387+
388+
return &fields_list;
389+
}
390+
317391
/*
318392
* Struct for promisor remotes involved in the "promisor-remote"
319393
* protocol capability.
@@ -326,6 +400,8 @@ static int allow_unsanitized(char ch)
326400
struct promisor_info {
327401
const char *name;
328402
const char *url;
403+
const char *filter;
404+
const char *token;
329405
};
330406

331407
static void promisor_info_list_clear(struct string_list *list)
@@ -334,15 +410,47 @@ static void promisor_info_list_clear(struct string_list *list)
334410
struct promisor_info *p = list->items[i].util;
335411
free((char *)p->name);
336412
free((char *)p->url);
413+
free((char *)p->filter);
414+
free((char *)p->token);
337415
}
338416
string_list_clear(list, 1);
339417
}
340418

419+
static void set_one_field(struct promisor_info *p,
420+
const char *field, const char *value)
421+
{
422+
if (!strcasecmp(field, promisor_field_filter))
423+
p->filter = xstrdup(value);
424+
else if (!strcasecmp(field, promisor_field_token))
425+
p->token = xstrdup(value);
426+
else
427+
BUG("invalid field '%s'", field);
428+
}
429+
430+
static void set_fields(struct promisor_info *p,
431+
struct string_list *field_names)
432+
{
433+
struct string_list_item *item;
434+
435+
for_each_string_list_item(item, field_names) {
436+
char *key = xstrfmt("remote.%s.%s", p->name, item->string);
437+
const char *val;
438+
if (!git_config_get_string_tmp(key, &val) && *val)
439+
set_one_field(p, item->string, val);
440+
free(key);
441+
}
442+
}
443+
341444
/*
342445
* Populate 'list' with promisor remote information from the config.
343-
* The 'util' pointer of each list item will hold a 'struct promisor_info'.
446+
* The 'util' pointer of each list item will hold a 'struct
447+
* promisor_info'. Except "name" and "url", only members of that
448+
* struct specified by the 'field_names' list are set (using values
449+
* from the configuration).
344450
*/
345-
static void promisor_config_info_list(struct repository *repo, struct string_list *list)
451+
static void promisor_config_info_list(struct repository *repo,
452+
struct string_list *list,
453+
struct string_list *field_names)
346454
{
347455
struct promisor_remote *r;
348456

@@ -360,6 +468,9 @@ static void promisor_config_info_list(struct repository *repo, struct string_lis
360468
new_info->name = xstrdup(r->name);
361469
new_info->url = xstrdup(url);
362470

471+
if (field_names)
472+
set_fields(new_info, field_names);
473+
363474
item = string_list_append(list, new_info->name);
364475
item->util = new_info;
365476
}
@@ -380,7 +491,7 @@ char *promisor_remote_info(struct repository *repo)
380491
if (!advertise_promisors)
381492
return NULL;
382493

383-
promisor_config_info_list(repo, &config_info);
494+
promisor_config_info_list(repo, &config_info, fields_sent());
384495

385496
if (!config_info.nr)
386497
return NULL;
@@ -395,6 +506,15 @@ char *promisor_remote_info(struct repository *repo)
395506
strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized);
396507
strbuf_addstr(&sb, ",url=");
397508
strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized);
509+
510+
if (p->filter) {
511+
strbuf_addf(&sb, ",%s=", promisor_field_filter);
512+
strbuf_addstr_urlencode(&sb, p->filter, allow_unsanitized);
513+
}
514+
if (p->token) {
515+
strbuf_addf(&sb, ",%s=", promisor_field_token);
516+
strbuf_addstr_urlencode(&sb, p->token, allow_unsanitized);
517+
}
398518
}
399519

400520
promisor_info_list_clear(&config_info);
@@ -475,7 +595,7 @@ static void filter_promisor_remote(struct repository *repo,
475595
return;
476596

477597
if (accept != ACCEPT_ALL) {
478-
promisor_config_info_list(repo, &config_info);
598+
promisor_config_info_list(repo, &config_info, NULL);
479599
string_list_sort(&config_info);
480600
}
481601

@@ -494,13 +614,9 @@ static void filter_promisor_remote(struct repository *repo,
494614
elems = strbuf_split(remotes[i], ',');
495615

496616
for (size_t j = 0; elems[j]; j++) {
497-
int res;
498617
strbuf_strip_suffix(elems[j], ",");
499-
res = skip_prefix(elems[j]->buf, "name=", &remote_name) ||
618+
if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
500619
skip_prefix(elems[j]->buf, "url=", &remote_url);
501-
if (!res)
502-
warning(_("unknown element '%s' from remote info"),
503-
elems[j]->buf);
504620
}
505621

506622
if (remote_name)

t/t5710-promisor-remote-capability.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,37 @@ test_expect_success "clone with 'KnownUrl' and empty url, so not advertised" '
295295
check_missing_objects server 1 "$oid"
296296
'
297297

298+
test_expect_success "clone with promisor.sendFields" '
299+
git -C server config promisor.advertise true &&
300+
test_when_finished "rm -rf client" &&
301+
302+
git -C server remote add otherLop "https://invalid.invalid" &&
303+
git -C server config remote.otherLop.token "fooBar" &&
304+
git -C server config remote.otherLop.stuff "baz" &&
305+
git -C server config remote.otherLop.partialCloneFilter "blob:limit=10k" &&
306+
test_when_finished "git -C server remote remove otherLop" &&
307+
test_config -C server promisor.sendFields "partialCloneFilter, token" &&
308+
test_when_finished "rm trace" &&
309+
310+
# Clone from server to create a client
311+
GIT_TRACE_PACKET="$(pwd)/trace" GIT_NO_LAZY_FETCH=0 git clone \
312+
-c remote.lop.promisor=true \
313+
-c remote.lop.fetch="+refs/heads/*:refs/remotes/lop/*" \
314+
-c remote.lop.url="file://$(pwd)/lop" \
315+
-c promisor.acceptfromserver=All \
316+
--no-local --filter="blob:limit=5k" server client &&
317+
318+
# Check that fields are properly transmitted
319+
ENCODED_URL=$(echo "file://$(pwd)/lop" | sed -e "s/ /%20/g") &&
320+
PR1="name=lop,url=$ENCODED_URL,partialCloneFilter=blob:none" &&
321+
PR2="name=otherLop,url=https://invalid.invalid,partialCloneFilter=blob:limit=10k,token=fooBar" &&
322+
test_grep "clone< promisor-remote=$PR1;$PR2" trace &&
323+
test_grep "clone> promisor-remote=lop;otherLop" trace &&
324+
325+
# Check that the largest object is still missing on the server
326+
check_missing_objects server 1 "$oid"
327+
'
328+
298329
test_expect_success "clone with promisor.advertise set to 'true' but don't delete the client" '
299330
git -C server config promisor.advertise true &&
300331

0 commit comments

Comments
 (0)