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