Merge branch 'rb/maint-python-path' into maint
[git/jnareb-git.git] / transport-helper.c
blobf822972020136b0fa04a95d7fcf26df40c81cfe6
1 #include "cache.h"
2 #include "transport.h"
3 #include "quote.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "quote.h"
9 #include "remote.h"
11 static int debug;
13 struct helper_data
15 const char *name;
16 struct child_process *helper;
17 FILE *out;
18 unsigned fetch : 1,
19 import : 1,
20 option : 1,
21 push : 1,
22 connect : 1,
23 no_disconnect_req : 1;
24 /* These go from remote name (as in "list") to private name */
25 struct refspec *refspecs;
26 int refspec_nr;
27 /* Transport options for fetch-pack/send-pack (should one of
28 * those be invoked).
30 struct git_transport_options transport_options;
33 static void sendline(struct helper_data *helper, struct strbuf *buffer)
35 if (debug)
36 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
37 if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
38 != buffer->len)
39 die_errno("Full write to remote helper failed");
42 static int recvline_fh(FILE *helper, struct strbuf *buffer)
44 strbuf_reset(buffer);
45 if (debug)
46 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
47 if (strbuf_getline(buffer, helper, '\n') == EOF) {
48 if (debug)
49 fprintf(stderr, "Debug: Remote helper quit.\n");
50 exit(128);
53 if (debug)
54 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
55 return 0;
58 static int recvline(struct helper_data *helper, struct strbuf *buffer)
60 return recvline_fh(helper->out, buffer);
63 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
65 sendline(helper, buffer);
66 recvline(helper, buffer);
69 static void write_constant(int fd, const char *str)
71 if (debug)
72 fprintf(stderr, "Debug: Remote helper: -> %s", str);
73 if (write_in_full(fd, str, strlen(str)) != strlen(str))
74 die_errno("Full write to remote helper failed");
77 const char *remove_ext_force(const char *url)
79 if (url) {
80 const char *colon = strchr(url, ':');
81 if (colon && colon[1] == ':')
82 return colon + 2;
84 return url;
87 static void do_take_over(struct transport *transport)
89 struct helper_data *data;
90 data = (struct helper_data *)transport->data;
91 transport_take_over(transport, data->helper);
92 fclose(data->out);
93 free(data);
96 static struct child_process *get_helper(struct transport *transport)
98 struct helper_data *data = transport->data;
99 struct strbuf buf = STRBUF_INIT;
100 struct child_process *helper;
101 const char **refspecs = NULL;
102 int refspec_nr = 0;
103 int refspec_alloc = 0;
104 int duped;
105 int code;
107 if (data->helper)
108 return data->helper;
110 helper = xcalloc(1, sizeof(*helper));
111 helper->in = -1;
112 helper->out = -1;
113 helper->err = 0;
114 helper->argv = xcalloc(4, sizeof(*helper->argv));
115 strbuf_addf(&buf, "git-remote-%s", data->name);
116 helper->argv[0] = strbuf_detach(&buf, NULL);
117 helper->argv[1] = transport->remote->name;
118 helper->argv[2] = remove_ext_force(transport->url);
119 helper->git_cmd = 0;
120 helper->silent_exec_failure = 1;
121 code = start_command(helper);
122 if (code < 0 && errno == ENOENT)
123 die("Unable to find remote helper for '%s'", data->name);
124 else if (code != 0)
125 exit(code);
127 data->helper = helper;
128 data->no_disconnect_req = 0;
131 * Open the output as FILE* so strbuf_getline() can be used.
132 * Do this with duped fd because fclose() will close the fd,
133 * and stuff like taking over will require the fd to remain.
135 duped = dup(helper->out);
136 if (duped < 0)
137 die_errno("Can't dup helper output fd");
138 data->out = xfdopen(duped, "r");
140 write_constant(helper->in, "capabilities\n");
142 while (1) {
143 const char *capname;
144 int mandatory = 0;
145 recvline(data, &buf);
147 if (!*buf.buf)
148 break;
150 if (*buf.buf == '*') {
151 capname = buf.buf + 1;
152 mandatory = 1;
153 } else
154 capname = buf.buf;
156 if (debug)
157 fprintf(stderr, "Debug: Got cap %s\n", capname);
158 if (!strcmp(capname, "fetch"))
159 data->fetch = 1;
160 else if (!strcmp(capname, "option"))
161 data->option = 1;
162 else if (!strcmp(capname, "push"))
163 data->push = 1;
164 else if (!strcmp(capname, "import"))
165 data->import = 1;
166 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
167 ALLOC_GROW(refspecs,
168 refspec_nr + 1,
169 refspec_alloc);
170 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
171 } else if (!strcmp(capname, "connect")) {
172 data->connect = 1;
173 } else if (mandatory) {
174 die("Unknown mandatory capability %s. This remote "
175 "helper probably needs newer version of Git.\n",
176 capname);
179 if (refspecs) {
180 int i;
181 data->refspec_nr = refspec_nr;
182 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
183 for (i = 0; i < refspec_nr; i++) {
184 free((char *)refspecs[i]);
186 free(refspecs);
188 strbuf_release(&buf);
189 if (debug)
190 fprintf(stderr, "Debug: Capabilities complete.\n");
191 return data->helper;
194 static int disconnect_helper(struct transport *transport)
196 struct helper_data *data = transport->data;
197 struct strbuf buf = STRBUF_INIT;
199 if (data->helper) {
200 if (debug)
201 fprintf(stderr, "Debug: Disconnecting.\n");
202 if (!data->no_disconnect_req) {
203 strbuf_addf(&buf, "\n");
204 sendline(data, &buf);
206 close(data->helper->in);
207 close(data->helper->out);
208 fclose(data->out);
209 finish_command(data->helper);
210 free((char *)data->helper->argv[0]);
211 free(data->helper->argv);
212 free(data->helper);
213 data->helper = NULL;
215 return 0;
218 static const char *unsupported_options[] = {
219 TRANS_OPT_UPLOADPACK,
220 TRANS_OPT_RECEIVEPACK,
221 TRANS_OPT_THIN,
222 TRANS_OPT_KEEP
224 static const char *boolean_options[] = {
225 TRANS_OPT_THIN,
226 TRANS_OPT_KEEP,
227 TRANS_OPT_FOLLOWTAGS
230 static int set_helper_option(struct transport *transport,
231 const char *name, const char *value)
233 struct helper_data *data = transport->data;
234 struct strbuf buf = STRBUF_INIT;
235 int i, ret, is_bool = 0;
237 get_helper(transport);
239 if (!data->option)
240 return 1;
242 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
243 if (!strcmp(name, unsupported_options[i]))
244 return 1;
247 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
248 if (!strcmp(name, boolean_options[i])) {
249 is_bool = 1;
250 break;
254 strbuf_addf(&buf, "option %s ", name);
255 if (is_bool)
256 strbuf_addstr(&buf, value ? "true" : "false");
257 else
258 quote_c_style(value, &buf, NULL, 0);
259 strbuf_addch(&buf, '\n');
261 xchgline(data, &buf);
263 if (!strcmp(buf.buf, "ok"))
264 ret = 0;
265 else if (!prefixcmp(buf.buf, "error")) {
266 ret = -1;
267 } else if (!strcmp(buf.buf, "unsupported"))
268 ret = 1;
269 else {
270 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
271 ret = 1;
273 strbuf_release(&buf);
274 return ret;
277 static void standard_options(struct transport *t)
279 char buf[16];
280 int n;
281 int v = t->verbose;
282 int no_progress = v < 0 || (!t->progress && !isatty(2));
284 set_helper_option(t, "progress", !no_progress ? "true" : "false");
286 n = snprintf(buf, sizeof(buf), "%d", v + 1);
287 if (n >= sizeof(buf))
288 die("impossibly large verbosity value");
289 set_helper_option(t, "verbosity", buf);
292 static int release_helper(struct transport *transport)
294 struct helper_data *data = transport->data;
295 free_refspec(data->refspec_nr, data->refspecs);
296 data->refspecs = NULL;
297 disconnect_helper(transport);
298 free(transport->data);
299 return 0;
302 static int fetch_with_fetch(struct transport *transport,
303 int nr_heads, struct ref **to_fetch)
305 struct helper_data *data = transport->data;
306 int i;
307 struct strbuf buf = STRBUF_INIT;
309 standard_options(transport);
311 for (i = 0; i < nr_heads; i++) {
312 const struct ref *posn = to_fetch[i];
313 if (posn->status & REF_STATUS_UPTODATE)
314 continue;
316 strbuf_addf(&buf, "fetch %s %s\n",
317 sha1_to_hex(posn->old_sha1), posn->name);
320 strbuf_addch(&buf, '\n');
321 sendline(data, &buf);
323 while (1) {
324 recvline(data, &buf);
326 if (!prefixcmp(buf.buf, "lock ")) {
327 const char *name = buf.buf + 5;
328 if (transport->pack_lockfile)
329 warning("%s also locked %s", data->name, name);
330 else
331 transport->pack_lockfile = xstrdup(name);
333 else if (!buf.len)
334 break;
335 else
336 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
338 strbuf_release(&buf);
339 return 0;
342 static int get_importer(struct transport *transport, struct child_process *fastimport)
344 struct child_process *helper = get_helper(transport);
345 memset(fastimport, 0, sizeof(*fastimport));
346 fastimport->in = helper->out;
347 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
348 fastimport->argv[0] = "fast-import";
349 fastimport->argv[1] = "--quiet";
351 fastimport->git_cmd = 1;
352 return start_command(fastimport);
355 static int fetch_with_import(struct transport *transport,
356 int nr_heads, struct ref **to_fetch)
358 struct child_process fastimport;
359 struct helper_data *data = transport->data;
360 int i;
361 struct ref *posn;
362 struct strbuf buf = STRBUF_INIT;
364 get_helper(transport);
366 if (get_importer(transport, &fastimport))
367 die("Couldn't run fast-import");
369 for (i = 0; i < nr_heads; i++) {
370 posn = to_fetch[i];
371 if (posn->status & REF_STATUS_UPTODATE)
372 continue;
374 strbuf_addf(&buf, "import %s\n", posn->name);
375 sendline(data, &buf);
376 strbuf_reset(&buf);
378 disconnect_helper(transport);
379 finish_command(&fastimport);
380 free(fastimport.argv);
381 fastimport.argv = NULL;
383 for (i = 0; i < nr_heads; i++) {
384 char *private;
385 posn = to_fetch[i];
386 if (posn->status & REF_STATUS_UPTODATE)
387 continue;
388 if (data->refspecs)
389 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
390 else
391 private = strdup(posn->name);
392 read_ref(private, posn->old_sha1);
393 free(private);
395 strbuf_release(&buf);
396 return 0;
399 static int process_connect_service(struct transport *transport,
400 const char *name, const char *exec)
402 struct helper_data *data = transport->data;
403 struct strbuf cmdbuf = STRBUF_INIT;
404 struct child_process *helper;
405 int r, duped, ret = 0;
406 FILE *input;
408 helper = get_helper(transport);
411 * Yes, dup the pipe another time, as we need unbuffered version
412 * of input pipe as FILE*. fclose() closes the underlying fd and
413 * stream buffering only can be changed before first I/O operation
414 * on it.
416 duped = dup(helper->out);
417 if (duped < 0)
418 die_errno("Can't dup helper output fd");
419 input = xfdopen(duped, "r");
420 setvbuf(input, NULL, _IONBF, 0);
423 * Handle --upload-pack and friends. This is fire and forget...
424 * just warn if it fails.
426 if (strcmp(name, exec)) {
427 r = set_helper_option(transport, "servpath", exec);
428 if (r > 0)
429 warning("Setting remote service path not supported by protocol.");
430 else if (r < 0)
431 warning("Invalid remote service path.");
434 if (data->connect)
435 strbuf_addf(&cmdbuf, "connect %s\n", name);
436 else
437 goto exit;
439 sendline(data, &cmdbuf);
440 recvline_fh(input, &cmdbuf);
441 if (!strcmp(cmdbuf.buf, "")) {
442 data->no_disconnect_req = 1;
443 if (debug)
444 fprintf(stderr, "Debug: Smart transport connection "
445 "ready.\n");
446 ret = 1;
447 } else if (!strcmp(cmdbuf.buf, "fallback")) {
448 if (debug)
449 fprintf(stderr, "Debug: Falling back to dumb "
450 "transport.\n");
451 } else
452 die("Unknown response to connect: %s",
453 cmdbuf.buf);
455 exit:
456 fclose(input);
457 return ret;
460 static int process_connect(struct transport *transport,
461 int for_push)
463 struct helper_data *data = transport->data;
464 const char *name;
465 const char *exec;
467 name = for_push ? "git-receive-pack" : "git-upload-pack";
468 if (for_push)
469 exec = data->transport_options.receivepack;
470 else
471 exec = data->transport_options.uploadpack;
473 return process_connect_service(transport, name, exec);
476 static int connect_helper(struct transport *transport, const char *name,
477 const char *exec, int fd[2])
479 struct helper_data *data = transport->data;
481 /* Get_helper so connect is inited. */
482 get_helper(transport);
483 if (!data->connect)
484 die("Operation not supported by protocol.");
486 if (!process_connect_service(transport, name, exec))
487 die("Can't connect to subservice %s.", name);
489 fd[0] = data->helper->out;
490 fd[1] = data->helper->in;
491 return 0;
494 static int fetch(struct transport *transport,
495 int nr_heads, struct ref **to_fetch)
497 struct helper_data *data = transport->data;
498 int i, count;
500 if (process_connect(transport, 0)) {
501 do_take_over(transport);
502 return transport->fetch(transport, nr_heads, to_fetch);
505 count = 0;
506 for (i = 0; i < nr_heads; i++)
507 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
508 count++;
510 if (!count)
511 return 0;
513 if (data->fetch)
514 return fetch_with_fetch(transport, nr_heads, to_fetch);
516 if (data->import)
517 return fetch_with_import(transport, nr_heads, to_fetch);
519 return -1;
522 static int push_refs(struct transport *transport,
523 struct ref *remote_refs, int flags)
525 int force_all = flags & TRANSPORT_PUSH_FORCE;
526 int mirror = flags & TRANSPORT_PUSH_MIRROR;
527 struct helper_data *data = transport->data;
528 struct strbuf buf = STRBUF_INIT;
529 struct child_process *helper;
530 struct ref *ref;
532 if (process_connect(transport, 1)) {
533 do_take_over(transport);
534 return transport->push_refs(transport, remote_refs, flags);
537 if (!remote_refs) {
538 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
539 "Perhaps you should specify a branch such as 'master'.\n");
540 return 0;
543 helper = get_helper(transport);
544 if (!data->push)
545 return 1;
547 for (ref = remote_refs; ref; ref = ref->next) {
548 if (!ref->peer_ref && !mirror)
549 continue;
551 /* Check for statuses set by set_ref_status_for_push() */
552 switch (ref->status) {
553 case REF_STATUS_REJECT_NONFASTFORWARD:
554 case REF_STATUS_UPTODATE:
555 continue;
556 default:
557 ; /* do nothing */
560 if (force_all)
561 ref->force = 1;
563 strbuf_addstr(&buf, "push ");
564 if (!ref->deletion) {
565 if (ref->force)
566 strbuf_addch(&buf, '+');
567 if (ref->peer_ref)
568 strbuf_addstr(&buf, ref->peer_ref->name);
569 else
570 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
572 strbuf_addch(&buf, ':');
573 strbuf_addstr(&buf, ref->name);
574 strbuf_addch(&buf, '\n');
576 if (buf.len == 0)
577 return 0;
579 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
580 standard_options(transport);
582 if (flags & TRANSPORT_PUSH_DRY_RUN) {
583 if (set_helper_option(transport, "dry-run", "true") != 0)
584 die("helper %s does not support dry-run", data->name);
587 strbuf_addch(&buf, '\n');
588 sendline(data, &buf);
590 ref = remote_refs;
591 while (1) {
592 char *refname, *msg;
593 int status;
595 recvline(data, &buf);
596 if (!buf.len)
597 break;
599 if (!prefixcmp(buf.buf, "ok ")) {
600 status = REF_STATUS_OK;
601 refname = buf.buf + 3;
602 } else if (!prefixcmp(buf.buf, "error ")) {
603 status = REF_STATUS_REMOTE_REJECT;
604 refname = buf.buf + 6;
605 } else
606 die("expected ok/error, helper said '%s'\n", buf.buf);
608 msg = strchr(refname, ' ');
609 if (msg) {
610 struct strbuf msg_buf = STRBUF_INIT;
611 const char *end;
613 *msg++ = '\0';
614 if (!unquote_c_style(&msg_buf, msg, &end))
615 msg = strbuf_detach(&msg_buf, NULL);
616 else
617 msg = xstrdup(msg);
618 strbuf_release(&msg_buf);
620 if (!strcmp(msg, "no match")) {
621 status = REF_STATUS_NONE;
622 free(msg);
623 msg = NULL;
625 else if (!strcmp(msg, "up to date")) {
626 status = REF_STATUS_UPTODATE;
627 free(msg);
628 msg = NULL;
630 else if (!strcmp(msg, "non-fast forward")) {
631 status = REF_STATUS_REJECT_NONFASTFORWARD;
632 free(msg);
633 msg = NULL;
637 if (ref)
638 ref = find_ref_by_name(ref, refname);
639 if (!ref)
640 ref = find_ref_by_name(remote_refs, refname);
641 if (!ref) {
642 warning("helper reported unexpected status of %s", refname);
643 continue;
646 if (ref->status != REF_STATUS_NONE) {
648 * Earlier, the ref was marked not to be pushed, so ignore the ref
649 * status reported by the remote helper if the latter is 'no match'.
651 if (status == REF_STATUS_NONE)
652 continue;
655 ref->status = status;
656 ref->remote_status = msg;
658 strbuf_release(&buf);
659 return 0;
662 static int has_attribute(const char *attrs, const char *attr) {
663 int len;
664 if (!attrs)
665 return 0;
667 len = strlen(attr);
668 for (;;) {
669 const char *space = strchrnul(attrs, ' ');
670 if (len == space - attrs && !strncmp(attrs, attr, len))
671 return 1;
672 if (!*space)
673 return 0;
674 attrs = space + 1;
678 static struct ref *get_refs_list(struct transport *transport, int for_push)
680 struct helper_data *data = transport->data;
681 struct child_process *helper;
682 struct ref *ret = NULL;
683 struct ref **tail = &ret;
684 struct ref *posn;
685 struct strbuf buf = STRBUF_INIT;
687 helper = get_helper(transport);
689 if (process_connect(transport, for_push)) {
690 do_take_over(transport);
691 return transport->get_refs_list(transport, for_push);
694 if (data->push && for_push)
695 write_str_in_full(helper->in, "list for-push\n");
696 else
697 write_str_in_full(helper->in, "list\n");
699 while (1) {
700 char *eov, *eon;
701 recvline(data, &buf);
703 if (!*buf.buf)
704 break;
706 eov = strchr(buf.buf, ' ');
707 if (!eov)
708 die("Malformed response in ref list: %s", buf.buf);
709 eon = strchr(eov + 1, ' ');
710 *eov = '\0';
711 if (eon)
712 *eon = '\0';
713 *tail = alloc_ref(eov + 1);
714 if (buf.buf[0] == '@')
715 (*tail)->symref = xstrdup(buf.buf + 1);
716 else if (buf.buf[0] != '?')
717 get_sha1_hex(buf.buf, (*tail)->old_sha1);
718 if (eon) {
719 if (has_attribute(eon + 1, "unchanged")) {
720 (*tail)->status |= REF_STATUS_UPTODATE;
721 read_ref((*tail)->name, (*tail)->old_sha1);
724 tail = &((*tail)->next);
726 if (debug)
727 fprintf(stderr, "Debug: Read ref listing.\n");
728 strbuf_release(&buf);
730 for (posn = ret; posn; posn = posn->next)
731 resolve_remote_symref(posn, ret);
733 return ret;
736 int transport_helper_init(struct transport *transport, const char *name)
738 struct helper_data *data = xcalloc(sizeof(*data), 1);
739 data->name = name;
741 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
742 debug = 1;
744 transport->data = data;
745 transport->set_option = set_helper_option;
746 transport->get_refs_list = get_refs_list;
747 transport->fetch = fetch;
748 transport->push_refs = push_refs;
749 transport->disconnect = release_helper;
750 transport->connect = connect_helper;
751 transport->smart_options = &(data->transport_options);
752 return 0;