|
11 | 11 | #include "strvec.h" |
12 | 12 | #include "packfile.h" |
13 | 13 | #include "environment.h" |
| 14 | +#include "url.h" |
14 | 15 |
|
15 | 16 | struct promisor_remote_config { |
16 | 17 | struct promisor_remote *promisors; |
@@ -219,6 +220,18 @@ int repo_has_promisor_remote(struct repository *r) |
219 | 220 | return !!repo_promisor_remote_find(r, NULL); |
220 | 221 | } |
221 | 222 |
|
| 223 | +int repo_has_accepted_promisor_remote(struct repository *r) |
| 224 | +{ |
| 225 | + struct promisor_remote *p; |
| 226 | + |
| 227 | + promisor_remote_init(r); |
| 228 | + |
| 229 | + for (p = r->promisor_remote_config->promisors; p; p = p->next) |
| 230 | + if (p->accepted) |
| 231 | + return 1; |
| 232 | + return 0; |
| 233 | +} |
| 234 | + |
222 | 235 | static int remove_fetched_oids(struct repository *repo, |
223 | 236 | struct object_id **oids, |
224 | 237 | int oid_nr, int to_free) |
@@ -290,3 +303,234 @@ void promisor_remote_get_direct(struct repository *repo, |
290 | 303 | if (to_free) |
291 | 304 | free(remaining_oids); |
292 | 305 | } |
| 306 | + |
| 307 | +static int allow_unsanitized(char ch) |
| 308 | +{ |
| 309 | + if (ch == ',' || ch == ';' || ch == '%') |
| 310 | + return 0; |
| 311 | + return ch > 32 && ch < 127; |
| 312 | +} |
| 313 | + |
| 314 | +static void promisor_info_vecs(struct repository *repo, |
| 315 | + struct strvec *names, |
| 316 | + struct strvec *urls) |
| 317 | +{ |
| 318 | + struct promisor_remote *r; |
| 319 | + |
| 320 | + promisor_remote_init(repo); |
| 321 | + |
| 322 | + for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
| 323 | + char *url; |
| 324 | + char *url_key = xstrfmt("remote.%s.url", r->name); |
| 325 | + |
| 326 | + strvec_push(names, r->name); |
| 327 | + strvec_push(urls, git_config_get_string(url_key, &url) ? NULL : url); |
| 328 | + |
| 329 | + free(url); |
| 330 | + free(url_key); |
| 331 | + } |
| 332 | +} |
| 333 | + |
| 334 | +char *promisor_remote_info(struct repository *repo) |
| 335 | +{ |
| 336 | + struct strbuf sb = STRBUF_INIT; |
| 337 | + int advertise_promisors = 0; |
| 338 | + struct strvec names = STRVEC_INIT; |
| 339 | + struct strvec urls = STRVEC_INIT; |
| 340 | + |
| 341 | + git_config_get_bool("promisor.advertise", &advertise_promisors); |
| 342 | + |
| 343 | + if (!advertise_promisors) |
| 344 | + return NULL; |
| 345 | + |
| 346 | + promisor_info_vecs(repo, &names, &urls); |
| 347 | + |
| 348 | + if (!names.nr) |
| 349 | + return NULL; |
| 350 | + |
| 351 | + for (size_t i = 0; i < names.nr; i++) { |
| 352 | + if (i) |
| 353 | + strbuf_addch(&sb, ';'); |
| 354 | + strbuf_addstr(&sb, "name="); |
| 355 | + strbuf_addstr_urlencode(&sb, names.v[i], allow_unsanitized); |
| 356 | + if (urls.v[i]) { |
| 357 | + strbuf_addstr(&sb, ",url="); |
| 358 | + strbuf_addstr_urlencode(&sb, urls.v[i], allow_unsanitized); |
| 359 | + } |
| 360 | + } |
| 361 | + |
| 362 | + strbuf_sanitize(&sb); |
| 363 | + |
| 364 | + strvec_clear(&names); |
| 365 | + strvec_clear(&urls); |
| 366 | + |
| 367 | + return strbuf_detach(&sb, NULL); |
| 368 | +} |
| 369 | + |
| 370 | +/* |
| 371 | + * Find first index of 'vec' where there is 'val'. 'val' is compared |
| 372 | + * case insensively to the strings in 'vec'. If not found 'vec->nr' is |
| 373 | + * returned. |
| 374 | + */ |
| 375 | +static size_t strvec_find_index(struct strvec *vec, const char *val) |
| 376 | +{ |
| 377 | + for (size_t i = 0; i < vec->nr; i++) |
| 378 | + if (!strcasecmp(vec->v[i], val)) |
| 379 | + return i; |
| 380 | + return vec->nr; |
| 381 | +} |
| 382 | + |
| 383 | +enum accept_promisor { |
| 384 | + ACCEPT_NONE = 0, |
| 385 | + ACCEPT_KNOWN_URL, |
| 386 | + ACCEPT_KNOWN_NAME, |
| 387 | + ACCEPT_ALL |
| 388 | +}; |
| 389 | + |
| 390 | +static int should_accept_remote(enum accept_promisor accept, |
| 391 | + const char *remote_name, const char *remote_url, |
| 392 | + struct strvec *names, struct strvec *urls) |
| 393 | +{ |
| 394 | + size_t i; |
| 395 | + |
| 396 | + if (accept == ACCEPT_ALL) |
| 397 | + return 1; |
| 398 | + |
| 399 | + i = strvec_find_index(names, remote_name); |
| 400 | + |
| 401 | + if (i >= names->nr) |
| 402 | + /* We don't know about that remote */ |
| 403 | + return 0; |
| 404 | + |
| 405 | + if (accept == ACCEPT_KNOWN_NAME) |
| 406 | + return 1; |
| 407 | + |
| 408 | + if (accept != ACCEPT_KNOWN_URL) |
| 409 | + BUG("Unhandled 'enum accept_promisor' value '%d'", accept); |
| 410 | + |
| 411 | + if (!strcasecmp(urls->v[i], remote_url)) |
| 412 | + return 1; |
| 413 | + |
| 414 | + warning(_("known remote named '%s' but with url '%s' instead of '%s'"), |
| 415 | + remote_name, urls->v[i], remote_url); |
| 416 | + |
| 417 | + return 0; |
| 418 | +} |
| 419 | + |
| 420 | +static void filter_promisor_remote(struct repository *repo, |
| 421 | + struct strvec *accepted, |
| 422 | + const char *info) |
| 423 | +{ |
| 424 | + struct strbuf **remotes; |
| 425 | + char *accept_str; |
| 426 | + enum accept_promisor accept = ACCEPT_NONE; |
| 427 | + struct strvec names = STRVEC_INIT; |
| 428 | + struct strvec urls = STRVEC_INIT; |
| 429 | + |
| 430 | + if (!git_config_get_string("promisor.acceptfromserver", &accept_str)) { |
| 431 | + if (!accept_str || !*accept_str || !strcasecmp("None", accept_str)) |
| 432 | + accept = ACCEPT_NONE; |
| 433 | + else if (!strcasecmp("KnownUrl", accept_str)) |
| 434 | + accept = ACCEPT_KNOWN_URL; |
| 435 | + else if (!strcasecmp("KnownName", accept_str)) |
| 436 | + accept = ACCEPT_KNOWN_NAME; |
| 437 | + else if (!strcasecmp("All", accept_str)) |
| 438 | + accept = ACCEPT_ALL; |
| 439 | + else |
| 440 | + warning(_("unknown '%s' value for '%s' config option"), |
| 441 | + accept_str, "promisor.acceptfromserver"); |
| 442 | + } |
| 443 | + |
| 444 | + if (accept == ACCEPT_NONE) |
| 445 | + return; |
| 446 | + |
| 447 | + if (accept != ACCEPT_ALL) |
| 448 | + promisor_info_vecs(repo, &names, &urls); |
| 449 | + |
| 450 | + /* Parse remote info received */ |
| 451 | + |
| 452 | + remotes = strbuf_split_str(info, ';', 0); |
| 453 | + |
| 454 | + for (size_t i = 0; remotes[i]; i++) { |
| 455 | + struct strbuf **elems; |
| 456 | + const char *remote_name = NULL; |
| 457 | + const char *remote_url = NULL; |
| 458 | + char *decoded_name = NULL; |
| 459 | + char *decoded_url = NULL; |
| 460 | + |
| 461 | + strbuf_trim_trailing_ch(remotes[i], ';'); |
| 462 | + elems = strbuf_split_str(remotes[i]->buf, ',', 0); |
| 463 | + |
| 464 | + for (size_t j = 0; elems[j]; j++) { |
| 465 | + int res; |
| 466 | + strbuf_trim_trailing_ch(elems[j], ','); |
| 467 | + res = skip_prefix(elems[j]->buf, "name=", &remote_name) || |
| 468 | + skip_prefix(elems[j]->buf, "url=", &remote_url); |
| 469 | + if (!res) |
| 470 | + warning(_("unknown element '%s' from remote info"), |
| 471 | + elems[j]->buf); |
| 472 | + } |
| 473 | + |
| 474 | + if (remote_name) |
| 475 | + decoded_name = url_percent_decode(remote_name); |
| 476 | + if (remote_url) |
| 477 | + decoded_url = url_percent_decode(remote_url); |
| 478 | + |
| 479 | + if (decoded_name && should_accept_remote(accept, decoded_name, decoded_url, &names, &urls)) |
| 480 | + strvec_push(accepted, decoded_name); |
| 481 | + |
| 482 | + strbuf_list_free(elems); |
| 483 | + free(decoded_name); |
| 484 | + free(decoded_url); |
| 485 | + } |
| 486 | + |
| 487 | + free(accept_str); |
| 488 | + strvec_clear(&names); |
| 489 | + strvec_clear(&urls); |
| 490 | + strbuf_list_free(remotes); |
| 491 | +} |
| 492 | + |
| 493 | +char *promisor_remote_reply(const char *info) |
| 494 | +{ |
| 495 | + struct strvec accepted = STRVEC_INIT; |
| 496 | + struct strbuf reply = STRBUF_INIT; |
| 497 | + |
| 498 | + filter_promisor_remote(the_repository, &accepted, info); |
| 499 | + |
| 500 | + if (!accepted.nr) |
| 501 | + return NULL; |
| 502 | + |
| 503 | + for (size_t i = 0; i < accepted.nr; i++) { |
| 504 | + if (i) |
| 505 | + strbuf_addch(&reply, ';'); |
| 506 | + strbuf_addstr_urlencode(&reply, accepted.v[i], allow_unsanitized); |
| 507 | + } |
| 508 | + |
| 509 | + strvec_clear(&accepted); |
| 510 | + |
| 511 | + return strbuf_detach(&reply, NULL); |
| 512 | +} |
| 513 | + |
| 514 | +void mark_promisor_remotes_as_accepted(struct repository *r, const char *remotes) |
| 515 | +{ |
| 516 | + struct strbuf **accepted_remotes = strbuf_split_str(remotes, ';', 0); |
| 517 | + |
| 518 | + for (size_t i = 0; accepted_remotes[i]; i++) { |
| 519 | + struct promisor_remote *p; |
| 520 | + char *decoded_remote; |
| 521 | + |
| 522 | + strbuf_trim_trailing_ch(accepted_remotes[i], ';'); |
| 523 | + decoded_remote = url_percent_decode(accepted_remotes[i]->buf); |
| 524 | + |
| 525 | + p = repo_promisor_remote_find(r, decoded_remote); |
| 526 | + if (p) |
| 527 | + p->accepted = 1; |
| 528 | + else |
| 529 | + warning(_("accepted promisor remote '%s' not found"), |
| 530 | + decoded_remote); |
| 531 | + |
| 532 | + free(decoded_remote); |
| 533 | + } |
| 534 | + |
| 535 | + strbuf_list_free(accepted_remotes); |
| 536 | +} |
0 commit comments