4 #include "run-command.h"
9 #include "string-list.h"
10 #include "thread-utils.h"
12 #include "argv-array.h"
14 #include "transport-internal.h"
21 struct child_process *helper;
30 stateless_connect : 1,
32 check_connectivity : 1,
33 no_disconnect_req : 1,
34 no_private_update : 1;
37 /* These go from remote name (as in "list") to private name */
38 struct refspec *refspecs;
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;
109 const char **refspecs = NULL;
111 int refspec_alloc = 0;
118 helper = xmalloc(sizeof(*helper));
119 child_process_init(helper);
123 argv_array_pushf(&helper->args, "git-remote-%s", data->name);
124 argv_array_push(&helper->args, transport->remote->name);
125 argv_array_push(&helper->args, remove_ext_force(transport->url));
127 helper->silent_exec_failure = 1;
130 argv_array_pushf(&helper->env_array, "%s=%s",
131 GIT_DIR_ENVIRONMENT, get_git_dir());
133 code = start_command(helper);
134 if (code < 0 && errno == ENOENT)
135 die("Unable to find remote helper for '%s'", data->name);
139 data->helper = helper;
140 data->no_disconnect_req = 0;
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 (!data->refspecs && skip_prefix(capname, "refspec ", &arg)) {
190 refspecs[refspec_nr++] = xstrdup(arg);
191 } else if (!strcmp(capname, "connect")) {
193 } else if (!strcmp(capname, "stateless-connect")) {
194 data->stateless_connect = 1;
195 } else if (!strcmp(capname, "signed-tags")) {
196 data->signed_tags = 1;
197 } else if (skip_prefix(capname, "export-marks ", &arg)) {
198 data->export_marks = xstrdup(arg);
199 } else if (skip_prefix(capname, "import-marks ", &arg)) {
200 data->import_marks = xstrdup(arg);
201 } else if (starts_with(capname, "no-private-update")) {
202 data->no_private_update = 1;
203 } else if (mandatory) {
204 die("Unknown mandatory capability %s. This remote "
205 "helper probably needs newer version of Git.",
211 data->refspec_nr = refspec_nr;
212 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
213 for (i = 0; i < refspec_nr; i++)
214 free((char *)refspecs[i]);
216 } else if (data->import || data->bidi_import || data->export) {
217 warning("This remote helper should implement refspec capability.");
219 strbuf_release(&buf);
221 fprintf(stderr, "Debug: Capabilities complete.\n");
222 standard_options(transport);
226 static int disconnect_helper(struct transport *transport)
228 struct helper_data *data = transport->data;
233 fprintf(stderr, "Debug: Disconnecting.\n");
234 if (!data->no_disconnect_req) {
236 * Ignore write errors; there's nothing we can do,
237 * since we're about to close the pipe anyway. And the
238 * most likely error is EPIPE due to the helper dying
239 * to report an error itself.
241 sigchain_push(SIGPIPE, SIG_IGN);
242 xwrite(data->helper->in, "\n", 1);
243 sigchain_pop(SIGPIPE);
245 close(data->helper->in);
246 close(data->helper->out);
248 res = finish_command(data->helper);
249 FREE_AND_NULL(data->helper);
254 static const char *unsupported_options[] = {
255 TRANS_OPT_UPLOADPACK,
256 TRANS_OPT_RECEIVEPACK,
261 static const char *boolean_options[] = {
264 TRANS_OPT_FOLLOWTAGS,
265 TRANS_OPT_DEEPEN_RELATIVE
268 static int strbuf_set_helper_option(struct helper_data *data,
274 if (recvline(data, buf))
277 if (!strcmp(buf->buf, "ok"))
279 else if (starts_with(buf->buf, "error"))
281 else if (!strcmp(buf->buf, "unsupported"))
284 warning("%s unexpectedly said: '%s'", data->name, buf->buf);
290 static int string_list_set_helper_option(struct helper_data *data,
292 struct string_list *list)
294 struct strbuf buf = STRBUF_INIT;
297 for (i = 0; i < list->nr; i++) {
298 strbuf_addf(&buf, "option %s ", name);
299 quote_c_style(list->items[i].string, &buf, NULL, 0);
300 strbuf_addch(&buf, '\n');
302 if ((ret = strbuf_set_helper_option(data, &buf)))
306 strbuf_release(&buf);
310 static int set_helper_option(struct transport *transport,
311 const char *name, const char *value)
313 struct helper_data *data = transport->data;
314 struct strbuf buf = STRBUF_INIT;
315 int i, ret, is_bool = 0;
317 get_helper(transport);
322 if (!strcmp(name, "deepen-not"))
323 return string_list_set_helper_option(data, name,
324 (struct string_list *)value);
326 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
327 if (!strcmp(name, unsupported_options[i]))
331 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
332 if (!strcmp(name, boolean_options[i])) {
338 strbuf_addf(&buf, "option %s ", name);
340 strbuf_addstr(&buf, value ? "true" : "false");
342 quote_c_style(value, &buf, NULL, 0);
343 strbuf_addch(&buf, '\n');
345 ret = strbuf_set_helper_option(data, &buf);
346 strbuf_release(&buf);
350 static void standard_options(struct transport *t)
355 set_helper_option(t, "progress", t->progress ? "true" : "false");
357 xsnprintf(buf, sizeof(buf), "%d", v + 1);
358 set_helper_option(t, "verbosity", buf);
361 case TRANSPORT_FAMILY_ALL:
363 * this is already the default,
364 * do not break old remote helpers by setting "all" here
367 case TRANSPORT_FAMILY_IPV4:
368 set_helper_option(t, "family", "ipv4");
370 case TRANSPORT_FAMILY_IPV6:
371 set_helper_option(t, "family", "ipv6");
376 static int release_helper(struct transport *transport)
379 struct helper_data *data = transport->data;
380 free_refspec(data->refspec_nr, data->refspecs);
381 data->refspecs = NULL;
382 res = disconnect_helper(transport);
383 free(transport->data);
387 static int fetch_with_fetch(struct transport *transport,
388 int nr_heads, struct ref **to_fetch)
390 struct helper_data *data = transport->data;
392 struct strbuf buf = STRBUF_INIT;
394 for (i = 0; i < nr_heads; i++) {
395 const struct ref *posn = to_fetch[i];
396 if (posn->status & REF_STATUS_UPTODATE)
399 strbuf_addf(&buf, "fetch %s %s\n",
400 oid_to_hex(&posn->old_oid),
401 posn->symref ? posn->symref : posn->name);
404 strbuf_addch(&buf, '\n');
405 sendline(data, &buf);
408 if (recvline(data, &buf))
411 if (starts_with(buf.buf, "lock ")) {
412 const char *name = buf.buf + 5;
413 if (transport->pack_lockfile)
414 warning("%s also locked %s", data->name, name);
416 transport->pack_lockfile = xstrdup(name);
418 else if (data->check_connectivity &&
419 data->transport_options.check_self_contained_and_connected &&
420 !strcmp(buf.buf, "connectivity-ok"))
421 data->transport_options.self_contained_and_connected = 1;
425 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
427 strbuf_release(&buf);
431 static int get_importer(struct transport *transport, struct child_process *fastimport)
433 struct child_process *helper = get_helper(transport);
434 struct helper_data *data = transport->data;
435 int cat_blob_fd, code;
436 child_process_init(fastimport);
437 fastimport->in = helper->out;
438 argv_array_push(&fastimport->args, "fast-import");
439 argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
441 if (data->bidi_import) {
442 cat_blob_fd = xdup(helper->in);
443 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
445 fastimport->git_cmd = 1;
447 code = start_command(fastimport);
451 static int get_exporter(struct transport *transport,
452 struct child_process *fastexport,
453 struct string_list *revlist_args)
455 struct helper_data *data = transport->data;
456 struct child_process *helper = get_helper(transport);
459 child_process_init(fastexport);
461 /* we need to duplicate helper->in because we want to use it after
462 * fastexport is done with it. */
463 fastexport->out = dup(helper->in);
464 argv_array_push(&fastexport->args, "fast-export");
465 argv_array_push(&fastexport->args, "--use-done-feature");
466 argv_array_push(&fastexport->args, data->signed_tags ?
467 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
468 if (data->export_marks)
469 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
470 if (data->import_marks)
471 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
473 for (i = 0; i < revlist_args->nr; i++)
474 argv_array_push(&fastexport->args, revlist_args->items[i].string);
476 fastexport->git_cmd = 1;
477 return start_command(fastexport);
480 static int fetch_with_import(struct transport *transport,
481 int nr_heads, struct ref **to_fetch)
483 struct child_process fastimport;
484 struct helper_data *data = transport->data;
487 struct strbuf buf = STRBUF_INIT;
489 get_helper(transport);
491 if (get_importer(transport, &fastimport))
492 die("Couldn't run fast-import");
494 for (i = 0; i < nr_heads; i++) {
496 if (posn->status & REF_STATUS_UPTODATE)
499 strbuf_addf(&buf, "import %s\n",
500 posn->symref ? posn->symref : posn->name);
501 sendline(data, &buf);
505 write_constant(data->helper->in, "\n");
507 * remote-helpers that advertise the bidi-import capability are required to
508 * buffer the complete batch of import commands until this newline before
509 * sending data to fast-import.
510 * These helpers read back data from fast-import on their stdin, which could
511 * be mixed with import commands, otherwise.
514 if (finish_command(&fastimport))
515 die("Error while running fast-import");
518 * The fast-import stream of a remote helper that advertises
519 * the "refspec" capability writes to the refs named after the
520 * right hand side of the first refspec matching each ref we
523 * (If no "refspec" capability was specified, for historical
524 * reasons we default to the equivalent of *:*.)
526 * Store the result in to_fetch[i].old_sha1. Callers such
527 * as "git fetch" can use the value to write feedback to the
528 * terminal, populate FETCH_HEAD, and determine what new value
529 * should be written to peer_ref if the update is a
530 * fast-forward or this is a forced update.
532 for (i = 0; i < nr_heads; i++) {
533 char *private, *name;
535 if (posn->status & REF_STATUS_UPTODATE)
537 name = posn->symref ? posn->symref : posn->name;
539 private = apply_refspecs(data->refspecs, data->refspec_nr, name);
541 private = xstrdup(name);
543 if (read_ref(private, &posn->old_oid) < 0)
544 die("Could not read ref %s", private);
548 strbuf_release(&buf);
552 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
554 struct helper_data *data = transport->data;
558 struct child_process *helper;
560 helper = get_helper(transport);
563 * Yes, dup the pipe another time, as we need unbuffered version
564 * of input pipe as FILE*. fclose() closes the underlying fd and
565 * stream buffering only can be changed before first I/O operation
568 duped = dup(helper->out);
570 die_errno("Can't dup helper output fd");
571 input = xfdopen(duped, "r");
572 setvbuf(input, NULL, _IONBF, 0);
574 sendline(data, cmdbuf);
575 if (recvline_fh(input, cmdbuf))
578 if (!strcmp(cmdbuf->buf, "")) {
579 data->no_disconnect_req = 1;
581 fprintf(stderr, "Debug: Smart transport connection "
584 } else if (!strcmp(cmdbuf->buf, "fallback")) {
586 fprintf(stderr, "Debug: Falling back to dumb "
589 die("Unknown response to connect: %s",
597 static int process_connect_service(struct transport *transport,
598 const char *name, const char *exec)
600 struct helper_data *data = transport->data;
601 struct strbuf cmdbuf = STRBUF_INIT;
605 * Handle --upload-pack and friends. This is fire and forget...
606 * just warn if it fails.
608 if (strcmp(name, exec)) {
609 int r = set_helper_option(transport, "servpath", exec);
611 warning("Setting remote service path not supported by protocol.");
613 warning("Invalid remote service path.");
617 strbuf_addf(&cmdbuf, "connect %s\n", name);
618 ret = run_connect(transport, &cmdbuf);
619 } else if (data->stateless_connect &&
620 (get_protocol_version_config() == protocol_v2) &&
621 !strcmp("git-upload-pack", name)) {
622 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
623 ret = run_connect(transport, &cmdbuf);
625 transport->stateless_rpc = 1;
628 strbuf_release(&cmdbuf);
632 static int process_connect(struct transport *transport,
635 struct helper_data *data = transport->data;
639 name = for_push ? "git-receive-pack" : "git-upload-pack";
641 exec = data->transport_options.receivepack;
643 exec = data->transport_options.uploadpack;
645 return process_connect_service(transport, name, exec);
648 static int connect_helper(struct transport *transport, const char *name,
649 const char *exec, int fd[2])
651 struct helper_data *data = transport->data;
653 /* Get_helper so connect is inited. */
654 get_helper(transport);
656 die("Operation not supported by protocol.");
658 if (!process_connect_service(transport, name, exec))
659 die("Can't connect to subservice %s.", name);
661 fd[0] = data->helper->out;
662 fd[1] = data->helper->in;
666 static int fetch(struct transport *transport,
667 int nr_heads, struct ref **to_fetch)
669 struct helper_data *data = transport->data;
672 if (process_connect(transport, 0)) {
673 do_take_over(transport);
674 return transport->vtable->fetch(transport, nr_heads, to_fetch);
678 for (i = 0; i < nr_heads; i++)
679 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
685 if (data->check_connectivity &&
686 data->transport_options.check_self_contained_and_connected)
687 set_helper_option(transport, "check-connectivity", "true");
689 if (transport->cloning)
690 set_helper_option(transport, "cloning", "true");
692 if (data->transport_options.update_shallow)
693 set_helper_option(transport, "update-shallow", "true");
696 return fetch_with_fetch(transport, nr_heads, to_fetch);
699 return fetch_with_import(transport, nr_heads, to_fetch);
704 static int push_update_ref_status(struct strbuf *buf,
706 struct ref *remote_refs)
709 int status, forced = 0;
711 if (starts_with(buf->buf, "ok ")) {
712 status = REF_STATUS_OK;
713 refname = buf->buf + 3;
714 } else if (starts_with(buf->buf, "error ")) {
715 status = REF_STATUS_REMOTE_REJECT;
716 refname = buf->buf + 6;
718 die("expected ok/error, helper said '%s'", buf->buf);
720 msg = strchr(refname, ' ');
722 struct strbuf msg_buf = STRBUF_INIT;
726 if (!unquote_c_style(&msg_buf, msg, &end))
727 msg = strbuf_detach(&msg_buf, NULL);
730 strbuf_release(&msg_buf);
732 if (!strcmp(msg, "no match")) {
733 status = REF_STATUS_NONE;
736 else if (!strcmp(msg, "up to date")) {
737 status = REF_STATUS_UPTODATE;
740 else if (!strcmp(msg, "non-fast forward")) {
741 status = REF_STATUS_REJECT_NONFASTFORWARD;
744 else if (!strcmp(msg, "already exists")) {
745 status = REF_STATUS_REJECT_ALREADY_EXISTS;
748 else if (!strcmp(msg, "fetch first")) {
749 status = REF_STATUS_REJECT_FETCH_FIRST;
752 else if (!strcmp(msg, "needs force")) {
753 status = REF_STATUS_REJECT_NEEDS_FORCE;
756 else if (!strcmp(msg, "stale info")) {
757 status = REF_STATUS_REJECT_STALE;
760 else if (!strcmp(msg, "forced update")) {
767 *ref = find_ref_by_name(*ref, refname);
769 *ref = find_ref_by_name(remote_refs, refname);
771 warning("helper reported unexpected status of %s", refname);
775 if ((*ref)->status != REF_STATUS_NONE) {
777 * Earlier, the ref was marked not to be pushed, so ignore the ref
778 * status reported by the remote helper if the latter is 'no match'.
780 if (status == REF_STATUS_NONE)
784 (*ref)->status = status;
785 (*ref)->forced_update |= forced;
786 (*ref)->remote_status = msg;
787 return !(status == REF_STATUS_OK);
790 static int push_update_refs_status(struct helper_data *data,
791 struct ref *remote_refs,
794 struct strbuf buf = STRBUF_INIT;
795 struct ref *ref = remote_refs;
801 if (recvline(data, &buf)) {
809 if (push_update_ref_status(&buf, &ref, remote_refs))
812 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->refspecs || data->no_private_update)
815 /* propagate back the update to the remote namespace */
816 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
819 update_ref("update by helper", private, &ref->new_oid, NULL,
823 strbuf_release(&buf);
827 static void set_common_push_options(struct transport *transport,
828 const char *name, int flags)
830 if (flags & TRANSPORT_PUSH_DRY_RUN) {
831 if (set_helper_option(transport, "dry-run", "true") != 0)
832 die("helper %s does not support dry-run", name);
833 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
834 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
835 die("helper %s does not support --signed", name);
836 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
837 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
838 die("helper %s does not support --signed=if-asked", name);
841 if (flags & TRANSPORT_PUSH_OPTIONS) {
842 struct string_list_item *item;
843 for_each_string_list_item(item, transport->push_options)
844 if (set_helper_option(transport, "push-option", item->string) != 0)
845 die("helper %s does not support 'push-option'", name);
849 static int push_refs_with_push(struct transport *transport,
850 struct ref *remote_refs, int flags)
852 int force_all = flags & TRANSPORT_PUSH_FORCE;
853 int mirror = flags & TRANSPORT_PUSH_MIRROR;
854 struct helper_data *data = transport->data;
855 struct strbuf buf = STRBUF_INIT;
857 struct string_list cas_options = STRING_LIST_INIT_DUP;
858 struct string_list_item *cas_option;
860 get_helper(transport);
864 for (ref = remote_refs; ref; ref = ref->next) {
865 if (!ref->peer_ref && !mirror)
868 /* Check for statuses set by set_ref_status_for_push() */
869 switch (ref->status) {
870 case REF_STATUS_REJECT_NONFASTFORWARD:
871 case REF_STATUS_REJECT_STALE:
872 case REF_STATUS_REJECT_ALREADY_EXISTS:
873 case REF_STATUS_UPTODATE:
882 strbuf_addstr(&buf, "push ");
883 if (!ref->deletion) {
885 strbuf_addch(&buf, '+');
887 strbuf_addstr(&buf, ref->peer_ref->name);
889 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
891 strbuf_addch(&buf, ':');
892 strbuf_addstr(&buf, ref->name);
893 strbuf_addch(&buf, '\n');
896 * The "--force-with-lease" options without explicit
897 * values to expect have already been expanded into
898 * the ref->old_oid_expect[] field; we can ignore
899 * transport->smart_options->cas altogether and instead
900 * can enumerate them from the refs.
902 if (ref->expect_old_sha1) {
903 struct strbuf cas = STRBUF_INIT;
904 strbuf_addf(&cas, "%s:%s",
905 ref->name, oid_to_hex(&ref->old_oid_expect));
906 string_list_append_nodup(&cas_options,
907 strbuf_detach(&cas, NULL));
911 string_list_clear(&cas_options, 0);
915 for_each_string_list_item(cas_option, &cas_options)
916 set_helper_option(transport, "cas", cas_option->string);
917 set_common_push_options(transport, data->name, flags);
919 strbuf_addch(&buf, '\n');
920 sendline(data, &buf);
921 strbuf_release(&buf);
922 string_list_clear(&cas_options, 0);
924 return push_update_refs_status(data, remote_refs, flags);
927 static int push_refs_with_export(struct transport *transport,
928 struct ref *remote_refs, int flags)
931 struct child_process *helper, exporter;
932 struct helper_data *data = transport->data;
933 struct string_list revlist_args = STRING_LIST_INIT_DUP;
934 struct strbuf buf = STRBUF_INIT;
937 die("remote-helper doesn't support push; refspec needed");
939 set_common_push_options(transport, data->name, flags);
940 if (flags & TRANSPORT_PUSH_FORCE) {
941 if (set_helper_option(transport, "force", "true") != 0)
942 warning("helper %s does not support 'force'", data->name);
945 helper = get_helper(transport);
947 write_constant(helper->in, "export\n");
949 for (ref = remote_refs; ref; ref = ref->next) {
951 struct object_id oid;
953 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
954 if (private && !get_oid(private, &oid)) {
955 strbuf_addf(&buf, "^%s", private);
956 string_list_append_nodup(&revlist_args,
957 strbuf_detach(&buf, NULL));
958 oidcpy(&ref->old_oid, &oid);
963 if (strcmp(ref->name, ref->peer_ref->name)) {
964 if (!ref->deletion) {
968 /* Follow symbolic refs (mainly for HEAD). */
969 name = resolve_ref_unsafe(ref->peer_ref->name,
972 if (!name || !(flag & REF_ISSYMREF))
973 name = ref->peer_ref->name;
975 strbuf_addf(&buf, "%s:%s", name, ref->name);
977 strbuf_addf(&buf, ":%s", ref->name);
979 string_list_append(&revlist_args, "--refspec");
980 string_list_append(&revlist_args, buf.buf);
981 strbuf_release(&buf);
984 string_list_append(&revlist_args, ref->peer_ref->name);
988 if (get_exporter(transport, &exporter, &revlist_args))
989 die("Couldn't run fast-export");
991 string_list_clear(&revlist_args, 1);
993 if (finish_command(&exporter))
994 die("Error while running fast-export");
995 if (push_update_refs_status(data, remote_refs, flags))
998 if (data->export_marks) {
999 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1000 rename(buf.buf, data->export_marks);
1001 strbuf_release(&buf);
1007 static int push_refs(struct transport *transport,
1008 struct ref *remote_refs, int flags)
1010 struct helper_data *data = transport->data;
1012 if (process_connect(transport, 1)) {
1013 do_take_over(transport);
1014 return transport->vtable->push_refs(transport, remote_refs, flags);
1018 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
1019 "Perhaps you should specify a branch such as 'master'.\n");
1024 return push_refs_with_push(transport, remote_refs, flags);
1027 return push_refs_with_export(transport, remote_refs, flags);
1033 static int has_attribute(const char *attrs, const char *attr) {
1040 const char *space = strchrnul(attrs, ' ');
1041 if (len == space - attrs && !strncmp(attrs, attr, len))
1049 static struct ref *get_refs_list(struct transport *transport, int for_push,
1050 const struct argv_array *ref_prefixes)
1052 struct helper_data *data = transport->data;
1053 struct child_process *helper;
1054 struct ref *ret = NULL;
1055 struct ref **tail = &ret;
1057 struct strbuf buf = STRBUF_INIT;
1059 helper = get_helper(transport);
1061 if (process_connect(transport, for_push)) {
1062 do_take_over(transport);
1063 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1066 if (data->push && for_push)
1067 write_str_in_full(helper->in, "list for-push\n");
1069 write_str_in_full(helper->in, "list\n");
1073 if (recvline(data, &buf))
1079 eov = strchr(buf.buf, ' ');
1081 die("Malformed response in ref list: %s", buf.buf);
1082 eon = strchr(eov + 1, ' ');
1086 *tail = alloc_ref(eov + 1);
1087 if (buf.buf[0] == '@')
1088 (*tail)->symref = xstrdup(buf.buf + 1);
1089 else if (buf.buf[0] != '?')
1090 get_oid_hex(buf.buf, &(*tail)->old_oid);
1092 if (has_attribute(eon + 1, "unchanged")) {
1093 (*tail)->status |= REF_STATUS_UPTODATE;
1094 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1095 die(_("Could not read ref %s"),
1099 tail = &((*tail)->next);
1102 fprintf(stderr, "Debug: Read ref listing.\n");
1103 strbuf_release(&buf);
1105 for (posn = ret; posn; posn = posn->next)
1106 resolve_remote_symref(posn, ret);
1111 static struct transport_vtable vtable = {
1120 int transport_helper_init(struct transport *transport, const char *name)
1122 struct helper_data *data = xcalloc(1, sizeof(*data));
1125 transport_check_allowed(name);
1127 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1130 transport->data = data;
1131 transport->vtable = &vtable;
1132 transport->smart_options = &(data->transport_options);
1137 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1138 * buffer less), so attempt reads and writes with up to that size.
1140 #define BUFFERSIZE 65536
1141 /* This should be enough to hold debugging message. */
1142 #define PBUFFERSIZE 8192
1144 /* Print bidirectional transfer loop debug message. */
1145 __attribute__((format (printf, 1, 2)))
1146 static void transfer_debug(const char *fmt, ...)
1149 * NEEDSWORK: This function is sometimes used from multiple threads, and
1150 * we end up using debug_enabled racily. That "should not matter" since
1151 * we always write the same value, but it's still wrong. This function
1152 * is listed in .tsan-suppressions for the time being.
1156 char msgbuf[PBUFFERSIZE];
1157 static int debug_enabled = -1;
1159 if (debug_enabled < 0)
1160 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1164 va_start(args, fmt);
1165 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1167 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1170 /* Stream state: More data may be coming in this direction. */
1171 #define SSTATE_TRANSFERRING 0
1173 * Stream state: No more data coming in this direction, flushing rest of
1176 #define SSTATE_FLUSHING 1
1177 /* Stream state: Transfer in this direction finished. */
1178 #define SSTATE_FINISHED 2
1180 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1181 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1182 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1184 /* Unidirectional transfer. */
1185 struct unidirectional_transfer {
1190 /* Is source socket? */
1192 /* Is destination socket? */
1194 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1197 char buf[BUFFERSIZE];
1200 /* Name of source. */
1201 const char *src_name;
1202 /* Name of destination. */
1203 const char *dest_name;
1206 /* Closes the target (for writing) if transfer has finished. */
1207 static void udt_close_if_finished(struct unidirectional_transfer *t)
1209 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1210 t->state = SSTATE_FINISHED;
1211 if (t->dest_is_sock)
1212 shutdown(t->dest, SHUT_WR);
1215 transfer_debug("Closed %s.", t->dest_name);
1220 * Tries to read data from source into buffer. If buffer is full,
1221 * no data is read. Returns 0 on success, -1 on error.
1223 static int udt_do_read(struct unidirectional_transfer *t)
1227 if (t->bufuse == BUFFERSIZE)
1228 return 0; /* No space for more. */
1230 transfer_debug("%s is readable", t->src_name);
1231 bytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1232 if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
1234 error_errno("read(%s) failed", t->src_name);
1236 } else if (bytes == 0) {
1237 transfer_debug("%s EOF (with %i bytes in buffer)",
1238 t->src_name, (int)t->bufuse);
1239 t->state = SSTATE_FLUSHING;
1240 } else if (bytes > 0) {
1242 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1243 (int)bytes, t->src_name, (int)t->bufuse);
1248 /* Tries to write data from buffer into destination. If buffer is empty,
1249 * no data is written. Returns 0 on success, -1 on error.
1251 static int udt_do_write(struct unidirectional_transfer *t)
1256 return 0; /* Nothing to write. */
1258 transfer_debug("%s is writable", t->dest_name);
1259 bytes = xwrite(t->dest, t->buf, t->bufuse);
1260 if (bytes < 0 && errno != EWOULDBLOCK) {
1261 error_errno("write(%s) failed", t->dest_name);
1263 } else if (bytes > 0) {
1266 memmove(t->buf, t->buf + bytes, t->bufuse);
1267 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1268 (int)bytes, t->dest_name, (int)t->bufuse);
1274 /* State of bidirectional transfer loop. */
1275 struct bidirectional_transfer_state {
1276 /* Direction from program to git. */
1277 struct unidirectional_transfer ptg;
1278 /* Direction from git to program. */
1279 struct unidirectional_transfer gtp;
1282 static void *udt_copy_task_routine(void *udt)
1284 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1285 while (t->state != SSTATE_FINISHED) {
1286 if (STATE_NEEDS_READING(t->state))
1289 if (STATE_NEEDS_WRITING(t->state))
1290 if (udt_do_write(t))
1292 if (STATE_NEEDS_CLOSING(t->state))
1293 udt_close_if_finished(t);
1295 return udt; /* Just some non-NULL value. */
1301 * Join thread, with appropriate errors on failure. Name is name for the
1302 * thread (for error messages). Returns 0 on success, 1 on failure.
1304 static int tloop_join(pthread_t thread, const char *name)
1308 err = pthread_join(thread, &tret);
1310 error("%s thread failed", name);
1314 error("%s thread failed to join: %s", name, strerror(err));
1321 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1324 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1326 pthread_t gtp_thread;
1327 pthread_t ptg_thread;
1330 err = pthread_create(>p_thread, NULL, udt_copy_task_routine,
1333 die("Can't start thread for copying data: %s", strerror(err));
1334 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1337 die("Can't start thread for copying data: %s", strerror(err));
1339 ret |= tloop_join(gtp_thread, "Git to program copy");
1340 ret |= tloop_join(ptg_thread, "Program to git copy");
1345 /* Close the source and target (for writing) for transfer. */
1346 static void udt_kill_transfer(struct unidirectional_transfer *t)
1348 t->state = SSTATE_FINISHED;
1350 * Socket read end left open isn't a disaster if nobody
1351 * attempts to read from it (mingw compat headers do not
1354 * We can't fully close the socket since otherwise gtp
1355 * task would first close the socket it sends data to
1356 * while closing the ptg file descriptors.
1358 if (!t->src_is_sock)
1360 if (t->dest_is_sock)
1361 shutdown(t->dest, SHUT_WR);
1367 * Join process, with appropriate errors on failure. Name is name for the
1368 * process (for error messages). Returns 0 on success, 1 on failure.
1370 static int tloop_join(pid_t pid, const char *name)
1373 if (waitpid(pid, &tret, 0) < 0) {
1374 error_errno("%s process failed to wait", name);
1377 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1378 error("%s process failed", name);
1385 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1388 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1393 /* Fork thread #1: git to program. */
1396 die_errno("Can't start thread for copying data");
1397 else if (pid1 == 0) {
1398 udt_kill_transfer(&s->ptg);
1399 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1402 /* Fork thread #2: program to git. */
1405 die_errno("Can't start thread for copying data");
1406 else if (pid2 == 0) {
1407 udt_kill_transfer(&s->gtp);
1408 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1412 * Close both streams in parent as to not interfere with
1413 * end of file detection and wait for both tasks to finish.
1415 udt_kill_transfer(&s->gtp);
1416 udt_kill_transfer(&s->ptg);
1417 ret |= tloop_join(pid1, "Git to program copy");
1418 ret |= tloop_join(pid2, "Program to git copy");
1424 * Copies data from stdin to output and from input to stdout simultaneously.
1425 * Additionally filtering through given filter. If filter is NULL, uses
1428 int bidirectional_transfer_loop(int input, int output)
1430 struct bidirectional_transfer_state state;
1432 /* Fill the state fields. */
1433 state.ptg.src = input;
1435 state.ptg.src_is_sock = (input == output);
1436 state.ptg.dest_is_sock = 0;
1437 state.ptg.state = SSTATE_TRANSFERRING;
1438 state.ptg.bufuse = 0;
1439 state.ptg.src_name = "remote input";
1440 state.ptg.dest_name = "stdout";
1443 state.gtp.dest = output;
1444 state.gtp.src_is_sock = 0;
1445 state.gtp.dest_is_sock = (input == output);
1446 state.gtp.state = SSTATE_TRANSFERRING;
1447 state.gtp.bufuse = 0;
1448 state.gtp.src_name = "stdin";
1449 state.gtp.dest_name = "remote output";
1451 return tloop_spawnwait_tasks(&state);