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