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