8 #include "run-command.h"
10 #include "string-list.h"
12 #include "argv-array.h"
13 #include "credential.h"
14 #include "sha1-array.h"
15 #include "send-pack.h"
17 static struct remote *remote;
18 /* always ends with a trailing slash */
19 static struct strbuf url = STRBUF_INIT;
25 struct string_list deepen_not;
26 struct string_list push_options;
28 unsigned progress : 1,
29 check_self_contained_and_connected : 1,
35 /* One of the SEND_PACK_PUSH_CERT_* constants. */
41 static struct options options;
42 static struct string_list cas_options = STRING_LIST_INIT_DUP;
44 static int set_option(const char *name, const char *value)
46 if (!strcmp(name, "verbosity")) {
48 int v = strtol(value, &end, 10);
49 if (value == end || *end)
51 options.verbosity = v;
54 else if (!strcmp(name, "progress")) {
55 if (!strcmp(value, "true"))
57 else if (!strcmp(value, "false"))
63 else if (!strcmp(name, "depth")) {
65 unsigned long v = strtoul(value, &end, 10);
66 if (value == end || *end)
71 else if (!strcmp(name, "deepen-since")) {
72 options.deepen_since = xstrdup(value);
75 else if (!strcmp(name, "deepen-not")) {
76 string_list_append(&options.deepen_not, value);
79 else if (!strcmp(name, "deepen-relative")) {
80 if (!strcmp(value, "true"))
81 options.deepen_relative = 1;
82 else if (!strcmp(value, "false"))
83 options.deepen_relative = 0;
88 else if (!strcmp(name, "followtags")) {
89 if (!strcmp(value, "true"))
90 options.followtags = 1;
91 else if (!strcmp(value, "false"))
92 options.followtags = 0;
97 else if (!strcmp(name, "dry-run")) {
98 if (!strcmp(value, "true"))
100 else if (!strcmp(value, "false"))
106 else if (!strcmp(name, "check-connectivity")) {
107 if (!strcmp(value, "true"))
108 options.check_self_contained_and_connected = 1;
109 else if (!strcmp(value, "false"))
110 options.check_self_contained_and_connected = 0;
115 else if (!strcmp(name, "cas")) {
116 struct strbuf val = STRBUF_INIT;
117 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
118 string_list_append(&cas_options, val.buf);
119 strbuf_release(&val);
121 } else if (!strcmp(name, "cloning")) {
122 if (!strcmp(value, "true"))
124 else if (!strcmp(value, "false"))
129 } else if (!strcmp(name, "update-shallow")) {
130 if (!strcmp(value, "true"))
131 options.update_shallow = 1;
132 else if (!strcmp(value, "false"))
133 options.update_shallow = 0;
137 } else if (!strcmp(name, "pushcert")) {
138 if (!strcmp(value, "true"))
139 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
140 else if (!strcmp(value, "false"))
141 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
142 else if (!strcmp(value, "if-asked"))
143 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
147 } else if (!strcmp(name, "push-option")) {
148 string_list_append(&options.push_options, value);
151 #if LIBCURL_VERSION_NUM >= 0x070a08
152 } else if (!strcmp(name, "family")) {
153 if (!strcmp(value, "ipv4"))
154 git_curl_ipresolve = CURL_IPRESOLVE_V4;
155 else if (!strcmp(value, "ipv6"))
156 git_curl_ipresolve = CURL_IPRESOLVE_V6;
157 else if (!strcmp(value, "all"))
158 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
162 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
163 } else if (!strcmp(name, "from-promisor")) {
164 options.from_promisor = 1;
166 } else if (!strcmp(name, "no-dependents")) {
167 options.no_dependents = 1;
169 } else if (!strcmp(name, "filter")) {
170 options.filter = xstrdup(value);;
173 return 1 /* unsupported */;
183 struct oid_array shallow;
184 unsigned proto_git : 1;
186 static struct discovery *last_discovery;
188 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
190 struct ref *list = NULL;
191 get_remote_heads(-1, heads->buf, heads->len, &list,
192 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
196 static struct ref *parse_info_refs(struct discovery *heads)
198 char *data, *start, *mid;
202 struct ref *refs = NULL;
203 struct ref *ref = NULL;
204 struct ref *last_ref = NULL;
209 while (i < heads->len) {
215 if (data[i] == '\n') {
216 if (mid - start != 40)
217 die("%sinfo/refs not valid: is this a git repository?",
221 ref = alloc_ref(ref_name);
222 get_oid_hex(start, &ref->old_oid);
226 last_ref->next = ref;
233 ref = alloc_ref("HEAD");
234 if (!http_fetch_ref(url.buf, ref) &&
235 !resolve_remote_symref(ref, refs)) {
245 static void free_discovery(struct discovery *d)
248 if (d == last_discovery)
249 last_discovery = NULL;
250 free(d->shallow.oid);
257 static int show_http_message(struct strbuf *type, struct strbuf *charset,
263 * We only show text/plain parts, as other types are likely
264 * to be ugly to look at on the user's terminal.
266 if (strcmp(type->buf, "text/plain"))
269 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
277 eol = strchrnul(p, '\n');
278 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
284 static struct discovery *discover_refs(const char *service, int for_push)
286 struct strbuf exp = STRBUF_INIT;
287 struct strbuf type = STRBUF_INIT;
288 struct strbuf charset = STRBUF_INIT;
289 struct strbuf buffer = STRBUF_INIT;
290 struct strbuf refs_url = STRBUF_INIT;
291 struct strbuf effective_url = STRBUF_INIT;
292 struct discovery *last = last_discovery;
293 int http_ret, maybe_smart = 0;
294 struct http_get_options http_options;
296 if (last && !strcmp(service, last->service))
298 free_discovery(last);
300 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
301 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
302 git_env_bool("GIT_SMART_HTTP", 1)) {
304 if (!strchr(url.buf, '?'))
305 strbuf_addch(&refs_url, '?');
307 strbuf_addch(&refs_url, '&');
308 strbuf_addf(&refs_url, "service=%s", service);
311 memset(&http_options, 0, sizeof(http_options));
312 http_options.content_type = &type;
313 http_options.charset = &charset;
314 http_options.effective_url = &effective_url;
315 http_options.base_url = &url;
316 http_options.initial_request = 1;
317 http_options.no_cache = 1;
318 http_options.keep_error = 1;
320 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
324 case HTTP_MISSING_TARGET:
325 show_http_message(&type, &charset, &buffer);
326 die("repository '%s' not found", url.buf);
328 show_http_message(&type, &charset, &buffer);
329 die("Authentication failed for '%s'", url.buf);
331 show_http_message(&type, &charset, &buffer);
332 die("unable to access '%s': %s", url.buf, curl_errorstr);
335 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
336 warning(_("redirecting to %s"), url.buf);
338 last= xcalloc(1, sizeof(*last_discovery));
339 last->service = service;
340 last->buf_alloc = strbuf_detach(&buffer, &last->len);
341 last->buf = last->buf_alloc;
343 strbuf_addf(&exp, "application/x-%s-advertisement", service);
345 (5 <= last->len && last->buf[4] == '#') &&
346 !strbuf_cmp(&exp, &type)) {
350 * smart HTTP response; validate that the service
351 * pkt-line matches our request.
353 line = packet_read_line_buf(&last->buf, &last->len, NULL);
356 strbuf_addf(&exp, "# service=%s", service);
357 if (strcmp(line, exp.buf))
358 die("invalid server response; got '%s'", line);
359 strbuf_release(&exp);
361 /* The header can include additional metadata lines, up
362 * until a packet flush marker. Ignore these now, but
363 * in the future we might start to scan them.
365 while (packet_read_line_buf(&last->buf, &last->len, NULL))
372 last->refs = parse_git_refs(last, for_push);
374 last->refs = parse_info_refs(last);
376 strbuf_release(&refs_url);
377 strbuf_release(&exp);
378 strbuf_release(&type);
379 strbuf_release(&charset);
380 strbuf_release(&effective_url);
381 strbuf_release(&buffer);
382 last_discovery = last;
386 static struct ref *get_refs(int for_push)
388 struct discovery *heads;
391 heads = discover_refs("git-receive-pack", for_push);
393 heads = discover_refs("git-upload-pack", for_push);
398 static void output_refs(struct ref *refs)
401 for (posn = refs; posn; posn = posn->next) {
403 printf("@%s %s\n", posn->symref, posn->name);
405 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
412 const char *service_name;
414 struct strbuf *stdin_preamble;
416 char *hdr_content_type;
425 struct strbuf result;
426 unsigned gzip_request : 1;
427 unsigned initial_buffer : 1;
430 static size_t rpc_out(void *ptr, size_t eltsize,
431 size_t nmemb, void *buffer_)
433 size_t max = eltsize * nmemb;
434 struct rpc_state *rpc = buffer_;
435 size_t avail = rpc->len - rpc->pos;
438 rpc->initial_buffer = 0;
439 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
448 memcpy(ptr, rpc->buf + rpc->pos, avail);
453 #ifndef NO_CURL_IOCTL
454 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
456 struct rpc_state *rpc = clientp;
462 case CURLIOCMD_RESTARTREAD:
463 if (rpc->initial_buffer) {
467 error("unable to rewind rpc post data - try increasing http.postBuffer");
468 return CURLIOE_FAILRESTART;
471 return CURLIOE_UNKNOWNCMD;
476 static size_t rpc_in(char *ptr, size_t eltsize,
477 size_t nmemb, void *buffer_)
479 size_t size = eltsize * nmemb;
480 struct rpc_state *rpc = buffer_;
482 rpc->any_written = 1;
483 write_or_die(rpc->in, ptr, size);
487 static int run_slot(struct active_request_slot *slot,
488 struct slot_results *results)
491 struct slot_results results_buf;
494 results = &results_buf;
496 err = run_one_slot(slot, results);
498 if (err != HTTP_OK && err != HTTP_REAUTH) {
499 struct strbuf msg = STRBUF_INIT;
500 if (results->http_code && results->http_code != 200)
501 strbuf_addf(&msg, "HTTP %ld", results->http_code);
502 if (results->curl_result != CURLE_OK) {
504 strbuf_addch(&msg, ' ');
505 strbuf_addf(&msg, "curl %d", results->curl_result);
506 if (curl_errorstr[0]) {
507 strbuf_addch(&msg, ' ');
508 strbuf_addstr(&msg, curl_errorstr);
511 error("RPC failed; %s", msg.buf);
512 strbuf_release(&msg);
518 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
520 struct active_request_slot *slot;
521 struct curl_slist *headers = http_copy_default_headers();
522 struct strbuf buf = STRBUF_INIT;
525 slot = get_active_slot();
527 headers = curl_slist_append(headers, rpc->hdr_content_type);
528 headers = curl_slist_append(headers, rpc->hdr_accept);
530 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
531 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
532 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
533 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
534 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
535 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
536 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
537 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
538 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
540 err = run_slot(slot, results);
542 curl_slist_free_all(headers);
543 strbuf_release(&buf);
547 static curl_off_t xcurl_off_t(ssize_t len) {
548 if (len > maximum_signed_value_of_type(curl_off_t))
549 die("cannot handle pushes this big");
550 return (curl_off_t) len;
553 static int post_rpc(struct rpc_state *rpc)
555 struct active_request_slot *slot;
556 struct curl_slist *headers = http_copy_default_headers();
557 int use_gzip = rpc->gzip_request;
558 char *gzip_body = NULL;
559 size_t gzip_size = 0;
560 int err, large_request = 0;
561 int needs_100_continue = 0;
563 /* Try to load the entire request, if we can fit it into the
564 * allocated buffer space we can use HTTP/1.0 and avoid the
565 * chunked encoding mess.
568 size_t left = rpc->alloc - rpc->len;
569 char *buf = rpc->buf + rpc->len;
572 if (left < LARGE_PACKET_MAX) {
578 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
585 struct slot_results results;
588 err = probe_rpc(rpc, &results);
589 if (err == HTTP_REAUTH)
590 credential_fill(&http_auth);
591 } while (err == HTTP_REAUTH);
595 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
596 needs_100_continue = 1;
599 headers = curl_slist_append(headers, rpc->hdr_content_type);
600 headers = curl_slist_append(headers, rpc->hdr_accept);
601 headers = curl_slist_append(headers, needs_100_continue ?
602 "Expect: 100-continue" : "Expect:");
605 slot = get_active_slot();
607 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
608 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
609 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
610 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
613 /* The request body is large and the size cannot be predicted.
614 * We must use chunked encoding to send it.
616 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
617 rpc->initial_buffer = 1;
618 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
619 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
620 #ifndef NO_CURL_IOCTL
621 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
622 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
624 if (options.verbosity > 1) {
625 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
629 } else if (gzip_body) {
631 * If we are looping to retry authentication, then the previous
632 * run will have set up the headers and gzip buffer already,
633 * and we just need to send it.
635 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
636 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
638 } else if (use_gzip && 1024 < rpc->len) {
639 /* The client backend isn't giving us compressed data so
640 * we can try to deflate it ourselves, this may save on.
646 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
647 gzip_size = git_deflate_bound(&stream, rpc->len);
648 gzip_body = xmalloc(gzip_size);
650 stream.next_in = (unsigned char *)rpc->buf;
651 stream.avail_in = rpc->len;
652 stream.next_out = (unsigned char *)gzip_body;
653 stream.avail_out = gzip_size;
655 ret = git_deflate(&stream, Z_FINISH);
656 if (ret != Z_STREAM_END)
657 die("cannot deflate request; zlib deflate error %d", ret);
659 ret = git_deflate_end_gently(&stream);
661 die("cannot deflate request; zlib end error %d", ret);
663 gzip_size = stream.total_out;
665 headers = curl_slist_append(headers, "Content-Encoding: gzip");
666 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
667 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
669 if (options.verbosity > 1) {
670 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
672 (unsigned long)rpc->len, (unsigned long)gzip_size);
676 /* We know the complete request size in advance, use the
677 * more normal Content-Length approach.
679 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
680 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
681 if (options.verbosity > 1) {
682 fprintf(stderr, "POST %s (%lu bytes)\n",
683 rpc->service_name, (unsigned long)rpc->len);
688 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
689 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
690 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
693 rpc->any_written = 0;
694 err = run_slot(slot, NULL);
695 if (err == HTTP_REAUTH && !large_request) {
696 credential_fill(&http_auth);
702 if (!rpc->any_written)
705 curl_slist_free_all(headers);
710 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
712 const char *svc = rpc->service_name;
713 struct strbuf buf = STRBUF_INIT;
714 struct strbuf *preamble = rpc->stdin_preamble;
715 struct child_process client = CHILD_PROCESS_INIT;
721 client.argv = rpc->argv;
722 if (start_command(&client))
725 write_or_die(client.in, preamble->buf, preamble->len);
727 write_or_die(client.in, heads->buf, heads->len);
729 rpc->alloc = http_post_buffer;
730 rpc->buf = xmalloc(rpc->alloc);
732 rpc->out = client.out;
733 strbuf_init(&rpc->result, 0);
735 strbuf_addf(&buf, "%s%s", url.buf, svc);
736 rpc->service_url = strbuf_detach(&buf, NULL);
738 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
739 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
741 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
742 rpc->hdr_accept = strbuf_detach(&buf, NULL);
745 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
750 err |= post_rpc(rpc);
756 strbuf_read(&rpc->result, client.out, 0);
760 if (xread(client.out, buf, sizeof(buf)) <= 0)
767 err |= finish_command(&client);
768 free(rpc->service_url);
769 free(rpc->hdr_content_type);
770 free(rpc->hdr_accept);
772 strbuf_release(&buf);
776 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
778 struct walker *walker;
782 ALLOC_ARRAY(targets, nr_heads);
783 if (options.depth || options.deepen_since)
784 die("dumb http transport does not support shallow capabilities");
785 for (i = 0; i < nr_heads; i++)
786 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
788 walker = get_http_walker(url.buf);
790 walker->get_tree = 1;
791 walker->get_history = 1;
792 walker->get_verbosely = options.verbosity >= 3;
793 walker->get_recover = 0;
794 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
797 for (i = 0; i < nr_heads; i++)
801 return ret ? error("fetch failed.") : 0;
804 static int fetch_git(struct discovery *heads,
805 int nr_heads, struct ref **to_fetch)
807 struct rpc_state rpc;
808 struct strbuf preamble = STRBUF_INIT;
810 struct argv_array args = ARGV_ARRAY_INIT;
812 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
813 "--stdin", "--lock-pack", NULL);
814 if (options.followtags)
815 argv_array_push(&args, "--include-tag");
817 argv_array_push(&args, "--thin");
818 if (options.verbosity >= 3)
819 argv_array_pushl(&args, "-v", "-v", NULL);
820 if (options.check_self_contained_and_connected)
821 argv_array_push(&args, "--check-self-contained-and-connected");
823 argv_array_push(&args, "--cloning");
824 if (options.update_shallow)
825 argv_array_push(&args, "--update-shallow");
826 if (!options.progress)
827 argv_array_push(&args, "--no-progress");
829 argv_array_pushf(&args, "--depth=%lu", options.depth);
830 if (options.deepen_since)
831 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
832 for (i = 0; i < options.deepen_not.nr; i++)
833 argv_array_pushf(&args, "--shallow-exclude=%s",
834 options.deepen_not.items[i].string);
835 if (options.deepen_relative && options.depth)
836 argv_array_push(&args, "--deepen-relative");
837 if (options.from_promisor)
838 argv_array_push(&args, "--from-promisor");
839 if (options.no_dependents)
840 argv_array_push(&args, "--no-dependents");
842 argv_array_pushf(&args, "--filter=%s", options.filter);
843 argv_array_push(&args, url.buf);
845 for (i = 0; i < nr_heads; i++) {
846 struct ref *ref = to_fetch[i];
848 die("cannot fetch by sha1 over smart http");
849 packet_buf_write(&preamble, "%s %s\n",
850 oid_to_hex(&ref->old_oid), ref->name);
852 packet_buf_flush(&preamble);
854 memset(&rpc, 0, sizeof(rpc));
855 rpc.service_name = "git-upload-pack",
856 rpc.argv = args.argv;
857 rpc.stdin_preamble = &preamble;
858 rpc.gzip_request = 1;
860 err = rpc_service(&rpc, heads);
862 write_or_die(1, rpc.result.buf, rpc.result.len);
863 strbuf_release(&rpc.result);
864 strbuf_release(&preamble);
865 argv_array_clear(&args);
869 static int fetch(int nr_heads, struct ref **to_fetch)
871 struct discovery *d = discover_refs("git-upload-pack", 0);
873 return fetch_git(d, nr_heads, to_fetch);
875 return fetch_dumb(nr_heads, to_fetch);
878 static void parse_fetch(struct strbuf *buf)
880 struct ref **to_fetch = NULL;
881 struct ref *list_head = NULL;
882 struct ref **list = &list_head;
883 int alloc_heads = 0, nr_heads = 0;
887 if (skip_prefix(buf->buf, "fetch ", &p)) {
890 struct object_id old_oid;
892 if (get_oid_hex(p, &old_oid))
893 die("protocol error: expected sha/ref, got %s'", p);
894 if (p[GIT_SHA1_HEXSZ] == ' ')
895 name = p + GIT_SHA1_HEXSZ + 1;
896 else if (!p[GIT_SHA1_HEXSZ])
899 die("protocol error: expected sha/ref, got %s'", p);
901 ref = alloc_ref(name);
902 oidcpy(&ref->old_oid, &old_oid);
907 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
908 to_fetch[nr_heads++] = ref;
911 die("http transport does not support %s", buf->buf);
914 if (strbuf_getline_lf(buf, stdin) == EOF)
920 if (fetch(nr_heads, to_fetch))
921 exit(128); /* error already reported */
922 free_refs(list_head);
930 static int push_dav(int nr_spec, char **specs)
932 struct child_process child = CHILD_PROCESS_INIT;
936 argv_array_push(&child.args, "http-push");
937 argv_array_push(&child.args, "--helper-status");
939 argv_array_push(&child.args, "--dry-run");
940 if (options.verbosity > 1)
941 argv_array_push(&child.args, "--verbose");
942 argv_array_push(&child.args, url.buf);
943 for (i = 0; i < nr_spec; i++)
944 argv_array_push(&child.args, specs[i]);
946 if (run_command(&child))
947 die("git-http-push failed");
951 static int push_git(struct discovery *heads, int nr_spec, char **specs)
953 struct rpc_state rpc;
955 struct argv_array args;
956 struct string_list_item *cas_option;
957 struct strbuf preamble = STRBUF_INIT;
959 argv_array_init(&args);
960 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
964 argv_array_push(&args, "--thin");
966 argv_array_push(&args, "--dry-run");
967 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
968 argv_array_push(&args, "--signed=yes");
969 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
970 argv_array_push(&args, "--signed=if-asked");
971 if (options.verbosity == 0)
972 argv_array_push(&args, "--quiet");
973 else if (options.verbosity > 1)
974 argv_array_push(&args, "--verbose");
975 for (i = 0; i < options.push_options.nr; i++)
976 argv_array_pushf(&args, "--push-option=%s",
977 options.push_options.items[i].string);
978 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
979 for_each_string_list_item(cas_option, &cas_options)
980 argv_array_push(&args, cas_option->string);
981 argv_array_push(&args, url.buf);
983 argv_array_push(&args, "--stdin");
984 for (i = 0; i < nr_spec; i++)
985 packet_buf_write(&preamble, "%s\n", specs[i]);
986 packet_buf_flush(&preamble);
988 memset(&rpc, 0, sizeof(rpc));
989 rpc.service_name = "git-receive-pack",
990 rpc.argv = args.argv;
991 rpc.stdin_preamble = &preamble;
993 err = rpc_service(&rpc, heads);
995 write_or_die(1, rpc.result.buf, rpc.result.len);
996 strbuf_release(&rpc.result);
997 strbuf_release(&preamble);
998 argv_array_clear(&args);
1002 static int push(int nr_spec, char **specs)
1004 struct discovery *heads = discover_refs("git-receive-pack", 1);
1007 if (heads->proto_git)
1008 ret = push_git(heads, nr_spec, specs);
1010 ret = push_dav(nr_spec, specs);
1011 free_discovery(heads);
1015 static void parse_push(struct strbuf *buf)
1017 char **specs = NULL;
1018 int alloc_spec = 0, nr_spec = 0, i, ret;
1021 if (starts_with(buf->buf, "push ")) {
1022 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1023 specs[nr_spec++] = xstrdup(buf->buf + 5);
1026 die("http transport does not support %s", buf->buf);
1029 if (strbuf_getline_lf(buf, stdin) == EOF)
1035 ret = push(nr_spec, specs);
1040 exit(128); /* error already reported */
1043 for (i = 0; i < nr_spec; i++)
1048 int cmd_main(int argc, const char **argv)
1050 struct strbuf buf = STRBUF_INIT;
1053 setup_git_directory_gently(&nongit);
1055 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1059 options.verbosity = 1;
1060 options.progress = !!isatty(2);
1062 string_list_init(&options.deepen_not, 1);
1063 string_list_init(&options.push_options, 1);
1065 remote = remote_get(argv[1]);
1068 end_url_with_slash(&url, argv[2]);
1070 end_url_with_slash(&url, remote->url[0]);
1073 http_init(remote, url.buf, 0);
1078 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1080 error("remote-curl: error reading command stream from git");
1085 if (starts_with(buf.buf, "fetch ")) {
1087 die("remote-curl: fetch attempted without a local repo");
1090 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1091 int for_push = !!strstr(buf.buf + 4, "for-push");
1092 output_refs(get_refs(for_push));
1094 } else if (starts_with(buf.buf, "push ")) {
1097 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1098 char *value = strchr(arg, ' ');
1106 result = set_option(arg, value);
1109 else if (result < 0)
1110 printf("error invalid value\n");
1112 printf("unsupported\n");
1115 } else if (!strcmp(buf.buf, "capabilities")) {
1119 printf("check-connectivity\n");
1123 error("remote-curl: unknown command '%s' from git", buf.buf);