7 #include "run-command.h"
11 static struct remote *remote;
12 static const char *url;
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, proto_git_candidate = 1;
100 if (last && !strcmp(service, last->service))
102 free_discovery(last);
104 strbuf_addf(&buffer, "%s/info/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);
117 /* try again with "plain" url (no ? or & appended) */
118 if (http_ret != HTTP_OK) {
120 strbuf_reset(&buffer);
122 proto_git_candidate = 0;
123 strbuf_addf(&buffer, "%s/info/refs", url);
124 refs_url = strbuf_detach(&buffer, NULL);
126 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
132 case HTTP_MISSING_TARGET:
133 die("%s not found: did you run git update-server-info on the"
134 " server?", refs_url);
136 http_error(refs_url, http_ret);
137 die("HTTP request failed");
140 last= xcalloc(1, sizeof(*last_discovery));
141 last->service = service;
142 last->buf_alloc = strbuf_detach(&buffer, &last->len);
143 last->buf = last->buf_alloc;
145 if (is_http && proto_git_candidate
146 && 5 <= last->len && last->buf[4] == '#') {
147 /* smart HTTP response; validate that the service
148 * pkt-line matches our request.
150 struct strbuf exp = STRBUF_INIT;
152 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
153 die("%s has invalid packet header", refs_url);
154 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
155 strbuf_setlen(&buffer, buffer.len - 1);
157 strbuf_addf(&exp, "# service=%s", service);
158 if (strbuf_cmp(&exp, &buffer))
159 die("invalid server response; got '%s'", buffer.buf);
160 strbuf_release(&exp);
162 /* The header can include additional metadata lines, up
163 * until a packet flush marker. Ignore these now, but
164 * in the future we might start to scan them.
166 strbuf_reset(&buffer);
167 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
168 strbuf_reset(&buffer);
174 strbuf_release(&buffer);
175 last_discovery = last;
179 static int write_discovery(int in, int out, void *data)
181 struct discovery *heads = data;
183 if (write_in_full(out, heads->buf, heads->len) != heads->len)
189 static struct ref *parse_git_refs(struct discovery *heads)
191 struct ref *list = NULL;
194 memset(&async, 0, sizeof(async));
195 async.proc = write_discovery;
199 if (start_async(&async))
200 die("cannot start thread to parse advertised refs");
201 get_remote_heads(async.out, &list, 0, NULL, 0, NULL);
203 if (finish_async(&async))
204 die("ref parsing thread failed");
208 static struct ref *parse_info_refs(struct discovery *heads)
210 char *data, *start, *mid;
214 struct ref *refs = NULL;
215 struct ref *ref = NULL;
216 struct ref *last_ref = NULL;
221 while (i < heads->len) {
227 if (data[i] == '\n') {
230 ref = xmalloc(sizeof(struct ref) +
231 strlen(ref_name) + 1);
232 memset(ref, 0, sizeof(struct ref));
233 strcpy(ref->name, ref_name);
234 get_sha1_hex(start, ref->old_sha1);
238 last_ref->next = ref;
245 ref = alloc_ref("HEAD");
246 if (!http_fetch_ref(url, ref) &&
247 !resolve_remote_symref(ref, refs)) {
257 static struct ref *get_refs(int for_push)
259 struct discovery *heads;
262 heads = discover_refs("git-receive-pack");
264 heads = discover_refs("git-upload-pack");
266 if (heads->proto_git)
267 return parse_git_refs(heads);
268 return parse_info_refs(heads);
271 static void output_refs(struct ref *refs)
274 for (posn = refs; posn; posn = posn->next) {
276 printf("@%s %s\n", posn->symref, posn->name);
278 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
286 const char *service_name;
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(const void *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 post_rpc(struct rpc_state *rpc)
359 struct active_request_slot *slot;
360 struct slot_results results;
361 struct curl_slist *headers = NULL;
362 int use_gzip = rpc->gzip_request;
363 char *gzip_body = NULL;
364 int err = 0, large_request = 0;
366 /* Try to load the entire request, if we can fit it into the
367 * allocated buffer space we can use HTTP/1.0 and avoid the
368 * chunked encoding mess.
371 size_t left = rpc->alloc - rpc->len;
372 char *buf = rpc->buf + rpc->len;
375 if (left < LARGE_PACKET_MAX) {
381 n = packet_read_line(rpc->out, buf, left);
387 slot = get_active_slot();
388 slot->results = &results;
390 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
391 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
392 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
393 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
395 headers = curl_slist_append(headers, rpc->hdr_content_type);
396 headers = curl_slist_append(headers, rpc->hdr_accept);
399 /* The request body is large and the size cannot be predicted.
400 * We must use chunked encoding to send it.
402 headers = curl_slist_append(headers, "Expect: 100-continue");
403 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
404 rpc->initial_buffer = 1;
405 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
406 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
407 #ifndef NO_CURL_IOCTL
408 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
409 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
411 if (options.verbosity > 1) {
412 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
416 } else if (use_gzip && 1024 < rpc->len) {
417 /* The client backend isn't giving us compressed data so
418 * we can try to deflate it ourselves, this may save on.
425 memset(&stream, 0, sizeof(stream));
426 ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
427 Z_DEFLATED, (15 + 16),
428 8, Z_DEFAULT_STRATEGY);
430 die("cannot deflate request; zlib init error %d", ret);
431 size = deflateBound(&stream, rpc->len);
432 gzip_body = xmalloc(size);
434 stream.next_in = (unsigned char *)rpc->buf;
435 stream.avail_in = rpc->len;
436 stream.next_out = (unsigned char *)gzip_body;
437 stream.avail_out = size;
439 ret = deflate(&stream, Z_FINISH);
440 if (ret != Z_STREAM_END)
441 die("cannot deflate request; zlib deflate error %d", ret);
443 ret = deflateEnd(&stream);
445 die("cannot deflate request; zlib end error %d", ret);
447 size = stream.total_out;
449 headers = curl_slist_append(headers, "Content-Encoding: gzip");
450 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
451 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
453 if (options.verbosity > 1) {
454 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
456 (unsigned long)rpc->len, (unsigned long)size);
460 /* We know the complete request size in advance, use the
461 * more normal Content-Length approach.
463 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
464 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
465 if (options.verbosity > 1) {
466 fprintf(stderr, "POST %s (%lu bytes)\n",
467 rpc->service_name, (unsigned long)rpc->len);
472 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
473 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
474 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
476 slot->curl_result = curl_easy_perform(slot->curl);
477 finish_active_slot(slot);
479 if (results.curl_result != CURLE_OK) {
480 err |= error("RPC failed; result=%d, HTTP code = %ld",
481 results.curl_result, results.http_code);
484 curl_slist_free_all(headers);
489 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
491 const char *svc = rpc->service_name;
492 struct strbuf buf = STRBUF_INIT;
493 struct child_process client;
496 memset(&client, 0, sizeof(client));
500 client.argv = rpc->argv;
501 if (start_command(&client))
504 write_or_die(client.in, heads->buf, heads->len);
506 rpc->alloc = http_post_buffer;
507 rpc->buf = xmalloc(rpc->alloc);
509 rpc->out = client.out;
510 strbuf_init(&rpc->result, 0);
512 strbuf_addf(&buf, "%s/%s", url, svc);
513 rpc->service_url = strbuf_detach(&buf, NULL);
515 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
516 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
518 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
519 rpc->hdr_accept = strbuf_detach(&buf, NULL);
522 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
527 err |= post_rpc(rpc);
529 strbuf_read(&rpc->result, client.out, 0);
536 err |= finish_command(&client);
537 free(rpc->service_url);
538 free(rpc->hdr_content_type);
539 free(rpc->hdr_accept);
541 strbuf_release(&buf);
545 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
547 struct walker *walker;
548 char **targets = xmalloc(nr_heads * sizeof(char*));
552 die("dumb http transport does not support --depth");
553 for (i = 0; i < nr_heads; i++)
554 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
556 walker = get_http_walker(url);
558 walker->get_tree = 1;
559 walker->get_history = 1;
560 walker->get_verbosely = options.verbosity >= 3;
561 walker->get_recover = 0;
562 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
565 for (i = 0; i < nr_heads; i++)
569 return ret ? error("Fetch failed.") : 0;
572 static int fetch_git(struct discovery *heads,
573 int nr_heads, struct ref **to_fetch)
575 struct rpc_state rpc;
576 char *depth_arg = NULL;
578 int argc = 0, i, err;
580 argv = xmalloc((15 + nr_heads) * sizeof(char*));
581 argv[argc++] = "fetch-pack";
582 argv[argc++] = "--stateless-rpc";
583 argv[argc++] = "--lock-pack";
584 if (options.followtags)
585 argv[argc++] = "--include-tag";
587 argv[argc++] = "--thin";
588 if (options.verbosity >= 3) {
592 if (!options.progress)
593 argv[argc++] = "--no-progress";
595 struct strbuf buf = STRBUF_INIT;
596 strbuf_addf(&buf, "--depth=%lu", options.depth);
597 depth_arg = strbuf_detach(&buf, NULL);
598 argv[argc++] = depth_arg;
601 for (i = 0; i < nr_heads; i++) {
602 struct ref *ref = to_fetch[i];
603 if (!ref->name || !*ref->name)
604 die("cannot fetch by sha1 over smart http");
605 argv[argc++] = ref->name;
609 memset(&rpc, 0, sizeof(rpc));
610 rpc.service_name = "git-upload-pack",
612 rpc.gzip_request = 1;
614 err = rpc_service(&rpc, heads);
616 safe_write(1, rpc.result.buf, rpc.result.len);
617 strbuf_release(&rpc.result);
623 static int fetch(int nr_heads, struct ref **to_fetch)
625 struct discovery *d = discover_refs("git-upload-pack");
627 return fetch_git(d, nr_heads, to_fetch);
629 return fetch_dumb(nr_heads, to_fetch);
632 static void parse_fetch(struct strbuf *buf)
634 struct ref **to_fetch = NULL;
635 struct ref *list_head = NULL;
636 struct ref **list = &list_head;
637 int alloc_heads = 0, nr_heads = 0;
640 if (!prefixcmp(buf->buf, "fetch ")) {
641 char *p = buf->buf + strlen("fetch ");
644 unsigned char old_sha1[20];
646 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
647 die("protocol error: expected sha/ref, got %s'", p);
653 die("protocol error: expected sha/ref, got %s'", p);
655 ref = alloc_ref(name);
656 hashcpy(ref->old_sha1, old_sha1);
661 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
662 to_fetch[nr_heads++] = ref;
665 die("http transport does not support %s", buf->buf);
668 if (strbuf_getline(buf, stdin, '\n') == EOF)
674 if (fetch(nr_heads, to_fetch))
675 exit(128); /* error already reported */
676 free_refs(list_head);
684 static int push_dav(int nr_spec, char **specs)
686 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
689 argv[argc++] = "http-push";
690 argv[argc++] = "--helper-status";
692 argv[argc++] = "--dry-run";
693 if (options.verbosity > 1)
694 argv[argc++] = "--verbose";
696 for (i = 0; i < nr_spec; i++)
697 argv[argc++] = specs[i];
700 if (run_command_v_opt(argv, RUN_GIT_CMD))
701 die("git-%s failed", argv[0]);
706 static int push_git(struct discovery *heads, int nr_spec, char **specs)
708 struct rpc_state rpc;
710 int argc = 0, i, err;
712 argv = xmalloc((10 + nr_spec) * sizeof(char*));
713 argv[argc++] = "send-pack";
714 argv[argc++] = "--stateless-rpc";
715 argv[argc++] = "--helper-status";
717 argv[argc++] = "--thin";
719 argv[argc++] = "--dry-run";
720 if (options.verbosity > 1)
721 argv[argc++] = "--verbose";
723 for (i = 0; i < nr_spec; i++)
724 argv[argc++] = specs[i];
727 memset(&rpc, 0, sizeof(rpc));
728 rpc.service_name = "git-receive-pack",
731 err = rpc_service(&rpc, heads);
733 safe_write(1, rpc.result.buf, rpc.result.len);
734 strbuf_release(&rpc.result);
739 static int push(int nr_spec, char **specs)
741 struct discovery *heads = discover_refs("git-receive-pack");
744 if (heads->proto_git)
745 ret = push_git(heads, nr_spec, specs);
747 ret = push_dav(nr_spec, specs);
748 free_discovery(heads);
752 static void parse_push(struct strbuf *buf)
755 int alloc_spec = 0, nr_spec = 0, i;
758 if (!prefixcmp(buf->buf, "push ")) {
759 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
760 specs[nr_spec++] = xstrdup(buf->buf + 5);
763 die("http transport does not support %s", buf->buf);
766 if (strbuf_getline(buf, stdin, '\n') == EOF)
772 if (push(nr_spec, specs))
773 exit(128); /* error already reported */
774 for (i = 0; i < nr_spec; i++)
782 int main(int argc, const char **argv)
784 struct strbuf buf = STRBUF_INIT;
787 git_extract_argv0_path(argv[0]);
788 setup_git_directory_gently(&nongit);
790 fprintf(stderr, "Remote needed\n");
794 options.verbosity = 1;
795 options.progress = !!isatty(2);
798 remote = remote_get(argv[1]);
803 url = remote->url[0];
809 if (strbuf_getline(&buf, stdin, '\n') == EOF)
811 if (!prefixcmp(buf.buf, "fetch ")) {
813 die("Fetch attempted without a local repo");
816 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
817 int for_push = !!strstr(buf.buf + 4, "for-push");
818 output_refs(get_refs(for_push));
820 } else if (!prefixcmp(buf.buf, "push ")) {
823 } else if (!prefixcmp(buf.buf, "option ")) {
824 char *name = buf.buf + strlen("option ");
825 char *value = strchr(name, ' ');
833 result = set_option(name, value);
837 printf("error invalid value\n");
839 printf("unsupported\n");
842 } else if (!strcmp(buf.buf, "capabilities")) {