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 struct discovery* discover_refs(const char *service, int for_push)
156 struct strbuf exp = STRBUF_INIT;
157 struct strbuf type = STRBUF_INIT;
158 struct strbuf buffer = STRBUF_INIT;
159 struct discovery *last = last_discovery;
161 int http_ret, maybe_smart = 0;
163 if (last && !strcmp(service, last->service))
165 free_discovery(last);
167 strbuf_addf(&buffer, "%sinfo/refs", url);
168 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
169 git_env_bool("GIT_SMART_HTTP", 1)) {
171 if (!strchr(url, '?'))
172 strbuf_addch(&buffer, '?');
174 strbuf_addch(&buffer, '&');
175 strbuf_addf(&buffer, "service=%s", service);
177 refs_url = strbuf_detach(&buffer, NULL);
179 http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
183 case HTTP_MISSING_TARGET:
184 die("%s not found: did you run git update-server-info on the"
185 " server?", refs_url);
187 die("Authentication failed");
189 http_error(refs_url, http_ret);
190 die("HTTP request failed");
193 last= xcalloc(1, sizeof(*last_discovery));
194 last->service = service;
195 last->buf_alloc = strbuf_detach(&buffer, &last->len);
196 last->buf = last->buf_alloc;
198 strbuf_addf(&exp, "application/x-%s-advertisement", service);
200 (5 <= last->len && last->buf[4] == '#') &&
201 !strbuf_cmp(&exp, &type)) {
205 * smart HTTP response; validate that the service
206 * pkt-line matches our request.
208 line = packet_read_line_buf(&last->buf, &last->len, NULL);
211 strbuf_addf(&exp, "# service=%s", service);
212 if (strcmp(line, exp.buf))
213 die("invalid server response; got '%s'", line);
214 strbuf_release(&exp);
216 /* The header can include additional metadata lines, up
217 * until a packet flush marker. Ignore these now, but
218 * in the future we might start to scan them.
220 while (packet_read_line_buf(&last->buf, &last->len, NULL))
227 last->refs = parse_git_refs(last, for_push);
229 last->refs = parse_info_refs(last);
232 strbuf_release(&exp);
233 strbuf_release(&type);
234 strbuf_release(&buffer);
235 last_discovery = last;
239 static struct ref *get_refs(int for_push)
241 struct discovery *heads;
244 heads = discover_refs("git-receive-pack", for_push);
246 heads = discover_refs("git-upload-pack", for_push);
251 static void output_refs(struct ref *refs)
254 for (posn = refs; posn; posn = posn->next) {
256 printf("@%s %s\n", posn->symref, posn->name);
258 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
265 const char *service_name;
267 struct strbuf *stdin_preamble;
269 char *hdr_content_type;
277 struct strbuf result;
278 unsigned gzip_request : 1;
279 unsigned initial_buffer : 1;
282 static size_t rpc_out(void *ptr, size_t eltsize,
283 size_t nmemb, void *buffer_)
285 size_t max = eltsize * nmemb;
286 struct rpc_state *rpc = buffer_;
287 size_t avail = rpc->len - rpc->pos;
290 rpc->initial_buffer = 0;
291 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
300 memcpy(ptr, rpc->buf + rpc->pos, avail);
305 #ifndef NO_CURL_IOCTL
306 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
308 struct rpc_state *rpc = clientp;
314 case CURLIOCMD_RESTARTREAD:
315 if (rpc->initial_buffer) {
319 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
320 return CURLIOE_FAILRESTART;
323 return CURLIOE_UNKNOWNCMD;
328 static size_t rpc_in(char *ptr, size_t eltsize,
329 size_t nmemb, void *buffer_)
331 size_t size = eltsize * nmemb;
332 struct rpc_state *rpc = buffer_;
333 write_or_die(rpc->in, ptr, size);
337 static int run_slot(struct active_request_slot *slot)
340 struct slot_results results;
342 slot->results = &results;
343 slot->curl_result = curl_easy_perform(slot->curl);
344 finish_active_slot(slot);
346 err = handle_curl_result(&results);
347 if (err != HTTP_OK && err != HTTP_REAUTH) {
348 error("RPC failed; result=%d, HTTP code = %ld",
349 results.curl_result, results.http_code);
355 static int probe_rpc(struct rpc_state *rpc)
357 struct active_request_slot *slot;
358 struct curl_slist *headers = NULL;
359 struct strbuf buf = STRBUF_INIT;
362 slot = get_active_slot();
364 headers = curl_slist_append(headers, rpc->hdr_content_type);
365 headers = curl_slist_append(headers, rpc->hdr_accept);
367 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
368 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
369 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
370 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
371 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
372 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
374 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
375 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
377 err = run_slot(slot);
379 curl_slist_free_all(headers);
380 strbuf_release(&buf);
384 static int post_rpc(struct rpc_state *rpc)
386 struct active_request_slot *slot;
387 struct curl_slist *headers = NULL;
388 int use_gzip = rpc->gzip_request;
389 char *gzip_body = NULL;
390 size_t gzip_size = 0;
391 int err, large_request = 0;
393 /* Try to load the entire request, if we can fit it into the
394 * allocated buffer space we can use HTTP/1.0 and avoid the
395 * chunked encoding mess.
398 size_t left = rpc->alloc - rpc->len;
399 char *buf = rpc->buf + rpc->len;
402 if (left < LARGE_PACKET_MAX) {
408 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
416 err = probe_rpc(rpc);
417 } while (err == HTTP_REAUTH);
422 headers = curl_slist_append(headers, rpc->hdr_content_type);
423 headers = curl_slist_append(headers, rpc->hdr_accept);
424 headers = curl_slist_append(headers, "Expect:");
427 slot = get_active_slot();
429 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
430 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
431 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
432 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
435 /* The request body is large and the size cannot be predicted.
436 * We must use chunked encoding to send it.
438 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
439 rpc->initial_buffer = 1;
440 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
441 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
442 #ifndef NO_CURL_IOCTL
443 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
444 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
446 if (options.verbosity > 1) {
447 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
451 } else if (gzip_body) {
453 * If we are looping to retry authentication, then the previous
454 * run will have set up the headers and gzip buffer already,
455 * and we just need to send it.
457 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
458 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
460 } else if (use_gzip && 1024 < rpc->len) {
461 /* The client backend isn't giving us compressed data so
462 * we can try to deflate it ourselves, this may save on.
468 memset(&stream, 0, sizeof(stream));
469 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
470 gzip_size = git_deflate_bound(&stream, rpc->len);
471 gzip_body = xmalloc(gzip_size);
473 stream.next_in = (unsigned char *)rpc->buf;
474 stream.avail_in = rpc->len;
475 stream.next_out = (unsigned char *)gzip_body;
476 stream.avail_out = gzip_size;
478 ret = git_deflate(&stream, Z_FINISH);
479 if (ret != Z_STREAM_END)
480 die("cannot deflate request; zlib deflate error %d", ret);
482 ret = git_deflate_end_gently(&stream);
484 die("cannot deflate request; zlib end error %d", ret);
486 gzip_size = stream.total_out;
488 headers = curl_slist_append(headers, "Content-Encoding: gzip");
489 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
490 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
492 if (options.verbosity > 1) {
493 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
495 (unsigned long)rpc->len, (unsigned long)gzip_size);
499 /* We know the complete request size in advance, use the
500 * more normal Content-Length approach.
502 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
503 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
504 if (options.verbosity > 1) {
505 fprintf(stderr, "POST %s (%lu bytes)\n",
506 rpc->service_name, (unsigned long)rpc->len);
511 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
512 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
513 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
515 err = run_slot(slot);
516 if (err == HTTP_REAUTH && !large_request)
521 curl_slist_free_all(headers);
526 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
528 const char *svc = rpc->service_name;
529 struct strbuf buf = STRBUF_INIT;
530 struct strbuf *preamble = rpc->stdin_preamble;
531 struct child_process client;
534 memset(&client, 0, sizeof(client));
538 client.argv = rpc->argv;
539 if (start_command(&client))
542 write_or_die(client.in, preamble->buf, preamble->len);
544 write_or_die(client.in, heads->buf, heads->len);
546 rpc->alloc = http_post_buffer;
547 rpc->buf = xmalloc(rpc->alloc);
549 rpc->out = client.out;
550 strbuf_init(&rpc->result, 0);
552 strbuf_addf(&buf, "%s%s", url, svc);
553 rpc->service_url = strbuf_detach(&buf, NULL);
555 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
556 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
558 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
559 rpc->hdr_accept = strbuf_detach(&buf, NULL);
562 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
567 err |= post_rpc(rpc);
573 strbuf_read(&rpc->result, client.out, 0);
577 if (xread(client.out, buf, sizeof(buf)) <= 0)
584 err |= finish_command(&client);
585 free(rpc->service_url);
586 free(rpc->hdr_content_type);
587 free(rpc->hdr_accept);
589 strbuf_release(&buf);
593 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
595 struct walker *walker;
596 char **targets = xmalloc(nr_heads * sizeof(char*));
600 die("dumb http transport does not support --depth");
601 for (i = 0; i < nr_heads; i++)
602 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
604 walker = get_http_walker(url);
606 walker->get_tree = 1;
607 walker->get_history = 1;
608 walker->get_verbosely = options.verbosity >= 3;
609 walker->get_recover = 0;
610 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
613 for (i = 0; i < nr_heads; i++)
617 return ret ? error("Fetch failed.") : 0;
620 static int fetch_git(struct discovery *heads,
621 int nr_heads, struct ref **to_fetch)
623 struct rpc_state rpc;
624 struct strbuf preamble = STRBUF_INIT;
625 char *depth_arg = NULL;
626 int argc = 0, i, err;
627 const char *argv[15];
629 argv[argc++] = "fetch-pack";
630 argv[argc++] = "--stateless-rpc";
631 argv[argc++] = "--stdin";
632 argv[argc++] = "--lock-pack";
633 if (options.followtags)
634 argv[argc++] = "--include-tag";
636 argv[argc++] = "--thin";
637 if (options.verbosity >= 3) {
641 if (!options.progress)
642 argv[argc++] = "--no-progress";
644 struct strbuf buf = STRBUF_INIT;
645 strbuf_addf(&buf, "--depth=%lu", options.depth);
646 depth_arg = strbuf_detach(&buf, NULL);
647 argv[argc++] = depth_arg;
652 for (i = 0; i < nr_heads; i++) {
653 struct ref *ref = to_fetch[i];
654 if (!ref->name || !*ref->name)
655 die("cannot fetch by sha1 over smart http");
656 packet_buf_write(&preamble, "%s\n", ref->name);
658 packet_buf_flush(&preamble);
660 memset(&rpc, 0, sizeof(rpc));
661 rpc.service_name = "git-upload-pack",
663 rpc.stdin_preamble = &preamble;
664 rpc.gzip_request = 1;
666 err = rpc_service(&rpc, heads);
668 write_or_die(1, rpc.result.buf, rpc.result.len);
669 strbuf_release(&rpc.result);
670 strbuf_release(&preamble);
675 static int fetch(int nr_heads, struct ref **to_fetch)
677 struct discovery *d = discover_refs("git-upload-pack", 0);
679 return fetch_git(d, nr_heads, to_fetch);
681 return fetch_dumb(nr_heads, to_fetch);
684 static void parse_fetch(struct strbuf *buf)
686 struct ref **to_fetch = NULL;
687 struct ref *list_head = NULL;
688 struct ref **list = &list_head;
689 int alloc_heads = 0, nr_heads = 0;
692 if (!prefixcmp(buf->buf, "fetch ")) {
693 char *p = buf->buf + strlen("fetch ");
696 unsigned char old_sha1[20];
698 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
699 die("protocol error: expected sha/ref, got %s'", p);
705 die("protocol error: expected sha/ref, got %s'", p);
707 ref = alloc_ref(name);
708 hashcpy(ref->old_sha1, old_sha1);
713 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
714 to_fetch[nr_heads++] = ref;
717 die("http transport does not support %s", buf->buf);
720 if (strbuf_getline(buf, stdin, '\n') == EOF)
726 if (fetch(nr_heads, to_fetch))
727 exit(128); /* error already reported */
728 free_refs(list_head);
736 static int push_dav(int nr_spec, char **specs)
738 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
741 argv[argc++] = "http-push";
742 argv[argc++] = "--helper-status";
744 argv[argc++] = "--dry-run";
745 if (options.verbosity > 1)
746 argv[argc++] = "--verbose";
748 for (i = 0; i < nr_spec; i++)
749 argv[argc++] = specs[i];
752 if (run_command_v_opt(argv, RUN_GIT_CMD))
753 die("git-%s failed", argv[0]);
758 static int push_git(struct discovery *heads, int nr_spec, char **specs)
760 struct rpc_state rpc;
762 int argc = 0, i, err;
764 argv = xmalloc((10 + nr_spec) * sizeof(char*));
765 argv[argc++] = "send-pack";
766 argv[argc++] = "--stateless-rpc";
767 argv[argc++] = "--helper-status";
769 argv[argc++] = "--thin";
771 argv[argc++] = "--dry-run";
772 if (options.verbosity == 0)
773 argv[argc++] = "--quiet";
774 else if (options.verbosity > 1)
775 argv[argc++] = "--verbose";
776 argv[argc++] = options.progress ? "--progress" : "--no-progress";
778 for (i = 0; i < nr_spec; i++)
779 argv[argc++] = specs[i];
782 memset(&rpc, 0, sizeof(rpc));
783 rpc.service_name = "git-receive-pack",
786 err = rpc_service(&rpc, heads);
788 write_or_die(1, rpc.result.buf, rpc.result.len);
789 strbuf_release(&rpc.result);
794 static int push(int nr_spec, char **specs)
796 struct discovery *heads = discover_refs("git-receive-pack", 1);
799 if (heads->proto_git)
800 ret = push_git(heads, nr_spec, specs);
802 ret = push_dav(nr_spec, specs);
803 free_discovery(heads);
807 static void parse_push(struct strbuf *buf)
810 int alloc_spec = 0, nr_spec = 0, i, ret;
813 if (!prefixcmp(buf->buf, "push ")) {
814 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
815 specs[nr_spec++] = xstrdup(buf->buf + 5);
818 die("http transport does not support %s", buf->buf);
821 if (strbuf_getline(buf, stdin, '\n') == EOF)
827 ret = push(nr_spec, specs);
832 exit(128); /* error already reported */
835 for (i = 0; i < nr_spec; i++)
840 int main(int argc, const char **argv)
842 struct strbuf buf = STRBUF_INIT;
845 git_extract_argv0_path(argv[0]);
846 setup_git_directory_gently(&nongit);
848 fprintf(stderr, "Remote needed\n");
852 options.verbosity = 1;
853 options.progress = !!isatty(2);
856 remote = remote_get(argv[1]);
859 end_url_with_slash(&buf, argv[2]);
861 end_url_with_slash(&buf, remote->url[0]);
864 url = strbuf_detach(&buf, NULL);
866 http_init(remote, url, 0);
869 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
871 fprintf(stderr, "Error reading command stream\n");
873 fprintf(stderr, "Unexpected end of command stream\n");
878 if (!prefixcmp(buf.buf, "fetch ")) {
880 die("Fetch attempted without a local repo");
883 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
884 int for_push = !!strstr(buf.buf + 4, "for-push");
885 output_refs(get_refs(for_push));
887 } else if (!prefixcmp(buf.buf, "push ")) {
890 } else if (!prefixcmp(buf.buf, "option ")) {
891 char *name = buf.buf + strlen("option ");
892 char *value = strchr(name, ' ');
900 result = set_option(name, value);
904 printf("error invalid value\n");
906 printf("unsupported\n");
909 } else if (!strcmp(buf.buf, "capabilities")) {
916 fprintf(stderr, "Unknown command '%s'\n", buf.buf);