Support taking over transports
[git/dkf.git] / transport-helper.c
blob97eed6cbf6bb7e076bd46f5d59cb5e66ac47ccc5
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 /* These go from remote name (as in "list") to private name */
23 struct refspec *refspecs;
24 int refspec_nr;
25 /* Transport options for fetch-pack/send-pack (should one of
26 * those be invoked).
28 struct git_transport_options transport_options;
31 static void sendline(struct helper_data *helper, struct strbuf *buffer)
33 if (debug)
34 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
35 if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
36 != buffer->len)
37 die_errno("Full write to remote helper failed");
40 static int recvline(struct helper_data *helper, struct strbuf *buffer)
42 strbuf_reset(buffer);
43 if (debug)
44 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
45 if (strbuf_getline(buffer, helper->out, '\n') == EOF) {
46 if (debug)
47 fprintf(stderr, "Debug: Remote helper quit.\n");
48 exit(128);
51 if (debug)
52 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
53 return 0;
56 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
58 sendline(helper, buffer);
59 recvline(helper, buffer);
62 static void write_constant(int fd, const char *str)
64 if (debug)
65 fprintf(stderr, "Debug: Remote helper: -> %s", str);
66 if (write_in_full(fd, str, strlen(str)) != strlen(str))
67 die_errno("Full write to remote helper failed");
70 const char *remove_ext_force(const char *url)
72 if (url) {
73 const char *colon = strchr(url, ':');
74 if (colon && colon[1] == ':')
75 return colon + 2;
77 return url;
80 static struct child_process *get_helper(struct transport *transport)
82 struct helper_data *data = transport->data;
83 struct strbuf buf = STRBUF_INIT;
84 struct child_process *helper;
85 const char **refspecs = NULL;
86 int refspec_nr = 0;
87 int refspec_alloc = 0;
88 int duped;
90 if (data->helper)
91 return data->helper;
93 helper = xcalloc(1, sizeof(*helper));
94 helper->in = -1;
95 helper->out = -1;
96 helper->err = 0;
97 helper->argv = xcalloc(4, sizeof(*helper->argv));
98 strbuf_addf(&buf, "remote-%s", data->name);
99 helper->argv[0] = strbuf_detach(&buf, NULL);
100 helper->argv[1] = transport->remote->name;
101 helper->argv[2] = remove_ext_force(transport->url);
102 helper->git_cmd = 1;
103 if (start_command(helper))
104 die("Unable to run helper: git %s", helper->argv[0]);
105 data->helper = helper;
108 * Open the output as FILE* so strbuf_getline() can be used.
109 * Do this with duped fd because fclose() will close the fd,
110 * and stuff like taking over will require the fd to remain.
113 duped = dup(helper->out);
114 if (duped < 0)
115 die_errno("Can't dup helper output fd");
116 data->out = xfdopen(duped, "r");
118 write_constant(helper->in, "capabilities\n");
120 while (1) {
121 const char *capname;
122 int mandatory = 0;
123 recvline(data, &buf);
125 if (!*buf.buf)
126 break;
128 if (*buf.buf == '*') {
129 capname = buf.buf + 1;
130 mandatory = 1;
131 } else
132 capname = buf.buf;
134 if (debug)
135 fprintf(stderr, "Debug: Got cap %s\n", capname);
136 if (!strcmp(capname, "fetch"))
137 data->fetch = 1;
138 else if (!strcmp(capname, "option"))
139 data->option = 1;
140 else if (!strcmp(capname, "push"))
141 data->push = 1;
142 else if (!strcmp(capname, "import"))
143 data->import = 1;
144 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
145 ALLOC_GROW(refspecs,
146 refspec_nr + 1,
147 refspec_alloc);
148 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
149 } else if (mandatory) {
150 die("Unknown madatory capability %s. This remote "
151 "helper probably needs newer version of Git.\n",
152 capname);
155 if (refspecs) {
156 int i;
157 data->refspec_nr = refspec_nr;
158 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
159 for (i = 0; i < refspec_nr; i++) {
160 free((char *)refspecs[i]);
162 free(refspecs);
164 strbuf_release(&buf);
165 if (debug)
166 fprintf(stderr, "Debug: Capabilities complete.\n");
167 return data->helper;
170 static int disconnect_helper(struct transport *transport)
172 struct helper_data *data = transport->data;
173 struct strbuf buf = STRBUF_INIT;
175 if (data->helper) {
176 if (debug)
177 fprintf(stderr, "Debug: Disconnecting.\n");
178 strbuf_addf(&buf, "\n");
179 sendline(data, &buf);
180 close(data->helper->in);
181 close(data->helper->out);
182 fclose(data->out);
183 finish_command(data->helper);
184 free((char *)data->helper->argv[0]);
185 free(data->helper->argv);
186 free(data->helper);
187 data->helper = NULL;
189 return 0;
192 static const char *unsupported_options[] = {
193 TRANS_OPT_UPLOADPACK,
194 TRANS_OPT_RECEIVEPACK,
195 TRANS_OPT_THIN,
196 TRANS_OPT_KEEP
198 static const char *boolean_options[] = {
199 TRANS_OPT_THIN,
200 TRANS_OPT_KEEP,
201 TRANS_OPT_FOLLOWTAGS
204 static int set_helper_option(struct transport *transport,
205 const char *name, const char *value)
207 struct helper_data *data = transport->data;
208 struct strbuf buf = STRBUF_INIT;
209 int i, ret, is_bool = 0;
211 get_helper(transport);
213 if (!data->option)
214 return 1;
216 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
217 if (!strcmp(name, unsupported_options[i]))
218 return 1;
221 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
222 if (!strcmp(name, boolean_options[i])) {
223 is_bool = 1;
224 break;
228 strbuf_addf(&buf, "option %s ", name);
229 if (is_bool)
230 strbuf_addstr(&buf, value ? "true" : "false");
231 else
232 quote_c_style(value, &buf, NULL, 0);
233 strbuf_addch(&buf, '\n');
235 xchgline(data, &buf);
237 if (!strcmp(buf.buf, "ok"))
238 ret = 0;
239 else if (!prefixcmp(buf.buf, "error")) {
240 ret = -1;
241 } else if (!strcmp(buf.buf, "unsupported"))
242 ret = 1;
243 else {
244 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
245 ret = 1;
247 strbuf_release(&buf);
248 return ret;
251 static void standard_options(struct transport *t)
253 char buf[16];
254 int n;
255 int v = t->verbose;
256 int no_progress = v < 0 || (!t->progress && !isatty(1));
258 set_helper_option(t, "progress", !no_progress ? "true" : "false");
260 n = snprintf(buf, sizeof(buf), "%d", v + 1);
261 if (n >= sizeof(buf))
262 die("impossibly large verbosity value");
263 set_helper_option(t, "verbosity", buf);
266 static int release_helper(struct transport *transport)
268 struct helper_data *data = transport->data;
269 free_refspec(data->refspec_nr, data->refspecs);
270 data->refspecs = NULL;
271 disconnect_helper(transport);
272 free(transport->data);
273 return 0;
276 static int fetch_with_fetch(struct transport *transport,
277 int nr_heads, struct ref **to_fetch)
279 struct helper_data *data = transport->data;
280 int i;
281 struct strbuf buf = STRBUF_INIT;
283 standard_options(transport);
285 for (i = 0; i < nr_heads; i++) {
286 const struct ref *posn = to_fetch[i];
287 if (posn->status & REF_STATUS_UPTODATE)
288 continue;
290 strbuf_addf(&buf, "fetch %s %s\n",
291 sha1_to_hex(posn->old_sha1), posn->name);
294 strbuf_addch(&buf, '\n');
295 sendline(data, &buf);
297 while (1) {
298 recvline(data, &buf);
300 if (!prefixcmp(buf.buf, "lock ")) {
301 const char *name = buf.buf + 5;
302 if (transport->pack_lockfile)
303 warning("%s also locked %s", data->name, name);
304 else
305 transport->pack_lockfile = xstrdup(name);
307 else if (!buf.len)
308 break;
309 else
310 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
312 strbuf_release(&buf);
313 return 0;
316 static int get_importer(struct transport *transport, struct child_process *fastimport)
318 struct child_process *helper = get_helper(transport);
319 memset(fastimport, 0, sizeof(*fastimport));
320 fastimport->in = helper->out;
321 fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
322 fastimport->argv[0] = "fast-import";
323 fastimport->argv[1] = "--quiet";
325 fastimport->git_cmd = 1;
326 return start_command(fastimport);
329 static int fetch_with_import(struct transport *transport,
330 int nr_heads, struct ref **to_fetch)
332 struct child_process fastimport;
333 struct helper_data *data = transport->data;
334 int i;
335 struct ref *posn;
336 struct strbuf buf = STRBUF_INIT;
338 get_helper(transport);
340 if (get_importer(transport, &fastimport))
341 die("Couldn't run fast-import");
343 for (i = 0; i < nr_heads; i++) {
344 posn = to_fetch[i];
345 if (posn->status & REF_STATUS_UPTODATE)
346 continue;
348 strbuf_addf(&buf, "import %s\n", posn->name);
349 sendline(data, &buf);
350 strbuf_reset(&buf);
352 disconnect_helper(transport);
353 finish_command(&fastimport);
354 free(fastimport.argv);
355 fastimport.argv = NULL;
357 for (i = 0; i < nr_heads; i++) {
358 char *private;
359 posn = to_fetch[i];
360 if (posn->status & REF_STATUS_UPTODATE)
361 continue;
362 if (data->refspecs)
363 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
364 else
365 private = strdup(posn->name);
366 read_ref(private, posn->old_sha1);
367 free(private);
369 strbuf_release(&buf);
370 return 0;
373 static int fetch(struct transport *transport,
374 int nr_heads, struct ref **to_fetch)
376 struct helper_data *data = transport->data;
377 int i, count;
379 count = 0;
380 for (i = 0; i < nr_heads; i++)
381 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
382 count++;
384 if (!count)
385 return 0;
387 if (data->fetch)
388 return fetch_with_fetch(transport, nr_heads, to_fetch);
390 if (data->import)
391 return fetch_with_import(transport, nr_heads, to_fetch);
393 return -1;
396 static int push_refs(struct transport *transport,
397 struct ref *remote_refs, int flags)
399 int force_all = flags & TRANSPORT_PUSH_FORCE;
400 int mirror = flags & TRANSPORT_PUSH_MIRROR;
401 struct helper_data *data = transport->data;
402 struct strbuf buf = STRBUF_INIT;
403 struct child_process *helper;
404 struct ref *ref;
406 if (!remote_refs)
407 return 0;
409 helper = get_helper(transport);
410 if (!data->push)
411 return 1;
413 for (ref = remote_refs; ref; ref = ref->next) {
414 if (ref->peer_ref)
415 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
416 else if (!mirror)
417 continue;
419 ref->deletion = is_null_sha1(ref->new_sha1);
420 if (!ref->deletion &&
421 !hashcmp(ref->old_sha1, ref->new_sha1)) {
422 ref->status = REF_STATUS_UPTODATE;
423 continue;
426 if (force_all)
427 ref->force = 1;
429 strbuf_addstr(&buf, "push ");
430 if (!ref->deletion) {
431 if (ref->force)
432 strbuf_addch(&buf, '+');
433 if (ref->peer_ref)
434 strbuf_addstr(&buf, ref->peer_ref->name);
435 else
436 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
438 strbuf_addch(&buf, ':');
439 strbuf_addstr(&buf, ref->name);
440 strbuf_addch(&buf, '\n');
442 if (buf.len == 0)
443 return 0;
445 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
446 standard_options(transport);
448 if (flags & TRANSPORT_PUSH_DRY_RUN) {
449 if (set_helper_option(transport, "dry-run", "true") != 0)
450 die("helper %s does not support dry-run", data->name);
453 strbuf_addch(&buf, '\n');
454 sendline(data, &buf);
456 ref = remote_refs;
457 while (1) {
458 char *refname, *msg;
459 int status;
461 recvline(data, &buf);
462 if (!buf.len)
463 break;
465 if (!prefixcmp(buf.buf, "ok ")) {
466 status = REF_STATUS_OK;
467 refname = buf.buf + 3;
468 } else if (!prefixcmp(buf.buf, "error ")) {
469 status = REF_STATUS_REMOTE_REJECT;
470 refname = buf.buf + 6;
471 } else
472 die("expected ok/error, helper said '%s'\n", buf.buf);
474 msg = strchr(refname, ' ');
475 if (msg) {
476 struct strbuf msg_buf = STRBUF_INIT;
477 const char *end;
479 *msg++ = '\0';
480 if (!unquote_c_style(&msg_buf, msg, &end))
481 msg = strbuf_detach(&msg_buf, NULL);
482 else
483 msg = xstrdup(msg);
484 strbuf_release(&msg_buf);
486 if (!strcmp(msg, "no match")) {
487 status = REF_STATUS_NONE;
488 free(msg);
489 msg = NULL;
491 else if (!strcmp(msg, "up to date")) {
492 status = REF_STATUS_UPTODATE;
493 free(msg);
494 msg = NULL;
496 else if (!strcmp(msg, "non-fast forward")) {
497 status = REF_STATUS_REJECT_NONFASTFORWARD;
498 free(msg);
499 msg = NULL;
503 if (ref)
504 ref = find_ref_by_name(ref, refname);
505 if (!ref)
506 ref = find_ref_by_name(remote_refs, refname);
507 if (!ref) {
508 warning("helper reported unexpected status of %s", refname);
509 continue;
512 ref->status = status;
513 ref->remote_status = msg;
515 strbuf_release(&buf);
516 return 0;
519 static int has_attribute(const char *attrs, const char *attr) {
520 int len;
521 if (!attrs)
522 return 0;
524 len = strlen(attr);
525 for (;;) {
526 const char *space = strchrnul(attrs, ' ');
527 if (len == space - attrs && !strncmp(attrs, attr, len))
528 return 1;
529 if (!*space)
530 return 0;
531 attrs = space + 1;
535 static struct ref *get_refs_list(struct transport *transport, int for_push)
537 struct helper_data *data = transport->data;
538 struct child_process *helper;
539 struct ref *ret = NULL;
540 struct ref **tail = &ret;
541 struct ref *posn;
542 struct strbuf buf = STRBUF_INIT;
544 helper = get_helper(transport);
546 if (data->push && for_push)
547 write_str_in_full(helper->in, "list for-push\n");
548 else
549 write_str_in_full(helper->in, "list\n");
551 while (1) {
552 char *eov, *eon;
553 recvline(data, &buf);
555 if (!*buf.buf)
556 break;
558 eov = strchr(buf.buf, ' ');
559 if (!eov)
560 die("Malformed response in ref list: %s", buf.buf);
561 eon = strchr(eov + 1, ' ');
562 *eov = '\0';
563 if (eon)
564 *eon = '\0';
565 *tail = alloc_ref(eov + 1);
566 if (buf.buf[0] == '@')
567 (*tail)->symref = xstrdup(buf.buf + 1);
568 else if (buf.buf[0] != '?')
569 get_sha1_hex(buf.buf, (*tail)->old_sha1);
570 if (eon) {
571 if (has_attribute(eon + 1, "unchanged")) {
572 (*tail)->status |= REF_STATUS_UPTODATE;
573 read_ref((*tail)->name, (*tail)->old_sha1);
576 tail = &((*tail)->next);
578 if (debug)
579 fprintf(stderr, "Debug: Read ref listing.\n");
580 strbuf_release(&buf);
582 for (posn = ret; posn; posn = posn->next)
583 resolve_remote_symref(posn, ret);
585 return ret;
588 int transport_helper_init(struct transport *transport, const char *name)
590 struct helper_data *data = xcalloc(sizeof(*data), 1);
591 data->name = name;
593 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
594 debug = 1;
596 transport->data = data;
597 transport->set_option = set_helper_option;
598 transport->get_refs_list = get_refs_list;
599 transport->fetch = fetch;
600 transport->push_refs = push_refs;
601 transport->disconnect = release_helper;
602 transport->smart_options = &(data->transport_options);
603 return 0;