Merge branch 'pc/remove-warn'
[git] / transport-helper.c
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"
10
11 static int debug;
12
13 struct helper_data
14 {
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                 connect : 1,
23                 no_disconnect_req : 1;
24         /* These go from remote name (as in "list") to private name */
25         struct refspec *refspecs;
26         int refspec_nr;
27         /* Transport options for fetch-pack/send-pack (should one of
28          * those be invoked).
29          */
30         struct git_transport_options transport_options;
31 };
32
33 static void sendline(struct helper_data *helper, struct strbuf *buffer)
34 {
35         if (debug)
36                 fprintf(stderr, "Debug: Remote helper: -> %s", buffer->buf);
37         if (write_in_full(helper->helper->in, buffer->buf, buffer->len)
38                 != buffer->len)
39                 die_errno("Full write to remote helper failed");
40 }
41
42 static int recvline_fh(FILE *helper, struct strbuf *buffer)
43 {
44         strbuf_reset(buffer);
45         if (debug)
46                 fprintf(stderr, "Debug: Remote helper: Waiting...\n");
47         if (strbuf_getline(buffer, helper, '\n') == EOF) {
48                 if (debug)
49                         fprintf(stderr, "Debug: Remote helper quit.\n");
50                 exit(128);
51         }
52
53         if (debug)
54                 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
55         return 0;
56 }
57
58 static int recvline(struct helper_data *helper, struct strbuf *buffer)
59 {
60         return recvline_fh(helper->out, buffer);
61 }
62
63 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
64 {
65         sendline(helper, buffer);
66         recvline(helper, buffer);
67 }
68
69 static void write_constant(int fd, const char *str)
70 {
71         if (debug)
72                 fprintf(stderr, "Debug: Remote helper: -> %s", str);
73         if (write_in_full(fd, str, strlen(str)) != strlen(str))
74                 die_errno("Full write to remote helper failed");
75 }
76
77 const char *remove_ext_force(const char *url)
78 {
79         if (url) {
80                 const char *colon = strchr(url, ':');
81                 if (colon && colon[1] == ':')
82                         return colon + 2;
83         }
84         return url;
85 }
86
87 static void do_take_over(struct transport *transport)
88 {
89         struct helper_data *data;
90         data = (struct helper_data *)transport->data;
91         transport_take_over(transport, data->helper);
92         fclose(data->out);
93         free(data);
94 }
95
96 static struct child_process *get_helper(struct transport *transport)
97 {
98         struct helper_data *data = transport->data;
99         struct strbuf buf = STRBUF_INIT;
100         struct child_process *helper;
101         const char **refspecs = NULL;
102         int refspec_nr = 0;
103         int refspec_alloc = 0;
104         int duped;
105         int code;
106
107         if (data->helper)
108                 return data->helper;
109
110         helper = xcalloc(1, sizeof(*helper));
111         helper->in = -1;
112         helper->out = -1;
113         helper->err = 0;
114         helper->argv = xcalloc(4, sizeof(*helper->argv));
115         strbuf_addf(&buf, "git-remote-%s", data->name);
116         helper->argv[0] = strbuf_detach(&buf, NULL);
117         helper->argv[1] = transport->remote->name;
118         helper->argv[2] = remove_ext_force(transport->url);
119         helper->git_cmd = 0;
120         helper->silent_exec_failure = 1;
121         code = start_command(helper);
122         if (code < 0 && errno == ENOENT)
123                 die("Unable to find remote helper for '%s'", data->name);
124         else if (code != 0)
125                 exit(code);
126
127         data->helper = helper;
128         data->no_disconnect_req = 0;
129
130         /*
131          * Open the output as FILE* so strbuf_getline() can be used.
132          * Do this with duped fd because fclose() will close the fd,
133          * and stuff like taking over will require the fd to remain.
134          */
135         duped = dup(helper->out);
136         if (duped < 0)
137                 die_errno("Can't dup helper output fd");
138         data->out = xfdopen(duped, "r");
139
140         write_constant(helper->in, "capabilities\n");
141
142         while (1) {
143                 const char *capname;
144                 int mandatory = 0;
145                 recvline(data, &buf);
146
147                 if (!*buf.buf)
148                         break;
149
150                 if (*buf.buf == '*') {
151                         capname = buf.buf + 1;
152                         mandatory = 1;
153                 } else
154                         capname = buf.buf;
155
156                 if (debug)
157                         fprintf(stderr, "Debug: Got cap %s\n", capname);
158                 if (!strcmp(capname, "fetch"))
159                         data->fetch = 1;
160                 else if (!strcmp(capname, "option"))
161                         data->option = 1;
162                 else if (!strcmp(capname, "push"))
163                         data->push = 1;
164                 else if (!strcmp(capname, "import"))
165                         data->import = 1;
166                 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
167                         ALLOC_GROW(refspecs,
168                                    refspec_nr + 1,
169                                    refspec_alloc);
170                         refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
171                 } else if (!strcmp(capname, "connect")) {
172                         data->connect = 1;
173                 } else if (mandatory) {
174                         die("Unknown mandatory capability %s. This remote "
175                             "helper probably needs newer version of Git.\n",
176                             capname);
177                 }
178         }
179         if (refspecs) {
180                 int i;
181                 data->refspec_nr = refspec_nr;
182                 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
183                 for (i = 0; i < refspec_nr; i++) {
184                         free((char *)refspecs[i]);
185                 }
186                 free(refspecs);
187         }
188         strbuf_release(&buf);
189         if (debug)
190                 fprintf(stderr, "Debug: Capabilities complete.\n");
191         return data->helper;
192 }
193
194 static int disconnect_helper(struct transport *transport)
195 {
196         struct helper_data *data = transport->data;
197         struct strbuf buf = STRBUF_INIT;
198
199         if (data->helper) {
200                 if (debug)
201                         fprintf(stderr, "Debug: Disconnecting.\n");
202                 if (!data->no_disconnect_req) {
203                         strbuf_addf(&buf, "\n");
204                         sendline(data, &buf);
205                 }
206                 close(data->helper->in);
207                 close(data->helper->out);
208                 fclose(data->out);
209                 finish_command(data->helper);
210                 free((char *)data->helper->argv[0]);
211                 free(data->helper->argv);
212                 free(data->helper);
213                 data->helper = NULL;
214         }
215         return 0;
216 }
217
218 static const char *unsupported_options[] = {
219         TRANS_OPT_UPLOADPACK,
220         TRANS_OPT_RECEIVEPACK,
221         TRANS_OPT_THIN,
222         TRANS_OPT_KEEP
223         };
224 static const char *boolean_options[] = {
225         TRANS_OPT_THIN,
226         TRANS_OPT_KEEP,
227         TRANS_OPT_FOLLOWTAGS
228         };
229
230 static int set_helper_option(struct transport *transport,
231                           const char *name, const char *value)
232 {
233         struct helper_data *data = transport->data;
234         struct strbuf buf = STRBUF_INIT;
235         int i, ret, is_bool = 0;
236
237         get_helper(transport);
238
239         if (!data->option)
240                 return 1;
241
242         for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
243                 if (!strcmp(name, unsupported_options[i]))
244                         return 1;
245         }
246
247         for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
248                 if (!strcmp(name, boolean_options[i])) {
249                         is_bool = 1;
250                         break;
251                 }
252         }
253
254         strbuf_addf(&buf, "option %s ", name);
255         if (is_bool)
256                 strbuf_addstr(&buf, value ? "true" : "false");
257         else
258                 quote_c_style(value, &buf, NULL, 0);
259         strbuf_addch(&buf, '\n');
260
261         xchgline(data, &buf);
262
263         if (!strcmp(buf.buf, "ok"))
264                 ret = 0;
265         else if (!prefixcmp(buf.buf, "error")) {
266                 ret = -1;
267         } else if (!strcmp(buf.buf, "unsupported"))
268                 ret = 1;
269         else {
270                 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
271                 ret = 1;
272         }
273         strbuf_release(&buf);
274         return ret;
275 }
276
277 static void standard_options(struct transport *t)
278 {
279         char buf[16];
280         int n;
281         int v = t->verbose;
282
283         set_helper_option(t, "progress", t->progress ? "true" : "false");
284
285         n = snprintf(buf, sizeof(buf), "%d", v + 1);
286         if (n >= sizeof(buf))
287                 die("impossibly large verbosity value");
288         set_helper_option(t, "verbosity", buf);
289 }
290
291 static int release_helper(struct transport *transport)
292 {
293         struct helper_data *data = transport->data;
294         free_refspec(data->refspec_nr, data->refspecs);
295         data->refspecs = NULL;
296         disconnect_helper(transport);
297         free(transport->data);
298         return 0;
299 }
300
301 static int fetch_with_fetch(struct transport *transport,
302                             int nr_heads, struct ref **to_fetch)
303 {
304         struct helper_data *data = transport->data;
305         int i;
306         struct strbuf buf = STRBUF_INIT;
307
308         standard_options(transport);
309
310         for (i = 0; i < nr_heads; i++) {
311                 const struct ref *posn = to_fetch[i];
312                 if (posn->status & REF_STATUS_UPTODATE)
313                         continue;
314
315                 strbuf_addf(&buf, "fetch %s %s\n",
316                             sha1_to_hex(posn->old_sha1), posn->name);
317         }
318
319         strbuf_addch(&buf, '\n');
320         sendline(data, &buf);
321
322         while (1) {
323                 recvline(data, &buf);
324
325                 if (!prefixcmp(buf.buf, "lock ")) {
326                         const char *name = buf.buf + 5;
327                         if (transport->pack_lockfile)
328                                 warning("%s also locked %s", data->name, name);
329                         else
330                                 transport->pack_lockfile = xstrdup(name);
331                 }
332                 else if (!buf.len)
333                         break;
334                 else
335                         warning("%s unexpectedly said: '%s'", data->name, buf.buf);
336         }
337         strbuf_release(&buf);
338         return 0;
339 }
340
341 static int get_importer(struct transport *transport, struct child_process *fastimport)
342 {
343         struct child_process *helper = get_helper(transport);
344         memset(fastimport, 0, sizeof(*fastimport));
345         fastimport->in = helper->out;
346         fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
347         fastimport->argv[0] = "fast-import";
348         fastimport->argv[1] = "--quiet";
349
350         fastimport->git_cmd = 1;
351         return start_command(fastimport);
352 }
353
354 static int fetch_with_import(struct transport *transport,
355                              int nr_heads, struct ref **to_fetch)
356 {
357         struct child_process fastimport;
358         struct helper_data *data = transport->data;
359         int i;
360         struct ref *posn;
361         struct strbuf buf = STRBUF_INIT;
362
363         get_helper(transport);
364
365         if (get_importer(transport, &fastimport))
366                 die("Couldn't run fast-import");
367
368         for (i = 0; i < nr_heads; i++) {
369                 posn = to_fetch[i];
370                 if (posn->status & REF_STATUS_UPTODATE)
371                         continue;
372
373                 strbuf_addf(&buf, "import %s\n", posn->name);
374                 sendline(data, &buf);
375                 strbuf_reset(&buf);
376         }
377         disconnect_helper(transport);
378         finish_command(&fastimport);
379         free(fastimport.argv);
380         fastimport.argv = NULL;
381
382         for (i = 0; i < nr_heads; i++) {
383                 char *private;
384                 posn = to_fetch[i];
385                 if (posn->status & REF_STATUS_UPTODATE)
386                         continue;
387                 if (data->refspecs)
388                         private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
389                 else
390                         private = strdup(posn->name);
391                 read_ref(private, posn->old_sha1);
392                 free(private);
393         }
394         strbuf_release(&buf);
395         return 0;
396 }
397
398 static int process_connect_service(struct transport *transport,
399                                    const char *name, const char *exec)
400 {
401         struct helper_data *data = transport->data;
402         struct strbuf cmdbuf = STRBUF_INIT;
403         struct child_process *helper;
404         int r, duped, ret = 0;
405         FILE *input;
406
407         helper = get_helper(transport);
408
409         /*
410          * Yes, dup the pipe another time, as we need unbuffered version
411          * of input pipe as FILE*. fclose() closes the underlying fd and
412          * stream buffering only can be changed before first I/O operation
413          * on it.
414          */
415         duped = dup(helper->out);
416         if (duped < 0)
417                 die_errno("Can't dup helper output fd");
418         input = xfdopen(duped, "r");
419         setvbuf(input, NULL, _IONBF, 0);
420
421         /*
422          * Handle --upload-pack and friends. This is fire and forget...
423          * just warn if it fails.
424          */
425         if (strcmp(name, exec)) {
426                 r = set_helper_option(transport, "servpath", exec);
427                 if (r > 0)
428                         warning("Setting remote service path not supported by protocol.");
429                 else if (r < 0)
430                         warning("Invalid remote service path.");
431         }
432
433         if (data->connect)
434                 strbuf_addf(&cmdbuf, "connect %s\n", name);
435         else
436                 goto exit;
437
438         sendline(data, &cmdbuf);
439         recvline_fh(input, &cmdbuf);
440         if (!strcmp(cmdbuf.buf, "")) {
441                 data->no_disconnect_req = 1;
442                 if (debug)
443                         fprintf(stderr, "Debug: Smart transport connection "
444                                 "ready.\n");
445                 ret = 1;
446         } else if (!strcmp(cmdbuf.buf, "fallback")) {
447                 if (debug)
448                         fprintf(stderr, "Debug: Falling back to dumb "
449                                 "transport.\n");
450         } else
451                 die("Unknown response to connect: %s",
452                         cmdbuf.buf);
453
454 exit:
455         fclose(input);
456         return ret;
457 }
458
459 static int process_connect(struct transport *transport,
460                                      int for_push)
461 {
462         struct helper_data *data = transport->data;
463         const char *name;
464         const char *exec;
465
466         name = for_push ? "git-receive-pack" : "git-upload-pack";
467         if (for_push)
468                 exec = data->transport_options.receivepack;
469         else
470                 exec = data->transport_options.uploadpack;
471
472         return process_connect_service(transport, name, exec);
473 }
474
475 static int connect_helper(struct transport *transport, const char *name,
476                    const char *exec, int fd[2])
477 {
478         struct helper_data *data = transport->data;
479
480         /* Get_helper so connect is inited. */
481         get_helper(transport);
482         if (!data->connect)
483                 die("Operation not supported by protocol.");
484
485         if (!process_connect_service(transport, name, exec))
486                 die("Can't connect to subservice %s.", name);
487
488         fd[0] = data->helper->out;
489         fd[1] = data->helper->in;
490         return 0;
491 }
492
493 static int fetch(struct transport *transport,
494                  int nr_heads, struct ref **to_fetch)
495 {
496         struct helper_data *data = transport->data;
497         int i, count;
498
499         if (process_connect(transport, 0)) {
500                 do_take_over(transport);
501                 return transport->fetch(transport, nr_heads, to_fetch);
502         }
503
504         count = 0;
505         for (i = 0; i < nr_heads; i++)
506                 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
507                         count++;
508
509         if (!count)
510                 return 0;
511
512         if (data->fetch)
513                 return fetch_with_fetch(transport, nr_heads, to_fetch);
514
515         if (data->import)
516                 return fetch_with_import(transport, nr_heads, to_fetch);
517
518         return -1;
519 }
520
521 static int push_refs(struct transport *transport,
522                 struct ref *remote_refs, int flags)
523 {
524         int force_all = flags & TRANSPORT_PUSH_FORCE;
525         int mirror = flags & TRANSPORT_PUSH_MIRROR;
526         struct helper_data *data = transport->data;
527         struct strbuf buf = STRBUF_INIT;
528         struct child_process *helper;
529         struct ref *ref;
530
531         if (process_connect(transport, 1)) {
532                 do_take_over(transport);
533                 return transport->push_refs(transport, remote_refs, flags);
534         }
535
536         if (!remote_refs) {
537                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
538                         "Perhaps you should specify a branch such as 'master'.\n");
539                 return 0;
540         }
541
542         helper = get_helper(transport);
543         if (!data->push)
544                 return 1;
545
546         for (ref = remote_refs; ref; ref = ref->next) {
547                 if (!ref->peer_ref && !mirror)
548                         continue;
549
550                 /* Check for statuses set by set_ref_status_for_push() */
551                 switch (ref->status) {
552                 case REF_STATUS_REJECT_NONFASTFORWARD:
553                 case REF_STATUS_UPTODATE:
554                         continue;
555                 default:
556                         ; /* do nothing */
557                 }
558
559                 if (force_all)
560                         ref->force = 1;
561
562                 strbuf_addstr(&buf, "push ");
563                 if (!ref->deletion) {
564                         if (ref->force)
565                                 strbuf_addch(&buf, '+');
566                         if (ref->peer_ref)
567                                 strbuf_addstr(&buf, ref->peer_ref->name);
568                         else
569                                 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
570                 }
571                 strbuf_addch(&buf, ':');
572                 strbuf_addstr(&buf, ref->name);
573                 strbuf_addch(&buf, '\n');
574         }
575         if (buf.len == 0)
576                 return 0;
577
578         standard_options(transport);
579
580         if (flags & TRANSPORT_PUSH_DRY_RUN) {
581                 if (set_helper_option(transport, "dry-run", "true") != 0)
582                         die("helper %s does not support dry-run", data->name);
583         }
584
585         strbuf_addch(&buf, '\n');
586         sendline(data, &buf);
587
588         ref = remote_refs;
589         while (1) {
590                 char *refname, *msg;
591                 int status;
592
593                 recvline(data, &buf);
594                 if (!buf.len)
595                         break;
596
597                 if (!prefixcmp(buf.buf, "ok ")) {
598                         status = REF_STATUS_OK;
599                         refname = buf.buf + 3;
600                 } else if (!prefixcmp(buf.buf, "error ")) {
601                         status = REF_STATUS_REMOTE_REJECT;
602                         refname = buf.buf + 6;
603                 } else
604                         die("expected ok/error, helper said '%s'\n", buf.buf);
605
606                 msg = strchr(refname, ' ');
607                 if (msg) {
608                         struct strbuf msg_buf = STRBUF_INIT;
609                         const char *end;
610
611                         *msg++ = '\0';
612                         if (!unquote_c_style(&msg_buf, msg, &end))
613                                 msg = strbuf_detach(&msg_buf, NULL);
614                         else
615                                 msg = xstrdup(msg);
616                         strbuf_release(&msg_buf);
617
618                         if (!strcmp(msg, "no match")) {
619                                 status = REF_STATUS_NONE;
620                                 free(msg);
621                                 msg = NULL;
622                         }
623                         else if (!strcmp(msg, "up to date")) {
624                                 status = REF_STATUS_UPTODATE;
625                                 free(msg);
626                                 msg = NULL;
627                         }
628                         else if (!strcmp(msg, "non-fast forward")) {
629                                 status = REF_STATUS_REJECT_NONFASTFORWARD;
630                                 free(msg);
631                                 msg = NULL;
632                         }
633                 }
634
635                 if (ref)
636                         ref = find_ref_by_name(ref, refname);
637                 if (!ref)
638                         ref = find_ref_by_name(remote_refs, refname);
639                 if (!ref) {
640                         warning("helper reported unexpected status of %s", refname);
641                         continue;
642                 }
643
644                 if (ref->status != REF_STATUS_NONE) {
645                         /*
646                          * Earlier, the ref was marked not to be pushed, so ignore the ref
647                          * status reported by the remote helper if the latter is 'no match'.
648                          */
649                         if (status == REF_STATUS_NONE)
650                                 continue;
651                 }
652
653                 ref->status = status;
654                 ref->remote_status = msg;
655         }
656         strbuf_release(&buf);
657         return 0;
658 }
659
660 static int has_attribute(const char *attrs, const char *attr) {
661         int len;
662         if (!attrs)
663                 return 0;
664
665         len = strlen(attr);
666         for (;;) {
667                 const char *space = strchrnul(attrs, ' ');
668                 if (len == space - attrs && !strncmp(attrs, attr, len))
669                         return 1;
670                 if (!*space)
671                         return 0;
672                 attrs = space + 1;
673         }
674 }
675
676 static struct ref *get_refs_list(struct transport *transport, int for_push)
677 {
678         struct helper_data *data = transport->data;
679         struct child_process *helper;
680         struct ref *ret = NULL;
681         struct ref **tail = &ret;
682         struct ref *posn;
683         struct strbuf buf = STRBUF_INIT;
684
685         helper = get_helper(transport);
686
687         if (process_connect(transport, for_push)) {
688                 do_take_over(transport);
689                 return transport->get_refs_list(transport, for_push);
690         }
691
692         if (data->push && for_push)
693                 write_str_in_full(helper->in, "list for-push\n");
694         else
695                 write_str_in_full(helper->in, "list\n");
696
697         while (1) {
698                 char *eov, *eon;
699                 recvline(data, &buf);
700
701                 if (!*buf.buf)
702                         break;
703
704                 eov = strchr(buf.buf, ' ');
705                 if (!eov)
706                         die("Malformed response in ref list: %s", buf.buf);
707                 eon = strchr(eov + 1, ' ');
708                 *eov = '\0';
709                 if (eon)
710                         *eon = '\0';
711                 *tail = alloc_ref(eov + 1);
712                 if (buf.buf[0] == '@')
713                         (*tail)->symref = xstrdup(buf.buf + 1);
714                 else if (buf.buf[0] != '?')
715                         get_sha1_hex(buf.buf, (*tail)->old_sha1);
716                 if (eon) {
717                         if (has_attribute(eon + 1, "unchanged")) {
718                                 (*tail)->status |= REF_STATUS_UPTODATE;
719                                 read_ref((*tail)->name, (*tail)->old_sha1);
720                         }
721                 }
722                 tail = &((*tail)->next);
723         }
724         if (debug)
725                 fprintf(stderr, "Debug: Read ref listing.\n");
726         strbuf_release(&buf);
727
728         for (posn = ret; posn; posn = posn->next)
729                 resolve_remote_symref(posn, ret);
730
731         return ret;
732 }
733
734 int transport_helper_init(struct transport *transport, const char *name)
735 {
736         struct helper_data *data = xcalloc(sizeof(*data), 1);
737         data->name = name;
738
739         if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
740                 debug = 1;
741
742         transport->data = data;
743         transport->set_option = set_helper_option;
744         transport->get_refs_list = get_refs_list;
745         transport->fetch = fetch;
746         transport->push_refs = push_refs;
747         transport->disconnect = release_helper;
748         transport->connect = connect_helper;
749         transport->smart_options = &(data->transport_options);
750         return 0;
751 }