Sync with 2.33.8
[git/debian.git] / remote-curl.c
blob1b5d9ac1d82d93c8ca0c20c89d5a74024278a661
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 force_if_includes : 1;
49 const struct git_hash_algo *hash_algo;
51 static struct options options;
52 static struct string_list cas_options = STRING_LIST_INIT_DUP;
54 static int set_option(const char *name, const char *value)
56 if (!strcmp(name, "verbosity")) {
57 char *end;
58 int v = strtol(value, &end, 10);
59 if (value == end || *end)
60 return -1;
61 options.verbosity = v;
62 return 0;
64 else if (!strcmp(name, "progress")) {
65 if (!strcmp(value, "true"))
66 options.progress = 1;
67 else if (!strcmp(value, "false"))
68 options.progress = 0;
69 else
70 return -1;
71 return 0;
73 else if (!strcmp(name, "depth")) {
74 char *end;
75 unsigned long v = strtoul(value, &end, 10);
76 if (value == end || *end)
77 return -1;
78 options.depth = v;
79 return 0;
81 else if (!strcmp(name, "deepen-since")) {
82 options.deepen_since = xstrdup(value);
83 return 0;
85 else if (!strcmp(name, "deepen-not")) {
86 string_list_append(&options.deepen_not, value);
87 return 0;
89 else if (!strcmp(name, "deepen-relative")) {
90 if (!strcmp(value, "true"))
91 options.deepen_relative = 1;
92 else if (!strcmp(value, "false"))
93 options.deepen_relative = 0;
94 else
95 return -1;
96 return 0;
98 else if (!strcmp(name, "followtags")) {
99 if (!strcmp(value, "true"))
100 options.followtags = 1;
101 else if (!strcmp(value, "false"))
102 options.followtags = 0;
103 else
104 return -1;
105 return 0;
107 else if (!strcmp(name, "dry-run")) {
108 if (!strcmp(value, "true"))
109 options.dry_run = 1;
110 else if (!strcmp(value, "false"))
111 options.dry_run = 0;
112 else
113 return -1;
114 return 0;
116 else if (!strcmp(name, "check-connectivity")) {
117 if (!strcmp(value, "true"))
118 options.check_self_contained_and_connected = 1;
119 else if (!strcmp(value, "false"))
120 options.check_self_contained_and_connected = 0;
121 else
122 return -1;
123 return 0;
125 else if (!strcmp(name, "cas")) {
126 struct strbuf val = STRBUF_INIT;
127 strbuf_addstr(&val, "--force-with-lease=");
128 if (*value != '"')
129 strbuf_addstr(&val, value);
130 else if (unquote_c_style(&val, value, NULL))
131 return -1;
132 string_list_append(&cas_options, val.buf);
133 strbuf_release(&val);
134 return 0;
135 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
136 if (!strcmp(value, "true"))
137 options.force_if_includes = 1;
138 else if (!strcmp(value, "false"))
139 options.force_if_includes = 0;
140 else
141 return -1;
142 return 0;
143 } else if (!strcmp(name, "cloning")) {
144 if (!strcmp(value, "true"))
145 options.cloning = 1;
146 else if (!strcmp(value, "false"))
147 options.cloning = 0;
148 else
149 return -1;
150 return 0;
151 } else if (!strcmp(name, "update-shallow")) {
152 if (!strcmp(value, "true"))
153 options.update_shallow = 1;
154 else if (!strcmp(value, "false"))
155 options.update_shallow = 0;
156 else
157 return -1;
158 return 0;
159 } else if (!strcmp(name, "pushcert")) {
160 if (!strcmp(value, "true"))
161 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
162 else if (!strcmp(value, "false"))
163 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
164 else if (!strcmp(value, "if-asked"))
165 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
166 else
167 return -1;
168 return 0;
169 } else if (!strcmp(name, "atomic")) {
170 if (!strcmp(value, "true"))
171 options.atomic = 1;
172 else if (!strcmp(value, "false"))
173 options.atomic = 0;
174 else
175 return -1;
176 return 0;
177 } else if (!strcmp(name, "push-option")) {
178 if (*value != '"')
179 string_list_append(&options.push_options, value);
180 else {
181 struct strbuf unquoted = STRBUF_INIT;
182 if (unquote_c_style(&unquoted, value, NULL) < 0)
183 die(_("invalid quoting in push-option value: '%s'"), value);
184 string_list_append_nodup(&options.push_options,
185 strbuf_detach(&unquoted, NULL));
187 return 0;
188 } else if (!strcmp(name, "family")) {
189 if (!strcmp(value, "ipv4"))
190 git_curl_ipresolve = CURL_IPRESOLVE_V4;
191 else if (!strcmp(value, "ipv6"))
192 git_curl_ipresolve = CURL_IPRESOLVE_V6;
193 else if (!strcmp(value, "all"))
194 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
195 else
196 return -1;
197 return 0;
198 } else if (!strcmp(name, "from-promisor")) {
199 options.from_promisor = 1;
200 return 0;
201 } else if (!strcmp(name, "filter")) {
202 options.filter = xstrdup(value);
203 return 0;
204 } else if (!strcmp(name, "object-format")) {
205 int algo;
206 options.object_format = 1;
207 if (strcmp(value, "true")) {
208 algo = hash_algo_by_name(value);
209 if (algo == GIT_HASH_UNKNOWN)
210 die("unknown object format '%s'", value);
211 options.hash_algo = &hash_algos[algo];
213 return 0;
214 } else {
215 return 1 /* unsupported */;
219 struct discovery {
220 char *service;
221 char *buf_alloc;
222 char *buf;
223 size_t len;
224 struct ref *refs;
225 struct oid_array shallow;
226 enum protocol_version version;
227 unsigned proto_git : 1;
229 static struct discovery *last_discovery;
231 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
233 struct ref *list = NULL;
234 struct packet_reader reader;
236 packet_reader_init(&reader, -1, heads->buf, heads->len,
237 PACKET_READ_CHOMP_NEWLINE |
238 PACKET_READ_GENTLE_ON_EOF |
239 PACKET_READ_DIE_ON_ERR_PACKET);
241 heads->version = discover_version(&reader);
242 switch (heads->version) {
243 case protocol_v2:
245 * Do nothing. This isn't a list of refs but rather a
246 * capability advertisement. Client would have run
247 * 'stateless-connect' so we'll dump this capability listing
248 * and let them request the refs themselves.
250 break;
251 case protocol_v1:
252 case protocol_v0:
253 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
254 NULL, &heads->shallow);
255 options.hash_algo = reader.hash_algo;
256 break;
257 case protocol_unknown_version:
258 BUG("unknown protocol version");
261 return list;
264 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
266 const char *p = memchr(heads->buf, '\t', heads->len);
267 int algo;
268 if (!p)
269 return the_hash_algo;
271 algo = hash_algo_by_length((p - heads->buf) / 2);
272 if (algo == GIT_HASH_UNKNOWN)
273 return NULL;
274 return &hash_algos[algo];
277 static struct ref *parse_info_refs(struct discovery *heads)
279 char *data, *start, *mid;
280 char *ref_name;
281 int i = 0;
283 struct ref *refs = NULL;
284 struct ref *ref = NULL;
285 struct ref *last_ref = NULL;
287 options.hash_algo = detect_hash_algo(heads);
288 if (!options.hash_algo)
289 die("%sinfo/refs not valid: could not determine hash algorithm; "
290 "is this a git repository?",
291 transport_anonymize_url(url.buf));
293 data = heads->buf;
294 start = NULL;
295 mid = data;
296 while (i < heads->len) {
297 if (!start) {
298 start = &data[i];
300 if (data[i] == '\t')
301 mid = &data[i];
302 if (data[i] == '\n') {
303 if (mid - start != options.hash_algo->hexsz)
304 die(_("%sinfo/refs not valid: is this a git repository?"),
305 transport_anonymize_url(url.buf));
306 data[i] = 0;
307 ref_name = mid + 1;
308 ref = alloc_ref(ref_name);
309 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
310 if (!refs)
311 refs = ref;
312 if (last_ref)
313 last_ref->next = ref;
314 last_ref = ref;
315 start = NULL;
317 i++;
320 ref = alloc_ref("HEAD");
321 if (!http_fetch_ref(url.buf, ref) &&
322 !resolve_remote_symref(ref, refs)) {
323 ref->next = refs;
324 refs = ref;
325 } else {
326 free(ref);
329 return refs;
332 static void free_discovery(struct discovery *d)
334 if (d) {
335 if (d == last_discovery)
336 last_discovery = NULL;
337 free(d->shallow.oid);
338 free(d->buf_alloc);
339 free_refs(d->refs);
340 free(d->service);
341 free(d);
345 static int show_http_message(struct strbuf *type, struct strbuf *charset,
346 struct strbuf *msg)
348 const char *p, *eol;
351 * We only show text/plain parts, as other types are likely
352 * to be ugly to look at on the user's terminal.
354 if (strcmp(type->buf, "text/plain"))
355 return -1;
356 if (charset->len)
357 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
359 strbuf_trim(msg);
360 if (!msg->len)
361 return -1;
363 p = msg->buf;
364 do {
365 eol = strchrnul(p, '\n');
366 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
367 p = eol + 1;
368 } while(*eol);
369 return 0;
372 static int get_protocol_http_header(enum protocol_version version,
373 struct strbuf *header)
375 if (version > 0) {
376 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
377 version);
379 return 1;
382 return 0;
385 static void check_smart_http(struct discovery *d, const char *service,
386 struct strbuf *type)
388 const char *p;
389 struct packet_reader reader;
392 * If we don't see x-$service-advertisement, then it's not smart-http.
393 * But once we do, we commit to it and assume any other protocol
394 * violations are hard errors.
396 if (!skip_prefix(type->buf, "application/x-", &p) ||
397 !skip_prefix(p, service, &p) ||
398 strcmp(p, "-advertisement"))
399 return;
401 packet_reader_init(&reader, -1, d->buf, d->len,
402 PACKET_READ_CHOMP_NEWLINE |
403 PACKET_READ_DIE_ON_ERR_PACKET);
404 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
405 die(_("invalid server response; expected service, got flush packet"));
407 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
409 * The header can include additional metadata lines, up
410 * until a packet flush marker. Ignore these now, but
411 * in the future we might start to scan them.
413 for (;;) {
414 packet_reader_read(&reader);
415 if (reader.pktlen <= 0) {
416 break;
421 * v0 smart http; callers expect us to soak up the
422 * service and header packets
424 d->buf = reader.src_buffer;
425 d->len = reader.src_len;
426 d->proto_git = 1;
428 } else if (!strcmp(reader.line, "version 2")) {
430 * v2 smart http; do not consume version packet, which will
431 * be handled elsewhere.
433 d->proto_git = 1;
435 } else {
436 die(_("invalid server response; got '%s'"), reader.line);
440 static struct discovery *discover_refs(const char *service, int for_push)
442 struct strbuf type = STRBUF_INIT;
443 struct strbuf charset = STRBUF_INIT;
444 struct strbuf buffer = STRBUF_INIT;
445 struct strbuf refs_url = STRBUF_INIT;
446 struct strbuf effective_url = STRBUF_INIT;
447 struct strbuf protocol_header = STRBUF_INIT;
448 struct string_list extra_headers = STRING_LIST_INIT_DUP;
449 struct discovery *last = last_discovery;
450 int http_ret, maybe_smart = 0;
451 struct http_get_options http_options;
452 enum protocol_version version = get_protocol_version_config();
454 if (last && !strcmp(service, last->service))
455 return last;
456 free_discovery(last);
458 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
459 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
460 git_env_bool("GIT_SMART_HTTP", 1)) {
461 maybe_smart = 1;
462 if (!strchr(url.buf, '?'))
463 strbuf_addch(&refs_url, '?');
464 else
465 strbuf_addch(&refs_url, '&');
466 strbuf_addf(&refs_url, "service=%s", service);
470 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
471 * to perform a push, then fallback to v0 since the client doesn't know
472 * how to push yet using v2.
474 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
475 version = protocol_v0;
477 /* Add the extra Git-Protocol header */
478 if (get_protocol_http_header(version, &protocol_header))
479 string_list_append(&extra_headers, protocol_header.buf);
481 memset(&http_options, 0, sizeof(http_options));
482 http_options.content_type = &type;
483 http_options.charset = &charset;
484 http_options.effective_url = &effective_url;
485 http_options.base_url = &url;
486 http_options.extra_headers = &extra_headers;
487 http_options.initial_request = 1;
488 http_options.no_cache = 1;
490 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
491 switch (http_ret) {
492 case HTTP_OK:
493 break;
494 case HTTP_MISSING_TARGET:
495 show_http_message(&type, &charset, &buffer);
496 die(_("repository '%s' not found"),
497 transport_anonymize_url(url.buf));
498 case HTTP_NOAUTH:
499 show_http_message(&type, &charset, &buffer);
500 die(_("Authentication failed for '%s'"),
501 transport_anonymize_url(url.buf));
502 case HTTP_NOMATCHPUBLICKEY:
503 show_http_message(&type, &charset, &buffer);
504 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
505 transport_anonymize_url(url.buf), curl_errorstr);
506 default:
507 show_http_message(&type, &charset, &buffer);
508 die(_("unable to access '%s': %s"),
509 transport_anonymize_url(url.buf), curl_errorstr);
512 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
513 char *u = transport_anonymize_url(url.buf);
514 warning(_("redirecting to %s"), u);
515 free(u);
518 last= xcalloc(1, sizeof(*last_discovery));
519 last->service = xstrdup(service);
520 last->buf_alloc = strbuf_detach(&buffer, &last->len);
521 last->buf = last->buf_alloc;
523 if (maybe_smart)
524 check_smart_http(last, service, &type);
526 if (last->proto_git)
527 last->refs = parse_git_refs(last, for_push);
528 else
529 last->refs = parse_info_refs(last);
531 strbuf_release(&refs_url);
532 strbuf_release(&type);
533 strbuf_release(&charset);
534 strbuf_release(&effective_url);
535 strbuf_release(&buffer);
536 strbuf_release(&protocol_header);
537 string_list_clear(&extra_headers, 0);
538 last_discovery = last;
539 return last;
542 static struct ref *get_refs(int for_push)
544 struct discovery *heads;
546 if (for_push)
547 heads = discover_refs("git-receive-pack", for_push);
548 else
549 heads = discover_refs("git-upload-pack", for_push);
551 return heads->refs;
554 static void output_refs(struct ref *refs)
556 struct ref *posn;
557 if (options.object_format && options.hash_algo) {
558 printf(":object-format %s\n", options.hash_algo->name);
559 repo_set_hash_algo(the_repository,
560 hash_algo_by_ptr(options.hash_algo));
562 for (posn = refs; posn; posn = posn->next) {
563 if (posn->symref)
564 printf("@%s %s\n", posn->symref, posn->name);
565 else
566 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
567 options.hash_algo),
568 posn->name);
570 printf("\n");
571 fflush(stdout);
574 struct rpc_state {
575 const char *service_name;
576 char *service_url;
577 char *hdr_content_type;
578 char *hdr_accept;
579 char *protocol_header;
580 char *buf;
581 size_t alloc;
582 size_t len;
583 size_t pos;
584 int in;
585 int out;
586 int any_written;
587 unsigned gzip_request : 1;
588 unsigned initial_buffer : 1;
591 * Whenever a pkt-line is read into buf, append the 4 characters
592 * denoting its length before appending the payload.
594 unsigned write_line_lengths : 1;
597 * Used by rpc_out; initialize to 0. This is true if a flush has been
598 * read, but the corresponding line length (if write_line_lengths is
599 * true) and EOF have not been sent to libcurl. Since each flush marks
600 * the end of a request, each flush must be completely sent before any
601 * further reading occurs.
603 unsigned flush_read_but_not_sent : 1;
607 * Appends the result of reading from rpc->out to the string represented by
608 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
609 * enough space, 0 otherwise.
611 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
612 * hexadecimal string before appending the result described above.
614 * Writes the total number of bytes appended into appended.
616 static int rpc_read_from_out(struct rpc_state *rpc, int options,
617 size_t *appended,
618 enum packet_read_status *status) {
619 size_t left;
620 char *buf;
621 int pktlen_raw;
623 if (rpc->write_line_lengths) {
624 left = rpc->alloc - rpc->len - 4;
625 buf = rpc->buf + rpc->len + 4;
626 } else {
627 left = rpc->alloc - rpc->len;
628 buf = rpc->buf + rpc->len;
631 if (left < LARGE_PACKET_MAX)
632 return 0;
634 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
635 left, &pktlen_raw, options);
636 if (*status != PACKET_READ_EOF) {
637 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
638 rpc->len += *appended;
641 if (rpc->write_line_lengths) {
642 switch (*status) {
643 case PACKET_READ_EOF:
644 if (!(options & PACKET_READ_GENTLE_ON_EOF))
645 die(_("shouldn't have EOF when not gentle on EOF"));
646 break;
647 case PACKET_READ_NORMAL:
648 set_packet_header(buf - 4, *appended);
649 break;
650 case PACKET_READ_DELIM:
651 memcpy(buf - 4, "0001", 4);
652 break;
653 case PACKET_READ_FLUSH:
654 memcpy(buf - 4, "0000", 4);
655 break;
656 case PACKET_READ_RESPONSE_END:
657 die(_("remote server sent unexpected response end packet"));
661 return 1;
664 static size_t rpc_out(void *ptr, size_t eltsize,
665 size_t nmemb, void *buffer_)
667 size_t max = eltsize * nmemb;
668 struct rpc_state *rpc = buffer_;
669 size_t avail = rpc->len - rpc->pos;
670 enum packet_read_status status;
672 if (!avail) {
673 rpc->initial_buffer = 0;
674 rpc->len = 0;
675 rpc->pos = 0;
676 if (!rpc->flush_read_but_not_sent) {
677 if (!rpc_read_from_out(rpc, 0, &avail, &status))
678 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
679 if (status == PACKET_READ_FLUSH)
680 rpc->flush_read_but_not_sent = 1;
683 * If flush_read_but_not_sent is true, we have already read one
684 * full request but have not fully sent it + EOF, which is why
685 * we need to refrain from reading.
688 if (rpc->flush_read_but_not_sent) {
689 if (!avail) {
691 * The line length either does not need to be sent at
692 * all or has already been completely sent. Now we can
693 * return 0, indicating EOF, meaning that the flush has
694 * been fully sent.
696 rpc->flush_read_but_not_sent = 0;
697 return 0;
700 * If avail is non-zero, the line length for the flush still
701 * hasn't been fully sent. Proceed with sending the line
702 * length.
706 if (max < avail)
707 avail = max;
708 memcpy(ptr, rpc->buf + rpc->pos, avail);
709 rpc->pos += avail;
710 return avail;
713 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
715 struct rpc_state *rpc = clientp;
717 if (origin != SEEK_SET)
718 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
720 if (rpc->initial_buffer) {
721 if (offset < 0 || offset > rpc->len) {
722 error("curl seek would be outside of rpc buffer");
723 return CURL_SEEKFUNC_FAIL;
725 rpc->pos = offset;
726 return CURL_SEEKFUNC_OK;
728 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
729 return CURL_SEEKFUNC_FAIL;
732 struct check_pktline_state {
733 char len_buf[4];
734 int len_filled;
735 int remaining;
738 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
740 while (size) {
741 if (!state->remaining) {
742 int digits_remaining = 4 - state->len_filled;
743 if (digits_remaining > size)
744 digits_remaining = size;
745 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
746 state->len_filled += digits_remaining;
747 ptr += digits_remaining;
748 size -= digits_remaining;
750 if (state->len_filled == 4) {
751 state->remaining = packet_length(state->len_buf);
752 if (state->remaining < 0) {
753 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
754 } else if (state->remaining == 2) {
755 die(_("remote-curl: unexpected response end packet"));
756 } else if (state->remaining < 4) {
757 state->remaining = 0;
758 } else {
759 state->remaining -= 4;
761 state->len_filled = 0;
765 if (state->remaining) {
766 int remaining = state->remaining;
767 if (remaining > size)
768 remaining = size;
769 ptr += remaining;
770 size -= remaining;
771 state->remaining -= remaining;
776 struct rpc_in_data {
777 struct rpc_state *rpc;
778 struct active_request_slot *slot;
779 int check_pktline;
780 struct check_pktline_state pktline_state;
784 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
785 * from ptr.
787 static size_t rpc_in(char *ptr, size_t eltsize,
788 size_t nmemb, void *buffer_)
790 size_t size = eltsize * nmemb;
791 struct rpc_in_data *data = buffer_;
792 long response_code;
794 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
795 &response_code) != CURLE_OK)
796 return size;
797 if (response_code >= 300)
798 return size;
799 if (size)
800 data->rpc->any_written = 1;
801 if (data->check_pktline)
802 check_pktline(&data->pktline_state, ptr, size);
803 write_or_die(data->rpc->in, ptr, size);
804 return size;
807 static int run_slot(struct active_request_slot *slot,
808 struct slot_results *results)
810 int err;
811 struct slot_results results_buf;
813 if (!results)
814 results = &results_buf;
816 err = run_one_slot(slot, results);
818 if (err != HTTP_OK && err != HTTP_REAUTH) {
819 struct strbuf msg = STRBUF_INIT;
820 if (results->http_code && results->http_code != 200)
821 strbuf_addf(&msg, "HTTP %ld", results->http_code);
822 if (results->curl_result != CURLE_OK) {
823 if (msg.len)
824 strbuf_addch(&msg, ' ');
825 strbuf_addf(&msg, "curl %d", results->curl_result);
826 if (curl_errorstr[0]) {
827 strbuf_addch(&msg, ' ');
828 strbuf_addstr(&msg, curl_errorstr);
831 error(_("RPC failed; %s"), msg.buf);
832 strbuf_release(&msg);
835 return err;
838 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
840 struct active_request_slot *slot;
841 struct curl_slist *headers = http_copy_default_headers();
842 struct strbuf buf = STRBUF_INIT;
843 int err;
845 slot = get_active_slot();
847 headers = curl_slist_append(headers, rpc->hdr_content_type);
848 headers = curl_slist_append(headers, rpc->hdr_accept);
850 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
851 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
852 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
853 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
854 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
855 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
856 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
857 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
858 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
860 err = run_slot(slot, results);
862 curl_slist_free_all(headers);
863 strbuf_release(&buf);
864 return err;
867 static curl_off_t xcurl_off_t(size_t len)
869 uintmax_t size = len;
870 if (size > maximum_signed_value_of_type(curl_off_t))
871 die(_("cannot handle pushes this big"));
872 return (curl_off_t)size;
876 * If flush_received is true, do not attempt to read any more; just use what's
877 * in rpc->buf.
879 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
881 struct active_request_slot *slot;
882 struct curl_slist *headers = http_copy_default_headers();
883 int use_gzip = rpc->gzip_request;
884 char *gzip_body = NULL;
885 size_t gzip_size = 0;
886 int err, large_request = 0;
887 int needs_100_continue = 0;
888 struct rpc_in_data rpc_in_data;
890 /* Try to load the entire request, if we can fit it into the
891 * allocated buffer space we can use HTTP/1.0 and avoid the
892 * chunked encoding mess.
894 if (!flush_received) {
895 while (1) {
896 size_t n;
897 enum packet_read_status status;
899 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
900 large_request = 1;
901 use_gzip = 0;
902 break;
904 if (status == PACKET_READ_FLUSH)
905 break;
909 if (large_request) {
910 struct slot_results results;
912 do {
913 err = probe_rpc(rpc, &results);
914 if (err == HTTP_REAUTH)
915 credential_fill(&http_auth);
916 } while (err == HTTP_REAUTH);
917 if (err != HTTP_OK)
918 return -1;
920 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
921 needs_100_continue = 1;
924 headers = curl_slist_append(headers, rpc->hdr_content_type);
925 headers = curl_slist_append(headers, rpc->hdr_accept);
926 headers = curl_slist_append(headers, needs_100_continue ?
927 "Expect: 100-continue" : "Expect:");
929 /* Add the extra Git-Protocol header */
930 if (rpc->protocol_header)
931 headers = curl_slist_append(headers, rpc->protocol_header);
933 retry:
934 slot = get_active_slot();
936 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
937 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
938 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
939 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
941 if (large_request) {
942 /* The request body is large and the size cannot be predicted.
943 * We must use chunked encoding to send it.
945 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
946 rpc->initial_buffer = 1;
947 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
948 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
949 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
950 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
951 if (options.verbosity > 1) {
952 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
953 fflush(stderr);
956 } else if (gzip_body) {
958 * If we are looping to retry authentication, then the previous
959 * run will have set up the headers and gzip buffer already,
960 * and we just need to send it.
962 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
963 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
965 } else if (use_gzip && 1024 < rpc->len) {
966 /* The client backend isn't giving us compressed data so
967 * we can try to deflate it ourselves, this may save on
968 * the transfer time.
970 git_zstream stream;
971 int ret;
973 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
974 gzip_size = git_deflate_bound(&stream, rpc->len);
975 gzip_body = xmalloc(gzip_size);
977 stream.next_in = (unsigned char *)rpc->buf;
978 stream.avail_in = rpc->len;
979 stream.next_out = (unsigned char *)gzip_body;
980 stream.avail_out = gzip_size;
982 ret = git_deflate(&stream, Z_FINISH);
983 if (ret != Z_STREAM_END)
984 die(_("cannot deflate request; zlib deflate error %d"), ret);
986 ret = git_deflate_end_gently(&stream);
987 if (ret != Z_OK)
988 die(_("cannot deflate request; zlib end error %d"), ret);
990 gzip_size = stream.total_out;
992 headers = curl_slist_append(headers, "Content-Encoding: gzip");
993 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
994 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
996 if (options.verbosity > 1) {
997 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
998 rpc->service_name,
999 (unsigned long)rpc->len, (unsigned long)gzip_size);
1000 fflush(stderr);
1002 } else {
1003 /* We know the complete request size in advance, use the
1004 * more normal Content-Length approach.
1006 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1007 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1008 if (options.verbosity > 1) {
1009 fprintf(stderr, "POST %s (%lu bytes)\n",
1010 rpc->service_name, (unsigned long)rpc->len);
1011 fflush(stderr);
1015 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1016 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1017 rpc_in_data.rpc = rpc;
1018 rpc_in_data.slot = slot;
1019 rpc_in_data.check_pktline = stateless_connect;
1020 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1021 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1022 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1025 rpc->any_written = 0;
1026 err = run_slot(slot, NULL);
1027 if (err == HTTP_REAUTH && !large_request) {
1028 credential_fill(&http_auth);
1029 goto retry;
1031 if (err != HTTP_OK)
1032 err = -1;
1034 if (!rpc->any_written)
1035 err = -1;
1037 if (rpc_in_data.pktline_state.len_filled)
1038 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1039 if (rpc_in_data.pktline_state.remaining)
1040 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1042 if (stateless_connect)
1043 packet_response_end(rpc->in);
1045 curl_slist_free_all(headers);
1046 free(gzip_body);
1047 return err;
1050 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1051 const char **client_argv, const struct strbuf *preamble,
1052 struct strbuf *rpc_result)
1054 const char *svc = rpc->service_name;
1055 struct strbuf buf = STRBUF_INIT;
1056 struct child_process client = CHILD_PROCESS_INIT;
1057 int err = 0;
1059 client.in = -1;
1060 client.out = -1;
1061 client.git_cmd = 1;
1062 client.argv = client_argv;
1063 if (start_command(&client))
1064 exit(1);
1065 write_or_die(client.in, preamble->buf, preamble->len);
1066 if (heads)
1067 write_or_die(client.in, heads->buf, heads->len);
1069 rpc->alloc = http_post_buffer;
1070 rpc->buf = xmalloc(rpc->alloc);
1071 rpc->in = client.in;
1072 rpc->out = client.out;
1074 strbuf_addf(&buf, "%s%s", url.buf, svc);
1075 rpc->service_url = strbuf_detach(&buf, NULL);
1077 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1078 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1080 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1081 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1083 if (get_protocol_http_header(heads->version, &buf))
1084 rpc->protocol_header = strbuf_detach(&buf, NULL);
1085 else
1086 rpc->protocol_header = NULL;
1088 while (!err) {
1089 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1090 if (!n)
1091 break;
1092 rpc->pos = 0;
1093 rpc->len = n;
1094 err |= post_rpc(rpc, 0, 0);
1097 close(client.in);
1098 client.in = -1;
1099 if (!err) {
1100 strbuf_read(rpc_result, client.out, 0);
1101 } else {
1102 char buf[4096];
1103 for (;;)
1104 if (xread(client.out, buf, sizeof(buf)) <= 0)
1105 break;
1108 close(client.out);
1109 client.out = -1;
1111 err |= finish_command(&client);
1112 free(rpc->service_url);
1113 free(rpc->hdr_content_type);
1114 free(rpc->hdr_accept);
1115 free(rpc->protocol_header);
1116 free(rpc->buf);
1117 strbuf_release(&buf);
1118 return err;
1121 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1123 struct walker *walker;
1124 char **targets;
1125 int ret, i;
1127 ALLOC_ARRAY(targets, nr_heads);
1128 if (options.depth || options.deepen_since)
1129 die(_("dumb http transport does not support shallow capabilities"));
1130 for (i = 0; i < nr_heads; i++)
1131 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1133 walker = get_http_walker(url.buf);
1134 walker->get_verbosely = options.verbosity >= 3;
1135 walker->get_progress = options.progress;
1136 walker->get_recover = 0;
1137 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1138 walker_free(walker);
1140 for (i = 0; i < nr_heads; i++)
1141 free(targets[i]);
1142 free(targets);
1144 return ret ? error(_("fetch failed.")) : 0;
1147 static int fetch_git(struct discovery *heads,
1148 int nr_heads, struct ref **to_fetch)
1150 struct rpc_state rpc;
1151 struct strbuf preamble = STRBUF_INIT;
1152 int i, err;
1153 struct strvec args = STRVEC_INIT;
1154 struct strbuf rpc_result = STRBUF_INIT;
1156 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1157 "--stdin", "--lock-pack", NULL);
1158 if (options.followtags)
1159 strvec_push(&args, "--include-tag");
1160 if (options.thin)
1161 strvec_push(&args, "--thin");
1162 if (options.verbosity >= 3)
1163 strvec_pushl(&args, "-v", "-v", NULL);
1164 if (options.check_self_contained_and_connected)
1165 strvec_push(&args, "--check-self-contained-and-connected");
1166 if (options.cloning)
1167 strvec_push(&args, "--cloning");
1168 if (options.update_shallow)
1169 strvec_push(&args, "--update-shallow");
1170 if (!options.progress)
1171 strvec_push(&args, "--no-progress");
1172 if (options.depth)
1173 strvec_pushf(&args, "--depth=%lu", options.depth);
1174 if (options.deepen_since)
1175 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1176 for (i = 0; i < options.deepen_not.nr; i++)
1177 strvec_pushf(&args, "--shallow-exclude=%s",
1178 options.deepen_not.items[i].string);
1179 if (options.deepen_relative && options.depth)
1180 strvec_push(&args, "--deepen-relative");
1181 if (options.from_promisor)
1182 strvec_push(&args, "--from-promisor");
1183 if (options.filter)
1184 strvec_pushf(&args, "--filter=%s", options.filter);
1185 strvec_push(&args, url.buf);
1187 for (i = 0; i < nr_heads; i++) {
1188 struct ref *ref = to_fetch[i];
1189 if (!*ref->name)
1190 die(_("cannot fetch by sha1 over smart http"));
1191 packet_buf_write(&preamble, "%s %s\n",
1192 oid_to_hex(&ref->old_oid), ref->name);
1194 packet_buf_flush(&preamble);
1196 memset(&rpc, 0, sizeof(rpc));
1197 rpc.service_name = "git-upload-pack",
1198 rpc.gzip_request = 1;
1200 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1201 if (rpc_result.len)
1202 write_or_die(1, rpc_result.buf, rpc_result.len);
1203 strbuf_release(&rpc_result);
1204 strbuf_release(&preamble);
1205 strvec_clear(&args);
1206 return err;
1209 static int fetch(int nr_heads, struct ref **to_fetch)
1211 struct discovery *d = discover_refs("git-upload-pack", 0);
1212 if (d->proto_git)
1213 return fetch_git(d, nr_heads, to_fetch);
1214 else
1215 return fetch_dumb(nr_heads, to_fetch);
1218 static void parse_fetch(struct strbuf *buf)
1220 struct ref **to_fetch = NULL;
1221 struct ref *list_head = NULL;
1222 struct ref **list = &list_head;
1223 int alloc_heads = 0, nr_heads = 0;
1225 do {
1226 const char *p;
1227 if (skip_prefix(buf->buf, "fetch ", &p)) {
1228 const char *name;
1229 struct ref *ref;
1230 struct object_id old_oid;
1231 const char *q;
1233 if (parse_oid_hex(p, &old_oid, &q))
1234 die(_("protocol error: expected sha/ref, got '%s'"), p);
1235 if (*q == ' ')
1236 name = q + 1;
1237 else if (!*q)
1238 name = "";
1239 else
1240 die(_("protocol error: expected sha/ref, got '%s'"), p);
1242 ref = alloc_ref(name);
1243 oidcpy(&ref->old_oid, &old_oid);
1245 *list = ref;
1246 list = &ref->next;
1248 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1249 to_fetch[nr_heads++] = ref;
1251 else
1252 die(_("http transport does not support %s"), buf->buf);
1254 strbuf_reset(buf);
1255 if (strbuf_getline_lf(buf, stdin) == EOF)
1256 return;
1257 if (!*buf->buf)
1258 break;
1259 } while (1);
1261 if (fetch(nr_heads, to_fetch))
1262 exit(128); /* error already reported */
1263 free_refs(list_head);
1264 free(to_fetch);
1266 printf("\n");
1267 fflush(stdout);
1268 strbuf_reset(buf);
1271 static int push_dav(int nr_spec, const char **specs)
1273 struct child_process child = CHILD_PROCESS_INIT;
1274 size_t i;
1276 child.git_cmd = 1;
1277 strvec_push(&child.args, "http-push");
1278 strvec_push(&child.args, "--helper-status");
1279 if (options.dry_run)
1280 strvec_push(&child.args, "--dry-run");
1281 if (options.verbosity > 1)
1282 strvec_push(&child.args, "--verbose");
1283 strvec_push(&child.args, url.buf);
1284 for (i = 0; i < nr_spec; i++)
1285 strvec_push(&child.args, specs[i]);
1287 if (run_command(&child))
1288 die(_("git-http-push failed"));
1289 return 0;
1292 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1294 struct rpc_state rpc;
1295 int i, err;
1296 struct strvec args;
1297 struct string_list_item *cas_option;
1298 struct strbuf preamble = STRBUF_INIT;
1299 struct strbuf rpc_result = STRBUF_INIT;
1301 strvec_init(&args);
1302 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1303 NULL);
1305 if (options.thin)
1306 strvec_push(&args, "--thin");
1307 if (options.dry_run)
1308 strvec_push(&args, "--dry-run");
1309 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1310 strvec_push(&args, "--signed=yes");
1311 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1312 strvec_push(&args, "--signed=if-asked");
1313 if (options.atomic)
1314 strvec_push(&args, "--atomic");
1315 if (options.verbosity == 0)
1316 strvec_push(&args, "--quiet");
1317 else if (options.verbosity > 1)
1318 strvec_push(&args, "--verbose");
1319 for (i = 0; i < options.push_options.nr; i++)
1320 strvec_pushf(&args, "--push-option=%s",
1321 options.push_options.items[i].string);
1322 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1323 for_each_string_list_item(cas_option, &cas_options)
1324 strvec_push(&args, cas_option->string);
1325 strvec_push(&args, url.buf);
1327 if (options.force_if_includes)
1328 strvec_push(&args, "--force-if-includes");
1330 strvec_push(&args, "--stdin");
1331 for (i = 0; i < nr_spec; i++)
1332 packet_buf_write(&preamble, "%s\n", specs[i]);
1333 packet_buf_flush(&preamble);
1335 memset(&rpc, 0, sizeof(rpc));
1336 rpc.service_name = "git-receive-pack",
1338 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1339 if (rpc_result.len)
1340 write_or_die(1, rpc_result.buf, rpc_result.len);
1341 strbuf_release(&rpc_result);
1342 strbuf_release(&preamble);
1343 strvec_clear(&args);
1344 return err;
1347 static int push(int nr_spec, const char **specs)
1349 struct discovery *heads = discover_refs("git-receive-pack", 1);
1350 int ret;
1352 if (heads->proto_git)
1353 ret = push_git(heads, nr_spec, specs);
1354 else
1355 ret = push_dav(nr_spec, specs);
1356 free_discovery(heads);
1357 return ret;
1360 static void parse_push(struct strbuf *buf)
1362 struct strvec specs = STRVEC_INIT;
1363 int ret;
1365 do {
1366 const char *arg;
1367 if (skip_prefix(buf->buf, "push ", &arg))
1368 strvec_push(&specs, arg);
1369 else
1370 die(_("http transport does not support %s"), buf->buf);
1372 strbuf_reset(buf);
1373 if (strbuf_getline_lf(buf, stdin) == EOF)
1374 goto free_specs;
1375 if (!*buf->buf)
1376 break;
1377 } while (1);
1379 ret = push(specs.nr, specs.v);
1380 printf("\n");
1381 fflush(stdout);
1383 if (ret)
1384 exit(128); /* error already reported */
1386 free_specs:
1387 strvec_clear(&specs);
1390 static int stateless_connect(const char *service_name)
1392 struct discovery *discover;
1393 struct rpc_state rpc;
1394 struct strbuf buf = STRBUF_INIT;
1397 * Run the info/refs request and see if the server supports protocol
1398 * v2. If and only if the server supports v2 can we successfully
1399 * establish a stateless connection, otherwise we need to tell the
1400 * client to fallback to using other transport helper functions to
1401 * complete their request.
1403 discover = discover_refs(service_name, 0);
1404 if (discover->version != protocol_v2) {
1405 printf("fallback\n");
1406 fflush(stdout);
1407 return -1;
1408 } else {
1409 /* Stateless Connection established */
1410 printf("\n");
1411 fflush(stdout);
1414 rpc.service_name = service_name;
1415 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1416 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1417 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1418 if (get_protocol_http_header(discover->version, &buf)) {
1419 rpc.protocol_header = strbuf_detach(&buf, NULL);
1420 } else {
1421 rpc.protocol_header = NULL;
1422 strbuf_release(&buf);
1424 rpc.buf = xmalloc(http_post_buffer);
1425 rpc.alloc = http_post_buffer;
1426 rpc.len = 0;
1427 rpc.pos = 0;
1428 rpc.in = 1;
1429 rpc.out = 0;
1430 rpc.any_written = 0;
1431 rpc.gzip_request = 1;
1432 rpc.initial_buffer = 0;
1433 rpc.write_line_lengths = 1;
1434 rpc.flush_read_but_not_sent = 0;
1437 * Dump the capability listing that we got from the server earlier
1438 * during the info/refs request.
1440 write_or_die(rpc.in, discover->buf, discover->len);
1442 /* Until we see EOF keep sending POSTs */
1443 while (1) {
1444 size_t avail;
1445 enum packet_read_status status;
1447 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1448 &status))
1449 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1450 if (status == PACKET_READ_EOF)
1451 break;
1452 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1453 /* We would have an err here */
1454 break;
1455 /* Reset the buffer for next request */
1456 rpc.len = 0;
1459 free(rpc.service_url);
1460 free(rpc.hdr_content_type);
1461 free(rpc.hdr_accept);
1462 free(rpc.protocol_header);
1463 free(rpc.buf);
1464 strbuf_release(&buf);
1466 return 0;
1469 int cmd_main(int argc, const char **argv)
1471 struct strbuf buf = STRBUF_INIT;
1472 int nongit;
1474 setup_git_directory_gently(&nongit);
1475 if (argc < 2) {
1476 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1477 return 1;
1480 options.verbosity = 1;
1481 options.progress = !!isatty(2);
1482 options.thin = 1;
1483 string_list_init_dup(&options.deepen_not);
1484 string_list_init_dup(&options.push_options);
1487 * Just report "remote-curl" here (folding all the various aliases
1488 * ("git-remote-http", "git-remote-https", and etc.) here since they
1489 * are all just copies of the same actual executable.
1491 trace2_cmd_name("remote-curl");
1493 remote = remote_get(argv[1]);
1495 if (argc > 2) {
1496 end_url_with_slash(&url, argv[2]);
1497 } else {
1498 end_url_with_slash(&url, remote->url[0]);
1501 http_init(remote, url.buf, 0);
1503 do {
1504 const char *arg;
1506 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1507 if (ferror(stdin))
1508 error(_("remote-curl: error reading command stream from git"));
1509 return 1;
1511 if (buf.len == 0)
1512 break;
1513 if (starts_with(buf.buf, "fetch ")) {
1514 if (nongit)
1515 die(_("remote-curl: fetch attempted without a local repo"));
1516 parse_fetch(&buf);
1518 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1519 int for_push = !!strstr(buf.buf + 4, "for-push");
1520 output_refs(get_refs(for_push));
1522 } else if (starts_with(buf.buf, "push ")) {
1523 parse_push(&buf);
1525 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1526 char *value = strchr(arg, ' ');
1527 int result;
1529 if (value)
1530 *value++ = '\0';
1531 else
1532 value = "true";
1534 result = set_option(arg, value);
1535 if (!result)
1536 printf("ok\n");
1537 else if (result < 0)
1538 printf("error invalid value\n");
1539 else
1540 printf("unsupported\n");
1541 fflush(stdout);
1543 } else if (!strcmp(buf.buf, "capabilities")) {
1544 printf("stateless-connect\n");
1545 printf("fetch\n");
1546 printf("option\n");
1547 printf("push\n");
1548 printf("check-connectivity\n");
1549 printf("object-format\n");
1550 printf("\n");
1551 fflush(stdout);
1552 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1553 if (!stateless_connect(arg))
1554 break;
1555 } else {
1556 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1557 return 1;
1559 strbuf_reset(&buf);
1560 } while (1);
1562 http_cleanup();
1564 return 0;