Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/transport-helper.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / transport-helper.c` 1708 lines ยท 45203 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "transport.h" 5 #include "quote.h" 6 #include "run-command.h" 7 #include "commit.h" 8 #include "environment.h" 9 #include "gettext.h" 10 #include "hex.h" 11 #include "object-name.h" 12 #include "repository.h" 13 #include "remote.h" 14 #include "string-list.h" 15 #include "thread-utils.h" 16 #include "sigchain.h" 17 #include "strvec.h" 18 #include "refs.h" 19 #include "refspec.h" 20 #include "transport-internal.h" 21 #include "protocol.h" 22 #include "packfile.h" 23 24 static int debug; 25 26 struct helper_data { 27 char *name; 28 struct child_process *helper; 29 FILE *out; 30 unsigned fetch : 1, 31 import : 1, 32 bidi_import : 1, 33 export : 1, 34 option : 1, 35 push : 1, 36 connect : 1, 37 stateless_connect : 1, 38 signed_tags : 1, 39 check_connectivity : 1, 40 no_disconnect_req : 1, 41 no_private_update : 1, 42 object_format : 1; 43 44 /* 45 * As an optimization, the transport code may invoke fetch before 46 * get_refs_list. If this happens, and if the transport helper doesn't 47 * support connect or stateless_connect, we need to invoke 48 * get_refs_list ourselves if we haven't already done so. Keep track of 49 * whether we have invoked get_refs_list. 50 */ 51 unsigned get_refs_list_called : 1; 52 53 char *export_marks; 54 char *import_marks; 55 /* These go from remote name (as in "list") to private name */ 56 struct refspec rs; 57 /* Transport options for fetch-pack/send-pack (should one of 58 * those be invoked). 59 */ 60 struct git_transport_options transport_options; 61 }; 62 63 static void sendline(struct helper_data *helper, struct strbuf *buffer) 64 { 65 if (debug) 66 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf); 67 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0) 68 die_errno(_("full write to remote helper failed")); 69 } 70 71 static int recvline_fh(FILE *helper, struct strbuf *buffer) 72 { 73 strbuf_reset(buffer); 74 if (debug) 75 fprintf(stderr, "Debug: Remote helper: Waiting...\n"); 76 if (strbuf_getline(buffer, helper) == EOF) { 77 if (debug) 78 fprintf(stderr, "Debug: Remote helper quit.\n"); 79 return 1; 80 } 81 82 if (debug) 83 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf); 84 return 0; 85 } 86 87 static int recvline(struct helper_data *helper, struct strbuf *buffer) 88 { 89 return recvline_fh(helper->out, buffer); 90 } 91 92 static int write_constant_gently(int fd, const char *str) 93 { 94 if (debug) 95 fprintf(stderr, "Debug: Remote helper: -> %s", str); 96 if (write_in_full(fd, str, strlen(str)) < 0) 97 return -1; 98 return 0; 99 } 100 101 static void write_constant(int fd, const char *str) 102 { 103 if (write_constant_gently(fd, str) < 0) 104 die_errno(_("full write to remote helper failed")); 105 } 106 107 static const char *remove_ext_force(const char *url) 108 { 109 if (url) { 110 const char *colon = strchr(url, ':'); 111 if (colon && colon[1] == ':') 112 return colon + 2; 113 } 114 return url; 115 } 116 117 static void do_take_over(struct transport *transport) 118 { 119 struct helper_data *data; 120 data = (struct helper_data *)transport->data; 121 transport_take_over(transport, data->helper); 122 fclose(data->out); 123 free(data->name); 124 free(data); 125 } 126 127 static void standard_options(struct transport *t); 128 129 static struct child_process *get_helper(struct transport *transport) 130 { 131 struct helper_data *data = transport->data; 132 struct strbuf buf = STRBUF_INIT; 133 struct child_process *helper; 134 int duped; 135 int code; 136 137 if (data->helper) 138 return data->helper; 139 140 helper = xmalloc(sizeof(*helper)); 141 child_process_init(helper); 142 helper->in = -1; 143 helper->out = -1; 144 helper->err = 0; 145 strvec_pushf(&helper->args, "remote-%s", data->name); 146 strvec_push(&helper->args, transport->remote->name); 147 strvec_push(&helper->args, remove_ext_force(transport->url)); 148 helper->git_cmd = 1; 149 helper->silent_exec_failure = 1; 150 151 if (have_git_dir()) 152 strvec_pushf(&helper->env, "%s=%s", 153 GIT_DIR_ENVIRONMENT, repo_get_git_dir(the_repository)); 154 155 helper->trace2_child_class = helper->args.v[0]; /* "remote-" */ 156 157 code = start_command(helper); 158 if (code < 0 && errno == ENOENT) 159 die(_("unable to find remote helper for '%s'"), data->name); 160 else if (code != 0) 161 exit(code); 162 163 data->helper = helper; 164 data->no_disconnect_req = 0; 165 refspec_init_fetch(&data->rs, the_hash_algo); 166 167 /* 168 * Open the output as FILE* so strbuf_getline_*() family of 169 * functions can be used. 170 * Do this with duped fd because fclose() will close the fd, 171 * and stuff like taking over will require the fd to remain. 172 */ 173 duped = dup(helper->out); 174 if (duped < 0) 175 die_errno(_("can't dup helper output fd")); 176 data->out = xfdopen(duped, "r"); 177 178 sigchain_push(SIGPIPE, SIG_IGN); 179 if (write_constant_gently(helper->in, "capabilities\n") < 0) 180 die("remote helper '%s' aborted session", data->name); 181 sigchain_pop(SIGPIPE); 182 183 while (1) { 184 const char *capname, *arg; 185 int mandatory = 0; 186 if (recvline(data, &buf)) 187 die("remote helper '%s' aborted session", data->name); 188 189 if (!*buf.buf) 190 break; 191 192 if (*buf.buf == '*') { 193 capname = buf.buf + 1; 194 mandatory = 1; 195 } else 196 capname = buf.buf; 197 198 if (debug) 199 fprintf(stderr, "Debug: Got cap %s\n", capname); 200 if (!strcmp(capname, "fetch")) 201 data->fetch = 1; 202 else if (!strcmp(capname, "option")) 203 data->option = 1; 204 else if (!strcmp(capname, "push")) 205 data->push = 1; 206 else if (!strcmp(capname, "import")) 207 data->import = 1; 208 else if (!strcmp(capname, "bidi-import")) 209 data->bidi_import = 1; 210 else if (!strcmp(capname, "export")) 211 data->export = 1; 212 else if (!strcmp(capname, "check-connectivity")) 213 data->check_connectivity = 1; 214 else if (skip_prefix(capname, "refspec ", &arg)) { 215 refspec_append(&data->rs, arg); 216 } else if (!strcmp(capname, "connect")) { 217 data->connect = 1; 218 } else if (!strcmp(capname, "stateless-connect")) { 219 data->stateless_connect = 1; 220 } else if (!strcmp(capname, "signed-tags")) { 221 data->signed_tags = 1; 222 } else if (skip_prefix(capname, "export-marks ", &arg)) { 223 data->export_marks = xstrdup(arg); 224 } else if (skip_prefix(capname, "import-marks ", &arg)) { 225 data->import_marks = xstrdup(arg); 226 } else if (starts_with(capname, "no-private-update")) { 227 data->no_private_update = 1; 228 } else if (starts_with(capname, "object-format")) { 229 data->object_format = 1; 230 } else if (mandatory) { 231 die(_("unknown mandatory capability %s; this remote " 232 "helper probably needs newer version of Git"), 233 capname); 234 } 235 } 236 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) { 237 warning(_("this remote helper should implement refspec capability")); 238 } 239 strbuf_release(&buf); 240 if (debug) 241 fprintf(stderr, "Debug: Capabilities complete.\n"); 242 standard_options(transport); 243 return data->helper; 244 } 245 246 static int disconnect_helper(struct transport *transport) 247 { 248 struct helper_data *data = transport->data; 249 int res = 0; 250 251 if (data->helper) { 252 if (debug) 253 fprintf(stderr, "Debug: Disconnecting.\n"); 254 if (!data->no_disconnect_req) { 255 /* 256 * Ignore write errors; there's nothing we can do, 257 * since we're about to close the pipe anyway. And the 258 * most likely error is EPIPE due to the helper dying 259 * to report an error itself. 260 */ 261 sigchain_push(SIGPIPE, SIG_IGN); 262 xwrite(data->helper->in, "\n", 1); 263 sigchain_pop(SIGPIPE); 264 } 265 close(data->helper->in); 266 close(data->helper->out); 267 fclose(data->out); 268 res = finish_command(data->helper); 269 FREE_AND_NULL(data->helper); 270 } 271 FREE_AND_NULL(data->name); 272 return res; 273 } 274 275 static const char *unsupported_options[] = { 276 TRANS_OPT_UPLOADPACK, 277 TRANS_OPT_RECEIVEPACK, 278 TRANS_OPT_THIN, 279 TRANS_OPT_KEEP 280 }; 281 282 static const char *boolean_options[] = { 283 TRANS_OPT_THIN, 284 TRANS_OPT_KEEP, 285 TRANS_OPT_FOLLOWTAGS, 286 TRANS_OPT_DEEPEN_RELATIVE 287 }; 288 289 static int strbuf_set_helper_option(struct helper_data *data, 290 struct strbuf *buf) 291 { 292 int ret; 293 294 sendline(data, buf); 295 if (recvline(data, buf)) 296 exit(128); 297 298 if (!strcmp(buf->buf, "ok")) 299 ret = 0; 300 else if (starts_with(buf->buf, "error")) 301 ret = -1; 302 else if (!strcmp(buf->buf, "unsupported")) 303 ret = 1; 304 else { 305 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf); 306 ret = 1; 307 } 308 return ret; 309 } 310 311 static int string_list_set_helper_option(struct helper_data *data, 312 const char *name, 313 struct string_list *list) 314 { 315 struct strbuf buf = STRBUF_INIT; 316 int ret = 0; 317 318 for (size_t i = 0; i < list->nr; i++) { 319 strbuf_addf(&buf, "option %s ", name); 320 quote_c_style(list->items[i].string, &buf, NULL, 0); 321 strbuf_addch(&buf, '\n'); 322 323 if ((ret = strbuf_set_helper_option(data, &buf))) 324 break; 325 strbuf_reset(&buf); 326 } 327 strbuf_release(&buf); 328 return ret; 329 } 330 331 static int set_helper_option(struct transport *transport, 332 const char *name, const char *value) 333 { 334 struct helper_data *data = transport->data; 335 struct strbuf buf = STRBUF_INIT; 336 int ret, is_bool = 0; 337 338 get_helper(transport); 339 340 if (!data->option) 341 return 1; 342 343 if (!strcmp(name, "deepen-not")) 344 return string_list_set_helper_option(data, name, 345 (struct string_list *)value); 346 347 for (size_t i = 0; i < ARRAY_SIZE(unsupported_options); i++) { 348 if (!strcmp(name, unsupported_options[i])) 349 return 1; 350 } 351 352 for (size_t i = 0; i < ARRAY_SIZE(boolean_options); i++) { 353 if (!strcmp(name, boolean_options[i])) { 354 is_bool = 1; 355 break; 356 } 357 } 358 359 strbuf_addf(&buf, "option %s ", name); 360 if (is_bool) 361 strbuf_addstr(&buf, value ? "true" : "false"); 362 else 363 quote_c_style(value, &buf, NULL, 0); 364 strbuf_addch(&buf, '\n'); 365 366 ret = strbuf_set_helper_option(data, &buf); 367 strbuf_release(&buf); 368 return ret; 369 } 370 371 static void standard_options(struct transport *t) 372 { 373 char buf[16]; 374 int v = t->verbose; 375 376 set_helper_option(t, "progress", t->progress ? "true" : "false"); 377 378 xsnprintf(buf, sizeof(buf), "%d", v + 1); 379 set_helper_option(t, "verbosity", buf); 380 381 switch (t->family) { 382 case TRANSPORT_FAMILY_ALL: 383 /* 384 * this is already the default, 385 * do not break old remote helpers by setting "all" here 386 */ 387 break; 388 case TRANSPORT_FAMILY_IPV4: 389 set_helper_option(t, "family", "ipv4"); 390 break; 391 case TRANSPORT_FAMILY_IPV6: 392 set_helper_option(t, "family", "ipv6"); 393 break; 394 } 395 } 396 397 static int release_helper(struct transport *transport) 398 { 399 int res = 0; 400 struct helper_data *data = transport->data; 401 refspec_clear(&data->rs); 402 free(data->import_marks); 403 free(data->export_marks); 404 transport_options_release(&data->transport_options); 405 res = disconnect_helper(transport); 406 free(transport->data); 407 return res; 408 } 409 410 static int fetch_with_fetch(struct transport *transport, 411 int nr_heads, struct ref **to_fetch) 412 { 413 struct helper_data *data = transport->data; 414 int i; 415 struct strbuf buf = STRBUF_INIT; 416 417 for (i = 0; i < nr_heads; i++) { 418 const struct ref *posn = to_fetch[i]; 419 if (posn->status & REF_STATUS_UPTODATE) 420 continue; 421 422 strbuf_addf(&buf, "fetch %s %s\n", 423 oid_to_hex(&posn->old_oid), 424 posn->symref ? posn->symref : posn->name); 425 } 426 427 strbuf_addch(&buf, '\n'); 428 sendline(data, &buf); 429 430 while (1) { 431 const char *name; 432 433 if (recvline(data, &buf)) 434 exit(128); 435 436 if (skip_prefix(buf.buf, "lock ", &name)) { 437 if (transport->pack_lockfiles.nr) 438 warning(_("%s also locked %s"), data->name, name); 439 else 440 string_list_append(&transport->pack_lockfiles, 441 name); 442 } 443 else if (data->check_connectivity && 444 data->transport_options.check_self_contained_and_connected && 445 !strcmp(buf.buf, "connectivity-ok")) 446 data->transport_options.self_contained_and_connected = 1; 447 else if (!buf.len) 448 break; 449 else 450 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf); 451 } 452 strbuf_release(&buf); 453 454 odb_reprepare(the_repository->objects); 455 return 0; 456 } 457 458 static int get_importer(struct transport *transport, struct child_process *fastimport) 459 { 460 struct child_process *helper = get_helper(transport); 461 struct helper_data *data = transport->data; 462 int cat_blob_fd, code; 463 child_process_init(fastimport); 464 fastimport->in = xdup(helper->out); 465 strvec_push(&fastimport->args, "fast-import"); 466 strvec_push(&fastimport->args, "--allow-unsafe-features"); 467 strvec_push(&fastimport->args, debug ? "--stats" : "--quiet"); 468 469 if (data->bidi_import) { 470 cat_blob_fd = xdup(helper->in); 471 strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd); 472 } 473 fastimport->git_cmd = 1; 474 475 code = start_command(fastimport); 476 return code; 477 } 478 479 static int get_exporter(struct transport *transport, 480 struct child_process *fastexport, 481 struct string_list *revlist_args) 482 { 483 struct helper_data *data = transport->data; 484 struct child_process *helper = get_helper(transport); 485 486 child_process_init(fastexport); 487 488 /* we need to duplicate helper->in because we want to use it after 489 * fastexport is done with it. */ 490 fastexport->out = dup(helper->in); 491 if (fastexport->out < 0) 492 return error_errno(_("could not dup helper output fd")); 493 strvec_push(&fastexport->args, "fast-export"); 494 strvec_push(&fastexport->args, "--use-done-feature"); 495 strvec_push(&fastexport->args, data->signed_tags ? 496 "--signed-tags=verbatim" : "--signed-tags=warn-strip"); 497 if (data->export_marks) 498 strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks); 499 if (data->import_marks) 500 strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks); 501 502 for (size_t i = 0; i < revlist_args->nr; i++) 503 strvec_push(&fastexport->args, revlist_args->items[i].string); 504 505 fastexport->git_cmd = 1; 506 return start_command(fastexport); 507 } 508 509 static int fetch_with_import(struct transport *transport, 510 int nr_heads, struct ref **to_fetch) 511 { 512 struct child_process fastimport; 513 struct helper_data *data = transport->data; 514 int i; 515 struct ref *posn; 516 struct strbuf buf = STRBUF_INIT; 517 518 get_helper(transport); 519 520 if (get_importer(transport, &fastimport)) 521 die(_("couldn't run fast-import")); 522 523 for (i = 0; i < nr_heads; i++) { 524 posn = to_fetch[i]; 525 if (posn->status & REF_STATUS_UPTODATE) 526 continue; 527 528 strbuf_addf(&buf, "import %s\n", 529 posn->symref ? posn->symref : posn->name); 530 sendline(data, &buf); 531 strbuf_reset(&buf); 532 } 533 534 write_constant(data->helper->in, "\n"); 535 /* 536 * remote-helpers that advertise the bidi-import capability are required to 537 * buffer the complete batch of import commands until this newline before 538 * sending data to fast-import. 539 * These helpers read back data from fast-import on their stdin, which could 540 * be mixed with import commands, otherwise. 541 */ 542 543 if (finish_command(&fastimport)) 544 die(_("error while running fast-import")); 545 546 /* 547 * The fast-import stream of a remote helper that advertises 548 * the "refspec" capability writes to the refs named after the 549 * right hand side of the first refspec matching each ref we 550 * were fetching. 551 * 552 * (If no "refspec" capability was specified, for historical 553 * reasons we default to the equivalent of *:*.) 554 * 555 * Store the result in to_fetch[i].old_sha1. Callers such 556 * as "git fetch" can use the value to write feedback to the 557 * terminal, populate FETCH_HEAD, and determine what new value 558 * should be written to peer_ref if the update is a 559 * fast-forward or this is a forced update. 560 */ 561 for (i = 0; i < nr_heads; i++) { 562 char *private, *name; 563 posn = to_fetch[i]; 564 if (posn->status & REF_STATUS_UPTODATE) 565 continue; 566 name = posn->symref ? posn->symref : posn->name; 567 if (data->rs.nr) 568 private = apply_refspecs(&data->rs, name); 569 else 570 private = xstrdup(name); 571 if (private) { 572 if (refs_read_ref(get_main_ref_store(the_repository), private, &posn->old_oid) < 0) 573 die(_("could not read ref %s"), private); 574 free(private); 575 } 576 } 577 strbuf_release(&buf); 578 return 0; 579 } 580 581 static int run_connect(struct transport *transport, struct strbuf *cmdbuf) 582 { 583 struct helper_data *data = transport->data; 584 int ret = 0; 585 int duped; 586 FILE *input; 587 struct child_process *helper; 588 589 helper = get_helper(transport); 590 591 /* 592 * Yes, dup the pipe another time, as we need unbuffered version 593 * of input pipe as FILE*. fclose() closes the underlying fd and 594 * stream buffering only can be changed before first I/O operation 595 * on it. 596 */ 597 duped = dup(helper->out); 598 if (duped < 0) 599 die_errno(_("can't dup helper output fd")); 600 input = xfdopen(duped, "r"); 601 setvbuf(input, NULL, _IONBF, 0); 602 603 sendline(data, cmdbuf); 604 if (recvline_fh(input, cmdbuf)) 605 exit(128); 606 607 if (!strcmp(cmdbuf->buf, "")) { 608 data->no_disconnect_req = 1; 609 if (debug) 610 fprintf(stderr, "Debug: Smart transport connection " 611 "ready.\n"); 612 ret = 1; 613 } else if (!strcmp(cmdbuf->buf, "fallback")) { 614 if (debug) 615 fprintf(stderr, "Debug: Falling back to dumb " 616 "transport.\n"); 617 } else { 618 die(_("unknown response to connect: %s"), 619 cmdbuf->buf); 620 } 621 622 fclose(input); 623 return ret; 624 } 625 626 static const char *connect_service_cmd(enum git_connect_service service) 627 { 628 switch (service) { 629 case GIT_CONNECT_UPLOAD_PACK: 630 return "git-upload-pack"; 631 case GIT_CONNECT_RECEIVE_PACK: 632 return "git-receive-pack"; 633 case GIT_CONNECT_UPLOAD_ARCHIVE: 634 return "git-upload-archive"; 635 } 636 BUG("unknown git_connect_service: %d", service); 637 } 638 639 static int process_connect_service(struct transport *transport, 640 enum git_connect_service service, 641 const char *exec) 642 { 643 struct helper_data *data = transport->data; 644 struct strbuf cmdbuf = STRBUF_INIT; 645 int ret = 0; 646 647 /* 648 * Handle --upload-pack and friends. This is fire and forget... 649 * just warn if it fails. 650 */ 651 if (strcmp(connect_service_cmd(service), exec)) { 652 int r = set_helper_option(transport, "servpath", exec); 653 if (r > 0) 654 warning(_("setting remote service path not supported by protocol")); 655 else if (r < 0) 656 warning(_("invalid remote service path")); 657 } 658 659 if (data->connect) { 660 strbuf_addf(&cmdbuf, "connect %s\n", 661 connect_service_cmd(service)); 662 ret = run_connect(transport, &cmdbuf); 663 } else if (data->stateless_connect && 664 (get_protocol_version_config() == protocol_v2) && 665 (service == GIT_CONNECT_UPLOAD_PACK || 666 service == GIT_CONNECT_UPLOAD_ARCHIVE)) { 667 strbuf_addf(&cmdbuf, "stateless-connect %s\n", 668 connect_service_cmd(service)); 669 ret = run_connect(transport, &cmdbuf); 670 if (ret) 671 transport->stateless_rpc = 1; 672 } 673 674 strbuf_release(&cmdbuf); 675 return ret; 676 } 677 678 static int process_connect(struct transport *transport, 679 int for_push) 680 { 681 struct helper_data *data = transport->data; 682 enum git_connect_service service; 683 const char *exec; 684 int ret; 685 686 service = for_push ? GIT_CONNECT_RECEIVE_PACK : GIT_CONNECT_UPLOAD_PACK; 687 if (for_push) 688 exec = data->transport_options.receivepack; 689 else 690 exec = data->transport_options.uploadpack; 691 692 ret = process_connect_service(transport, service, exec); 693 if (ret) 694 do_take_over(transport); 695 return ret; 696 } 697 698 static int connect_helper(struct transport *transport, enum git_connect_service service, 699 const char *exec, int fd[2]) 700 { 701 struct helper_data *data = transport->data; 702 703 /* Get_helper so connect is inited. */ 704 get_helper(transport); 705 706 if (!process_connect_service(transport, service, exec)) 707 die(_("can't connect to subservice %s"), 708 connect_service_cmd(service)); 709 710 fd[0] = data->helper->out; 711 fd[1] = data->helper->in; 712 713 do_take_over(transport); 714 return 0; 715 } 716 717 static struct ref *get_refs_list_using_list(struct transport *transport, 718 int for_push); 719 720 static int fetch_refs(struct transport *transport, 721 int nr_heads, struct ref **to_fetch) 722 { 723 struct helper_data *data = transport->data; 724 int i, count; 725 726 get_helper(transport); 727 728 if (process_connect(transport, 0)) 729 return transport->vtable->fetch_refs(transport, nr_heads, to_fetch); 730 731 /* 732 * If we reach here, then the server, the client, and/or the transport 733 * helper does not support protocol v2. --negotiate-only requires 734 * protocol v2. 735 */ 736 if (data->transport_options.acked_commits) { 737 warning(_("--negotiate-only requires protocol v2")); 738 return -1; 739 } 740 741 if (!data->get_refs_list_called) { 742 /* 743 * We do not care about the list of refs returned, but only 744 * that the "list" command was sent. 745 */ 746 struct ref *dummy = get_refs_list_using_list(transport, 0); 747 free_refs(dummy); 748 } 749 750 count = 0; 751 for (i = 0; i < nr_heads; i++) 752 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE)) 753 count++; 754 755 if (!count) 756 return 0; 757 758 if (data->check_connectivity && 759 data->transport_options.check_self_contained_and_connected) 760 set_helper_option(transport, "check-connectivity", "true"); 761 762 if (transport->cloning) 763 set_helper_option(transport, "cloning", "true"); 764 765 if (data->transport_options.update_shallow) 766 set_helper_option(transport, "update-shallow", "true"); 767 768 if (data->transport_options.refetch) 769 set_helper_option(transport, "refetch", "true"); 770 771 if (data->transport_options.filter_options.choice) { 772 const char *spec = expand_list_objects_filter_spec( 773 &data->transport_options.filter_options); 774 set_helper_option(transport, "filter", spec); 775 } 776 777 if (data->transport_options.negotiation_restrict_tips) 778 warning(_("ignoring %s because the protocol does not support it."), 779 "--negotiation-restrict"); 780 781 if (data->fetch) 782 return fetch_with_fetch(transport, nr_heads, to_fetch); 783 784 if (data->import) 785 return fetch_with_import(transport, nr_heads, to_fetch); 786 787 return -1; 788 } 789 790 static int fetch_object_info_helper(struct transport *transport, 791 const struct oid_array *oids, 792 struct fetch_object_info_results *results) 793 { 794 get_helper(transport); 795 if (process_connect(transport, 0)) 796 return transport->vtable->fetch_object_info(transport, oids, 797 results); 798 799 die(_("object-info requires protocol v2")); 800 } 801 802 struct push_update_ref_state { 803 struct ref *hint; 804 struct ref_push_report *report; 805 int new_report; 806 }; 807 808 static int push_update_ref_status(struct strbuf *buf, 809 struct push_update_ref_state *state, 810 struct ref *remote_refs) 811 { 812 char *refname, *msg; 813 int status, forced = 0; 814 815 if (starts_with(buf->buf, "option ")) { 816 struct object_id old_oid, new_oid; 817 char *key; 818 const char *val; 819 char *p; 820 821 if (!state->hint || !(state->report || state->new_report)) 822 die(_("'option' without a matching 'ok/error' directive")); 823 if (state->new_report) { 824 if (!state->hint->report) { 825 CALLOC_ARRAY(state->hint->report, 1); 826 state->report = state->hint->report; 827 } else { 828 state->report = state->hint->report; 829 while (state->report->next) 830 state->report = state->report->next; 831 CALLOC_ARRAY(state->report->next, 1); 832 state->report = state->report->next; 833 } 834 state->new_report = 0; 835 } 836 key = buf->buf + 7; 837 p = strchr(key, ' '); 838 if (p) 839 *p++ = '\0'; 840 val = p; 841 if (!strcmp(key, "refname")) 842 state->report->ref_name = xstrdup_or_null(val); 843 else if (!strcmp(key, "old-oid") && val && 844 !parse_oid_hex(val, &old_oid, &val)) 845 state->report->old_oid = oiddup(&old_oid); 846 else if (!strcmp(key, "new-oid") && val && 847 !parse_oid_hex(val, &new_oid, &val)) 848 state->report->new_oid = oiddup(&new_oid); 849 else if (!strcmp(key, "forced-update")) 850 state->report->forced_update = 1; 851 /* Not update remote namespace again. */ 852 return 1; 853 } 854 855 state->report = NULL; 856 state->new_report = 0; 857 858 if (starts_with(buf->buf, "ok ")) { 859 status = REF_STATUS_OK; 860 refname = buf->buf + 3; 861 } else if (starts_with(buf->buf, "error ")) { 862 status = REF_STATUS_REMOTE_REJECT; 863 refname = buf->buf + 6; 864 } else 865 die(_("expected ok/error, helper said '%s'"), buf->buf); 866 867 msg = strchr(refname, ' '); 868 if (msg) { 869 struct strbuf msg_buf = STRBUF_INIT; 870 const char *end; 871 872 *msg++ = '\0'; 873 if (!unquote_c_style(&msg_buf, msg, &end)) 874 msg = strbuf_detach(&msg_buf, NULL); 875 else 876 msg = xstrdup(msg); 877 strbuf_release(&msg_buf); 878 879 if (!strcmp(msg, "no match")) { 880 status = REF_STATUS_NONE; 881 FREE_AND_NULL(msg); 882 } 883 else if (!strcmp(msg, "up to date")) { 884 status = REF_STATUS_UPTODATE; 885 FREE_AND_NULL(msg); 886 } 887 else if (!strcmp(msg, "non-fast forward")) { 888 status = REF_STATUS_REJECT_NONFASTFORWARD; 889 FREE_AND_NULL(msg); 890 } 891 else if (!strcmp(msg, "already exists")) { 892 status = REF_STATUS_REJECT_ALREADY_EXISTS; 893 FREE_AND_NULL(msg); 894 } 895 else if (!strcmp(msg, "fetch first")) { 896 status = REF_STATUS_REJECT_FETCH_FIRST; 897 FREE_AND_NULL(msg); 898 } 899 else if (!strcmp(msg, "needs force")) { 900 status = REF_STATUS_REJECT_NEEDS_FORCE; 901 FREE_AND_NULL(msg); 902 } 903 else if (!strcmp(msg, "stale info")) { 904 status = REF_STATUS_REJECT_STALE; 905 FREE_AND_NULL(msg); 906 } 907 else if (!strcmp(msg, "remote ref updated since checkout")) { 908 status = REF_STATUS_REJECT_REMOTE_UPDATED; 909 FREE_AND_NULL(msg); 910 } 911 else if (!strcmp(msg, "forced update")) { 912 forced = 1; 913 FREE_AND_NULL(msg); 914 } 915 else if (!strcmp(msg, "expecting report")) { 916 status = REF_STATUS_EXPECTING_REPORT; 917 FREE_AND_NULL(msg); 918 } 919 } 920 921 if (state->hint) 922 state->hint = find_ref_by_name(state->hint, refname); 923 if (!state->hint) 924 state->hint = find_ref_by_name(remote_refs, refname); 925 if (!state->hint) { 926 warning(_("helper reported unexpected status of %s"), refname); 927 return 1; 928 } 929 930 if (state->hint->status != REF_STATUS_NONE) { 931 /* 932 * Earlier, the ref was marked not to be pushed, so ignore the ref 933 * status reported by the remote helper if the latter is 'no match'. 934 */ 935 if (status == REF_STATUS_NONE) 936 return 1; 937 } 938 939 if (status == REF_STATUS_OK) 940 state->new_report = 1; 941 state->hint->status = status; 942 state->hint->forced_update |= forced; 943 state->hint->remote_status = msg; 944 return !(status == REF_STATUS_OK); 945 } 946 947 static int push_update_refs_status(struct helper_data *data, 948 struct ref *remote_refs, 949 int flags) 950 { 951 struct ref *ref; 952 struct ref_push_report *report; 953 struct strbuf buf = STRBUF_INIT; 954 struct push_update_ref_state state = { remote_refs, NULL, 0 }; 955 956 for (;;) { 957 if (recvline(data, &buf)) { 958 strbuf_release(&buf); 959 return 1; 960 } 961 if (!buf.len) 962 break; 963 push_update_ref_status(&buf, &state, remote_refs); 964 } 965 strbuf_release(&buf); 966 967 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update) 968 return 0; 969 970 /* propagate back the update to the remote namespace */ 971 for (ref = remote_refs; ref; ref = ref->next) { 972 char *private; 973 974 if (ref->status != REF_STATUS_OK) 975 continue; 976 977 if (!ref->report) { 978 private = apply_refspecs(&data->rs, ref->name); 979 if (!private) 980 continue; 981 refs_update_ref(get_main_ref_store(the_repository), 982 "update by helper", private, 983 &(ref->new_oid), 984 NULL, 0, 0); 985 free(private); 986 } else { 987 for (report = ref->report; report; report = report->next) { 988 private = apply_refspecs(&data->rs, 989 report->ref_name 990 ? report->ref_name 991 : ref->name); 992 if (!private) 993 continue; 994 refs_update_ref(get_main_ref_store(the_repository), 995 "update by helper", private, 996 report->new_oid 997 ? report->new_oid 998 : &(ref->new_oid), 999 NULL, 0, 0); 1000 free(private); 1001 } 1002 } 1003 } 1004 return 0; 1005 } 1006 1007 static void set_common_push_options(struct transport *transport, 1008 const char *name, int flags) 1009 { 1010 if (flags & TRANSPORT_PUSH_DRY_RUN) { 1011 if (set_helper_option(transport, "dry-run", "true") != 0) 1012 die(_("helper %s does not support dry-run"), name); 1013 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) { 1014 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0) 1015 die(_("helper %s does not support --signed"), name); 1016 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) { 1017 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0) 1018 die(_("helper %s does not support --signed=if-asked"), name); 1019 } 1020 1021 if (flags & TRANSPORT_PUSH_ATOMIC) 1022 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0) 1023 die(_("helper %s does not support --atomic"), name); 1024 1025 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES) 1026 if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0) 1027 die(_("helper %s does not support --%s"), 1028 name, TRANS_OPT_FORCE_IF_INCLUDES); 1029 1030 if (flags & TRANSPORT_PUSH_OPTIONS) { 1031 struct string_list_item *item; 1032 for_each_string_list_item(item, transport->push_options) 1033 if (set_helper_option(transport, "push-option", item->string) != 0) 1034 die(_("helper %s does not support 'push-option'"), name); 1035 } 1036 } 1037 1038 static int push_refs_with_push(struct transport *transport, 1039 struct ref *remote_refs, int flags) 1040 { 1041 int force_all = flags & TRANSPORT_PUSH_FORCE; 1042 int mirror = flags & TRANSPORT_PUSH_MIRROR; 1043 int atomic = flags & TRANSPORT_PUSH_ATOMIC; 1044 struct helper_data *data = transport->data; 1045 struct strbuf buf = STRBUF_INIT; 1046 struct ref *ref; 1047 struct string_list cas_options = STRING_LIST_INIT_DUP; 1048 struct string_list_item *cas_option; 1049 1050 get_helper(transport); 1051 if (!data->push) 1052 return 1; 1053 1054 for (ref = remote_refs; ref; ref = ref->next) { 1055 if (!ref->peer_ref && !mirror) 1056 continue; 1057 1058 /* Check for statuses set by set_ref_status_for_push() */ 1059 switch (ref->status) { 1060 case REF_STATUS_REJECT_NONFASTFORWARD: 1061 case REF_STATUS_REJECT_STALE: 1062 case REF_STATUS_REJECT_ALREADY_EXISTS: 1063 case REF_STATUS_REJECT_REMOTE_UPDATED: 1064 if (atomic) { 1065 reject_atomic_push(remote_refs, mirror); 1066 string_list_clear(&cas_options, 0); 1067 strbuf_release(&buf); 1068 return 0; 1069 } else 1070 continue; 1071 case REF_STATUS_UPTODATE: 1072 continue; 1073 default: 1074 ; /* do nothing */ 1075 } 1076 1077 if (force_all) 1078 ref->force = 1; 1079 1080 strbuf_addstr(&buf, "push "); 1081 if (!ref->deletion) { 1082 if (ref->force) 1083 strbuf_addch(&buf, '+'); 1084 if (ref->peer_ref) 1085 strbuf_addstr(&buf, ref->peer_ref->name); 1086 else 1087 strbuf_add_oid_hex(&buf, &ref->new_oid); 1088 } 1089 strbuf_addch(&buf, ':'); 1090 strbuf_addstr(&buf, ref->name); 1091 strbuf_addch(&buf, '\n'); 1092 1093 /* 1094 * The "--force-with-lease" options without explicit 1095 * values to expect have already been expanded into 1096 * the ref->old_oid_expect[] field; we can ignore 1097 * transport->smart_options->cas altogether and instead 1098 * can enumerate them from the refs. 1099 */ 1100 if (ref->expect_old_sha1) { 1101 struct strbuf cas = STRBUF_INIT; 1102 strbuf_addf(&cas, "%s:%s", 1103 ref->name, oid_to_hex(&ref->old_oid_expect)); 1104 string_list_append_nodup(&cas_options, 1105 strbuf_detach(&cas, NULL)); 1106 } 1107 } 1108 if (buf.len == 0) { 1109 string_list_clear(&cas_options, 0); 1110 return 0; 1111 } 1112 1113 for_each_string_list_item(cas_option, &cas_options) 1114 set_helper_option(transport, "cas", cas_option->string); 1115 set_common_push_options(transport, data->name, flags); 1116 1117 strbuf_addch(&buf, '\n'); 1118 sendline(data, &buf); 1119 strbuf_release(&buf); 1120 string_list_clear(&cas_options, 0); 1121 1122 return push_update_refs_status(data, remote_refs, flags); 1123 } 1124 1125 static int push_refs_with_export(struct transport *transport, 1126 struct ref *remote_refs, int flags) 1127 { 1128 struct ref *ref; 1129 struct child_process *helper, exporter; 1130 struct helper_data *data = transport->data; 1131 struct string_list revlist_args = STRING_LIST_INIT_DUP; 1132 struct strbuf buf = STRBUF_INIT; 1133 1134 if (!data->rs.nr) 1135 die(_("remote-helper doesn't support push; refspec needed")); 1136 1137 set_common_push_options(transport, data->name, flags); 1138 if (flags & TRANSPORT_PUSH_FORCE) { 1139 if (set_helper_option(transport, "force", "true") != 0) 1140 warning(_("helper %s does not support '--force'"), data->name); 1141 } 1142 1143 helper = get_helper(transport); 1144 1145 write_constant(helper->in, "export\n"); 1146 1147 for (ref = remote_refs; ref; ref = ref->next) { 1148 char *private; 1149 struct object_id oid; 1150 1151 private = apply_refspecs(&data->rs, ref->name); 1152 if (private && !repo_get_oid(the_repository, private, &oid)) { 1153 strbuf_addf(&buf, "^%s", private); 1154 string_list_append_nodup(&revlist_args, 1155 strbuf_detach(&buf, NULL)); 1156 oidcpy(&ref->old_oid, &oid); 1157 } 1158 free(private); 1159 1160 if (ref->peer_ref) { 1161 if (strcmp(ref->name, ref->peer_ref->name)) { 1162 if (!ref->deletion) { 1163 const char *name; 1164 int flag; 1165 1166 /* Follow symbolic refs (mainly for HEAD). */ 1167 name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), 1168 ref->peer_ref->name, 1169 RESOLVE_REF_READING, 1170 &oid, 1171 &flag); 1172 if (!name || !(flag & REF_ISSYMREF)) 1173 name = ref->peer_ref->name; 1174 1175 strbuf_addf(&buf, "%s:%s", name, ref->name); 1176 } else 1177 strbuf_addf(&buf, ":%s", ref->name); 1178 1179 string_list_append(&revlist_args, "--refspec"); 1180 string_list_append(&revlist_args, buf.buf); 1181 strbuf_release(&buf); 1182 } 1183 if (!ref->deletion) 1184 string_list_append(&revlist_args, ref->peer_ref->name); 1185 } 1186 } 1187 1188 if (get_exporter(transport, &exporter, &revlist_args)) 1189 die(_("couldn't run fast-export")); 1190 1191 string_list_clear(&revlist_args, 1); 1192 1193 if (finish_command(&exporter)) 1194 die(_("error while running fast-export")); 1195 if (push_update_refs_status(data, remote_refs, flags)) 1196 return 1; 1197 1198 if (data->export_marks) { 1199 strbuf_addf(&buf, "%s.tmp", data->export_marks); 1200 if (rename(buf.buf, data->export_marks)) 1201 warning_errno(_("could not rename '%s' to '%s'"), 1202 buf.buf, data->export_marks); 1203 strbuf_release(&buf); 1204 } 1205 1206 return 0; 1207 } 1208 1209 static int push_refs(struct transport *transport, 1210 struct ref *remote_refs, int flags) 1211 { 1212 struct helper_data *data = transport->data; 1213 1214 if (process_connect(transport, 1)) 1215 return transport->vtable->push_refs(transport, remote_refs, flags); 1216 1217 if (!remote_refs) { 1218 fprintf(stderr, 1219 _("No refs in common and none specified; doing nothing.\n" 1220 "Perhaps you should specify a branch.\n")); 1221 return 0; 1222 } 1223 1224 if (data->push) 1225 return push_refs_with_push(transport, remote_refs, flags); 1226 1227 if (data->export) 1228 return push_refs_with_export(transport, remote_refs, flags); 1229 1230 return -1; 1231 } 1232 1233 1234 static int has_attribute(const char *attrs, const char *attr) 1235 { 1236 int len; 1237 if (!attrs) 1238 return 0; 1239 1240 len = strlen(attr); 1241 for (;;) { 1242 const char *space = strchrnul(attrs, ' '); 1243 if (len == space - attrs && !strncmp(attrs, attr, len)) 1244 return 1; 1245 if (!*space) 1246 return 0; 1247 attrs = space + 1; 1248 } 1249 } 1250 1251 static struct ref *get_refs_list(struct transport *transport, int for_push, 1252 struct transport_ls_refs_options *transport_options) 1253 { 1254 get_helper(transport); 1255 1256 if (process_connect(transport, for_push)) 1257 return transport->vtable->get_refs_list(transport, for_push, 1258 transport_options); 1259 1260 return get_refs_list_using_list(transport, for_push); 1261 } 1262 1263 static struct ref *get_refs_list_using_list(struct transport *transport, 1264 int for_push) 1265 { 1266 struct helper_data *data = transport->data; 1267 struct child_process *helper; 1268 struct ref *ret = NULL; 1269 struct ref **tail = &ret; 1270 struct ref *posn; 1271 struct strbuf buf = STRBUF_INIT; 1272 1273 data->get_refs_list_called = 1; 1274 helper = get_helper(transport); 1275 1276 if (data->object_format) 1277 set_helper_option(transport, "object-format", "true"); 1278 1279 if (data->push && for_push) 1280 write_constant(helper->in, "list for-push\n"); 1281 else 1282 write_constant(helper->in, "list\n"); 1283 1284 while (1) { 1285 char *eov, *eon; 1286 if (recvline(data, &buf)) 1287 exit(128); 1288 1289 if (!*buf.buf) 1290 break; 1291 else if (buf.buf[0] == ':') { 1292 const char *value; 1293 if (skip_prefix(buf.buf, ":object-format ", &value)) { 1294 int algo = hash_algo_by_name(value); 1295 if (algo == GIT_HASH_UNKNOWN) 1296 die(_("unsupported object format '%s'"), 1297 value); 1298 transport->hash_algo = &hash_algos[algo]; 1299 } 1300 continue; 1301 } 1302 1303 eov = strchr(buf.buf, ' '); 1304 if (!eov) 1305 die(_("malformed response in ref list: %s"), buf.buf); 1306 eon = strchr(eov + 1, ' '); 1307 *eov = '\0'; 1308 if (eon) 1309 *eon = '\0'; 1310 *tail = alloc_ref(eov + 1); 1311 if (buf.buf[0] == '@') 1312 (*tail)->symref = xstrdup(buf.buf + 1); 1313 else if (buf.buf[0] != '?') 1314 get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo); 1315 if (eon) { 1316 if (has_attribute(eon + 1, "unchanged")) { 1317 (*tail)->status |= REF_STATUS_UPTODATE; 1318 if (refs_read_ref(get_main_ref_store(the_repository), (*tail)->name, &(*tail)->old_oid) < 0) 1319 die(_("could not read ref %s"), 1320 (*tail)->name); 1321 } 1322 } 1323 tail = &((*tail)->next); 1324 } 1325 if (debug) 1326 fprintf(stderr, "Debug: Read ref listing.\n"); 1327 strbuf_release(&buf); 1328 1329 for (posn = ret; posn; posn = posn->next) 1330 resolve_remote_symref(posn, ret); 1331 1332 return ret; 1333 } 1334 1335 static int get_bundle_uri(struct transport *transport) 1336 { 1337 get_helper(transport); 1338 1339 if (process_connect(transport, 0)) 1340 return transport->vtable->get_bundle_uri(transport); 1341 1342 return -1; 1343 } 1344 1345 static struct transport_vtable vtable = { 1346 .set_option = set_helper_option, 1347 .get_refs_list = get_refs_list, 1348 .get_bundle_uri = get_bundle_uri, 1349 .fetch_refs = fetch_refs, 1350 .fetch_object_info = fetch_object_info_helper, 1351 .push_refs = push_refs, 1352 .connect = connect_helper, 1353 .disconnect = release_helper 1354 }; 1355 1356 int transport_helper_init(struct transport *transport, const char *name) 1357 { 1358 struct helper_data *data = xcalloc(1, sizeof(*data)); 1359 data->name = xstrdup(name); 1360 1361 transport_check_allowed(name); 1362 1363 if (getenv("GIT_TRANSPORT_HELPER_DEBUG")) 1364 debug = 1; 1365 1366 list_objects_filter_init(&data->transport_options.filter_options); 1367 1368 transport->data = data; 1369 transport->vtable = &vtable; 1370 transport->smart_options = &(data->transport_options); 1371 return 0; 1372 } 1373 1374 /* 1375 * Linux pipes can buffer 65536 bytes at once (and most platforms can 1376 * buffer less), so attempt reads and writes with up to that size. 1377 */ 1378 #define BUFFERSIZE 65536 1379 /* This should be enough to hold debugging message. */ 1380 #define PBUFFERSIZE 8192 1381 1382 static int transfer_debug_enabled = -1; 1383 1384 /* Print bidirectional transfer loop debug message. */ 1385 __attribute__((format (printf, 1, 2))) 1386 static void transfer_debug(const char *fmt, ...) 1387 { 1388 va_list args; 1389 char msgbuf[PBUFFERSIZE]; 1390 1391 if (transfer_debug_enabled < 0) 1392 BUG("somebody forgot to check GIT_TRANSLOOP_DEBUG!"); 1393 if (!transfer_debug_enabled) 1394 return; 1395 1396 va_start(args, fmt); 1397 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args); 1398 va_end(args); 1399 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf); 1400 } 1401 1402 /* Stream state: More data may be coming in this direction. */ 1403 #define SSTATE_TRANSFERRING 0 1404 /* 1405 * Stream state: No more data coming in this direction, flushing rest of 1406 * data. 1407 */ 1408 #define SSTATE_FLUSHING 1 1409 /* Stream state: Transfer in this direction finished. */ 1410 #define SSTATE_FINISHED 2 1411 1412 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING) 1413 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING) 1414 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING) 1415 1416 /* Unidirectional transfer. */ 1417 struct unidirectional_transfer { 1418 /* Source */ 1419 int src; 1420 /* Destination */ 1421 int dest; 1422 /* Is source socket? */ 1423 int src_is_sock; 1424 /* Is destination socket? */ 1425 int dest_is_sock; 1426 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */ 1427 int state; 1428 /* Buffer. */ 1429 char buf[BUFFERSIZE]; 1430 /* Buffer used. */ 1431 size_t bufuse; 1432 /* Name of source. */ 1433 const char *src_name; 1434 /* Name of destination. */ 1435 const char *dest_name; 1436 }; 1437 1438 /* Closes the target (for writing) if transfer has finished. */ 1439 static void udt_close_if_finished(struct unidirectional_transfer *t) 1440 { 1441 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) { 1442 t->state = SSTATE_FINISHED; 1443 if (t->dest_is_sock) 1444 shutdown(t->dest, SHUT_WR); 1445 else 1446 close(t->dest); 1447 transfer_debug("Closed %s.", t->dest_name); 1448 } 1449 } 1450 1451 /* 1452 * Tries to read data from source into buffer. If buffer is full, 1453 * no data is read. Returns 0 on success, -1 on error. 1454 */ 1455 static int udt_do_read(struct unidirectional_transfer *t) 1456 { 1457 ssize_t bytes; 1458 1459 if (t->bufuse == BUFFERSIZE) 1460 return 0; /* No space for more. */ 1461 1462 transfer_debug("%s is readable", t->src_name); 1463 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse); 1464 if (bytes < 0) { 1465 error_errno(_("read(%s) failed"), t->src_name); 1466 return -1; 1467 } else if (bytes == 0) { 1468 transfer_debug("%s EOF (with %i bytes in buffer)", 1469 t->src_name, (int)t->bufuse); 1470 t->state = SSTATE_FLUSHING; 1471 } else { 1472 t->bufuse += bytes; 1473 transfer_debug("Read %i bytes from %s (buffer now at %i)", 1474 (int)bytes, t->src_name, (int)t->bufuse); 1475 } 1476 return 0; 1477 } 1478 1479 /* Tries to write data from buffer into destination. If buffer is empty, 1480 * no data is written. Returns 0 on success, -1 on error. 1481 */ 1482 static int udt_do_write(struct unidirectional_transfer *t) 1483 { 1484 ssize_t bytes; 1485 1486 if (t->bufuse == 0) 1487 return 0; /* Nothing to write. */ 1488 1489 transfer_debug("%s is writable", t->dest_name); 1490 bytes = xwrite(t->dest, t->buf, t->bufuse); 1491 if (bytes < 0) { 1492 error_errno(_("write(%s) failed"), t->dest_name); 1493 return -1; 1494 } else if (bytes > 0) { 1495 t->bufuse -= bytes; 1496 if (t->bufuse) 1497 memmove(t->buf, t->buf + bytes, t->bufuse); 1498 transfer_debug("Wrote %i bytes to %s (buffer now at %i)", 1499 (int)bytes, t->dest_name, (int)t->bufuse); 1500 } 1501 return 0; 1502 } 1503 1504 1505 /* State of bidirectional transfer loop. */ 1506 struct bidirectional_transfer_state { 1507 /* Direction from program to git. */ 1508 struct unidirectional_transfer ptg; 1509 /* Direction from git to program. */ 1510 struct unidirectional_transfer gtp; 1511 }; 1512 1513 static void *udt_copy_task_routine(void *udt) 1514 { 1515 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt; 1516 while (t->state != SSTATE_FINISHED) { 1517 if (STATE_NEEDS_READING(t->state)) 1518 if (udt_do_read(t)) 1519 return NULL; 1520 if (STATE_NEEDS_WRITING(t->state)) 1521 if (udt_do_write(t)) 1522 return NULL; 1523 if (STATE_NEEDS_CLOSING(t->state)) 1524 udt_close_if_finished(t); 1525 } 1526 return udt; /* Just some non-NULL value. */ 1527 } 1528 1529 #ifndef NO_PTHREADS 1530 1531 /* 1532 * Join thread, with appropriate errors on failure. Name is name for the 1533 * thread (for error messages). Returns 0 on success, 1 on failure. 1534 */ 1535 static int tloop_join(pthread_t thread, const char *name) 1536 { 1537 int err; 1538 void *tret; 1539 err = pthread_join(thread, &tret); 1540 if (!tret) { 1541 error(_("%s thread failed"), name); 1542 return 1; 1543 } 1544 if (err) { 1545 error(_("%s thread failed to join: %s"), name, strerror(err)); 1546 return 1; 1547 } 1548 return 0; 1549 } 1550 1551 /* 1552 * Spawn the transfer tasks and then wait for them. Returns 0 on success, 1553 * -1 on failure. 1554 */ 1555 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) 1556 { 1557 pthread_t gtp_thread; 1558 pthread_t ptg_thread; 1559 int err; 1560 int ret = 0; 1561 err = pthread_create(>p_thread, NULL, udt_copy_task_routine, 1562 &s->gtp); 1563 if (err) 1564 die(_("can't start thread for copying data: %s"), strerror(err)); 1565 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine, 1566 &s->ptg); 1567 if (err) 1568 die(_("can't start thread for copying data: %s"), strerror(err)); 1569 1570 ret |= tloop_join(gtp_thread, "Git to program copy"); 1571 ret |= tloop_join(ptg_thread, "Program to git copy"); 1572 return ret; 1573 } 1574 #else 1575 1576 /* Close the source and target (for writing) for transfer. */ 1577 static void udt_kill_transfer(struct unidirectional_transfer *t) 1578 { 1579 t->state = SSTATE_FINISHED; 1580 /* 1581 * Socket read end left open isn't a disaster if nobody 1582 * attempts to read from it (mingw compat headers do not 1583 * have SHUT_RD)... 1584 * 1585 * We can't fully close the socket since otherwise gtp 1586 * task would first close the socket it sends data to 1587 * while closing the ptg file descriptors. 1588 */ 1589 if (!t->src_is_sock) 1590 close(t->src); 1591 if (t->dest_is_sock) 1592 shutdown(t->dest, SHUT_WR); 1593 else 1594 close(t->dest); 1595 } 1596 1597 /* 1598 * Join process, with appropriate errors on failure. Name is name for the 1599 * process (for error messages). Returns 0 on success, 1 on failure. 1600 */ 1601 static int tloop_join(pid_t pid, const char *name) 1602 { 1603 int tret; 1604 if (waitpid(pid, &tret, 0) < 0) { 1605 error_errno(_("%s process failed to wait"), name); 1606 return 1; 1607 } 1608 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) { 1609 error(_("%s process failed"), name); 1610 return 1; 1611 } 1612 return 0; 1613 } 1614 1615 /* 1616 * Spawn the transfer tasks and then wait for them. Returns 0 on success, 1617 * -1 on failure. 1618 */ 1619 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s) 1620 { 1621 pid_t pid1, pid2; 1622 int ret = 0; 1623 1624 /* Fork thread #1: git to program. */ 1625 pid1 = fork(); 1626 if (pid1 < 0) 1627 die_errno(_("can't start thread for copying data")); 1628 else if (pid1 == 0) { 1629 udt_kill_transfer(&s->ptg); 1630 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1); 1631 } 1632 1633 /* Fork thread #2: program to git. */ 1634 pid2 = fork(); 1635 if (pid2 < 0) 1636 die_errno(_("can't start thread for copying data")); 1637 else if (pid2 == 0) { 1638 udt_kill_transfer(&s->gtp); 1639 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1); 1640 } 1641 1642 /* 1643 * Close both streams in parent as to not interfere with 1644 * end of file detection and wait for both tasks to finish. 1645 */ 1646 udt_kill_transfer(&s->gtp); 1647 udt_kill_transfer(&s->ptg); 1648 ret |= tloop_join(pid1, "Git to program copy"); 1649 ret |= tloop_join(pid2, "Program to git copy"); 1650 return ret; 1651 } 1652 #endif 1653 1654 /* 1655 * Copies data from stdin to output and from input to stdout simultaneously. 1656 * Additionally filtering through given filter. If filter is NULL, uses 1657 * identity filter. 1658 */ 1659 int bidirectional_transfer_loop(int input, int output) 1660 { 1661 struct bidirectional_transfer_state state; 1662 1663 if (transfer_debug_enabled < 0) 1664 transfer_debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0; 1665 1666 /* Fill the state fields. */ 1667 state.ptg.src = input; 1668 state.ptg.dest = 1; 1669 state.ptg.src_is_sock = (input == output); 1670 state.ptg.dest_is_sock = 0; 1671 state.ptg.state = SSTATE_TRANSFERRING; 1672 state.ptg.bufuse = 0; 1673 state.ptg.src_name = "remote input"; 1674 state.ptg.dest_name = "stdout"; 1675 1676 state.gtp.src = 0; 1677 state.gtp.dest = output; 1678 state.gtp.src_is_sock = 0; 1679 state.gtp.dest_is_sock = (input == output); 1680 state.gtp.state = SSTATE_TRANSFERRING; 1681 state.gtp.bufuse = 0; 1682 state.gtp.src_name = "stdin"; 1683 state.gtp.dest_name = "remote output"; 1684 1685 return tloop_spawnwait_tasks(&state); 1686 } 1687 1688 void reject_atomic_push(struct ref *remote_refs, int mirror_mode) 1689 { 1690 struct ref *ref; 1691 1692 /* Mark other refs as failed */ 1693 for (ref = remote_refs; ref; ref = ref->next) { 1694 if (!ref->peer_ref && !mirror_mode) 1695 continue; 1696 1697 switch (ref->status) { 1698 case REF_STATUS_NONE: 1699 case REF_STATUS_OK: 1700 case REF_STATUS_EXPECTING_REPORT: 1701 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED; 1702 continue; 1703 default: 1704 break; /* do nothing */ 1705 } 1706 } 1707 return; 1708 }