Merge branch 'maint-1.6.0' into maint
[git] / remote-curl.c
1 #include "cache.h"
2 #include "remote.h"
3 #include "strbuf.h"
4 #include "walker.h"
5 #include "http.h"
6 #include "exec_cmd.h"
7 #include "run-command.h"
8 #include "pkt-line.h"
9 #include "sideband.h"
10
11 static struct remote *remote;
12 static const char *url;
13 static struct walker *walker;
14
15 struct options {
16         int verbosity;
17         unsigned long depth;
18         unsigned progress : 1,
19                 followtags : 1,
20                 dry_run : 1,
21                 thin : 1;
22 };
23 static struct options options;
24
25 static void init_walker(void)
26 {
27         if (!walker)
28                 walker = get_http_walker(url, remote);
29 }
30
31 static int set_option(const char *name, const char *value)
32 {
33         if (!strcmp(name, "verbosity")) {
34                 char *end;
35                 int v = strtol(value, &end, 10);
36                 if (value == end || *end)
37                         return -1;
38                 options.verbosity = v;
39                 return 0;
40         }
41         else if (!strcmp(name, "progress")) {
42                 if (!strcmp(value, "true"))
43                         options.progress = 1;
44                 else if (!strcmp(value, "false"))
45                         options.progress = 0;
46                 else
47                         return -1;
48                 return 0;
49         }
50         else if (!strcmp(name, "depth")) {
51                 char *end;
52                 unsigned long v = strtoul(value, &end, 10);
53                 if (value == end || *end)
54                         return -1;
55                 options.depth = v;
56                 return 0;
57         }
58         else if (!strcmp(name, "followtags")) {
59                 if (!strcmp(value, "true"))
60                         options.followtags = 1;
61                 else if (!strcmp(value, "false"))
62                         options.followtags = 0;
63                 else
64                         return -1;
65                 return 0;
66         }
67         else if (!strcmp(name, "dry-run")) {
68                 if (!strcmp(value, "true"))
69                         options.dry_run = 1;
70                 else if (!strcmp(value, "false"))
71                         options.dry_run = 0;
72                 else
73                         return -1;
74                 return 0;
75         }
76         else {
77                 return 1 /* unsupported */;
78         }
79 }
80
81 struct discovery {
82         const char *service;
83         char *buf_alloc;
84         char *buf;
85         size_t len;
86         unsigned proto_git : 1;
87 };
88 static struct discovery *last_discovery;
89
90 static void free_discovery(struct discovery *d)
91 {
92         if (d) {
93                 if (d == last_discovery)
94                         last_discovery = NULL;
95                 free(d->buf_alloc);
96                 free(d);
97         }
98 }
99
100 static struct discovery* discover_refs(const char *service)
101 {
102         struct strbuf buffer = STRBUF_INIT;
103         struct discovery *last = last_discovery;
104         char *refs_url;
105         int http_ret, is_http = 0;
106
107         if (last && !strcmp(service, last->service))
108                 return last;
109         free_discovery(last);
110
111         strbuf_addf(&buffer, "%s/info/refs", url);
112         if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) {
113                 is_http = 1;
114                 if (!strchr(url, '?'))
115                         strbuf_addch(&buffer, '?');
116                 else
117                         strbuf_addch(&buffer, '&');
118                 strbuf_addf(&buffer, "service=%s", service);
119         }
120         refs_url = strbuf_detach(&buffer, NULL);
121
122         init_walker();
123         http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
124         switch (http_ret) {
125         case HTTP_OK:
126                 break;
127         case HTTP_MISSING_TARGET:
128                 die("%s not found: did you run git update-server-info on the"
129                     " server?", refs_url);
130         default:
131                 http_error(refs_url, http_ret);
132                 die("HTTP request failed");
133         }
134
135         last= xcalloc(1, sizeof(*last_discovery));
136         last->service = service;
137         last->buf_alloc = strbuf_detach(&buffer, &last->len);
138         last->buf = last->buf_alloc;
139
140         if (is_http && 5 <= last->len && last->buf[4] == '#') {
141                 /* smart HTTP response; validate that the service
142                  * pkt-line matches our request.
143                  */
144                 struct strbuf exp = STRBUF_INIT;
145
146                 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
147                         die("%s has invalid packet header", refs_url);
148                 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
149                         strbuf_setlen(&buffer, buffer.len - 1);
150
151                 strbuf_addf(&exp, "# service=%s", service);
152                 if (strbuf_cmp(&exp, &buffer))
153                         die("invalid server response; got '%s'", buffer.buf);
154                 strbuf_release(&exp);
155
156                 /* The header can include additional metadata lines, up
157                  * until a packet flush marker.  Ignore these now, but
158                  * in the future we might start to scan them.
159                  */
160                 strbuf_reset(&buffer);
161                 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
162                         strbuf_reset(&buffer);
163
164                 last->proto_git = 1;
165         }
166
167         free(refs_url);
168         strbuf_release(&buffer);
169         last_discovery = last;
170         return last;
171 }
172
173 static int write_discovery(int fd, void *data)
174 {
175         struct discovery *heads = data;
176         int err = 0;
177         if (write_in_full(fd, heads->buf, heads->len) != heads->len)
178                 err = 1;
179         close(fd);
180         return err;
181 }
182
183 static struct ref *parse_git_refs(struct discovery *heads)
184 {
185         struct ref *list = NULL;
186         struct async async;
187
188         memset(&async, 0, sizeof(async));
189         async.proc = write_discovery;
190         async.data = heads;
191
192         if (start_async(&async))
193                 die("cannot start thread to parse advertised refs");
194         get_remote_heads(async.out, &list, 0, NULL, 0, NULL);
195         close(async.out);
196         if (finish_async(&async))
197                 die("ref parsing thread failed");
198         return list;
199 }
200
201 static struct ref *parse_info_refs(struct discovery *heads)
202 {
203         char *data, *start, *mid;
204         char *ref_name;
205         int i = 0;
206
207         struct ref *refs = NULL;
208         struct ref *ref = NULL;
209         struct ref *last_ref = NULL;
210
211         data = heads->buf;
212         start = NULL;
213         mid = data;
214         while (i < heads->len) {
215                 if (!start) {
216                         start = &data[i];
217                 }
218                 if (data[i] == '\t')
219                         mid = &data[i];
220                 if (data[i] == '\n') {
221                         data[i] = 0;
222                         ref_name = mid + 1;
223                         ref = xmalloc(sizeof(struct ref) +
224                                       strlen(ref_name) + 1);
225                         memset(ref, 0, sizeof(struct ref));
226                         strcpy(ref->name, ref_name);
227                         get_sha1_hex(start, ref->old_sha1);
228                         if (!refs)
229                                 refs = ref;
230                         if (last_ref)
231                                 last_ref->next = ref;
232                         last_ref = ref;
233                         start = NULL;
234                 }
235                 i++;
236         }
237
238         init_walker();
239         ref = alloc_ref("HEAD");
240         if (!walker->fetch_ref(walker, ref) &&
241             !resolve_remote_symref(ref, refs)) {
242                 ref->next = refs;
243                 refs = ref;
244         } else {
245                 free(ref);
246         }
247
248         return refs;
249 }
250
251 static struct ref *get_refs(int for_push)
252 {
253         struct discovery *heads;
254
255         if (for_push)
256                 heads = discover_refs("git-receive-pack");
257         else
258                 heads = discover_refs("git-upload-pack");
259
260         if (heads->proto_git)
261                 return parse_git_refs(heads);
262         return parse_info_refs(heads);
263 }
264
265 static void output_refs(struct ref *refs)
266 {
267         struct ref *posn;
268         for (posn = refs; posn; posn = posn->next) {
269                 if (posn->symref)
270                         printf("@%s %s\n", posn->symref, posn->name);
271                 else
272                         printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
273         }
274         printf("\n");
275         fflush(stdout);
276         free_refs(refs);
277 }
278
279 struct rpc_state {
280         const char *service_name;
281         const char **argv;
282         char *service_url;
283         char *hdr_content_type;
284         char *hdr_accept;
285         char *buf;
286         size_t alloc;
287         size_t len;
288         size_t pos;
289         int in;
290         int out;
291         struct strbuf result;
292         unsigned gzip_request : 1;
293 };
294
295 static size_t rpc_out(void *ptr, size_t eltsize,
296                 size_t nmemb, void *buffer_)
297 {
298         size_t max = eltsize * nmemb;
299         struct rpc_state *rpc = buffer_;
300         size_t avail = rpc->len - rpc->pos;
301
302         if (!avail) {
303                 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
304                 if (!avail)
305                         return 0;
306                 rpc->pos = 0;
307                 rpc->len = avail;
308         }
309
310         if (max < avail)
311                 avail = max;
312         memcpy(ptr, rpc->buf + rpc->pos, avail);
313         rpc->pos += avail;
314         return avail;
315 }
316
317 static size_t rpc_in(const void *ptr, size_t eltsize,
318                 size_t nmemb, void *buffer_)
319 {
320         size_t size = eltsize * nmemb;
321         struct rpc_state *rpc = buffer_;
322         write_or_die(rpc->in, ptr, size);
323         return size;
324 }
325
326 static int post_rpc(struct rpc_state *rpc)
327 {
328         struct active_request_slot *slot;
329         struct slot_results results;
330         struct curl_slist *headers = NULL;
331         int use_gzip = rpc->gzip_request;
332         char *gzip_body = NULL;
333         int err = 0, large_request = 0;
334
335         /* Try to load the entire request, if we can fit it into the
336          * allocated buffer space we can use HTTP/1.0 and avoid the
337          * chunked encoding mess.
338          */
339         while (1) {
340                 size_t left = rpc->alloc - rpc->len;
341                 char *buf = rpc->buf + rpc->len;
342                 int n;
343
344                 if (left < LARGE_PACKET_MAX) {
345                         large_request = 1;
346                         use_gzip = 0;
347                         break;
348                 }
349
350                 n = packet_read_line(rpc->out, buf, left);
351                 if (!n)
352                         break;
353                 rpc->len += n;
354         }
355
356         slot = get_active_slot();
357         slot->results = &results;
358
359         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
360         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
361         curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
362         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
363
364         headers = curl_slist_append(headers, rpc->hdr_content_type);
365         headers = curl_slist_append(headers, rpc->hdr_accept);
366
367         if (large_request) {
368                 /* The request body is large and the size cannot be predicted.
369                  * We must use chunked encoding to send it.
370                  */
371                 headers = curl_slist_append(headers, "Expect: 100-continue");
372                 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
373                 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
374                 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
375                 if (options.verbosity > 1) {
376                         fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
377                         fflush(stderr);
378                 }
379
380         } else if (use_gzip && 1024 < rpc->len) {
381                 /* The client backend isn't giving us compressed data so
382                  * we can try to deflate it ourselves, this may save on.
383                  * the transfer time.
384                  */
385                 size_t size;
386                 z_stream stream;
387                 int ret;
388
389                 memset(&stream, 0, sizeof(stream));
390                 ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
391                                 Z_DEFLATED, (15 + 16),
392                                 8, Z_DEFAULT_STRATEGY);
393                 if (ret != Z_OK)
394                         die("cannot deflate request; zlib init error %d", ret);
395                 size = deflateBound(&stream, rpc->len);
396                 gzip_body = xmalloc(size);
397
398                 stream.next_in = (unsigned char *)rpc->buf;
399                 stream.avail_in = rpc->len;
400                 stream.next_out = (unsigned char *)gzip_body;
401                 stream.avail_out = size;
402
403                 ret = deflate(&stream, Z_FINISH);
404                 if (ret != Z_STREAM_END)
405                         die("cannot deflate request; zlib deflate error %d", ret);
406
407                 ret = deflateEnd(&stream);
408                 if (ret != Z_OK)
409                         die("cannot deflate request; zlib end error %d", ret);
410
411                 size = stream.total_out;
412
413                 headers = curl_slist_append(headers, "Content-Encoding: gzip");
414                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
415                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
416
417                 if (options.verbosity > 1) {
418                         fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
419                                 rpc->service_name,
420                                 (unsigned long)rpc->len, (unsigned long)size);
421                         fflush(stderr);
422                 }
423         } else {
424                 /* We know the complete request size in advance, use the
425                  * more normal Content-Length approach.
426                  */
427                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
428                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
429                 if (options.verbosity > 1) {
430                         fprintf(stderr, "POST %s (%lu bytes)\n",
431                                 rpc->service_name, (unsigned long)rpc->len);
432                         fflush(stderr);
433                 }
434         }
435
436         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
437         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
438         curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
439
440         slot->curl_result = curl_easy_perform(slot->curl);
441         finish_active_slot(slot);
442
443         if (results.curl_result != CURLE_OK) {
444                 err |= error("RPC failed; result=%d, HTTP code = %ld",
445                         results.curl_result, results.http_code);
446         }
447
448         curl_slist_free_all(headers);
449         free(gzip_body);
450         return err;
451 }
452
453 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
454 {
455         const char *svc = rpc->service_name;
456         struct strbuf buf = STRBUF_INIT;
457         struct child_process client;
458         int err = 0;
459
460         init_walker();
461         memset(&client, 0, sizeof(client));
462         client.in = -1;
463         client.out = -1;
464         client.git_cmd = 1;
465         client.argv = rpc->argv;
466         if (start_command(&client))
467                 exit(1);
468         if (heads)
469                 write_or_die(client.in, heads->buf, heads->len);
470
471         rpc->alloc = http_post_buffer;
472         rpc->buf = xmalloc(rpc->alloc);
473         rpc->in = client.in;
474         rpc->out = client.out;
475         strbuf_init(&rpc->result, 0);
476
477         strbuf_addf(&buf, "%s/%s", url, svc);
478         rpc->service_url = strbuf_detach(&buf, NULL);
479
480         strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
481         rpc->hdr_content_type = strbuf_detach(&buf, NULL);
482
483         strbuf_addf(&buf, "Accept: application/x-%s-response", svc);
484         rpc->hdr_accept = strbuf_detach(&buf, NULL);
485
486         while (!err) {
487                 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
488                 if (!n)
489                         break;
490                 rpc->pos = 0;
491                 rpc->len = n;
492                 err |= post_rpc(rpc);
493         }
494         strbuf_read(&rpc->result, client.out, 0);
495
496         close(client.in);
497         close(client.out);
498         client.in = -1;
499         client.out = -1;
500
501         err |= finish_command(&client);
502         free(rpc->service_url);
503         free(rpc->hdr_content_type);
504         free(rpc->hdr_accept);
505         free(rpc->buf);
506         strbuf_release(&buf);
507         return err;
508 }
509
510 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
511 {
512         char **targets = xmalloc(nr_heads * sizeof(char*));
513         int ret, i;
514
515         if (options.depth)
516                 die("dumb http transport does not support --depth");
517         for (i = 0; i < nr_heads; i++)
518                 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
519
520         init_walker();
521         walker->get_all = 1;
522         walker->get_tree = 1;
523         walker->get_history = 1;
524         walker->get_verbosely = options.verbosity >= 3;
525         walker->get_recover = 0;
526         ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
527
528         for (i = 0; i < nr_heads; i++)
529                 free(targets[i]);
530         free(targets);
531
532         return ret ? error("Fetch failed.") : 0;
533 }
534
535 static int fetch_git(struct discovery *heads,
536         int nr_heads, struct ref **to_fetch)
537 {
538         struct rpc_state rpc;
539         char *depth_arg = NULL;
540         const char **argv;
541         int argc = 0, i, err;
542
543         argv = xmalloc((15 + nr_heads) * sizeof(char*));
544         argv[argc++] = "fetch-pack";
545         argv[argc++] = "--stateless-rpc";
546         argv[argc++] = "--lock-pack";
547         if (options.followtags)
548                 argv[argc++] = "--include-tag";
549         if (options.thin)
550                 argv[argc++] = "--thin";
551         if (options.verbosity >= 3) {
552                 argv[argc++] = "-v";
553                 argv[argc++] = "-v";
554         }
555         if (!options.progress)
556                 argv[argc++] = "--no-progress";
557         if (options.depth) {
558                 struct strbuf buf = STRBUF_INIT;
559                 strbuf_addf(&buf, "--depth=%lu", options.depth);
560                 depth_arg = strbuf_detach(&buf, NULL);
561                 argv[argc++] = depth_arg;
562         }
563         argv[argc++] = url;
564         for (i = 0; i < nr_heads; i++) {
565                 struct ref *ref = to_fetch[i];
566                 if (!ref->name || !*ref->name)
567                         die("cannot fetch by sha1 over smart http");
568                 argv[argc++] = ref->name;
569         }
570         argv[argc++] = NULL;
571
572         memset(&rpc, 0, sizeof(rpc));
573         rpc.service_name = "git-upload-pack",
574         rpc.argv = argv;
575         rpc.gzip_request = 1;
576
577         err = rpc_service(&rpc, heads);
578         if (rpc.result.len)
579                 safe_write(1, rpc.result.buf, rpc.result.len);
580         strbuf_release(&rpc.result);
581         free(argv);
582         free(depth_arg);
583         return err;
584 }
585
586 static int fetch(int nr_heads, struct ref **to_fetch)
587 {
588         struct discovery *d = discover_refs("git-upload-pack");
589         if (d->proto_git)
590                 return fetch_git(d, nr_heads, to_fetch);
591         else
592                 return fetch_dumb(nr_heads, to_fetch);
593 }
594
595 static void parse_fetch(struct strbuf *buf)
596 {
597         struct ref **to_fetch = NULL;
598         struct ref *list_head = NULL;
599         struct ref **list = &list_head;
600         int alloc_heads = 0, nr_heads = 0;
601
602         do {
603                 if (!prefixcmp(buf->buf, "fetch ")) {
604                         char *p = buf->buf + strlen("fetch ");
605                         char *name;
606                         struct ref *ref;
607                         unsigned char old_sha1[20];
608
609                         if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
610                                 die("protocol error: expected sha/ref, got %s'", p);
611                         if (p[40] == ' ')
612                                 name = p + 41;
613                         else if (!p[40])
614                                 name = "";
615                         else
616                                 die("protocol error: expected sha/ref, got %s'", p);
617
618                         ref = alloc_ref(name);
619                         hashcpy(ref->old_sha1, old_sha1);
620
621                         *list = ref;
622                         list = &ref->next;
623
624                         ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
625                         to_fetch[nr_heads++] = ref;
626                 }
627                 else
628                         die("http transport does not support %s", buf->buf);
629
630                 strbuf_reset(buf);
631                 if (strbuf_getline(buf, stdin, '\n') == EOF)
632                         return;
633                 if (!*buf->buf)
634                         break;
635         } while (1);
636
637         if (fetch(nr_heads, to_fetch))
638                 exit(128); /* error already reported */
639         free_refs(list_head);
640         free(to_fetch);
641
642         printf("\n");
643         fflush(stdout);
644         strbuf_reset(buf);
645 }
646
647 static int push_dav(int nr_spec, char **specs)
648 {
649         const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
650         int argc = 0, i;
651
652         argv[argc++] = "http-push";
653         argv[argc++] = "--helper-status";
654         if (options.dry_run)
655                 argv[argc++] = "--dry-run";
656         if (options.verbosity > 1)
657                 argv[argc++] = "--verbose";
658         argv[argc++] = url;
659         for (i = 0; i < nr_spec; i++)
660                 argv[argc++] = specs[i];
661         argv[argc++] = NULL;
662
663         if (run_command_v_opt(argv, RUN_GIT_CMD))
664                 die("git-%s failed", argv[0]);
665         free(argv);
666         return 0;
667 }
668
669 static int push_git(struct discovery *heads, int nr_spec, char **specs)
670 {
671         struct rpc_state rpc;
672         const char **argv;
673         int argc = 0, i, err;
674
675         argv = xmalloc((10 + nr_spec) * sizeof(char*));
676         argv[argc++] = "send-pack";
677         argv[argc++] = "--stateless-rpc";
678         argv[argc++] = "--helper-status";
679         if (options.thin)
680                 argv[argc++] = "--thin";
681         if (options.dry_run)
682                 argv[argc++] = "--dry-run";
683         if (options.verbosity > 1)
684                 argv[argc++] = "--verbose";
685         argv[argc++] = url;
686         for (i = 0; i < nr_spec; i++)
687                 argv[argc++] = specs[i];
688         argv[argc++] = NULL;
689
690         memset(&rpc, 0, sizeof(rpc));
691         rpc.service_name = "git-receive-pack",
692         rpc.argv = argv;
693
694         err = rpc_service(&rpc, heads);
695         if (rpc.result.len)
696                 safe_write(1, rpc.result.buf, rpc.result.len);
697         strbuf_release(&rpc.result);
698         free(argv);
699         return err;
700 }
701
702 static int push(int nr_spec, char **specs)
703 {
704         struct discovery *heads = discover_refs("git-receive-pack");
705         int ret;
706
707         if (heads->proto_git)
708                 ret = push_git(heads, nr_spec, specs);
709         else
710                 ret = push_dav(nr_spec, specs);
711         free_discovery(heads);
712         return ret;
713 }
714
715 static void parse_push(struct strbuf *buf)
716 {
717         char **specs = NULL;
718         int alloc_spec = 0, nr_spec = 0, i;
719
720         do {
721                 if (!prefixcmp(buf->buf, "push ")) {
722                         ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
723                         specs[nr_spec++] = xstrdup(buf->buf + 5);
724                 }
725                 else
726                         die("http transport does not support %s", buf->buf);
727
728                 strbuf_reset(buf);
729                 if (strbuf_getline(buf, stdin, '\n') == EOF)
730                         return;
731                 if (!*buf->buf)
732                         break;
733         } while (1);
734
735         if (push(nr_spec, specs))
736                 exit(128); /* error already reported */
737         for (i = 0; i < nr_spec; i++)
738                 free(specs[i]);
739         free(specs);
740
741         printf("\n");
742         fflush(stdout);
743 }
744
745 int main(int argc, const char **argv)
746 {
747         struct strbuf buf = STRBUF_INIT;
748         int nongit;
749
750         git_extract_argv0_path(argv[0]);
751         setup_git_directory_gently(&nongit);
752         if (argc < 2) {
753                 fprintf(stderr, "Remote needed\n");
754                 return 1;
755         }
756
757         options.verbosity = 1;
758         options.progress = !!isatty(2);
759         options.thin = 1;
760
761         remote = remote_get(argv[1]);
762
763         if (argc > 2) {
764                 url = argv[2];
765         } else {
766                 url = remote->url[0];
767         }
768
769         do {
770                 if (strbuf_getline(&buf, stdin, '\n') == EOF)
771                         break;
772                 if (!prefixcmp(buf.buf, "fetch ")) {
773                         if (nongit)
774                                 die("Fetch attempted without a local repo");
775                         parse_fetch(&buf);
776
777                 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
778                         int for_push = !!strstr(buf.buf + 4, "for-push");
779                         output_refs(get_refs(for_push));
780
781                 } else if (!prefixcmp(buf.buf, "push ")) {
782                         parse_push(&buf);
783
784                 } else if (!prefixcmp(buf.buf, "option ")) {
785                         char *name = buf.buf + strlen("option ");
786                         char *value = strchr(name, ' ');
787                         int result;
788
789                         if (value)
790                                 *value++ = '\0';
791                         else
792                                 value = "true";
793
794                         result = set_option(name, value);
795                         if (!result)
796                                 printf("ok\n");
797                         else if (result < 0)
798                                 printf("error invalid value\n");
799                         else
800                                 printf("unsupported\n");
801                         fflush(stdout);
802
803                 } else if (!strcmp(buf.buf, "capabilities")) {
804                         printf("fetch\n");
805                         printf("option\n");
806                         printf("push\n");
807                         printf("\n");
808                         fflush(stdout);
809                 } else {
810                         return 1;
811                 }
812                 strbuf_reset(&buf);
813         } while (1);
814         return 0;
815 }