]> git.ipfire.org Git - thirdparty/git.git/blob - transport-helper.c
doc: add documentation for the proc-receive hook
[thirdparty/git.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_lockfile)
414 warning(_("%s also locked %s"), data->name, name);
415 else
416 transport->pack_lockfile = xstrdup(name);
417 }
418 else if (data->check_connectivity &&
419 data->transport_options.check_self_contained_and_connected &&
420 !strcmp(buf.buf, "connectivity-ok"))
421 data->transport_options.self_contained_and_connected = 1;
422 else if (!buf.len)
423 break;
424 else
425 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
426 }
427 strbuf_release(&buf);
428 return 0;
429 }
430
431 static int get_importer(struct transport *transport, struct child_process *fastimport)
432 {
433 struct child_process *helper = get_helper(transport);
434 struct helper_data *data = transport->data;
435 int cat_blob_fd, code;
436 child_process_init(fastimport);
437 fastimport->in = xdup(helper->out);
438 argv_array_push(&fastimport->args, "fast-import");
439 argv_array_push(&fastimport->args, "--allow-unsafe-features");
440 argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");
441
442 if (data->bidi_import) {
443 cat_blob_fd = xdup(helper->in);
444 argv_array_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
445 }
446 fastimport->git_cmd = 1;
447
448 code = start_command(fastimport);
449 return code;
450 }
451
452 static int get_exporter(struct transport *transport,
453 struct child_process *fastexport,
454 struct string_list *revlist_args)
455 {
456 struct helper_data *data = transport->data;
457 struct child_process *helper = get_helper(transport);
458 int i;
459
460 child_process_init(fastexport);
461
462 /* we need to duplicate helper->in because we want to use it after
463 * fastexport is done with it. */
464 fastexport->out = dup(helper->in);
465 argv_array_push(&fastexport->args, "fast-export");
466 argv_array_push(&fastexport->args, "--use-done-feature");
467 argv_array_push(&fastexport->args, data->signed_tags ?
468 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
469 if (data->export_marks)
470 argv_array_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
471 if (data->import_marks)
472 argv_array_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
473
474 for (i = 0; i < revlist_args->nr; i++)
475 argv_array_push(&fastexport->args, revlist_args->items[i].string);
476
477 fastexport->git_cmd = 1;
478 return start_command(fastexport);
479 }
480
481 static int fetch_with_import(struct transport *transport,
482 int nr_heads, struct ref **to_fetch)
483 {
484 struct child_process fastimport;
485 struct helper_data *data = transport->data;
486 int i;
487 struct ref *posn;
488 struct strbuf buf = STRBUF_INIT;
489
490 get_helper(transport);
491
492 if (get_importer(transport, &fastimport))
493 die(_("couldn't run fast-import"));
494
495 for (i = 0; i < nr_heads; i++) {
496 posn = to_fetch[i];
497 if (posn->status & REF_STATUS_UPTODATE)
498 continue;
499
500 strbuf_addf(&buf, "import %s\n",
501 posn->symref ? posn->symref : posn->name);
502 sendline(data, &buf);
503 strbuf_reset(&buf);
504 }
505
506 write_constant(data->helper->in, "\n");
507 /*
508 * remote-helpers that advertise the bidi-import capability are required to
509 * buffer the complete batch of import commands until this newline before
510 * sending data to fast-import.
511 * These helpers read back data from fast-import on their stdin, which could
512 * be mixed with import commands, otherwise.
513 */
514
515 if (finish_command(&fastimport))
516 die(_("error while running fast-import"));
517
518 /*
519 * The fast-import stream of a remote helper that advertises
520 * the "refspec" capability writes to the refs named after the
521 * right hand side of the first refspec matching each ref we
522 * were fetching.
523 *
524 * (If no "refspec" capability was specified, for historical
525 * reasons we default to the equivalent of *:*.)
526 *
527 * Store the result in to_fetch[i].old_sha1. Callers such
528 * as "git fetch" can use the value to write feedback to the
529 * terminal, populate FETCH_HEAD, and determine what new value
530 * should be written to peer_ref if the update is a
531 * fast-forward or this is a forced update.
532 */
533 for (i = 0; i < nr_heads; i++) {
534 char *private, *name;
535 posn = to_fetch[i];
536 if (posn->status & REF_STATUS_UPTODATE)
537 continue;
538 name = posn->symref ? posn->symref : posn->name;
539 if (data->rs.nr)
540 private = apply_refspecs(&data->rs, name);
541 else
542 private = xstrdup(name);
543 if (private) {
544 if (read_ref(private, &posn->old_oid) < 0)
545 die(_("could not read ref %s"), private);
546 free(private);
547 }
548 }
549 strbuf_release(&buf);
550 return 0;
551 }
552
553 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
554 {
555 struct helper_data *data = transport->data;
556 int ret = 0;
557 int duped;
558 FILE *input;
559 struct child_process *helper;
560
561 helper = get_helper(transport);
562
563 /*
564 * Yes, dup the pipe another time, as we need unbuffered version
565 * of input pipe as FILE*. fclose() closes the underlying fd and
566 * stream buffering only can be changed before first I/O operation
567 * on it.
568 */
569 duped = dup(helper->out);
570 if (duped < 0)
571 die_errno(_("can't dup helper output fd"));
572 input = xfdopen(duped, "r");
573 setvbuf(input, NULL, _IONBF, 0);
574
575 sendline(data, cmdbuf);
576 if (recvline_fh(input, cmdbuf))
577 exit(128);
578
579 if (!strcmp(cmdbuf->buf, "")) {
580 data->no_disconnect_req = 1;
581 if (debug)
582 fprintf(stderr, "Debug: Smart transport connection "
583 "ready.\n");
584 ret = 1;
585 } else if (!strcmp(cmdbuf->buf, "fallback")) {
586 if (debug)
587 fprintf(stderr, "Debug: Falling back to dumb "
588 "transport.\n");
589 } else {
590 die(_("unknown response to connect: %s"),
591 cmdbuf->buf);
592 }
593
594 fclose(input);
595 return ret;
596 }
597
598 static int process_connect_service(struct transport *transport,
599 const char *name, const char *exec)
600 {
601 struct helper_data *data = transport->data;
602 struct strbuf cmdbuf = STRBUF_INIT;
603 int ret = 0;
604
605 /*
606 * Handle --upload-pack and friends. This is fire and forget...
607 * just warn if it fails.
608 */
609 if (strcmp(name, exec)) {
610 int r = set_helper_option(transport, "servpath", exec);
611 if (r > 0)
612 warning(_("setting remote service path not supported by protocol"));
613 else if (r < 0)
614 warning(_("invalid remote service path"));
615 }
616
617 if (data->connect) {
618 strbuf_addf(&cmdbuf, "connect %s\n", name);
619 ret = run_connect(transport, &cmdbuf);
620 } else if (data->stateless_connect &&
621 (get_protocol_version_config() == protocol_v2) &&
622 !strcmp("git-upload-pack", name)) {
623 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
624 ret = run_connect(transport, &cmdbuf);
625 if (ret)
626 transport->stateless_rpc = 1;
627 }
628
629 strbuf_release(&cmdbuf);
630 return ret;
631 }
632
633 static int process_connect(struct transport *transport,
634 int for_push)
635 {
636 struct helper_data *data = transport->data;
637 const char *name;
638 const char *exec;
639
640 name = for_push ? "git-receive-pack" : "git-upload-pack";
641 if (for_push)
642 exec = data->transport_options.receivepack;
643 else
644 exec = data->transport_options.uploadpack;
645
646 return process_connect_service(transport, name, exec);
647 }
648
649 static int connect_helper(struct transport *transport, const char *name,
650 const char *exec, int fd[2])
651 {
652 struct helper_data *data = transport->data;
653
654 /* Get_helper so connect is inited. */
655 get_helper(transport);
656 if (!data->connect)
657 die(_("operation not supported by protocol"));
658
659 if (!process_connect_service(transport, name, exec))
660 die(_("can't connect to subservice %s"), name);
661
662 fd[0] = data->helper->out;
663 fd[1] = data->helper->in;
664 return 0;
665 }
666
667 static struct ref *get_refs_list_using_list(struct transport *transport,
668 int for_push);
669
670 static int fetch(struct transport *transport,
671 int nr_heads, struct ref **to_fetch)
672 {
673 struct helper_data *data = transport->data;
674 int i, count;
675
676 get_helper(transport);
677
678 if (process_connect(transport, 0)) {
679 do_take_over(transport);
680 return transport->vtable->fetch(transport, nr_heads, to_fetch);
681 }
682
683 if (!data->get_refs_list_called)
684 get_refs_list_using_list(transport, 0);
685
686 count = 0;
687 for (i = 0; i < nr_heads; i++)
688 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
689 count++;
690
691 if (!count)
692 return 0;
693
694 if (data->check_connectivity &&
695 data->transport_options.check_self_contained_and_connected)
696 set_helper_option(transport, "check-connectivity", "true");
697
698 if (transport->cloning)
699 set_helper_option(transport, "cloning", "true");
700
701 if (data->transport_options.update_shallow)
702 set_helper_option(transport, "update-shallow", "true");
703
704 if (data->transport_options.filter_options.choice) {
705 const char *spec = expand_list_objects_filter_spec(
706 &data->transport_options.filter_options);
707 set_helper_option(transport, "filter", spec);
708 }
709
710 if (data->transport_options.negotiation_tips)
711 warning("Ignoring --negotiation-tip because the protocol does not support it.");
712
713 if (data->fetch)
714 return fetch_with_fetch(transport, nr_heads, to_fetch);
715
716 if (data->import)
717 return fetch_with_import(transport, nr_heads, to_fetch);
718
719 return -1;
720 }
721
722 struct push_update_ref_state {
723 struct ref *hint;
724 struct ref_push_report *report;
725 int new_report;
726 };
727
728 static int push_update_ref_status(struct strbuf *buf,
729 struct push_update_ref_state *state,
730 struct ref *remote_refs)
731 {
732 char *refname, *msg;
733 int status, forced = 0;
734
735 if (starts_with(buf->buf, "option ")) {
736 struct object_id old_oid, new_oid;
737 const char *key, *val;
738 char *p;
739
740 if (!state->hint || !(state->report || state->new_report))
741 die(_("'option' without a matching 'ok/error' directive"));
742 if (state->new_report) {
743 if (!state->hint->report) {
744 state->hint->report = xcalloc(1, sizeof(struct ref_push_report));
745 state->report = state->hint->report;
746 } else {
747 state->report = state->hint->report;
748 while (state->report->next)
749 state->report = state->report->next;
750 state->report->next = xcalloc(1, sizeof(struct ref_push_report));
751 state->report = state->report->next;
752 }
753 state->new_report = 0;
754 }
755 key = buf->buf + 7;
756 p = strchr(key, ' ');
757 if (p)
758 *p++ = '\0';
759 val = p;
760 if (!strcmp(key, "refname"))
761 state->report->ref_name = xstrdup_or_null(val);
762 else if (!strcmp(key, "old-oid") && val &&
763 !parse_oid_hex(val, &old_oid, &val))
764 state->report->old_oid = oiddup(&old_oid);
765 else if (!strcmp(key, "new-oid") && val &&
766 !parse_oid_hex(val, &new_oid, &val))
767 state->report->new_oid = oiddup(&new_oid);
768 else if (!strcmp(key, "forced-update"))
769 state->report->forced_update = 1;
770 /* Not update remote namespace again. */
771 return 1;
772 }
773
774 state->report = NULL;
775 state->new_report = 0;
776
777 if (starts_with(buf->buf, "ok ")) {
778 status = REF_STATUS_OK;
779 refname = buf->buf + 3;
780 } else if (starts_with(buf->buf, "error ")) {
781 status = REF_STATUS_REMOTE_REJECT;
782 refname = buf->buf + 6;
783 } else
784 die(_("expected ok/error, helper said '%s'"), buf->buf);
785
786 msg = strchr(refname, ' ');
787 if (msg) {
788 struct strbuf msg_buf = STRBUF_INIT;
789 const char *end;
790
791 *msg++ = '\0';
792 if (!unquote_c_style(&msg_buf, msg, &end))
793 msg = strbuf_detach(&msg_buf, NULL);
794 else
795 msg = xstrdup(msg);
796 strbuf_release(&msg_buf);
797
798 if (!strcmp(msg, "no match")) {
799 status = REF_STATUS_NONE;
800 FREE_AND_NULL(msg);
801 }
802 else if (!strcmp(msg, "up to date")) {
803 status = REF_STATUS_UPTODATE;
804 FREE_AND_NULL(msg);
805 }
806 else if (!strcmp(msg, "non-fast forward")) {
807 status = REF_STATUS_REJECT_NONFASTFORWARD;
808 FREE_AND_NULL(msg);
809 }
810 else if (!strcmp(msg, "already exists")) {
811 status = REF_STATUS_REJECT_ALREADY_EXISTS;
812 FREE_AND_NULL(msg);
813 }
814 else if (!strcmp(msg, "fetch first")) {
815 status = REF_STATUS_REJECT_FETCH_FIRST;
816 FREE_AND_NULL(msg);
817 }
818 else if (!strcmp(msg, "needs force")) {
819 status = REF_STATUS_REJECT_NEEDS_FORCE;
820 FREE_AND_NULL(msg);
821 }
822 else if (!strcmp(msg, "stale info")) {
823 status = REF_STATUS_REJECT_STALE;
824 FREE_AND_NULL(msg);
825 }
826 else if (!strcmp(msg, "forced update")) {
827 forced = 1;
828 FREE_AND_NULL(msg);
829 }
830 }
831
832 if (state->hint)
833 state->hint = find_ref_by_name(state->hint, refname);
834 if (!state->hint)
835 state->hint = find_ref_by_name(remote_refs, refname);
836 if (!state->hint) {
837 warning(_("helper reported unexpected status of %s"), refname);
838 return 1;
839 }
840
841 if (state->hint->status != REF_STATUS_NONE) {
842 /*
843 * Earlier, the ref was marked not to be pushed, so ignore the ref
844 * status reported by the remote helper if the latter is 'no match'.
845 */
846 if (status == REF_STATUS_NONE)
847 return 1;
848 }
849
850 if (status == REF_STATUS_OK)
851 state->new_report = 1;
852 state->hint->status = status;
853 state->hint->forced_update |= forced;
854 state->hint->remote_status = msg;
855 return !(status == REF_STATUS_OK);
856 }
857
858 static int push_update_refs_status(struct helper_data *data,
859 struct ref *remote_refs,
860 int flags)
861 {
862 struct ref *ref;
863 struct ref_push_report *report;
864 struct strbuf buf = STRBUF_INIT;
865 struct push_update_ref_state state = { remote_refs, NULL, 0 };
866
867 for (;;) {
868 if (recvline(data, &buf)) {
869 strbuf_release(&buf);
870 return 1;
871 }
872 if (!buf.len)
873 break;
874 push_update_ref_status(&buf, &state, remote_refs);
875 }
876 strbuf_release(&buf);
877
878 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
879 return 0;
880
881 /* propagate back the update to the remote namespace */
882 for (ref = remote_refs; ref; ref = ref->next) {
883 char *private;
884
885 if (ref->status != REF_STATUS_OK)
886 continue;
887
888 if (!ref->report) {
889 private = apply_refspecs(&data->rs, ref->name);
890 if (!private)
891 continue;
892 update_ref("update by helper", private, &(ref->new_oid),
893 NULL, 0, 0);
894 free(private);
895 } else {
896 for (report = ref->report; report; report = report->next) {
897 private = apply_refspecs(&data->rs,
898 report->ref_name
899 ? report->ref_name
900 : ref->name);
901 if (!private)
902 continue;
903 update_ref("update by helper", private,
904 report->new_oid
905 ? report->new_oid
906 : &(ref->new_oid),
907 NULL, 0, 0);
908 free(private);
909 }
910 }
911 }
912 return 0;
913 }
914
915 static void set_common_push_options(struct transport *transport,
916 const char *name, int flags)
917 {
918 if (flags & TRANSPORT_PUSH_DRY_RUN) {
919 if (set_helper_option(transport, "dry-run", "true") != 0)
920 die(_("helper %s does not support dry-run"), name);
921 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
922 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
923 die(_("helper %s does not support --signed"), name);
924 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
925 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
926 die(_("helper %s does not support --signed=if-asked"), name);
927 }
928
929 if (flags & TRANSPORT_PUSH_ATOMIC)
930 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
931 die(_("helper %s does not support --atomic"), name);
932
933 if (flags & TRANSPORT_PUSH_OPTIONS) {
934 struct string_list_item *item;
935 for_each_string_list_item(item, transport->push_options)
936 if (set_helper_option(transport, "push-option", item->string) != 0)
937 die(_("helper %s does not support 'push-option'"), name);
938 }
939 }
940
941 static int push_refs_with_push(struct transport *transport,
942 struct ref *remote_refs, int flags)
943 {
944 int force_all = flags & TRANSPORT_PUSH_FORCE;
945 int mirror = flags & TRANSPORT_PUSH_MIRROR;
946 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
947 struct helper_data *data = transport->data;
948 struct strbuf buf = STRBUF_INIT;
949 struct ref *ref;
950 struct string_list cas_options = STRING_LIST_INIT_DUP;
951 struct string_list_item *cas_option;
952
953 get_helper(transport);
954 if (!data->push)
955 return 1;
956
957 for (ref = remote_refs; ref; ref = ref->next) {
958 if (!ref->peer_ref && !mirror)
959 continue;
960
961 /* Check for statuses set by set_ref_status_for_push() */
962 switch (ref->status) {
963 case REF_STATUS_REJECT_NONFASTFORWARD:
964 case REF_STATUS_REJECT_STALE:
965 case REF_STATUS_REJECT_ALREADY_EXISTS:
966 if (atomic) {
967 reject_atomic_push(remote_refs, mirror);
968 string_list_clear(&cas_options, 0);
969 return 0;
970 } else
971 continue;
972 case REF_STATUS_UPTODATE:
973 continue;
974 default:
975 ; /* do nothing */
976 }
977
978 if (force_all)
979 ref->force = 1;
980
981 strbuf_addstr(&buf, "push ");
982 if (!ref->deletion) {
983 if (ref->force)
984 strbuf_addch(&buf, '+');
985 if (ref->peer_ref)
986 strbuf_addstr(&buf, ref->peer_ref->name);
987 else
988 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
989 }
990 strbuf_addch(&buf, ':');
991 strbuf_addstr(&buf, ref->name);
992 strbuf_addch(&buf, '\n');
993
994 /*
995 * The "--force-with-lease" options without explicit
996 * values to expect have already been expanded into
997 * the ref->old_oid_expect[] field; we can ignore
998 * transport->smart_options->cas altogether and instead
999 * can enumerate them from the refs.
1000 */
1001 if (ref->expect_old_sha1) {
1002 struct strbuf cas = STRBUF_INIT;
1003 strbuf_addf(&cas, "%s:%s",
1004 ref->name, oid_to_hex(&ref->old_oid_expect));
1005 string_list_append_nodup(&cas_options,
1006 strbuf_detach(&cas, NULL));
1007 }
1008 }
1009 if (buf.len == 0) {
1010 string_list_clear(&cas_options, 0);
1011 return 0;
1012 }
1013
1014 for_each_string_list_item(cas_option, &cas_options)
1015 set_helper_option(transport, "cas", cas_option->string);
1016 set_common_push_options(transport, data->name, flags);
1017
1018 strbuf_addch(&buf, '\n');
1019 sendline(data, &buf);
1020 strbuf_release(&buf);
1021 string_list_clear(&cas_options, 0);
1022
1023 return push_update_refs_status(data, remote_refs, flags);
1024 }
1025
1026 static int push_refs_with_export(struct transport *transport,
1027 struct ref *remote_refs, int flags)
1028 {
1029 struct ref *ref;
1030 struct child_process *helper, exporter;
1031 struct helper_data *data = transport->data;
1032 struct string_list revlist_args = STRING_LIST_INIT_DUP;
1033 struct strbuf buf = STRBUF_INIT;
1034
1035 if (!data->rs.nr)
1036 die(_("remote-helper doesn't support push; refspec needed"));
1037
1038 set_common_push_options(transport, data->name, flags);
1039 if (flags & TRANSPORT_PUSH_FORCE) {
1040 if (set_helper_option(transport, "force", "true") != 0)
1041 warning(_("helper %s does not support 'force'"), data->name);
1042 }
1043
1044 helper = get_helper(transport);
1045
1046 write_constant(helper->in, "export\n");
1047
1048 for (ref = remote_refs; ref; ref = ref->next) {
1049 char *private;
1050 struct object_id oid;
1051
1052 private = apply_refspecs(&data->rs, ref->name);
1053 if (private && !get_oid(private, &oid)) {
1054 strbuf_addf(&buf, "^%s", private);
1055 string_list_append_nodup(&revlist_args,
1056 strbuf_detach(&buf, NULL));
1057 oidcpy(&ref->old_oid, &oid);
1058 }
1059 free(private);
1060
1061 if (ref->peer_ref) {
1062 if (strcmp(ref->name, ref->peer_ref->name)) {
1063 if (!ref->deletion) {
1064 const char *name;
1065 int flag;
1066
1067 /* Follow symbolic refs (mainly for HEAD). */
1068 name = resolve_ref_unsafe(ref->peer_ref->name,
1069 RESOLVE_REF_READING,
1070 &oid, &flag);
1071 if (!name || !(flag & REF_ISSYMREF))
1072 name = ref->peer_ref->name;
1073
1074 strbuf_addf(&buf, "%s:%s", name, ref->name);
1075 } else
1076 strbuf_addf(&buf, ":%s", ref->name);
1077
1078 string_list_append(&revlist_args, "--refspec");
1079 string_list_append(&revlist_args, buf.buf);
1080 strbuf_release(&buf);
1081 }
1082 if (!ref->deletion)
1083 string_list_append(&revlist_args, ref->peer_ref->name);
1084 }
1085 }
1086
1087 if (get_exporter(transport, &exporter, &revlist_args))
1088 die(_("couldn't run fast-export"));
1089
1090 string_list_clear(&revlist_args, 1);
1091
1092 if (finish_command(&exporter))
1093 die(_("error while running fast-export"));
1094 if (push_update_refs_status(data, remote_refs, flags))
1095 return 1;
1096
1097 if (data->export_marks) {
1098 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1099 rename(buf.buf, data->export_marks);
1100 strbuf_release(&buf);
1101 }
1102
1103 return 0;
1104 }
1105
1106 static int push_refs(struct transport *transport,
1107 struct ref *remote_refs, int flags)
1108 {
1109 struct helper_data *data = transport->data;
1110
1111 if (process_connect(transport, 1)) {
1112 do_take_over(transport);
1113 return transport->vtable->push_refs(transport, remote_refs, flags);
1114 }
1115
1116 if (!remote_refs) {
1117 fprintf(stderr,
1118 _("No refs in common and none specified; doing nothing.\n"
1119 "Perhaps you should specify a branch such as 'master'.\n"));
1120 return 0;
1121 }
1122
1123 if (data->push)
1124 return push_refs_with_push(transport, remote_refs, flags);
1125
1126 if (data->export)
1127 return push_refs_with_export(transport, remote_refs, flags);
1128
1129 return -1;
1130 }
1131
1132
1133 static int has_attribute(const char *attrs, const char *attr)
1134 {
1135 int len;
1136 if (!attrs)
1137 return 0;
1138
1139 len = strlen(attr);
1140 for (;;) {
1141 const char *space = strchrnul(attrs, ' ');
1142 if (len == space - attrs && !strncmp(attrs, attr, len))
1143 return 1;
1144 if (!*space)
1145 return 0;
1146 attrs = space + 1;
1147 }
1148 }
1149
1150 static struct ref *get_refs_list(struct transport *transport, int for_push,
1151 const struct argv_array *ref_prefixes)
1152 {
1153 get_helper(transport);
1154
1155 if (process_connect(transport, for_push)) {
1156 do_take_over(transport);
1157 return transport->vtable->get_refs_list(transport, for_push, ref_prefixes);
1158 }
1159
1160 return get_refs_list_using_list(transport, for_push);
1161 }
1162
1163 static struct ref *get_refs_list_using_list(struct transport *transport,
1164 int for_push)
1165 {
1166 struct helper_data *data = transport->data;
1167 struct child_process *helper;
1168 struct ref *ret = NULL;
1169 struct ref **tail = &ret;
1170 struct ref *posn;
1171 struct strbuf buf = STRBUF_INIT;
1172
1173 data->get_refs_list_called = 1;
1174 helper = get_helper(transport);
1175
1176 if (data->push && for_push)
1177 write_str_in_full(helper->in, "list for-push\n");
1178 else
1179 write_str_in_full(helper->in, "list\n");
1180
1181 while (1) {
1182 char *eov, *eon;
1183 if (recvline(data, &buf))
1184 exit(128);
1185
1186 if (!*buf.buf)
1187 break;
1188
1189 eov = strchr(buf.buf, ' ');
1190 if (!eov)
1191 die(_("malformed response in ref list: %s"), buf.buf);
1192 eon = strchr(eov + 1, ' ');
1193 *eov = '\0';
1194 if (eon)
1195 *eon = '\0';
1196 *tail = alloc_ref(eov + 1);
1197 if (buf.buf[0] == '@')
1198 (*tail)->symref = xstrdup(buf.buf + 1);
1199 else if (buf.buf[0] != '?')
1200 get_oid_hex(buf.buf, &(*tail)->old_oid);
1201 if (eon) {
1202 if (has_attribute(eon + 1, "unchanged")) {
1203 (*tail)->status |= REF_STATUS_UPTODATE;
1204 if (read_ref((*tail)->name, &(*tail)->old_oid) < 0)
1205 die(_("could not read ref %s"),
1206 (*tail)->name);
1207 }
1208 }
1209 tail = &((*tail)->next);
1210 }
1211 if (debug)
1212 fprintf(stderr, "Debug: Read ref listing.\n");
1213 strbuf_release(&buf);
1214
1215 for (posn = ret; posn; posn = posn->next)
1216 resolve_remote_symref(posn, ret);
1217
1218 return ret;
1219 }
1220
1221 static struct transport_vtable vtable = {
1222 set_helper_option,
1223 get_refs_list,
1224 fetch,
1225 push_refs,
1226 connect_helper,
1227 release_helper
1228 };
1229
1230 int transport_helper_init(struct transport *transport, const char *name)
1231 {
1232 struct helper_data *data = xcalloc(1, sizeof(*data));
1233 data->name = name;
1234
1235 transport_check_allowed(name);
1236
1237 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1238 debug = 1;
1239
1240 transport->data = data;
1241 transport->vtable = &vtable;
1242 transport->smart_options = &(data->transport_options);
1243 return 0;
1244 }
1245
1246 /*
1247 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1248 * buffer less), so attempt reads and writes with up to that size.
1249 */
1250 #define BUFFERSIZE 65536
1251 /* This should be enough to hold debugging message. */
1252 #define PBUFFERSIZE 8192
1253
1254 /* Print bidirectional transfer loop debug message. */
1255 __attribute__((format (printf, 1, 2)))
1256 static void transfer_debug(const char *fmt, ...)
1257 {
1258 /*
1259 * NEEDSWORK: This function is sometimes used from multiple threads, and
1260 * we end up using debug_enabled racily. That "should not matter" since
1261 * we always write the same value, but it's still wrong. This function
1262 * is listed in .tsan-suppressions for the time being.
1263 */
1264
1265 va_list args;
1266 char msgbuf[PBUFFERSIZE];
1267 static int debug_enabled = -1;
1268
1269 if (debug_enabled < 0)
1270 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1271 if (!debug_enabled)
1272 return;
1273
1274 va_start(args, fmt);
1275 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1276 va_end(args);
1277 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1278 }
1279
1280 /* Stream state: More data may be coming in this direction. */
1281 #define SSTATE_TRANSFERRING 0
1282 /*
1283 * Stream state: No more data coming in this direction, flushing rest of
1284 * data.
1285 */
1286 #define SSTATE_FLUSHING 1
1287 /* Stream state: Transfer in this direction finished. */
1288 #define SSTATE_FINISHED 2
1289
1290 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1291 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1292 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1293
1294 /* Unidirectional transfer. */
1295 struct unidirectional_transfer {
1296 /* Source */
1297 int src;
1298 /* Destination */
1299 int dest;
1300 /* Is source socket? */
1301 int src_is_sock;
1302 /* Is destination socket? */
1303 int dest_is_sock;
1304 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1305 int state;
1306 /* Buffer. */
1307 char buf[BUFFERSIZE];
1308 /* Buffer used. */
1309 size_t bufuse;
1310 /* Name of source. */
1311 const char *src_name;
1312 /* Name of destination. */
1313 const char *dest_name;
1314 };
1315
1316 /* Closes the target (for writing) if transfer has finished. */
1317 static void udt_close_if_finished(struct unidirectional_transfer *t)
1318 {
1319 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1320 t->state = SSTATE_FINISHED;
1321 if (t->dest_is_sock)
1322 shutdown(t->dest, SHUT_WR);
1323 else
1324 close(t->dest);
1325 transfer_debug("Closed %s.", t->dest_name);
1326 }
1327 }
1328
1329 /*
1330 * Tries to read data from source into buffer. If buffer is full,
1331 * no data is read. Returns 0 on success, -1 on error.
1332 */
1333 static int udt_do_read(struct unidirectional_transfer *t)
1334 {
1335 ssize_t bytes;
1336
1337 if (t->bufuse == BUFFERSIZE)
1338 return 0; /* No space for more. */
1339
1340 transfer_debug("%s is readable", t->src_name);
1341 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1342 if (bytes < 0) {
1343 error_errno(_("read(%s) failed"), t->src_name);
1344 return -1;
1345 } else if (bytes == 0) {
1346 transfer_debug("%s EOF (with %i bytes in buffer)",
1347 t->src_name, (int)t->bufuse);
1348 t->state = SSTATE_FLUSHING;
1349 } else if (bytes > 0) {
1350 t->bufuse += bytes;
1351 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1352 (int)bytes, t->src_name, (int)t->bufuse);
1353 }
1354 return 0;
1355 }
1356
1357 /* Tries to write data from buffer into destination. If buffer is empty,
1358 * no data is written. Returns 0 on success, -1 on error.
1359 */
1360 static int udt_do_write(struct unidirectional_transfer *t)
1361 {
1362 ssize_t bytes;
1363
1364 if (t->bufuse == 0)
1365 return 0; /* Nothing to write. */
1366
1367 transfer_debug("%s is writable", t->dest_name);
1368 bytes = xwrite(t->dest, t->buf, t->bufuse);
1369 if (bytes < 0) {
1370 error_errno(_("write(%s) failed"), t->dest_name);
1371 return -1;
1372 } else if (bytes > 0) {
1373 t->bufuse -= bytes;
1374 if (t->bufuse)
1375 memmove(t->buf, t->buf + bytes, t->bufuse);
1376 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1377 (int)bytes, t->dest_name, (int)t->bufuse);
1378 }
1379 return 0;
1380 }
1381
1382
1383 /* State of bidirectional transfer loop. */
1384 struct bidirectional_transfer_state {
1385 /* Direction from program to git. */
1386 struct unidirectional_transfer ptg;
1387 /* Direction from git to program. */
1388 struct unidirectional_transfer gtp;
1389 };
1390
1391 static void *udt_copy_task_routine(void *udt)
1392 {
1393 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1394 while (t->state != SSTATE_FINISHED) {
1395 if (STATE_NEEDS_READING(t->state))
1396 if (udt_do_read(t))
1397 return NULL;
1398 if (STATE_NEEDS_WRITING(t->state))
1399 if (udt_do_write(t))
1400 return NULL;
1401 if (STATE_NEEDS_CLOSING(t->state))
1402 udt_close_if_finished(t);
1403 }
1404 return udt; /* Just some non-NULL value. */
1405 }
1406
1407 #ifndef NO_PTHREADS
1408
1409 /*
1410 * Join thread, with appropriate errors on failure. Name is name for the
1411 * thread (for error messages). Returns 0 on success, 1 on failure.
1412 */
1413 static int tloop_join(pthread_t thread, const char *name)
1414 {
1415 int err;
1416 void *tret;
1417 err = pthread_join(thread, &tret);
1418 if (!tret) {
1419 error(_("%s thread failed"), name);
1420 return 1;
1421 }
1422 if (err) {
1423 error(_("%s thread failed to join: %s"), name, strerror(err));
1424 return 1;
1425 }
1426 return 0;
1427 }
1428
1429 /*
1430 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1431 * -1 on failure.
1432 */
1433 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1434 {
1435 pthread_t gtp_thread;
1436 pthread_t ptg_thread;
1437 int err;
1438 int ret = 0;
1439 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1440 &s->gtp);
1441 if (err)
1442 die(_("can't start thread for copying data: %s"), strerror(err));
1443 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1444 &s->ptg);
1445 if (err)
1446 die(_("can't start thread for copying data: %s"), strerror(err));
1447
1448 ret |= tloop_join(gtp_thread, "Git to program copy");
1449 ret |= tloop_join(ptg_thread, "Program to git copy");
1450 return ret;
1451 }
1452 #else
1453
1454 /* Close the source and target (for writing) for transfer. */
1455 static void udt_kill_transfer(struct unidirectional_transfer *t)
1456 {
1457 t->state = SSTATE_FINISHED;
1458 /*
1459 * Socket read end left open isn't a disaster if nobody
1460 * attempts to read from it (mingw compat headers do not
1461 * have SHUT_RD)...
1462 *
1463 * We can't fully close the socket since otherwise gtp
1464 * task would first close the socket it sends data to
1465 * while closing the ptg file descriptors.
1466 */
1467 if (!t->src_is_sock)
1468 close(t->src);
1469 if (t->dest_is_sock)
1470 shutdown(t->dest, SHUT_WR);
1471 else
1472 close(t->dest);
1473 }
1474
1475 /*
1476 * Join process, with appropriate errors on failure. Name is name for the
1477 * process (for error messages). Returns 0 on success, 1 on failure.
1478 */
1479 static int tloop_join(pid_t pid, const char *name)
1480 {
1481 int tret;
1482 if (waitpid(pid, &tret, 0) < 0) {
1483 error_errno(_("%s process failed to wait"), name);
1484 return 1;
1485 }
1486 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1487 error(_("%s process failed"), name);
1488 return 1;
1489 }
1490 return 0;
1491 }
1492
1493 /*
1494 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1495 * -1 on failure.
1496 */
1497 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1498 {
1499 pid_t pid1, pid2;
1500 int ret = 0;
1501
1502 /* Fork thread #1: git to program. */
1503 pid1 = fork();
1504 if (pid1 < 0)
1505 die_errno(_("can't start thread for copying data"));
1506 else if (pid1 == 0) {
1507 udt_kill_transfer(&s->ptg);
1508 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1509 }
1510
1511 /* Fork thread #2: program to git. */
1512 pid2 = fork();
1513 if (pid2 < 0)
1514 die_errno(_("can't start thread for copying data"));
1515 else if (pid2 == 0) {
1516 udt_kill_transfer(&s->gtp);
1517 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1518 }
1519
1520 /*
1521 * Close both streams in parent as to not interfere with
1522 * end of file detection and wait for both tasks to finish.
1523 */
1524 udt_kill_transfer(&s->gtp);
1525 udt_kill_transfer(&s->ptg);
1526 ret |= tloop_join(pid1, "Git to program copy");
1527 ret |= tloop_join(pid2, "Program to git copy");
1528 return ret;
1529 }
1530 #endif
1531
1532 /*
1533 * Copies data from stdin to output and from input to stdout simultaneously.
1534 * Additionally filtering through given filter. If filter is NULL, uses
1535 * identity filter.
1536 */
1537 int bidirectional_transfer_loop(int input, int output)
1538 {
1539 struct bidirectional_transfer_state state;
1540
1541 /* Fill the state fields. */
1542 state.ptg.src = input;
1543 state.ptg.dest = 1;
1544 state.ptg.src_is_sock = (input == output);
1545 state.ptg.dest_is_sock = 0;
1546 state.ptg.state = SSTATE_TRANSFERRING;
1547 state.ptg.bufuse = 0;
1548 state.ptg.src_name = "remote input";
1549 state.ptg.dest_name = "stdout";
1550
1551 state.gtp.src = 0;
1552 state.gtp.dest = output;
1553 state.gtp.src_is_sock = 0;
1554 state.gtp.dest_is_sock = (input == output);
1555 state.gtp.state = SSTATE_TRANSFERRING;
1556 state.gtp.bufuse = 0;
1557 state.gtp.src_name = "stdin";
1558 state.gtp.dest_name = "remote output";
1559
1560 return tloop_spawnwait_tasks(&state);
1561 }
1562
1563 void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1564 {
1565 struct ref *ref;
1566
1567 /* Mark other refs as failed */
1568 for (ref = remote_refs; ref; ref = ref->next) {
1569 if (!ref->peer_ref && !mirror_mode)
1570 continue;
1571
1572 switch (ref->status) {
1573 case REF_STATUS_NONE:
1574 case REF_STATUS_OK:
1575 case REF_STATUS_EXPECTING_REPORT:
1576 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1577 continue;
1578 default:
1579 break; /* do nothing */
1580 }
1581 }
1582 return;
1583 }