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