Git.pm: add the "use warnings" pragma
[git.git] / remote-curl.c
blob6ec535243565d4b1a50e22868966b4aec33776f8
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "strbuf.h"
5 #include "walker.h"
6 #include "http.h"
7 #include "exec_cmd.h"
8 #include "run-command.h"
9 #include "pkt-line.h"
10 #include "string-list.h"
11 #include "sideband.h"
12 #include "argv-array.h"
13 #include "credential.h"
14 #include "sha1-array.h"
15 #include "send-pack.h"
17 static struct remote *remote;
18 /* always ends with a trailing slash */
19 static struct strbuf url = STRBUF_INIT;
21 struct options {
22 int verbosity;
23 unsigned long depth;
24 char *deepen_since;
25 struct string_list deepen_not;
26 struct string_list push_options;
27 char *filter;
28 unsigned progress : 1,
29 check_self_contained_and_connected : 1,
30 cloning : 1,
31 update_shallow : 1,
32 followtags : 1,
33 dry_run : 1,
34 thin : 1,
35 /* One of the SEND_PACK_PUSH_CERT_* constants. */
36 push_cert : 2,
37 deepen_relative : 1,
38 from_promisor : 1,
39 no_dependents : 1;
41 static struct options options;
42 static struct string_list cas_options = STRING_LIST_INIT_DUP;
44 static int set_option(const char *name, const char *value)
46 if (!strcmp(name, "verbosity")) {
47 char *end;
48 int v = strtol(value, &end, 10);
49 if (value == end || *end)
50 return -1;
51 options.verbosity = v;
52 return 0;
54 else if (!strcmp(name, "progress")) {
55 if (!strcmp(value, "true"))
56 options.progress = 1;
57 else if (!strcmp(value, "false"))
58 options.progress = 0;
59 else
60 return -1;
61 return 0;
63 else if (!strcmp(name, "depth")) {
64 char *end;
65 unsigned long v = strtoul(value, &end, 10);
66 if (value == end || *end)
67 return -1;
68 options.depth = v;
69 return 0;
71 else if (!strcmp(name, "deepen-since")) {
72 options.deepen_since = xstrdup(value);
73 return 0;
75 else if (!strcmp(name, "deepen-not")) {
76 string_list_append(&options.deepen_not, value);
77 return 0;
79 else if (!strcmp(name, "deepen-relative")) {
80 if (!strcmp(value, "true"))
81 options.deepen_relative = 1;
82 else if (!strcmp(value, "false"))
83 options.deepen_relative = 0;
84 else
85 return -1;
86 return 0;
88 else if (!strcmp(name, "followtags")) {
89 if (!strcmp(value, "true"))
90 options.followtags = 1;
91 else if (!strcmp(value, "false"))
92 options.followtags = 0;
93 else
94 return -1;
95 return 0;
97 else if (!strcmp(name, "dry-run")) {
98 if (!strcmp(value, "true"))
99 options.dry_run = 1;
100 else if (!strcmp(value, "false"))
101 options.dry_run = 0;
102 else
103 return -1;
104 return 0;
106 else if (!strcmp(name, "check-connectivity")) {
107 if (!strcmp(value, "true"))
108 options.check_self_contained_and_connected = 1;
109 else if (!strcmp(value, "false"))
110 options.check_self_contained_and_connected = 0;
111 else
112 return -1;
113 return 0;
115 else if (!strcmp(name, "cas")) {
116 struct strbuf val = STRBUF_INIT;
117 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
118 string_list_append(&cas_options, val.buf);
119 strbuf_release(&val);
120 return 0;
121 } else if (!strcmp(name, "cloning")) {
122 if (!strcmp(value, "true"))
123 options.cloning = 1;
124 else if (!strcmp(value, "false"))
125 options.cloning = 0;
126 else
127 return -1;
128 return 0;
129 } else if (!strcmp(name, "update-shallow")) {
130 if (!strcmp(value, "true"))
131 options.update_shallow = 1;
132 else if (!strcmp(value, "false"))
133 options.update_shallow = 0;
134 else
135 return -1;
136 return 0;
137 } else if (!strcmp(name, "pushcert")) {
138 if (!strcmp(value, "true"))
139 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
140 else if (!strcmp(value, "false"))
141 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
142 else if (!strcmp(value, "if-asked"))
143 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
144 else
145 return -1;
146 return 0;
147 } else if (!strcmp(name, "push-option")) {
148 string_list_append(&options.push_options, value);
149 return 0;
151 #if LIBCURL_VERSION_NUM >= 0x070a08
152 } else if (!strcmp(name, "family")) {
153 if (!strcmp(value, "ipv4"))
154 git_curl_ipresolve = CURL_IPRESOLVE_V4;
155 else if (!strcmp(value, "ipv6"))
156 git_curl_ipresolve = CURL_IPRESOLVE_V6;
157 else if (!strcmp(value, "all"))
158 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
159 else
160 return -1;
161 return 0;
162 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
163 } else if (!strcmp(name, "from-promisor")) {
164 options.from_promisor = 1;
165 return 0;
166 } else if (!strcmp(name, "no-dependents")) {
167 options.no_dependents = 1;
168 return 0;
169 } else if (!strcmp(name, "filter")) {
170 options.filter = xstrdup(value);;
171 return 0;
172 } else {
173 return 1 /* unsupported */;
177 struct discovery {
178 const char *service;
179 char *buf_alloc;
180 char *buf;
181 size_t len;
182 struct ref *refs;
183 struct oid_array shallow;
184 unsigned proto_git : 1;
186 static struct discovery *last_discovery;
188 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
190 struct ref *list = NULL;
191 get_remote_heads(-1, heads->buf, heads->len, &list,
192 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
193 return list;
196 static struct ref *parse_info_refs(struct discovery *heads)
198 char *data, *start, *mid;
199 char *ref_name;
200 int i = 0;
202 struct ref *refs = NULL;
203 struct ref *ref = NULL;
204 struct ref *last_ref = NULL;
206 data = heads->buf;
207 start = NULL;
208 mid = data;
209 while (i < heads->len) {
210 if (!start) {
211 start = &data[i];
213 if (data[i] == '\t')
214 mid = &data[i];
215 if (data[i] == '\n') {
216 if (mid - start != 40)
217 die("%sinfo/refs not valid: is this a git repository?",
218 url.buf);
219 data[i] = 0;
220 ref_name = mid + 1;
221 ref = alloc_ref(ref_name);
222 get_oid_hex(start, &ref->old_oid);
223 if (!refs)
224 refs = ref;
225 if (last_ref)
226 last_ref->next = ref;
227 last_ref = ref;
228 start = NULL;
230 i++;
233 ref = alloc_ref("HEAD");
234 if (!http_fetch_ref(url.buf, ref) &&
235 !resolve_remote_symref(ref, refs)) {
236 ref->next = refs;
237 refs = ref;
238 } else {
239 free(ref);
242 return refs;
245 static void free_discovery(struct discovery *d)
247 if (d) {
248 if (d == last_discovery)
249 last_discovery = NULL;
250 free(d->shallow.oid);
251 free(d->buf_alloc);
252 free_refs(d->refs);
253 free(d);
257 static int show_http_message(struct strbuf *type, struct strbuf *charset,
258 struct strbuf *msg)
260 const char *p, *eol;
263 * We only show text/plain parts, as other types are likely
264 * to be ugly to look at on the user's terminal.
266 if (strcmp(type->buf, "text/plain"))
267 return -1;
268 if (charset->len)
269 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
271 strbuf_trim(msg);
272 if (!msg->len)
273 return -1;
275 p = msg->buf;
276 do {
277 eol = strchrnul(p, '\n');
278 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
279 p = eol + 1;
280 } while(*eol);
281 return 0;
284 static struct discovery *discover_refs(const char *service, int for_push)
286 struct strbuf exp = STRBUF_INIT;
287 struct strbuf type = STRBUF_INIT;
288 struct strbuf charset = STRBUF_INIT;
289 struct strbuf buffer = STRBUF_INIT;
290 struct strbuf refs_url = STRBUF_INIT;
291 struct strbuf effective_url = STRBUF_INIT;
292 struct discovery *last = last_discovery;
293 int http_ret, maybe_smart = 0;
294 struct http_get_options http_options;
296 if (last && !strcmp(service, last->service))
297 return last;
298 free_discovery(last);
300 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
301 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
302 git_env_bool("GIT_SMART_HTTP", 1)) {
303 maybe_smart = 1;
304 if (!strchr(url.buf, '?'))
305 strbuf_addch(&refs_url, '?');
306 else
307 strbuf_addch(&refs_url, '&');
308 strbuf_addf(&refs_url, "service=%s", service);
311 memset(&http_options, 0, sizeof(http_options));
312 http_options.content_type = &type;
313 http_options.charset = &charset;
314 http_options.effective_url = &effective_url;
315 http_options.base_url = &url;
316 http_options.initial_request = 1;
317 http_options.no_cache = 1;
318 http_options.keep_error = 1;
320 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
321 switch (http_ret) {
322 case HTTP_OK:
323 break;
324 case HTTP_MISSING_TARGET:
325 show_http_message(&type, &charset, &buffer);
326 die("repository '%s' not found", url.buf);
327 case HTTP_NOAUTH:
328 show_http_message(&type, &charset, &buffer);
329 die("Authentication failed for '%s'", url.buf);
330 default:
331 show_http_message(&type, &charset, &buffer);
332 die("unable to access '%s': %s", url.buf, curl_errorstr);
335 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
336 warning(_("redirecting to %s"), url.buf);
338 last= xcalloc(1, sizeof(*last_discovery));
339 last->service = service;
340 last->buf_alloc = strbuf_detach(&buffer, &last->len);
341 last->buf = last->buf_alloc;
343 strbuf_addf(&exp, "application/x-%s-advertisement", service);
344 if (maybe_smart &&
345 (5 <= last->len && last->buf[4] == '#') &&
346 !strbuf_cmp(&exp, &type)) {
347 char *line;
350 * smart HTTP response; validate that the service
351 * pkt-line matches our request.
353 line = packet_read_line_buf(&last->buf, &last->len, NULL);
355 strbuf_reset(&exp);
356 strbuf_addf(&exp, "# service=%s", service);
357 if (strcmp(line, exp.buf))
358 die("invalid server response; got '%s'", line);
359 strbuf_release(&exp);
361 /* The header can include additional metadata lines, up
362 * until a packet flush marker. Ignore these now, but
363 * in the future we might start to scan them.
365 while (packet_read_line_buf(&last->buf, &last->len, NULL))
368 last->proto_git = 1;
371 if (last->proto_git)
372 last->refs = parse_git_refs(last, for_push);
373 else
374 last->refs = parse_info_refs(last);
376 strbuf_release(&refs_url);
377 strbuf_release(&exp);
378 strbuf_release(&type);
379 strbuf_release(&charset);
380 strbuf_release(&effective_url);
381 strbuf_release(&buffer);
382 last_discovery = last;
383 return last;
386 static struct ref *get_refs(int for_push)
388 struct discovery *heads;
390 if (for_push)
391 heads = discover_refs("git-receive-pack", for_push);
392 else
393 heads = discover_refs("git-upload-pack", for_push);
395 return heads->refs;
398 static void output_refs(struct ref *refs)
400 struct ref *posn;
401 for (posn = refs; posn; posn = posn->next) {
402 if (posn->symref)
403 printf("@%s %s\n", posn->symref, posn->name);
404 else
405 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
407 printf("\n");
408 fflush(stdout);
411 struct rpc_state {
412 const char *service_name;
413 const char **argv;
414 struct strbuf *stdin_preamble;
415 char *service_url;
416 char *hdr_content_type;
417 char *hdr_accept;
418 char *buf;
419 size_t alloc;
420 size_t len;
421 size_t pos;
422 int in;
423 int out;
424 int any_written;
425 struct strbuf result;
426 unsigned gzip_request : 1;
427 unsigned initial_buffer : 1;
430 static size_t rpc_out(void *ptr, size_t eltsize,
431 size_t nmemb, void *buffer_)
433 size_t max = eltsize * nmemb;
434 struct rpc_state *rpc = buffer_;
435 size_t avail = rpc->len - rpc->pos;
437 if (!avail) {
438 rpc->initial_buffer = 0;
439 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
440 if (!avail)
441 return 0;
442 rpc->pos = 0;
443 rpc->len = avail;
446 if (max < avail)
447 avail = max;
448 memcpy(ptr, rpc->buf + rpc->pos, avail);
449 rpc->pos += avail;
450 return avail;
453 #ifndef NO_CURL_IOCTL
454 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
456 struct rpc_state *rpc = clientp;
458 switch (cmd) {
459 case CURLIOCMD_NOP:
460 return CURLIOE_OK;
462 case CURLIOCMD_RESTARTREAD:
463 if (rpc->initial_buffer) {
464 rpc->pos = 0;
465 return CURLIOE_OK;
467 error("unable to rewind rpc post data - try increasing http.postBuffer");
468 return CURLIOE_FAILRESTART;
470 default:
471 return CURLIOE_UNKNOWNCMD;
474 #endif
476 static size_t rpc_in(char *ptr, size_t eltsize,
477 size_t nmemb, void *buffer_)
479 size_t size = eltsize * nmemb;
480 struct rpc_state *rpc = buffer_;
481 if (size)
482 rpc->any_written = 1;
483 write_or_die(rpc->in, ptr, size);
484 return size;
487 static int run_slot(struct active_request_slot *slot,
488 struct slot_results *results)
490 int err;
491 struct slot_results results_buf;
493 if (!results)
494 results = &results_buf;
496 err = run_one_slot(slot, results);
498 if (err != HTTP_OK && err != HTTP_REAUTH) {
499 struct strbuf msg = STRBUF_INIT;
500 if (results->http_code && results->http_code != 200)
501 strbuf_addf(&msg, "HTTP %ld", results->http_code);
502 if (results->curl_result != CURLE_OK) {
503 if (msg.len)
504 strbuf_addch(&msg, ' ');
505 strbuf_addf(&msg, "curl %d", results->curl_result);
506 if (curl_errorstr[0]) {
507 strbuf_addch(&msg, ' ');
508 strbuf_addstr(&msg, curl_errorstr);
511 error("RPC failed; %s", msg.buf);
512 strbuf_release(&msg);
515 return err;
518 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
520 struct active_request_slot *slot;
521 struct curl_slist *headers = http_copy_default_headers();
522 struct strbuf buf = STRBUF_INIT;
523 int err;
525 slot = get_active_slot();
527 headers = curl_slist_append(headers, rpc->hdr_content_type);
528 headers = curl_slist_append(headers, rpc->hdr_accept);
530 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
531 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
532 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
533 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
534 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
535 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
536 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
537 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
538 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
540 err = run_slot(slot, results);
542 curl_slist_free_all(headers);
543 strbuf_release(&buf);
544 return err;
547 static curl_off_t xcurl_off_t(ssize_t len) {
548 if (len > maximum_signed_value_of_type(curl_off_t))
549 die("cannot handle pushes this big");
550 return (curl_off_t) len;
553 static int post_rpc(struct rpc_state *rpc)
555 struct active_request_slot *slot;
556 struct curl_slist *headers = http_copy_default_headers();
557 int use_gzip = rpc->gzip_request;
558 char *gzip_body = NULL;
559 size_t gzip_size = 0;
560 int err, large_request = 0;
561 int needs_100_continue = 0;
563 /* Try to load the entire request, if we can fit it into the
564 * allocated buffer space we can use HTTP/1.0 and avoid the
565 * chunked encoding mess.
567 while (1) {
568 size_t left = rpc->alloc - rpc->len;
569 char *buf = rpc->buf + rpc->len;
570 int n;
572 if (left < LARGE_PACKET_MAX) {
573 large_request = 1;
574 use_gzip = 0;
575 break;
578 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
579 if (!n)
580 break;
581 rpc->len += n;
584 if (large_request) {
585 struct slot_results results;
587 do {
588 err = probe_rpc(rpc, &results);
589 if (err == HTTP_REAUTH)
590 credential_fill(&http_auth);
591 } while (err == HTTP_REAUTH);
592 if (err != HTTP_OK)
593 return -1;
595 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
596 needs_100_continue = 1;
599 headers = curl_slist_append(headers, rpc->hdr_content_type);
600 headers = curl_slist_append(headers, rpc->hdr_accept);
601 headers = curl_slist_append(headers, needs_100_continue ?
602 "Expect: 100-continue" : "Expect:");
604 retry:
605 slot = get_active_slot();
607 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
608 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
609 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
610 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
612 if (large_request) {
613 /* The request body is large and the size cannot be predicted.
614 * We must use chunked encoding to send it.
616 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
617 rpc->initial_buffer = 1;
618 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
619 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
620 #ifndef NO_CURL_IOCTL
621 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
622 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
623 #endif
624 if (options.verbosity > 1) {
625 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
626 fflush(stderr);
629 } else if (gzip_body) {
631 * If we are looping to retry authentication, then the previous
632 * run will have set up the headers and gzip buffer already,
633 * and we just need to send it.
635 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
636 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
638 } else if (use_gzip && 1024 < rpc->len) {
639 /* The client backend isn't giving us compressed data so
640 * we can try to deflate it ourselves, this may save on.
641 * the transfer time.
643 git_zstream stream;
644 int ret;
646 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
647 gzip_size = git_deflate_bound(&stream, rpc->len);
648 gzip_body = xmalloc(gzip_size);
650 stream.next_in = (unsigned char *)rpc->buf;
651 stream.avail_in = rpc->len;
652 stream.next_out = (unsigned char *)gzip_body;
653 stream.avail_out = gzip_size;
655 ret = git_deflate(&stream, Z_FINISH);
656 if (ret != Z_STREAM_END)
657 die("cannot deflate request; zlib deflate error %d", ret);
659 ret = git_deflate_end_gently(&stream);
660 if (ret != Z_OK)
661 die("cannot deflate request; zlib end error %d", ret);
663 gzip_size = stream.total_out;
665 headers = curl_slist_append(headers, "Content-Encoding: gzip");
666 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
667 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
669 if (options.verbosity > 1) {
670 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
671 rpc->service_name,
672 (unsigned long)rpc->len, (unsigned long)gzip_size);
673 fflush(stderr);
675 } else {
676 /* We know the complete request size in advance, use the
677 * more normal Content-Length approach.
679 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
680 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
681 if (options.verbosity > 1) {
682 fprintf(stderr, "POST %s (%lu bytes)\n",
683 rpc->service_name, (unsigned long)rpc->len);
684 fflush(stderr);
688 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
689 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
690 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
693 rpc->any_written = 0;
694 err = run_slot(slot, NULL);
695 if (err == HTTP_REAUTH && !large_request) {
696 credential_fill(&http_auth);
697 goto retry;
699 if (err != HTTP_OK)
700 err = -1;
702 if (!rpc->any_written)
703 err = -1;
705 curl_slist_free_all(headers);
706 free(gzip_body);
707 return err;
710 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
712 const char *svc = rpc->service_name;
713 struct strbuf buf = STRBUF_INIT;
714 struct strbuf *preamble = rpc->stdin_preamble;
715 struct child_process client = CHILD_PROCESS_INIT;
716 int err = 0;
718 client.in = -1;
719 client.out = -1;
720 client.git_cmd = 1;
721 client.argv = rpc->argv;
722 if (start_command(&client))
723 exit(1);
724 if (preamble)
725 write_or_die(client.in, preamble->buf, preamble->len);
726 if (heads)
727 write_or_die(client.in, heads->buf, heads->len);
729 rpc->alloc = http_post_buffer;
730 rpc->buf = xmalloc(rpc->alloc);
731 rpc->in = client.in;
732 rpc->out = client.out;
733 strbuf_init(&rpc->result, 0);
735 strbuf_addf(&buf, "%s%s", url.buf, svc);
736 rpc->service_url = strbuf_detach(&buf, NULL);
738 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
739 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
741 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
742 rpc->hdr_accept = strbuf_detach(&buf, NULL);
744 while (!err) {
745 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
746 if (!n)
747 break;
748 rpc->pos = 0;
749 rpc->len = n;
750 err |= post_rpc(rpc);
753 close(client.in);
754 client.in = -1;
755 if (!err) {
756 strbuf_read(&rpc->result, client.out, 0);
757 } else {
758 char buf[4096];
759 for (;;)
760 if (xread(client.out, buf, sizeof(buf)) <= 0)
761 break;
764 close(client.out);
765 client.out = -1;
767 err |= finish_command(&client);
768 free(rpc->service_url);
769 free(rpc->hdr_content_type);
770 free(rpc->hdr_accept);
771 free(rpc->buf);
772 strbuf_release(&buf);
773 return err;
776 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
778 struct walker *walker;
779 char **targets;
780 int ret, i;
782 ALLOC_ARRAY(targets, nr_heads);
783 if (options.depth || options.deepen_since)
784 die("dumb http transport does not support shallow capabilities");
785 for (i = 0; i < nr_heads; i++)
786 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
788 walker = get_http_walker(url.buf);
789 walker->get_all = 1;
790 walker->get_tree = 1;
791 walker->get_history = 1;
792 walker->get_verbosely = options.verbosity >= 3;
793 walker->get_recover = 0;
794 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
795 walker_free(walker);
797 for (i = 0; i < nr_heads; i++)
798 free(targets[i]);
799 free(targets);
801 return ret ? error("fetch failed.") : 0;
804 static int fetch_git(struct discovery *heads,
805 int nr_heads, struct ref **to_fetch)
807 struct rpc_state rpc;
808 struct strbuf preamble = STRBUF_INIT;
809 int i, err;
810 struct argv_array args = ARGV_ARRAY_INIT;
812 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
813 "--stdin", "--lock-pack", NULL);
814 if (options.followtags)
815 argv_array_push(&args, "--include-tag");
816 if (options.thin)
817 argv_array_push(&args, "--thin");
818 if (options.verbosity >= 3)
819 argv_array_pushl(&args, "-v", "-v", NULL);
820 if (options.check_self_contained_and_connected)
821 argv_array_push(&args, "--check-self-contained-and-connected");
822 if (options.cloning)
823 argv_array_push(&args, "--cloning");
824 if (options.update_shallow)
825 argv_array_push(&args, "--update-shallow");
826 if (!options.progress)
827 argv_array_push(&args, "--no-progress");
828 if (options.depth)
829 argv_array_pushf(&args, "--depth=%lu", options.depth);
830 if (options.deepen_since)
831 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
832 for (i = 0; i < options.deepen_not.nr; i++)
833 argv_array_pushf(&args, "--shallow-exclude=%s",
834 options.deepen_not.items[i].string);
835 if (options.deepen_relative && options.depth)
836 argv_array_push(&args, "--deepen-relative");
837 if (options.from_promisor)
838 argv_array_push(&args, "--from-promisor");
839 if (options.no_dependents)
840 argv_array_push(&args, "--no-dependents");
841 if (options.filter)
842 argv_array_pushf(&args, "--filter=%s", options.filter);
843 argv_array_push(&args, url.buf);
845 for (i = 0; i < nr_heads; i++) {
846 struct ref *ref = to_fetch[i];
847 if (!*ref->name)
848 die("cannot fetch by sha1 over smart http");
849 packet_buf_write(&preamble, "%s %s\n",
850 oid_to_hex(&ref->old_oid), ref->name);
852 packet_buf_flush(&preamble);
854 memset(&rpc, 0, sizeof(rpc));
855 rpc.service_name = "git-upload-pack",
856 rpc.argv = args.argv;
857 rpc.stdin_preamble = &preamble;
858 rpc.gzip_request = 1;
860 err = rpc_service(&rpc, heads);
861 if (rpc.result.len)
862 write_or_die(1, rpc.result.buf, rpc.result.len);
863 strbuf_release(&rpc.result);
864 strbuf_release(&preamble);
865 argv_array_clear(&args);
866 return err;
869 static int fetch(int nr_heads, struct ref **to_fetch)
871 struct discovery *d = discover_refs("git-upload-pack", 0);
872 if (d->proto_git)
873 return fetch_git(d, nr_heads, to_fetch);
874 else
875 return fetch_dumb(nr_heads, to_fetch);
878 static void parse_fetch(struct strbuf *buf)
880 struct ref **to_fetch = NULL;
881 struct ref *list_head = NULL;
882 struct ref **list = &list_head;
883 int alloc_heads = 0, nr_heads = 0;
885 do {
886 const char *p;
887 if (skip_prefix(buf->buf, "fetch ", &p)) {
888 const char *name;
889 struct ref *ref;
890 struct object_id old_oid;
892 if (get_oid_hex(p, &old_oid))
893 die("protocol error: expected sha/ref, got %s'", p);
894 if (p[GIT_SHA1_HEXSZ] == ' ')
895 name = p + GIT_SHA1_HEXSZ + 1;
896 else if (!p[GIT_SHA1_HEXSZ])
897 name = "";
898 else
899 die("protocol error: expected sha/ref, got %s'", p);
901 ref = alloc_ref(name);
902 oidcpy(&ref->old_oid, &old_oid);
904 *list = ref;
905 list = &ref->next;
907 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
908 to_fetch[nr_heads++] = ref;
910 else
911 die("http transport does not support %s", buf->buf);
913 strbuf_reset(buf);
914 if (strbuf_getline_lf(buf, stdin) == EOF)
915 return;
916 if (!*buf->buf)
917 break;
918 } while (1);
920 if (fetch(nr_heads, to_fetch))
921 exit(128); /* error already reported */
922 free_refs(list_head);
923 free(to_fetch);
925 printf("\n");
926 fflush(stdout);
927 strbuf_reset(buf);
930 static int push_dav(int nr_spec, char **specs)
932 struct child_process child = CHILD_PROCESS_INIT;
933 size_t i;
935 child.git_cmd = 1;
936 argv_array_push(&child.args, "http-push");
937 argv_array_push(&child.args, "--helper-status");
938 if (options.dry_run)
939 argv_array_push(&child.args, "--dry-run");
940 if (options.verbosity > 1)
941 argv_array_push(&child.args, "--verbose");
942 argv_array_push(&child.args, url.buf);
943 for (i = 0; i < nr_spec; i++)
944 argv_array_push(&child.args, specs[i]);
946 if (run_command(&child))
947 die("git-http-push failed");
948 return 0;
951 static int push_git(struct discovery *heads, int nr_spec, char **specs)
953 struct rpc_state rpc;
954 int i, err;
955 struct argv_array args;
956 struct string_list_item *cas_option;
957 struct strbuf preamble = STRBUF_INIT;
959 argv_array_init(&args);
960 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
961 NULL);
963 if (options.thin)
964 argv_array_push(&args, "--thin");
965 if (options.dry_run)
966 argv_array_push(&args, "--dry-run");
967 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
968 argv_array_push(&args, "--signed=yes");
969 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
970 argv_array_push(&args, "--signed=if-asked");
971 if (options.verbosity == 0)
972 argv_array_push(&args, "--quiet");
973 else if (options.verbosity > 1)
974 argv_array_push(&args, "--verbose");
975 for (i = 0; i < options.push_options.nr; i++)
976 argv_array_pushf(&args, "--push-option=%s",
977 options.push_options.items[i].string);
978 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
979 for_each_string_list_item(cas_option, &cas_options)
980 argv_array_push(&args, cas_option->string);
981 argv_array_push(&args, url.buf);
983 argv_array_push(&args, "--stdin");
984 for (i = 0; i < nr_spec; i++)
985 packet_buf_write(&preamble, "%s\n", specs[i]);
986 packet_buf_flush(&preamble);
988 memset(&rpc, 0, sizeof(rpc));
989 rpc.service_name = "git-receive-pack",
990 rpc.argv = args.argv;
991 rpc.stdin_preamble = &preamble;
993 err = rpc_service(&rpc, heads);
994 if (rpc.result.len)
995 write_or_die(1, rpc.result.buf, rpc.result.len);
996 strbuf_release(&rpc.result);
997 strbuf_release(&preamble);
998 argv_array_clear(&args);
999 return err;
1002 static int push(int nr_spec, char **specs)
1004 struct discovery *heads = discover_refs("git-receive-pack", 1);
1005 int ret;
1007 if (heads->proto_git)
1008 ret = push_git(heads, nr_spec, specs);
1009 else
1010 ret = push_dav(nr_spec, specs);
1011 free_discovery(heads);
1012 return ret;
1015 static void parse_push(struct strbuf *buf)
1017 char **specs = NULL;
1018 int alloc_spec = 0, nr_spec = 0, i, ret;
1020 do {
1021 if (starts_with(buf->buf, "push ")) {
1022 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1023 specs[nr_spec++] = xstrdup(buf->buf + 5);
1025 else
1026 die("http transport does not support %s", buf->buf);
1028 strbuf_reset(buf);
1029 if (strbuf_getline_lf(buf, stdin) == EOF)
1030 goto free_specs;
1031 if (!*buf->buf)
1032 break;
1033 } while (1);
1035 ret = push(nr_spec, specs);
1036 printf("\n");
1037 fflush(stdout);
1039 if (ret)
1040 exit(128); /* error already reported */
1042 free_specs:
1043 for (i = 0; i < nr_spec; i++)
1044 free(specs[i]);
1045 free(specs);
1048 int cmd_main(int argc, const char **argv)
1050 struct strbuf buf = STRBUF_INIT;
1051 int nongit;
1053 setup_git_directory_gently(&nongit);
1054 if (argc < 2) {
1055 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1056 return 1;
1059 options.verbosity = 1;
1060 options.progress = !!isatty(2);
1061 options.thin = 1;
1062 string_list_init(&options.deepen_not, 1);
1063 string_list_init(&options.push_options, 1);
1065 remote = remote_get(argv[1]);
1067 if (argc > 2) {
1068 end_url_with_slash(&url, argv[2]);
1069 } else {
1070 end_url_with_slash(&url, remote->url[0]);
1073 http_init(remote, url.buf, 0);
1075 do {
1076 const char *arg;
1078 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1079 if (ferror(stdin))
1080 error("remote-curl: error reading command stream from git");
1081 return 1;
1083 if (buf.len == 0)
1084 break;
1085 if (starts_with(buf.buf, "fetch ")) {
1086 if (nongit)
1087 die("remote-curl: fetch attempted without a local repo");
1088 parse_fetch(&buf);
1090 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1091 int for_push = !!strstr(buf.buf + 4, "for-push");
1092 output_refs(get_refs(for_push));
1094 } else if (starts_with(buf.buf, "push ")) {
1095 parse_push(&buf);
1097 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1098 char *value = strchr(arg, ' ');
1099 int result;
1101 if (value)
1102 *value++ = '\0';
1103 else
1104 value = "true";
1106 result = set_option(arg, value);
1107 if (!result)
1108 printf("ok\n");
1109 else if (result < 0)
1110 printf("error invalid value\n");
1111 else
1112 printf("unsupported\n");
1113 fflush(stdout);
1115 } else if (!strcmp(buf.buf, "capabilities")) {
1116 printf("fetch\n");
1117 printf("option\n");
1118 printf("push\n");
1119 printf("check-connectivity\n");
1120 printf("\n");
1121 fflush(stdout);
1122 } else {
1123 error("remote-curl: unknown command '%s' from git", buf.buf);
1124 return 1;
1126 strbuf_reset(&buf);
1127 } while (1);
1129 http_cleanup();
1131 return 0;