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