9 #include "run-command.h"
11 #include "string-list.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
20 static struct remote *remote;
21 /* always ends with a trailing slash */
22 static struct strbuf url = STRBUF_INIT;
28 struct string_list deepen_not;
29 struct string_list push_options;
31 unsigned progress : 1,
32 check_self_contained_and_connected : 1,
38 /* One of the SEND_PACK_PUSH_CERT_* constants. */
44 static struct options options;
45 static struct string_list cas_options = STRING_LIST_INIT_DUP;
47 static int set_option(const char *name, const char *value)
49 if (!strcmp(name, "verbosity")) {
51 int v = strtol(value, &end, 10);
52 if (value == end || *end)
54 options.verbosity = v;
57 else if (!strcmp(name, "progress")) {
58 if (!strcmp(value, "true"))
60 else if (!strcmp(value, "false"))
66 else if (!strcmp(name, "depth")) {
68 unsigned long v = strtoul(value, &end, 10);
69 if (value == end || *end)
74 else if (!strcmp(name, "deepen-since")) {
75 options.deepen_since = xstrdup(value);
78 else if (!strcmp(name, "deepen-not")) {
79 string_list_append(&options.deepen_not, value);
82 else if (!strcmp(name, "deepen-relative")) {
83 if (!strcmp(value, "true"))
84 options.deepen_relative = 1;
85 else if (!strcmp(value, "false"))
86 options.deepen_relative = 0;
91 else if (!strcmp(name, "followtags")) {
92 if (!strcmp(value, "true"))
93 options.followtags = 1;
94 else if (!strcmp(value, "false"))
95 options.followtags = 0;
100 else if (!strcmp(name, "dry-run")) {
101 if (!strcmp(value, "true"))
103 else if (!strcmp(value, "false"))
109 else if (!strcmp(name, "check-connectivity")) {
110 if (!strcmp(value, "true"))
111 options.check_self_contained_and_connected = 1;
112 else if (!strcmp(value, "false"))
113 options.check_self_contained_and_connected = 0;
118 else if (!strcmp(name, "cas")) {
119 struct strbuf val = STRBUF_INIT;
120 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
121 string_list_append(&cas_options, val.buf);
122 strbuf_release(&val);
124 } else if (!strcmp(name, "cloning")) {
125 if (!strcmp(value, "true"))
127 else if (!strcmp(value, "false"))
132 } else if (!strcmp(name, "update-shallow")) {
133 if (!strcmp(value, "true"))
134 options.update_shallow = 1;
135 else if (!strcmp(value, "false"))
136 options.update_shallow = 0;
140 } else if (!strcmp(name, "pushcert")) {
141 if (!strcmp(value, "true"))
142 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
143 else if (!strcmp(value, "false"))
144 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
145 else if (!strcmp(value, "if-asked"))
146 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
150 } else if (!strcmp(name, "push-option")) {
152 string_list_append(&options.push_options, value);
154 struct strbuf unquoted = STRBUF_INIT;
155 if (unquote_c_style(&unquoted, value, NULL) < 0)
156 die("invalid quoting in push-option value");
157 string_list_append_nodup(&options.push_options,
158 strbuf_detach(&unquoted, NULL));
162 #if LIBCURL_VERSION_NUM >= 0x070a08
163 } else if (!strcmp(name, "family")) {
164 if (!strcmp(value, "ipv4"))
165 git_curl_ipresolve = CURL_IPRESOLVE_V4;
166 else if (!strcmp(value, "ipv6"))
167 git_curl_ipresolve = CURL_IPRESOLVE_V6;
168 else if (!strcmp(value, "all"))
169 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
173 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
174 } else if (!strcmp(name, "from-promisor")) {
175 options.from_promisor = 1;
177 } else if (!strcmp(name, "no-dependents")) {
178 options.no_dependents = 1;
180 } else if (!strcmp(name, "filter")) {
181 options.filter = xstrdup(value);
184 return 1 /* unsupported */;
194 struct oid_array shallow;
195 enum protocol_version version;
196 unsigned proto_git : 1;
198 static struct discovery *last_discovery;
200 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
202 struct ref *list = NULL;
203 struct packet_reader reader;
205 packet_reader_init(&reader, -1, heads->buf, heads->len,
206 PACKET_READ_CHOMP_NEWLINE |
207 PACKET_READ_GENTLE_ON_EOF);
209 heads->version = discover_version(&reader);
210 switch (heads->version) {
213 * Do nothing. This isn't a list of refs but rather a
214 * capability advertisement. Client would have run
215 * 'stateless-connect' so we'll dump this capability listing
216 * and let them request the refs themselves.
221 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
222 NULL, &heads->shallow);
224 case protocol_unknown_version:
225 BUG("unknown protocol version");
231 static struct ref *parse_info_refs(struct discovery *heads)
233 char *data, *start, *mid;
237 struct ref *refs = NULL;
238 struct ref *ref = NULL;
239 struct ref *last_ref = NULL;
244 while (i < heads->len) {
250 if (data[i] == '\n') {
251 if (mid - start != 40)
252 die("%sinfo/refs not valid: is this a git repository?",
256 ref = alloc_ref(ref_name);
257 get_oid_hex(start, &ref->old_oid);
261 last_ref->next = ref;
268 ref = alloc_ref("HEAD");
269 if (!http_fetch_ref(url.buf, ref) &&
270 !resolve_remote_symref(ref, refs)) {
280 static void free_discovery(struct discovery *d)
283 if (d == last_discovery)
284 last_discovery = NULL;
285 free(d->shallow.oid);
293 static int show_http_message(struct strbuf *type, struct strbuf *charset,
299 * We only show text/plain parts, as other types are likely
300 * to be ugly to look at on the user's terminal.
302 if (strcmp(type->buf, "text/plain"))
305 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
313 eol = strchrnul(p, '\n');
314 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
320 static int get_protocol_http_header(enum protocol_version version,
321 struct strbuf *header)
324 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
333 static struct discovery *discover_refs(const char *service, int for_push)
335 struct strbuf exp = STRBUF_INIT;
336 struct strbuf type = STRBUF_INIT;
337 struct strbuf charset = STRBUF_INIT;
338 struct strbuf buffer = STRBUF_INIT;
339 struct strbuf refs_url = STRBUF_INIT;
340 struct strbuf effective_url = STRBUF_INIT;
341 struct strbuf protocol_header = STRBUF_INIT;
342 struct string_list extra_headers = STRING_LIST_INIT_DUP;
343 struct discovery *last = last_discovery;
344 int http_ret, maybe_smart = 0;
345 struct http_get_options http_options;
346 enum protocol_version version = get_protocol_version_config();
348 if (last && !strcmp(service, last->service))
350 free_discovery(last);
352 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
353 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
354 git_env_bool("GIT_SMART_HTTP", 1)) {
356 if (!strchr(url.buf, '?'))
357 strbuf_addch(&refs_url, '?');
359 strbuf_addch(&refs_url, '&');
360 strbuf_addf(&refs_url, "service=%s", service);
364 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
365 * to perform a push, then fallback to v0 since the client doesn't know
366 * how to push yet using v2.
368 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
369 version = protocol_v0;
371 /* Add the extra Git-Protocol header */
372 if (get_protocol_http_header(version, &protocol_header))
373 string_list_append(&extra_headers, protocol_header.buf);
375 memset(&http_options, 0, sizeof(http_options));
376 http_options.content_type = &type;
377 http_options.charset = &charset;
378 http_options.effective_url = &effective_url;
379 http_options.base_url = &url;
380 http_options.extra_headers = &extra_headers;
381 http_options.initial_request = 1;
382 http_options.no_cache = 1;
384 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
388 case HTTP_MISSING_TARGET:
389 show_http_message(&type, &charset, &buffer);
390 die("repository '%s' not found", url.buf);
392 show_http_message(&type, &charset, &buffer);
393 die("Authentication failed for '%s'", url.buf);
395 show_http_message(&type, &charset, &buffer);
396 die("unable to access '%s': %s", url.buf, curl_errorstr);
399 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
400 warning(_("redirecting to %s"), url.buf);
402 last= xcalloc(1, sizeof(*last_discovery));
403 last->service = xstrdup(service);
404 last->buf_alloc = strbuf_detach(&buffer, &last->len);
405 last->buf = last->buf_alloc;
407 strbuf_addf(&exp, "application/x-%s-advertisement", service);
409 (5 <= last->len && last->buf[4] == '#') &&
410 !strbuf_cmp(&exp, &type)) {
414 * smart HTTP response; validate that the service
415 * pkt-line matches our request.
417 line = packet_read_line_buf(&last->buf, &last->len, NULL);
419 die("invalid server response; expected service, got flush packet");
422 strbuf_addf(&exp, "# service=%s", service);
423 if (strcmp(line, exp.buf))
424 die("invalid server response; got '%s'", line);
425 strbuf_release(&exp);
427 /* The header can include additional metadata lines, up
428 * until a packet flush marker. Ignore these now, but
429 * in the future we might start to scan them.
431 while (packet_read_line_buf(&last->buf, &last->len, NULL))
435 } else if (maybe_smart &&
436 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
441 last->refs = parse_git_refs(last, for_push);
443 last->refs = parse_info_refs(last);
445 strbuf_release(&refs_url);
446 strbuf_release(&exp);
447 strbuf_release(&type);
448 strbuf_release(&charset);
449 strbuf_release(&effective_url);
450 strbuf_release(&buffer);
451 strbuf_release(&protocol_header);
452 string_list_clear(&extra_headers, 0);
453 last_discovery = last;
457 static struct ref *get_refs(int for_push)
459 struct discovery *heads;
462 heads = discover_refs("git-receive-pack", for_push);
464 heads = discover_refs("git-upload-pack", for_push);
469 static void output_refs(struct ref *refs)
472 for (posn = refs; posn; posn = posn->next) {
474 printf("@%s %s\n", posn->symref, posn->name);
476 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
483 const char *service_name;
485 struct strbuf *stdin_preamble;
487 char *hdr_content_type;
489 char *protocol_header;
497 struct strbuf result;
498 unsigned gzip_request : 1;
499 unsigned initial_buffer : 1;
502 static size_t rpc_out(void *ptr, size_t eltsize,
503 size_t nmemb, void *buffer_)
505 size_t max = eltsize * nmemb;
506 struct rpc_state *rpc = buffer_;
507 size_t avail = rpc->len - rpc->pos;
510 rpc->initial_buffer = 0;
511 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
520 memcpy(ptr, rpc->buf + rpc->pos, avail);
525 #ifndef NO_CURL_IOCTL
526 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
528 struct rpc_state *rpc = clientp;
534 case CURLIOCMD_RESTARTREAD:
535 if (rpc->initial_buffer) {
539 error("unable to rewind rpc post data - try increasing http.postBuffer");
540 return CURLIOE_FAILRESTART;
543 return CURLIOE_UNKNOWNCMD;
549 struct rpc_state *rpc;
550 struct active_request_slot *slot;
554 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
557 static size_t rpc_in(char *ptr, size_t eltsize,
558 size_t nmemb, void *buffer_)
560 size_t size = eltsize * nmemb;
561 struct rpc_in_data *data = buffer_;
564 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
565 &response_code) != CURLE_OK)
567 if (response_code >= 300)
570 data->rpc->any_written = 1;
571 write_or_die(data->rpc->in, ptr, size);
575 static int run_slot(struct active_request_slot *slot,
576 struct slot_results *results)
579 struct slot_results results_buf;
582 results = &results_buf;
584 err = run_one_slot(slot, results);
586 if (err != HTTP_OK && err != HTTP_REAUTH) {
587 struct strbuf msg = STRBUF_INIT;
588 if (results->http_code && results->http_code != 200)
589 strbuf_addf(&msg, "HTTP %ld", results->http_code);
590 if (results->curl_result != CURLE_OK) {
592 strbuf_addch(&msg, ' ');
593 strbuf_addf(&msg, "curl %d", results->curl_result);
594 if (curl_errorstr[0]) {
595 strbuf_addch(&msg, ' ');
596 strbuf_addstr(&msg, curl_errorstr);
599 error("RPC failed; %s", msg.buf);
600 strbuf_release(&msg);
606 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
608 struct active_request_slot *slot;
609 struct curl_slist *headers = http_copy_default_headers();
610 struct strbuf buf = STRBUF_INIT;
613 slot = get_active_slot();
615 headers = curl_slist_append(headers, rpc->hdr_content_type);
616 headers = curl_slist_append(headers, rpc->hdr_accept);
618 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
619 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
620 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
621 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
622 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
623 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
624 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
625 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
626 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
628 err = run_slot(slot, results);
630 curl_slist_free_all(headers);
631 strbuf_release(&buf);
635 static curl_off_t xcurl_off_t(size_t len)
637 uintmax_t size = len;
638 if (size > maximum_signed_value_of_type(curl_off_t))
639 die("cannot handle pushes this big");
640 return (curl_off_t)size;
643 static int post_rpc(struct rpc_state *rpc)
645 struct active_request_slot *slot;
646 struct curl_slist *headers = http_copy_default_headers();
647 int use_gzip = rpc->gzip_request;
648 char *gzip_body = NULL;
649 size_t gzip_size = 0;
650 int err, large_request = 0;
651 int needs_100_continue = 0;
652 struct rpc_in_data rpc_in_data;
654 /* Try to load the entire request, if we can fit it into the
655 * allocated buffer space we can use HTTP/1.0 and avoid the
656 * chunked encoding mess.
659 size_t left = rpc->alloc - rpc->len;
660 char *buf = rpc->buf + rpc->len;
663 if (left < LARGE_PACKET_MAX) {
669 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
676 struct slot_results results;
679 err = probe_rpc(rpc, &results);
680 if (err == HTTP_REAUTH)
681 credential_fill(&http_auth);
682 } while (err == HTTP_REAUTH);
686 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
687 needs_100_continue = 1;
690 headers = curl_slist_append(headers, rpc->hdr_content_type);
691 headers = curl_slist_append(headers, rpc->hdr_accept);
692 headers = curl_slist_append(headers, needs_100_continue ?
693 "Expect: 100-continue" : "Expect:");
695 /* Add the extra Git-Protocol header */
696 if (rpc->protocol_header)
697 headers = curl_slist_append(headers, rpc->protocol_header);
700 slot = get_active_slot();
702 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
703 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
704 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
705 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
708 /* The request body is large and the size cannot be predicted.
709 * We must use chunked encoding to send it.
711 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
712 rpc->initial_buffer = 1;
713 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
714 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
715 #ifndef NO_CURL_IOCTL
716 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
717 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
719 if (options.verbosity > 1) {
720 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
724 } else if (gzip_body) {
726 * If we are looping to retry authentication, then the previous
727 * run will have set up the headers and gzip buffer already,
728 * and we just need to send it.
730 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
731 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
733 } else if (use_gzip && 1024 < rpc->len) {
734 /* The client backend isn't giving us compressed data so
735 * we can try to deflate it ourselves, this may save on
741 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
742 gzip_size = git_deflate_bound(&stream, rpc->len);
743 gzip_body = xmalloc(gzip_size);
745 stream.next_in = (unsigned char *)rpc->buf;
746 stream.avail_in = rpc->len;
747 stream.next_out = (unsigned char *)gzip_body;
748 stream.avail_out = gzip_size;
750 ret = git_deflate(&stream, Z_FINISH);
751 if (ret != Z_STREAM_END)
752 die("cannot deflate request; zlib deflate error %d", ret);
754 ret = git_deflate_end_gently(&stream);
756 die("cannot deflate request; zlib end error %d", ret);
758 gzip_size = stream.total_out;
760 headers = curl_slist_append(headers, "Content-Encoding: gzip");
761 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
762 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
764 if (options.verbosity > 1) {
765 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
767 (unsigned long)rpc->len, (unsigned long)gzip_size);
771 /* We know the complete request size in advance, use the
772 * more normal Content-Length approach.
774 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
775 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
776 if (options.verbosity > 1) {
777 fprintf(stderr, "POST %s (%lu bytes)\n",
778 rpc->service_name, (unsigned long)rpc->len);
783 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
784 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
785 rpc_in_data.rpc = rpc;
786 rpc_in_data.slot = slot;
787 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
788 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
791 rpc->any_written = 0;
792 err = run_slot(slot, NULL);
793 if (err == HTTP_REAUTH && !large_request) {
794 credential_fill(&http_auth);
800 if (!rpc->any_written)
803 curl_slist_free_all(headers);
808 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
810 const char *svc = rpc->service_name;
811 struct strbuf buf = STRBUF_INIT;
812 struct strbuf *preamble = rpc->stdin_preamble;
813 struct child_process client = CHILD_PROCESS_INIT;
819 client.argv = rpc->argv;
820 if (start_command(&client))
823 write_or_die(client.in, preamble->buf, preamble->len);
825 write_or_die(client.in, heads->buf, heads->len);
827 rpc->alloc = http_post_buffer;
828 rpc->buf = xmalloc(rpc->alloc);
830 rpc->out = client.out;
831 strbuf_init(&rpc->result, 0);
833 strbuf_addf(&buf, "%s%s", url.buf, svc);
834 rpc->service_url = strbuf_detach(&buf, NULL);
836 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
837 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
839 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
840 rpc->hdr_accept = strbuf_detach(&buf, NULL);
842 if (get_protocol_http_header(heads->version, &buf))
843 rpc->protocol_header = strbuf_detach(&buf, NULL);
845 rpc->protocol_header = NULL;
848 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
853 err |= post_rpc(rpc);
859 strbuf_read(&rpc->result, client.out, 0);
863 if (xread(client.out, buf, sizeof(buf)) <= 0)
870 err |= finish_command(&client);
871 free(rpc->service_url);
872 free(rpc->hdr_content_type);
873 free(rpc->hdr_accept);
874 free(rpc->protocol_header);
876 strbuf_release(&buf);
880 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
882 struct walker *walker;
886 ALLOC_ARRAY(targets, nr_heads);
887 if (options.depth || options.deepen_since)
888 die("dumb http transport does not support shallow capabilities");
889 for (i = 0; i < nr_heads; i++)
890 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
892 walker = get_http_walker(url.buf);
893 walker->get_verbosely = options.verbosity >= 3;
894 walker->get_recover = 0;
895 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
898 for (i = 0; i < nr_heads; i++)
902 return ret ? error("fetch failed.") : 0;
905 static int fetch_git(struct discovery *heads,
906 int nr_heads, struct ref **to_fetch)
908 struct rpc_state rpc;
909 struct strbuf preamble = STRBUF_INIT;
911 struct argv_array args = ARGV_ARRAY_INIT;
913 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
914 "--stdin", "--lock-pack", NULL);
915 if (options.followtags)
916 argv_array_push(&args, "--include-tag");
918 argv_array_push(&args, "--thin");
919 if (options.verbosity >= 3)
920 argv_array_pushl(&args, "-v", "-v", NULL);
921 if (options.check_self_contained_and_connected)
922 argv_array_push(&args, "--check-self-contained-and-connected");
924 argv_array_push(&args, "--cloning");
925 if (options.update_shallow)
926 argv_array_push(&args, "--update-shallow");
927 if (!options.progress)
928 argv_array_push(&args, "--no-progress");
930 argv_array_pushf(&args, "--depth=%lu", options.depth);
931 if (options.deepen_since)
932 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
933 for (i = 0; i < options.deepen_not.nr; i++)
934 argv_array_pushf(&args, "--shallow-exclude=%s",
935 options.deepen_not.items[i].string);
936 if (options.deepen_relative && options.depth)
937 argv_array_push(&args, "--deepen-relative");
938 if (options.from_promisor)
939 argv_array_push(&args, "--from-promisor");
940 if (options.no_dependents)
941 argv_array_push(&args, "--no-dependents");
943 argv_array_pushf(&args, "--filter=%s", options.filter);
944 argv_array_push(&args, url.buf);
946 for (i = 0; i < nr_heads; i++) {
947 struct ref *ref = to_fetch[i];
949 die("cannot fetch by sha1 over smart http");
950 packet_buf_write(&preamble, "%s %s\n",
951 oid_to_hex(&ref->old_oid), ref->name);
953 packet_buf_flush(&preamble);
955 memset(&rpc, 0, sizeof(rpc));
956 rpc.service_name = "git-upload-pack",
957 rpc.argv = args.argv;
958 rpc.stdin_preamble = &preamble;
959 rpc.gzip_request = 1;
961 err = rpc_service(&rpc, heads);
963 write_or_die(1, rpc.result.buf, rpc.result.len);
964 strbuf_release(&rpc.result);
965 strbuf_release(&preamble);
966 argv_array_clear(&args);
970 static int fetch(int nr_heads, struct ref **to_fetch)
972 struct discovery *d = discover_refs("git-upload-pack", 0);
974 return fetch_git(d, nr_heads, to_fetch);
976 return fetch_dumb(nr_heads, to_fetch);
979 static void parse_fetch(struct strbuf *buf)
981 struct ref **to_fetch = NULL;
982 struct ref *list_head = NULL;
983 struct ref **list = &list_head;
984 int alloc_heads = 0, nr_heads = 0;
988 if (skip_prefix(buf->buf, "fetch ", &p)) {
991 struct object_id old_oid;
993 if (get_oid_hex(p, &old_oid))
994 die("protocol error: expected sha/ref, got %s'", p);
995 if (p[GIT_SHA1_HEXSZ] == ' ')
996 name = p + GIT_SHA1_HEXSZ + 1;
997 else if (!p[GIT_SHA1_HEXSZ])
1000 die("protocol error: expected sha/ref, got %s'", p);
1002 ref = alloc_ref(name);
1003 oidcpy(&ref->old_oid, &old_oid);
1008 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1009 to_fetch[nr_heads++] = ref;
1012 die("http transport does not support %s", buf->buf);
1015 if (strbuf_getline_lf(buf, stdin) == EOF)
1021 if (fetch(nr_heads, to_fetch))
1022 exit(128); /* error already reported */
1023 free_refs(list_head);
1031 static int push_dav(int nr_spec, char **specs)
1033 struct child_process child = CHILD_PROCESS_INIT;
1037 argv_array_push(&child.args, "http-push");
1038 argv_array_push(&child.args, "--helper-status");
1039 if (options.dry_run)
1040 argv_array_push(&child.args, "--dry-run");
1041 if (options.verbosity > 1)
1042 argv_array_push(&child.args, "--verbose");
1043 argv_array_push(&child.args, url.buf);
1044 for (i = 0; i < nr_spec; i++)
1045 argv_array_push(&child.args, specs[i]);
1047 if (run_command(&child))
1048 die("git-http-push failed");
1052 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1054 struct rpc_state rpc;
1056 struct argv_array args;
1057 struct string_list_item *cas_option;
1058 struct strbuf preamble = STRBUF_INIT;
1060 argv_array_init(&args);
1061 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1065 argv_array_push(&args, "--thin");
1066 if (options.dry_run)
1067 argv_array_push(&args, "--dry-run");
1068 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1069 argv_array_push(&args, "--signed=yes");
1070 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1071 argv_array_push(&args, "--signed=if-asked");
1072 if (options.verbosity == 0)
1073 argv_array_push(&args, "--quiet");
1074 else if (options.verbosity > 1)
1075 argv_array_push(&args, "--verbose");
1076 for (i = 0; i < options.push_options.nr; i++)
1077 argv_array_pushf(&args, "--push-option=%s",
1078 options.push_options.items[i].string);
1079 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1080 for_each_string_list_item(cas_option, &cas_options)
1081 argv_array_push(&args, cas_option->string);
1082 argv_array_push(&args, url.buf);
1084 argv_array_push(&args, "--stdin");
1085 for (i = 0; i < nr_spec; i++)
1086 packet_buf_write(&preamble, "%s\n", specs[i]);
1087 packet_buf_flush(&preamble);
1089 memset(&rpc, 0, sizeof(rpc));
1090 rpc.service_name = "git-receive-pack",
1091 rpc.argv = args.argv;
1092 rpc.stdin_preamble = &preamble;
1094 err = rpc_service(&rpc, heads);
1096 write_or_die(1, rpc.result.buf, rpc.result.len);
1097 strbuf_release(&rpc.result);
1098 strbuf_release(&preamble);
1099 argv_array_clear(&args);
1103 static int push(int nr_spec, char **specs)
1105 struct discovery *heads = discover_refs("git-receive-pack", 1);
1108 if (heads->proto_git)
1109 ret = push_git(heads, nr_spec, specs);
1111 ret = push_dav(nr_spec, specs);
1112 free_discovery(heads);
1116 static void parse_push(struct strbuf *buf)
1118 char **specs = NULL;
1119 int alloc_spec = 0, nr_spec = 0, i, ret;
1122 if (starts_with(buf->buf, "push ")) {
1123 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1124 specs[nr_spec++] = xstrdup(buf->buf + 5);
1127 die("http transport does not support %s", buf->buf);
1130 if (strbuf_getline_lf(buf, stdin) == EOF)
1136 ret = push(nr_spec, specs);
1141 exit(128); /* error already reported */
1144 for (i = 0; i < nr_spec; i++)
1150 * Used to represent the state of a connection to an HTTP server when
1151 * communicating using git's wire-protocol version 2.
1153 struct proxy_state {
1156 struct curl_slist *headers;
1157 struct strbuf request_buffer;
1160 struct packet_reader reader;
1165 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1166 enum protocol_version version)
1168 struct strbuf buf = STRBUF_INIT;
1170 memset(p, 0, sizeof(*p));
1171 p->service_name = xstrdup(service_name);
1175 strbuf_init(&p->request_buffer, 0);
1177 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1178 p->service_url = strbuf_detach(&buf, NULL);
1180 p->headers = http_copy_default_headers();
1182 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1183 p->headers = curl_slist_append(p->headers, buf.buf);
1186 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1187 p->headers = curl_slist_append(p->headers, buf.buf);
1190 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1192 /* Add the Git-Protocol header */
1193 if (get_protocol_http_header(version, &buf))
1194 p->headers = curl_slist_append(p->headers, buf.buf);
1196 packet_reader_init(&p->reader, p->in, NULL, 0,
1197 PACKET_READ_GENTLE_ON_EOF);
1199 strbuf_release(&buf);
1202 static void proxy_state_clear(struct proxy_state *p)
1204 free(p->service_name);
1205 free(p->service_url);
1206 curl_slist_free_all(p->headers);
1207 strbuf_release(&p->request_buffer);
1211 * CURLOPT_READFUNCTION callback function.
1212 * Attempts to copy over a single packet-line at a time into the
1213 * curl provided buffer.
1215 static size_t proxy_in(char *buffer, size_t eltsize,
1216 size_t nmemb, void *userdata)
1219 struct proxy_state *p = userdata;
1220 size_t avail = p->request_buffer.len - p->pos;
1224 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1225 (uintmax_t)eltsize);
1229 if (p->seen_flush) {
1234 strbuf_reset(&p->request_buffer);
1235 switch (packet_reader_read(&p->reader)) {
1236 case PACKET_READ_EOF:
1237 die("unexpected EOF when reading from parent process");
1238 case PACKET_READ_NORMAL:
1239 packet_buf_write_len(&p->request_buffer, p->reader.line,
1242 case PACKET_READ_DELIM:
1243 packet_buf_delim(&p->request_buffer);
1245 case PACKET_READ_FLUSH:
1246 packet_buf_flush(&p->request_buffer);
1251 avail = p->request_buffer.len;
1256 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1261 static size_t proxy_out(char *buffer, size_t eltsize,
1262 size_t nmemb, void *userdata)
1265 struct proxy_state *p = userdata;
1268 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1269 (uintmax_t)eltsize);
1272 write_or_die(p->out, buffer, size);
1276 /* Issues a request to the HTTP server configured in `p` */
1277 static int proxy_request(struct proxy_state *p)
1279 struct active_request_slot *slot;
1281 slot = get_active_slot();
1283 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
1284 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1285 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1286 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1287 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1289 /* Setup function to read request from client */
1290 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1291 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1293 /* Setup function to write server response to client */
1294 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1295 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1297 if (run_slot(slot, NULL) != HTTP_OK)
1303 static int stateless_connect(const char *service_name)
1305 struct discovery *discover;
1306 struct proxy_state p;
1309 * Run the info/refs request and see if the server supports protocol
1310 * v2. If and only if the server supports v2 can we successfully
1311 * establish a stateless connection, otherwise we need to tell the
1312 * client to fallback to using other transport helper functions to
1313 * complete their request.
1315 discover = discover_refs(service_name, 0);
1316 if (discover->version != protocol_v2) {
1317 printf("fallback\n");
1321 /* Stateless Connection established */
1326 proxy_state_init(&p, service_name, discover->version);
1329 * Dump the capability listing that we got from the server earlier
1330 * during the info/refs request.
1332 write_or_die(p.out, discover->buf, discover->len);
1334 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1335 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1336 if (proxy_request(&p)) {
1337 /* We would have an err here */
1342 proxy_state_clear(&p);
1346 int cmd_main(int argc, const char **argv)
1348 struct strbuf buf = STRBUF_INIT;
1351 setup_git_directory_gently(&nongit);
1353 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1357 options.verbosity = 1;
1358 options.progress = !!isatty(2);
1360 string_list_init(&options.deepen_not, 1);
1361 string_list_init(&options.push_options, 1);
1363 remote = remote_get(argv[1]);
1366 end_url_with_slash(&url, argv[2]);
1368 end_url_with_slash(&url, remote->url[0]);
1371 http_init(remote, url.buf, 0);
1376 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1378 error("remote-curl: error reading command stream from git");
1383 if (starts_with(buf.buf, "fetch ")) {
1385 die("remote-curl: fetch attempted without a local repo");
1388 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1389 int for_push = !!strstr(buf.buf + 4, "for-push");
1390 output_refs(get_refs(for_push));
1392 } else if (starts_with(buf.buf, "push ")) {
1395 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1396 char *value = strchr(arg, ' ');
1404 result = set_option(arg, value);
1407 else if (result < 0)
1408 printf("error invalid value\n");
1410 printf("unsupported\n");
1413 } else if (!strcmp(buf.buf, "capabilities")) {
1414 printf("stateless-connect\n");
1418 printf("check-connectivity\n");
1421 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1422 if (!stateless_connect(arg))
1425 error("remote-curl: unknown command '%s' from git", buf.buf);