7 #include "run-command.h"
9 #include "string-list.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;
24 static struct string_list cas_options = STRING_LIST_INIT_DUP;
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, "cas")) {
72 struct strbuf val = STRBUF_INIT;
73 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
74 string_list_append(&cas_options, val.buf);
79 return 1 /* unsupported */;
89 unsigned proto_git : 1;
91 static struct discovery *last_discovery;
93 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
95 struct ref *list = NULL;
96 get_remote_heads(-1, heads->buf, heads->len, &list,
97 for_push ? REF_NORMAL : 0, NULL);
101 static struct ref *parse_info_refs(struct discovery *heads)
103 char *data, *start, *mid;
107 struct ref *refs = NULL;
108 struct ref *ref = NULL;
109 struct ref *last_ref = NULL;
114 while (i < heads->len) {
120 if (data[i] == '\n') {
121 if (mid - start != 40)
122 die("%sinfo/refs not valid: is this a git repository?", url);
125 ref = xmalloc(sizeof(struct ref) +
126 strlen(ref_name) + 1);
127 memset(ref, 0, sizeof(struct ref));
128 strcpy(ref->name, ref_name);
129 get_sha1_hex(start, ref->old_sha1);
133 last_ref->next = ref;
140 ref = alloc_ref("HEAD");
141 if (!http_fetch_ref(url, ref) &&
142 !resolve_remote_symref(ref, refs)) {
152 static void free_discovery(struct discovery *d)
155 if (d == last_discovery)
156 last_discovery = NULL;
163 static int show_http_message(struct strbuf *type, struct strbuf *msg)
168 * We only show text/plain parts, as other types are likely
169 * to be ugly to look at on the user's terminal.
171 * TODO should handle "; charset=XXX", and re-encode into
174 if (strcasecmp(type->buf, "text/plain"))
183 eol = strchrnul(p, '\n');
184 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
190 static struct discovery* discover_refs(const char *service, int for_push)
192 struct strbuf exp = STRBUF_INIT;
193 struct strbuf type = STRBUF_INIT;
194 struct strbuf buffer = STRBUF_INIT;
195 struct discovery *last = last_discovery;
197 int http_ret, maybe_smart = 0;
199 if (last && !strcmp(service, last->service))
201 free_discovery(last);
203 strbuf_addf(&buffer, "%sinfo/refs", url);
204 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
205 git_env_bool("GIT_SMART_HTTP", 1)) {
207 if (!strchr(url, '?'))
208 strbuf_addch(&buffer, '?');
210 strbuf_addch(&buffer, '&');
211 strbuf_addf(&buffer, "service=%s", service);
213 refs_url = strbuf_detach(&buffer, NULL);
215 http_ret = http_get_strbuf(refs_url, &type, &buffer,
216 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
220 case HTTP_MISSING_TARGET:
221 show_http_message(&type, &buffer);
222 die("repository '%s' not found", url);
224 show_http_message(&type, &buffer);
225 die("Authentication failed for '%s'", url);
227 show_http_message(&type, &buffer);
228 die("unable to access '%s': %s", url, curl_errorstr);
231 last= xcalloc(1, sizeof(*last_discovery));
232 last->service = service;
233 last->buf_alloc = strbuf_detach(&buffer, &last->len);
234 last->buf = last->buf_alloc;
236 strbuf_addf(&exp, "application/x-%s-advertisement", service);
238 (5 <= last->len && last->buf[4] == '#') &&
239 !strbuf_cmp(&exp, &type)) {
243 * smart HTTP response; validate that the service
244 * pkt-line matches our request.
246 line = packet_read_line_buf(&last->buf, &last->len, NULL);
249 strbuf_addf(&exp, "# service=%s", service);
250 if (strcmp(line, exp.buf))
251 die("invalid server response; got '%s'", line);
252 strbuf_release(&exp);
254 /* The header can include additional metadata lines, up
255 * until a packet flush marker. Ignore these now, but
256 * in the future we might start to scan them.
258 while (packet_read_line_buf(&last->buf, &last->len, NULL))
265 last->refs = parse_git_refs(last, for_push);
267 last->refs = parse_info_refs(last);
270 strbuf_release(&exp);
271 strbuf_release(&type);
272 strbuf_release(&buffer);
273 last_discovery = last;
277 static struct ref *get_refs(int for_push)
279 struct discovery *heads;
282 heads = discover_refs("git-receive-pack", for_push);
284 heads = discover_refs("git-upload-pack", for_push);
289 static void output_refs(struct ref *refs)
292 for (posn = refs; posn; posn = posn->next) {
294 printf("@%s %s\n", posn->symref, posn->name);
296 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
303 const char *service_name;
305 struct strbuf *stdin_preamble;
307 char *hdr_content_type;
315 struct strbuf result;
316 unsigned gzip_request : 1;
317 unsigned initial_buffer : 1;
320 static size_t rpc_out(void *ptr, size_t eltsize,
321 size_t nmemb, void *buffer_)
323 size_t max = eltsize * nmemb;
324 struct rpc_state *rpc = buffer_;
325 size_t avail = rpc->len - rpc->pos;
328 rpc->initial_buffer = 0;
329 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
338 memcpy(ptr, rpc->buf + rpc->pos, avail);
343 #ifndef NO_CURL_IOCTL
344 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
346 struct rpc_state *rpc = clientp;
352 case CURLIOCMD_RESTARTREAD:
353 if (rpc->initial_buffer) {
357 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
358 return CURLIOE_FAILRESTART;
361 return CURLIOE_UNKNOWNCMD;
366 static size_t rpc_in(char *ptr, size_t eltsize,
367 size_t nmemb, void *buffer_)
369 size_t size = eltsize * nmemb;
370 struct rpc_state *rpc = buffer_;
371 write_or_die(rpc->in, ptr, size);
375 static int run_slot(struct active_request_slot *slot)
378 struct slot_results results;
380 slot->results = &results;
381 slot->curl_result = curl_easy_perform(slot->curl);
382 finish_active_slot(slot);
384 err = handle_curl_result(&results);
385 if (err != HTTP_OK && err != HTTP_REAUTH) {
386 error("RPC failed; result=%d, HTTP code = %ld",
387 results.curl_result, results.http_code);
393 static int probe_rpc(struct rpc_state *rpc)
395 struct active_request_slot *slot;
396 struct curl_slist *headers = NULL;
397 struct strbuf buf = STRBUF_INIT;
400 slot = get_active_slot();
402 headers = curl_slist_append(headers, rpc->hdr_content_type);
403 headers = curl_slist_append(headers, rpc->hdr_accept);
405 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
406 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
407 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
408 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
409 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
410 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
411 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
412 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
413 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
415 err = run_slot(slot);
417 curl_slist_free_all(headers);
418 strbuf_release(&buf);
422 static int post_rpc(struct rpc_state *rpc)
424 struct active_request_slot *slot;
425 struct curl_slist *headers = NULL;
426 int use_gzip = rpc->gzip_request;
427 char *gzip_body = NULL;
428 size_t gzip_size = 0;
429 int err, large_request = 0;
431 /* Try to load the entire request, if we can fit it into the
432 * allocated buffer space we can use HTTP/1.0 and avoid the
433 * chunked encoding mess.
436 size_t left = rpc->alloc - rpc->len;
437 char *buf = rpc->buf + rpc->len;
440 if (left < LARGE_PACKET_MAX) {
446 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
454 err = probe_rpc(rpc);
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)
559 curl_slist_free_all(headers);
564 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
566 const char *svc = rpc->service_name;
567 struct strbuf buf = STRBUF_INIT;
568 struct strbuf *preamble = rpc->stdin_preamble;
569 struct child_process client;
572 memset(&client, 0, sizeof(client));
576 client.argv = rpc->argv;
577 if (start_command(&client))
580 write_or_die(client.in, preamble->buf, preamble->len);
582 write_or_die(client.in, heads->buf, heads->len);
584 rpc->alloc = http_post_buffer;
585 rpc->buf = xmalloc(rpc->alloc);
587 rpc->out = client.out;
588 strbuf_init(&rpc->result, 0);
590 strbuf_addf(&buf, "%s%s", url, svc);
591 rpc->service_url = strbuf_detach(&buf, NULL);
593 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
594 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
596 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
597 rpc->hdr_accept = strbuf_detach(&buf, NULL);
600 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
605 err |= post_rpc(rpc);
611 strbuf_read(&rpc->result, client.out, 0);
615 if (xread(client.out, buf, sizeof(buf)) <= 0)
622 err |= finish_command(&client);
623 free(rpc->service_url);
624 free(rpc->hdr_content_type);
625 free(rpc->hdr_accept);
627 strbuf_release(&buf);
631 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
633 struct walker *walker;
634 char **targets = xmalloc(nr_heads * sizeof(char*));
638 die("dumb http transport does not support --depth");
639 for (i = 0; i < nr_heads; i++)
640 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
642 walker = get_http_walker(url);
644 walker->get_tree = 1;
645 walker->get_history = 1;
646 walker->get_verbosely = options.verbosity >= 3;
647 walker->get_recover = 0;
648 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
651 for (i = 0; i < nr_heads; i++)
655 return ret ? error("Fetch failed.") : 0;
658 static int fetch_git(struct discovery *heads,
659 int nr_heads, struct ref **to_fetch)
661 struct rpc_state rpc;
662 struct strbuf preamble = STRBUF_INIT;
663 char *depth_arg = NULL;
664 int argc = 0, i, err;
665 const char *argv[15];
667 argv[argc++] = "fetch-pack";
668 argv[argc++] = "--stateless-rpc";
669 argv[argc++] = "--stdin";
670 argv[argc++] = "--lock-pack";
671 if (options.followtags)
672 argv[argc++] = "--include-tag";
674 argv[argc++] = "--thin";
675 if (options.verbosity >= 3) {
679 if (!options.progress)
680 argv[argc++] = "--no-progress";
682 struct strbuf buf = STRBUF_INIT;
683 strbuf_addf(&buf, "--depth=%lu", options.depth);
684 depth_arg = strbuf_detach(&buf, NULL);
685 argv[argc++] = depth_arg;
690 for (i = 0; i < nr_heads; i++) {
691 struct ref *ref = to_fetch[i];
692 if (!ref->name || !*ref->name)
693 die("cannot fetch by sha1 over smart http");
694 packet_buf_write(&preamble, "%s\n", ref->name);
696 packet_buf_flush(&preamble);
698 memset(&rpc, 0, sizeof(rpc));
699 rpc.service_name = "git-upload-pack",
701 rpc.stdin_preamble = &preamble;
702 rpc.gzip_request = 1;
704 err = rpc_service(&rpc, heads);
706 write_or_die(1, rpc.result.buf, rpc.result.len);
707 strbuf_release(&rpc.result);
708 strbuf_release(&preamble);
713 static int fetch(int nr_heads, struct ref **to_fetch)
715 struct discovery *d = discover_refs("git-upload-pack", 0);
717 return fetch_git(d, nr_heads, to_fetch);
719 return fetch_dumb(nr_heads, to_fetch);
722 static void parse_fetch(struct strbuf *buf)
724 struct ref **to_fetch = NULL;
725 struct ref *list_head = NULL;
726 struct ref **list = &list_head;
727 int alloc_heads = 0, nr_heads = 0;
730 if (!prefixcmp(buf->buf, "fetch ")) {
731 char *p = buf->buf + strlen("fetch ");
734 unsigned char old_sha1[20];
736 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
737 die("protocol error: expected sha/ref, got %s'", p);
743 die("protocol error: expected sha/ref, got %s'", p);
745 ref = alloc_ref(name);
746 hashcpy(ref->old_sha1, old_sha1);
751 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
752 to_fetch[nr_heads++] = ref;
755 die("http transport does not support %s", buf->buf);
758 if (strbuf_getline(buf, stdin, '\n') == EOF)
764 if (fetch(nr_heads, to_fetch))
765 exit(128); /* error already reported */
766 free_refs(list_head);
774 static int push_dav(int nr_spec, char **specs)
776 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
779 argv[argc++] = "http-push";
780 argv[argc++] = "--helper-status";
782 argv[argc++] = "--dry-run";
783 if (options.verbosity > 1)
784 argv[argc++] = "--verbose";
786 for (i = 0; i < nr_spec; i++)
787 argv[argc++] = specs[i];
790 if (run_command_v_opt(argv, RUN_GIT_CMD))
791 die("git-%s failed", argv[0]);
796 static int push_git(struct discovery *heads, int nr_spec, char **specs)
798 struct rpc_state rpc;
800 int argc = 0, i, err;
801 struct string_list_item *cas_option;
803 argv = xmalloc((10 + nr_spec + cas_options.nr) * sizeof(char *));
804 argv[argc++] = "send-pack";
805 argv[argc++] = "--stateless-rpc";
806 argv[argc++] = "--helper-status";
808 argv[argc++] = "--thin";
810 argv[argc++] = "--dry-run";
811 if (options.verbosity == 0)
812 argv[argc++] = "--quiet";
813 else if (options.verbosity > 1)
814 argv[argc++] = "--verbose";
815 argv[argc++] = options.progress ? "--progress" : "--no-progress";
817 for_each_string_list_item(cas_option, &cas_options)
818 argv[argc++] = cas_option->string;
821 for (i = 0; i < nr_spec; i++)
822 argv[argc++] = specs[i];
825 memset(&rpc, 0, sizeof(rpc));
826 rpc.service_name = "git-receive-pack",
829 err = rpc_service(&rpc, heads);
831 write_or_die(1, rpc.result.buf, rpc.result.len);
832 strbuf_release(&rpc.result);
837 static int push(int nr_spec, char **specs)
839 struct discovery *heads = discover_refs("git-receive-pack", 1);
842 if (heads->proto_git)
843 ret = push_git(heads, nr_spec, specs);
845 ret = push_dav(nr_spec, specs);
846 free_discovery(heads);
850 static void parse_push(struct strbuf *buf)
853 int alloc_spec = 0, nr_spec = 0, i, ret;
856 if (!prefixcmp(buf->buf, "push ")) {
857 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
858 specs[nr_spec++] = xstrdup(buf->buf + 5);
861 die("http transport does not support %s", buf->buf);
864 if (strbuf_getline(buf, stdin, '\n') == EOF)
870 ret = push(nr_spec, specs);
875 exit(128); /* error already reported */
878 for (i = 0; i < nr_spec; i++)
883 int main(int argc, const char **argv)
885 struct strbuf buf = STRBUF_INIT;
888 git_extract_argv0_path(argv[0]);
889 setup_git_directory_gently(&nongit);
891 fprintf(stderr, "Remote needed\n");
895 options.verbosity = 1;
896 options.progress = !!isatty(2);
899 remote = remote_get(argv[1]);
902 end_url_with_slash(&buf, argv[2]);
904 end_url_with_slash(&buf, remote->url[0]);
907 url = strbuf_detach(&buf, NULL);
909 http_init(remote, url, 0);
912 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
914 fprintf(stderr, "Error reading command stream\n");
916 fprintf(stderr, "Unexpected end of command stream\n");
921 if (!prefixcmp(buf.buf, "fetch ")) {
923 die("Fetch attempted without a local repo");
926 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
927 int for_push = !!strstr(buf.buf + 4, "for-push");
928 output_refs(get_refs(for_push));
930 } else if (!prefixcmp(buf.buf, "push ")) {
933 } else if (!prefixcmp(buf.buf, "option ")) {
934 char *name = buf.buf + strlen("option ");
935 char *value = strchr(name, ' ');
943 result = set_option(name, value);
947 printf("error invalid value\n");
949 printf("unsupported\n");
952 } else if (!strcmp(buf.buf, "capabilities")) {
959 fprintf(stderr, "Unknown command '%s'\n", buf.buf);