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 unsigned progress : 1,
26 check_self_contained_and_connected : 1,
32 /* One of the SEND_PACK_PUSH_CERT_* constants. */
36 static struct options options;
37 static struct string_list cas_options = STRING_LIST_INIT_DUP;
39 static int set_option(const char *name, const char *value)
41 if (!strcmp(name, "verbosity")) {
43 int v = strtol(value, &end, 10);
44 if (value == end || *end)
46 options.verbosity = v;
49 else if (!strcmp(name, "progress")) {
50 if (!strcmp(value, "true"))
52 else if (!strcmp(value, "false"))
58 else if (!strcmp(name, "depth")) {
60 unsigned long v = strtoul(value, &end, 10);
61 if (value == end || *end)
66 else if (!strcmp(name, "deepen-since")) {
67 options.deepen_since = xstrdup(value);
70 else if (!strcmp(name, "deepen-not")) {
71 string_list_append(&options.deepen_not, value);
74 else if (!strcmp(name, "deepen-relative")) {
75 if (!strcmp(value, "true"))
76 options.deepen_relative = 1;
77 else if (!strcmp(value, "false"))
78 options.deepen_relative = 0;
83 else if (!strcmp(name, "followtags")) {
84 if (!strcmp(value, "true"))
85 options.followtags = 1;
86 else if (!strcmp(value, "false"))
87 options.followtags = 0;
92 else if (!strcmp(name, "dry-run")) {
93 if (!strcmp(value, "true"))
95 else if (!strcmp(value, "false"))
101 else if (!strcmp(name, "check-connectivity")) {
102 if (!strcmp(value, "true"))
103 options.check_self_contained_and_connected = 1;
104 else if (!strcmp(value, "false"))
105 options.check_self_contained_and_connected = 0;
110 else if (!strcmp(name, "cas")) {
111 struct strbuf val = STRBUF_INIT;
112 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
113 string_list_append(&cas_options, val.buf);
114 strbuf_release(&val);
116 } else if (!strcmp(name, "cloning")) {
117 if (!strcmp(value, "true"))
119 else if (!strcmp(value, "false"))
124 } else if (!strcmp(name, "update-shallow")) {
125 if (!strcmp(value, "true"))
126 options.update_shallow = 1;
127 else if (!strcmp(value, "false"))
128 options.update_shallow = 0;
132 } else if (!strcmp(name, "pushcert")) {
133 if (!strcmp(value, "true"))
134 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
135 else if (!strcmp(value, "false"))
136 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
137 else if (!strcmp(value, "if-asked"))
138 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
143 #if LIBCURL_VERSION_NUM >= 0x070a08
144 } else if (!strcmp(name, "family")) {
145 if (!strcmp(value, "ipv4"))
146 git_curl_ipresolve = CURL_IPRESOLVE_V4;
147 else if (!strcmp(value, "ipv6"))
148 git_curl_ipresolve = CURL_IPRESOLVE_V6;
149 else if (!strcmp(value, "all"))
150 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
154 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
156 return 1 /* unsupported */;
166 struct sha1_array shallow;
167 unsigned proto_git : 1;
169 static struct discovery *last_discovery;
171 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
173 struct ref *list = NULL;
174 get_remote_heads(-1, heads->buf, heads->len, &list,
175 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
179 static struct ref *parse_info_refs(struct discovery *heads)
181 char *data, *start, *mid;
185 struct ref *refs = NULL;
186 struct ref *ref = NULL;
187 struct ref *last_ref = NULL;
192 while (i < heads->len) {
198 if (data[i] == '\n') {
199 if (mid - start != 40)
200 die("%sinfo/refs not valid: is this a git repository?",
204 ref = alloc_ref(ref_name);
205 get_oid_hex(start, &ref->old_oid);
209 last_ref->next = ref;
216 ref = alloc_ref("HEAD");
217 if (!http_fetch_ref(url.buf, ref) &&
218 !resolve_remote_symref(ref, refs)) {
228 static void free_discovery(struct discovery *d)
231 if (d == last_discovery)
232 last_discovery = NULL;
233 free(d->shallow.sha1);
240 static int show_http_message(struct strbuf *type, struct strbuf *charset,
246 * We only show text/plain parts, as other types are likely
247 * to be ugly to look at on the user's terminal.
249 if (strcmp(type->buf, "text/plain"))
252 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
260 eol = strchrnul(p, '\n');
261 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
267 static struct discovery *discover_refs(const char *service, int for_push)
269 struct strbuf exp = STRBUF_INIT;
270 struct strbuf type = STRBUF_INIT;
271 struct strbuf charset = STRBUF_INIT;
272 struct strbuf buffer = STRBUF_INIT;
273 struct strbuf refs_url = STRBUF_INIT;
274 struct strbuf effective_url = STRBUF_INIT;
275 struct discovery *last = last_discovery;
276 int http_ret, maybe_smart = 0;
277 struct http_get_options options;
279 if (last && !strcmp(service, last->service))
281 free_discovery(last);
283 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
284 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
285 git_env_bool("GIT_SMART_HTTP", 1)) {
287 if (!strchr(url.buf, '?'))
288 strbuf_addch(&refs_url, '?');
290 strbuf_addch(&refs_url, '&');
291 strbuf_addf(&refs_url, "service=%s", service);
294 memset(&options, 0, sizeof(options));
295 options.content_type = &type;
296 options.charset = &charset;
297 options.effective_url = &effective_url;
298 options.base_url = &url;
299 options.no_cache = 1;
300 options.keep_error = 1;
302 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
306 case HTTP_MISSING_TARGET:
307 show_http_message(&type, &charset, &buffer);
308 die("repository '%s' not found", url.buf);
310 show_http_message(&type, &charset, &buffer);
311 die("Authentication failed for '%s'", url.buf);
313 show_http_message(&type, &charset, &buffer);
314 die("unable to access '%s': %s", url.buf, curl_errorstr);
317 last= xcalloc(1, sizeof(*last_discovery));
318 last->service = service;
319 last->buf_alloc = strbuf_detach(&buffer, &last->len);
320 last->buf = last->buf_alloc;
322 strbuf_addf(&exp, "application/x-%s-advertisement", service);
324 (5 <= last->len && last->buf[4] == '#') &&
325 !strbuf_cmp(&exp, &type)) {
329 * smart HTTP response; validate that the service
330 * pkt-line matches our request.
332 line = packet_read_line_buf(&last->buf, &last->len, NULL);
335 strbuf_addf(&exp, "# service=%s", service);
336 if (strcmp(line, exp.buf))
337 die("invalid server response; got '%s'", line);
338 strbuf_release(&exp);
340 /* The header can include additional metadata lines, up
341 * until a packet flush marker. Ignore these now, but
342 * in the future we might start to scan them.
344 while (packet_read_line_buf(&last->buf, &last->len, NULL))
351 last->refs = parse_git_refs(last, for_push);
353 last->refs = parse_info_refs(last);
355 strbuf_release(&refs_url);
356 strbuf_release(&exp);
357 strbuf_release(&type);
358 strbuf_release(&charset);
359 strbuf_release(&effective_url);
360 strbuf_release(&buffer);
361 last_discovery = last;
365 static struct ref *get_refs(int for_push)
367 struct discovery *heads;
370 heads = discover_refs("git-receive-pack", for_push);
372 heads = discover_refs("git-upload-pack", for_push);
377 static void output_refs(struct ref *refs)
380 for (posn = refs; posn; posn = posn->next) {
382 printf("@%s %s\n", posn->symref, posn->name);
384 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
391 const char *service_name;
393 struct strbuf *stdin_preamble;
395 char *hdr_content_type;
403 struct strbuf result;
404 unsigned gzip_request : 1;
405 unsigned initial_buffer : 1;
408 static size_t rpc_out(void *ptr, size_t eltsize,
409 size_t nmemb, void *buffer_)
411 size_t max = eltsize * nmemb;
412 struct rpc_state *rpc = buffer_;
413 size_t avail = rpc->len - rpc->pos;
416 rpc->initial_buffer = 0;
417 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
426 memcpy(ptr, rpc->buf + rpc->pos, avail);
431 #ifndef NO_CURL_IOCTL
432 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
434 struct rpc_state *rpc = clientp;
440 case CURLIOCMD_RESTARTREAD:
441 if (rpc->initial_buffer) {
445 error("unable to rewind rpc post data - try increasing http.postBuffer");
446 return CURLIOE_FAILRESTART;
449 return CURLIOE_UNKNOWNCMD;
454 static size_t rpc_in(char *ptr, size_t eltsize,
455 size_t nmemb, void *buffer_)
457 size_t size = eltsize * nmemb;
458 struct rpc_state *rpc = buffer_;
459 write_or_die(rpc->in, ptr, size);
463 static int run_slot(struct active_request_slot *slot,
464 struct slot_results *results)
467 struct slot_results results_buf;
470 results = &results_buf;
472 err = run_one_slot(slot, results);
474 if (err != HTTP_OK && err != HTTP_REAUTH) {
475 struct strbuf msg = STRBUF_INIT;
476 if (results->http_code && results->http_code != 200)
477 strbuf_addf(&msg, "HTTP %ld", results->http_code);
478 if (results->curl_result != CURLE_OK) {
480 strbuf_addch(&msg, ' ');
481 strbuf_addf(&msg, "curl %d", results->curl_result);
482 if (curl_errorstr[0]) {
483 strbuf_addch(&msg, ' ');
484 strbuf_addstr(&msg, curl_errorstr);
487 error("RPC failed; %s", msg.buf);
488 strbuf_release(&msg);
494 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
496 struct active_request_slot *slot;
497 struct curl_slist *headers = http_copy_default_headers();
498 struct strbuf buf = STRBUF_INIT;
501 slot = get_active_slot();
503 headers = curl_slist_append(headers, rpc->hdr_content_type);
504 headers = curl_slist_append(headers, rpc->hdr_accept);
506 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
507 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
508 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
509 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
510 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
511 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
512 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
513 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
514 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
516 err = run_slot(slot, results);
518 curl_slist_free_all(headers);
519 strbuf_release(&buf);
523 static int post_rpc(struct rpc_state *rpc)
525 struct active_request_slot *slot;
526 struct curl_slist *headers = http_copy_default_headers();
527 int use_gzip = rpc->gzip_request;
528 char *gzip_body = NULL;
529 size_t gzip_size = 0;
530 int err, large_request = 0;
531 int needs_100_continue = 0;
533 /* Try to load the entire request, if we can fit it into the
534 * allocated buffer space we can use HTTP/1.0 and avoid the
535 * chunked encoding mess.
538 size_t left = rpc->alloc - rpc->len;
539 char *buf = rpc->buf + rpc->len;
542 if (left < LARGE_PACKET_MAX) {
548 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
555 struct slot_results results;
558 err = probe_rpc(rpc, &results);
559 if (err == HTTP_REAUTH)
560 credential_fill(&http_auth);
561 } while (err == HTTP_REAUTH);
565 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
566 needs_100_continue = 1;
569 headers = curl_slist_append(headers, rpc->hdr_content_type);
570 headers = curl_slist_append(headers, rpc->hdr_accept);
571 headers = curl_slist_append(headers, needs_100_continue ?
572 "Expect: 100-continue" : "Expect:");
575 slot = get_active_slot();
577 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
578 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
579 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
580 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
583 /* The request body is large and the size cannot be predicted.
584 * We must use chunked encoding to send it.
586 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
587 rpc->initial_buffer = 1;
588 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
589 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
590 #ifndef NO_CURL_IOCTL
591 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
592 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
594 if (options.verbosity > 1) {
595 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
599 } else if (gzip_body) {
601 * If we are looping to retry authentication, then the previous
602 * run will have set up the headers and gzip buffer already,
603 * and we just need to send it.
605 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
606 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
608 } else if (use_gzip && 1024 < rpc->len) {
609 /* The client backend isn't giving us compressed data so
610 * we can try to deflate it ourselves, this may save on.
616 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
617 gzip_size = git_deflate_bound(&stream, rpc->len);
618 gzip_body = xmalloc(gzip_size);
620 stream.next_in = (unsigned char *)rpc->buf;
621 stream.avail_in = rpc->len;
622 stream.next_out = (unsigned char *)gzip_body;
623 stream.avail_out = gzip_size;
625 ret = git_deflate(&stream, Z_FINISH);
626 if (ret != Z_STREAM_END)
627 die("cannot deflate request; zlib deflate error %d", ret);
629 ret = git_deflate_end_gently(&stream);
631 die("cannot deflate request; zlib end error %d", ret);
633 gzip_size = stream.total_out;
635 headers = curl_slist_append(headers, "Content-Encoding: gzip");
636 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
637 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
639 if (options.verbosity > 1) {
640 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
642 (unsigned long)rpc->len, (unsigned long)gzip_size);
646 /* We know the complete request size in advance, use the
647 * more normal Content-Length approach.
649 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
650 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
651 if (options.verbosity > 1) {
652 fprintf(stderr, "POST %s (%lu bytes)\n",
653 rpc->service_name, (unsigned long)rpc->len);
658 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
659 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
660 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
662 err = run_slot(slot, NULL);
663 if (err == HTTP_REAUTH && !large_request) {
664 credential_fill(&http_auth);
670 curl_slist_free_all(headers);
675 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
677 const char *svc = rpc->service_name;
678 struct strbuf buf = STRBUF_INIT;
679 struct strbuf *preamble = rpc->stdin_preamble;
680 struct child_process client = CHILD_PROCESS_INIT;
686 client.argv = rpc->argv;
687 if (start_command(&client))
690 write_or_die(client.in, preamble->buf, preamble->len);
692 write_or_die(client.in, heads->buf, heads->len);
694 rpc->alloc = http_post_buffer;
695 rpc->buf = xmalloc(rpc->alloc);
697 rpc->out = client.out;
698 strbuf_init(&rpc->result, 0);
700 strbuf_addf(&buf, "%s%s", url.buf, svc);
701 rpc->service_url = strbuf_detach(&buf, NULL);
703 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
704 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
706 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
707 rpc->hdr_accept = strbuf_detach(&buf, NULL);
710 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
715 err |= post_rpc(rpc);
721 strbuf_read(&rpc->result, client.out, 0);
725 if (xread(client.out, buf, sizeof(buf)) <= 0)
732 err |= finish_command(&client);
733 free(rpc->service_url);
734 free(rpc->hdr_content_type);
735 free(rpc->hdr_accept);
737 strbuf_release(&buf);
741 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
743 struct walker *walker;
747 ALLOC_ARRAY(targets, nr_heads);
748 if (options.depth || options.deepen_since)
749 die("dumb http transport does not support shallow capabilities");
750 for (i = 0; i < nr_heads; i++)
751 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
753 walker = get_http_walker(url.buf);
755 walker->get_tree = 1;
756 walker->get_history = 1;
757 walker->get_verbosely = options.verbosity >= 3;
758 walker->get_recover = 0;
759 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
762 for (i = 0; i < nr_heads; i++)
766 return ret ? error("fetch failed.") : 0;
769 static int fetch_git(struct discovery *heads,
770 int nr_heads, struct ref **to_fetch)
772 struct rpc_state rpc;
773 struct strbuf preamble = STRBUF_INIT;
775 struct argv_array args = ARGV_ARRAY_INIT;
777 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
778 "--stdin", "--lock-pack", NULL);
779 if (options.followtags)
780 argv_array_push(&args, "--include-tag");
782 argv_array_push(&args, "--thin");
783 if (options.verbosity >= 3)
784 argv_array_pushl(&args, "-v", "-v", NULL);
785 if (options.check_self_contained_and_connected)
786 argv_array_push(&args, "--check-self-contained-and-connected");
788 argv_array_push(&args, "--cloning");
789 if (options.update_shallow)
790 argv_array_push(&args, "--update-shallow");
791 if (!options.progress)
792 argv_array_push(&args, "--no-progress");
794 argv_array_pushf(&args, "--depth=%lu", options.depth);
795 if (options.deepen_since)
796 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
797 for (i = 0; i < options.deepen_not.nr; i++)
798 argv_array_pushf(&args, "--shallow-exclude=%s",
799 options.deepen_not.items[i].string);
800 if (options.deepen_relative && options.depth)
801 argv_array_push(&args, "--deepen-relative");
802 argv_array_push(&args, url.buf);
804 for (i = 0; i < nr_heads; i++) {
805 struct ref *ref = to_fetch[i];
807 die("cannot fetch by sha1 over smart http");
808 packet_buf_write(&preamble, "%s %s\n",
809 oid_to_hex(&ref->old_oid), ref->name);
811 packet_buf_flush(&preamble);
813 memset(&rpc, 0, sizeof(rpc));
814 rpc.service_name = "git-upload-pack",
815 rpc.argv = args.argv;
816 rpc.stdin_preamble = &preamble;
817 rpc.gzip_request = 1;
819 err = rpc_service(&rpc, heads);
821 write_or_die(1, rpc.result.buf, rpc.result.len);
822 strbuf_release(&rpc.result);
823 strbuf_release(&preamble);
824 argv_array_clear(&args);
828 static int fetch(int nr_heads, struct ref **to_fetch)
830 struct discovery *d = discover_refs("git-upload-pack", 0);
832 return fetch_git(d, nr_heads, to_fetch);
834 return fetch_dumb(nr_heads, to_fetch);
837 static void parse_fetch(struct strbuf *buf)
839 struct ref **to_fetch = NULL;
840 struct ref *list_head = NULL;
841 struct ref **list = &list_head;
842 int alloc_heads = 0, nr_heads = 0;
846 if (skip_prefix(buf->buf, "fetch ", &p)) {
849 struct object_id old_oid;
851 if (get_oid_hex(p, &old_oid))
852 die("protocol error: expected sha/ref, got %s'", p);
853 if (p[GIT_SHA1_HEXSZ] == ' ')
854 name = p + GIT_SHA1_HEXSZ + 1;
855 else if (!p[GIT_SHA1_HEXSZ])
858 die("protocol error: expected sha/ref, got %s'", p);
860 ref = alloc_ref(name);
861 oidcpy(&ref->old_oid, &old_oid);
866 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
867 to_fetch[nr_heads++] = ref;
870 die("http transport does not support %s", buf->buf);
873 if (strbuf_getline_lf(buf, stdin) == EOF)
879 if (fetch(nr_heads, to_fetch))
880 exit(128); /* error already reported */
881 free_refs(list_head);
889 static int push_dav(int nr_spec, char **specs)
891 struct child_process child = CHILD_PROCESS_INIT;
895 argv_array_push(&child.args, "http-push");
896 argv_array_push(&child.args, "--helper-status");
898 argv_array_push(&child.args, "--dry-run");
899 if (options.verbosity > 1)
900 argv_array_push(&child.args, "--verbose");
901 argv_array_push(&child.args, url.buf);
902 for (i = 0; i < nr_spec; i++)
903 argv_array_push(&child.args, specs[i]);
905 if (run_command(&child))
906 die("git-http-push failed");
910 static int push_git(struct discovery *heads, int nr_spec, char **specs)
912 struct rpc_state rpc;
914 struct argv_array args;
915 struct string_list_item *cas_option;
916 struct strbuf preamble = STRBUF_INIT;
918 argv_array_init(&args);
919 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
923 argv_array_push(&args, "--thin");
925 argv_array_push(&args, "--dry-run");
926 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
927 argv_array_push(&args, "--signed=yes");
928 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
929 argv_array_push(&args, "--signed=if-asked");
930 if (options.verbosity == 0)
931 argv_array_push(&args, "--quiet");
932 else if (options.verbosity > 1)
933 argv_array_push(&args, "--verbose");
934 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
935 for_each_string_list_item(cas_option, &cas_options)
936 argv_array_push(&args, cas_option->string);
937 argv_array_push(&args, url.buf);
939 argv_array_push(&args, "--stdin");
940 for (i = 0; i < nr_spec; i++)
941 packet_buf_write(&preamble, "%s\n", specs[i]);
942 packet_buf_flush(&preamble);
944 memset(&rpc, 0, sizeof(rpc));
945 rpc.service_name = "git-receive-pack",
946 rpc.argv = args.argv;
947 rpc.stdin_preamble = &preamble;
949 err = rpc_service(&rpc, heads);
951 write_or_die(1, rpc.result.buf, rpc.result.len);
952 strbuf_release(&rpc.result);
953 strbuf_release(&preamble);
954 argv_array_clear(&args);
958 static int push(int nr_spec, char **specs)
960 struct discovery *heads = discover_refs("git-receive-pack", 1);
963 if (heads->proto_git)
964 ret = push_git(heads, nr_spec, specs);
966 ret = push_dav(nr_spec, specs);
967 free_discovery(heads);
971 static void parse_push(struct strbuf *buf)
974 int alloc_spec = 0, nr_spec = 0, i, ret;
977 if (starts_with(buf->buf, "push ")) {
978 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
979 specs[nr_spec++] = xstrdup(buf->buf + 5);
982 die("http transport does not support %s", buf->buf);
985 if (strbuf_getline_lf(buf, stdin) == EOF)
991 ret = push(nr_spec, specs);
996 exit(128); /* error already reported */
999 for (i = 0; i < nr_spec; i++)
1004 int cmd_main(int argc, const char **argv)
1006 struct strbuf buf = STRBUF_INIT;
1009 setup_git_directory_gently(&nongit);
1011 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1015 options.verbosity = 1;
1016 options.progress = !!isatty(2);
1018 string_list_init(&options.deepen_not, 1);
1020 remote = remote_get(argv[1]);
1023 end_url_with_slash(&url, argv[2]);
1025 end_url_with_slash(&url, remote->url[0]);
1028 http_init(remote, url.buf, 0);
1033 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1035 error("remote-curl: error reading command stream from git");
1040 if (starts_with(buf.buf, "fetch ")) {
1042 die("remote-curl: fetch attempted without a local repo");
1045 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1046 int for_push = !!strstr(buf.buf + 4, "for-push");
1047 output_refs(get_refs(for_push));
1049 } else if (starts_with(buf.buf, "push ")) {
1052 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1053 char *value = strchr(arg, ' ');
1061 result = set_option(arg, value);
1064 else if (result < 0)
1065 printf("error invalid value\n");
1067 printf("unsupported\n");
1070 } else if (!strcmp(buf.buf, "capabilities")) {
1074 printf("check-connectivity\n");
1078 error("remote-curl: unknown command '%s' from git", buf.buf);