|
11 | 11 | #include "strvec.h"
|
12 | 12 | #include "packfile.h"
|
13 | 13 | #include "environment.h"
|
| 14 | +#include "url.h" |
| 15 | +#include "version.h" |
14 | 16 |
|
15 | 17 | struct promisor_remote_config {
|
16 | 18 | struct promisor_remote *promisors;
|
@@ -221,6 +223,18 @@ int repo_has_promisor_remote(struct repository *r)
|
221 | 223 | return !!repo_promisor_remote_find(r, NULL);
|
222 | 224 | }
|
223 | 225 |
|
| 226 | +int repo_has_accepted_promisor_remote(struct repository *r) |
| 227 | +{ |
| 228 | + struct promisor_remote *p; |
| 229 | + |
| 230 | + promisor_remote_init(r); |
| 231 | + |
| 232 | + for (p = r->promisor_remote_config->promisors; p; p = p->next) |
| 233 | + if (p->accepted) |
| 234 | + return 1; |
| 235 | + return 0; |
| 236 | +} |
| 237 | + |
224 | 238 | static int remove_fetched_oids(struct repository *repo,
|
225 | 239 | struct object_id **oids,
|
226 | 240 | int oid_nr, int to_free)
|
@@ -292,3 +306,183 @@ void promisor_remote_get_direct(struct repository *repo,
|
292 | 306 | if (to_free)
|
293 | 307 | free(remaining_oids);
|
294 | 308 | }
|
| 309 | + |
| 310 | +static int allow_unsanitized(char ch) |
| 311 | +{ |
| 312 | + if (ch == ',' || ch == ';' || ch == '%') |
| 313 | + return 0; |
| 314 | + return ch > 32 && ch < 127; |
| 315 | +} |
| 316 | + |
| 317 | +static void promisor_info_vecs(struct repository *repo, |
| 318 | + struct strvec *names, |
| 319 | + struct strvec *urls) |
| 320 | +{ |
| 321 | + struct promisor_remote *r; |
| 322 | + |
| 323 | + promisor_remote_init(repo); |
| 324 | + |
| 325 | + for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
| 326 | + char *url; |
| 327 | + char *url_key = xstrfmt("remote.%s.url", r->name); |
| 328 | + |
| 329 | + strvec_push(names, r->name); |
| 330 | + strvec_push(urls, git_config_get_string(url_key, &url) ? NULL : url); |
| 331 | + |
| 332 | + free(url); |
| 333 | + free(url_key); |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +char *promisor_remote_info(struct repository *repo) |
| 338 | +{ |
| 339 | + struct strbuf sb = STRBUF_INIT; |
| 340 | + int advertise_promisors = 0; |
| 341 | + struct strvec names = STRVEC_INIT; |
| 342 | + struct strvec urls = STRVEC_INIT; |
| 343 | + |
| 344 | + git_config_get_bool("promisor.advertise", &advertise_promisors); |
| 345 | + |
| 346 | + if (!advertise_promisors) |
| 347 | + return NULL; |
| 348 | + |
| 349 | + promisor_info_vecs(repo, &names, &urls); |
| 350 | + |
| 351 | + if (!names.nr) |
| 352 | + return NULL; |
| 353 | + |
| 354 | + for (size_t i = 0; i < names.nr; i++) { |
| 355 | + if (i) |
| 356 | + strbuf_addch(&sb, ';'); |
| 357 | + strbuf_addstr(&sb, "name="); |
| 358 | + strbuf_addstr_urlencode(&sb, names.v[i], allow_unsanitized); |
| 359 | + if (urls.v[i]) { |
| 360 | + strbuf_addstr(&sb, ",url="); |
| 361 | + strbuf_addstr_urlencode(&sb, urls.v[i], allow_unsanitized); |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + strvec_clear(&names); |
| 366 | + strvec_clear(&urls); |
| 367 | + |
| 368 | + return strbuf_detach(&sb, NULL); |
| 369 | +} |
| 370 | + |
| 371 | +enum accept_promisor { |
| 372 | + ACCEPT_NONE = 0, |
| 373 | + ACCEPT_ALL |
| 374 | +}; |
| 375 | + |
| 376 | +static int should_accept_remote(enum accept_promisor accept, |
| 377 | + const char *remote_name UNUSED, |
| 378 | + const char *remote_url UNUSED) |
| 379 | +{ |
| 380 | + if (accept == ACCEPT_ALL) |
| 381 | + return 1; |
| 382 | + |
| 383 | + BUG("Unhandled 'enum accept_promisor' value '%d'", accept); |
| 384 | +} |
| 385 | + |
| 386 | +static void filter_promisor_remote(struct strvec *accepted, const char *info) |
| 387 | +{ |
| 388 | + struct strbuf **remotes; |
| 389 | + const char *accept_str; |
| 390 | + enum accept_promisor accept = ACCEPT_NONE; |
| 391 | + |
| 392 | + if (!git_config_get_string_tmp("promisor.acceptfromserver", &accept_str)) { |
| 393 | + if (!*accept_str || !strcasecmp("None", accept_str)) |
| 394 | + accept = ACCEPT_NONE; |
| 395 | + else if (!strcasecmp("All", accept_str)) |
| 396 | + accept = ACCEPT_ALL; |
| 397 | + else |
| 398 | + warning(_("unknown '%s' value for '%s' config option"), |
| 399 | + accept_str, "promisor.acceptfromserver"); |
| 400 | + } |
| 401 | + |
| 402 | + if (accept == ACCEPT_NONE) |
| 403 | + return; |
| 404 | + |
| 405 | + /* Parse remote info received */ |
| 406 | + |
| 407 | + remotes = strbuf_split_str(info, ';', 0); |
| 408 | + |
| 409 | + for (size_t i = 0; remotes[i]; i++) { |
| 410 | + struct strbuf **elems; |
| 411 | + const char *remote_name = NULL; |
| 412 | + const char *remote_url = NULL; |
| 413 | + char *decoded_name = NULL; |
| 414 | + char *decoded_url = NULL; |
| 415 | + |
| 416 | + strbuf_strip_suffix(remotes[i], ";"); |
| 417 | + elems = strbuf_split(remotes[i], ','); |
| 418 | + |
| 419 | + for (size_t j = 0; elems[j]; j++) { |
| 420 | + int res; |
| 421 | + strbuf_strip_suffix(elems[j], ","); |
| 422 | + res = skip_prefix(elems[j]->buf, "name=", &remote_name) || |
| 423 | + skip_prefix(elems[j]->buf, "url=", &remote_url); |
| 424 | + if (!res) |
| 425 | + warning(_("unknown element '%s' from remote info"), |
| 426 | + elems[j]->buf); |
| 427 | + } |
| 428 | + |
| 429 | + if (remote_name) |
| 430 | + decoded_name = url_percent_decode(remote_name); |
| 431 | + if (remote_url) |
| 432 | + decoded_url = url_percent_decode(remote_url); |
| 433 | + |
| 434 | + if (decoded_name && should_accept_remote(accept, decoded_name, decoded_url)) |
| 435 | + strvec_push(accepted, decoded_name); |
| 436 | + |
| 437 | + strbuf_list_free(elems); |
| 438 | + free(decoded_name); |
| 439 | + free(decoded_url); |
| 440 | + } |
| 441 | + |
| 442 | + strbuf_list_free(remotes); |
| 443 | +} |
| 444 | + |
| 445 | +char *promisor_remote_reply(const char *info) |
| 446 | +{ |
| 447 | + struct strvec accepted = STRVEC_INIT; |
| 448 | + struct strbuf reply = STRBUF_INIT; |
| 449 | + |
| 450 | + filter_promisor_remote(&accepted, info); |
| 451 | + |
| 452 | + if (!accepted.nr) |
| 453 | + return NULL; |
| 454 | + |
| 455 | + for (size_t i = 0; i < accepted.nr; i++) { |
| 456 | + if (i) |
| 457 | + strbuf_addch(&reply, ';'); |
| 458 | + strbuf_addstr_urlencode(&reply, accepted.v[i], allow_unsanitized); |
| 459 | + } |
| 460 | + |
| 461 | + strvec_clear(&accepted); |
| 462 | + |
| 463 | + return strbuf_detach(&reply, NULL); |
| 464 | +} |
| 465 | + |
| 466 | +void mark_promisor_remotes_as_accepted(struct repository *r, const char *remotes) |
| 467 | +{ |
| 468 | + struct strbuf **accepted_remotes = strbuf_split_str(remotes, ';', 0); |
| 469 | + |
| 470 | + for (size_t i = 0; accepted_remotes[i]; i++) { |
| 471 | + struct promisor_remote *p; |
| 472 | + char *decoded_remote; |
| 473 | + |
| 474 | + strbuf_strip_suffix(accepted_remotes[i], ";"); |
| 475 | + decoded_remote = url_percent_decode(accepted_remotes[i]->buf); |
| 476 | + |
| 477 | + p = repo_promisor_remote_find(r, decoded_remote); |
| 478 | + if (p) |
| 479 | + p->accepted = 1; |
| 480 | + else |
| 481 | + warning(_("accepted promisor remote '%s' not found"), |
| 482 | + decoded_remote); |
| 483 | + |
| 484 | + free(decoded_remote); |
| 485 | + } |
| 486 | + |
| 487 | + strbuf_list_free(accepted_remotes); |
| 488 | +} |
0 commit comments