7 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
12 #include "credential.h"
13 #include "sha1-array.h"
14 #include "send-pack.h"
16 static struct remote *remote;
17 /* always ends with a trailing slash */
18 static struct strbuf url = STRBUF_INIT;
24 struct string_list deepen_not;
25 struct string_list push_options;
26 unsigned progress : 1,
27 check_self_contained_and_connected : 1,
33 /* One of the SEND_PACK_PUSH_CERT_* constants. */
37 static struct options options;
38 static struct string_list cas_options = STRING_LIST_INIT_DUP;
40 static int set_option(const char *name, const char *value)
42 if (!strcmp(name, "verbosity")) {
44 int v = strtol(value, &end, 10);
45 if (value == end || *end)
47 options.verbosity = v;
50 else if (!strcmp(name, "progress")) {
51 if (!strcmp(value, "true"))
53 else if (!strcmp(value, "false"))
59 else if (!strcmp(name, "depth")) {
61 unsigned long v = strtoul(value, &end, 10);
62 if (value == end || *end)
67 else if (!strcmp(name, "deepen-since")) {
68 options.deepen_since = xstrdup(value);
71 else if (!strcmp(name, "deepen-not")) {
72 string_list_append(&options.deepen_not, value);
75 else if (!strcmp(name, "deepen-relative")) {
76 if (!strcmp(value, "true"))
77 options.deepen_relative = 1;
78 else if (!strcmp(value, "false"))
79 options.deepen_relative = 0;
84 else if (!strcmp(name, "followtags")) {
85 if (!strcmp(value, "true"))
86 options.followtags = 1;
87 else if (!strcmp(value, "false"))
88 options.followtags = 0;
93 else if (!strcmp(name, "dry-run")) {
94 if (!strcmp(value, "true"))
96 else if (!strcmp(value, "false"))
102 else if (!strcmp(name, "check-connectivity")) {
103 if (!strcmp(value, "true"))
104 options.check_self_contained_and_connected = 1;
105 else if (!strcmp(value, "false"))
106 options.check_self_contained_and_connected = 0;
111 else if (!strcmp(name, "cas")) {
112 struct strbuf val = STRBUF_INIT;
113 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
114 string_list_append(&cas_options, val.buf);
115 strbuf_release(&val);
117 } else if (!strcmp(name, "cloning")) {
118 if (!strcmp(value, "true"))
120 else if (!strcmp(value, "false"))
125 } else if (!strcmp(name, "update-shallow")) {
126 if (!strcmp(value, "true"))
127 options.update_shallow = 1;
128 else if (!strcmp(value, "false"))
129 options.update_shallow = 0;
133 } else if (!strcmp(name, "pushcert")) {
134 if (!strcmp(value, "true"))
135 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
136 else if (!strcmp(value, "false"))
137 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
138 else if (!strcmp(value, "if-asked"))
139 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
143 } else if (!strcmp(name, "push-option")) {
144 string_list_append(&options.push_options, value);
147 #if LIBCURL_VERSION_NUM >= 0x070a08
148 } else if (!strcmp(name, "family")) {
149 if (!strcmp(value, "ipv4"))
150 git_curl_ipresolve = CURL_IPRESOLVE_V4;
151 else if (!strcmp(value, "ipv6"))
152 git_curl_ipresolve = CURL_IPRESOLVE_V6;
153 else if (!strcmp(value, "all"))
154 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
158 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
160 return 1 /* unsupported */;
170 struct oid_array shallow;
171 unsigned proto_git : 1;
173 static struct discovery *last_discovery;
175 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
177 struct ref *list = NULL;
178 get_remote_heads(-1, heads->buf, heads->len, &list,
179 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
183 static struct ref *parse_info_refs(struct discovery *heads)
185 char *data, *start, *mid;
189 struct ref *refs = NULL;
190 struct ref *ref = NULL;
191 struct ref *last_ref = NULL;
196 while (i < heads->len) {
202 if (data[i] == '\n') {
203 if (mid - start != 40)
204 die("%sinfo/refs not valid: is this a git repository?",
208 ref = alloc_ref(ref_name);
209 get_oid_hex(start, &ref->old_oid);
213 last_ref->next = ref;
220 ref = alloc_ref("HEAD");
221 if (!http_fetch_ref(url.buf, ref) &&
222 !resolve_remote_symref(ref, refs)) {
232 static void free_discovery(struct discovery *d)
235 if (d == last_discovery)
236 last_discovery = NULL;
237 free(d->shallow.oid);
244 static int show_http_message(struct strbuf *type, struct strbuf *charset,
250 * We only show text/plain parts, as other types are likely
251 * to be ugly to look at on the user's terminal.
253 if (strcmp(type->buf, "text/plain"))
256 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
264 eol = strchrnul(p, '\n');
265 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
271 static struct discovery *discover_refs(const char *service, int for_push)
273 struct strbuf exp = STRBUF_INIT;
274 struct strbuf type = STRBUF_INIT;
275 struct strbuf charset = STRBUF_INIT;
276 struct strbuf buffer = STRBUF_INIT;
277 struct strbuf refs_url = STRBUF_INIT;
278 struct strbuf effective_url = STRBUF_INIT;
279 struct discovery *last = last_discovery;
280 int http_ret, maybe_smart = 0;
281 struct http_get_options http_options;
283 if (last && !strcmp(service, last->service))
285 free_discovery(last);
287 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
288 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
289 git_env_bool("GIT_SMART_HTTP", 1)) {
291 if (!strchr(url.buf, '?'))
292 strbuf_addch(&refs_url, '?');
294 strbuf_addch(&refs_url, '&');
295 strbuf_addf(&refs_url, "service=%s", service);
298 memset(&http_options, 0, sizeof(http_options));
299 http_options.content_type = &type;
300 http_options.charset = &charset;
301 http_options.effective_url = &effective_url;
302 http_options.base_url = &url;
303 http_options.initial_request = 1;
304 http_options.no_cache = 1;
305 http_options.keep_error = 1;
307 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
311 case HTTP_MISSING_TARGET:
312 show_http_message(&type, &charset, &buffer);
313 die("repository '%s' not found", url.buf);
315 show_http_message(&type, &charset, &buffer);
316 die("Authentication failed for '%s'", url.buf);
318 show_http_message(&type, &charset, &buffer);
319 die("unable to access '%s': %s", url.buf, curl_errorstr);
322 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
323 warning(_("redirecting to %s"), url.buf);
325 last= xcalloc(1, sizeof(*last_discovery));
326 last->service = service;
327 last->buf_alloc = strbuf_detach(&buffer, &last->len);
328 last->buf = last->buf_alloc;
330 strbuf_addf(&exp, "application/x-%s-advertisement", service);
332 (5 <= last->len && last->buf[4] == '#') &&
333 !strbuf_cmp(&exp, &type)) {
337 * smart HTTP response; validate that the service
338 * pkt-line matches our request.
340 line = packet_read_line_buf(&last->buf, &last->len, NULL);
343 strbuf_addf(&exp, "# service=%s", service);
344 if (strcmp(line, exp.buf))
345 die("invalid server response; got '%s'", line);
346 strbuf_release(&exp);
348 /* The header can include additional metadata lines, up
349 * until a packet flush marker. Ignore these now, but
350 * in the future we might start to scan them.
352 while (packet_read_line_buf(&last->buf, &last->len, NULL))
359 last->refs = parse_git_refs(last, for_push);
361 last->refs = parse_info_refs(last);
363 strbuf_release(&refs_url);
364 strbuf_release(&exp);
365 strbuf_release(&type);
366 strbuf_release(&charset);
367 strbuf_release(&effective_url);
368 strbuf_release(&buffer);
369 last_discovery = last;
373 static struct ref *get_refs(int for_push)
375 struct discovery *heads;
378 heads = discover_refs("git-receive-pack", for_push);
380 heads = discover_refs("git-upload-pack", for_push);
385 static void output_refs(struct ref *refs)
388 for (posn = refs; posn; posn = posn->next) {
390 printf("@%s %s\n", posn->symref, posn->name);
392 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
399 const char *service_name;
401 struct strbuf *stdin_preamble;
403 char *hdr_content_type;
412 struct strbuf result;
413 unsigned gzip_request : 1;
414 unsigned initial_buffer : 1;
417 static size_t rpc_out(void *ptr, size_t eltsize,
418 size_t nmemb, void *buffer_)
420 size_t max = eltsize * nmemb;
421 struct rpc_state *rpc = buffer_;
422 size_t avail = rpc->len - rpc->pos;
425 rpc->initial_buffer = 0;
426 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
435 memcpy(ptr, rpc->buf + rpc->pos, avail);
440 #ifndef NO_CURL_IOCTL
441 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
443 struct rpc_state *rpc = clientp;
449 case CURLIOCMD_RESTARTREAD:
450 if (rpc->initial_buffer) {
454 error("unable to rewind rpc post data - try increasing http.postBuffer");
455 return CURLIOE_FAILRESTART;
458 return CURLIOE_UNKNOWNCMD;
463 static size_t rpc_in(char *ptr, size_t eltsize,
464 size_t nmemb, void *buffer_)
466 size_t size = eltsize * nmemb;
467 struct rpc_state *rpc = buffer_;
469 rpc->any_written = 1;
470 write_or_die(rpc->in, ptr, size);
474 static int run_slot(struct active_request_slot *slot,
475 struct slot_results *results)
478 struct slot_results results_buf;
481 results = &results_buf;
483 err = run_one_slot(slot, results);
485 if (err != HTTP_OK && err != HTTP_REAUTH) {
486 struct strbuf msg = STRBUF_INIT;
487 if (results->http_code && results->http_code != 200)
488 strbuf_addf(&msg, "HTTP %ld", results->http_code);
489 if (results->curl_result != CURLE_OK) {
491 strbuf_addch(&msg, ' ');
492 strbuf_addf(&msg, "curl %d", results->curl_result);
493 if (curl_errorstr[0]) {
494 strbuf_addch(&msg, ' ');
495 strbuf_addstr(&msg, curl_errorstr);
498 error("RPC failed; %s", msg.buf);
499 strbuf_release(&msg);
505 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
507 struct active_request_slot *slot;
508 struct curl_slist *headers = http_copy_default_headers();
509 struct strbuf buf = STRBUF_INIT;
512 slot = get_active_slot();
514 headers = curl_slist_append(headers, rpc->hdr_content_type);
515 headers = curl_slist_append(headers, rpc->hdr_accept);
517 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
518 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
519 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
520 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
521 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
522 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
523 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
524 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
525 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
527 err = run_slot(slot, results);
529 curl_slist_free_all(headers);
530 strbuf_release(&buf);
534 static curl_off_t xcurl_off_t(ssize_t len) {
535 if (len > maximum_signed_value_of_type(curl_off_t))
536 die("cannot handle pushes this big");
537 return (curl_off_t) len;
540 static int post_rpc(struct rpc_state *rpc)
542 struct active_request_slot *slot;
543 struct curl_slist *headers = http_copy_default_headers();
544 int use_gzip = rpc->gzip_request;
545 char *gzip_body = NULL;
546 size_t gzip_size = 0;
547 int err, large_request = 0;
548 int needs_100_continue = 0;
550 /* Try to load the entire request, if we can fit it into the
551 * allocated buffer space we can use HTTP/1.0 and avoid the
552 * chunked encoding mess.
555 size_t left = rpc->alloc - rpc->len;
556 char *buf = rpc->buf + rpc->len;
559 if (left < LARGE_PACKET_MAX) {
565 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
572 struct slot_results results;
575 err = probe_rpc(rpc, &results);
576 if (err == HTTP_REAUTH)
577 credential_fill(&http_auth);
578 } while (err == HTTP_REAUTH);
582 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
583 needs_100_continue = 1;
586 headers = curl_slist_append(headers, rpc->hdr_content_type);
587 headers = curl_slist_append(headers, rpc->hdr_accept);
588 headers = curl_slist_append(headers, needs_100_continue ?
589 "Expect: 100-continue" : "Expect:");
592 slot = get_active_slot();
594 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
595 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
596 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
597 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
600 /* The request body is large and the size cannot be predicted.
601 * We must use chunked encoding to send it.
603 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
604 rpc->initial_buffer = 1;
605 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
606 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
607 #ifndef NO_CURL_IOCTL
608 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
609 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
611 if (options.verbosity > 1) {
612 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
616 } else if (gzip_body) {
618 * If we are looping to retry authentication, then the previous
619 * run will have set up the headers and gzip buffer already,
620 * and we just need to send it.
622 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
623 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
625 } else if (use_gzip && 1024 < rpc->len) {
626 /* The client backend isn't giving us compressed data so
627 * we can try to deflate it ourselves, this may save on.
633 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
634 gzip_size = git_deflate_bound(&stream, rpc->len);
635 gzip_body = xmalloc(gzip_size);
637 stream.next_in = (unsigned char *)rpc->buf;
638 stream.avail_in = rpc->len;
639 stream.next_out = (unsigned char *)gzip_body;
640 stream.avail_out = gzip_size;
642 ret = git_deflate(&stream, Z_FINISH);
643 if (ret != Z_STREAM_END)
644 die("cannot deflate request; zlib deflate error %d", ret);
646 ret = git_deflate_end_gently(&stream);
648 die("cannot deflate request; zlib end error %d", ret);
650 gzip_size = stream.total_out;
652 headers = curl_slist_append(headers, "Content-Encoding: gzip");
653 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
654 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
656 if (options.verbosity > 1) {
657 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
659 (unsigned long)rpc->len, (unsigned long)gzip_size);
663 /* We know the complete request size in advance, use the
664 * more normal Content-Length approach.
666 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
667 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
668 if (options.verbosity > 1) {
669 fprintf(stderr, "POST %s (%lu bytes)\n",
670 rpc->service_name, (unsigned long)rpc->len);
675 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
676 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
677 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
680 rpc->any_written = 0;
681 err = run_slot(slot, NULL);
682 if (err == HTTP_REAUTH && !large_request) {
683 credential_fill(&http_auth);
689 if (!rpc->any_written)
692 curl_slist_free_all(headers);
697 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
699 const char *svc = rpc->service_name;
700 struct strbuf buf = STRBUF_INIT;
701 struct strbuf *preamble = rpc->stdin_preamble;
702 struct child_process client = CHILD_PROCESS_INIT;
708 client.argv = rpc->argv;
709 if (start_command(&client))
712 write_or_die(client.in, preamble->buf, preamble->len);
714 write_or_die(client.in, heads->buf, heads->len);
716 rpc->alloc = http_post_buffer;
717 rpc->buf = xmalloc(rpc->alloc);
719 rpc->out = client.out;
720 strbuf_init(&rpc->result, 0);
722 strbuf_addf(&buf, "%s%s", url.buf, svc);
723 rpc->service_url = strbuf_detach(&buf, NULL);
725 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
726 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
728 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
729 rpc->hdr_accept = strbuf_detach(&buf, NULL);
732 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
737 err |= post_rpc(rpc);
743 strbuf_read(&rpc->result, client.out, 0);
747 if (xread(client.out, buf, sizeof(buf)) <= 0)
754 err |= finish_command(&client);
755 free(rpc->service_url);
756 free(rpc->hdr_content_type);
757 free(rpc->hdr_accept);
759 strbuf_release(&buf);
763 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
765 struct walker *walker;
769 ALLOC_ARRAY(targets, nr_heads);
770 if (options.depth || options.deepen_since)
771 die("dumb http transport does not support shallow capabilities");
772 for (i = 0; i < nr_heads; i++)
773 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
775 walker = get_http_walker(url.buf);
777 walker->get_tree = 1;
778 walker->get_history = 1;
779 walker->get_verbosely = options.verbosity >= 3;
780 walker->get_recover = 0;
781 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
784 for (i = 0; i < nr_heads; i++)
788 return ret ? error("fetch failed.") : 0;
791 static int fetch_git(struct discovery *heads,
792 int nr_heads, struct ref **to_fetch)
794 struct rpc_state rpc;
795 struct strbuf preamble = STRBUF_INIT;
797 struct argv_array args = ARGV_ARRAY_INIT;
799 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
800 "--stdin", "--lock-pack", NULL);
801 if (options.followtags)
802 argv_array_push(&args, "--include-tag");
804 argv_array_push(&args, "--thin");
805 if (options.verbosity >= 3)
806 argv_array_pushl(&args, "-v", "-v", NULL);
807 if (options.check_self_contained_and_connected)
808 argv_array_push(&args, "--check-self-contained-and-connected");
810 argv_array_push(&args, "--cloning");
811 if (options.update_shallow)
812 argv_array_push(&args, "--update-shallow");
813 if (!options.progress)
814 argv_array_push(&args, "--no-progress");
816 argv_array_pushf(&args, "--depth=%lu", options.depth);
817 if (options.deepen_since)
818 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
819 for (i = 0; i < options.deepen_not.nr; i++)
820 argv_array_pushf(&args, "--shallow-exclude=%s",
821 options.deepen_not.items[i].string);
822 if (options.deepen_relative && options.depth)
823 argv_array_push(&args, "--deepen-relative");
824 argv_array_push(&args, url.buf);
826 for (i = 0; i < nr_heads; i++) {
827 struct ref *ref = to_fetch[i];
829 die("cannot fetch by sha1 over smart http");
830 packet_buf_write(&preamble, "%s %s\n",
831 oid_to_hex(&ref->old_oid), ref->name);
833 packet_buf_flush(&preamble);
835 memset(&rpc, 0, sizeof(rpc));
836 rpc.service_name = "git-upload-pack",
837 rpc.argv = args.argv;
838 rpc.stdin_preamble = &preamble;
839 rpc.gzip_request = 1;
841 err = rpc_service(&rpc, heads);
843 write_or_die(1, rpc.result.buf, rpc.result.len);
844 strbuf_release(&rpc.result);
845 strbuf_release(&preamble);
846 argv_array_clear(&args);
850 static int fetch(int nr_heads, struct ref **to_fetch)
852 struct discovery *d = discover_refs("git-upload-pack", 0);
854 return fetch_git(d, nr_heads, to_fetch);
856 return fetch_dumb(nr_heads, to_fetch);
859 static void parse_fetch(struct strbuf *buf)
861 struct ref **to_fetch = NULL;
862 struct ref *list_head = NULL;
863 struct ref **list = &list_head;
864 int alloc_heads = 0, nr_heads = 0;
868 if (skip_prefix(buf->buf, "fetch ", &p)) {
871 struct object_id old_oid;
873 if (get_oid_hex(p, &old_oid))
874 die("protocol error: expected sha/ref, got %s'", p);
875 if (p[GIT_SHA1_HEXSZ] == ' ')
876 name = p + GIT_SHA1_HEXSZ + 1;
877 else if (!p[GIT_SHA1_HEXSZ])
880 die("protocol error: expected sha/ref, got %s'", p);
882 ref = alloc_ref(name);
883 oidcpy(&ref->old_oid, &old_oid);
888 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
889 to_fetch[nr_heads++] = ref;
892 die("http transport does not support %s", buf->buf);
895 if (strbuf_getline_lf(buf, stdin) == EOF)
901 if (fetch(nr_heads, to_fetch))
902 exit(128); /* error already reported */
903 free_refs(list_head);
911 static int push_dav(int nr_spec, char **specs)
913 struct child_process child = CHILD_PROCESS_INIT;
917 argv_array_push(&child.args, "http-push");
918 argv_array_push(&child.args, "--helper-status");
920 argv_array_push(&child.args, "--dry-run");
921 if (options.verbosity > 1)
922 argv_array_push(&child.args, "--verbose");
923 argv_array_push(&child.args, url.buf);
924 for (i = 0; i < nr_spec; i++)
925 argv_array_push(&child.args, specs[i]);
927 if (run_command(&child))
928 die("git-http-push failed");
932 static int push_git(struct discovery *heads, int nr_spec, char **specs)
934 struct rpc_state rpc;
936 struct argv_array args;
937 struct string_list_item *cas_option;
938 struct strbuf preamble = STRBUF_INIT;
940 argv_array_init(&args);
941 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
945 argv_array_push(&args, "--thin");
947 argv_array_push(&args, "--dry-run");
948 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
949 argv_array_push(&args, "--signed=yes");
950 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
951 argv_array_push(&args, "--signed=if-asked");
952 if (options.verbosity == 0)
953 argv_array_push(&args, "--quiet");
954 else if (options.verbosity > 1)
955 argv_array_push(&args, "--verbose");
956 for (i = 0; i < options.push_options.nr; i++)
957 argv_array_pushf(&args, "--push-option=%s",
958 options.push_options.items[i].string);
959 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
960 for_each_string_list_item(cas_option, &cas_options)
961 argv_array_push(&args, cas_option->string);
962 argv_array_push(&args, url.buf);
964 argv_array_push(&args, "--stdin");
965 for (i = 0; i < nr_spec; i++)
966 packet_buf_write(&preamble, "%s\n", specs[i]);
967 packet_buf_flush(&preamble);
969 memset(&rpc, 0, sizeof(rpc));
970 rpc.service_name = "git-receive-pack",
971 rpc.argv = args.argv;
972 rpc.stdin_preamble = &preamble;
974 err = rpc_service(&rpc, heads);
976 write_or_die(1, rpc.result.buf, rpc.result.len);
977 strbuf_release(&rpc.result);
978 strbuf_release(&preamble);
979 argv_array_clear(&args);
983 static int push(int nr_spec, char **specs)
985 struct discovery *heads = discover_refs("git-receive-pack", 1);
988 if (heads->proto_git)
989 ret = push_git(heads, nr_spec, specs);
991 ret = push_dav(nr_spec, specs);
992 free_discovery(heads);
996 static void parse_push(struct strbuf *buf)
999 int alloc_spec = 0, nr_spec = 0, i, ret;
1002 if (starts_with(buf->buf, "push ")) {
1003 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1004 specs[nr_spec++] = xstrdup(buf->buf + 5);
1007 die("http transport does not support %s", buf->buf);
1010 if (strbuf_getline_lf(buf, stdin) == EOF)
1016 ret = push(nr_spec, specs);
1021 exit(128); /* error already reported */
1024 for (i = 0; i < nr_spec; i++)
1029 int cmd_main(int argc, const char **argv)
1031 struct strbuf buf = STRBUF_INIT;
1034 setup_git_directory_gently(&nongit);
1036 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1040 options.verbosity = 1;
1041 options.progress = !!isatty(2);
1043 string_list_init(&options.deepen_not, 1);
1044 string_list_init(&options.push_options, 1);
1046 remote = remote_get(argv[1]);
1049 end_url_with_slash(&url, argv[2]);
1051 end_url_with_slash(&url, remote->url[0]);
1054 http_init(remote, url.buf, 0);
1059 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1061 error("remote-curl: error reading command stream from git");
1066 if (starts_with(buf.buf, "fetch ")) {
1068 die("remote-curl: fetch attempted without a local repo");
1071 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1072 int for_push = !!strstr(buf.buf + 4, "for-push");
1073 output_refs(get_refs(for_push));
1075 } else if (starts_with(buf.buf, "push ")) {
1078 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1079 char *value = strchr(arg, ' ');
1087 result = set_option(arg, value);
1090 else if (result < 0)
1091 printf("error invalid value\n");
1093 printf("unsupported\n");
1096 } else if (!strcmp(buf.buf, "capabilities")) {
1100 printf("check-connectivity\n");
1104 error("remote-curl: unknown command '%s' from git", buf.buf);