4 #include "run-command.h"
14 struct child_process *helper;
20 /* These go from remote name (as in "list") to private name */
21 struct refspec *refspecs;
25 static struct child_process *get_helper(struct transport *transport)
27 struct helper_data *data = transport->data;
28 struct strbuf buf = STRBUF_INIT;
29 struct child_process *helper;
30 const char **refspecs = NULL;
32 int refspec_alloc = 0;
37 helper = xcalloc(1, sizeof(*helper));
41 helper->argv = xcalloc(4, sizeof(*helper->argv));
42 strbuf_addf(&buf, "remote-%s", data->name);
43 helper->argv[0] = strbuf_detach(&buf, NULL);
44 helper->argv[1] = transport->remote->name;
45 helper->argv[2] = transport->url;
47 if (start_command(helper))
48 die("Unable to run helper: git %s", helper->argv[0]);
49 data->helper = helper;
51 write_str_in_full(helper->in, "capabilities\n");
53 data->out = xfdopen(helper->out, "r");
55 if (strbuf_getline(&buf, data->out, '\n') == EOF)
56 exit(128); /* child died, message supplied already */
60 if (!strcmp(buf.buf, "fetch"))
62 if (!strcmp(buf.buf, "option"))
64 if (!strcmp(buf.buf, "push"))
66 if (!strcmp(buf.buf, "import"))
68 if (!data->refspecs && !prefixcmp(buf.buf, "refspec ")) {
72 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
77 data->refspec_nr = refspec_nr;
78 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
79 for (i = 0; i < refspec_nr; i++) {
80 free((char *)refspecs[i]);
88 static int disconnect_helper(struct transport *transport)
90 struct helper_data *data = transport->data;
92 write_str_in_full(data->helper->in, "\n");
93 close(data->helper->in);
95 finish_command(data->helper);
96 free((char *)data->helper->argv[0]);
97 free(data->helper->argv);
104 static const char *unsupported_options[] = {
105 TRANS_OPT_UPLOADPACK,
106 TRANS_OPT_RECEIVEPACK,
110 static const char *boolean_options[] = {
116 static int set_helper_option(struct transport *transport,
117 const char *name, const char *value)
119 struct helper_data *data = transport->data;
120 struct child_process *helper = get_helper(transport);
121 struct strbuf buf = STRBUF_INIT;
122 int i, ret, is_bool = 0;
127 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
128 if (!strcmp(name, unsupported_options[i]))
132 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
133 if (!strcmp(name, boolean_options[i])) {
139 strbuf_addf(&buf, "option %s ", name);
141 strbuf_addstr(&buf, value ? "true" : "false");
143 quote_c_style(value, &buf, NULL, 0);
144 strbuf_addch(&buf, '\n');
146 if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
147 die_errno("cannot send option to %s", data->name);
150 if (strbuf_getline(&buf, data->out, '\n') == EOF)
151 exit(128); /* child died, message supplied already */
153 if (!strcmp(buf.buf, "ok"))
155 else if (!prefixcmp(buf.buf, "error")) {
157 } else if (!strcmp(buf.buf, "unsupported"))
160 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
163 strbuf_release(&buf);
167 static void standard_options(struct transport *t)
172 int no_progress = v < 0 || (!t->progress && !isatty(1));
174 set_helper_option(t, "progress", !no_progress ? "true" : "false");
176 n = snprintf(buf, sizeof(buf), "%d", v + 1);
177 if (n >= sizeof(buf))
178 die("impossibly large verbosity value");
179 set_helper_option(t, "verbosity", buf);
182 static int release_helper(struct transport *transport)
184 struct helper_data *data = transport->data;
185 free_refspec(data->refspec_nr, data->refspecs);
186 data->refspecs = NULL;
187 disconnect_helper(transport);
188 free(transport->data);
192 static int fetch_with_fetch(struct transport *transport,
193 int nr_heads, struct ref **to_fetch)
195 struct helper_data *data = transport->data;
197 struct strbuf buf = STRBUF_INIT;
199 standard_options(transport);
201 for (i = 0; i < nr_heads; i++) {
202 const struct ref *posn = to_fetch[i];
203 if (posn->status & REF_STATUS_UPTODATE)
206 strbuf_addf(&buf, "fetch %s %s\n",
207 sha1_to_hex(posn->old_sha1), posn->name);
210 strbuf_addch(&buf, '\n');
211 if (write_in_full(data->helper->in, buf.buf, buf.len) != buf.len)
212 die_errno("cannot send fetch to %s", data->name);
216 if (strbuf_getline(&buf, data->out, '\n') == EOF)
217 exit(128); /* child died, message supplied already */
219 if (!prefixcmp(buf.buf, "lock ")) {
220 const char *name = buf.buf + 5;
221 if (transport->pack_lockfile)
222 warning("%s also locked %s", data->name, name);
224 transport->pack_lockfile = xstrdup(name);
229 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
231 strbuf_release(&buf);
235 static int get_importer(struct transport *transport, struct child_process *fastimport)
237 struct child_process *helper = get_helper(transport);
238 memset(fastimport, 0, sizeof(*fastimport));
239 fastimport->in = helper->out;
240 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
241 fastimport->argv[0] = "fast-import";
242 fastimport->argv[1] = "--quiet";
244 fastimport->git_cmd = 1;
245 return start_command(fastimport);
248 static int fetch_with_import(struct transport *transport,
249 int nr_heads, struct ref **to_fetch)
251 struct child_process fastimport;
252 struct child_process *helper = get_helper(transport);
253 struct helper_data *data = transport->data;
256 struct strbuf buf = STRBUF_INIT;
258 if (get_importer(transport, &fastimport))
259 die("Couldn't run fast-import");
261 for (i = 0; i < nr_heads; i++) {
263 if (posn->status & REF_STATUS_UPTODATE)
266 strbuf_addf(&buf, "import %s\n", posn->name);
267 write_in_full(helper->in, buf.buf, buf.len);
270 disconnect_helper(transport);
271 finish_command(&fastimport);
272 free(fastimport.argv);
273 fastimport.argv = NULL;
275 for (i = 0; i < nr_heads; i++) {
278 if (posn->status & REF_STATUS_UPTODATE)
281 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
283 private = strdup(posn->name);
284 read_ref(private, posn->old_sha1);
287 strbuf_release(&buf);
291 static int fetch(struct transport *transport,
292 int nr_heads, struct ref **to_fetch)
294 struct helper_data *data = transport->data;
298 for (i = 0; i < nr_heads; i++)
299 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
306 return fetch_with_fetch(transport, nr_heads, to_fetch);
309 return fetch_with_import(transport, nr_heads, to_fetch);
314 static int push_refs(struct transport *transport,
315 struct ref *remote_refs, int flags)
317 int force_all = flags & TRANSPORT_PUSH_FORCE;
318 int mirror = flags & TRANSPORT_PUSH_MIRROR;
319 struct helper_data *data = transport->data;
320 struct strbuf buf = STRBUF_INIT;
321 struct child_process *helper;
327 helper = get_helper(transport);
331 for (ref = remote_refs; ref; ref = ref->next) {
333 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
337 ref->deletion = is_null_sha1(ref->new_sha1);
338 if (!ref->deletion &&
339 !hashcmp(ref->old_sha1, ref->new_sha1)) {
340 ref->status = REF_STATUS_UPTODATE;
347 strbuf_addstr(&buf, "push ");
348 if (!ref->deletion) {
350 strbuf_addch(&buf, '+');
352 strbuf_addstr(&buf, ref->peer_ref->name);
354 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
356 strbuf_addch(&buf, ':');
357 strbuf_addstr(&buf, ref->name);
358 strbuf_addch(&buf, '\n');
363 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
364 standard_options(transport);
366 if (flags & TRANSPORT_PUSH_DRY_RUN) {
367 if (set_helper_option(transport, "dry-run", "true") != 0)
368 die("helper %s does not support dry-run", data->name);
371 strbuf_addch(&buf, '\n');
372 if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
381 if (strbuf_getline(&buf, data->out, '\n') == EOF)
382 exit(128); /* child died, message supplied already */
386 if (!prefixcmp(buf.buf, "ok ")) {
387 status = REF_STATUS_OK;
388 refname = buf.buf + 3;
389 } else if (!prefixcmp(buf.buf, "error ")) {
390 status = REF_STATUS_REMOTE_REJECT;
391 refname = buf.buf + 6;
393 die("expected ok/error, helper said '%s'\n", buf.buf);
395 msg = strchr(refname, ' ');
397 struct strbuf msg_buf = STRBUF_INIT;
401 if (!unquote_c_style(&msg_buf, msg, &end))
402 msg = strbuf_detach(&msg_buf, NULL);
405 strbuf_release(&msg_buf);
407 if (!strcmp(msg, "no match")) {
408 status = REF_STATUS_NONE;
412 else if (!strcmp(msg, "up to date")) {
413 status = REF_STATUS_UPTODATE;
417 else if (!strcmp(msg, "non-fast forward")) {
418 status = REF_STATUS_REJECT_NONFASTFORWARD;
425 ref = find_ref_by_name(ref, refname);
427 ref = find_ref_by_name(remote_refs, refname);
429 warning("helper reported unexpected status of %s", refname);
433 ref->status = status;
434 ref->remote_status = msg;
436 strbuf_release(&buf);
440 static int has_attribute(const char *attrs, const char *attr) {
447 const char *space = strchrnul(attrs, ' ');
448 if (len == space - attrs && !strncmp(attrs, attr, len))
456 static struct ref *get_refs_list(struct transport *transport, int for_push)
458 struct helper_data *data = transport->data;
459 struct child_process *helper;
460 struct ref *ret = NULL;
461 struct ref **tail = &ret;
463 struct strbuf buf = STRBUF_INIT;
465 helper = get_helper(transport);
467 if (data->push && for_push)
468 write_str_in_full(helper->in, "list for-push\n");
470 write_str_in_full(helper->in, "list\n");
474 if (strbuf_getline(&buf, data->out, '\n') == EOF)
475 exit(128); /* child died, message supplied already */
480 eov = strchr(buf.buf, ' ');
482 die("Malformed response in ref list: %s", buf.buf);
483 eon = strchr(eov + 1, ' ');
487 *tail = alloc_ref(eov + 1);
488 if (buf.buf[0] == '@')
489 (*tail)->symref = xstrdup(buf.buf + 1);
490 else if (buf.buf[0] != '?')
491 get_sha1_hex(buf.buf, (*tail)->old_sha1);
493 if (has_attribute(eon + 1, "unchanged")) {
494 (*tail)->status |= REF_STATUS_UPTODATE;
495 read_ref((*tail)->name, (*tail)->old_sha1);
498 tail = &((*tail)->next);
500 strbuf_release(&buf);
502 for (posn = ret; posn; posn = posn->next)
503 resolve_remote_symref(posn, ret);
508 int transport_helper_init(struct transport *transport, const char *name)
510 struct helper_data *data = xcalloc(sizeof(*data), 1);
513 transport->data = data;
514 transport->set_option = set_helper_option;
515 transport->get_refs_list = get_refs_list;
516 transport->fetch = fetch;
517 transport->push_refs = push_refs;
518 transport->disconnect = release_helper;