Code

Merge branch 'maint'
[git.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"
11 static int debug;
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 };
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 }
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         }
53         if (debug)
54                 fprintf(stderr, "Debug: Remote helper: <- %s\n", buffer->buf);
55         return 0;
56 }
58 static int recvline(struct helper_data *helper, struct strbuf *buffer)
59 {
60         return recvline_fh(helper->out, buffer);
61 }
63 static void xchgline(struct helper_data *helper, struct strbuf *buffer)
64 {
65         sendline(helper, buffer);
66         recvline(helper, buffer);
67 }
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 }
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 }
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 }
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;
106         if (data->helper)
107                 return data->helper;
109         helper = xcalloc(1, sizeof(*helper));
110         helper->in = -1;
111         helper->out = -1;
112         helper->err = 0;
113         helper->argv = xcalloc(4, sizeof(*helper->argv));
114         strbuf_addf(&buf, "remote-%s", data->name);
115         helper->argv[0] = strbuf_detach(&buf, NULL);
116         helper->argv[1] = transport->remote->name;
117         helper->argv[2] = remove_ext_force(transport->url);
118         helper->git_cmd = 1;
119         if (start_command(helper))
120                 die("Unable to run helper: git %s", helper->argv[0]);
121         data->helper = helper;
122         data->no_disconnect_req = 0;
124         /*
125          * Open the output as FILE* so strbuf_getline() can be used.
126          * Do this with duped fd because fclose() will close the fd,
127          * and stuff like taking over will require the fd to remain.
128          */
129         duped = dup(helper->out);
130         if (duped < 0)
131                 die_errno("Can't dup helper output fd");
132         data->out = xfdopen(duped, "r");
134         write_constant(helper->in, "capabilities\n");
136         while (1) {
137                 const char *capname;
138                 int mandatory = 0;
139                 recvline(data, &buf);
141                 if (!*buf.buf)
142                         break;
144                 if (*buf.buf == '*') {
145                         capname = buf.buf + 1;
146                         mandatory = 1;
147                 } else
148                         capname = buf.buf;
150                 if (debug)
151                         fprintf(stderr, "Debug: Got cap %s\n", capname);
152                 if (!strcmp(capname, "fetch"))
153                         data->fetch = 1;
154                 else if (!strcmp(capname, "option"))
155                         data->option = 1;
156                 else if (!strcmp(capname, "push"))
157                         data->push = 1;
158                 else if (!strcmp(capname, "import"))
159                         data->import = 1;
160                 else if (!data->refspecs && !prefixcmp(capname, "refspec ")) {
161                         ALLOC_GROW(refspecs,
162                                    refspec_nr + 1,
163                                    refspec_alloc);
164                         refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
165                 } else if (!strcmp(capname, "connect")) {
166                         data->connect = 1;
167                 } else if (mandatory) {
168                         die("Unknown madatory capability %s. This remote "
169                             "helper probably needs newer version of Git.\n",
170                             capname);
171                 }
172         }
173         if (refspecs) {
174                 int i;
175                 data->refspec_nr = refspec_nr;
176                 data->refspecs = parse_fetch_refspec(refspec_nr, refspecs);
177                 for (i = 0; i < refspec_nr; i++) {
178                         free((char *)refspecs[i]);
179                 }
180                 free(refspecs);
181         }
182         strbuf_release(&buf);
183         if (debug)
184                 fprintf(stderr, "Debug: Capabilities complete.\n");
185         return data->helper;
188 static int disconnect_helper(struct transport *transport)
190         struct helper_data *data = transport->data;
191         struct strbuf buf = STRBUF_INIT;
193         if (data->helper) {
194                 if (debug)
195                         fprintf(stderr, "Debug: Disconnecting.\n");
196                 if (!data->no_disconnect_req) {
197                         strbuf_addf(&buf, "\n");
198                         sendline(data, &buf);
199                 }
200                 close(data->helper->in);
201                 close(data->helper->out);
202                 fclose(data->out);
203                 finish_command(data->helper);
204                 free((char *)data->helper->argv[0]);
205                 free(data->helper->argv);
206                 free(data->helper);
207                 data->helper = NULL;
208         }
209         return 0;
212 static const char *unsupported_options[] = {
213         TRANS_OPT_UPLOADPACK,
214         TRANS_OPT_RECEIVEPACK,
215         TRANS_OPT_THIN,
216         TRANS_OPT_KEEP
217         };
218 static const char *boolean_options[] = {
219         TRANS_OPT_THIN,
220         TRANS_OPT_KEEP,
221         TRANS_OPT_FOLLOWTAGS
222         };
224 static int set_helper_option(struct transport *transport,
225                           const char *name, const char *value)
227         struct helper_data *data = transport->data;
228         struct strbuf buf = STRBUF_INIT;
229         int i, ret, is_bool = 0;
231         get_helper(transport);
233         if (!data->option)
234                 return 1;
236         for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
237                 if (!strcmp(name, unsupported_options[i]))
238                         return 1;
239         }
241         for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
242                 if (!strcmp(name, boolean_options[i])) {
243                         is_bool = 1;
244                         break;
245                 }
246         }
248         strbuf_addf(&buf, "option %s ", name);
249         if (is_bool)
250                 strbuf_addstr(&buf, value ? "true" : "false");
251         else
252                 quote_c_style(value, &buf, NULL, 0);
253         strbuf_addch(&buf, '\n');
255         xchgline(data, &buf);
257         if (!strcmp(buf.buf, "ok"))
258                 ret = 0;
259         else if (!prefixcmp(buf.buf, "error")) {
260                 ret = -1;
261         } else if (!strcmp(buf.buf, "unsupported"))
262                 ret = 1;
263         else {
264                 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
265                 ret = 1;
266         }
267         strbuf_release(&buf);
268         return ret;
271 static void standard_options(struct transport *t)
273         char buf[16];
274         int n;
275         int v = t->verbose;
276         int no_progress = v < 0 || (!t->progress && !isatty(2));
278         set_helper_option(t, "progress", !no_progress ? "true" : "false");
280         n = snprintf(buf, sizeof(buf), "%d", v + 1);
281         if (n >= sizeof(buf))
282                 die("impossibly large verbosity value");
283         set_helper_option(t, "verbosity", buf);
286 static int release_helper(struct transport *transport)
288         struct helper_data *data = transport->data;
289         free_refspec(data->refspec_nr, data->refspecs);
290         data->refspecs = NULL;
291         disconnect_helper(transport);
292         free(transport->data);
293         return 0;
296 static int fetch_with_fetch(struct transport *transport,
297                             int nr_heads, struct ref **to_fetch)
299         struct helper_data *data = transport->data;
300         int i;
301         struct strbuf buf = STRBUF_INIT;
303         standard_options(transport);
305         for (i = 0; i < nr_heads; i++) {
306                 const struct ref *posn = to_fetch[i];
307                 if (posn->status & REF_STATUS_UPTODATE)
308                         continue;
310                 strbuf_addf(&buf, "fetch %s %s\n",
311                             sha1_to_hex(posn->old_sha1), posn->name);
312         }
314         strbuf_addch(&buf, '\n');
315         sendline(data, &buf);
317         while (1) {
318                 recvline(data, &buf);
320                 if (!prefixcmp(buf.buf, "lock ")) {
321                         const char *name = buf.buf + 5;
322                         if (transport->pack_lockfile)
323                                 warning("%s also locked %s", data->name, name);
324                         else
325                                 transport->pack_lockfile = xstrdup(name);
326                 }
327                 else if (!buf.len)
328                         break;
329                 else
330                         warning("%s unexpectedly said: '%s'", data->name, buf.buf);
331         }
332         strbuf_release(&buf);
333         return 0;
336 static int get_importer(struct transport *transport, struct child_process *fastimport)
338         struct child_process *helper = get_helper(transport);
339         memset(fastimport, 0, sizeof(*fastimport));
340         fastimport->in = helper->out;
341         fastimport->argv = xcalloc(5, sizeof(*fastimport->argv));
342         fastimport->argv[0] = "fast-import";
343         fastimport->argv[1] = "--quiet";
345         fastimport->git_cmd = 1;
346         return start_command(fastimport);
349 static int fetch_with_import(struct transport *transport,
350                              int nr_heads, struct ref **to_fetch)
352         struct child_process fastimport;
353         struct helper_data *data = transport->data;
354         int i;
355         struct ref *posn;
356         struct strbuf buf = STRBUF_INIT;
358         get_helper(transport);
360         if (get_importer(transport, &fastimport))
361                 die("Couldn't run fast-import");
363         for (i = 0; i < nr_heads; i++) {
364                 posn = to_fetch[i];
365                 if (posn->status & REF_STATUS_UPTODATE)
366                         continue;
368                 strbuf_addf(&buf, "import %s\n", posn->name);
369                 sendline(data, &buf);
370                 strbuf_reset(&buf);
371         }
372         disconnect_helper(transport);
373         finish_command(&fastimport);
374         free(fastimport.argv);
375         fastimport.argv = NULL;
377         for (i = 0; i < nr_heads; i++) {
378                 char *private;
379                 posn = to_fetch[i];
380                 if (posn->status & REF_STATUS_UPTODATE)
381                         continue;
382                 if (data->refspecs)
383                         private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
384                 else
385                         private = strdup(posn->name);
386                 read_ref(private, posn->old_sha1);
387                 free(private);
388         }
389         strbuf_release(&buf);
390         return 0;
393 static int process_connect_service(struct transport *transport,
394                                    const char *name, const char *exec)
396         struct helper_data *data = transport->data;
397         struct strbuf cmdbuf = STRBUF_INIT;
398         struct child_process *helper;
399         int r, duped, ret = 0;
400         FILE *input;
402         helper = get_helper(transport);
404         /*
405          * Yes, dup the pipe another time, as we need unbuffered version
406          * of input pipe as FILE*. fclose() closes the underlying fd and
407          * stream buffering only can be changed before first I/O operation
408          * on it.
409          */
410         duped = dup(helper->out);
411         if (duped < 0)
412                 die_errno("Can't dup helper output fd");
413         input = xfdopen(duped, "r");
414         setvbuf(input, NULL, _IONBF, 0);
416         /*
417          * Handle --upload-pack and friends. This is fire and forget...
418          * just warn if it fails.
419          */
420         if (strcmp(name, exec)) {
421                 r = set_helper_option(transport, "servpath", exec);
422                 if (r > 0)
423                         warning("Setting remote service path not supported by protocol.");
424                 else if (r < 0)
425                         warning("Invalid remote service path.");
426         }
428         if (data->connect)
429                 strbuf_addf(&cmdbuf, "connect %s\n", name);
430         else
431                 goto exit;
433         sendline(data, &cmdbuf);
434         recvline_fh(input, &cmdbuf);
435         if (!strcmp(cmdbuf.buf, "")) {
436                 data->no_disconnect_req = 1;
437                 if (debug)
438                         fprintf(stderr, "Debug: Smart transport connection "
439                                 "ready.\n");
440                 ret = 1;
441         } else if (!strcmp(cmdbuf.buf, "fallback")) {
442                 if (debug)
443                         fprintf(stderr, "Debug: Falling back to dumb "
444                                 "transport.\n");
445         } else
446                 die("Unknown response to connect: %s",
447                         cmdbuf.buf);
449 exit:
450         fclose(input);
451         return ret;
454 static int process_connect(struct transport *transport,
455                                      int for_push)
457         struct helper_data *data = transport->data;
458         const char *name;
459         const char *exec;
461         name = for_push ? "git-receive-pack" : "git-upload-pack";
462         if (for_push)
463                 exec = data->transport_options.receivepack;
464         else
465                 exec = data->transport_options.uploadpack;
467         return process_connect_service(transport, name, exec);
470 static int connect_helper(struct transport *transport, const char *name,
471                    const char *exec, int fd[2])
473         struct helper_data *data = transport->data;
475         /* Get_helper so connect is inited. */
476         get_helper(transport);
477         if (!data->connect)
478                 die("Operation not supported by protocol.");
480         if (!process_connect_service(transport, name, exec))
481                 die("Can't connect to subservice %s.", name);
483         fd[0] = data->helper->out;
484         fd[1] = data->helper->in;
485         return 0;
488 static int fetch(struct transport *transport,
489                  int nr_heads, struct ref **to_fetch)
491         struct helper_data *data = transport->data;
492         int i, count;
494         if (process_connect(transport, 0)) {
495                 do_take_over(transport);
496                 return transport->fetch(transport, nr_heads, to_fetch);
497         }
499         count = 0;
500         for (i = 0; i < nr_heads; i++)
501                 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
502                         count++;
504         if (!count)
505                 return 0;
507         if (data->fetch)
508                 return fetch_with_fetch(transport, nr_heads, to_fetch);
510         if (data->import)
511                 return fetch_with_import(transport, nr_heads, to_fetch);
513         return -1;
516 static int push_refs(struct transport *transport,
517                 struct ref *remote_refs, int flags)
519         int force_all = flags & TRANSPORT_PUSH_FORCE;
520         int mirror = flags & TRANSPORT_PUSH_MIRROR;
521         struct helper_data *data = transport->data;
522         struct strbuf buf = STRBUF_INIT;
523         struct child_process *helper;
524         struct ref *ref;
526         if (process_connect(transport, 1)) {
527                 do_take_over(transport);
528                 return transport->push_refs(transport, remote_refs, flags);
529         }
531         if (!remote_refs)
532                 return 0;
534         helper = get_helper(transport);
535         if (!data->push)
536                 return 1;
538         for (ref = remote_refs; ref; ref = ref->next) {
539                 if (ref->peer_ref)
540                         hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
541                 else if (!mirror)
542                         continue;
544                 ref->deletion = is_null_sha1(ref->new_sha1);
545                 if (!ref->deletion &&
546                         !hashcmp(ref->old_sha1, ref->new_sha1)) {
547                         ref->status = REF_STATUS_UPTODATE;
548                         continue;
549                 }
551                 if (force_all)
552                         ref->force = 1;
554                 strbuf_addstr(&buf, "push ");
555                 if (!ref->deletion) {
556                         if (ref->force)
557                                 strbuf_addch(&buf, '+');
558                         if (ref->peer_ref)
559                                 strbuf_addstr(&buf, ref->peer_ref->name);
560                         else
561                                 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
562                 }
563                 strbuf_addch(&buf, ':');
564                 strbuf_addstr(&buf, ref->name);
565                 strbuf_addch(&buf, '\n');
566         }
567         if (buf.len == 0)
568                 return 0;
570         transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
571         standard_options(transport);
573         if (flags & TRANSPORT_PUSH_DRY_RUN) {
574                 if (set_helper_option(transport, "dry-run", "true") != 0)
575                         die("helper %s does not support dry-run", data->name);
576         }
578         strbuf_addch(&buf, '\n');
579         sendline(data, &buf);
581         ref = remote_refs;
582         while (1) {
583                 char *refname, *msg;
584                 int status;
586                 recvline(data, &buf);
587                 if (!buf.len)
588                         break;
590                 if (!prefixcmp(buf.buf, "ok ")) {
591                         status = REF_STATUS_OK;
592                         refname = buf.buf + 3;
593                 } else if (!prefixcmp(buf.buf, "error ")) {
594                         status = REF_STATUS_REMOTE_REJECT;
595                         refname = buf.buf + 6;
596                 } else
597                         die("expected ok/error, helper said '%s'\n", buf.buf);
599                 msg = strchr(refname, ' ');
600                 if (msg) {
601                         struct strbuf msg_buf = STRBUF_INIT;
602                         const char *end;
604                         *msg++ = '\0';
605                         if (!unquote_c_style(&msg_buf, msg, &end))
606                                 msg = strbuf_detach(&msg_buf, NULL);
607                         else
608                                 msg = xstrdup(msg);
609                         strbuf_release(&msg_buf);
611                         if (!strcmp(msg, "no match")) {
612                                 status = REF_STATUS_NONE;
613                                 free(msg);
614                                 msg = NULL;
615                         }
616                         else if (!strcmp(msg, "up to date")) {
617                                 status = REF_STATUS_UPTODATE;
618                                 free(msg);
619                                 msg = NULL;
620                         }
621                         else if (!strcmp(msg, "non-fast forward")) {
622                                 status = REF_STATUS_REJECT_NONFASTFORWARD;
623                                 free(msg);
624                                 msg = NULL;
625                         }
626                 }
628                 if (ref)
629                         ref = find_ref_by_name(ref, refname);
630                 if (!ref)
631                         ref = find_ref_by_name(remote_refs, refname);
632                 if (!ref) {
633                         warning("helper reported unexpected status of %s", refname);
634                         continue;
635                 }
637                 ref->status = status;
638                 ref->remote_status = msg;
639         }
640         strbuf_release(&buf);
641         return 0;
644 static int has_attribute(const char *attrs, const char *attr) {
645         int len;
646         if (!attrs)
647                 return 0;
649         len = strlen(attr);
650         for (;;) {
651                 const char *space = strchrnul(attrs, ' ');
652                 if (len == space - attrs && !strncmp(attrs, attr, len))
653                         return 1;
654                 if (!*space)
655                         return 0;
656                 attrs = space + 1;
657         }
660 static struct ref *get_refs_list(struct transport *transport, int for_push)
662         struct helper_data *data = transport->data;
663         struct child_process *helper;
664         struct ref *ret = NULL;
665         struct ref **tail = &ret;
666         struct ref *posn;
667         struct strbuf buf = STRBUF_INIT;
669         helper = get_helper(transport);
671         if (process_connect(transport, for_push)) {
672                 do_take_over(transport);
673                 return transport->get_refs_list(transport, for_push);
674         }
676         if (data->push && for_push)
677                 write_str_in_full(helper->in, "list for-push\n");
678         else
679                 write_str_in_full(helper->in, "list\n");
681         while (1) {
682                 char *eov, *eon;
683                 recvline(data, &buf);
685                 if (!*buf.buf)
686                         break;
688                 eov = strchr(buf.buf, ' ');
689                 if (!eov)
690                         die("Malformed response in ref list: %s", buf.buf);
691                 eon = strchr(eov + 1, ' ');
692                 *eov = '\0';
693                 if (eon)
694                         *eon = '\0';
695                 *tail = alloc_ref(eov + 1);
696                 if (buf.buf[0] == '@')
697                         (*tail)->symref = xstrdup(buf.buf + 1);
698                 else if (buf.buf[0] != '?')
699                         get_sha1_hex(buf.buf, (*tail)->old_sha1);
700                 if (eon) {
701                         if (has_attribute(eon + 1, "unchanged")) {
702                                 (*tail)->status |= REF_STATUS_UPTODATE;
703                                 read_ref((*tail)->name, (*tail)->old_sha1);
704                         }
705                 }
706                 tail = &((*tail)->next);
707         }
708         if (debug)
709                 fprintf(stderr, "Debug: Read ref listing.\n");
710         strbuf_release(&buf);
712         for (posn = ret; posn; posn = posn->next)
713                 resolve_remote_symref(posn, ret);
715         return ret;
718 int transport_helper_init(struct transport *transport, const char *name)
720         struct helper_data *data = xcalloc(sizeof(*data), 1);
721         data->name = name;
723         if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
724                 debug = 1;
726         transport->data = data;
727         transport->set_option = set_helper_option;
728         transport->get_refs_list = get_refs_list;
729         transport->fetch = fetch;
730         transport->push_refs = push_refs;
731         transport->disconnect = release_helper;
732         transport->connect = connect_helper;
733         transport->smart_options = &(data->transport_options);
734         return 0;