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