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, maybe_smart = 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://")) &&
106 git_env_bool("GIT_SMART_HTTP", 1)) {
108 if (!strchr(url, '?'))
109 strbuf_addch(&buffer, '?');
111 strbuf_addch(&buffer, '&');
112 strbuf_addf(&buffer, "service=%s", service);
114 refs_url = strbuf_detach(&buffer, NULL);
116 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
120 case HTTP_MISSING_TARGET:
121 die("%s not found: did you run git update-server-info on the"
122 " server?", refs_url);
124 die("Authentication failed");
126 http_error(refs_url, http_ret);
127 die("HTTP request failed");
130 last= xcalloc(1, sizeof(*last_discovery));
131 last->service = service;
132 last->buf_alloc = strbuf_detach(&buffer, &last->len);
133 last->buf = last->buf_alloc;
135 if (maybe_smart && 5 <= last->len && last->buf[4] == '#') {
136 /* smart HTTP response; validate that the service
137 * pkt-line matches our request.
139 struct strbuf exp = STRBUF_INIT;
141 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
142 die("%s has invalid packet header", refs_url);
143 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
144 strbuf_setlen(&buffer, buffer.len - 1);
146 strbuf_addf(&exp, "# service=%s", service);
147 if (strbuf_cmp(&exp, &buffer))
148 die("invalid server response; got '%s'", buffer.buf);
149 strbuf_release(&exp);
151 /* The header can include additional metadata lines, up
152 * until a packet flush marker. Ignore these now, but
153 * in the future we might start to scan them.
155 strbuf_reset(&buffer);
156 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
157 strbuf_reset(&buffer);
163 strbuf_release(&buffer);
164 last_discovery = last;
168 static int write_discovery(int in, int out, void *data)
170 struct discovery *heads = data;
172 if (write_in_full(out, heads->buf, heads->len) != heads->len)
178 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
180 struct ref *list = NULL;
183 memset(&async, 0, sizeof(async));
184 async.proc = write_discovery;
188 if (start_async(&async))
189 die("cannot start thread to parse advertised refs");
190 get_remote_heads(async.out, &list,
191 for_push ? REF_NORMAL : 0, NULL);
193 if (finish_async(&async))
194 die("ref parsing thread failed");
198 static struct ref *parse_info_refs(struct discovery *heads)
200 char *data, *start, *mid;
204 struct ref *refs = NULL;
205 struct ref *ref = NULL;
206 struct ref *last_ref = NULL;
211 while (i < heads->len) {
217 if (data[i] == '\n') {
218 if (mid - start != 40)
219 die("%sinfo/refs not valid: is this a git repository?", url);
222 ref = xmalloc(sizeof(struct ref) +
223 strlen(ref_name) + 1);
224 memset(ref, 0, sizeof(struct ref));
225 strcpy(ref->name, ref_name);
226 get_sha1_hex(start, ref->old_sha1);
230 last_ref->next = ref;
237 ref = alloc_ref("HEAD");
238 if (!http_fetch_ref(url, ref) &&
239 !resolve_remote_symref(ref, refs)) {
249 static struct ref *get_refs(int for_push)
251 struct discovery *heads;
254 heads = discover_refs("git-receive-pack");
256 heads = discover_refs("git-upload-pack");
258 if (heads->proto_git)
259 return parse_git_refs(heads, for_push);
260 return parse_info_refs(heads);
263 static void output_refs(struct ref *refs)
266 for (posn = refs; posn; posn = posn->next) {
268 printf("@%s %s\n", posn->symref, posn->name);
270 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
278 const char *service_name;
280 struct strbuf *stdin_preamble;
282 char *hdr_content_type;
290 struct strbuf result;
291 unsigned gzip_request : 1;
292 unsigned initial_buffer : 1;
295 static size_t rpc_out(void *ptr, size_t eltsize,
296 size_t nmemb, void *buffer_)
298 size_t max = eltsize * nmemb;
299 struct rpc_state *rpc = buffer_;
300 size_t avail = rpc->len - rpc->pos;
303 rpc->initial_buffer = 0;
304 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
313 memcpy(ptr, rpc->buf + rpc->pos, avail);
318 #ifndef NO_CURL_IOCTL
319 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
321 struct rpc_state *rpc = clientp;
327 case CURLIOCMD_RESTARTREAD:
328 if (rpc->initial_buffer) {
332 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
333 return CURLIOE_FAILRESTART;
336 return CURLIOE_UNKNOWNCMD;
341 static size_t rpc_in(char *ptr, size_t eltsize,
342 size_t nmemb, void *buffer_)
344 size_t size = eltsize * nmemb;
345 struct rpc_state *rpc = buffer_;
346 write_or_die(rpc->in, ptr, size);
350 static int run_slot(struct active_request_slot *slot)
353 struct slot_results results;
355 slot->results = &results;
356 slot->curl_result = curl_easy_perform(slot->curl);
357 finish_active_slot(slot);
359 err = handle_curl_result(&results);
360 if (err != HTTP_OK && err != HTTP_REAUTH) {
361 error("RPC failed; result=%d, HTTP code = %ld",
362 results.curl_result, results.http_code);
368 static int probe_rpc(struct rpc_state *rpc)
370 struct active_request_slot *slot;
371 struct curl_slist *headers = NULL;
372 struct strbuf buf = STRBUF_INIT;
375 slot = get_active_slot();
377 headers = curl_slist_append(headers, rpc->hdr_content_type);
378 headers = curl_slist_append(headers, rpc->hdr_accept);
380 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
381 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
382 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
383 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
384 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
385 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
386 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
387 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
388 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
390 err = run_slot(slot);
392 curl_slist_free_all(headers);
393 strbuf_release(&buf);
397 static int post_rpc(struct rpc_state *rpc)
399 struct active_request_slot *slot;
400 struct curl_slist *headers = NULL;
401 int use_gzip = rpc->gzip_request;
402 char *gzip_body = NULL;
403 size_t gzip_size = 0;
404 int err, large_request = 0;
406 /* Try to load the entire request, if we can fit it into the
407 * allocated buffer space we can use HTTP/1.0 and avoid the
408 * chunked encoding mess.
411 size_t left = rpc->alloc - rpc->len;
412 char *buf = rpc->buf + rpc->len;
415 if (left < LARGE_PACKET_MAX) {
421 n = packet_read_line(rpc->out, buf, left);
429 err = probe_rpc(rpc);
430 } while (err == HTTP_REAUTH);
435 headers = curl_slist_append(headers, rpc->hdr_content_type);
436 headers = curl_slist_append(headers, rpc->hdr_accept);
437 headers = curl_slist_append(headers, "Expect:");
440 slot = get_active_slot();
442 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
443 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
444 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
445 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
448 /* The request body is large and the size cannot be predicted.
449 * We must use chunked encoding to send it.
451 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
452 rpc->initial_buffer = 1;
453 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
454 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
455 #ifndef NO_CURL_IOCTL
456 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
457 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
459 if (options.verbosity > 1) {
460 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
464 } else if (gzip_body) {
466 * If we are looping to retry authentication, then the previous
467 * run will have set up the headers and gzip buffer already,
468 * and we just need to send it.
470 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
471 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
473 } else if (use_gzip && 1024 < rpc->len) {
474 /* The client backend isn't giving us compressed data so
475 * we can try to deflate it ourselves, this may save on.
481 memset(&stream, 0, sizeof(stream));
482 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
483 gzip_size = git_deflate_bound(&stream, rpc->len);
484 gzip_body = xmalloc(gzip_size);
486 stream.next_in = (unsigned char *)rpc->buf;
487 stream.avail_in = rpc->len;
488 stream.next_out = (unsigned char *)gzip_body;
489 stream.avail_out = gzip_size;
491 ret = git_deflate(&stream, Z_FINISH);
492 if (ret != Z_STREAM_END)
493 die("cannot deflate request; zlib deflate error %d", ret);
495 ret = git_deflate_end_gently(&stream);
497 die("cannot deflate request; zlib end error %d", ret);
499 gzip_size = stream.total_out;
501 headers = curl_slist_append(headers, "Content-Encoding: gzip");
502 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
503 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
505 if (options.verbosity > 1) {
506 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
508 (unsigned long)rpc->len, (unsigned long)gzip_size);
512 /* We know the complete request size in advance, use the
513 * more normal Content-Length approach.
515 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
516 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
517 if (options.verbosity > 1) {
518 fprintf(stderr, "POST %s (%lu bytes)\n",
519 rpc->service_name, (unsigned long)rpc->len);
524 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
525 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
526 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
528 err = run_slot(slot);
529 if (err == HTTP_REAUTH && !large_request)
534 curl_slist_free_all(headers);
539 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
541 const char *svc = rpc->service_name;
542 struct strbuf buf = STRBUF_INIT;
543 struct strbuf *preamble = rpc->stdin_preamble;
544 struct child_process client;
547 memset(&client, 0, sizeof(client));
551 client.argv = rpc->argv;
552 if (start_command(&client))
555 write_or_die(client.in, preamble->buf, preamble->len);
557 write_or_die(client.in, heads->buf, heads->len);
559 rpc->alloc = http_post_buffer;
560 rpc->buf = xmalloc(rpc->alloc);
562 rpc->out = client.out;
563 strbuf_init(&rpc->result, 0);
565 strbuf_addf(&buf, "%s%s", url, svc);
566 rpc->service_url = strbuf_detach(&buf, NULL);
568 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
569 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
571 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
572 rpc->hdr_accept = strbuf_detach(&buf, NULL);
575 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
580 err |= post_rpc(rpc);
586 strbuf_read(&rpc->result, client.out, 0);
590 if (xread(client.out, buf, sizeof(buf)) <= 0)
597 err |= finish_command(&client);
598 free(rpc->service_url);
599 free(rpc->hdr_content_type);
600 free(rpc->hdr_accept);
602 strbuf_release(&buf);
606 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
608 struct walker *walker;
609 char **targets = xmalloc(nr_heads * sizeof(char*));
613 die("dumb http transport does not support --depth");
614 for (i = 0; i < nr_heads; i++)
615 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
617 walker = get_http_walker(url);
619 walker->get_tree = 1;
620 walker->get_history = 1;
621 walker->get_verbosely = options.verbosity >= 3;
622 walker->get_recover = 0;
623 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
626 for (i = 0; i < nr_heads; i++)
630 return ret ? error("Fetch failed.") : 0;
633 static int fetch_git(struct discovery *heads,
634 int nr_heads, struct ref **to_fetch)
636 struct rpc_state rpc;
637 struct strbuf preamble = STRBUF_INIT;
638 char *depth_arg = NULL;
639 int argc = 0, i, err;
640 const char *argv[15];
642 argv[argc++] = "fetch-pack";
643 argv[argc++] = "--stateless-rpc";
644 argv[argc++] = "--stdin";
645 argv[argc++] = "--lock-pack";
646 if (options.followtags)
647 argv[argc++] = "--include-tag";
649 argv[argc++] = "--thin";
650 if (options.verbosity >= 3) {
654 if (!options.progress)
655 argv[argc++] = "--no-progress";
657 struct strbuf buf = STRBUF_INIT;
658 strbuf_addf(&buf, "--depth=%lu", options.depth);
659 depth_arg = strbuf_detach(&buf, NULL);
660 argv[argc++] = depth_arg;
665 for (i = 0; i < nr_heads; i++) {
666 struct ref *ref = to_fetch[i];
667 if (!ref->name || !*ref->name)
668 die("cannot fetch by sha1 over smart http");
669 packet_buf_write(&preamble, "%s\n", ref->name);
671 packet_buf_flush(&preamble);
673 memset(&rpc, 0, sizeof(rpc));
674 rpc.service_name = "git-upload-pack",
676 rpc.stdin_preamble = &preamble;
677 rpc.gzip_request = 1;
679 err = rpc_service(&rpc, heads);
681 safe_write(1, rpc.result.buf, rpc.result.len);
682 strbuf_release(&rpc.result);
683 strbuf_release(&preamble);
688 static int fetch(int nr_heads, struct ref **to_fetch)
690 struct discovery *d = discover_refs("git-upload-pack");
692 return fetch_git(d, nr_heads, to_fetch);
694 return fetch_dumb(nr_heads, to_fetch);
697 static void parse_fetch(struct strbuf *buf)
699 struct ref **to_fetch = NULL;
700 struct ref *list_head = NULL;
701 struct ref **list = &list_head;
702 int alloc_heads = 0, nr_heads = 0;
705 if (!prefixcmp(buf->buf, "fetch ")) {
706 char *p = buf->buf + strlen("fetch ");
709 unsigned char old_sha1[20];
711 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
712 die("protocol error: expected sha/ref, got %s'", p);
718 die("protocol error: expected sha/ref, got %s'", p);
720 ref = alloc_ref(name);
721 hashcpy(ref->old_sha1, old_sha1);
726 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
727 to_fetch[nr_heads++] = ref;
730 die("http transport does not support %s", buf->buf);
733 if (strbuf_getline(buf, stdin, '\n') == EOF)
739 if (fetch(nr_heads, to_fetch))
740 exit(128); /* error already reported */
741 free_refs(list_head);
749 static int push_dav(int nr_spec, char **specs)
751 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
754 argv[argc++] = "http-push";
755 argv[argc++] = "--helper-status";
757 argv[argc++] = "--dry-run";
758 if (options.verbosity > 1)
759 argv[argc++] = "--verbose";
761 for (i = 0; i < nr_spec; i++)
762 argv[argc++] = specs[i];
765 if (run_command_v_opt(argv, RUN_GIT_CMD))
766 die("git-%s failed", argv[0]);
771 static int push_git(struct discovery *heads, int nr_spec, char **specs)
773 struct rpc_state rpc;
775 int argc = 0, i, err;
777 argv = xmalloc((10 + nr_spec) * sizeof(char*));
778 argv[argc++] = "send-pack";
779 argv[argc++] = "--stateless-rpc";
780 argv[argc++] = "--helper-status";
782 argv[argc++] = "--thin";
784 argv[argc++] = "--dry-run";
785 if (options.verbosity == 0)
786 argv[argc++] = "--quiet";
787 else if (options.verbosity > 1)
788 argv[argc++] = "--verbose";
789 argv[argc++] = options.progress ? "--progress" : "--no-progress";
791 for (i = 0; i < nr_spec; i++)
792 argv[argc++] = specs[i];
795 memset(&rpc, 0, sizeof(rpc));
796 rpc.service_name = "git-receive-pack",
799 err = rpc_service(&rpc, heads);
801 safe_write(1, rpc.result.buf, rpc.result.len);
802 strbuf_release(&rpc.result);
807 static int push(int nr_spec, char **specs)
809 struct discovery *heads = discover_refs("git-receive-pack");
812 if (heads->proto_git)
813 ret = push_git(heads, nr_spec, specs);
815 ret = push_dav(nr_spec, specs);
816 free_discovery(heads);
820 static void parse_push(struct strbuf *buf)
823 int alloc_spec = 0, nr_spec = 0, i, ret;
826 if (!prefixcmp(buf->buf, "push ")) {
827 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
828 specs[nr_spec++] = xstrdup(buf->buf + 5);
831 die("http transport does not support %s", buf->buf);
834 if (strbuf_getline(buf, stdin, '\n') == EOF)
840 ret = push(nr_spec, specs);
845 exit(128); /* error already reported */
848 for (i = 0; i < nr_spec; i++)
853 int main(int argc, const char **argv)
855 struct strbuf buf = STRBUF_INIT;
858 git_extract_argv0_path(argv[0]);
859 setup_git_directory_gently(&nongit);
861 fprintf(stderr, "Remote needed\n");
865 options.verbosity = 1;
866 options.progress = !!isatty(2);
869 remote = remote_get(argv[1]);
872 end_url_with_slash(&buf, argv[2]);
874 end_url_with_slash(&buf, remote->url[0]);
877 url = strbuf_detach(&buf, NULL);
879 http_init(remote, url, 0);
882 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
884 fprintf(stderr, "Error reading command stream\n");
886 fprintf(stderr, "Unexpected end of command stream\n");
891 if (!prefixcmp(buf.buf, "fetch ")) {
893 die("Fetch attempted without a local repo");
896 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
897 int for_push = !!strstr(buf.buf + 4, "for-push");
898 output_refs(get_refs(for_push));
900 } else if (!prefixcmp(buf.buf, "push ")) {
903 } else if (!prefixcmp(buf.buf, "option ")) {
904 char *name = buf.buf + strlen("option ");
905 char *value = strchr(name, ' ');
913 result = set_option(name, value);
917 printf("error invalid value\n");
919 printf("unsupported\n");
922 } else if (!strcmp(buf.buf, "capabilities")) {
929 fprintf(stderr, "Unknown command '%s'\n", buf.buf);