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 buffer = STRBUF_INIT;
96 struct discovery *last = last_discovery;
98 int http_ret, is_http = 0;
100 if (last && !strcmp(service, last->service))
102 free_discovery(last);
104 strbuf_addf(&buffer, "%sinfo/refs", url);
105 if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) {
107 if (!strchr(url, '?'))
108 strbuf_addch(&buffer, '?');
110 strbuf_addch(&buffer, '&');
111 strbuf_addf(&buffer, "service=%s", service);
113 refs_url = strbuf_detach(&buffer, NULL);
115 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
119 case HTTP_MISSING_TARGET:
120 die("%s not found: did you run git update-server-info on the"
121 " server?", refs_url);
123 die("Authentication failed");
125 http_error(refs_url, http_ret);
126 die("HTTP request failed");
129 last= xcalloc(1, sizeof(*last_discovery));
130 last->service = service;
131 last->buf_alloc = strbuf_detach(&buffer, &last->len);
132 last->buf = last->buf_alloc;
134 if (is_http && 5 <= last->len && last->buf[4] == '#') {
135 /* smart HTTP response; validate that the service
136 * pkt-line matches our request.
138 struct strbuf exp = STRBUF_INIT;
140 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
141 die("%s has invalid packet header", refs_url);
142 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
143 strbuf_setlen(&buffer, buffer.len - 1);
145 strbuf_addf(&exp, "# service=%s", service);
146 if (strbuf_cmp(&exp, &buffer))
147 die("invalid server response; got '%s'", buffer.buf);
148 strbuf_release(&exp);
150 /* The header can include additional metadata lines, up
151 * until a packet flush marker. Ignore these now, but
152 * in the future we might start to scan them.
154 strbuf_reset(&buffer);
155 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
156 strbuf_reset(&buffer);
162 strbuf_release(&buffer);
163 last_discovery = last;
167 static int write_discovery(int in, int out, void *data)
169 struct discovery *heads = data;
171 if (write_in_full(out, heads->buf, heads->len) != heads->len)
177 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
179 struct ref *list = NULL;
182 memset(&async, 0, sizeof(async));
183 async.proc = write_discovery;
187 if (start_async(&async))
188 die("cannot start thread to parse advertised refs");
189 get_remote_heads(async.out, &list,
190 for_push ? REF_NORMAL : 0, NULL);
192 if (finish_async(&async))
193 die("ref parsing thread failed");
197 static struct ref *parse_info_refs(struct discovery *heads)
199 char *data, *start, *mid;
203 struct ref *refs = NULL;
204 struct ref *ref = NULL;
205 struct ref *last_ref = NULL;
210 while (i < heads->len) {
216 if (data[i] == '\n') {
217 if (mid - start != 40)
218 die("%sinfo/refs not valid: is this a git repository?", url);
221 ref = xmalloc(sizeof(struct ref) +
222 strlen(ref_name) + 1);
223 memset(ref, 0, sizeof(struct ref));
224 strcpy(ref->name, ref_name);
225 get_sha1_hex(start, ref->old_sha1);
229 last_ref->next = ref;
236 ref = alloc_ref("HEAD");
237 if (!http_fetch_ref(url, ref) &&
238 !resolve_remote_symref(ref, refs)) {
248 static struct ref *get_refs(int for_push)
250 struct discovery *heads;
253 heads = discover_refs("git-receive-pack");
255 heads = discover_refs("git-upload-pack");
257 if (heads->proto_git)
258 return parse_git_refs(heads, for_push);
259 return parse_info_refs(heads);
262 static void output_refs(struct ref *refs)
265 for (posn = refs; posn; posn = posn->next) {
267 printf("@%s %s\n", posn->symref, posn->name);
269 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
277 const char *service_name;
279 struct strbuf *stdin_preamble;
281 char *hdr_content_type;
289 struct strbuf result;
290 unsigned gzip_request : 1;
291 unsigned initial_buffer : 1;
294 static size_t rpc_out(void *ptr, size_t eltsize,
295 size_t nmemb, void *buffer_)
297 size_t max = eltsize * nmemb;
298 struct rpc_state *rpc = buffer_;
299 size_t avail = rpc->len - rpc->pos;
302 rpc->initial_buffer = 0;
303 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
312 memcpy(ptr, rpc->buf + rpc->pos, avail);
317 #ifndef NO_CURL_IOCTL
318 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
320 struct rpc_state *rpc = clientp;
326 case CURLIOCMD_RESTARTREAD:
327 if (rpc->initial_buffer) {
331 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
332 return CURLIOE_FAILRESTART;
335 return CURLIOE_UNKNOWNCMD;
340 static size_t rpc_in(char *ptr, size_t eltsize,
341 size_t nmemb, void *buffer_)
343 size_t size = eltsize * nmemb;
344 struct rpc_state *rpc = buffer_;
345 write_or_die(rpc->in, ptr, size);
349 static int run_slot(struct active_request_slot *slot)
352 struct slot_results results;
354 slot->results = &results;
355 slot->curl_result = curl_easy_perform(slot->curl);
356 finish_active_slot(slot);
358 err = handle_curl_result(slot, &results);
359 if (err != HTTP_OK && err != HTTP_REAUTH) {
360 error("RPC failed; result=%d, HTTP code = %ld",
361 results.curl_result, results.http_code);
367 static int probe_rpc(struct rpc_state *rpc)
369 struct active_request_slot *slot;
370 struct curl_slist *headers = NULL;
371 struct strbuf buf = STRBUF_INIT;
374 slot = get_active_slot();
376 headers = curl_slist_append(headers, rpc->hdr_content_type);
377 headers = curl_slist_append(headers, rpc->hdr_accept);
379 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
380 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
381 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
382 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
383 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
384 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
385 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
386 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
387 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
389 err = run_slot(slot);
391 curl_slist_free_all(headers);
392 strbuf_release(&buf);
396 static int post_rpc(struct rpc_state *rpc)
398 struct active_request_slot *slot;
399 struct curl_slist *headers = NULL;
400 int use_gzip = rpc->gzip_request;
401 char *gzip_body = NULL;
402 int err, large_request = 0;
404 /* Try to load the entire request, if we can fit it into the
405 * allocated buffer space we can use HTTP/1.0 and avoid the
406 * chunked encoding mess.
409 size_t left = rpc->alloc - rpc->len;
410 char *buf = rpc->buf + rpc->len;
413 if (left < LARGE_PACKET_MAX) {
419 n = packet_read_line(rpc->out, buf, left);
427 err = probe_rpc(rpc);
428 } while (err == HTTP_REAUTH);
433 slot = get_active_slot();
435 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
436 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
437 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
438 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
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 /* The request body is large and the size cannot be predicted.
446 * We must use chunked encoding to send it.
448 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
449 rpc->initial_buffer = 1;
450 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
451 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
452 #ifndef NO_CURL_IOCTL
453 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
454 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
456 if (options.verbosity > 1) {
457 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
461 } else if (use_gzip && 1024 < rpc->len) {
462 /* The client backend isn't giving us compressed data so
463 * we can try to deflate it ourselves, this may save on.
470 memset(&stream, 0, sizeof(stream));
471 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
472 size = git_deflate_bound(&stream, rpc->len);
473 gzip_body = xmalloc(size);
475 stream.next_in = (unsigned char *)rpc->buf;
476 stream.avail_in = rpc->len;
477 stream.next_out = (unsigned char *)gzip_body;
478 stream.avail_out = size;
480 ret = git_deflate(&stream, Z_FINISH);
481 if (ret != Z_STREAM_END)
482 die("cannot deflate request; zlib deflate error %d", ret);
484 ret = git_deflate_end_gently(&stream);
486 die("cannot deflate request; zlib end error %d", ret);
488 size = stream.total_out;
490 headers = curl_slist_append(headers, "Content-Encoding: gzip");
491 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
492 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
494 if (options.verbosity > 1) {
495 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
497 (unsigned long)rpc->len, (unsigned long)size);
501 /* We know the complete request size in advance, use the
502 * more normal Content-Length approach.
504 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
505 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
506 if (options.verbosity > 1) {
507 fprintf(stderr, "POST %s (%lu bytes)\n",
508 rpc->service_name, (unsigned long)rpc->len);
513 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
514 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
515 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
518 err = run_slot(slot);
519 } while (err == HTTP_REAUTH && !large_request && !use_gzip);
523 curl_slist_free_all(headers);
528 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
530 const char *svc = rpc->service_name;
531 struct strbuf buf = STRBUF_INIT;
532 struct strbuf *preamble = rpc->stdin_preamble;
533 struct child_process client;
536 memset(&client, 0, sizeof(client));
540 client.argv = rpc->argv;
541 if (start_command(&client))
544 write_or_die(client.in, preamble->buf, preamble->len);
546 write_or_die(client.in, heads->buf, heads->len);
548 rpc->alloc = http_post_buffer;
549 rpc->buf = xmalloc(rpc->alloc);
551 rpc->out = client.out;
552 strbuf_init(&rpc->result, 0);
554 strbuf_addf(&buf, "%s%s", url, svc);
555 rpc->service_url = strbuf_detach(&buf, NULL);
557 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
558 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
560 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
561 rpc->hdr_accept = strbuf_detach(&buf, NULL);
564 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
569 err |= post_rpc(rpc);
575 strbuf_read(&rpc->result, client.out, 0);
579 if (xread(client.out, buf, sizeof(buf)) <= 0)
586 err |= finish_command(&client);
587 free(rpc->service_url);
588 free(rpc->hdr_content_type);
589 free(rpc->hdr_accept);
591 strbuf_release(&buf);
595 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
597 struct walker *walker;
598 char **targets = xmalloc(nr_heads * sizeof(char*));
602 die("dumb http transport does not support --depth");
603 for (i = 0; i < nr_heads; i++)
604 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
606 walker = get_http_walker(url);
608 walker->get_tree = 1;
609 walker->get_history = 1;
610 walker->get_verbosely = options.verbosity >= 3;
611 walker->get_recover = 0;
612 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
615 for (i = 0; i < nr_heads; i++)
619 return ret ? error("Fetch failed.") : 0;
622 static int fetch_git(struct discovery *heads,
623 int nr_heads, struct ref **to_fetch)
625 struct rpc_state rpc;
626 struct strbuf preamble = STRBUF_INIT;
627 char *depth_arg = NULL;
628 int argc = 0, i, err;
629 const char *argv[15];
631 argv[argc++] = "fetch-pack";
632 argv[argc++] = "--stateless-rpc";
633 argv[argc++] = "--stdin";
634 argv[argc++] = "--lock-pack";
635 if (options.followtags)
636 argv[argc++] = "--include-tag";
638 argv[argc++] = "--thin";
639 if (options.verbosity >= 3) {
643 if (!options.progress)
644 argv[argc++] = "--no-progress";
646 struct strbuf buf = STRBUF_INIT;
647 strbuf_addf(&buf, "--depth=%lu", options.depth);
648 depth_arg = strbuf_detach(&buf, NULL);
649 argv[argc++] = depth_arg;
654 for (i = 0; i < nr_heads; i++) {
655 struct ref *ref = to_fetch[i];
656 if (!ref->name || !*ref->name)
657 die("cannot fetch by sha1 over smart http");
658 packet_buf_write(&preamble, "%s\n", ref->name);
660 packet_buf_flush(&preamble);
662 memset(&rpc, 0, sizeof(rpc));
663 rpc.service_name = "git-upload-pack",
665 rpc.stdin_preamble = &preamble;
666 rpc.gzip_request = 1;
668 err = rpc_service(&rpc, heads);
670 safe_write(1, rpc.result.buf, rpc.result.len);
671 strbuf_release(&rpc.result);
672 strbuf_release(&preamble);
677 static int fetch(int nr_heads, struct ref **to_fetch)
679 struct discovery *d = discover_refs("git-upload-pack");
681 return fetch_git(d, nr_heads, to_fetch);
683 return fetch_dumb(nr_heads, to_fetch);
686 static void parse_fetch(struct strbuf *buf)
688 struct ref **to_fetch = NULL;
689 struct ref *list_head = NULL;
690 struct ref **list = &list_head;
691 int alloc_heads = 0, nr_heads = 0;
694 if (!prefixcmp(buf->buf, "fetch ")) {
695 char *p = buf->buf + strlen("fetch ");
698 unsigned char old_sha1[20];
700 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
701 die("protocol error: expected sha/ref, got %s'", p);
707 die("protocol error: expected sha/ref, got %s'", p);
709 ref = alloc_ref(name);
710 hashcpy(ref->old_sha1, old_sha1);
715 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
716 to_fetch[nr_heads++] = ref;
719 die("http transport does not support %s", buf->buf);
722 if (strbuf_getline(buf, stdin, '\n') == EOF)
728 if (fetch(nr_heads, to_fetch))
729 exit(128); /* error already reported */
730 free_refs(list_head);
738 static int push_dav(int nr_spec, char **specs)
740 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
743 argv[argc++] = "http-push";
744 argv[argc++] = "--helper-status";
746 argv[argc++] = "--dry-run";
747 if (options.verbosity > 1)
748 argv[argc++] = "--verbose";
750 for (i = 0; i < nr_spec; i++)
751 argv[argc++] = specs[i];
754 if (run_command_v_opt(argv, RUN_GIT_CMD))
755 die("git-%s failed", argv[0]);
760 static int push_git(struct discovery *heads, int nr_spec, char **specs)
762 struct rpc_state rpc;
764 int argc = 0, i, err;
766 argv = xmalloc((10 + nr_spec) * sizeof(char*));
767 argv[argc++] = "send-pack";
768 argv[argc++] = "--stateless-rpc";
769 argv[argc++] = "--helper-status";
771 argv[argc++] = "--thin";
773 argv[argc++] = "--dry-run";
774 if (options.verbosity == 0)
775 argv[argc++] = "--quiet";
776 else if (options.verbosity > 1)
777 argv[argc++] = "--verbose";
778 argv[argc++] = options.progress ? "--progress" : "--no-progress";
780 for (i = 0; i < nr_spec; i++)
781 argv[argc++] = specs[i];
784 memset(&rpc, 0, sizeof(rpc));
785 rpc.service_name = "git-receive-pack",
788 err = rpc_service(&rpc, heads);
790 safe_write(1, rpc.result.buf, rpc.result.len);
791 strbuf_release(&rpc.result);
796 static int push(int nr_spec, char **specs)
798 struct discovery *heads = discover_refs("git-receive-pack");
801 if (heads->proto_git)
802 ret = push_git(heads, nr_spec, specs);
804 ret = push_dav(nr_spec, specs);
805 free_discovery(heads);
809 static void parse_push(struct strbuf *buf)
812 int alloc_spec = 0, nr_spec = 0, i, ret;
815 if (!prefixcmp(buf->buf, "push ")) {
816 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
817 specs[nr_spec++] = xstrdup(buf->buf + 5);
820 die("http transport does not support %s", buf->buf);
823 if (strbuf_getline(buf, stdin, '\n') == EOF)
829 ret = push(nr_spec, specs);
834 exit(128); /* error already reported */
837 for (i = 0; i < nr_spec; i++)
842 int main(int argc, const char **argv)
844 struct strbuf buf = STRBUF_INIT;
847 git_extract_argv0_path(argv[0]);
848 setup_git_directory_gently(&nongit);
850 fprintf(stderr, "Remote needed\n");
854 options.verbosity = 1;
855 options.progress = !!isatty(2);
858 remote = remote_get(argv[1]);
861 end_url_with_slash(&buf, argv[2]);
863 end_url_with_slash(&buf, remote->url[0]);
866 url = strbuf_detach(&buf, NULL);
868 http_init(remote, url, 0);
871 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
873 fprintf(stderr, "Error reading command stream\n");
875 fprintf(stderr, "Unexpected end of command stream\n");
880 if (!prefixcmp(buf.buf, "fetch ")) {
882 die("Fetch attempted without a local repo");
885 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
886 int for_push = !!strstr(buf.buf + 4, "for-push");
887 output_refs(get_refs(for_push));
889 } else if (!prefixcmp(buf.buf, "push ")) {
892 } else if (!prefixcmp(buf.buf, "option ")) {
893 char *name = buf.buf + strlen("option ");
894 char *value = strchr(name, ' ');
902 result = set_option(name, value);
906 printf("error invalid value\n");
908 printf("unsupported\n");
911 } else if (!strcmp(buf.buf, "capabilities")) {
918 fprintf(stderr, "Unknown command '%s'\n", buf.buf);