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 unsigned proto_git : 1;
176 static struct discovery *last_discovery;
178 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
180 struct ref *list = NULL;
181 struct packet_reader reader;
183 packet_reader_init(&reader, -1, heads->buf, heads->len,
184 PACKET_READ_CHOMP_NEWLINE |
185 PACKET_READ_GENTLE_ON_EOF);
187 switch (discover_version(&reader)) {
189 die("support for protocol v2 not implemented yet");
193 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
194 NULL, &heads->shallow);
196 case protocol_unknown_version:
197 BUG("unknown protocol version");
203 static struct ref *parse_info_refs(struct discovery *heads)
205 char *data, *start, *mid;
209 struct ref *refs = NULL;
210 struct ref *ref = NULL;
211 struct ref *last_ref = NULL;
216 while (i < heads->len) {
222 if (data[i] == '\n') {
223 if (mid - start != 40)
224 die("%sinfo/refs not valid: is this a git repository?",
228 ref = alloc_ref(ref_name);
229 get_oid_hex(start, &ref->old_oid);
233 last_ref->next = ref;
240 ref = alloc_ref("HEAD");
241 if (!http_fetch_ref(url.buf, ref) &&
242 !resolve_remote_symref(ref, refs)) {
252 static void free_discovery(struct discovery *d)
255 if (d == last_discovery)
256 last_discovery = NULL;
257 free(d->shallow.oid);
264 static int show_http_message(struct strbuf *type, struct strbuf *charset,
270 * We only show text/plain parts, as other types are likely
271 * to be ugly to look at on the user's terminal.
273 if (strcmp(type->buf, "text/plain"))
276 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
284 eol = strchrnul(p, '\n');
285 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
291 static struct discovery *discover_refs(const char *service, int for_push)
293 struct strbuf exp = STRBUF_INIT;
294 struct strbuf type = STRBUF_INIT;
295 struct strbuf charset = STRBUF_INIT;
296 struct strbuf buffer = STRBUF_INIT;
297 struct strbuf refs_url = STRBUF_INIT;
298 struct strbuf effective_url = STRBUF_INIT;
299 struct discovery *last = last_discovery;
300 int http_ret, maybe_smart = 0;
301 struct http_get_options http_options;
303 if (last && !strcmp(service, last->service))
305 free_discovery(last);
307 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
308 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
309 git_env_bool("GIT_SMART_HTTP", 1)) {
311 if (!strchr(url.buf, '?'))
312 strbuf_addch(&refs_url, '?');
314 strbuf_addch(&refs_url, '&');
315 strbuf_addf(&refs_url, "service=%s", service);
318 memset(&http_options, 0, sizeof(http_options));
319 http_options.content_type = &type;
320 http_options.charset = &charset;
321 http_options.effective_url = &effective_url;
322 http_options.base_url = &url;
323 http_options.initial_request = 1;
324 http_options.no_cache = 1;
325 http_options.keep_error = 1;
327 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
331 case HTTP_MISSING_TARGET:
332 show_http_message(&type, &charset, &buffer);
333 die("repository '%s' not found", url.buf);
335 show_http_message(&type, &charset, &buffer);
336 die("Authentication failed for '%s'", url.buf);
338 show_http_message(&type, &charset, &buffer);
339 die("unable to access '%s': %s", url.buf, curl_errorstr);
342 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
343 warning(_("redirecting to %s"), url.buf);
345 last= xcalloc(1, sizeof(*last_discovery));
346 last->service = service;
347 last->buf_alloc = strbuf_detach(&buffer, &last->len);
348 last->buf = last->buf_alloc;
350 strbuf_addf(&exp, "application/x-%s-advertisement", service);
352 (5 <= last->len && last->buf[4] == '#') &&
353 !strbuf_cmp(&exp, &type)) {
357 * smart HTTP response; validate that the service
358 * pkt-line matches our request.
360 line = packet_read_line_buf(&last->buf, &last->len, NULL);
363 strbuf_addf(&exp, "# service=%s", service);
364 if (strcmp(line, exp.buf))
365 die("invalid server response; got '%s'", line);
366 strbuf_release(&exp);
368 /* The header can include additional metadata lines, up
369 * until a packet flush marker. Ignore these now, but
370 * in the future we might start to scan them.
372 while (packet_read_line_buf(&last->buf, &last->len, NULL))
379 last->refs = parse_git_refs(last, for_push);
381 last->refs = parse_info_refs(last);
383 strbuf_release(&refs_url);
384 strbuf_release(&exp);
385 strbuf_release(&type);
386 strbuf_release(&charset);
387 strbuf_release(&effective_url);
388 strbuf_release(&buffer);
389 last_discovery = last;
393 static struct ref *get_refs(int for_push)
395 struct discovery *heads;
398 heads = discover_refs("git-receive-pack", for_push);
400 heads = discover_refs("git-upload-pack", for_push);
405 static void output_refs(struct ref *refs)
408 for (posn = refs; posn; posn = posn->next) {
410 printf("@%s %s\n", posn->symref, posn->name);
412 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
419 const char *service_name;
421 struct strbuf *stdin_preamble;
423 char *hdr_content_type;
432 struct strbuf result;
433 unsigned gzip_request : 1;
434 unsigned initial_buffer : 1;
437 static size_t rpc_out(void *ptr, size_t eltsize,
438 size_t nmemb, void *buffer_)
440 size_t max = eltsize * nmemb;
441 struct rpc_state *rpc = buffer_;
442 size_t avail = rpc->len - rpc->pos;
445 rpc->initial_buffer = 0;
446 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
455 memcpy(ptr, rpc->buf + rpc->pos, avail);
460 #ifndef NO_CURL_IOCTL
461 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
463 struct rpc_state *rpc = clientp;
469 case CURLIOCMD_RESTARTREAD:
470 if (rpc->initial_buffer) {
474 error("unable to rewind rpc post data - try increasing http.postBuffer");
475 return CURLIOE_FAILRESTART;
478 return CURLIOE_UNKNOWNCMD;
483 static size_t rpc_in(char *ptr, size_t eltsize,
484 size_t nmemb, void *buffer_)
486 size_t size = eltsize * nmemb;
487 struct rpc_state *rpc = buffer_;
489 rpc->any_written = 1;
490 write_or_die(rpc->in, ptr, size);
494 static int run_slot(struct active_request_slot *slot,
495 struct slot_results *results)
498 struct slot_results results_buf;
501 results = &results_buf;
503 err = run_one_slot(slot, results);
505 if (err != HTTP_OK && err != HTTP_REAUTH) {
506 struct strbuf msg = STRBUF_INIT;
507 if (results->http_code && results->http_code != 200)
508 strbuf_addf(&msg, "HTTP %ld", results->http_code);
509 if (results->curl_result != CURLE_OK) {
511 strbuf_addch(&msg, ' ');
512 strbuf_addf(&msg, "curl %d", results->curl_result);
513 if (curl_errorstr[0]) {
514 strbuf_addch(&msg, ' ');
515 strbuf_addstr(&msg, curl_errorstr);
518 error("RPC failed; %s", msg.buf);
519 strbuf_release(&msg);
525 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
527 struct active_request_slot *slot;
528 struct curl_slist *headers = http_copy_default_headers();
529 struct strbuf buf = STRBUF_INIT;
532 slot = get_active_slot();
534 headers = curl_slist_append(headers, rpc->hdr_content_type);
535 headers = curl_slist_append(headers, rpc->hdr_accept);
537 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
538 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
539 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
540 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
541 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
542 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
543 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
544 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
545 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
547 err = run_slot(slot, results);
549 curl_slist_free_all(headers);
550 strbuf_release(&buf);
554 static curl_off_t xcurl_off_t(ssize_t len) {
555 if (len > maximum_signed_value_of_type(curl_off_t))
556 die("cannot handle pushes this big");
557 return (curl_off_t) len;
560 static int post_rpc(struct rpc_state *rpc)
562 struct active_request_slot *slot;
563 struct curl_slist *headers = http_copy_default_headers();
564 int use_gzip = rpc->gzip_request;
565 char *gzip_body = NULL;
566 size_t gzip_size = 0;
567 int err, large_request = 0;
568 int needs_100_continue = 0;
570 /* Try to load the entire request, if we can fit it into the
571 * allocated buffer space we can use HTTP/1.0 and avoid the
572 * chunked encoding mess.
575 size_t left = rpc->alloc - rpc->len;
576 char *buf = rpc->buf + rpc->len;
579 if (left < LARGE_PACKET_MAX) {
585 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
592 struct slot_results results;
595 err = probe_rpc(rpc, &results);
596 if (err == HTTP_REAUTH)
597 credential_fill(&http_auth);
598 } while (err == HTTP_REAUTH);
602 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
603 needs_100_continue = 1;
606 headers = curl_slist_append(headers, rpc->hdr_content_type);
607 headers = curl_slist_append(headers, rpc->hdr_accept);
608 headers = curl_slist_append(headers, needs_100_continue ?
609 "Expect: 100-continue" : "Expect:");
612 slot = get_active_slot();
614 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
615 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
616 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
617 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
620 /* The request body is large and the size cannot be predicted.
621 * We must use chunked encoding to send it.
623 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
624 rpc->initial_buffer = 1;
625 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
626 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
627 #ifndef NO_CURL_IOCTL
628 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
629 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
631 if (options.verbosity > 1) {
632 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
636 } else if (gzip_body) {
638 * If we are looping to retry authentication, then the previous
639 * run will have set up the headers and gzip buffer already,
640 * and we just need to send it.
642 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
643 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
645 } else if (use_gzip && 1024 < rpc->len) {
646 /* The client backend isn't giving us compressed data so
647 * we can try to deflate it ourselves, this may save on.
653 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
654 gzip_size = git_deflate_bound(&stream, rpc->len);
655 gzip_body = xmalloc(gzip_size);
657 stream.next_in = (unsigned char *)rpc->buf;
658 stream.avail_in = rpc->len;
659 stream.next_out = (unsigned char *)gzip_body;
660 stream.avail_out = gzip_size;
662 ret = git_deflate(&stream, Z_FINISH);
663 if (ret != Z_STREAM_END)
664 die("cannot deflate request; zlib deflate error %d", ret);
666 ret = git_deflate_end_gently(&stream);
668 die("cannot deflate request; zlib end error %d", ret);
670 gzip_size = stream.total_out;
672 headers = curl_slist_append(headers, "Content-Encoding: gzip");
673 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
674 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
676 if (options.verbosity > 1) {
677 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
679 (unsigned long)rpc->len, (unsigned long)gzip_size);
683 /* We know the complete request size in advance, use the
684 * more normal Content-Length approach.
686 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
687 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
688 if (options.verbosity > 1) {
689 fprintf(stderr, "POST %s (%lu bytes)\n",
690 rpc->service_name, (unsigned long)rpc->len);
695 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
696 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
697 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
700 rpc->any_written = 0;
701 err = run_slot(slot, NULL);
702 if (err == HTTP_REAUTH && !large_request) {
703 credential_fill(&http_auth);
709 if (!rpc->any_written)
712 curl_slist_free_all(headers);
717 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
719 const char *svc = rpc->service_name;
720 struct strbuf buf = STRBUF_INIT;
721 struct strbuf *preamble = rpc->stdin_preamble;
722 struct child_process client = CHILD_PROCESS_INIT;
728 client.argv = rpc->argv;
729 if (start_command(&client))
732 write_or_die(client.in, preamble->buf, preamble->len);
734 write_or_die(client.in, heads->buf, heads->len);
736 rpc->alloc = http_post_buffer;
737 rpc->buf = xmalloc(rpc->alloc);
739 rpc->out = client.out;
740 strbuf_init(&rpc->result, 0);
742 strbuf_addf(&buf, "%s%s", url.buf, svc);
743 rpc->service_url = strbuf_detach(&buf, NULL);
745 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
746 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
748 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
749 rpc->hdr_accept = strbuf_detach(&buf, NULL);
752 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
757 err |= post_rpc(rpc);
763 strbuf_read(&rpc->result, client.out, 0);
767 if (xread(client.out, buf, sizeof(buf)) <= 0)
774 err |= finish_command(&client);
775 free(rpc->service_url);
776 free(rpc->hdr_content_type);
777 free(rpc->hdr_accept);
779 strbuf_release(&buf);
783 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
785 struct walker *walker;
789 ALLOC_ARRAY(targets, nr_heads);
790 if (options.depth || options.deepen_since)
791 die("dumb http transport does not support shallow capabilities");
792 for (i = 0; i < nr_heads; i++)
793 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
795 walker = get_http_walker(url.buf);
797 walker->get_tree = 1;
798 walker->get_history = 1;
799 walker->get_verbosely = options.verbosity >= 3;
800 walker->get_recover = 0;
801 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
804 for (i = 0; i < nr_heads; i++)
808 return ret ? error("fetch failed.") : 0;
811 static int fetch_git(struct discovery *heads,
812 int nr_heads, struct ref **to_fetch)
814 struct rpc_state rpc;
815 struct strbuf preamble = STRBUF_INIT;
817 struct argv_array args = ARGV_ARRAY_INIT;
819 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
820 "--stdin", "--lock-pack", NULL);
821 if (options.followtags)
822 argv_array_push(&args, "--include-tag");
824 argv_array_push(&args, "--thin");
825 if (options.verbosity >= 3)
826 argv_array_pushl(&args, "-v", "-v", NULL);
827 if (options.check_self_contained_and_connected)
828 argv_array_push(&args, "--check-self-contained-and-connected");
830 argv_array_push(&args, "--cloning");
831 if (options.update_shallow)
832 argv_array_push(&args, "--update-shallow");
833 if (!options.progress)
834 argv_array_push(&args, "--no-progress");
836 argv_array_pushf(&args, "--depth=%lu", options.depth);
837 if (options.deepen_since)
838 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
839 for (i = 0; i < options.deepen_not.nr; i++)
840 argv_array_pushf(&args, "--shallow-exclude=%s",
841 options.deepen_not.items[i].string);
842 if (options.deepen_relative && options.depth)
843 argv_array_push(&args, "--deepen-relative");
844 argv_array_push(&args, url.buf);
846 for (i = 0; i < nr_heads; i++) {
847 struct ref *ref = to_fetch[i];
849 die("cannot fetch by sha1 over smart http");
850 packet_buf_write(&preamble, "%s %s\n",
851 oid_to_hex(&ref->old_oid), ref->name);
853 packet_buf_flush(&preamble);
855 memset(&rpc, 0, sizeof(rpc));
856 rpc.service_name = "git-upload-pack",
857 rpc.argv = args.argv;
858 rpc.stdin_preamble = &preamble;
859 rpc.gzip_request = 1;
861 err = rpc_service(&rpc, heads);
863 write_or_die(1, rpc.result.buf, rpc.result.len);
864 strbuf_release(&rpc.result);
865 strbuf_release(&preamble);
866 argv_array_clear(&args);
870 static int fetch(int nr_heads, struct ref **to_fetch)
872 struct discovery *d = discover_refs("git-upload-pack", 0);
874 return fetch_git(d, nr_heads, to_fetch);
876 return fetch_dumb(nr_heads, to_fetch);
879 static void parse_fetch(struct strbuf *buf)
881 struct ref **to_fetch = NULL;
882 struct ref *list_head = NULL;
883 struct ref **list = &list_head;
884 int alloc_heads = 0, nr_heads = 0;
888 if (skip_prefix(buf->buf, "fetch ", &p)) {
891 struct object_id old_oid;
893 if (get_oid_hex(p, &old_oid))
894 die("protocol error: expected sha/ref, got %s'", p);
895 if (p[GIT_SHA1_HEXSZ] == ' ')
896 name = p + GIT_SHA1_HEXSZ + 1;
897 else if (!p[GIT_SHA1_HEXSZ])
900 die("protocol error: expected sha/ref, got %s'", p);
902 ref = alloc_ref(name);
903 oidcpy(&ref->old_oid, &old_oid);
908 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
909 to_fetch[nr_heads++] = ref;
912 die("http transport does not support %s", buf->buf);
915 if (strbuf_getline_lf(buf, stdin) == EOF)
921 if (fetch(nr_heads, to_fetch))
922 exit(128); /* error already reported */
923 free_refs(list_head);
931 static int push_dav(int nr_spec, char **specs)
933 struct child_process child = CHILD_PROCESS_INIT;
937 argv_array_push(&child.args, "http-push");
938 argv_array_push(&child.args, "--helper-status");
940 argv_array_push(&child.args, "--dry-run");
941 if (options.verbosity > 1)
942 argv_array_push(&child.args, "--verbose");
943 argv_array_push(&child.args, url.buf);
944 for (i = 0; i < nr_spec; i++)
945 argv_array_push(&child.args, specs[i]);
947 if (run_command(&child))
948 die("git-http-push failed");
952 static int push_git(struct discovery *heads, int nr_spec, char **specs)
954 struct rpc_state rpc;
956 struct argv_array args;
957 struct string_list_item *cas_option;
958 struct strbuf preamble = STRBUF_INIT;
960 argv_array_init(&args);
961 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
965 argv_array_push(&args, "--thin");
967 argv_array_push(&args, "--dry-run");
968 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
969 argv_array_push(&args, "--signed=yes");
970 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
971 argv_array_push(&args, "--signed=if-asked");
972 if (options.verbosity == 0)
973 argv_array_push(&args, "--quiet");
974 else if (options.verbosity > 1)
975 argv_array_push(&args, "--verbose");
976 for (i = 0; i < options.push_options.nr; i++)
977 argv_array_pushf(&args, "--push-option=%s",
978 options.push_options.items[i].string);
979 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
980 for_each_string_list_item(cas_option, &cas_options)
981 argv_array_push(&args, cas_option->string);
982 argv_array_push(&args, url.buf);
984 argv_array_push(&args, "--stdin");
985 for (i = 0; i < nr_spec; i++)
986 packet_buf_write(&preamble, "%s\n", specs[i]);
987 packet_buf_flush(&preamble);
989 memset(&rpc, 0, sizeof(rpc));
990 rpc.service_name = "git-receive-pack",
991 rpc.argv = args.argv;
992 rpc.stdin_preamble = &preamble;
994 err = rpc_service(&rpc, heads);
996 write_or_die(1, rpc.result.buf, rpc.result.len);
997 strbuf_release(&rpc.result);
998 strbuf_release(&preamble);
999 argv_array_clear(&args);
1003 static int push(int nr_spec, char **specs)
1005 struct discovery *heads = discover_refs("git-receive-pack", 1);
1008 if (heads->proto_git)
1009 ret = push_git(heads, nr_spec, specs);
1011 ret = push_dav(nr_spec, specs);
1012 free_discovery(heads);
1016 static void parse_push(struct strbuf *buf)
1018 char **specs = NULL;
1019 int alloc_spec = 0, nr_spec = 0, i, ret;
1022 if (starts_with(buf->buf, "push ")) {
1023 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1024 specs[nr_spec++] = xstrdup(buf->buf + 5);
1027 die("http transport does not support %s", buf->buf);
1030 if (strbuf_getline_lf(buf, stdin) == EOF)
1036 ret = push(nr_spec, specs);
1041 exit(128); /* error already reported */
1044 for (i = 0; i < nr_spec; i++)
1049 int cmd_main(int argc, const char **argv)
1051 struct strbuf buf = STRBUF_INIT;
1054 setup_git_directory_gently(&nongit);
1056 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1060 options.verbosity = 1;
1061 options.progress = !!isatty(2);
1063 string_list_init(&options.deepen_not, 1);
1064 string_list_init(&options.push_options, 1);
1066 remote = remote_get(argv[1]);
1069 end_url_with_slash(&url, argv[2]);
1071 end_url_with_slash(&url, remote->url[0]);
1074 http_init(remote, url.buf, 0);
1079 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1081 error("remote-curl: error reading command stream from git");
1086 if (starts_with(buf.buf, "fetch ")) {
1088 die("remote-curl: fetch attempted without a local repo");
1091 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1092 int for_push = !!strstr(buf.buf + 4, "for-push");
1093 output_refs(get_refs(for_push));
1095 } else if (starts_with(buf.buf, "push ")) {
1098 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1099 char *value = strchr(arg, ' ');
1107 result = set_option(arg, value);
1110 else if (result < 0)
1111 printf("error invalid value\n");
1113 printf("unsupported\n");
1116 } else if (!strcmp(buf.buf, "capabilities")) {
1120 printf("check-connectivity\n");
1124 error("remote-curl: unknown command '%s' from git", buf.buf);