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 ((starts_with(url.buf, "http://") || starts_with(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,
398 struct slot_results *results)
401 struct slot_results results_buf;
404 results = &results_buf;
406 slot->results = results;
407 slot->curl_result = curl_easy_perform(slot->curl);
408 finish_active_slot(slot);
410 err = handle_curl_result(results);
411 if (err != HTTP_OK && err != HTTP_REAUTH) {
412 error("RPC failed; result=%d, HTTP code = %ld",
413 results->curl_result, results->http_code);
419 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
421 struct active_request_slot *slot;
422 struct curl_slist *headers = NULL;
423 struct strbuf buf = STRBUF_INIT;
426 slot = get_active_slot();
428 headers = curl_slist_append(headers, rpc->hdr_content_type);
429 headers = curl_slist_append(headers, rpc->hdr_accept);
431 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
432 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
433 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
434 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
435 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
436 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
437 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
438 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
439 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
441 err = run_slot(slot, results);
443 curl_slist_free_all(headers);
444 strbuf_release(&buf);
448 static int post_rpc(struct rpc_state *rpc)
450 struct active_request_slot *slot;
451 struct curl_slist *headers = NULL;
452 int use_gzip = rpc->gzip_request;
453 char *gzip_body = NULL;
454 size_t gzip_size = 0;
455 int err, large_request = 0;
456 int needs_100_continue = 0;
458 /* Try to load the entire request, if we can fit it into the
459 * allocated buffer space we can use HTTP/1.0 and avoid the
460 * chunked encoding mess.
463 size_t left = rpc->alloc - rpc->len;
464 char *buf = rpc->buf + rpc->len;
467 if (left < LARGE_PACKET_MAX) {
473 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
480 struct slot_results results;
483 err = probe_rpc(rpc, &results);
484 if (err == HTTP_REAUTH)
485 credential_fill(&http_auth);
486 } while (err == HTTP_REAUTH);
490 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
491 needs_100_continue = 1;
494 headers = curl_slist_append(headers, rpc->hdr_content_type);
495 headers = curl_slist_append(headers, rpc->hdr_accept);
496 headers = curl_slist_append(headers, needs_100_continue ?
497 "Expect: 100-continue" : "Expect:");
500 slot = get_active_slot();
502 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
503 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
504 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
505 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
508 /* The request body is large and the size cannot be predicted.
509 * We must use chunked encoding to send it.
511 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
512 rpc->initial_buffer = 1;
513 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
514 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
515 #ifndef NO_CURL_IOCTL
516 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
517 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
519 if (options.verbosity > 1) {
520 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
524 } else if (gzip_body) {
526 * If we are looping to retry authentication, then the previous
527 * run will have set up the headers and gzip buffer already,
528 * and we just need to send it.
530 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
531 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
533 } else if (use_gzip && 1024 < rpc->len) {
534 /* The client backend isn't giving us compressed data so
535 * we can try to deflate it ourselves, this may save on.
541 memset(&stream, 0, sizeof(stream));
542 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
543 gzip_size = git_deflate_bound(&stream, rpc->len);
544 gzip_body = xmalloc(gzip_size);
546 stream.next_in = (unsigned char *)rpc->buf;
547 stream.avail_in = rpc->len;
548 stream.next_out = (unsigned char *)gzip_body;
549 stream.avail_out = gzip_size;
551 ret = git_deflate(&stream, Z_FINISH);
552 if (ret != Z_STREAM_END)
553 die("cannot deflate request; zlib deflate error %d", ret);
555 ret = git_deflate_end_gently(&stream);
557 die("cannot deflate request; zlib end error %d", ret);
559 gzip_size = stream.total_out;
561 headers = curl_slist_append(headers, "Content-Encoding: gzip");
562 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
563 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
565 if (options.verbosity > 1) {
566 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
568 (unsigned long)rpc->len, (unsigned long)gzip_size);
572 /* We know the complete request size in advance, use the
573 * more normal Content-Length approach.
575 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
576 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
577 if (options.verbosity > 1) {
578 fprintf(stderr, "POST %s (%lu bytes)\n",
579 rpc->service_name, (unsigned long)rpc->len);
584 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
585 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
586 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
588 err = run_slot(slot, NULL);
589 if (err == HTTP_REAUTH && !large_request) {
590 credential_fill(&http_auth);
596 curl_slist_free_all(headers);
601 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
603 const char *svc = rpc->service_name;
604 struct strbuf buf = STRBUF_INIT;
605 struct strbuf *preamble = rpc->stdin_preamble;
606 struct child_process client;
609 memset(&client, 0, sizeof(client));
613 client.argv = rpc->argv;
614 if (start_command(&client))
617 write_or_die(client.in, preamble->buf, preamble->len);
619 write_or_die(client.in, heads->buf, heads->len);
621 rpc->alloc = http_post_buffer;
622 rpc->buf = xmalloc(rpc->alloc);
624 rpc->out = client.out;
625 strbuf_init(&rpc->result, 0);
627 strbuf_addf(&buf, "%s%s", url.buf, svc);
628 rpc->service_url = strbuf_detach(&buf, NULL);
630 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
631 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
633 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
634 rpc->hdr_accept = strbuf_detach(&buf, NULL);
637 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
642 err |= post_rpc(rpc);
648 strbuf_read(&rpc->result, client.out, 0);
652 if (xread(client.out, buf, sizeof(buf)) <= 0)
659 err |= finish_command(&client);
660 free(rpc->service_url);
661 free(rpc->hdr_content_type);
662 free(rpc->hdr_accept);
664 strbuf_release(&buf);
668 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
670 struct walker *walker;
671 char **targets = xmalloc(nr_heads * sizeof(char*));
675 die("dumb http transport does not support --depth");
676 for (i = 0; i < nr_heads; i++)
677 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
679 walker = get_http_walker(url.buf);
681 walker->get_tree = 1;
682 walker->get_history = 1;
683 walker->get_verbosely = options.verbosity >= 3;
684 walker->get_recover = 0;
685 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
688 for (i = 0; i < nr_heads; i++)
692 return ret ? error("Fetch failed.") : 0;
695 static int fetch_git(struct discovery *heads,
696 int nr_heads, struct ref **to_fetch)
698 struct rpc_state rpc;
699 struct strbuf preamble = STRBUF_INIT;
700 char *depth_arg = NULL;
701 int argc = 0, i, err;
702 const char *argv[16];
704 argv[argc++] = "fetch-pack";
705 argv[argc++] = "--stateless-rpc";
706 argv[argc++] = "--stdin";
707 argv[argc++] = "--lock-pack";
708 if (options.followtags)
709 argv[argc++] = "--include-tag";
711 argv[argc++] = "--thin";
712 if (options.verbosity >= 3) {
716 if (options.check_self_contained_and_connected)
717 argv[argc++] = "--check-self-contained-and-connected";
718 if (!options.progress)
719 argv[argc++] = "--no-progress";
721 struct strbuf buf = STRBUF_INIT;
722 strbuf_addf(&buf, "--depth=%lu", options.depth);
723 depth_arg = strbuf_detach(&buf, NULL);
724 argv[argc++] = depth_arg;
726 argv[argc++] = url.buf;
729 for (i = 0; i < nr_heads; i++) {
730 struct ref *ref = to_fetch[i];
731 if (!ref->name || !*ref->name)
732 die("cannot fetch by sha1 over smart http");
733 packet_buf_write(&preamble, "%s\n", ref->name);
735 packet_buf_flush(&preamble);
737 memset(&rpc, 0, sizeof(rpc));
738 rpc.service_name = "git-upload-pack",
740 rpc.stdin_preamble = &preamble;
741 rpc.gzip_request = 1;
743 err = rpc_service(&rpc, heads);
745 write_or_die(1, rpc.result.buf, rpc.result.len);
746 strbuf_release(&rpc.result);
747 strbuf_release(&preamble);
752 static int fetch(int nr_heads, struct ref **to_fetch)
754 struct discovery *d = discover_refs("git-upload-pack", 0);
756 return fetch_git(d, nr_heads, to_fetch);
758 return fetch_dumb(nr_heads, to_fetch);
761 static void parse_fetch(struct strbuf *buf)
763 struct ref **to_fetch = NULL;
764 struct ref *list_head = NULL;
765 struct ref **list = &list_head;
766 int alloc_heads = 0, nr_heads = 0;
769 if (starts_with(buf->buf, "fetch ")) {
770 char *p = buf->buf + strlen("fetch ");
773 unsigned char old_sha1[20];
775 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
776 die("protocol error: expected sha/ref, got %s'", p);
782 die("protocol error: expected sha/ref, got %s'", p);
784 ref = alloc_ref(name);
785 hashcpy(ref->old_sha1, old_sha1);
790 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
791 to_fetch[nr_heads++] = ref;
794 die("http transport does not support %s", buf->buf);
797 if (strbuf_getline(buf, stdin, '\n') == EOF)
803 if (fetch(nr_heads, to_fetch))
804 exit(128); /* error already reported */
805 free_refs(list_head);
813 static int push_dav(int nr_spec, char **specs)
815 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
818 argv[argc++] = "http-push";
819 argv[argc++] = "--helper-status";
821 argv[argc++] = "--dry-run";
822 if (options.verbosity > 1)
823 argv[argc++] = "--verbose";
824 argv[argc++] = url.buf;
825 for (i = 0; i < nr_spec; i++)
826 argv[argc++] = specs[i];
829 if (run_command_v_opt(argv, RUN_GIT_CMD))
830 die("git-%s failed", argv[0]);
835 static int push_git(struct discovery *heads, int nr_spec, char **specs)
837 struct rpc_state rpc;
839 struct argv_array args;
840 struct string_list_item *cas_option;
842 argv_array_init(&args);
843 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
847 argv_array_push(&args, "--thin");
849 argv_array_push(&args, "--dry-run");
850 if (options.verbosity == 0)
851 argv_array_push(&args, "--quiet");
852 else if (options.verbosity > 1)
853 argv_array_push(&args, "--verbose");
854 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
855 for_each_string_list_item(cas_option, &cas_options)
856 argv_array_push(&args, cas_option->string);
857 argv_array_push(&args, url.buf);
858 for (i = 0; i < nr_spec; i++)
859 argv_array_push(&args, specs[i]);
861 memset(&rpc, 0, sizeof(rpc));
862 rpc.service_name = "git-receive-pack",
863 rpc.argv = args.argv;
865 err = rpc_service(&rpc, heads);
867 write_or_die(1, rpc.result.buf, rpc.result.len);
868 strbuf_release(&rpc.result);
869 argv_array_clear(&args);
873 static int push(int nr_spec, char **specs)
875 struct discovery *heads = discover_refs("git-receive-pack", 1);
878 if (heads->proto_git)
879 ret = push_git(heads, nr_spec, specs);
881 ret = push_dav(nr_spec, specs);
882 free_discovery(heads);
886 static void parse_push(struct strbuf *buf)
889 int alloc_spec = 0, nr_spec = 0, i, ret;
892 if (starts_with(buf->buf, "push ")) {
893 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
894 specs[nr_spec++] = xstrdup(buf->buf + 5);
897 die("http transport does not support %s", buf->buf);
900 if (strbuf_getline(buf, stdin, '\n') == EOF)
906 ret = push(nr_spec, specs);
911 exit(128); /* error already reported */
914 for (i = 0; i < nr_spec; i++)
919 int main(int argc, const char **argv)
921 struct strbuf buf = STRBUF_INIT;
924 git_extract_argv0_path(argv[0]);
925 setup_git_directory_gently(&nongit);
927 fprintf(stderr, "Remote needed\n");
931 options.verbosity = 1;
932 options.progress = !!isatty(2);
935 remote = remote_get(argv[1]);
938 end_url_with_slash(&url, argv[2]);
940 end_url_with_slash(&url, remote->url[0]);
943 http_init(remote, url.buf, 0);
946 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
948 fprintf(stderr, "Error reading command stream\n");
950 fprintf(stderr, "Unexpected end of command stream\n");
955 if (starts_with(buf.buf, "fetch ")) {
957 die("Fetch attempted without a local repo");
960 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
961 int for_push = !!strstr(buf.buf + 4, "for-push");
962 output_refs(get_refs(for_push));
964 } else if (starts_with(buf.buf, "push ")) {
967 } else if (starts_with(buf.buf, "option ")) {
968 char *name = buf.buf + strlen("option ");
969 char *value = strchr(name, ' ');
977 result = set_option(name, value);
981 printf("error invalid value\n");
983 printf("unsupported\n");
986 } else if (!strcmp(buf.buf, "capabilities")) {
990 printf("check-connectivity\n");
994 fprintf(stderr, "Unknown command '%s'\n", buf.buf);