Skip to content

Commit 8c31bbc

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 fields 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 a2cacd7 commit 8c31bbc

File tree

4 files changed

+212
-25
lines changed

4 files changed

+212
-25
lines changed

Documentation/config/promisor.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ 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+
fields that a server will send while advertising its promisor
15+
remotes using the "promisor-remote" capability, see
16+
linkgit:gitprotocol-v2[5]. Currently, only the
17+
"partialCloneFilter" and "token" fields are supported. The
18+
"partialCloneFilter" field contains the partial clone filter
19+
used for the remote, and the "token" field contains an
20+
authentication token for the remote.
21+
+
22+
When a field is part of this list and a corresponding
23+
"remote.foo.<field>" config variable is set on the server to a
24+
non-empty value, then the field and its value will be sent when
25+
advertising the promisor remote "foo". This list has no effect unless
26+
the "promisor.advertise" config variable is set to "true", and the
27+
"name" and "url" fields are always advertised regardless of this
28+
setting.
29+
1230
promisor.acceptFromServer::
1331
If set to "all", a client will accept all the promisor remotes
1432
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,73 @@ 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+
filter_string_list(fields_list, 0, is_valid_field, (void *)config_key);
365+
}
366+
367+
return fields;
368+
}
369+
370+
static struct string_list *fields_sent(void)
371+
{
372+
static struct string_list fields_list = STRING_LIST_INIT_NODUP;
373+
static int initialized = 0;
374+
375+
if (!initialized) {
376+
fields_list.cmp = strcasecmp;
377+
fields_from_config(&fields_list, "promisor.sendFields");
378+
initialized = 1;
379+
}
380+
381+
return &fields_list;
382+
}
383+
317384
/*
318385
* Linked list for promisor remotes involved in the "promisor-remote"
319386
* protocol capability.
@@ -326,6 +393,8 @@ struct promisor_info {
326393
struct promisor_info *next;
327394
const char *name;
328395
const char *url;
396+
const char *filter;
397+
const char *token;
329398
};
330399

331400
static void promisor_info_list_free(struct promisor_info *p)
@@ -336,12 +405,45 @@ static void promisor_info_list_free(struct promisor_info *p)
336405
next = p->next;
337406
free((char *)p->name);
338407
free((char *)p->url);
408+
free((char *)p->filter);
409+
free((char *)p->token);
339410
free(p);
340411
}
341412
}
342413

343-
/* Prepare a 'struct promisor_info' linked list with config information. */
344-
static struct promisor_info *promisor_config_info_list(struct repository *repo)
414+
static void set_one_field(struct promisor_info *p,
415+
const char *field, const char *value)
416+
{
417+
if (!strcasecmp(field, promisor_field_filter))
418+
p->filter = xstrdup(value);
419+
else if (!strcasecmp(field, promisor_field_token))
420+
p->token = xstrdup(value);
421+
else
422+
BUG("Invalid field '%s'", field);
423+
}
424+
425+
static void set_fields(struct promisor_info *p,
426+
struct string_list *field_names)
427+
{
428+
struct string_list_item *item;
429+
430+
for_each_string_list_item(item, field_names) {
431+
char *key = xstrfmt("remote.%s.%s", p->name, item->string);
432+
const char *val;
433+
if (!git_config_get_string_tmp(key, &val) && *val)
434+
set_one_field(p, item->string, val);
435+
free(key);
436+
}
437+
}
438+
439+
/*
440+
* Prepare a 'struct promisor_info' linked list of promisor remotes
441+
* with config information. Only members of that struct specified by
442+
* the 'field_names' linked list are set (using values from the
443+
* configuration).
444+
*/
445+
static struct promisor_info *promisor_config_info_list(struct repository *repo,
446+
struct string_list *field_names)
345447
{
346448
struct promisor_info *infos = NULL;
347449
struct promisor_info **last_info = &infos;
@@ -360,6 +462,9 @@ static struct promisor_info *promisor_config_info_list(struct repository *repo)
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
*last_info = new_info;
364469
last_info = &new_info->next;
365470
}
@@ -382,7 +487,7 @@ char *promisor_remote_info(struct repository *repo)
382487
if (!advertise_promisors)
383488
return NULL;
384489

385-
config_info = promisor_config_info_list(repo);
490+
config_info = promisor_config_info_list(repo, fields_sent());
386491

387492
if (!config_info)
388493
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_free(config_info);
@@ -489,7 +603,7 @@ static void filter_promisor_remote(struct repository *repo,
489603
return;
490604

491605
if (accept != ACCEPT_ALL)
492-
config_info = promisor_config_info_list(repo);
606+
config_info = promisor_config_info_list(repo, NULL);
493607

494608
/* Parse remote info received */
495609

@@ -506,13 +620,9 @@ static void filter_promisor_remote(struct repository *repo,
506620
elems = strbuf_split(remotes[i], ',');
507621

508622
for (size_t j = 0; elems[j]; j++) {
509-
int res;
510623
strbuf_strip_suffix(elems[j], ",");
511-
res = skip_prefix(elems[j]->buf, "name=", &remote_name) ||
624+
if (!skip_prefix(elems[j]->buf, "name=", &remote_name))
512625
skip_prefix(elems[j]->buf, "url=", &remote_url);
513-
if (!res)
514-
warning(_("unknown element '%s' from remote info"),
515-
elems[j]->buf);
516626
}
517627

518628
if (remote_name)

t/t5710-promisor-remote-capability.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,38 @@ 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+
git -C server config promisor.sendFields "partialCloneFilter, token" &&
308+
test_when_finished "git -C server config unset promisor.sendFields" &&
309+
test_when_finished "rm trace" &&
310+
311+
# Clone from server to create a client
312+
GIT_TRACE_PACKET="$(pwd)/trace" GIT_NO_LAZY_FETCH=0 git clone \
313+
-c remote.lop.promisor=true \
314+
-c remote.lop.fetch="+refs/heads/*:refs/remotes/lop/*" \
315+
-c remote.lop.url="file://$(pwd)/lop" \
316+
-c promisor.acceptfromserver=All \
317+
--no-local --filter="blob:limit=5k" server client &&
318+
319+
# Check that fields are properly transmitted
320+
ENCODED_URL=$(echo "file://$(pwd)/lop" | sed -e "s/ /%20/g") &&
321+
PR1="name=lop,url=$ENCODED_URL,partialCloneFilter=blob:none" &&
322+
PR2="name=otherLop,url=https://invalid.invalid,partialCloneFilter=blob:limit=10k,token=fooBar" &&
323+
test_grep "clone< promisor-remote=$PR1;$PR2" trace &&
324+
test_grep "clone> promisor-remote=lop;otherLop" trace &&
325+
326+
# Check that the largest object is still missing on the server
327+
check_missing_objects server 1 "$oid"
328+
'
329+
298330
test_expect_success "clone with promisor.advertise set to 'true' but don't delete the client" '
299331
git -C server config promisor.advertise true &&
300332

0 commit comments

Comments
 (0)