7 #include "run-command.h"
9 #include "string-list.h"
11 #include "argv-array.h"
13 static struct remote *remote;
14 static const char *url; /* always ends with a trailing slash */
19 unsigned progress : 1,
20 check_self_contained_and_connected : 1,
25 static struct options options;
26 static struct string_list cas_options = STRING_LIST_INIT_DUP;
28 static int set_option(const char *name, const char *value)
30 if (!strcmp(name, "verbosity")) {
32 int v = strtol(value, &end, 10);
33 if (value == end || *end)
35 options.verbosity = v;
38 else if (!strcmp(name, "progress")) {
39 if (!strcmp(value, "true"))
41 else if (!strcmp(value, "false"))
47 else if (!strcmp(name, "depth")) {
49 unsigned long v = strtoul(value, &end, 10);
50 if (value == end || *end)
55 else if (!strcmp(name, "followtags")) {
56 if (!strcmp(value, "true"))
57 options.followtags = 1;
58 else if (!strcmp(value, "false"))
59 options.followtags = 0;
64 else if (!strcmp(name, "dry-run")) {
65 if (!strcmp(value, "true"))
67 else if (!strcmp(value, "false"))
73 else if (!strcmp(name, "check-connectivity")) {
74 if (!strcmp(value, "true"))
75 options.check_self_contained_and_connected = 1;
76 else if (!strcmp(value, "false"))
77 options.check_self_contained_and_connected = 0;
82 else if (!strcmp(name, "cas")) {
83 struct strbuf val = STRBUF_INIT;
84 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
85 string_list_append(&cas_options, val.buf);
90 return 1 /* unsupported */;
100 unsigned proto_git : 1;
102 static struct discovery *last_discovery;
104 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
106 struct ref *list = NULL;
107 get_remote_heads(-1, heads->buf, heads->len, &list,
108 for_push ? REF_NORMAL : 0, NULL);
112 static struct ref *parse_info_refs(struct discovery *heads)
114 char *data, *start, *mid;
118 struct ref *refs = NULL;
119 struct ref *ref = NULL;
120 struct ref *last_ref = NULL;
125 while (i < heads->len) {
131 if (data[i] == '\n') {
132 if (mid - start != 40)
133 die("%sinfo/refs not valid: is this a git repository?", url);
136 ref = xmalloc(sizeof(struct ref) +
137 strlen(ref_name) + 1);
138 memset(ref, 0, sizeof(struct ref));
139 strcpy(ref->name, ref_name);
140 get_sha1_hex(start, ref->old_sha1);
144 last_ref->next = ref;
151 ref = alloc_ref("HEAD");
152 if (!http_fetch_ref(url, ref) &&
153 !resolve_remote_symref(ref, refs)) {
163 static void free_discovery(struct discovery *d)
166 if (d == last_discovery)
167 last_discovery = NULL;
174 static int show_http_message(struct strbuf *type, struct strbuf *msg)
179 * We only show text/plain parts, as other types are likely
180 * to be ugly to look at on the user's terminal.
182 * TODO should handle "; charset=XXX", and re-encode into
185 if (strcasecmp(type->buf, "text/plain"))
194 eol = strchrnul(p, '\n');
195 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
201 static struct discovery* discover_refs(const char *service, int for_push)
203 struct strbuf exp = STRBUF_INIT;
204 struct strbuf type = STRBUF_INIT;
205 struct strbuf buffer = STRBUF_INIT;
206 struct discovery *last = last_discovery;
208 int http_ret, maybe_smart = 0;
210 if (last && !strcmp(service, last->service))
212 free_discovery(last);
214 strbuf_addf(&buffer, "%sinfo/refs", url);
215 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
216 git_env_bool("GIT_SMART_HTTP", 1)) {
218 if (!strchr(url, '?'))
219 strbuf_addch(&buffer, '?');
221 strbuf_addch(&buffer, '&');
222 strbuf_addf(&buffer, "service=%s", service);
224 refs_url = strbuf_detach(&buffer, NULL);
226 http_ret = http_get_strbuf(refs_url, &type, &buffer,
227 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
231 case HTTP_MISSING_TARGET:
232 show_http_message(&type, &buffer);
233 die("repository '%s' not found", url);
235 show_http_message(&type, &buffer);
236 die("Authentication failed for '%s'", url);
238 show_http_message(&type, &buffer);
239 die("unable to access '%s': %s", url, curl_errorstr);
242 last= xcalloc(1, sizeof(*last_discovery));
243 last->service = service;
244 last->buf_alloc = strbuf_detach(&buffer, &last->len);
245 last->buf = last->buf_alloc;
247 strbuf_addf(&exp, "application/x-%s-advertisement", service);
249 (5 <= last->len && last->buf[4] == '#') &&
250 !strbuf_cmp(&exp, &type)) {
254 * smart HTTP response; validate that the service
255 * pkt-line matches our request.
257 line = packet_read_line_buf(&last->buf, &last->len, NULL);
260 strbuf_addf(&exp, "# service=%s", service);
261 if (strcmp(line, exp.buf))
262 die("invalid server response; got '%s'", line);
263 strbuf_release(&exp);
265 /* The header can include additional metadata lines, up
266 * until a packet flush marker. Ignore these now, but
267 * in the future we might start to scan them.
269 while (packet_read_line_buf(&last->buf, &last->len, NULL))
276 last->refs = parse_git_refs(last, for_push);
278 last->refs = parse_info_refs(last);
281 strbuf_release(&exp);
282 strbuf_release(&type);
283 strbuf_release(&buffer);
284 last_discovery = last;
288 static struct ref *get_refs(int for_push)
290 struct discovery *heads;
293 heads = discover_refs("git-receive-pack", for_push);
295 heads = discover_refs("git-upload-pack", for_push);
300 static void output_refs(struct ref *refs)
303 for (posn = refs; posn; posn = posn->next) {
305 printf("@%s %s\n", posn->symref, posn->name);
307 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
314 const char *service_name;
316 struct strbuf *stdin_preamble;
318 char *hdr_content_type;
326 struct strbuf result;
327 unsigned gzip_request : 1;
328 unsigned initial_buffer : 1;
331 static size_t rpc_out(void *ptr, size_t eltsize,
332 size_t nmemb, void *buffer_)
334 size_t max = eltsize * nmemb;
335 struct rpc_state *rpc = buffer_;
336 size_t avail = rpc->len - rpc->pos;
339 rpc->initial_buffer = 0;
340 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
349 memcpy(ptr, rpc->buf + rpc->pos, avail);
354 #ifndef NO_CURL_IOCTL
355 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
357 struct rpc_state *rpc = clientp;
363 case CURLIOCMD_RESTARTREAD:
364 if (rpc->initial_buffer) {
368 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
369 return CURLIOE_FAILRESTART;
372 return CURLIOE_UNKNOWNCMD;
377 static size_t rpc_in(char *ptr, size_t eltsize,
378 size_t nmemb, void *buffer_)
380 size_t size = eltsize * nmemb;
381 struct rpc_state *rpc = buffer_;
382 write_or_die(rpc->in, ptr, size);
386 static int run_slot(struct active_request_slot *slot)
389 struct slot_results results;
391 slot->results = &results;
392 slot->curl_result = curl_easy_perform(slot->curl);
393 finish_active_slot(slot);
395 err = handle_curl_result(&results);
396 if (err != HTTP_OK && err != HTTP_REAUTH) {
397 error("RPC failed; result=%d, HTTP code = %ld",
398 results.curl_result, results.http_code);
404 static int probe_rpc(struct rpc_state *rpc)
406 struct active_request_slot *slot;
407 struct curl_slist *headers = NULL;
408 struct strbuf buf = STRBUF_INIT;
411 slot = get_active_slot();
413 headers = curl_slist_append(headers, rpc->hdr_content_type);
414 headers = curl_slist_append(headers, rpc->hdr_accept);
416 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
417 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
418 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
419 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
420 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
421 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
422 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
423 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
424 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
426 err = run_slot(slot);
428 curl_slist_free_all(headers);
429 strbuf_release(&buf);
433 static int post_rpc(struct rpc_state *rpc)
435 struct active_request_slot *slot;
436 struct curl_slist *headers = NULL;
437 int use_gzip = rpc->gzip_request;
438 char *gzip_body = NULL;
439 size_t gzip_size = 0;
440 int err, large_request = 0;
442 /* Try to load the entire request, if we can fit it into the
443 * allocated buffer space we can use HTTP/1.0 and avoid the
444 * chunked encoding mess.
447 size_t left = rpc->alloc - rpc->len;
448 char *buf = rpc->buf + rpc->len;
451 if (left < LARGE_PACKET_MAX) {
457 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
465 err = probe_rpc(rpc);
466 } while (err == HTTP_REAUTH);
471 headers = curl_slist_append(headers, rpc->hdr_content_type);
472 headers = curl_slist_append(headers, rpc->hdr_accept);
473 headers = curl_slist_append(headers, "Expect:");
476 slot = get_active_slot();
478 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
479 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
480 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
481 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
484 /* The request body is large and the size cannot be predicted.
485 * We must use chunked encoding to send it.
487 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
488 rpc->initial_buffer = 1;
489 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
490 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
491 #ifndef NO_CURL_IOCTL
492 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
493 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
495 if (options.verbosity > 1) {
496 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
500 } else if (gzip_body) {
502 * If we are looping to retry authentication, then the previous
503 * run will have set up the headers and gzip buffer already,
504 * and we just need to send it.
506 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
507 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
509 } else if (use_gzip && 1024 < rpc->len) {
510 /* The client backend isn't giving us compressed data so
511 * we can try to deflate it ourselves, this may save on.
517 memset(&stream, 0, sizeof(stream));
518 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
519 gzip_size = git_deflate_bound(&stream, rpc->len);
520 gzip_body = xmalloc(gzip_size);
522 stream.next_in = (unsigned char *)rpc->buf;
523 stream.avail_in = rpc->len;
524 stream.next_out = (unsigned char *)gzip_body;
525 stream.avail_out = gzip_size;
527 ret = git_deflate(&stream, Z_FINISH);
528 if (ret != Z_STREAM_END)
529 die("cannot deflate request; zlib deflate error %d", ret);
531 ret = git_deflate_end_gently(&stream);
533 die("cannot deflate request; zlib end error %d", ret);
535 gzip_size = stream.total_out;
537 headers = curl_slist_append(headers, "Content-Encoding: gzip");
538 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
539 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
541 if (options.verbosity > 1) {
542 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
544 (unsigned long)rpc->len, (unsigned long)gzip_size);
548 /* We know the complete request size in advance, use the
549 * more normal Content-Length approach.
551 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
552 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
553 if (options.verbosity > 1) {
554 fprintf(stderr, "POST %s (%lu bytes)\n",
555 rpc->service_name, (unsigned long)rpc->len);
560 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
561 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
562 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
564 err = run_slot(slot);
565 if (err == HTTP_REAUTH && !large_request)
570 curl_slist_free_all(headers);
575 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
577 const char *svc = rpc->service_name;
578 struct strbuf buf = STRBUF_INIT;
579 struct strbuf *preamble = rpc->stdin_preamble;
580 struct child_process client;
583 memset(&client, 0, sizeof(client));
587 client.argv = rpc->argv;
588 if (start_command(&client))
591 write_or_die(client.in, preamble->buf, preamble->len);
593 write_or_die(client.in, heads->buf, heads->len);
595 rpc->alloc = http_post_buffer;
596 rpc->buf = xmalloc(rpc->alloc);
598 rpc->out = client.out;
599 strbuf_init(&rpc->result, 0);
601 strbuf_addf(&buf, "%s%s", url, svc);
602 rpc->service_url = strbuf_detach(&buf, NULL);
604 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
605 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
607 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
608 rpc->hdr_accept = strbuf_detach(&buf, NULL);
611 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
616 err |= post_rpc(rpc);
622 strbuf_read(&rpc->result, client.out, 0);
626 if (xread(client.out, buf, sizeof(buf)) <= 0)
633 err |= finish_command(&client);
634 free(rpc->service_url);
635 free(rpc->hdr_content_type);
636 free(rpc->hdr_accept);
638 strbuf_release(&buf);
642 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
644 struct walker *walker;
645 char **targets = xmalloc(nr_heads * sizeof(char*));
649 die("dumb http transport does not support --depth");
650 for (i = 0; i < nr_heads; i++)
651 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
653 walker = get_http_walker(url);
655 walker->get_tree = 1;
656 walker->get_history = 1;
657 walker->get_verbosely = options.verbosity >= 3;
658 walker->get_recover = 0;
659 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
662 for (i = 0; i < nr_heads; i++)
666 return ret ? error("Fetch failed.") : 0;
669 static int fetch_git(struct discovery *heads,
670 int nr_heads, struct ref **to_fetch)
672 struct rpc_state rpc;
673 struct strbuf preamble = STRBUF_INIT;
674 char *depth_arg = NULL;
675 int argc = 0, i, err;
676 const char *argv[16];
678 argv[argc++] = "fetch-pack";
679 argv[argc++] = "--stateless-rpc";
680 argv[argc++] = "--stdin";
681 argv[argc++] = "--lock-pack";
682 if (options.followtags)
683 argv[argc++] = "--include-tag";
685 argv[argc++] = "--thin";
686 if (options.verbosity >= 3) {
690 if (options.check_self_contained_and_connected)
691 argv[argc++] = "--check-self-contained-and-connected";
692 if (!options.progress)
693 argv[argc++] = "--no-progress";
695 struct strbuf buf = STRBUF_INIT;
696 strbuf_addf(&buf, "--depth=%lu", options.depth);
697 depth_arg = strbuf_detach(&buf, NULL);
698 argv[argc++] = depth_arg;
703 for (i = 0; i < nr_heads; i++) {
704 struct ref *ref = to_fetch[i];
705 if (!ref->name || !*ref->name)
706 die("cannot fetch by sha1 over smart http");
707 packet_buf_write(&preamble, "%s\n", ref->name);
709 packet_buf_flush(&preamble);
711 memset(&rpc, 0, sizeof(rpc));
712 rpc.service_name = "git-upload-pack",
714 rpc.stdin_preamble = &preamble;
715 rpc.gzip_request = 1;
717 err = rpc_service(&rpc, heads);
719 write_or_die(1, rpc.result.buf, rpc.result.len);
720 strbuf_release(&rpc.result);
721 strbuf_release(&preamble);
726 static int fetch(int nr_heads, struct ref **to_fetch)
728 struct discovery *d = discover_refs("git-upload-pack", 0);
730 return fetch_git(d, nr_heads, to_fetch);
732 return fetch_dumb(nr_heads, to_fetch);
735 static void parse_fetch(struct strbuf *buf)
737 struct ref **to_fetch = NULL;
738 struct ref *list_head = NULL;
739 struct ref **list = &list_head;
740 int alloc_heads = 0, nr_heads = 0;
743 if (!prefixcmp(buf->buf, "fetch ")) {
744 char *p = buf->buf + strlen("fetch ");
747 unsigned char old_sha1[20];
749 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
750 die("protocol error: expected sha/ref, got %s'", p);
756 die("protocol error: expected sha/ref, got %s'", p);
758 ref = alloc_ref(name);
759 hashcpy(ref->old_sha1, old_sha1);
764 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
765 to_fetch[nr_heads++] = ref;
768 die("http transport does not support %s", buf->buf);
771 if (strbuf_getline(buf, stdin, '\n') == EOF)
777 if (fetch(nr_heads, to_fetch))
778 exit(128); /* error already reported */
779 free_refs(list_head);
787 static int push_dav(int nr_spec, char **specs)
789 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
792 argv[argc++] = "http-push";
793 argv[argc++] = "--helper-status";
795 argv[argc++] = "--dry-run";
796 if (options.verbosity > 1)
797 argv[argc++] = "--verbose";
799 for (i = 0; i < nr_spec; i++)
800 argv[argc++] = specs[i];
803 if (run_command_v_opt(argv, RUN_GIT_CMD))
804 die("git-%s failed", argv[0]);
809 static int push_git(struct discovery *heads, int nr_spec, char **specs)
811 struct rpc_state rpc;
813 struct argv_array args;
814 struct string_list_item *cas_option;
816 argv_array_init(&args);
817 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
821 argv_array_push(&args, "--thin");
823 argv_array_push(&args, "--dry-run");
824 if (options.verbosity == 0)
825 argv_array_push(&args, "--quiet");
826 else if (options.verbosity > 1)
827 argv_array_push(&args, "--verbose");
828 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
829 for_each_string_list_item(cas_option, &cas_options)
830 argv_array_push(&args, cas_option->string);
831 argv_array_push(&args, url);
832 for (i = 0; i < nr_spec; i++)
833 argv_array_push(&args, specs[i]);
835 memset(&rpc, 0, sizeof(rpc));
836 rpc.service_name = "git-receive-pack",
837 rpc.argv = args.argv;
839 err = rpc_service(&rpc, heads);
841 write_or_die(1, rpc.result.buf, rpc.result.len);
842 strbuf_release(&rpc.result);
843 argv_array_clear(&args);
847 static int push(int nr_spec, char **specs)
849 struct discovery *heads = discover_refs("git-receive-pack", 1);
852 if (heads->proto_git)
853 ret = push_git(heads, nr_spec, specs);
855 ret = push_dav(nr_spec, specs);
856 free_discovery(heads);
860 static void parse_push(struct strbuf *buf)
863 int alloc_spec = 0, nr_spec = 0, i, ret;
866 if (!prefixcmp(buf->buf, "push ")) {
867 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
868 specs[nr_spec++] = xstrdup(buf->buf + 5);
871 die("http transport does not support %s", buf->buf);
874 if (strbuf_getline(buf, stdin, '\n') == EOF)
880 ret = push(nr_spec, specs);
885 exit(128); /* error already reported */
888 for (i = 0; i < nr_spec; i++)
893 int main(int argc, const char **argv)
895 struct strbuf buf = STRBUF_INIT;
898 git_extract_argv0_path(argv[0]);
899 setup_git_directory_gently(&nongit);
901 fprintf(stderr, "Remote needed\n");
905 options.verbosity = 1;
906 options.progress = !!isatty(2);
909 remote = remote_get(argv[1]);
912 end_url_with_slash(&buf, argv[2]);
914 end_url_with_slash(&buf, remote->url[0]);
917 url = strbuf_detach(&buf, NULL);
919 http_init(remote, url, 0);
922 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
924 fprintf(stderr, "Error reading command stream\n");
926 fprintf(stderr, "Unexpected end of command stream\n");
931 if (!prefixcmp(buf.buf, "fetch ")) {
933 die("Fetch attempted without a local repo");
936 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
937 int for_push = !!strstr(buf.buf + 4, "for-push");
938 output_refs(get_refs(for_push));
940 } else if (!prefixcmp(buf.buf, "push ")) {
943 } else if (!prefixcmp(buf.buf, "option ")) {
944 char *name = buf.buf + strlen("option ");
945 char *value = strchr(name, ' ');
953 result = set_option(name, value);
957 printf("error invalid value\n");
959 printf("unsupported\n");
962 } else if (!strcmp(buf.buf, "capabilities")) {
966 printf("check-connectivity\n");
970 fprintf(stderr, "Unknown command '%s'\n", buf.buf);