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