Code

Merge branch 'jk/warn-author-committer-after-commit'
[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                 fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
533                         "Perhaps you should specify a branch such as 'master'.\n");
534                 return 0;
535         }
537         helper = get_helper(transport);
538         if (!data->push)
539                 return 1;
541         for (ref = remote_refs; ref; ref = ref->next) {
542                 if (!ref->peer_ref && !mirror)
543                         continue;
545                 /* Check for statuses set by set_ref_status_for_push() */
546                 switch (ref->status) {
547                 case REF_STATUS_REJECT_NONFASTFORWARD:
548                 case REF_STATUS_UPTODATE:
549                         continue;
550                 default:
551                         ; /* do nothing */
552                 }
554                 if (force_all)
555                         ref->force = 1;
557                 strbuf_addstr(&buf, "push ");
558                 if (!ref->deletion) {
559                         if (ref->force)
560                                 strbuf_addch(&buf, '+');
561                         if (ref->peer_ref)
562                                 strbuf_addstr(&buf, ref->peer_ref->name);
563                         else
564                                 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
565                 }
566                 strbuf_addch(&buf, ':');
567                 strbuf_addstr(&buf, ref->name);
568                 strbuf_addch(&buf, '\n');
569         }
570         if (buf.len == 0)
571                 return 0;
573         transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
574         standard_options(transport);
576         if (flags & TRANSPORT_PUSH_DRY_RUN) {
577                 if (set_helper_option(transport, "dry-run", "true") != 0)
578                         die("helper %s does not support dry-run", data->name);
579         }
581         strbuf_addch(&buf, '\n');
582         sendline(data, &buf);
584         ref = remote_refs;
585         while (1) {
586                 char *refname, *msg;
587                 int status;
589                 recvline(data, &buf);
590                 if (!buf.len)
591                         break;
593                 if (!prefixcmp(buf.buf, "ok ")) {
594                         status = REF_STATUS_OK;
595                         refname = buf.buf + 3;
596                 } else if (!prefixcmp(buf.buf, "error ")) {
597                         status = REF_STATUS_REMOTE_REJECT;
598                         refname = buf.buf + 6;
599                 } else
600                         die("expected ok/error, helper said '%s'\n", buf.buf);
602                 msg = strchr(refname, ' ');
603                 if (msg) {
604                         struct strbuf msg_buf = STRBUF_INIT;
605                         const char *end;
607                         *msg++ = '\0';
608                         if (!unquote_c_style(&msg_buf, msg, &end))
609                                 msg = strbuf_detach(&msg_buf, NULL);
610                         else
611                                 msg = xstrdup(msg);
612                         strbuf_release(&msg_buf);
614                         if (!strcmp(msg, "no match")) {
615                                 status = REF_STATUS_NONE;
616                                 free(msg);
617                                 msg = NULL;
618                         }
619                         else if (!strcmp(msg, "up to date")) {
620                                 status = REF_STATUS_UPTODATE;
621                                 free(msg);
622                                 msg = NULL;
623                         }
624                         else if (!strcmp(msg, "non-fast forward")) {
625                                 status = REF_STATUS_REJECT_NONFASTFORWARD;
626                                 free(msg);
627                                 msg = NULL;
628                         }
629                 }
631                 if (ref)
632                         ref = find_ref_by_name(ref, refname);
633                 if (!ref)
634                         ref = find_ref_by_name(remote_refs, refname);
635                 if (!ref) {
636                         warning("helper reported unexpected status of %s", refname);
637                         continue;
638                 }
640                 if (ref->status != REF_STATUS_NONE) {
641                         /*
642                          * Earlier, the ref was marked not to be pushed, so ignore the ref
643                          * status reported by the remote helper if the latter is 'no match'.
644                          */
645                         if (status == REF_STATUS_NONE)
646                                 continue;
647                 }
649                 ref->status = status;
650                 ref->remote_status = msg;
651         }
652         strbuf_release(&buf);
653         return 0;
656 static int has_attribute(const char *attrs, const char *attr) {
657         int len;
658         if (!attrs)
659                 return 0;
661         len = strlen(attr);
662         for (;;) {
663                 const char *space = strchrnul(attrs, ' ');
664                 if (len == space - attrs && !strncmp(attrs, attr, len))
665                         return 1;
666                 if (!*space)
667                         return 0;
668                 attrs = space + 1;
669         }
672 static struct ref *get_refs_list(struct transport *transport, int for_push)
674         struct helper_data *data = transport->data;
675         struct child_process *helper;
676         struct ref *ret = NULL;
677         struct ref **tail = &ret;
678         struct ref *posn;
679         struct strbuf buf = STRBUF_INIT;
681         helper = get_helper(transport);
683         if (process_connect(transport, for_push)) {
684                 do_take_over(transport);
685                 return transport->get_refs_list(transport, for_push);
686         }
688         if (data->push && for_push)
689                 write_str_in_full(helper->in, "list for-push\n");
690         else
691                 write_str_in_full(helper->in, "list\n");
693         while (1) {
694                 char *eov, *eon;
695                 recvline(data, &buf);
697                 if (!*buf.buf)
698                         break;
700                 eov = strchr(buf.buf, ' ');
701                 if (!eov)
702                         die("Malformed response in ref list: %s", buf.buf);
703                 eon = strchr(eov + 1, ' ');
704                 *eov = '\0';
705                 if (eon)
706                         *eon = '\0';
707                 *tail = alloc_ref(eov + 1);
708                 if (buf.buf[0] == '@')
709                         (*tail)->symref = xstrdup(buf.buf + 1);
710                 else if (buf.buf[0] != '?')
711                         get_sha1_hex(buf.buf, (*tail)->old_sha1);
712                 if (eon) {
713                         if (has_attribute(eon + 1, "unchanged")) {
714                                 (*tail)->status |= REF_STATUS_UPTODATE;
715                                 read_ref((*tail)->name, (*tail)->old_sha1);
716                         }
717                 }
718                 tail = &((*tail)->next);
719         }
720         if (debug)
721                 fprintf(stderr, "Debug: Read ref listing.\n");
722         strbuf_release(&buf);
724         for (posn = ret; posn; posn = posn->next)
725                 resolve_remote_symref(posn, ret);
727         return ret;
730 int transport_helper_init(struct transport *transport, const char *name)
732         struct helper_data *data = xcalloc(sizeof(*data), 1);
733         data->name = name;
735         if (getenv("GIT_TRANSPORT_HELPER_DEBUG"))
736                 debug = 1;
738         transport->data = data;
739         transport->set_option = set_helper_option;
740         transport->get_refs_list = get_refs_list;
741         transport->fetch = fetch;
742         transport->push_refs = push_refs;
743         transport->disconnect = release_helper;
744         transport->connect = connect_helper;
745         transport->smart_options = &(data->transport_options);
746         return 0;