7 #include "run-command.h"
10 #include "argv-array.h"
11 #include "credential.h"
13 static struct remote *remote;
14 static const char *url; /* always ends with a trailing slash */
19 unsigned progress : 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"))
72 return 1 /* unsupported */;
82 unsigned proto_git : 1;
84 static struct discovery *last_discovery;
86 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
88 struct ref *list = NULL;
89 get_remote_heads(-1, heads->buf, heads->len, &list,
90 for_push ? REF_NORMAL : 0, NULL);
94 static struct ref *parse_info_refs(struct discovery *heads)
96 char *data, *start, *mid;
100 struct ref *refs = NULL;
101 struct ref *ref = NULL;
102 struct ref *last_ref = NULL;
107 while (i < heads->len) {
113 if (data[i] == '\n') {
114 if (mid - start != 40)
115 die("%sinfo/refs not valid: is this a git repository?", url);
118 ref = xmalloc(sizeof(struct ref) +
119 strlen(ref_name) + 1);
120 memset(ref, 0, sizeof(struct ref));
121 strcpy(ref->name, ref_name);
122 get_sha1_hex(start, ref->old_sha1);
126 last_ref->next = ref;
133 ref = alloc_ref("HEAD");
134 if (!http_fetch_ref(url, ref) &&
135 !resolve_remote_symref(ref, refs)) {
145 static void free_discovery(struct discovery *d)
148 if (d == last_discovery)
149 last_discovery = NULL;
156 static int show_http_message(struct strbuf *type, struct strbuf *msg)
161 * We only show text/plain parts, as other types are likely
162 * to be ugly to look at on the user's terminal.
164 * TODO should handle "; charset=XXX", and re-encode into
167 if (strcasecmp(type->buf, "text/plain"))
176 eol = strchrnul(p, '\n');
177 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
183 static struct discovery* discover_refs(const char *service, int for_push)
185 struct strbuf exp = STRBUF_INIT;
186 struct strbuf type = STRBUF_INIT;
187 struct strbuf buffer = STRBUF_INIT;
188 struct discovery *last = last_discovery;
190 int http_ret, maybe_smart = 0;
191 struct http_get_options options;
193 if (last && !strcmp(service, last->service))
195 free_discovery(last);
197 strbuf_addf(&buffer, "%sinfo/refs", url);
198 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
199 git_env_bool("GIT_SMART_HTTP", 1)) {
201 if (!strchr(url, '?'))
202 strbuf_addch(&buffer, '?');
204 strbuf_addch(&buffer, '&');
205 strbuf_addf(&buffer, "service=%s", service);
207 refs_url = strbuf_detach(&buffer, NULL);
209 memset(&options, 0, sizeof(options));
210 options.content_type = &type;
211 options.no_cache = 1;
212 options.keep_error = 1;
214 http_ret = http_get_strbuf(refs_url, &buffer, &options);
218 case HTTP_MISSING_TARGET:
219 show_http_message(&type, &buffer);
220 die("repository '%s' not found", url);
222 show_http_message(&type, &buffer);
223 die("Authentication failed for '%s'", url);
225 show_http_message(&type, &buffer);
226 die("unable to access '%s': %s", url, curl_errorstr);
229 last= xcalloc(1, sizeof(*last_discovery));
230 last->service = service;
231 last->buf_alloc = strbuf_detach(&buffer, &last->len);
232 last->buf = last->buf_alloc;
234 strbuf_addf(&exp, "application/x-%s-advertisement", service);
236 (5 <= last->len && last->buf[4] == '#') &&
237 !strbuf_cmp(&exp, &type)) {
241 * smart HTTP response; validate that the service
242 * pkt-line matches our request.
244 line = packet_read_line_buf(&last->buf, &last->len, NULL);
247 strbuf_addf(&exp, "# service=%s", service);
248 if (strcmp(line, exp.buf))
249 die("invalid server response; got '%s'", line);
250 strbuf_release(&exp);
252 /* The header can include additional metadata lines, up
253 * until a packet flush marker. Ignore these now, but
254 * in the future we might start to scan them.
256 while (packet_read_line_buf(&last->buf, &last->len, NULL))
263 last->refs = parse_git_refs(last, for_push);
265 last->refs = parse_info_refs(last);
268 strbuf_release(&exp);
269 strbuf_release(&type);
270 strbuf_release(&buffer);
271 last_discovery = last;
275 static struct ref *get_refs(int for_push)
277 struct discovery *heads;
280 heads = discover_refs("git-receive-pack", for_push);
282 heads = discover_refs("git-upload-pack", for_push);
287 static void output_refs(struct ref *refs)
290 for (posn = refs; posn; posn = posn->next) {
292 printf("@%s %s\n", posn->symref, posn->name);
294 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
301 const char *service_name;
303 struct strbuf *stdin_preamble;
305 char *hdr_content_type;
313 struct strbuf result;
314 unsigned gzip_request : 1;
315 unsigned initial_buffer : 1;
318 static size_t rpc_out(void *ptr, size_t eltsize,
319 size_t nmemb, void *buffer_)
321 size_t max = eltsize * nmemb;
322 struct rpc_state *rpc = buffer_;
323 size_t avail = rpc->len - rpc->pos;
326 rpc->initial_buffer = 0;
327 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
336 memcpy(ptr, rpc->buf + rpc->pos, avail);
341 #ifndef NO_CURL_IOCTL
342 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
344 struct rpc_state *rpc = clientp;
350 case CURLIOCMD_RESTARTREAD:
351 if (rpc->initial_buffer) {
355 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
356 return CURLIOE_FAILRESTART;
359 return CURLIOE_UNKNOWNCMD;
364 static size_t rpc_in(char *ptr, size_t eltsize,
365 size_t nmemb, void *buffer_)
367 size_t size = eltsize * nmemb;
368 struct rpc_state *rpc = buffer_;
369 write_or_die(rpc->in, ptr, size);
373 static int run_slot(struct active_request_slot *slot)
376 struct slot_results results;
378 slot->results = &results;
379 slot->curl_result = curl_easy_perform(slot->curl);
380 finish_active_slot(slot);
382 err = handle_curl_result(&results);
383 if (err != HTTP_OK && err != HTTP_REAUTH) {
384 error("RPC failed; result=%d, HTTP code = %ld",
385 results.curl_result, results.http_code);
391 static int probe_rpc(struct rpc_state *rpc)
393 struct active_request_slot *slot;
394 struct curl_slist *headers = NULL;
395 struct strbuf buf = STRBUF_INIT;
398 slot = get_active_slot();
400 headers = curl_slist_append(headers, rpc->hdr_content_type);
401 headers = curl_slist_append(headers, rpc->hdr_accept);
403 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
404 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
405 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
406 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
407 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
408 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
409 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
410 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
411 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
413 err = run_slot(slot);
415 curl_slist_free_all(headers);
416 strbuf_release(&buf);
420 static int post_rpc(struct rpc_state *rpc)
422 struct active_request_slot *slot;
423 struct curl_slist *headers = NULL;
424 int use_gzip = rpc->gzip_request;
425 char *gzip_body = NULL;
426 size_t gzip_size = 0;
427 int err, large_request = 0;
429 /* Try to load the entire request, if we can fit it into the
430 * allocated buffer space we can use HTTP/1.0 and avoid the
431 * chunked encoding mess.
434 size_t left = rpc->alloc - rpc->len;
435 char *buf = rpc->buf + rpc->len;
438 if (left < LARGE_PACKET_MAX) {
444 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
452 err = probe_rpc(rpc);
453 if (err == HTTP_REAUTH)
454 credential_fill(&http_auth);
455 } while (err == HTTP_REAUTH);
460 headers = curl_slist_append(headers, rpc->hdr_content_type);
461 headers = curl_slist_append(headers, rpc->hdr_accept);
462 headers = curl_slist_append(headers, "Expect:");
465 slot = get_active_slot();
467 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
468 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
469 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
470 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
473 /* The request body is large and the size cannot be predicted.
474 * We must use chunked encoding to send it.
476 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
477 rpc->initial_buffer = 1;
478 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
479 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
480 #ifndef NO_CURL_IOCTL
481 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
482 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
484 if (options.verbosity > 1) {
485 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
489 } else if (gzip_body) {
491 * If we are looping to retry authentication, then the previous
492 * run will have set up the headers and gzip buffer already,
493 * and we just need to send it.
495 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
496 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
498 } else if (use_gzip && 1024 < rpc->len) {
499 /* The client backend isn't giving us compressed data so
500 * we can try to deflate it ourselves, this may save on.
506 memset(&stream, 0, sizeof(stream));
507 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
508 gzip_size = git_deflate_bound(&stream, rpc->len);
509 gzip_body = xmalloc(gzip_size);
511 stream.next_in = (unsigned char *)rpc->buf;
512 stream.avail_in = rpc->len;
513 stream.next_out = (unsigned char *)gzip_body;
514 stream.avail_out = gzip_size;
516 ret = git_deflate(&stream, Z_FINISH);
517 if (ret != Z_STREAM_END)
518 die("cannot deflate request; zlib deflate error %d", ret);
520 ret = git_deflate_end_gently(&stream);
522 die("cannot deflate request; zlib end error %d", ret);
524 gzip_size = stream.total_out;
526 headers = curl_slist_append(headers, "Content-Encoding: gzip");
527 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
528 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
530 if (options.verbosity > 1) {
531 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
533 (unsigned long)rpc->len, (unsigned long)gzip_size);
537 /* We know the complete request size in advance, use the
538 * more normal Content-Length approach.
540 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
541 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
542 if (options.verbosity > 1) {
543 fprintf(stderr, "POST %s (%lu bytes)\n",
544 rpc->service_name, (unsigned long)rpc->len);
549 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
550 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
551 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
553 err = run_slot(slot);
554 if (err == HTTP_REAUTH && !large_request) {
555 credential_fill(&http_auth);
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[15];
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.progress)
682 argv[argc++] = "--no-progress";
684 struct strbuf buf = STRBUF_INIT;
685 strbuf_addf(&buf, "--depth=%lu", options.depth);
686 depth_arg = strbuf_detach(&buf, NULL);
687 argv[argc++] = depth_arg;
692 for (i = 0; i < nr_heads; i++) {
693 struct ref *ref = to_fetch[i];
694 if (!ref->name || !*ref->name)
695 die("cannot fetch by sha1 over smart http");
696 packet_buf_write(&preamble, "%s\n", ref->name);
698 packet_buf_flush(&preamble);
700 memset(&rpc, 0, sizeof(rpc));
701 rpc.service_name = "git-upload-pack",
703 rpc.stdin_preamble = &preamble;
704 rpc.gzip_request = 1;
706 err = rpc_service(&rpc, heads);
708 write_or_die(1, rpc.result.buf, rpc.result.len);
709 strbuf_release(&rpc.result);
710 strbuf_release(&preamble);
715 static int fetch(int nr_heads, struct ref **to_fetch)
717 struct discovery *d = discover_refs("git-upload-pack", 0);
719 return fetch_git(d, nr_heads, to_fetch);
721 return fetch_dumb(nr_heads, to_fetch);
724 static void parse_fetch(struct strbuf *buf)
726 struct ref **to_fetch = NULL;
727 struct ref *list_head = NULL;
728 struct ref **list = &list_head;
729 int alloc_heads = 0, nr_heads = 0;
732 if (!prefixcmp(buf->buf, "fetch ")) {
733 char *p = buf->buf + strlen("fetch ");
736 unsigned char old_sha1[20];
738 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
739 die("protocol error: expected sha/ref, got %s'", p);
745 die("protocol error: expected sha/ref, got %s'", p);
747 ref = alloc_ref(name);
748 hashcpy(ref->old_sha1, old_sha1);
753 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
754 to_fetch[nr_heads++] = ref;
757 die("http transport does not support %s", buf->buf);
760 if (strbuf_getline(buf, stdin, '\n') == EOF)
766 if (fetch(nr_heads, to_fetch))
767 exit(128); /* error already reported */
768 free_refs(list_head);
776 static int push_dav(int nr_spec, char **specs)
778 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
781 argv[argc++] = "http-push";
782 argv[argc++] = "--helper-status";
784 argv[argc++] = "--dry-run";
785 if (options.verbosity > 1)
786 argv[argc++] = "--verbose";
788 for (i = 0; i < nr_spec; i++)
789 argv[argc++] = specs[i];
792 if (run_command_v_opt(argv, RUN_GIT_CMD))
793 die("git-%s failed", argv[0]);
798 static int push_git(struct discovery *heads, int nr_spec, char **specs)
800 struct rpc_state rpc;
802 struct argv_array args;
804 argv_array_init(&args);
805 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
809 argv_array_push(&args, "--thin");
811 argv_array_push(&args, "--dry-run");
812 if (options.verbosity == 0)
813 argv_array_push(&args, "--quiet");
814 else if (options.verbosity > 1)
815 argv_array_push(&args, "--verbose");
816 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
817 argv_array_push(&args, url);
818 for (i = 0; i < nr_spec; i++)
819 argv_array_push(&args, specs[i]);
821 memset(&rpc, 0, sizeof(rpc));
822 rpc.service_name = "git-receive-pack",
823 rpc.argv = args.argv;
825 err = rpc_service(&rpc, heads);
827 write_or_die(1, rpc.result.buf, rpc.result.len);
828 strbuf_release(&rpc.result);
829 argv_array_clear(&args);
833 static int push(int nr_spec, char **specs)
835 struct discovery *heads = discover_refs("git-receive-pack", 1);
838 if (heads->proto_git)
839 ret = push_git(heads, nr_spec, specs);
841 ret = push_dav(nr_spec, specs);
842 free_discovery(heads);
846 static void parse_push(struct strbuf *buf)
849 int alloc_spec = 0, nr_spec = 0, i, ret;
852 if (!prefixcmp(buf->buf, "push ")) {
853 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
854 specs[nr_spec++] = xstrdup(buf->buf + 5);
857 die("http transport does not support %s", buf->buf);
860 if (strbuf_getline(buf, stdin, '\n') == EOF)
866 ret = push(nr_spec, specs);
871 exit(128); /* error already reported */
874 for (i = 0; i < nr_spec; i++)
879 int main(int argc, const char **argv)
881 struct strbuf buf = STRBUF_INIT;
884 git_extract_argv0_path(argv[0]);
885 setup_git_directory_gently(&nongit);
887 fprintf(stderr, "Remote needed\n");
891 options.verbosity = 1;
892 options.progress = !!isatty(2);
895 remote = remote_get(argv[1]);
898 end_url_with_slash(&buf, argv[2]);
900 end_url_with_slash(&buf, remote->url[0]);
903 url = strbuf_detach(&buf, NULL);
905 http_init(remote, url, 0);
908 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
910 fprintf(stderr, "Error reading command stream\n");
912 fprintf(stderr, "Unexpected end of command stream\n");
917 if (!prefixcmp(buf.buf, "fetch ")) {
919 die("Fetch attempted without a local repo");
922 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
923 int for_push = !!strstr(buf.buf + 4, "for-push");
924 output_refs(get_refs(for_push));
926 } else if (!prefixcmp(buf.buf, "push ")) {
929 } else if (!prefixcmp(buf.buf, "option ")) {
930 char *name = buf.buf + strlen("option ");
931 char *value = strchr(name, ' ');
939 result = set_option(name, value);
943 printf("error invalid value\n");
945 printf("unsupported\n");
948 } else if (!strcmp(buf.buf, "capabilities")) {
955 fprintf(stderr, "Unknown command '%s'\n", buf.buf);