t5541: mark passing c-a-s test as success
[alt-git.git] / remote-curl.c
blob53c8a3d1a3f24436b6b650a439d36dfaae6547f5
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 "string-list.h"
10 #include "sideband.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;
24 static struct string_list cas_options = STRING_LIST_INIT_DUP;
26 static int set_option(const char *name, const char *value)
28 if (!strcmp(name, "verbosity")) {
29 char *end;
30 int v = strtol(value, &end, 10);
31 if (value == end || *end)
32 return -1;
33 options.verbosity = v;
34 return 0;
36 else if (!strcmp(name, "progress")) {
37 if (!strcmp(value, "true"))
38 options.progress = 1;
39 else if (!strcmp(value, "false"))
40 options.progress = 0;
41 else
42 return -1;
43 return 0;
45 else if (!strcmp(name, "depth")) {
46 char *end;
47 unsigned long v = strtoul(value, &end, 10);
48 if (value == end || *end)
49 return -1;
50 options.depth = v;
51 return 0;
53 else if (!strcmp(name, "followtags")) {
54 if (!strcmp(value, "true"))
55 options.followtags = 1;
56 else if (!strcmp(value, "false"))
57 options.followtags = 0;
58 else
59 return -1;
60 return 0;
62 else if (!strcmp(name, "dry-run")) {
63 if (!strcmp(value, "true"))
64 options.dry_run = 1;
65 else if (!strcmp(value, "false"))
66 options.dry_run = 0;
67 else
68 return -1;
69 return 0;
71 else if (!strcmp(name, "cas")) {
72 struct strbuf val = STRBUF_INIT;
73 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
74 string_list_append(&cas_options, val.buf);
75 strbuf_release(&val);
76 return 0;
78 else {
79 return 1 /* unsupported */;
83 struct discovery {
84 const char *service;
85 char *buf_alloc;
86 char *buf;
87 size_t len;
88 struct ref *refs;
89 unsigned proto_git : 1;
91 static struct discovery *last_discovery;
93 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
95 struct ref *list = NULL;
96 get_remote_heads(-1, heads->buf, heads->len, &list,
97 for_push ? REF_NORMAL : 0, NULL);
98 return list;
101 static struct ref *parse_info_refs(struct discovery *heads)
103 char *data, *start, *mid;
104 char *ref_name;
105 int i = 0;
107 struct ref *refs = NULL;
108 struct ref *ref = NULL;
109 struct ref *last_ref = NULL;
111 data = heads->buf;
112 start = NULL;
113 mid = data;
114 while (i < heads->len) {
115 if (!start) {
116 start = &data[i];
118 if (data[i] == '\t')
119 mid = &data[i];
120 if (data[i] == '\n') {
121 if (mid - start != 40)
122 die("%sinfo/refs not valid: is this a git repository?", url);
123 data[i] = 0;
124 ref_name = mid + 1;
125 ref = xmalloc(sizeof(struct ref) +
126 strlen(ref_name) + 1);
127 memset(ref, 0, sizeof(struct ref));
128 strcpy(ref->name, ref_name);
129 get_sha1_hex(start, ref->old_sha1);
130 if (!refs)
131 refs = ref;
132 if (last_ref)
133 last_ref->next = ref;
134 last_ref = ref;
135 start = NULL;
137 i++;
140 ref = alloc_ref("HEAD");
141 if (!http_fetch_ref(url, ref) &&
142 !resolve_remote_symref(ref, refs)) {
143 ref->next = refs;
144 refs = ref;
145 } else {
146 free(ref);
149 return refs;
152 static void free_discovery(struct discovery *d)
154 if (d) {
155 if (d == last_discovery)
156 last_discovery = NULL;
157 free(d->buf_alloc);
158 free_refs(d->refs);
159 free(d);
163 static int show_http_message(struct strbuf *type, struct strbuf *msg)
165 const char *p, *eol;
168 * We only show text/plain parts, as other types are likely
169 * to be ugly to look at on the user's terminal.
171 * TODO should handle "; charset=XXX", and re-encode into
172 * logoutputencoding
174 if (strcasecmp(type->buf, "text/plain"))
175 return -1;
177 strbuf_trim(msg);
178 if (!msg->len)
179 return -1;
181 p = msg->buf;
182 do {
183 eol = strchrnul(p, '\n');
184 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
185 p = eol + 1;
186 } while(*eol);
187 return 0;
190 static struct discovery* discover_refs(const char *service, int for_push)
192 struct strbuf exp = STRBUF_INIT;
193 struct strbuf type = STRBUF_INIT;
194 struct strbuf buffer = STRBUF_INIT;
195 struct discovery *last = last_discovery;
196 char *refs_url;
197 int http_ret, maybe_smart = 0;
199 if (last && !strcmp(service, last->service))
200 return last;
201 free_discovery(last);
203 strbuf_addf(&buffer, "%sinfo/refs", url);
204 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
205 git_env_bool("GIT_SMART_HTTP", 1)) {
206 maybe_smart = 1;
207 if (!strchr(url, '?'))
208 strbuf_addch(&buffer, '?');
209 else
210 strbuf_addch(&buffer, '&');
211 strbuf_addf(&buffer, "service=%s", service);
213 refs_url = strbuf_detach(&buffer, NULL);
215 http_ret = http_get_strbuf(refs_url, &type, &buffer,
216 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
217 switch (http_ret) {
218 case HTTP_OK:
219 break;
220 case HTTP_MISSING_TARGET:
221 show_http_message(&type, &buffer);
222 die("repository '%s' not found", url);
223 case HTTP_NOAUTH:
224 show_http_message(&type, &buffer);
225 die("Authentication failed for '%s'", url);
226 default:
227 show_http_message(&type, &buffer);
228 die("unable to access '%s': %s", url, curl_errorstr);
231 last= xcalloc(1, sizeof(*last_discovery));
232 last->service = service;
233 last->buf_alloc = strbuf_detach(&buffer, &last->len);
234 last->buf = last->buf_alloc;
236 strbuf_addf(&exp, "application/x-%s-advertisement", service);
237 if (maybe_smart &&
238 (5 <= last->len && last->buf[4] == '#') &&
239 !strbuf_cmp(&exp, &type)) {
240 char *line;
243 * smart HTTP response; validate that the service
244 * pkt-line matches our request.
246 line = packet_read_line_buf(&last->buf, &last->len, NULL);
248 strbuf_reset(&exp);
249 strbuf_addf(&exp, "# service=%s", service);
250 if (strcmp(line, exp.buf))
251 die("invalid server response; got '%s'", line);
252 strbuf_release(&exp);
254 /* The header can include additional metadata lines, up
255 * until a packet flush marker. Ignore these now, but
256 * in the future we might start to scan them.
258 while (packet_read_line_buf(&last->buf, &last->len, NULL))
261 last->proto_git = 1;
264 if (last->proto_git)
265 last->refs = parse_git_refs(last, for_push);
266 else
267 last->refs = parse_info_refs(last);
269 free(refs_url);
270 strbuf_release(&exp);
271 strbuf_release(&type);
272 strbuf_release(&buffer);
273 last_discovery = last;
274 return last;
277 static struct ref *get_refs(int for_push)
279 struct discovery *heads;
281 if (for_push)
282 heads = discover_refs("git-receive-pack", for_push);
283 else
284 heads = discover_refs("git-upload-pack", for_push);
286 return heads->refs;
289 static void output_refs(struct ref *refs)
291 struct ref *posn;
292 for (posn = refs; posn; posn = posn->next) {
293 if (posn->symref)
294 printf("@%s %s\n", posn->symref, posn->name);
295 else
296 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
298 printf("\n");
299 fflush(stdout);
302 struct rpc_state {
303 const char *service_name;
304 const char **argv;
305 struct strbuf *stdin_preamble;
306 char *service_url;
307 char *hdr_content_type;
308 char *hdr_accept;
309 char *buf;
310 size_t alloc;
311 size_t len;
312 size_t pos;
313 int in;
314 int out;
315 struct strbuf result;
316 unsigned gzip_request : 1;
317 unsigned initial_buffer : 1;
320 static size_t rpc_out(void *ptr, size_t eltsize,
321 size_t nmemb, void *buffer_)
323 size_t max = eltsize * nmemb;
324 struct rpc_state *rpc = buffer_;
325 size_t avail = rpc->len - rpc->pos;
327 if (!avail) {
328 rpc->initial_buffer = 0;
329 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
330 if (!avail)
331 return 0;
332 rpc->pos = 0;
333 rpc->len = avail;
336 if (max < avail)
337 avail = max;
338 memcpy(ptr, rpc->buf + rpc->pos, avail);
339 rpc->pos += avail;
340 return avail;
343 #ifndef NO_CURL_IOCTL
344 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
346 struct rpc_state *rpc = clientp;
348 switch (cmd) {
349 case CURLIOCMD_NOP:
350 return CURLIOE_OK;
352 case CURLIOCMD_RESTARTREAD:
353 if (rpc->initial_buffer) {
354 rpc->pos = 0;
355 return CURLIOE_OK;
357 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
358 return CURLIOE_FAILRESTART;
360 default:
361 return CURLIOE_UNKNOWNCMD;
364 #endif
366 static size_t rpc_in(char *ptr, size_t eltsize,
367 size_t nmemb, void *buffer_)
369 size_t size = eltsize * nmemb;
370 struct rpc_state *rpc = buffer_;
371 write_or_die(rpc->in, ptr, size);
372 return size;
375 static int run_slot(struct active_request_slot *slot)
377 int err;
378 struct slot_results results;
380 slot->results = &results;
381 slot->curl_result = curl_easy_perform(slot->curl);
382 finish_active_slot(slot);
384 err = handle_curl_result(&results);
385 if (err != HTTP_OK && err != HTTP_REAUTH) {
386 error("RPC failed; result=%d, HTTP code = %ld",
387 results.curl_result, results.http_code);
390 return err;
393 static int probe_rpc(struct rpc_state *rpc)
395 struct active_request_slot *slot;
396 struct curl_slist *headers = NULL;
397 struct strbuf buf = STRBUF_INIT;
398 int err;
400 slot = get_active_slot();
402 headers = curl_slist_append(headers, rpc->hdr_content_type);
403 headers = curl_slist_append(headers, rpc->hdr_accept);
405 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
406 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
407 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
408 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
409 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
410 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
411 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
412 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
413 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
415 err = run_slot(slot);
417 curl_slist_free_all(headers);
418 strbuf_release(&buf);
419 return err;
422 static int post_rpc(struct rpc_state *rpc)
424 struct active_request_slot *slot;
425 struct curl_slist *headers = NULL;
426 int use_gzip = rpc->gzip_request;
427 char *gzip_body = NULL;
428 size_t gzip_size = 0;
429 int err, large_request = 0;
431 /* Try to load the entire request, if we can fit it into the
432 * allocated buffer space we can use HTTP/1.0 and avoid the
433 * chunked encoding mess.
435 while (1) {
436 size_t left = rpc->alloc - rpc->len;
437 char *buf = rpc->buf + rpc->len;
438 int n;
440 if (left < LARGE_PACKET_MAX) {
441 large_request = 1;
442 use_gzip = 0;
443 break;
446 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
447 if (!n)
448 break;
449 rpc->len += n;
452 if (large_request) {
453 do {
454 err = probe_rpc(rpc);
455 } while (err == HTTP_REAUTH);
456 if (err != HTTP_OK)
457 return -1;
460 headers = curl_slist_append(headers, rpc->hdr_content_type);
461 headers = curl_slist_append(headers, rpc->hdr_accept);
462 headers = curl_slist_append(headers, "Expect:");
464 retry:
465 slot = get_active_slot();
467 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
468 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
469 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
470 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
472 if (large_request) {
473 /* The request body is large and the size cannot be predicted.
474 * We must use chunked encoding to send it.
476 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
477 rpc->initial_buffer = 1;
478 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
479 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
480 #ifndef NO_CURL_IOCTL
481 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
482 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
483 #endif
484 if (options.verbosity > 1) {
485 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
486 fflush(stderr);
489 } else if (gzip_body) {
491 * If we are looping to retry authentication, then the previous
492 * run will have set up the headers and gzip buffer already,
493 * and we just need to send it.
495 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
496 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
498 } else if (use_gzip && 1024 < rpc->len) {
499 /* The client backend isn't giving us compressed data so
500 * we can try to deflate it ourselves, this may save on.
501 * the transfer time.
503 git_zstream stream;
504 int ret;
506 memset(&stream, 0, sizeof(stream));
507 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
508 gzip_size = git_deflate_bound(&stream, rpc->len);
509 gzip_body = xmalloc(gzip_size);
511 stream.next_in = (unsigned char *)rpc->buf;
512 stream.avail_in = rpc->len;
513 stream.next_out = (unsigned char *)gzip_body;
514 stream.avail_out = gzip_size;
516 ret = git_deflate(&stream, Z_FINISH);
517 if (ret != Z_STREAM_END)
518 die("cannot deflate request; zlib deflate error %d", ret);
520 ret = git_deflate_end_gently(&stream);
521 if (ret != Z_OK)
522 die("cannot deflate request; zlib end error %d", ret);
524 gzip_size = stream.total_out;
526 headers = curl_slist_append(headers, "Content-Encoding: gzip");
527 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
528 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
530 if (options.verbosity > 1) {
531 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
532 rpc->service_name,
533 (unsigned long)rpc->len, (unsigned long)gzip_size);
534 fflush(stderr);
536 } else {
537 /* We know the complete request size in advance, use the
538 * more normal Content-Length approach.
540 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
541 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
542 if (options.verbosity > 1) {
543 fprintf(stderr, "POST %s (%lu bytes)\n",
544 rpc->service_name, (unsigned long)rpc->len);
545 fflush(stderr);
549 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
550 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
551 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
553 err = run_slot(slot);
554 if (err == HTTP_REAUTH && !large_request)
555 goto retry;
556 if (err != HTTP_OK)
557 err = -1;
559 curl_slist_free_all(headers);
560 free(gzip_body);
561 return err;
564 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
566 const char *svc = rpc->service_name;
567 struct strbuf buf = STRBUF_INIT;
568 struct strbuf *preamble = rpc->stdin_preamble;
569 struct child_process client;
570 int err = 0;
572 memset(&client, 0, sizeof(client));
573 client.in = -1;
574 client.out = -1;
575 client.git_cmd = 1;
576 client.argv = rpc->argv;
577 if (start_command(&client))
578 exit(1);
579 if (preamble)
580 write_or_die(client.in, preamble->buf, preamble->len);
581 if (heads)
582 write_or_die(client.in, heads->buf, heads->len);
584 rpc->alloc = http_post_buffer;
585 rpc->buf = xmalloc(rpc->alloc);
586 rpc->in = client.in;
587 rpc->out = client.out;
588 strbuf_init(&rpc->result, 0);
590 strbuf_addf(&buf, "%s%s", url, svc);
591 rpc->service_url = strbuf_detach(&buf, NULL);
593 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
594 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
596 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
597 rpc->hdr_accept = strbuf_detach(&buf, NULL);
599 while (!err) {
600 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
601 if (!n)
602 break;
603 rpc->pos = 0;
604 rpc->len = n;
605 err |= post_rpc(rpc);
608 close(client.in);
609 client.in = -1;
610 if (!err) {
611 strbuf_read(&rpc->result, client.out, 0);
612 } else {
613 char buf[4096];
614 for (;;)
615 if (xread(client.out, buf, sizeof(buf)) <= 0)
616 break;
619 close(client.out);
620 client.out = -1;
622 err |= finish_command(&client);
623 free(rpc->service_url);
624 free(rpc->hdr_content_type);
625 free(rpc->hdr_accept);
626 free(rpc->buf);
627 strbuf_release(&buf);
628 return err;
631 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
633 struct walker *walker;
634 char **targets = xmalloc(nr_heads * sizeof(char*));
635 int ret, i;
637 if (options.depth)
638 die("dumb http transport does not support --depth");
639 for (i = 0; i < nr_heads; i++)
640 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
642 walker = get_http_walker(url);
643 walker->get_all = 1;
644 walker->get_tree = 1;
645 walker->get_history = 1;
646 walker->get_verbosely = options.verbosity >= 3;
647 walker->get_recover = 0;
648 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
649 walker_free(walker);
651 for (i = 0; i < nr_heads; i++)
652 free(targets[i]);
653 free(targets);
655 return ret ? error("Fetch failed.") : 0;
658 static int fetch_git(struct discovery *heads,
659 int nr_heads, struct ref **to_fetch)
661 struct rpc_state rpc;
662 struct strbuf preamble = STRBUF_INIT;
663 char *depth_arg = NULL;
664 int argc = 0, i, err;
665 const char *argv[15];
667 argv[argc++] = "fetch-pack";
668 argv[argc++] = "--stateless-rpc";
669 argv[argc++] = "--stdin";
670 argv[argc++] = "--lock-pack";
671 if (options.followtags)
672 argv[argc++] = "--include-tag";
673 if (options.thin)
674 argv[argc++] = "--thin";
675 if (options.verbosity >= 3) {
676 argv[argc++] = "-v";
677 argv[argc++] = "-v";
679 if (!options.progress)
680 argv[argc++] = "--no-progress";
681 if (options.depth) {
682 struct strbuf buf = STRBUF_INIT;
683 strbuf_addf(&buf, "--depth=%lu", options.depth);
684 depth_arg = strbuf_detach(&buf, NULL);
685 argv[argc++] = depth_arg;
687 argv[argc++] = url;
688 argv[argc++] = NULL;
690 for (i = 0; i < nr_heads; i++) {
691 struct ref *ref = to_fetch[i];
692 if (!ref->name || !*ref->name)
693 die("cannot fetch by sha1 over smart http");
694 packet_buf_write(&preamble, "%s\n", ref->name);
696 packet_buf_flush(&preamble);
698 memset(&rpc, 0, sizeof(rpc));
699 rpc.service_name = "git-upload-pack",
700 rpc.argv = argv;
701 rpc.stdin_preamble = &preamble;
702 rpc.gzip_request = 1;
704 err = rpc_service(&rpc, heads);
705 if (rpc.result.len)
706 write_or_die(1, rpc.result.buf, rpc.result.len);
707 strbuf_release(&rpc.result);
708 strbuf_release(&preamble);
709 free(depth_arg);
710 return err;
713 static int fetch(int nr_heads, struct ref **to_fetch)
715 struct discovery *d = discover_refs("git-upload-pack", 0);
716 if (d->proto_git)
717 return fetch_git(d, nr_heads, to_fetch);
718 else
719 return fetch_dumb(nr_heads, to_fetch);
722 static void parse_fetch(struct strbuf *buf)
724 struct ref **to_fetch = NULL;
725 struct ref *list_head = NULL;
726 struct ref **list = &list_head;
727 int alloc_heads = 0, nr_heads = 0;
729 do {
730 if (!prefixcmp(buf->buf, "fetch ")) {
731 char *p = buf->buf + strlen("fetch ");
732 char *name;
733 struct ref *ref;
734 unsigned char old_sha1[20];
736 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
737 die("protocol error: expected sha/ref, got %s'", p);
738 if (p[40] == ' ')
739 name = p + 41;
740 else if (!p[40])
741 name = "";
742 else
743 die("protocol error: expected sha/ref, got %s'", p);
745 ref = alloc_ref(name);
746 hashcpy(ref->old_sha1, old_sha1);
748 *list = ref;
749 list = &ref->next;
751 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
752 to_fetch[nr_heads++] = ref;
754 else
755 die("http transport does not support %s", buf->buf);
757 strbuf_reset(buf);
758 if (strbuf_getline(buf, stdin, '\n') == EOF)
759 return;
760 if (!*buf->buf)
761 break;
762 } while (1);
764 if (fetch(nr_heads, to_fetch))
765 exit(128); /* error already reported */
766 free_refs(list_head);
767 free(to_fetch);
769 printf("\n");
770 fflush(stdout);
771 strbuf_reset(buf);
774 static int push_dav(int nr_spec, char **specs)
776 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
777 int argc = 0, i;
779 argv[argc++] = "http-push";
780 argv[argc++] = "--helper-status";
781 if (options.dry_run)
782 argv[argc++] = "--dry-run";
783 if (options.verbosity > 1)
784 argv[argc++] = "--verbose";
785 argv[argc++] = url;
786 for (i = 0; i < nr_spec; i++)
787 argv[argc++] = specs[i];
788 argv[argc++] = NULL;
790 if (run_command_v_opt(argv, RUN_GIT_CMD))
791 die("git-%s failed", argv[0]);
792 free(argv);
793 return 0;
796 static int push_git(struct discovery *heads, int nr_spec, char **specs)
798 struct rpc_state rpc;
799 const char **argv;
800 int argc = 0, i, err;
801 struct string_list_item *cas_option;
803 argv = xmalloc((10 + nr_spec + cas_options.nr) * sizeof(char *));
804 argv[argc++] = "send-pack";
805 argv[argc++] = "--stateless-rpc";
806 argv[argc++] = "--helper-status";
807 if (options.thin)
808 argv[argc++] = "--thin";
809 if (options.dry_run)
810 argv[argc++] = "--dry-run";
811 if (options.verbosity == 0)
812 argv[argc++] = "--quiet";
813 else if (options.verbosity > 1)
814 argv[argc++] = "--verbose";
815 argv[argc++] = options.progress ? "--progress" : "--no-progress";
817 for_each_string_list_item(cas_option, &cas_options)
818 argv[argc++] = cas_option->string;
820 argv[argc++] = url;
821 for (i = 0; i < nr_spec; i++)
822 argv[argc++] = specs[i];
823 argv[argc++] = NULL;
825 memset(&rpc, 0, sizeof(rpc));
826 rpc.service_name = "git-receive-pack",
827 rpc.argv = argv;
829 err = rpc_service(&rpc, heads);
830 if (rpc.result.len)
831 write_or_die(1, rpc.result.buf, rpc.result.len);
832 strbuf_release(&rpc.result);
833 free(argv);
834 return err;
837 static int push(int nr_spec, char **specs)
839 struct discovery *heads = discover_refs("git-receive-pack", 1);
840 int ret;
842 if (heads->proto_git)
843 ret = push_git(heads, nr_spec, specs);
844 else
845 ret = push_dav(nr_spec, specs);
846 free_discovery(heads);
847 return ret;
850 static void parse_push(struct strbuf *buf)
852 char **specs = NULL;
853 int alloc_spec = 0, nr_spec = 0, i, ret;
855 do {
856 if (!prefixcmp(buf->buf, "push ")) {
857 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
858 specs[nr_spec++] = xstrdup(buf->buf + 5);
860 else
861 die("http transport does not support %s", buf->buf);
863 strbuf_reset(buf);
864 if (strbuf_getline(buf, stdin, '\n') == EOF)
865 goto free_specs;
866 if (!*buf->buf)
867 break;
868 } while (1);
870 ret = push(nr_spec, specs);
871 printf("\n");
872 fflush(stdout);
874 if (ret)
875 exit(128); /* error already reported */
877 free_specs:
878 for (i = 0; i < nr_spec; i++)
879 free(specs[i]);
880 free(specs);
883 int main(int argc, const char **argv)
885 struct strbuf buf = STRBUF_INIT;
886 int nongit;
888 git_extract_argv0_path(argv[0]);
889 setup_git_directory_gently(&nongit);
890 if (argc < 2) {
891 fprintf(stderr, "Remote needed\n");
892 return 1;
895 options.verbosity = 1;
896 options.progress = !!isatty(2);
897 options.thin = 1;
899 remote = remote_get(argv[1]);
901 if (argc > 2) {
902 end_url_with_slash(&buf, argv[2]);
903 } else {
904 end_url_with_slash(&buf, remote->url[0]);
907 url = strbuf_detach(&buf, NULL);
909 http_init(remote, url, 0);
911 do {
912 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
913 if (ferror(stdin))
914 fprintf(stderr, "Error reading command stream\n");
915 else
916 fprintf(stderr, "Unexpected end of command stream\n");
917 return 1;
919 if (buf.len == 0)
920 break;
921 if (!prefixcmp(buf.buf, "fetch ")) {
922 if (nongit)
923 die("Fetch attempted without a local repo");
924 parse_fetch(&buf);
926 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
927 int for_push = !!strstr(buf.buf + 4, "for-push");
928 output_refs(get_refs(for_push));
930 } else if (!prefixcmp(buf.buf, "push ")) {
931 parse_push(&buf);
933 } else if (!prefixcmp(buf.buf, "option ")) {
934 char *name = buf.buf + strlen("option ");
935 char *value = strchr(name, ' ');
936 int result;
938 if (value)
939 *value++ = '\0';
940 else
941 value = "true";
943 result = set_option(name, value);
944 if (!result)
945 printf("ok\n");
946 else if (result < 0)
947 printf("error invalid value\n");
948 else
949 printf("unsupported\n");
950 fflush(stdout);
952 } else if (!strcmp(buf.buf, "capabilities")) {
953 printf("fetch\n");
954 printf("option\n");
955 printf("push\n");
956 printf("\n");
957 fflush(stdout);
958 } else {
959 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
960 return 1;
962 strbuf_reset(&buf);
963 } while (1);
965 http_cleanup();
967 return 0;