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"
19 static struct remote *remote;
20 /* always ends with a trailing slash */
21 static struct strbuf url = STRBUF_INIT;
27 struct string_list deepen_not;
28 struct string_list push_options;
29 unsigned progress : 1,
30 check_self_contained_and_connected : 1,
36 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 static struct options options;
41 static struct string_list cas_options = STRING_LIST_INIT_DUP;
43 static int set_option(const char *name, const char *value)
45 if (!strcmp(name, "verbosity")) {
47 int v = strtol(value, &end, 10);
48 if (value == end || *end)
50 options.verbosity = v;
53 else if (!strcmp(name, "progress")) {
54 if (!strcmp(value, "true"))
56 else if (!strcmp(value, "false"))
62 else if (!strcmp(name, "depth")) {
64 unsigned long v = strtoul(value, &end, 10);
65 if (value == end || *end)
70 else if (!strcmp(name, "deepen-since")) {
71 options.deepen_since = xstrdup(value);
74 else if (!strcmp(name, "deepen-not")) {
75 string_list_append(&options.deepen_not, value);
78 else if (!strcmp(name, "deepen-relative")) {
79 if (!strcmp(value, "true"))
80 options.deepen_relative = 1;
81 else if (!strcmp(value, "false"))
82 options.deepen_relative = 0;
87 else if (!strcmp(name, "followtags")) {
88 if (!strcmp(value, "true"))
89 options.followtags = 1;
90 else if (!strcmp(value, "false"))
91 options.followtags = 0;
96 else if (!strcmp(name, "dry-run")) {
97 if (!strcmp(value, "true"))
99 else if (!strcmp(value, "false"))
105 else if (!strcmp(name, "check-connectivity")) {
106 if (!strcmp(value, "true"))
107 options.check_self_contained_and_connected = 1;
108 else if (!strcmp(value, "false"))
109 options.check_self_contained_and_connected = 0;
114 else if (!strcmp(name, "cas")) {
115 struct strbuf val = STRBUF_INIT;
116 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
117 string_list_append(&cas_options, val.buf);
118 strbuf_release(&val);
120 } else if (!strcmp(name, "cloning")) {
121 if (!strcmp(value, "true"))
123 else if (!strcmp(value, "false"))
128 } else if (!strcmp(name, "update-shallow")) {
129 if (!strcmp(value, "true"))
130 options.update_shallow = 1;
131 else if (!strcmp(value, "false"))
132 options.update_shallow = 0;
136 } else if (!strcmp(name, "pushcert")) {
137 if (!strcmp(value, "true"))
138 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
139 else if (!strcmp(value, "false"))
140 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
141 else if (!strcmp(value, "if-asked"))
142 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
146 } else if (!strcmp(name, "push-option")) {
147 string_list_append(&options.push_options, value);
150 #if LIBCURL_VERSION_NUM >= 0x070a08
151 } else if (!strcmp(name, "family")) {
152 if (!strcmp(value, "ipv4"))
153 git_curl_ipresolve = CURL_IPRESOLVE_V4;
154 else if (!strcmp(value, "ipv6"))
155 git_curl_ipresolve = CURL_IPRESOLVE_V6;
156 else if (!strcmp(value, "all"))
157 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
161 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
163 return 1 /* unsupported */;
173 struct oid_array shallow;
174 enum protocol_version version;
175 unsigned proto_git : 1;
177 static struct discovery *last_discovery;
179 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
181 struct ref *list = NULL;
182 struct packet_reader reader;
184 packet_reader_init(&reader, -1, heads->buf, heads->len,
185 PACKET_READ_CHOMP_NEWLINE |
186 PACKET_READ_GENTLE_ON_EOF);
188 heads->version = discover_version(&reader);
189 switch (heads->version) {
191 die("support for protocol v2 not implemented yet");
195 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
196 NULL, &heads->shallow);
198 case protocol_unknown_version:
199 BUG("unknown protocol version");
205 static struct ref *parse_info_refs(struct discovery *heads)
207 char *data, *start, *mid;
211 struct ref *refs = NULL;
212 struct ref *ref = NULL;
213 struct ref *last_ref = NULL;
218 while (i < heads->len) {
224 if (data[i] == '\n') {
225 if (mid - start != 40)
226 die("%sinfo/refs not valid: is this a git repository?",
230 ref = alloc_ref(ref_name);
231 get_oid_hex(start, &ref->old_oid);
235 last_ref->next = ref;
242 ref = alloc_ref("HEAD");
243 if (!http_fetch_ref(url.buf, ref) &&
244 !resolve_remote_symref(ref, refs)) {
254 static void free_discovery(struct discovery *d)
257 if (d == last_discovery)
258 last_discovery = NULL;
259 free(d->shallow.oid);
267 static int show_http_message(struct strbuf *type, struct strbuf *charset,
273 * We only show text/plain parts, as other types are likely
274 * to be ugly to look at on the user's terminal.
276 if (strcmp(type->buf, "text/plain"))
279 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
287 eol = strchrnul(p, '\n');
288 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
294 static int get_protocol_http_header(enum protocol_version version,
295 struct strbuf *header)
298 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
307 static struct discovery *discover_refs(const char *service, int for_push)
309 struct strbuf exp = STRBUF_INIT;
310 struct strbuf type = STRBUF_INIT;
311 struct strbuf charset = STRBUF_INIT;
312 struct strbuf buffer = STRBUF_INIT;
313 struct strbuf refs_url = STRBUF_INIT;
314 struct strbuf effective_url = STRBUF_INIT;
315 struct strbuf protocol_header = STRBUF_INIT;
316 struct string_list extra_headers = STRING_LIST_INIT_DUP;
317 struct discovery *last = last_discovery;
318 int http_ret, maybe_smart = 0;
319 struct http_get_options http_options;
321 if (last && !strcmp(service, last->service))
323 free_discovery(last);
325 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
326 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
327 git_env_bool("GIT_SMART_HTTP", 1)) {
329 if (!strchr(url.buf, '?'))
330 strbuf_addch(&refs_url, '?');
332 strbuf_addch(&refs_url, '&');
333 strbuf_addf(&refs_url, "service=%s", service);
336 /* Add the extra Git-Protocol header */
337 if (get_protocol_http_header(get_protocol_version_config(), &protocol_header))
338 string_list_append(&extra_headers, protocol_header.buf);
340 memset(&http_options, 0, sizeof(http_options));
341 http_options.content_type = &type;
342 http_options.charset = &charset;
343 http_options.effective_url = &effective_url;
344 http_options.base_url = &url;
345 http_options.extra_headers = &extra_headers;
346 http_options.initial_request = 1;
347 http_options.no_cache = 1;
348 http_options.keep_error = 1;
350 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
354 case HTTP_MISSING_TARGET:
355 show_http_message(&type, &charset, &buffer);
356 die("repository '%s' not found", url.buf);
358 show_http_message(&type, &charset, &buffer);
359 die("Authentication failed for '%s'", url.buf);
361 show_http_message(&type, &charset, &buffer);
362 die("unable to access '%s': %s", url.buf, curl_errorstr);
365 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
366 warning(_("redirecting to %s"), url.buf);
368 last= xcalloc(1, sizeof(*last_discovery));
369 last->service = xstrdup(service);
370 last->buf_alloc = strbuf_detach(&buffer, &last->len);
371 last->buf = last->buf_alloc;
373 strbuf_addf(&exp, "application/x-%s-advertisement", service);
375 (5 <= last->len && last->buf[4] == '#') &&
376 !strbuf_cmp(&exp, &type)) {
380 * smart HTTP response; validate that the service
381 * pkt-line matches our request.
383 line = packet_read_line_buf(&last->buf, &last->len, NULL);
386 strbuf_addf(&exp, "# service=%s", service);
387 if (strcmp(line, exp.buf))
388 die("invalid server response; got '%s'", line);
389 strbuf_release(&exp);
391 /* The header can include additional metadata lines, up
392 * until a packet flush marker. Ignore these now, but
393 * in the future we might start to scan them.
395 while (packet_read_line_buf(&last->buf, &last->len, NULL))
402 last->refs = parse_git_refs(last, for_push);
404 last->refs = parse_info_refs(last);
406 strbuf_release(&refs_url);
407 strbuf_release(&exp);
408 strbuf_release(&type);
409 strbuf_release(&charset);
410 strbuf_release(&effective_url);
411 strbuf_release(&buffer);
412 strbuf_release(&protocol_header);
413 string_list_clear(&extra_headers, 0);
414 last_discovery = last;
418 static struct ref *get_refs(int for_push)
420 struct discovery *heads;
423 heads = discover_refs("git-receive-pack", for_push);
425 heads = discover_refs("git-upload-pack", for_push);
430 static void output_refs(struct ref *refs)
433 for (posn = refs; posn; posn = posn->next) {
435 printf("@%s %s\n", posn->symref, posn->name);
437 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
444 const char *service_name;
446 struct strbuf *stdin_preamble;
448 char *hdr_content_type;
450 char *protocol_header;
458 struct strbuf result;
459 unsigned gzip_request : 1;
460 unsigned initial_buffer : 1;
463 static size_t rpc_out(void *ptr, size_t eltsize,
464 size_t nmemb, void *buffer_)
466 size_t max = eltsize * nmemb;
467 struct rpc_state *rpc = buffer_;
468 size_t avail = rpc->len - rpc->pos;
471 rpc->initial_buffer = 0;
472 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
481 memcpy(ptr, rpc->buf + rpc->pos, avail);
486 #ifndef NO_CURL_IOCTL
487 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
489 struct rpc_state *rpc = clientp;
495 case CURLIOCMD_RESTARTREAD:
496 if (rpc->initial_buffer) {
500 error("unable to rewind rpc post data - try increasing http.postBuffer");
501 return CURLIOE_FAILRESTART;
504 return CURLIOE_UNKNOWNCMD;
509 static size_t rpc_in(char *ptr, size_t eltsize,
510 size_t nmemb, void *buffer_)
512 size_t size = eltsize * nmemb;
513 struct rpc_state *rpc = buffer_;
515 rpc->any_written = 1;
516 write_or_die(rpc->in, ptr, size);
520 static int run_slot(struct active_request_slot *slot,
521 struct slot_results *results)
524 struct slot_results results_buf;
527 results = &results_buf;
529 err = run_one_slot(slot, results);
531 if (err != HTTP_OK && err != HTTP_REAUTH) {
532 struct strbuf msg = STRBUF_INIT;
533 if (results->http_code && results->http_code != 200)
534 strbuf_addf(&msg, "HTTP %ld", results->http_code);
535 if (results->curl_result != CURLE_OK) {
537 strbuf_addch(&msg, ' ');
538 strbuf_addf(&msg, "curl %d", results->curl_result);
539 if (curl_errorstr[0]) {
540 strbuf_addch(&msg, ' ');
541 strbuf_addstr(&msg, curl_errorstr);
544 error("RPC failed; %s", msg.buf);
545 strbuf_release(&msg);
551 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
553 struct active_request_slot *slot;
554 struct curl_slist *headers = http_copy_default_headers();
555 struct strbuf buf = STRBUF_INIT;
558 slot = get_active_slot();
560 headers = curl_slist_append(headers, rpc->hdr_content_type);
561 headers = curl_slist_append(headers, rpc->hdr_accept);
563 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
564 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
565 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
566 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
567 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
568 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
569 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
570 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
571 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
573 err = run_slot(slot, results);
575 curl_slist_free_all(headers);
576 strbuf_release(&buf);
580 static curl_off_t xcurl_off_t(ssize_t len) {
581 if (len > maximum_signed_value_of_type(curl_off_t))
582 die("cannot handle pushes this big");
583 return (curl_off_t) len;
586 static int post_rpc(struct rpc_state *rpc)
588 struct active_request_slot *slot;
589 struct curl_slist *headers = http_copy_default_headers();
590 int use_gzip = rpc->gzip_request;
591 char *gzip_body = NULL;
592 size_t gzip_size = 0;
593 int err, large_request = 0;
594 int needs_100_continue = 0;
596 /* Try to load the entire request, if we can fit it into the
597 * allocated buffer space we can use HTTP/1.0 and avoid the
598 * chunked encoding mess.
601 size_t left = rpc->alloc - rpc->len;
602 char *buf = rpc->buf + rpc->len;
605 if (left < LARGE_PACKET_MAX) {
611 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
618 struct slot_results results;
621 err = probe_rpc(rpc, &results);
622 if (err == HTTP_REAUTH)
623 credential_fill(&http_auth);
624 } while (err == HTTP_REAUTH);
628 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
629 needs_100_continue = 1;
632 headers = curl_slist_append(headers, rpc->hdr_content_type);
633 headers = curl_slist_append(headers, rpc->hdr_accept);
634 headers = curl_slist_append(headers, needs_100_continue ?
635 "Expect: 100-continue" : "Expect:");
637 /* Add the extra Git-Protocol header */
638 if (rpc->protocol_header)
639 headers = curl_slist_append(headers, rpc->protocol_header);
642 slot = get_active_slot();
644 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
645 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
646 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
647 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
650 /* The request body is large and the size cannot be predicted.
651 * We must use chunked encoding to send it.
653 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
654 rpc->initial_buffer = 1;
655 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
656 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
657 #ifndef NO_CURL_IOCTL
658 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
659 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
661 if (options.verbosity > 1) {
662 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
666 } else if (gzip_body) {
668 * If we are looping to retry authentication, then the previous
669 * run will have set up the headers and gzip buffer already,
670 * and we just need to send it.
672 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
673 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
675 } else if (use_gzip && 1024 < rpc->len) {
676 /* The client backend isn't giving us compressed data so
677 * we can try to deflate it ourselves, this may save on.
683 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
684 gzip_size = git_deflate_bound(&stream, rpc->len);
685 gzip_body = xmalloc(gzip_size);
687 stream.next_in = (unsigned char *)rpc->buf;
688 stream.avail_in = rpc->len;
689 stream.next_out = (unsigned char *)gzip_body;
690 stream.avail_out = gzip_size;
692 ret = git_deflate(&stream, Z_FINISH);
693 if (ret != Z_STREAM_END)
694 die("cannot deflate request; zlib deflate error %d", ret);
696 ret = git_deflate_end_gently(&stream);
698 die("cannot deflate request; zlib end error %d", ret);
700 gzip_size = stream.total_out;
702 headers = curl_slist_append(headers, "Content-Encoding: gzip");
703 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
704 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
706 if (options.verbosity > 1) {
707 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
709 (unsigned long)rpc->len, (unsigned long)gzip_size);
713 /* We know the complete request size in advance, use the
714 * more normal Content-Length approach.
716 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
717 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
718 if (options.verbosity > 1) {
719 fprintf(stderr, "POST %s (%lu bytes)\n",
720 rpc->service_name, (unsigned long)rpc->len);
725 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
726 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
727 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
730 rpc->any_written = 0;
731 err = run_slot(slot, NULL);
732 if (err == HTTP_REAUTH && !large_request) {
733 credential_fill(&http_auth);
739 if (!rpc->any_written)
742 curl_slist_free_all(headers);
747 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
749 const char *svc = rpc->service_name;
750 struct strbuf buf = STRBUF_INIT;
751 struct strbuf *preamble = rpc->stdin_preamble;
752 struct child_process client = CHILD_PROCESS_INIT;
758 client.argv = rpc->argv;
759 if (start_command(&client))
762 write_or_die(client.in, preamble->buf, preamble->len);
764 write_or_die(client.in, heads->buf, heads->len);
766 rpc->alloc = http_post_buffer;
767 rpc->buf = xmalloc(rpc->alloc);
769 rpc->out = client.out;
770 strbuf_init(&rpc->result, 0);
772 strbuf_addf(&buf, "%s%s", url.buf, svc);
773 rpc->service_url = strbuf_detach(&buf, NULL);
775 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
776 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
778 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
779 rpc->hdr_accept = strbuf_detach(&buf, NULL);
781 if (get_protocol_http_header(heads->version, &buf))
782 rpc->protocol_header = strbuf_detach(&buf, NULL);
784 rpc->protocol_header = NULL;
787 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
792 err |= post_rpc(rpc);
798 strbuf_read(&rpc->result, client.out, 0);
802 if (xread(client.out, buf, sizeof(buf)) <= 0)
809 err |= finish_command(&client);
810 free(rpc->service_url);
811 free(rpc->hdr_content_type);
812 free(rpc->hdr_accept);
813 free(rpc->protocol_header);
815 strbuf_release(&buf);
819 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
821 struct walker *walker;
825 ALLOC_ARRAY(targets, nr_heads);
826 if (options.depth || options.deepen_since)
827 die("dumb http transport does not support shallow capabilities");
828 for (i = 0; i < nr_heads; i++)
829 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
831 walker = get_http_walker(url.buf);
833 walker->get_tree = 1;
834 walker->get_history = 1;
835 walker->get_verbosely = options.verbosity >= 3;
836 walker->get_recover = 0;
837 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
840 for (i = 0; i < nr_heads; i++)
844 return ret ? error("fetch failed.") : 0;
847 static int fetch_git(struct discovery *heads,
848 int nr_heads, struct ref **to_fetch)
850 struct rpc_state rpc;
851 struct strbuf preamble = STRBUF_INIT;
853 struct argv_array args = ARGV_ARRAY_INIT;
855 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
856 "--stdin", "--lock-pack", NULL);
857 if (options.followtags)
858 argv_array_push(&args, "--include-tag");
860 argv_array_push(&args, "--thin");
861 if (options.verbosity >= 3)
862 argv_array_pushl(&args, "-v", "-v", NULL);
863 if (options.check_self_contained_and_connected)
864 argv_array_push(&args, "--check-self-contained-and-connected");
866 argv_array_push(&args, "--cloning");
867 if (options.update_shallow)
868 argv_array_push(&args, "--update-shallow");
869 if (!options.progress)
870 argv_array_push(&args, "--no-progress");
872 argv_array_pushf(&args, "--depth=%lu", options.depth);
873 if (options.deepen_since)
874 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
875 for (i = 0; i < options.deepen_not.nr; i++)
876 argv_array_pushf(&args, "--shallow-exclude=%s",
877 options.deepen_not.items[i].string);
878 if (options.deepen_relative && options.depth)
879 argv_array_push(&args, "--deepen-relative");
880 argv_array_push(&args, url.buf);
882 for (i = 0; i < nr_heads; i++) {
883 struct ref *ref = to_fetch[i];
885 die("cannot fetch by sha1 over smart http");
886 packet_buf_write(&preamble, "%s %s\n",
887 oid_to_hex(&ref->old_oid), ref->name);
889 packet_buf_flush(&preamble);
891 memset(&rpc, 0, sizeof(rpc));
892 rpc.service_name = "git-upload-pack",
893 rpc.argv = args.argv;
894 rpc.stdin_preamble = &preamble;
895 rpc.gzip_request = 1;
897 err = rpc_service(&rpc, heads);
899 write_or_die(1, rpc.result.buf, rpc.result.len);
900 strbuf_release(&rpc.result);
901 strbuf_release(&preamble);
902 argv_array_clear(&args);
906 static int fetch(int nr_heads, struct ref **to_fetch)
908 struct discovery *d = discover_refs("git-upload-pack", 0);
910 return fetch_git(d, nr_heads, to_fetch);
912 return fetch_dumb(nr_heads, to_fetch);
915 static void parse_fetch(struct strbuf *buf)
917 struct ref **to_fetch = NULL;
918 struct ref *list_head = NULL;
919 struct ref **list = &list_head;
920 int alloc_heads = 0, nr_heads = 0;
924 if (skip_prefix(buf->buf, "fetch ", &p)) {
927 struct object_id old_oid;
929 if (get_oid_hex(p, &old_oid))
930 die("protocol error: expected sha/ref, got %s'", p);
931 if (p[GIT_SHA1_HEXSZ] == ' ')
932 name = p + GIT_SHA1_HEXSZ + 1;
933 else if (!p[GIT_SHA1_HEXSZ])
936 die("protocol error: expected sha/ref, got %s'", p);
938 ref = alloc_ref(name);
939 oidcpy(&ref->old_oid, &old_oid);
944 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
945 to_fetch[nr_heads++] = ref;
948 die("http transport does not support %s", buf->buf);
951 if (strbuf_getline_lf(buf, stdin) == EOF)
957 if (fetch(nr_heads, to_fetch))
958 exit(128); /* error already reported */
959 free_refs(list_head);
967 static int push_dav(int nr_spec, char **specs)
969 struct child_process child = CHILD_PROCESS_INIT;
973 argv_array_push(&child.args, "http-push");
974 argv_array_push(&child.args, "--helper-status");
976 argv_array_push(&child.args, "--dry-run");
977 if (options.verbosity > 1)
978 argv_array_push(&child.args, "--verbose");
979 argv_array_push(&child.args, url.buf);
980 for (i = 0; i < nr_spec; i++)
981 argv_array_push(&child.args, specs[i]);
983 if (run_command(&child))
984 die("git-http-push failed");
988 static int push_git(struct discovery *heads, int nr_spec, char **specs)
990 struct rpc_state rpc;
992 struct argv_array args;
993 struct string_list_item *cas_option;
994 struct strbuf preamble = STRBUF_INIT;
996 argv_array_init(&args);
997 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1001 argv_array_push(&args, "--thin");
1002 if (options.dry_run)
1003 argv_array_push(&args, "--dry-run");
1004 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1005 argv_array_push(&args, "--signed=yes");
1006 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1007 argv_array_push(&args, "--signed=if-asked");
1008 if (options.verbosity == 0)
1009 argv_array_push(&args, "--quiet");
1010 else if (options.verbosity > 1)
1011 argv_array_push(&args, "--verbose");
1012 for (i = 0; i < options.push_options.nr; i++)
1013 argv_array_pushf(&args, "--push-option=%s",
1014 options.push_options.items[i].string);
1015 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1016 for_each_string_list_item(cas_option, &cas_options)
1017 argv_array_push(&args, cas_option->string);
1018 argv_array_push(&args, url.buf);
1020 argv_array_push(&args, "--stdin");
1021 for (i = 0; i < nr_spec; i++)
1022 packet_buf_write(&preamble, "%s\n", specs[i]);
1023 packet_buf_flush(&preamble);
1025 memset(&rpc, 0, sizeof(rpc));
1026 rpc.service_name = "git-receive-pack",
1027 rpc.argv = args.argv;
1028 rpc.stdin_preamble = &preamble;
1030 err = rpc_service(&rpc, heads);
1032 write_or_die(1, rpc.result.buf, rpc.result.len);
1033 strbuf_release(&rpc.result);
1034 strbuf_release(&preamble);
1035 argv_array_clear(&args);
1039 static int push(int nr_spec, char **specs)
1041 struct discovery *heads = discover_refs("git-receive-pack", 1);
1044 if (heads->proto_git)
1045 ret = push_git(heads, nr_spec, specs);
1047 ret = push_dav(nr_spec, specs);
1048 free_discovery(heads);
1052 static void parse_push(struct strbuf *buf)
1054 char **specs = NULL;
1055 int alloc_spec = 0, nr_spec = 0, i, ret;
1058 if (starts_with(buf->buf, "push ")) {
1059 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1060 specs[nr_spec++] = xstrdup(buf->buf + 5);
1063 die("http transport does not support %s", buf->buf);
1066 if (strbuf_getline_lf(buf, stdin) == EOF)
1072 ret = push(nr_spec, specs);
1077 exit(128); /* error already reported */
1080 for (i = 0; i < nr_spec; i++)
1085 int cmd_main(int argc, const char **argv)
1087 struct strbuf buf = STRBUF_INIT;
1090 setup_git_directory_gently(&nongit);
1092 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1096 options.verbosity = 1;
1097 options.progress = !!isatty(2);
1099 string_list_init(&options.deepen_not, 1);
1100 string_list_init(&options.push_options, 1);
1102 remote = remote_get(argv[1]);
1105 end_url_with_slash(&url, argv[2]);
1107 end_url_with_slash(&url, remote->url[0]);
1110 http_init(remote, url.buf, 0);
1115 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1117 error("remote-curl: error reading command stream from git");
1122 if (starts_with(buf.buf, "fetch ")) {
1124 die("remote-curl: fetch attempted without a local repo");
1127 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1128 int for_push = !!strstr(buf.buf + 4, "for-push");
1129 output_refs(get_refs(for_push));
1131 } else if (starts_with(buf.buf, "push ")) {
1134 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1135 char *value = strchr(arg, ' ');
1143 result = set_option(arg, value);
1146 else if (result < 0)
1147 printf("error invalid value\n");
1149 printf("unsupported\n");
1152 } else if (!strcmp(buf.buf, "capabilities")) {
1156 printf("check-connectivity\n");
1160 error("remote-curl: unknown command '%s' from git", buf.buf);