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 */;
80 unsigned proto_git : 1;
82 static struct discovery *last_discovery;
84 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
86 struct ref *list = NULL;
87 get_remote_heads(-1, heads->buf, heads->len, &list,
88 for_push ? REF_NORMAL : 0, NULL);
92 static struct ref *parse_info_refs(struct discovery *heads)
94 char *data, *start, *mid;
98 struct ref *refs = NULL;
99 struct ref *ref = NULL;
100 struct ref *last_ref = NULL;
105 while (i < heads->len) {
111 if (data[i] == '\n') {
112 if (mid - start != 40)
113 die("%sinfo/refs not valid: is this a git repository?", url);
116 ref = xmalloc(sizeof(struct ref) +
117 strlen(ref_name) + 1);
118 memset(ref, 0, sizeof(struct ref));
119 strcpy(ref->name, ref_name);
120 get_sha1_hex(start, ref->old_sha1);
124 last_ref->next = ref;
131 ref = alloc_ref("HEAD");
132 if (!http_fetch_ref(url, ref) &&
133 !resolve_remote_symref(ref, refs)) {
143 static void free_discovery(struct discovery *d)
146 if (d == last_discovery)
147 last_discovery = NULL;
154 static int show_http_message(struct strbuf *type, struct strbuf *msg)
159 * We only show text/plain parts, as other types are likely
160 * to be ugly to look at on the user's terminal.
162 * TODO should handle "; charset=XXX", and re-encode into
165 if (strcasecmp(type->buf, "text/plain"))
174 eol = strchrnul(p, '\n');
175 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
181 static struct discovery* discover_refs(const char *service, int for_push)
183 struct strbuf exp = STRBUF_INIT;
184 struct strbuf type = STRBUF_INIT;
185 struct strbuf buffer = STRBUF_INIT;
186 struct discovery *last = last_discovery;
188 int http_ret, maybe_smart = 0;
190 if (last && !strcmp(service, last->service))
192 free_discovery(last);
194 strbuf_addf(&buffer, "%sinfo/refs", url);
195 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
196 git_env_bool("GIT_SMART_HTTP", 1)) {
198 if (!strchr(url, '?'))
199 strbuf_addch(&buffer, '?');
201 strbuf_addch(&buffer, '&');
202 strbuf_addf(&buffer, "service=%s", service);
204 refs_url = strbuf_detach(&buffer, NULL);
206 http_ret = http_get_strbuf(refs_url, &type, &buffer,
207 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
211 case HTTP_MISSING_TARGET:
212 show_http_message(&type, &buffer);
213 die("%s not found: did you run git update-server-info on the"
214 " server?", refs_url);
216 show_http_message(&type, &buffer);
217 die("Authentication failed");
219 show_http_message(&type, &buffer);
220 http_error(refs_url, http_ret);
221 die("HTTP request failed");
224 last= xcalloc(1, sizeof(*last_discovery));
225 last->service = service;
226 last->buf_alloc = strbuf_detach(&buffer, &last->len);
227 last->buf = last->buf_alloc;
229 strbuf_addf(&exp, "application/x-%s-advertisement", service);
231 (5 <= last->len && last->buf[4] == '#') &&
232 !strbuf_cmp(&exp, &type)) {
236 * smart HTTP response; validate that the service
237 * pkt-line matches our request.
239 line = packet_read_line_buf(&last->buf, &last->len, NULL);
242 strbuf_addf(&exp, "# service=%s", service);
243 if (strcmp(line, exp.buf))
244 die("invalid server response; got '%s'", line);
245 strbuf_release(&exp);
247 /* The header can include additional metadata lines, up
248 * until a packet flush marker. Ignore these now, but
249 * in the future we might start to scan them.
251 while (packet_read_line_buf(&last->buf, &last->len, NULL))
258 last->refs = parse_git_refs(last, for_push);
260 last->refs = parse_info_refs(last);
263 strbuf_release(&exp);
264 strbuf_release(&type);
265 strbuf_release(&buffer);
266 last_discovery = last;
270 static struct ref *get_refs(int for_push)
272 struct discovery *heads;
275 heads = discover_refs("git-receive-pack", for_push);
277 heads = discover_refs("git-upload-pack", for_push);
282 static void output_refs(struct ref *refs)
285 for (posn = refs; posn; posn = posn->next) {
287 printf("@%s %s\n", posn->symref, posn->name);
289 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
296 const char *service_name;
298 struct strbuf *stdin_preamble;
300 char *hdr_content_type;
308 struct strbuf result;
309 unsigned gzip_request : 1;
310 unsigned initial_buffer : 1;
313 static size_t rpc_out(void *ptr, size_t eltsize,
314 size_t nmemb, void *buffer_)
316 size_t max = eltsize * nmemb;
317 struct rpc_state *rpc = buffer_;
318 size_t avail = rpc->len - rpc->pos;
321 rpc->initial_buffer = 0;
322 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
331 memcpy(ptr, rpc->buf + rpc->pos, avail);
336 #ifndef NO_CURL_IOCTL
337 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
339 struct rpc_state *rpc = clientp;
345 case CURLIOCMD_RESTARTREAD:
346 if (rpc->initial_buffer) {
350 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
351 return CURLIOE_FAILRESTART;
354 return CURLIOE_UNKNOWNCMD;
359 static size_t rpc_in(char *ptr, size_t eltsize,
360 size_t nmemb, void *buffer_)
362 size_t size = eltsize * nmemb;
363 struct rpc_state *rpc = buffer_;
364 write_or_die(rpc->in, ptr, size);
368 static int run_slot(struct active_request_slot *slot)
371 struct slot_results results;
373 slot->results = &results;
374 slot->curl_result = curl_easy_perform(slot->curl);
375 finish_active_slot(slot);
377 err = handle_curl_result(&results);
378 if (err != HTTP_OK && err != HTTP_REAUTH) {
379 error("RPC failed; result=%d, HTTP code = %ld",
380 results.curl_result, results.http_code);
386 static int probe_rpc(struct rpc_state *rpc)
388 struct active_request_slot *slot;
389 struct curl_slist *headers = NULL;
390 struct strbuf buf = STRBUF_INIT;
393 slot = get_active_slot();
395 headers = curl_slist_append(headers, rpc->hdr_content_type);
396 headers = curl_slist_append(headers, rpc->hdr_accept);
398 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
399 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
400 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
401 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
402 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
403 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
404 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
405 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
406 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
408 err = run_slot(slot);
410 curl_slist_free_all(headers);
411 strbuf_release(&buf);
415 static int post_rpc(struct rpc_state *rpc)
417 struct active_request_slot *slot;
418 struct curl_slist *headers = NULL;
419 int use_gzip = rpc->gzip_request;
420 char *gzip_body = NULL;
421 size_t gzip_size = 0;
422 int err, large_request = 0;
424 /* Try to load the entire request, if we can fit it into the
425 * allocated buffer space we can use HTTP/1.0 and avoid the
426 * chunked encoding mess.
429 size_t left = rpc->alloc - rpc->len;
430 char *buf = rpc->buf + rpc->len;
433 if (left < LARGE_PACKET_MAX) {
439 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
447 err = probe_rpc(rpc);
448 } while (err == HTTP_REAUTH);
453 headers = curl_slist_append(headers, rpc->hdr_content_type);
454 headers = curl_slist_append(headers, rpc->hdr_accept);
455 headers = curl_slist_append(headers, "Expect:");
458 slot = get_active_slot();
460 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
461 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
462 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
463 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
466 /* The request body is large and the size cannot be predicted.
467 * We must use chunked encoding to send it.
469 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
470 rpc->initial_buffer = 1;
471 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
472 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
473 #ifndef NO_CURL_IOCTL
474 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
475 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
477 if (options.verbosity > 1) {
478 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
482 } else if (gzip_body) {
484 * If we are looping to retry authentication, then the previous
485 * run will have set up the headers and gzip buffer already,
486 * and we just need to send it.
488 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
489 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
491 } else if (use_gzip && 1024 < rpc->len) {
492 /* The client backend isn't giving us compressed data so
493 * we can try to deflate it ourselves, this may save on.
499 memset(&stream, 0, sizeof(stream));
500 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
501 gzip_size = git_deflate_bound(&stream, rpc->len);
502 gzip_body = xmalloc(gzip_size);
504 stream.next_in = (unsigned char *)rpc->buf;
505 stream.avail_in = rpc->len;
506 stream.next_out = (unsigned char *)gzip_body;
507 stream.avail_out = gzip_size;
509 ret = git_deflate(&stream, Z_FINISH);
510 if (ret != Z_STREAM_END)
511 die("cannot deflate request; zlib deflate error %d", ret);
513 ret = git_deflate_end_gently(&stream);
515 die("cannot deflate request; zlib end error %d", ret);
517 gzip_size = stream.total_out;
519 headers = curl_slist_append(headers, "Content-Encoding: gzip");
520 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
521 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
523 if (options.verbosity > 1) {
524 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
526 (unsigned long)rpc->len, (unsigned long)gzip_size);
530 /* We know the complete request size in advance, use the
531 * more normal Content-Length approach.
533 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
534 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
535 if (options.verbosity > 1) {
536 fprintf(stderr, "POST %s (%lu bytes)\n",
537 rpc->service_name, (unsigned long)rpc->len);
542 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
543 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
544 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
546 err = run_slot(slot);
547 if (err == HTTP_REAUTH && !large_request)
552 curl_slist_free_all(headers);
557 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
559 const char *svc = rpc->service_name;
560 struct strbuf buf = STRBUF_INIT;
561 struct strbuf *preamble = rpc->stdin_preamble;
562 struct child_process client;
565 memset(&client, 0, sizeof(client));
569 client.argv = rpc->argv;
570 if (start_command(&client))
573 write_or_die(client.in, preamble->buf, preamble->len);
575 write_or_die(client.in, heads->buf, heads->len);
577 rpc->alloc = http_post_buffer;
578 rpc->buf = xmalloc(rpc->alloc);
580 rpc->out = client.out;
581 strbuf_init(&rpc->result, 0);
583 strbuf_addf(&buf, "%s%s", url, svc);
584 rpc->service_url = strbuf_detach(&buf, NULL);
586 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
587 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
589 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
590 rpc->hdr_accept = strbuf_detach(&buf, NULL);
593 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
598 err |= post_rpc(rpc);
604 strbuf_read(&rpc->result, client.out, 0);
608 if (xread(client.out, buf, sizeof(buf)) <= 0)
615 err |= finish_command(&client);
616 free(rpc->service_url);
617 free(rpc->hdr_content_type);
618 free(rpc->hdr_accept);
620 strbuf_release(&buf);
624 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
626 struct walker *walker;
627 char **targets = xmalloc(nr_heads * sizeof(char*));
631 die("dumb http transport does not support --depth");
632 for (i = 0; i < nr_heads; i++)
633 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
635 walker = get_http_walker(url);
637 walker->get_tree = 1;
638 walker->get_history = 1;
639 walker->get_verbosely = options.verbosity >= 3;
640 walker->get_recover = 0;
641 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
644 for (i = 0; i < nr_heads; i++)
648 return ret ? error("Fetch failed.") : 0;
651 static int fetch_git(struct discovery *heads,
652 int nr_heads, struct ref **to_fetch)
654 struct rpc_state rpc;
655 struct strbuf preamble = STRBUF_INIT;
656 char *depth_arg = NULL;
657 int argc = 0, i, err;
658 const char *argv[15];
660 argv[argc++] = "fetch-pack";
661 argv[argc++] = "--stateless-rpc";
662 argv[argc++] = "--stdin";
663 argv[argc++] = "--lock-pack";
664 if (options.followtags)
665 argv[argc++] = "--include-tag";
667 argv[argc++] = "--thin";
668 if (options.verbosity >= 3) {
672 if (!options.progress)
673 argv[argc++] = "--no-progress";
675 struct strbuf buf = STRBUF_INIT;
676 strbuf_addf(&buf, "--depth=%lu", options.depth);
677 depth_arg = strbuf_detach(&buf, NULL);
678 argv[argc++] = depth_arg;
683 for (i = 0; i < nr_heads; i++) {
684 struct ref *ref = to_fetch[i];
685 if (!ref->name || !*ref->name)
686 die("cannot fetch by sha1 over smart http");
687 packet_buf_write(&preamble, "%s\n", ref->name);
689 packet_buf_flush(&preamble);
691 memset(&rpc, 0, sizeof(rpc));
692 rpc.service_name = "git-upload-pack",
694 rpc.stdin_preamble = &preamble;
695 rpc.gzip_request = 1;
697 err = rpc_service(&rpc, heads);
699 write_or_die(1, rpc.result.buf, rpc.result.len);
700 strbuf_release(&rpc.result);
701 strbuf_release(&preamble);
706 static int fetch(int nr_heads, struct ref **to_fetch)
708 struct discovery *d = discover_refs("git-upload-pack", 0);
710 return fetch_git(d, nr_heads, to_fetch);
712 return fetch_dumb(nr_heads, to_fetch);
715 static void parse_fetch(struct strbuf *buf)
717 struct ref **to_fetch = NULL;
718 struct ref *list_head = NULL;
719 struct ref **list = &list_head;
720 int alloc_heads = 0, nr_heads = 0;
723 if (!prefixcmp(buf->buf, "fetch ")) {
724 char *p = buf->buf + strlen("fetch ");
727 unsigned char old_sha1[20];
729 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
730 die("protocol error: expected sha/ref, got %s'", p);
736 die("protocol error: expected sha/ref, got %s'", p);
738 ref = alloc_ref(name);
739 hashcpy(ref->old_sha1, old_sha1);
744 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
745 to_fetch[nr_heads++] = ref;
748 die("http transport does not support %s", buf->buf);
751 if (strbuf_getline(buf, stdin, '\n') == EOF)
757 if (fetch(nr_heads, to_fetch))
758 exit(128); /* error already reported */
759 free_refs(list_head);
767 static int push_dav(int nr_spec, char **specs)
769 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
772 argv[argc++] = "http-push";
773 argv[argc++] = "--helper-status";
775 argv[argc++] = "--dry-run";
776 if (options.verbosity > 1)
777 argv[argc++] = "--verbose";
779 for (i = 0; i < nr_spec; i++)
780 argv[argc++] = specs[i];
783 if (run_command_v_opt(argv, RUN_GIT_CMD))
784 die("git-%s failed", argv[0]);
789 static int push_git(struct discovery *heads, int nr_spec, char **specs)
791 struct rpc_state rpc;
793 int argc = 0, i, err;
795 argv = xmalloc((10 + nr_spec) * sizeof(char*));
796 argv[argc++] = "send-pack";
797 argv[argc++] = "--stateless-rpc";
798 argv[argc++] = "--helper-status";
800 argv[argc++] = "--thin";
802 argv[argc++] = "--dry-run";
803 if (options.verbosity == 0)
804 argv[argc++] = "--quiet";
805 else if (options.verbosity > 1)
806 argv[argc++] = "--verbose";
807 argv[argc++] = options.progress ? "--progress" : "--no-progress";
809 for (i = 0; i < nr_spec; i++)
810 argv[argc++] = specs[i];
813 memset(&rpc, 0, sizeof(rpc));
814 rpc.service_name = "git-receive-pack",
817 err = rpc_service(&rpc, heads);
819 write_or_die(1, rpc.result.buf, rpc.result.len);
820 strbuf_release(&rpc.result);
825 static int push(int nr_spec, char **specs)
827 struct discovery *heads = discover_refs("git-receive-pack", 1);
830 if (heads->proto_git)
831 ret = push_git(heads, nr_spec, specs);
833 ret = push_dav(nr_spec, specs);
834 free_discovery(heads);
838 static void parse_push(struct strbuf *buf)
841 int alloc_spec = 0, nr_spec = 0, i, ret;
844 if (!prefixcmp(buf->buf, "push ")) {
845 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
846 specs[nr_spec++] = xstrdup(buf->buf + 5);
849 die("http transport does not support %s", buf->buf);
852 if (strbuf_getline(buf, stdin, '\n') == EOF)
858 ret = push(nr_spec, specs);
863 exit(128); /* error already reported */
866 for (i = 0; i < nr_spec; i++)
871 int main(int argc, const char **argv)
873 struct strbuf buf = STRBUF_INIT;
876 git_extract_argv0_path(argv[0]);
877 setup_git_directory_gently(&nongit);
879 fprintf(stderr, "Remote needed\n");
883 options.verbosity = 1;
884 options.progress = !!isatty(2);
887 remote = remote_get(argv[1]);
890 end_url_with_slash(&buf, argv[2]);
892 end_url_with_slash(&buf, remote->url[0]);
895 url = strbuf_detach(&buf, NULL);
897 http_init(remote, url, 0);
900 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
902 fprintf(stderr, "Error reading command stream\n");
904 fprintf(stderr, "Unexpected end of command stream\n");
909 if (!prefixcmp(buf.buf, "fetch ")) {
911 die("Fetch attempted without a local repo");
914 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
915 int for_push = !!strstr(buf.buf + 4, "for-push");
916 output_refs(get_refs(for_push));
918 } else if (!prefixcmp(buf.buf, "push ")) {
921 } else if (!prefixcmp(buf.buf, "option ")) {
922 char *name = buf.buf + strlen("option ");
923 char *value = strchr(name, ' ');
931 result = set_option(name, value);
935 printf("error invalid value\n");
937 printf("unsupported\n");
940 } else if (!strcmp(buf.buf, "capabilities")) {
947 fprintf(stderr, "Unknown command '%s'\n", buf.buf);