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 struct discovery *discover_refs(const char *service, int for_push)
296 struct strbuf exp = STRBUF_INIT;
297 struct strbuf type = STRBUF_INIT;
298 struct strbuf charset = STRBUF_INIT;
299 struct strbuf buffer = STRBUF_INIT;
300 struct strbuf refs_url = STRBUF_INIT;
301 struct strbuf effective_url = STRBUF_INIT;
302 struct discovery *last = last_discovery;
303 int http_ret, maybe_smart = 0;
304 struct http_get_options http_options;
306 if (last && !strcmp(service, last->service))
308 free_discovery(last);
310 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
311 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
312 git_env_bool("GIT_SMART_HTTP", 1)) {
314 if (!strchr(url.buf, '?'))
315 strbuf_addch(&refs_url, '?');
317 strbuf_addch(&refs_url, '&');
318 strbuf_addf(&refs_url, "service=%s", service);
321 memset(&http_options, 0, sizeof(http_options));
322 http_options.content_type = &type;
323 http_options.charset = &charset;
324 http_options.effective_url = &effective_url;
325 http_options.base_url = &url;
326 http_options.initial_request = 1;
327 http_options.no_cache = 1;
328 http_options.keep_error = 1;
330 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
334 case HTTP_MISSING_TARGET:
335 show_http_message(&type, &charset, &buffer);
336 die("repository '%s' not found", url.buf);
338 show_http_message(&type, &charset, &buffer);
339 die("Authentication failed for '%s'", url.buf);
341 show_http_message(&type, &charset, &buffer);
342 die("unable to access '%s': %s", url.buf, curl_errorstr);
345 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
346 warning(_("redirecting to %s"), url.buf);
348 last= xcalloc(1, sizeof(*last_discovery));
349 last->service = xstrdup(service);
350 last->buf_alloc = strbuf_detach(&buffer, &last->len);
351 last->buf = last->buf_alloc;
353 strbuf_addf(&exp, "application/x-%s-advertisement", service);
355 (5 <= last->len && last->buf[4] == '#') &&
356 !strbuf_cmp(&exp, &type)) {
360 * smart HTTP response; validate that the service
361 * pkt-line matches our request.
363 line = packet_read_line_buf(&last->buf, &last->len, NULL);
366 strbuf_addf(&exp, "# service=%s", service);
367 if (strcmp(line, exp.buf))
368 die("invalid server response; got '%s'", line);
369 strbuf_release(&exp);
371 /* The header can include additional metadata lines, up
372 * until a packet flush marker. Ignore these now, but
373 * in the future we might start to scan them.
375 while (packet_read_line_buf(&last->buf, &last->len, NULL))
382 last->refs = parse_git_refs(last, for_push);
384 last->refs = parse_info_refs(last);
386 strbuf_release(&refs_url);
387 strbuf_release(&exp);
388 strbuf_release(&type);
389 strbuf_release(&charset);
390 strbuf_release(&effective_url);
391 strbuf_release(&buffer);
392 last_discovery = last;
396 static struct ref *get_refs(int for_push)
398 struct discovery *heads;
401 heads = discover_refs("git-receive-pack", for_push);
403 heads = discover_refs("git-upload-pack", for_push);
408 static void output_refs(struct ref *refs)
411 for (posn = refs; posn; posn = posn->next) {
413 printf("@%s %s\n", posn->symref, posn->name);
415 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
422 const char *service_name;
424 struct strbuf *stdin_preamble;
426 char *hdr_content_type;
435 struct strbuf result;
436 unsigned gzip_request : 1;
437 unsigned initial_buffer : 1;
440 static size_t rpc_out(void *ptr, size_t eltsize,
441 size_t nmemb, void *buffer_)
443 size_t max = eltsize * nmemb;
444 struct rpc_state *rpc = buffer_;
445 size_t avail = rpc->len - rpc->pos;
448 rpc->initial_buffer = 0;
449 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
458 memcpy(ptr, rpc->buf + rpc->pos, avail);
463 #ifndef NO_CURL_IOCTL
464 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
466 struct rpc_state *rpc = clientp;
472 case CURLIOCMD_RESTARTREAD:
473 if (rpc->initial_buffer) {
477 error("unable to rewind rpc post data - try increasing http.postBuffer");
478 return CURLIOE_FAILRESTART;
481 return CURLIOE_UNKNOWNCMD;
486 static size_t rpc_in(char *ptr, size_t eltsize,
487 size_t nmemb, void *buffer_)
489 size_t size = eltsize * nmemb;
490 struct rpc_state *rpc = buffer_;
492 rpc->any_written = 1;
493 write_or_die(rpc->in, ptr, size);
497 static int run_slot(struct active_request_slot *slot,
498 struct slot_results *results)
501 struct slot_results results_buf;
504 results = &results_buf;
506 err = run_one_slot(slot, results);
508 if (err != HTTP_OK && err != HTTP_REAUTH) {
509 struct strbuf msg = STRBUF_INIT;
510 if (results->http_code && results->http_code != 200)
511 strbuf_addf(&msg, "HTTP %ld", results->http_code);
512 if (results->curl_result != CURLE_OK) {
514 strbuf_addch(&msg, ' ');
515 strbuf_addf(&msg, "curl %d", results->curl_result);
516 if (curl_errorstr[0]) {
517 strbuf_addch(&msg, ' ');
518 strbuf_addstr(&msg, curl_errorstr);
521 error("RPC failed; %s", msg.buf);
522 strbuf_release(&msg);
528 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
530 struct active_request_slot *slot;
531 struct curl_slist *headers = http_copy_default_headers();
532 struct strbuf buf = STRBUF_INIT;
535 slot = get_active_slot();
537 headers = curl_slist_append(headers, rpc->hdr_content_type);
538 headers = curl_slist_append(headers, rpc->hdr_accept);
540 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
541 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
542 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
543 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
544 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
545 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
546 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
547 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
548 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
550 err = run_slot(slot, results);
552 curl_slist_free_all(headers);
553 strbuf_release(&buf);
557 static curl_off_t xcurl_off_t(ssize_t len) {
558 if (len > maximum_signed_value_of_type(curl_off_t))
559 die("cannot handle pushes this big");
560 return (curl_off_t) len;
563 static int post_rpc(struct rpc_state *rpc)
565 struct active_request_slot *slot;
566 struct curl_slist *headers = http_copy_default_headers();
567 int use_gzip = rpc->gzip_request;
568 char *gzip_body = NULL;
569 size_t gzip_size = 0;
570 int err, large_request = 0;
571 int needs_100_continue = 0;
573 /* Try to load the entire request, if we can fit it into the
574 * allocated buffer space we can use HTTP/1.0 and avoid the
575 * chunked encoding mess.
578 size_t left = rpc->alloc - rpc->len;
579 char *buf = rpc->buf + rpc->len;
582 if (left < LARGE_PACKET_MAX) {
588 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
595 struct slot_results results;
598 err = probe_rpc(rpc, &results);
599 if (err == HTTP_REAUTH)
600 credential_fill(&http_auth);
601 } while (err == HTTP_REAUTH);
605 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
606 needs_100_continue = 1;
609 headers = curl_slist_append(headers, rpc->hdr_content_type);
610 headers = curl_slist_append(headers, rpc->hdr_accept);
611 headers = curl_slist_append(headers, needs_100_continue ?
612 "Expect: 100-continue" : "Expect:");
615 slot = get_active_slot();
617 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
618 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
619 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
620 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
623 /* The request body is large and the size cannot be predicted.
624 * We must use chunked encoding to send it.
626 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
627 rpc->initial_buffer = 1;
628 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
629 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
630 #ifndef NO_CURL_IOCTL
631 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
632 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
634 if (options.verbosity > 1) {
635 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
639 } else if (gzip_body) {
641 * If we are looping to retry authentication, then the previous
642 * run will have set up the headers and gzip buffer already,
643 * and we just need to send it.
645 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
646 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
648 } else if (use_gzip && 1024 < rpc->len) {
649 /* The client backend isn't giving us compressed data so
650 * we can try to deflate it ourselves, this may save on.
656 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
657 gzip_size = git_deflate_bound(&stream, rpc->len);
658 gzip_body = xmalloc(gzip_size);
660 stream.next_in = (unsigned char *)rpc->buf;
661 stream.avail_in = rpc->len;
662 stream.next_out = (unsigned char *)gzip_body;
663 stream.avail_out = gzip_size;
665 ret = git_deflate(&stream, Z_FINISH);
666 if (ret != Z_STREAM_END)
667 die("cannot deflate request; zlib deflate error %d", ret);
669 ret = git_deflate_end_gently(&stream);
671 die("cannot deflate request; zlib end error %d", ret);
673 gzip_size = stream.total_out;
675 headers = curl_slist_append(headers, "Content-Encoding: gzip");
676 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
677 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
679 if (options.verbosity > 1) {
680 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
682 (unsigned long)rpc->len, (unsigned long)gzip_size);
686 /* We know the complete request size in advance, use the
687 * more normal Content-Length approach.
689 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
690 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
691 if (options.verbosity > 1) {
692 fprintf(stderr, "POST %s (%lu bytes)\n",
693 rpc->service_name, (unsigned long)rpc->len);
698 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
699 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
700 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
703 rpc->any_written = 0;
704 err = run_slot(slot, NULL);
705 if (err == HTTP_REAUTH && !large_request) {
706 credential_fill(&http_auth);
712 if (!rpc->any_written)
715 curl_slist_free_all(headers);
720 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
722 const char *svc = rpc->service_name;
723 struct strbuf buf = STRBUF_INIT;
724 struct strbuf *preamble = rpc->stdin_preamble;
725 struct child_process client = CHILD_PROCESS_INIT;
731 client.argv = rpc->argv;
732 if (start_command(&client))
735 write_or_die(client.in, preamble->buf, preamble->len);
737 write_or_die(client.in, heads->buf, heads->len);
739 rpc->alloc = http_post_buffer;
740 rpc->buf = xmalloc(rpc->alloc);
742 rpc->out = client.out;
743 strbuf_init(&rpc->result, 0);
745 strbuf_addf(&buf, "%s%s", url.buf, svc);
746 rpc->service_url = strbuf_detach(&buf, NULL);
748 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
749 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
751 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
752 rpc->hdr_accept = strbuf_detach(&buf, NULL);
755 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
760 err |= post_rpc(rpc);
766 strbuf_read(&rpc->result, client.out, 0);
770 if (xread(client.out, buf, sizeof(buf)) <= 0)
777 err |= finish_command(&client);
778 free(rpc->service_url);
779 free(rpc->hdr_content_type);
780 free(rpc->hdr_accept);
782 strbuf_release(&buf);
786 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
788 struct walker *walker;
792 ALLOC_ARRAY(targets, nr_heads);
793 if (options.depth || options.deepen_since)
794 die("dumb http transport does not support shallow capabilities");
795 for (i = 0; i < nr_heads; i++)
796 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
798 walker = get_http_walker(url.buf);
800 walker->get_tree = 1;
801 walker->get_history = 1;
802 walker->get_verbosely = options.verbosity >= 3;
803 walker->get_recover = 0;
804 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
807 for (i = 0; i < nr_heads; i++)
811 return ret ? error("fetch failed.") : 0;
814 static int fetch_git(struct discovery *heads,
815 int nr_heads, struct ref **to_fetch)
817 struct rpc_state rpc;
818 struct strbuf preamble = STRBUF_INIT;
820 struct argv_array args = ARGV_ARRAY_INIT;
822 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
823 "--stdin", "--lock-pack", NULL);
824 if (options.followtags)
825 argv_array_push(&args, "--include-tag");
827 argv_array_push(&args, "--thin");
828 if (options.verbosity >= 3)
829 argv_array_pushl(&args, "-v", "-v", NULL);
830 if (options.check_self_contained_and_connected)
831 argv_array_push(&args, "--check-self-contained-and-connected");
833 argv_array_push(&args, "--cloning");
834 if (options.update_shallow)
835 argv_array_push(&args, "--update-shallow");
836 if (!options.progress)
837 argv_array_push(&args, "--no-progress");
839 argv_array_pushf(&args, "--depth=%lu", options.depth);
840 if (options.deepen_since)
841 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
842 for (i = 0; i < options.deepen_not.nr; i++)
843 argv_array_pushf(&args, "--shallow-exclude=%s",
844 options.deepen_not.items[i].string);
845 if (options.deepen_relative && options.depth)
846 argv_array_push(&args, "--deepen-relative");
847 argv_array_push(&args, url.buf);
849 for (i = 0; i < nr_heads; i++) {
850 struct ref *ref = to_fetch[i];
852 die("cannot fetch by sha1 over smart http");
853 packet_buf_write(&preamble, "%s %s\n",
854 oid_to_hex(&ref->old_oid), ref->name);
856 packet_buf_flush(&preamble);
858 memset(&rpc, 0, sizeof(rpc));
859 rpc.service_name = "git-upload-pack",
860 rpc.argv = args.argv;
861 rpc.stdin_preamble = &preamble;
862 rpc.gzip_request = 1;
864 err = rpc_service(&rpc, heads);
866 write_or_die(1, rpc.result.buf, rpc.result.len);
867 strbuf_release(&rpc.result);
868 strbuf_release(&preamble);
869 argv_array_clear(&args);
873 static int fetch(int nr_heads, struct ref **to_fetch)
875 struct discovery *d = discover_refs("git-upload-pack", 0);
877 return fetch_git(d, nr_heads, to_fetch);
879 return fetch_dumb(nr_heads, to_fetch);
882 static void parse_fetch(struct strbuf *buf)
884 struct ref **to_fetch = NULL;
885 struct ref *list_head = NULL;
886 struct ref **list = &list_head;
887 int alloc_heads = 0, nr_heads = 0;
891 if (skip_prefix(buf->buf, "fetch ", &p)) {
894 struct object_id old_oid;
896 if (get_oid_hex(p, &old_oid))
897 die("protocol error: expected sha/ref, got %s'", p);
898 if (p[GIT_SHA1_HEXSZ] == ' ')
899 name = p + GIT_SHA1_HEXSZ + 1;
900 else if (!p[GIT_SHA1_HEXSZ])
903 die("protocol error: expected sha/ref, got %s'", p);
905 ref = alloc_ref(name);
906 oidcpy(&ref->old_oid, &old_oid);
911 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
912 to_fetch[nr_heads++] = ref;
915 die("http transport does not support %s", buf->buf);
918 if (strbuf_getline_lf(buf, stdin) == EOF)
924 if (fetch(nr_heads, to_fetch))
925 exit(128); /* error already reported */
926 free_refs(list_head);
934 static int push_dav(int nr_spec, char **specs)
936 struct child_process child = CHILD_PROCESS_INIT;
940 argv_array_push(&child.args, "http-push");
941 argv_array_push(&child.args, "--helper-status");
943 argv_array_push(&child.args, "--dry-run");
944 if (options.verbosity > 1)
945 argv_array_push(&child.args, "--verbose");
946 argv_array_push(&child.args, url.buf);
947 for (i = 0; i < nr_spec; i++)
948 argv_array_push(&child.args, specs[i]);
950 if (run_command(&child))
951 die("git-http-push failed");
955 static int push_git(struct discovery *heads, int nr_spec, char **specs)
957 struct rpc_state rpc;
959 struct argv_array args;
960 struct string_list_item *cas_option;
961 struct strbuf preamble = STRBUF_INIT;
963 argv_array_init(&args);
964 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
968 argv_array_push(&args, "--thin");
970 argv_array_push(&args, "--dry-run");
971 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
972 argv_array_push(&args, "--signed=yes");
973 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
974 argv_array_push(&args, "--signed=if-asked");
975 if (options.verbosity == 0)
976 argv_array_push(&args, "--quiet");
977 else if (options.verbosity > 1)
978 argv_array_push(&args, "--verbose");
979 for (i = 0; i < options.push_options.nr; i++)
980 argv_array_pushf(&args, "--push-option=%s",
981 options.push_options.items[i].string);
982 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
983 for_each_string_list_item(cas_option, &cas_options)
984 argv_array_push(&args, cas_option->string);
985 argv_array_push(&args, url.buf);
987 argv_array_push(&args, "--stdin");
988 for (i = 0; i < nr_spec; i++)
989 packet_buf_write(&preamble, "%s\n", specs[i]);
990 packet_buf_flush(&preamble);
992 memset(&rpc, 0, sizeof(rpc));
993 rpc.service_name = "git-receive-pack",
994 rpc.argv = args.argv;
995 rpc.stdin_preamble = &preamble;
997 err = rpc_service(&rpc, heads);
999 write_or_die(1, rpc.result.buf, rpc.result.len);
1000 strbuf_release(&rpc.result);
1001 strbuf_release(&preamble);
1002 argv_array_clear(&args);
1006 static int push(int nr_spec, char **specs)
1008 struct discovery *heads = discover_refs("git-receive-pack", 1);
1011 if (heads->proto_git)
1012 ret = push_git(heads, nr_spec, specs);
1014 ret = push_dav(nr_spec, specs);
1015 free_discovery(heads);
1019 static void parse_push(struct strbuf *buf)
1021 char **specs = NULL;
1022 int alloc_spec = 0, nr_spec = 0, i, ret;
1025 if (starts_with(buf->buf, "push ")) {
1026 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1027 specs[nr_spec++] = xstrdup(buf->buf + 5);
1030 die("http transport does not support %s", buf->buf);
1033 if (strbuf_getline_lf(buf, stdin) == EOF)
1039 ret = push(nr_spec, specs);
1044 exit(128); /* error already reported */
1047 for (i = 0; i < nr_spec; i++)
1052 int cmd_main(int argc, const char **argv)
1054 struct strbuf buf = STRBUF_INIT;
1057 setup_git_directory_gently(&nongit);
1059 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1063 options.verbosity = 1;
1064 options.progress = !!isatty(2);
1066 string_list_init(&options.deepen_not, 1);
1067 string_list_init(&options.push_options, 1);
1069 remote = remote_get(argv[1]);
1072 end_url_with_slash(&url, argv[2]);
1074 end_url_with_slash(&url, remote->url[0]);
1077 http_init(remote, url.buf, 0);
1082 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1084 error("remote-curl: error reading command stream from git");
1089 if (starts_with(buf.buf, "fetch ")) {
1091 die("remote-curl: fetch attempted without a local repo");
1094 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1095 int for_push = !!strstr(buf.buf + 4, "for-push");
1096 output_refs(get_refs(for_push));
1098 } else if (starts_with(buf.buf, "push ")) {
1101 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1102 char *value = strchr(arg, ' ');
1110 result = set_option(arg, value);
1113 else if (result < 0)
1114 printf("error invalid value\n");
1116 printf("unsupported\n");
1119 } else if (!strcmp(buf.buf, "capabilities")) {
1123 printf("check-connectivity\n");
1127 error("remote-curl: unknown command '%s' from git", buf.buf);