http: refactor options to http_get_*
[git/raj.git] / remote-curl.c
blobed1499b62cbf76001cdaa6db0595d613862ee3c1
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 #include "argv-array.h"
12 static struct remote *remote;
13 static const char *url; /* always ends with a trailing slash */
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 int set_option(const char *name, const char *value)
27 if (!strcmp(name, "verbosity")) {
28 char *end;
29 int v = strtol(value, &end, 10);
30 if (value == end || *end)
31 return -1;
32 options.verbosity = v;
33 return 0;
35 else if (!strcmp(name, "progress")) {
36 if (!strcmp(value, "true"))
37 options.progress = 1;
38 else if (!strcmp(value, "false"))
39 options.progress = 0;
40 else
41 return -1;
42 return 0;
44 else if (!strcmp(name, "depth")) {
45 char *end;
46 unsigned long v = strtoul(value, &end, 10);
47 if (value == end || *end)
48 return -1;
49 options.depth = v;
50 return 0;
52 else if (!strcmp(name, "followtags")) {
53 if (!strcmp(value, "true"))
54 options.followtags = 1;
55 else if (!strcmp(value, "false"))
56 options.followtags = 0;
57 else
58 return -1;
59 return 0;
61 else if (!strcmp(name, "dry-run")) {
62 if (!strcmp(value, "true"))
63 options.dry_run = 1;
64 else if (!strcmp(value, "false"))
65 options.dry_run = 0;
66 else
67 return -1;
68 return 0;
70 else {
71 return 1 /* unsupported */;
75 struct discovery {
76 const char *service;
77 char *buf_alloc;
78 char *buf;
79 size_t len;
80 struct ref *refs;
81 unsigned proto_git : 1;
83 static struct discovery *last_discovery;
85 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
87 struct ref *list = NULL;
88 get_remote_heads(-1, heads->buf, heads->len, &list,
89 for_push ? REF_NORMAL : 0, NULL);
90 return list;
93 static struct ref *parse_info_refs(struct discovery *heads)
95 char *data, *start, *mid;
96 char *ref_name;
97 int i = 0;
99 struct ref *refs = NULL;
100 struct ref *ref = NULL;
101 struct ref *last_ref = NULL;
103 data = heads->buf;
104 start = NULL;
105 mid = data;
106 while (i < heads->len) {
107 if (!start) {
108 start = &data[i];
110 if (data[i] == '\t')
111 mid = &data[i];
112 if (data[i] == '\n') {
113 if (mid - start != 40)
114 die("%sinfo/refs not valid: is this a git repository?", url);
115 data[i] = 0;
116 ref_name = mid + 1;
117 ref = xmalloc(sizeof(struct ref) +
118 strlen(ref_name) + 1);
119 memset(ref, 0, sizeof(struct ref));
120 strcpy(ref->name, ref_name);
121 get_sha1_hex(start, ref->old_sha1);
122 if (!refs)
123 refs = ref;
124 if (last_ref)
125 last_ref->next = ref;
126 last_ref = ref;
127 start = NULL;
129 i++;
132 ref = alloc_ref("HEAD");
133 if (!http_fetch_ref(url, ref) &&
134 !resolve_remote_symref(ref, refs)) {
135 ref->next = refs;
136 refs = ref;
137 } else {
138 free(ref);
141 return refs;
144 static void free_discovery(struct discovery *d)
146 if (d) {
147 if (d == last_discovery)
148 last_discovery = NULL;
149 free(d->buf_alloc);
150 free_refs(d->refs);
151 free(d);
155 static int show_http_message(struct strbuf *type, struct strbuf *msg)
157 const char *p, *eol;
160 * We only show text/plain parts, as other types are likely
161 * to be ugly to look at on the user's terminal.
163 * TODO should handle "; charset=XXX", and re-encode into
164 * logoutputencoding
166 if (strcasecmp(type->buf, "text/plain"))
167 return -1;
169 strbuf_trim(msg);
170 if (!msg->len)
171 return -1;
173 p = msg->buf;
174 do {
175 eol = strchrnul(p, '\n');
176 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
177 p = eol + 1;
178 } while(*eol);
179 return 0;
182 static struct discovery* discover_refs(const char *service, int for_push)
184 struct strbuf exp = STRBUF_INIT;
185 struct strbuf type = STRBUF_INIT;
186 struct strbuf buffer = STRBUF_INIT;
187 struct discovery *last = last_discovery;
188 char *refs_url;
189 int http_ret, maybe_smart = 0;
190 struct http_get_options options;
192 if (last && !strcmp(service, last->service))
193 return last;
194 free_discovery(last);
196 strbuf_addf(&buffer, "%sinfo/refs", url);
197 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
198 git_env_bool("GIT_SMART_HTTP", 1)) {
199 maybe_smart = 1;
200 if (!strchr(url, '?'))
201 strbuf_addch(&buffer, '?');
202 else
203 strbuf_addch(&buffer, '&');
204 strbuf_addf(&buffer, "service=%s", service);
206 refs_url = strbuf_detach(&buffer, NULL);
208 memset(&options, 0, sizeof(options));
209 options.content_type = &type;
210 options.no_cache = 1;
211 options.keep_error = 1;
213 http_ret = http_get_strbuf(refs_url, &buffer, &options);
214 switch (http_ret) {
215 case HTTP_OK:
216 break;
217 case HTTP_MISSING_TARGET:
218 show_http_message(&type, &buffer);
219 die("repository '%s' not found", url);
220 case HTTP_NOAUTH:
221 show_http_message(&type, &buffer);
222 die("Authentication failed for '%s'", url);
223 default:
224 show_http_message(&type, &buffer);
225 die("unable to access '%s': %s", url, curl_errorstr);
228 last= xcalloc(1, sizeof(*last_discovery));
229 last->service = service;
230 last->buf_alloc = strbuf_detach(&buffer, &last->len);
231 last->buf = last->buf_alloc;
233 strbuf_addf(&exp, "application/x-%s-advertisement", service);
234 if (maybe_smart &&
235 (5 <= last->len && last->buf[4] == '#') &&
236 !strbuf_cmp(&exp, &type)) {
237 char *line;
240 * smart HTTP response; validate that the service
241 * pkt-line matches our request.
243 line = packet_read_line_buf(&last->buf, &last->len, NULL);
245 strbuf_reset(&exp);
246 strbuf_addf(&exp, "# service=%s", service);
247 if (strcmp(line, exp.buf))
248 die("invalid server response; got '%s'", line);
249 strbuf_release(&exp);
251 /* The header can include additional metadata lines, up
252 * until a packet flush marker. Ignore these now, but
253 * in the future we might start to scan them.
255 while (packet_read_line_buf(&last->buf, &last->len, NULL))
258 last->proto_git = 1;
261 if (last->proto_git)
262 last->refs = parse_git_refs(last, for_push);
263 else
264 last->refs = parse_info_refs(last);
266 free(refs_url);
267 strbuf_release(&exp);
268 strbuf_release(&type);
269 strbuf_release(&buffer);
270 last_discovery = last;
271 return last;
274 static struct ref *get_refs(int for_push)
276 struct discovery *heads;
278 if (for_push)
279 heads = discover_refs("git-receive-pack", for_push);
280 else
281 heads = discover_refs("git-upload-pack", for_push);
283 return heads->refs;
286 static void output_refs(struct ref *refs)
288 struct ref *posn;
289 for (posn = refs; posn; posn = posn->next) {
290 if (posn->symref)
291 printf("@%s %s\n", posn->symref, posn->name);
292 else
293 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
295 printf("\n");
296 fflush(stdout);
299 struct rpc_state {
300 const char *service_name;
301 const char **argv;
302 struct strbuf *stdin_preamble;
303 char *service_url;
304 char *hdr_content_type;
305 char *hdr_accept;
306 char *buf;
307 size_t alloc;
308 size_t len;
309 size_t pos;
310 int in;
311 int out;
312 struct strbuf result;
313 unsigned gzip_request : 1;
314 unsigned initial_buffer : 1;
317 static size_t rpc_out(void *ptr, size_t eltsize,
318 size_t nmemb, void *buffer_)
320 size_t max = eltsize * nmemb;
321 struct rpc_state *rpc = buffer_;
322 size_t avail = rpc->len - rpc->pos;
324 if (!avail) {
325 rpc->initial_buffer = 0;
326 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
327 if (!avail)
328 return 0;
329 rpc->pos = 0;
330 rpc->len = avail;
333 if (max < avail)
334 avail = max;
335 memcpy(ptr, rpc->buf + rpc->pos, avail);
336 rpc->pos += avail;
337 return avail;
340 #ifndef NO_CURL_IOCTL
341 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
343 struct rpc_state *rpc = clientp;
345 switch (cmd) {
346 case CURLIOCMD_NOP:
347 return CURLIOE_OK;
349 case CURLIOCMD_RESTARTREAD:
350 if (rpc->initial_buffer) {
351 rpc->pos = 0;
352 return CURLIOE_OK;
354 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
355 return CURLIOE_FAILRESTART;
357 default:
358 return CURLIOE_UNKNOWNCMD;
361 #endif
363 static size_t rpc_in(char *ptr, size_t eltsize,
364 size_t nmemb, void *buffer_)
366 size_t size = eltsize * nmemb;
367 struct rpc_state *rpc = buffer_;
368 write_or_die(rpc->in, ptr, size);
369 return size;
372 static int run_slot(struct active_request_slot *slot)
374 int err;
375 struct slot_results results;
377 slot->results = &results;
378 slot->curl_result = curl_easy_perform(slot->curl);
379 finish_active_slot(slot);
381 err = handle_curl_result(&results);
382 if (err != HTTP_OK && err != HTTP_REAUTH) {
383 error("RPC failed; result=%d, HTTP code = %ld",
384 results.curl_result, results.http_code);
387 return err;
390 static int probe_rpc(struct rpc_state *rpc)
392 struct active_request_slot *slot;
393 struct curl_slist *headers = NULL;
394 struct strbuf buf = STRBUF_INIT;
395 int err;
397 slot = get_active_slot();
399 headers = curl_slist_append(headers, rpc->hdr_content_type);
400 headers = curl_slist_append(headers, rpc->hdr_accept);
402 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
403 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
404 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
405 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
406 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
407 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
408 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
409 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
410 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
412 err = run_slot(slot);
414 curl_slist_free_all(headers);
415 strbuf_release(&buf);
416 return err;
419 static int post_rpc(struct rpc_state *rpc)
421 struct active_request_slot *slot;
422 struct curl_slist *headers = NULL;
423 int use_gzip = rpc->gzip_request;
424 char *gzip_body = NULL;
425 size_t gzip_size = 0;
426 int err, large_request = 0;
428 /* Try to load the entire request, if we can fit it into the
429 * allocated buffer space we can use HTTP/1.0 and avoid the
430 * chunked encoding mess.
432 while (1) {
433 size_t left = rpc->alloc - rpc->len;
434 char *buf = rpc->buf + rpc->len;
435 int n;
437 if (left < LARGE_PACKET_MAX) {
438 large_request = 1;
439 use_gzip = 0;
440 break;
443 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
444 if (!n)
445 break;
446 rpc->len += n;
449 if (large_request) {
450 do {
451 err = probe_rpc(rpc);
452 } while (err == HTTP_REAUTH);
453 if (err != HTTP_OK)
454 return -1;
457 headers = curl_slist_append(headers, rpc->hdr_content_type);
458 headers = curl_slist_append(headers, rpc->hdr_accept);
459 headers = curl_slist_append(headers, "Expect:");
461 retry:
462 slot = get_active_slot();
464 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
465 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
466 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
467 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
469 if (large_request) {
470 /* The request body is large and the size cannot be predicted.
471 * We must use chunked encoding to send it.
473 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
474 rpc->initial_buffer = 1;
475 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
476 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
477 #ifndef NO_CURL_IOCTL
478 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
479 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
480 #endif
481 if (options.verbosity > 1) {
482 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
483 fflush(stderr);
486 } else if (gzip_body) {
488 * If we are looping to retry authentication, then the previous
489 * run will have set up the headers and gzip buffer already,
490 * and we just need to send it.
492 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
493 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
495 } else if (use_gzip && 1024 < rpc->len) {
496 /* The client backend isn't giving us compressed data so
497 * we can try to deflate it ourselves, this may save on.
498 * the transfer time.
500 git_zstream stream;
501 int ret;
503 memset(&stream, 0, sizeof(stream));
504 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
505 gzip_size = git_deflate_bound(&stream, rpc->len);
506 gzip_body = xmalloc(gzip_size);
508 stream.next_in = (unsigned char *)rpc->buf;
509 stream.avail_in = rpc->len;
510 stream.next_out = (unsigned char *)gzip_body;
511 stream.avail_out = gzip_size;
513 ret = git_deflate(&stream, Z_FINISH);
514 if (ret != Z_STREAM_END)
515 die("cannot deflate request; zlib deflate error %d", ret);
517 ret = git_deflate_end_gently(&stream);
518 if (ret != Z_OK)
519 die("cannot deflate request; zlib end error %d", ret);
521 gzip_size = stream.total_out;
523 headers = curl_slist_append(headers, "Content-Encoding: gzip");
524 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
525 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
527 if (options.verbosity > 1) {
528 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
529 rpc->service_name,
530 (unsigned long)rpc->len, (unsigned long)gzip_size);
531 fflush(stderr);
533 } else {
534 /* We know the complete request size in advance, use the
535 * more normal Content-Length approach.
537 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
538 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
539 if (options.verbosity > 1) {
540 fprintf(stderr, "POST %s (%lu bytes)\n",
541 rpc->service_name, (unsigned long)rpc->len);
542 fflush(stderr);
546 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
547 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
548 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
550 err = run_slot(slot);
551 if (err == HTTP_REAUTH && !large_request)
552 goto retry;
553 if (err != HTTP_OK)
554 err = -1;
556 curl_slist_free_all(headers);
557 free(gzip_body);
558 return err;
561 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
563 const char *svc = rpc->service_name;
564 struct strbuf buf = STRBUF_INIT;
565 struct strbuf *preamble = rpc->stdin_preamble;
566 struct child_process client;
567 int err = 0;
569 memset(&client, 0, sizeof(client));
570 client.in = -1;
571 client.out = -1;
572 client.git_cmd = 1;
573 client.argv = rpc->argv;
574 if (start_command(&client))
575 exit(1);
576 if (preamble)
577 write_or_die(client.in, preamble->buf, preamble->len);
578 if (heads)
579 write_or_die(client.in, heads->buf, heads->len);
581 rpc->alloc = http_post_buffer;
582 rpc->buf = xmalloc(rpc->alloc);
583 rpc->in = client.in;
584 rpc->out = client.out;
585 strbuf_init(&rpc->result, 0);
587 strbuf_addf(&buf, "%s%s", url, svc);
588 rpc->service_url = strbuf_detach(&buf, NULL);
590 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
591 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
593 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
594 rpc->hdr_accept = strbuf_detach(&buf, NULL);
596 while (!err) {
597 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
598 if (!n)
599 break;
600 rpc->pos = 0;
601 rpc->len = n;
602 err |= post_rpc(rpc);
605 close(client.in);
606 client.in = -1;
607 if (!err) {
608 strbuf_read(&rpc->result, client.out, 0);
609 } else {
610 char buf[4096];
611 for (;;)
612 if (xread(client.out, buf, sizeof(buf)) <= 0)
613 break;
616 close(client.out);
617 client.out = -1;
619 err |= finish_command(&client);
620 free(rpc->service_url);
621 free(rpc->hdr_content_type);
622 free(rpc->hdr_accept);
623 free(rpc->buf);
624 strbuf_release(&buf);
625 return err;
628 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
630 struct walker *walker;
631 char **targets = xmalloc(nr_heads * sizeof(char*));
632 int ret, i;
634 if (options.depth)
635 die("dumb http transport does not support --depth");
636 for (i = 0; i < nr_heads; i++)
637 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
639 walker = get_http_walker(url);
640 walker->get_all = 1;
641 walker->get_tree = 1;
642 walker->get_history = 1;
643 walker->get_verbosely = options.verbosity >= 3;
644 walker->get_recover = 0;
645 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
646 walker_free(walker);
648 for (i = 0; i < nr_heads; i++)
649 free(targets[i]);
650 free(targets);
652 return ret ? error("Fetch failed.") : 0;
655 static int fetch_git(struct discovery *heads,
656 int nr_heads, struct ref **to_fetch)
658 struct rpc_state rpc;
659 struct strbuf preamble = STRBUF_INIT;
660 char *depth_arg = NULL;
661 int argc = 0, i, err;
662 const char *argv[15];
664 argv[argc++] = "fetch-pack";
665 argv[argc++] = "--stateless-rpc";
666 argv[argc++] = "--stdin";
667 argv[argc++] = "--lock-pack";
668 if (options.followtags)
669 argv[argc++] = "--include-tag";
670 if (options.thin)
671 argv[argc++] = "--thin";
672 if (options.verbosity >= 3) {
673 argv[argc++] = "-v";
674 argv[argc++] = "-v";
676 if (!options.progress)
677 argv[argc++] = "--no-progress";
678 if (options.depth) {
679 struct strbuf buf = STRBUF_INIT;
680 strbuf_addf(&buf, "--depth=%lu", options.depth);
681 depth_arg = strbuf_detach(&buf, NULL);
682 argv[argc++] = depth_arg;
684 argv[argc++] = url;
685 argv[argc++] = NULL;
687 for (i = 0; i < nr_heads; i++) {
688 struct ref *ref = to_fetch[i];
689 if (!ref->name || !*ref->name)
690 die("cannot fetch by sha1 over smart http");
691 packet_buf_write(&preamble, "%s\n", ref->name);
693 packet_buf_flush(&preamble);
695 memset(&rpc, 0, sizeof(rpc));
696 rpc.service_name = "git-upload-pack",
697 rpc.argv = argv;
698 rpc.stdin_preamble = &preamble;
699 rpc.gzip_request = 1;
701 err = rpc_service(&rpc, heads);
702 if (rpc.result.len)
703 write_or_die(1, rpc.result.buf, rpc.result.len);
704 strbuf_release(&rpc.result);
705 strbuf_release(&preamble);
706 free(depth_arg);
707 return err;
710 static int fetch(int nr_heads, struct ref **to_fetch)
712 struct discovery *d = discover_refs("git-upload-pack", 0);
713 if (d->proto_git)
714 return fetch_git(d, nr_heads, to_fetch);
715 else
716 return fetch_dumb(nr_heads, to_fetch);
719 static void parse_fetch(struct strbuf *buf)
721 struct ref **to_fetch = NULL;
722 struct ref *list_head = NULL;
723 struct ref **list = &list_head;
724 int alloc_heads = 0, nr_heads = 0;
726 do {
727 if (!prefixcmp(buf->buf, "fetch ")) {
728 char *p = buf->buf + strlen("fetch ");
729 char *name;
730 struct ref *ref;
731 unsigned char old_sha1[20];
733 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
734 die("protocol error: expected sha/ref, got %s'", p);
735 if (p[40] == ' ')
736 name = p + 41;
737 else if (!p[40])
738 name = "";
739 else
740 die("protocol error: expected sha/ref, got %s'", p);
742 ref = alloc_ref(name);
743 hashcpy(ref->old_sha1, old_sha1);
745 *list = ref;
746 list = &ref->next;
748 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
749 to_fetch[nr_heads++] = ref;
751 else
752 die("http transport does not support %s", buf->buf);
754 strbuf_reset(buf);
755 if (strbuf_getline(buf, stdin, '\n') == EOF)
756 return;
757 if (!*buf->buf)
758 break;
759 } while (1);
761 if (fetch(nr_heads, to_fetch))
762 exit(128); /* error already reported */
763 free_refs(list_head);
764 free(to_fetch);
766 printf("\n");
767 fflush(stdout);
768 strbuf_reset(buf);
771 static int push_dav(int nr_spec, char **specs)
773 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
774 int argc = 0, i;
776 argv[argc++] = "http-push";
777 argv[argc++] = "--helper-status";
778 if (options.dry_run)
779 argv[argc++] = "--dry-run";
780 if (options.verbosity > 1)
781 argv[argc++] = "--verbose";
782 argv[argc++] = url;
783 for (i = 0; i < nr_spec; i++)
784 argv[argc++] = specs[i];
785 argv[argc++] = NULL;
787 if (run_command_v_opt(argv, RUN_GIT_CMD))
788 die("git-%s failed", argv[0]);
789 free(argv);
790 return 0;
793 static int push_git(struct discovery *heads, int nr_spec, char **specs)
795 struct rpc_state rpc;
796 int i, err;
797 struct argv_array args;
799 argv_array_init(&args);
800 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
801 NULL);
803 if (options.thin)
804 argv_array_push(&args, "--thin");
805 if (options.dry_run)
806 argv_array_push(&args, "--dry-run");
807 if (options.verbosity == 0)
808 argv_array_push(&args, "--quiet");
809 else if (options.verbosity > 1)
810 argv_array_push(&args, "--verbose");
811 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
812 argv_array_push(&args, url);
813 for (i = 0; i < nr_spec; i++)
814 argv_array_push(&args, specs[i]);
816 memset(&rpc, 0, sizeof(rpc));
817 rpc.service_name = "git-receive-pack",
818 rpc.argv = args.argv;
820 err = rpc_service(&rpc, heads);
821 if (rpc.result.len)
822 write_or_die(1, rpc.result.buf, rpc.result.len);
823 strbuf_release(&rpc.result);
824 argv_array_clear(&args);
825 return err;
828 static int push(int nr_spec, char **specs)
830 struct discovery *heads = discover_refs("git-receive-pack", 1);
831 int ret;
833 if (heads->proto_git)
834 ret = push_git(heads, nr_spec, specs);
835 else
836 ret = push_dav(nr_spec, specs);
837 free_discovery(heads);
838 return ret;
841 static void parse_push(struct strbuf *buf)
843 char **specs = NULL;
844 int alloc_spec = 0, nr_spec = 0, i, ret;
846 do {
847 if (!prefixcmp(buf->buf, "push ")) {
848 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
849 specs[nr_spec++] = xstrdup(buf->buf + 5);
851 else
852 die("http transport does not support %s", buf->buf);
854 strbuf_reset(buf);
855 if (strbuf_getline(buf, stdin, '\n') == EOF)
856 goto free_specs;
857 if (!*buf->buf)
858 break;
859 } while (1);
861 ret = push(nr_spec, specs);
862 printf("\n");
863 fflush(stdout);
865 if (ret)
866 exit(128); /* error already reported */
868 free_specs:
869 for (i = 0; i < nr_spec; i++)
870 free(specs[i]);
871 free(specs);
874 int main(int argc, const char **argv)
876 struct strbuf buf = STRBUF_INIT;
877 int nongit;
879 git_extract_argv0_path(argv[0]);
880 setup_git_directory_gently(&nongit);
881 if (argc < 2) {
882 fprintf(stderr, "Remote needed\n");
883 return 1;
886 options.verbosity = 1;
887 options.progress = !!isatty(2);
888 options.thin = 1;
890 remote = remote_get(argv[1]);
892 if (argc > 2) {
893 end_url_with_slash(&buf, argv[2]);
894 } else {
895 end_url_with_slash(&buf, remote->url[0]);
898 url = strbuf_detach(&buf, NULL);
900 http_init(remote, url, 0);
902 do {
903 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
904 if (ferror(stdin))
905 fprintf(stderr, "Error reading command stream\n");
906 else
907 fprintf(stderr, "Unexpected end of command stream\n");
908 return 1;
910 if (buf.len == 0)
911 break;
912 if (!prefixcmp(buf.buf, "fetch ")) {
913 if (nongit)
914 die("Fetch attempted without a local repo");
915 parse_fetch(&buf);
917 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
918 int for_push = !!strstr(buf.buf + 4, "for-push");
919 output_refs(get_refs(for_push));
921 } else if (!prefixcmp(buf.buf, "push ")) {
922 parse_push(&buf);
924 } else if (!prefixcmp(buf.buf, "option ")) {
925 char *name = buf.buf + strlen("option ");
926 char *value = strchr(name, ' ');
927 int result;
929 if (value)
930 *value++ = '\0';
931 else
932 value = "true";
934 result = set_option(name, value);
935 if (!result)
936 printf("ok\n");
937 else if (result < 0)
938 printf("error invalid value\n");
939 else
940 printf("unsupported\n");
941 fflush(stdout);
943 } else if (!strcmp(buf.buf, "capabilities")) {
944 printf("fetch\n");
945 printf("option\n");
946 printf("push\n");
947 printf("\n");
948 fflush(stdout);
949 } else {
950 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
951 return 1;
953 strbuf_reset(&buf);
954 } while (1);
956 http_cleanup();
958 return 0;