Skip to content

Commit f1fc34c

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>". Let's make it possible to pass more information by introducing a new "promisor.sendFields" configuration variable. This 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 fields are allowed. The only fields in this set are "partialCloneFilter" and "token". The "partialCloneFilter" field specifies the filter definition used by the promisor remote, and the "token" field can provide an authentication credential for accessing it. For example, if "promisor.sendFields" is set to "partialCloneFilter", and the server has the "remote.<name>.partialCloneFilter" config variable set to a value for a remote, then that value will be passed 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 8e59b61 commit f1fc34c

File tree

4 files changed

+215
-25
lines changed

4 files changed

+215
-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 will send 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 will be 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: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -785,33 +785,59 @@ 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>
788+
promisor-remote=<pr-info>
789789
~~~~~~~~~~~~~~~~~~~~~~~~~~
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-info
797797
798-
pr-info = "name=" pr-name | "name=" pr-name "," "url=" pr-url
798+
pr-fields = field-name "=" field-value | pr-fields "," pr-fields
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+
where all the `field-name` and `field-value` in a given `pr-fields`
801+
are field names and values related to a single promisor remote.
802802
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:
803+
The server MUST advertise at least the "name" and "url" field names
804+
along with the associated field values, which are the name of a valid
805+
remote and its URL, in each `pr-fields`. The "name" and "url" fields
806+
MUST appear first in each pr-fields, in that order.
806807
807-
pr-names = pr-name | pr-names ";" pr-name
808+
After these mandatory fields, the server MAY advertise the following
809+
optional fields in any order:
810+
811+
- "partialCloneFilter": The filter specification used by the remote.
812+
Clients can use this to determine if the remote's filtering strategy
813+
is compatible with their needs (e.g., checking if both use "blob:none").
814+
It corresponds to the "remote.<name>.partialCloneFilter" config setting.
815+
816+
- "token": An authentication token that clients can use when
817+
connecting to the remote. It corresponds to the "remote.<name>.token"
818+
config setting.
819+
820+
No other fields are defined by the protocol at this time. Clients MUST
821+
ignore fields they don't recognize to allow for future protocol
822+
extensions.
823+
824+
For now, the client can only use information transmitted through these
825+
fields to decide if it accepts the advertised promisor remote. In the
826+
future that information might be used for other purposes though.
827+
828+
Field values MUST be urlencoded.
829+
830+
If the client decides to use one or more promisor remotes the server
831+
advertised, it can reply with "promisor-remote=<pr-names>" where
832+
<pr-names> should be of the form:
833+
834+
pr-names = pr-name | pr-names ";" pr-names
808835
809836
where `pr-name` is the urlencoded name of a promisor remote the server
810837
advertised and the client accepts.
811838
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`.
839+
Note that, everywhere in this document, the ';' and ',' characters
840+
MUST be encoded if they appear in `pr-name` or `field-value`.
815841
816842
If the server doesn't know any promisor remote that could be good for
817843
a client to use, or prefers a client not to use any promisor remote it
@@ -822,9 +848,10 @@ In this case, or if the client doesn't want to use any promisor remote
822848
the server advertised, the client shouldn't advertise the
823849
"promisor-remote" capability at all in its reply.
824850
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
851+
On the server side, the "promisor.advertise" and "promisor.sendFields"
852+
configuration options can be used to control what it advertises. On
853+
the client side, the "promisor.acceptFromServer" configuration option
854+
can be used to control what it accepts. See the documentation of these
828855
configuration options for more information.
829856
830857
Note that in the future it would be nice if the "promisor-remote"

promisor-remote.c

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,74 @@ 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+
string_list_split_in_place(fields_list, fields, ", ", -1);
364+
string_list_remove_empty_items(fields_list, 0);
365+
filter_string_list(fields_list, 0, is_valid_field, (void *)config_key);
366+
}
367+
368+
return fields;
369+
}
370+
371+
static struct string_list *fields_sent(void)
372+
{
373+
static struct string_list fields_list = STRING_LIST_INIT_NODUP;
374+
static int initialized = 0;
375+
376+
if (!initialized) {
377+
fields_list.cmp = strcasecmp;
378+
fields_from_config(&fields_list, "promisor.sendFields");
379+
initialized = 1;
380+
}
381+
382+
return &fields_list;
383+
}
384+
317385
/*
318386
* Struct for promisor remotes involved in the "promisor-remote"
319387
* protocol capability.
@@ -326,6 +394,8 @@ static int allow_unsanitized(char ch)
326394
struct promisor_info {
327395
const char *name;
328396
const char *url;
397+
const char *filter;
398+
const char *token;
329399
};
330400

331401
static void promisor_info_list_clear(struct string_list *list)
@@ -334,15 +404,47 @@ static void promisor_info_list_clear(struct string_list *list)
334404
struct promisor_info *p = list->items[i].util;
335405
free((char *)p->name);
336406
free((char *)p->url);
407+
free((char *)p->filter);
408+
free((char *)p->token);
337409
}
338410
string_list_clear(list, 1);
339411
}
340412

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

@@ -360,6 +462,9 @@ static void promisor_config_info_list(struct repository *repo, struct string_lis
360462
new_info->name = xstrdup(r->name);
361463
new_info->url = xstrdup(url);
362464

465+
if (field_names)
466+
set_fields(new_info, field_names);
467+
363468
item = string_list_append(list, new_info->name);
364469
item->util = new_info;
365470
}
@@ -380,7 +485,7 @@ char *promisor_remote_info(struct repository *repo)
380485
if (!advertise_promisors)
381486
return NULL;
382487

383-
promisor_config_info_list(repo, &config_info);
488+
promisor_config_info_list(repo, &config_info, fields_sent());
384489

385490
if (!config_info.nr)
386491
return NULL;
@@ -395,6 +500,15 @@ char *promisor_remote_info(struct repository *repo)
395500
strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized);
396501
strbuf_addstr(&sb, ",url=");
397502
strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized);
503+
504+
if (p->filter) {
505+
strbuf_addf(&sb, ",%s=", promisor_field_filter);
506+
strbuf_addstr_urlencode(&sb, p->filter, allow_unsanitized);
507+
}
508+
if (p->token) {
509+
strbuf_addf(&sb, ",%s=", promisor_field_token);
510+
strbuf_addstr_urlencode(&sb, p->token, allow_unsanitized);
511+
}
398512
}
399513

400514
promisor_info_list_clear(&config_info);
@@ -479,7 +593,7 @@ static void filter_promisor_remote(struct repository *repo,
479593
return;
480594

481595
if (accept != ACCEPT_ALL) {
482-
promisor_config_info_list(repo, &config_info);
596+
promisor_config_info_list(repo, &config_info, NULL);
483597
string_list_sort(&config_info);
484598
}
485599

@@ -498,13 +612,9 @@ static void filter_promisor_remote(struct repository *repo,
498612
elems = strbuf_split(remotes[i], ',');
499613

500614
for (size_t j = 0; elems[j]; j++) {
501-
int res;
502615
strbuf_strip_suffix(elems[j], ",");
503-
res = skip_prefix(elems[j]->buf, "name=", &remote_name) ||
616+
if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
504617
skip_prefix(elems[j]->buf, "url=", &remote_url);
505-
if (!res)
506-
warning(_("unknown element '%s' from remote info"),
507-
elems[j]->buf);
508618
}
509619

510620
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)