fast-import: fix over-allocation of marks storage
[git/debian.git] / remote-curl.c
blob32cc4a0c553b3e6f953be247bd593e07222dd5b5
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
8 #include "exec-cmd.h"
9 #include "run-command.h"
10 #include "pkt-line.h"
11 #include "string-list.h"
12 #include "sideband.h"
13 #include "strvec.h"
14 #include "credential.h"
15 #include "oid-array.h"
16 #include "send-pack.h"
17 #include "protocol.h"
18 #include "quote.h"
19 #include "transport.h"
21 static struct remote *remote;
22 /* always ends with a trailing slash */
23 static struct strbuf url = STRBUF_INIT;
25 struct options {
26 int verbosity;
27 unsigned long depth;
28 char *deepen_since;
29 struct string_list deepen_not;
30 struct string_list push_options;
31 char *filter;
32 unsigned progress : 1,
33 check_self_contained_and_connected : 1,
34 cloning : 1,
35 update_shallow : 1,
36 followtags : 1,
37 dry_run : 1,
38 thin : 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 push_cert : 2,
41 deepen_relative : 1,
43 /* see documentation of corresponding flag in fetch-pack.h */
44 from_promisor : 1,
46 atomic : 1,
47 object_format : 1;
48 const struct git_hash_algo *hash_algo;
50 static struct options options;
51 static struct string_list cas_options = STRING_LIST_INIT_DUP;
53 static int set_option(const char *name, const char *value)
55 if (!strcmp(name, "verbosity")) {
56 char *end;
57 int v = strtol(value, &end, 10);
58 if (value == end || *end)
59 return -1;
60 options.verbosity = v;
61 return 0;
63 else if (!strcmp(name, "progress")) {
64 if (!strcmp(value, "true"))
65 options.progress = 1;
66 else if (!strcmp(value, "false"))
67 options.progress = 0;
68 else
69 return -1;
70 return 0;
72 else if (!strcmp(name, "depth")) {
73 char *end;
74 unsigned long v = strtoul(value, &end, 10);
75 if (value == end || *end)
76 return -1;
77 options.depth = v;
78 return 0;
80 else if (!strcmp(name, "deepen-since")) {
81 options.deepen_since = xstrdup(value);
82 return 0;
84 else if (!strcmp(name, "deepen-not")) {
85 string_list_append(&options.deepen_not, value);
86 return 0;
88 else if (!strcmp(name, "deepen-relative")) {
89 if (!strcmp(value, "true"))
90 options.deepen_relative = 1;
91 else if (!strcmp(value, "false"))
92 options.deepen_relative = 0;
93 else
94 return -1;
95 return 0;
97 else if (!strcmp(name, "followtags")) {
98 if (!strcmp(value, "true"))
99 options.followtags = 1;
100 else if (!strcmp(value, "false"))
101 options.followtags = 0;
102 else
103 return -1;
104 return 0;
106 else if (!strcmp(name, "dry-run")) {
107 if (!strcmp(value, "true"))
108 options.dry_run = 1;
109 else if (!strcmp(value, "false"))
110 options.dry_run = 0;
111 else
112 return -1;
113 return 0;
115 else if (!strcmp(name, "check-connectivity")) {
116 if (!strcmp(value, "true"))
117 options.check_self_contained_and_connected = 1;
118 else if (!strcmp(value, "false"))
119 options.check_self_contained_and_connected = 0;
120 else
121 return -1;
122 return 0;
124 else if (!strcmp(name, "cas")) {
125 struct strbuf val = STRBUF_INIT;
126 strbuf_addstr(&val, "--force-with-lease=");
127 if (*value != '"')
128 strbuf_addstr(&val, value);
129 else if (unquote_c_style(&val, value, NULL))
130 return -1;
131 string_list_append(&cas_options, val.buf);
132 strbuf_release(&val);
133 return 0;
134 } else if (!strcmp(name, "cloning")) {
135 if (!strcmp(value, "true"))
136 options.cloning = 1;
137 else if (!strcmp(value, "false"))
138 options.cloning = 0;
139 else
140 return -1;
141 return 0;
142 } else if (!strcmp(name, "update-shallow")) {
143 if (!strcmp(value, "true"))
144 options.update_shallow = 1;
145 else if (!strcmp(value, "false"))
146 options.update_shallow = 0;
147 else
148 return -1;
149 return 0;
150 } else if (!strcmp(name, "pushcert")) {
151 if (!strcmp(value, "true"))
152 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
153 else if (!strcmp(value, "false"))
154 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
155 else if (!strcmp(value, "if-asked"))
156 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
157 else
158 return -1;
159 return 0;
160 } else if (!strcmp(name, "atomic")) {
161 if (!strcmp(value, "true"))
162 options.atomic = 1;
163 else if (!strcmp(value, "false"))
164 options.atomic = 0;
165 else
166 return -1;
167 return 0;
168 } else if (!strcmp(name, "push-option")) {
169 if (*value != '"')
170 string_list_append(&options.push_options, value);
171 else {
172 struct strbuf unquoted = STRBUF_INIT;
173 if (unquote_c_style(&unquoted, value, NULL) < 0)
174 die(_("invalid quoting in push-option value: '%s'"), value);
175 string_list_append_nodup(&options.push_options,
176 strbuf_detach(&unquoted, NULL));
178 return 0;
180 #if LIBCURL_VERSION_NUM >= 0x070a08
181 } else if (!strcmp(name, "family")) {
182 if (!strcmp(value, "ipv4"))
183 git_curl_ipresolve = CURL_IPRESOLVE_V4;
184 else if (!strcmp(value, "ipv6"))
185 git_curl_ipresolve = CURL_IPRESOLVE_V6;
186 else if (!strcmp(value, "all"))
187 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
188 else
189 return -1;
190 return 0;
191 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
192 } else if (!strcmp(name, "from-promisor")) {
193 options.from_promisor = 1;
194 return 0;
195 } else if (!strcmp(name, "filter")) {
196 options.filter = xstrdup(value);
197 return 0;
198 } else if (!strcmp(name, "object-format")) {
199 int algo;
200 options.object_format = 1;
201 if (strcmp(value, "true")) {
202 algo = hash_algo_by_name(value);
203 if (algo == GIT_HASH_UNKNOWN)
204 die("unknown object format '%s'", value);
205 options.hash_algo = &hash_algos[algo];
207 return 0;
208 } else {
209 return 1 /* unsupported */;
213 struct discovery {
214 char *service;
215 char *buf_alloc;
216 char *buf;
217 size_t len;
218 struct ref *refs;
219 struct oid_array shallow;
220 enum protocol_version version;
221 unsigned proto_git : 1;
223 static struct discovery *last_discovery;
225 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
227 struct ref *list = NULL;
228 struct packet_reader reader;
230 packet_reader_init(&reader, -1, heads->buf, heads->len,
231 PACKET_READ_CHOMP_NEWLINE |
232 PACKET_READ_GENTLE_ON_EOF |
233 PACKET_READ_DIE_ON_ERR_PACKET);
235 heads->version = discover_version(&reader);
236 switch (heads->version) {
237 case protocol_v2:
239 * Do nothing. This isn't a list of refs but rather a
240 * capability advertisement. Client would have run
241 * 'stateless-connect' so we'll dump this capability listing
242 * and let them request the refs themselves.
244 break;
245 case protocol_v1:
246 case protocol_v0:
247 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
248 NULL, &heads->shallow);
249 options.hash_algo = reader.hash_algo;
250 break;
251 case protocol_unknown_version:
252 BUG("unknown protocol version");
255 return list;
258 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
260 const char *p = memchr(heads->buf, '\t', heads->len);
261 int algo;
262 if (!p)
263 return the_hash_algo;
265 algo = hash_algo_by_length((p - heads->buf) / 2);
266 if (algo == GIT_HASH_UNKNOWN)
267 return NULL;
268 return &hash_algos[algo];
271 static struct ref *parse_info_refs(struct discovery *heads)
273 char *data, *start, *mid;
274 char *ref_name;
275 int i = 0;
277 struct ref *refs = NULL;
278 struct ref *ref = NULL;
279 struct ref *last_ref = NULL;
281 options.hash_algo = detect_hash_algo(heads);
282 if (!options.hash_algo)
283 die("%sinfo/refs not valid: could not determine hash algorithm; "
284 "is this a git repository?",
285 transport_anonymize_url(url.buf));
287 data = heads->buf;
288 start = NULL;
289 mid = data;
290 while (i < heads->len) {
291 if (!start) {
292 start = &data[i];
294 if (data[i] == '\t')
295 mid = &data[i];
296 if (data[i] == '\n') {
297 if (mid - start != options.hash_algo->hexsz)
298 die(_("%sinfo/refs not valid: is this a git repository?"),
299 transport_anonymize_url(url.buf));
300 data[i] = 0;
301 ref_name = mid + 1;
302 ref = alloc_ref(ref_name);
303 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
304 if (!refs)
305 refs = ref;
306 if (last_ref)
307 last_ref->next = ref;
308 last_ref = ref;
309 start = NULL;
311 i++;
314 ref = alloc_ref("HEAD");
315 if (!http_fetch_ref(url.buf, ref) &&
316 !resolve_remote_symref(ref, refs)) {
317 ref->next = refs;
318 refs = ref;
319 } else {
320 free(ref);
323 return refs;
326 static void free_discovery(struct discovery *d)
328 if (d) {
329 if (d == last_discovery)
330 last_discovery = NULL;
331 free(d->shallow.oid);
332 free(d->buf_alloc);
333 free_refs(d->refs);
334 free(d->service);
335 free(d);
339 static int show_http_message(struct strbuf *type, struct strbuf *charset,
340 struct strbuf *msg)
342 const char *p, *eol;
345 * We only show text/plain parts, as other types are likely
346 * to be ugly to look at on the user's terminal.
348 if (strcmp(type->buf, "text/plain"))
349 return -1;
350 if (charset->len)
351 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
353 strbuf_trim(msg);
354 if (!msg->len)
355 return -1;
357 p = msg->buf;
358 do {
359 eol = strchrnul(p, '\n');
360 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
361 p = eol + 1;
362 } while(*eol);
363 return 0;
366 static int get_protocol_http_header(enum protocol_version version,
367 struct strbuf *header)
369 if (version > 0) {
370 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
371 version);
373 return 1;
376 return 0;
379 static void check_smart_http(struct discovery *d, const char *service,
380 struct strbuf *type)
382 const char *p;
383 struct packet_reader reader;
386 * If we don't see x-$service-advertisement, then it's not smart-http.
387 * But once we do, we commit to it and assume any other protocol
388 * violations are hard errors.
390 if (!skip_prefix(type->buf, "application/x-", &p) ||
391 !skip_prefix(p, service, &p) ||
392 strcmp(p, "-advertisement"))
393 return;
395 packet_reader_init(&reader, -1, d->buf, d->len,
396 PACKET_READ_CHOMP_NEWLINE |
397 PACKET_READ_DIE_ON_ERR_PACKET);
398 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
399 die(_("invalid server response; expected service, got flush packet"));
401 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
403 * The header can include additional metadata lines, up
404 * until a packet flush marker. Ignore these now, but
405 * in the future we might start to scan them.
407 for (;;) {
408 packet_reader_read(&reader);
409 if (reader.pktlen <= 0) {
410 break;
415 * v0 smart http; callers expect us to soak up the
416 * service and header packets
418 d->buf = reader.src_buffer;
419 d->len = reader.src_len;
420 d->proto_git = 1;
422 } else if (!strcmp(reader.line, "version 2")) {
424 * v2 smart http; do not consume version packet, which will
425 * be handled elsewhere.
427 d->proto_git = 1;
429 } else {
430 die(_("invalid server response; got '%s'"), reader.line);
434 static struct discovery *discover_refs(const char *service, int for_push)
436 struct strbuf type = STRBUF_INIT;
437 struct strbuf charset = STRBUF_INIT;
438 struct strbuf buffer = STRBUF_INIT;
439 struct strbuf refs_url = STRBUF_INIT;
440 struct strbuf effective_url = STRBUF_INIT;
441 struct strbuf protocol_header = STRBUF_INIT;
442 struct string_list extra_headers = STRING_LIST_INIT_DUP;
443 struct discovery *last = last_discovery;
444 int http_ret, maybe_smart = 0;
445 struct http_get_options http_options;
446 enum protocol_version version = get_protocol_version_config();
448 if (last && !strcmp(service, last->service))
449 return last;
450 free_discovery(last);
452 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
453 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
454 git_env_bool("GIT_SMART_HTTP", 1)) {
455 maybe_smart = 1;
456 if (!strchr(url.buf, '?'))
457 strbuf_addch(&refs_url, '?');
458 else
459 strbuf_addch(&refs_url, '&');
460 strbuf_addf(&refs_url, "service=%s", service);
464 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
465 * to perform a push, then fallback to v0 since the client doesn't know
466 * how to push yet using v2.
468 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
469 version = protocol_v0;
471 /* Add the extra Git-Protocol header */
472 if (get_protocol_http_header(version, &protocol_header))
473 string_list_append(&extra_headers, protocol_header.buf);
475 memset(&http_options, 0, sizeof(http_options));
476 http_options.content_type = &type;
477 http_options.charset = &charset;
478 http_options.effective_url = &effective_url;
479 http_options.base_url = &url;
480 http_options.extra_headers = &extra_headers;
481 http_options.initial_request = 1;
482 http_options.no_cache = 1;
484 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
485 switch (http_ret) {
486 case HTTP_OK:
487 break;
488 case HTTP_MISSING_TARGET:
489 show_http_message(&type, &charset, &buffer);
490 die(_("repository '%s' not found"),
491 transport_anonymize_url(url.buf));
492 case HTTP_NOAUTH:
493 show_http_message(&type, &charset, &buffer);
494 die(_("Authentication failed for '%s'"),
495 transport_anonymize_url(url.buf));
496 default:
497 show_http_message(&type, &charset, &buffer);
498 die(_("unable to access '%s': %s"),
499 transport_anonymize_url(url.buf), curl_errorstr);
502 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
503 char *u = transport_anonymize_url(url.buf);
504 warning(_("redirecting to %s"), u);
505 free(u);
508 last= xcalloc(1, sizeof(*last_discovery));
509 last->service = xstrdup(service);
510 last->buf_alloc = strbuf_detach(&buffer, &last->len);
511 last->buf = last->buf_alloc;
513 if (maybe_smart)
514 check_smart_http(last, service, &type);
516 if (last->proto_git)
517 last->refs = parse_git_refs(last, for_push);
518 else
519 last->refs = parse_info_refs(last);
521 strbuf_release(&refs_url);
522 strbuf_release(&type);
523 strbuf_release(&charset);
524 strbuf_release(&effective_url);
525 strbuf_release(&buffer);
526 strbuf_release(&protocol_header);
527 string_list_clear(&extra_headers, 0);
528 last_discovery = last;
529 return last;
532 static struct ref *get_refs(int for_push)
534 struct discovery *heads;
536 if (for_push)
537 heads = discover_refs("git-receive-pack", for_push);
538 else
539 heads = discover_refs("git-upload-pack", for_push);
541 return heads->refs;
544 static void output_refs(struct ref *refs)
546 struct ref *posn;
547 if (options.object_format && options.hash_algo) {
548 printf(":object-format %s\n", options.hash_algo->name);
550 for (posn = refs; posn; posn = posn->next) {
551 if (posn->symref)
552 printf("@%s %s\n", posn->symref, posn->name);
553 else
554 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
555 options.hash_algo),
556 posn->name);
558 printf("\n");
559 fflush(stdout);
562 struct rpc_state {
563 const char *service_name;
564 char *service_url;
565 char *hdr_content_type;
566 char *hdr_accept;
567 char *protocol_header;
568 char *buf;
569 size_t alloc;
570 size_t len;
571 size_t pos;
572 int in;
573 int out;
574 int any_written;
575 unsigned gzip_request : 1;
576 unsigned initial_buffer : 1;
579 * Whenever a pkt-line is read into buf, append the 4 characters
580 * denoting its length before appending the payload.
582 unsigned write_line_lengths : 1;
585 * Used by rpc_out; initialize to 0. This is true if a flush has been
586 * read, but the corresponding line length (if write_line_lengths is
587 * true) and EOF have not been sent to libcurl. Since each flush marks
588 * the end of a request, each flush must be completely sent before any
589 * further reading occurs.
591 unsigned flush_read_but_not_sent : 1;
595 * Appends the result of reading from rpc->out to the string represented by
596 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
597 * enough space, 0 otherwise.
599 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
600 * hexadecimal string before appending the result described above.
602 * Writes the total number of bytes appended into appended.
604 static int rpc_read_from_out(struct rpc_state *rpc, int options,
605 size_t *appended,
606 enum packet_read_status *status) {
607 size_t left;
608 char *buf;
609 int pktlen_raw;
611 if (rpc->write_line_lengths) {
612 left = rpc->alloc - rpc->len - 4;
613 buf = rpc->buf + rpc->len + 4;
614 } else {
615 left = rpc->alloc - rpc->len;
616 buf = rpc->buf + rpc->len;
619 if (left < LARGE_PACKET_MAX)
620 return 0;
622 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
623 left, &pktlen_raw, options);
624 if (*status != PACKET_READ_EOF) {
625 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
626 rpc->len += *appended;
629 if (rpc->write_line_lengths) {
630 switch (*status) {
631 case PACKET_READ_EOF:
632 if (!(options & PACKET_READ_GENTLE_ON_EOF))
633 die(_("shouldn't have EOF when not gentle on EOF"));
634 break;
635 case PACKET_READ_NORMAL:
636 set_packet_header(buf - 4, *appended);
637 break;
638 case PACKET_READ_DELIM:
639 memcpy(buf - 4, "0001", 4);
640 break;
641 case PACKET_READ_FLUSH:
642 memcpy(buf - 4, "0000", 4);
643 break;
644 case PACKET_READ_RESPONSE_END:
645 die(_("remote server sent stateless separator"));
649 return 1;
652 static size_t rpc_out(void *ptr, size_t eltsize,
653 size_t nmemb, void *buffer_)
655 size_t max = eltsize * nmemb;
656 struct rpc_state *rpc = buffer_;
657 size_t avail = rpc->len - rpc->pos;
658 enum packet_read_status status;
660 if (!avail) {
661 rpc->initial_buffer = 0;
662 rpc->len = 0;
663 rpc->pos = 0;
664 if (!rpc->flush_read_but_not_sent) {
665 if (!rpc_read_from_out(rpc, 0, &avail, &status))
666 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
667 if (status == PACKET_READ_FLUSH)
668 rpc->flush_read_but_not_sent = 1;
671 * If flush_read_but_not_sent is true, we have already read one
672 * full request but have not fully sent it + EOF, which is why
673 * we need to refrain from reading.
676 if (rpc->flush_read_but_not_sent) {
677 if (!avail) {
679 * The line length either does not need to be sent at
680 * all or has already been completely sent. Now we can
681 * return 0, indicating EOF, meaning that the flush has
682 * been fully sent.
684 rpc->flush_read_but_not_sent = 0;
685 return 0;
688 * If avail is non-zero, the line length for the flush still
689 * hasn't been fully sent. Proceed with sending the line
690 * length.
694 if (max < avail)
695 avail = max;
696 memcpy(ptr, rpc->buf + rpc->pos, avail);
697 rpc->pos += avail;
698 return avail;
701 #ifndef NO_CURL_IOCTL
702 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
704 struct rpc_state *rpc = clientp;
706 switch (cmd) {
707 case CURLIOCMD_NOP:
708 return CURLIOE_OK;
710 case CURLIOCMD_RESTARTREAD:
711 if (rpc->initial_buffer) {
712 rpc->pos = 0;
713 return CURLIOE_OK;
715 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
716 return CURLIOE_FAILRESTART;
718 default:
719 return CURLIOE_UNKNOWNCMD;
722 #endif
724 struct check_pktline_state {
725 char len_buf[4];
726 int len_filled;
727 int remaining;
730 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
732 while (size) {
733 if (!state->remaining) {
734 int digits_remaining = 4 - state->len_filled;
735 if (digits_remaining > size)
736 digits_remaining = size;
737 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
738 state->len_filled += digits_remaining;
739 ptr += digits_remaining;
740 size -= digits_remaining;
742 if (state->len_filled == 4) {
743 state->remaining = packet_length(state->len_buf);
744 if (state->remaining < 0) {
745 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
746 } else if (state->remaining == 2) {
747 die(_("remote-curl: unexpected response end packet"));
748 } else if (state->remaining < 4) {
749 state->remaining = 0;
750 } else {
751 state->remaining -= 4;
753 state->len_filled = 0;
757 if (state->remaining) {
758 int remaining = state->remaining;
759 if (remaining > size)
760 remaining = size;
761 ptr += remaining;
762 size -= remaining;
763 state->remaining -= remaining;
768 struct rpc_in_data {
769 struct rpc_state *rpc;
770 struct active_request_slot *slot;
771 int check_pktline;
772 struct check_pktline_state pktline_state;
776 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
777 * from ptr.
779 static size_t rpc_in(char *ptr, size_t eltsize,
780 size_t nmemb, void *buffer_)
782 size_t size = eltsize * nmemb;
783 struct rpc_in_data *data = buffer_;
784 long response_code;
786 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
787 &response_code) != CURLE_OK)
788 return size;
789 if (response_code >= 300)
790 return size;
791 if (size)
792 data->rpc->any_written = 1;
793 if (data->check_pktline)
794 check_pktline(&data->pktline_state, ptr, size);
795 write_or_die(data->rpc->in, ptr, size);
796 return size;
799 static int run_slot(struct active_request_slot *slot,
800 struct slot_results *results)
802 int err;
803 struct slot_results results_buf;
805 if (!results)
806 results = &results_buf;
808 err = run_one_slot(slot, results);
810 if (err != HTTP_OK && err != HTTP_REAUTH) {
811 struct strbuf msg = STRBUF_INIT;
812 if (results->http_code && results->http_code != 200)
813 strbuf_addf(&msg, "HTTP %ld", results->http_code);
814 if (results->curl_result != CURLE_OK) {
815 if (msg.len)
816 strbuf_addch(&msg, ' ');
817 strbuf_addf(&msg, "curl %d", results->curl_result);
818 if (curl_errorstr[0]) {
819 strbuf_addch(&msg, ' ');
820 strbuf_addstr(&msg, curl_errorstr);
823 error(_("RPC failed; %s"), msg.buf);
824 strbuf_release(&msg);
827 return err;
830 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
832 struct active_request_slot *slot;
833 struct curl_slist *headers = http_copy_default_headers();
834 struct strbuf buf = STRBUF_INIT;
835 int err;
837 slot = get_active_slot();
839 headers = curl_slist_append(headers, rpc->hdr_content_type);
840 headers = curl_slist_append(headers, rpc->hdr_accept);
842 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
843 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
844 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
845 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
846 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
847 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
848 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
849 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
850 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
852 err = run_slot(slot, results);
854 curl_slist_free_all(headers);
855 strbuf_release(&buf);
856 return err;
859 static curl_off_t xcurl_off_t(size_t len)
861 uintmax_t size = len;
862 if (size > maximum_signed_value_of_type(curl_off_t))
863 die(_("cannot handle pushes this big"));
864 return (curl_off_t)size;
868 * If flush_received is true, do not attempt to read any more; just use what's
869 * in rpc->buf.
871 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
873 struct active_request_slot *slot;
874 struct curl_slist *headers = http_copy_default_headers();
875 int use_gzip = rpc->gzip_request;
876 char *gzip_body = NULL;
877 size_t gzip_size = 0;
878 int err, large_request = 0;
879 int needs_100_continue = 0;
880 struct rpc_in_data rpc_in_data;
882 /* Try to load the entire request, if we can fit it into the
883 * allocated buffer space we can use HTTP/1.0 and avoid the
884 * chunked encoding mess.
886 if (!flush_received) {
887 while (1) {
888 size_t n;
889 enum packet_read_status status;
891 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
892 large_request = 1;
893 use_gzip = 0;
894 break;
896 if (status == PACKET_READ_FLUSH)
897 break;
901 if (large_request) {
902 struct slot_results results;
904 do {
905 err = probe_rpc(rpc, &results);
906 if (err == HTTP_REAUTH)
907 credential_fill(&http_auth);
908 } while (err == HTTP_REAUTH);
909 if (err != HTTP_OK)
910 return -1;
912 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
913 needs_100_continue = 1;
916 headers = curl_slist_append(headers, rpc->hdr_content_type);
917 headers = curl_slist_append(headers, rpc->hdr_accept);
918 headers = curl_slist_append(headers, needs_100_continue ?
919 "Expect: 100-continue" : "Expect:");
921 /* Add the extra Git-Protocol header */
922 if (rpc->protocol_header)
923 headers = curl_slist_append(headers, rpc->protocol_header);
925 retry:
926 slot = get_active_slot();
928 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
929 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
930 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
931 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
933 if (large_request) {
934 /* The request body is large and the size cannot be predicted.
935 * We must use chunked encoding to send it.
937 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
938 rpc->initial_buffer = 1;
939 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
940 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
941 #ifndef NO_CURL_IOCTL
942 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
943 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
944 #endif
945 if (options.verbosity > 1) {
946 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
947 fflush(stderr);
950 } else if (gzip_body) {
952 * If we are looping to retry authentication, then the previous
953 * run will have set up the headers and gzip buffer already,
954 * and we just need to send it.
956 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
957 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
959 } else if (use_gzip && 1024 < rpc->len) {
960 /* The client backend isn't giving us compressed data so
961 * we can try to deflate it ourselves, this may save on
962 * the transfer time.
964 git_zstream stream;
965 int ret;
967 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
968 gzip_size = git_deflate_bound(&stream, rpc->len);
969 gzip_body = xmalloc(gzip_size);
971 stream.next_in = (unsigned char *)rpc->buf;
972 stream.avail_in = rpc->len;
973 stream.next_out = (unsigned char *)gzip_body;
974 stream.avail_out = gzip_size;
976 ret = git_deflate(&stream, Z_FINISH);
977 if (ret != Z_STREAM_END)
978 die(_("cannot deflate request; zlib deflate error %d"), ret);
980 ret = git_deflate_end_gently(&stream);
981 if (ret != Z_OK)
982 die(_("cannot deflate request; zlib end error %d"), ret);
984 gzip_size = stream.total_out;
986 headers = curl_slist_append(headers, "Content-Encoding: gzip");
987 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
988 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
990 if (options.verbosity > 1) {
991 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
992 rpc->service_name,
993 (unsigned long)rpc->len, (unsigned long)gzip_size);
994 fflush(stderr);
996 } else {
997 /* We know the complete request size in advance, use the
998 * more normal Content-Length approach.
1000 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1001 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1002 if (options.verbosity > 1) {
1003 fprintf(stderr, "POST %s (%lu bytes)\n",
1004 rpc->service_name, (unsigned long)rpc->len);
1005 fflush(stderr);
1009 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1010 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1011 rpc_in_data.rpc = rpc;
1012 rpc_in_data.slot = slot;
1013 rpc_in_data.check_pktline = stateless_connect;
1014 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1015 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
1016 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1019 rpc->any_written = 0;
1020 err = run_slot(slot, NULL);
1021 if (err == HTTP_REAUTH && !large_request) {
1022 credential_fill(&http_auth);
1023 goto retry;
1025 if (err != HTTP_OK)
1026 err = -1;
1028 if (!rpc->any_written)
1029 err = -1;
1031 if (rpc_in_data.pktline_state.len_filled)
1032 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1033 if (rpc_in_data.pktline_state.remaining)
1034 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1036 if (stateless_connect)
1037 packet_response_end(rpc->in);
1039 curl_slist_free_all(headers);
1040 free(gzip_body);
1041 return err;
1044 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1045 const char **client_argv, const struct strbuf *preamble,
1046 struct strbuf *rpc_result)
1048 const char *svc = rpc->service_name;
1049 struct strbuf buf = STRBUF_INIT;
1050 struct child_process client = CHILD_PROCESS_INIT;
1051 int err = 0;
1053 client.in = -1;
1054 client.out = -1;
1055 client.git_cmd = 1;
1056 client.argv = client_argv;
1057 if (start_command(&client))
1058 exit(1);
1059 write_or_die(client.in, preamble->buf, preamble->len);
1060 if (heads)
1061 write_or_die(client.in, heads->buf, heads->len);
1063 rpc->alloc = http_post_buffer;
1064 rpc->buf = xmalloc(rpc->alloc);
1065 rpc->in = client.in;
1066 rpc->out = client.out;
1068 strbuf_addf(&buf, "%s%s", url.buf, svc);
1069 rpc->service_url = strbuf_detach(&buf, NULL);
1071 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1072 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1074 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1075 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1077 if (get_protocol_http_header(heads->version, &buf))
1078 rpc->protocol_header = strbuf_detach(&buf, NULL);
1079 else
1080 rpc->protocol_header = NULL;
1082 while (!err) {
1083 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
1084 if (!n)
1085 break;
1086 rpc->pos = 0;
1087 rpc->len = n;
1088 err |= post_rpc(rpc, 0, 0);
1091 close(client.in);
1092 client.in = -1;
1093 if (!err) {
1094 strbuf_read(rpc_result, client.out, 0);
1095 } else {
1096 char buf[4096];
1097 for (;;)
1098 if (xread(client.out, buf, sizeof(buf)) <= 0)
1099 break;
1102 close(client.out);
1103 client.out = -1;
1105 err |= finish_command(&client);
1106 free(rpc->service_url);
1107 free(rpc->hdr_content_type);
1108 free(rpc->hdr_accept);
1109 free(rpc->protocol_header);
1110 free(rpc->buf);
1111 strbuf_release(&buf);
1112 return err;
1115 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1117 struct walker *walker;
1118 char **targets;
1119 int ret, i;
1121 ALLOC_ARRAY(targets, nr_heads);
1122 if (options.depth || options.deepen_since)
1123 die(_("dumb http transport does not support shallow capabilities"));
1124 for (i = 0; i < nr_heads; i++)
1125 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1127 walker = get_http_walker(url.buf);
1128 walker->get_verbosely = options.verbosity >= 3;
1129 walker->get_progress = options.progress;
1130 walker->get_recover = 0;
1131 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1132 walker_free(walker);
1134 for (i = 0; i < nr_heads; i++)
1135 free(targets[i]);
1136 free(targets);
1138 return ret ? error(_("fetch failed.")) : 0;
1141 static int fetch_git(struct discovery *heads,
1142 int nr_heads, struct ref **to_fetch)
1144 struct rpc_state rpc;
1145 struct strbuf preamble = STRBUF_INIT;
1146 int i, err;
1147 struct strvec args = STRVEC_INIT;
1148 struct strbuf rpc_result = STRBUF_INIT;
1150 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1151 "--stdin", "--lock-pack", NULL);
1152 if (options.followtags)
1153 strvec_push(&args, "--include-tag");
1154 if (options.thin)
1155 strvec_push(&args, "--thin");
1156 if (options.verbosity >= 3)
1157 strvec_pushl(&args, "-v", "-v", NULL);
1158 if (options.check_self_contained_and_connected)
1159 strvec_push(&args, "--check-self-contained-and-connected");
1160 if (options.cloning)
1161 strvec_push(&args, "--cloning");
1162 if (options.update_shallow)
1163 strvec_push(&args, "--update-shallow");
1164 if (!options.progress)
1165 strvec_push(&args, "--no-progress");
1166 if (options.depth)
1167 strvec_pushf(&args, "--depth=%lu", options.depth);
1168 if (options.deepen_since)
1169 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1170 for (i = 0; i < options.deepen_not.nr; i++)
1171 strvec_pushf(&args, "--shallow-exclude=%s",
1172 options.deepen_not.items[i].string);
1173 if (options.deepen_relative && options.depth)
1174 strvec_push(&args, "--deepen-relative");
1175 if (options.from_promisor)
1176 strvec_push(&args, "--from-promisor");
1177 if (options.filter)
1178 strvec_pushf(&args, "--filter=%s", options.filter);
1179 strvec_push(&args, url.buf);
1181 for (i = 0; i < nr_heads; i++) {
1182 struct ref *ref = to_fetch[i];
1183 if (!*ref->name)
1184 die(_("cannot fetch by sha1 over smart http"));
1185 packet_buf_write(&preamble, "%s %s\n",
1186 oid_to_hex(&ref->old_oid), ref->name);
1188 packet_buf_flush(&preamble);
1190 memset(&rpc, 0, sizeof(rpc));
1191 rpc.service_name = "git-upload-pack",
1192 rpc.gzip_request = 1;
1194 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1195 if (rpc_result.len)
1196 write_or_die(1, rpc_result.buf, rpc_result.len);
1197 strbuf_release(&rpc_result);
1198 strbuf_release(&preamble);
1199 strvec_clear(&args);
1200 return err;
1203 static int fetch(int nr_heads, struct ref **to_fetch)
1205 struct discovery *d = discover_refs("git-upload-pack", 0);
1206 if (d->proto_git)
1207 return fetch_git(d, nr_heads, to_fetch);
1208 else
1209 return fetch_dumb(nr_heads, to_fetch);
1212 static void parse_fetch(struct strbuf *buf)
1214 struct ref **to_fetch = NULL;
1215 struct ref *list_head = NULL;
1216 struct ref **list = &list_head;
1217 int alloc_heads = 0, nr_heads = 0;
1219 do {
1220 const char *p;
1221 if (skip_prefix(buf->buf, "fetch ", &p)) {
1222 const char *name;
1223 struct ref *ref;
1224 struct object_id old_oid;
1225 const char *q;
1227 if (parse_oid_hex(p, &old_oid, &q))
1228 die(_("protocol error: expected sha/ref, got '%s'"), p);
1229 if (*q == ' ')
1230 name = q + 1;
1231 else if (!*q)
1232 name = "";
1233 else
1234 die(_("protocol error: expected sha/ref, got '%s'"), p);
1236 ref = alloc_ref(name);
1237 oidcpy(&ref->old_oid, &old_oid);
1239 *list = ref;
1240 list = &ref->next;
1242 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1243 to_fetch[nr_heads++] = ref;
1245 else
1246 die(_("http transport does not support %s"), buf->buf);
1248 strbuf_reset(buf);
1249 if (strbuf_getline_lf(buf, stdin) == EOF)
1250 return;
1251 if (!*buf->buf)
1252 break;
1253 } while (1);
1255 if (fetch(nr_heads, to_fetch))
1256 exit(128); /* error already reported */
1257 free_refs(list_head);
1258 free(to_fetch);
1260 printf("\n");
1261 fflush(stdout);
1262 strbuf_reset(buf);
1265 static int push_dav(int nr_spec, const char **specs)
1267 struct child_process child = CHILD_PROCESS_INIT;
1268 size_t i;
1270 child.git_cmd = 1;
1271 strvec_push(&child.args, "http-push");
1272 strvec_push(&child.args, "--helper-status");
1273 if (options.dry_run)
1274 strvec_push(&child.args, "--dry-run");
1275 if (options.verbosity > 1)
1276 strvec_push(&child.args, "--verbose");
1277 strvec_push(&child.args, url.buf);
1278 for (i = 0; i < nr_spec; i++)
1279 strvec_push(&child.args, specs[i]);
1281 if (run_command(&child))
1282 die(_("git-http-push failed"));
1283 return 0;
1286 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1288 struct rpc_state rpc;
1289 int i, err;
1290 struct strvec args;
1291 struct string_list_item *cas_option;
1292 struct strbuf preamble = STRBUF_INIT;
1293 struct strbuf rpc_result = STRBUF_INIT;
1295 strvec_init(&args);
1296 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1297 NULL);
1299 if (options.thin)
1300 strvec_push(&args, "--thin");
1301 if (options.dry_run)
1302 strvec_push(&args, "--dry-run");
1303 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1304 strvec_push(&args, "--signed=yes");
1305 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1306 strvec_push(&args, "--signed=if-asked");
1307 if (options.atomic)
1308 strvec_push(&args, "--atomic");
1309 if (options.verbosity == 0)
1310 strvec_push(&args, "--quiet");
1311 else if (options.verbosity > 1)
1312 strvec_push(&args, "--verbose");
1313 for (i = 0; i < options.push_options.nr; i++)
1314 strvec_pushf(&args, "--push-option=%s",
1315 options.push_options.items[i].string);
1316 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1317 for_each_string_list_item(cas_option, &cas_options)
1318 strvec_push(&args, cas_option->string);
1319 strvec_push(&args, url.buf);
1321 strvec_push(&args, "--stdin");
1322 for (i = 0; i < nr_spec; i++)
1323 packet_buf_write(&preamble, "%s\n", specs[i]);
1324 packet_buf_flush(&preamble);
1326 memset(&rpc, 0, sizeof(rpc));
1327 rpc.service_name = "git-receive-pack",
1329 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1330 if (rpc_result.len)
1331 write_or_die(1, rpc_result.buf, rpc_result.len);
1332 strbuf_release(&rpc_result);
1333 strbuf_release(&preamble);
1334 strvec_clear(&args);
1335 return err;
1338 static int push(int nr_spec, const char **specs)
1340 struct discovery *heads = discover_refs("git-receive-pack", 1);
1341 int ret;
1343 if (heads->proto_git)
1344 ret = push_git(heads, nr_spec, specs);
1345 else
1346 ret = push_dav(nr_spec, specs);
1347 free_discovery(heads);
1348 return ret;
1351 static void parse_push(struct strbuf *buf)
1353 struct strvec specs = STRVEC_INIT;
1354 int ret;
1356 do {
1357 const char *arg;
1358 if (skip_prefix(buf->buf, "push ", &arg))
1359 strvec_push(&specs, arg);
1360 else
1361 die(_("http transport does not support %s"), buf->buf);
1363 strbuf_reset(buf);
1364 if (strbuf_getline_lf(buf, stdin) == EOF)
1365 goto free_specs;
1366 if (!*buf->buf)
1367 break;
1368 } while (1);
1370 ret = push(specs.nr, specs.v);
1371 printf("\n");
1372 fflush(stdout);
1374 if (ret)
1375 exit(128); /* error already reported */
1377 free_specs:
1378 strvec_clear(&specs);
1381 static int stateless_connect(const char *service_name)
1383 struct discovery *discover;
1384 struct rpc_state rpc;
1385 struct strbuf buf = STRBUF_INIT;
1388 * Run the info/refs request and see if the server supports protocol
1389 * v2. If and only if the server supports v2 can we successfully
1390 * establish a stateless connection, otherwise we need to tell the
1391 * client to fallback to using other transport helper functions to
1392 * complete their request.
1394 discover = discover_refs(service_name, 0);
1395 if (discover->version != protocol_v2) {
1396 printf("fallback\n");
1397 fflush(stdout);
1398 return -1;
1399 } else {
1400 /* Stateless Connection established */
1401 printf("\n");
1402 fflush(stdout);
1405 rpc.service_name = service_name;
1406 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1407 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1408 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1409 if (get_protocol_http_header(discover->version, &buf)) {
1410 rpc.protocol_header = strbuf_detach(&buf, NULL);
1411 } else {
1412 rpc.protocol_header = NULL;
1413 strbuf_release(&buf);
1415 rpc.buf = xmalloc(http_post_buffer);
1416 rpc.alloc = http_post_buffer;
1417 rpc.len = 0;
1418 rpc.pos = 0;
1419 rpc.in = 1;
1420 rpc.out = 0;
1421 rpc.any_written = 0;
1422 rpc.gzip_request = 1;
1423 rpc.initial_buffer = 0;
1424 rpc.write_line_lengths = 1;
1425 rpc.flush_read_but_not_sent = 0;
1428 * Dump the capability listing that we got from the server earlier
1429 * during the info/refs request.
1431 write_or_die(rpc.in, discover->buf, discover->len);
1433 /* Until we see EOF keep sending POSTs */
1434 while (1) {
1435 size_t avail;
1436 enum packet_read_status status;
1438 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1439 &status))
1440 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1441 if (status == PACKET_READ_EOF)
1442 break;
1443 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1444 /* We would have an err here */
1445 break;
1446 /* Reset the buffer for next request */
1447 rpc.len = 0;
1450 free(rpc.service_url);
1451 free(rpc.hdr_content_type);
1452 free(rpc.hdr_accept);
1453 free(rpc.protocol_header);
1454 free(rpc.buf);
1455 strbuf_release(&buf);
1457 return 0;
1460 int cmd_main(int argc, const char **argv)
1462 struct strbuf buf = STRBUF_INIT;
1463 int nongit;
1465 setup_git_directory_gently(&nongit);
1466 if (argc < 2) {
1467 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1468 return 1;
1471 options.verbosity = 1;
1472 options.progress = !!isatty(2);
1473 options.thin = 1;
1474 string_list_init(&options.deepen_not, 1);
1475 string_list_init(&options.push_options, 1);
1478 * Just report "remote-curl" here (folding all the various aliases
1479 * ("git-remote-http", "git-remote-https", and etc.) here since they
1480 * are all just copies of the same actual executable.
1482 trace2_cmd_name("remote-curl");
1484 remote = remote_get(argv[1]);
1486 if (argc > 2) {
1487 end_url_with_slash(&url, argv[2]);
1488 } else {
1489 end_url_with_slash(&url, remote->url[0]);
1492 http_init(remote, url.buf, 0);
1494 do {
1495 const char *arg;
1497 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1498 if (ferror(stdin))
1499 error(_("remote-curl: error reading command stream from git"));
1500 return 1;
1502 if (buf.len == 0)
1503 break;
1504 if (starts_with(buf.buf, "fetch ")) {
1505 if (nongit)
1506 die(_("remote-curl: fetch attempted without a local repo"));
1507 parse_fetch(&buf);
1509 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1510 int for_push = !!strstr(buf.buf + 4, "for-push");
1511 output_refs(get_refs(for_push));
1513 } else if (starts_with(buf.buf, "push ")) {
1514 parse_push(&buf);
1516 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1517 char *value = strchr(arg, ' ');
1518 int result;
1520 if (value)
1521 *value++ = '\0';
1522 else
1523 value = "true";
1525 result = set_option(arg, value);
1526 if (!result)
1527 printf("ok\n");
1528 else if (result < 0)
1529 printf("error invalid value\n");
1530 else
1531 printf("unsupported\n");
1532 fflush(stdout);
1534 } else if (!strcmp(buf.buf, "capabilities")) {
1535 printf("stateless-connect\n");
1536 printf("fetch\n");
1537 printf("option\n");
1538 printf("push\n");
1539 printf("check-connectivity\n");
1540 printf("object-format\n");
1541 printf("\n");
1542 fflush(stdout);
1543 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1544 if (!stateless_connect(arg))
1545 break;
1546 } else {
1547 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1548 return 1;
1550 strbuf_reset(&buf);
1551 } while (1);
1553 http_cleanup();
1555 return 0;