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