4 #include "run-command.h"
10 #include "string-list.h"
17 struct child_process *helper;
25 no_disconnect_req : 1;
28 /* These go from remote name (as in "list") to private name */
29 struct refspec *refspecs;
31 /* Transport options for fetch-pack/send-pack (should one of
34 struct git_transport_options transport_options;
37 static void sendline(struct helper_data *helper, struct strbuf *buffer)
40 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
41 if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
43 die_errno("Full write to remote helper failed");
46 static int recvline_fh(FILE *helper, struct strbuf *buffer)
50 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
51 if (strbuf_getline(buffer, helper, '\n') == EOF) {
53 fprintf(stderr, "Debug: Remote helper quit.\n");
58 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
62 static int recvline(struct helper_data *helper, struct strbuf *buffer)
64 return recvline_fh(helper->out, buffer);
67 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
69 sendline(helper, buffer);
70 recvline(helper, buffer);
73 static void write_constant(int fd, const char *str)
76 fprintf(stderr, "Debug: Remote helper: -> %s", str);
77 if (write_in_full(fd, str, strlen(str)) != strlen(str))
78 die_errno("Full write to remote helper failed");
81 const char *remove_ext_force(const char *url)
84 const char *colon = strchr(url, ':');
85 if (colon && colon[1] == ':')
91 static void do_take_over(struct transport *transport)
93 struct helper_data *data;
94 data = (struct helper_data *)transport->data;
95 transport_take_over(transport, data->helper);
100 static struct child_process *get_helper(struct transport *transport)
102 struct helper_data *data = transport->data;
103 struct strbuf buf = STRBUF_INIT;
104 struct child_process *helper;
105 const char **refspecs = NULL;
107 int refspec_alloc = 0;
114 helper = xcalloc(1, sizeof(*helper));
118 helper->argv = xcalloc(4, sizeof(*helper->argv));
119 strbuf_addf(&buf, "git-remote-%s", data->name);
120 helper->argv[0] = strbuf_detach(&buf, NULL);
121 helper->argv[1] = transport->remote->name;
122 helper->argv[2] = remove_ext_force(transport->url);
124 helper->silent_exec_failure = 1;
125 code = start_command(helper);
126 if (code < 0 && errno == ENOENT)
127 die("Unable to find remote helper for '%s'", data->name);
131 data->helper = helper;
132 data->no_disconnect_req = 0;
135 * Open the output as FILE* so strbuf_getline() can be used.
136 * Do this with duped fd because fclose() will close the fd,
137 * and stuff like taking over will require the fd to remain.
139 duped = dup(helper->out);
141 die_errno("Can't dup helper output fd");
142 data->out = xfdopen(duped, "r");
144 write_constant(helper->in, "capabilities\n");
149 recvline(data, &buf);
154 if (*buf.buf == '*') {
155 capname = buf.buf + 1;
161 fprintf(stderr, "Debug: Got cap %s\n", capname);
162 if (!strcmp(capname, "fetch"))
164 else if (!strcmp(capname, "option"))
166 else if (!strcmp(capname, "push"))
168 else if (!strcmp(capname, "import"))
170 else if (!strcmp(capname, "export"))
172 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
176 refspecs[refspec_nr++] = strdup(capname + strlen("refspec "));
177 } else if (!strcmp(capname, "connect")) {
179 } else if (!strcmp(capname, "gitdir")) {
180 struct strbuf gitdir = STRBUF_INIT;
181 strbuf_addf(&gitdir, "gitdir %s\n", get_git_dir());
182 sendline(data, &gitdir);
183 strbuf_release(&gitdir);
184 } else if (!prefixcmp(capname, "export-marks ")) {
185 struct strbuf arg = STRBUF_INIT;
186 strbuf_addstr(&arg, "--export-marks=");
187 strbuf_addstr(&arg, capname + strlen("export-marks "));
188 data->export_marks = strbuf_detach(&arg, NULL);
189 } else if (!prefixcmp(capname, "import-marks")) {
190 struct strbuf arg = STRBUF_INIT;
191 strbuf_addstr(&arg, "--import-marks=");
192 strbuf_addstr(&arg, capname + strlen("import-marks "));
193 data->import_marks = strbuf_detach(&arg, NULL);
194 } else if (mandatory) {
195 die("Unknown mandatory capability %s. This remote "
196 "helper probably needs newer version of Git.\n",
202 data->refspec_nr = refspec_nr;
203 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
204 for (i = 0; i < refspec_nr; i++) {
205 free((char *)refspecs[i]);
209 strbuf_release(&buf);
211 fprintf(stderr, "Debug: Capabilities complete.\n");
215 static int disconnect_helper(struct transport *transport)
217 struct helper_data *data = transport->data;
218 struct strbuf buf = STRBUF_INIT;
223 fprintf(stderr, "Debug: Disconnecting.\n");
224 if (!data->no_disconnect_req) {
225 strbuf_addf(&buf, "\n");
226 sendline(data, &buf);
228 close(data->helper->in);
229 close(data->helper->out);
231 res = finish_command(data->helper);
232 free((char *)data->helper->argv[0]);
233 free(data->helper->argv);
240 static const char *unsupported_options[] = {
241 TRANS_OPT_UPLOADPACK,
242 TRANS_OPT_RECEIVEPACK,
246 static const char *boolean_options[] = {
252 static int set_helper_option(struct transport *transport,
253 const char *name, const char *value)
255 struct helper_data *data = transport->data;
256 struct strbuf buf = STRBUF_INIT;
257 int i, ret, is_bool = 0;
259 get_helper(transport);
264 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
265 if (!strcmp(name, unsupported_options[i]))
269 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
270 if (!strcmp(name, boolean_options[i])) {
276 strbuf_addf(&buf, "option %s ", name);
278 strbuf_addstr(&buf, value ? "true" : "false");
280 quote_c_style(value, &buf, NULL, 0);
281 strbuf_addch(&buf, '\n');
283 xchgline(data, &buf);
285 if (!strcmp(buf.buf, "ok"))
287 else if (!prefixcmp(buf.buf, "error")) {
289 } else if (!strcmp(buf.buf, "unsupported"))
292 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
295 strbuf_release(&buf);
299 static void standard_options(struct transport *t)
305 set_helper_option(t, "progress", t->progress ? "true" : "false");
307 n = snprintf(buf, sizeof(buf), "%d", v + 1);
308 if (n >= sizeof(buf))
309 die("impossibly large verbosity value");
310 set_helper_option(t, "verbosity", buf);
313 static int release_helper(struct transport *transport)
316 struct helper_data *data = transport->data;
317 free_refspec(data->refspec_nr, data->refspecs);
318 data->refspecs = NULL;
319 res = disconnect_helper(transport);
320 free(transport->data);
324 static int fetch_with_fetch(struct transport *transport,
325 int nr_heads, struct ref **to_fetch)
327 struct helper_data *data = transport->data;
329 struct strbuf buf = STRBUF_INIT;
331 standard_options(transport);
333 for (i = 0; i < nr_heads; i++) {
334 const struct ref *posn = to_fetch[i];
335 if (posn->status & REF_STATUS_UPTODATE)
338 strbuf_addf(&buf, "fetch %s %s\n",
339 sha1_to_hex(posn->old_sha1), posn->name);
342 strbuf_addch(&buf, '\n');
343 sendline(data, &buf);
346 recvline(data, &buf);
348 if (!prefixcmp(buf.buf, "lock ")) {
349 const char *name = buf.buf + 5;
350 if (transport->pack_lockfile)
351 warning("%s also locked %s", data->name, name);
353 transport->pack_lockfile = xstrdup(name);
358 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
360 strbuf_release(&buf);
364 static int get_importer(struct transport *transport, struct child_process *fastimport)
366 struct child_process *helper = get_helper(transport);
367 memset(fastimport, 0, sizeof(*fastimport));
368 fastimport->in = helper->out;
369 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
370 fastimport->argv[0] = "fast-import";
371 fastimport->argv[1] = "--quiet";
373 fastimport->git_cmd = 1;
374 return start_command(fastimport);
377 static int get_exporter(struct transport *transport,
378 struct child_process *fastexport,
379 struct string_list *revlist_args)
381 struct helper_data *data = transport->data;
382 struct child_process *helper = get_helper(transport);
384 memset(fastexport, 0, sizeof(*fastexport));
386 /* we need to duplicate helper->in because we want to use it after
387 * fastexport is done with it. */
388 fastexport->out = dup(helper->in);
389 fastexport->argv = xcalloc(6 + revlist_args->nr, sizeof(*fastexport->argv));
390 fastexport->argv[argc++] = "fast-export";
391 fastexport->argv[argc++] = "--use-done-feature";
392 if (data->export_marks)
393 fastexport->argv[argc++] = data->export_marks;
394 if (data->import_marks)
395 fastexport->argv[argc++] = data->import_marks;
397 for (i = 0; i < revlist_args->nr; i++)
398 fastexport->argv[argc++] = revlist_args->items[i].string;
400 fastexport->argv[argc++] = "--";
402 fastexport->git_cmd = 1;
403 return start_command(fastexport);
406 static int fetch_with_import(struct transport *transport,
407 int nr_heads, struct ref **to_fetch)
409 struct child_process fastimport;
410 struct helper_data *data = transport->data;
413 struct strbuf buf = STRBUF_INIT;
415 get_helper(transport);
417 if (get_importer(transport, &fastimport))
418 die("Couldn't run fast-import");
420 write_constant(data->helper->in, "import\n");
422 for (i = 0; i < nr_heads; i++) {
424 if (posn->status & REF_STATUS_UPTODATE)
427 strbuf_addf(&buf, "%s\n", posn->name);
428 sendline(data, &buf);
432 write_constant(data->helper->in, "\n");
434 if (finish_command(&fastimport))
435 die("Error while running fast-import");
436 free(fastimport.argv);
437 fastimport.argv = NULL;
439 for (i = 0; i < nr_heads; i++) {
442 if (posn->status & REF_STATUS_UPTODATE)
445 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
447 private = strdup(posn->name);
448 read_ref(private, posn->old_sha1);
451 strbuf_release(&buf);
455 static int process_connect_service(struct transport *transport,
456 const char *name, const char *exec)
458 struct helper_data *data = transport->data;
459 struct strbuf cmdbuf = STRBUF_INIT;
460 struct child_process *helper;
461 int r, duped, ret = 0;
464 helper = get_helper(transport);
467 * Yes, dup the pipe another time, as we need unbuffered version
468 * of input pipe as FILE*. fclose() closes the underlying fd and
469 * stream buffering only can be changed before first I/O operation
472 duped = dup(helper->out);
474 die_errno("Can't dup helper output fd");
475 input = xfdopen(duped, "r");
476 setvbuf(input, NULL, _IONBF, 0);
479 * Handle --upload-pack and friends. This is fire and forget...
480 * just warn if it fails.
482 if (strcmp(name, exec)) {
483 r = set_helper_option(transport, "servpath", exec);
485 warning("Setting remote service path not supported by protocol.");
487 warning("Invalid remote service path.");
491 strbuf_addf(&cmdbuf, "connect %s\n", name);
495 sendline(data, &cmdbuf);
496 recvline_fh(input, &cmdbuf);
497 if (!strcmp(cmdbuf.buf, "")) {
498 data->no_disconnect_req = 1;
500 fprintf(stderr, "Debug: Smart transport connection "
503 } else if (!strcmp(cmdbuf.buf, "fallback")) {
505 fprintf(stderr, "Debug: Falling back to dumb "
508 die("Unknown response to connect: %s",
516 static int process_connect(struct transport *transport,
519 struct helper_data *data = transport->data;
523 name = for_push ? "git-receive-pack" : "git-upload-pack";
525 exec = data->transport_options.receivepack;
527 exec = data->transport_options.uploadpack;
529 return process_connect_service(transport, name, exec);
532 static int connect_helper(struct transport *transport, const char *name,
533 const char *exec, int fd[2])
535 struct helper_data *data = transport->data;
537 /* Get_helper so connect is inited. */
538 get_helper(transport);
540 die("Operation not supported by protocol.");
542 if (!process_connect_service(transport, name, exec))
543 die("Can't connect to subservice %s.", name);
545 fd[0] = data->helper->out;
546 fd[1] = data->helper->in;
550 static int fetch(struct transport *transport,
551 int nr_heads, struct ref **to_fetch)
553 struct helper_data *data = transport->data;
556 if (process_connect(transport, 0)) {
557 do_take_over(transport);
558 return transport->fetch(transport, nr_heads, to_fetch);
562 for (i = 0; i < nr_heads; i++)
563 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
570 return fetch_with_fetch(transport, nr_heads, to_fetch);
573 return fetch_with_import(transport, nr_heads, to_fetch);
578 static void push_update_refs_status(struct helper_data *data,
579 struct ref *remote_refs);
581 static int push_refs_with_push(struct transport *transport,
582 struct ref *remote_refs, int flags)
584 int force_all = flags & TRANSPORT_PUSH_FORCE;
585 int mirror = flags & TRANSPORT_PUSH_MIRROR;
586 struct helper_data *data = transport->data;
587 struct strbuf buf = STRBUF_INIT;
588 struct child_process *helper;
591 helper = get_helper(transport);
595 for (ref = remote_refs; ref; ref = ref->next) {
596 if (!ref->peer_ref && !mirror)
599 /* Check for statuses set by set_ref_status_for_push() */
600 switch (ref->status) {
601 case REF_STATUS_REJECT_NONFASTFORWARD:
602 case REF_STATUS_UPTODATE:
611 strbuf_addstr(&buf, "push ");
612 if (!ref->deletion) {
614 strbuf_addch(&buf, '+');
616 strbuf_addstr(&buf, ref->peer_ref->name);
618 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
620 strbuf_addch(&buf, ':');
621 strbuf_addstr(&buf, ref->name);
622 strbuf_addch(&buf, '\n');
627 standard_options(transport);
629 if (flags & TRANSPORT_PUSH_DRY_RUN) {
630 if (set_helper_option(transport, "dry-run", "true") != 0)
631 die("helper %s does not support dry-run", data->name);
634 strbuf_addch(&buf, '\n');
635 sendline(data, &buf);
636 strbuf_release(&buf);
638 push_update_refs_status(data, remote_refs);
642 static void push_update_ref_status(struct strbuf *buf,
644 struct ref *remote_refs)
649 if (!prefixcmp(buf->buf, "ok ")) {
650 status = REF_STATUS_OK;
651 refname = buf->buf + 3;
652 } else if (!prefixcmp(buf->buf, "error ")) {
653 status = REF_STATUS_REMOTE_REJECT;
654 refname = buf->buf + 6;
656 die("expected ok/error, helper said '%s'\n", buf->buf);
658 msg = strchr(refname, ' ');
660 struct strbuf msg_buf = STRBUF_INIT;
664 if (!unquote_c_style(&msg_buf, msg, &end))
665 msg = strbuf_detach(&msg_buf, NULL);
668 strbuf_release(&msg_buf);
670 if (!strcmp(msg, "no match")) {
671 status = REF_STATUS_NONE;
675 else if (!strcmp(msg, "up to date")) {
676 status = REF_STATUS_UPTODATE;
680 else if (!strcmp(msg, "non-fast forward")) {
681 status = REF_STATUS_REJECT_NONFASTFORWARD;
688 ref = find_ref_by_name(ref, refname);
690 ref = find_ref_by_name(remote_refs, refname);
692 warning("helper reported unexpected status of %s", refname);
696 if (ref->status != REF_STATUS_NONE) {
698 * Earlier, the ref was marked not to be pushed, so ignore the ref
699 * status reported by the remote helper if the latter is 'no match'.
701 if (status == REF_STATUS_NONE)
705 ref->status = status;
706 ref->remote_status = msg;
709 static void push_update_refs_status(struct helper_data *data,
710 struct ref *remote_refs)
712 struct strbuf buf = STRBUF_INIT;
713 struct ref *ref = remote_refs;
715 recvline(data, &buf);
719 push_update_ref_status(&buf, ref, remote_refs);
721 strbuf_release(&buf);
724 static int push_refs_with_export(struct transport *transport,
725 struct ref *remote_refs, int flags)
728 struct child_process *helper, exporter;
729 struct helper_data *data = transport->data;
730 struct string_list revlist_args = STRING_LIST_INIT_NODUP;
731 struct strbuf buf = STRBUF_INIT;
733 helper = get_helper(transport);
735 write_constant(helper->in, "export\n");
739 for (ref = remote_refs; ref; ref = ref->next) {
741 unsigned char sha1[20];
743 // TODO: this can't be right
747 //warning("adding ref %s (%s)\n", ref->name, ref->peer_ref);
749 strbuf_addf(&buf, "%s\n", ref->name);
750 sendline(data, &buf);
753 private = apply_refspecs(data->refspecs, data->refspec_nr, ref->name);
754 if (private && !get_sha1(private, sha1)) {
755 strbuf_addf(&buf, "^%s", private);
756 string_list_append(&revlist_args, strbuf_detach(&buf, NULL));
759 string_list_append(&revlist_args, ref->name);
763 write_constant(helper->in, "\n");
765 if (get_exporter(transport, &exporter, &revlist_args))
766 die("Couldn't run fast-export");
768 if(finish_command(&exporter))
769 die("Error while running fast-export");
770 push_update_refs_status(data, remote_refs);
774 static int push_refs(struct transport *transport,
775 struct ref *remote_refs, int flags)
777 struct helper_data *data = transport->data;
779 if (process_connect(transport, 1)) {
780 do_take_over(transport);
781 return transport->push_refs(transport, remote_refs, flags);
785 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
786 "Perhaps you should specify a branch such as 'master'.\n");
791 return push_refs_with_push(transport, remote_refs, flags);
794 return push_refs_with_export(transport, remote_refs, flags);
800 static int has_attribute(const char *attrs, const char *attr) {
807 const char *space = strchrnul(attrs, ' ');
808 if (len == space - attrs && !strncmp(attrs, attr, len))
816 static struct ref *get_refs_list(struct transport *transport, int for_push)
818 struct helper_data *data = transport->data;
819 struct child_process *helper;
820 struct ref *ret = NULL;
821 struct ref **tail = &ret;
823 struct strbuf buf = STRBUF_INIT;
825 helper = get_helper(transport);
827 if (process_connect(transport, for_push)) {
828 do_take_over(transport);
829 return transport->get_refs_list(transport, for_push);
832 if (data->push && for_push)
833 write_constant(helper->in, "list for-push\n");
835 write_constant(helper->in, "list\n");
839 recvline(data, &buf);
844 eov = strchr(buf.buf, ' ');
846 die("Malformed response in ref list: %s", buf.buf);
847 eon = strchr(eov + 1, ' ');
851 *tail = alloc_ref(eov + 1);
852 if (buf.buf[0] == '@')
853 (*tail)->symref = xstrdup(buf.buf + 1);
854 else if (buf.buf[0] != '?')
855 get_sha1_hex(buf.buf, (*tail)->old_sha1);
857 if (has_attribute(eon + 1, "unchanged")) {
858 (*tail)->status |= REF_STATUS_UPTODATE;
859 read_ref((*tail)->name, (*tail)->old_sha1);
862 tail = &((*tail)->next);
865 fprintf(stderr, "Debug: Read ref listing.\n");
866 strbuf_release(&buf);
868 for (posn = ret; posn; posn = posn->next)
869 resolve_remote_symref(posn, ret);
874 int transport_helper_init(struct transport *transport, const char *name)
876 struct helper_data *data = xcalloc(sizeof(*data), 1);
879 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
882 transport->data = data;
883 transport->set_option = set_helper_option;
884 transport->get_refs_list = get_refs_list;
885 transport->fetch = fetch;
886 transport->push_refs = push_refs;
887 transport->disconnect = release_helper;
888 transport->connect = connect_helper;
889 transport->smart_options = &(data->transport_options);