remote-curl: Fix push status report when all branches fail
[git/git.git] / transport-helper.c
CommitLineData
6eb996b5
DB
1#include "cache.h"
2#include "transport.h"
ae4efe19 3#include "quote.h"
6eb996b5
DB
4#include "run-command.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
ef08ef9e 8#include "quote.h"
72ff8943 9#include "remote.h"
73b49a75 10#include "string-list.h"
7851b1e6 11#include "thread-utils.h"
7851b1e6 12
bf3c523c
IL
13static int debug;
14
9cba13ca 15struct helper_data {
6eb996b5
DB
16 const char *name;
17 struct child_process *helper;
292ce46b 18 FILE *out;
ef08ef9e 19 unsigned fetch : 1,
a24a32dd 20 import : 1,
73b49a75 21 export : 1,
ae4efe19 22 option : 1,
fa8c097c
IL
23 push : 1,
24 connect : 1,
25 no_disconnect_req : 1;
72ff8943
DB
26 /* These go from remote name (as in "list") to private name */
27 struct refspec *refspecs;
28 int refspec_nr;
61b075bd
IL
29 /* Transport options for fetch-pack/send-pack (should one of
30 * those be invoked).
31 */
32 struct git_transport_options transport_options;
6eb996b5
DB
33};
34
bf3c523c
IL
35static void sendline(struct helper_data *helper, struct strbuf *buffer)
36{
37 if (debug)
38 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
39 if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
40 != buffer->len)
41 die_errno("Full write to remote helper failed");
42}
43
fa8c097c 44static int recvline_fh(FILE *helper, struct strbuf *buffer)
bf3c523c
IL
45{
46 strbuf_reset(buffer);
47 if (debug)
48 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
fa8c097c 49 if (strbuf_getline(buffer, helper, '\n') == EOF) {
bf3c523c
IL
50 if (debug)
51 fprintf(stderr, "Debug: Remote helper quit.\n");
52 exit(128);
53 }
54
55 if (debug)
56 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
57 return 0;
58}
59
fa8c097c
IL
60static int recvline(struct helper_data *helper, struct strbuf *buffer)
61{
62 return recvline_fh(helper->out, buffer);
63}
64
bf3c523c
IL
65static void xchgline(struct helper_data *helper, struct strbuf *buffer)
66{
67 sendline(helper, buffer);
68 recvline(helper, buffer);
69}
70
71static void write_constant(int fd, const char *str)
72{
73 if (debug)
74 fprintf(stderr, "Debug: Remote helper: -> %s", str);
75 if (write_in_full(fd, str, strlen(str)) != strlen(str))
76 die_errno("Full write to remote helper failed");
77}
78
c2e86add 79static const char *remove_ext_force(const char *url)
25d5cc48
IL
80{
81 if (url) {
82 const char *colon = strchr(url, ':');
83 if (colon && colon[1] == ':')
84 return colon + 2;
85 }
86 return url;
87}
88
fa8c097c
IL
89static void do_take_over(struct transport *transport)
90{
91 struct helper_data *data;
92 data = (struct helper_data *)transport->data;
93 transport_take_over(transport, data->helper);
94 fclose(data->out);
95 free(data);
96}
97
6eb996b5
DB
98static struct child_process *get_helper(struct transport *transport)
99{
100 struct helper_data *data = transport->data;
101 struct strbuf buf = STRBUF_INIT;
102 struct child_process *helper;
72ff8943
DB
103 const char **refspecs = NULL;
104 int refspec_nr = 0;
105 int refspec_alloc = 0;
61b075bd 106 int duped;
6b02de3b 107 int code;
6eb996b5
DB
108
109 if (data->helper)
110 return data->helper;
111
112 helper = xcalloc(1, sizeof(*helper));
113 helper->in = -1;
114 helper->out = -1;
115 helper->err = 0;
116 helper->argv = xcalloc(4, sizeof(*helper->argv));
6b02de3b 117 strbuf_addf(&buf, "git-remote-%s", data->name);
6eb996b5
DB
118 helper->argv[0] = strbuf_detach(&buf, NULL);
119 helper->argv[1] = transport->remote->name;
25d5cc48 120 helper->argv[2] = remove_ext_force(transport->url);
6b02de3b
IL
121 helper->git_cmd = 0;
122 helper->silent_exec_failure = 1;
123 code = start_command(helper);
124 if (code < 0 && errno == ENOENT)
125 die("Unable to find remote helper for '%s'", data->name);
126 else if (code != 0)
127 exit(code);
128
6eb996b5 129 data->helper = helper;
fa8c097c 130 data->no_disconnect_req = 0;
6eb996b5 131
61b075bd
IL
132 /*
133 * Open the output as FILE* so strbuf_getline() can be used.
134 * Do this with duped fd because fclose() will close the fd,
135 * and stuff like taking over will require the fd to remain.
61b075bd
IL
136 */
137 duped = dup(helper->out);
138 if (duped < 0)
139 die_errno("Can't dup helper output fd");
140 data->out = xfdopen(duped, "r");
141
bf3c523c 142 write_constant(helper->in, "capabilities\n");
2d14d65c 143
6eb996b5 144 while (1) {
28ed5b35
IL
145 const char *capname;
146 int mandatory = 0;
bf3c523c 147 recvline(data, &buf);
6eb996b5
DB
148
149 if (!*buf.buf)
150 break;
28ed5b35
IL
151
152 if (*buf.buf == '*') {
153 capname = buf.buf + 1;
154 mandatory = 1;
155 } else
156 capname = buf.buf;
157
bf3c523c 158 if (debug)
28ed5b35
IL
159 fprintf(stderr, "Debug: Got cap %s\n", capname);
160 if (!strcmp(capname, "fetch"))
6eb996b5 161 data->fetch = 1;
28ed5b35 162 else if (!strcmp(capname, "option"))
ef08ef9e 163 data->option = 1;
28ed5b35 164 else if (!strcmp(capname, "push"))
ae4efe19 165 data->push = 1;
28ed5b35 166 else if (!strcmp(capname, "import"))
e65e91ed 167 data->import = 1;
73b49a75
SR
168 else if (!strcmp(capname, "export"))
169 data->export = 1;
28ed5b35 170 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
72ff8943
DB
171 ALLOC_GROW(refspecs,
172 refspec_nr + 1,
173 refspec_alloc);
174 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
fa8c097c
IL
175 } else if (!strcmp(capname, "connect")) {
176 data->connect = 1;
88f3b2b0
SR
177 } else if (!strcmp(buf.buf, "gitdir")) {
178 struct strbuf gitdir = STRBUF_INIT;
179 strbuf_addf(&gitdir, "gitdir %s\n", get_git_dir());
180 sendline(data, &gitdir);
181 strbuf_release(&gitdir);
28ed5b35 182 } else if (mandatory) {
9517e6b8 183 die("Unknown mandatory capability %s. This remote "
28ed5b35
IL
184 "helper probably needs newer version of Git.\n",
185 capname);
72ff8943
DB
186 }
187 }
188 if (refspecs) {
189 int i;
190 data->refspec_nr = refspec_nr;
191 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
192 for (i = 0; i < refspec_nr; i++) {
193 free((char *)refspecs[i]);
194 }
195 free(refspecs);
6eb996b5 196 }
b962dbdc 197 strbuf_release(&buf);
bf3c523c
IL
198 if (debug)
199 fprintf(stderr, "Debug: Capabilities complete.\n");
6eb996b5
DB
200 return data->helper;
201}
202
203static int disconnect_helper(struct transport *transport)
204{
205 struct helper_data *data = transport->data;
bf3c523c
IL
206 struct strbuf buf = STRBUF_INIT;
207
6eb996b5 208 if (data->helper) {
bf3c523c
IL
209 if (debug)
210 fprintf(stderr, "Debug: Disconnecting.\n");
fa8c097c
IL
211 if (!data->no_disconnect_req) {
212 strbuf_addf(&buf, "\n");
213 sendline(data, &buf);
214 }
6eb996b5 215 close(data->helper->in);
61b075bd 216 close(data->helper->out);
292ce46b 217 fclose(data->out);
6eb996b5
DB
218 finish_command(data->helper);
219 free((char *)data->helper->argv[0]);
220 free(data->helper->argv);
221 free(data->helper);
222 data->helper = NULL;
223 }
224 return 0;
225}
226
ef08ef9e
SP
227static const char *unsupported_options[] = {
228 TRANS_OPT_UPLOADPACK,
229 TRANS_OPT_RECEIVEPACK,
230 TRANS_OPT_THIN,
231 TRANS_OPT_KEEP
232 };
233static const char *boolean_options[] = {
234 TRANS_OPT_THIN,
235 TRANS_OPT_KEEP,
236 TRANS_OPT_FOLLOWTAGS
237 };
238
239static int set_helper_option(struct transport *transport,
240 const char *name, const char *value)
241{
242 struct helper_data *data = transport->data;
ef08ef9e
SP
243 struct strbuf buf = STRBUF_INIT;
244 int i, ret, is_bool = 0;
245
bf3c523c
IL
246 get_helper(transport);
247
ef08ef9e
SP
248 if (!data->option)
249 return 1;
250
251 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
252 if (!strcmp(name, unsupported_options[i]))
253 return 1;
254 }
255
256 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
257 if (!strcmp(name, boolean_options[i])) {
258 is_bool = 1;
259 break;
260 }
261 }
262
263 strbuf_addf(&buf, "option %s ", name);
264 if (is_bool)
265 strbuf_addstr(&buf, value ? "true" : "false");
266 else
267 quote_c_style(value, &buf, NULL, 0);
268 strbuf_addch(&buf, '\n');
269
bf3c523c 270 xchgline(data, &buf);
ef08ef9e
SP
271
272 if (!strcmp(buf.buf, "ok"))
273 ret = 0;
274 else if (!prefixcmp(buf.buf, "error")) {
275 ret = -1;
276 } else if (!strcmp(buf.buf, "unsupported"))
277 ret = 1;
278 else {
279 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
280 ret = 1;
281 }
282 strbuf_release(&buf);
283 return ret;
284}
285
286static void standard_options(struct transport *t)
287{
288 char buf[16];
289 int n;
290 int v = t->verbose;
ef08ef9e 291
d01b3c02 292 set_helper_option(t, "progress", t->progress ? "true" : "false");
ef08ef9e
SP
293
294 n = snprintf(buf, sizeof(buf), "%d", v + 1);
295 if (n >= sizeof(buf))
296 die("impossibly large verbosity value");
297 set_helper_option(t, "verbosity", buf);
298}
299
f2a37151
DB
300static int release_helper(struct transport *transport)
301{
72ff8943
DB
302 struct helper_data *data = transport->data;
303 free_refspec(data->refspec_nr, data->refspecs);
304 data->refspecs = NULL;
f2a37151
DB
305 disconnect_helper(transport);
306 free(transport->data);
307 return 0;
308}
309
6eb996b5 310static int fetch_with_fetch(struct transport *transport,
37148311 311 int nr_heads, struct ref **to_fetch)
6eb996b5 312{
292ce46b 313 struct helper_data *data = transport->data;
6eb996b5
DB
314 int i;
315 struct strbuf buf = STRBUF_INIT;
316
ef08ef9e
SP
317 standard_options(transport);
318
6eb996b5 319 for (i = 0; i < nr_heads; i++) {
1088261f 320 const struct ref *posn = to_fetch[i];
6eb996b5
DB
321 if (posn->status & REF_STATUS_UPTODATE)
322 continue;
2d14d65c
DB
323
324 strbuf_addf(&buf, "fetch %s %s\n",
325 sha1_to_hex(posn->old_sha1), posn->name);
292ce46b 326 }
2d14d65c 327
292ce46b 328 strbuf_addch(&buf, '\n');
bf3c523c 329 sendline(data, &buf);
292ce46b
SP
330
331 while (1) {
bf3c523c 332 recvline(data, &buf);
292ce46b
SP
333
334 if (!prefixcmp(buf.buf, "lock ")) {
335 const char *name = buf.buf + 5;
336 if (transport->pack_lockfile)
337 warning("%s also locked %s", data->name, name);
338 else
339 transport->pack_lockfile = xstrdup(name);
340 }
341 else if (!buf.len)
342 break;
343 else
344 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
6eb996b5 345 }
292ce46b 346 strbuf_release(&buf);
6eb996b5
DB
347 return 0;
348}
349
e65e91ed
DB
350static int get_importer(struct transport *transport, struct child_process *fastimport)
351{
352 struct child_process *helper = get_helper(transport);
353 memset(fastimport, 0, sizeof(*fastimport));
354 fastimport->in = helper->out;
355 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
356 fastimport->argv[0] = "fast-import";
357 fastimport->argv[1] = "--quiet";
358
359 fastimport->git_cmd = 1;
360 return start_command(fastimport);
361}
362
73b49a75
SR
363static int get_exporter(struct transport *transport,
364 struct child_process *fastexport,
365 const char *export_marks,
366 const char *import_marks,
367 struct string_list *revlist_args)
368{
369 struct child_process *helper = get_helper(transport);
370 int argc = 0, i;
371 memset(fastexport, 0, sizeof(*fastexport));
372
373 /* we need to duplicate helper->in because we want to use it after
374 * fastexport is done with it. */
375 fastexport->out = dup(helper->in);
376 fastexport->argv = xcalloc(4 + revlist_args->nr, sizeof(*fastexport->argv));
377 fastexport->argv[argc++] = "fast-export";
378 if (export_marks)
379 fastexport->argv[argc++] = export_marks;
380 if (import_marks)
381 fastexport->argv[argc++] = import_marks;
382
383 for (i = 0; i < revlist_args->nr; i++)
384 fastexport->argv[argc++] = revlist_args->items[i].string;
385
386 fastexport->git_cmd = 1;
387 return start_command(fastexport);
388}
389
e65e91ed
DB
390static int fetch_with_import(struct transport *transport,
391 int nr_heads, struct ref **to_fetch)
392{
393 struct child_process fastimport;
72ff8943 394 struct helper_data *data = transport->data;
e65e91ed
DB
395 int i;
396 struct ref *posn;
397 struct strbuf buf = STRBUF_INIT;
398
bf3c523c
IL
399 get_helper(transport);
400
e65e91ed
DB
401 if (get_importer(transport, &fastimport))
402 die("Couldn't run fast-import");
403
404 for (i = 0; i < nr_heads; i++) {
405 posn = to_fetch[i];
406 if (posn->status & REF_STATUS_UPTODATE)
407 continue;
408
409 strbuf_addf(&buf, "import %s\n", posn->name);
bf3c523c 410 sendline(data, &buf);
e65e91ed
DB
411 strbuf_reset(&buf);
412 }
413 disconnect_helper(transport);
414 finish_command(&fastimport);
b962dbdc
SR
415 free(fastimport.argv);
416 fastimport.argv = NULL;
e65e91ed
DB
417
418 for (i = 0; i < nr_heads; i++) {
72ff8943 419 char *private;
e65e91ed
DB
420 posn = to_fetch[i];
421 if (posn->status & REF_STATUS_UPTODATE)
422 continue;
72ff8943
DB
423 if (data->refspecs)
424 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
425 else
426 private = strdup(posn->name);
427 read_ref(private, posn->old_sha1);
428 free(private);
e65e91ed 429 }
b962dbdc 430 strbuf_release(&buf);
e65e91ed
DB
431 return 0;
432}
433
fa8c097c
IL
434static int process_connect_service(struct transport *transport,
435 const char *name, const char *exec)
436{
437 struct helper_data *data = transport->data;
438 struct strbuf cmdbuf = STRBUF_INIT;
439 struct child_process *helper;
440 int r, duped, ret = 0;
441 FILE *input;
442
443 helper = get_helper(transport);
444
445 /*
446 * Yes, dup the pipe another time, as we need unbuffered version
447 * of input pipe as FILE*. fclose() closes the underlying fd and
448 * stream buffering only can be changed before first I/O operation
449 * on it.
450 */
451 duped = dup(helper->out);
452 if (duped < 0)
453 die_errno("Can't dup helper output fd");
454 input = xfdopen(duped, "r");
455 setvbuf(input, NULL, _IONBF, 0);
456
457 /*
458 * Handle --upload-pack and friends. This is fire and forget...
459 * just warn if it fails.
460 */
461 if (strcmp(name, exec)) {
462 r = set_helper_option(transport, "servpath", exec);
463 if (r > 0)
464 warning("Setting remote service path not supported by protocol.");
465 else if (r < 0)
466 warning("Invalid remote service path.");
467 }
468
469 if (data->connect)
470 strbuf_addf(&cmdbuf, "connect %s\n", name);
471 else
472 goto exit;
473
474 sendline(data, &cmdbuf);
475 recvline_fh(input, &cmdbuf);
476 if (!strcmp(cmdbuf.buf, "")) {
477 data->no_disconnect_req = 1;
478 if (debug)
479 fprintf(stderr, "Debug: Smart transport connection "
480 "ready.\n");
481 ret = 1;
482 } else if (!strcmp(cmdbuf.buf, "fallback")) {
483 if (debug)
484 fprintf(stderr, "Debug: Falling back to dumb "
485 "transport.\n");
486 } else
487 die("Unknown response to connect: %s",
488 cmdbuf.buf);
489
490exit:
491 fclose(input);
492 return ret;
493}
494
495static int process_connect(struct transport *transport,
496 int for_push)
497{
498 struct helper_data *data = transport->data;
499 const char *name;
500 const char *exec;
501
502 name = for_push ? "git-receive-pack" : "git-upload-pack";
503 if (for_push)
504 exec = data->transport_options.receivepack;
505 else
506 exec = data->transport_options.uploadpack;
507
508 return process_connect_service(transport, name, exec);
509}
510
b236752a
IL
511static int connect_helper(struct transport *transport, const char *name,
512 const char *exec, int fd[2])
513{
514 struct helper_data *data = transport->data;
515
516 /* Get_helper so connect is inited. */
517 get_helper(transport);
518 if (!data->connect)
519 die("Operation not supported by protocol.");
520
521 if (!process_connect_service(transport, name, exec))
522 die("Can't connect to subservice %s.", name);
523
524 fd[0] = data->helper->out;
525 fd[1] = data->helper->in;
526 return 0;
527}
528
6eb996b5 529static int fetch(struct transport *transport,
37148311 530 int nr_heads, struct ref **to_fetch)
6eb996b5
DB
531{
532 struct helper_data *data = transport->data;
533 int i, count;
534
fa8c097c
IL
535 if (process_connect(transport, 0)) {
536 do_take_over(transport);
537 return transport->fetch(transport, nr_heads, to_fetch);
538 }
539
6eb996b5
DB
540 count = 0;
541 for (i = 0; i < nr_heads; i++)
542 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
543 count++;
544
545 if (!count)
546 return 0;
547
548 if (data->fetch)
549 return fetch_with_fetch(transport, nr_heads, to_fetch);
550
e65e91ed
DB
551 if (data->import)
552 return fetch_with_import(transport, nr_heads, to_fetch);
553
6eb996b5
DB
554 return -1;
555}
556
73b49a75 557static int push_refs_with_push(struct transport *transport,
ae4efe19
SP
558 struct ref *remote_refs, int flags)
559{
560 int force_all = flags & TRANSPORT_PUSH_FORCE;
561 int mirror = flags & TRANSPORT_PUSH_MIRROR;
562 struct helper_data *data = transport->data;
563 struct strbuf buf = STRBUF_INIT;
ae4efe19
SP
564 struct ref *ref;
565
c0aa335c 566 get_helper(transport);
ae4efe19
SP
567 if (!data->push)
568 return 1;
569
570 for (ref = remote_refs; ref; ref = ref->next) {
20e8b465 571 if (!ref->peer_ref && !mirror)
ae4efe19
SP
572 continue;
573
20e8b465
TRC
574 /* Check for statuses set by set_ref_status_for_push() */
575 switch (ref->status) {
576 case REF_STATUS_REJECT_NONFASTFORWARD:
577 case REF_STATUS_UPTODATE:
ae4efe19 578 continue;
20e8b465
TRC
579 default:
580 ; /* do nothing */
ae4efe19
SP
581 }
582
583 if (force_all)
584 ref->force = 1;
585
586 strbuf_addstr(&buf, "push ");
587 if (!ref->deletion) {
588 if (ref->force)
589 strbuf_addch(&buf, '+');
590 if (ref->peer_ref)
591 strbuf_addstr(&buf, ref->peer_ref->name);
592 else
593 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
594 }
595 strbuf_addch(&buf, ':');
596 strbuf_addstr(&buf, ref->name);
597 strbuf_addch(&buf, '\n');
598 }
d8f67d20
CB
599 if (buf.len == 0)
600 return 0;
ae4efe19 601
ae4efe19
SP
602 standard_options(transport);
603
604 if (flags & TRANSPORT_PUSH_DRY_RUN) {
605 if (set_helper_option(transport, "dry-run", "true") != 0)
606 die("helper %s does not support dry-run", data->name);
607 }
608
609 strbuf_addch(&buf, '\n');
bf3c523c 610 sendline(data, &buf);
ae4efe19
SP
611
612 ref = remote_refs;
613 while (1) {
614 char *refname, *msg;
615 int status;
616
bf3c523c 617 recvline(data, &buf);
ae4efe19
SP
618 if (!buf.len)
619 break;
620
621 if (!prefixcmp(buf.buf, "ok ")) {
622 status = REF_STATUS_OK;
623 refname = buf.buf + 3;
624 } else if (!prefixcmp(buf.buf, "error ")) {
625 status = REF_STATUS_REMOTE_REJECT;
626 refname = buf.buf + 6;
627 } else
628 die("expected ok/error, helper said '%s'\n", buf.buf);
629
630 msg = strchr(refname, ' ');
631 if (msg) {
632 struct strbuf msg_buf = STRBUF_INIT;
633 const char *end;
634
635 *msg++ = '\0';
636 if (!unquote_c_style(&msg_buf, msg, &end))
637 msg = strbuf_detach(&msg_buf, NULL);
638 else
639 msg = xstrdup(msg);
640 strbuf_release(&msg_buf);
641
642 if (!strcmp(msg, "no match")) {
643 status = REF_STATUS_NONE;
644 free(msg);
645 msg = NULL;
646 }
647 else if (!strcmp(msg, "up to date")) {
648 status = REF_STATUS_UPTODATE;
649 free(msg);
650 msg = NULL;
651 }
652 else if (!strcmp(msg, "non-fast forward")) {
653 status = REF_STATUS_REJECT_NONFASTFORWARD;
654 free(msg);
655 msg = NULL;
656 }
657 }
658
659 if (ref)
660 ref = find_ref_by_name(ref, refname);
661 if (!ref)
662 ref = find_ref_by_name(remote_refs, refname);
663 if (!ref) {
664 warning("helper reported unexpected status of %s", refname);
665 continue;
666 }
667
08d63a42
TRC
668 if (ref->status != REF_STATUS_NONE) {
669 /*
670 * Earlier, the ref was marked not to be pushed, so ignore the ref
671 * status reported by the remote helper if the latter is 'no match'.
672 */
673 if (status == REF_STATUS_NONE)
674 continue;
675 }
676
ae4efe19
SP
677 ref->status = status;
678 ref->remote_status = msg;
679 }
680 strbuf_release(&buf);
681 return 0;
682}
683
73b49a75
SR
684static int push_refs_with_export(struct transport *transport,
685 struct ref *remote_refs, int flags)
686{
687 struct ref *ref;
688 struct child_process *helper, exporter;
689 struct helper_data *data = transport->data;
690 char *export_marks = NULL, *import_marks = NULL;
183113a5 691 struct string_list revlist_args = STRING_LIST_INIT_NODUP;
73b49a75
SR
692 struct strbuf buf = STRBUF_INIT;
693
694 helper = get_helper(transport);
695
696 write_constant(helper->in, "export\n");
697
698 recvline(data, &buf);
699 if (debug)
700 fprintf(stderr, "Debug: Got export_marks '%s'\n", buf.buf);
701 if (buf.len) {
702 struct strbuf arg = STRBUF_INIT;
703 strbuf_addstr(&arg, "--export-marks=");
704 strbuf_addbuf(&arg, &buf);
705 export_marks = strbuf_detach(&arg, NULL);
706 }
707
708 recvline(data, &buf);
709 if (debug)
710 fprintf(stderr, "Debug: Got import_marks '%s'\n", buf.buf);
711 if (buf.len) {
712 struct strbuf arg = STRBUF_INIT;
713 strbuf_addstr(&arg, "--import-marks=");
714 strbuf_addbuf(&arg, &buf);
715 import_marks = strbuf_detach(&arg, NULL);
716 }
717
718 strbuf_reset(&buf);
719
720 for (ref = remote_refs; ref; ref = ref->next) {
721 char *private;
722 unsigned char sha1[20];
723
724 if (!data->refspecs)
725 continue;
726 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
727 if (private && !get_sha1(private, sha1)) {
728 strbuf_addf(&buf, "^%s", private);
1d2f80fa 729 string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
73b49a75
SR
730 }
731
1d2f80fa 732 string_list_append(&revlist_args, ref->name);
73b49a75
SR
733
734 }
735
736 if (get_exporter(transport, &exporter,
737 export_marks, import_marks, &revlist_args))
738 die("Couldn't run fast-export");
739
740 data->no_disconnect_req = 1;
741 finish_command(&exporter);
742 disconnect_helper(transport);
743 return 0;
744}
745
746static int push_refs(struct transport *transport,
747 struct ref *remote_refs, int flags)
748{
749 struct helper_data *data = transport->data;
750
751 if (process_connect(transport, 1)) {
752 do_take_over(transport);
753 return transport->push_refs(transport, remote_refs, flags);
754 }
755
756 if (!remote_refs) {
757 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
758 "Perhaps you should specify a branch such as 'master'.\n");
759 return 0;
760 }
761
762 if (data->push)
763 return push_refs_with_push(transport, remote_refs, flags);
764
765 if (data->export)
766 return push_refs_with_export(transport, remote_refs, flags);
767
768 return -1;
769}
770
771
f8ec9167
DB
772static int has_attribute(const char *attrs, const char *attr) {
773 int len;
774 if (!attrs)
775 return 0;
776
777 len = strlen(attr);
778 for (;;) {
779 const char *space = strchrnul(attrs, ' ');
780 if (len == space - attrs && !strncmp(attrs, attr, len))
781 return 1;
782 if (!*space)
783 return 0;
784 attrs = space + 1;
785 }
786}
787
6eb996b5
DB
788static struct ref *get_refs_list(struct transport *transport, int for_push)
789{
292ce46b 790 struct helper_data *data = transport->data;
6eb996b5
DB
791 struct child_process *helper;
792 struct ref *ret = NULL;
793 struct ref **tail = &ret;
794 struct ref *posn;
795 struct strbuf buf = STRBUF_INIT;
6eb996b5
DB
796
797 helper = get_helper(transport);
2d14d65c 798
fa8c097c
IL
799 if (process_connect(transport, for_push)) {
800 do_take_over(transport);
801 return transport->get_refs_list(transport, for_push);
802 }
803
ae4efe19
SP
804 if (data->push && for_push)
805 write_str_in_full(helper->in, "list for-push\n");
806 else
807 write_str_in_full(helper->in, "list\n");
6eb996b5 808
6eb996b5
DB
809 while (1) {
810 char *eov, *eon;
bf3c523c 811 recvline(data, &buf);
6eb996b5
DB
812
813 if (!*buf.buf)
814 break;
815
816 eov = strchr(buf.buf, ' ');
817 if (!eov)
818 die("Malformed response in ref list: %s", buf.buf);
819 eon = strchr(eov + 1, ' ');
820 *eov = '\0';
821 if (eon)
822 *eon = '\0';
823 *tail = alloc_ref(eov + 1);
824 if (buf.buf[0] == '@')
825 (*tail)->symref = xstrdup(buf.buf + 1);
826 else if (buf.buf[0] != '?')
827 get_sha1_hex(buf.buf, (*tail)->old_sha1);
f8ec9167
DB
828 if (eon) {
829 if (has_attribute(eon + 1, "unchanged")) {
830 (*tail)->status |= REF_STATUS_UPTODATE;
831 read_ref((*tail)->name, (*tail)->old_sha1);
832 }
833 }
6eb996b5
DB
834 tail = &((*tail)->next);
835 }
bf3c523c
IL
836 if (debug)
837 fprintf(stderr, "Debug: Read ref listing.\n");
6eb996b5
DB
838 strbuf_release(&buf);
839
840 for (posn = ret; posn; posn = posn->next)
841 resolve_remote_symref(posn, ret);
842
843 return ret;
844}
845
c9e388bb 846int transport_helper_init(struct transport *transport, const char *name)
6eb996b5
DB
847{
848 struct helper_data *data = xcalloc(sizeof(*data), 1);
c9e388bb 849 data->name = name;
6eb996b5 850
bf3c523c
IL
851 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
852 debug = 1;
853
6eb996b5 854 transport->data = data;
ef08ef9e 855 transport->set_option = set_helper_option;
6eb996b5
DB
856 transport->get_refs_list = get_refs_list;
857 transport->fetch = fetch;
ae4efe19 858 transport->push_refs = push_refs;
f2a37151 859 transport->disconnect = release_helper;
b236752a 860 transport->connect = connect_helper;
61b075bd 861 transport->smart_options = &(data->transport_options);
6eb996b5
DB
862 return 0;
863}
419f37db
IL
864
865/*
866 * Linux pipes can buffer 65536 bytes at once (and most platforms can
867 * buffer less), so attempt reads and writes with up to that size.
868 */
869#define BUFFERSIZE 65536
870/* This should be enough to hold debugging message. */
871#define PBUFFERSIZE 8192
872
873/* Print bidirectional transfer loop debug message. */
874static void transfer_debug(const char *fmt, ...)
875{
876 va_list args;
877 char msgbuf[PBUFFERSIZE];
878 static int debug_enabled = -1;
879
880 if (debug_enabled < 0)
881 debug_enabled = getenv("GIT_TRANSLOOP_DEBUG") ? 1 : 0;
882 if (!debug_enabled)
883 return;
884
885 va_start(args, fmt);
886 vsnprintf(msgbuf, PBUFFERSIZE, fmt, args);
887 va_end(args);
888 fprintf(stderr, "Transfer loop debugging: %s\n", msgbuf);
889}
890
891/* Stream state: More data may be coming in this direction. */
892#define SSTATE_TRANSFERING 0
893/*
894 * Stream state: No more data coming in this direction, flushing rest of
895 * data.
896 */
897#define SSTATE_FLUSHING 1
898/* Stream state: Transfer in this direction finished. */
899#define SSTATE_FINISHED 2
900
901#define STATE_NEEDS_READING(state) ((state) <= SSTATE_TRANSFERING)
902#define STATE_NEEDS_WRITING(state) ((state) <= SSTATE_FLUSHING)
903#define STATE_NEEDS_CLOSING(state) ((state) == SSTATE_FLUSHING)
904
905/* Unidirectional transfer. */
906struct unidirectional_transfer {
907 /* Source */
908 int src;
909 /* Destination */
910 int dest;
911 /* Is source socket? */
912 int src_is_sock;
913 /* Is destination socket? */
914 int dest_is_sock;
915 /* Transfer state (TRANSFERING/FLUSHING/FINISHED) */
916 int state;
917 /* Buffer. */
918 char buf[BUFFERSIZE];
919 /* Buffer used. */
920 size_t bufuse;
921 /* Name of source. */
922 const char *src_name;
923 /* Name of destination. */
924 const char *dest_name;
925};
926
927/* Closes the target (for writing) if transfer has finished. */
928static void udt_close_if_finished(struct unidirectional_transfer *t)
929{
930 if (STATE_NEEDS_CLOSING(t->state) && !t->bufuse) {
931 t->state = SSTATE_FINISHED;
932 if (t->dest_is_sock)
933 shutdown(t->dest, SHUT_WR);
934 else
935 close(t->dest);
936 transfer_debug("Closed %s.", t->dest_name);
937 }
938}
939
940/*
941 * Tries to read read data from source into buffer. If buffer is full,
942 * no data is read. Returns 0 on success, -1 on error.
943 */
944static int udt_do_read(struct unidirectional_transfer *t)
945{
946 ssize_t bytes;
947
948 if (t->bufuse == BUFFERSIZE)
949 return 0; /* No space for more. */
950
951 transfer_debug("%s is readable", t->src_name);
952 bytes = read(t->src, t->buf + t->bufuse, BUFFERSIZE - t->bufuse);
953 if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
954 errno != EINTR) {
955 error("read(%s) failed: %s", t->src_name, strerror(errno));
956 return -1;
957 } else if (bytes == 0) {
958 transfer_debug("%s EOF (with %i bytes in buffer)",
959 t->src_name, t->bufuse);
960 t->state = SSTATE_FLUSHING;
961 } else if (bytes > 0) {
962 t->bufuse += bytes;
963 transfer_debug("Read %i bytes from %s (buffer now at %i)",
964 (int)bytes, t->src_name, (int)t->bufuse);
965 }
966 return 0;
967}
968
969/* Tries to write data from buffer into destination. If buffer is empty,
970 * no data is written. Returns 0 on success, -1 on error.
971 */
972static int udt_do_write(struct unidirectional_transfer *t)
973{
803dbdb9 974 ssize_t bytes;
419f37db
IL
975
976 if (t->bufuse == 0)
977 return 0; /* Nothing to write. */
978
979 transfer_debug("%s is writable", t->dest_name);
980 bytes = write(t->dest, t->buf, t->bufuse);
981 if (bytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN &&
982 errno != EINTR) {
983 error("write(%s) failed: %s", t->dest_name, strerror(errno));
984 return -1;
985 } else if (bytes > 0) {
986 t->bufuse -= bytes;
987 if (t->bufuse)
988 memmove(t->buf, t->buf + bytes, t->bufuse);
989 transfer_debug("Wrote %i bytes to %s (buffer now at %i)",
990 (int)bytes, t->dest_name, (int)t->bufuse);
991 }
992 return 0;
993}
994
995
996/* State of bidirectional transfer loop. */
997struct bidirectional_transfer_state {
998 /* Direction from program to git. */
999 struct unidirectional_transfer ptg;
1000 /* Direction from git to program. */
1001 struct unidirectional_transfer gtp;
1002};
1003
1004static void *udt_copy_task_routine(void *udt)
1005{
1006 struct unidirectional_transfer *t = (struct unidirectional_transfer *)udt;
1007 while (t->state != SSTATE_FINISHED) {
1008 if (STATE_NEEDS_READING(t->state))
1009 if (udt_do_read(t))
1010 return NULL;
1011 if (STATE_NEEDS_WRITING(t->state))
1012 if (udt_do_write(t))
1013 return NULL;
1014 if (STATE_NEEDS_CLOSING(t->state))
1015 udt_close_if_finished(t);
1016 }
1017 return udt; /* Just some non-NULL value. */
1018}
1019
1020#ifndef NO_PTHREADS
1021
1022/*
1023 * Join thread, with apporiate errors on failure. Name is name for the
1024 * thread (for error messages). Returns 0 on success, 1 on failure.
1025 */
1026static int tloop_join(pthread_t thread, const char *name)
1027{
1028 int err;
1029 void *tret;
1030 err = pthread_join(thread, &tret);
1031 if (!tret) {
1032 error("%s thread failed", name);
1033 return 1;
1034 }
1035 if (err) {
1036 error("%s thread failed to join: %s", name, strerror(err));
1037 return 1;
1038 }
1039 return 0;
1040}
1041
1042/*
1043 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1044 * -1 on failure.
1045 */
1046static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1047{
1048 pthread_t gtp_thread;
1049 pthread_t ptg_thread;
1050 int err;
1051 int ret = 0;
1052 err = pthread_create(&gtp_thread, NULL, udt_copy_task_routine,
1053 &s->gtp);
1054 if (err)
1055 die("Can't start thread for copying data: %s", strerror(err));
1056 err = pthread_create(&ptg_thread, NULL, udt_copy_task_routine,
1057 &s->ptg);
1058 if (err)
1059 die("Can't start thread for copying data: %s", strerror(err));
1060
1061 ret |= tloop_join(gtp_thread, "Git to program copy");
1062 ret |= tloop_join(ptg_thread, "Program to git copy");
1063 return ret;
1064}
1065#else
1066
1067/* Close the source and target (for writing) for transfer. */
1068static void udt_kill_transfer(struct unidirectional_transfer *t)
1069{
1070 t->state = SSTATE_FINISHED;
1071 /*
1072 * Socket read end left open isn't a disaster if nobody
1073 * attempts to read from it (mingw compat headers do not
1074 * have SHUT_RD)...
1075 *
1076 * We can't fully close the socket since otherwise gtp
1077 * task would first close the socket it sends data to
1078 * while closing the ptg file descriptors.
1079 */
1080 if (!t->src_is_sock)
1081 close(t->src);
1082 if (t->dest_is_sock)
1083 shutdown(t->dest, SHUT_WR);
1084 else
1085 close(t->dest);
1086}
1087
1088/*
1089 * Join process, with apporiate errors on failure. Name is name for the
1090 * process (for error messages). Returns 0 on success, 1 on failure.
1091 */
1092static int tloop_join(pid_t pid, const char *name)
1093{
1094 int tret;
1095 if (waitpid(pid, &tret, 0) < 0) {
1096 error("%s process failed to wait: %s", name, strerror(errno));
1097 return 1;
1098 }
1099 if (!WIFEXITED(tret) || WEXITSTATUS(tret)) {
1100 error("%s process failed", name);
1101 return 1;
1102 }
1103 return 0;
1104}
1105
1106/*
1107 * Spawn the transfer tasks and then wait for them. Returns 0 on success,
1108 * -1 on failure.
1109 */
1110static int tloop_spawnwait_tasks(struct bidirectional_transfer_state *s)
1111{
1112 pid_t pid1, pid2;
1113 int ret = 0;
1114
1115 /* Fork thread #1: git to program. */
1116 pid1 = fork();
1117 if (pid1 < 0)
1118 die_errno("Can't start thread for copying data");
1119 else if (pid1 == 0) {
1120 udt_kill_transfer(&s->ptg);
1121 exit(udt_copy_task_routine(&s->gtp) ? 0 : 1);
1122 }
1123
1124 /* Fork thread #2: program to git. */
1125 pid2 = fork();
1126 if (pid2 < 0)
1127 die_errno("Can't start thread for copying data");
1128 else if (pid2 == 0) {
1129 udt_kill_transfer(&s->gtp);
1130 exit(udt_copy_task_routine(&s->ptg) ? 0 : 1);
1131 }
1132
1133 /*
1134 * Close both streams in parent as to not interfere with
1135 * end of file detection and wait for both tasks to finish.
1136 */
1137 udt_kill_transfer(&s->gtp);
1138 udt_kill_transfer(&s->ptg);
1139 ret |= tloop_join(pid1, "Git to program copy");
1140 ret |= tloop_join(pid2, "Program to git copy");
1141 return ret;
1142}
1143#endif
1144
1145/*
1146 * Copies data from stdin to output and from input to stdout simultaneously.
1147 * Additionally filtering through given filter. If filter is NULL, uses
1148 * identity filter.
1149 */
1150int bidirectional_transfer_loop(int input, int output)
1151{
1152 struct bidirectional_transfer_state state;
1153
1154 /* Fill the state fields. */
1155 state.ptg.src = input;
1156 state.ptg.dest = 1;
1157 state.ptg.src_is_sock = (input == output);
1158 state.ptg.dest_is_sock = 0;
1159 state.ptg.state = SSTATE_TRANSFERING;
1160 state.ptg.bufuse = 0;
1161 state.ptg.src_name = "remote input";
1162 state.ptg.dest_name = "stdout";
1163
1164 state.gtp.src = 0;
1165 state.gtp.dest = output;
1166 state.gtp.src_is_sock = 0;
1167 state.gtp.dest_is_sock = (input == output);
1168 state.gtp.state = SSTATE_TRANSFERING;
1169 state.gtp.bufuse = 0;
1170 state.gtp.src_name = "stdin";
1171 state.gtp.dest_name = "remote output";
1172
1173 return tloop_spawnwait_tasks(&state);
1174}