First batch post 2.28
[git.git] / remote-curl.c
blobc9921c552c09c7a7f6b074ce1d9bb8f0889cd299
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 "argv-array.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,
42 from_promisor : 1,
43 no_dependents : 1,
44 atomic : 1,
45 object_format : 1;
46 const struct git_hash_algo *hash_algo;
48 static struct options options;
49 static struct string_list cas_options = STRING_LIST_INIT_DUP;
51 static int set_option(const char *name, const char *value)
53 if (!strcmp(name, "verbosity")) {
54 char *end;
55 int v = strtol(value, &end, 10);
56 if (value == end || *end)
57 return -1;
58 options.verbosity = v;
59 return 0;
61 else if (!strcmp(name, "progress")) {
62 if (!strcmp(value, "true"))
63 options.progress = 1;
64 else if (!strcmp(value, "false"))
65 options.progress = 0;
66 else
67 return -1;
68 return 0;
70 else if (!strcmp(name, "depth")) {
71 char *end;
72 unsigned long v = strtoul(value, &end, 10);
73 if (value == end || *end)
74 return -1;
75 options.depth = v;
76 return 0;
78 else if (!strcmp(name, "deepen-since")) {
79 options.deepen_since = xstrdup(value);
80 return 0;
82 else if (!strcmp(name, "deepen-not")) {
83 string_list_append(&options.deepen_not, value);
84 return 0;
86 else if (!strcmp(name, "deepen-relative")) {
87 if (!strcmp(value, "true"))
88 options.deepen_relative = 1;
89 else if (!strcmp(value, "false"))
90 options.deepen_relative = 0;
91 else
92 return -1;
93 return 0;
95 else if (!strcmp(name, "followtags")) {
96 if (!strcmp(value, "true"))
97 options.followtags = 1;
98 else if (!strcmp(value, "false"))
99 options.followtags = 0;
100 else
101 return -1;
102 return 0;
104 else if (!strcmp(name, "dry-run")) {
105 if (!strcmp(value, "true"))
106 options.dry_run = 1;
107 else if (!strcmp(value, "false"))
108 options.dry_run = 0;
109 else
110 return -1;
111 return 0;
113 else if (!strcmp(name, "check-connectivity")) {
114 if (!strcmp(value, "true"))
115 options.check_self_contained_and_connected = 1;
116 else if (!strcmp(value, "false"))
117 options.check_self_contained_and_connected = 0;
118 else
119 return -1;
120 return 0;
122 else if (!strcmp(name, "cas")) {
123 struct strbuf val = STRBUF_INIT;
124 strbuf_addstr(&val, "--force-with-lease=");
125 if (*value != '"')
126 strbuf_addstr(&val, value);
127 else if (unquote_c_style(&val, value, NULL))
128 return -1;
129 string_list_append(&cas_options, val.buf);
130 strbuf_release(&val);
131 return 0;
132 } else if (!strcmp(name, "cloning")) {
133 if (!strcmp(value, "true"))
134 options.cloning = 1;
135 else if (!strcmp(value, "false"))
136 options.cloning = 0;
137 else
138 return -1;
139 return 0;
140 } else if (!strcmp(name, "update-shallow")) {
141 if (!strcmp(value, "true"))
142 options.update_shallow = 1;
143 else if (!strcmp(value, "false"))
144 options.update_shallow = 0;
145 else
146 return -1;
147 return 0;
148 } else if (!strcmp(name, "pushcert")) {
149 if (!strcmp(value, "true"))
150 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
151 else if (!strcmp(value, "false"))
152 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
153 else if (!strcmp(value, "if-asked"))
154 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
155 else
156 return -1;
157 return 0;
158 } else if (!strcmp(name, "atomic")) {
159 if (!strcmp(value, "true"))
160 options.atomic = 1;
161 else if (!strcmp(value, "false"))
162 options.atomic = 0;
163 else
164 return -1;
165 return 0;
166 } else if (!strcmp(name, "push-option")) {
167 if (*value != '"')
168 string_list_append(&options.push_options, value);
169 else {
170 struct strbuf unquoted = STRBUF_INIT;
171 if (unquote_c_style(&unquoted, value, NULL) < 0)
172 die(_("invalid quoting in push-option value: '%s'"), value);
173 string_list_append_nodup(&options.push_options,
174 strbuf_detach(&unquoted, NULL));
176 return 0;
178 #if LIBCURL_VERSION_NUM >= 0x070a08
179 } else if (!strcmp(name, "family")) {
180 if (!strcmp(value, "ipv4"))
181 git_curl_ipresolve = CURL_IPRESOLVE_V4;
182 else if (!strcmp(value, "ipv6"))
183 git_curl_ipresolve = CURL_IPRESOLVE_V6;
184 else if (!strcmp(value, "all"))
185 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
186 else
187 return -1;
188 return 0;
189 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
190 } else if (!strcmp(name, "from-promisor")) {
191 options.from_promisor = 1;
192 return 0;
193 } else if (!strcmp(name, "no-dependents")) {
194 options.no_dependents = 1;
195 return 0;
196 } else if (!strcmp(name, "filter")) {
197 options.filter = xstrdup(value);
198 return 0;
199 } else if (!strcmp(name, "object-format")) {
200 int algo;
201 options.object_format = 1;
202 if (strcmp(value, "true")) {
203 algo = hash_algo_by_name(value);
204 if (algo == GIT_HASH_UNKNOWN)
205 die("unknown object format '%s'", value);
206 options.hash_algo = &hash_algos[algo];
208 return 0;
209 } else {
210 return 1 /* unsupported */;
214 struct discovery {
215 char *service;
216 char *buf_alloc;
217 char *buf;
218 size_t len;
219 struct ref *refs;
220 struct oid_array shallow;
221 enum protocol_version version;
222 unsigned proto_git : 1;
224 static struct discovery *last_discovery;
226 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
228 struct ref *list = NULL;
229 struct packet_reader reader;
231 packet_reader_init(&reader, -1, heads->buf, heads->len,
232 PACKET_READ_CHOMP_NEWLINE |
233 PACKET_READ_GENTLE_ON_EOF |
234 PACKET_READ_DIE_ON_ERR_PACKET);
236 heads->version = discover_version(&reader);
237 switch (heads->version) {
238 case protocol_v2:
240 * Do nothing. This isn't a list of refs but rather a
241 * capability advertisement. Client would have run
242 * 'stateless-connect' so we'll dump this capability listing
243 * and let them request the refs themselves.
245 break;
246 case protocol_v1:
247 case protocol_v0:
248 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
249 NULL, &heads->shallow);
250 options.hash_algo = reader.hash_algo;
251 break;
252 case protocol_unknown_version:
253 BUG("unknown protocol version");
256 return list;
259 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
261 const char *p = memchr(heads->buf, '\t', heads->len);
262 int algo;
263 if (!p)
264 return the_hash_algo;
266 algo = hash_algo_by_length((p - heads->buf) / 2);
267 if (algo == GIT_HASH_UNKNOWN)
268 return NULL;
269 return &hash_algos[algo];
272 static struct ref *parse_info_refs(struct discovery *heads)
274 char *data, *start, *mid;
275 char *ref_name;
276 int i = 0;
278 struct ref *refs = NULL;
279 struct ref *ref = NULL;
280 struct ref *last_ref = NULL;
282 options.hash_algo = detect_hash_algo(heads);
283 if (!options.hash_algo)
284 die("%sinfo/refs not valid: could not determine hash algorithm; "
285 "is this a git repository?",
286 transport_anonymize_url(url.buf));
288 data = heads->buf;
289 start = NULL;
290 mid = data;
291 while (i < heads->len) {
292 if (!start) {
293 start = &data[i];
295 if (data[i] == '\t')
296 mid = &data[i];
297 if (data[i] == '\n') {
298 if (mid - start != options.hash_algo->hexsz)
299 die(_("%sinfo/refs not valid: is this a git repository?"),
300 transport_anonymize_url(url.buf));
301 data[i] = 0;
302 ref_name = mid + 1;
303 ref = alloc_ref(ref_name);
304 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
305 if (!refs)
306 refs = ref;
307 if (last_ref)
308 last_ref->next = ref;
309 last_ref = ref;
310 start = NULL;
312 i++;
315 ref = alloc_ref("HEAD");
316 if (!http_fetch_ref(url.buf, ref) &&
317 !resolve_remote_symref(ref, refs)) {
318 ref->next = refs;
319 refs = ref;
320 } else {
321 free(ref);
324 return refs;
327 static void free_discovery(struct discovery *d)
329 if (d) {
330 if (d == last_discovery)
331 last_discovery = NULL;
332 free(d->shallow.oid);
333 free(d->buf_alloc);
334 free_refs(d->refs);
335 free(d->service);
336 free(d);
340 static int show_http_message(struct strbuf *type, struct strbuf *charset,
341 struct strbuf *msg)
343 const char *p, *eol;
346 * We only show text/plain parts, as other types are likely
347 * to be ugly to look at on the user's terminal.
349 if (strcmp(type->buf, "text/plain"))
350 return -1;
351 if (charset->len)
352 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
354 strbuf_trim(msg);
355 if (!msg->len)
356 return -1;
358 p = msg->buf;
359 do {
360 eol = strchrnul(p, '\n');
361 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
362 p = eol + 1;
363 } while(*eol);
364 return 0;
367 static int get_protocol_http_header(enum protocol_version version,
368 struct strbuf *header)
370 if (version > 0) {
371 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
372 version);
374 return 1;
377 return 0;
380 static void check_smart_http(struct discovery *d, const char *service,
381 struct strbuf *type)
383 const char *p;
384 struct packet_reader reader;
387 * If we don't see x-$service-advertisement, then it's not smart-http.
388 * But once we do, we commit to it and assume any other protocol
389 * violations are hard errors.
391 if (!skip_prefix(type->buf, "application/x-", &p) ||
392 !skip_prefix(p, service, &p) ||
393 strcmp(p, "-advertisement"))
394 return;
396 packet_reader_init(&reader, -1, d->buf, d->len,
397 PACKET_READ_CHOMP_NEWLINE |
398 PACKET_READ_DIE_ON_ERR_PACKET);
399 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
400 die(_("invalid server response; expected service, got flush packet"));
402 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
404 * The header can include additional metadata lines, up
405 * until a packet flush marker. Ignore these now, but
406 * in the future we might start to scan them.
408 for (;;) {
409 packet_reader_read(&reader);
410 if (reader.pktlen <= 0) {
411 break;
416 * v0 smart http; callers expect us to soak up the
417 * service and header packets
419 d->buf = reader.src_buffer;
420 d->len = reader.src_len;
421 d->proto_git = 1;
423 } else if (!strcmp(reader.line, "version 2")) {
425 * v2 smart http; do not consume version packet, which will
426 * be handled elsewhere.
428 d->proto_git = 1;
430 } else {
431 die(_("invalid server response; got '%s'"), reader.line);
435 static struct discovery *discover_refs(const char *service, int for_push)
437 struct strbuf type = STRBUF_INIT;
438 struct strbuf charset = STRBUF_INIT;
439 struct strbuf buffer = STRBUF_INIT;
440 struct strbuf refs_url = STRBUF_INIT;
441 struct strbuf effective_url = STRBUF_INIT;
442 struct strbuf protocol_header = STRBUF_INIT;
443 struct string_list extra_headers = STRING_LIST_INIT_DUP;
444 struct discovery *last = last_discovery;
445 int http_ret, maybe_smart = 0;
446 struct http_get_options http_options;
447 enum protocol_version version = get_protocol_version_config();
449 if (last && !strcmp(service, last->service))
450 return last;
451 free_discovery(last);
453 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
454 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
455 git_env_bool("GIT_SMART_HTTP", 1)) {
456 maybe_smart = 1;
457 if (!strchr(url.buf, '?'))
458 strbuf_addch(&refs_url, '?');
459 else
460 strbuf_addch(&refs_url, '&');
461 strbuf_addf(&refs_url, "service=%s", service);
465 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
466 * to perform a push, then fallback to v0 since the client doesn't know
467 * how to push yet using v2.
469 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
470 version = protocol_v0;
472 /* Add the extra Git-Protocol header */
473 if (get_protocol_http_header(version, &protocol_header))
474 string_list_append(&extra_headers, protocol_header.buf);
476 memset(&http_options, 0, sizeof(http_options));
477 http_options.content_type = &type;
478 http_options.charset = &charset;
479 http_options.effective_url = &effective_url;
480 http_options.base_url = &url;
481 http_options.extra_headers = &extra_headers;
482 http_options.initial_request = 1;
483 http_options.no_cache = 1;
485 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
486 switch (http_ret) {
487 case HTTP_OK:
488 break;
489 case HTTP_MISSING_TARGET:
490 show_http_message(&type, &charset, &buffer);
491 die(_("repository '%s' not found"),
492 transport_anonymize_url(url.buf));
493 case HTTP_NOAUTH:
494 show_http_message(&type, &charset, &buffer);
495 die(_("Authentication failed for '%s'"),
496 transport_anonymize_url(url.buf));
497 default:
498 show_http_message(&type, &charset, &buffer);
499 die(_("unable to access '%s': %s"),
500 transport_anonymize_url(url.buf), curl_errorstr);
503 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
504 char *u = transport_anonymize_url(url.buf);
505 warning(_("redirecting to %s"), u);
506 free(u);
509 last= xcalloc(1, sizeof(*last_discovery));
510 last->service = xstrdup(service);
511 last->buf_alloc = strbuf_detach(&buffer, &last->len);
512 last->buf = last->buf_alloc;
514 if (maybe_smart)
515 check_smart_http(last, service, &type);
517 if (last->proto_git)
518 last->refs = parse_git_refs(last, for_push);
519 else
520 last->refs = parse_info_refs(last);
522 strbuf_release(&refs_url);
523 strbuf_release(&type);
524 strbuf_release(&charset);
525 strbuf_release(&effective_url);
526 strbuf_release(&buffer);
527 strbuf_release(&protocol_header);
528 string_list_clear(&extra_headers, 0);
529 last_discovery = last;
530 return last;
533 static struct ref *get_refs(int for_push)
535 struct discovery *heads;
537 if (for_push)
538 heads = discover_refs("git-receive-pack", for_push);
539 else
540 heads = discover_refs("git-upload-pack", for_push);
542 return heads->refs;
545 static void output_refs(struct ref *refs)
547 struct ref *posn;
548 if (options.object_format && options.hash_algo) {
549 printf(":object-format %s\n", options.hash_algo->name);
551 for (posn = refs; posn; posn = posn->next) {
552 if (posn->symref)
553 printf("@%s %s\n", posn->symref, posn->name);
554 else
555 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
556 options.hash_algo),
557 posn->name);
559 printf("\n");
560 fflush(stdout);
563 struct rpc_state {
564 const char *service_name;
565 char *service_url;
566 char *hdr_content_type;
567 char *hdr_accept;
568 char *protocol_header;
569 char *buf;
570 size_t alloc;
571 size_t len;
572 size_t pos;
573 int in;
574 int out;
575 int any_written;
576 unsigned gzip_request : 1;
577 unsigned initial_buffer : 1;
580 * Whenever a pkt-line is read into buf, append the 4 characters
581 * denoting its length before appending the payload.
583 unsigned write_line_lengths : 1;
586 * Used by rpc_out; initialize to 0. This is true if a flush has been
587 * read, but the corresponding line length (if write_line_lengths is
588 * true) and EOF have not been sent to libcurl. Since each flush marks
589 * the end of a request, each flush must be completely sent before any
590 * further reading occurs.
592 unsigned flush_read_but_not_sent : 1;
596 * Appends the result of reading from rpc->out to the string represented by
597 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
598 * enough space, 0 otherwise.
600 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
601 * hexadecimal string before appending the result described above.
603 * Writes the total number of bytes appended into appended.
605 static int rpc_read_from_out(struct rpc_state *rpc, int options,
606 size_t *appended,
607 enum packet_read_status *status) {
608 size_t left;
609 char *buf;
610 int pktlen_raw;
612 if (rpc->write_line_lengths) {
613 left = rpc->alloc - rpc->len - 4;
614 buf = rpc->buf + rpc->len + 4;
615 } else {
616 left = rpc->alloc - rpc->len;
617 buf = rpc->buf + rpc->len;
620 if (left < LARGE_PACKET_MAX)
621 return 0;
623 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
624 left, &pktlen_raw, options);
625 if (*status != PACKET_READ_EOF) {
626 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
627 rpc->len += *appended;
630 if (rpc->write_line_lengths) {
631 switch (*status) {
632 case PACKET_READ_EOF:
633 if (!(options & PACKET_READ_GENTLE_ON_EOF))
634 die(_("shouldn't have EOF when not gentle on EOF"));
635 break;
636 case PACKET_READ_NORMAL:
637 set_packet_header(buf - 4, *appended);
638 break;
639 case PACKET_READ_DELIM:
640 memcpy(buf - 4, "0001", 4);
641 break;
642 case PACKET_READ_FLUSH:
643 memcpy(buf - 4, "0000", 4);
644 break;
645 case PACKET_READ_RESPONSE_END:
646 die(_("remote server sent stateless separator"));
650 return 1;
653 static size_t rpc_out(void *ptr, size_t eltsize,
654 size_t nmemb, void *buffer_)
656 size_t max = eltsize * nmemb;
657 struct rpc_state *rpc = buffer_;
658 size_t avail = rpc->len - rpc->pos;
659 enum packet_read_status status;
661 if (!avail) {
662 rpc->initial_buffer = 0;
663 rpc->len = 0;
664 rpc->pos = 0;
665 if (!rpc->flush_read_but_not_sent) {
666 if (!rpc_read_from_out(rpc, 0, &avail, &status))
667 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
668 if (status == PACKET_READ_FLUSH)
669 rpc->flush_read_but_not_sent = 1;
672 * If flush_read_but_not_sent is true, we have already read one
673 * full request but have not fully sent it + EOF, which is why
674 * we need to refrain from reading.
677 if (rpc->flush_read_but_not_sent) {
678 if (!avail) {
680 * The line length either does not need to be sent at
681 * all or has already been completely sent. Now we can
682 * return 0, indicating EOF, meaning that the flush has
683 * been fully sent.
685 rpc->flush_read_but_not_sent = 0;
686 return 0;
689 * If avail is non-zero, the line length for the flush still
690 * hasn't been fully sent. Proceed with sending the line
691 * length.
695 if (max < avail)
696 avail = max;
697 memcpy(ptr, rpc->buf + rpc->pos, avail);
698 rpc->pos += avail;
699 return avail;
702 #ifndef NO_CURL_IOCTL
703 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
705 struct rpc_state *rpc = clientp;
707 switch (cmd) {
708 case CURLIOCMD_NOP:
709 return CURLIOE_OK;
711 case CURLIOCMD_RESTARTREAD:
712 if (rpc->initial_buffer) {
713 rpc->pos = 0;
714 return CURLIOE_OK;
716 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
717 return CURLIOE_FAILRESTART;
719 default:
720 return CURLIOE_UNKNOWNCMD;
723 #endif
725 struct check_pktline_state {
726 char len_buf[4];
727 int len_filled;
728 int remaining;
731 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
733 while (size) {
734 if (!state->remaining) {
735 int digits_remaining = 4 - state->len_filled;
736 if (digits_remaining > size)
737 digits_remaining = size;
738 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
739 state->len_filled += digits_remaining;
740 ptr += digits_remaining;
741 size -= digits_remaining;
743 if (state->len_filled == 4) {
744 state->remaining = packet_length(state->len_buf);
745 if (state->remaining < 0) {
746 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
747 } else if (state->remaining == 2) {
748 die(_("remote-curl: unexpected response end packet"));
749 } else if (state->remaining < 4) {
750 state->remaining = 0;
751 } else {
752 state->remaining -= 4;
754 state->len_filled = 0;
758 if (state->remaining) {
759 int remaining = state->remaining;
760 if (remaining > size)
761 remaining = size;
762 ptr += remaining;
763 size -= remaining;
764 state->remaining -= remaining;
769 struct rpc_in_data {
770 struct rpc_state *rpc;
771 struct active_request_slot *slot;
772 int check_pktline;
773 struct check_pktline_state pktline_state;
777 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
778 * from ptr.
780 static size_t rpc_in(char *ptr, size_t eltsize,
781 size_t nmemb, void *buffer_)
783 size_t size = eltsize * nmemb;
784 struct rpc_in_data *data = buffer_;
785 long response_code;
787 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
788 &response_code) != CURLE_OK)
789 return size;
790 if (response_code >= 300)
791 return size;
792 if (size)
793 data->rpc->any_written = 1;
794 if (data->check_pktline)
795 check_pktline(&data->pktline_state, ptr, size);
796 write_or_die(data->rpc->in, ptr, size);
797 return size;
800 static int run_slot(struct active_request_slot *slot,
801 struct slot_results *results)
803 int err;
804 struct slot_results results_buf;
806 if (!results)
807 results = &results_buf;
809 err = run_one_slot(slot, results);
811 if (err != HTTP_OK && err != HTTP_REAUTH) {
812 struct strbuf msg = STRBUF_INIT;
813 if (results->http_code && results->http_code != 200)
814 strbuf_addf(&msg, "HTTP %ld", results->http_code);
815 if (results->curl_result != CURLE_OK) {
816 if (msg.len)
817 strbuf_addch(&msg, ' ');
818 strbuf_addf(&msg, "curl %d", results->curl_result);
819 if (curl_errorstr[0]) {
820 strbuf_addch(&msg, ' ');
821 strbuf_addstr(&msg, curl_errorstr);
824 error(_("RPC failed; %s"), msg.buf);
825 strbuf_release(&msg);
828 return err;
831 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
833 struct active_request_slot *slot;
834 struct curl_slist *headers = http_copy_default_headers();
835 struct strbuf buf = STRBUF_INIT;
836 int err;
838 slot = get_active_slot();
840 headers = curl_slist_append(headers, rpc->hdr_content_type);
841 headers = curl_slist_append(headers, rpc->hdr_accept);
843 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
844 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
845 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
846 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
847 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
848 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
849 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
850 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
851 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
853 err = run_slot(slot, results);
855 curl_slist_free_all(headers);
856 strbuf_release(&buf);
857 return err;
860 static curl_off_t xcurl_off_t(size_t len)
862 uintmax_t size = len;
863 if (size > maximum_signed_value_of_type(curl_off_t))
864 die(_("cannot handle pushes this big"));
865 return (curl_off_t)size;
869 * If flush_received is true, do not attempt to read any more; just use what's
870 * in rpc->buf.
872 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
874 struct active_request_slot *slot;
875 struct curl_slist *headers = http_copy_default_headers();
876 int use_gzip = rpc->gzip_request;
877 char *gzip_body = NULL;
878 size_t gzip_size = 0;
879 int err, large_request = 0;
880 int needs_100_continue = 0;
881 struct rpc_in_data rpc_in_data;
883 /* Try to load the entire request, if we can fit it into the
884 * allocated buffer space we can use HTTP/1.0 and avoid the
885 * chunked encoding mess.
887 if (!flush_received) {
888 while (1) {
889 size_t n;
890 enum packet_read_status status;
892 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
893 large_request = 1;
894 use_gzip = 0;
895 break;
897 if (status == PACKET_READ_FLUSH)
898 break;
902 if (large_request) {
903 struct slot_results results;
905 do {
906 err = probe_rpc(rpc, &results);
907 if (err == HTTP_REAUTH)
908 credential_fill(&http_auth);
909 } while (err == HTTP_REAUTH);
910 if (err != HTTP_OK)
911 return -1;
913 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
914 needs_100_continue = 1;
917 headers = curl_slist_append(headers, rpc->hdr_content_type);
918 headers = curl_slist_append(headers, rpc->hdr_accept);
919 headers = curl_slist_append(headers, needs_100_continue ?
920 "Expect: 100-continue" : "Expect:");
922 /* Add the extra Git-Protocol header */
923 if (rpc->protocol_header)
924 headers = curl_slist_append(headers, rpc->protocol_header);
926 retry:
927 slot = get_active_slot();
929 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
930 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
931 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
932 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
934 if (large_request) {
935 /* The request body is large and the size cannot be predicted.
936 * We must use chunked encoding to send it.
938 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
939 rpc->initial_buffer = 1;
940 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
941 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
942 #ifndef NO_CURL_IOCTL
943 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
944 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
945 #endif
946 if (options.verbosity > 1) {
947 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
948 fflush(stderr);
951 } else if (gzip_body) {
953 * If we are looping to retry authentication, then the previous
954 * run will have set up the headers and gzip buffer already,
955 * and we just need to send it.
957 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
958 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
960 } else if (use_gzip && 1024 < rpc->len) {
961 /* The client backend isn't giving us compressed data so
962 * we can try to deflate it ourselves, this may save on
963 * the transfer time.
965 git_zstream stream;
966 int ret;
968 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
969 gzip_size = git_deflate_bound(&stream, rpc->len);
970 gzip_body = xmalloc(gzip_size);
972 stream.next_in = (unsigned char *)rpc->buf;
973 stream.avail_in = rpc->len;
974 stream.next_out = (unsigned char *)gzip_body;
975 stream.avail_out = gzip_size;
977 ret = git_deflate(&stream, Z_FINISH);
978 if (ret != Z_STREAM_END)
979 die(_("cannot deflate request; zlib deflate error %d"), ret);
981 ret = git_deflate_end_gently(&stream);
982 if (ret != Z_OK)
983 die(_("cannot deflate request; zlib end error %d"), ret);
985 gzip_size = stream.total_out;
987 headers = curl_slist_append(headers, "Content-Encoding: gzip");
988 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
989 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
991 if (options.verbosity > 1) {
992 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
993 rpc->service_name,
994 (unsigned long)rpc->len, (unsigned long)gzip_size);
995 fflush(stderr);
997 } else {
998 /* We know the complete request size in advance, use the
999 * more normal Content-Length approach.
1001 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1002 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1003 if (options.verbosity > 1) {
1004 fprintf(stderr, "POST %s (%lu bytes)\n",
1005 rpc->service_name, (unsigned long)rpc->len);
1006 fflush(stderr);
1010 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1011 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1012 rpc_in_data.rpc = rpc;
1013 rpc_in_data.slot = slot;
1014 rpc_in_data.check_pktline = stateless_connect;
1015 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1016 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
1017 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1020 rpc->any_written = 0;
1021 err = run_slot(slot, NULL);
1022 if (err == HTTP_REAUTH && !large_request) {
1023 credential_fill(&http_auth);
1024 goto retry;
1026 if (err != HTTP_OK)
1027 err = -1;
1029 if (!rpc->any_written)
1030 err = -1;
1032 if (rpc_in_data.pktline_state.len_filled)
1033 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1034 if (rpc_in_data.pktline_state.remaining)
1035 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1037 if (stateless_connect)
1038 packet_response_end(rpc->in);
1040 curl_slist_free_all(headers);
1041 free(gzip_body);
1042 return err;
1045 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1046 const char **client_argv, const struct strbuf *preamble,
1047 struct strbuf *rpc_result)
1049 const char *svc = rpc->service_name;
1050 struct strbuf buf = STRBUF_INIT;
1051 struct child_process client = CHILD_PROCESS_INIT;
1052 int err = 0;
1054 client.in = -1;
1055 client.out = -1;
1056 client.git_cmd = 1;
1057 client.argv = client_argv;
1058 if (start_command(&client))
1059 exit(1);
1060 write_or_die(client.in, preamble->buf, preamble->len);
1061 if (heads)
1062 write_or_die(client.in, heads->buf, heads->len);
1064 rpc->alloc = http_post_buffer;
1065 rpc->buf = xmalloc(rpc->alloc);
1066 rpc->in = client.in;
1067 rpc->out = client.out;
1069 strbuf_addf(&buf, "%s%s", url.buf, svc);
1070 rpc->service_url = strbuf_detach(&buf, NULL);
1072 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1073 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1075 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1076 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1078 if (get_protocol_http_header(heads->version, &buf))
1079 rpc->protocol_header = strbuf_detach(&buf, NULL);
1080 else
1081 rpc->protocol_header = NULL;
1083 while (!err) {
1084 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
1085 if (!n)
1086 break;
1087 rpc->pos = 0;
1088 rpc->len = n;
1089 err |= post_rpc(rpc, 0, 0);
1092 close(client.in);
1093 client.in = -1;
1094 if (!err) {
1095 strbuf_read(rpc_result, client.out, 0);
1096 } else {
1097 char buf[4096];
1098 for (;;)
1099 if (xread(client.out, buf, sizeof(buf)) <= 0)
1100 break;
1103 close(client.out);
1104 client.out = -1;
1106 err |= finish_command(&client);
1107 free(rpc->service_url);
1108 free(rpc->hdr_content_type);
1109 free(rpc->hdr_accept);
1110 free(rpc->protocol_header);
1111 free(rpc->buf);
1112 strbuf_release(&buf);
1113 return err;
1116 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1118 struct walker *walker;
1119 char **targets;
1120 int ret, i;
1122 ALLOC_ARRAY(targets, nr_heads);
1123 if (options.depth || options.deepen_since)
1124 die(_("dumb http transport does not support shallow capabilities"));
1125 for (i = 0; i < nr_heads; i++)
1126 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1128 walker = get_http_walker(url.buf);
1129 walker->get_verbosely = options.verbosity >= 3;
1130 walker->get_progress = options.progress;
1131 walker->get_recover = 0;
1132 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1133 walker_free(walker);
1135 for (i = 0; i < nr_heads; i++)
1136 free(targets[i]);
1137 free(targets);
1139 return ret ? error(_("fetch failed.")) : 0;
1142 static int fetch_git(struct discovery *heads,
1143 int nr_heads, struct ref **to_fetch)
1145 struct rpc_state rpc;
1146 struct strbuf preamble = STRBUF_INIT;
1147 int i, err;
1148 struct argv_array args = ARGV_ARRAY_INIT;
1149 struct strbuf rpc_result = STRBUF_INIT;
1151 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
1152 "--stdin", "--lock-pack", NULL);
1153 if (options.followtags)
1154 argv_array_push(&args, "--include-tag");
1155 if (options.thin)
1156 argv_array_push(&args, "--thin");
1157 if (options.verbosity >= 3)
1158 argv_array_pushl(&args, "-v", "-v", NULL);
1159 if (options.check_self_contained_and_connected)
1160 argv_array_push(&args, "--check-self-contained-and-connected");
1161 if (options.cloning)
1162 argv_array_push(&args, "--cloning");
1163 if (options.update_shallow)
1164 argv_array_push(&args, "--update-shallow");
1165 if (!options.progress)
1166 argv_array_push(&args, "--no-progress");
1167 if (options.depth)
1168 argv_array_pushf(&args, "--depth=%lu", options.depth);
1169 if (options.deepen_since)
1170 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
1171 for (i = 0; i < options.deepen_not.nr; i++)
1172 argv_array_pushf(&args, "--shallow-exclude=%s",
1173 options.deepen_not.items[i].string);
1174 if (options.deepen_relative && options.depth)
1175 argv_array_push(&args, "--deepen-relative");
1176 if (options.from_promisor)
1177 argv_array_push(&args, "--from-promisor");
1178 if (options.no_dependents)
1179 argv_array_push(&args, "--no-dependents");
1180 if (options.filter)
1181 argv_array_pushf(&args, "--filter=%s", options.filter);
1182 argv_array_push(&args, url.buf);
1184 for (i = 0; i < nr_heads; i++) {
1185 struct ref *ref = to_fetch[i];
1186 if (!*ref->name)
1187 die(_("cannot fetch by sha1 over smart http"));
1188 packet_buf_write(&preamble, "%s %s\n",
1189 oid_to_hex(&ref->old_oid), ref->name);
1191 packet_buf_flush(&preamble);
1193 memset(&rpc, 0, sizeof(rpc));
1194 rpc.service_name = "git-upload-pack",
1195 rpc.gzip_request = 1;
1197 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1198 if (rpc_result.len)
1199 write_or_die(1, rpc_result.buf, rpc_result.len);
1200 strbuf_release(&rpc_result);
1201 strbuf_release(&preamble);
1202 argv_array_clear(&args);
1203 return err;
1206 static int fetch(int nr_heads, struct ref **to_fetch)
1208 struct discovery *d = discover_refs("git-upload-pack", 0);
1209 if (d->proto_git)
1210 return fetch_git(d, nr_heads, to_fetch);
1211 else
1212 return fetch_dumb(nr_heads, to_fetch);
1215 static void parse_fetch(struct strbuf *buf)
1217 struct ref **to_fetch = NULL;
1218 struct ref *list_head = NULL;
1219 struct ref **list = &list_head;
1220 int alloc_heads = 0, nr_heads = 0;
1222 do {
1223 const char *p;
1224 if (skip_prefix(buf->buf, "fetch ", &p)) {
1225 const char *name;
1226 struct ref *ref;
1227 struct object_id old_oid;
1228 const char *q;
1230 if (parse_oid_hex(p, &old_oid, &q))
1231 die(_("protocol error: expected sha/ref, got '%s'"), p);
1232 if (*q == ' ')
1233 name = q + 1;
1234 else if (!*q)
1235 name = "";
1236 else
1237 die(_("protocol error: expected sha/ref, got '%s'"), p);
1239 ref = alloc_ref(name);
1240 oidcpy(&ref->old_oid, &old_oid);
1242 *list = ref;
1243 list = &ref->next;
1245 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1246 to_fetch[nr_heads++] = ref;
1248 else
1249 die(_("http transport does not support %s"), buf->buf);
1251 strbuf_reset(buf);
1252 if (strbuf_getline_lf(buf, stdin) == EOF)
1253 return;
1254 if (!*buf->buf)
1255 break;
1256 } while (1);
1258 if (fetch(nr_heads, to_fetch))
1259 exit(128); /* error already reported */
1260 free_refs(list_head);
1261 free(to_fetch);
1263 printf("\n");
1264 fflush(stdout);
1265 strbuf_reset(buf);
1268 static int push_dav(int nr_spec, const char **specs)
1270 struct child_process child = CHILD_PROCESS_INIT;
1271 size_t i;
1273 child.git_cmd = 1;
1274 argv_array_push(&child.args, "http-push");
1275 argv_array_push(&child.args, "--helper-status");
1276 if (options.dry_run)
1277 argv_array_push(&child.args, "--dry-run");
1278 if (options.verbosity > 1)
1279 argv_array_push(&child.args, "--verbose");
1280 argv_array_push(&child.args, url.buf);
1281 for (i = 0; i < nr_spec; i++)
1282 argv_array_push(&child.args, specs[i]);
1284 if (run_command(&child))
1285 die(_("git-http-push failed"));
1286 return 0;
1289 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1291 struct rpc_state rpc;
1292 int i, err;
1293 struct argv_array args;
1294 struct string_list_item *cas_option;
1295 struct strbuf preamble = STRBUF_INIT;
1296 struct strbuf rpc_result = STRBUF_INIT;
1298 argv_array_init(&args);
1299 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1300 NULL);
1302 if (options.thin)
1303 argv_array_push(&args, "--thin");
1304 if (options.dry_run)
1305 argv_array_push(&args, "--dry-run");
1306 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1307 argv_array_push(&args, "--signed=yes");
1308 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1309 argv_array_push(&args, "--signed=if-asked");
1310 if (options.atomic)
1311 argv_array_push(&args, "--atomic");
1312 if (options.verbosity == 0)
1313 argv_array_push(&args, "--quiet");
1314 else if (options.verbosity > 1)
1315 argv_array_push(&args, "--verbose");
1316 for (i = 0; i < options.push_options.nr; i++)
1317 argv_array_pushf(&args, "--push-option=%s",
1318 options.push_options.items[i].string);
1319 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1320 for_each_string_list_item(cas_option, &cas_options)
1321 argv_array_push(&args, cas_option->string);
1322 argv_array_push(&args, url.buf);
1324 argv_array_push(&args, "--stdin");
1325 for (i = 0; i < nr_spec; i++)
1326 packet_buf_write(&preamble, "%s\n", specs[i]);
1327 packet_buf_flush(&preamble);
1329 memset(&rpc, 0, sizeof(rpc));
1330 rpc.service_name = "git-receive-pack",
1332 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1333 if (rpc_result.len)
1334 write_or_die(1, rpc_result.buf, rpc_result.len);
1335 strbuf_release(&rpc_result);
1336 strbuf_release(&preamble);
1337 argv_array_clear(&args);
1338 return err;
1341 static int push(int nr_spec, const char **specs)
1343 struct discovery *heads = discover_refs("git-receive-pack", 1);
1344 int ret;
1346 if (heads->proto_git)
1347 ret = push_git(heads, nr_spec, specs);
1348 else
1349 ret = push_dav(nr_spec, specs);
1350 free_discovery(heads);
1351 return ret;
1354 static void parse_push(struct strbuf *buf)
1356 struct argv_array specs = ARGV_ARRAY_INIT;
1357 int ret;
1359 do {
1360 const char *arg;
1361 if (skip_prefix(buf->buf, "push ", &arg))
1362 argv_array_push(&specs, arg);
1363 else
1364 die(_("http transport does not support %s"), buf->buf);
1366 strbuf_reset(buf);
1367 if (strbuf_getline_lf(buf, stdin) == EOF)
1368 goto free_specs;
1369 if (!*buf->buf)
1370 break;
1371 } while (1);
1373 ret = push(specs.argc, specs.argv);
1374 printf("\n");
1375 fflush(stdout);
1377 if (ret)
1378 exit(128); /* error already reported */
1380 free_specs:
1381 argv_array_clear(&specs);
1384 static int stateless_connect(const char *service_name)
1386 struct discovery *discover;
1387 struct rpc_state rpc;
1388 struct strbuf buf = STRBUF_INIT;
1391 * Run the info/refs request and see if the server supports protocol
1392 * v2. If and only if the server supports v2 can we successfully
1393 * establish a stateless connection, otherwise we need to tell the
1394 * client to fallback to using other transport helper functions to
1395 * complete their request.
1397 discover = discover_refs(service_name, 0);
1398 if (discover->version != protocol_v2) {
1399 printf("fallback\n");
1400 fflush(stdout);
1401 return -1;
1402 } else {
1403 /* Stateless Connection established */
1404 printf("\n");
1405 fflush(stdout);
1408 rpc.service_name = service_name;
1409 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1410 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1411 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1412 if (get_protocol_http_header(discover->version, &buf)) {
1413 rpc.protocol_header = strbuf_detach(&buf, NULL);
1414 } else {
1415 rpc.protocol_header = NULL;
1416 strbuf_release(&buf);
1418 rpc.buf = xmalloc(http_post_buffer);
1419 rpc.alloc = http_post_buffer;
1420 rpc.len = 0;
1421 rpc.pos = 0;
1422 rpc.in = 1;
1423 rpc.out = 0;
1424 rpc.any_written = 0;
1425 rpc.gzip_request = 1;
1426 rpc.initial_buffer = 0;
1427 rpc.write_line_lengths = 1;
1428 rpc.flush_read_but_not_sent = 0;
1431 * Dump the capability listing that we got from the server earlier
1432 * during the info/refs request.
1434 write_or_die(rpc.in, discover->buf, discover->len);
1436 /* Until we see EOF keep sending POSTs */
1437 while (1) {
1438 size_t avail;
1439 enum packet_read_status status;
1441 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1442 &status))
1443 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1444 if (status == PACKET_READ_EOF)
1445 break;
1446 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1447 /* We would have an err here */
1448 break;
1449 /* Reset the buffer for next request */
1450 rpc.len = 0;
1453 free(rpc.service_url);
1454 free(rpc.hdr_content_type);
1455 free(rpc.hdr_accept);
1456 free(rpc.protocol_header);
1457 free(rpc.buf);
1458 strbuf_release(&buf);
1460 return 0;
1463 int cmd_main(int argc, const char **argv)
1465 struct strbuf buf = STRBUF_INIT;
1466 int nongit;
1468 setup_git_directory_gently(&nongit);
1469 if (argc < 2) {
1470 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1471 return 1;
1474 options.verbosity = 1;
1475 options.progress = !!isatty(2);
1476 options.thin = 1;
1477 string_list_init(&options.deepen_not, 1);
1478 string_list_init(&options.push_options, 1);
1481 * Just report "remote-curl" here (folding all the various aliases
1482 * ("git-remote-http", "git-remote-https", and etc.) here since they
1483 * are all just copies of the same actual executable.
1485 trace2_cmd_name("remote-curl");
1487 remote = remote_get(argv[1]);
1489 if (argc > 2) {
1490 end_url_with_slash(&url, argv[2]);
1491 } else {
1492 end_url_with_slash(&url, remote->url[0]);
1495 http_init(remote, url.buf, 0);
1497 do {
1498 const char *arg;
1500 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1501 if (ferror(stdin))
1502 error(_("remote-curl: error reading command stream from git"));
1503 return 1;
1505 if (buf.len == 0)
1506 break;
1507 if (starts_with(buf.buf, "fetch ")) {
1508 if (nongit)
1509 die(_("remote-curl: fetch attempted without a local repo"));
1510 parse_fetch(&buf);
1512 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1513 int for_push = !!strstr(buf.buf + 4, "for-push");
1514 output_refs(get_refs(for_push));
1516 } else if (starts_with(buf.buf, "push ")) {
1517 parse_push(&buf);
1519 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1520 char *value = strchr(arg, ' ');
1521 int result;
1523 if (value)
1524 *value++ = '\0';
1525 else
1526 value = "true";
1528 result = set_option(arg, value);
1529 if (!result)
1530 printf("ok\n");
1531 else if (result < 0)
1532 printf("error invalid value\n");
1533 else
1534 printf("unsupported\n");
1535 fflush(stdout);
1537 } else if (!strcmp(buf.buf, "capabilities")) {
1538 printf("stateless-connect\n");
1539 printf("fetch\n");
1540 printf("option\n");
1541 printf("push\n");
1542 printf("check-connectivity\n");
1543 printf("object-format\n");
1544 printf("\n");
1545 fflush(stdout);
1546 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1547 if (!stateless_connect(arg))
1548 break;
1549 } else {
1550 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1551 return 1;
1553 strbuf_reset(&buf);
1554 } while (1);
1556 http_cleanup();
1558 return 0;