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