Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "odb.h"
7 #include "promisor-remote.h"
8 #include "config.h"
9 #include "trace2.h"
10 #include "transport.h"
11 #include "strvec.h"
12 #include "packfile.h"
13 #include "environment.h"
14 #include "url.h"
15 #include "version.h"
16
17 struct promisor_remote_config {
18 struct promisor_remote *promisors;
19 struct promisor_remote **promisors_tail;
20 };
21
22 static int fetch_objects(struct repository *repo,
23 const char *remote_name,
24 const struct object_id *oids,
25 int oid_nr)
26 {
27 struct child_process child = CHILD_PROCESS_INIT;
28 int i;
29 FILE *child_in;
30 int quiet;
31
32 if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0)) {
33 static int warning_shown;
34 if (!warning_shown) {
35 warning_shown = 1;
36 warning(_("lazy fetching disabled; some objects may not be available"));
37 }
38 return -1;
39 }
40
41 child.git_cmd = 1;
42 child.in = -1;
43 if (repo != the_repository)
44 prepare_other_repo_env(&child.env, repo->gitdir);
45 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
46 "fetch", remote_name, "--no-tags",
47 "--no-write-fetch-head", "--recurse-submodules=no",
48 "--filter=blob:none", "--stdin", NULL);
49 if (!repo_config_get_bool(repo, "promisor.quiet", &quiet) && quiet)
50 strvec_push(&child.args, "--quiet");
51 if (start_command(&child))
52 die(_("promisor-remote: unable to fork off fetch subprocess"));
53 child_in = xfdopen(child.in, "w");
54
55 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
56
57 for (i = 0; i < oid_nr; i++) {
58 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
59 die_errno(_("promisor-remote: could not write to fetch subprocess"));
60 if (fputc('\n', child_in) < 0)
61 die_errno(_("promisor-remote: could not write to fetch subprocess"));
62 }
63
64 if (fclose(child_in) < 0)
65 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
66 return finish_command(&child) ? -1 : 0;
67 }
68
69 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
70 const char *remote_name)
71 {
72 struct promisor_remote *r;
73
74 if (*remote_name == '/') {
75 warning(_("promisor remote name cannot begin with '/': %s"),
76 remote_name);
77 return NULL;
78 }
79
80 FLEX_ALLOC_STR(r, name, remote_name);
81
82 *config->promisors_tail = r;
83 config->promisors_tail = &r->next;
84
85 return r;
86 }
87
88 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
89 const char *remote_name,
90 struct promisor_remote **previous)
91 {
92 struct promisor_remote *r, *p;
93
94 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
95 if (!strcmp(r->name, remote_name)) {
96 if (previous)
97 *previous = p;
98 return r;
99 }
100
101 return NULL;
102 }
103
104 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
105 struct promisor_remote *r,
106 struct promisor_remote *previous)
107 {
108 if (!r->next)
109 return;
110
111 if (previous)
112 previous->next = r->next;
113 else
114 config->promisors = r->next ? r->next : r;
115 r->next = NULL;
116 *config->promisors_tail = r;
117 config->promisors_tail = &r->next;
118 }
119
120 static int promisor_remote_config(const char *var, const char *value,
121 const struct config_context *ctx UNUSED,
122 void *data)
123 {
124 struct promisor_remote_config *config = data;
125 const char *name;
126 size_t namelen;
127 const char *subkey;
128
129 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
130 return 0;
131
132 if (!strcmp(subkey, "promisor")) {
133 char *remote_name;
134
135 if (!git_config_bool(var, value))
136 return 0;
137
138 remote_name = xmemdupz(name, namelen);
139
140 if (!promisor_remote_lookup(config, remote_name, NULL))
141 promisor_remote_new(config, remote_name);
142
143 free(remote_name);
144 return 0;
145 }
146 if (!strcmp(subkey, "partialclonefilter")) {
147 struct promisor_remote *r;
148 char *remote_name = xmemdupz(name, namelen);
149
150 r = promisor_remote_lookup(config, remote_name, NULL);
151 if (!r)
152 r = promisor_remote_new(config, remote_name);
153
154 free(remote_name);
155
156 if (!r)
157 return 0;
158
159 FREE_AND_NULL(r->partial_clone_filter);
160 return git_config_string(&r->partial_clone_filter, var, value);
161 }
162
163 return 0;
164 }
165
166 static void promisor_remote_init(struct repository *r)
167 {
168 struct promisor_remote_config *config;
169
170 if (r->promisor_remote_config)
171 return;
172 config = r->promisor_remote_config =
173 xcalloc(1, sizeof(*r->promisor_remote_config));
174 config->promisors_tail = &config->promisors;
175
176 repo_config(r, promisor_remote_config, config);
177
178 if (r->repository_format_partial_clone) {
179 struct promisor_remote *o, *previous;
180
181 o = promisor_remote_lookup(config,
182 r->repository_format_partial_clone,
183 &previous);
184 if (o)
185 promisor_remote_move_to_tail(config, o, previous);
186 else
187 promisor_remote_new(config, r->repository_format_partial_clone);
188 }
189 }
190
191 void promisor_remote_clear(struct promisor_remote_config *config)
192 {
193 while (config->promisors) {
194 struct promisor_remote *r = config->promisors;
195 free(r->partial_clone_filter);
196 free(r->advertised_filter);
197 config->promisors = config->promisors->next;
198 free(r);
199 }
200
201 config->promisors_tail = &config->promisors;
202 }
203
204 void repo_promisor_remote_reinit(struct repository *r)
205 {
206 promisor_remote_clear(r->promisor_remote_config);
207 FREE_AND_NULL(r->promisor_remote_config);
208 promisor_remote_init(r);
209 }
210
211 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
212 const char *remote_name)
213 {
214 promisor_remote_init(r);
215
216 if (!remote_name)
217 return r->promisor_remote_config->promisors;
218
219 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
220 }
221
222 int repo_has_promisor_remote(struct repository *r)
223 {
224 return !!repo_promisor_remote_find(r, NULL);
225 }
226
227 int repo_has_accepted_promisor_remote(struct repository *r)
228 {
229 struct promisor_remote *p;
230
231 promisor_remote_init(r);
232
233 for (p = r->promisor_remote_config->promisors; p; p = p->next)
234 if (p->accepted)
235 return 1;
236 return 0;
237 }
238
239 static int remove_fetched_oids(struct repository *repo,
240 struct object_id **oids,
241 int oid_nr, int to_free)
242 {
243 int i, remaining_nr = 0;
244 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
245 struct object_id *old_oids = *oids;
246 struct object_id *new_oids;
247
248 for (i = 0; i < oid_nr; i++)
249 if (odb_read_object_info_extended(repo->objects, &old_oids[i], NULL,
250 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
251 remaining[i] = 1;
252 remaining_nr++;
253 }
254
255 if (remaining_nr) {
256 int j = 0;
257 CALLOC_ARRAY(new_oids, remaining_nr);
258 for (i = 0; i < oid_nr; i++)
259 if (remaining[i])
260 oidcpy(&new_oids[j++], &old_oids[i]);
261 *oids = new_oids;
262 if (to_free)
263 free(old_oids);
264 }
265
266 free(remaining);
267
268 return remaining_nr;
269 }
270
271 static int try_promisor_remotes(struct repository *repo,
272 struct object_id **remaining_oids,
273 int *remaining_nr, int *to_free,
274 bool accepted_only)
275 {
276 struct promisor_remote *r = repo->promisor_remote_config->promisors;
277
278 for (; r; r = r->next) {
279 if (accepted_only != r->accepted)
280 continue;
281 if (fetch_objects(repo, r->name, *remaining_oids, *remaining_nr) < 0) {
282 if (*remaining_nr == 1)
283 continue;
284 *remaining_nr = remove_fetched_oids(repo, remaining_oids,
285 *remaining_nr, *to_free);
286 if (*remaining_nr) {
287 *to_free = 1;
288 continue;
289 }
290 }
291 return 1; /* all fetched */
292 }
293 return 0;
294 }
295
296 void promisor_remote_get_direct(struct repository *repo,
297 const struct object_id *oids,
298 int oid_nr)
299 {
300 struct object_id *remaining_oids = (struct object_id *)oids;
301 int remaining_nr = oid_nr;
302 int to_free = 0;
303 int i;
304
305 if (oid_nr == 0)
306 return;
307
308 promisor_remote_init(repo);
309
310 /* Try accepted remotes first (those the server told us to use) */
311 if (try_promisor_remotes(repo, &remaining_oids, &remaining_nr,
312 &to_free, true))
313 goto all_fetched;
314 if (try_promisor_remotes(repo, &remaining_oids, &remaining_nr,
315 &to_free, false))
316 goto all_fetched;
317
318 for (i = 0; i < remaining_nr; i++) {
319 if (is_promisor_object(repo, &remaining_oids[i]))
320 die(_("could not fetch %s from promisor remote"),
321 oid_to_hex(&remaining_oids[i]));
322 }
323
324 all_fetched:
325 if (to_free)
326 free(remaining_oids);
327 }
328
329 static int allow_unsanitized(char ch)
330 {
331 if (ch == ',' || ch == ';' || ch == '%')
332 return 0;
333 return ch > 32 && ch < 127;
334 }
335
336 /*
337 * All the fields used in "promisor-remote" protocol capability,
338 * including the mandatory "name" and "url" ones.
339 */
340 static const char promisor_field_name[] = "name";
341 static const char promisor_field_url[] = "url";
342 static const char promisor_field_filter[] = "partialCloneFilter";
343 static const char promisor_field_token[] = "token";
344
345 /*
346 * List of optional field names that can be used in the
347 * "promisor-remote" protocol capability (others must be
348 * ignored). Each field should correspond to a configurable property
349 * of a remote that can be relevant for the client.
350 */
351 static const char *known_fields[] = {
352 promisor_field_filter, /* Filter used for partial clone */
353 promisor_field_token, /* Authentication token for the remote */
354 NULL
355 };
356
357 /*
358 * Check if 'field' is in the list of the known field names for the
359 * "promisor-remote" protocol capability.
360 */
361 static int is_known_field(const char *field)
362 {
363 const char **p;
364
365 for (p = known_fields; *p; p++)
366 if (!strcasecmp(*p, field))
367 return 1;
368 return 0;
369 }
370
371 static int is_valid_field(struct string_list_item *item, void *cb_data)
372 {
373 const char *field = item->string;
374 const char *config_key = (const char *)cb_data;
375
376 if (!is_known_field(field)) {
377 warning(_("unsupported field '%s' in '%s' config"), field, config_key);
378 return 0;
379 }
380 return 1;
381 }
382
383 static char *fields_from_config(struct string_list *fields_list, const char *config_key)
384 {
385 char *fields = NULL;
386
387 if (!repo_config_get_string(the_repository, config_key, &fields) && *fields) {
388 string_list_split_in_place_f(fields_list, fields, ",", -1,
389 STRING_LIST_SPLIT_TRIM |
390 STRING_LIST_SPLIT_NONEMPTY);
391 filter_string_list(fields_list, 0, is_valid_field, (void *)config_key);
392 }
393
394 return fields;
395 }
396
397 static struct string_list *initialize_fields_list(struct string_list *fields_list, int *initialized,
398 const char *config_key)
399 {
400 if (!*initialized) {
401 fields_list->cmp = strcasecmp;
402 fields_from_config(fields_list, config_key);
403 *initialized = 1;
404 }
405
406 return fields_list;
407 }
408
409 static struct string_list *fields_sent(void)
410 {
411 static struct string_list fields_list = STRING_LIST_INIT_NODUP;
412 static int initialized;
413
414 return initialize_fields_list(&fields_list, &initialized, "promisor.sendFields");
415 }
416
417 static struct string_list *fields_checked(void)
418 {
419 static struct string_list fields_list = STRING_LIST_INIT_NODUP;
420 static int initialized;
421
422 return initialize_fields_list(&fields_list, &initialized, "promisor.checkFields");
423 }
424
425 static struct string_list *fields_stored(void)
426 {
427 static struct string_list fields_list = STRING_LIST_INIT_NODUP;
428 static int initialized;
429
430 return initialize_fields_list(&fields_list, &initialized, "promisor.storeFields");
431 }
432
433 /*
434 * Struct for promisor remotes involved in the "promisor-remote"
435 * protocol capability.
436 *
437 * Except for "name", each <member> in this struct and its <value>
438 * should correspond (either on the client side or on the server side)
439 * to a "remote.<name>.<member>" config variable set to <value> where
440 * "<name>" is a promisor remote name.
441 */
442 struct promisor_info {
443 const char *name;
444 const char *url;
445 const char *filter;
446 const char *token;
447 };
448
449 static void promisor_info_free(struct promisor_info *p)
450 {
451 free((char *)p->name);
452 free((char *)p->url);
453 free((char *)p->filter);
454 free((char *)p->token);
455 free(p);
456 }
457
458 static void promisor_info_list_clear(struct string_list *list)
459 {
460 for (size_t i = 0; i < list->nr; i++)
461 promisor_info_free(list->items[i].util);
462 string_list_clear(list, 0);
463 }
464
465 static void set_one_field(struct promisor_info *p,
466 const char *field, const char *value)
467 {
468 if (!strcasecmp(field, promisor_field_filter))
469 p->filter = xstrdup(value);
470 else if (!strcasecmp(field, promisor_field_token))
471 p->token = xstrdup(value);
472 else
473 BUG("invalid field '%s'", field);
474 }
475
476 static void set_fields(struct promisor_info *p,
477 struct string_list *field_names)
478 {
479 struct string_list_item *item;
480
481 for_each_string_list_item(item, field_names) {
482 char *key = xstrfmt("remote.%s.%s", p->name, item->string);
483 const char *val;
484 if (!repo_config_get_string_tmp(the_repository, key, &val) && *val)
485 set_one_field(p, item->string, val);
486 free(key);
487 }
488 }
489
490 /*
491 * Populate 'list' with promisor remote information from the config.
492 * The 'util' pointer of each list item will hold a 'struct
493 * promisor_info'. Except "name" and "url", only members of that
494 * struct specified by the 'field_names' list are set (using values
495 * from the configuration).
496 */
497 static void promisor_config_info_list(struct repository *repo,
498 struct string_list *list,
499 struct string_list *field_names)
500 {
501 struct promisor_remote *r;
502
503 promisor_remote_init(repo);
504
505 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
506 const char *url;
507 char *url_key = xstrfmt("remote.%s.url", r->name);
508
509 /* Only add remotes with a non empty URL */
510 if (!repo_config_get_string_tmp(the_repository, url_key, &url) && *url) {
511 struct promisor_info *new_info = xcalloc(1, sizeof(*new_info));
512 struct string_list_item *item;
513
514 new_info->name = xstrdup(r->name);
515 new_info->url = xstrdup(url);
516
517 if (field_names)
518 set_fields(new_info, field_names);
519
520 item = string_list_append(list, new_info->name);
521 item->util = new_info;
522 }
523
524 free(url_key);
525 }
526 }
527
528 char *promisor_remote_info(struct repository *repo)
529 {
530 struct strbuf sb = STRBUF_INIT;
531 int advertise_promisors = 0;
532 struct string_list config_info = STRING_LIST_INIT_NODUP;
533 struct string_list_item *item;
534
535 repo_config_get_bool(the_repository, "promisor.advertise", &advertise_promisors);
536
537 if (!advertise_promisors)
538 return NULL;
539
540 promisor_config_info_list(repo, &config_info, fields_sent());
541
542 if (!config_info.nr)
543 return NULL;
544
545 for_each_string_list_item(item, &config_info) {
546 struct promisor_info *p = item->util;
547
548 if (item != config_info.items)
549 strbuf_addch(&sb, ';');
550
551 strbuf_addf(&sb, "%s=", promisor_field_name);
552 strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized);
553 strbuf_addf(&sb, ",%s=", promisor_field_url);
554 strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized);
555
556 if (p->filter) {
557 strbuf_addf(&sb, ",%s=", promisor_field_filter);
558 strbuf_addstr_urlencode(&sb, p->filter, allow_unsanitized);
559 }
560 if (p->token) {
561 strbuf_addf(&sb, ",%s=", promisor_field_token);
562 strbuf_addstr_urlencode(&sb, p->token, allow_unsanitized);
563 }
564 }
565
566 promisor_info_list_clear(&config_info);
567
568 return strbuf_detach(&sb, NULL);
569 }
570
571 enum accept_promisor {
572 ACCEPT_NONE = 0,
573 ACCEPT_KNOWN_URL,
574 ACCEPT_KNOWN_NAME,
575 ACCEPT_ALL
576 };
577
578 /*
579 * Check if a specific field and its advertised value match the local
580 * configuration of a given promisor remote.
581 *
582 * Returns 1 if they match, 0 otherwise.
583 */
584 static int match_field_against_config(const char *field, const char *value,
585 struct promisor_info *config_info)
586 {
587 if (config_info->filter && !strcasecmp(field, promisor_field_filter))
588 return !strcmp(config_info->filter, value);
589 else if (config_info->token && !strcasecmp(field, promisor_field_token))
590 return !strcmp(config_info->token, value);
591
592 return 0;
593 }
594
595 /*
596 * Check that the advertised fields match the local configuration.
597 *
598 * When 'config_entry' is NULL (ACCEPT_ALL mode), every checked field
599 * must match at least one remote in 'config_info'.
600 *
601 * When 'config_entry' points to a specific remote's config, the
602 * checked fields are compared against that single remote only.
603 */
604 static int all_fields_match(struct promisor_info *advertised,
605 struct string_list *config_info,
606 struct promisor_info *config_entry)
607 {
608 struct string_list *fields = fields_checked();
609 struct string_list_item *item_checked;
610
611 for_each_string_list_item(item_checked, fields) {
612 int match = 0;
613 const char *field = item_checked->string;
614 const char *value = NULL;
615
616 if (!strcasecmp(field, promisor_field_filter))
617 value = advertised->filter;
618 else if (!strcasecmp(field, promisor_field_token))
619 value = advertised->token;
620
621 if (!value)
622 return 0;
623
624 if (config_entry) {
625 match = match_field_against_config(field, value,
626 config_entry);
627 } else {
628 struct string_list_item *item;
629 for_each_string_list_item(item, config_info) {
630 struct promisor_info *p = item->util;
631 if (match_field_against_config(field, value, p)) {
632 match = 1;
633 break;
634 }
635 }
636 }
637
638 if (!match)
639 return 0;
640 }
641
642 return 1;
643 }
644
645 static bool has_control_char(const char *s)
646 {
647 for (const char *c = s; *c; c++)
648 if (iscntrl(*c))
649 return true;
650 return false;
651 }
652
653 static int should_accept_remote(enum accept_promisor accept,
654 struct promisor_info *advertised,
655 struct string_list *config_info)
656 {
657 struct promisor_info *p;
658 struct string_list_item *item;
659 const char *remote_name = advertised->name;
660 const char *remote_url = advertised->url;
661
662 if (!remote_url || !*remote_url)
663 BUG("no or empty URL advertised for remote '%s'; "
664 "this remote should have been rejected earlier",
665 remote_name);
666
667 if (accept == ACCEPT_ALL)
668 return all_fields_match(advertised, config_info, NULL);
669
670 /* Get config info for that promisor remote */
671 item = string_list_lookup(config_info, remote_name);
672
673 if (!item)
674 /* We don't know about that remote */
675 return 0;
676
677 p = item->util;
678
679 if (accept == ACCEPT_KNOWN_NAME)
680 return all_fields_match(advertised, config_info, p);
681
682 if (accept != ACCEPT_KNOWN_URL)
683 BUG("Unhandled 'enum accept_promisor' value '%d'", accept);
684
685 if (strcmp(p->url, remote_url)) {
686 warning(_("known remote named '%s' but with URL '%s' instead of '%s', "
687 "ignoring this remote"),
688 remote_name, p->url, remote_url);
689 return 0;
690 }
691
692 return all_fields_match(advertised, config_info, p);
693 }
694
695 static int skip_field_name_prefix(const char *elem, const char *field_name, const char **value)
696 {
697 const char *p;
698 if (!skip_prefix(elem, field_name, &p) || *p != '=')
699 return 0;
700 *value = p + 1;
701 return 1;
702 }
703
704 static struct promisor_info *parse_one_advertised_remote(const char *remote_info)
705 {
706 struct promisor_info *info = xcalloc(1, sizeof(*info));
707 struct string_list elem_list = STRING_LIST_INIT_DUP;
708 struct string_list_item *item;
709
710 string_list_split(&elem_list, remote_info, ",", -1);
711
712 for_each_string_list_item(item, &elem_list) {
713 const char *elem = item->string;
714 const char *p = strchr(elem, '=');
715
716 if (!p) {
717 warning(_("invalid element '%s' from remote info"), elem);
718 continue;
719 }
720
721 if (skip_field_name_prefix(elem, promisor_field_name, &p))
722 info->name = url_percent_decode(p);
723 else if (skip_field_name_prefix(elem, promisor_field_url, &p))
724 info->url = url_percent_decode(p);
725 else if (skip_field_name_prefix(elem, promisor_field_filter, &p))
726 info->filter = url_percent_decode(p);
727 else if (skip_field_name_prefix(elem, promisor_field_token, &p))
728 info->token = url_percent_decode(p);
729 }
730
731 string_list_clear(&elem_list, 0);
732
733 if (!info->name || !*info->name || !info->url || !*info->url) {
734 warning(_("server advertised a promisor remote without a name or URL: '%s', "
735 "ignoring this remote"), remote_info);
736 promisor_info_free(info);
737 return NULL;
738 }
739
740 return info;
741 }
742
743 static bool store_one_field(struct repository *repo, const char *remote_name,
744 const char *field_name, const char *field_key,
745 const char *advertised, const char *current)
746 {
747 if (advertised && (!current || strcmp(current, advertised))) {
748 char *key = xstrfmt("remote.%s.%s", remote_name, field_key);
749
750 fprintf(stderr, _("Storing new %s from server for remote '%s'.\n"
751 " '%s' -> '%s'\n"),
752 field_name, remote_name,
753 current ? current : "",
754 advertised);
755
756 repo_config_set_gently(repo, key, advertised);
757 free(key);
758
759 return true;
760 }
761
762 return false;
763 }
764
765 /* Check that a filter is valid by parsing it */
766 static bool valid_filter(const char *filter, const char *remote_name)
767 {
768 struct list_objects_filter_options filter_opts = LIST_OBJECTS_FILTER_INIT;
769 struct strbuf err = STRBUF_INIT;
770 int res = gently_parse_list_objects_filter(&filter_opts, filter, &err);
771
772 if (res)
773 warning(_("invalid filter '%s' for remote '%s' "
774 "will not be stored: %s"),
775 filter, remote_name, err.buf);
776
777 list_objects_filter_release(&filter_opts);
778 strbuf_release(&err);
779
780 return !res;
781 }
782
783 static bool valid_token(const char *token, const char *remote_name)
784 {
785 if (has_control_char(token)) {
786 warning(_("invalid token '%s' for remote '%s' "
787 "will not be stored"),
788 token, remote_name);
789 return false;
790 }
791
792 return true;
793 }
794
795 struct store_info {
796 struct repository *repo;
797 struct string_list config_info;
798 bool store_filter;
799 bool store_token;
800 };
801
802 static struct store_info *store_info_new(struct repository *repo)
803 {
804 struct string_list *fields_to_store = fields_stored();
805 struct store_info *s = xmalloc(sizeof(*s));
806
807 s->repo = repo;
808
809 string_list_init_nodup(&s->config_info);
810 promisor_config_info_list(repo, &s->config_info, fields_to_store);
811 string_list_sort(&s->config_info);
812
813 s->store_filter = !!string_list_lookup(fields_to_store, promisor_field_filter);
814 s->store_token = !!string_list_lookup(fields_to_store, promisor_field_token);
815
816 return s;
817 }
818
819 static void store_info_free(struct store_info *s)
820 {
821 if (s) {
822 promisor_info_list_clear(&s->config_info);
823 free(s);
824 }
825 }
826
827 static bool promisor_store_advertised_fields(struct promisor_info *advertised,
828 struct store_info *store_info)
829 {
830 struct promisor_info *p;
831 struct string_list_item *item;
832 const char *remote_name = advertised->name;
833 bool reload_config = false;
834
835 if (!(store_info->store_filter || store_info->store_token))
836 return false;
837
838 /*
839 * Get existing config info for the advertised promisor
840 * remote. This ensures the remote is already configured on
841 * the client side.
842 */
843 item = string_list_lookup(&store_info->config_info, remote_name);
844
845 if (!item)
846 return false;
847
848 p = item->util;
849
850 if (store_info->store_filter && advertised->filter &&
851 valid_filter(advertised->filter, remote_name))
852 reload_config |= store_one_field(store_info->repo, remote_name,
853 "filter", promisor_field_filter,
854 advertised->filter, p->filter);
855
856 if (store_info->store_token && advertised->token &&
857 valid_token(advertised->token, remote_name))
858 reload_config |= store_one_field(store_info->repo, remote_name,
859 "token", promisor_field_token,
860 advertised->token, p->token);
861
862 return reload_config;
863 }
864
865 static enum accept_promisor accept_from_server(struct repository *repo)
866 {
867 const char *accept_str;
868 enum accept_promisor accept = ACCEPT_NONE;
869
870 if (!repo_config_get_string_tmp(repo, "promisor.acceptfromserver", &accept_str)) {
871 if (!*accept_str || !strcasecmp("None", accept_str))
872 accept = ACCEPT_NONE;
873 else if (!strcasecmp("KnownUrl", accept_str))
874 accept = ACCEPT_KNOWN_URL;
875 else if (!strcasecmp("KnownName", accept_str))
876 accept = ACCEPT_KNOWN_NAME;
877 else if (!strcasecmp("All", accept_str))
878 accept = ACCEPT_ALL;
879 else
880 warning(_("unknown '%s' value for '%s' config option"),
881 accept_str, "promisor.acceptfromserver");
882 }
883
884 return accept;
885 }
886
887 static void filter_promisor_remote(struct repository *repo,
888 struct string_list *accepted_remotes,
889 const char *info)
890 {
891 struct string_list config_info = STRING_LIST_INIT_NODUP;
892 struct string_list remote_info = STRING_LIST_INIT_DUP;
893 struct store_info *store_info = NULL;
894 struct string_list_item *item;
895 bool reload_config = false;
896 enum accept_promisor accept = accept_from_server(repo);
897
898 if (accept == ACCEPT_NONE)
899 return;
900
901 /* Parse remote info received */
902
903 string_list_split(&remote_info, info, ";", -1);
904
905 for_each_string_list_item(item, &remote_info) {
906 struct promisor_info *advertised;
907
908 advertised = parse_one_advertised_remote(item->string);
909
910 if (!advertised)
911 continue;
912
913 if (!config_info.nr) {
914 promisor_config_info_list(repo, &config_info, fields_checked());
915 string_list_sort(&config_info);
916 }
917
918 if (should_accept_remote(accept, advertised, &config_info)) {
919 if (!store_info)
920 store_info = store_info_new(repo);
921 if (promisor_store_advertised_fields(advertised, store_info))
922 reload_config = true;
923
924 string_list_append(accepted_remotes, advertised->name)->util = advertised;
925 } else {
926 promisor_info_free(advertised);
927 }
928 }
929
930 promisor_info_list_clear(&config_info);
931 string_list_clear(&remote_info, 0);
932 store_info_free(store_info);
933
934 if (reload_config)
935 repo_promisor_remote_reinit(repo);
936
937 /* Apply accepted remotes to the stable repo state */
938 for_each_string_list_item(item, accepted_remotes) {
939 struct promisor_info *info = item->util;
940 struct promisor_remote *r = repo_promisor_remote_find(repo, info->name);
941
942 if (r) {
943 r->accepted = 1;
944 if (info->filter) {
945 free(r->advertised_filter);
946 r->advertised_filter = xstrdup(info->filter);
947 }
948 }
949 }
950 }
951
952 void promisor_remote_reply(const char *info, char **accepted_out)
953 {
954 struct string_list accepted_remotes = STRING_LIST_INIT_NODUP;
955
956 filter_promisor_remote(the_repository, &accepted_remotes, info);
957
958 if (accepted_out) {
959 if (accepted_remotes.nr) {
960 struct strbuf reply = STRBUF_INIT;
961 struct string_list_item *item;
962
963 for_each_string_list_item(item, &accepted_remotes) {
964 if (reply.len)
965 strbuf_addch(&reply, ';');
966 strbuf_addstr_urlencode(&reply, item->string, allow_unsanitized);
967 }
968 *accepted_out = strbuf_detach(&reply, NULL);
969 } else {
970 *accepted_out = NULL;
971 }
972 }
973
974 promisor_info_list_clear(&accepted_remotes);
975 }
976
977 void mark_promisor_remotes_as_accepted(struct repository *r, const char *remotes)
978 {
979 struct string_list accepted_remotes = STRING_LIST_INIT_DUP;
980 struct string_list_item *item;
981
982 string_list_split(&accepted_remotes, remotes, ";", -1);
983
984 for_each_string_list_item(item, &accepted_remotes) {
985 char *decoded_remote = url_percent_decode(item->string);
986 struct promisor_remote *p = repo_promisor_remote_find(r, decoded_remote);
987
988 if (p)
989 p->accepted = 1;
990 else
991 warning(_("accepted promisor remote '%s' not found"),
992 decoded_remote);
993
994 free(decoded_remote);
995 }
996
997 string_list_clear(&accepted_remotes, 0);
998 }
999
1000 char *promisor_remote_construct_filter(struct repository *repo)
1001 {
1002 struct promisor_remote *r;
1003 struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
1004 struct strbuf err = STRBUF_INIT;
1005 char *result = NULL;
1006
1007 promisor_remote_init(repo);
1008
1009 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
1010 if (r->accepted && r->advertised_filter)
1011 if (gently_parse_list_objects_filter(&filter_options,
1012 r->advertised_filter,
1013 &err)) {
1014 warning(_("promisor remote '%s' advertised invalid filter '%s': %s"),
1015 r->name, r->advertised_filter, err.buf);
1016 strbuf_reset(&err);
1017 continue;
1018 }
1019 }
1020
1021 if (filter_options.choice)
1022 result = xstrdup(expand_list_objects_filter_spec(&filter_options));
1023
1024 list_objects_filter_release(&filter_options);
1025 strbuf_release(&err);
1026
1027 return result;
1028 }