Merge branch 'ct/advise-push-default' 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; /* 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, int for_push)
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,
204                         for_push ? REF_NORMAL : 0, NULL);
205         close(async.out);
206         if (finish_async(&async))
207                 die("ref parsing thread failed");
208         return list;
209 }
210
211 static struct ref *parse_info_refs(struct discovery *heads)
212 {
213         char *data, *start, *mid;
214         char *ref_name;
215         int i = 0;
216
217         struct ref *refs = NULL;
218         struct ref *ref = NULL;
219         struct ref *last_ref = NULL;
220
221         data = heads->buf;
222         start = NULL;
223         mid = data;
224         while (i < heads->len) {
225                 if (!start) {
226                         start = &data[i];
227                 }
228                 if (data[i] == '\t')
229                         mid = &data[i];
230                 if (data[i] == '\n') {
231                         if (mid - start != 40)
232                                 die("%sinfo/refs not valid: is this a git repository?", url);
233                         data[i] = 0;
234                         ref_name = mid + 1;
235                         ref = xmalloc(sizeof(struct ref) +
236                                       strlen(ref_name) + 1);
237                         memset(ref, 0, sizeof(struct ref));
238                         strcpy(ref->name, ref_name);
239                         get_sha1_hex(start, ref->old_sha1);
240                         if (!refs)
241                                 refs = ref;
242                         if (last_ref)
243                                 last_ref->next = ref;
244                         last_ref = ref;
245                         start = NULL;
246                 }
247                 i++;
248         }
249
250         ref = alloc_ref("HEAD");
251         if (!http_fetch_ref(url, ref) &&
252             !resolve_remote_symref(ref, refs)) {
253                 ref->next = refs;
254                 refs = ref;
255         } else {
256                 free(ref);
257         }
258
259         return refs;
260 }
261
262 static struct ref *get_refs(int for_push)
263 {
264         struct discovery *heads;
265
266         if (for_push)
267                 heads = discover_refs("git-receive-pack");
268         else
269                 heads = discover_refs("git-upload-pack");
270
271         if (heads->proto_git)
272                 return parse_git_refs(heads, for_push);
273         return parse_info_refs(heads);
274 }
275
276 static void output_refs(struct ref *refs)
277 {
278         struct ref *posn;
279         for (posn = refs; posn; posn = posn->next) {
280                 if (posn->symref)
281                         printf("@%s %s\n", posn->symref, posn->name);
282                 else
283                         printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
284         }
285         printf("\n");
286         fflush(stdout);
287         free_refs(refs);
288 }
289
290 struct rpc_state {
291         const char *service_name;
292         const char **argv;
293         struct strbuf *stdin_preamble;
294         char *service_url;
295         char *hdr_content_type;
296         char *hdr_accept;
297         char *buf;
298         size_t alloc;
299         size_t len;
300         size_t pos;
301         int in;
302         int out;
303         struct strbuf result;
304         unsigned gzip_request : 1;
305         unsigned initial_buffer : 1;
306 };
307
308 static size_t rpc_out(void *ptr, size_t eltsize,
309                 size_t nmemb, void *buffer_)
310 {
311         size_t max = eltsize * nmemb;
312         struct rpc_state *rpc = buffer_;
313         size_t avail = rpc->len - rpc->pos;
314
315         if (!avail) {
316                 rpc->initial_buffer = 0;
317                 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
318                 if (!avail)
319                         return 0;
320                 rpc->pos = 0;
321                 rpc->len = avail;
322         }
323
324         if (max < avail)
325                 avail = max;
326         memcpy(ptr, rpc->buf + rpc->pos, avail);
327         rpc->pos += avail;
328         return avail;
329 }
330
331 #ifndef NO_CURL_IOCTL
332 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
333 {
334         struct rpc_state *rpc = clientp;
335
336         switch (cmd) {
337         case CURLIOCMD_NOP:
338                 return CURLIOE_OK;
339
340         case CURLIOCMD_RESTARTREAD:
341                 if (rpc->initial_buffer) {
342                         rpc->pos = 0;
343                         return CURLIOE_OK;
344                 }
345                 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
346                 return CURLIOE_FAILRESTART;
347
348         default:
349                 return CURLIOE_UNKNOWNCMD;
350         }
351 }
352 #endif
353
354 static size_t rpc_in(char *ptr, size_t eltsize,
355                 size_t nmemb, void *buffer_)
356 {
357         size_t size = eltsize * nmemb;
358         struct rpc_state *rpc = buffer_;
359         write_or_die(rpc->in, ptr, size);
360         return size;
361 }
362
363 static int run_slot(struct active_request_slot *slot)
364 {
365         int err = 0;
366         struct slot_results results;
367
368         slot->results = &results;
369         slot->curl_result = curl_easy_perform(slot->curl);
370         finish_active_slot(slot);
371
372         if (results.curl_result != CURLE_OK) {
373                 err |= error("RPC failed; result=%d, HTTP code = %ld",
374                         results.curl_result, results.http_code);
375         }
376
377         return err;
378 }
379
380 static int probe_rpc(struct rpc_state *rpc)
381 {
382         struct active_request_slot *slot;
383         struct curl_slist *headers = NULL;
384         struct strbuf buf = STRBUF_INIT;
385         int err;
386
387         slot = get_active_slot();
388
389         headers = curl_slist_append(headers, rpc->hdr_content_type);
390         headers = curl_slist_append(headers, rpc->hdr_accept);
391
392         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
393         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
394         curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
395         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
396         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
397         curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
398         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
399         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
400         curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
401
402         err = run_slot(slot);
403
404         curl_slist_free_all(headers);
405         strbuf_release(&buf);
406         return err;
407 }
408
409 static int post_rpc(struct rpc_state *rpc)
410 {
411         struct active_request_slot *slot;
412         struct curl_slist *headers = NULL;
413         int use_gzip = rpc->gzip_request;
414         char *gzip_body = NULL;
415         int err, large_request = 0;
416
417         /* Try to load the entire request, if we can fit it into the
418          * allocated buffer space we can use HTTP/1.0 and avoid the
419          * chunked encoding mess.
420          */
421         while (1) {
422                 size_t left = rpc->alloc - rpc->len;
423                 char *buf = rpc->buf + rpc->len;
424                 int n;
425
426                 if (left < LARGE_PACKET_MAX) {
427                         large_request = 1;
428                         use_gzip = 0;
429                         break;
430                 }
431
432                 n = packet_read_line(rpc->out, buf, left);
433                 if (!n)
434                         break;
435                 rpc->len += n;
436         }
437
438         if (large_request) {
439                 err = probe_rpc(rpc);
440                 if (err)
441                         return err;
442         }
443
444         slot = get_active_slot();
445
446         curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
447         curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
448         curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
449         curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
450
451         headers = curl_slist_append(headers, rpc->hdr_content_type);
452         headers = curl_slist_append(headers, rpc->hdr_accept);
453         headers = curl_slist_append(headers, "Expect:");
454
455         if (large_request) {
456                 /* The request body is large and the size cannot be predicted.
457                  * We must use chunked encoding to send it.
458                  */
459                 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
460                 rpc->initial_buffer = 1;
461                 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
462                 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
463 #ifndef NO_CURL_IOCTL
464                 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
465                 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
466 #endif
467                 if (options.verbosity > 1) {
468                         fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
469                         fflush(stderr);
470                 }
471
472         } else if (use_gzip && 1024 < rpc->len) {
473                 /* The client backend isn't giving us compressed data so
474                  * we can try to deflate it ourselves, this may save on.
475                  * the transfer time.
476                  */
477                 size_t size;
478                 git_zstream stream;
479                 int ret;
480
481                 memset(&stream, 0, sizeof(stream));
482                 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
483                 size = git_deflate_bound(&stream, rpc->len);
484                 gzip_body = xmalloc(size);
485
486                 stream.next_in = (unsigned char *)rpc->buf;
487                 stream.avail_in = rpc->len;
488                 stream.next_out = (unsigned char *)gzip_body;
489                 stream.avail_out = size;
490
491                 ret = git_deflate(&stream, Z_FINISH);
492                 if (ret != Z_STREAM_END)
493                         die("cannot deflate request; zlib deflate error %d", ret);
494
495                 ret = git_deflate_end_gently(&stream);
496                 if (ret != Z_OK)
497                         die("cannot deflate request; zlib end error %d", ret);
498
499                 size = stream.total_out;
500
501                 headers = curl_slist_append(headers, "Content-Encoding: gzip");
502                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
503                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
504
505                 if (options.verbosity > 1) {
506                         fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
507                                 rpc->service_name,
508                                 (unsigned long)rpc->len, (unsigned long)size);
509                         fflush(stderr);
510                 }
511         } else {
512                 /* We know the complete request size in advance, use the
513                  * more normal Content-Length approach.
514                  */
515                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
516                 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
517                 if (options.verbosity > 1) {
518                         fprintf(stderr, "POST %s (%lu bytes)\n",
519                                 rpc->service_name, (unsigned long)rpc->len);
520                         fflush(stderr);
521                 }
522         }
523
524         curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
525         curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
526         curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
527
528         err = run_slot(slot);
529
530         curl_slist_free_all(headers);
531         free(gzip_body);
532         return err;
533 }
534
535 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
536 {
537         const char *svc = rpc->service_name;
538         struct strbuf buf = STRBUF_INIT;
539         struct strbuf *preamble = rpc->stdin_preamble;
540         struct child_process client;
541         int err = 0;
542
543         memset(&client, 0, sizeof(client));
544         client.in = -1;
545         client.out = -1;
546         client.git_cmd = 1;
547         client.argv = rpc->argv;
548         if (start_command(&client))
549                 exit(1);
550         if (preamble)
551                 write_or_die(client.in, preamble->buf, preamble->len);
552         if (heads)
553                 write_or_die(client.in, heads->buf, heads->len);
554
555         rpc->alloc = http_post_buffer;
556         rpc->buf = xmalloc(rpc->alloc);
557         rpc->in = client.in;
558         rpc->out = client.out;
559         strbuf_init(&rpc->result, 0);
560
561         strbuf_addf(&buf, "%s%s", url, svc);
562         rpc->service_url = strbuf_detach(&buf, NULL);
563
564         strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
565         rpc->hdr_content_type = strbuf_detach(&buf, NULL);
566
567         strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
568         rpc->hdr_accept = strbuf_detach(&buf, NULL);
569
570         while (!err) {
571                 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
572                 if (!n)
573                         break;
574                 rpc->pos = 0;
575                 rpc->len = n;
576                 err |= post_rpc(rpc);
577         }
578
579         close(client.in);
580         client.in = -1;
581         if (!err) {
582                 strbuf_read(&rpc->result, client.out, 0);
583         } else {
584                 char buf[4096];
585                 for (;;)
586                         if (xread(client.out, buf, sizeof(buf)) <= 0)
587                                 break;
588         }
589
590         close(client.out);
591         client.out = -1;
592
593         err |= finish_command(&client);
594         free(rpc->service_url);
595         free(rpc->hdr_content_type);
596         free(rpc->hdr_accept);
597         free(rpc->buf);
598         strbuf_release(&buf);
599         return err;
600 }
601
602 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
603 {
604         struct walker *walker;
605         char **targets = xmalloc(nr_heads * sizeof(char*));
606         int ret, i;
607
608         if (options.depth)
609                 die("dumb http transport does not support --depth");
610         for (i = 0; i < nr_heads; i++)
611                 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
612
613         walker = get_http_walker(url);
614         walker->get_all = 1;
615         walker->get_tree = 1;
616         walker->get_history = 1;
617         walker->get_verbosely = options.verbosity >= 3;
618         walker->get_recover = 0;
619         ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
620         walker_free(walker);
621
622         for (i = 0; i < nr_heads; i++)
623                 free(targets[i]);
624         free(targets);
625
626         return ret ? error("Fetch failed.") : 0;
627 }
628
629 static int fetch_git(struct discovery *heads,
630         int nr_heads, struct ref **to_fetch)
631 {
632         struct rpc_state rpc;
633         struct strbuf preamble = STRBUF_INIT;
634         char *depth_arg = NULL;
635         int argc = 0, i, err;
636         const char *argv[15];
637
638         argv[argc++] = "fetch-pack";
639         argv[argc++] = "--stateless-rpc";
640         argv[argc++] = "--stdin";
641         argv[argc++] = "--lock-pack";
642         if (options.followtags)
643                 argv[argc++] = "--include-tag";
644         if (options.thin)
645                 argv[argc++] = "--thin";
646         if (options.verbosity >= 3) {
647                 argv[argc++] = "-v";
648                 argv[argc++] = "-v";
649         }
650         if (!options.progress)
651                 argv[argc++] = "--no-progress";
652         if (options.depth) {
653                 struct strbuf buf = STRBUF_INIT;
654                 strbuf_addf(&buf, "--depth=%lu", options.depth);
655                 depth_arg = strbuf_detach(&buf, NULL);
656                 argv[argc++] = depth_arg;
657         }
658         argv[argc++] = url;
659         argv[argc++] = NULL;
660
661         for (i = 0; i < nr_heads; i++) {
662                 struct ref *ref = to_fetch[i];
663                 if (!ref->name || !*ref->name)
664                         die("cannot fetch by sha1 over smart http");
665                 packet_buf_write(&preamble, "%s\n", ref->name);
666         }
667         packet_buf_flush(&preamble);
668
669         memset(&rpc, 0, sizeof(rpc));
670         rpc.service_name = "git-upload-pack",
671         rpc.argv = argv;
672         rpc.stdin_preamble = &preamble;
673         rpc.gzip_request = 1;
674
675         err = rpc_service(&rpc, heads);
676         if (rpc.result.len)
677                 safe_write(1, rpc.result.buf, rpc.result.len);
678         strbuf_release(&rpc.result);
679         strbuf_release(&preamble);
680         free(depth_arg);
681         return err;
682 }
683
684 static int fetch(int nr_heads, struct ref **to_fetch)
685 {
686         struct discovery *d = discover_refs("git-upload-pack");
687         if (d->proto_git)
688                 return fetch_git(d, nr_heads, to_fetch);
689         else
690                 return fetch_dumb(nr_heads, to_fetch);
691 }
692
693 static void parse_fetch(struct strbuf *buf)
694 {
695         struct ref **to_fetch = NULL;
696         struct ref *list_head = NULL;
697         struct ref **list = &list_head;
698         int alloc_heads = 0, nr_heads = 0;
699
700         do {
701                 if (!prefixcmp(buf->buf, "fetch ")) {
702                         char *p = buf->buf + strlen("fetch ");
703                         char *name;
704                         struct ref *ref;
705                         unsigned char old_sha1[20];
706
707                         if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
708                                 die("protocol error: expected sha/ref, got %s'", p);
709                         if (p[40] == ' ')
710                                 name = p + 41;
711                         else if (!p[40])
712                                 name = "";
713                         else
714                                 die("protocol error: expected sha/ref, got %s'", p);
715
716                         ref = alloc_ref(name);
717                         hashcpy(ref->old_sha1, old_sha1);
718
719                         *list = ref;
720                         list = &ref->next;
721
722                         ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
723                         to_fetch[nr_heads++] = ref;
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 (fetch(nr_heads, to_fetch))
736                 exit(128); /* error already reported */
737         free_refs(list_head);
738         free(to_fetch);
739
740         printf("\n");
741         fflush(stdout);
742         strbuf_reset(buf);
743 }
744
745 static int push_dav(int nr_spec, char **specs)
746 {
747         const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
748         int argc = 0, i;
749
750         argv[argc++] = "http-push";
751         argv[argc++] = "--helper-status";
752         if (options.dry_run)
753                 argv[argc++] = "--dry-run";
754         if (options.verbosity > 1)
755                 argv[argc++] = "--verbose";
756         argv[argc++] = url;
757         for (i = 0; i < nr_spec; i++)
758                 argv[argc++] = specs[i];
759         argv[argc++] = NULL;
760
761         if (run_command_v_opt(argv, RUN_GIT_CMD))
762                 die("git-%s failed", argv[0]);
763         free(argv);
764         return 0;
765 }
766
767 static int push_git(struct discovery *heads, int nr_spec, char **specs)
768 {
769         struct rpc_state rpc;
770         const char **argv;
771         int argc = 0, i, err;
772
773         argv = xmalloc((10 + nr_spec) * sizeof(char*));
774         argv[argc++] = "send-pack";
775         argv[argc++] = "--stateless-rpc";
776         argv[argc++] = "--helper-status";
777         if (options.thin)
778                 argv[argc++] = "--thin";
779         if (options.dry_run)
780                 argv[argc++] = "--dry-run";
781         if (options.verbosity == 0)
782                 argv[argc++] = "--quiet";
783         else if (options.verbosity > 1)
784                 argv[argc++] = "--verbose";
785         argv[argc++] = options.progress ? "--progress" : "--no-progress";
786         argv[argc++] = url;
787         for (i = 0; i < nr_spec; i++)
788                 argv[argc++] = specs[i];
789         argv[argc++] = NULL;
790
791         memset(&rpc, 0, sizeof(rpc));
792         rpc.service_name = "git-receive-pack",
793         rpc.argv = argv;
794
795         err = rpc_service(&rpc, heads);
796         if (rpc.result.len)
797                 safe_write(1, rpc.result.buf, rpc.result.len);
798         strbuf_release(&rpc.result);
799         free(argv);
800         return err;
801 }
802
803 static int push(int nr_spec, char **specs)
804 {
805         struct discovery *heads = discover_refs("git-receive-pack");
806         int ret;
807
808         if (heads->proto_git)
809                 ret = push_git(heads, nr_spec, specs);
810         else
811                 ret = push_dav(nr_spec, specs);
812         free_discovery(heads);
813         return ret;
814 }
815
816 static void parse_push(struct strbuf *buf)
817 {
818         char **specs = NULL;
819         int alloc_spec = 0, nr_spec = 0, i, ret;
820
821         do {
822                 if (!prefixcmp(buf->buf, "push ")) {
823                         ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
824                         specs[nr_spec++] = xstrdup(buf->buf + 5);
825                 }
826                 else
827                         die("http transport does not support %s", buf->buf);
828
829                 strbuf_reset(buf);
830                 if (strbuf_getline(buf, stdin, '\n') == EOF)
831                         goto free_specs;
832                 if (!*buf->buf)
833                         break;
834         } while (1);
835
836         ret = push(nr_spec, specs);
837         printf("\n");
838         fflush(stdout);
839
840         if (ret)
841                 exit(128); /* error already reported */
842
843  free_specs:
844         for (i = 0; i < nr_spec; i++)
845                 free(specs[i]);
846         free(specs);
847 }
848
849 int main(int argc, const char **argv)
850 {
851         struct strbuf buf = STRBUF_INIT;
852         int nongit;
853
854         git_extract_argv0_path(argv[0]);
855         setup_git_directory_gently(&nongit);
856         if (argc < 2) {
857                 fprintf(stderr, "Remote needed\n");
858                 return 1;
859         }
860
861         options.verbosity = 1;
862         options.progress = !!isatty(2);
863         options.thin = 1;
864
865         remote = remote_get(argv[1]);
866
867         if (argc > 2) {
868                 end_url_with_slash(&buf, argv[2]);
869         } else {
870                 end_url_with_slash(&buf, remote->url[0]);
871         }
872
873         url = strbuf_detach(&buf, NULL);
874
875         http_init(remote, url, 0);
876
877         do {
878                 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
879                         if (ferror(stdin))
880                                 fprintf(stderr, "Error reading command stream\n");
881                         else
882                                 fprintf(stderr, "Unexpected end of command stream\n");
883                         return 1;
884                 }
885                 if (buf.len == 0)
886                         break;
887                 if (!prefixcmp(buf.buf, "fetch ")) {
888                         if (nongit)
889                                 die("Fetch attempted without a local repo");
890                         parse_fetch(&buf);
891
892                 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
893                         int for_push = !!strstr(buf.buf + 4, "for-push");
894                         output_refs(get_refs(for_push));
895
896                 } else if (!prefixcmp(buf.buf, "push ")) {
897                         parse_push(&buf);
898
899                 } else if (!prefixcmp(buf.buf, "option ")) {
900                         char *name = buf.buf + strlen("option ");
901                         char *value = strchr(name, ' ');
902                         int result;
903
904                         if (value)
905                                 *value++ = '\0';
906                         else
907                                 value = "true";
908
909                         result = set_option(name, value);
910                         if (!result)
911                                 printf("ok\n");
912                         else if (result < 0)
913                                 printf("error invalid value\n");
914                         else
915                                 printf("unsupported\n");
916                         fflush(stdout);
917
918                 } else if (!strcmp(buf.buf, "capabilities")) {
919                         printf("fetch\n");
920                         printf("option\n");
921                         printf("push\n");
922                         printf("\n");
923                         fflush(stdout);
924                 } else {
925                         fprintf(stderr, "Unknown command '%s'\n", buf.buf);
926                         return 1;
927                 }
928                 strbuf_reset(&buf);
929         } while (1);
930
931         http_cleanup();
932
933         return 0;
934 }