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)) {
142 * smart HTTP response; validate that the service
143 * pkt-line matches our request.
145 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
146 die("%s has invalid packet header", refs_url);
147 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
148 strbuf_setlen(&buffer, buffer.len - 1);
151 strbuf_addf(&exp, "# service=%s", service);
152 if (strbuf_cmp(&exp, &buffer))
153 die("invalid server response; got '%s'", buffer.buf);
154 strbuf_release(&exp);
156 /* The header can include additional metadata lines, up
157 * until a packet flush marker. Ignore these now, but
158 * in the future we might start to scan them.
160 strbuf_reset(&buffer);
161 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
162 strbuf_reset(&buffer);
168 strbuf_release(&exp);
169 strbuf_release(&type);
170 strbuf_release(&buffer);
171 last_discovery = last;
175 static int write_discovery(int in, int out, void *data)
177 struct discovery *heads = data;
179 if (write_in_full(out, heads->buf, heads->len) != heads->len)
185 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
187 struct ref *list = NULL;
190 memset(&async, 0, sizeof(async));
191 async.proc = write_discovery;
195 if (start_async(&async))
196 die("cannot start thread to parse advertised refs");
197 get_remote_heads(async.out, &list,
198 for_push ? REF_NORMAL : 0, NULL);
200 if (finish_async(&async))
201 die("ref parsing thread failed");
205 static struct ref *parse_info_refs(struct discovery *heads)
207 char *data, *start, *mid;
211 struct ref *refs = NULL;
212 struct ref *ref = NULL;
213 struct ref *last_ref = NULL;
218 while (i < heads->len) {
224 if (data[i] == '\n') {
225 if (mid - start != 40)
226 die("%sinfo/refs not valid: is this a git repository?", url);
229 ref = xmalloc(sizeof(struct ref) +
230 strlen(ref_name) + 1);
231 memset(ref, 0, sizeof(struct ref));
232 strcpy(ref->name, ref_name);
233 get_sha1_hex(start, ref->old_sha1);
237 last_ref->next = ref;
244 ref = alloc_ref("HEAD");
245 if (!http_fetch_ref(url, ref) &&
246 !resolve_remote_symref(ref, refs)) {
256 static struct ref *get_refs(int for_push)
258 struct discovery *heads;
261 heads = discover_refs("git-receive-pack");
263 heads = discover_refs("git-upload-pack");
265 if (heads->proto_git)
266 return parse_git_refs(heads, for_push);
267 return parse_info_refs(heads);
270 static void output_refs(struct ref *refs)
273 for (posn = refs; posn; posn = posn->next) {
275 printf("@%s %s\n", posn->symref, posn->name);
277 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
285 const char *service_name;
287 struct strbuf *stdin_preamble;
289 char *hdr_content_type;
297 struct strbuf result;
298 unsigned gzip_request : 1;
299 unsigned initial_buffer : 1;
302 static size_t rpc_out(void *ptr, size_t eltsize,
303 size_t nmemb, void *buffer_)
305 size_t max = eltsize * nmemb;
306 struct rpc_state *rpc = buffer_;
307 size_t avail = rpc->len - rpc->pos;
310 rpc->initial_buffer = 0;
311 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
320 memcpy(ptr, rpc->buf + rpc->pos, avail);
325 #ifndef NO_CURL_IOCTL
326 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
328 struct rpc_state *rpc = clientp;
334 case CURLIOCMD_RESTARTREAD:
335 if (rpc->initial_buffer) {
339 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
340 return CURLIOE_FAILRESTART;
343 return CURLIOE_UNKNOWNCMD;
348 static size_t rpc_in(char *ptr, size_t eltsize,
349 size_t nmemb, void *buffer_)
351 size_t size = eltsize * nmemb;
352 struct rpc_state *rpc = buffer_;
353 write_or_die(rpc->in, ptr, size);
357 static int run_slot(struct active_request_slot *slot)
360 struct slot_results results;
362 slot->results = &results;
363 slot->curl_result = curl_easy_perform(slot->curl);
364 finish_active_slot(slot);
366 err = handle_curl_result(&results);
367 if (err != HTTP_OK && err != HTTP_REAUTH) {
368 error("RPC failed; result=%d, HTTP code = %ld",
369 results.curl_result, results.http_code);
375 static int probe_rpc(struct rpc_state *rpc)
377 struct active_request_slot *slot;
378 struct curl_slist *headers = NULL;
379 struct strbuf buf = STRBUF_INIT;
382 slot = get_active_slot();
384 headers = curl_slist_append(headers, rpc->hdr_content_type);
385 headers = curl_slist_append(headers, rpc->hdr_accept);
387 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
388 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
389 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
390 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
391 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
392 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
393 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
394 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
395 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
397 err = run_slot(slot);
399 curl_slist_free_all(headers);
400 strbuf_release(&buf);
404 static int post_rpc(struct rpc_state *rpc)
406 struct active_request_slot *slot;
407 struct curl_slist *headers = NULL;
408 int use_gzip = rpc->gzip_request;
409 char *gzip_body = NULL;
410 size_t gzip_size = 0;
411 int err, large_request = 0;
413 /* Try to load the entire request, if we can fit it into the
414 * allocated buffer space we can use HTTP/1.0 and avoid the
415 * chunked encoding mess.
418 size_t left = rpc->alloc - rpc->len;
419 char *buf = rpc->buf + rpc->len;
422 if (left < LARGE_PACKET_MAX) {
428 n = packet_read_line(rpc->out, buf, left);
436 err = probe_rpc(rpc);
437 } while (err == HTTP_REAUTH);
442 headers = curl_slist_append(headers, rpc->hdr_content_type);
443 headers = curl_slist_append(headers, rpc->hdr_accept);
444 headers = curl_slist_append(headers, "Expect:");
447 slot = get_active_slot();
449 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
450 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
451 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
452 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
455 /* The request body is large and the size cannot be predicted.
456 * We must use chunked encoding to send it.
458 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
459 rpc->initial_buffer = 1;
460 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
461 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
462 #ifndef NO_CURL_IOCTL
463 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
464 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
466 if (options.verbosity > 1) {
467 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
471 } else if (gzip_body) {
473 * If we are looping to retry authentication, then the previous
474 * run will have set up the headers and gzip buffer already,
475 * and we just need to send it.
477 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
478 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
480 } else if (use_gzip && 1024 < rpc->len) {
481 /* The client backend isn't giving us compressed data so
482 * we can try to deflate it ourselves, this may save on.
488 memset(&stream, 0, sizeof(stream));
489 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
490 gzip_size = git_deflate_bound(&stream, rpc->len);
491 gzip_body = xmalloc(gzip_size);
493 stream.next_in = (unsigned char *)rpc->buf;
494 stream.avail_in = rpc->len;
495 stream.next_out = (unsigned char *)gzip_body;
496 stream.avail_out = gzip_size;
498 ret = git_deflate(&stream, Z_FINISH);
499 if (ret != Z_STREAM_END)
500 die("cannot deflate request; zlib deflate error %d", ret);
502 ret = git_deflate_end_gently(&stream);
504 die("cannot deflate request; zlib end error %d", ret);
506 gzip_size = stream.total_out;
508 headers = curl_slist_append(headers, "Content-Encoding: gzip");
509 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
510 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
512 if (options.verbosity > 1) {
513 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
515 (unsigned long)rpc->len, (unsigned long)gzip_size);
519 /* We know the complete request size in advance, use the
520 * more normal Content-Length approach.
522 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
523 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
524 if (options.verbosity > 1) {
525 fprintf(stderr, "POST %s (%lu bytes)\n",
526 rpc->service_name, (unsigned long)rpc->len);
531 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
532 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
533 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
535 err = run_slot(slot);
536 if (err == HTTP_REAUTH && !large_request)
541 curl_slist_free_all(headers);
546 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
548 const char *svc = rpc->service_name;
549 struct strbuf buf = STRBUF_INIT;
550 struct strbuf *preamble = rpc->stdin_preamble;
551 struct child_process client;
554 memset(&client, 0, sizeof(client));
558 client.argv = rpc->argv;
559 if (start_command(&client))
562 write_or_die(client.in, preamble->buf, preamble->len);
564 write_or_die(client.in, heads->buf, heads->len);
566 rpc->alloc = http_post_buffer;
567 rpc->buf = xmalloc(rpc->alloc);
569 rpc->out = client.out;
570 strbuf_init(&rpc->result, 0);
572 strbuf_addf(&buf, "%s%s", url, svc);
573 rpc->service_url = strbuf_detach(&buf, NULL);
575 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
576 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
578 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
579 rpc->hdr_accept = strbuf_detach(&buf, NULL);
582 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
587 err |= post_rpc(rpc);
593 strbuf_read(&rpc->result, client.out, 0);
597 if (xread(client.out, buf, sizeof(buf)) <= 0)
604 err |= finish_command(&client);
605 free(rpc->service_url);
606 free(rpc->hdr_content_type);
607 free(rpc->hdr_accept);
609 strbuf_release(&buf);
613 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
615 struct walker *walker;
616 char **targets = xmalloc(nr_heads * sizeof(char*));
620 die("dumb http transport does not support --depth");
621 for (i = 0; i < nr_heads; i++)
622 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
624 walker = get_http_walker(url);
626 walker->get_tree = 1;
627 walker->get_history = 1;
628 walker->get_verbosely = options.verbosity >= 3;
629 walker->get_recover = 0;
630 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
633 for (i = 0; i < nr_heads; i++)
637 return ret ? error("Fetch failed.") : 0;
640 static int fetch_git(struct discovery *heads,
641 int nr_heads, struct ref **to_fetch)
643 struct rpc_state rpc;
644 struct strbuf preamble = STRBUF_INIT;
645 char *depth_arg = NULL;
646 int argc = 0, i, err;
647 const char *argv[15];
649 argv[argc++] = "fetch-pack";
650 argv[argc++] = "--stateless-rpc";
651 argv[argc++] = "--stdin";
652 argv[argc++] = "--lock-pack";
653 if (options.followtags)
654 argv[argc++] = "--include-tag";
656 argv[argc++] = "--thin";
657 if (options.verbosity >= 3) {
661 if (!options.progress)
662 argv[argc++] = "--no-progress";
664 struct strbuf buf = STRBUF_INIT;
665 strbuf_addf(&buf, "--depth=%lu", options.depth);
666 depth_arg = strbuf_detach(&buf, NULL);
667 argv[argc++] = depth_arg;
672 for (i = 0; i < nr_heads; i++) {
673 struct ref *ref = to_fetch[i];
674 if (!ref->name || !*ref->name)
675 die("cannot fetch by sha1 over smart http");
676 packet_buf_write(&preamble, "%s\n", ref->name);
678 packet_buf_flush(&preamble);
680 memset(&rpc, 0, sizeof(rpc));
681 rpc.service_name = "git-upload-pack",
683 rpc.stdin_preamble = &preamble;
684 rpc.gzip_request = 1;
686 err = rpc_service(&rpc, heads);
688 safe_write(1, rpc.result.buf, rpc.result.len);
689 strbuf_release(&rpc.result);
690 strbuf_release(&preamble);
695 static int fetch(int nr_heads, struct ref **to_fetch)
697 struct discovery *d = discover_refs("git-upload-pack");
699 return fetch_git(d, nr_heads, to_fetch);
701 return fetch_dumb(nr_heads, to_fetch);
704 static void parse_fetch(struct strbuf *buf)
706 struct ref **to_fetch = NULL;
707 struct ref *list_head = NULL;
708 struct ref **list = &list_head;
709 int alloc_heads = 0, nr_heads = 0;
712 if (!prefixcmp(buf->buf, "fetch ")) {
713 char *p = buf->buf + strlen("fetch ");
716 unsigned char old_sha1[20];
718 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
719 die("protocol error: expected sha/ref, got %s'", p);
725 die("protocol error: expected sha/ref, got %s'", p);
727 ref = alloc_ref(name);
728 hashcpy(ref->old_sha1, old_sha1);
733 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
734 to_fetch[nr_heads++] = ref;
737 die("http transport does not support %s", buf->buf);
740 if (strbuf_getline(buf, stdin, '\n') == EOF)
746 if (fetch(nr_heads, to_fetch))
747 exit(128); /* error already reported */
748 free_refs(list_head);
756 static int push_dav(int nr_spec, char **specs)
758 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
761 argv[argc++] = "http-push";
762 argv[argc++] = "--helper-status";
764 argv[argc++] = "--dry-run";
765 if (options.verbosity > 1)
766 argv[argc++] = "--verbose";
768 for (i = 0; i < nr_spec; i++)
769 argv[argc++] = specs[i];
772 if (run_command_v_opt(argv, RUN_GIT_CMD))
773 die("git-%s failed", argv[0]);
778 static int push_git(struct discovery *heads, int nr_spec, char **specs)
780 struct rpc_state rpc;
782 int argc = 0, i, err;
784 argv = xmalloc((10 + nr_spec) * sizeof(char*));
785 argv[argc++] = "send-pack";
786 argv[argc++] = "--stateless-rpc";
787 argv[argc++] = "--helper-status";
789 argv[argc++] = "--thin";
791 argv[argc++] = "--dry-run";
792 if (options.verbosity == 0)
793 argv[argc++] = "--quiet";
794 else if (options.verbosity > 1)
795 argv[argc++] = "--verbose";
796 argv[argc++] = options.progress ? "--progress" : "--no-progress";
798 for (i = 0; i < nr_spec; i++)
799 argv[argc++] = specs[i];
802 memset(&rpc, 0, sizeof(rpc));
803 rpc.service_name = "git-receive-pack",
806 err = rpc_service(&rpc, heads);
808 safe_write(1, rpc.result.buf, rpc.result.len);
809 strbuf_release(&rpc.result);
814 static int push(int nr_spec, char **specs)
816 struct discovery *heads = discover_refs("git-receive-pack");
819 if (heads->proto_git)
820 ret = push_git(heads, nr_spec, specs);
822 ret = push_dav(nr_spec, specs);
823 free_discovery(heads);
827 static void parse_push(struct strbuf *buf)
830 int alloc_spec = 0, nr_spec = 0, i, ret;
833 if (!prefixcmp(buf->buf, "push ")) {
834 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
835 specs[nr_spec++] = xstrdup(buf->buf + 5);
838 die("http transport does not support %s", buf->buf);
841 if (strbuf_getline(buf, stdin, '\n') == EOF)
847 ret = push(nr_spec, specs);
852 exit(128); /* error already reported */
855 for (i = 0; i < nr_spec; i++)
860 int main(int argc, const char **argv)
862 struct strbuf buf = STRBUF_INIT;
865 git_extract_argv0_path(argv[0]);
866 setup_git_directory_gently(&nongit);
868 fprintf(stderr, "Remote needed\n");
872 options.verbosity = 1;
873 options.progress = !!isatty(2);
876 remote = remote_get(argv[1]);
879 end_url_with_slash(&buf, argv[2]);
881 end_url_with_slash(&buf, remote->url[0]);
884 url = strbuf_detach(&buf, NULL);
886 http_init(remote, url, 0);
889 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
891 fprintf(stderr, "Error reading command stream\n");
893 fprintf(stderr, "Unexpected end of command stream\n");
898 if (!prefixcmp(buf.buf, "fetch ")) {
900 die("Fetch attempted without a local repo");
903 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
904 int for_push = !!strstr(buf.buf + 4, "for-push");
905 output_refs(get_refs(for_push));
907 } else if (!prefixcmp(buf.buf, "push ")) {
910 } else if (!prefixcmp(buf.buf, "option ")) {
911 char *name = buf.buf + strlen("option ");
912 char *value = strchr(name, ' ');
920 result = set_option(name, value);
924 printf("error invalid value\n");
926 printf("unsupported\n");
929 } else if (!strcmp(buf.buf, "capabilities")) {
936 fprintf(stderr, "Unknown command '%s'\n", buf.buf);