4 #include "run-command.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
12 #include "argv-array.h"
15 #include "transport-internal.h"
22 struct child_process *helper;
31 stateless_connect : 1,
33 check_connectivity : 1,
34 no_disconnect_req : 1,
35 no_private_update : 1;
38 /* These go from remote name (as in "list") to private name */
40 /* Transport options for fetch-pack/send-pack (should one of
43 struct git_transport_options transport_options;
46 static void sendline(struct helper_data *helper, struct strbuf *buffer)
49 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
50 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
51 die_errno(_("full write to remote helper failed"));
54 static int recvline_fh(FILE *helper, struct strbuf *buffer)
58 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
59 if (strbuf_getline(buffer, helper) == EOF) {
61 fprintf(stderr, "Debug: Remote helper quit.\n");
66 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
70 static int recvline(struct helper_data *helper, struct strbuf *buffer)
72 return recvline_fh(helper->out, buffer);
75 static void write_constant(int fd, const char *str)
78 fprintf(stderr, "Debug: Remote helper: -> %s", str);
79 if (write_in_full(fd, str, strlen(str)) < 0)
80 die_errno(_("full write to remote helper failed"));
83 static const char *remove_ext_force(const char *url)
86 const char *colon = strchr(url, ':');
87 if (colon && colon[1] == ':')
93 static void do_take_over(struct transport *transport)
95 struct helper_data *data;
96 data = (struct helper_data *)transport->data;
97 transport_take_over(transport, data->helper);
102 static void standard_options(struct transport *t);
104 static struct child_process *get_helper(struct transport *transport)
106 struct helper_data *data = transport->data;
107 struct strbuf buf = STRBUF_INIT;
108 struct child_process *helper;
115 helper = xmalloc(sizeof(*helper));
116 child_process_init(helper);
120 argv_array_pushf(&helper->args, "git-remote-%s", data->name);
121 argv_array_push(&helper->args, transport->remote->name);
122 argv_array_push(&helper->args, remove_ext_force(transport->url));
124 helper->silent_exec_failure = 1;
127 argv_array_pushf(&helper->env_array, "%s=%s",
128 GIT_DIR_ENVIRONMENT, get_git_dir());
130 helper->trace2_child_class = helper->args.argv[0]; /* "remote-<name>" */
132 code = start_command(helper);
133 if (code < 0 && errno == ENOENT)
134 die(_("unable to find remote helper for '%s'"), data->name);
138 data->helper = helper;
139 data->no_disconnect_req = 0;
140 refspec_init(&data->rs, REFSPEC_FETCH);
143 * Open the output as FILE* so strbuf_getline_*() family of
144 * functions can be used.
145 * Do this with duped fd because fclose() will close the fd,
146 * and stuff like taking over will require the fd to remain.
148 duped = dup(helper->out);
150 die_errno(_("can't dup helper output fd"));
151 data->out = xfdopen(duped, "r");
153 write_constant(helper->in, "capabilities\n");
156 const char *capname, *arg;
158 if (recvline(data, &buf))
164 if (*buf.buf == '*') {
165 capname = buf.buf + 1;
171 fprintf(stderr, "Debug: Got cap %s\n", capname);
172 if (!strcmp(capname, "fetch"))
174 else if (!strcmp(capname, "option"))
176 else if (!strcmp(capname, "push"))
178 else if (!strcmp(capname, "import"))
180 else if (!strcmp(capname, "bidi-import"))
181 data->bidi_import = 1;
182 else if (!strcmp(capname, "export"))
184 else if (!strcmp(capname, "check-connectivity"))
185 data->check_connectivity = 1;
186 else if (skip_prefix(capname, "refspec ", &arg)) {
187 refspec_append(&data->rs, arg);
188 } else if (!strcmp(capname, "connect")) {
190 } else if (!strcmp(capname, "stateless-connect")) {
191 data->stateless_connect = 1;
192 } else if (!strcmp(capname, "signed-tags")) {
193 data->signed_tags = 1;
194 } else if (skip_prefix(capname, "export-marks ", &arg)) {
195 data->export_marks = xstrdup(arg);
196 } else if (skip_prefix(capname, "import-marks ", &arg)) {
197 data->import_marks = xstrdup(arg);
198 } else if (starts_with(capname, "no-private-update")) {
199 data->no_private_update = 1;
200 } else if (mandatory) {
201 die(_("unknown mandatory capability %s; this remote "
202 "helper probably needs newer version of Git"),
206 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
207 warning(_("this remote helper should implement refspec capability"));
209 strbuf_release(&buf);
211 fprintf(stderr, "Debug: Capabilities complete.\n");
212 standard_options(transport);
216 static int disconnect_helper(struct transport *transport)
218 struct helper_data *data = transport->data;
223 fprintf(stderr, "Debug: Disconnecting.\n");
224 if (!data->no_disconnect_req) {
226 * Ignore write errors; there's nothing we can do,
227 * since we're about to close the pipe anyway. And the
228 * most likely error is EPIPE due to the helper dying
229 * to report an error itself.
231 sigchain_push(SIGPIPE, SIG_IGN);
232 xwrite(data->helper->in, "\n", 1);
233 sigchain_pop(SIGPIPE);
235 close(data->helper->in);
236 close(data->helper->out);
238 res = finish_command(data->helper);
239 FREE_AND_NULL(data->helper);
244 static const char *unsupported_options[] = {
245 TRANS_OPT_UPLOADPACK,
246 TRANS_OPT_RECEIVEPACK,
251 static const char *boolean_options[] = {
254 TRANS_OPT_FOLLOWTAGS,
255 TRANS_OPT_DEEPEN_RELATIVE
258 static int strbuf_set_helper_option(struct helper_data *data,
264 if (recvline(data, buf))
267 if (!strcmp(buf->buf, "ok"))
269 else if (starts_with(buf->buf, "error"))
271 else if (!strcmp(buf->buf, "unsupported"))
274 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
280 static int string_list_set_helper_option(struct helper_data *data,
282 struct string_list *list)
284 struct strbuf buf = STRBUF_INIT;
287 for (i = 0; i < list->nr; i++) {
288 strbuf_addf(&buf, "option %s ", name);
289 quote_c_style(list->items[i].string, &buf, NULL, 0);
290 strbuf_addch(&buf, '\n');
292 if ((ret = strbuf_set_helper_option(data, &buf)))
296 strbuf_release(&buf);
300 static int set_helper_option(struct transport *transport,
301 const char *name, const char *value)
303 struct helper_data *data = transport->data;
304 struct strbuf buf = STRBUF_INIT;
305 int i, ret, is_bool = 0;
307 get_helper(transport);
312 if (!strcmp(name, "deepen-not"))
313 return string_list_set_helper_option(data, name,
314 (struct string_list *)value);
316 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
317 if (!strcmp(name, unsupported_options[i]))
321 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
322 if (!strcmp(name, boolean_options[i])) {
328 strbuf_addf(&buf, "option %s ", name);
330 strbuf_addstr(&buf, value ? "true" : "false");
332 quote_c_style(value, &buf, NULL, 0);
333 strbuf_addch(&buf, '\n');
335 ret = strbuf_set_helper_option(data, &buf);
336 strbuf_release(&buf);
340 static void standard_options(struct transport *t)
345 set_helper_option(t, "progress", t->progress ? "true" : "false");
347 xsnprintf(buf, sizeof(buf), "%d", v + 1);
348 set_helper_option(t, "verbosity", buf);
351 case TRANSPORT_FAMILY_ALL:
353 * this is already the default,
354 * do not break old remote helpers by setting "all" here
357 case TRANSPORT_FAMILY_IPV4:
358 set_helper_option(t, "family", "ipv4");
360 case TRANSPORT_FAMILY_IPV6:
361 set_helper_option(t, "family", "ipv6");
366 static int release_helper(struct transport *transport)
369 struct helper_data *data = transport->data;
370 refspec_clear(&data->rs);
371 res = disconnect_helper(transport);
372 free(transport->data);
376 static int fetch_with_fetch(struct transport *transport,
377 int nr_heads, struct ref **to_fetch)
379 struct helper_data *data = transport->data;
381 struct strbuf buf = STRBUF_INIT;
383 for (i = 0; i < nr_heads; i++) {
384 const struct ref *posn = to_fetch[i];
385 if (posn->status & REF_STATUS_UPTODATE)
388 strbuf_addf(&buf, "fetch %s %s\n",
389 oid_to_hex(&posn->old_oid),
390 posn->symref ? posn->symref : posn->name);
393 strbuf_addch(&buf, '\n');
394 sendline(data, &buf);
397 if (recvline(data, &buf))
400 if (starts_with(buf.buf, "lock ")) {
401 const char *name = buf.buf + 5;
402 if (transport->pack_lockfile)
403 warning(_("%s also locked %s"), data->name, name);
405 transport->pack_lockfile = xstrdup(name);
407 else if (data->check_connectivity &&
408 data->transport_options.check_self_contained_and_connected &&
409 !strcmp(buf.buf, "connectivity-ok"))
410 data->transport_options.self_contained_and_connected = 1;
414 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
416 strbuf_release(&buf);
420 static int get_importer(struct transport *transport, struct child_process *fastimport)
422 struct child_process *helper = get_helper(transport);
423 struct helper_data *data = transport->data;
424 int cat_blob_fd, code;
425 child_process_init(fastimport);
426 fastimport->in = xdup(helper->out);
427 argv_array_push(&fastimport->args, "fast-import");
428 argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
430 if (data->bidi_import) {
431 cat_blob_fd = xdup(helper->in);
432 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
434 fastimport->git_cmd = 1;
436 code = start_command(fastimport);
440 static int get_exporter(struct transport *transport,
441 struct child_process *fastexport,
442 struct string_list *revlist_args)
444 struct helper_data *data = transport->data;
445 struct child_process *helper = get_helper(transport);
448 child_process_init(fastexport);
450 /* we need to duplicate helper->in because we want to use it after
451 * fastexport is done with it. */
452 fastexport->out = dup(helper->in);
453 argv_array_push(&fastexport->args, "fast-export");
454 argv_array_push(&fastexport->args, "--use-done-feature");
455 argv_array_push(&fastexport->args, data->signed_tags ?
456 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
457 if (data->export_marks)
458 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
459 if (data->import_marks)
460 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
462 for (i = 0; i < revlist_args->nr; i++)
463 argv_array_push(&fastexport->args, revlist_args->items[i].string);
465 fastexport->git_cmd = 1;
466 return start_command(fastexport);
469 static int fetch_with_import(struct transport *transport,
470 int nr_heads, struct ref **to_fetch)
472 struct child_process fastimport;
473 struct helper_data *data = transport->data;
476 struct strbuf buf = STRBUF_INIT;
478 get_helper(transport);
480 if (get_importer(transport, &fastimport))
481 die(_("couldn't run fast-import"));
483 for (i = 0; i < nr_heads; i++) {
485 if (posn->status & REF_STATUS_UPTODATE)
488 strbuf_addf(&buf, "import %s\n",
489 posn->symref ? posn->symref : posn->name);
490 sendline(data, &buf);
494 write_constant(data->helper->in, "\n");
496 * remote-helpers that advertise the bidi-import capability are required to
497 * buffer the complete batch of import commands until this newline before
498 * sending data to fast-import.
499 * These helpers read back data from fast-import on their stdin, which could
500 * be mixed with import commands, otherwise.
503 if (finish_command(&fastimport))
504 die(_("error while running fast-import"));
507 * The fast-import stream of a remote helper that advertises
508 * the "refspec" capability writes to the refs named after the
509 * right hand side of the first refspec matching each ref we
512 * (If no "refspec" capability was specified, for historical
513 * reasons we default to the equivalent of *:*.)
515 * Store the result in to_fetch[i].old_sha1. Callers such
516 * as "git fetch" can use the value to write feedback to the
517 * terminal, populate FETCH_HEAD, and determine what new value
518 * should be written to peer_ref if the update is a
519 * fast-forward or this is a forced update.
521 for (i = 0; i < nr_heads; i++) {
522 char *private, *name;
524 if (posn->status & REF_STATUS_UPTODATE)
526 name = posn->symref ? posn->symref : posn->name;
528 private = apply_refspecs(&data->rs, name);
530 private = xstrdup(name);
532 if (read_ref(private, &posn->old_oid) < 0)
533 die(_("could not read ref %s"), private);
537 strbuf_release(&buf);
541 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
543 struct helper_data *data = transport->data;
547 struct child_process *helper;
549 helper = get_helper(transport);
552 * Yes, dup the pipe another time, as we need unbuffered version
553 * of input pipe as FILE*. fclose() closes the underlying fd and
554 * stream buffering only can be changed before first I/O operation
557 duped = dup(helper->out);
559 die_errno(_("can't dup helper output fd"));
560 input = xfdopen(duped, "r");
561 setvbuf(input, NULL, _IONBF, 0);
563 sendline(data, cmdbuf);
564 if (recvline_fh(input, cmdbuf))
567 if (!strcmp(cmdbuf->buf, "")) {
568 data->no_disconnect_req = 1;
570 fprintf(stderr, "Debug: Smart transport connection "
573 } else if (!strcmp(cmdbuf->buf, "fallback")) {
575 fprintf(stderr, "Debug: Falling back to dumb "
578 die(_("unknown response to connect: %s"),
586 static int process_connect_service(struct transport *transport,
587 const char *name, const char *exec)
589 struct helper_data *data = transport->data;
590 struct strbuf cmdbuf = STRBUF_INIT;
594 * Handle --upload-pack and friends. This is fire and forget...
595 * just warn if it fails.
597 if (strcmp(name, exec)) {
598 int r = set_helper_option(transport, "servpath", exec);
600 warning(_("setting remote service path not supported by protocol"));
602 warning(_("invalid remote service path"));
606 strbuf_addf(&cmdbuf, "connect %s\n", name);
607 ret = run_connect(transport, &cmdbuf);
608 } else if (data->stateless_connect &&
609 (get_protocol_version_config() == protocol_v2) &&
610 !strcmp("git-upload-pack", name)) {
611 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
612 ret = run_connect(transport, &cmdbuf);
614 transport->stateless_rpc = 1;
617 strbuf_release(&cmdbuf);
621 static int process_connect(struct transport *transport,
624 struct helper_data *data = transport->data;
628 name = for_push ? "git-receive-pack" : "git-upload-pack";
630 exec = data->transport_options.receivepack;
632 exec = data->transport_options.uploadpack;
634 return process_connect_service(transport, name, exec);
637 static int connect_helper(struct transport *transport, const char *name,
638 const char *exec, int fd[2])
640 struct helper_data *data = transport->data;
642 /* Get_helper so connect is inited. */
643 get_helper(transport);
645 die(_("operation not supported by protocol"));
647 if (!process_connect_service(transport, name, exec))
648 die(_("can't connect to subservice %s"), name);
650 fd[0] = data->helper->out;
651 fd[1] = data->helper->in;
655 static int fetch(struct transport *transport,
656 int nr_heads, struct ref **to_fetch)
658 struct helper_data *data = transport->data;
661 if (process_connect(transport, 0)) {
662 do_take_over(transport);
663 return transport->vtable->fetch(transport, nr_heads, to_fetch);
667 for (i = 0; i < nr_heads; i++)
668 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
674 if (data->check_connectivity &&
675 data->transport_options.check_self_contained_and_connected)
676 set_helper_option(transport, "check-connectivity", "true");
678 if (transport->cloning)
679 set_helper_option(transport, "cloning", "true");
681 if (data->transport_options.update_shallow)
682 set_helper_option(transport, "update-shallow", "true");
684 if (data->transport_options.filter_options.choice) {
685 struct strbuf expanded_filter_spec = STRBUF_INIT;
686 expand_list_objects_filter_spec(
687 &data->transport_options.filter_options,
688 &expanded_filter_spec);
689 set_helper_option(transport, "filter",
690 expanded_filter_spec.buf);
691 strbuf_release(&expanded_filter_spec);
694 if (data->transport_options.negotiation_tips)
695 warning("Ignoring --negotiation-tip because the protocol does not support it.");
698 return fetch_with_fetch(transport, nr_heads, to_fetch);
701 return fetch_with_import(transport, nr_heads, to_fetch);
706 static int push_update_ref_status(struct strbuf *buf,
708 struct ref *remote_refs)
711 int status, forced = 0;
713 if (starts_with(buf->buf, "ok ")) {
714 status = REF_STATUS_OK;
715 refname = buf->buf + 3;
716 } else if (starts_with(buf->buf, "error ")) {
717 status = REF_STATUS_REMOTE_REJECT;
718 refname = buf->buf + 6;
720 die(_("expected ok/error, helper said '%s'"), buf->buf);
722 msg = strchr(refname, ' ');
724 struct strbuf msg_buf = STRBUF_INIT;
728 if (!unquote_c_style(&msg_buf, msg, &end))
729 msg = strbuf_detach(&msg_buf, NULL);
732 strbuf_release(&msg_buf);
734 if (!strcmp(msg, "no match")) {
735 status = REF_STATUS_NONE;
738 else if (!strcmp(msg, "up to date")) {
739 status = REF_STATUS_UPTODATE;
742 else if (!strcmp(msg, "non-fast forward")) {
743 status = REF_STATUS_REJECT_NONFASTFORWARD;
746 else if (!strcmp(msg, "already exists")) {
747 status = REF_STATUS_REJECT_ALREADY_EXISTS;
750 else if (!strcmp(msg, "fetch first")) {
751 status = REF_STATUS_REJECT_FETCH_FIRST;
754 else if (!strcmp(msg, "needs force")) {
755 status = REF_STATUS_REJECT_NEEDS_FORCE;
758 else if (!strcmp(msg, "stale info")) {
759 status = REF_STATUS_REJECT_STALE;
762 else if (!strcmp(msg, "forced update")) {
769 *ref = find_ref_by_name(*ref, refname);
771 *ref = find_ref_by_name(remote_refs, refname);
773 warning(_("helper reported unexpected status of %s"), refname);
777 if ((*ref)->status != REF_STATUS_NONE) {
779 * Earlier, the ref was marked not to be pushed, so ignore the ref
780 * status reported by the remote helper if the latter is 'no match'.
782 if (status == REF_STATUS_NONE)
786 (*ref)->status = status;
787 (*ref)->forced_update |= forced;
788 (*ref)->remote_status = msg;
789 return !(status == REF_STATUS_OK);
792 static int push_update_refs_status(struct helper_data *data,
793 struct ref *remote_refs,
796 struct strbuf buf = STRBUF_INIT;
797 struct ref *ref = remote_refs;
803 if (recvline(data, &buf)) {
811 if (push_update_ref_status(&buf, &ref, remote_refs))
814 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
817 /* propagate back the update to the remote namespace */
818 private = apply_refspecs(&data->rs, ref->name);
821 update_ref("update by helper", private, &ref->new_oid, NULL,
825 strbuf_release(&buf);
829 static void set_common_push_options(struct transport *transport,
830 const char *name, int flags)
832 if (flags & TRANSPORT_PUSH_DRY_RUN) {
833 if (set_helper_option(transport, "dry-run", "true") != 0)
834 die(_("helper %s does not support dry-run"), name);
835 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
836 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
837 die(_("helper %s does not support --signed"), name);
838 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
839 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
840 die(_("helper %s does not support --signed=if-asked"), name);
843 if (flags & TRANSPORT_PUSH_OPTIONS) {
844 struct string_list_item *item;
845 for_each_string_list_item(item, transport->push_options)
846 if (set_helper_option(transport, "push-option", item->string) != 0)
847 die(_("helper %s does not support 'push-option'"), name);
851 static int push_refs_with_push(struct transport *transport,
852 struct ref *remote_refs, int flags)
854 int force_all = flags & TRANSPORT_PUSH_FORCE;
855 int mirror = flags & TRANSPORT_PUSH_MIRROR;
856 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
857 struct helper_data *data = transport->data;
858 struct strbuf buf = STRBUF_INIT;
860 struct string_list cas_options = STRING_LIST_INIT_DUP;
861 struct string_list_item *cas_option;
863 get_helper(transport);
867 for (ref = remote_refs; ref; ref = ref->next) {
868 if (!ref->peer_ref && !mirror)
871 /* Check for statuses set by set_ref_status_for_push() */
872 switch (ref->status) {
873 case REF_STATUS_REJECT_NONFASTFORWARD:
874 case REF_STATUS_REJECT_STALE:
875 case REF_STATUS_REJECT_ALREADY_EXISTS:
877 string_list_clear(&cas_options, 0);
881 case REF_STATUS_UPTODATE:
890 strbuf_addstr(&buf, "push ");
891 if (!ref->deletion) {
893 strbuf_addch(&buf, '+');
895 strbuf_addstr(&buf, ref->peer_ref->name);
897 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
899 strbuf_addch(&buf, ':');
900 strbuf_addstr(&buf, ref->name);
901 strbuf_addch(&buf, '\n');
904 * The "--force-with-lease" options without explicit
905 * values to expect have already been expanded into
906 * the ref->old_oid_expect[] field; we can ignore
907 * transport->smart_options->cas altogether and instead
908 * can enumerate them from the refs.
910 if (ref->expect_old_sha1) {
911 struct strbuf cas = STRBUF_INIT;
912 strbuf_addf(&cas, "%s:%s",
913 ref->name, oid_to_hex(&ref->old_oid_expect));
914 string_list_append_nodup(&cas_options,
915 strbuf_detach(&cas, NULL));
919 string_list_clear(&cas_options, 0);
923 for_each_string_list_item(cas_option, &cas_options)
924 set_helper_option(transport, "cas", cas_option->string);
925 set_common_push_options(transport, data->name, flags);
927 strbuf_addch(&buf, '\n');
928 sendline(data, &buf);
929 strbuf_release(&buf);
930 string_list_clear(&cas_options, 0);
932 return push_update_refs_status(data, remote_refs, flags);
935 static int push_refs_with_export(struct transport *transport,
936 struct ref *remote_refs, int flags)
939 struct child_process *helper, exporter;
940 struct helper_data *data = transport->data;
941 struct string_list revlist_args = STRING_LIST_INIT_DUP;
942 struct strbuf buf = STRBUF_INIT;
945 die(_("remote-helper doesn't support push; refspec needed"));
947 set_common_push_options(transport, data->name, flags);
948 if (flags & TRANSPORT_PUSH_FORCE) {
949 if (set_helper_option(transport, "force", "true") != 0)
950 warning(_("helper %s does not support 'force'"), data->name);
953 helper = get_helper(transport);
955 write_constant(helper->in, "export\n");
957 for (ref = remote_refs; ref; ref = ref->next) {
959 struct object_id oid;
961 private = apply_refspecs(&data->rs, ref->name);
962 if (private && !get_oid(private, &oid)) {
963 strbuf_addf(&buf, "^%s", private);
964 string_list_append_nodup(&revlist_args,
965 strbuf_detach(&buf, NULL));
966 oidcpy(&ref->old_oid, &oid);
971 if (strcmp(ref->name, ref->peer_ref->name)) {
972 if (!ref->deletion) {
976 /* Follow symbolic refs (mainly for HEAD). */
977 name = resolve_ref_unsafe(ref->peer_ref->name,
980 if (!name || !(flag & REF_ISSYMREF))
981 name = ref->peer_ref->name;
983 strbuf_addf(&buf, "%s:%s", name, ref->name);
985 strbuf_addf(&buf, ":%s", ref->name);
987 string_list_append(&revlist_args, "--refspec");
988 string_list_append(&revlist_args, buf.buf);
989 strbuf_release(&buf);
992 string_list_append(&revlist_args, ref->peer_ref->name);
996 if (get_exporter(transport, &exporter, &revlist_args))
997 die(_("couldn't run fast-export"));
999 string_list_clear(&revlist_args, 1);
1001 if (finish_command(&exporter))
1002 die(_("error while running fast-export"));
1003 if (push_update_refs_status(data, remote_refs, flags))
1006 if (data->export_marks) {
1007 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1008 rename(buf.buf, data->export_marks);
1009 strbuf_release(&buf);
1015 static int push_refs(struct transport *transport,
1016 struct ref *remote_refs, int flags)
1018 struct helper_data *data = transport->data;
1020 if (process_connect(transport, 1)) {
1021 do_take_over(transport);
1022 return transport->vtable->push_refs(transport, remote_refs, flags);
1027 _("No refs in common and none specified; doing nothing.\n"
1028 "Perhaps you should specify a branch such as 'master'.\n"));
1033 return push_refs_with_push(transport, remote_refs, flags);
1036 return push_refs_with_export(transport, remote_refs, flags);
1042 static int has_attribute(const char *attrs, const char *attr)
1050 const char *space = strchrnul(attrs, ' ');
1051 if (len == space - attrs && !strncmp(attrs, attr, len))
1059 static struct ref *get_refs_list(struct transport *transport, int for_push,
1060 const struct argv_array *ref_prefixes)
1062 struct helper_data *data = transport->data;
1063 struct child_process *helper;
1064 struct ref *ret = NULL;
1065 struct ref **tail = &ret;
1067 struct strbuf buf = STRBUF_INIT;
1069 helper = get_helper(transport);
1071 if (process_connect(transport, for_push)) {
1072 do_take_over(transport);
1073 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1076 if (data->push && for_push)
1077 write_str_in_full(helper->in, "list for-push\n");
1079 write_str_in_full(helper->in, "list\n");
1083 if (recvline(data, &buf))
1089 eov = strchr(buf.buf, ' ');
1091 die(_("malformed response in ref list: %s"), buf.buf);
1092 eon = strchr(eov + 1, ' ');
1096 *tail = alloc_ref(eov + 1);
1097 if (buf.buf[0] == '@')
1098 (*tail)->symref = xstrdup(buf.buf + 1);
1099 else if (buf.buf[0] != '?')
1100 get_oid_hex(buf.buf, &(*tail)->old_oid);
1102 if (has_attribute(eon + 1, "unchanged")) {
1103 (*tail)->status |= REF_STATUS_UPTODATE;
1104 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1105 die(_("could not read ref %s"),
1109 tail = &((*tail)->next);
1112 fprintf(stderr, "Debug: Read ref listing.\n");
1113 strbuf_release(&buf);
1115 for (posn = ret; posn; posn = posn->next)
1116 resolve_remote_symref(posn, ret);
1121 static struct transport_vtable vtable = {
1131 int transport_helper_init(struct transport *transport, const char *name)
1133 struct helper_data *data = xcalloc(1, sizeof(*data));
1136 transport_check_allowed(name);
1138 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1141 transport->data = data;
1142 transport->vtable = &vtable;
1143 transport->smart_options = &(data->transport_options);
1148 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1149 * buffer less), so attempt reads and writes with up to that size.
1151 #define BUFFERSIZE 65536
1152 /* This should be enough to hold debugging message. */
1153 #define PBUFFERSIZE 8192
1155 /* Print bidirectional transfer loop debug message. */
1156 __attribute__((format (printf, 1, 2)))
1157 static void transfer_debug(const char *fmt, ...)
1160 * NEEDSWORK: This function is sometimes used from multiple threads, and
1161 * we end up using debug_enabled racily. That "should not matter" since
1162 * we always write the same value, but it's still wrong. This function
1163 * is listed in .tsan-suppressions for the time being.
1167 char msgbuf[PBUFFERSIZE];
1168 static int debug_enabled = -1;
1170 if (debug_enabled < 0)
1171 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1175 va_start(args, fmt);
1176 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1178 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1181 /* Stream state: More data may be coming in this direction. */
1182 #define SSTATE_TRANSFERRING 0
1184 * Stream state: No more data coming in this direction, flushing rest of
1187 #define SSTATE_FLUSHING 1
1188 /* Stream state: Transfer in this direction finished. */
1189 #define SSTATE_FINISHED 2
1191 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1192 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1193 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1195 /* Unidirectional transfer. */
1196 struct unidirectional_transfer {
1201 /* Is source socket? */
1203 /* Is destination socket? */
1205 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1208 char buf[BUFFERSIZE];
1211 /* Name of source. */
1212 const char *src_name;
1213 /* Name of destination. */
1214 const char *dest_name;
1217 /* Closes the target (for writing) if transfer has finished. */
1218 static void udt_close_if_finished(struct unidirectional_transfer *t)
1220 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1221 t->state = SSTATE_FINISHED;
1222 if (t->dest_is_sock)
1223 shutdown(t->dest, SHUT_WR);
1226 transfer_debug("Closed %s.", t->dest_name);
1231 * Tries to read data from source into buffer. If buffer is full,
1232 * no data is read. Returns 0 on success, -1 on error.
1234 static int udt_do_read(struct unidirectional_transfer *t)
1238 if (t->bufuse == BUFFERSIZE)
1239 return 0; /* No space for more. */
1241 transfer_debug("%s is readable", t->src_name);
1242 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1244 error_errno(_("read(%s) failed"), t->src_name);
1246 } else if (bytes == 0) {
1247 transfer_debug("%s EOF (with %i bytes in buffer)",
1248 t->src_name, (int)t->bufuse);
1249 t->state = SSTATE_FLUSHING;
1250 } else if (bytes > 0) {
1252 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1253 (int)bytes, t->src_name, (int)t->bufuse);
1258 /* Tries to write data from buffer into destination. If buffer is empty,
1259 * no data is written. Returns 0 on success, -1 on error.
1261 static int udt_do_write(struct unidirectional_transfer *t)
1266 return 0; /* Nothing to write. */
1268 transfer_debug("%s is writable", t->dest_name);
1269 bytes = xwrite(t->dest, t->buf, t->bufuse);
1271 error_errno(_("write(%s) failed"), t->dest_name);
1273 } else if (bytes > 0) {
1276 memmove(t->buf, t->buf + bytes, t->bufuse);
1277 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1278 (int)bytes, t->dest_name, (int)t->bufuse);
1284 /* State of bidirectional transfer loop. */
1285 struct bidirectional_transfer_state {
1286 /* Direction from program to git. */
1287 struct unidirectional_transfer ptg;
1288 /* Direction from git to program. */
1289 struct unidirectional_transfer gtp;
1292 static void *udt_copy_task_routine(void *udt)
1294 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1295 while (t->state != SSTATE_FINISHED) {
1296 if (STATE_NEEDS_READING(t->state))
1299 if (STATE_NEEDS_WRITING(t->state))
1300 if (udt_do_write(t))
1302 if (STATE_NEEDS_CLOSING(t->state))
1303 udt_close_if_finished(t);
1305 return udt; /* Just some non-NULL value. */
1311 * Join thread, with appropriate errors on failure. Name is name for the
1312 * thread (for error messages). Returns 0 on success, 1 on failure.
1314 static int tloop_join(pthread_t thread, const char *name)
1318 err = pthread_join(thread, &tret);
1320 error(_("%s thread failed"), name);
1324 error(_("%s thread failed to join: %s"), name, strerror(err));
1331 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1334 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1336 pthread_t gtp_thread;
1337 pthread_t ptg_thread;
1340 err = pthread_create(>p_thread, NULL, udt_copy_task_routine,
1343 die(_("can't start thread for copying data: %s"), strerror(err));
1344 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1347 die(_("can't start thread for copying data: %s"), strerror(err));
1349 ret |= tloop_join(gtp_thread, "Git to program copy");
1350 ret |= tloop_join(ptg_thread, "Program to git copy");
1355 /* Close the source and target (for writing) for transfer. */
1356 static void udt_kill_transfer(struct unidirectional_transfer *t)
1358 t->state = SSTATE_FINISHED;
1360 * Socket read end left open isn't a disaster if nobody
1361 * attempts to read from it (mingw compat headers do not
1364 * We can't fully close the socket since otherwise gtp
1365 * task would first close the socket it sends data to
1366 * while closing the ptg file descriptors.
1368 if (!t->src_is_sock)
1370 if (t->dest_is_sock)
1371 shutdown(t->dest, SHUT_WR);
1377 * Join process, with appropriate errors on failure. Name is name for the
1378 * process (for error messages). Returns 0 on success, 1 on failure.
1380 static int tloop_join(pid_t pid, const char *name)
1383 if (waitpid(pid, &tret, 0) < 0) {
1384 error_errno(_("%s process failed to wait"), name);
1387 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1388 error(_("%s process failed"), name);
1395 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1398 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1403 /* Fork thread #1: git to program. */
1406 die_errno(_("can't start thread for copying data"));
1407 else if (pid1 == 0) {
1408 udt_kill_transfer(&s->ptg);
1409 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1412 /* Fork thread #2: program to git. */
1415 die_errno(_("can't start thread for copying data"));
1416 else if (pid2 == 0) {
1417 udt_kill_transfer(&s->gtp);
1418 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1422 * Close both streams in parent as to not interfere with
1423 * end of file detection and wait for both tasks to finish.
1425 udt_kill_transfer(&s->gtp);
1426 udt_kill_transfer(&s->ptg);
1427 ret |= tloop_join(pid1, "Git to program copy");
1428 ret |= tloop_join(pid2, "Program to git copy");
1434 * Copies data from stdin to output and from input to stdout simultaneously.
1435 * Additionally filtering through given filter. If filter is NULL, uses
1438 int bidirectional_transfer_loop(int input, int output)
1440 struct bidirectional_transfer_state state;
1442 /* Fill the state fields. */
1443 state.ptg.src = input;
1445 state.ptg.src_is_sock = (input == output);
1446 state.ptg.dest_is_sock = 0;
1447 state.ptg.state = SSTATE_TRANSFERRING;
1448 state.ptg.bufuse = 0;
1449 state.ptg.src_name = "remote input";
1450 state.ptg.dest_name = "stdout";
1453 state.gtp.dest = output;
1454 state.gtp.src_is_sock = 0;
1455 state.gtp.dest_is_sock = (input == output);
1456 state.gtp.state = SSTATE_TRANSFERRING;
1457 state.gtp.bufuse = 0;
1458 state.gtp.src_name = "stdin";
1459 state.gtp.dest_name = "remote output";
1461 return tloop_spawnwait_tasks(&state);