Code

transport-helper.c::push_refs(): emit "no refs" error message
[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                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
326                         "Perhaps you should specify a branch such as 'master'.\n");
327                 return 0;
328         }
330         helper = get_helper(transport);
331         if (!data->push)
332                 return 1;
334         for (ref = remote_refs; ref; ref = ref->next) {
335                 if (!ref->peer_ref && !mirror)
336                         continue;
338                 /* Check for statuses set by set_ref_status_for_push() */
339                 switch (ref->status) {
340                 case REF_STATUS_REJECT_NONFASTFORWARD:
341                 case REF_STATUS_UPTODATE:
342                         continue;
343                 default:
344                         ; /* do nothing */
345                 }
347                 if (force_all)
348                         ref->force = 1;
350                 strbuf_addstr(&buf, "push ");
351                 if (!ref->deletion) {
352                         if (ref->force)
353                                 strbuf_addch(&buf, '+');
354                         if (ref->peer_ref)
355                                 strbuf_addstr(&buf, ref->peer_ref->name);
356                         else
357                                 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
358                 }
359                 strbuf_addch(&buf, ':');
360                 strbuf_addstr(&buf, ref->name);
361                 strbuf_addch(&buf, '\n');
362         }
363         if (buf.len == 0)
364                 return 0;
366         transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
367         standard_options(transport);
369         if (flags & TRANSPORT_PUSH_DRY_RUN) {
370                 if (set_helper_option(transport, "dry-run", "true") != 0)
371                         die("helper %s does not support dry-run", data->name);
372         }
374         strbuf_addch(&buf, '\n');
375         if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
376                 exit(128);
378         ref = remote_refs;
379         while (1) {
380                 char *refname, *msg;
381                 int status;
383                 strbuf_reset(&buf);
384                 if (strbuf_getline(&buf, data->out, '\n') == EOF)
385                         exit(128); /* child died, message supplied already */
386                 if (!buf.len)
387                         break;
389                 if (!prefixcmp(buf.buf, "ok ")) {
390                         status = REF_STATUS_OK;
391                         refname = buf.buf + 3;
392                 } else if (!prefixcmp(buf.buf, "error ")) {
393                         status = REF_STATUS_REMOTE_REJECT;
394                         refname = buf.buf + 6;
395                 } else
396                         die("expected ok/error, helper said '%s'\n", buf.buf);
398                 msg = strchr(refname, ' ');
399                 if (msg) {
400                         struct strbuf msg_buf = STRBUF_INIT;
401                         const char *end;
403                         *msg++ = '\0';
404                         if (!unquote_c_style(&msg_buf, msg, &end))
405                                 msg = strbuf_detach(&msg_buf, NULL);
406                         else
407                                 msg = xstrdup(msg);
408                         strbuf_release(&msg_buf);
410                         if (!strcmp(msg, "no match")) {
411                                 status = REF_STATUS_NONE;
412                                 free(msg);
413                                 msg = NULL;
414                         }
415                         else if (!strcmp(msg, "up to date")) {
416                                 status = REF_STATUS_UPTODATE;
417                                 free(msg);
418                                 msg = NULL;
419                         }
420                         else if (!strcmp(msg, "non-fast forward")) {
421                                 status = REF_STATUS_REJECT_NONFASTFORWARD;
422                                 free(msg);
423                                 msg = NULL;
424                         }
425                 }
427                 if (ref)
428                         ref = find_ref_by_name(ref, refname);
429                 if (!ref)
430                         ref = find_ref_by_name(remote_refs, refname);
431                 if (!ref) {
432                         warning("helper reported unexpected status of %s", refname);
433                         continue;
434                 }
436                 if (ref->status != REF_STATUS_NONE) {
437                         /*
438                          * Earlier, the ref was marked not to be pushed, so ignore the ref
439                          * status reported by the remote helper if the latter is 'no match'.
440                          */
441                         if (status == REF_STATUS_NONE)
442                                 continue;
443                 }
445                 ref->status = status;
446                 ref->remote_status = msg;
447         }
448         strbuf_release(&buf);
449         return 0;
452 static int has_attribute(const char *attrs, const char *attr) {
453         int len;
454         if (!attrs)
455                 return 0;
457         len = strlen(attr);
458         for (;;) {
459                 const char *space = strchrnul(attrs, ' ');
460                 if (len == space - attrs && !strncmp(attrs, attr, len))
461                         return 1;
462                 if (!*space)
463                         return 0;
464                 attrs = space + 1;
465         }
468 static struct ref *get_refs_list(struct transport *transport, int for_push)
470         struct helper_data *data = transport->data;
471         struct child_process *helper;
472         struct ref *ret = NULL;
473         struct ref **tail = &ret;
474         struct ref *posn;
475         struct strbuf buf = STRBUF_INIT;
477         helper = get_helper(transport);
479         if (data->push && for_push)
480                 write_str_in_full(helper->in, "list for-push\n");
481         else
482                 write_str_in_full(helper->in, "list\n");
484         while (1) {
485                 char *eov, *eon;
486                 if (strbuf_getline(&buf, data->out, '\n') == EOF)
487                         exit(128); /* child died, message supplied already */
489                 if (!*buf.buf)
490                         break;
492                 eov = strchr(buf.buf, ' ');
493                 if (!eov)
494                         die("Malformed response in ref list: %s", buf.buf);
495                 eon = strchr(eov + 1, ' ');
496                 *eov = '\0';
497                 if (eon)
498                         *eon = '\0';
499                 *tail = alloc_ref(eov + 1);
500                 if (buf.buf[0] == '@')
501                         (*tail)->symref = xstrdup(buf.buf + 1);
502                 else if (buf.buf[0] != '?')
503                         get_sha1_hex(buf.buf, (*tail)->old_sha1);
504                 if (eon) {
505                         if (has_attribute(eon + 1, "unchanged")) {
506                                 (*tail)->status |= REF_STATUS_UPTODATE;
507                                 read_ref((*tail)->name, (*tail)->old_sha1);
508                         }
509                 }
510                 tail = &((*tail)->next);
511         }
512         strbuf_release(&buf);
514         for (posn = ret; posn; posn = posn->next)
515                 resolve_remote_symref(posn, ret);
517         return ret;
520 int transport_helper_init(struct transport *transport, const char *name)
522         struct helper_data *data = xcalloc(sizeof(*data), 1);
523         data->name = name;
525         transport->data = data;
526         transport->set_option = set_helper_option;
527         transport->get_refs_list = get_refs_list;
528         transport->fetch = fetch;
529         transport->push_refs = push_refs;
530         transport->disconnect = release_helper;
531         return 0;