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