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