4 #include "run-command.h"
16 struct child_process *helper;
22 /* These go from remote name (as in "list") to private name */
23 struct refspec *refspecs;
27 static void sendline(struct helper_data *helper, struct strbuf *buffer)
30 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
31 if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
33 die_errno("Full write to remote helper failed");
36 static int recvline(struct helper_data *helper, struct strbuf *buffer)
40 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
41 if (strbuf_getline(buffer, helper->out, '\n') == EOF) {
43 fprintf(stderr, "Debug: Remote helper quit.\n");
48 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
52 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
54 sendline(helper, buffer);
55 recvline(helper, buffer);
58 static void write_constant(int fd, const char *str)
61 fprintf(stderr, "Debug: Remote helper: -> %s", str);
62 if (write_in_full(fd, str, strlen(str)) != strlen(str))
63 die_errno("Full write to remote helper failed");
66 const char *remove_ext_force(const char *url)
69 const char *colon = strchr(url, ':');
70 if (colon && colon[1] == ':')
76 static struct child_process *get_helper(struct transport *transport)
78 struct helper_data *data = transport->data;
79 struct strbuf buf = STRBUF_INIT;
80 struct child_process *helper;
81 const char **refspecs = NULL;
83 int refspec_alloc = 0;
88 helper = xcalloc(1, sizeof(*helper));
92 helper->argv = xcalloc(4, sizeof(*helper->argv));
93 strbuf_addf(&buf, "remote-%s", data->name);
94 helper->argv[0] = strbuf_detach(&buf, NULL);
95 helper->argv[1] = transport->remote->name;
96 helper->argv[2] = remove_ext_force(transport->url);
98 if (start_command(helper))
99 die("Unable to run helper: git %s", helper->argv[0]);
100 data->helper = helper;
102 write_constant(helper->in, "capabilities\n");
104 data->out = xfdopen(helper->out, "r");
108 recvline(data, &buf);
113 if (*buf.buf == '*') {
114 capname = buf.buf + 1;
120 fprintf(stderr, "Debug: Got cap %s\n", capname);
121 if (!strcmp(capname, "fetch"))
123 else if (!strcmp(capname, "option"))
125 else if (!strcmp(capname, "push"))
127 else if (!strcmp(capname, "import"))
129 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
133 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
134 } else if (mandatory) {
135 die("Unknown madatory capability %s. This remote "
136 "helper probably needs newer version of Git.\n",
142 data->refspec_nr = refspec_nr;
143 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
144 for (i = 0; i < refspec_nr; i++) {
145 free((char *)refspecs[i]);
149 strbuf_release(&buf);
151 fprintf(stderr, "Debug: Capabilities complete.\n");
155 static int disconnect_helper(struct transport *transport)
157 struct helper_data *data = transport->data;
158 struct strbuf buf = STRBUF_INIT;
162 fprintf(stderr, "Debug: Disconnecting.\n");
163 strbuf_addf(&buf, "\n");
164 sendline(data, &buf);
165 close(data->helper->in);
167 finish_command(data->helper);
168 free((char *)data->helper->argv[0]);
169 free(data->helper->argv);
176 static const char *unsupported_options[] = {
177 TRANS_OPT_UPLOADPACK,
178 TRANS_OPT_RECEIVEPACK,
182 static const char *boolean_options[] = {
188 static int set_helper_option(struct transport *transport,
189 const char *name, const char *value)
191 struct helper_data *data = transport->data;
192 struct strbuf buf = STRBUF_INIT;
193 int i, ret, is_bool = 0;
195 get_helper(transport);
200 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
201 if (!strcmp(name, unsupported_options[i]))
205 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
206 if (!strcmp(name, boolean_options[i])) {
212 strbuf_addf(&buf, "option %s ", name);
214 strbuf_addstr(&buf, value ? "true" : "false");
216 quote_c_style(value, &buf, NULL, 0);
217 strbuf_addch(&buf, '\n');
219 xchgline(data, &buf);
221 if (!strcmp(buf.buf, "ok"))
223 else if (!prefixcmp(buf.buf, "error")) {
225 } else if (!strcmp(buf.buf, "unsupported"))
228 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
231 strbuf_release(&buf);
235 static void standard_options(struct transport *t)
240 int no_progress = v < 0 || (!t->progress && !isatty(1));
242 set_helper_option(t, "progress", !no_progress ? "true" : "false");
244 n = snprintf(buf, sizeof(buf), "%d", v + 1);
245 if (n >= sizeof(buf))
246 die("impossibly large verbosity value");
247 set_helper_option(t, "verbosity", buf);
250 static int release_helper(struct transport *transport)
252 struct helper_data *data = transport->data;
253 free_refspec(data->refspec_nr, data->refspecs);
254 data->refspecs = NULL;
255 disconnect_helper(transport);
256 free(transport->data);
260 static int fetch_with_fetch(struct transport *transport,
261 int nr_heads, struct ref **to_fetch)
263 struct helper_data *data = transport->data;
265 struct strbuf buf = STRBUF_INIT;
267 standard_options(transport);
269 for (i = 0; i < nr_heads; i++) {
270 const struct ref *posn = to_fetch[i];
271 if (posn->status & REF_STATUS_UPTODATE)
274 strbuf_addf(&buf, "fetch %s %s\n",
275 sha1_to_hex(posn->old_sha1), posn->name);
278 strbuf_addch(&buf, '\n');
279 sendline(data, &buf);
282 recvline(data, &buf);
284 if (!prefixcmp(buf.buf, "lock ")) {
285 const char *name = buf.buf + 5;
286 if (transport->pack_lockfile)
287 warning("%s also locked %s", data->name, name);
289 transport->pack_lockfile = xstrdup(name);
294 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
296 strbuf_release(&buf);
300 static int get_importer(struct transport *transport, struct child_process *fastimport)
302 struct child_process *helper = get_helper(transport);
303 memset(fastimport, 0, sizeof(*fastimport));
304 fastimport->in = helper->out;
305 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
306 fastimport->argv[0] = "fast-import";
307 fastimport->argv[1] = "--quiet";
309 fastimport->git_cmd = 1;
310 return start_command(fastimport);
313 static int fetch_with_import(struct transport *transport,
314 int nr_heads, struct ref **to_fetch)
316 struct child_process fastimport;
317 struct helper_data *data = transport->data;
320 struct strbuf buf = STRBUF_INIT;
322 get_helper(transport);
324 if (get_importer(transport, &fastimport))
325 die("Couldn't run fast-import");
327 for (i = 0; i < nr_heads; i++) {
329 if (posn->status & REF_STATUS_UPTODATE)
332 strbuf_addf(&buf, "import %s\n", posn->name);
333 sendline(data, &buf);
336 disconnect_helper(transport);
337 finish_command(&fastimport);
338 free(fastimport.argv);
339 fastimport.argv = NULL;
341 for (i = 0; i < nr_heads; i++) {
344 if (posn->status & REF_STATUS_UPTODATE)
347 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
349 private = strdup(posn->name);
350 read_ref(private, posn->old_sha1);
353 strbuf_release(&buf);
357 static int fetch(struct transport *transport,
358 int nr_heads, struct ref **to_fetch)
360 struct helper_data *data = transport->data;
364 for (i = 0; i < nr_heads; i++)
365 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
372 return fetch_with_fetch(transport, nr_heads, to_fetch);
375 return fetch_with_import(transport, nr_heads, to_fetch);
380 static int push_refs(struct transport *transport,
381 struct ref *remote_refs, int flags)
383 int force_all = flags & TRANSPORT_PUSH_FORCE;
384 int mirror = flags & TRANSPORT_PUSH_MIRROR;
385 struct helper_data *data = transport->data;
386 struct strbuf buf = STRBUF_INIT;
387 struct child_process *helper;
393 helper = get_helper(transport);
397 for (ref = remote_refs; ref; ref = ref->next) {
399 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
403 ref->deletion = is_null_sha1(ref->new_sha1);
404 if (!ref->deletion &&
405 !hashcmp(ref->old_sha1, ref->new_sha1)) {
406 ref->status = REF_STATUS_UPTODATE;
413 strbuf_addstr(&buf, "push ");
414 if (!ref->deletion) {
416 strbuf_addch(&buf, '+');
418 strbuf_addstr(&buf, ref->peer_ref->name);
420 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
422 strbuf_addch(&buf, ':');
423 strbuf_addstr(&buf, ref->name);
424 strbuf_addch(&buf, '\n');
429 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
430 standard_options(transport);
432 if (flags & TRANSPORT_PUSH_DRY_RUN) {
433 if (set_helper_option(transport, "dry-run", "true") != 0)
434 die("helper %s does not support dry-run", data->name);
437 strbuf_addch(&buf, '\n');
438 sendline(data, &buf);
445 recvline(data, &buf);
449 if (!prefixcmp(buf.buf, "ok ")) {
450 status = REF_STATUS_OK;
451 refname = buf.buf + 3;
452 } else if (!prefixcmp(buf.buf, "error ")) {
453 status = REF_STATUS_REMOTE_REJECT;
454 refname = buf.buf + 6;
456 die("expected ok/error, helper said '%s'\n", buf.buf);
458 msg = strchr(refname, ' ');
460 struct strbuf msg_buf = STRBUF_INIT;
464 if (!unquote_c_style(&msg_buf, msg, &end))
465 msg = strbuf_detach(&msg_buf, NULL);
468 strbuf_release(&msg_buf);
470 if (!strcmp(msg, "no match")) {
471 status = REF_STATUS_NONE;
475 else if (!strcmp(msg, "up to date")) {
476 status = REF_STATUS_UPTODATE;
480 else if (!strcmp(msg, "non-fast forward")) {
481 status = REF_STATUS_REJECT_NONFASTFORWARD;
488 ref = find_ref_by_name(ref, refname);
490 ref = find_ref_by_name(remote_refs, refname);
492 warning("helper reported unexpected status of %s", refname);
496 ref->status = status;
497 ref->remote_status = msg;
499 strbuf_release(&buf);
503 static int has_attribute(const char *attrs, const char *attr) {
510 const char *space = strchrnul(attrs, ' ');
511 if (len == space - attrs && !strncmp(attrs, attr, len))
519 static struct ref *get_refs_list(struct transport *transport, int for_push)
521 struct helper_data *data = transport->data;
522 struct child_process *helper;
523 struct ref *ret = NULL;
524 struct ref **tail = &ret;
526 struct strbuf buf = STRBUF_INIT;
528 helper = get_helper(transport);
530 if (data->push && for_push)
531 write_str_in_full(helper->in, "list for-push\n");
533 write_str_in_full(helper->in, "list\n");
537 recvline(data, &buf);
542 eov = strchr(buf.buf, ' ');
544 die("Malformed response in ref list: %s", buf.buf);
545 eon = strchr(eov + 1, ' ');
549 *tail = alloc_ref(eov + 1);
550 if (buf.buf[0] == '@')
551 (*tail)->symref = xstrdup(buf.buf + 1);
552 else if (buf.buf[0] != '?')
553 get_sha1_hex(buf.buf, (*tail)->old_sha1);
555 if (has_attribute(eon + 1, "unchanged")) {
556 (*tail)->status |= REF_STATUS_UPTODATE;
557 read_ref((*tail)->name, (*tail)->old_sha1);
560 tail = &((*tail)->next);
563 fprintf(stderr, "Debug: Read ref listing.\n");
564 strbuf_release(&buf);
566 for (posn = ret; posn; posn = posn->next)
567 resolve_remote_symref(posn, ret);
572 int transport_helper_init(struct transport *transport, const char *name)
574 struct helper_data *data = xcalloc(sizeof(*data), 1);
577 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
580 transport->data = data;
581 transport->set_option = set_helper_option;
582 transport->get_refs_list = get_refs_list;
583 transport->fetch = fetch;
584 transport->push_refs = push_refs;
585 transport->disconnect = release_helper;