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,
23 static struct options options;
25 static int set_option(const char *name, const char *value)
27 if (!strcmp(name, "verbosity")) {
29 int v = strtol(value, &end, 10);
30 if (value == end || *end)
32 options.verbosity = v;
35 else if (!strcmp(name, "progress")) {
36 if (!strcmp(value, "true"))
38 else if (!strcmp(value, "false"))
44 else if (!strcmp(name, "depth")) {
46 unsigned long v = strtoul(value, &end, 10);
47 if (value == end || *end)
52 else if (!strcmp(name, "followtags")) {
53 if (!strcmp(value, "true"))
54 options.followtags = 1;
55 else if (!strcmp(value, "false"))
56 options.followtags = 0;
61 else if (!strcmp(name, "dry-run")) {
62 if (!strcmp(value, "true"))
64 else if (!strcmp(value, "false"))
71 return 1 /* unsupported */;
81 unsigned proto_git : 1;
83 static struct discovery *last_discovery;
85 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
87 struct ref *list = NULL;
88 get_remote_heads(-1, heads->buf, heads->len, &list,
89 for_push ? REF_NORMAL : 0, NULL);
93 static struct ref *parse_info_refs(struct discovery *heads)
95 char *data, *start, *mid;
99 struct ref *refs = NULL;
100 struct ref *ref = NULL;
101 struct ref *last_ref = NULL;
106 while (i < heads->len) {
112 if (data[i] == '\n') {
113 if (mid - start != 40)
114 die("%sinfo/refs not valid: is this a git repository?", url);
117 ref = xmalloc(sizeof(struct ref) +
118 strlen(ref_name) + 1);
119 memset(ref, 0, sizeof(struct ref));
120 strcpy(ref->name, ref_name);
121 get_sha1_hex(start, ref->old_sha1);
125 last_ref->next = ref;
132 ref = alloc_ref("HEAD");
133 if (!http_fetch_ref(url, ref) &&
134 !resolve_remote_symref(ref, refs)) {
144 static void free_discovery(struct discovery *d)
147 if (d == last_discovery)
148 last_discovery = NULL;
155 static int show_http_message(struct strbuf *type, struct strbuf *msg)
160 * We only show text/plain parts, as other types are likely
161 * to be ugly to look at on the user's terminal.
163 * TODO should handle "; charset=XXX", and re-encode into
166 if (strcasecmp(type->buf, "text/plain"))
175 eol = strchrnul(p, '\n');
176 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
182 static struct discovery* discover_refs(const char *service, int for_push)
184 struct strbuf exp = STRBUF_INIT;
185 struct strbuf type = STRBUF_INIT;
186 struct strbuf buffer = STRBUF_INIT;
187 struct discovery *last = last_discovery;
189 int http_ret, maybe_smart = 0;
190 struct http_get_options options;
192 if (last && !strcmp(service, last->service))
194 free_discovery(last);
196 strbuf_addf(&buffer, "%sinfo/refs", url);
197 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
198 git_env_bool("GIT_SMART_HTTP", 1)) {
200 if (!strchr(url, '?'))
201 strbuf_addch(&buffer, '?');
203 strbuf_addch(&buffer, '&');
204 strbuf_addf(&buffer, "service=%s", service);
206 refs_url = strbuf_detach(&buffer, NULL);
208 memset(&options, 0, sizeof(options));
209 options.content_type = &type;
210 options.no_cache = 1;
211 options.keep_error = 1;
213 http_ret = http_get_strbuf(refs_url, &buffer, &options);
217 case HTTP_MISSING_TARGET:
218 show_http_message(&type, &buffer);
219 die("repository '%s' not found", url);
221 show_http_message(&type, &buffer);
222 die("Authentication failed for '%s'", url);
224 show_http_message(&type, &buffer);
225 die("unable to access '%s': %s", url, curl_errorstr);
228 last= xcalloc(1, sizeof(*last_discovery));
229 last->service = service;
230 last->buf_alloc = strbuf_detach(&buffer, &last->len);
231 last->buf = last->buf_alloc;
233 strbuf_addf(&exp, "application/x-%s-advertisement", service);
235 (5 <= last->len && last->buf[4] == '#') &&
236 !strbuf_cmp(&exp, &type)) {
240 * smart HTTP response; validate that the service
241 * pkt-line matches our request.
243 line = packet_read_line_buf(&last->buf, &last->len, NULL);
246 strbuf_addf(&exp, "# service=%s", service);
247 if (strcmp(line, exp.buf))
248 die("invalid server response; got '%s'", line);
249 strbuf_release(&exp);
251 /* The header can include additional metadata lines, up
252 * until a packet flush marker. Ignore these now, but
253 * in the future we might start to scan them.
255 while (packet_read_line_buf(&last->buf, &last->len, NULL))
262 last->refs = parse_git_refs(last, for_push);
264 last->refs = parse_info_refs(last);
267 strbuf_release(&exp);
268 strbuf_release(&type);
269 strbuf_release(&buffer);
270 last_discovery = last;
274 static struct ref *get_refs(int for_push)
276 struct discovery *heads;
279 heads = discover_refs("git-receive-pack", for_push);
281 heads = discover_refs("git-upload-pack", for_push);
286 static void output_refs(struct ref *refs)
289 for (posn = refs; posn; posn = posn->next) {
291 printf("@%s %s\n", posn->symref, posn->name);
293 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
300 const char *service_name;
302 struct strbuf *stdin_preamble;
304 char *hdr_content_type;
312 struct strbuf result;
313 unsigned gzip_request : 1;
314 unsigned initial_buffer : 1;
317 static size_t rpc_out(void *ptr, size_t eltsize,
318 size_t nmemb, void *buffer_)
320 size_t max = eltsize * nmemb;
321 struct rpc_state *rpc = buffer_;
322 size_t avail = rpc->len - rpc->pos;
325 rpc->initial_buffer = 0;
326 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
335 memcpy(ptr, rpc->buf + rpc->pos, avail);
340 #ifndef NO_CURL_IOCTL
341 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
343 struct rpc_state *rpc = clientp;
349 case CURLIOCMD_RESTARTREAD:
350 if (rpc->initial_buffer) {
354 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
355 return CURLIOE_FAILRESTART;
358 return CURLIOE_UNKNOWNCMD;
363 static size_t rpc_in(char *ptr, size_t eltsize,
364 size_t nmemb, void *buffer_)
366 size_t size = eltsize * nmemb;
367 struct rpc_state *rpc = buffer_;
368 write_or_die(rpc->in, ptr, size);
372 static int run_slot(struct active_request_slot *slot)
375 struct slot_results results;
377 slot->results = &results;
378 slot->curl_result = curl_easy_perform(slot->curl);
379 finish_active_slot(slot);
381 err = handle_curl_result(&results);
382 if (err != HTTP_OK && err != HTTP_REAUTH) {
383 error("RPC failed; result=%d, HTTP code = %ld",
384 results.curl_result, results.http_code);
390 static int probe_rpc(struct rpc_state *rpc)
392 struct active_request_slot *slot;
393 struct curl_slist *headers = NULL;
394 struct strbuf buf = STRBUF_INIT;
397 slot = get_active_slot();
399 headers = curl_slist_append(headers, rpc->hdr_content_type);
400 headers = curl_slist_append(headers, rpc->hdr_accept);
402 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
403 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
404 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
405 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
406 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
407 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
408 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
409 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
410 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
412 err = run_slot(slot);
414 curl_slist_free_all(headers);
415 strbuf_release(&buf);
419 static int post_rpc(struct rpc_state *rpc)
421 struct active_request_slot *slot;
422 struct curl_slist *headers = NULL;
423 int use_gzip = rpc->gzip_request;
424 char *gzip_body = NULL;
425 size_t gzip_size = 0;
426 int err, large_request = 0;
428 /* Try to load the entire request, if we can fit it into the
429 * allocated buffer space we can use HTTP/1.0 and avoid the
430 * chunked encoding mess.
433 size_t left = rpc->alloc - rpc->len;
434 char *buf = rpc->buf + rpc->len;
437 if (left < LARGE_PACKET_MAX) {
443 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
451 err = probe_rpc(rpc);
452 } while (err == HTTP_REAUTH);
457 headers = curl_slist_append(headers, rpc->hdr_content_type);
458 headers = curl_slist_append(headers, rpc->hdr_accept);
459 headers = curl_slist_append(headers, "Expect:");
462 slot = get_active_slot();
464 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
465 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
466 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
467 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
470 /* The request body is large and the size cannot be predicted.
471 * We must use chunked encoding to send it.
473 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
474 rpc->initial_buffer = 1;
475 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
476 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
477 #ifndef NO_CURL_IOCTL
478 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
479 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
481 if (options.verbosity > 1) {
482 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
486 } else if (gzip_body) {
488 * If we are looping to retry authentication, then the previous
489 * run will have set up the headers and gzip buffer already,
490 * and we just need to send it.
492 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
493 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
495 } else if (use_gzip && 1024 < rpc->len) {
496 /* The client backend isn't giving us compressed data so
497 * we can try to deflate it ourselves, this may save on.
503 memset(&stream, 0, sizeof(stream));
504 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
505 gzip_size = git_deflate_bound(&stream, rpc->len);
506 gzip_body = xmalloc(gzip_size);
508 stream.next_in = (unsigned char *)rpc->buf;
509 stream.avail_in = rpc->len;
510 stream.next_out = (unsigned char *)gzip_body;
511 stream.avail_out = gzip_size;
513 ret = git_deflate(&stream, Z_FINISH);
514 if (ret != Z_STREAM_END)
515 die("cannot deflate request; zlib deflate error %d", ret);
517 ret = git_deflate_end_gently(&stream);
519 die("cannot deflate request; zlib end error %d", ret);
521 gzip_size = stream.total_out;
523 headers = curl_slist_append(headers, "Content-Encoding: gzip");
524 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
525 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
527 if (options.verbosity > 1) {
528 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
530 (unsigned long)rpc->len, (unsigned long)gzip_size);
534 /* We know the complete request size in advance, use the
535 * more normal Content-Length approach.
537 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
538 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
539 if (options.verbosity > 1) {
540 fprintf(stderr, "POST %s (%lu bytes)\n",
541 rpc->service_name, (unsigned long)rpc->len);
546 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
547 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
548 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
550 err = run_slot(slot);
551 if (err == HTTP_REAUTH && !large_request)
556 curl_slist_free_all(headers);
561 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
563 const char *svc = rpc->service_name;
564 struct strbuf buf = STRBUF_INIT;
565 struct strbuf *preamble = rpc->stdin_preamble;
566 struct child_process client;
569 memset(&client, 0, sizeof(client));
573 client.argv = rpc->argv;
574 if (start_command(&client))
577 write_or_die(client.in, preamble->buf, preamble->len);
579 write_or_die(client.in, heads->buf, heads->len);
581 rpc->alloc = http_post_buffer;
582 rpc->buf = xmalloc(rpc->alloc);
584 rpc->out = client.out;
585 strbuf_init(&rpc->result, 0);
587 strbuf_addf(&buf, "%s%s", url, svc);
588 rpc->service_url = strbuf_detach(&buf, NULL);
590 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
591 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
593 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
594 rpc->hdr_accept = strbuf_detach(&buf, NULL);
597 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
602 err |= post_rpc(rpc);
608 strbuf_read(&rpc->result, client.out, 0);
612 if (xread(client.out, buf, sizeof(buf)) <= 0)
619 err |= finish_command(&client);
620 free(rpc->service_url);
621 free(rpc->hdr_content_type);
622 free(rpc->hdr_accept);
624 strbuf_release(&buf);
628 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
630 struct walker *walker;
631 char **targets = xmalloc(nr_heads * sizeof(char*));
635 die("dumb http transport does not support --depth");
636 for (i = 0; i < nr_heads; i++)
637 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
639 walker = get_http_walker(url);
641 walker->get_tree = 1;
642 walker->get_history = 1;
643 walker->get_verbosely = options.verbosity >= 3;
644 walker->get_recover = 0;
645 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
648 for (i = 0; i < nr_heads; i++)
652 return ret ? error("Fetch failed.") : 0;
655 static int fetch_git(struct discovery *heads,
656 int nr_heads, struct ref **to_fetch)
658 struct rpc_state rpc;
659 struct strbuf preamble = STRBUF_INIT;
660 char *depth_arg = NULL;
661 int argc = 0, i, err;
662 const char *argv[15];
664 argv[argc++] = "fetch-pack";
665 argv[argc++] = "--stateless-rpc";
666 argv[argc++] = "--stdin";
667 argv[argc++] = "--lock-pack";
668 if (options.followtags)
669 argv[argc++] = "--include-tag";
671 argv[argc++] = "--thin";
672 if (options.verbosity >= 3) {
676 if (!options.progress)
677 argv[argc++] = "--no-progress";
679 struct strbuf buf = STRBUF_INIT;
680 strbuf_addf(&buf, "--depth=%lu", options.depth);
681 depth_arg = strbuf_detach(&buf, NULL);
682 argv[argc++] = depth_arg;
687 for (i = 0; i < nr_heads; i++) {
688 struct ref *ref = to_fetch[i];
689 if (!ref->name || !*ref->name)
690 die("cannot fetch by sha1 over smart http");
691 packet_buf_write(&preamble, "%s\n", ref->name);
693 packet_buf_flush(&preamble);
695 memset(&rpc, 0, sizeof(rpc));
696 rpc.service_name = "git-upload-pack",
698 rpc.stdin_preamble = &preamble;
699 rpc.gzip_request = 1;
701 err = rpc_service(&rpc, heads);
703 write_or_die(1, rpc.result.buf, rpc.result.len);
704 strbuf_release(&rpc.result);
705 strbuf_release(&preamble);
710 static int fetch(int nr_heads, struct ref **to_fetch)
712 struct discovery *d = discover_refs("git-upload-pack", 0);
714 return fetch_git(d, nr_heads, to_fetch);
716 return fetch_dumb(nr_heads, to_fetch);
719 static void parse_fetch(struct strbuf *buf)
721 struct ref **to_fetch = NULL;
722 struct ref *list_head = NULL;
723 struct ref **list = &list_head;
724 int alloc_heads = 0, nr_heads = 0;
727 if (!prefixcmp(buf->buf, "fetch ")) {
728 char *p = buf->buf + strlen("fetch ");
731 unsigned char old_sha1[20];
733 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
734 die("protocol error: expected sha/ref, got %s'", p);
740 die("protocol error: expected sha/ref, got %s'", p);
742 ref = alloc_ref(name);
743 hashcpy(ref->old_sha1, old_sha1);
748 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
749 to_fetch[nr_heads++] = ref;
752 die("http transport does not support %s", buf->buf);
755 if (strbuf_getline(buf, stdin, '\n') == EOF)
761 if (fetch(nr_heads, to_fetch))
762 exit(128); /* error already reported */
763 free_refs(list_head);
771 static int push_dav(int nr_spec, char **specs)
773 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
776 argv[argc++] = "http-push";
777 argv[argc++] = "--helper-status";
779 argv[argc++] = "--dry-run";
780 if (options.verbosity > 1)
781 argv[argc++] = "--verbose";
783 for (i = 0; i < nr_spec; i++)
784 argv[argc++] = specs[i];
787 if (run_command_v_opt(argv, RUN_GIT_CMD))
788 die("git-%s failed", argv[0]);
793 static int push_git(struct discovery *heads, int nr_spec, char **specs)
795 struct rpc_state rpc;
797 struct argv_array args;
799 argv_array_init(&args);
800 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
804 argv_array_push(&args, "--thin");
806 argv_array_push(&args, "--dry-run");
807 if (options.verbosity == 0)
808 argv_array_push(&args, "--quiet");
809 else if (options.verbosity > 1)
810 argv_array_push(&args, "--verbose");
811 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
812 argv_array_push(&args, url);
813 for (i = 0; i < nr_spec; i++)
814 argv_array_push(&args, specs[i]);
816 memset(&rpc, 0, sizeof(rpc));
817 rpc.service_name = "git-receive-pack",
818 rpc.argv = args.argv;
820 err = rpc_service(&rpc, heads);
822 write_or_die(1, rpc.result.buf, rpc.result.len);
823 strbuf_release(&rpc.result);
824 argv_array_clear(&args);
828 static int push(int nr_spec, char **specs)
830 struct discovery *heads = discover_refs("git-receive-pack", 1);
833 if (heads->proto_git)
834 ret = push_git(heads, nr_spec, specs);
836 ret = push_dav(nr_spec, specs);
837 free_discovery(heads);
841 static void parse_push(struct strbuf *buf)
844 int alloc_spec = 0, nr_spec = 0, i, ret;
847 if (!prefixcmp(buf->buf, "push ")) {
848 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
849 specs[nr_spec++] = xstrdup(buf->buf + 5);
852 die("http transport does not support %s", buf->buf);
855 if (strbuf_getline(buf, stdin, '\n') == EOF)
861 ret = push(nr_spec, specs);
866 exit(128); /* error already reported */
869 for (i = 0; i < nr_spec; i++)
874 int main(int argc, const char **argv)
876 struct strbuf buf = STRBUF_INIT;
879 git_extract_argv0_path(argv[0]);
880 setup_git_directory_gently(&nongit);
882 fprintf(stderr, "Remote needed\n");
886 options.verbosity = 1;
887 options.progress = !!isatty(2);
890 remote = remote_get(argv[1]);
893 end_url_with_slash(&buf, argv[2]);
895 end_url_with_slash(&buf, remote->url[0]);
898 url = strbuf_detach(&buf, NULL);
900 http_init(remote, url, 0);
903 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
905 fprintf(stderr, "Error reading command stream\n");
907 fprintf(stderr, "Unexpected end of command stream\n");
912 if (!prefixcmp(buf.buf, "fetch ")) {
914 die("Fetch attempted without a local repo");
917 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
918 int for_push = !!strstr(buf.buf + 4, "for-push");
919 output_refs(get_refs(for_push));
921 } else if (!prefixcmp(buf.buf, "push ")) {
924 } else if (!prefixcmp(buf.buf, "option ")) {
925 char *name = buf.buf + strlen("option ");
926 char *value = strchr(name, ' ');
934 result = set_option(name, value);
938 printf("error invalid value\n");
940 printf("unsupported\n");
943 } else if (!strcmp(buf.buf, "capabilities")) {
950 fprintf(stderr, "Unknown command '%s'\n", buf.buf);