git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / promisor-remote.c
| 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 "urlmatch.h" |
| 16 | #include "version.h" |
| 17 | #include "wildmatch.h" |
| 18 | |
| 19 | struct promisor_remote_config { |
| 20 | struct promisor_remote *promisors; |
| 21 | struct promisor_remote **promisors_tail; |
| 22 | }; |
| 23 | |
| 24 | static int fetch_objects(struct repository *repo, |
| 25 | const char *remote_name, |
| 26 | const struct object_id *oids, |
| 27 | int oid_nr, int depth) |
| 28 | { |
| 29 | struct child_process child = CHILD_PROCESS_INIT; |
| 30 | int i; |
| 31 | FILE *child_in; |
| 32 | int quiet; |
| 33 | |
| 34 | child.git_cmd = 1; |
| 35 | child.in = -1; |
| 36 | if (repo != the_repository) |
| 37 | prepare_other_repo_env(&child.env, repo->gitdir); |
| 38 | strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop", |
| 39 | "fetch", remote_name, "--no-tags", |
| 40 | "--no-write-fetch-head", "--recurse-submodules=no", |
| 41 | "--filter=blob:none", "--stdin", NULL); |
| 42 | if (!repo_config_get_bool(repo, "promisor.quiet", &quiet) && quiet) |
| 43 | strvec_push(&child.args, "--quiet"); |
| 44 | strvec_pushf(&child.env, "%s=%d", LAZY_FETCH_DEPTH_ENVIRONMENT, depth + 1); |
| 45 | if (start_command(&child)) |
| 46 | die(_("promisor-remote: unable to fork off fetch subprocess")); |
| 47 | child_in = xfdopen(child.in, "w"); |
| 48 | |
| 49 | trace2_data_intmax("promisor", repo, "fetch_count", oid_nr); |
| 50 | |
| 51 | for (i = 0; i < oid_nr; i++) { |
| 52 | if (fputs(oid_to_hex(&oids[i]), child_in) < 0) |
| 53 | die_errno(_("promisor-remote: could not write to fetch subprocess")); |
| 54 | if (fputc('\n', child_in) < 0) |
| 55 | die_errno(_("promisor-remote: could not write to fetch subprocess")); |
| 56 | } |
| 57 | |
| 58 | if (fclose(child_in) < 0) |
| 59 | die_errno(_("promisor-remote: could not close stdin to fetch subprocess")); |
| 60 | return finish_command(&child) ? -1 : 0; |
| 61 | } |
| 62 | |
| 63 | static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config, |
| 64 | const char *remote_name) |
| 65 | { |
| 66 | struct promisor_remote *r; |
| 67 | |
| 68 | if (*remote_name == '/') { |
| 69 | warning(_("promisor remote name cannot begin with '/': %s"), |
| 70 | remote_name); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | FLEX_ALLOC_STR(r, name, remote_name); |
| 75 | |
| 76 | *config->promisors_tail = r; |
| 77 | config->promisors_tail = &r->next; |
| 78 | |
| 79 | return r; |
| 80 | } |
| 81 | |
| 82 | static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config, |
| 83 | const char *remote_name, |
| 84 | struct promisor_remote **previous) |
| 85 | { |
| 86 | struct promisor_remote *r, *p; |
| 87 | |
| 88 | for (p = NULL, r = config->promisors; r; p = r, r = r->next) |
| 89 | if (!strcmp(r->name, remote_name)) { |
| 90 | if (previous) |
| 91 | *previous = p; |
| 92 | return r; |
| 93 | } |
| 94 | |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | static void promisor_remote_move_to_tail(struct promisor_remote_config *config, |
| 99 | struct promisor_remote *r, |
| 100 | struct promisor_remote *previous) |
| 101 | { |
| 102 | if (!r->next) |
| 103 | return; |
| 104 | |
| 105 | if (previous) |
| 106 | previous->next = r->next; |
| 107 | else |
| 108 | config->promisors = r->next ? r->next : r; |
| 109 | r->next = NULL; |
| 110 | *config->promisors_tail = r; |
| 111 | config->promisors_tail = &r->next; |
| 112 | } |
| 113 | |
| 114 | static int promisor_remote_config(const char *var, const char *value, |
| 115 | const struct config_context *ctx UNUSED, |
| 116 | void *data) |
| 117 | { |
| 118 | struct promisor_remote_config *config = data; |
| 119 | const char *name; |
| 120 | size_t namelen; |
| 121 | const char *subkey; |
| 122 | |
| 123 | if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0) |
| 124 | return 0; |
| 125 | |
| 126 | if (!strcmp(subkey, "promisor")) { |
| 127 | char *remote_name; |
| 128 | |
| 129 | if (!git_config_bool(var, value)) |
| 130 | return 0; |
| 131 | |
| 132 | remote_name = xmemdupz(name, namelen); |
| 133 | |
| 134 | if (!promisor_remote_lookup(config, remote_name, NULL)) |
| 135 | promisor_remote_new(config, remote_name); |
| 136 | |
| 137 | free(remote_name); |
| 138 | return 0; |
| 139 | } |
| 140 | if (!strcmp(subkey, "partialclonefilter")) { |
| 141 | struct promisor_remote *r; |
| 142 | char *remote_name = xmemdupz(name, namelen); |
| 143 | |
| 144 | r = promisor_remote_lookup(config, remote_name, NULL); |
| 145 | if (!r) |
| 146 | r = promisor_remote_new(config, remote_name); |
| 147 | |
| 148 | free(remote_name); |
| 149 | |
| 150 | if (!r) |
| 151 | return 0; |
| 152 | |
| 153 | FREE_AND_NULL(r->partial_clone_filter); |
| 154 | return git_config_string(&r->partial_clone_filter, var, value); |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void promisor_remote_init(struct repository *r) |
| 161 | { |
| 162 | struct promisor_remote_config *config; |
| 163 | |
| 164 | if (r->promisor_remote_config) |
| 165 | return; |
| 166 | config = r->promisor_remote_config = |
| 167 | xcalloc(1, sizeof(*r->promisor_remote_config)); |
| 168 | config->promisors_tail = &config->promisors; |
| 169 | |
| 170 | repo_config(r, promisor_remote_config, config); |
| 171 | |
| 172 | if (r->repository_format_partial_clone) { |
| 173 | struct promisor_remote *o, *previous; |
| 174 | |
| 175 | o = promisor_remote_lookup(config, |
| 176 | r->repository_format_partial_clone, |
| 177 | &previous); |
| 178 | if (o) |
| 179 | promisor_remote_move_to_tail(config, o, previous); |
| 180 | else |
| 181 | promisor_remote_new(config, r->repository_format_partial_clone); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void promisor_remote_clear(struct promisor_remote_config *config) |
| 186 | { |
| 187 | while (config->promisors) { |
| 188 | struct promisor_remote *r = config->promisors; |
| 189 | free(r->partial_clone_filter); |
| 190 | free(r->advertised_filter); |
| 191 | config->promisors = config->promisors->next; |
| 192 | free(r); |
| 193 | } |
| 194 | |
| 195 | config->promisors_tail = &config->promisors; |
| 196 | } |
| 197 | |
| 198 | void repo_promisor_remote_reinit(struct repository *r) |
| 199 | { |
| 200 | promisor_remote_clear(r->promisor_remote_config); |
| 201 | FREE_AND_NULL(r->promisor_remote_config); |
| 202 | promisor_remote_init(r); |
| 203 | } |
| 204 | |
| 205 | struct promisor_remote *repo_promisor_remote_find(struct repository *r, |
| 206 | const char *remote_name) |
| 207 | { |
| 208 | promisor_remote_init(r); |
| 209 | |
| 210 | if (!remote_name) |
| 211 | return r->promisor_remote_config->promisors; |
| 212 | |
| 213 | return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL); |
| 214 | } |
| 215 | |
| 216 | int repo_has_promisor_remote(struct repository *r) |
| 217 | { |
| 218 | return !!repo_promisor_remote_find(r, NULL); |
| 219 | } |
| 220 | |
| 221 | int repo_has_accepted_promisor_remote(struct repository *r) |
| 222 | { |
| 223 | struct promisor_remote *p; |
| 224 | |
| 225 | promisor_remote_init(r); |
| 226 | |
| 227 | for (p = r->promisor_remote_config->promisors; p; p = p->next) |
| 228 | if (p->accepted) |
| 229 | return 1; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int remove_fetched_oids(struct repository *repo, |
| 234 | struct object_id **oids, |
| 235 | int oid_nr, int to_free) |
| 236 | { |
| 237 | int i, remaining_nr = 0; |
| 238 | int *remaining = xcalloc(oid_nr, sizeof(*remaining)); |
| 239 | struct object_id *old_oids = *oids; |
| 240 | struct object_id *new_oids; |
| 241 | |
| 242 | for (i = 0; i < oid_nr; i++) |
| 243 | if (odb_read_object_info_extended(repo->objects, &old_oids[i], NULL, |
| 244 | OBJECT_INFO_SKIP_FETCH_OBJECT)) { |
| 245 | remaining[i] = 1; |
| 246 | remaining_nr++; |
| 247 | } |
| 248 | |
| 249 | if (remaining_nr) { |
| 250 | int j = 0; |
| 251 | CALLOC_ARRAY(new_oids, remaining_nr); |
| 252 | for (i = 0; i < oid_nr; i++) |
| 253 | if (remaining[i]) |
| 254 | oidcpy(&new_oids[j++], &old_oids[i]); |
| 255 | *oids = new_oids; |
| 256 | if (to_free) |
| 257 | free(old_oids); |
| 258 | } |
| 259 | |
| 260 | free(remaining); |
| 261 | |
| 262 | return remaining_nr; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Fetch the remaining objects (given in '*remaining_oids', which |
| 267 | * contains '*remaining_nr' object ids) from the known promisor |
| 268 | * remotes. If 'accepted_only' is true, ignore promisor remotes with |
| 269 | * their 'accepted' member unset. |
| 270 | * |
| 271 | * When a fetch from a remote fails, the objects that are still |
| 272 | * missing are computed, and '*remaining_oids' and '*remaining_nr' are |
| 273 | * updated accordingly before trying the next remote. In that case |
| 274 | * '*remaining_oids' points to a new array that this function |
| 275 | * allocated, and '*to_free' is set to 1 to tell the caller that it |
| 276 | * owns that array and should free it. '*to_free' should be 0 on the |
| 277 | * first call. |
| 278 | * |
| 279 | * Return 1 when all the requested objects have been fetched, 0 |
| 280 | * otherwise. |
| 281 | */ |
| 282 | static int try_promisor_remotes(struct repository *repo, |
| 283 | struct object_id **remaining_oids, |
| 284 | int *remaining_nr, |
| 285 | int *to_free, |
| 286 | int depth, |
| 287 | bool accepted_only) |
| 288 | { |
| 289 | struct promisor_remote *r = repo->promisor_remote_config->promisors; |
| 290 | |
| 291 | for (; r; r = r->next) { |
| 292 | if (accepted_only != r->accepted) |
| 293 | continue; |
| 294 | if (fetch_objects(repo, r->name, |
| 295 | *remaining_oids, *remaining_nr, depth) < 0) { |
| 296 | if (*remaining_nr == 1) |
| 297 | continue; |
| 298 | *remaining_nr = remove_fetched_oids(repo, remaining_oids, |
| 299 | *remaining_nr, *to_free); |
| 300 | if (*remaining_nr) { |
| 301 | *to_free = 1; |
| 302 | continue; |
| 303 | } |
| 304 | } |
| 305 | return 1; /* all fetched */ |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | #define MAX_LAZY_FETCH_DEPTH 5 |
| 311 | |
| 312 | /* |
| 313 | * Lazily fetch the objects given in '*remaining_oids' from the |
| 314 | * promisor remotes, trying the accepted ones first. See |
| 315 | * try_promisor_remotes() above for how '*remaining_oids', |
| 316 | * '*remaining_nr' and '*to_free' are used. |
| 317 | * |
| 318 | * Return 1 when all the requested objects have been fetched, 0 |
| 319 | * otherwise. |
| 320 | */ |
| 321 | static int lazy_fetch_objects(struct repository *repo, |
| 322 | struct object_id **remaining_oids, |
| 323 | int *remaining_nr, |
| 324 | int *to_free) |
| 325 | { |
| 326 | int depth = (int)git_env_ulong(LAZY_FETCH_DEPTH_ENVIRONMENT, 0); |
| 327 | |
| 328 | if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0)) { |
| 329 | static int warning_shown; |
| 330 | if (!warning_shown) { |
| 331 | warning_shown = 1; |
| 332 | warning(_("lazy fetching disabled; some objects may not be available")); |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | if (depth >= MAX_LAZY_FETCH_DEPTH) { |
| 338 | static int warning_shown; |
| 339 | if (!warning_shown) { |
| 340 | warning_shown = 1; |
| 341 | warning(_("too many nested lazy fetches (%d); " |
| 342 | "is a promisor remote pointing at the repository itself?"), |
| 343 | depth); |
| 344 | } |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | promisor_remote_init(repo); |
| 349 | |
| 350 | /* Try accepted remotes first (those the server told us to use) */ |
| 351 | return try_promisor_remotes(repo, remaining_oids, remaining_nr, |
| 352 | to_free, depth, true) || |
| 353 | try_promisor_remotes(repo, remaining_oids, remaining_nr, |
| 354 | to_free, depth, false); |
| 355 | } |
| 356 | |
| 357 | void promisor_remote_get_direct(struct repository *repo, |
| 358 | const struct object_id *oids, |
| 359 | int oid_nr) |
| 360 | { |
| 361 | struct object_id *remaining_oids = (struct object_id *)oids; |
| 362 | int remaining_nr = oid_nr; |
| 363 | int to_free = 0; |
| 364 | |
| 365 | if (oid_nr == 0) |
| 366 | return; |
| 367 | |
| 368 | if (!lazy_fetch_objects(repo, &remaining_oids, &remaining_nr, &to_free)) { |
| 369 | for (int i = 0; i < remaining_nr; i++) { |
| 370 | if (is_promisor_object(repo, &remaining_oids[i])) |
| 371 | die(_("could not fetch %s from promisor remote"), |
| 372 | oid_to_hex(&remaining_oids[i])); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (to_free) |
| 377 | free(remaining_oids); |
| 378 | } |
| 379 | |
| 380 | static int allow_unsanitized(char ch) |
| 381 | { |
| 382 | if (ch == ',' || ch == ';' || ch == '%') |
| 383 | return 0; |
| 384 | return ch > 32 && ch < 127; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * All the fields used in "promisor-remote" protocol capability, |
| 389 | * including the mandatory "name" and "url" ones. |
| 390 | */ |
| 391 | static const char promisor_field_name[] = "name"; |
| 392 | static const char promisor_field_url[] = "url"; |
| 393 | static const char promisor_field_filter[] = "partialCloneFilter"; |
| 394 | static const char promisor_field_token[] = "token"; |
| 395 | |
| 396 | /* |
| 397 | * List of optional field names that can be used in the |
| 398 | * "promisor-remote" protocol capability (others must be |
| 399 | * ignored). Each field should correspond to a configurable property |
| 400 | * of a remote that can be relevant for the client. |
| 401 | */ |
| 402 | static const char *known_fields[] = { |
| 403 | promisor_field_filter, /* Filter used for partial clone */ |
| 404 | promisor_field_token, /* Authentication token for the remote */ |
| 405 | NULL |
| 406 | }; |
| 407 | |
| 408 | /* |
| 409 | * Check if 'field' is in the list of the known field names for the |
| 410 | * "promisor-remote" protocol capability. |
| 411 | */ |
| 412 | static int is_known_field(const char *field) |
| 413 | { |
| 414 | const char **p; |
| 415 | |
| 416 | for (p = known_fields; *p; p++) |
| 417 | if (!strcasecmp(*p, field)) |
| 418 | return 1; |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int is_valid_field(struct string_list_item *item, void *cb_data) |
| 423 | { |
| 424 | const char *field = item->string; |
| 425 | const char *config_key = (const char *)cb_data; |
| 426 | |
| 427 | if (!is_known_field(field)) { |
| 428 | warning(_("unsupported field '%s' in '%s' config"), field, config_key); |
| 429 | return 0; |
| 430 | } |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | static char *fields_from_config(struct string_list *fields_list, const char *config_key) |
| 435 | { |
| 436 | char *fields = NULL; |
| 437 | |
| 438 | if (!repo_config_get_string(the_repository, config_key, &fields) && *fields) { |
| 439 | string_list_split_in_place_f(fields_list, fields, ",", -1, |
| 440 | STRING_LIST_SPLIT_TRIM | |
| 441 | STRING_LIST_SPLIT_NONEMPTY); |
| 442 | filter_string_list(fields_list, 0, is_valid_field, (void *)config_key); |
| 443 | } |
| 444 | |
| 445 | return fields; |
| 446 | } |
| 447 | |
| 448 | static struct string_list *initialize_fields_list(struct string_list *fields_list, int *initialized, |
| 449 | const char *config_key) |
| 450 | { |
| 451 | if (!*initialized) { |
| 452 | fields_list->cmp = strcasecmp; |
| 453 | fields_from_config(fields_list, config_key); |
| 454 | *initialized = 1; |
| 455 | } |
| 456 | |
| 457 | return fields_list; |
| 458 | } |
| 459 | |
| 460 | static struct string_list *fields_sent(void) |
| 461 | { |
| 462 | static struct string_list fields_list = STRING_LIST_INIT_NODUP; |
| 463 | static int initialized; |
| 464 | |
| 465 | return initialize_fields_list(&fields_list, &initialized, "promisor.sendFields"); |
| 466 | } |
| 467 | |
| 468 | static struct string_list *fields_checked(void) |
| 469 | { |
| 470 | static struct string_list fields_list = STRING_LIST_INIT_NODUP; |
| 471 | static int initialized; |
| 472 | |
| 473 | return initialize_fields_list(&fields_list, &initialized, "promisor.checkFields"); |
| 474 | } |
| 475 | |
| 476 | static struct string_list *fields_stored(void) |
| 477 | { |
| 478 | static struct string_list fields_list = STRING_LIST_INIT_NODUP; |
| 479 | static int initialized; |
| 480 | |
| 481 | return initialize_fields_list(&fields_list, &initialized, "promisor.storeFields"); |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Struct for promisor remotes involved in the "promisor-remote" |
| 486 | * protocol capability. |
| 487 | * |
| 488 | * Except for "name" and "local_name", each <member> in this struct |
| 489 | * and its <value> should correspond (either on the client side or on |
| 490 | * the server side) to a "remote.<name>.<member>" config variable set |
| 491 | * to <value> where "<name>" is a promisor remote name. |
| 492 | */ |
| 493 | struct promisor_info { |
| 494 | const char *name; /* name the server advertised */ |
| 495 | const char *local_name; /* name used locally (may be auto-generated) */ |
| 496 | const char *url; |
| 497 | const char *filter; |
| 498 | const char *token; |
| 499 | }; |
| 500 | |
| 501 | static void promisor_info_free(struct promisor_info *p) |
| 502 | { |
| 503 | free((char *)p->name); |
| 504 | free((char *)p->local_name); |
| 505 | free((char *)p->url); |
| 506 | free((char *)p->filter); |
| 507 | free((char *)p->token); |
| 508 | free(p); |
| 509 | } |
| 510 | |
| 511 | static void promisor_info_list_clear(struct string_list *list) |
| 512 | { |
| 513 | for (size_t i = 0; i < list->nr; i++) |
| 514 | promisor_info_free(list->items[i].util); |
| 515 | string_list_clear(list, 0); |
| 516 | } |
| 517 | |
| 518 | static const char *promisor_info_local_name(struct promisor_info *p) |
| 519 | { |
| 520 | return p->local_name ? p->local_name : p->name; |
| 521 | } |
| 522 | |
| 523 | static void set_one_field(struct promisor_info *p, |
| 524 | const char *field, const char *value) |
| 525 | { |
| 526 | if (!strcasecmp(field, promisor_field_filter)) |
| 527 | p->filter = xstrdup(value); |
| 528 | else if (!strcasecmp(field, promisor_field_token)) |
| 529 | p->token = xstrdup(value); |
| 530 | else |
| 531 | BUG("invalid field '%s'", field); |
| 532 | } |
| 533 | |
| 534 | static void set_fields(struct promisor_info *p, |
| 535 | struct string_list *field_names) |
| 536 | { |
| 537 | struct string_list_item *item; |
| 538 | |
| 539 | for_each_string_list_item(item, field_names) { |
| 540 | char *key = xstrfmt("remote.%s.%s", p->name, item->string); |
| 541 | const char *val; |
| 542 | if (!repo_config_get_string_tmp(the_repository, key, &val) && *val) |
| 543 | set_one_field(p, item->string, val); |
| 544 | free(key); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * Populate 'list' with promisor remote information from the config. |
| 550 | * The 'util' pointer of each list item will hold a 'struct |
| 551 | * promisor_info'. Except "name" and "url", only members of that |
| 552 | * struct specified by the 'field_names' list are set (using values |
| 553 | * from the configuration). |
| 554 | */ |
| 555 | static void promisor_config_info_list(struct repository *repo, |
| 556 | struct string_list *list, |
| 557 | struct string_list *field_names) |
| 558 | { |
| 559 | struct promisor_remote *r; |
| 560 | |
| 561 | promisor_remote_init(repo); |
| 562 | |
| 563 | for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
| 564 | const char *url; |
| 565 | char *url_key = xstrfmt("remote.%s.url", r->name); |
| 566 | |
| 567 | /* Only add remotes with a non empty URL */ |
| 568 | if (!repo_config_get_string_tmp(the_repository, url_key, &url) && *url) { |
| 569 | struct promisor_info *new_info = xcalloc(1, sizeof(*new_info)); |
| 570 | struct string_list_item *item; |
| 571 | |
| 572 | new_info->name = xstrdup(r->name); |
| 573 | new_info->url = xstrdup(url); |
| 574 | |
| 575 | if (field_names) |
| 576 | set_fields(new_info, field_names); |
| 577 | |
| 578 | item = string_list_append(list, new_info->name); |
| 579 | item->util = new_info; |
| 580 | } |
| 581 | |
| 582 | free(url_key); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | char *promisor_remote_info(struct repository *repo) |
| 587 | { |
| 588 | struct strbuf sb = STRBUF_INIT; |
| 589 | int advertise_promisors = 0; |
| 590 | struct string_list config_info = STRING_LIST_INIT_NODUP; |
| 591 | struct string_list_item *item; |
| 592 | |
| 593 | repo_config_get_bool(the_repository, "promisor.advertise", &advertise_promisors); |
| 594 | |
| 595 | if (!advertise_promisors) |
| 596 | return NULL; |
| 597 | |
| 598 | promisor_config_info_list(repo, &config_info, fields_sent()); |
| 599 | |
| 600 | if (!config_info.nr) |
| 601 | return NULL; |
| 602 | |
| 603 | for_each_string_list_item(item, &config_info) { |
| 604 | struct promisor_info *p = item->util; |
| 605 | |
| 606 | if (item != config_info.items) |
| 607 | strbuf_addch(&sb, ';'); |
| 608 | |
| 609 | strbuf_addf(&sb, "%s=", promisor_field_name); |
| 610 | strbuf_addstr_urlencode(&sb, p->name, allow_unsanitized); |
| 611 | strbuf_addf(&sb, ",%s=", promisor_field_url); |
| 612 | strbuf_addstr_urlencode(&sb, p->url, allow_unsanitized); |
| 613 | |
| 614 | if (p->filter) { |
| 615 | strbuf_addf(&sb, ",%s=", promisor_field_filter); |
| 616 | strbuf_addstr_urlencode(&sb, p->filter, allow_unsanitized); |
| 617 | } |
| 618 | if (p->token) { |
| 619 | strbuf_addf(&sb, ",%s=", promisor_field_token); |
| 620 | strbuf_addstr_urlencode(&sb, p->token, allow_unsanitized); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | promisor_info_list_clear(&config_info); |
| 625 | |
| 626 | return strbuf_detach(&sb, NULL); |
| 627 | } |
| 628 | |
| 629 | enum accept_promisor { |
| 630 | ACCEPT_NONE = 0, |
| 631 | ACCEPT_KNOWN_URL, |
| 632 | ACCEPT_KNOWN_NAME, |
| 633 | ACCEPT_ALL |
| 634 | }; |
| 635 | |
| 636 | /* |
| 637 | * Check if a specific field and its advertised value match the local |
| 638 | * configuration of a given promisor remote. |
| 639 | * |
| 640 | * Returns 1 if they match, 0 otherwise. |
| 641 | */ |
| 642 | static int match_field_against_config(const char *field, const char *value, |
| 643 | struct promisor_info *config_info) |
| 644 | { |
| 645 | if (config_info->filter && !strcasecmp(field, promisor_field_filter)) |
| 646 | return !strcmp(config_info->filter, value); |
| 647 | else if (config_info->token && !strcasecmp(field, promisor_field_token)) |
| 648 | return !strcmp(config_info->token, value); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Check that the advertised fields match the local configuration. |
| 655 | * |
| 656 | * When 'config_entry' is NULL (ACCEPT_ALL mode), every checked field |
| 657 | * must match at least one remote in 'config_info'. |
| 658 | * |
| 659 | * When 'config_entry' points to a specific remote's config, the |
| 660 | * checked fields are compared against that single remote only. |
| 661 | */ |
| 662 | static int all_fields_match(struct promisor_info *advertised, |
| 663 | struct string_list *config_info, |
| 664 | struct promisor_info *config_entry) |
| 665 | { |
| 666 | struct string_list *fields = fields_checked(); |
| 667 | struct string_list_item *item_checked; |
| 668 | |
| 669 | for_each_string_list_item(item_checked, fields) { |
| 670 | int match = 0; |
| 671 | const char *field = item_checked->string; |
| 672 | const char *value = NULL; |
| 673 | |
| 674 | if (!strcasecmp(field, promisor_field_filter)) |
| 675 | value = advertised->filter; |
| 676 | else if (!strcasecmp(field, promisor_field_token)) |
| 677 | value = advertised->token; |
| 678 | |
| 679 | if (!value) |
| 680 | return 0; |
| 681 | |
| 682 | if (config_entry) { |
| 683 | match = match_field_against_config(field, value, |
| 684 | config_entry); |
| 685 | } else { |
| 686 | struct string_list_item *item; |
| 687 | for_each_string_list_item(item, config_info) { |
| 688 | struct promisor_info *p = item->util; |
| 689 | if (match_field_against_config(field, value, p)) { |
| 690 | match = 1; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (!match) |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | static bool has_control_char(const char *s) |
| 704 | { |
| 705 | for (const char *c = s; *c; c++) |
| 706 | if (iscntrl(*c)) |
| 707 | return true; |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | struct allowed_url { |
| 712 | char *remote_name; |
| 713 | char *url_pattern; |
| 714 | struct url_info pattern_info; |
| 715 | }; |
| 716 | |
| 717 | static void allowed_url_free(void *util, const char *str UNUSED) |
| 718 | { |
| 719 | struct allowed_url *allowed = util; |
| 720 | |
| 721 | if (!allowed) |
| 722 | return; |
| 723 | |
| 724 | /* Depending on prefix, free either remote_name or url_pattern */ |
| 725 | free(allowed->remote_name ? allowed->remote_name : allowed->url_pattern); |
| 726 | free(allowed->pattern_info.url); |
| 727 | free(allowed); |
| 728 | } |
| 729 | |
| 730 | static struct allowed_url *valid_accept_url(const char *url) |
| 731 | { |
| 732 | char *dup, *p; |
| 733 | struct allowed_url *allowed; |
| 734 | |
| 735 | if (!url) |
| 736 | return NULL; |
| 737 | |
| 738 | dup = xstrdup(url); |
| 739 | p = strchr(dup, '='); |
| 740 | if (p) { |
| 741 | *p = '\0'; |
| 742 | if (!valid_remote_name(dup)) { |
| 743 | warning(_("invalid remote name '%s' before '=' sign " |
| 744 | "in '%s' from promisor.acceptFromServerUrl config"), |
| 745 | dup, url); |
| 746 | free(dup); |
| 747 | return NULL; |
| 748 | } |
| 749 | p++; |
| 750 | } else { |
| 751 | p = dup; |
| 752 | } |
| 753 | |
| 754 | if (has_control_char(p)) { |
| 755 | warning(_("invalid url pattern '%s' " |
| 756 | "in '%s' from promisor.acceptFromServerUrl config"), p, url); |
| 757 | free(dup); |
| 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | allowed = xmalloc(sizeof(*allowed)); |
| 762 | allowed->remote_name = (p == dup) ? NULL : dup; |
| 763 | allowed->url_pattern = p; |
| 764 | allowed->pattern_info.url = url_normalize_pattern(p, &allowed->pattern_info); |
| 765 | if (!allowed->pattern_info.url) { |
| 766 | warning(_("invalid url pattern '%s' " |
| 767 | "in '%s' from promisor.acceptFromServerUrl config"), p, url); |
| 768 | free(dup); |
| 769 | free(allowed); |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | return allowed; |
| 774 | } |
| 775 | |
| 776 | static void load_accept_from_server_url(struct repository *repo, |
| 777 | struct string_list *accept_urls) |
| 778 | { |
| 779 | const struct string_list *config_urls; |
| 780 | |
| 781 | if (!repo_config_get_string_multi(repo, "promisor.acceptfromserverurl", &config_urls)) { |
| 782 | struct string_list_item *item; |
| 783 | |
| 784 | for_each_string_list_item(item, config_urls) { |
| 785 | struct allowed_url *allowed = valid_accept_url(item->string); |
| 786 | if (allowed) { |
| 787 | struct string_list_item *new; |
| 788 | new = string_list_append(accept_urls, item->string); |
| 789 | new->util = allowed; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static bool match_pattern_url(const char *pat, size_t pat_len, |
| 796 | const char *url, size_t url_len) |
| 797 | { |
| 798 | char *p_str = xstrndup(pat, pat_len); |
| 799 | char *u_str = xstrndup(url, url_len); |
| 800 | bool res = !wildmatch(p_str, u_str, 0); |
| 801 | |
| 802 | free(p_str); |
| 803 | free(u_str); |
| 804 | |
| 805 | return res; |
| 806 | } |
| 807 | |
| 808 | static bool match_one_url(const struct url_info *pi, const struct url_info *ui) |
| 809 | { |
| 810 | const char *pat = pi->url; |
| 811 | const char *url = ui->url; |
| 812 | |
| 813 | /* |
| 814 | * Schemes must match exactly. They are case-folded by |
| 815 | * url_normalize(), so strncmp() suffices. |
| 816 | */ |
| 817 | if (pi->scheme_len != ui->scheme_len || strncmp(pat, url, pi->scheme_len)) |
| 818 | return false; |
| 819 | |
| 820 | /* |
| 821 | * Ports must match exactly. url_normalize() strips default |
| 822 | * ports (like 443 for https), so length and content |
| 823 | * comparisons are sufficient. |
| 824 | */ |
| 825 | if (pi->port_len != ui->port_len || |
| 826 | strncmp(pat + pi->port_off, url + ui->port_off, pi->port_len)) |
| 827 | return false; |
| 828 | |
| 829 | /* |
| 830 | * Match host and path separately to prevent a '*' in the host |
| 831 | * portion of the pattern from matching across the '/' |
| 832 | * boundary into the path. |
| 833 | */ |
| 834 | |
| 835 | return match_pattern_url(pat + pi->host_off, pi->host_len, |
| 836 | url + ui->host_off, ui->host_len) && |
| 837 | match_pattern_url(pat + pi->path_off, pi->path_len, |
| 838 | url + ui->path_off, ui->path_len); |
| 839 | } |
| 840 | |
| 841 | static struct allowed_url *url_matches_accept_list( |
| 842 | struct string_list *accept_urls, const char *url) |
| 843 | { |
| 844 | struct string_list_item *item; |
| 845 | struct url_info url_info; |
| 846 | |
| 847 | url_info.url = url_normalize(url, &url_info); |
| 848 | |
| 849 | if (!url_info.url) |
| 850 | return NULL; |
| 851 | |
| 852 | for_each_string_list_item(item, accept_urls) { |
| 853 | struct allowed_url *allowed = item->util; |
| 854 | |
| 855 | if (match_one_url(&allowed->pattern_info, &url_info)) { |
| 856 | free(url_info.url); |
| 857 | return allowed; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | free(url_info.url); |
| 862 | return NULL; |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * Sanitize the buffer to make it a valid remote name coming from the |
| 867 | * server by: |
| 868 | * |
| 869 | * - replacing any non alphanumeric character with a '-' |
| 870 | * - stripping any leading '-', |
| 871 | * - condensing multiple '-' into one, |
| 872 | * - prepending "promisor-auto-", |
| 873 | * - validating the result. |
| 874 | */ |
| 875 | static int sanitize_remote_name(struct strbuf *buf, const char *url) |
| 876 | { |
| 877 | char prev = '-'; |
| 878 | for (size_t i = 0; i < buf->len; ) { |
| 879 | if (!isalnum(buf->buf[i])) |
| 880 | buf->buf[i] = '-'; |
| 881 | if (prev == '-' && buf->buf[i] == '-') { |
| 882 | strbuf_remove(buf, i, 1); |
| 883 | } else { |
| 884 | prev = buf->buf[i]; |
| 885 | i++; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | strbuf_strip_suffix(buf, "-"); |
| 890 | |
| 891 | if (!buf->len) { |
| 892 | warning(_("couldn't generate a valid remote name from " |
| 893 | "advertised url '%s', ignoring this remote"), url); |
| 894 | return -1; |
| 895 | } |
| 896 | |
| 897 | strbuf_insertstr(buf, 0, "promisor-auto-"); |
| 898 | |
| 899 | if (!valid_remote_name(buf->buf)) { |
| 900 | warning(_("generated remote name '%s' from advertised url '%s' " |
| 901 | "is invalid, ignoring this remote"), buf->buf, url); |
| 902 | return -1; |
| 903 | } |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static char *promisor_remote_name_from_url(const char *url) |
| 909 | { |
| 910 | struct url_info url_info = { 0 }; |
| 911 | char *normalized = url_normalize(url, &url_info); |
| 912 | struct strbuf buf = STRBUF_INIT; |
| 913 | |
| 914 | if (!normalized) { |
| 915 | warning(_("couldn't normalize advertised url '%s', " |
| 916 | "ignoring this remote"), url); |
| 917 | return NULL; |
| 918 | } |
| 919 | |
| 920 | if (url_info.host_len) { |
| 921 | strbuf_add(&buf, normalized + url_info.host_off, url_info.host_len); |
| 922 | strbuf_addch(&buf, '-'); |
| 923 | } |
| 924 | |
| 925 | if (url_info.port_len) { |
| 926 | strbuf_add(&buf, normalized + url_info.port_off, url_info.port_len); |
| 927 | strbuf_addch(&buf, '-'); |
| 928 | } |
| 929 | |
| 930 | if (url_info.path_len) { |
| 931 | strbuf_add(&buf, normalized + url_info.path_off, url_info.path_len); |
| 932 | strbuf_trim_trailing_dir_sep(&buf); |
| 933 | strbuf_strip_suffix(&buf, ".git"); |
| 934 | } |
| 935 | |
| 936 | free(normalized); |
| 937 | |
| 938 | if (sanitize_remote_name(&buf, url)) { |
| 939 | strbuf_release(&buf); |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | return strbuf_detach(&buf, NULL); |
| 944 | } |
| 945 | |
| 946 | static void configure_auto_promisor_remote(struct repository *repo, |
| 947 | const char *name, |
| 948 | const char *url, |
| 949 | const char *advertised_as, |
| 950 | bool reuse) |
| 951 | { |
| 952 | char *key; |
| 953 | |
| 954 | if (!reuse) { |
| 955 | fprintf(stderr, _("Auto-creating promisor remote '%s' for URL '%s'\n"), |
| 956 | name, url); |
| 957 | |
| 958 | key = xstrfmt("remote.%s.url", name); |
| 959 | repo_config_set_gently(repo, key, url); |
| 960 | free(key); |
| 961 | } |
| 962 | |
| 963 | /* NB: when reusing, this promotes an existing non-promisor remote */ |
| 964 | key = xstrfmt("remote.%s.promisor", name); |
| 965 | repo_config_set_gently(repo, key, "true"); |
| 966 | free(key); |
| 967 | |
| 968 | if (advertised_as) { |
| 969 | key = xstrfmt("remote.%s.advertisedAs", name); |
| 970 | repo_config_set_gently(repo, key, advertised_as); |
| 971 | free(key); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | #define MAX_REMOTES_WITH_SIMILAR_NAMES 20 |
| 976 | |
| 977 | /* Return the allocated local name, or NULL on failure */ |
| 978 | static char *handle_matching_allowed_url(struct repository *repo, |
| 979 | char *allowed_name, |
| 980 | const char *remote_url, |
| 981 | const char *remote_name) |
| 982 | { |
| 983 | char *name; |
| 984 | char *basename = allowed_name ? |
| 985 | xstrdup(allowed_name) : |
| 986 | promisor_remote_name_from_url(remote_url); |
| 987 | int i = 0; |
| 988 | bool reuse = false; |
| 989 | |
| 990 | if (!basename) |
| 991 | return NULL; |
| 992 | |
| 993 | name = xstrdup(basename); |
| 994 | |
| 995 | while (i < MAX_REMOTES_WITH_SIMILAR_NAMES) { |
| 996 | char *url_key = xstrfmt("remote.%s.url", name); |
| 997 | const char *existing_url; |
| 998 | int exists = !repo_config_get_string_tmp(repo, url_key, &existing_url); |
| 999 | |
| 1000 | free(url_key); |
| 1001 | |
| 1002 | if (!exists) |
| 1003 | break; /* Free to use */ |
| 1004 | |
| 1005 | if (!strcmp(existing_url, remote_url)) { |
| 1006 | reuse = true; |
| 1007 | break; /* Same URL, so safe to reuse */ |
| 1008 | } |
| 1009 | |
| 1010 | i++; |
| 1011 | free(name); |
| 1012 | name = xstrfmt("%s-%d", basename, i); |
| 1013 | } |
| 1014 | |
| 1015 | if (i < MAX_REMOTES_WITH_SIMILAR_NAMES) { |
| 1016 | configure_auto_promisor_remote(repo, name, |
| 1017 | remote_url, remote_name, |
| 1018 | reuse); |
| 1019 | } else { |
| 1020 | warning(_("too many remotes accepted with name like '%s-X', " |
| 1021 | "ignoring this remote"), basename); |
| 1022 | FREE_AND_NULL(name); |
| 1023 | } |
| 1024 | |
| 1025 | free(basename); |
| 1026 | return name; |
| 1027 | } |
| 1028 | |
| 1029 | static int should_accept_new_remote_url(struct repository *repo, |
| 1030 | struct string_list *accept_urls, |
| 1031 | struct promisor_info *advertised) |
| 1032 | { |
| 1033 | struct allowed_url *allowed = url_matches_accept_list(accept_urls, |
| 1034 | advertised->url); |
| 1035 | if (allowed) { |
| 1036 | char *name = handle_matching_allowed_url(repo, |
| 1037 | allowed->remote_name, |
| 1038 | advertised->url, |
| 1039 | advertised->name); |
| 1040 | if (name) { |
| 1041 | free((char *)advertised->local_name); |
| 1042 | advertised->local_name = name; |
| 1043 | return 1; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static int should_accept_remote(struct repository *repo, |
| 1051 | enum accept_promisor accept, |
| 1052 | struct promisor_info *advertised, |
| 1053 | struct string_list *accept_urls, |
| 1054 | struct string_list *config_info, |
| 1055 | bool *reload_config) |
| 1056 | { |
| 1057 | struct promisor_info *p; |
| 1058 | struct string_list_item *item; |
| 1059 | const char *remote_name = advertised->name; |
| 1060 | const char *remote_url = advertised->url; |
| 1061 | |
| 1062 | if (!remote_url || !*remote_url) |
| 1063 | BUG("no or empty URL advertised for remote '%s'; " |
| 1064 | "this remote should have been rejected earlier", |
| 1065 | remote_name); |
| 1066 | |
| 1067 | /* Get config info for that promisor remote */ |
| 1068 | item = string_list_lookup(config_info, remote_name); |
| 1069 | |
| 1070 | if (!item) { |
| 1071 | /* We don't know about that remote */ |
| 1072 | |
| 1073 | int res = should_accept_new_remote_url(repo, accept_urls, advertised); |
| 1074 | if (res) { |
| 1075 | *reload_config = true; |
| 1076 | return res; |
| 1077 | } |
| 1078 | |
| 1079 | if (accept == ACCEPT_ALL) |
| 1080 | return all_fields_match(advertised, config_info, NULL); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | p = item->util; |
| 1085 | |
| 1086 | /* Known remote in the allowlist? */ |
| 1087 | if (!strcmp(p->url, remote_url) && url_matches_accept_list(accept_urls, remote_url)) |
| 1088 | return all_fields_match(advertised, config_info, p); |
| 1089 | |
| 1090 | if (accept == ACCEPT_ALL) |
| 1091 | return all_fields_match(advertised, config_info, NULL); |
| 1092 | |
| 1093 | if (accept == ACCEPT_KNOWN_NAME) |
| 1094 | return all_fields_match(advertised, config_info, p); |
| 1095 | |
| 1096 | if (strcmp(p->url, remote_url)) { |
| 1097 | warning(_("known remote named '%s' but with URL '%s' instead of '%s', " |
| 1098 | "ignoring this remote"), |
| 1099 | remote_name, p->url, remote_url); |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | if (accept == ACCEPT_KNOWN_URL) |
| 1104 | return all_fields_match(advertised, config_info, p); |
| 1105 | |
| 1106 | if (accept != ACCEPT_NONE) |
| 1107 | BUG("Unhandled 'enum accept_promisor' value '%d'", accept); |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static int skip_field_name_prefix(const char *elem, const char *field_name, const char **value) |
| 1113 | { |
| 1114 | const char *p; |
| 1115 | if (!skip_prefix(elem, field_name, &p) || *p != '=') |
| 1116 | return 0; |
| 1117 | *value = p + 1; |
| 1118 | return 1; |
| 1119 | } |
| 1120 | |
| 1121 | static struct promisor_info *parse_one_advertised_remote(const char *remote_info) |
| 1122 | { |
| 1123 | struct promisor_info *info = xcalloc(1, sizeof(*info)); |
| 1124 | struct string_list elem_list = STRING_LIST_INIT_DUP; |
| 1125 | struct string_list_item *item; |
| 1126 | |
| 1127 | string_list_split(&elem_list, remote_info, ",", -1); |
| 1128 | |
| 1129 | for_each_string_list_item(item, &elem_list) { |
| 1130 | const char *elem = item->string; |
| 1131 | const char *p = strchr(elem, '='); |
| 1132 | |
| 1133 | if (!p) { |
| 1134 | warning(_("invalid element '%s' from remote info"), elem); |
| 1135 | continue; |
| 1136 | } |
| 1137 | |
| 1138 | if (skip_field_name_prefix(elem, promisor_field_name, &p)) |
| 1139 | info->name = url_percent_decode(p); |
| 1140 | else if (skip_field_name_prefix(elem, promisor_field_url, &p)) |
| 1141 | info->url = url_percent_decode(p); |
| 1142 | else if (skip_field_name_prefix(elem, promisor_field_filter, &p)) |
| 1143 | info->filter = url_percent_decode(p); |
| 1144 | else if (skip_field_name_prefix(elem, promisor_field_token, &p)) |
| 1145 | info->token = url_percent_decode(p); |
| 1146 | } |
| 1147 | |
| 1148 | string_list_clear(&elem_list, 0); |
| 1149 | |
| 1150 | if (!info->name || !*info->name || !info->url || !*info->url) { |
| 1151 | warning(_("server advertised a promisor remote without a name or URL: '%s', " |
| 1152 | "ignoring this remote"), remote_info); |
| 1153 | promisor_info_free(info); |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
| 1157 | return info; |
| 1158 | } |
| 1159 | |
| 1160 | static bool store_one_field(struct repository *repo, const char *remote_name, |
| 1161 | const char *field_name, const char *field_key, |
| 1162 | const char *advertised, const char *current) |
| 1163 | { |
| 1164 | if (advertised && (!current || strcmp(current, advertised))) { |
| 1165 | char *key = xstrfmt("remote.%s.%s", remote_name, field_key); |
| 1166 | |
| 1167 | fprintf(stderr, _("Storing new %s from server for remote '%s'.\n" |
| 1168 | " '%s' -> '%s'\n"), |
| 1169 | field_name, remote_name, |
| 1170 | current ? current : "", |
| 1171 | advertised); |
| 1172 | |
| 1173 | repo_config_set_gently(repo, key, advertised); |
| 1174 | free(key); |
| 1175 | |
| 1176 | return true; |
| 1177 | } |
| 1178 | |
| 1179 | return false; |
| 1180 | } |
| 1181 | |
| 1182 | /* Check that a filter is valid by parsing it */ |
| 1183 | static bool valid_filter(const char *filter, const char *remote_name) |
| 1184 | { |
| 1185 | struct list_objects_filter_options filter_opts = LIST_OBJECTS_FILTER_INIT; |
| 1186 | struct strbuf err = STRBUF_INIT; |
| 1187 | int res = gently_parse_list_objects_filter(&filter_opts, filter, &err); |
| 1188 | |
| 1189 | if (res) |
| 1190 | warning(_("invalid filter '%s' for remote '%s' " |
| 1191 | "will not be stored: %s"), |
| 1192 | filter, remote_name, err.buf); |
| 1193 | |
| 1194 | list_objects_filter_release(&filter_opts); |
| 1195 | strbuf_release(&err); |
| 1196 | |
| 1197 | return !res; |
| 1198 | } |
| 1199 | |
| 1200 | static bool valid_token(const char *token, const char *remote_name) |
| 1201 | { |
| 1202 | if (has_control_char(token)) { |
| 1203 | warning(_("invalid token '%s' for remote '%s' " |
| 1204 | "will not be stored"), |
| 1205 | token, remote_name); |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | return true; |
| 1210 | } |
| 1211 | |
| 1212 | struct store_info { |
| 1213 | struct repository *repo; |
| 1214 | struct string_list config_info; |
| 1215 | bool store_filter; |
| 1216 | bool store_token; |
| 1217 | }; |
| 1218 | |
| 1219 | static struct store_info *store_info_new(struct repository *repo) |
| 1220 | { |
| 1221 | struct string_list *fields_to_store = fields_stored(); |
| 1222 | struct store_info *s = xmalloc(sizeof(*s)); |
| 1223 | |
| 1224 | s->repo = repo; |
| 1225 | |
| 1226 | string_list_init_nodup(&s->config_info); |
| 1227 | promisor_config_info_list(repo, &s->config_info, fields_to_store); |
| 1228 | string_list_sort(&s->config_info); |
| 1229 | |
| 1230 | s->store_filter = !!string_list_lookup(fields_to_store, promisor_field_filter); |
| 1231 | s->store_token = !!string_list_lookup(fields_to_store, promisor_field_token); |
| 1232 | |
| 1233 | return s; |
| 1234 | } |
| 1235 | |
| 1236 | static void store_info_free(struct store_info *s) |
| 1237 | { |
| 1238 | if (s) { |
| 1239 | promisor_info_list_clear(&s->config_info); |
| 1240 | free(s); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | static bool promisor_store_advertised_fields(struct promisor_info *advertised, |
| 1245 | struct store_info *store_info) |
| 1246 | { |
| 1247 | struct promisor_info *p; |
| 1248 | struct string_list_item *item; |
| 1249 | const char *remote_name = promisor_info_local_name(advertised); |
| 1250 | bool reload_config = false; |
| 1251 | |
| 1252 | if (!(store_info->store_filter || store_info->store_token)) |
| 1253 | return false; |
| 1254 | |
| 1255 | /* |
| 1256 | * Get existing config info for the advertised promisor |
| 1257 | * remote. This ensures the remote is already configured on |
| 1258 | * the client side. |
| 1259 | */ |
| 1260 | item = string_list_lookup(&store_info->config_info, remote_name); |
| 1261 | |
| 1262 | if (!item) |
| 1263 | return false; |
| 1264 | |
| 1265 | p = item->util; |
| 1266 | |
| 1267 | if (store_info->store_filter && advertised->filter && |
| 1268 | valid_filter(advertised->filter, remote_name)) |
| 1269 | reload_config |= store_one_field(store_info->repo, remote_name, |
| 1270 | "filter", promisor_field_filter, |
| 1271 | advertised->filter, p->filter); |
| 1272 | |
| 1273 | if (store_info->store_token && advertised->token && |
| 1274 | valid_token(advertised->token, remote_name)) |
| 1275 | reload_config |= store_one_field(store_info->repo, remote_name, |
| 1276 | "token", promisor_field_token, |
| 1277 | advertised->token, p->token); |
| 1278 | |
| 1279 | return reload_config; |
| 1280 | } |
| 1281 | |
| 1282 | static enum accept_promisor accept_from_server(struct repository *repo) |
| 1283 | { |
| 1284 | const char *accept_str; |
| 1285 | enum accept_promisor accept = ACCEPT_NONE; |
| 1286 | |
| 1287 | if (!repo_config_get_string_tmp(repo, "promisor.acceptfromserver", &accept_str)) { |
| 1288 | if (!*accept_str || !strcasecmp("None", accept_str)) |
| 1289 | accept = ACCEPT_NONE; |
| 1290 | else if (!strcasecmp("KnownUrl", accept_str)) |
| 1291 | accept = ACCEPT_KNOWN_URL; |
| 1292 | else if (!strcasecmp("KnownName", accept_str)) |
| 1293 | accept = ACCEPT_KNOWN_NAME; |
| 1294 | else if (!strcasecmp("All", accept_str)) |
| 1295 | accept = ACCEPT_ALL; |
| 1296 | else |
| 1297 | warning(_("unknown '%s' value for '%s' config option"), |
| 1298 | accept_str, "promisor.acceptfromserver"); |
| 1299 | } |
| 1300 | |
| 1301 | return accept; |
| 1302 | } |
| 1303 | |
| 1304 | static void filter_promisor_remote(struct repository *repo, |
| 1305 | struct string_list *accepted_remotes, |
| 1306 | const char *info) |
| 1307 | { |
| 1308 | struct string_list config_info = STRING_LIST_INIT_NODUP; |
| 1309 | struct string_list remote_info = STRING_LIST_INIT_DUP; |
| 1310 | struct store_info *store_info = NULL; |
| 1311 | struct string_list_item *item; |
| 1312 | bool reload_config = false; |
| 1313 | enum accept_promisor accept = accept_from_server(repo); |
| 1314 | struct string_list accept_urls = STRING_LIST_INIT_DUP; |
| 1315 | |
| 1316 | /* Load and validate the acceptFromServerUrl config */ |
| 1317 | load_accept_from_server_url(repo, &accept_urls); |
| 1318 | |
| 1319 | if (accept == ACCEPT_NONE && !accept_urls.nr) |
| 1320 | return; |
| 1321 | |
| 1322 | /* Parse remote info received */ |
| 1323 | |
| 1324 | string_list_split(&remote_info, info, ";", -1); |
| 1325 | |
| 1326 | for_each_string_list_item(item, &remote_info) { |
| 1327 | struct promisor_info *advertised; |
| 1328 | |
| 1329 | advertised = parse_one_advertised_remote(item->string); |
| 1330 | |
| 1331 | if (!advertised) |
| 1332 | continue; |
| 1333 | |
| 1334 | if (!config_info.nr) { |
| 1335 | promisor_config_info_list(repo, &config_info, fields_checked()); |
| 1336 | string_list_sort(&config_info); |
| 1337 | } |
| 1338 | |
| 1339 | if (should_accept_remote(repo, accept, advertised, &accept_urls, |
| 1340 | &config_info, &reload_config)) { |
| 1341 | if (!store_info) |
| 1342 | store_info = store_info_new(repo); |
| 1343 | if (promisor_store_advertised_fields(advertised, store_info)) |
| 1344 | reload_config = true; |
| 1345 | |
| 1346 | string_list_append(accepted_remotes, advertised->name)->util = advertised; |
| 1347 | } else { |
| 1348 | promisor_info_free(advertised); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | string_list_clear_func(&accept_urls, allowed_url_free); |
| 1353 | promisor_info_list_clear(&config_info); |
| 1354 | string_list_clear(&remote_info, 0); |
| 1355 | store_info_free(store_info); |
| 1356 | |
| 1357 | if (reload_config) |
| 1358 | repo_promisor_remote_reinit(repo); |
| 1359 | |
| 1360 | /* Apply accepted remotes to the stable repo state */ |
| 1361 | for_each_string_list_item(item, accepted_remotes) { |
| 1362 | struct promisor_info *info = item->util; |
| 1363 | const char *remote_name = promisor_info_local_name(info); |
| 1364 | struct promisor_remote *r = repo_promisor_remote_find(repo, remote_name); |
| 1365 | |
| 1366 | if (r) { |
| 1367 | r->accepted = 1; |
| 1368 | if (info->filter) { |
| 1369 | free(r->advertised_filter); |
| 1370 | r->advertised_filter = xstrdup(info->filter); |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | void promisor_remote_reply(const char *info, char **accepted_out) |
| 1377 | { |
| 1378 | struct string_list accepted_remotes = STRING_LIST_INIT_NODUP; |
| 1379 | |
| 1380 | filter_promisor_remote(the_repository, &accepted_remotes, info); |
| 1381 | |
| 1382 | if (accepted_out) { |
| 1383 | if (accepted_remotes.nr) { |
| 1384 | struct strbuf reply = STRBUF_INIT; |
| 1385 | struct string_list_item *item; |
| 1386 | |
| 1387 | for_each_string_list_item(item, &accepted_remotes) { |
| 1388 | if (reply.len) |
| 1389 | strbuf_addch(&reply, ';'); |
| 1390 | strbuf_addstr_urlencode(&reply, item->string, allow_unsanitized); |
| 1391 | } |
| 1392 | *accepted_out = strbuf_detach(&reply, NULL); |
| 1393 | } else { |
| 1394 | *accepted_out = NULL; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | promisor_info_list_clear(&accepted_remotes); |
| 1399 | } |
| 1400 | |
| 1401 | void mark_promisor_remotes_as_accepted(struct repository *r, const char *remotes) |
| 1402 | { |
| 1403 | struct string_list accepted_remotes = STRING_LIST_INIT_DUP; |
| 1404 | struct string_list_item *item; |
| 1405 | |
| 1406 | string_list_split(&accepted_remotes, remotes, ";", -1); |
| 1407 | |
| 1408 | for_each_string_list_item(item, &accepted_remotes) { |
| 1409 | char *decoded_remote = url_percent_decode(item->string); |
| 1410 | struct promisor_remote *p = repo_promisor_remote_find(r, decoded_remote); |
| 1411 | |
| 1412 | if (p) |
| 1413 | p->accepted = 1; |
| 1414 | else |
| 1415 | warning(_("accepted promisor remote '%s' not found"), |
| 1416 | decoded_remote); |
| 1417 | |
| 1418 | free(decoded_remote); |
| 1419 | } |
| 1420 | |
| 1421 | string_list_clear(&accepted_remotes, 0); |
| 1422 | } |
| 1423 | |
| 1424 | char *promisor_remote_construct_filter(struct repository *repo) |
| 1425 | { |
| 1426 | struct promisor_remote *r; |
| 1427 | struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT; |
| 1428 | struct strbuf err = STRBUF_INIT; |
| 1429 | char *result = NULL; |
| 1430 | |
| 1431 | promisor_remote_init(repo); |
| 1432 | |
| 1433 | for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
| 1434 | if (r->accepted && r->advertised_filter) |
| 1435 | if (gently_parse_list_objects_filter(&filter_options, |
| 1436 | r->advertised_filter, |
| 1437 | &err)) { |
| 1438 | warning(_("promisor remote '%s' advertised invalid filter '%s': %s"), |
| 1439 | r->name, r->advertised_filter, err.buf); |
| 1440 | strbuf_reset(&err); |
| 1441 | continue; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (filter_options.choice) |
| 1446 | result = xstrdup(expand_list_objects_filter_spec(&filter_options)); |
| 1447 | |
| 1448 | list_objects_filter_release(&filter_options); |
| 1449 | strbuf_release(&err); |
| 1450 | |
| 1451 | return result; |
| 1452 | } |