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 struct helper_data
12 {
13         const char *name;
14         struct child_process *helper;
15         FILE *out;
16         unsigned fetch : 1,
17                 import : 1,
18                 option : 1,
19                 push : 1;
20         /* These go from remote name (as in "list") to private name */
21         struct refspec *refspecs;
22         int refspec_nr;
23 };
25 static struct child_process *get_helper(struct transport *transport)
26 {
27         struct helper_data *data = transport->data;
28         struct strbuf buf = STRBUF_INIT;
29         struct child_process *helper;
30         const char **refspecs = NULL;
31         int refspec_nr = 0;
32         int refspec_alloc = 0;
34         if (data->helper)
35                 return data->helper;
37         helper = xcalloc(1, sizeof(*helper));
38         helper->in = -1;
39         helper->out = -1;
40         helper->err = 0;
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;
46         helper->git_cmd = 1;
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");
54         while (1) {
55                 if (strbuf_getline(&buf, data->out, '\n') == EOF)
56                         exit(128); /* child died, message supplied already */
58                 if (!*buf.buf)
59                         break;
60                 if (!strcmp(buf.buf, "fetch"))
61                         data->fetch = 1;
62                 if (!strcmp(buf.buf, "option"))
63                         data->option = 1;
64                 if (!strcmp(buf.buf, "push"))
65                         data->push = 1;
66                 if (!strcmp(buf.buf, "import"))
67                         data->import = 1;
68                 if (!data->refspecs && !prefixcmp(buf.buf, "refspec ")) {
69                         ALLOC_GROW(refspecs,
70                                    refspec_nr + 1,
71                                    refspec_alloc);
72                         refspecs[refspec_nr++] = strdup(buf.buf + strlen("refspec "));
73                 }
74         }
75         if (refspecs) {
76                 int i;
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]);
81                 }
82                 free(refspecs);
83         }
84         strbuf_release(&buf);
85         return data->helper;
86 }
88 static int disconnect_helper(struct transport *transport)
89 {
90         struct helper_data *data = transport->data;
91         if (data->helper) {
92                 write_str_in_full(data->helper->in, "\n");
93                 close(data->helper->in);
94                 fclose(data->out);
95                 finish_command(data->helper);
96                 free((char *)data->helper->argv[0]);
97                 free(data->helper->argv);
98                 free(data->helper);
99                 data->helper = NULL;
100         }
101         return 0;
104 static const char *unsupported_options[] = {
105         TRANS_OPT_UPLOADPACK,
106         TRANS_OPT_RECEIVEPACK,
107         TRANS_OPT_THIN,
108         TRANS_OPT_KEEP
109         };
110 static const char *boolean_options[] = {
111         TRANS_OPT_THIN,
112         TRANS_OPT_KEEP,
113         TRANS_OPT_FOLLOWTAGS
114         };
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;
124         if (!data->option)
125                 return 1;
127         for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
128                 if (!strcmp(name, unsupported_options[i]))
129                         return 1;
130         }
132         for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
133                 if (!strcmp(name, boolean_options[i])) {
134                         is_bool = 1;
135                         break;
136                 }
137         }
139         strbuf_addf(&buf, "option %s ", name);
140         if (is_bool)
141                 strbuf_addstr(&buf, value ? "true" : "false");
142         else
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);
149         strbuf_reset(&buf);
150         if (strbuf_getline(&buf, data->out, '\n') == EOF)
151                 exit(128); /* child died, message supplied already */
153         if (!strcmp(buf.buf, "ok"))
154                 ret = 0;
155         else if (!prefixcmp(buf.buf, "error")) {
156                 ret = -1;
157         } else if (!strcmp(buf.buf, "unsupported"))
158                 ret = 1;
159         else {
160                 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
161                 ret = 1;
162         }
163         strbuf_release(&buf);
164         return ret;
167 static void standard_options(struct transport *t)
169         char buf[16];
170         int n;
171         int v = t->verbose;
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);
189         return 0;
192 static int fetch_with_fetch(struct transport *transport,
193                             int nr_heads, struct ref **to_fetch)
195         struct helper_data *data = transport->data;
196         int i;
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)
204                         continue;
206                 strbuf_addf(&buf, "fetch %s %s\n",
207                             sha1_to_hex(posn->old_sha1), posn->name);
208         }
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);
214         while (1) {
215                 strbuf_reset(&buf);
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);
223                         else
224                                 transport->pack_lockfile = xstrdup(name);
225                 }
226                 else if (!buf.len)
227                         break;
228                 else
229                         warning("%s unexpectedly said: '%s'", data->name, buf.buf);
230         }
231         strbuf_release(&buf);
232         return 0;
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;
254         int i;
255         struct ref *posn;
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++) {
262                 posn = to_fetch[i];
263                 if (posn->status & REF_STATUS_UPTODATE)
264                         continue;
266                 strbuf_addf(&buf, "import %s\n", posn->name);
267                 write_in_full(helper->in, buf.buf, buf.len);
268                 strbuf_reset(&buf);
269         }
270         disconnect_helper(transport);
271         finish_command(&fastimport);
272         free(fastimport.argv);
273         fastimport.argv = NULL;
275         for (i = 0; i < nr_heads; i++) {
276                 char *private;
277                 posn = to_fetch[i];
278                 if (posn->status & REF_STATUS_UPTODATE)
279                         continue;
280                 if (data->refspecs)
281                         private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
282                 else
283                         private = strdup(posn->name);
284                 read_ref(private, posn->old_sha1);
285                 free(private);
286         }
287         strbuf_release(&buf);
288         return 0;
291 static int fetch(struct transport *transport,
292                  int nr_heads, struct ref **to_fetch)
294         struct helper_data *data = transport->data;
295         int i, count;
297         count = 0;
298         for (i = 0; i < nr_heads; i++)
299                 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
300                         count++;
302         if (!count)
303                 return 0;
305         if (data->fetch)
306                 return fetch_with_fetch(transport, nr_heads, to_fetch);
308         if (data->import)
309                 return fetch_with_import(transport, nr_heads, to_fetch);
311         return -1;
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;
322         struct ref *ref;
324         if (!remote_refs)
325                 return 0;
327         helper = get_helper(transport);
328         if (!data->push)
329                 return 1;
331         for (ref = remote_refs; ref; ref = ref->next) {
332                 if (ref->peer_ref)
333                         hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
334                 else if (!mirror)
335                         continue;
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;
341                         continue;
342                 }
344                 if (force_all)
345                         ref->force = 1;
347                 strbuf_addstr(&buf, "push ");
348                 if (!ref->deletion) {
349                         if (ref->force)
350                                 strbuf_addch(&buf, '+');
351                         if (ref->peer_ref)
352                                 strbuf_addstr(&buf, ref->peer_ref->name);
353                         else
354                                 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
355                 }
356                 strbuf_addch(&buf, ':');
357                 strbuf_addstr(&buf, ref->name);
358                 strbuf_addch(&buf, '\n');
359         }
360         if (buf.len == 0)
361                 return 0;
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);
369         }
371         strbuf_addch(&buf, '\n');
372         if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
373                 exit(128);
375         ref = remote_refs;
376         while (1) {
377                 char *refname, *msg;
378                 int status;
380                 strbuf_reset(&buf);
381                 if (strbuf_getline(&buf, data->out, '\n') == EOF)
382                         exit(128); /* child died, message supplied already */
383                 if (!buf.len)
384                         break;
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;
392                 } else
393                         die("expected ok/error, helper said '%s'\n", buf.buf);
395                 msg = strchr(refname, ' ');
396                 if (msg) {
397                         struct strbuf msg_buf = STRBUF_INIT;
398                         const char *end;
400                         *msg++ = '\0';
401                         if (!unquote_c_style(&msg_buf, msg, &end))
402                                 msg = strbuf_detach(&msg_buf, NULL);
403                         else
404                                 msg = xstrdup(msg);
405                         strbuf_release(&msg_buf);
407                         if (!strcmp(msg, "no match")) {
408                                 status = REF_STATUS_NONE;
409                                 free(msg);
410                                 msg = NULL;
411                         }
412                         else if (!strcmp(msg, "up to date")) {
413                                 status = REF_STATUS_UPTODATE;
414                                 free(msg);
415                                 msg = NULL;
416                         }
417                         else if (!strcmp(msg, "non-fast forward")) {
418                                 status = REF_STATUS_REJECT_NONFASTFORWARD;
419                                 free(msg);
420                                 msg = NULL;
421                         }
422                 }
424                 if (ref)
425                         ref = find_ref_by_name(ref, refname);
426                 if (!ref)
427                         ref = find_ref_by_name(remote_refs, refname);
428                 if (!ref) {
429                         warning("helper reported unexpected status of %s", refname);
430                         continue;
431                 }
433                 ref->status = status;
434                 ref->remote_status = msg;
435         }
436         strbuf_release(&buf);
437         return 0;
440 static int has_attribute(const char *attrs, const char *attr) {
441         int len;
442         if (!attrs)
443                 return 0;
445         len = strlen(attr);
446         for (;;) {
447                 const char *space = strchrnul(attrs, ' ');
448                 if (len == space - attrs && !strncmp(attrs, attr, len))
449                         return 1;
450                 if (!*space)
451                         return 0;
452                 attrs = space + 1;
453         }
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;
462         struct ref *posn;
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");
469         else
470                 write_str_in_full(helper->in, "list\n");
472         while (1) {
473                 char *eov, *eon;
474                 if (strbuf_getline(&buf, data->out, '\n') == EOF)
475                         exit(128); /* child died, message supplied already */
477                 if (!*buf.buf)
478                         break;
480                 eov = strchr(buf.buf, ' ');
481                 if (!eov)
482                         die("Malformed response in ref list: %s", buf.buf);
483                 eon = strchr(eov + 1, ' ');
484                 *eov = '\0';
485                 if (eon)
486                         *eon = '\0';
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);
492                 if (eon) {
493                         if (has_attribute(eon + 1, "unchanged")) {
494                                 (*tail)->status |= REF_STATUS_UPTODATE;
495                                 read_ref((*tail)->name, (*tail)->old_sha1);
496                         }
497                 }
498                 tail = &((*tail)->next);
499         }
500         strbuf_release(&buf);
502         for (posn = ret; posn; posn = posn->next)
503                 resolve_remote_symref(posn, ret);
505         return ret;
508 int transport_helper_init(struct transport *transport, const char *name)
510         struct helper_data *data = xcalloc(sizeof(*data), 1);
511         data->name = name;
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;
519         return 0;