7 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
12 #include "credential.h"
14 static struct remote *remote;
15 /* always ends with a trailing slash */
16 static struct strbuf url = STRBUF_INIT;
21 unsigned progress : 1,
22 check_self_contained_and_connected : 1,
27 static struct options options;
28 static struct string_list cas_options = STRING_LIST_INIT_DUP;
30 static int set_option(const char *name, const char *value)
32 if (!strcmp(name, "verbosity")) {
34 int v = strtol(value, &end, 10);
35 if (value == end || *end)
37 options.verbosity = v;
40 else if (!strcmp(name, "progress")) {
41 if (!strcmp(value, "true"))
43 else if (!strcmp(value, "false"))
49 else if (!strcmp(name, "depth")) {
51 unsigned long v = strtoul(value, &end, 10);
52 if (value == end || *end)
57 else if (!strcmp(name, "followtags")) {
58 if (!strcmp(value, "true"))
59 options.followtags = 1;
60 else if (!strcmp(value, "false"))
61 options.followtags = 0;
66 else if (!strcmp(name, "dry-run")) {
67 if (!strcmp(value, "true"))
69 else if (!strcmp(value, "false"))
75 else if (!strcmp(name, "check-connectivity")) {
76 if (!strcmp(value, "true"))
77 options.check_self_contained_and_connected = 1;
78 else if (!strcmp(value, "false"))
79 options.check_self_contained_and_connected = 0;
84 else if (!strcmp(name, "cas")) {
85 struct strbuf val = STRBUF_INIT;
86 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
87 string_list_append(&cas_options, val.buf);
92 return 1 /* unsupported */;
102 unsigned proto_git : 1;
104 static struct discovery *last_discovery;
106 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
108 struct ref *list = NULL;
109 get_remote_heads(-1, heads->buf, heads->len, &list,
110 for_push ? REF_NORMAL : 0, NULL);
114 static struct ref *parse_info_refs(struct discovery *heads)
116 char *data, *start, *mid;
120 struct ref *refs = NULL;
121 struct ref *ref = NULL;
122 struct ref *last_ref = NULL;
127 while (i < heads->len) {
133 if (data[i] == '\n') {
134 if (mid - start != 40)
135 die("%sinfo/refs not valid: is this a git repository?",
139 ref = xmalloc(sizeof(struct ref) +
140 strlen(ref_name) + 1);
141 memset(ref, 0, sizeof(struct ref));
142 strcpy(ref->name, ref_name);
143 get_sha1_hex(start, ref->old_sha1);
147 last_ref->next = ref;
154 ref = alloc_ref("HEAD");
155 if (!http_fetch_ref(url.buf, ref) &&
156 !resolve_remote_symref(ref, refs)) {
166 static void free_discovery(struct discovery *d)
169 if (d == last_discovery)
170 last_discovery = NULL;
177 static int show_http_message(struct strbuf *type, struct strbuf *msg)
182 * We only show text/plain parts, as other types are likely
183 * to be ugly to look at on the user's terminal.
185 * TODO should handle "; charset=XXX", and re-encode into
188 if (strcasecmp(type->buf, "text/plain"))
197 eol = strchrnul(p, '\n');
198 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
204 static struct discovery* discover_refs(const char *service, int for_push)
206 struct strbuf exp = STRBUF_INIT;
207 struct strbuf type = STRBUF_INIT;
208 struct strbuf buffer = STRBUF_INIT;
209 struct strbuf refs_url = STRBUF_INIT;
210 struct strbuf effective_url = STRBUF_INIT;
211 struct discovery *last = last_discovery;
212 int http_ret, maybe_smart = 0;
213 struct http_get_options options;
215 if (last && !strcmp(service, last->service))
217 free_discovery(last);
219 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
220 if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
221 git_env_bool("GIT_SMART_HTTP", 1)) {
223 if (!strchr(url.buf, '?'))
224 strbuf_addch(&refs_url, '?');
226 strbuf_addch(&refs_url, '&');
227 strbuf_addf(&refs_url, "service=%s", service);
230 memset(&options, 0, sizeof(options));
231 options.content_type = &type;
232 options.effective_url = &effective_url;
233 options.base_url = &url;
234 options.no_cache = 1;
235 options.keep_error = 1;
237 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
241 case HTTP_MISSING_TARGET:
242 show_http_message(&type, &buffer);
243 die("repository '%s' not found", url.buf);
245 show_http_message(&type, &buffer);
246 die("Authentication failed for '%s'", url.buf);
248 show_http_message(&type, &buffer);
249 die("unable to access '%s': %s", url.buf, curl_errorstr);
252 last= xcalloc(1, sizeof(*last_discovery));
253 last->service = service;
254 last->buf_alloc = strbuf_detach(&buffer, &last->len);
255 last->buf = last->buf_alloc;
257 strbuf_addf(&exp, "application/x-%s-advertisement", service);
259 (5 <= last->len && last->buf[4] == '#') &&
260 !strbuf_cmp(&exp, &type)) {
264 * smart HTTP response; validate that the service
265 * pkt-line matches our request.
267 line = packet_read_line_buf(&last->buf, &last->len, NULL);
270 strbuf_addf(&exp, "# service=%s", service);
271 if (strcmp(line, exp.buf))
272 die("invalid server response; got '%s'", line);
273 strbuf_release(&exp);
275 /* The header can include additional metadata lines, up
276 * until a packet flush marker. Ignore these now, but
277 * in the future we might start to scan them.
279 while (packet_read_line_buf(&last->buf, &last->len, NULL))
286 last->refs = parse_git_refs(last, for_push);
288 last->refs = parse_info_refs(last);
290 strbuf_release(&refs_url);
291 strbuf_release(&exp);
292 strbuf_release(&type);
293 strbuf_release(&effective_url);
294 strbuf_release(&buffer);
295 last_discovery = last;
299 static struct ref *get_refs(int for_push)
301 struct discovery *heads;
304 heads = discover_refs("git-receive-pack", for_push);
306 heads = discover_refs("git-upload-pack", for_push);
311 static void output_refs(struct ref *refs)
314 for (posn = refs; posn; posn = posn->next) {
316 printf("@%s %s\n", posn->symref, posn->name);
318 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
325 const char *service_name;
327 struct strbuf *stdin_preamble;
329 char *hdr_content_type;
337 struct strbuf result;
338 unsigned gzip_request : 1;
339 unsigned initial_buffer : 1;
342 static size_t rpc_out(void *ptr, size_t eltsize,
343 size_t nmemb, void *buffer_)
345 size_t max = eltsize * nmemb;
346 struct rpc_state *rpc = buffer_;
347 size_t avail = rpc->len - rpc->pos;
350 rpc->initial_buffer = 0;
351 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
360 memcpy(ptr, rpc->buf + rpc->pos, avail);
365 #ifndef NO_CURL_IOCTL
366 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
368 struct rpc_state *rpc = clientp;
374 case CURLIOCMD_RESTARTREAD:
375 if (rpc->initial_buffer) {
379 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
380 return CURLIOE_FAILRESTART;
383 return CURLIOE_UNKNOWNCMD;
388 static size_t rpc_in(char *ptr, size_t eltsize,
389 size_t nmemb, void *buffer_)
391 size_t size = eltsize * nmemb;
392 struct rpc_state *rpc = buffer_;
393 write_or_die(rpc->in, ptr, size);
397 static int run_slot(struct active_request_slot *slot)
400 struct slot_results results;
402 slot->results = &results;
403 slot->curl_result = curl_easy_perform(slot->curl);
404 finish_active_slot(slot);
406 err = handle_curl_result(&results);
407 if (err != HTTP_OK && err != HTTP_REAUTH) {
408 error("RPC failed; result=%d, HTTP code = %ld",
409 results.curl_result, results.http_code);
415 static int probe_rpc(struct rpc_state *rpc)
417 struct active_request_slot *slot;
418 struct curl_slist *headers = NULL;
419 struct strbuf buf = STRBUF_INIT;
422 slot = get_active_slot();
424 headers = curl_slist_append(headers, rpc->hdr_content_type);
425 headers = curl_slist_append(headers, rpc->hdr_accept);
427 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
428 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
429 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
430 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
431 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
432 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
433 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
434 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
435 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
437 err = run_slot(slot);
439 curl_slist_free_all(headers);
440 strbuf_release(&buf);
444 static int post_rpc(struct rpc_state *rpc)
446 struct active_request_slot *slot;
447 struct curl_slist *headers = NULL;
448 int use_gzip = rpc->gzip_request;
449 char *gzip_body = NULL;
450 size_t gzip_size = 0;
451 int err, large_request = 0;
453 /* Try to load the entire request, if we can fit it into the
454 * allocated buffer space we can use HTTP/1.0 and avoid the
455 * chunked encoding mess.
458 size_t left = rpc->alloc - rpc->len;
459 char *buf = rpc->buf + rpc->len;
462 if (left < LARGE_PACKET_MAX) {
468 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
476 err = probe_rpc(rpc);
477 if (err == HTTP_REAUTH)
478 credential_fill(&http_auth);
479 } while (err == HTTP_REAUTH);
484 headers = curl_slist_append(headers, rpc->hdr_content_type);
485 headers = curl_slist_append(headers, rpc->hdr_accept);
486 headers = curl_slist_append(headers, "Expect:");
489 slot = get_active_slot();
491 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
492 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
493 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
494 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
497 /* The request body is large and the size cannot be predicted.
498 * We must use chunked encoding to send it.
500 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
501 rpc->initial_buffer = 1;
502 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
503 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
504 #ifndef NO_CURL_IOCTL
505 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
506 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
508 if (options.verbosity > 1) {
509 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
513 } else if (gzip_body) {
515 * If we are looping to retry authentication, then the previous
516 * run will have set up the headers and gzip buffer already,
517 * and we just need to send it.
519 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
520 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
522 } else if (use_gzip && 1024 < rpc->len) {
523 /* The client backend isn't giving us compressed data so
524 * we can try to deflate it ourselves, this may save on.
530 memset(&stream, 0, sizeof(stream));
531 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
532 gzip_size = git_deflate_bound(&stream, rpc->len);
533 gzip_body = xmalloc(gzip_size);
535 stream.next_in = (unsigned char *)rpc->buf;
536 stream.avail_in = rpc->len;
537 stream.next_out = (unsigned char *)gzip_body;
538 stream.avail_out = gzip_size;
540 ret = git_deflate(&stream, Z_FINISH);
541 if (ret != Z_STREAM_END)
542 die("cannot deflate request; zlib deflate error %d", ret);
544 ret = git_deflate_end_gently(&stream);
546 die("cannot deflate request; zlib end error %d", ret);
548 gzip_size = stream.total_out;
550 headers = curl_slist_append(headers, "Content-Encoding: gzip");
551 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
552 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
554 if (options.verbosity > 1) {
555 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
557 (unsigned long)rpc->len, (unsigned long)gzip_size);
561 /* We know the complete request size in advance, use the
562 * more normal Content-Length approach.
564 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
565 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
566 if (options.verbosity > 1) {
567 fprintf(stderr, "POST %s (%lu bytes)\n",
568 rpc->service_name, (unsigned long)rpc->len);
573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
574 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
575 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
577 err = run_slot(slot);
578 if (err == HTTP_REAUTH && !large_request) {
579 credential_fill(&http_auth);
585 curl_slist_free_all(headers);
590 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
592 const char *svc = rpc->service_name;
593 struct strbuf buf = STRBUF_INIT;
594 struct strbuf *preamble = rpc->stdin_preamble;
595 struct child_process client;
598 memset(&client, 0, sizeof(client));
602 client.argv = rpc->argv;
603 if (start_command(&client))
606 write_or_die(client.in, preamble->buf, preamble->len);
608 write_or_die(client.in, heads->buf, heads->len);
610 rpc->alloc = http_post_buffer;
611 rpc->buf = xmalloc(rpc->alloc);
613 rpc->out = client.out;
614 strbuf_init(&rpc->result, 0);
616 strbuf_addf(&buf, "%s%s", url.buf, svc);
617 rpc->service_url = strbuf_detach(&buf, NULL);
619 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
620 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
622 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
623 rpc->hdr_accept = strbuf_detach(&buf, NULL);
626 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
631 err |= post_rpc(rpc);
637 strbuf_read(&rpc->result, client.out, 0);
641 if (xread(client.out, buf, sizeof(buf)) <= 0)
648 err |= finish_command(&client);
649 free(rpc->service_url);
650 free(rpc->hdr_content_type);
651 free(rpc->hdr_accept);
653 strbuf_release(&buf);
657 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
659 struct walker *walker;
660 char **targets = xmalloc(nr_heads * sizeof(char*));
664 die("dumb http transport does not support --depth");
665 for (i = 0; i < nr_heads; i++)
666 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
668 walker = get_http_walker(url.buf);
670 walker->get_tree = 1;
671 walker->get_history = 1;
672 walker->get_verbosely = options.verbosity >= 3;
673 walker->get_recover = 0;
674 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
677 for (i = 0; i < nr_heads; i++)
681 return ret ? error("Fetch failed.") : 0;
684 static int fetch_git(struct discovery *heads,
685 int nr_heads, struct ref **to_fetch)
687 struct rpc_state rpc;
688 struct strbuf preamble = STRBUF_INIT;
689 char *depth_arg = NULL;
690 int argc = 0, i, err;
691 const char *argv[16];
693 argv[argc++] = "fetch-pack";
694 argv[argc++] = "--stateless-rpc";
695 argv[argc++] = "--stdin";
696 argv[argc++] = "--lock-pack";
697 if (options.followtags)
698 argv[argc++] = "--include-tag";
700 argv[argc++] = "--thin";
701 if (options.verbosity >= 3) {
705 if (options.check_self_contained_and_connected)
706 argv[argc++] = "--check-self-contained-and-connected";
707 if (!options.progress)
708 argv[argc++] = "--no-progress";
710 struct strbuf buf = STRBUF_INIT;
711 strbuf_addf(&buf, "--depth=%lu", options.depth);
712 depth_arg = strbuf_detach(&buf, NULL);
713 argv[argc++] = depth_arg;
715 argv[argc++] = url.buf;
718 for (i = 0; i < nr_heads; i++) {
719 struct ref *ref = to_fetch[i];
720 if (!ref->name || !*ref->name)
721 die("cannot fetch by sha1 over smart http");
722 packet_buf_write(&preamble, "%s\n", ref->name);
724 packet_buf_flush(&preamble);
726 memset(&rpc, 0, sizeof(rpc));
727 rpc.service_name = "git-upload-pack",
729 rpc.stdin_preamble = &preamble;
730 rpc.gzip_request = 1;
732 err = rpc_service(&rpc, heads);
734 write_or_die(1, rpc.result.buf, rpc.result.len);
735 strbuf_release(&rpc.result);
736 strbuf_release(&preamble);
741 static int fetch(int nr_heads, struct ref **to_fetch)
743 struct discovery *d = discover_refs("git-upload-pack", 0);
745 return fetch_git(d, nr_heads, to_fetch);
747 return fetch_dumb(nr_heads, to_fetch);
750 static void parse_fetch(struct strbuf *buf)
752 struct ref **to_fetch = NULL;
753 struct ref *list_head = NULL;
754 struct ref **list = &list_head;
755 int alloc_heads = 0, nr_heads = 0;
758 if (!prefixcmp(buf->buf, "fetch ")) {
759 char *p = buf->buf + strlen("fetch ");
762 unsigned char old_sha1[20];
764 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
765 die("protocol error: expected sha/ref, got %s'", p);
771 die("protocol error: expected sha/ref, got %s'", p);
773 ref = alloc_ref(name);
774 hashcpy(ref->old_sha1, old_sha1);
779 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
780 to_fetch[nr_heads++] = ref;
783 die("http transport does not support %s", buf->buf);
786 if (strbuf_getline(buf, stdin, '\n') == EOF)
792 if (fetch(nr_heads, to_fetch))
793 exit(128); /* error already reported */
794 free_refs(list_head);
802 static int push_dav(int nr_spec, char **specs)
804 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
807 argv[argc++] = "http-push";
808 argv[argc++] = "--helper-status";
810 argv[argc++] = "--dry-run";
811 if (options.verbosity > 1)
812 argv[argc++] = "--verbose";
813 argv[argc++] = url.buf;
814 for (i = 0; i < nr_spec; i++)
815 argv[argc++] = specs[i];
818 if (run_command_v_opt(argv, RUN_GIT_CMD))
819 die("git-%s failed", argv[0]);
824 static int push_git(struct discovery *heads, int nr_spec, char **specs)
826 struct rpc_state rpc;
828 struct argv_array args;
829 struct string_list_item *cas_option;
831 argv_array_init(&args);
832 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
836 argv_array_push(&args, "--thin");
838 argv_array_push(&args, "--dry-run");
839 if (options.verbosity == 0)
840 argv_array_push(&args, "--quiet");
841 else if (options.verbosity > 1)
842 argv_array_push(&args, "--verbose");
843 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
844 for_each_string_list_item(cas_option, &cas_options)
845 argv_array_push(&args, cas_option->string);
846 argv_array_push(&args, url.buf);
847 for (i = 0; i < nr_spec; i++)
848 argv_array_push(&args, specs[i]);
850 memset(&rpc, 0, sizeof(rpc));
851 rpc.service_name = "git-receive-pack",
852 rpc.argv = args.argv;
854 err = rpc_service(&rpc, heads);
856 write_or_die(1, rpc.result.buf, rpc.result.len);
857 strbuf_release(&rpc.result);
858 argv_array_clear(&args);
862 static int push(int nr_spec, char **specs)
864 struct discovery *heads = discover_refs("git-receive-pack", 1);
867 if (heads->proto_git)
868 ret = push_git(heads, nr_spec, specs);
870 ret = push_dav(nr_spec, specs);
871 free_discovery(heads);
875 static void parse_push(struct strbuf *buf)
878 int alloc_spec = 0, nr_spec = 0, i, ret;
881 if (!prefixcmp(buf->buf, "push ")) {
882 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
883 specs[nr_spec++] = xstrdup(buf->buf + 5);
886 die("http transport does not support %s", buf->buf);
889 if (strbuf_getline(buf, stdin, '\n') == EOF)
895 ret = push(nr_spec, specs);
900 exit(128); /* error already reported */
903 for (i = 0; i < nr_spec; i++)
908 int main(int argc, const char **argv)
910 struct strbuf buf = STRBUF_INIT;
913 git_extract_argv0_path(argv[0]);
914 setup_git_directory_gently(&nongit);
916 fprintf(stderr, "Remote needed\n");
920 options.verbosity = 1;
921 options.progress = !!isatty(2);
924 remote = remote_get(argv[1]);
927 end_url_with_slash(&url, argv[2]);
929 end_url_with_slash(&url, remote->url[0]);
932 http_init(remote, url.buf, 0);
935 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
937 fprintf(stderr, "Error reading command stream\n");
939 fprintf(stderr, "Unexpected end of command stream\n");
944 if (!prefixcmp(buf.buf, "fetch ")) {
946 die("Fetch attempted without a local repo");
949 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
950 int for_push = !!strstr(buf.buf + 4, "for-push");
951 output_refs(get_refs(for_push));
953 } else if (!prefixcmp(buf.buf, "push ")) {
956 } else if (!prefixcmp(buf.buf, "option ")) {
957 char *name = buf.buf + strlen("option ");
958 char *value = strchr(name, ' ');
966 result = set_option(name, value);
970 printf("error invalid value\n");
972 printf("unsupported\n");
975 } else if (!strcmp(buf.buf, "capabilities")) {
979 printf("check-connectivity\n");
983 fprintf(stderr, "Unknown command '%s'\n", buf.buf);