Merge branch 'mk/old-expat'
[git/gitweb.git] / remote-curl.c
blob933c69ac2627c1903276b92b457843e12649f39e
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; /* always ends with a trailing slash */
14 struct options {
15 int verbosity;
16 unsigned long depth;
17 unsigned progress : 1,
18 followtags : 1,
19 dry_run : 1,
20 thin : 1;
22 static struct options options;
24 static int set_option(const char *name, const char *value)
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;
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;
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;
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;
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;
69 else {
70 return 1 /* unsupported */;
74 struct discovery {
75 const char *service;
76 char *buf_alloc;
77 char *buf;
78 size_t len;
79 unsigned proto_git : 1;
81 static struct discovery *last_discovery;
83 static void free_discovery(struct discovery *d)
85 if (d) {
86 if (d == last_discovery)
87 last_discovery = NULL;
88 free(d->buf_alloc);
89 free(d);
93 static struct discovery* discover_refs(const char *service)
95 struct strbuf exp = STRBUF_INIT;
96 struct strbuf type = STRBUF_INIT;
97 struct strbuf buffer = STRBUF_INIT;
98 struct discovery *last = last_discovery;
99 char *refs_url;
100 int http_ret, maybe_smart = 0;
102 if (last && !strcmp(service, last->service))
103 return last;
104 free_discovery(last);
106 strbuf_addf(&buffer, "%sinfo/refs", url);
107 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
108 git_env_bool("GIT_SMART_HTTP", 1)) {
109 maybe_smart = 1;
110 if (!strchr(url, '?'))
111 strbuf_addch(&buffer, '?');
112 else
113 strbuf_addch(&buffer, '&');
114 strbuf_addf(&buffer, "service=%s", service);
116 refs_url = strbuf_detach(&buffer, NULL);
118 http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
119 switch (http_ret) {
120 case HTTP_OK:
121 break;
122 case HTTP_MISSING_TARGET:
123 die("%s not found: did you run git update-server-info on the"
124 " server?", refs_url);
125 case HTTP_NOAUTH:
126 die("Authentication failed");
127 default:
128 http_error(refs_url, http_ret);
129 die("HTTP request failed");
132 last= xcalloc(1, sizeof(*last_discovery));
133 last->service = service;
134 last->buf_alloc = strbuf_detach(&buffer, &last->len);
135 last->buf = last->buf_alloc;
137 strbuf_addf(&exp, "application/x-%s-advertisement", service);
138 if (maybe_smart &&
139 (5 <= last->len && last->buf[4] == '#') &&
140 !strbuf_cmp(&exp, &type)) {
142 * smart HTTP response; validate that the service
143 * pkt-line matches our request.
145 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
146 die("%s has invalid packet header", refs_url);
147 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
148 strbuf_setlen(&buffer, buffer.len - 1);
150 strbuf_reset(&exp);
151 strbuf_addf(&exp, "# service=%s", service);
152 if (strbuf_cmp(&exp, &buffer))
153 die("invalid server response; got '%s'", buffer.buf);
154 strbuf_release(&exp);
156 /* The header can include additional metadata lines, up
157 * until a packet flush marker. Ignore these now, but
158 * in the future we might start to scan them.
160 strbuf_reset(&buffer);
161 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
162 strbuf_reset(&buffer);
164 last->proto_git = 1;
167 free(refs_url);
168 strbuf_release(&exp);
169 strbuf_release(&type);
170 strbuf_release(&buffer);
171 last_discovery = last;
172 return last;
175 static int write_discovery(int in, int out, void *data)
177 struct discovery *heads = data;
178 int err = 0;
179 if (write_in_full(out, heads->buf, heads->len) != heads->len)
180 err = 1;
181 close(out);
182 return err;
185 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
187 struct ref *list = NULL;
188 struct async async;
190 memset(&async, 0, sizeof(async));
191 async.proc = write_discovery;
192 async.data = heads;
193 async.out = -1;
195 if (start_async(&async))
196 die("cannot start thread to parse advertised refs");
197 get_remote_heads(async.out, &list,
198 for_push ? REF_NORMAL : 0, NULL);
199 close(async.out);
200 if (finish_async(&async))
201 die("ref parsing thread failed");
202 return list;
205 static struct ref *parse_info_refs(struct discovery *heads)
207 char *data, *start, *mid;
208 char *ref_name;
209 int i = 0;
211 struct ref *refs = NULL;
212 struct ref *ref = NULL;
213 struct ref *last_ref = NULL;
215 data = heads->buf;
216 start = NULL;
217 mid = data;
218 while (i < heads->len) {
219 if (!start) {
220 start = &data[i];
222 if (data[i] == '\t')
223 mid = &data[i];
224 if (data[i] == '\n') {
225 if (mid - start != 40)
226 die("%sinfo/refs not valid: is this a git repository?", url);
227 data[i] = 0;
228 ref_name = mid + 1;
229 ref = xmalloc(sizeof(struct ref) +
230 strlen(ref_name) + 1);
231 memset(ref, 0, sizeof(struct ref));
232 strcpy(ref->name, ref_name);
233 get_sha1_hex(start, ref->old_sha1);
234 if (!refs)
235 refs = ref;
236 if (last_ref)
237 last_ref->next = ref;
238 last_ref = ref;
239 start = NULL;
241 i++;
244 ref = alloc_ref("HEAD");
245 if (!http_fetch_ref(url, ref) &&
246 !resolve_remote_symref(ref, refs)) {
247 ref->next = refs;
248 refs = ref;
249 } else {
250 free(ref);
253 return refs;
256 static struct ref *get_refs(int for_push)
258 struct discovery *heads;
260 if (for_push)
261 heads = discover_refs("git-receive-pack");
262 else
263 heads = discover_refs("git-upload-pack");
265 if (heads->proto_git)
266 return parse_git_refs(heads, for_push);
267 return parse_info_refs(heads);
270 static void output_refs(struct ref *refs)
272 struct ref *posn;
273 for (posn = refs; posn; posn = posn->next) {
274 if (posn->symref)
275 printf("@%s %s\n", posn->symref, posn->name);
276 else
277 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
279 printf("\n");
280 fflush(stdout);
281 free_refs(refs);
284 struct rpc_state {
285 const char *service_name;
286 const char **argv;
287 struct strbuf *stdin_preamble;
288 char *service_url;
289 char *hdr_content_type;
290 char *hdr_accept;
291 char *buf;
292 size_t alloc;
293 size_t len;
294 size_t pos;
295 int in;
296 int out;
297 struct strbuf result;
298 unsigned gzip_request : 1;
299 unsigned initial_buffer : 1;
302 static size_t rpc_out(void *ptr, size_t eltsize,
303 size_t nmemb, void *buffer_)
305 size_t max = eltsize * nmemb;
306 struct rpc_state *rpc = buffer_;
307 size_t avail = rpc->len - rpc->pos;
309 if (!avail) {
310 rpc->initial_buffer = 0;
311 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
312 if (!avail)
313 return 0;
314 rpc->pos = 0;
315 rpc->len = avail;
318 if (max < avail)
319 avail = max;
320 memcpy(ptr, rpc->buf + rpc->pos, avail);
321 rpc->pos += avail;
322 return avail;
325 #ifndef NO_CURL_IOCTL
326 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
328 struct rpc_state *rpc = clientp;
330 switch (cmd) {
331 case CURLIOCMD_NOP:
332 return CURLIOE_OK;
334 case CURLIOCMD_RESTARTREAD:
335 if (rpc->initial_buffer) {
336 rpc->pos = 0;
337 return CURLIOE_OK;
339 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
340 return CURLIOE_FAILRESTART;
342 default:
343 return CURLIOE_UNKNOWNCMD;
346 #endif
348 static size_t rpc_in(char *ptr, size_t eltsize,
349 size_t nmemb, void *buffer_)
351 size_t size = eltsize * nmemb;
352 struct rpc_state *rpc = buffer_;
353 write_or_die(rpc->in, ptr, size);
354 return size;
357 static int run_slot(struct active_request_slot *slot)
359 int err;
360 struct slot_results results;
362 slot->results = &results;
363 slot->curl_result = curl_easy_perform(slot->curl);
364 finish_active_slot(slot);
366 err = handle_curl_result(&results);
367 if (err != HTTP_OK && err != HTTP_REAUTH) {
368 error("RPC failed; result=%d, HTTP code = %ld",
369 results.curl_result, results.http_code);
372 return err;
375 static int probe_rpc(struct rpc_state *rpc)
377 struct active_request_slot *slot;
378 struct curl_slist *headers = NULL;
379 struct strbuf buf = STRBUF_INIT;
380 int err;
382 slot = get_active_slot();
384 headers = curl_slist_append(headers, rpc->hdr_content_type);
385 headers = curl_slist_append(headers, rpc->hdr_accept);
387 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
388 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
389 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
390 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
391 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
392 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
393 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
394 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
395 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
397 err = run_slot(slot);
399 curl_slist_free_all(headers);
400 strbuf_release(&buf);
401 return err;
404 static int post_rpc(struct rpc_state *rpc)
406 struct active_request_slot *slot;
407 struct curl_slist *headers = NULL;
408 int use_gzip = rpc->gzip_request;
409 char *gzip_body = NULL;
410 size_t gzip_size = 0;
411 int err, large_request = 0;
413 /* Try to load the entire request, if we can fit it into the
414 * allocated buffer space we can use HTTP/1.0 and avoid the
415 * chunked encoding mess.
417 while (1) {
418 size_t left = rpc->alloc - rpc->len;
419 char *buf = rpc->buf + rpc->len;
420 int n;
422 if (left < LARGE_PACKET_MAX) {
423 large_request = 1;
424 use_gzip = 0;
425 break;
428 n = packet_read_line(rpc->out, buf, left);
429 if (!n)
430 break;
431 rpc->len += n;
434 if (large_request) {
435 do {
436 err = probe_rpc(rpc);
437 } while (err == HTTP_REAUTH);
438 if (err != HTTP_OK)
439 return -1;
442 headers = curl_slist_append(headers, rpc->hdr_content_type);
443 headers = curl_slist_append(headers, rpc->hdr_accept);
444 headers = curl_slist_append(headers, "Expect:");
446 retry:
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, "gzip");
454 if (large_request) {
455 /* The request body is large and the size cannot be predicted.
456 * We must use chunked encoding to send it.
458 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
459 rpc->initial_buffer = 1;
460 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
461 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
462 #ifndef NO_CURL_IOCTL
463 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
464 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
465 #endif
466 if (options.verbosity > 1) {
467 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
468 fflush(stderr);
471 } else if (gzip_body) {
473 * If we are looping to retry authentication, then the previous
474 * run will have set up the headers and gzip buffer already,
475 * and we just need to send it.
477 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
478 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
480 } else if (use_gzip && 1024 < rpc->len) {
481 /* The client backend isn't giving us compressed data so
482 * we can try to deflate it ourselves, this may save on.
483 * the transfer time.
485 git_zstream stream;
486 int ret;
488 memset(&stream, 0, sizeof(stream));
489 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
490 gzip_size = git_deflate_bound(&stream, rpc->len);
491 gzip_body = xmalloc(gzip_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 = gzip_size;
498 ret = git_deflate(&stream, Z_FINISH);
499 if (ret != Z_STREAM_END)
500 die("cannot deflate request; zlib deflate error %d", ret);
502 ret = git_deflate_end_gently(&stream);
503 if (ret != Z_OK)
504 die("cannot deflate request; zlib end error %d", ret);
506 gzip_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, gzip_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)gzip_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);
536 if (err == HTTP_REAUTH && !large_request)
537 goto retry;
538 if (err != HTTP_OK)
539 err = -1;
541 curl_slist_free_all(headers);
542 free(gzip_body);
543 return err;
546 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
548 const char *svc = rpc->service_name;
549 struct strbuf buf = STRBUF_INIT;
550 struct strbuf *preamble = rpc->stdin_preamble;
551 struct child_process client;
552 int err = 0;
554 memset(&client, 0, sizeof(client));
555 client.in = -1;
556 client.out = -1;
557 client.git_cmd = 1;
558 client.argv = rpc->argv;
559 if (start_command(&client))
560 exit(1);
561 if (preamble)
562 write_or_die(client.in, preamble->buf, preamble->len);
563 if (heads)
564 write_or_die(client.in, heads->buf, heads->len);
566 rpc->alloc = http_post_buffer;
567 rpc->buf = xmalloc(rpc->alloc);
568 rpc->in = client.in;
569 rpc->out = client.out;
570 strbuf_init(&rpc->result, 0);
572 strbuf_addf(&buf, "%s%s", url, svc);
573 rpc->service_url = strbuf_detach(&buf, NULL);
575 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
576 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
578 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
579 rpc->hdr_accept = strbuf_detach(&buf, NULL);
581 while (!err) {
582 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
583 if (!n)
584 break;
585 rpc->pos = 0;
586 rpc->len = n;
587 err |= post_rpc(rpc);
590 close(client.in);
591 client.in = -1;
592 if (!err) {
593 strbuf_read(&rpc->result, client.out, 0);
594 } else {
595 char buf[4096];
596 for (;;)
597 if (xread(client.out, buf, sizeof(buf)) <= 0)
598 break;
601 close(client.out);
602 client.out = -1;
604 err |= finish_command(&client);
605 free(rpc->service_url);
606 free(rpc->hdr_content_type);
607 free(rpc->hdr_accept);
608 free(rpc->buf);
609 strbuf_release(&buf);
610 return err;
613 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
615 struct walker *walker;
616 char **targets = xmalloc(nr_heads * sizeof(char*));
617 int ret, i;
619 if (options.depth)
620 die("dumb http transport does not support --depth");
621 for (i = 0; i < nr_heads; i++)
622 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
624 walker = get_http_walker(url);
625 walker->get_all = 1;
626 walker->get_tree = 1;
627 walker->get_history = 1;
628 walker->get_verbosely = options.verbosity >= 3;
629 walker->get_recover = 0;
630 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
631 walker_free(walker);
633 for (i = 0; i < nr_heads; i++)
634 free(targets[i]);
635 free(targets);
637 return ret ? error("Fetch failed.") : 0;
640 static int fetch_git(struct discovery *heads,
641 int nr_heads, struct ref **to_fetch)
643 struct rpc_state rpc;
644 struct strbuf preamble = STRBUF_INIT;
645 char *depth_arg = NULL;
646 int argc = 0, i, err;
647 const char *argv[15];
649 argv[argc++] = "fetch-pack";
650 argv[argc++] = "--stateless-rpc";
651 argv[argc++] = "--stdin";
652 argv[argc++] = "--lock-pack";
653 if (options.followtags)
654 argv[argc++] = "--include-tag";
655 if (options.thin)
656 argv[argc++] = "--thin";
657 if (options.verbosity >= 3) {
658 argv[argc++] = "-v";
659 argv[argc++] = "-v";
661 if (!options.progress)
662 argv[argc++] = "--no-progress";
663 if (options.depth) {
664 struct strbuf buf = STRBUF_INIT;
665 strbuf_addf(&buf, "--depth=%lu", options.depth);
666 depth_arg = strbuf_detach(&buf, NULL);
667 argv[argc++] = depth_arg;
669 argv[argc++] = url;
670 argv[argc++] = NULL;
672 for (i = 0; i < nr_heads; i++) {
673 struct ref *ref = to_fetch[i];
674 if (!ref->name || !*ref->name)
675 die("cannot fetch by sha1 over smart http");
676 packet_buf_write(&preamble, "%s\n", ref->name);
678 packet_buf_flush(&preamble);
680 memset(&rpc, 0, sizeof(rpc));
681 rpc.service_name = "git-upload-pack",
682 rpc.argv = argv;
683 rpc.stdin_preamble = &preamble;
684 rpc.gzip_request = 1;
686 err = rpc_service(&rpc, heads);
687 if (rpc.result.len)
688 safe_write(1, rpc.result.buf, rpc.result.len);
689 strbuf_release(&rpc.result);
690 strbuf_release(&preamble);
691 free(depth_arg);
692 return err;
695 static int fetch(int nr_heads, struct ref **to_fetch)
697 struct discovery *d = discover_refs("git-upload-pack");
698 if (d->proto_git)
699 return fetch_git(d, nr_heads, to_fetch);
700 else
701 return fetch_dumb(nr_heads, to_fetch);
704 static void parse_fetch(struct strbuf *buf)
706 struct ref **to_fetch = NULL;
707 struct ref *list_head = NULL;
708 struct ref **list = &list_head;
709 int alloc_heads = 0, nr_heads = 0;
711 do {
712 if (!prefixcmp(buf->buf, "fetch ")) {
713 char *p = buf->buf + strlen("fetch ");
714 char *name;
715 struct ref *ref;
716 unsigned char old_sha1[20];
718 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
719 die("protocol error: expected sha/ref, got %s'", p);
720 if (p[40] == ' ')
721 name = p + 41;
722 else if (!p[40])
723 name = "";
724 else
725 die("protocol error: expected sha/ref, got %s'", p);
727 ref = alloc_ref(name);
728 hashcpy(ref->old_sha1, old_sha1);
730 *list = ref;
731 list = &ref->next;
733 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
734 to_fetch[nr_heads++] = ref;
736 else
737 die("http transport does not support %s", buf->buf);
739 strbuf_reset(buf);
740 if (strbuf_getline(buf, stdin, '\n') == EOF)
741 return;
742 if (!*buf->buf)
743 break;
744 } while (1);
746 if (fetch(nr_heads, to_fetch))
747 exit(128); /* error already reported */
748 free_refs(list_head);
749 free(to_fetch);
751 printf("\n");
752 fflush(stdout);
753 strbuf_reset(buf);
756 static int push_dav(int nr_spec, char **specs)
758 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
759 int argc = 0, i;
761 argv[argc++] = "http-push";
762 argv[argc++] = "--helper-status";
763 if (options.dry_run)
764 argv[argc++] = "--dry-run";
765 if (options.verbosity > 1)
766 argv[argc++] = "--verbose";
767 argv[argc++] = url;
768 for (i = 0; i < nr_spec; i++)
769 argv[argc++] = specs[i];
770 argv[argc++] = NULL;
772 if (run_command_v_opt(argv, RUN_GIT_CMD))
773 die("git-%s failed", argv[0]);
774 free(argv);
775 return 0;
778 static int push_git(struct discovery *heads, int nr_spec, char **specs)
780 struct rpc_state rpc;
781 const char **argv;
782 int argc = 0, i, err;
784 argv = xmalloc((10 + nr_spec) * sizeof(char*));
785 argv[argc++] = "send-pack";
786 argv[argc++] = "--stateless-rpc";
787 argv[argc++] = "--helper-status";
788 if (options.thin)
789 argv[argc++] = "--thin";
790 if (options.dry_run)
791 argv[argc++] = "--dry-run";
792 if (options.verbosity == 0)
793 argv[argc++] = "--quiet";
794 else if (options.verbosity > 1)
795 argv[argc++] = "--verbose";
796 argv[argc++] = options.progress ? "--progress" : "--no-progress";
797 argv[argc++] = url;
798 for (i = 0; i < nr_spec; i++)
799 argv[argc++] = specs[i];
800 argv[argc++] = NULL;
802 memset(&rpc, 0, sizeof(rpc));
803 rpc.service_name = "git-receive-pack",
804 rpc.argv = argv;
806 err = rpc_service(&rpc, heads);
807 if (rpc.result.len)
808 safe_write(1, rpc.result.buf, rpc.result.len);
809 strbuf_release(&rpc.result);
810 free(argv);
811 return err;
814 static int push(int nr_spec, char **specs)
816 struct discovery *heads = discover_refs("git-receive-pack");
817 int ret;
819 if (heads->proto_git)
820 ret = push_git(heads, nr_spec, specs);
821 else
822 ret = push_dav(nr_spec, specs);
823 free_discovery(heads);
824 return ret;
827 static void parse_push(struct strbuf *buf)
829 char **specs = NULL;
830 int alloc_spec = 0, nr_spec = 0, i, ret;
832 do {
833 if (!prefixcmp(buf->buf, "push ")) {
834 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
835 specs[nr_spec++] = xstrdup(buf->buf + 5);
837 else
838 die("http transport does not support %s", buf->buf);
840 strbuf_reset(buf);
841 if (strbuf_getline(buf, stdin, '\n') == EOF)
842 goto free_specs;
843 if (!*buf->buf)
844 break;
845 } while (1);
847 ret = push(nr_spec, specs);
848 printf("\n");
849 fflush(stdout);
851 if (ret)
852 exit(128); /* error already reported */
854 free_specs:
855 for (i = 0; i < nr_spec; i++)
856 free(specs[i]);
857 free(specs);
860 int main(int argc, const char **argv)
862 struct strbuf buf = STRBUF_INIT;
863 int nongit;
865 git_extract_argv0_path(argv[0]);
866 setup_git_directory_gently(&nongit);
867 if (argc < 2) {
868 fprintf(stderr, "Remote needed\n");
869 return 1;
872 options.verbosity = 1;
873 options.progress = !!isatty(2);
874 options.thin = 1;
876 remote = remote_get(argv[1]);
878 if (argc > 2) {
879 end_url_with_slash(&buf, argv[2]);
880 } else {
881 end_url_with_slash(&buf, remote->url[0]);
884 url = strbuf_detach(&buf, NULL);
886 http_init(remote, url, 0);
888 do {
889 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
890 if (ferror(stdin))
891 fprintf(stderr, "Error reading command stream\n");
892 else
893 fprintf(stderr, "Unexpected end of command stream\n");
894 return 1;
896 if (buf.len == 0)
897 break;
898 if (!prefixcmp(buf.buf, "fetch ")) {
899 if (nongit)
900 die("Fetch attempted without a local repo");
901 parse_fetch(&buf);
903 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
904 int for_push = !!strstr(buf.buf + 4, "for-push");
905 output_refs(get_refs(for_push));
907 } else if (!prefixcmp(buf.buf, "push ")) {
908 parse_push(&buf);
910 } else if (!prefixcmp(buf.buf, "option ")) {
911 char *name = buf.buf + strlen("option ");
912 char *value = strchr(name, ' ');
913 int result;
915 if (value)
916 *value++ = '\0';
917 else
918 value = "true";
920 result = set_option(name, value);
921 if (!result)
922 printf("ok\n");
923 else if (result < 0)
924 printf("error invalid value\n");
925 else
926 printf("unsupported\n");
927 fflush(stdout);
929 } else if (!strcmp(buf.buf, "capabilities")) {
930 printf("fetch\n");
931 printf("option\n");
932 printf("push\n");
933 printf("\n");
934 fflush(stdout);
935 } else {
936 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
937 return 1;
939 strbuf_reset(&buf);
940 } while (1);
942 http_cleanup();
944 return 0;