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