Merge branch 'jk/fsmonitor-event-listener-race-fix' into maint-2.47
[git/gitster.git] / transport-helper.c
blob013ec79dc9cdc54094c37262381350a0bfa43731
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "transport.h"
5 #include "quote.h"
6 #include "run-command.h"
7 #include "commit.h"
8 #include "environment.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "object-name.h"
12 #include "repository.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 "packfile.h"
24 static int debug;
26 struct helper_data {
27 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;
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.
51 unsigned get_refs_list_called : 1;
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).
60 struct git_transport_options transport_options;
63 static void sendline(struct helper_data *helper, struct strbuf *buffer)
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"));
71 static int recvline_fh(FILE *helper, struct strbuf *buffer)
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;
82 if (debug)
83 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
84 return 0;
87 static int recvline(struct helper_data *helper, struct strbuf *buffer)
89 return recvline_fh(helper->out, buffer);
92 static int write_constant_gently(int fd, const char *str)
94 if (debug)
95 fprintf(stderr, "Debug: Remote helper: -> %s", str);
96 if (write_in_full(fd, str, strlen(str)) < 0)
97 return -1;
98 return 0;
101 static void write_constant(int fd, const char *str)
103 if (write_constant_gently(fd, str) < 0)
104 die_errno(_("full write to remote helper failed"));
107 static const char *remove_ext_force(const char *url)
109 if (url) {
110 const char *colon = strchr(url, ':');
111 if (colon && colon[1] == ':')
112 return colon + 2;
114 return url;
117 static void do_take_over(struct transport *transport)
119 struct helper_data *data;
120 data = (struct helper_data *)transport->data;
121 transport_take_over(transport, data->helper);
122 fclose(data->out);
123 free(data->name);
124 free(data);
127 static void standard_options(struct transport *t);
129 static struct child_process *get_helper(struct transport *transport)
131 struct helper_data *data = transport->data;
132 struct strbuf buf = STRBUF_INIT;
133 struct child_process *helper;
134 int duped;
135 int code;
137 if (data->helper)
138 return data->helper;
140 helper = xmalloc(sizeof(*helper));
141 child_process_init(helper);
142 helper->in = -1;
143 helper->out = -1;
144 helper->err = 0;
145 strvec_pushf(&helper->args, "remote-%s", data->name);
146 strvec_push(&helper->args, transport->remote->name);
147 strvec_push(&helper->args, remove_ext_force(transport->url));
148 helper->git_cmd = 1;
149 helper->silent_exec_failure = 1;
151 if (have_git_dir())
152 strvec_pushf(&helper->env, "%s=%s",
153 GIT_DIR_ENVIRONMENT, repo_get_git_dir(the_repository));
155 helper->trace2_child_class = helper->args.v[0]; /* "remote-<name>" */
157 code = start_command(helper);
158 if (code < 0 && errno == ENOENT)
159 die(_("unable to find remote helper for '%s'"), data->name);
160 else if (code != 0)
161 exit(code);
163 data->helper = helper;
164 data->no_disconnect_req = 0;
165 refspec_init(&data->rs, REFSPEC_FETCH);
168 * Open the output as FILE* so strbuf_getline_*() family of
169 * functions can be used.
170 * Do this with duped fd because fclose() will close the fd,
171 * and stuff like taking over will require the fd to remain.
173 duped = dup(helper->out);
174 if (duped < 0)
175 die_errno(_("can't dup helper output fd"));
176 data->out = xfdopen(duped, "r");
178 sigchain_push(SIGPIPE, SIG_IGN);
179 if (write_constant_gently(helper->in, "capabilities\n") < 0)
180 die("remote helper '%s' aborted session", data->name);
181 sigchain_pop(SIGPIPE);
183 while (1) {
184 const char *capname, *arg;
185 int mandatory = 0;
186 if (recvline(data, &buf))
187 die("remote helper '%s' aborted session", data->name);
189 if (!*buf.buf)
190 break;
192 if (*buf.buf == '*') {
193 capname = buf.buf + 1;
194 mandatory = 1;
195 } else
196 capname = buf.buf;
198 if (debug)
199 fprintf(stderr, "Debug: Got cap %s\n", capname);
200 if (!strcmp(capname, "fetch"))
201 data->fetch = 1;
202 else if (!strcmp(capname, "option"))
203 data->option = 1;
204 else if (!strcmp(capname, "push"))
205 data->push = 1;
206 else if (!strcmp(capname, "import"))
207 data->import = 1;
208 else if (!strcmp(capname, "bidi-import"))
209 data->bidi_import = 1;
210 else if (!strcmp(capname, "export"))
211 data->export = 1;
212 else if (!strcmp(capname, "check-connectivity"))
213 data->check_connectivity = 1;
214 else if (skip_prefix(capname, "refspec ", &arg)) {
215 refspec_append(&data->rs, arg);
216 } else if (!strcmp(capname, "connect")) {
217 data->connect = 1;
218 } else if (!strcmp(capname, "stateless-connect")) {
219 data->stateless_connect = 1;
220 } else if (!strcmp(capname, "signed-tags")) {
221 data->signed_tags = 1;
222 } else if (skip_prefix(capname, "export-marks ", &arg)) {
223 data->export_marks = xstrdup(arg);
224 } else if (skip_prefix(capname, "import-marks ", &arg)) {
225 data->import_marks = xstrdup(arg);
226 } else if (starts_with(capname, "no-private-update")) {
227 data->no_private_update = 1;
228 } else if (starts_with(capname, "object-format")) {
229 data->object_format = 1;
230 } else if (mandatory) {
231 die(_("unknown mandatory capability %s; this remote "
232 "helper probably needs newer version of Git"),
233 capname);
236 if (!data->rs.nr && (data->import || data->bidi_import || data->export)) {
237 warning(_("this remote helper should implement refspec capability"));
239 strbuf_release(&buf);
240 if (debug)
241 fprintf(stderr, "Debug: Capabilities complete.\n");
242 standard_options(transport);
243 return data->helper;
246 static int disconnect_helper(struct transport *transport)
248 struct helper_data *data = transport->data;
249 int res = 0;
251 if (data->helper) {
252 if (debug)
253 fprintf(stderr, "Debug: Disconnecting.\n");
254 if (!data->no_disconnect_req) {
256 * Ignore write errors; there's nothing we can do,
257 * since we're about to close the pipe anyway. And the
258 * most likely error is EPIPE due to the helper dying
259 * to report an error itself.
261 sigchain_push(SIGPIPE, SIG_IGN);
262 xwrite(data->helper->in, "\n", 1);
263 sigchain_pop(SIGPIPE);
265 close(data->helper->in);
266 close(data->helper->out);
267 fclose(data->out);
268 res = finish_command(data->helper);
269 FREE_AND_NULL(data->name);
270 FREE_AND_NULL(data->helper);
272 return res;
275 static const char *unsupported_options[] = {
276 TRANS_OPT_UPLOADPACK,
277 TRANS_OPT_RECEIVEPACK,
278 TRANS_OPT_THIN,
279 TRANS_OPT_KEEP
282 static const char *boolean_options[] = {
283 TRANS_OPT_THIN,
284 TRANS_OPT_KEEP,
285 TRANS_OPT_FOLLOWTAGS,
286 TRANS_OPT_DEEPEN_RELATIVE
289 static int strbuf_set_helper_option(struct helper_data *data,
290 struct strbuf *buf)
292 int ret;
294 sendline(data, buf);
295 if (recvline(data, buf))
296 exit(128);
298 if (!strcmp(buf->buf, "ok"))
299 ret = 0;
300 else if (starts_with(buf->buf, "error"))
301 ret = -1;
302 else if (!strcmp(buf->buf, "unsupported"))
303 ret = 1;
304 else {
305 warning(_("%s unexpectedly said: '%s'"), data->name, buf->buf);
306 ret = 1;
308 return ret;
311 static int string_list_set_helper_option(struct helper_data *data,
312 const char *name,
313 struct string_list *list)
315 struct strbuf buf = STRBUF_INIT;
316 int i, ret = 0;
318 for (i = 0; i < list->nr; i++) {
319 strbuf_addf(&buf, "option %s ", name);
320 quote_c_style(list->items[i].string, &buf, NULL, 0);
321 strbuf_addch(&buf, '\n');
323 if ((ret = strbuf_set_helper_option(data, &buf)))
324 break;
325 strbuf_reset(&buf);
327 strbuf_release(&buf);
328 return ret;
331 static int set_helper_option(struct transport *transport,
332 const char *name, const char *value)
334 struct helper_data *data = transport->data;
335 struct strbuf buf = STRBUF_INIT;
336 int i, ret, is_bool = 0;
338 get_helper(transport);
340 if (!data->option)
341 return 1;
343 if (!strcmp(name, "deepen-not"))
344 return string_list_set_helper_option(data, name,
345 (struct string_list *)value);
347 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
348 if (!strcmp(name, unsupported_options[i]))
349 return 1;
352 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
353 if (!strcmp(name, boolean_options[i])) {
354 is_bool = 1;
355 break;
359 strbuf_addf(&buf, "option %s ", name);
360 if (is_bool)
361 strbuf_addstr(&buf, value ? "true" : "false");
362 else
363 quote_c_style(value, &buf, NULL, 0);
364 strbuf_addch(&buf, '\n');
366 ret = strbuf_set_helper_option(data, &buf);
367 strbuf_release(&buf);
368 return ret;
371 static void standard_options(struct transport *t)
373 char buf[16];
374 int v = t->verbose;
376 set_helper_option(t, "progress", t->progress ? "true" : "false");
378 xsnprintf(buf, sizeof(buf), "%d", v + 1);
379 set_helper_option(t, "verbosity", buf);
381 switch (t->family) {
382 case TRANSPORT_FAMILY_ALL:
384 * this is already the default,
385 * do not break old remote helpers by setting "all" here
387 break;
388 case TRANSPORT_FAMILY_IPV4:
389 set_helper_option(t, "family", "ipv4");
390 break;
391 case TRANSPORT_FAMILY_IPV6:
392 set_helper_option(t, "family", "ipv6");
393 break;
397 static int release_helper(struct transport *transport)
399 int res = 0;
400 struct helper_data *data = transport->data;
401 refspec_clear(&data->rs);
402 res = disconnect_helper(transport);
403 free(transport->data);
404 return res;
407 static int fetch_with_fetch(struct transport *transport,
408 int nr_heads, struct ref **to_fetch)
410 struct helper_data *data = transport->data;
411 int i;
412 struct strbuf buf = STRBUF_INIT;
414 for (i = 0; i < nr_heads; i++) {
415 const struct ref *posn = to_fetch[i];
416 if (posn->status & REF_STATUS_UPTODATE)
417 continue;
419 strbuf_addf(&buf, "fetch %s %s\n",
420 oid_to_hex(&posn->old_oid),
421 posn->symref ? posn->symref : posn->name);
424 strbuf_addch(&buf, '\n');
425 sendline(data, &buf);
427 while (1) {
428 const char *name;
430 if (recvline(data, &buf))
431 exit(128);
433 if (skip_prefix(buf.buf, "lock ", &name)) {
434 if (transport->pack_lockfiles.nr)
435 warning(_("%s also locked %s"), data->name, name);
436 else
437 string_list_append(&transport->pack_lockfiles,
438 name);
440 else if (data->check_connectivity &&
441 data->transport_options.check_self_contained_and_connected &&
442 !strcmp(buf.buf, "connectivity-ok"))
443 data->transport_options.self_contained_and_connected = 1;
444 else if (!buf.len)
445 break;
446 else
447 warning(_("%s unexpectedly said: '%s'"), data->name, buf.buf);
449 strbuf_release(&buf);
451 reprepare_packed_git(the_repository);
452 return 0;
455 static int get_importer(struct transport *transport, struct child_process *fastimport)
457 struct child_process *helper = get_helper(transport);
458 struct helper_data *data = transport->data;
459 int cat_blob_fd, code;
460 child_process_init(fastimport);
461 fastimport->in = xdup(helper->out);
462 strvec_push(&fastimport->args, "fast-import");
463 strvec_push(&fastimport->args, "--allow-unsafe-features");
464 strvec_push(&fastimport->args, debug ? "--stats" : "--quiet");
466 if (data->bidi_import) {
467 cat_blob_fd = xdup(helper->in);
468 strvec_pushf(&fastimport->args, "--cat-blob-fd=%d", cat_blob_fd);
470 fastimport->git_cmd = 1;
472 code = start_command(fastimport);
473 return code;
476 static int get_exporter(struct transport *transport,
477 struct child_process *fastexport,
478 struct string_list *revlist_args)
480 struct helper_data *data = transport->data;
481 struct child_process *helper = get_helper(transport);
482 int i;
484 child_process_init(fastexport);
486 /* we need to duplicate helper->in because we want to use it after
487 * fastexport is done with it. */
488 fastexport->out = dup(helper->in);
489 strvec_push(&fastexport->args, "fast-export");
490 strvec_push(&fastexport->args, "--use-done-feature");
491 strvec_push(&fastexport->args, data->signed_tags ?
492 "--signed-tags=verbatim" : "--signed-tags=warn-strip");
493 if (data->export_marks)
494 strvec_pushf(&fastexport->args, "--export-marks=%s.tmp", data->export_marks);
495 if (data->import_marks)
496 strvec_pushf(&fastexport->args, "--import-marks=%s", data->import_marks);
498 for (i = 0; i < revlist_args->nr; i++)
499 strvec_push(&fastexport->args, revlist_args->items[i].string);
501 fastexport->git_cmd = 1;
502 return start_command(fastexport);
505 static int fetch_with_import(struct transport *transport,
506 int nr_heads, struct ref **to_fetch)
508 struct child_process fastimport;
509 struct helper_data *data = transport->data;
510 int i;
511 struct ref *posn;
512 struct strbuf buf = STRBUF_INIT;
514 get_helper(transport);
516 if (get_importer(transport, &fastimport))
517 die(_("couldn't run fast-import"));
519 for (i = 0; i < nr_heads; i++) {
520 posn = to_fetch[i];
521 if (posn->status & REF_STATUS_UPTODATE)
522 continue;
524 strbuf_addf(&buf, "import %s\n",
525 posn->symref ? posn->symref : posn->name);
526 sendline(data, &buf);
527 strbuf_reset(&buf);
530 write_constant(data->helper->in, "\n");
532 * remote-helpers that advertise the bidi-import capability are required to
533 * buffer the complete batch of import commands until this newline before
534 * sending data to fast-import.
535 * These helpers read back data from fast-import on their stdin, which could
536 * be mixed with import commands, otherwise.
539 if (finish_command(&fastimport))
540 die(_("error while running fast-import"));
543 * The fast-import stream of a remote helper that advertises
544 * the "refspec" capability writes to the refs named after the
545 * right hand side of the first refspec matching each ref we
546 * were fetching.
548 * (If no "refspec" capability was specified, for historical
549 * reasons we default to the equivalent of *:*.)
551 * Store the result in to_fetch[i].old_sha1. Callers such
552 * as "git fetch" can use the value to write feedback to the
553 * terminal, populate FETCH_HEAD, and determine what new value
554 * should be written to peer_ref if the update is a
555 * fast-forward or this is a forced update.
557 for (i = 0; i < nr_heads; i++) {
558 char *private, *name;
559 posn = to_fetch[i];
560 if (posn->status & REF_STATUS_UPTODATE)
561 continue;
562 name = posn->symref ? posn->symref : posn->name;
563 if (data->rs.nr)
564 private = apply_refspecs(&data->rs, name);
565 else
566 private = xstrdup(name);
567 if (private) {
568 if (refs_read_ref(get_main_ref_store(the_repository), private, &posn->old_oid) < 0)
569 die(_("could not read ref %s"), private);
570 free(private);
573 strbuf_release(&buf);
574 return 0;
577 static int run_connect(struct transport *transport, struct strbuf *cmdbuf)
579 struct helper_data *data = transport->data;
580 int ret = 0;
581 int duped;
582 FILE *input;
583 struct child_process *helper;
585 helper = get_helper(transport);
588 * Yes, dup the pipe another time, as we need unbuffered version
589 * of input pipe as FILE*. fclose() closes the underlying fd and
590 * stream buffering only can be changed before first I/O operation
591 * on it.
593 duped = dup(helper->out);
594 if (duped < 0)
595 die_errno(_("can't dup helper output fd"));
596 input = xfdopen(duped, "r");
597 setvbuf(input, NULL, _IONBF, 0);
599 sendline(data, cmdbuf);
600 if (recvline_fh(input, cmdbuf))
601 exit(128);
603 if (!strcmp(cmdbuf->buf, "")) {
604 data->no_disconnect_req = 1;
605 if (debug)
606 fprintf(stderr, "Debug: Smart transport connection "
607 "ready.\n");
608 ret = 1;
609 } else if (!strcmp(cmdbuf->buf, "fallback")) {
610 if (debug)
611 fprintf(stderr, "Debug: Falling back to dumb "
612 "transport.\n");
613 } else {
614 die(_("unknown response to connect: %s"),
615 cmdbuf->buf);
618 fclose(input);
619 return ret;
622 static int process_connect_service(struct transport *transport,
623 const char *name, const char *exec)
625 struct helper_data *data = transport->data;
626 struct strbuf cmdbuf = STRBUF_INIT;
627 int ret = 0;
630 * Handle --upload-pack and friends. This is fire and forget...
631 * just warn if it fails.
633 if (strcmp(name, exec)) {
634 int r = set_helper_option(transport, "servpath", exec);
635 if (r > 0)
636 warning(_("setting remote service path not supported by protocol"));
637 else if (r < 0)
638 warning(_("invalid remote service path"));
641 if (data->connect) {
642 strbuf_addf(&cmdbuf, "connect %s\n", name);
643 ret = run_connect(transport, &cmdbuf);
644 } else if (data->stateless_connect &&
645 (get_protocol_version_config() == protocol_v2) &&
646 (!strcmp("git-upload-pack", name) ||
647 !strcmp("git-upload-archive", name))) {
648 strbuf_addf(&cmdbuf, "stateless-connect %s\n", name);
649 ret = run_connect(transport, &cmdbuf);
650 if (ret)
651 transport->stateless_rpc = 1;
654 strbuf_release(&cmdbuf);
655 return ret;
658 static int process_connect(struct transport *transport,
659 int for_push)
661 struct helper_data *data = transport->data;
662 const char *name;
663 const char *exec;
664 int ret;
666 name = for_push ? "git-receive-pack" : "git-upload-pack";
667 if (for_push)
668 exec = data->transport_options.receivepack;
669 else
670 exec = data->transport_options.uploadpack;
672 ret = process_connect_service(transport, name, exec);
673 if (ret)
674 do_take_over(transport);
675 return ret;
678 static int connect_helper(struct transport *transport, const char *name,
679 const char *exec, int fd[2])
681 struct helper_data *data = transport->data;
683 /* Get_helper so connect is inited. */
684 get_helper(transport);
686 if (!process_connect_service(transport, name, exec))
687 die(_("can't connect to subservice %s"), name);
689 fd[0] = data->helper->out;
690 fd[1] = data->helper->in;
692 do_take_over(transport);
693 return 0;
696 static struct ref *get_refs_list_using_list(struct transport *transport,
697 int for_push);
699 static int fetch_refs(struct transport *transport,
700 int nr_heads, struct ref **to_fetch)
702 struct helper_data *data = transport->data;
703 int i, count;
705 get_helper(transport);
707 if (process_connect(transport, 0))
708 return transport->vtable->fetch_refs(transport, nr_heads, to_fetch);
711 * If we reach here, then the server, the client, and/or the transport
712 * helper does not support protocol v2. --negotiate-only requires
713 * protocol v2.
715 if (data->transport_options.acked_commits) {
716 warning(_("--negotiate-only requires protocol v2"));
717 return -1;
720 if (!data->get_refs_list_called) {
722 * We do not care about the list of refs returned, but only
723 * that the "list" command was sent.
725 struct ref *dummy = get_refs_list_using_list(transport, 0);
726 free_refs(dummy);
729 count = 0;
730 for (i = 0; i < nr_heads; i++)
731 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
732 count++;
734 if (!count)
735 return 0;
737 if (data->check_connectivity &&
738 data->transport_options.check_self_contained_and_connected)
739 set_helper_option(transport, "check-connectivity", "true");
741 if (transport->cloning)
742 set_helper_option(transport, "cloning", "true");
744 if (data->transport_options.update_shallow)
745 set_helper_option(transport, "update-shallow", "true");
747 if (data->transport_options.refetch)
748 set_helper_option(transport, "refetch", "true");
750 if (data->transport_options.filter_options.choice) {
751 const char *spec = expand_list_objects_filter_spec(
752 &data->transport_options.filter_options);
753 set_helper_option(transport, "filter", spec);
756 if (data->transport_options.negotiation_tips)
757 warning("Ignoring --negotiation-tip because the protocol does not support it.");
759 if (data->fetch)
760 return fetch_with_fetch(transport, nr_heads, to_fetch);
762 if (data->import)
763 return fetch_with_import(transport, nr_heads, to_fetch);
765 return -1;
768 struct push_update_ref_state {
769 struct ref *hint;
770 struct ref_push_report *report;
771 int new_report;
774 static int push_update_ref_status(struct strbuf *buf,
775 struct push_update_ref_state *state,
776 struct ref *remote_refs)
778 char *refname, *msg;
779 int status, forced = 0;
781 if (starts_with(buf->buf, "option ")) {
782 struct object_id old_oid, new_oid;
783 const char *key, *val;
784 char *p;
786 if (!state->hint || !(state->report || state->new_report))
787 die(_("'option' without a matching 'ok/error' directive"));
788 if (state->new_report) {
789 if (!state->hint->report) {
790 CALLOC_ARRAY(state->hint->report, 1);
791 state->report = state->hint->report;
792 } else {
793 state->report = state->hint->report;
794 while (state->report->next)
795 state->report = state->report->next;
796 CALLOC_ARRAY(state->report->next, 1);
797 state->report = state->report->next;
799 state->new_report = 0;
801 key = buf->buf + 7;
802 p = strchr(key, ' ');
803 if (p)
804 *p++ = '\0';
805 val = p;
806 if (!strcmp(key, "refname"))
807 state->report->ref_name = xstrdup_or_null(val);
808 else if (!strcmp(key, "old-oid") && val &&
809 !parse_oid_hex(val, &old_oid, &val))
810 state->report->old_oid = oiddup(&old_oid);
811 else if (!strcmp(key, "new-oid") && val &&
812 !parse_oid_hex(val, &new_oid, &val))
813 state->report->new_oid = oiddup(&new_oid);
814 else if (!strcmp(key, "forced-update"))
815 state->report->forced_update = 1;
816 /* Not update remote namespace again. */
817 return 1;
820 state->report = NULL;
821 state->new_report = 0;
823 if (starts_with(buf->buf, "ok ")) {
824 status = REF_STATUS_OK;
825 refname = buf->buf + 3;
826 } else if (starts_with(buf->buf, "error ")) {
827 status = REF_STATUS_REMOTE_REJECT;
828 refname = buf->buf + 6;
829 } else
830 die(_("expected ok/error, helper said '%s'"), buf->buf);
832 msg = strchr(refname, ' ');
833 if (msg) {
834 struct strbuf msg_buf = STRBUF_INIT;
835 const char *end;
837 *msg++ = '\0';
838 if (!unquote_c_style(&msg_buf, msg, &end))
839 msg = strbuf_detach(&msg_buf, NULL);
840 else
841 msg = xstrdup(msg);
842 strbuf_release(&msg_buf);
844 if (!strcmp(msg, "no match")) {
845 status = REF_STATUS_NONE;
846 FREE_AND_NULL(msg);
848 else if (!strcmp(msg, "up to date")) {
849 status = REF_STATUS_UPTODATE;
850 FREE_AND_NULL(msg);
852 else if (!strcmp(msg, "non-fast forward")) {
853 status = REF_STATUS_REJECT_NONFASTFORWARD;
854 FREE_AND_NULL(msg);
856 else if (!strcmp(msg, "already exists")) {
857 status = REF_STATUS_REJECT_ALREADY_EXISTS;
858 FREE_AND_NULL(msg);
860 else if (!strcmp(msg, "fetch first")) {
861 status = REF_STATUS_REJECT_FETCH_FIRST;
862 FREE_AND_NULL(msg);
864 else if (!strcmp(msg, "needs force")) {
865 status = REF_STATUS_REJECT_NEEDS_FORCE;
866 FREE_AND_NULL(msg);
868 else if (!strcmp(msg, "stale info")) {
869 status = REF_STATUS_REJECT_STALE;
870 FREE_AND_NULL(msg);
872 else if (!strcmp(msg, "remote ref updated since checkout")) {
873 status = REF_STATUS_REJECT_REMOTE_UPDATED;
874 FREE_AND_NULL(msg);
876 else if (!strcmp(msg, "forced update")) {
877 forced = 1;
878 FREE_AND_NULL(msg);
880 else if (!strcmp(msg, "expecting report")) {
881 status = REF_STATUS_EXPECTING_REPORT;
882 FREE_AND_NULL(msg);
886 if (state->hint)
887 state->hint = find_ref_by_name(state->hint, refname);
888 if (!state->hint)
889 state->hint = find_ref_by_name(remote_refs, refname);
890 if (!state->hint) {
891 warning(_("helper reported unexpected status of %s"), refname);
892 return 1;
895 if (state->hint->status != REF_STATUS_NONE) {
897 * Earlier, the ref was marked not to be pushed, so ignore the ref
898 * status reported by the remote helper if the latter is 'no match'.
900 if (status == REF_STATUS_NONE)
901 return 1;
904 if (status == REF_STATUS_OK)
905 state->new_report = 1;
906 state->hint->status = status;
907 state->hint->forced_update |= forced;
908 state->hint->remote_status = msg;
909 return !(status == REF_STATUS_OK);
912 static int push_update_refs_status(struct helper_data *data,
913 struct ref *remote_refs,
914 int flags)
916 struct ref *ref;
917 struct ref_push_report *report;
918 struct strbuf buf = STRBUF_INIT;
919 struct push_update_ref_state state = { remote_refs, NULL, 0 };
921 for (;;) {
922 if (recvline(data, &buf)) {
923 strbuf_release(&buf);
924 return 1;
926 if (!buf.len)
927 break;
928 push_update_ref_status(&buf, &state, remote_refs);
930 strbuf_release(&buf);
932 if (flags & TRANSPORT_PUSH_DRY_RUN || !data->rs.nr || data->no_private_update)
933 return 0;
935 /* propagate back the update to the remote namespace */
936 for (ref = remote_refs; ref; ref = ref->next) {
937 char *private;
939 if (ref->status != REF_STATUS_OK)
940 continue;
942 if (!ref->report) {
943 private = apply_refspecs(&data->rs, ref->name);
944 if (!private)
945 continue;
946 refs_update_ref(get_main_ref_store(the_repository),
947 "update by helper", private,
948 &(ref->new_oid),
949 NULL, 0, 0);
950 free(private);
951 } else {
952 for (report = ref->report; report; report = report->next) {
953 private = apply_refspecs(&data->rs,
954 report->ref_name
955 ? report->ref_name
956 : ref->name);
957 if (!private)
958 continue;
959 refs_update_ref(get_main_ref_store(the_repository),
960 "update by helper", private,
961 report->new_oid
962 ? report->new_oid
963 : &(ref->new_oid),
964 NULL, 0, 0);
965 free(private);
969 return 0;
972 static void set_common_push_options(struct transport *transport,
973 const char *name, int flags)
975 if (flags & TRANSPORT_PUSH_DRY_RUN) {
976 if (set_helper_option(transport, "dry-run", "true") != 0)
977 die(_("helper %s does not support dry-run"), name);
978 } else if (flags & TRANSPORT_PUSH_CERT_ALWAYS) {
979 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "true") != 0)
980 die(_("helper %s does not support --signed"), name);
981 } else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) {
982 if (set_helper_option(transport, TRANS_OPT_PUSH_CERT, "if-asked") != 0)
983 die(_("helper %s does not support --signed=if-asked"), name);
986 if (flags & TRANSPORT_PUSH_ATOMIC)
987 if (set_helper_option(transport, TRANS_OPT_ATOMIC, "true") != 0)
988 die(_("helper %s does not support --atomic"), name);
990 if (flags & TRANSPORT_PUSH_FORCE_IF_INCLUDES)
991 if (set_helper_option(transport, TRANS_OPT_FORCE_IF_INCLUDES, "true") != 0)
992 die(_("helper %s does not support --%s"),
993 name, TRANS_OPT_FORCE_IF_INCLUDES);
995 if (flags & TRANSPORT_PUSH_OPTIONS) {
996 struct string_list_item *item;
997 for_each_string_list_item(item, transport->push_options)
998 if (set_helper_option(transport, "push-option", item->string) != 0)
999 die(_("helper %s does not support 'push-option'"), name);
1003 static int push_refs_with_push(struct transport *transport,
1004 struct ref *remote_refs, int flags)
1006 int force_all = flags & TRANSPORT_PUSH_FORCE;
1007 int mirror = flags & TRANSPORT_PUSH_MIRROR;
1008 int atomic = flags & TRANSPORT_PUSH_ATOMIC;
1009 struct helper_data *data = transport->data;
1010 struct strbuf buf = STRBUF_INIT;
1011 struct ref *ref;
1012 struct string_list cas_options = STRING_LIST_INIT_DUP;
1013 struct string_list_item *cas_option;
1015 get_helper(transport);
1016 if (!data->push)
1017 return 1;
1019 for (ref = remote_refs; ref; ref = ref->next) {
1020 if (!ref->peer_ref && !mirror)
1021 continue;
1023 /* Check for statuses set by set_ref_status_for_push() */
1024 switch (ref->status) {
1025 case REF_STATUS_REJECT_NONFASTFORWARD:
1026 case REF_STATUS_REJECT_STALE:
1027 case REF_STATUS_REJECT_ALREADY_EXISTS:
1028 case REF_STATUS_REJECT_REMOTE_UPDATED:
1029 if (atomic) {
1030 reject_atomic_push(remote_refs, mirror);
1031 string_list_clear(&cas_options, 0);
1032 strbuf_release(&buf);
1033 return 0;
1034 } else
1035 continue;
1036 case REF_STATUS_UPTODATE:
1037 continue;
1038 default:
1039 ; /* do nothing */
1042 if (force_all)
1043 ref->force = 1;
1045 strbuf_addstr(&buf, "push ");
1046 if (!ref->deletion) {
1047 if (ref->force)
1048 strbuf_addch(&buf, '+');
1049 if (ref->peer_ref)
1050 strbuf_addstr(&buf, ref->peer_ref->name);
1051 else
1052 strbuf_addstr(&buf, oid_to_hex(&ref->new_oid));
1054 strbuf_addch(&buf, ':');
1055 strbuf_addstr(&buf, ref->name);
1056 strbuf_addch(&buf, '\n');
1059 * The "--force-with-lease" options without explicit
1060 * values to expect have already been expanded into
1061 * the ref->old_oid_expect[] field; we can ignore
1062 * transport->smart_options->cas altogether and instead
1063 * can enumerate them from the refs.
1065 if (ref->expect_old_sha1) {
1066 struct strbuf cas = STRBUF_INIT;
1067 strbuf_addf(&cas, "%s:%s",
1068 ref->name, oid_to_hex(&ref->old_oid_expect));
1069 string_list_append_nodup(&cas_options,
1070 strbuf_detach(&cas, NULL));
1073 if (buf.len == 0) {
1074 string_list_clear(&cas_options, 0);
1075 return 0;
1078 for_each_string_list_item(cas_option, &cas_options)
1079 set_helper_option(transport, "cas", cas_option->string);
1080 set_common_push_options(transport, data->name, flags);
1082 strbuf_addch(&buf, '\n');
1083 sendline(data, &buf);
1084 strbuf_release(&buf);
1085 string_list_clear(&cas_options, 0);
1087 return push_update_refs_status(data, remote_refs, flags);
1090 static int push_refs_with_export(struct transport *transport,
1091 struct ref *remote_refs, int flags)
1093 struct ref *ref;
1094 struct child_process *helper, exporter;
1095 struct helper_data *data = transport->data;
1096 struct string_list revlist_args = STRING_LIST_INIT_DUP;
1097 struct strbuf buf = STRBUF_INIT;
1099 if (!data->rs.nr)
1100 die(_("remote-helper doesn't support push; refspec needed"));
1102 set_common_push_options(transport, data->name, flags);
1103 if (flags & TRANSPORT_PUSH_FORCE) {
1104 if (set_helper_option(transport, "force", "true") != 0)
1105 warning(_("helper %s does not support '--force'"), data->name);
1108 helper = get_helper(transport);
1110 write_constant(helper->in, "export\n");
1112 for (ref = remote_refs; ref; ref = ref->next) {
1113 char *private;
1114 struct object_id oid;
1116 private = apply_refspecs(&data->rs, ref->name);
1117 if (private && !repo_get_oid(the_repository, private, &oid)) {
1118 strbuf_addf(&buf, "^%s", private);
1119 string_list_append_nodup(&revlist_args,
1120 strbuf_detach(&buf, NULL));
1121 oidcpy(&ref->old_oid, &oid);
1123 free(private);
1125 if (ref->peer_ref) {
1126 if (strcmp(ref->name, ref->peer_ref->name)) {
1127 if (!ref->deletion) {
1128 const char *name;
1129 int flag;
1131 /* Follow symbolic refs (mainly for HEAD). */
1132 name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
1133 ref->peer_ref->name,
1134 RESOLVE_REF_READING,
1135 &oid,
1136 &flag);
1137 if (!name || !(flag & REF_ISSYMREF))
1138 name = ref->peer_ref->name;
1140 strbuf_addf(&buf, "%s:%s", name, ref->name);
1141 } else
1142 strbuf_addf(&buf, ":%s", ref->name);
1144 string_list_append(&revlist_args, "--refspec");
1145 string_list_append(&revlist_args, buf.buf);
1146 strbuf_release(&buf);
1148 if (!ref->deletion)
1149 string_list_append(&revlist_args, ref->peer_ref->name);
1153 if (get_exporter(transport, &exporter, &revlist_args))
1154 die(_("couldn't run fast-export"));
1156 string_list_clear(&revlist_args, 1);
1158 if (finish_command(&exporter))
1159 die(_("error while running fast-export"));
1160 if (push_update_refs_status(data, remote_refs, flags))
1161 return 1;
1163 if (data->export_marks) {
1164 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1165 rename(buf.buf, data->export_marks);
1166 strbuf_release(&buf);
1169 return 0;
1172 static int push_refs(struct transport *transport,
1173 struct ref *remote_refs, int flags)
1175 struct helper_data *data = transport->data;
1177 if (process_connect(transport, 1))
1178 return transport->vtable->push_refs(transport, remote_refs, flags);
1180 if (!remote_refs) {
1181 fprintf(stderr,
1182 _("No refs in common and none specified; doing nothing.\n"
1183 "Perhaps you should specify a branch.\n"));
1184 return 0;
1187 if (data->push)
1188 return push_refs_with_push(transport, remote_refs, flags);
1190 if (data->export)
1191 return push_refs_with_export(transport, remote_refs, flags);
1193 return -1;
1197 static int has_attribute(const char *attrs, const char *attr)
1199 int len;
1200 if (!attrs)
1201 return 0;
1203 len = strlen(attr);
1204 for (;;) {
1205 const char *space = strchrnul(attrs, ' ');
1206 if (len == space - attrs && !strncmp(attrs, attr, len))
1207 return 1;
1208 if (!*space)
1209 return 0;
1210 attrs = space + 1;
1214 static struct ref *get_refs_list(struct transport *transport, int for_push,
1215 struct transport_ls_refs_options *transport_options)
1217 get_helper(transport);
1219 if (process_connect(transport, for_push))
1220 return transport->vtable->get_refs_list(transport, for_push,
1221 transport_options);
1223 return get_refs_list_using_list(transport, for_push);
1226 static struct ref *get_refs_list_using_list(struct transport *transport,
1227 int for_push)
1229 struct helper_data *data = transport->data;
1230 struct child_process *helper;
1231 struct ref *ret = NULL;
1232 struct ref **tail = &ret;
1233 struct ref *posn;
1234 struct strbuf buf = STRBUF_INIT;
1236 data->get_refs_list_called = 1;
1237 helper = get_helper(transport);
1239 if (data->object_format)
1240 set_helper_option(transport, "object-format", "true");
1242 if (data->push && for_push)
1243 write_constant(helper->in, "list for-push\n");
1244 else
1245 write_constant(helper->in, "list\n");
1247 while (1) {
1248 char *eov, *eon;
1249 if (recvline(data, &buf))
1250 exit(128);
1252 if (!*buf.buf)
1253 break;
1254 else if (buf.buf[0] == ':') {
1255 const char *value;
1256 if (skip_prefix(buf.buf, ":object-format ", &value)) {
1257 int algo = hash_algo_by_name(value);
1258 if (algo == GIT_HASH_UNKNOWN)
1259 die(_("unsupported object format '%s'"),
1260 value);
1261 transport->hash_algo = &hash_algos[algo];
1263 continue;
1266 eov = strchr(buf.buf, ' ');
1267 if (!eov)
1268 die(_("malformed response in ref list: %s"), buf.buf);
1269 eon = strchr(eov + 1, ' ');
1270 *eov = '\0';
1271 if (eon)
1272 *eon = '\0';
1273 *tail = alloc_ref(eov + 1);
1274 if (buf.buf[0] == '@')
1275 (*tail)->symref = xstrdup(buf.buf + 1);
1276 else if (buf.buf[0] != '?')
1277 get_oid_hex_algop(buf.buf, &(*tail)->old_oid, transport->hash_algo);
1278 if (eon) {
1279 if (has_attribute(eon + 1, "unchanged")) {
1280 (*tail)->status |= REF_STATUS_UPTODATE;
1281 if (refs_read_ref(get_main_ref_store(the_repository), (*tail)->name, &(*tail)->old_oid) < 0)
1282 die(_("could not read ref %s"),
1283 (*tail)->name);
1286 tail = &((*tail)->next);
1288 if (debug)
1289 fprintf(stderr, "Debug: Read ref listing.\n");
1290 strbuf_release(&buf);
1292 for (posn = ret; posn; posn = posn->next)
1293 resolve_remote_symref(posn, ret);
1295 return ret;
1298 static int get_bundle_uri(struct transport *transport)
1300 get_helper(transport);
1302 if (process_connect(transport, 0))
1303 return transport->vtable->get_bundle_uri(transport);
1305 return -1;
1308 static struct transport_vtable vtable = {
1309 .set_option = set_helper_option,
1310 .get_refs_list = get_refs_list,
1311 .get_bundle_uri = get_bundle_uri,
1312 .fetch_refs = fetch_refs,
1313 .push_refs = push_refs,
1314 .connect = connect_helper,
1315 .disconnect = release_helper
1318 int transport_helper_init(struct transport *transport, const char *name)
1320 struct helper_data *data = xcalloc(1, sizeof(*data));
1321 data->name = xstrdup(name);
1323 transport_check_allowed(name);
1325 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
1326 debug = 1;
1328 list_objects_filter_init(&data->transport_options.filter_options);
1330 transport->data = data;
1331 transport->vtable = &vtable;
1332 transport->smart_options = &(data->transport_options);
1333 return 0;
1337 * Linux pipes can buffer 65536 bytes at once (and most platforms can
1338 * buffer less), so attempt reads and writes with up to that size.
1340 #define BUFFERSIZE 65536
1341 /* This should be enough to hold debugging message. */
1342 #define PBUFFERSIZE 8192
1344 /* Print bidirectional transfer loop debug message. */
1345 __attribute__((format (printf, 1, 2)))
1346 static void transfer_debug(const char *fmt, ...)
1349 * NEEDSWORK: This function is sometimes used from multiple threads, and
1350 * we end up using debug_enabled racily. That "should not matter" since
1351 * we always write the same value, but it's still wrong. This function
1352 * is listed in .tsan-suppressions for the time being.
1355 va_list args;
1356 char msgbuf[PBUFFERSIZE];
1357 static int debug_enabled = -1;
1359 if (debug_enabled < 0)
1360 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
1361 if (!debug_enabled)
1362 return;
1364 va_start(args, fmt);
1365 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
1366 va_end(args);
1367 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
1370 /* Stream state: More data may be coming in this direction. */
1371 #define SSTATE_TRANSFERRING 0
1373 * Stream state: No more data coming in this direction, flushing rest of
1374 * data.
1376 #define SSTATE_FLUSHING 1
1377 /* Stream state: Transfer in this direction finished. */
1378 #define SSTATE_FINISHED 2
1380 #define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERRING)
1381 #define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
1382 #define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
1384 /* Unidirectional transfer. */
1385 struct unidirectional_transfer {
1386 /* Source */
1387 int src;
1388 /* Destination */
1389 int dest;
1390 /* Is source socket? */
1391 int src_is_sock;
1392 /* Is destination socket? */
1393 int dest_is_sock;
1394 /* Transfer state (TRANSFERRING/FLUSHING/FINISHED) */
1395 int state;
1396 /* Buffer. */
1397 char buf[BUFFERSIZE];
1398 /* Buffer used. */
1399 size_t bufuse;
1400 /* Name of source. */
1401 const char *src_name;
1402 /* Name of destination. */
1403 const char *dest_name;
1406 /* Closes the target (for writing) if transfer has finished. */
1407 static void udt_close_if_finished(struct unidirectional_transfer *t)
1409 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
1410 t->state = SSTATE_FINISHED;
1411 if (t->dest_is_sock)
1412 shutdown(t->dest, SHUT_WR);
1413 else
1414 close(t->dest);
1415 transfer_debug("Closed %s.", t->dest_name);
1420 * Tries to read data from source into buffer. If buffer is full,
1421 * no data is read. Returns 0 on success, -1 on error.
1423 static int udt_do_read(struct unidirectional_transfer *t)
1425 ssize_t bytes;
1427 if (t->bufuse == BUFFERSIZE)
1428 return 0; /* No space for more. */
1430 transfer_debug("%s is readable", t->src_name);
1431 bytes = xread(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
1432 if (bytes < 0) {
1433 error_errno(_("read(%s) failed"), t->src_name);
1434 return -1;
1435 } else if (bytes == 0) {
1436 transfer_debug("%s EOF (with %i bytes in buffer)",
1437 t->src_name, (int)t->bufuse);
1438 t->state = SSTATE_FLUSHING;
1439 } else if (bytes > 0) {
1440 t->bufuse += bytes;
1441 transfer_debug("Read %i bytes from %s (buffer now at %i)",
1442 (int)bytes, t->src_name, (int)t->bufuse);
1444 return 0;
1447 /* Tries to write data from buffer into destination. If buffer is empty,
1448 * no data is written. Returns 0 on success, -1 on error.
1450 static int udt_do_write(struct unidirectional_transfer *t)
1452 ssize_t bytes;
1454 if (t->bufuse == 0)
1455 return 0; /* Nothing to write. */
1457 transfer_debug("%s is writable", t->dest_name);
1458 bytes = xwrite(t->dest, t->buf, t->bufuse);
1459 if (bytes < 0) {
1460 error_errno(_("write(%s) failed"), t->dest_name);
1461 return -1;
1462 } else if (bytes > 0) {
1463 t->bufuse -= bytes;
1464 if (t->bufuse)
1465 memmove(t->buf, t->buf + bytes, t->bufuse);
1466 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
1467 (int)bytes, t->dest_name, (int)t->bufuse);
1469 return 0;
1473 /* State of bidirectional transfer loop. */
1474 struct bidirectional_transfer_state {
1475 /* Direction from program to git. */
1476 struct unidirectional_transfer ptg;
1477 /* Direction from git to program. */
1478 struct unidirectional_transfer gtp;
1481 static void *udt_copy_task_routine(void *udt)
1483 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1484 while (t->state != SSTATE_FINISHED) {
1485 if (STATE_NEEDS_READING(t->state))
1486 if (udt_do_read(t))
1487 return NULL;
1488 if (STATE_NEEDS_WRITING(t->state))
1489 if (udt_do_write(t))
1490 return NULL;
1491 if (STATE_NEEDS_CLOSING(t->state))
1492 udt_close_if_finished(t);
1494 return udt; /* Just some non-NULL value. */
1497 #ifndef NO_PTHREADS
1500 * Join thread, with appropriate errors on failure. Name is name for the
1501 * thread (for error messages). Returns 0 on success, 1 on failure.
1503 static int tloop_join(pthread_t thread, const char *name)
1505 int err;
1506 void *tret;
1507 err = pthread_join(thread, &tret);
1508 if (!tret) {
1509 error(_("%s thread failed"), name);
1510 return 1;
1512 if (err) {
1513 error(_("%s thread failed to join: %s"), name, strerror(err));
1514 return 1;
1516 return 0;
1520 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1521 * -1 on failure.
1523 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1525 pthread_t gtp_thread;
1526 pthread_t ptg_thread;
1527 int err;
1528 int ret = 0;
1529 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1530 &s->gtp);
1531 if (err)
1532 die(_("can't start thread for copying data: %s"), strerror(err));
1533 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1534 &s->ptg);
1535 if (err)
1536 die(_("can't start thread for copying data: %s"), strerror(err));
1538 ret |= tloop_join(gtp_thread, "Git to program copy");
1539 ret |= tloop_join(ptg_thread, "Program to git copy");
1540 return ret;
1542 #else
1544 /* Close the source and target (for writing) for transfer. */
1545 static void udt_kill_transfer(struct unidirectional_transfer *t)
1547 t->state = SSTATE_FINISHED;
1549 * Socket read end left open isn't a disaster if nobody
1550 * attempts to read from it (mingw compat headers do not
1551 * have SHUT_RD)...
1553 * We can't fully close the socket since otherwise gtp
1554 * task would first close the socket it sends data to
1555 * while closing the ptg file descriptors.
1557 if (!t->src_is_sock)
1558 close(t->src);
1559 if (t->dest_is_sock)
1560 shutdown(t->dest, SHUT_WR);
1561 else
1562 close(t->dest);
1566 * Join process, with appropriate errors on failure. Name is name for the
1567 * process (for error messages). Returns 0 on success, 1 on failure.
1569 static int tloop_join(pid_t pid, const char *name)
1571 int tret;
1572 if (waitpid(pid, &tret, 0) < 0) {
1573 error_errno(_("%s process failed to wait"), name);
1574 return 1;
1576 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1577 error(_("%s process failed"), name);
1578 return 1;
1580 return 0;
1584 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1585 * -1 on failure.
1587 static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1589 pid_t pid1, pid2;
1590 int ret = 0;
1592 /* Fork thread #1: git to program. */
1593 pid1 = fork();
1594 if (pid1 < 0)
1595 die_errno(_("can't start thread for copying data"));
1596 else if (pid1 == 0) {
1597 udt_kill_transfer(&s->ptg);
1598 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1601 /* Fork thread #2: program to git. */
1602 pid2 = fork();
1603 if (pid2 < 0)
1604 die_errno(_("can't start thread for copying data"));
1605 else if (pid2 == 0) {
1606 udt_kill_transfer(&s->gtp);
1607 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1611 * Close both streams in parent as to not interfere with
1612 * end of file detection and wait for both tasks to finish.
1614 udt_kill_transfer(&s->gtp);
1615 udt_kill_transfer(&s->ptg);
1616 ret |= tloop_join(pid1, "Git to program copy");
1617 ret |= tloop_join(pid2, "Program to git copy");
1618 return ret;
1620 #endif
1623 * Copies data from stdin to output and from input to stdout simultaneously.
1624 * Additionally filtering through given filter. If filter is NULL, uses
1625 * identity filter.
1627 int bidirectional_transfer_loop(int input, int output)
1629 struct bidirectional_transfer_state state;
1631 /* Fill the state fields. */
1632 state.ptg.src = input;
1633 state.ptg.dest = 1;
1634 state.ptg.src_is_sock = (input == output);
1635 state.ptg.dest_is_sock = 0;
1636 state.ptg.state = SSTATE_TRANSFERRING;
1637 state.ptg.bufuse = 0;
1638 state.ptg.src_name = "remote input";
1639 state.ptg.dest_name = "stdout";
1641 state.gtp.src = 0;
1642 state.gtp.dest = output;
1643 state.gtp.src_is_sock = 0;
1644 state.gtp.dest_is_sock = (input == output);
1645 state.gtp.state = SSTATE_TRANSFERRING;
1646 state.gtp.bufuse = 0;
1647 state.gtp.src_name = "stdin";
1648 state.gtp.dest_name = "remote output";
1650 return tloop_spawnwait_tasks(&state);
1653 void reject_atomic_push(struct ref *remote_refs, int mirror_mode)
1655 struct ref *ref;
1657 /* Mark other refs as failed */
1658 for (ref = remote_refs; ref; ref = ref->next) {
1659 if (!ref->peer_ref && !mirror_mode)
1660 continue;
1662 switch (ref->status) {
1663 case REF_STATUS_NONE:
1664 case REF_STATUS_OK:
1665 case REF_STATUS_EXPECTING_REPORT:
1666 ref->status = REF_STATUS_ATOMIC_PUSH_FAILED;
1667 continue;
1668 default:
1669 break; /* do nothing */
1672 return;