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