fetch-pack: use a common function for verbose printing
[git/raj.git] / remote-curl.c
blobfd030c13c842919c1d23fe7d3c906c9623a4a9a4
1 #include "cache.h"
2 #include "remote.h"
3 #include "strbuf.h"
4 #include "walker.h"
5 #include "http.h"
6 #include "exec_cmd.h"
7 #include "run-command.h"
8 #include "pkt-line.h"
9 #include "string-list.h"
10 #include "sideband.h"
11 #include "argv-array.h"
12 #include "credential.h"
13 #include "sha1-array.h"
14 #include "send-pack.h"
16 static struct remote *remote;
17 /* always ends with a trailing slash */
18 static struct strbuf url = STRBUF_INIT;
20 struct options {
21 int verbosity;
22 unsigned long depth;
23 unsigned progress : 1,
24 check_self_contained_and_connected : 1,
25 cloning : 1,
26 update_shallow : 1,
27 followtags : 1,
28 dry_run : 1,
29 thin : 1,
30 /* One of the SEND_PACK_PUSH_CERT_* constants. */
31 push_cert : 2;
33 static struct options options;
34 static struct string_list cas_options = STRING_LIST_INIT_DUP;
36 static int set_option(const char *name, const char *value)
38 if (!strcmp(name, "verbosity")) {
39 char *end;
40 int v = strtol(value, &end, 10);
41 if (value == end || *end)
42 return -1;
43 options.verbosity = v;
44 return 0;
46 else if (!strcmp(name, "progress")) {
47 if (!strcmp(value, "true"))
48 options.progress = 1;
49 else if (!strcmp(value, "false"))
50 options.progress = 0;
51 else
52 return -1;
53 return 0;
55 else if (!strcmp(name, "depth")) {
56 char *end;
57 unsigned long v = strtoul(value, &end, 10);
58 if (value == end || *end)
59 return -1;
60 options.depth = v;
61 return 0;
63 else if (!strcmp(name, "followtags")) {
64 if (!strcmp(value, "true"))
65 options.followtags = 1;
66 else if (!strcmp(value, "false"))
67 options.followtags = 0;
68 else
69 return -1;
70 return 0;
72 else if (!strcmp(name, "dry-run")) {
73 if (!strcmp(value, "true"))
74 options.dry_run = 1;
75 else if (!strcmp(value, "false"))
76 options.dry_run = 0;
77 else
78 return -1;
79 return 0;
81 else if (!strcmp(name, "check-connectivity")) {
82 if (!strcmp(value, "true"))
83 options.check_self_contained_and_connected = 1;
84 else if (!strcmp(value, "false"))
85 options.check_self_contained_and_connected = 0;
86 else
87 return -1;
88 return 0;
90 else if (!strcmp(name, "cas")) {
91 struct strbuf val = STRBUF_INIT;
92 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
93 string_list_append(&cas_options, val.buf);
94 strbuf_release(&val);
95 return 0;
96 } else if (!strcmp(name, "cloning")) {
97 if (!strcmp(value, "true"))
98 options.cloning = 1;
99 else if (!strcmp(value, "false"))
100 options.cloning = 0;
101 else
102 return -1;
103 return 0;
104 } else if (!strcmp(name, "update-shallow")) {
105 if (!strcmp(value, "true"))
106 options.update_shallow = 1;
107 else if (!strcmp(value, "false"))
108 options.update_shallow = 0;
109 else
110 return -1;
111 return 0;
112 } else if (!strcmp(name, "pushcert")) {
113 if (!strcmp(value, "true"))
114 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
115 else if (!strcmp(value, "false"))
116 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
117 else if (!strcmp(value, "if-asked"))
118 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
119 else
120 return -1;
121 return 0;
122 } else {
123 return 1 /* unsupported */;
127 struct discovery {
128 const char *service;
129 char *buf_alloc;
130 char *buf;
131 size_t len;
132 struct ref *refs;
133 struct sha1_array shallow;
134 unsigned proto_git : 1;
136 static struct discovery *last_discovery;
138 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
140 struct ref *list = NULL;
141 get_remote_heads(-1, heads->buf, heads->len, &list,
142 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
143 return list;
146 static struct ref *parse_info_refs(struct discovery *heads)
148 char *data, *start, *mid;
149 char *ref_name;
150 int i = 0;
152 struct ref *refs = NULL;
153 struct ref *ref = NULL;
154 struct ref *last_ref = NULL;
156 data = heads->buf;
157 start = NULL;
158 mid = data;
159 while (i < heads->len) {
160 if (!start) {
161 start = &data[i];
163 if (data[i] == '\t')
164 mid = &data[i];
165 if (data[i] == '\n') {
166 if (mid - start != 40)
167 die("%sinfo/refs not valid: is this a git repository?",
168 url.buf);
169 data[i] = 0;
170 ref_name = mid + 1;
171 ref = alloc_ref(ref_name);
172 get_oid_hex(start, &ref->old_oid);
173 if (!refs)
174 refs = ref;
175 if (last_ref)
176 last_ref->next = ref;
177 last_ref = ref;
178 start = NULL;
180 i++;
183 ref = alloc_ref("HEAD");
184 if (!http_fetch_ref(url.buf, ref) &&
185 !resolve_remote_symref(ref, refs)) {
186 ref->next = refs;
187 refs = ref;
188 } else {
189 free(ref);
192 return refs;
195 static void free_discovery(struct discovery *d)
197 if (d) {
198 if (d == last_discovery)
199 last_discovery = NULL;
200 free(d->shallow.sha1);
201 free(d->buf_alloc);
202 free_refs(d->refs);
203 free(d);
207 static int show_http_message(struct strbuf *type, struct strbuf *charset,
208 struct strbuf *msg)
210 const char *p, *eol;
213 * We only show text/plain parts, as other types are likely
214 * to be ugly to look at on the user's terminal.
216 if (strcmp(type->buf, "text/plain"))
217 return -1;
218 if (charset->len)
219 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
221 strbuf_trim(msg);
222 if (!msg->len)
223 return -1;
225 p = msg->buf;
226 do {
227 eol = strchrnul(p, '\n');
228 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
229 p = eol + 1;
230 } while(*eol);
231 return 0;
234 static struct discovery *discover_refs(const char *service, int for_push)
236 struct strbuf exp = STRBUF_INIT;
237 struct strbuf type = STRBUF_INIT;
238 struct strbuf charset = STRBUF_INIT;
239 struct strbuf buffer = STRBUF_INIT;
240 struct strbuf refs_url = STRBUF_INIT;
241 struct strbuf effective_url = STRBUF_INIT;
242 struct discovery *last = last_discovery;
243 int http_ret, maybe_smart = 0;
244 struct http_get_options options;
246 if (last && !strcmp(service, last->service))
247 return last;
248 free_discovery(last);
250 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
251 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
252 git_env_bool("GIT_SMART_HTTP", 1)) {
253 maybe_smart = 1;
254 if (!strchr(url.buf, '?'))
255 strbuf_addch(&refs_url, '?');
256 else
257 strbuf_addch(&refs_url, '&');
258 strbuf_addf(&refs_url, "service=%s", service);
261 memset(&options, 0, sizeof(options));
262 options.content_type = &type;
263 options.charset = &charset;
264 options.effective_url = &effective_url;
265 options.base_url = &url;
266 options.no_cache = 1;
267 options.keep_error = 1;
269 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
270 switch (http_ret) {
271 case HTTP_OK:
272 break;
273 case HTTP_MISSING_TARGET:
274 show_http_message(&type, &charset, &buffer);
275 die("repository '%s' not found", url.buf);
276 case HTTP_NOAUTH:
277 show_http_message(&type, &charset, &buffer);
278 die("Authentication failed for '%s'", url.buf);
279 default:
280 show_http_message(&type, &charset, &buffer);
281 die("unable to access '%s': %s", url.buf, curl_errorstr);
284 last= xcalloc(1, sizeof(*last_discovery));
285 last->service = service;
286 last->buf_alloc = strbuf_detach(&buffer, &last->len);
287 last->buf = last->buf_alloc;
289 strbuf_addf(&exp, "application/x-%s-advertisement", service);
290 if (maybe_smart &&
291 (5 <= last->len && last->buf[4] == '#') &&
292 !strbuf_cmp(&exp, &type)) {
293 char *line;
296 * smart HTTP response; validate that the service
297 * pkt-line matches our request.
299 line = packet_read_line_buf(&last->buf, &last->len, NULL);
301 strbuf_reset(&exp);
302 strbuf_addf(&exp, "# service=%s", service);
303 if (strcmp(line, exp.buf))
304 die("invalid server response; got '%s'", line);
305 strbuf_release(&exp);
307 /* The header can include additional metadata lines, up
308 * until a packet flush marker. Ignore these now, but
309 * in the future we might start to scan them.
311 while (packet_read_line_buf(&last->buf, &last->len, NULL))
314 last->proto_git = 1;
317 if (last->proto_git)
318 last->refs = parse_git_refs(last, for_push);
319 else
320 last->refs = parse_info_refs(last);
322 strbuf_release(&refs_url);
323 strbuf_release(&exp);
324 strbuf_release(&type);
325 strbuf_release(&charset);
326 strbuf_release(&effective_url);
327 strbuf_release(&buffer);
328 last_discovery = last;
329 return last;
332 static struct ref *get_refs(int for_push)
334 struct discovery *heads;
336 if (for_push)
337 heads = discover_refs("git-receive-pack", for_push);
338 else
339 heads = discover_refs("git-upload-pack", for_push);
341 return heads->refs;
344 static void output_refs(struct ref *refs)
346 struct ref *posn;
347 for (posn = refs; posn; posn = posn->next) {
348 if (posn->symref)
349 printf("@%s %s\n", posn->symref, posn->name);
350 else
351 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
353 printf("\n");
354 fflush(stdout);
357 struct rpc_state {
358 const char *service_name;
359 const char **argv;
360 struct strbuf *stdin_preamble;
361 char *service_url;
362 char *hdr_content_type;
363 char *hdr_accept;
364 char *buf;
365 size_t alloc;
366 size_t len;
367 size_t pos;
368 int in;
369 int out;
370 struct strbuf result;
371 unsigned gzip_request : 1;
372 unsigned initial_buffer : 1;
375 static size_t rpc_out(void *ptr, size_t eltsize,
376 size_t nmemb, void *buffer_)
378 size_t max = eltsize * nmemb;
379 struct rpc_state *rpc = buffer_;
380 size_t avail = rpc->len - rpc->pos;
382 if (!avail) {
383 rpc->initial_buffer = 0;
384 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
385 if (!avail)
386 return 0;
387 rpc->pos = 0;
388 rpc->len = avail;
391 if (max < avail)
392 avail = max;
393 memcpy(ptr, rpc->buf + rpc->pos, avail);
394 rpc->pos += avail;
395 return avail;
398 #ifndef NO_CURL_IOCTL
399 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
401 struct rpc_state *rpc = clientp;
403 switch (cmd) {
404 case CURLIOCMD_NOP:
405 return CURLIOE_OK;
407 case CURLIOCMD_RESTARTREAD:
408 if (rpc->initial_buffer) {
409 rpc->pos = 0;
410 return CURLIOE_OK;
412 error("unable to rewind rpc post data - try increasing http.postBuffer");
413 return CURLIOE_FAILRESTART;
415 default:
416 return CURLIOE_UNKNOWNCMD;
419 #endif
421 static size_t rpc_in(char *ptr, size_t eltsize,
422 size_t nmemb, void *buffer_)
424 size_t size = eltsize * nmemb;
425 struct rpc_state *rpc = buffer_;
426 write_or_die(rpc->in, ptr, size);
427 return size;
430 static int run_slot(struct active_request_slot *slot,
431 struct slot_results *results)
433 int err;
434 struct slot_results results_buf;
436 if (!results)
437 results = &results_buf;
439 err = run_one_slot(slot, results);
441 if (err != HTTP_OK && err != HTTP_REAUTH) {
442 error("RPC failed; result=%d, HTTP code = %ld",
443 results->curl_result, results->http_code);
446 return err;
449 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
451 struct active_request_slot *slot;
452 struct curl_slist *headers = NULL;
453 struct strbuf buf = STRBUF_INIT;
454 int err;
456 slot = get_active_slot();
458 headers = curl_slist_append(headers, rpc->hdr_content_type);
459 headers = curl_slist_append(headers, rpc->hdr_accept);
461 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
462 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
463 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
464 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
465 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
466 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
467 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
468 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
469 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
471 err = run_slot(slot, results);
473 curl_slist_free_all(headers);
474 strbuf_release(&buf);
475 return err;
478 static int post_rpc(struct rpc_state *rpc)
480 struct active_request_slot *slot;
481 struct curl_slist *headers = NULL;
482 int use_gzip = rpc->gzip_request;
483 char *gzip_body = NULL;
484 size_t gzip_size = 0;
485 int err, large_request = 0;
486 int needs_100_continue = 0;
488 /* Try to load the entire request, if we can fit it into the
489 * allocated buffer space we can use HTTP/1.0 and avoid the
490 * chunked encoding mess.
492 while (1) {
493 size_t left = rpc->alloc - rpc->len;
494 char *buf = rpc->buf + rpc->len;
495 int n;
497 if (left < LARGE_PACKET_MAX) {
498 large_request = 1;
499 use_gzip = 0;
500 break;
503 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
504 if (!n)
505 break;
506 rpc->len += n;
509 if (large_request) {
510 struct slot_results results;
512 do {
513 err = probe_rpc(rpc, &results);
514 if (err == HTTP_REAUTH)
515 credential_fill(&http_auth);
516 } while (err == HTTP_REAUTH);
517 if (err != HTTP_OK)
518 return -1;
520 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
521 needs_100_continue = 1;
524 headers = curl_slist_append(headers, rpc->hdr_content_type);
525 headers = curl_slist_append(headers, rpc->hdr_accept);
526 headers = curl_slist_append(headers, needs_100_continue ?
527 "Expect: 100-continue" : "Expect:");
529 retry:
530 slot = get_active_slot();
532 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
533 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
534 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
535 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
537 if (large_request) {
538 /* The request body is large and the size cannot be predicted.
539 * We must use chunked encoding to send it.
541 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
542 rpc->initial_buffer = 1;
543 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
544 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
545 #ifndef NO_CURL_IOCTL
546 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
547 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
548 #endif
549 if (options.verbosity > 1) {
550 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
551 fflush(stderr);
554 } else if (gzip_body) {
556 * If we are looping to retry authentication, then the previous
557 * run will have set up the headers and gzip buffer already,
558 * and we just need to send it.
560 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
561 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
563 } else if (use_gzip && 1024 < rpc->len) {
564 /* The client backend isn't giving us compressed data so
565 * we can try to deflate it ourselves, this may save on.
566 * the transfer time.
568 git_zstream stream;
569 int ret;
571 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
572 gzip_size = git_deflate_bound(&stream, rpc->len);
573 gzip_body = xmalloc(gzip_size);
575 stream.next_in = (unsigned char *)rpc->buf;
576 stream.avail_in = rpc->len;
577 stream.next_out = (unsigned char *)gzip_body;
578 stream.avail_out = gzip_size;
580 ret = git_deflate(&stream, Z_FINISH);
581 if (ret != Z_STREAM_END)
582 die("cannot deflate request; zlib deflate error %d", ret);
584 ret = git_deflate_end_gently(&stream);
585 if (ret != Z_OK)
586 die("cannot deflate request; zlib end error %d", ret);
588 gzip_size = stream.total_out;
590 headers = curl_slist_append(headers, "Content-Encoding: gzip");
591 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
592 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
594 if (options.verbosity > 1) {
595 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
596 rpc->service_name,
597 (unsigned long)rpc->len, (unsigned long)gzip_size);
598 fflush(stderr);
600 } else {
601 /* We know the complete request size in advance, use the
602 * more normal Content-Length approach.
604 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
605 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
606 if (options.verbosity > 1) {
607 fprintf(stderr, "POST %s (%lu bytes)\n",
608 rpc->service_name, (unsigned long)rpc->len);
609 fflush(stderr);
613 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
614 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
615 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
617 err = run_slot(slot, NULL);
618 if (err == HTTP_REAUTH && !large_request) {
619 credential_fill(&http_auth);
620 goto retry;
622 if (err != HTTP_OK)
623 err = -1;
625 curl_slist_free_all(headers);
626 free(gzip_body);
627 return err;
630 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
632 const char *svc = rpc->service_name;
633 struct strbuf buf = STRBUF_INIT;
634 struct strbuf *preamble = rpc->stdin_preamble;
635 struct child_process client = CHILD_PROCESS_INIT;
636 int err = 0;
638 client.in = -1;
639 client.out = -1;
640 client.git_cmd = 1;
641 client.argv = rpc->argv;
642 if (start_command(&client))
643 exit(1);
644 if (preamble)
645 write_or_die(client.in, preamble->buf, preamble->len);
646 if (heads)
647 write_or_die(client.in, heads->buf, heads->len);
649 rpc->alloc = http_post_buffer;
650 rpc->buf = xmalloc(rpc->alloc);
651 rpc->in = client.in;
652 rpc->out = client.out;
653 strbuf_init(&rpc->result, 0);
655 strbuf_addf(&buf, "%s%s", url.buf, svc);
656 rpc->service_url = strbuf_detach(&buf, NULL);
658 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
659 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
661 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
662 rpc->hdr_accept = strbuf_detach(&buf, NULL);
664 while (!err) {
665 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
666 if (!n)
667 break;
668 rpc->pos = 0;
669 rpc->len = n;
670 err |= post_rpc(rpc);
673 close(client.in);
674 client.in = -1;
675 if (!err) {
676 strbuf_read(&rpc->result, client.out, 0);
677 } else {
678 char buf[4096];
679 for (;;)
680 if (xread(client.out, buf, sizeof(buf)) <= 0)
681 break;
684 close(client.out);
685 client.out = -1;
687 err |= finish_command(&client);
688 free(rpc->service_url);
689 free(rpc->hdr_content_type);
690 free(rpc->hdr_accept);
691 free(rpc->buf);
692 strbuf_release(&buf);
693 return err;
696 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
698 struct walker *walker;
699 char **targets = xmalloc(nr_heads * sizeof(char*));
700 int ret, i;
702 if (options.depth)
703 die("dumb http transport does not support --depth");
704 for (i = 0; i < nr_heads; i++)
705 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
707 walker = get_http_walker(url.buf);
708 walker->get_all = 1;
709 walker->get_tree = 1;
710 walker->get_history = 1;
711 walker->get_verbosely = options.verbosity >= 3;
712 walker->get_recover = 0;
713 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
714 walker_free(walker);
716 for (i = 0; i < nr_heads; i++)
717 free(targets[i]);
718 free(targets);
720 return ret ? error("fetch failed.") : 0;
723 static int fetch_git(struct discovery *heads,
724 int nr_heads, struct ref **to_fetch)
726 struct rpc_state rpc;
727 struct strbuf preamble = STRBUF_INIT;
728 int i, err;
729 struct argv_array args = ARGV_ARRAY_INIT;
731 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
732 "--stdin", "--lock-pack", NULL);
733 if (options.followtags)
734 argv_array_push(&args, "--include-tag");
735 if (options.thin)
736 argv_array_push(&args, "--thin");
737 if (options.verbosity >= 3)
738 argv_array_pushl(&args, "-v", "-v", NULL);
739 if (options.check_self_contained_and_connected)
740 argv_array_push(&args, "--check-self-contained-and-connected");
741 if (options.cloning)
742 argv_array_push(&args, "--cloning");
743 if (options.update_shallow)
744 argv_array_push(&args, "--update-shallow");
745 if (!options.progress)
746 argv_array_push(&args, "--no-progress");
747 if (options.depth)
748 argv_array_pushf(&args, "--depth=%lu", options.depth);
749 argv_array_push(&args, url.buf);
751 for (i = 0; i < nr_heads; i++) {
752 struct ref *ref = to_fetch[i];
753 if (!*ref->name)
754 die("cannot fetch by sha1 over smart http");
755 packet_buf_write(&preamble, "%s %s\n",
756 oid_to_hex(&ref->old_oid), ref->name);
758 packet_buf_flush(&preamble);
760 memset(&rpc, 0, sizeof(rpc));
761 rpc.service_name = "git-upload-pack",
762 rpc.argv = args.argv;
763 rpc.stdin_preamble = &preamble;
764 rpc.gzip_request = 1;
766 err = rpc_service(&rpc, heads);
767 if (rpc.result.len)
768 write_or_die(1, rpc.result.buf, rpc.result.len);
769 strbuf_release(&rpc.result);
770 strbuf_release(&preamble);
771 argv_array_clear(&args);
772 return err;
775 static int fetch(int nr_heads, struct ref **to_fetch)
777 struct discovery *d = discover_refs("git-upload-pack", 0);
778 if (d->proto_git)
779 return fetch_git(d, nr_heads, to_fetch);
780 else
781 return fetch_dumb(nr_heads, to_fetch);
784 static void parse_fetch(struct strbuf *buf)
786 struct ref **to_fetch = NULL;
787 struct ref *list_head = NULL;
788 struct ref **list = &list_head;
789 int alloc_heads = 0, nr_heads = 0;
791 do {
792 const char *p;
793 if (skip_prefix(buf->buf, "fetch ", &p)) {
794 const char *name;
795 struct ref *ref;
796 struct object_id old_oid;
798 if (get_oid_hex(p, &old_oid))
799 die("protocol error: expected sha/ref, got %s'", p);
800 if (p[GIT_SHA1_HEXSZ] == ' ')
801 name = p + GIT_SHA1_HEXSZ + 1;
802 else if (!p[GIT_SHA1_HEXSZ])
803 name = "";
804 else
805 die("protocol error: expected sha/ref, got %s'", p);
807 ref = alloc_ref(name);
808 oidcpy(&ref->old_oid, &old_oid);
810 *list = ref;
811 list = &ref->next;
813 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
814 to_fetch[nr_heads++] = ref;
816 else
817 die("http transport does not support %s", buf->buf);
819 strbuf_reset(buf);
820 if (strbuf_getline_lf(buf, stdin) == EOF)
821 return;
822 if (!*buf->buf)
823 break;
824 } while (1);
826 if (fetch(nr_heads, to_fetch))
827 exit(128); /* error already reported */
828 free_refs(list_head);
829 free(to_fetch);
831 printf("\n");
832 fflush(stdout);
833 strbuf_reset(buf);
836 static int push_dav(int nr_spec, char **specs)
838 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
839 int argc = 0, i;
841 argv[argc++] = "http-push";
842 argv[argc++] = "--helper-status";
843 if (options.dry_run)
844 argv[argc++] = "--dry-run";
845 if (options.verbosity > 1)
846 argv[argc++] = "--verbose";
847 argv[argc++] = url.buf;
848 for (i = 0; i < nr_spec; i++)
849 argv[argc++] = specs[i];
850 argv[argc++] = NULL;
852 if (run_command_v_opt(argv, RUN_GIT_CMD))
853 die("git-%s failed", argv[0]);
854 free(argv);
855 return 0;
858 static int push_git(struct discovery *heads, int nr_spec, char **specs)
860 struct rpc_state rpc;
861 int i, err;
862 struct argv_array args;
863 struct string_list_item *cas_option;
864 struct strbuf preamble = STRBUF_INIT;
866 argv_array_init(&args);
867 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
868 NULL);
870 if (options.thin)
871 argv_array_push(&args, "--thin");
872 if (options.dry_run)
873 argv_array_push(&args, "--dry-run");
874 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
875 argv_array_push(&args, "--signed=yes");
876 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
877 argv_array_push(&args, "--signed=if-asked");
878 if (options.verbosity == 0)
879 argv_array_push(&args, "--quiet");
880 else if (options.verbosity > 1)
881 argv_array_push(&args, "--verbose");
882 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
883 for_each_string_list_item(cas_option, &cas_options)
884 argv_array_push(&args, cas_option->string);
885 argv_array_push(&args, url.buf);
887 argv_array_push(&args, "--stdin");
888 for (i = 0; i < nr_spec; i++)
889 packet_buf_write(&preamble, "%s\n", specs[i]);
890 packet_buf_flush(&preamble);
892 memset(&rpc, 0, sizeof(rpc));
893 rpc.service_name = "git-receive-pack",
894 rpc.argv = args.argv;
895 rpc.stdin_preamble = &preamble;
897 err = rpc_service(&rpc, heads);
898 if (rpc.result.len)
899 write_or_die(1, rpc.result.buf, rpc.result.len);
900 strbuf_release(&rpc.result);
901 strbuf_release(&preamble);
902 argv_array_clear(&args);
903 return err;
906 static int push(int nr_spec, char **specs)
908 struct discovery *heads = discover_refs("git-receive-pack", 1);
909 int ret;
911 if (heads->proto_git)
912 ret = push_git(heads, nr_spec, specs);
913 else
914 ret = push_dav(nr_spec, specs);
915 free_discovery(heads);
916 return ret;
919 static void parse_push(struct strbuf *buf)
921 char **specs = NULL;
922 int alloc_spec = 0, nr_spec = 0, i, ret;
924 do {
925 if (starts_with(buf->buf, "push ")) {
926 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
927 specs[nr_spec++] = xstrdup(buf->buf + 5);
929 else
930 die("http transport does not support %s", buf->buf);
932 strbuf_reset(buf);
933 if (strbuf_getline_lf(buf, stdin) == EOF)
934 goto free_specs;
935 if (!*buf->buf)
936 break;
937 } while (1);
939 ret = push(nr_spec, specs);
940 printf("\n");
941 fflush(stdout);
943 if (ret)
944 exit(128); /* error already reported */
946 free_specs:
947 for (i = 0; i < nr_spec; i++)
948 free(specs[i]);
949 free(specs);
952 int main(int argc, const char **argv)
954 struct strbuf buf = STRBUF_INIT;
955 int nongit;
957 git_setup_gettext();
959 git_extract_argv0_path(argv[0]);
960 setup_git_directory_gently(&nongit);
961 if (argc < 2) {
962 error("remote-curl: usage: git remote-curl <remote> [<url>]");
963 return 1;
966 options.verbosity = 1;
967 options.progress = !!isatty(2);
968 options.thin = 1;
970 remote = remote_get(argv[1]);
972 if (argc > 2) {
973 end_url_with_slash(&url, argv[2]);
974 } else {
975 end_url_with_slash(&url, remote->url[0]);
978 http_init(remote, url.buf, 0);
980 do {
981 const char *arg;
983 if (strbuf_getline_lf(&buf, stdin) == EOF) {
984 if (ferror(stdin))
985 error("remote-curl: error reading command stream from git");
986 return 1;
988 if (buf.len == 0)
989 break;
990 if (starts_with(buf.buf, "fetch ")) {
991 if (nongit)
992 die("remote-curl: fetch attempted without a local repo");
993 parse_fetch(&buf);
995 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
996 int for_push = !!strstr(buf.buf + 4, "for-push");
997 output_refs(get_refs(for_push));
999 } else if (starts_with(buf.buf, "push ")) {
1000 parse_push(&buf);
1002 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1003 char *value = strchr(arg, ' ');
1004 int result;
1006 if (value)
1007 *value++ = '\0';
1008 else
1009 value = "true";
1011 result = set_option(arg, value);
1012 if (!result)
1013 printf("ok\n");
1014 else if (result < 0)
1015 printf("error invalid value\n");
1016 else
1017 printf("unsupported\n");
1018 fflush(stdout);
1020 } else if (!strcmp(buf.buf, "capabilities")) {
1021 printf("fetch\n");
1022 printf("option\n");
1023 printf("push\n");
1024 printf("check-connectivity\n");
1025 printf("\n");
1026 fflush(stdout);
1027 } else {
1028 error("remote-curl: unknown command '%s' from git", buf.buf);
1029 return 1;
1031 strbuf_reset(&buf);
1032 } while (1);
1034 http_cleanup();
1036 return 0;