remote-helpers: allow requesing the path to the .git directory
[git/dscho.git] / transport-helper.c
blobc8705b78d640e87d686fba6d35c30a18646d83f8
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 (!strcmp(buf.buf, "gitdir")) {
174 struct strbuf gitdir = STRBUF_INIT;
175 strbuf_addf(&gitdir, "gitdir %s\n", get_git_dir());
176 sendline(data, &gitdir);
177 strbuf_release(&gitdir);
178 } else if (mandatory) {
179 die("Unknown mandatory capability %s. This remote "
180 "helper probably needs newer version of Git.\n",
181 capname);
184 if (refspecs) {
185 int i;
186 data->refspec_nr = refspec_nr;
187 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
188 for (i = 0; i < refspec_nr; i++) {
189 free((char *)refspecs[i]);
191 free(refspecs);
193 strbuf_release(&buf);
194 if (debug)
195 fprintf(stderr, "Debug: Capabilities complete.\n");
196 return data->helper;
199 static int disconnect_helper(struct transport *transport)
201 struct helper_data *data = transport->data;
202 struct strbuf buf = STRBUF_INIT;
204 if (data->helper) {
205 if (debug)
206 fprintf(stderr, "Debug: Disconnecting.\n");
207 if (!data->no_disconnect_req) {
208 strbuf_addf(&buf, "\n");
209 sendline(data, &buf);
211 close(data->helper->in);
212 close(data->helper->out);
213 fclose(data->out);
214 finish_command(data->helper);
215 free((char *)data->helper->argv[0]);
216 free(data->helper->argv);
217 free(data->helper);
218 data->helper = NULL;
220 return 0;
223 static const char *unsupported_options[] = {
224 TRANS_OPT_UPLOADPACK,
225 TRANS_OPT_RECEIVEPACK,
226 TRANS_OPT_THIN,
227 TRANS_OPT_KEEP
229 static const char *boolean_options[] = {
230 TRANS_OPT_THIN,
231 TRANS_OPT_KEEP,
232 TRANS_OPT_FOLLOWTAGS
235 static int set_helper_option(struct transport *transport,
236 const char *name, const char *value)
238 struct helper_data *data = transport->data;
239 struct strbuf buf = STRBUF_INIT;
240 int i, ret, is_bool = 0;
242 get_helper(transport);
244 if (!data->option)
245 return 1;
247 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
248 if (!strcmp(name, unsupported_options[i]))
249 return 1;
252 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
253 if (!strcmp(name, boolean_options[i])) {
254 is_bool = 1;
255 break;
259 strbuf_addf(&buf, "option %s ", name);
260 if (is_bool)
261 strbuf_addstr(&buf, value ? "true" : "false");
262 else
263 quote_c_style(value, &buf, NULL, 0);
264 strbuf_addch(&buf, '\n');
266 xchgline(data, &buf);
268 if (!strcmp(buf.buf, "ok"))
269 ret = 0;
270 else if (!prefixcmp(buf.buf, "error")) {
271 ret = -1;
272 } else if (!strcmp(buf.buf, "unsupported"))
273 ret = 1;
274 else {
275 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
276 ret = 1;
278 strbuf_release(&buf);
279 return ret;
282 static void standard_options(struct transport *t)
284 char buf[16];
285 int n;
286 int v = t->verbose;
288 set_helper_option(t, "progress", t->progress ? "true" : "false");
290 n = snprintf(buf, sizeof(buf), "%d", v + 1);
291 if (n >= sizeof(buf))
292 die("impossibly large verbosity value");
293 set_helper_option(t, "verbosity", buf);
296 static int release_helper(struct transport *transport)
298 struct helper_data *data = transport->data;
299 free_refspec(data->refspec_nr, data->refspecs);
300 data->refspecs = NULL;
301 disconnect_helper(transport);
302 free(transport->data);
303 return 0;
306 static int fetch_with_fetch(struct transport *transport,
307 int nr_heads, struct ref **to_fetch)
309 struct helper_data *data = transport->data;
310 int i;
311 struct strbuf buf = STRBUF_INIT;
313 standard_options(transport);
315 for (i = 0; i < nr_heads; i++) {
316 const struct ref *posn = to_fetch[i];
317 if (posn->status & REF_STATUS_UPTODATE)
318 continue;
320 strbuf_addf(&buf, "fetch %s %s\n",
321 sha1_to_hex(posn->old_sha1), posn->name);
324 strbuf_addch(&buf, '\n');
325 sendline(data, &buf);
327 while (1) {
328 recvline(data, &buf);
330 if (!prefixcmp(buf.buf, "lock ")) {
331 const char *name = buf.buf + 5;
332 if (transport->pack_lockfile)
333 warning("%s also locked %s", data->name, name);
334 else
335 transport->pack_lockfile = xstrdup(name);
337 else if (!buf.len)
338 break;
339 else
340 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
342 strbuf_release(&buf);
343 return 0;
346 static int get_importer(struct transport *transport, struct child_process *fastimport)
348 struct child_process *helper = get_helper(transport);
349 memset(fastimport, 0, sizeof(*fastimport));
350 fastimport->in = helper->out;
351 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
352 fastimport->argv[0] = "fast-import";
353 fastimport->argv[1] = "--quiet";
355 fastimport->git_cmd = 1;
356 return start_command(fastimport);
359 static int fetch_with_import(struct transport *transport,
360 int nr_heads, struct ref **to_fetch)
362 struct child_process fastimport;
363 struct helper_data *data = transport->data;
364 int i;
365 struct ref *posn;
366 struct strbuf buf = STRBUF_INIT;
368 get_helper(transport);
370 if (get_importer(transport, &fastimport))
371 die("Couldn't run fast-import");
373 for (i = 0; i < nr_heads; i++) {
374 posn = to_fetch[i];
375 if (posn->status & REF_STATUS_UPTODATE)
376 continue;
378 strbuf_addf(&buf, "import %s\n", posn->name);
379 sendline(data, &buf);
380 strbuf_reset(&buf);
382 disconnect_helper(transport);
383 finish_command(&fastimport);
384 free(fastimport.argv);
385 fastimport.argv = NULL;
387 for (i = 0; i < nr_heads; i++) {
388 char *private;
389 posn = to_fetch[i];
390 if (posn->status & REF_STATUS_UPTODATE)
391 continue;
392 if (data->refspecs)
393 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
394 else
395 private = strdup(posn->name);
396 read_ref(private, posn->old_sha1);
397 free(private);
399 strbuf_release(&buf);
400 return 0;
403 static int process_connect_service(struct transport *transport,
404 const char *name, const char *exec)
406 struct helper_data *data = transport->data;
407 struct strbuf cmdbuf = STRBUF_INIT;
408 struct child_process *helper;
409 int r, duped, ret = 0;
410 FILE *input;
412 helper = get_helper(transport);
415 * Yes, dup the pipe another time, as we need unbuffered version
416 * of input pipe as FILE*. fclose() closes the underlying fd and
417 * stream buffering only can be changed before first I/O operation
418 * on it.
420 duped = dup(helper->out);
421 if (duped < 0)
422 die_errno("Can't dup helper output fd");
423 input = xfdopen(duped, "r");
424 setvbuf(input, NULL, _IONBF, 0);
427 * Handle --upload-pack and friends. This is fire and forget...
428 * just warn if it fails.
430 if (strcmp(name, exec)) {
431 r = set_helper_option(transport, "servpath", exec);
432 if (r > 0)
433 warning("Setting remote service path not supported by protocol.");
434 else if (r < 0)
435 warning("Invalid remote service path.");
438 if (data->connect)
439 strbuf_addf(&cmdbuf, "connect %s\n", name);
440 else
441 goto exit;
443 sendline(data, &cmdbuf);
444 recvline_fh(input, &cmdbuf);
445 if (!strcmp(cmdbuf.buf, "")) {
446 data->no_disconnect_req = 1;
447 if (debug)
448 fprintf(stderr, "Debug: Smart transport connection "
449 "ready.\n");
450 ret = 1;
451 } else if (!strcmp(cmdbuf.buf, "fallback")) {
452 if (debug)
453 fprintf(stderr, "Debug: Falling back to dumb "
454 "transport.\n");
455 } else
456 die("Unknown response to connect: %s",
457 cmdbuf.buf);
459 exit:
460 fclose(input);
461 return ret;
464 static int process_connect(struct transport *transport,
465 int for_push)
467 struct helper_data *data = transport->data;
468 const char *name;
469 const char *exec;
471 name = for_push ? "git-receive-pack" : "git-upload-pack";
472 if (for_push)
473 exec = data->transport_options.receivepack;
474 else
475 exec = data->transport_options.uploadpack;
477 return process_connect_service(transport, name, exec);
480 static int connect_helper(struct transport *transport, const char *name,
481 const char *exec, int fd[2])
483 struct helper_data *data = transport->data;
485 /* Get_helper so connect is inited. */
486 get_helper(transport);
487 if (!data->connect)
488 die("Operation not supported by protocol.");
490 if (!process_connect_service(transport, name, exec))
491 die("Can't connect to subservice %s.", name);
493 fd[0] = data->helper->out;
494 fd[1] = data->helper->in;
495 return 0;
498 static int fetch(struct transport *transport,
499 int nr_heads, struct ref **to_fetch)
501 struct helper_data *data = transport->data;
502 int i, count;
504 if (process_connect(transport, 0)) {
505 do_take_over(transport);
506 return transport->fetch(transport, nr_heads, to_fetch);
509 count = 0;
510 for (i = 0; i < nr_heads; i++)
511 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
512 count++;
514 if (!count)
515 return 0;
517 if (data->fetch)
518 return fetch_with_fetch(transport, nr_heads, to_fetch);
520 if (data->import)
521 return fetch_with_import(transport, nr_heads, to_fetch);
523 return -1;
526 static int push_refs(struct transport *transport,
527 struct ref *remote_refs, int flags)
529 int force_all = flags & TRANSPORT_PUSH_FORCE;
530 int mirror = flags & TRANSPORT_PUSH_MIRROR;
531 struct helper_data *data = transport->data;
532 struct strbuf buf = STRBUF_INIT;
533 struct child_process *helper;
534 struct ref *ref;
536 if (process_connect(transport, 1)) {
537 do_take_over(transport);
538 return transport->push_refs(transport, remote_refs, flags);
541 if (!remote_refs) {
542 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
543 "Perhaps you should specify a branch such as 'master'.\n");
544 return 0;
547 helper = get_helper(transport);
548 if (!data->push)
549 return 1;
551 for (ref = remote_refs; ref; ref = ref->next) {
552 if (!ref->peer_ref && !mirror)
553 continue;
555 /* Check for statuses set by set_ref_status_for_push() */
556 switch (ref->status) {
557 case REF_STATUS_REJECT_NONFASTFORWARD:
558 case REF_STATUS_UPTODATE:
559 continue;
560 default:
561 ; /* do nothing */
564 if (force_all)
565 ref->force = 1;
567 strbuf_addstr(&buf, "push ");
568 if (!ref->deletion) {
569 if (ref->force)
570 strbuf_addch(&buf, '+');
571 if (ref->peer_ref)
572 strbuf_addstr(&buf, ref->peer_ref->name);
573 else
574 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
576 strbuf_addch(&buf, ':');
577 strbuf_addstr(&buf, ref->name);
578 strbuf_addch(&buf, '\n');
580 if (buf.len == 0)
581 return 0;
583 standard_options(transport);
585 if (flags & TRANSPORT_PUSH_DRY_RUN) {
586 if (set_helper_option(transport, "dry-run", "true") != 0)
587 die("helper %s does not support dry-run", data->name);
590 strbuf_addch(&buf, '\n');
591 sendline(data, &buf);
593 ref = remote_refs;
594 while (1) {
595 char *refname, *msg;
596 int status;
598 recvline(data, &buf);
599 if (!buf.len)
600 break;
602 if (!prefixcmp(buf.buf, "ok ")) {
603 status = REF_STATUS_OK;
604 refname = buf.buf + 3;
605 } else if (!prefixcmp(buf.buf, "error ")) {
606 status = REF_STATUS_REMOTE_REJECT;
607 refname = buf.buf + 6;
608 } else
609 die("expected ok/error, helper said '%s'\n", buf.buf);
611 msg = strchr(refname, ' ');
612 if (msg) {
613 struct strbuf msg_buf = STRBUF_INIT;
614 const char *end;
616 *msg++ = '\0';
617 if (!unquote_c_style(&msg_buf, msg, &end))
618 msg = strbuf_detach(&msg_buf, NULL);
619 else
620 msg = xstrdup(msg);
621 strbuf_release(&msg_buf);
623 if (!strcmp(msg, "no match")) {
624 status = REF_STATUS_NONE;
625 free(msg);
626 msg = NULL;
628 else if (!strcmp(msg, "up to date")) {
629 status = REF_STATUS_UPTODATE;
630 free(msg);
631 msg = NULL;
633 else if (!strcmp(msg, "non-fast forward")) {
634 status = REF_STATUS_REJECT_NONFASTFORWARD;
635 free(msg);
636 msg = NULL;
640 if (ref)
641 ref = find_ref_by_name(ref, refname);
642 if (!ref)
643 ref = find_ref_by_name(remote_refs, refname);
644 if (!ref) {
645 warning("helper reported unexpected status of %s", refname);
646 continue;
649 if (ref->status != REF_STATUS_NONE) {
651 * Earlier, the ref was marked not to be pushed, so ignore the ref
652 * status reported by the remote helper if the latter is 'no match'.
654 if (status == REF_STATUS_NONE)
655 continue;
658 ref->status = status;
659 ref->remote_status = msg;
661 strbuf_release(&buf);
662 return 0;
665 static int has_attribute(const char *attrs, const char *attr) {
666 int len;
667 if (!attrs)
668 return 0;
670 len = strlen(attr);
671 for (;;) {
672 const char *space = strchrnul(attrs, ' ');
673 if (len == space - attrs && !strncmp(attrs, attr, len))
674 return 1;
675 if (!*space)
676 return 0;
677 attrs = space + 1;
681 static struct ref *get_refs_list(struct transport *transport, int for_push)
683 struct helper_data *data = transport->data;
684 struct child_process *helper;
685 struct ref *ret = NULL;
686 struct ref **tail = &ret;
687 struct ref *posn;
688 struct strbuf buf = STRBUF_INIT;
690 helper = get_helper(transport);
692 if (process_connect(transport, for_push)) {
693 do_take_over(transport);
694 return transport->get_refs_list(transport, for_push);
697 if (data->push && for_push)
698 write_str_in_full(helper->in, "list for-push\n");
699 else
700 write_str_in_full(helper->in, "list\n");
702 while (1) {
703 char *eov, *eon;
704 recvline(data, &buf);
706 if (!*buf.buf)
707 break;
709 eov = strchr(buf.buf, ' ');
710 if (!eov)
711 die("Malformed response in ref list: %s", buf.buf);
712 eon = strchr(eov + 1, ' ');
713 *eov = '\0';
714 if (eon)
715 *eon = '\0';
716 *tail = alloc_ref(eov + 1);
717 if (buf.buf[0] == '@')
718 (*tail)->symref = xstrdup(buf.buf + 1);
719 else if (buf.buf[0] != '?')
720 get_sha1_hex(buf.buf, (*tail)->old_sha1);
721 if (eon) {
722 if (has_attribute(eon + 1, "unchanged")) {
723 (*tail)->status |= REF_STATUS_UPTODATE;
724 read_ref((*tail)->name, (*tail)->old_sha1);
727 tail = &((*tail)->next);
729 if (debug)
730 fprintf(stderr, "Debug: Read ref listing.\n");
731 strbuf_release(&buf);
733 for (posn = ret; posn; posn = posn->next)
734 resolve_remote_symref(posn, ret);
736 return ret;
739 int transport_helper_init(struct transport *transport, const char *name)
741 struct helper_data *data = xcalloc(sizeof(*data), 1);
742 data->name = name;
744 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
745 debug = 1;
747 transport->data = data;
748 transport->set_option = set_helper_option;
749 transport->get_refs_list = get_refs_list;
750 transport->fetch = fetch;
751 transport->push_refs = push_refs;
752 transport->disconnect = release_helper;
753 transport->connect = connect_helper;
754 transport->smart_options = &(data->transport_options);
755 return 0;