Merge branch 'jk/unused-post-2.40'
[git.git] / transport-helper.c
blob82ac63e260949fc518f358ccf70e69218dbee047
1 #include "cache.h"
2 #include "transport.h"
3 #include "quote.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "hex.h"
8 #include "revision.h"
9 #include "remote.h"
10 #include "string-list.h"
11 #include "thread-utils.h"
12 #include "sigchain.h"
13 #include "strvec.h"
14 #include "refs.h"
15 #include "refspec.h"
16 #include "transport-internal.h"
17 #include "protocol.h"
19 static int debug;
21 struct helper_data {
22 const char *name;
23 struct child_process *helper;
24 FILE *out;
25 unsigned fetch : 1,
26 import : 1,
27 bidi_import : 1,
28 export : 1,
29 option : 1,
30 push : 1,
31 connect : 1,
32 stateless_connect : 1,
33 signed_tags : 1,
34 check_connectivity : 1,
35 no_disconnect_req : 1,
36 no_private_update : 1,
37 object_format : 1;
40 * As an optimization, the transport code may invoke fetch before
41 * get_refs_list. If this happens, and if the transport helper doesn't
42 * support connect or stateless_connect, we need to invoke
43 * get_refs_list ourselves if we haven't already done so. Keep track of
44 * whether we have invoked get_refs_list.
46 unsigned get_refs_list_called : 1;
48 char *export_marks;
49 char *import_marks;
50 /* These go from remote name (as in "list") to private name */
51 struct refspec rs;
52 /* Transport options for fetch-pack/send-pack (should one of
53 * those be invoked).
55 struct git_transport_options transport_options;
58 static void sendline(struct helper_data *helper, struct strbuf *buffer)
60 if (debug)
61 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
62 if (write_in_full(helper->helper->in, buffer->buf, buffer->len) < 0)
63 die_errno(_("full write to remote helper failed"));
66 static int recvline_fh(FILE *helper, struct strbuf *buffer)
68 strbuf_reset(buffer);
69 if (debug)
70 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
71 if (strbuf_getline(buffer, helper) == EOF) {
72 if (debug)
73 fprintf(stderr, "Debug: Remote helper quit.\n");
74 return 1;
77 if (debug)
78 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
79 return 0;
82 static int recvline(struct helper_data *helper, struct strbuf *buffer)
84 return recvline_fh(helper->out, buffer);
87 static void write_constant(int fd, const char *str)
89 if (debug)
90 fprintf(stderr, "Debug: Remote helper: -> %s", str);
91 if (write_in_full(fd, str, strlen(str)) < 0)
92 die_errno(_("full write to remote helper failed"));
95 static const char *remove_ext_force(const char *url)
97 if (url) {
98 const char *colon = strchr(url, ':');
99 if (colon && colon[1] == ':')
100 return colon + 2;
102 return url;
105 static void do_take_over(struct transport *transport)
107 struct helper_data *data;
108 data = (struct helper_data *)transport->data;
109 transport_take_over(transport, data->helper);
110 fclose(data->out);
111 free(data);
114 static void standard_options(struct transport *t);
116 static struct child_process *get_helper(struct transport *transport)
118 struct helper_data *data = transport->data;
119 struct strbuf buf = STRBUF_INIT;
120 struct child_process *helper;
121 int duped;
122 int code;
124 if (data->helper)
125 return data->helper;
127 helper = xmalloc(sizeof(*helper));
128 child_process_init(helper);
129 helper->in = -1;
130 helper->out = -1;
131 helper->err = 0;
132 strvec_pushf(&helper->args, "remote-%s", data->name);
133 strvec_push(&helper->args, transport->remote->name);
134 strvec_push(&helper->args, remove_ext_force(transport->url));
135 helper->git_cmd = 1;
136 helper->silent_exec_failure = 1;
138 if (have_git_dir())
139 strvec_pushf(&helper->env, "%s=%s",
140 GIT_DIR_ENVIRONMENT, get_git_dir());
142 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
144 code = start_command(helper);
145 if (code < 0 && errno == ENOENT)
146 die(_("unable to find remote helper for '%s'"), data->name);
147 else if (code != 0)
148 exit(code);
150 data->helper = helper;
151 data->no_disconnect_req = 0;
152 refspec_init(&data->rs, REFSPEC_FETCH);
155 * Open the output as FILE* so strbuf_getline_*() family of
156 * functions can be used.
157 * Do this with duped fd because fclose() will close the fd,
158 * and stuff like taking over will require the fd to remain.
160 duped = dup(helper->out);
161 if (duped < 0)
162 die_errno(_("can't dup helper output fd"));
163 data->out = xfdopen(duped, "r");
165 write_constant(helper->in, "capabilities\n");
167 while (1) {
168 const char *capname, *arg;
169 int mandatory = 0;
170 if (recvline(data, &buf))
171 exit(128);
173 if (!*buf.buf)
174 break;
176 if (*buf.buf == '*') {
177 capname = buf.buf + 1;
178 mandatory = 1;
179 } else
180 capname = buf.buf;
182 if (debug)
183 fprintf(stderr, "Debug: Got cap %s\n", capname);
184 if (!strcmp(capname, "fetch"))
185 data->fetch = 1;
186 else if (!strcmp(capname, "option"))
187 data->option = 1;
188 else if (!strcmp(capname, "push"))
189 data->push = 1;
190 else if (!strcmp(capname, "import"))
191 data->import = 1;
192 else if (!strcmp(capname, "bidi-import"))
193 data->bidi_import = 1;
194 else if (!strcmp(capname, "export"))
195 data->export = 1;
196 else if (!strcmp(capname, "check-connectivity"))
197 data->check_connectivity = 1;
198 else if (skip_prefix(capname, "refspec ", &arg)) {
199 refspec_append(&data->rs, arg);
200 } else if (!strcmp(capname, "connect")) {
201 data->connect = 1;
202 } else if (!strcmp(capname, "stateless-connect")) {
203 data->stateless_connect = 1;
204 } else if (!strcmp(capname, "signed-tags")) {
205 data->signed_tags = 1;
206 } else if (skip_prefix(capname, "export-marks ", &arg)) {
207 data->export_marks = xstrdup(arg);
208 } else if (skip_prefix(capname, "import-marks ", &arg)) {
209 data->import_marks = xstrdup(arg);
210 } else if (starts_with(capname, "no-private-update")) {
211 data->no_private_update = 1;
212 } else if (starts_with(capname, "object-format")) {
213 data->object_format = 1;
214 } else if (mandatory) {
215 die(_("unknown mandatory capability %s; this remote "
216 "helper probably needs newer version of Git"),
217 capname);
220 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
221 warning(_("this remote helper should implement refspec capability"));
223 strbuf_release(&buf);
224 if (debug)
225 fprintf(stderr, "Debug: Capabilities complete.\n");
226 standard_options(transport);
227 return data->helper;
230 static int disconnect_helper(struct transport *transport)
232 struct helper_data *data = transport->data;
233 int res = 0;
235 if (data->helper) {
236 if (debug)
237 fprintf(stderr, "Debug: Disconnecting.\n");
238 if (!data->no_disconnect_req) {
240 * Ignore write errors; there's nothing we can do,
241 * since we're about to close the pipe anyway. And the
242 * most likely error is EPIPE due to the helper dying
243 * to report an error itself.
245 sigchain_push(SIGPIPE, SIG_IGN);
246 xwrite(data->helper->in, "\n", 1);
247 sigchain_pop(SIGPIPE);
249 close(data->helper->in);
250 close(data->helper->out);
251 fclose(data->out);
252 res = finish_command(data->helper);
253 FREE_AND_NULL(data->helper);
255 return res;
258 static const char *unsupported_options[] = {
259 TRANS_OPT_UPLOADPACK,
260 TRANS_OPT_RECEIVEPACK,
261 TRANS_OPT_THIN,
262 TRANS_OPT_KEEP
265 static const char *boolean_options[] = {
266 TRANS_OPT_THIN,
267 TRANS_OPT_KEEP,
268 TRANS_OPT_FOLLOWTAGS,
269 TRANS_OPT_DEEPEN_RELATIVE
272 static int strbuf_set_helper_option(struct helper_data *data,
273 struct strbuf *buf)
275 int ret;
277 sendline(data, buf);
278 if (recvline(data, buf))
279 exit(128);
281 if (!strcmp(buf->buf, "ok"))
282 ret = 0;
283 else if (starts_with(buf->buf, "error"))
284 ret = -1;
285 else if (!strcmp(buf->buf, "unsupported"))
286 ret = 1;
287 else {
288 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
289 ret = 1;
291 return ret;
294 static int string_list_set_helper_option(struct helper_data *data,
295 const char *name,
296 struct string_list *list)
298 struct strbuf buf = STRBUF_INIT;
299 int i, ret = 0;
301 for (i = 0; i < list->nr; i++) {
302 strbuf_addf(&buf, "option %s ", name);
303 quote_c_style(list->items[i].string, &buf, NULL, 0);
304 strbuf_addch(&buf, '\n');
306 if ((ret = strbuf_set_helper_option(data, &buf)))
307 break;
308 strbuf_reset(&buf);
310 strbuf_release(&buf);
311 return ret;
314 static int set_helper_option(struct transport *transport,
315 const char *name, const char *value)
317 struct helper_data *data = transport->data;
318 struct strbuf buf = STRBUF_INIT;
319 int i, ret, is_bool = 0;
321 get_helper(transport);
323 if (!data->option)
324 return 1;
326 if (!strcmp(name, "deepen-not"))
327 return string_list_set_helper_option(data, name,
328 (struct string_list *)value);
330 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
331 if (!strcmp(name, unsupported_options[i]))
332 return 1;
335 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
336 if (!strcmp(name, boolean_options[i])) {
337 is_bool = 1;
338 break;
342 strbuf_addf(&buf, "option %s ", name);
343 if (is_bool)
344 strbuf_addstr(&buf, value ? "true" : "false");
345 else
346 quote_c_style(value, &buf, NULL, 0);
347 strbuf_addch(&buf, '\n');
349 ret = strbuf_set_helper_option(data, &buf);
350 strbuf_release(&buf);
351 return ret;
354 static void standard_options(struct transport *t)
356 char buf[16];
357 int v = t->verbose;
359 set_helper_option(t, "progress", t->progress ? "true" : "false");
361 xsnprintf(buf, sizeof(buf), "%d", v + 1);
362 set_helper_option(t, "verbosity", buf);
364 switch (t->family) {
365 case TRANSPORT_FAMILY_ALL:
367 * this is already the default,
368 * do not break old remote helpers by setting "all" here
370 break;
371 case TRANSPORT_FAMILY_IPV4:
372 set_helper_option(t, "family", "ipv4");
373 break;
374 case TRANSPORT_FAMILY_IPV6:
375 set_helper_option(t, "family", "ipv6");
376 break;
380 static int release_helper(struct transport *transport)
382 int res = 0;
383 struct helper_data *data = transport->data;
384 refspec_clear(&data->rs);
385 res = disconnect_helper(transport);
386 free(transport->data);
387 return res;
390 static int fetch_with_fetch(struct transport *transport,
391 int nr_heads, struct ref **to_fetch)
393 struct helper_data *data = transport->data;
394 int i;
395 struct strbuf buf = STRBUF_INIT;
397 for (i = 0; i < nr_heads; i++) {
398 const struct ref *posn = to_fetch[i];
399 if (posn->status & REF_STATUS_UPTODATE)
400 continue;
402 strbuf_addf(&buf, "fetch %s %s\n",
403 oid_to_hex(&posn->old_oid),
404 posn->symref ? posn->symref : posn->name);
407 strbuf_addch(&buf, '\n');
408 sendline(data, &buf);
410 while (1) {
411 const char *name;
413 if (recvline(data, &buf))
414 exit(128);
416 if (skip_prefix(buf.buf, "lock ", &name)) {
417 if (transport->pack_lockfiles.nr)
418 warning(_("%s also locked %s"), data->name, name);
419 else
420 string_list_append(&transport->pack_lockfiles,
421 name);
423 else if (data->check_connectivity &&
424 data->transport_options.check_self_contained_and_connected &&
425 !strcmp(buf.buf, "connectivity-ok"))
426 data->transport_options.self_contained_and_connected = 1;
427 else if (!buf.len)
428 break;
429 else
430 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
432 strbuf_release(&buf);
433 return 0;
436 static int get_importer(struct transport *transport, struct child_process *fastimport)
438 struct child_process *helper = get_helper(transport);
439 struct helper_data *data = transport->data;
440 int cat_blob_fd, code;
441 child_process_init(fastimport);
442 fastimport->in = xdup(helper->out);
443 strvec_push(&fastimport->args, "fast-import");
444 strvec_push(&fastimport->args, "--allow-unsafe-features");
445 strvec_push(&fastimport->args, debug ? "--stats" : "--quiet");
447 if (data->bidi_import) {
448 cat_blob_fd = xdup(helper->in);
449 strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
451 fastimport->git_cmd = 1;
453 code = start_command(fastimport);
454 return code;
457 static int get_exporter(struct transport *transport,
458 struct child_process *fastexport,
459 struct string_list *revlist_args)
461 struct helper_data *data = transport->data;
462 struct child_process *helper = get_helper(transport);
463 int i;
465 child_process_init(fastexport);
467 /* we need to duplicate helper->in because we want to use it after
468 * fastexport is done with it. */
469 fastexport->out = dup(helper->in);
470 strvec_push(&fastexport->args, "fast-export");
471 strvec_push(&fastexport->args, "--use-done-feature");
472 strvec_push(&fastexport->args, data->signed_tags ?
473 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
474 if (data->export_marks)
475 strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
476 if (data->import_marks)
477 strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
479 for (i = 0; i < revlist_args->nr; i++)
480 strvec_push(&fastexport->args, revlist_args->items[i].string);
482 fastexport->git_cmd = 1;
483 return start_command(fastexport);
486 static int fetch_with_import(struct transport *transport,
487 int nr_heads, struct ref **to_fetch)
489 struct child_process fastimport;
490 struct helper_data *data = transport->data;
491 int i;
492 struct ref *posn;
493 struct strbuf buf = STRBUF_INIT;
495 get_helper(transport);
497 if (get_importer(transport, &fastimport))
498 die(_("couldn't run fast-import"));
500 for (i = 0; i < nr_heads; i++) {
501 posn = to_fetch[i];
502 if (posn->status & REF_STATUS_UPTODATE)
503 continue;
505 strbuf_addf(&buf, "import %s\n",
506 posn->symref ? posn->symref : posn->name);
507 sendline(data, &buf);
508 strbuf_reset(&buf);
511 write_constant(data->helper->in, "\n");
513 * remote-helpers that advertise the bidi-import capability are required to
514 * buffer the complete batch of import commands until this newline before
515 * sending data to fast-import.
516 * These helpers read back data from fast-import on their stdin, which could
517 * be mixed with import commands, otherwise.
520 if (finish_command(&fastimport))
521 die(_("error while running fast-import"));
524 * The fast-import stream of a remote helper that advertises
525 * the "refspec" capability writes to the refs named after the
526 * right hand side of the first refspec matching each ref we
527 * were fetching.
529 * (If no "refspec" capability was specified, for historical
530 * reasons we default to the equivalent of *:*.)
532 * Store the result in to_fetch[i].old_sha1. Callers such
533 * as "git fetch" can use the value to write feedback to the
534 * terminal, populate FETCH_HEAD, and determine what new value
535 * should be written to peer_ref if the update is a
536 * fast-forward or this is a forced update.
538 for (i = 0; i < nr_heads; i++) {
539 char *private, *name;
540 posn = to_fetch[i];
541 if (posn->status & REF_STATUS_UPTODATE)
542 continue;
543 name = posn->symref ? posn->symref : posn->name;
544 if (data->rs.nr)
545 private = apply_refspecs(&data->rs, name);
546 else
547 private = xstrdup(name);
548 if (private) {
549 if (read_ref(private, &posn->old_oid) < 0)
550 die(_("could not read ref %s"), private);
551 free(private);
554 strbuf_release(&buf);
555 return 0;
558 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
560 struct helper_data *data = transport->data;
561 int ret = 0;
562 int duped;
563 FILE *input;
564 struct child_process *helper;
566 helper = get_helper(transport);
569 * Yes, dup the pipe another time, as we need unbuffered version
570 * of input pipe as FILE*. fclose() closes the underlying fd and
571 * stream buffering only can be changed before first I/O operation
572 * on it.
574 duped = dup(helper->out);
575 if (duped < 0)
576 die_errno(_("can't dup helper output fd"));
577 input = xfdopen(duped, "r");
578 setvbuf(input, NULL, _IONBF, 0);
580 sendline(data, cmdbuf);
581 if (recvline_fh(input, cmdbuf))
582 exit(128);
584 if (!strcmp(cmdbuf->buf, "")) {
585 data->no_disconnect_req = 1;
586 if (debug)
587 fprintf(stderr, "Debug: Smart transport connection "
588 "ready.\n");
589 ret = 1;
590 } else if (!strcmp(cmdbuf->buf, "fallback")) {
591 if (debug)
592 fprintf(stderr, "Debug: Falling back to dumb "
593 "transport.\n");
594 } else {
595 die(_("unknown response to connect: %s"),
596 cmdbuf->buf);
599 fclose(input);
600 return ret;
603 static int process_connect_service(struct transport *transport,
604 const char *name, const char *exec)
606 struct helper_data *data = transport->data;
607 struct strbuf cmdbuf = STRBUF_INIT;
608 int ret = 0;
611 * Handle --upload-pack and friends. This is fire and forget...
612 * just warn if it fails.
614 if (strcmp(name, exec)) {
615 int r = set_helper_option(transport, "servpath", exec);
616 if (r > 0)
617 warning(_("setting remote service path not supported by protocol"));
618 else if (r < 0)
619 warning(_("invalid remote service path"));
622 if (data->connect) {
623 strbuf_addf(&cmdbuf, "connect %s\n", name);
624 ret = run_connect(transport, &cmdbuf);
625 } else if (data->stateless_connect &&
626 (get_protocol_version_config() == protocol_v2) &&
627 !strcmp("git-upload-pack", name)) {
628 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
629 ret = run_connect(transport, &cmdbuf);
630 if (ret)
631 transport->stateless_rpc = 1;
634 strbuf_release(&cmdbuf);
635 return ret;
638 static int process_connect(struct transport *transport,
639 int for_push)
641 struct helper_data *data = transport->data;
642 const char *name;
643 const char *exec;
645 name = for_push ? "git-receive-pack" : "git-upload-pack";
646 if (for_push)
647 exec = data->transport_options.receivepack;
648 else
649 exec = data->transport_options.uploadpack;
651 return process_connect_service(transport, name, exec);
654 static int connect_helper(struct transport *transport, const char *name,
655 const char *exec, int fd[2])
657 struct helper_data *data = transport->data;
659 /* Get_helper so connect is inited. */
660 get_helper(transport);
661 if (!data->connect)
662 die(_("operation not supported by protocol"));
664 if (!process_connect_service(transport, name, exec))
665 die(_("can't connect to subservice %s"), name);
667 fd[0] = data->helper->out;
668 fd[1] = data->helper->in;
669 return 0;
672 static struct ref *get_refs_list_using_list(struct transport *transport,
673 int for_push);
675 static int fetch_refs(struct transport *transport,
676 int nr_heads, struct ref **to_fetch)
678 struct helper_data *data = transport->data;
679 int i, count;
681 get_helper(transport);
683 if (process_connect(transport, 0)) {
684 do_take_over(transport);
685 return transport->vtable->fetch_refs(transport, nr_heads, to_fetch);
689 * If we reach here, then the server, the client, and/or the transport
690 * helper does not support protocol v2. --negotiate-only requires
691 * protocol v2.
693 if (data->transport_options.acked_commits) {
694 warning(_("--negotiate-only requires protocol v2"));
695 return -1;
698 if (!data->get_refs_list_called)
699 get_refs_list_using_list(transport, 0);
701 count = 0;
702 for (i = 0; i < nr_heads; i++)
703 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
704 count++;
706 if (!count)
707 return 0;
709 if (data->check_connectivity &&
710 data->transport_options.check_self_contained_and_connected)
711 set_helper_option(transport, "check-connectivity", "true");
713 if (transport->cloning)
714 set_helper_option(transport, "cloning", "true");
716 if (data->transport_options.update_shallow)
717 set_helper_option(transport, "update-shallow", "true");
719 if (data->transport_options.refetch)
720 set_helper_option(transport, "refetch", "true");
722 if (data->transport_options.filter_options.choice) {
723 const char *spec = expand_list_objects_filter_spec(
724 &data->transport_options.filter_options);
725 set_helper_option(transport, "filter", spec);
728 if (data->transport_options.negotiation_tips)
729 warning("Ignoring --negotiation-tip because the protocol does not support it.");
731 if (data->fetch)
732 return fetch_with_fetch(transport, nr_heads, to_fetch);
734 if (data->import)
735 return fetch_with_import(transport, nr_heads, to_fetch);
737 return -1;
740 struct push_update_ref_state {
741 struct ref *hint;
742 struct ref_push_report *report;
743 int new_report;
746 static int push_update_ref_status(struct strbuf *buf,
747 struct push_update_ref_state *state,
748 struct ref *remote_refs)
750 char *refname, *msg;
751 int status, forced = 0;
753 if (starts_with(buf->buf, "option ")) {
754 struct object_id old_oid, new_oid;
755 const char *key, *val;
756 char *p;
758 if (!state->hint || !(state->report || state->new_report))
759 die(_("'option' without a matching 'ok/error' directive"));
760 if (state->new_report) {
761 if (!state->hint->report) {
762 CALLOC_ARRAY(state->hint->report, 1);
763 state->report = state->hint->report;
764 } else {
765 state->report = state->hint->report;
766 while (state->report->next)
767 state->report = state->report->next;
768 CALLOC_ARRAY(state->report->next, 1);
769 state->report = state->report->next;
771 state->new_report = 0;
773 key = buf->buf + 7;
774 p = strchr(key, ' ');
775 if (p)
776 *p++ = '\0';
777 val = p;
778 if (!strcmp(key, "refname"))
779 state->report->ref_name = xstrdup_or_null(val);
780 else if (!strcmp(key, "old-oid") && val &&
781 !parse_oid_hex(val, &old_oid, &val))
782 state->report->old_oid = oiddup(&old_oid);
783 else if (!strcmp(key, "new-oid") && val &&
784 !parse_oid_hex(val, &new_oid, &val))
785 state->report->new_oid = oiddup(&new_oid);
786 else if (!strcmp(key, "forced-update"))
787 state->report->forced_update = 1;
788 /* Not update remote namespace again. */
789 return 1;
792 state->report = NULL;
793 state->new_report = 0;
795 if (starts_with(buf->buf, "ok ")) {
796 status = REF_STATUS_OK;
797 refname = buf->buf + 3;
798 } else if (starts_with(buf->buf, "error ")) {
799 status = REF_STATUS_REMOTE_REJECT;
800 refname = buf->buf + 6;
801 } else
802 die(_("expected ok/error, helper said '%s'"), buf->buf);
804 msg = strchr(refname, ' ');
805 if (msg) {
806 struct strbuf msg_buf = STRBUF_INIT;
807 const char *end;
809 *msg++ = '\0';
810 if (!unquote_c_style(&msg_buf, msg, &end))
811 msg = strbuf_detach(&msg_buf, NULL);
812 else
813 msg = xstrdup(msg);
814 strbuf_release(&msg_buf);
816 if (!strcmp(msg, "no match")) {
817 status = REF_STATUS_NONE;
818 FREE_AND_NULL(msg);
820 else if (!strcmp(msg, "up to date")) {
821 status = REF_STATUS_UPTODATE;
822 FREE_AND_NULL(msg);
824 else if (!strcmp(msg, "non-fast forward")) {
825 status = REF_STATUS_REJECT_NONFASTFORWARD;
826 FREE_AND_NULL(msg);
828 else if (!strcmp(msg, "already exists")) {
829 status = REF_STATUS_REJECT_ALREADY_EXISTS;
830 FREE_AND_NULL(msg);
832 else if (!strcmp(msg, "fetch first")) {
833 status = REF_STATUS_REJECT_FETCH_FIRST;
834 FREE_AND_NULL(msg);
836 else if (!strcmp(msg, "needs force")) {
837 status = REF_STATUS_REJECT_NEEDS_FORCE;
838 FREE_AND_NULL(msg);
840 else if (!strcmp(msg, "stale info")) {
841 status = REF_STATUS_REJECT_STALE;
842 FREE_AND_NULL(msg);
844 else if (!strcmp(msg, "remote ref updated since checkout")) {
845 status = REF_STATUS_REJECT_REMOTE_UPDATED;
846 FREE_AND_NULL(msg);
848 else if (!strcmp(msg, "forced update")) {
849 forced = 1;
850 FREE_AND_NULL(msg);
852 else if (!strcmp(msg, "expecting report")) {
853 status = REF_STATUS_EXPECTING_REPORT;
854 FREE_AND_NULL(msg);
858 if (state->hint)
859 state->hint = find_ref_by_name(state->hint, refname);
860 if (!state->hint)
861 state->hint = find_ref_by_name(remote_refs, refname);
862 if (!state->hint) {
863 warning(_("helper reported unexpected status of %s"), refname);
864 return 1;
867 if (state->hint->status != REF_STATUS_NONE) {
869 * Earlier, the ref was marked not to be pushed, so ignore the ref
870 * status reported by the remote helper if the latter is 'no match'.
872 if (status == REF_STATUS_NONE)
873 return 1;
876 if (status == REF_STATUS_OK)
877 state->new_report = 1;
878 state->hint->status = status;
879 state->hint->forced_update |= forced;
880 state->hint->remote_status = msg;
881 return !(status == REF_STATUS_OK);
884 static int push_update_refs_status(struct helper_data *data,
885 struct ref *remote_refs,
886 int flags)
888 struct ref *ref;
889 struct ref_push_report *report;
890 struct strbuf buf = STRBUF_INIT;
891 struct push_update_ref_state state = { remote_refs, NULL, 0 };
893 for (;;) {
894 if (recvline(data, &buf)) {
895 strbuf_release(&buf);
896 return 1;
898 if (!buf.len)
899 break;
900 push_update_ref_status(&buf, &state, remote_refs);
902 strbuf_release(&buf);
904 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
905 return 0;
907 /* propagate back the update to the remote namespace */
908 for (ref = remote_refs; ref; ref = ref->next) {
909 char *private;
911 if (ref->status != REF_STATUS_OK)
912 continue;
914 if (!ref->report) {
915 private = apply_refspecs(&data->rs, ref->name);
916 if (!private)
917 continue;
918 update_ref("update by helper", private, &(ref->new_oid),
919 NULL, 0, 0);
920 free(private);
921 } else {
922 for (report = ref->report; report; report = report->next) {
923 private = apply_refspecs(&data->rs,
924 report->ref_name
925 ? report->ref_name
926 : ref->name);
927 if (!private)
928 continue;
929 update_ref("update by helper", private,
930 report->new_oid
931 ? report->new_oid
932 : &(ref->new_oid),
933 NULL, 0, 0);
934 free(private);
938 return 0;
941 static void set_common_push_options(struct transport *transport,
942 const char *name, int flags)
944 if (flags & TRANSPORT_PUSH_DRY_RUN) {
945 if (set_helper_option(transport, "dry-run", "true") != 0)
946 die(_("helper %s does not support dry-run"), name);
947 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
948 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
949 die(_("helper %s does not support --signed"), name);
950 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
951 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
952 die(_("helper %s does not support --signed=if-asked"), name);
955 if (flags & TRANSPORT_PUSH_ATOMIC)
956 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
957 die(_("helper %s does not support --atomic"), name);
959 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
960 if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0)
961 die(_("helper %s does not support --%s"),
962 name, TRANS_OPT_FORCE_IF_INCLUDES);
964 if (flags & TRANSPORT_PUSH_OPTIONS) {
965 struct string_list_item *item;
966 for_each_string_list_item(item, transport->push_options)
967 if (set_helper_option(transport, "push-option", item->string) != 0)
968 die(_("helper %s does not support 'push-option'"), name);
972 static int push_refs_with_push(struct transport *transport,
973 struct ref *remote_refs, int flags)
975 int force_all = flags & TRANSPORT_PUSH_FORCE;
976 int mirror = flags & TRANSPORT_PUSH_MIRROR;
977 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
978 struct helper_data *data = transport->data;
979 struct strbuf buf = STRBUF_INIT;
980 struct ref *ref;
981 struct string_list cas_options = STRING_LIST_INIT_DUP;
982 struct string_list_item *cas_option;
984 get_helper(transport);
985 if (!data->push)
986 return 1;
988 for (ref = remote_refs; ref; ref = ref->next) {
989 if (!ref->peer_ref && !mirror)
990 continue;
992 /* Check for statuses set by set_ref_status_for_push() */
993 switch (ref->status) {
994 case REF_STATUS_REJECT_NONFASTFORWARD:
995 case REF_STATUS_REJECT_STALE:
996 case REF_STATUS_REJECT_ALREADY_EXISTS:
997 case REF_STATUS_REJECT_REMOTE_UPDATED:
998 if (atomic) {
999 reject_atomic_push(remote_refs, mirror);
1000 string_list_clear(&cas_options, 0);
1001 return 0;
1002 } else
1003 continue;
1004 case REF_STATUS_UPTODATE:
1005 continue;
1006 default:
1007 ; /* do nothing */
1010 if (force_all)
1011 ref->force = 1;
1013 strbuf_addstr(&buf, "push ");
1014 if (!ref->deletion) {
1015 if (ref->force)
1016 strbuf_addch(&buf, '+');
1017 if (ref->peer_ref)
1018 strbuf_addstr(&buf, ref->peer_ref->name);
1019 else
1020 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
1022 strbuf_addch(&buf, ':');
1023 strbuf_addstr(&buf, ref->name);
1024 strbuf_addch(&buf, '\n');
1027 * The "--force-with-lease" options without explicit
1028 * values to expect have already been expanded into
1029 * the ref->old_oid_expect[] field; we can ignore
1030 * transport->smart_options->cas altogether and instead
1031 * can enumerate them from the refs.
1033 if (ref->expect_old_sha1) {
1034 struct strbuf cas = STRBUF_INIT;
1035 strbuf_addf(&cas, "%s:%s",
1036 ref->name, oid_to_hex(&ref->old_oid_expect));
1037 string_list_append_nodup(&cas_options,
1038 strbuf_detach(&cas, NULL));
1041 if (buf.len == 0) {
1042 string_list_clear(&cas_options, 0);
1043 return 0;
1046 for_each_string_list_item(cas_option, &cas_options)
1047 set_helper_option(transport, "cas", cas_option->string);
1048 set_common_push_options(transport, data->name, flags);
1050 strbuf_addch(&buf, '\n');
1051 sendline(data, &buf);
1052 strbuf_release(&buf);
1053 string_list_clear(&cas_options, 0);
1055 return push_update_refs_status(data, remote_refs, flags);
1058 static int push_refs_with_export(struct transport *transport,
1059 struct ref *remote_refs, int flags)
1061 struct ref *ref;
1062 struct child_process *helper, exporter;
1063 struct helper_data *data = transport->data;
1064 struct string_list revlist_args = STRING_LIST_INIT_DUP;
1065 struct strbuf buf = STRBUF_INIT;
1067 if (!data->rs.nr)
1068 die(_("remote-helper doesn't support push; refspec needed"));
1070 set_common_push_options(transport, data->name, flags);
1071 if (flags & TRANSPORT_PUSH_FORCE) {
1072 if (set_helper_option(transport, "force", "true") != 0)
1073 warning(_("helper %s does not support 'force'"), data->name);
1076 helper = get_helper(transport);
1078 write_constant(helper->in, "export\n");
1080 for (ref = remote_refs; ref; ref = ref->next) {
1081 char *private;
1082 struct object_id oid;
1084 private = apply_refspecs(&data->rs, ref->name);
1085 if (private && !get_oid(private, &oid)) {
1086 strbuf_addf(&buf, "^%s", private);
1087 string_list_append_nodup(&revlist_args,
1088 strbuf_detach(&buf, NULL));
1089 oidcpy(&ref->old_oid, &oid);
1091 free(private);
1093 if (ref->peer_ref) {
1094 if (strcmp(ref->name, ref->peer_ref->name)) {
1095 if (!ref->deletion) {
1096 const char *name;
1097 int flag;
1099 /* Follow symbolic refs (mainly for HEAD). */
1100 name = resolve_ref_unsafe(ref->peer_ref->name,
1101 RESOLVE_REF_READING,
1102 &oid, &flag);
1103 if (!name || !(flag & REF_ISSYMREF))
1104 name = ref->peer_ref->name;
1106 strbuf_addf(&buf, "%s:%s", name, ref->name);
1107 } else
1108 strbuf_addf(&buf, ":%s", ref->name);
1110 string_list_append(&revlist_args, "--refspec");
1111 string_list_append(&revlist_args, buf.buf);
1112 strbuf_release(&buf);
1114 if (!ref->deletion)
1115 string_list_append(&revlist_args, ref->peer_ref->name);
1119 if (get_exporter(transport, &exporter, &revlist_args))
1120 die(_("couldn't run fast-export"));
1122 string_list_clear(&revlist_args, 1);
1124 if (finish_command(&exporter))
1125 die(_("error while running fast-export"));
1126 if (push_update_refs_status(data, remote_refs, flags))
1127 return 1;
1129 if (data->export_marks) {
1130 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1131 rename(buf.buf, data->export_marks);
1132 strbuf_release(&buf);
1135 return 0;
1138 static int push_refs(struct transport *transport,
1139 struct ref *remote_refs, int flags)
1141 struct helper_data *data = transport->data;
1143 if (process_connect(transport, 1)) {
1144 do_take_over(transport);
1145 return transport->vtable->push_refs(transport, remote_refs, flags);
1148 if (!remote_refs) {
1149 fprintf(stderr,
1150 _("No refs in common and none specified; doing nothing.\n"
1151 "Perhaps you should specify a branch.\n"));
1152 return 0;
1155 if (data->push)
1156 return push_refs_with_push(transport, remote_refs, flags);
1158 if (data->export)
1159 return push_refs_with_export(transport, remote_refs, flags);
1161 return -1;
1165 static int has_attribute(const char *attrs, const char *attr)
1167 int len;
1168 if (!attrs)
1169 return 0;
1171 len = strlen(attr);
1172 for (;;) {
1173 const char *space = strchrnul(attrs, ' ');
1174 if (len == space - attrs && !strncmp(attrs, attr, len))
1175 return 1;
1176 if (!*space)
1177 return 0;
1178 attrs = space + 1;
1182 static struct ref *get_refs_list(struct transport *transport, int for_push,
1183 struct transport_ls_refs_options *transport_options)
1185 get_helper(transport);
1187 if (process_connect(transport, for_push)) {
1188 do_take_over(transport);
1189 return transport->vtable->get_refs_list(transport, for_push,
1190 transport_options);
1193 return get_refs_list_using_list(transport, for_push);
1196 static struct ref *get_refs_list_using_list(struct transport *transport,
1197 int for_push)
1199 struct helper_data *data = transport->data;
1200 struct child_process *helper;
1201 struct ref *ret = NULL;
1202 struct ref **tail = &ret;
1203 struct ref *posn;
1204 struct strbuf buf = STRBUF_INIT;
1206 data->get_refs_list_called = 1;
1207 helper = get_helper(transport);
1209 if (data->object_format) {
1210 write_str_in_full(helper->in, "option object-format\n");
1211 if (recvline(data, &buf) || strcmp(buf.buf, "ok"))
1212 exit(128);
1215 if (data->push && for_push)
1216 write_str_in_full(helper->in, "list for-push\n");
1217 else
1218 write_str_in_full(helper->in, "list\n");
1220 while (1) {
1221 char *eov, *eon;
1222 if (recvline(data, &buf))
1223 exit(128);
1225 if (!*buf.buf)
1226 break;
1227 else if (buf.buf[0] == ':') {
1228 const char *value;
1229 if (skip_prefix(buf.buf, ":object-format ", &value)) {
1230 int algo = hash_algo_by_name(value);
1231 if (algo == GIT_HASH_UNKNOWN)
1232 die(_("unsupported object format '%s'"),
1233 value);
1234 transport->hash_algo = &hash_algos[algo];
1236 continue;
1239 eov = strchr(buf.buf, ' ');
1240 if (!eov)
1241 die(_("malformed response in ref list: %s"), buf.buf);
1242 eon = strchr(eov + 1, ' ');
1243 *eov = '\0';
1244 if (eon)
1245 *eon = '\0';
1246 *tail = alloc_ref(eov + 1);
1247 if (buf.buf[0] == '@')
1248 (*tail)->symref = xstrdup(buf.buf + 1);
1249 else if (buf.buf[0] != '?')
1250 get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo);
1251 if (eon) {
1252 if (has_attribute(eon + 1, "unchanged")) {
1253 (*tail)->status |= REF_STATUS_UPTODATE;
1254 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1255 die(_("could not read ref %s"),
1256 (*tail)->name);
1259 tail = &((*tail)->next);
1261 if (debug)
1262 fprintf(stderr, "Debug: Read ref listing.\n");
1263 strbuf_release(&buf);
1265 for (posn = ret; posn; posn = posn->next)
1266 resolve_remote_symref(posn, ret);
1268 return ret;
1271 static int get_bundle_uri(struct transport *transport)
1273 get_helper(transport);
1275 if (process_connect(transport, 0)) {
1276 do_take_over(transport);
1277 return transport->vtable->get_bundle_uri(transport);
1280 return -1;
1283 static struct transport_vtable vtable = {
1284 .set_option = set_helper_option,
1285 .get_refs_list = get_refs_list,
1286 .get_bundle_uri = get_bundle_uri,
1287 .fetch_refs = fetch_refs,
1288 .push_refs = push_refs,
1289 .connect = connect_helper,
1290 .disconnect = release_helper
1293 int transport_helper_init(struct transport *transport, const char *name)
1295 struct helper_data *data = xcalloc(1, sizeof(*data));
1296 data->name = name;
1298 transport_check_allowed(name);
1300 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1301 debug = 1;
1303 list_objects_filter_init(&data->transport_options.filter_options);
1305 transport->data = data;
1306 transport->vtable = &vtable;
1307 transport->smart_options = &(data->transport_options);
1308 return 0;
1312 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1313 * buffer less), so attempt reads and writes with up to that size.
1315 #define BUFFERSIZE 65536
1316 /* This should be enough to hold debugging message. */
1317 #define PBUFFERSIZE 8192
1319 /* Print bidirectional transfer loop debug message. */
1320 __attribute__((format (printf, 1, 2)))
1321 static void transfer_debug(const char *fmt, ...)
1324 * NEEDSWORK: This function is sometimes used from multiple threads, and
1325 * we end up using debug_enabled racily. That "should not matter" since
1326 * we always write the same value, but it's still wrong. This function
1327 * is listed in .tsan-suppressions for the time being.
1330 va_list args;
1331 char msgbuf[PBUFFERSIZE];
1332 static int debug_enabled = -1;
1334 if (debug_enabled < 0)
1335 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1336 if (!debug_enabled)
1337 return;
1339 va_start(args, fmt);
1340 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1341 va_end(args);
1342 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1345 /* Stream state: More data may be coming in this direction. */
1346 #define SSTATE_TRANSFERRING 0
1348 * Stream state: No more data coming in this direction, flushing rest of
1349 * data.
1351 #define SSTATE_FLUSHING 1
1352 /* Stream state: Transfer in this direction finished. */
1353 #define SSTATE_FINISHED 2
1355 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1356 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1357 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1359 /* Unidirectional transfer. */
1360 struct unidirectional_transfer {
1361 /* Source */
1362 int src;
1363 /* Destination */
1364 int dest;
1365 /* Is source socket? */
1366 int src_is_sock;
1367 /* Is destination socket? */
1368 int dest_is_sock;
1369 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1370 int state;
1371 /* Buffer. */
1372 char buf[BUFFERSIZE];
1373 /* Buffer used. */
1374 size_t bufuse;
1375 /* Name of source. */
1376 const char *src_name;
1377 /* Name of destination. */
1378 const char *dest_name;
1381 /* Closes the target (for writing) if transfer has finished. */
1382 static void udt_close_if_finished(struct unidirectional_transfer *t)
1384 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1385 t->state = SSTATE_FINISHED;
1386 if (t->dest_is_sock)
1387 shutdown(t->dest, SHUT_WR);
1388 else
1389 close(t->dest);
1390 transfer_debug("Closed %s.", t->dest_name);
1395 * Tries to read data from source into buffer. If buffer is full,
1396 * no data is read. Returns 0 on success, -1 on error.
1398 static int udt_do_read(struct unidirectional_transfer *t)
1400 ssize_t bytes;
1402 if (t->bufuse == BUFFERSIZE)
1403 return 0; /* No space for more. */
1405 transfer_debug("%s is readable", t->src_name);
1406 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1407 if (bytes < 0) {
1408 error_errno(_("read(%s) failed"), t->src_name);
1409 return -1;
1410 } else if (bytes == 0) {
1411 transfer_debug("%s EOF (with %i bytes in buffer)",
1412 t->src_name, (int)t->bufuse);
1413 t->state = SSTATE_FLUSHING;
1414 } else if (bytes > 0) {
1415 t->bufuse += bytes;
1416 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1417 (int)bytes, t->src_name, (int)t->bufuse);
1419 return 0;
1422 /* Tries to write data from buffer into destination. If buffer is empty,
1423 * no data is written. Returns 0 on success, -1 on error.
1425 static int udt_do_write(struct unidirectional_transfer *t)
1427 ssize_t bytes;
1429 if (t->bufuse == 0)
1430 return 0; /* Nothing to write. */
1432 transfer_debug("%s is writable", t->dest_name);
1433 bytes = xwrite(t->dest, t->buf, t->bufuse);
1434 if (bytes < 0) {
1435 error_errno(_("write(%s) failed"), t->dest_name);
1436 return -1;
1437 } else if (bytes > 0) {
1438 t->bufuse -= bytes;
1439 if (t->bufuse)
1440 memmove(t->buf, t->buf + bytes, t->bufuse);
1441 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1442 (int)bytes, t->dest_name, (int)t->bufuse);
1444 return 0;
1448 /* State of bidirectional transfer loop. */
1449 struct bidirectional_transfer_state {
1450 /* Direction from program to git. */
1451 struct unidirectional_transfer ptg;
1452 /* Direction from git to program. */
1453 struct unidirectional_transfer gtp;
1456 static void *udt_copy_task_routine(void *udt)
1458 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1459 while (t->state != SSTATE_FINISHED) {
1460 if (STATE_NEEDS_READING(t->state))
1461 if (udt_do_read(t))
1462 return NULL;
1463 if (STATE_NEEDS_WRITING(t->state))
1464 if (udt_do_write(t))
1465 return NULL;
1466 if (STATE_NEEDS_CLOSING(t->state))
1467 udt_close_if_finished(t);
1469 return udt; /* Just some non-NULL value. */
1472 #ifndef NO_PTHREADS
1475 * Join thread, with appropriate errors on failure. Name is name for the
1476 * thread (for error messages). Returns 0 on success, 1 on failure.
1478 static int tloop_join(pthread_t thread, const char *name)
1480 int err;
1481 void *tret;
1482 err = pthread_join(thread, &tret);
1483 if (!tret) {
1484 error(_("%s thread failed"), name);
1485 return 1;
1487 if (err) {
1488 error(_("%s thread failed to join: %s"), name, strerror(err));
1489 return 1;
1491 return 0;
1495 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1496 * -1 on failure.
1498 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1500 pthread_t gtp_thread;
1501 pthread_t ptg_thread;
1502 int err;
1503 int ret = 0;
1504 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1505 &s->gtp);
1506 if (err)
1507 die(_("can't start thread for copying data: %s"), strerror(err));
1508 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1509 &s->ptg);
1510 if (err)
1511 die(_("can't start thread for copying data: %s"), strerror(err));
1513 ret |= tloop_join(gtp_thread, "Git to program copy");
1514 ret |= tloop_join(ptg_thread, "Program to git copy");
1515 return ret;
1517 #else
1519 /* Close the source and target (for writing) for transfer. */
1520 static void udt_kill_transfer(struct unidirectional_transfer *t)
1522 t->state = SSTATE_FINISHED;
1524 * Socket read end left open isn't a disaster if nobody
1525 * attempts to read from it (mingw compat headers do not
1526 * have SHUT_RD)...
1528 * We can't fully close the socket since otherwise gtp
1529 * task would first close the socket it sends data to
1530 * while closing the ptg file descriptors.
1532 if (!t->src_is_sock)
1533 close(t->src);
1534 if (t->dest_is_sock)
1535 shutdown(t->dest, SHUT_WR);
1536 else
1537 close(t->dest);
1541 * Join process, with appropriate errors on failure. Name is name for the
1542 * process (for error messages). Returns 0 on success, 1 on failure.
1544 static int tloop_join(pid_t pid, const char *name)
1546 int tret;
1547 if (waitpid(pid, &tret, 0) < 0) {
1548 error_errno(_("%s process failed to wait"), name);
1549 return 1;
1551 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1552 error(_("%s process failed"), name);
1553 return 1;
1555 return 0;
1559 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1560 * -1 on failure.
1562 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1564 pid_t pid1, pid2;
1565 int ret = 0;
1567 /* Fork thread #1: git to program. */
1568 pid1 = fork();
1569 if (pid1 < 0)
1570 die_errno(_("can't start thread for copying data"));
1571 else if (pid1 == 0) {
1572 udt_kill_transfer(&s->ptg);
1573 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1576 /* Fork thread #2: program to git. */
1577 pid2 = fork();
1578 if (pid2 < 0)
1579 die_errno(_("can't start thread for copying data"));
1580 else if (pid2 == 0) {
1581 udt_kill_transfer(&s->gtp);
1582 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1586 * Close both streams in parent as to not interfere with
1587 * end of file detection and wait for both tasks to finish.
1589 udt_kill_transfer(&s->gtp);
1590 udt_kill_transfer(&s->ptg);
1591 ret |= tloop_join(pid1, "Git to program copy");
1592 ret |= tloop_join(pid2, "Program to git copy");
1593 return ret;
1595 #endif
1598 * Copies data from stdin to output and from input to stdout simultaneously.
1599 * Additionally filtering through given filter. If filter is NULL, uses
1600 * identity filter.
1602 int bidirectional_transfer_loop(int input, int output)
1604 struct bidirectional_transfer_state state;
1606 /* Fill the state fields. */
1607 state.ptg.src = input;
1608 state.ptg.dest = 1;
1609 state.ptg.src_is_sock = (input == output);
1610 state.ptg.dest_is_sock = 0;
1611 state.ptg.state = SSTATE_TRANSFERRING;
1612 state.ptg.bufuse = 0;
1613 state.ptg.src_name = "remote input";
1614 state.ptg.dest_name = "stdout";
1616 state.gtp.src = 0;
1617 state.gtp.dest = output;
1618 state.gtp.src_is_sock = 0;
1619 state.gtp.dest_is_sock = (input == output);
1620 state.gtp.state = SSTATE_TRANSFERRING;
1621 state.gtp.bufuse = 0;
1622 state.gtp.src_name = "stdin";
1623 state.gtp.dest_name = "remote output";
1625 return tloop_spawnwait_tasks(&state);
1628 void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1630 struct ref *ref;
1632 /* Mark other refs as failed */
1633 for (ref = remote_refs; ref; ref = ref->next) {
1634 if (!ref->peer_ref && !mirror_mode)
1635 continue;
1637 switch (ref->status) {
1638 case REF_STATUS_NONE:
1639 case REF_STATUS_OK:
1640 case REF_STATUS_EXPECTING_REPORT:
1641 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1642 continue;
1643 default:
1644 break; /* do nothing */
1647 return;