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