7 #include "run-command.h"
10 #include "argv-array.h"
12 static struct remote *remote;
13 static const char *url; /* always ends with a trailing slash */
18 unsigned progress : 1,
19 check_self_contained_and_connected : 1,
24 static struct options options;
26 static int set_option(const char *name, const char *value)
28 if (!strcmp(name, "verbosity")) {
30 int v = strtol(value, &end, 10);
31 if (value == end || *end)
33 options.verbosity = v;
36 else if (!strcmp(name, "progress")) {
37 if (!strcmp(value, "true"))
39 else if (!strcmp(value, "false"))
45 else if (!strcmp(name, "depth")) {
47 unsigned long v = strtoul(value, &end, 10);
48 if (value == end || *end)
53 else if (!strcmp(name, "followtags")) {
54 if (!strcmp(value, "true"))
55 options.followtags = 1;
56 else if (!strcmp(value, "false"))
57 options.followtags = 0;
62 else if (!strcmp(name, "dry-run")) {
63 if (!strcmp(value, "true"))
65 else if (!strcmp(value, "false"))
71 else if (!strcmp(name, "check-connectivity")) {
72 if (!strcmp(value, "true"))
73 options.check_self_contained_and_connected = 1;
74 else if (!strcmp(value, "false"))
75 options.check_self_contained_and_connected = 0;
81 return 1 /* unsupported */;
91 unsigned proto_git : 1;
93 static struct discovery *last_discovery;
95 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
97 struct ref *list = NULL;
98 get_remote_heads(-1, heads->buf, heads->len, &list,
99 for_push ? REF_NORMAL : 0, NULL);
103 static struct ref *parse_info_refs(struct discovery *heads)
105 char *data, *start, *mid;
109 struct ref *refs = NULL;
110 struct ref *ref = NULL;
111 struct ref *last_ref = NULL;
116 while (i < heads->len) {
122 if (data[i] == '\n') {
123 if (mid - start != 40)
124 die("%sinfo/refs not valid: is this a git repository?", url);
127 ref = xmalloc(sizeof(struct ref) +
128 strlen(ref_name) + 1);
129 memset(ref, 0, sizeof(struct ref));
130 strcpy(ref->name, ref_name);
131 get_sha1_hex(start, ref->old_sha1);
135 last_ref->next = ref;
142 ref = alloc_ref("HEAD");
143 if (!http_fetch_ref(url, ref) &&
144 !resolve_remote_symref(ref, refs)) {
154 static void free_discovery(struct discovery *d)
157 if (d == last_discovery)
158 last_discovery = NULL;
165 static int show_http_message(struct strbuf *type, struct strbuf *msg)
170 * We only show text/plain parts, as other types are likely
171 * to be ugly to look at on the user's terminal.
173 * TODO should handle "; charset=XXX", and re-encode into
176 if (strcasecmp(type->buf, "text/plain"))
185 eol = strchrnul(p, '\n');
186 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
192 static struct discovery* discover_refs(const char *service, int for_push)
194 struct strbuf exp = STRBUF_INIT;
195 struct strbuf type = STRBUF_INIT;
196 struct strbuf buffer = STRBUF_INIT;
197 struct discovery *last = last_discovery;
199 int http_ret, maybe_smart = 0;
201 if (last && !strcmp(service, last->service))
203 free_discovery(last);
205 strbuf_addf(&buffer, "%sinfo/refs", url);
206 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
207 git_env_bool("GIT_SMART_HTTP", 1)) {
209 if (!strchr(url, '?'))
210 strbuf_addch(&buffer, '?');
212 strbuf_addch(&buffer, '&');
213 strbuf_addf(&buffer, "service=%s", service);
215 refs_url = strbuf_detach(&buffer, NULL);
217 http_ret = http_get_strbuf(refs_url, &type, &buffer,
218 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
222 case HTTP_MISSING_TARGET:
223 show_http_message(&type, &buffer);
224 die("repository '%s' not found", url);
226 show_http_message(&type, &buffer);
227 die("Authentication failed for '%s'", url);
229 show_http_message(&type, &buffer);
230 die("unable to access '%s': %s", url, curl_errorstr);
233 last= xcalloc(1, sizeof(*last_discovery));
234 last->service = service;
235 last->buf_alloc = strbuf_detach(&buffer, &last->len);
236 last->buf = last->buf_alloc;
238 strbuf_addf(&exp, "application/x-%s-advertisement", service);
240 (5 <= last->len && last->buf[4] == '#') &&
241 !strbuf_cmp(&exp, &type)) {
245 * smart HTTP response; validate that the service
246 * pkt-line matches our request.
248 line = packet_read_line_buf(&last->buf, &last->len, NULL);
251 strbuf_addf(&exp, "# service=%s", service);
252 if (strcmp(line, exp.buf))
253 die("invalid server response; got '%s'", line);
254 strbuf_release(&exp);
256 /* The header can include additional metadata lines, up
257 * until a packet flush marker. Ignore these now, but
258 * in the future we might start to scan them.
260 while (packet_read_line_buf(&last->buf, &last->len, NULL))
267 last->refs = parse_git_refs(last, for_push);
269 last->refs = parse_info_refs(last);
272 strbuf_release(&exp);
273 strbuf_release(&type);
274 strbuf_release(&buffer);
275 last_discovery = last;
279 static struct ref *get_refs(int for_push)
281 struct discovery *heads;
284 heads = discover_refs("git-receive-pack", for_push);
286 heads = discover_refs("git-upload-pack", for_push);
291 static void output_refs(struct ref *refs)
294 for (posn = refs; posn; posn = posn->next) {
296 printf("@%s %s\n", posn->symref, posn->name);
298 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
305 const char *service_name;
307 struct strbuf *stdin_preamble;
309 char *hdr_content_type;
317 struct strbuf result;
318 unsigned gzip_request : 1;
319 unsigned initial_buffer : 1;
322 static size_t rpc_out(void *ptr, size_t eltsize,
323 size_t nmemb, void *buffer_)
325 size_t max = eltsize * nmemb;
326 struct rpc_state *rpc = buffer_;
327 size_t avail = rpc->len - rpc->pos;
330 rpc->initial_buffer = 0;
331 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
340 memcpy(ptr, rpc->buf + rpc->pos, avail);
345 #ifndef NO_CURL_IOCTL
346 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
348 struct rpc_state *rpc = clientp;
354 case CURLIOCMD_RESTARTREAD:
355 if (rpc->initial_buffer) {
359 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
360 return CURLIOE_FAILRESTART;
363 return CURLIOE_UNKNOWNCMD;
368 static size_t rpc_in(char *ptr, size_t eltsize,
369 size_t nmemb, void *buffer_)
371 size_t size = eltsize * nmemb;
372 struct rpc_state *rpc = buffer_;
373 write_or_die(rpc->in, ptr, size);
377 static int run_slot(struct active_request_slot *slot)
380 struct slot_results results;
382 slot->results = &results;
383 slot->curl_result = curl_easy_perform(slot->curl);
384 finish_active_slot(slot);
386 err = handle_curl_result(&results);
387 if (err != HTTP_OK && err != HTTP_REAUTH) {
388 error("RPC failed; result=%d, HTTP code = %ld",
389 results.curl_result, results.http_code);
395 static int probe_rpc(struct rpc_state *rpc)
397 struct active_request_slot *slot;
398 struct curl_slist *headers = NULL;
399 struct strbuf buf = STRBUF_INIT;
402 slot = get_active_slot();
404 headers = curl_slist_append(headers, rpc->hdr_content_type);
405 headers = curl_slist_append(headers, rpc->hdr_accept);
407 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
408 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
409 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
410 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
411 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
412 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
413 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
414 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
415 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
417 err = run_slot(slot);
419 curl_slist_free_all(headers);
420 strbuf_release(&buf);
424 static int post_rpc(struct rpc_state *rpc)
426 struct active_request_slot *slot;
427 struct curl_slist *headers = NULL;
428 int use_gzip = rpc->gzip_request;
429 char *gzip_body = NULL;
430 size_t gzip_size = 0;
431 int err, large_request = 0;
433 /* Try to load the entire request, if we can fit it into the
434 * allocated buffer space we can use HTTP/1.0 and avoid the
435 * chunked encoding mess.
438 size_t left = rpc->alloc - rpc->len;
439 char *buf = rpc->buf + rpc->len;
442 if (left < LARGE_PACKET_MAX) {
448 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
456 err = probe_rpc(rpc);
457 } while (err == HTTP_REAUTH);
462 headers = curl_slist_append(headers, rpc->hdr_content_type);
463 headers = curl_slist_append(headers, rpc->hdr_accept);
464 headers = curl_slist_append(headers, "Expect:");
467 slot = get_active_slot();
469 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
470 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
471 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
472 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
475 /* The request body is large and the size cannot be predicted.
476 * We must use chunked encoding to send it.
478 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
479 rpc->initial_buffer = 1;
480 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
481 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
482 #ifndef NO_CURL_IOCTL
483 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
484 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
486 if (options.verbosity > 1) {
487 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
491 } else if (gzip_body) {
493 * If we are looping to retry authentication, then the previous
494 * run will have set up the headers and gzip buffer already,
495 * and we just need to send it.
497 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
498 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
500 } else if (use_gzip && 1024 < rpc->len) {
501 /* The client backend isn't giving us compressed data so
502 * we can try to deflate it ourselves, this may save on.
508 memset(&stream, 0, sizeof(stream));
509 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
510 gzip_size = git_deflate_bound(&stream, rpc->len);
511 gzip_body = xmalloc(gzip_size);
513 stream.next_in = (unsigned char *)rpc->buf;
514 stream.avail_in = rpc->len;
515 stream.next_out = (unsigned char *)gzip_body;
516 stream.avail_out = gzip_size;
518 ret = git_deflate(&stream, Z_FINISH);
519 if (ret != Z_STREAM_END)
520 die("cannot deflate request; zlib deflate error %d", ret);
522 ret = git_deflate_end_gently(&stream);
524 die("cannot deflate request; zlib end error %d", ret);
526 gzip_size = stream.total_out;
528 headers = curl_slist_append(headers, "Content-Encoding: gzip");
529 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
530 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
532 if (options.verbosity > 1) {
533 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
535 (unsigned long)rpc->len, (unsigned long)gzip_size);
539 /* We know the complete request size in advance, use the
540 * more normal Content-Length approach.
542 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
543 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
544 if (options.verbosity > 1) {
545 fprintf(stderr, "POST %s (%lu bytes)\n",
546 rpc->service_name, (unsigned long)rpc->len);
551 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
552 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
553 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
555 err = run_slot(slot);
556 if (err == HTTP_REAUTH && !large_request)
561 curl_slist_free_all(headers);
566 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
568 const char *svc = rpc->service_name;
569 struct strbuf buf = STRBUF_INIT;
570 struct strbuf *preamble = rpc->stdin_preamble;
571 struct child_process client;
574 memset(&client, 0, sizeof(client));
578 client.argv = rpc->argv;
579 if (start_command(&client))
582 write_or_die(client.in, preamble->buf, preamble->len);
584 write_or_die(client.in, heads->buf, heads->len);
586 rpc->alloc = http_post_buffer;
587 rpc->buf = xmalloc(rpc->alloc);
589 rpc->out = client.out;
590 strbuf_init(&rpc->result, 0);
592 strbuf_addf(&buf, "%s%s", url, svc);
593 rpc->service_url = strbuf_detach(&buf, NULL);
595 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
596 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
598 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
599 rpc->hdr_accept = strbuf_detach(&buf, NULL);
602 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
607 err |= post_rpc(rpc);
613 strbuf_read(&rpc->result, client.out, 0);
617 if (xread(client.out, buf, sizeof(buf)) <= 0)
624 err |= finish_command(&client);
625 free(rpc->service_url);
626 free(rpc->hdr_content_type);
627 free(rpc->hdr_accept);
629 strbuf_release(&buf);
633 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
635 struct walker *walker;
636 char **targets = xmalloc(nr_heads * sizeof(char*));
640 die("dumb http transport does not support --depth");
641 for (i = 0; i < nr_heads; i++)
642 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
644 walker = get_http_walker(url);
646 walker->get_tree = 1;
647 walker->get_history = 1;
648 walker->get_verbosely = options.verbosity >= 3;
649 walker->get_recover = 0;
650 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
653 for (i = 0; i < nr_heads; i++)
657 return ret ? error("Fetch failed.") : 0;
660 static int fetch_git(struct discovery *heads,
661 int nr_heads, struct ref **to_fetch)
663 struct rpc_state rpc;
664 struct strbuf preamble = STRBUF_INIT;
665 char *depth_arg = NULL;
666 int argc = 0, i, err;
667 const char *argv[16];
669 argv[argc++] = "fetch-pack";
670 argv[argc++] = "--stateless-rpc";
671 argv[argc++] = "--stdin";
672 argv[argc++] = "--lock-pack";
673 if (options.followtags)
674 argv[argc++] = "--include-tag";
676 argv[argc++] = "--thin";
677 if (options.verbosity >= 3) {
681 if (options.check_self_contained_and_connected)
682 argv[argc++] = "--check-self-contained-and-connected";
683 if (!options.progress)
684 argv[argc++] = "--no-progress";
686 struct strbuf buf = STRBUF_INIT;
687 strbuf_addf(&buf, "--depth=%lu", options.depth);
688 depth_arg = strbuf_detach(&buf, NULL);
689 argv[argc++] = depth_arg;
694 for (i = 0; i < nr_heads; i++) {
695 struct ref *ref = to_fetch[i];
696 if (!ref->name || !*ref->name)
697 die("cannot fetch by sha1 over smart http");
698 packet_buf_write(&preamble, "%s\n", ref->name);
700 packet_buf_flush(&preamble);
702 memset(&rpc, 0, sizeof(rpc));
703 rpc.service_name = "git-upload-pack",
705 rpc.stdin_preamble = &preamble;
706 rpc.gzip_request = 1;
708 err = rpc_service(&rpc, heads);
710 write_or_die(1, rpc.result.buf, rpc.result.len);
711 strbuf_release(&rpc.result);
712 strbuf_release(&preamble);
717 static int fetch(int nr_heads, struct ref **to_fetch)
719 struct discovery *d = discover_refs("git-upload-pack", 0);
721 return fetch_git(d, nr_heads, to_fetch);
723 return fetch_dumb(nr_heads, to_fetch);
726 static void parse_fetch(struct strbuf *buf)
728 struct ref **to_fetch = NULL;
729 struct ref *list_head = NULL;
730 struct ref **list = &list_head;
731 int alloc_heads = 0, nr_heads = 0;
734 if (!prefixcmp(buf->buf, "fetch ")) {
735 char *p = buf->buf + strlen("fetch ");
738 unsigned char old_sha1[20];
740 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
741 die("protocol error: expected sha/ref, got %s'", p);
747 die("protocol error: expected sha/ref, got %s'", p);
749 ref = alloc_ref(name);
750 hashcpy(ref->old_sha1, old_sha1);
755 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
756 to_fetch[nr_heads++] = ref;
759 die("http transport does not support %s", buf->buf);
762 if (strbuf_getline(buf, stdin, '\n') == EOF)
768 if (fetch(nr_heads, to_fetch))
769 exit(128); /* error already reported */
770 free_refs(list_head);
778 static int push_dav(int nr_spec, char **specs)
780 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
783 argv[argc++] = "http-push";
784 argv[argc++] = "--helper-status";
786 argv[argc++] = "--dry-run";
787 if (options.verbosity > 1)
788 argv[argc++] = "--verbose";
790 for (i = 0; i < nr_spec; i++)
791 argv[argc++] = specs[i];
794 if (run_command_v_opt(argv, RUN_GIT_CMD))
795 die("git-%s failed", argv[0]);
800 static int push_git(struct discovery *heads, int nr_spec, char **specs)
802 struct rpc_state rpc;
804 struct argv_array args;
806 argv_array_init(&args);
807 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
811 argv_array_push(&args, "--thin");
813 argv_array_push(&args, "--dry-run");
814 if (options.verbosity == 0)
815 argv_array_push(&args, "--quiet");
816 else if (options.verbosity > 1)
817 argv_array_push(&args, "--verbose");
818 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
819 argv_array_push(&args, url);
820 for (i = 0; i < nr_spec; i++)
821 argv_array_push(&args, specs[i]);
823 memset(&rpc, 0, sizeof(rpc));
824 rpc.service_name = "git-receive-pack",
825 rpc.argv = args.argv;
827 err = rpc_service(&rpc, heads);
829 write_or_die(1, rpc.result.buf, rpc.result.len);
830 strbuf_release(&rpc.result);
831 argv_array_clear(&args);
835 static int push(int nr_spec, char **specs)
837 struct discovery *heads = discover_refs("git-receive-pack", 1);
840 if (heads->proto_git)
841 ret = push_git(heads, nr_spec, specs);
843 ret = push_dav(nr_spec, specs);
844 free_discovery(heads);
848 static void parse_push(struct strbuf *buf)
851 int alloc_spec = 0, nr_spec = 0, i, ret;
854 if (!prefixcmp(buf->buf, "push ")) {
855 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
856 specs[nr_spec++] = xstrdup(buf->buf + 5);
859 die("http transport does not support %s", buf->buf);
862 if (strbuf_getline(buf, stdin, '\n') == EOF)
868 ret = push(nr_spec, specs);
873 exit(128); /* error already reported */
876 for (i = 0; i < nr_spec; i++)
881 int main(int argc, const char **argv)
883 struct strbuf buf = STRBUF_INIT;
886 git_extract_argv0_path(argv[0]);
887 setup_git_directory_gently(&nongit);
889 fprintf(stderr, "Remote needed\n");
893 options.verbosity = 1;
894 options.progress = !!isatty(2);
897 remote = remote_get(argv[1]);
900 end_url_with_slash(&buf, argv[2]);
902 end_url_with_slash(&buf, remote->url[0]);
905 url = strbuf_detach(&buf, NULL);
907 http_init(remote, url, 0);
910 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
912 fprintf(stderr, "Error reading command stream\n");
914 fprintf(stderr, "Unexpected end of command stream\n");
919 if (!prefixcmp(buf.buf, "fetch ")) {
921 die("Fetch attempted without a local repo");
924 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
925 int for_push = !!strstr(buf.buf + 4, "for-push");
926 output_refs(get_refs(for_push));
928 } else if (!prefixcmp(buf.buf, "push ")) {
931 } else if (!prefixcmp(buf.buf, "option ")) {
932 char *name = buf.buf + strlen("option ");
933 char *value = strchr(name, ' ');
941 result = set_option(name, value);
945 printf("error invalid value\n");
947 printf("unsupported\n");
950 } else if (!strcmp(buf.buf, "capabilities")) {
954 printf("check-connectivity\n");
958 fprintf(stderr, "Unknown command '%s'\n", buf.buf);