smart-http: Really never use Expect: 100-continue
[git/mingw.git] / remote-curl.c
blob1f41a90936e5865d94883f57887e7ce57b716f0f
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"
11 static struct remote *remote;
12 static const char *url;
13 static struct walker *walker;
15 struct options {
16 int verbosity;
17 unsigned long depth;
18 unsigned progress : 1,
19 followtags : 1,
20 dry_run : 1,
21 thin : 1;
23 static struct options options;
25 static void init_walker(void)
27 if (!walker)
28 walker = get_http_walker(url, remote);
31 static int set_option(const char *name, const char *value)
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;
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;
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;
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;
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;
76 else {
77 return 1 /* unsupported */;
81 struct discovery {
82 const char *service;
83 char *buf_alloc;
84 char *buf;
85 size_t len;
86 unsigned proto_git : 1;
88 static struct discovery *last_discovery;
90 static void free_discovery(struct discovery *d)
92 if (d) {
93 if (d == last_discovery)
94 last_discovery = NULL;
95 free(d->buf_alloc);
96 free(d);
100 static struct discovery* discover_refs(const char *service)
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;
107 if (last && !strcmp(service, last->service))
108 return last;
109 free_discovery(last);
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);
120 refs_url = strbuf_detach(&buffer, NULL);
122 init_walker();
123 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
125 /* try again with "plain" url (no ? or & appended) */
126 if (http_ret != HTTP_OK) {
127 free(refs_url);
128 strbuf_reset(&buffer);
130 proto_git_candidate = 0;
131 strbuf_addf(&buffer, "%s/info/refs", url);
132 refs_url = strbuf_detach(&buffer, NULL);
134 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
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");
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;
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.
158 struct strbuf exp = STRBUF_INIT;
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);
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);
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.
174 strbuf_reset(&buffer);
175 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
176 strbuf_reset(&buffer);
178 last->proto_git = 1;
181 free(refs_url);
182 strbuf_release(&buffer);
183 last_discovery = last;
184 return last;
187 static int write_discovery(int in, int out, void *data)
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;
197 static struct ref *parse_git_refs(struct discovery *heads)
199 struct ref *list = NULL;
200 struct async async;
202 memset(&async, 0, sizeof(async));
203 async.proc = write_discovery;
204 async.data = heads;
205 async.out = -1;
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;
216 static struct ref *parse_info_refs(struct discovery *heads)
218 char *data, *start, *mid;
219 char *ref_name;
220 int i = 0;
222 struct ref *refs = NULL;
223 struct ref *ref = NULL;
224 struct ref *last_ref = NULL;
226 data = heads->buf;
227 start = NULL;
228 mid = data;
229 while (i < heads->len) {
230 if (!start) {
231 start = &data[i];
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;
250 i++;
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);
263 return refs;
266 static struct ref *get_refs(int for_push)
268 struct discovery *heads;
270 if (for_push)
271 heads = discover_refs("git-receive-pack");
272 else
273 heads = discover_refs("git-upload-pack");
275 if (heads->proto_git)
276 return parse_git_refs(heads);
277 return parse_info_refs(heads);
280 static void output_refs(struct ref *refs)
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);
289 printf("\n");
290 fflush(stdout);
291 free_refs(refs);
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;
311 static size_t rpc_out(void *ptr, size_t eltsize,
312 size_t nmemb, void *buffer_)
314 size_t max = eltsize * nmemb;
315 struct rpc_state *rpc = buffer_;
316 size_t avail = rpc->len - rpc->pos;
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;
327 if (max < avail)
328 avail = max;
329 memcpy(ptr, rpc->buf + rpc->pos, avail);
330 rpc->pos += avail;
331 return avail;
334 #ifndef NO_CURL_IOCTL
335 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
337 struct rpc_state *rpc = clientp;
339 switch (cmd) {
340 case CURLIOCMD_NOP:
341 return CURLIOE_OK;
343 case CURLIOCMD_RESTARTREAD:
344 if (rpc->initial_buffer) {
345 rpc->pos = 0;
346 return CURLIOE_OK;
348 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
349 return CURLIOE_FAILRESTART;
351 default:
352 return CURLIOE_UNKNOWNCMD;
355 #endif
357 static size_t rpc_in(const void *ptr, size_t eltsize,
358 size_t nmemb, void *buffer_)
360 size_t size = eltsize * nmemb;
361 struct rpc_state *rpc = buffer_;
362 write_or_die(rpc->in, ptr, size);
363 return size;
366 static int run_slot(struct active_request_slot *slot)
368 int err = 0;
369 struct slot_results results;
371 slot->results = &results;
372 slot->curl_result = curl_easy_perform(slot->curl);
373 finish_active_slot(slot);
375 if (results.curl_result != CURLE_OK) {
376 err |= error("RPC failed; result=%d, HTTP code = %ld",
377 results.curl_result, results.http_code);
380 return err;
383 static int probe_rpc(struct rpc_state *rpc)
385 struct active_request_slot *slot;
386 struct curl_slist *headers = NULL;
387 struct strbuf buf = STRBUF_INIT;
388 int err;
390 slot = get_active_slot();
392 headers = curl_slist_append(headers, rpc->hdr_content_type);
393 headers = curl_slist_append(headers, rpc->hdr_accept);
395 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
396 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
397 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
398 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
399 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
400 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
401 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
402 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
403 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
405 err = run_slot(slot);
407 curl_slist_free_all(headers);
408 strbuf_release(&buf);
409 return err;
412 static int post_rpc(struct rpc_state *rpc)
414 struct active_request_slot *slot;
415 struct curl_slist *headers = NULL;
416 int use_gzip = rpc->gzip_request;
417 char *gzip_body = NULL;
418 int err, large_request = 0;
420 /* Try to load the entire request, if we can fit it into the
421 * allocated buffer space we can use HTTP/1.0 and avoid the
422 * chunked encoding mess.
424 while (1) {
425 size_t left = rpc->alloc - rpc->len;
426 char *buf = rpc->buf + rpc->len;
427 int n;
429 if (left < LARGE_PACKET_MAX) {
430 large_request = 1;
431 use_gzip = 0;
432 break;
435 n = packet_read_line(rpc->out, buf, left);
436 if (!n)
437 break;
438 rpc->len += n;
441 if (large_request) {
442 err = probe_rpc(rpc);
443 if (err)
444 return err;
447 slot = get_active_slot();
449 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
450 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
451 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
452 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
454 headers = curl_slist_append(headers, rpc->hdr_content_type);
455 headers = curl_slist_append(headers, rpc->hdr_accept);
456 headers = curl_slist_append(headers, "Expect:");
458 if (large_request) {
459 /* The request body is large and the size cannot be predicted.
460 * We must use chunked encoding to send it.
462 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
463 rpc->initial_buffer = 1;
464 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
465 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
466 #ifndef NO_CURL_IOCTL
467 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
468 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
469 #endif
470 if (options.verbosity > 1) {
471 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
472 fflush(stderr);
475 } else if (use_gzip && 1024 < rpc->len) {
476 /* The client backend isn't giving us compressed data so
477 * we can try to deflate it ourselves, this may save on.
478 * the transfer time.
480 size_t size;
481 z_stream stream;
482 int ret;
484 memset(&stream, 0, sizeof(stream));
485 ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
486 Z_DEFLATED, (15 + 16),
487 8, Z_DEFAULT_STRATEGY);
488 if (ret != Z_OK)
489 die("cannot deflate request; zlib init error %d", ret);
490 size = deflateBound(&stream, rpc->len);
491 gzip_body = xmalloc(size);
493 stream.next_in = (unsigned char *)rpc->buf;
494 stream.avail_in = rpc->len;
495 stream.next_out = (unsigned char *)gzip_body;
496 stream.avail_out = size;
498 ret = deflate(&stream, Z_FINISH);
499 if (ret != Z_STREAM_END)
500 die("cannot deflate request; zlib deflate error %d", ret);
502 ret = deflateEnd(&stream);
503 if (ret != Z_OK)
504 die("cannot deflate request; zlib end error %d", ret);
506 size = stream.total_out;
508 headers = curl_slist_append(headers, "Content-Encoding: gzip");
509 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
510 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
512 if (options.verbosity > 1) {
513 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
514 rpc->service_name,
515 (unsigned long)rpc->len, (unsigned long)size);
516 fflush(stderr);
518 } else {
519 /* We know the complete request size in advance, use the
520 * more normal Content-Length approach.
522 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
523 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
524 if (options.verbosity > 1) {
525 fprintf(stderr, "POST %s (%lu bytes)\n",
526 rpc->service_name, (unsigned long)rpc->len);
527 fflush(stderr);
531 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
532 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
533 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
535 err = run_slot(slot);
537 curl_slist_free_all(headers);
538 free(gzip_body);
539 return err;
542 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
544 const char *svc = rpc->service_name;
545 struct strbuf buf = STRBUF_INIT;
546 struct child_process client;
547 int err = 0;
549 init_walker();
550 memset(&client, 0, sizeof(client));
551 client.in = -1;
552 client.out = -1;
553 client.git_cmd = 1;
554 client.argv = rpc->argv;
555 if (start_command(&client))
556 exit(1);
557 if (heads)
558 write_or_die(client.in, heads->buf, heads->len);
560 rpc->alloc = http_post_buffer;
561 rpc->buf = xmalloc(rpc->alloc);
562 rpc->in = client.in;
563 rpc->out = client.out;
564 strbuf_init(&rpc->result, 0);
566 strbuf_addf(&buf, "%s/%s", url, svc);
567 rpc->service_url = strbuf_detach(&buf, NULL);
569 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
570 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
572 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
573 rpc->hdr_accept = strbuf_detach(&buf, NULL);
575 while (!err) {
576 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
577 if (!n)
578 break;
579 rpc->pos = 0;
580 rpc->len = n;
581 err |= post_rpc(rpc);
583 strbuf_read(&rpc->result, client.out, 0);
585 close(client.in);
586 close(client.out);
587 client.in = -1;
588 client.out = -1;
590 err |= finish_command(&client);
591 free(rpc->service_url);
592 free(rpc->hdr_content_type);
593 free(rpc->hdr_accept);
594 free(rpc->buf);
595 strbuf_release(&buf);
596 return err;
599 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
601 char **targets = xmalloc(nr_heads * sizeof(char*));
602 int ret, i;
604 if (options.depth)
605 die("dumb http transport does not support --depth");
606 for (i = 0; i < nr_heads; i++)
607 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
609 init_walker();
610 walker->get_all = 1;
611 walker->get_tree = 1;
612 walker->get_history = 1;
613 walker->get_verbosely = options.verbosity >= 3;
614 walker->get_recover = 0;
615 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
617 for (i = 0; i < nr_heads; i++)
618 free(targets[i]);
619 free(targets);
621 return ret ? error("Fetch failed.") : 0;
624 static int fetch_git(struct discovery *heads,
625 int nr_heads, struct ref **to_fetch)
627 struct rpc_state rpc;
628 char *depth_arg = NULL;
629 const char **argv;
630 int argc = 0, i, err;
632 argv = xmalloc((15 + nr_heads) * sizeof(char*));
633 argv[argc++] = "fetch-pack";
634 argv[argc++] = "--stateless-rpc";
635 argv[argc++] = "--lock-pack";
636 if (options.followtags)
637 argv[argc++] = "--include-tag";
638 if (options.thin)
639 argv[argc++] = "--thin";
640 if (options.verbosity >= 3) {
641 argv[argc++] = "-v";
642 argv[argc++] = "-v";
644 if (!options.progress)
645 argv[argc++] = "--no-progress";
646 if (options.depth) {
647 struct strbuf buf = STRBUF_INIT;
648 strbuf_addf(&buf, "--depth=%lu", options.depth);
649 depth_arg = strbuf_detach(&buf, NULL);
650 argv[argc++] = depth_arg;
652 argv[argc++] = url;
653 for (i = 0; i < nr_heads; i++) {
654 struct ref *ref = to_fetch[i];
655 if (!ref->name || !*ref->name)
656 die("cannot fetch by sha1 over smart http");
657 argv[argc++] = ref->name;
659 argv[argc++] = NULL;
661 memset(&rpc, 0, sizeof(rpc));
662 rpc.service_name = "git-upload-pack",
663 rpc.argv = argv;
664 rpc.gzip_request = 1;
666 err = rpc_service(&rpc, heads);
667 if (rpc.result.len)
668 safe_write(1, rpc.result.buf, rpc.result.len);
669 strbuf_release(&rpc.result);
670 free(argv);
671 free(depth_arg);
672 return err;
675 static int fetch(int nr_heads, struct ref **to_fetch)
677 struct discovery *d = discover_refs("git-upload-pack");
678 if (d->proto_git)
679 return fetch_git(d, nr_heads, to_fetch);
680 else
681 return fetch_dumb(nr_heads, to_fetch);
684 static void parse_fetch(struct strbuf *buf)
686 struct ref **to_fetch = NULL;
687 struct ref *list_head = NULL;
688 struct ref **list = &list_head;
689 int alloc_heads = 0, nr_heads = 0;
691 do {
692 if (!prefixcmp(buf->buf, "fetch ")) {
693 char *p = buf->buf + strlen("fetch ");
694 char *name;
695 struct ref *ref;
696 unsigned char old_sha1[20];
698 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
699 die("protocol error: expected sha/ref, got %s'", p);
700 if (p[40] == ' ')
701 name = p + 41;
702 else if (!p[40])
703 name = "";
704 else
705 die("protocol error: expected sha/ref, got %s'", p);
707 ref = alloc_ref(name);
708 hashcpy(ref->old_sha1, old_sha1);
710 *list = ref;
711 list = &ref->next;
713 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
714 to_fetch[nr_heads++] = ref;
716 else
717 die("http transport does not support %s", buf->buf);
719 strbuf_reset(buf);
720 if (strbuf_getline(buf, stdin, '\n') == EOF)
721 return;
722 if (!*buf->buf)
723 break;
724 } while (1);
726 if (fetch(nr_heads, to_fetch))
727 exit(128); /* error already reported */
728 free_refs(list_head);
729 free(to_fetch);
731 printf("\n");
732 fflush(stdout);
733 strbuf_reset(buf);
736 static int push_dav(int nr_spec, char **specs)
738 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
739 int argc = 0, i;
741 argv[argc++] = "http-push";
742 argv[argc++] = "--helper-status";
743 if (options.dry_run)
744 argv[argc++] = "--dry-run";
745 if (options.verbosity > 1)
746 argv[argc++] = "--verbose";
747 argv[argc++] = url;
748 for (i = 0; i < nr_spec; i++)
749 argv[argc++] = specs[i];
750 argv[argc++] = NULL;
752 if (run_command_v_opt(argv, RUN_GIT_CMD))
753 die("git-%s failed", argv[0]);
754 free(argv);
755 return 0;
758 static int push_git(struct discovery *heads, int nr_spec, char **specs)
760 struct rpc_state rpc;
761 const char **argv;
762 int argc = 0, i, err;
764 argv = xmalloc((10 + nr_spec) * sizeof(char*));
765 argv[argc++] = "send-pack";
766 argv[argc++] = "--stateless-rpc";
767 argv[argc++] = "--helper-status";
768 if (options.thin)
769 argv[argc++] = "--thin";
770 if (options.dry_run)
771 argv[argc++] = "--dry-run";
772 if (options.verbosity > 1)
773 argv[argc++] = "--verbose";
774 argv[argc++] = url;
775 for (i = 0; i < nr_spec; i++)
776 argv[argc++] = specs[i];
777 argv[argc++] = NULL;
779 memset(&rpc, 0, sizeof(rpc));
780 rpc.service_name = "git-receive-pack",
781 rpc.argv = argv;
783 err = rpc_service(&rpc, heads);
784 if (rpc.result.len)
785 safe_write(1, rpc.result.buf, rpc.result.len);
786 strbuf_release(&rpc.result);
787 free(argv);
788 return err;
791 static int push(int nr_spec, char **specs)
793 struct discovery *heads = discover_refs("git-receive-pack");
794 int ret;
796 if (heads->proto_git)
797 ret = push_git(heads, nr_spec, specs);
798 else
799 ret = push_dav(nr_spec, specs);
800 free_discovery(heads);
801 return ret;
804 static void parse_push(struct strbuf *buf)
806 char **specs = NULL;
807 int alloc_spec = 0, nr_spec = 0, i;
809 do {
810 if (!prefixcmp(buf->buf, "push ")) {
811 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
812 specs[nr_spec++] = xstrdup(buf->buf + 5);
814 else
815 die("http transport does not support %s", buf->buf);
817 strbuf_reset(buf);
818 if (strbuf_getline(buf, stdin, '\n') == EOF)
819 return;
820 if (!*buf->buf)
821 break;
822 } while (1);
824 if (push(nr_spec, specs))
825 exit(128); /* error already reported */
826 for (i = 0; i < nr_spec; i++)
827 free(specs[i]);
828 free(specs);
830 printf("\n");
831 fflush(stdout);
834 int main(int argc, const char **argv)
836 struct strbuf buf = STRBUF_INIT;
837 int nongit;
839 git_extract_argv0_path(argv[0]);
840 setup_git_directory_gently(&nongit);
841 if (argc < 2) {
842 fprintf(stderr, "Remote needed\n");
843 return 1;
846 options.verbosity = 1;
847 options.progress = !!isatty(2);
848 options.thin = 1;
850 remote = remote_get(argv[1]);
852 if (argc > 2) {
853 url = argv[2];
854 } else {
855 url = remote->url[0];
858 do {
859 if (strbuf_getline(&buf, stdin, '\n') == EOF)
860 break;
861 if (!prefixcmp(buf.buf, "fetch ")) {
862 if (nongit)
863 die("Fetch attempted without a local repo");
864 parse_fetch(&buf);
866 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
867 int for_push = !!strstr(buf.buf + 4, "for-push");
868 output_refs(get_refs(for_push));
870 } else if (!prefixcmp(buf.buf, "push ")) {
871 parse_push(&buf);
873 } else if (!prefixcmp(buf.buf, "option ")) {
874 char *name = buf.buf + strlen("option ");
875 char *value = strchr(name, ' ');
876 int result;
878 if (value)
879 *value++ = '\0';
880 else
881 value = "true";
883 result = set_option(name, value);
884 if (!result)
885 printf("ok\n");
886 else if (result < 0)
887 printf("error invalid value\n");
888 else
889 printf("unsupported\n");
890 fflush(stdout);
892 } else if (!strcmp(buf.buf, "capabilities")) {
893 printf("fetch\n");
894 printf("option\n");
895 printf("push\n");
896 printf("\n");
897 fflush(stdout);
898 } else {
899 return 1;
901 strbuf_reset(&buf);
902 } while (1);
903 return 0;