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