Merge branch 'ry/clarify-fast-forward-in-glossary'
[git/debian.git] / remote-curl.c
blob9d432c299a242e4f0b337756bc6aedfcc5be45b8
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;
189 #if LIBCURL_VERSION_NUM >= 0x070a08
190 } else if (!strcmp(name, "family")) {
191 if (!strcmp(value, "ipv4"))
192 git_curl_ipresolve = CURL_IPRESOLVE_V4;
193 else if (!strcmp(value, "ipv6"))
194 git_curl_ipresolve = CURL_IPRESOLVE_V6;
195 else if (!strcmp(value, "all"))
196 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
197 else
198 return -1;
199 return 0;
200 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
201 } else if (!strcmp(name, "from-promisor")) {
202 options.from_promisor = 1;
203 return 0;
204 } else if (!strcmp(name, "filter")) {
205 options.filter = xstrdup(value);
206 return 0;
207 } else if (!strcmp(name, "object-format")) {
208 int algo;
209 options.object_format = 1;
210 if (strcmp(value, "true")) {
211 algo = hash_algo_by_name(value);
212 if (algo == GIT_HASH_UNKNOWN)
213 die("unknown object format '%s'", value);
214 options.hash_algo = &hash_algos[algo];
216 return 0;
217 } else {
218 return 1 /* unsupported */;
222 struct discovery {
223 char *service;
224 char *buf_alloc;
225 char *buf;
226 size_t len;
227 struct ref *refs;
228 struct oid_array shallow;
229 enum protocol_version version;
230 unsigned proto_git : 1;
232 static struct discovery *last_discovery;
234 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
236 struct ref *list = NULL;
237 struct packet_reader reader;
239 packet_reader_init(&reader, -1, heads->buf, heads->len,
240 PACKET_READ_CHOMP_NEWLINE |
241 PACKET_READ_GENTLE_ON_EOF |
242 PACKET_READ_DIE_ON_ERR_PACKET);
244 heads->version = discover_version(&reader);
245 switch (heads->version) {
246 case protocol_v2:
248 * Do nothing. This isn't a list of refs but rather a
249 * capability advertisement. Client would have run
250 * 'stateless-connect' so we'll dump this capability listing
251 * and let them request the refs themselves.
253 break;
254 case protocol_v1:
255 case protocol_v0:
256 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
257 NULL, &heads->shallow);
258 options.hash_algo = reader.hash_algo;
259 break;
260 case protocol_unknown_version:
261 BUG("unknown protocol version");
264 return list;
267 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
269 const char *p = memchr(heads->buf, '\t', heads->len);
270 int algo;
271 if (!p)
272 return the_hash_algo;
274 algo = hash_algo_by_length((p - heads->buf) / 2);
275 if (algo == GIT_HASH_UNKNOWN)
276 return NULL;
277 return &hash_algos[algo];
280 static struct ref *parse_info_refs(struct discovery *heads)
282 char *data, *start, *mid;
283 char *ref_name;
284 int i = 0;
286 struct ref *refs = NULL;
287 struct ref *ref = NULL;
288 struct ref *last_ref = NULL;
290 options.hash_algo = detect_hash_algo(heads);
291 if (!options.hash_algo)
292 die("%sinfo/refs not valid: could not determine hash algorithm; "
293 "is this a git repository?",
294 transport_anonymize_url(url.buf));
296 data = heads->buf;
297 start = NULL;
298 mid = data;
299 while (i < heads->len) {
300 if (!start) {
301 start = &data[i];
303 if (data[i] == '\t')
304 mid = &data[i];
305 if (data[i] == '\n') {
306 if (mid - start != options.hash_algo->hexsz)
307 die(_("%sinfo/refs not valid: is this a git repository?"),
308 transport_anonymize_url(url.buf));
309 data[i] = 0;
310 ref_name = mid + 1;
311 ref = alloc_ref(ref_name);
312 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
313 if (!refs)
314 refs = ref;
315 if (last_ref)
316 last_ref->next = ref;
317 last_ref = ref;
318 start = NULL;
320 i++;
323 ref = alloc_ref("HEAD");
324 if (!http_fetch_ref(url.buf, ref) &&
325 !resolve_remote_symref(ref, refs)) {
326 ref->next = refs;
327 refs = ref;
328 } else {
329 free(ref);
332 return refs;
335 static void free_discovery(struct discovery *d)
337 if (d) {
338 if (d == last_discovery)
339 last_discovery = NULL;
340 free(d->shallow.oid);
341 free(d->buf_alloc);
342 free_refs(d->refs);
343 free(d->service);
344 free(d);
348 static int show_http_message(struct strbuf *type, struct strbuf *charset,
349 struct strbuf *msg)
351 const char *p, *eol;
354 * We only show text/plain parts, as other types are likely
355 * to be ugly to look at on the user's terminal.
357 if (strcmp(type->buf, "text/plain"))
358 return -1;
359 if (charset->len)
360 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
362 strbuf_trim(msg);
363 if (!msg->len)
364 return -1;
366 p = msg->buf;
367 do {
368 eol = strchrnul(p, '\n');
369 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
370 p = eol + 1;
371 } while(*eol);
372 return 0;
375 static int get_protocol_http_header(enum protocol_version version,
376 struct strbuf *header)
378 if (version > 0) {
379 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
380 version);
382 return 1;
385 return 0;
388 static void check_smart_http(struct discovery *d, const char *service,
389 struct strbuf *type)
391 const char *p;
392 struct packet_reader reader;
395 * If we don't see x-$service-advertisement, then it's not smart-http.
396 * But once we do, we commit to it and assume any other protocol
397 * violations are hard errors.
399 if (!skip_prefix(type->buf, "application/x-", &p) ||
400 !skip_prefix(p, service, &p) ||
401 strcmp(p, "-advertisement"))
402 return;
404 packet_reader_init(&reader, -1, d->buf, d->len,
405 PACKET_READ_CHOMP_NEWLINE |
406 PACKET_READ_DIE_ON_ERR_PACKET);
407 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
408 die(_("invalid server response; expected service, got flush packet"));
410 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
412 * The header can include additional metadata lines, up
413 * until a packet flush marker. Ignore these now, but
414 * in the future we might start to scan them.
416 for (;;) {
417 packet_reader_read(&reader);
418 if (reader.pktlen <= 0) {
419 break;
424 * v0 smart http; callers expect us to soak up the
425 * service and header packets
427 d->buf = reader.src_buffer;
428 d->len = reader.src_len;
429 d->proto_git = 1;
431 } else if (!strcmp(reader.line, "version 2")) {
433 * v2 smart http; do not consume version packet, which will
434 * be handled elsewhere.
436 d->proto_git = 1;
438 } else {
439 die(_("invalid server response; got '%s'"), reader.line);
443 static struct discovery *discover_refs(const char *service, int for_push)
445 struct strbuf type = STRBUF_INIT;
446 struct strbuf charset = STRBUF_INIT;
447 struct strbuf buffer = STRBUF_INIT;
448 struct strbuf refs_url = STRBUF_INIT;
449 struct strbuf effective_url = STRBUF_INIT;
450 struct strbuf protocol_header = STRBUF_INIT;
451 struct string_list extra_headers = STRING_LIST_INIT_DUP;
452 struct discovery *last = last_discovery;
453 int http_ret, maybe_smart = 0;
454 struct http_get_options http_options;
455 enum protocol_version version = get_protocol_version_config();
457 if (last && !strcmp(service, last->service))
458 return last;
459 free_discovery(last);
461 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
462 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
463 git_env_bool("GIT_SMART_HTTP", 1)) {
464 maybe_smart = 1;
465 if (!strchr(url.buf, '?'))
466 strbuf_addch(&refs_url, '?');
467 else
468 strbuf_addch(&refs_url, '&');
469 strbuf_addf(&refs_url, "service=%s", service);
473 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
474 * to perform a push, then fallback to v0 since the client doesn't know
475 * how to push yet using v2.
477 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
478 version = protocol_v0;
480 /* Add the extra Git-Protocol header */
481 if (get_protocol_http_header(version, &protocol_header))
482 string_list_append(&extra_headers, protocol_header.buf);
484 memset(&http_options, 0, sizeof(http_options));
485 http_options.content_type = &type;
486 http_options.charset = &charset;
487 http_options.effective_url = &effective_url;
488 http_options.base_url = &url;
489 http_options.extra_headers = &extra_headers;
490 http_options.initial_request = 1;
491 http_options.no_cache = 1;
493 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
494 switch (http_ret) {
495 case HTTP_OK:
496 break;
497 case HTTP_MISSING_TARGET:
498 show_http_message(&type, &charset, &buffer);
499 die(_("repository '%s' not found"),
500 transport_anonymize_url(url.buf));
501 case HTTP_NOAUTH:
502 show_http_message(&type, &charset, &buffer);
503 die(_("Authentication failed for '%s'"),
504 transport_anonymize_url(url.buf));
505 default:
506 show_http_message(&type, &charset, &buffer);
507 die(_("unable to access '%s': %s"),
508 transport_anonymize_url(url.buf), curl_errorstr);
511 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
512 char *u = transport_anonymize_url(url.buf);
513 warning(_("redirecting to %s"), u);
514 free(u);
517 last= xcalloc(1, sizeof(*last_discovery));
518 last->service = xstrdup(service);
519 last->buf_alloc = strbuf_detach(&buffer, &last->len);
520 last->buf = last->buf_alloc;
522 if (maybe_smart)
523 check_smart_http(last, service, &type);
525 if (last->proto_git)
526 last->refs = parse_git_refs(last, for_push);
527 else
528 last->refs = parse_info_refs(last);
530 strbuf_release(&refs_url);
531 strbuf_release(&type);
532 strbuf_release(&charset);
533 strbuf_release(&effective_url);
534 strbuf_release(&buffer);
535 strbuf_release(&protocol_header);
536 string_list_clear(&extra_headers, 0);
537 last_discovery = last;
538 return last;
541 static struct ref *get_refs(int for_push)
543 struct discovery *heads;
545 if (for_push)
546 heads = discover_refs("git-receive-pack", for_push);
547 else
548 heads = discover_refs("git-upload-pack", for_push);
550 return heads->refs;
553 static void output_refs(struct ref *refs)
555 struct ref *posn;
556 if (options.object_format && options.hash_algo) {
557 printf(":object-format %s\n", options.hash_algo->name);
558 repo_set_hash_algo(the_repository,
559 hash_algo_by_ptr(options.hash_algo));
561 for (posn = refs; posn; posn = posn->next) {
562 if (posn->symref)
563 printf("@%s %s\n", posn->symref, posn->name);
564 else
565 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
566 options.hash_algo),
567 posn->name);
569 printf("\n");
570 fflush(stdout);
573 struct rpc_state {
574 const char *service_name;
575 char *service_url;
576 char *hdr_content_type;
577 char *hdr_accept;
578 char *protocol_header;
579 char *buf;
580 size_t alloc;
581 size_t len;
582 size_t pos;
583 int in;
584 int out;
585 int any_written;
586 unsigned gzip_request : 1;
587 unsigned initial_buffer : 1;
590 * Whenever a pkt-line is read into buf, append the 4 characters
591 * denoting its length before appending the payload.
593 unsigned write_line_lengths : 1;
596 * Used by rpc_out; initialize to 0. This is true if a flush has been
597 * read, but the corresponding line length (if write_line_lengths is
598 * true) and EOF have not been sent to libcurl. Since each flush marks
599 * the end of a request, each flush must be completely sent before any
600 * further reading occurs.
602 unsigned flush_read_but_not_sent : 1;
606 * Appends the result of reading from rpc->out to the string represented by
607 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
608 * enough space, 0 otherwise.
610 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
611 * hexadecimal string before appending the result described above.
613 * Writes the total number of bytes appended into appended.
615 static int rpc_read_from_out(struct rpc_state *rpc, int options,
616 size_t *appended,
617 enum packet_read_status *status) {
618 size_t left;
619 char *buf;
620 int pktlen_raw;
622 if (rpc->write_line_lengths) {
623 left = rpc->alloc - rpc->len - 4;
624 buf = rpc->buf + rpc->len + 4;
625 } else {
626 left = rpc->alloc - rpc->len;
627 buf = rpc->buf + rpc->len;
630 if (left < LARGE_PACKET_MAX)
631 return 0;
633 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
634 left, &pktlen_raw, options);
635 if (*status != PACKET_READ_EOF) {
636 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
637 rpc->len += *appended;
640 if (rpc->write_line_lengths) {
641 switch (*status) {
642 case PACKET_READ_EOF:
643 if (!(options & PACKET_READ_GENTLE_ON_EOF))
644 die(_("shouldn't have EOF when not gentle on EOF"));
645 break;
646 case PACKET_READ_NORMAL:
647 set_packet_header(buf - 4, *appended);
648 break;
649 case PACKET_READ_DELIM:
650 memcpy(buf - 4, "0001", 4);
651 break;
652 case PACKET_READ_FLUSH:
653 memcpy(buf - 4, "0000", 4);
654 break;
655 case PACKET_READ_RESPONSE_END:
656 die(_("remote server sent stateless separator"));
660 return 1;
663 static size_t rpc_out(void *ptr, size_t eltsize,
664 size_t nmemb, void *buffer_)
666 size_t max = eltsize * nmemb;
667 struct rpc_state *rpc = buffer_;
668 size_t avail = rpc->len - rpc->pos;
669 enum packet_read_status status;
671 if (!avail) {
672 rpc->initial_buffer = 0;
673 rpc->len = 0;
674 rpc->pos = 0;
675 if (!rpc->flush_read_but_not_sent) {
676 if (!rpc_read_from_out(rpc, 0, &avail, &status))
677 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
678 if (status == PACKET_READ_FLUSH)
679 rpc->flush_read_but_not_sent = 1;
682 * If flush_read_but_not_sent is true, we have already read one
683 * full request but have not fully sent it + EOF, which is why
684 * we need to refrain from reading.
687 if (rpc->flush_read_but_not_sent) {
688 if (!avail) {
690 * The line length either does not need to be sent at
691 * all or has already been completely sent. Now we can
692 * return 0, indicating EOF, meaning that the flush has
693 * been fully sent.
695 rpc->flush_read_but_not_sent = 0;
696 return 0;
699 * If avail is non-zero, the line length for the flush still
700 * hasn't been fully sent. Proceed with sending the line
701 * length.
705 if (max < avail)
706 avail = max;
707 memcpy(ptr, rpc->buf + rpc->pos, avail);
708 rpc->pos += avail;
709 return avail;
712 #ifndef NO_CURL_IOCTL
713 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
715 struct rpc_state *rpc = clientp;
717 switch (cmd) {
718 case CURLIOCMD_NOP:
719 return CURLIOE_OK;
721 case CURLIOCMD_RESTARTREAD:
722 if (rpc->initial_buffer) {
723 rpc->pos = 0;
724 return CURLIOE_OK;
726 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
727 return CURLIOE_FAILRESTART;
729 default:
730 return CURLIOE_UNKNOWNCMD;
733 #endif
735 struct check_pktline_state {
736 char len_buf[4];
737 int len_filled;
738 int remaining;
741 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
743 while (size) {
744 if (!state->remaining) {
745 int digits_remaining = 4 - state->len_filled;
746 if (digits_remaining > size)
747 digits_remaining = size;
748 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
749 state->len_filled += digits_remaining;
750 ptr += digits_remaining;
751 size -= digits_remaining;
753 if (state->len_filled == 4) {
754 state->remaining = packet_length(state->len_buf);
755 if (state->remaining < 0) {
756 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
757 } else if (state->remaining == 2) {
758 die(_("remote-curl: unexpected response end packet"));
759 } else if (state->remaining < 4) {
760 state->remaining = 0;
761 } else {
762 state->remaining -= 4;
764 state->len_filled = 0;
768 if (state->remaining) {
769 int remaining = state->remaining;
770 if (remaining > size)
771 remaining = size;
772 ptr += remaining;
773 size -= remaining;
774 state->remaining -= remaining;
779 struct rpc_in_data {
780 struct rpc_state *rpc;
781 struct active_request_slot *slot;
782 int check_pktline;
783 struct check_pktline_state pktline_state;
787 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
788 * from ptr.
790 static size_t rpc_in(char *ptr, size_t eltsize,
791 size_t nmemb, void *buffer_)
793 size_t size = eltsize * nmemb;
794 struct rpc_in_data *data = buffer_;
795 long response_code;
797 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
798 &response_code) != CURLE_OK)
799 return size;
800 if (response_code >= 300)
801 return size;
802 if (size)
803 data->rpc->any_written = 1;
804 if (data->check_pktline)
805 check_pktline(&data->pktline_state, ptr, size);
806 write_or_die(data->rpc->in, ptr, size);
807 return size;
810 static int run_slot(struct active_request_slot *slot,
811 struct slot_results *results)
813 int err;
814 struct slot_results results_buf;
816 if (!results)
817 results = &results_buf;
819 err = run_one_slot(slot, results);
821 if (err != HTTP_OK && err != HTTP_REAUTH) {
822 struct strbuf msg = STRBUF_INIT;
823 if (results->http_code && results->http_code != 200)
824 strbuf_addf(&msg, "HTTP %ld", results->http_code);
825 if (results->curl_result != CURLE_OK) {
826 if (msg.len)
827 strbuf_addch(&msg, ' ');
828 strbuf_addf(&msg, "curl %d", results->curl_result);
829 if (curl_errorstr[0]) {
830 strbuf_addch(&msg, ' ');
831 strbuf_addstr(&msg, curl_errorstr);
834 error(_("RPC failed; %s"), msg.buf);
835 strbuf_release(&msg);
838 return err;
841 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
843 struct active_request_slot *slot;
844 struct curl_slist *headers = http_copy_default_headers();
845 struct strbuf buf = STRBUF_INIT;
846 int err;
848 slot = get_active_slot();
850 headers = curl_slist_append(headers, rpc->hdr_content_type);
851 headers = curl_slist_append(headers, rpc->hdr_accept);
853 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
854 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
855 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
856 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
857 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
858 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
859 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
860 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
861 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
863 err = run_slot(slot, results);
865 curl_slist_free_all(headers);
866 strbuf_release(&buf);
867 return err;
870 static curl_off_t xcurl_off_t(size_t len)
872 uintmax_t size = len;
873 if (size > maximum_signed_value_of_type(curl_off_t))
874 die(_("cannot handle pushes this big"));
875 return (curl_off_t)size;
879 * If flush_received is true, do not attempt to read any more; just use what's
880 * in rpc->buf.
882 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
884 struct active_request_slot *slot;
885 struct curl_slist *headers = http_copy_default_headers();
886 int use_gzip = rpc->gzip_request;
887 char *gzip_body = NULL;
888 size_t gzip_size = 0;
889 int err, large_request = 0;
890 int needs_100_continue = 0;
891 struct rpc_in_data rpc_in_data;
893 /* Try to load the entire request, if we can fit it into the
894 * allocated buffer space we can use HTTP/1.0 and avoid the
895 * chunked encoding mess.
897 if (!flush_received) {
898 while (1) {
899 size_t n;
900 enum packet_read_status status;
902 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
903 large_request = 1;
904 use_gzip = 0;
905 break;
907 if (status == PACKET_READ_FLUSH)
908 break;
912 if (large_request) {
913 struct slot_results results;
915 do {
916 err = probe_rpc(rpc, &results);
917 if (err == HTTP_REAUTH)
918 credential_fill(&http_auth);
919 } while (err == HTTP_REAUTH);
920 if (err != HTTP_OK)
921 return -1;
923 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
924 needs_100_continue = 1;
927 headers = curl_slist_append(headers, rpc->hdr_content_type);
928 headers = curl_slist_append(headers, rpc->hdr_accept);
929 headers = curl_slist_append(headers, needs_100_continue ?
930 "Expect: 100-continue" : "Expect:");
932 /* Add the extra Git-Protocol header */
933 if (rpc->protocol_header)
934 headers = curl_slist_append(headers, rpc->protocol_header);
936 retry:
937 slot = get_active_slot();
939 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
940 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
941 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
942 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
944 if (large_request) {
945 /* The request body is large and the size cannot be predicted.
946 * We must use chunked encoding to send it.
948 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
949 rpc->initial_buffer = 1;
950 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
951 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
952 #ifndef NO_CURL_IOCTL
953 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
954 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
955 #endif
956 if (options.verbosity > 1) {
957 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
958 fflush(stderr);
961 } else if (gzip_body) {
963 * If we are looping to retry authentication, then the previous
964 * run will have set up the headers and gzip buffer already,
965 * and we just need to send it.
967 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
968 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
970 } else if (use_gzip && 1024 < rpc->len) {
971 /* The client backend isn't giving us compressed data so
972 * we can try to deflate it ourselves, this may save on
973 * the transfer time.
975 git_zstream stream;
976 int ret;
978 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
979 gzip_size = git_deflate_bound(&stream, rpc->len);
980 gzip_body = xmalloc(gzip_size);
982 stream.next_in = (unsigned char *)rpc->buf;
983 stream.avail_in = rpc->len;
984 stream.next_out = (unsigned char *)gzip_body;
985 stream.avail_out = gzip_size;
987 ret = git_deflate(&stream, Z_FINISH);
988 if (ret != Z_STREAM_END)
989 die(_("cannot deflate request; zlib deflate error %d"), ret);
991 ret = git_deflate_end_gently(&stream);
992 if (ret != Z_OK)
993 die(_("cannot deflate request; zlib end error %d"), ret);
995 gzip_size = stream.total_out;
997 headers = curl_slist_append(headers, "Content-Encoding: gzip");
998 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
999 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1001 if (options.verbosity > 1) {
1002 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1003 rpc->service_name,
1004 (unsigned long)rpc->len, (unsigned long)gzip_size);
1005 fflush(stderr);
1007 } else {
1008 /* We know the complete request size in advance, use the
1009 * more normal Content-Length approach.
1011 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1012 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1013 if (options.verbosity > 1) {
1014 fprintf(stderr, "POST %s (%lu bytes)\n",
1015 rpc->service_name, (unsigned long)rpc->len);
1016 fflush(stderr);
1020 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1021 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1022 rpc_in_data.rpc = rpc;
1023 rpc_in_data.slot = slot;
1024 rpc_in_data.check_pktline = stateless_connect;
1025 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1026 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
1027 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1030 rpc->any_written = 0;
1031 err = run_slot(slot, NULL);
1032 if (err == HTTP_REAUTH && !large_request) {
1033 credential_fill(&http_auth);
1034 goto retry;
1036 if (err != HTTP_OK)
1037 err = -1;
1039 if (!rpc->any_written)
1040 err = -1;
1042 if (rpc_in_data.pktline_state.len_filled)
1043 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1044 if (rpc_in_data.pktline_state.remaining)
1045 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1047 if (stateless_connect)
1048 packet_response_end(rpc->in);
1050 curl_slist_free_all(headers);
1051 free(gzip_body);
1052 return err;
1055 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1056 const char **client_argv, const struct strbuf *preamble,
1057 struct strbuf *rpc_result)
1059 const char *svc = rpc->service_name;
1060 struct strbuf buf = STRBUF_INIT;
1061 struct child_process client = CHILD_PROCESS_INIT;
1062 int err = 0;
1064 client.in = -1;
1065 client.out = -1;
1066 client.git_cmd = 1;
1067 client.argv = client_argv;
1068 if (start_command(&client))
1069 exit(1);
1070 write_or_die(client.in, preamble->buf, preamble->len);
1071 if (heads)
1072 write_or_die(client.in, heads->buf, heads->len);
1074 rpc->alloc = http_post_buffer;
1075 rpc->buf = xmalloc(rpc->alloc);
1076 rpc->in = client.in;
1077 rpc->out = client.out;
1079 strbuf_addf(&buf, "%s%s", url.buf, svc);
1080 rpc->service_url = strbuf_detach(&buf, NULL);
1082 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1083 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1085 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1086 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1088 if (get_protocol_http_header(heads->version, &buf))
1089 rpc->protocol_header = strbuf_detach(&buf, NULL);
1090 else
1091 rpc->protocol_header = NULL;
1093 while (!err) {
1094 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
1095 if (!n)
1096 break;
1097 rpc->pos = 0;
1098 rpc->len = n;
1099 err |= post_rpc(rpc, 0, 0);
1102 close(client.in);
1103 client.in = -1;
1104 if (!err) {
1105 strbuf_read(rpc_result, client.out, 0);
1106 } else {
1107 char buf[4096];
1108 for (;;)
1109 if (xread(client.out, buf, sizeof(buf)) <= 0)
1110 break;
1113 close(client.out);
1114 client.out = -1;
1116 err |= finish_command(&client);
1117 free(rpc->service_url);
1118 free(rpc->hdr_content_type);
1119 free(rpc->hdr_accept);
1120 free(rpc->protocol_header);
1121 free(rpc->buf);
1122 strbuf_release(&buf);
1123 return err;
1126 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1128 struct walker *walker;
1129 char **targets;
1130 int ret, i;
1132 ALLOC_ARRAY(targets, nr_heads);
1133 if (options.depth || options.deepen_since)
1134 die(_("dumb http transport does not support shallow capabilities"));
1135 for (i = 0; i < nr_heads; i++)
1136 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1138 walker = get_http_walker(url.buf);
1139 walker->get_verbosely = options.verbosity >= 3;
1140 walker->get_progress = options.progress;
1141 walker->get_recover = 0;
1142 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1143 walker_free(walker);
1145 for (i = 0; i < nr_heads; i++)
1146 free(targets[i]);
1147 free(targets);
1149 return ret ? error(_("fetch failed.")) : 0;
1152 static int fetch_git(struct discovery *heads,
1153 int nr_heads, struct ref **to_fetch)
1155 struct rpc_state rpc;
1156 struct strbuf preamble = STRBUF_INIT;
1157 int i, err;
1158 struct strvec args = STRVEC_INIT;
1159 struct strbuf rpc_result = STRBUF_INIT;
1161 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1162 "--stdin", "--lock-pack", NULL);
1163 if (options.followtags)
1164 strvec_push(&args, "--include-tag");
1165 if (options.thin)
1166 strvec_push(&args, "--thin");
1167 if (options.verbosity >= 3)
1168 strvec_pushl(&args, "-v", "-v", NULL);
1169 if (options.check_self_contained_and_connected)
1170 strvec_push(&args, "--check-self-contained-and-connected");
1171 if (options.cloning)
1172 strvec_push(&args, "--cloning");
1173 if (options.update_shallow)
1174 strvec_push(&args, "--update-shallow");
1175 if (!options.progress)
1176 strvec_push(&args, "--no-progress");
1177 if (options.depth)
1178 strvec_pushf(&args, "--depth=%lu", options.depth);
1179 if (options.deepen_since)
1180 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1181 for (i = 0; i < options.deepen_not.nr; i++)
1182 strvec_pushf(&args, "--shallow-exclude=%s",
1183 options.deepen_not.items[i].string);
1184 if (options.deepen_relative && options.depth)
1185 strvec_push(&args, "--deepen-relative");
1186 if (options.from_promisor)
1187 strvec_push(&args, "--from-promisor");
1188 if (options.filter)
1189 strvec_pushf(&args, "--filter=%s", options.filter);
1190 strvec_push(&args, url.buf);
1192 for (i = 0; i < nr_heads; i++) {
1193 struct ref *ref = to_fetch[i];
1194 if (!*ref->name)
1195 die(_("cannot fetch by sha1 over smart http"));
1196 packet_buf_write(&preamble, "%s %s\n",
1197 oid_to_hex(&ref->old_oid), ref->name);
1199 packet_buf_flush(&preamble);
1201 memset(&rpc, 0, sizeof(rpc));
1202 rpc.service_name = "git-upload-pack",
1203 rpc.gzip_request = 1;
1205 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1206 if (rpc_result.len)
1207 write_or_die(1, rpc_result.buf, rpc_result.len);
1208 strbuf_release(&rpc_result);
1209 strbuf_release(&preamble);
1210 strvec_clear(&args);
1211 return err;
1214 static int fetch(int nr_heads, struct ref **to_fetch)
1216 struct discovery *d = discover_refs("git-upload-pack", 0);
1217 if (d->proto_git)
1218 return fetch_git(d, nr_heads, to_fetch);
1219 else
1220 return fetch_dumb(nr_heads, to_fetch);
1223 static void parse_fetch(struct strbuf *buf)
1225 struct ref **to_fetch = NULL;
1226 struct ref *list_head = NULL;
1227 struct ref **list = &list_head;
1228 int alloc_heads = 0, nr_heads = 0;
1230 do {
1231 const char *p;
1232 if (skip_prefix(buf->buf, "fetch ", &p)) {
1233 const char *name;
1234 struct ref *ref;
1235 struct object_id old_oid;
1236 const char *q;
1238 if (parse_oid_hex(p, &old_oid, &q))
1239 die(_("protocol error: expected sha/ref, got '%s'"), p);
1240 if (*q == ' ')
1241 name = q + 1;
1242 else if (!*q)
1243 name = "";
1244 else
1245 die(_("protocol error: expected sha/ref, got '%s'"), p);
1247 ref = alloc_ref(name);
1248 oidcpy(&ref->old_oid, &old_oid);
1250 *list = ref;
1251 list = &ref->next;
1253 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1254 to_fetch[nr_heads++] = ref;
1256 else
1257 die(_("http transport does not support %s"), buf->buf);
1259 strbuf_reset(buf);
1260 if (strbuf_getline_lf(buf, stdin) == EOF)
1261 return;
1262 if (!*buf->buf)
1263 break;
1264 } while (1);
1266 if (fetch(nr_heads, to_fetch))
1267 exit(128); /* error already reported */
1268 free_refs(list_head);
1269 free(to_fetch);
1271 printf("\n");
1272 fflush(stdout);
1273 strbuf_reset(buf);
1276 static int push_dav(int nr_spec, const char **specs)
1278 struct child_process child = CHILD_PROCESS_INIT;
1279 size_t i;
1281 child.git_cmd = 1;
1282 strvec_push(&child.args, "http-push");
1283 strvec_push(&child.args, "--helper-status");
1284 if (options.dry_run)
1285 strvec_push(&child.args, "--dry-run");
1286 if (options.verbosity > 1)
1287 strvec_push(&child.args, "--verbose");
1288 strvec_push(&child.args, url.buf);
1289 for (i = 0; i < nr_spec; i++)
1290 strvec_push(&child.args, specs[i]);
1292 if (run_command(&child))
1293 die(_("git-http-push failed"));
1294 return 0;
1297 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1299 struct rpc_state rpc;
1300 int i, err;
1301 struct strvec args;
1302 struct string_list_item *cas_option;
1303 struct strbuf preamble = STRBUF_INIT;
1304 struct strbuf rpc_result = STRBUF_INIT;
1306 strvec_init(&args);
1307 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1308 NULL);
1310 if (options.thin)
1311 strvec_push(&args, "--thin");
1312 if (options.dry_run)
1313 strvec_push(&args, "--dry-run");
1314 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1315 strvec_push(&args, "--signed=yes");
1316 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1317 strvec_push(&args, "--signed=if-asked");
1318 if (options.atomic)
1319 strvec_push(&args, "--atomic");
1320 if (options.verbosity == 0)
1321 strvec_push(&args, "--quiet");
1322 else if (options.verbosity > 1)
1323 strvec_push(&args, "--verbose");
1324 for (i = 0; i < options.push_options.nr; i++)
1325 strvec_pushf(&args, "--push-option=%s",
1326 options.push_options.items[i].string);
1327 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1328 for_each_string_list_item(cas_option, &cas_options)
1329 strvec_push(&args, cas_option->string);
1330 strvec_push(&args, url.buf);
1332 if (options.force_if_includes)
1333 strvec_push(&args, "--force-if-includes");
1335 strvec_push(&args, "--stdin");
1336 for (i = 0; i < nr_spec; i++)
1337 packet_buf_write(&preamble, "%s\n", specs[i]);
1338 packet_buf_flush(&preamble);
1340 memset(&rpc, 0, sizeof(rpc));
1341 rpc.service_name = "git-receive-pack",
1343 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1344 if (rpc_result.len)
1345 write_or_die(1, rpc_result.buf, rpc_result.len);
1346 strbuf_release(&rpc_result);
1347 strbuf_release(&preamble);
1348 strvec_clear(&args);
1349 return err;
1352 static int push(int nr_spec, const char **specs)
1354 struct discovery *heads = discover_refs("git-receive-pack", 1);
1355 int ret;
1357 if (heads->proto_git)
1358 ret = push_git(heads, nr_spec, specs);
1359 else
1360 ret = push_dav(nr_spec, specs);
1361 free_discovery(heads);
1362 return ret;
1365 static void parse_push(struct strbuf *buf)
1367 struct strvec specs = STRVEC_INIT;
1368 int ret;
1370 do {
1371 const char *arg;
1372 if (skip_prefix(buf->buf, "push ", &arg))
1373 strvec_push(&specs, arg);
1374 else
1375 die(_("http transport does not support %s"), buf->buf);
1377 strbuf_reset(buf);
1378 if (strbuf_getline_lf(buf, stdin) == EOF)
1379 goto free_specs;
1380 if (!*buf->buf)
1381 break;
1382 } while (1);
1384 ret = push(specs.nr, specs.v);
1385 printf("\n");
1386 fflush(stdout);
1388 if (ret)
1389 exit(128); /* error already reported */
1391 free_specs:
1392 strvec_clear(&specs);
1395 static int stateless_connect(const char *service_name)
1397 struct discovery *discover;
1398 struct rpc_state rpc;
1399 struct strbuf buf = STRBUF_INIT;
1402 * Run the info/refs request and see if the server supports protocol
1403 * v2. If and only if the server supports v2 can we successfully
1404 * establish a stateless connection, otherwise we need to tell the
1405 * client to fallback to using other transport helper functions to
1406 * complete their request.
1408 discover = discover_refs(service_name, 0);
1409 if (discover->version != protocol_v2) {
1410 printf("fallback\n");
1411 fflush(stdout);
1412 return -1;
1413 } else {
1414 /* Stateless Connection established */
1415 printf("\n");
1416 fflush(stdout);
1419 rpc.service_name = service_name;
1420 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1421 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1422 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1423 if (get_protocol_http_header(discover->version, &buf)) {
1424 rpc.protocol_header = strbuf_detach(&buf, NULL);
1425 } else {
1426 rpc.protocol_header = NULL;
1427 strbuf_release(&buf);
1429 rpc.buf = xmalloc(http_post_buffer);
1430 rpc.alloc = http_post_buffer;
1431 rpc.len = 0;
1432 rpc.pos = 0;
1433 rpc.in = 1;
1434 rpc.out = 0;
1435 rpc.any_written = 0;
1436 rpc.gzip_request = 1;
1437 rpc.initial_buffer = 0;
1438 rpc.write_line_lengths = 1;
1439 rpc.flush_read_but_not_sent = 0;
1442 * Dump the capability listing that we got from the server earlier
1443 * during the info/refs request.
1445 write_or_die(rpc.in, discover->buf, discover->len);
1447 /* Until we see EOF keep sending POSTs */
1448 while (1) {
1449 size_t avail;
1450 enum packet_read_status status;
1452 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1453 &status))
1454 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1455 if (status == PACKET_READ_EOF)
1456 break;
1457 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1458 /* We would have an err here */
1459 break;
1460 /* Reset the buffer for next request */
1461 rpc.len = 0;
1464 free(rpc.service_url);
1465 free(rpc.hdr_content_type);
1466 free(rpc.hdr_accept);
1467 free(rpc.protocol_header);
1468 free(rpc.buf);
1469 strbuf_release(&buf);
1471 return 0;
1474 int cmd_main(int argc, const char **argv)
1476 struct strbuf buf = STRBUF_INIT;
1477 int nongit;
1479 setup_git_directory_gently(&nongit);
1480 if (argc < 2) {
1481 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1482 return 1;
1485 options.verbosity = 1;
1486 options.progress = !!isatty(2);
1487 options.thin = 1;
1488 string_list_init(&options.deepen_not, 1);
1489 string_list_init(&options.push_options, 1);
1492 * Just report "remote-curl" here (folding all the various aliases
1493 * ("git-remote-http", "git-remote-https", and etc.) here since they
1494 * are all just copies of the same actual executable.
1496 trace2_cmd_name("remote-curl");
1498 remote = remote_get(argv[1]);
1500 if (argc > 2) {
1501 end_url_with_slash(&url, argv[2]);
1502 } else {
1503 end_url_with_slash(&url, remote->url[0]);
1506 http_init(remote, url.buf, 0);
1508 do {
1509 const char *arg;
1511 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1512 if (ferror(stdin))
1513 error(_("remote-curl: error reading command stream from git"));
1514 return 1;
1516 if (buf.len == 0)
1517 break;
1518 if (starts_with(buf.buf, "fetch ")) {
1519 if (nongit)
1520 die(_("remote-curl: fetch attempted without a local repo"));
1521 parse_fetch(&buf);
1523 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1524 int for_push = !!strstr(buf.buf + 4, "for-push");
1525 output_refs(get_refs(for_push));
1527 } else if (starts_with(buf.buf, "push ")) {
1528 parse_push(&buf);
1530 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1531 char *value = strchr(arg, ' ');
1532 int result;
1534 if (value)
1535 *value++ = '\0';
1536 else
1537 value = "true";
1539 result = set_option(arg, value);
1540 if (!result)
1541 printf("ok\n");
1542 else if (result < 0)
1543 printf("error invalid value\n");
1544 else
1545 printf("unsupported\n");
1546 fflush(stdout);
1548 } else if (!strcmp(buf.buf, "capabilities")) {
1549 printf("stateless-connect\n");
1550 printf("fetch\n");
1551 printf("option\n");
1552 printf("push\n");
1553 printf("check-connectivity\n");
1554 printf("object-format\n");
1555 printf("\n");
1556 fflush(stdout);
1557 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1558 if (!stateless_connect(arg))
1559 break;
1560 } else {
1561 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1562 return 1;
1564 strbuf_reset(&buf);
1565 } while (1);
1567 http_cleanup();
1569 return 0;