7 #include "run-command.h"
11 static struct remote *remote;
12 static const char *url; /* always ends with a trailing slash */
17 unsigned progress : 1,
22 static struct options options;
24 static int set_option(const char *name, const char *value)
26 if (!strcmp(name, "verbosity")) {
28 int v = strtol(value, &end, 10);
29 if (value == end || *end)
31 options.verbosity = v;
34 else if (!strcmp(name, "progress")) {
35 if (!strcmp(value, "true"))
37 else if (!strcmp(value, "false"))
43 else if (!strcmp(name, "depth")) {
45 unsigned long v = strtoul(value, &end, 10);
46 if (value == end || *end)
51 else if (!strcmp(name, "followtags")) {
52 if (!strcmp(value, "true"))
53 options.followtags = 1;
54 else if (!strcmp(value, "false"))
55 options.followtags = 0;
60 else if (!strcmp(name, "dry-run")) {
61 if (!strcmp(value, "true"))
63 else if (!strcmp(value, "false"))
70 return 1 /* unsupported */;
79 unsigned proto_git : 1;
81 static struct discovery *last_discovery;
83 static void free_discovery(struct discovery *d)
86 if (d == last_discovery)
87 last_discovery = NULL;
93 static struct discovery* discover_refs(const char *service)
95 struct strbuf exp = STRBUF_INIT;
96 struct strbuf type = STRBUF_INIT;
97 struct strbuf buffer = STRBUF_INIT;
98 struct discovery *last = last_discovery;
100 int http_ret, maybe_smart = 0;
102 if (last && !strcmp(service, last->service))
104 free_discovery(last);
106 strbuf_addf(&buffer, "%sinfo/refs", url);
107 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
108 git_env_bool("GIT_SMART_HTTP", 1)) {
110 if (!strchr(url, '?'))
111 strbuf_addch(&buffer, '?');
113 strbuf_addch(&buffer, '&');
114 strbuf_addf(&buffer, "service=%s", service);
116 refs_url = strbuf_detach(&buffer, NULL);
118 http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
122 case HTTP_MISSING_TARGET:
123 die("%s not found: did you run git update-server-info on the"
124 " server?", refs_url);
126 die("Authentication failed");
128 http_error(refs_url, http_ret);
129 die("HTTP request failed");
132 last= xcalloc(1, sizeof(*last_discovery));
133 last->service = service;
134 last->buf_alloc = strbuf_detach(&buffer, &last->len);
135 last->buf = last->buf_alloc;
137 strbuf_addf(&exp, "application/x-%s-advertisement", service);
139 (5 <= last->len && last->buf[4] == '#') &&
140 !strbuf_cmp(&exp, &type)) {
144 * smart HTTP response; validate that the service
145 * pkt-line matches our request.
147 line = packet_read_line_buf(&last->buf, &last->len, NULL);
150 strbuf_addf(&exp, "# service=%s", service);
151 if (strcmp(line, exp.buf))
152 die("invalid server response; got '%s'", line);
153 strbuf_release(&exp);
155 /* The header can include additional metadata lines, up
156 * until a packet flush marker. Ignore these now, but
157 * in the future we might start to scan them.
159 while (packet_read_line_buf(&last->buf, &last->len, NULL))
166 strbuf_release(&exp);
167 strbuf_release(&type);
168 strbuf_release(&buffer);
169 last_discovery = last;
173 static int write_discovery(int in, int out, void *data)
175 struct discovery *heads = data;
177 if (write_in_full(out, heads->buf, heads->len) != heads->len)
183 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
185 struct ref *list = NULL;
188 memset(&async, 0, sizeof(async));
189 async.proc = write_discovery;
193 if (start_async(&async))
194 die("cannot start thread to parse advertised refs");
195 get_remote_heads(async.out, &list,
196 for_push ? REF_NORMAL : 0, NULL);
198 if (finish_async(&async))
199 die("ref parsing thread failed");
203 static struct ref *parse_info_refs(struct discovery *heads)
205 char *data, *start, *mid;
209 struct ref *refs = NULL;
210 struct ref *ref = NULL;
211 struct ref *last_ref = NULL;
216 while (i < heads->len) {
222 if (data[i] == '\n') {
223 if (mid - start != 40)
224 die("%sinfo/refs not valid: is this a git repository?", url);
227 ref = xmalloc(sizeof(struct ref) +
228 strlen(ref_name) + 1);
229 memset(ref, 0, sizeof(struct ref));
230 strcpy(ref->name, ref_name);
231 get_sha1_hex(start, ref->old_sha1);
235 last_ref->next = ref;
242 ref = alloc_ref("HEAD");
243 if (!http_fetch_ref(url, ref) &&
244 !resolve_remote_symref(ref, refs)) {
254 static struct ref *get_refs(int for_push)
256 struct discovery *heads;
259 heads = discover_refs("git-receive-pack");
261 heads = discover_refs("git-upload-pack");
263 if (heads->proto_git)
264 return parse_git_refs(heads, for_push);
265 return parse_info_refs(heads);
268 static void output_refs(struct ref *refs)
271 for (posn = refs; posn; posn = posn->next) {
273 printf("@%s %s\n", posn->symref, posn->name);
275 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
283 const char *service_name;
285 struct strbuf *stdin_preamble;
287 char *hdr_content_type;
295 struct strbuf result;
296 unsigned gzip_request : 1;
297 unsigned initial_buffer : 1;
300 static size_t rpc_out(void *ptr, size_t eltsize,
301 size_t nmemb, void *buffer_)
303 size_t max = eltsize * nmemb;
304 struct rpc_state *rpc = buffer_;
305 size_t avail = rpc->len - rpc->pos;
308 rpc->initial_buffer = 0;
309 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
318 memcpy(ptr, rpc->buf + rpc->pos, avail);
323 #ifndef NO_CURL_IOCTL
324 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
326 struct rpc_state *rpc = clientp;
332 case CURLIOCMD_RESTARTREAD:
333 if (rpc->initial_buffer) {
337 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
338 return CURLIOE_FAILRESTART;
341 return CURLIOE_UNKNOWNCMD;
346 static size_t rpc_in(char *ptr, size_t eltsize,
347 size_t nmemb, void *buffer_)
349 size_t size = eltsize * nmemb;
350 struct rpc_state *rpc = buffer_;
351 write_or_die(rpc->in, ptr, size);
355 static int run_slot(struct active_request_slot *slot)
358 struct slot_results results;
360 slot->results = &results;
361 slot->curl_result = curl_easy_perform(slot->curl);
362 finish_active_slot(slot);
364 err = handle_curl_result(&results);
365 if (err != HTTP_OK && err != HTTP_REAUTH) {
366 error("RPC failed; result=%d, HTTP code = %ld",
367 results.curl_result, results.http_code);
373 static int probe_rpc(struct rpc_state *rpc)
375 struct active_request_slot *slot;
376 struct curl_slist *headers = NULL;
377 struct strbuf buf = STRBUF_INIT;
380 slot = get_active_slot();
382 headers = curl_slist_append(headers, rpc->hdr_content_type);
383 headers = curl_slist_append(headers, rpc->hdr_accept);
385 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
386 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
387 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
388 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
389 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
390 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
391 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
392 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
393 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
395 err = run_slot(slot);
397 curl_slist_free_all(headers);
398 strbuf_release(&buf);
402 static int post_rpc(struct rpc_state *rpc)
404 struct active_request_slot *slot;
405 struct curl_slist *headers = NULL;
406 int use_gzip = rpc->gzip_request;
407 char *gzip_body = NULL;
408 size_t gzip_size = 0;
409 int err, large_request = 0;
411 /* Try to load the entire request, if we can fit it into the
412 * allocated buffer space we can use HTTP/1.0 and avoid the
413 * chunked encoding mess.
416 size_t left = rpc->alloc - rpc->len;
417 char *buf = rpc->buf + rpc->len;
420 if (left < LARGE_PACKET_MAX) {
426 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
434 err = probe_rpc(rpc);
435 } while (err == HTTP_REAUTH);
440 headers = curl_slist_append(headers, rpc->hdr_content_type);
441 headers = curl_slist_append(headers, rpc->hdr_accept);
442 headers = curl_slist_append(headers, "Expect:");
445 slot = get_active_slot();
447 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
448 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
449 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
450 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
453 /* The request body is large and the size cannot be predicted.
454 * We must use chunked encoding to send it.
456 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
457 rpc->initial_buffer = 1;
458 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
459 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
460 #ifndef NO_CURL_IOCTL
461 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
462 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
464 if (options.verbosity > 1) {
465 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
469 } else if (gzip_body) {
471 * If we are looping to retry authentication, then the previous
472 * run will have set up the headers and gzip buffer already,
473 * and we just need to send it.
475 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
476 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
478 } else if (use_gzip && 1024 < rpc->len) {
479 /* The client backend isn't giving us compressed data so
480 * we can try to deflate it ourselves, this may save on.
486 memset(&stream, 0, sizeof(stream));
487 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
488 gzip_size = git_deflate_bound(&stream, rpc->len);
489 gzip_body = xmalloc(gzip_size);
491 stream.next_in = (unsigned char *)rpc->buf;
492 stream.avail_in = rpc->len;
493 stream.next_out = (unsigned char *)gzip_body;
494 stream.avail_out = gzip_size;
496 ret = git_deflate(&stream, Z_FINISH);
497 if (ret != Z_STREAM_END)
498 die("cannot deflate request; zlib deflate error %d", ret);
500 ret = git_deflate_end_gently(&stream);
502 die("cannot deflate request; zlib end error %d", ret);
504 gzip_size = stream.total_out;
506 headers = curl_slist_append(headers, "Content-Encoding: gzip");
507 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
508 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
510 if (options.verbosity > 1) {
511 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
513 (unsigned long)rpc->len, (unsigned long)gzip_size);
517 /* We know the complete request size in advance, use the
518 * more normal Content-Length approach.
520 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
521 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
522 if (options.verbosity > 1) {
523 fprintf(stderr, "POST %s (%lu bytes)\n",
524 rpc->service_name, (unsigned long)rpc->len);
529 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
530 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
531 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
533 err = run_slot(slot);
534 if (err == HTTP_REAUTH && !large_request)
539 curl_slist_free_all(headers);
544 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
546 const char *svc = rpc->service_name;
547 struct strbuf buf = STRBUF_INIT;
548 struct strbuf *preamble = rpc->stdin_preamble;
549 struct child_process client;
552 memset(&client, 0, sizeof(client));
556 client.argv = rpc->argv;
557 if (start_command(&client))
560 write_or_die(client.in, preamble->buf, preamble->len);
562 write_or_die(client.in, heads->buf, heads->len);
564 rpc->alloc = http_post_buffer;
565 rpc->buf = xmalloc(rpc->alloc);
567 rpc->out = client.out;
568 strbuf_init(&rpc->result, 0);
570 strbuf_addf(&buf, "%s%s", url, svc);
571 rpc->service_url = strbuf_detach(&buf, NULL);
573 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
574 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
576 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
577 rpc->hdr_accept = strbuf_detach(&buf, NULL);
580 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
585 err |= post_rpc(rpc);
591 strbuf_read(&rpc->result, client.out, 0);
595 if (xread(client.out, buf, sizeof(buf)) <= 0)
602 err |= finish_command(&client);
603 free(rpc->service_url);
604 free(rpc->hdr_content_type);
605 free(rpc->hdr_accept);
607 strbuf_release(&buf);
611 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
613 struct walker *walker;
614 char **targets = xmalloc(nr_heads * sizeof(char*));
618 die("dumb http transport does not support --depth");
619 for (i = 0; i < nr_heads; i++)
620 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
622 walker = get_http_walker(url);
624 walker->get_tree = 1;
625 walker->get_history = 1;
626 walker->get_verbosely = options.verbosity >= 3;
627 walker->get_recover = 0;
628 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
631 for (i = 0; i < nr_heads; i++)
635 return ret ? error("Fetch failed.") : 0;
638 static int fetch_git(struct discovery *heads,
639 int nr_heads, struct ref **to_fetch)
641 struct rpc_state rpc;
642 struct strbuf preamble = STRBUF_INIT;
643 char *depth_arg = NULL;
644 int argc = 0, i, err;
645 const char *argv[15];
647 argv[argc++] = "fetch-pack";
648 argv[argc++] = "--stateless-rpc";
649 argv[argc++] = "--stdin";
650 argv[argc++] = "--lock-pack";
651 if (options.followtags)
652 argv[argc++] = "--include-tag";
654 argv[argc++] = "--thin";
655 if (options.verbosity >= 3) {
659 if (!options.progress)
660 argv[argc++] = "--no-progress";
662 struct strbuf buf = STRBUF_INIT;
663 strbuf_addf(&buf, "--depth=%lu", options.depth);
664 depth_arg = strbuf_detach(&buf, NULL);
665 argv[argc++] = depth_arg;
670 for (i = 0; i < nr_heads; i++) {
671 struct ref *ref = to_fetch[i];
672 if (!ref->name || !*ref->name)
673 die("cannot fetch by sha1 over smart http");
674 packet_buf_write(&preamble, "%s\n", ref->name);
676 packet_buf_flush(&preamble);
678 memset(&rpc, 0, sizeof(rpc));
679 rpc.service_name = "git-upload-pack",
681 rpc.stdin_preamble = &preamble;
682 rpc.gzip_request = 1;
684 err = rpc_service(&rpc, heads);
686 write_or_die(1, rpc.result.buf, rpc.result.len);
687 strbuf_release(&rpc.result);
688 strbuf_release(&preamble);
693 static int fetch(int nr_heads, struct ref **to_fetch)
695 struct discovery *d = discover_refs("git-upload-pack");
697 return fetch_git(d, nr_heads, to_fetch);
699 return fetch_dumb(nr_heads, to_fetch);
702 static void parse_fetch(struct strbuf *buf)
704 struct ref **to_fetch = NULL;
705 struct ref *list_head = NULL;
706 struct ref **list = &list_head;
707 int alloc_heads = 0, nr_heads = 0;
710 if (!prefixcmp(buf->buf, "fetch ")) {
711 char *p = buf->buf + strlen("fetch ");
714 unsigned char old_sha1[20];
716 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
717 die("protocol error: expected sha/ref, got %s'", p);
723 die("protocol error: expected sha/ref, got %s'", p);
725 ref = alloc_ref(name);
726 hashcpy(ref->old_sha1, old_sha1);
731 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
732 to_fetch[nr_heads++] = ref;
735 die("http transport does not support %s", buf->buf);
738 if (strbuf_getline(buf, stdin, '\n') == EOF)
744 if (fetch(nr_heads, to_fetch))
745 exit(128); /* error already reported */
746 free_refs(list_head);
754 static int push_dav(int nr_spec, char **specs)
756 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
759 argv[argc++] = "http-push";
760 argv[argc++] = "--helper-status";
762 argv[argc++] = "--dry-run";
763 if (options.verbosity > 1)
764 argv[argc++] = "--verbose";
766 for (i = 0; i < nr_spec; i++)
767 argv[argc++] = specs[i];
770 if (run_command_v_opt(argv, RUN_GIT_CMD))
771 die("git-%s failed", argv[0]);
776 static int push_git(struct discovery *heads, int nr_spec, char **specs)
778 struct rpc_state rpc;
780 int argc = 0, i, err;
782 argv = xmalloc((10 + nr_spec) * sizeof(char*));
783 argv[argc++] = "send-pack";
784 argv[argc++] = "--stateless-rpc";
785 argv[argc++] = "--helper-status";
787 argv[argc++] = "--thin";
789 argv[argc++] = "--dry-run";
790 if (options.verbosity == 0)
791 argv[argc++] = "--quiet";
792 else if (options.verbosity > 1)
793 argv[argc++] = "--verbose";
794 argv[argc++] = options.progress ? "--progress" : "--no-progress";
796 for (i = 0; i < nr_spec; i++)
797 argv[argc++] = specs[i];
800 memset(&rpc, 0, sizeof(rpc));
801 rpc.service_name = "git-receive-pack",
804 err = rpc_service(&rpc, heads);
806 write_or_die(1, rpc.result.buf, rpc.result.len);
807 strbuf_release(&rpc.result);
812 static int push(int nr_spec, char **specs)
814 struct discovery *heads = discover_refs("git-receive-pack");
817 if (heads->proto_git)
818 ret = push_git(heads, nr_spec, specs);
820 ret = push_dav(nr_spec, specs);
821 free_discovery(heads);
825 static void parse_push(struct strbuf *buf)
828 int alloc_spec = 0, nr_spec = 0, i, ret;
831 if (!prefixcmp(buf->buf, "push ")) {
832 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
833 specs[nr_spec++] = xstrdup(buf->buf + 5);
836 die("http transport does not support %s", buf->buf);
839 if (strbuf_getline(buf, stdin, '\n') == EOF)
845 ret = push(nr_spec, specs);
850 exit(128); /* error already reported */
853 for (i = 0; i < nr_spec; i++)
858 int main(int argc, const char **argv)
860 struct strbuf buf = STRBUF_INIT;
863 git_extract_argv0_path(argv[0]);
864 setup_git_directory_gently(&nongit);
866 fprintf(stderr, "Remote needed\n");
870 options.verbosity = 1;
871 options.progress = !!isatty(2);
874 remote = remote_get(argv[1]);
877 end_url_with_slash(&buf, argv[2]);
879 end_url_with_slash(&buf, remote->url[0]);
882 url = strbuf_detach(&buf, NULL);
884 http_init(remote, url, 0);
887 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
889 fprintf(stderr, "Error reading command stream\n");
891 fprintf(stderr, "Unexpected end of command stream\n");
896 if (!prefixcmp(buf.buf, "fetch ")) {
898 die("Fetch attempted without a local repo");
901 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
902 int for_push = !!strstr(buf.buf + 4, "for-push");
903 output_refs(get_refs(for_push));
905 } else if (!prefixcmp(buf.buf, "push ")) {
908 } else if (!prefixcmp(buf.buf, "option ")) {
909 char *name = buf.buf + strlen("option ");
910 char *value = strchr(name, ' ');
918 result = set_option(name, value);
922 printf("error invalid value\n");
924 printf("unsupported\n");
927 } else if (!strcmp(buf.buf, "capabilities")) {
934 fprintf(stderr, "Unknown command '%s'\n", buf.buf);