]> git.ipfire.org Git - thirdparty/git.git/blame - transport-helper.c
Support taking over transports
[thirdparty/git.git] / transport-helper.c
CommitLineData
6eb996b5
DB
1#include "cache.h"
2#include "transport.h"
ae4efe19 3#include "quote.h"
6eb996b5
DB
4#include "run-command.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
ef08ef9e 8#include "quote.h"
72ff8943 9#include "remote.h"
6eb996b5 10
bf3c523c
IL
11static int debug;
12
6eb996b5
DB
13struct helper_data
14{
15 const char *name;
16 struct child_process *helper;
292ce46b 17 FILE *out;
ef08ef9e 18 unsigned fetch : 1,
a24a32dd 19 import : 1,
ae4efe19
SP
20 option : 1,
21 push : 1;
72ff8943
DB
22 /* These go from remote name (as in "list") to private name */
23 struct refspec *refspecs;
24 int refspec_nr;
61b075bd
IL
25 /* Transport options for fetch-pack/send-pack (should one of
26 * those be invoked).
27 */
28 struct git_transport_options transport_options;
6eb996b5
DB
29};
30
bf3c523c
IL
31static void sendline(struct helper_data *helper, struct strbuf *buffer)
32{
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");
38}
39
40static int recvline(struct helper_data *helper, struct strbuf *buffer)
41{
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);
49 }
50
51 if (debug)
52 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
53 return 0;
54}
55
56static void xchgline(struct helper_data *helper, struct strbuf *buffer)
57{
58 sendline(helper, buffer);
59 recvline(helper, buffer);
60}
61
62static void write_constant(int fd, const char *str)
63{
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");
68}
69
25d5cc48
IL
70const char *remove_ext_force(const char *url)
71{
72 if (url) {
73 const char *colon = strchr(url, ':');
74 if (colon && colon[1] == ':')
75 return colon + 2;
76 }
77 return url;
78}
79
6eb996b5
DB
80static struct child_process *get_helper(struct transport *transport)
81{
82 struct helper_data *data = transport->data;
83 struct strbuf buf = STRBUF_INIT;
84 struct child_process *helper;
72ff8943
DB
85 const char **refspecs = NULL;
86 int refspec_nr = 0;
87 int refspec_alloc = 0;
61b075bd 88 int duped;
6eb996b5
DB
89
90 if (data->helper)
91 return data->helper;
92
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;
25d5cc48 101 helper->argv[2] = remove_ext_force(transport->url);
6eb996b5
DB
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;
106
61b075bd
IL
107 /*
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.
111 *
112 */
113 duped = dup(helper->out);
114 if (duped < 0)
115 die_errno("Can't dup helper output fd");
116 data->out = xfdopen(duped, "r");
117
bf3c523c 118 write_constant(helper->in, "capabilities\n");
2d14d65c 119
6eb996b5 120 while (1) {
28ed5b35
IL
121 const char *capname;
122 int mandatory = 0;
bf3c523c 123 recvline(data, &buf);
6eb996b5
DB
124
125 if (!*buf.buf)
126 break;
28ed5b35
IL
127
128 if (*buf.buf == '*') {
129 capname = buf.buf + 1;
130 mandatory = 1;
131 } else
132 capname = buf.buf;
133
bf3c523c 134 if (debug)
28ed5b35
IL
135 fprintf(stderr, "Debug: Got cap %s\n", capname);
136 if (!strcmp(capname, "fetch"))
6eb996b5 137 data->fetch = 1;
28ed5b35 138 else if (!strcmp(capname, "option"))
ef08ef9e 139 data->option = 1;
28ed5b35 140 else if (!strcmp(capname, "push"))
ae4efe19 141 data->push = 1;
28ed5b35 142 else if (!strcmp(capname, "import"))
e65e91ed 143 data->import = 1;
28ed5b35 144 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
72ff8943
DB
145 ALLOC_GROW(refspecs,
146 refspec_nr + 1,
147 refspec_alloc);
148 refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
28ed5b35
IL
149 } else if (mandatory) {
150 die("Unknown madatory capability %s. This remote "
151 "helper probably needs newer version of Git.\n",
152 capname);
72ff8943
DB
153 }
154 }
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]);
161 }
162 free(refspecs);
6eb996b5 163 }
b962dbdc 164 strbuf_release(&buf);
bf3c523c
IL
165 if (debug)
166 fprintf(stderr, "Debug: Capabilities complete.\n");
6eb996b5
DB
167 return data->helper;
168}
169
170static int disconnect_helper(struct transport *transport)
171{
172 struct helper_data *data = transport->data;
bf3c523c
IL
173 struct strbuf buf = STRBUF_INIT;
174
6eb996b5 175 if (data->helper) {
bf3c523c
IL
176 if (debug)
177 fprintf(stderr, "Debug: Disconnecting.\n");
178 strbuf_addf(&buf, "\n");
179 sendline(data, &buf);
6eb996b5 180 close(data->helper->in);
61b075bd 181 close(data->helper->out);
292ce46b 182 fclose(data->out);
6eb996b5
DB
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;
188 }
189 return 0;
190}
191
ef08ef9e
SP
192static const char *unsupported_options[] = {
193 TRANS_OPT_UPLOADPACK,
194 TRANS_OPT_RECEIVEPACK,
195 TRANS_OPT_THIN,
196 TRANS_OPT_KEEP
197 };
198static const char *boolean_options[] = {
199 TRANS_OPT_THIN,
200 TRANS_OPT_KEEP,
201 TRANS_OPT_FOLLOWTAGS
202 };
203
204static int set_helper_option(struct transport *transport,
205 const char *name, const char *value)
206{
207 struct helper_data *data = transport->data;
ef08ef9e
SP
208 struct strbuf buf = STRBUF_INIT;
209 int i, ret, is_bool = 0;
210
bf3c523c
IL
211 get_helper(transport);
212
ef08ef9e
SP
213 if (!data->option)
214 return 1;
215
216 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
217 if (!strcmp(name, unsupported_options[i]))
218 return 1;
219 }
220
221 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
222 if (!strcmp(name, boolean_options[i])) {
223 is_bool = 1;
224 break;
225 }
226 }
227
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');
234
bf3c523c 235 xchgline(data, &buf);
ef08ef9e
SP
236
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;
246 }
247 strbuf_release(&buf);
248 return ret;
249}
250
251static void standard_options(struct transport *t)
252{
253 char buf[16];
254 int n;
255 int v = t->verbose;
256 int no_progress = v < 0 || (!t->progress && !isatty(1));
257
258 set_helper_option(t, "progress", !no_progress ? "true" : "false");
259
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);
264}
265
f2a37151
DB
266static int release_helper(struct transport *transport)
267{
72ff8943
DB
268 struct helper_data *data = transport->data;
269 free_refspec(data->refspec_nr, data->refspecs);
270 data->refspecs = NULL;
f2a37151
DB
271 disconnect_helper(transport);
272 free(transport->data);
273 return 0;
274}
275
6eb996b5 276static int fetch_with_fetch(struct transport *transport,
37148311 277 int nr_heads, struct ref **to_fetch)
6eb996b5 278{
292ce46b 279 struct helper_data *data = transport->data;
6eb996b5
DB
280 int i;
281 struct strbuf buf = STRBUF_INIT;
282
ef08ef9e
SP
283 standard_options(transport);
284
6eb996b5 285 for (i = 0; i < nr_heads; i++) {
1088261f 286 const struct ref *posn = to_fetch[i];
6eb996b5
DB
287 if (posn->status & REF_STATUS_UPTODATE)
288 continue;
2d14d65c
DB
289
290 strbuf_addf(&buf, "fetch %s %s\n",
291 sha1_to_hex(posn->old_sha1), posn->name);
292ce46b 292 }
2d14d65c 293
292ce46b 294 strbuf_addch(&buf, '\n');
bf3c523c 295 sendline(data, &buf);
292ce46b
SP
296
297 while (1) {
bf3c523c 298 recvline(data, &buf);
292ce46b
SP
299
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);
306 }
307 else if (!buf.len)
308 break;
309 else
310 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
6eb996b5 311 }
292ce46b 312 strbuf_release(&buf);
6eb996b5
DB
313 return 0;
314}
315
e65e91ed
DB
316static int get_importer(struct transport *transport, struct child_process *fastimport)
317{
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";
324
325 fastimport->git_cmd = 1;
326 return start_command(fastimport);
327}
328
329static int fetch_with_import(struct transport *transport,
330 int nr_heads, struct ref **to_fetch)
331{
332 struct child_process fastimport;
72ff8943 333 struct helper_data *data = transport->data;
e65e91ed
DB
334 int i;
335 struct ref *posn;
336 struct strbuf buf = STRBUF_INIT;
337
bf3c523c
IL
338 get_helper(transport);
339
e65e91ed
DB
340 if (get_importer(transport, &fastimport))
341 die("Couldn't run fast-import");
342
343 for (i = 0; i < nr_heads; i++) {
344 posn = to_fetch[i];
345 if (posn->status & REF_STATUS_UPTODATE)
346 continue;
347
348 strbuf_addf(&buf, "import %s\n", posn->name);
bf3c523c 349 sendline(data, &buf);
e65e91ed
DB
350 strbuf_reset(&buf);
351 }
352 disconnect_helper(transport);
353 finish_command(&fastimport);
b962dbdc
SR
354 free(fastimport.argv);
355 fastimport.argv = NULL;
e65e91ed
DB
356
357 for (i = 0; i < nr_heads; i++) {
72ff8943 358 char *private;
e65e91ed
DB
359 posn = to_fetch[i];
360 if (posn->status & REF_STATUS_UPTODATE)
361 continue;
72ff8943
DB
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);
e65e91ed 368 }
b962dbdc 369 strbuf_release(&buf);
e65e91ed
DB
370 return 0;
371}
372
6eb996b5 373static int fetch(struct transport *transport,
37148311 374 int nr_heads, struct ref **to_fetch)
6eb996b5
DB
375{
376 struct helper_data *data = transport->data;
377 int i, count;
378
379 count = 0;
380 for (i = 0; i < nr_heads; i++)
381 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
382 count++;
383
384 if (!count)
385 return 0;
386
387 if (data->fetch)
388 return fetch_with_fetch(transport, nr_heads, to_fetch);
389
e65e91ed
DB
390 if (data->import)
391 return fetch_with_import(transport, nr_heads, to_fetch);
392
6eb996b5
DB
393 return -1;
394}
395
ae4efe19
SP
396static int push_refs(struct transport *transport,
397 struct ref *remote_refs, int flags)
398{
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;
405
406 if (!remote_refs)
407 return 0;
408
409 helper = get_helper(transport);
410 if (!data->push)
411 return 1;
412
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;
418
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;
424 }
425
426 if (force_all)
427 ref->force = 1;
428
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));
437 }
438 strbuf_addch(&buf, ':');
439 strbuf_addstr(&buf, ref->name);
440 strbuf_addch(&buf, '\n');
441 }
d8f67d20
CB
442 if (buf.len == 0)
443 return 0;
ae4efe19
SP
444
445 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
446 standard_options(transport);
447
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);
451 }
452
453 strbuf_addch(&buf, '\n');
bf3c523c 454 sendline(data, &buf);
ae4efe19
SP
455
456 ref = remote_refs;
457 while (1) {
458 char *refname, *msg;
459 int status;
460
bf3c523c 461 recvline(data, &buf);
ae4efe19
SP
462 if (!buf.len)
463 break;
464
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);
473
474 msg = strchr(refname, ' ');
475 if (msg) {
476 struct strbuf msg_buf = STRBUF_INIT;
477 const char *end;
478
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);
485
486 if (!strcmp(msg, "no match")) {
487 status = REF_STATUS_NONE;
488 free(msg);
489 msg = NULL;
490 }
491 else if (!strcmp(msg, "up to date")) {
492 status = REF_STATUS_UPTODATE;
493 free(msg);
494 msg = NULL;
495 }
496 else if (!strcmp(msg, "non-fast forward")) {
497 status = REF_STATUS_REJECT_NONFASTFORWARD;
498 free(msg);
499 msg = NULL;
500 }
501 }
502
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;
510 }
511
512 ref->status = status;
513 ref->remote_status = msg;
514 }
515 strbuf_release(&buf);
516 return 0;
517}
518
f8ec9167
DB
519static int has_attribute(const char *attrs, const char *attr) {
520 int len;
521 if (!attrs)
522 return 0;
523
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;
532 }
533}
534
6eb996b5
DB
535static struct ref *get_refs_list(struct transport *transport, int for_push)
536{
292ce46b 537 struct helper_data *data = transport->data;
6eb996b5
DB
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;
6eb996b5
DB
543
544 helper = get_helper(transport);
2d14d65c 545
ae4efe19
SP
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");
6eb996b5 550
6eb996b5
DB
551 while (1) {
552 char *eov, *eon;
bf3c523c 553 recvline(data, &buf);
6eb996b5
DB
554
555 if (!*buf.buf)
556 break;
557
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);
f8ec9167
DB
570 if (eon) {
571 if (has_attribute(eon + 1, "unchanged")) {
572 (*tail)->status |= REF_STATUS_UPTODATE;
573 read_ref((*tail)->name, (*tail)->old_sha1);
574 }
575 }
6eb996b5
DB
576 tail = &((*tail)->next);
577 }
bf3c523c
IL
578 if (debug)
579 fprintf(stderr, "Debug: Read ref listing.\n");
6eb996b5
DB
580 strbuf_release(&buf);
581
582 for (posn = ret; posn; posn = posn->next)
583 resolve_remote_symref(posn, ret);
584
585 return ret;
586}
587
c9e388bb 588int transport_helper_init(struct transport *transport, const char *name)
6eb996b5
DB
589{
590 struct helper_data *data = xcalloc(sizeof(*data), 1);
c9e388bb 591 data->name = name;
6eb996b5 592
bf3c523c
IL
593 if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
594 debug = 1;
595
6eb996b5 596 transport->data = data;
ef08ef9e 597 transport->set_option = set_helper_option;
6eb996b5
DB
598 transport->get_refs_list = get_refs_list;
599 transport->fetch = fetch;
ae4efe19 600 transport->push_refs = push_refs;
f2a37151 601 transport->disconnect = release_helper;
61b075bd 602 transport->smart_options = &(data->transport_options);
6eb996b5
DB
603 return 0;
604}