blob: 72dfb8fb86aab0e93780416049cf8f8bfae157bc [file] [log] [blame]
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04003#include "remote.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -07004#include "connect.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04005#include "strbuf.h"
6#include "walker.h"
7#include "http.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07008#include "exec-cmd.h"
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07009#include "run-command.h"
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -070010#include "pkt-line.h"
Junio C Hamano05c1eb12013-08-02 15:14:50 -070011#include "string-list.h"
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070012#include "sideband.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040013#include "strvec.h"
Jeff King2501aff2013-09-28 04:31:45 -040014#include "credential.h"
Jeff Kingfe299ec2020-03-30 10:03:46 -040015#include "oid-array.h"
Dave Borowitz30261092015-08-19 11:26:46 -040016#include "send-pack.h"
Brandon Williamsad6ac122018-03-14 11:31:45 -070017#include "protocol.h"
Jeff King90dce212018-02-19 14:50:14 -050018#include "quote.h"
Johannes Schindelinc1284b22019-03-04 07:33:46 -080019#include "transport.h"
Daniel Barkalowa2d725b2009-08-05 01:01:56 -040020
Shawn O. Pearce37a87682009-10-30 17:47:26 -070021static struct remote *remote;
Jeff Kingb227bbc2013-09-28 04:35:25 -040022/* always ends with a trailing slash */
23static struct strbuf url = STRBUF_INIT;
Shawn O. Pearce37a87682009-10-30 17:47:26 -070024
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070025struct options {
26 int verbosity;
27 unsigned long depth;
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070028 char *deepen_since;
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070029 struct string_list deepen_not;
Brandon Williams511155d2017-03-22 15:22:00 -070030 struct string_list push_options;
Jeff Hostetleracb0c572017-12-08 15:58:44 +000031 char *filter;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070032 unsigned progress : 1,
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +070033 check_self_contained_and_connected : 1,
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070034 cloning : 1,
35 update_shallow : 1,
Shawn O. Pearceae4efe12009-10-30 17:47:30 -070036 followtags : 1,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -070037 dry_run : 1,
Junio C Hamano0ea47f92014-09-15 14:59:00 -070038 thin : 1,
Dave Borowitz30261092015-08-19 11:26:46 -040039 /* One of the SEND_PACK_PUSH_CERT_* constants. */
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070040 push_cert : 2,
Jonathan Tan88e2f9e2017-12-05 16:58:49 +000041 deepen_relative : 1,
Jonathan Tan42d418d2020-08-17 12:48:18 -070042
43 /* see documentation of corresponding flag in fetch-pack.h */
Jonathan Tan88e2f9e2017-12-05 16:58:49 +000044 from_promisor : 1,
Jonathan Tan42d418d2020-08-17 12:48:18 -070045
Robert Coup869a0eb2022-03-28 14:02:07 +000046 refetch : 1,
brian m. carlson7f605012020-05-25 19:59:04 +000047 atomic : 1,
Srinidhi Kaushik3b990aa2020-10-03 17:40:45 +053048 object_format : 1,
49 force_if_includes : 1;
brian m. carlson7f605012020-05-25 19:59:04 +000050 const struct git_hash_algo *hash_algo;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070051};
52static struct options options;
Junio C Hamano05c1eb12013-08-02 15:14:50 -070053static struct string_list cas_options = STRING_LIST_INIT_DUP;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070054
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070055static int set_option(const char *name, const char *value)
56{
57 if (!strcmp(name, "verbosity")) {
58 char *end;
59 int v = strtol(value, &end, 10);
60 if (value == end || *end)
61 return -1;
62 options.verbosity = v;
63 return 0;
64 }
65 else if (!strcmp(name, "progress")) {
66 if (!strcmp(value, "true"))
67 options.progress = 1;
68 else if (!strcmp(value, "false"))
69 options.progress = 0;
70 else
71 return -1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070072 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070073 }
74 else if (!strcmp(name, "depth")) {
75 char *end;
76 unsigned long v = strtoul(value, &end, 10);
77 if (value == end || *end)
78 return -1;
79 options.depth = v;
Shawn O. Pearce249b2002009-10-30 17:47:42 -070080 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070081 }
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +070082 else if (!strcmp(name, "deepen-since")) {
83 options.deepen_since = xstrdup(value);
84 return 0;
85 }
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +070086 else if (!strcmp(name, "deepen-not")) {
87 string_list_append(&options.deepen_not, value);
88 return 0;
89 }
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +070090 else if (!strcmp(name, "deepen-relative")) {
91 if (!strcmp(value, "true"))
92 options.deepen_relative = 1;
93 else if (!strcmp(value, "false"))
94 options.deepen_relative = 0;
95 else
96 return -1;
97 return 0;
98 }
Shawn O. Pearceef08ef92009-10-30 17:47:29 -070099 else if (!strcmp(name, "followtags")) {
100 if (!strcmp(value, "true"))
101 options.followtags = 1;
102 else if (!strcmp(value, "false"))
103 options.followtags = 0;
104 else
105 return -1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700106 return 0;
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700107 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700108 else if (!strcmp(name, "dry-run")) {
109 if (!strcmp(value, "true"))
110 options.dry_run = 1;
111 else if (!strcmp(value, "false"))
112 options.dry_run = 0;
113 else
114 return -1;
115 return 0;
116 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700117 else if (!strcmp(name, "check-connectivity")) {
118 if (!strcmp(value, "true"))
119 options.check_self_contained_and_connected = 1;
120 else if (!strcmp(value, "false"))
121 options.check_self_contained_and_connected = 0;
122 else
123 return -1;
124 return 0;
125 }
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700126 else if (!strcmp(name, "cas")) {
127 struct strbuf val = STRBUF_INIT;
brian m. carlsoncd85b442020-07-21 01:15:11 +0000128 strbuf_addstr(&val, "--force-with-lease=");
129 if (*value != '"')
130 strbuf_addstr(&val, value);
131 else if (unquote_c_style(&val, value, NULL))
132 return -1;
Junio C Hamano05c1eb12013-08-02 15:14:50 -0700133 string_list_append(&cas_options, val.buf);
134 strbuf_release(&val);
135 return 0;
Srinidhi Kaushik3b990aa2020-10-03 17:40:45 +0530136 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
137 if (!strcmp(value, "true"))
138 options.force_if_includes = 1;
139 else if (!strcmp(value, "false"))
140 options.force_if_includes = 0;
141 else
142 return -1;
143 return 0;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700144 } else if (!strcmp(name, "cloning")) {
145 if (!strcmp(value, "true"))
146 options.cloning = 1;
147 else if (!strcmp(value, "false"))
148 options.cloning = 0;
149 else
150 return -1;
151 return 0;
152 } else if (!strcmp(name, "update-shallow")) {
153 if (!strcmp(value, "true"))
154 options.update_shallow = 1;
155 else if (!strcmp(value, "false"))
156 options.update_shallow = 0;
157 else
158 return -1;
159 return 0;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700160 } else if (!strcmp(name, "pushcert")) {
161 if (!strcmp(value, "true"))
Dave Borowitz30261092015-08-19 11:26:46 -0400162 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700163 else if (!strcmp(value, "false"))
Dave Borowitz30261092015-08-19 11:26:46 -0400164 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
165 else if (!strcmp(value, "if-asked"))
166 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
Junio C Hamano0ea47f92014-09-15 14:59:00 -0700167 else
168 return -1;
169 return 0;
brian m. carlson6f119422019-10-16 23:45:34 +0000170 } else if (!strcmp(name, "atomic")) {
171 if (!strcmp(value, "true"))
172 options.atomic = 1;
173 else if (!strcmp(value, "false"))
174 options.atomic = 0;
175 else
176 return -1;
177 return 0;
Brandon Williams511155d2017-03-22 15:22:00 -0700178 } else if (!strcmp(name, "push-option")) {
Jeff King90dce212018-02-19 14:50:14 -0500179 if (*value != '"')
180 string_list_append(&options.push_options, value);
181 else {
182 struct strbuf unquoted = STRBUF_INIT;
183 if (unquote_c_style(&unquoted, value, NULL) < 0)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800184 die(_("invalid quoting in push-option value: '%s'"), value);
Jeff King90dce212018-02-19 14:50:14 -0500185 string_list_append_nodup(&options.push_options,
186 strbuf_detach(&unquoted, NULL));
187 }
Brandon Williams511155d2017-03-22 15:22:00 -0700188 return 0;
Eric Wongc915f112016-02-03 04:09:14 +0000189 } else if (!strcmp(name, "family")) {
190 if (!strcmp(value, "ipv4"))
191 git_curl_ipresolve = CURL_IPRESOLVE_V4;
192 else if (!strcmp(value, "ipv6"))
193 git_curl_ipresolve = CURL_IPRESOLVE_V6;
194 else if (!strcmp(value, "all"))
195 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
196 else
197 return -1;
198 return 0;
Jonathan Tan88e2f9e2017-12-05 16:58:49 +0000199 } else if (!strcmp(name, "from-promisor")) {
200 options.from_promisor = 1;
201 return 0;
Robert Coup869a0eb2022-03-28 14:02:07 +0000202 } else if (!strcmp(name, "refetch")) {
203 options.refetch = 1;
204 return 0;
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000205 } else if (!strcmp(name, "filter")) {
Elijah Newrenc3b9bc92018-09-05 10:03:07 -0700206 options.filter = xstrdup(value);
Jeff Hostetleracb0c572017-12-08 15:58:44 +0000207 return 0;
brian m. carlson7f605012020-05-25 19:59:04 +0000208 } else if (!strcmp(name, "object-format")) {
209 int algo;
210 options.object_format = 1;
211 if (strcmp(value, "true")) {
212 algo = hash_algo_by_name(value);
213 if (algo == GIT_HASH_UNKNOWN)
214 die("unknown object format '%s'", value);
215 options.hash_algo = &hash_algos[algo];
216 }
217 return 0;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700218 } else {
Shawn O. Pearceef08ef92009-10-30 17:47:29 -0700219 return 1 /* unsupported */;
220 }
221}
222
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700223struct discovery {
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700224 char *service;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700225 char *buf_alloc;
226 char *buf;
227 size_t len;
Jeff King2a455202013-02-20 15:07:19 -0500228 struct ref *refs;
brian m. carlson910650d2017-03-31 01:40:00 +0000229 struct oid_array shallow;
Brandon Williams49e85e92018-03-15 10:31:37 -0700230 enum protocol_version version;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700231 unsigned proto_git : 1;
232};
233static struct discovery *last_discovery;
234
Jeff Kingb8054bb2013-02-20 15:07:11 -0500235static struct ref *parse_git_refs(struct discovery *heads, int for_push)
236{
237 struct ref *list = NULL;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700238 struct packet_reader reader;
239
240 packet_reader_init(&reader, -1, heads->buf, heads->len,
241 PACKET_READ_CHOMP_NEWLINE |
Masaya Suzuki2d103c32018-12-29 13:19:15 -0800242 PACKET_READ_GENTLE_ON_EOF |
243 PACKET_READ_DIE_ON_ERR_PACKET);
Brandon Williamsad6ac122018-03-14 11:31:45 -0700244
Brandon Williams49e85e92018-03-15 10:31:37 -0700245 heads->version = discover_version(&reader);
246 switch (heads->version) {
Brandon Williams8f6982b2018-03-14 11:31:47 -0700247 case protocol_v2:
Brandon Williams0f1dc532018-03-15 10:31:41 -0700248 /*
249 * Do nothing. This isn't a list of refs but rather a
250 * capability advertisement. Client would have run
251 * 'stateless-connect' so we'll dump this capability listing
252 * and let them request the refs themselves.
253 */
Brandon Williams8f6982b2018-03-14 11:31:47 -0700254 break;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700255 case protocol_v1:
256 case protocol_v0:
257 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
258 NULL, &heads->shallow);
brian m. carlson7f605012020-05-25 19:59:04 +0000259 options.hash_algo = reader.hash_algo;
Brandon Williamsad6ac122018-03-14 11:31:45 -0700260 break;
261 case protocol_unknown_version:
262 BUG("unknown protocol version");
263 }
264
Jeff Kingb8054bb2013-02-20 15:07:11 -0500265 return list;
266}
267
brian m. carlsonac093d02020-06-19 17:55:51 +0000268static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
269{
270 const char *p = memchr(heads->buf, '\t', heads->len);
271 int algo;
272 if (!p)
273 return the_hash_algo;
274
275 algo = hash_algo_by_length((p - heads->buf) / 2);
276 if (algo == GIT_HASH_UNKNOWN)
277 return NULL;
278 return &hash_algos[algo];
279}
280
Jeff Kingb8054bb2013-02-20 15:07:11 -0500281static struct ref *parse_info_refs(struct discovery *heads)
282{
283 char *data, *start, *mid;
284 char *ref_name;
285 int i = 0;
286
287 struct ref *refs = NULL;
288 struct ref *ref = NULL;
289 struct ref *last_ref = NULL;
290
brian m. carlsonac093d02020-06-19 17:55:51 +0000291 options.hash_algo = detect_hash_algo(heads);
292 if (!options.hash_algo)
293 die("%sinfo/refs not valid: could not determine hash algorithm; "
294 "is this a git repository?",
295 transport_anonymize_url(url.buf));
296
Jeff Kingb8054bb2013-02-20 15:07:11 -0500297 data = heads->buf;
298 start = NULL;
299 mid = data;
300 while (i < heads->len) {
301 if (!start) {
302 start = &data[i];
303 }
304 if (data[i] == '\t')
305 mid = &data[i];
306 if (data[i] == '\n') {
brian m. carlsonac093d02020-06-19 17:55:51 +0000307 if (mid - start != options.hash_algo->hexsz)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800308 die(_("%sinfo/refs not valid: is this a git repository?"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800309 transport_anonymize_url(url.buf));
Jeff Kingb8054bb2013-02-20 15:07:11 -0500310 data[i] = 0;
311 ref_name = mid + 1;
Jeff King6f687c22015-09-24 17:08:09 -0400312 ref = alloc_ref(ref_name);
brian m. carlsonac093d02020-06-19 17:55:51 +0000313 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
Jeff Kingb8054bb2013-02-20 15:07:11 -0500314 if (!refs)
315 refs = ref;
316 if (last_ref)
317 last_ref->next = ref;
318 last_ref = ref;
319 start = NULL;
320 }
321 i++;
322 }
323
324 ref = alloc_ref("HEAD");
Jeff Kingb227bbc2013-09-28 04:35:25 -0400325 if (!http_fetch_ref(url.buf, ref) &&
Jeff Kingb8054bb2013-02-20 15:07:11 -0500326 !resolve_remote_symref(ref, refs)) {
327 ref->next = refs;
328 refs = ref;
329 } else {
330 free(ref);
331 }
332
333 return refs;
334}
335
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700336static void free_discovery(struct discovery *d)
337{
338 if (d) {
339 if (d == last_discovery)
340 last_discovery = NULL;
brian m. carlsonee3051b2017-03-26 16:01:37 +0000341 free(d->shallow.oid);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700342 free(d->buf_alloc);
Jeff King2a455202013-02-20 15:07:19 -0500343 free_refs(d->refs);
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700344 free(d->service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700345 free(d);
346 }
347}
348
Jeff Kingfc1b7742014-05-22 05:30:29 -0400349static int show_http_message(struct strbuf *type, struct strbuf *charset,
350 struct strbuf *msg)
Jeff King426e70d2013-04-05 18:17:23 -0400351{
352 const char *p, *eol;
353
354 /*
355 * We only show text/plain parts, as other types are likely
356 * to be ugly to look at on the user's terminal.
Jeff King426e70d2013-04-05 18:17:23 -0400357 */
Jeff Kingbf197fd2014-05-22 05:29:47 -0400358 if (strcmp(type->buf, "text/plain"))
Jeff King426e70d2013-04-05 18:17:23 -0400359 return -1;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400360 if (charset->len)
361 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
Jeff King426e70d2013-04-05 18:17:23 -0400362
363 strbuf_trim(msg);
364 if (!msg->len)
365 return -1;
366
367 p = msg->buf;
368 do {
369 eol = strchrnul(p, '\n');
370 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
371 p = eol + 1;
372 } while(*eol);
373 return 0;
374}
375
Brandon Williams884e5862018-03-15 10:31:39 -0700376static int get_protocol_http_header(enum protocol_version version,
377 struct strbuf *header)
378{
379 if (version > 0) {
380 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
381 version);
382
383 return 1;
384 }
385
386 return 0;
387}
388
Jeff King8ee3e122019-02-06 14:18:48 -0500389static void check_smart_http(struct discovery *d, const char *service,
390 struct strbuf *type)
391{
392 const char *p;
393 struct packet_reader reader;
394
395 /*
396 * If we don't see x-$service-advertisement, then it's not smart-http.
397 * But once we do, we commit to it and assume any other protocol
398 * violations are hard errors.
399 */
400 if (!skip_prefix(type->buf, "application/x-", &p) ||
401 !skip_prefix(p, service, &p) ||
402 strcmp(p, "-advertisement"))
403 return;
404
405 packet_reader_init(&reader, -1, d->buf, d->len,
406 PACKET_READ_CHOMP_NEWLINE |
407 PACKET_READ_DIE_ON_ERR_PACKET);
408 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900409 die(_("invalid server response; expected service, got flush packet"));
Jeff King8ee3e122019-02-06 14:18:48 -0500410
411 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
412 /*
413 * The header can include additional metadata lines, up
414 * until a packet flush marker. Ignore these now, but
415 * in the future we might start to scan them.
416 */
417 for (;;) {
418 packet_reader_read(&reader);
419 if (reader.pktlen <= 0) {
420 break;
421 }
422 }
423
424 /*
425 * v0 smart http; callers expect us to soak up the
426 * service and header packets
427 */
428 d->buf = reader.src_buffer;
429 d->len = reader.src_len;
430 d->proto_git = 1;
431
Jeff Kingcbdb8d12019-02-06 14:18:58 -0500432 } else if (!strcmp(reader.line, "version 2")) {
Jeff King8ee3e122019-02-06 14:18:48 -0500433 /*
434 * v2 smart http; do not consume version packet, which will
435 * be handled elsewhere.
436 */
437 d->proto_git = 1;
438
439 } else {
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900440 die(_("invalid server response; got '%s'"), reader.line);
Jeff King8ee3e122019-02-06 14:18:48 -0500441 }
442}
443
David Aguilar24d36f12014-08-31 13:11:31 -0700444static struct discovery *discover_refs(const char *service, int for_push)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400445{
Shawn Pearce4656bf42013-01-31 13:02:07 -0800446 struct strbuf type = STRBUF_INIT;
Jeff Kingfc1b7742014-05-22 05:30:29 -0400447 struct strbuf charset = STRBUF_INIT;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400448 struct strbuf buffer = STRBUF_INIT;
Jeff Kingc65d5692013-09-28 04:35:10 -0400449 struct strbuf refs_url = STRBUF_INIT;
Jeff King050ef362013-09-28 04:35:35 -0400450 struct strbuf effective_url = STRBUF_INIT;
Brandon Williams884e5862018-03-15 10:31:39 -0700451 struct strbuf protocol_header = STRBUF_INIT;
452 struct string_list extra_headers = STRING_LIST_INIT_DUP;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700453 struct discovery *last = last_discovery;
Jeff King243c3292012-09-20 13:00:22 -0400454 int http_ret, maybe_smart = 0;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500455 struct http_get_options http_options;
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700456 enum protocol_version version = get_protocol_version_config();
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400457
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700458 if (last && !strcmp(service, last->service))
459 return last;
460 free_discovery(last);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400461
Jeff Kingb227bbc2013-09-28 04:35:25 -0400462 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
Christian Couder59556542013-11-30 21:55:40 +0100463 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
Jeff King02572c22012-09-20 17:30:58 -0400464 git_env_bool("GIT_SMART_HTTP", 1)) {
Jeff King243c3292012-09-20 13:00:22 -0400465 maybe_smart = 1;
Jeff Kingb227bbc2013-09-28 04:35:25 -0400466 if (!strchr(url.buf, '?'))
Jeff Kingc65d5692013-09-28 04:35:10 -0400467 strbuf_addch(&refs_url, '?');
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700468 else
Jeff Kingc65d5692013-09-28 04:35:10 -0400469 strbuf_addch(&refs_url, '&');
470 strbuf_addf(&refs_url, "service=%s", service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700471 }
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400472
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700473 /*
474 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
475 * to perform a push, then fallback to v0 since the client doesn't know
476 * how to push yet using v2.
477 */
478 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
479 version = protocol_v0;
480
Brandon Williams884e5862018-03-15 10:31:39 -0700481 /* Add the extra Git-Protocol header */
Brandon Williamsa4d78ce2018-03-15 10:31:42 -0700482 if (get_protocol_http_header(version, &protocol_header))
Brandon Williams884e5862018-03-15 10:31:39 -0700483 string_list_append(&extra_headers, protocol_header.buf);
484
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500485 memset(&http_options, 0, sizeof(http_options));
486 http_options.content_type = &type;
487 http_options.charset = &charset;
488 http_options.effective_url = &effective_url;
489 http_options.base_url = &url;
Brandon Williams884e5862018-03-15 10:31:39 -0700490 http_options.extra_headers = &extra_headers;
Jeff King50d34132016-12-06 13:24:41 -0500491 http_options.initial_request = 1;
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500492 http_options.no_cache = 1;
Jeff King1bbcc222013-09-28 04:31:23 -0400493
Jeff Kingfcaa6e62016-12-06 13:24:38 -0500494 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400495 switch (http_ret) {
496 case HTTP_OK:
497 break;
498 case HTTP_MISSING_TARGET:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400499 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800500 die(_("repository '%s' not found"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800501 transport_anonymize_url(url.buf));
Scott Chacon42653c02010-04-01 15:14:35 -0700502 case HTTP_NOAUTH:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400503 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800504 die(_("Authentication failed for '%s'"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800505 transport_anonymize_url(url.buf));
Ævar Arnfjörð Bjarmason3e8084f2021-09-24 12:08:20 +0200506 case HTTP_NOMATCHPUBLICKEY:
507 show_http_message(&type, &charset, &buffer);
508 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
509 transport_anonymize_url(url.buf), curl_errorstr);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400510 default:
Jeff Kingfc1b7742014-05-22 05:30:29 -0400511 show_http_message(&type, &charset, &buffer);
Johannes Schindelined8b4132019-03-05 15:20:40 -0800512 die(_("unable to access '%s': %s"),
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800513 transport_anonymize_url(url.buf), curl_errorstr);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -0400514 }
515
Johannes Schindelinc1284b22019-03-04 07:33:46 -0800516 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
517 char *u = transport_anonymize_url(url.buf);
518 warning(_("redirecting to %s"), u);
519 free(u);
520 }
Jeff King50d34132016-12-06 13:24:41 -0500521
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700522 last= xcalloc(1, sizeof(*last_discovery));
Brandon Williamsf08a5d42018-03-15 10:31:36 -0700523 last->service = xstrdup(service);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700524 last->buf_alloc = strbuf_detach(&buffer, &last->len);
525 last->buf = last->buf_alloc;
526
Jeff King8ee3e122019-02-06 14:18:48 -0500527 if (maybe_smart)
528 check_smart_http(last, service, &type);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700529
Jeff King2a455202013-02-20 15:07:19 -0500530 if (last->proto_git)
531 last->refs = parse_git_refs(last, for_push);
532 else
533 last->refs = parse_info_refs(last);
534
Jeff Kingc65d5692013-09-28 04:35:10 -0400535 strbuf_release(&refs_url);
Shawn Pearce4656bf42013-01-31 13:02:07 -0800536 strbuf_release(&type);
Jeff Kingfc1b7742014-05-22 05:30:29 -0400537 strbuf_release(&charset);
Jeff King050ef362013-09-28 04:35:35 -0400538 strbuf_release(&effective_url);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700539 strbuf_release(&buffer);
Brandon Williams884e5862018-03-15 10:31:39 -0700540 strbuf_release(&protocol_header);
541 string_list_clear(&extra_headers, 0);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700542 last_discovery = last;
543 return last;
544}
545
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700546static struct ref *get_refs(int for_push)
547{
548 struct discovery *heads;
549
550 if (for_push)
Jeff King2a455202013-02-20 15:07:19 -0500551 heads = discover_refs("git-receive-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700552 else
Jeff King2a455202013-02-20 15:07:19 -0500553 heads = discover_refs("git-upload-pack", for_push);
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700554
Jeff King2a455202013-02-20 15:07:19 -0500555 return heads->refs;
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -0700556}
557
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700558static void output_refs(struct ref *refs)
559{
560 struct ref *posn;
brian m. carlson7f605012020-05-25 19:59:04 +0000561 if (options.object_format && options.hash_algo) {
562 printf(":object-format %s\n", options.hash_algo->name);
Eric Wong00bc8392021-05-11 10:37:30 +0000563 repo_set_hash_algo(the_repository,
564 hash_algo_by_ptr(options.hash_algo));
brian m. carlson7f605012020-05-25 19:59:04 +0000565 }
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700566 for (posn = refs; posn; posn = posn->next) {
567 if (posn->symref)
568 printf("@%s %s\n", posn->symref, posn->name);
569 else
brian m. carlson97997e62020-06-19 17:55:54 +0000570 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
571 options.hash_algo),
572 posn->name);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700573 }
574 printf("\n");
575 fflush(stdout);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -0700576}
577
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700578struct rpc_state {
579 const char *service_name;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700580 char *service_url;
581 char *hdr_content_type;
582 char *hdr_accept;
Li Linchaob0c4adc2022-07-11 05:58:54 +0000583 char *hdr_accept_language;
Brandon Williams884e5862018-03-15 10:31:39 -0700584 char *protocol_header;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700585 char *buf;
586 size_t alloc;
587 size_t len;
588 size_t pos;
589 int in;
590 int out;
David Turner296b8472016-11-18 15:30:49 -0500591 int any_written;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700592 unsigned gzip_request : 1;
Martin Storsjö6c81a992009-12-01 12:33:39 +0200593 unsigned initial_buffer : 1;
Jonathan Tana97d0072019-02-21 12:24:41 -0800594
595 /*
596 * Whenever a pkt-line is read into buf, append the 4 characters
597 * denoting its length before appending the payload.
598 */
599 unsigned write_line_lengths : 1;
600
601 /*
602 * Used by rpc_out; initialize to 0. This is true if a flush has been
603 * read, but the corresponding line length (if write_line_lengths is
604 * true) and EOF have not been sent to libcurl. Since each flush marks
605 * the end of a request, each flush must be completely sent before any
606 * further reading occurs.
607 */
608 unsigned flush_read_but_not_sent : 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700609};
610
Li Linchaob0c4adc2022-07-11 05:58:54 +0000611#define RPC_STATE_INIT { 0 }
612
Jonathan Tan78ad9172019-02-21 12:24:40 -0800613/*
614 * Appends the result of reading from rpc->out to the string represented by
615 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
616 * enough space, 0 otherwise.
617 *
Jonathan Tana97d0072019-02-21 12:24:41 -0800618 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
619 * hexadecimal string before appending the result described above.
620 *
621 * Writes the total number of bytes appended into appended.
Jonathan Tan78ad9172019-02-21 12:24:40 -0800622 */
Jonathan Tana97d0072019-02-21 12:24:41 -0800623static int rpc_read_from_out(struct rpc_state *rpc, int options,
624 size_t *appended,
625 enum packet_read_status *status) {
626 size_t left;
627 char *buf;
628 int pktlen_raw;
629
630 if (rpc->write_line_lengths) {
631 left = rpc->alloc - rpc->len - 4;
632 buf = rpc->buf + rpc->len + 4;
633 } else {
634 left = rpc->alloc - rpc->len;
635 buf = rpc->buf + rpc->len;
636 }
Jonathan Tan78ad9172019-02-21 12:24:40 -0800637
638 if (left < LARGE_PACKET_MAX)
639 return 0;
640
Jonathan Tana97d0072019-02-21 12:24:41 -0800641 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
642 left, &pktlen_raw, options);
643 if (*status != PACKET_READ_EOF) {
644 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
645 rpc->len += *appended;
646 }
647
648 if (rpc->write_line_lengths) {
649 switch (*status) {
650 case PACKET_READ_EOF:
651 if (!(options & PACKET_READ_GENTLE_ON_EOF))
Junio C Hamanoaa1edf12019-04-16 19:28:05 +0900652 die(_("shouldn't have EOF when not gentle on EOF"));
Jonathan Tana97d0072019-02-21 12:24:41 -0800653 break;
654 case PACKET_READ_NORMAL:
655 set_packet_header(buf - 4, *appended);
656 break;
657 case PACKET_READ_DELIM:
658 memcpy(buf - 4, "0001", 4);
659 break;
660 case PACKET_READ_FLUSH:
661 memcpy(buf - 4, "0000", 4);
662 break;
Denton Liu0181b602020-05-19 06:53:59 -0400663 case PACKET_READ_RESPONSE_END:
Denton Liu8232a0f2021-07-08 19:27:22 -0700664 die(_("remote server sent unexpected response end packet"));
Jonathan Tana97d0072019-02-21 12:24:41 -0800665 }
666 }
667
Jonathan Tan78ad9172019-02-21 12:24:40 -0800668 return 1;
669}
670
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700671static size_t rpc_out(void *ptr, size_t eltsize,
672 size_t nmemb, void *buffer_)
673{
674 size_t max = eltsize * nmemb;
675 struct rpc_state *rpc = buffer_;
676 size_t avail = rpc->len - rpc->pos;
Jonathan Tana97d0072019-02-21 12:24:41 -0800677 enum packet_read_status status;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700678
679 if (!avail) {
Martin Storsjö6c81a992009-12-01 12:33:39 +0200680 rpc->initial_buffer = 0;
Jonathan Tan78ad9172019-02-21 12:24:40 -0800681 rpc->len = 0;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700682 rpc->pos = 0;
Jonathan Tana97d0072019-02-21 12:24:41 -0800683 if (!rpc->flush_read_but_not_sent) {
684 if (!rpc_read_from_out(rpc, 0, &avail, &status))
685 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
686 if (status == PACKET_READ_FLUSH)
687 rpc->flush_read_but_not_sent = 1;
688 }
689 /*
690 * If flush_read_but_not_sent is true, we have already read one
691 * full request but have not fully sent it + EOF, which is why
692 * we need to refrain from reading.
693 */
694 }
695 if (rpc->flush_read_but_not_sent) {
696 if (!avail) {
697 /*
698 * The line length either does not need to be sent at
699 * all or has already been completely sent. Now we can
700 * return 0, indicating EOF, meaning that the flush has
701 * been fully sent.
702 */
703 rpc->flush_read_but_not_sent = 0;
704 return 0;
705 }
706 /*
Denton Liu51ca7f82020-05-18 11:47:18 -0400707 * If avail is non-zero, the line length for the flush still
Jonathan Tana97d0072019-02-21 12:24:41 -0800708 * hasn't been fully sent. Proceed with sending the line
709 * length.
710 */
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700711 }
712
Tay Ray Chuan48310602009-11-24 10:31:30 +0800713 if (max < avail)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700714 avail = max;
715 memcpy(ptr, rpc->buf + rpc->pos, avail);
716 rpc->pos += avail;
717 return avail;
718}
719
Junio C Hamano5092d3e2010-01-11 22:30:36 -0800720static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
Martin Storsjö6c81a992009-12-01 12:33:39 +0200721{
722 struct rpc_state *rpc = clientp;
723
724 switch (cmd) {
725 case CURLIOCMD_NOP:
726 return CURLIOE_OK;
727
728 case CURLIOCMD_RESTARTREAD:
729 if (rpc->initial_buffer) {
730 rpc->pos = 0;
731 return CURLIOE_OK;
732 }
Johannes Schindelined8b4132019-03-05 15:20:40 -0800733 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
Martin Storsjö6c81a992009-12-01 12:33:39 +0200734 return CURLIOE_FAILRESTART;
735
736 default:
737 return CURLIOE_UNKNOWNCMD;
738 }
739}
Martin Storsjö6c81a992009-12-01 12:33:39 +0200740
Denton Liu74b082a2020-05-19 06:53:58 -0400741struct check_pktline_state {
742 char len_buf[4];
743 int len_filled;
744 int remaining;
745};
746
747static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
748{
749 while (size) {
750 if (!state->remaining) {
751 int digits_remaining = 4 - state->len_filled;
752 if (digits_remaining > size)
753 digits_remaining = size;
754 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
755 state->len_filled += digits_remaining;
756 ptr += digits_remaining;
757 size -= digits_remaining;
758
759 if (state->len_filled == 4) {
760 state->remaining = packet_length(state->len_buf);
761 if (state->remaining < 0) {
762 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
Denton Liub0df0c12020-05-19 06:54:00 -0400763 } else if (state->remaining == 2) {
764 die(_("remote-curl: unexpected response end packet"));
Denton Liu74b082a2020-05-19 06:53:58 -0400765 } else if (state->remaining < 4) {
766 state->remaining = 0;
767 } else {
768 state->remaining -= 4;
769 }
770 state->len_filled = 0;
771 }
772 }
773
774 if (state->remaining) {
775 int remaining = state->remaining;
776 if (remaining > size)
777 remaining = size;
778 ptr += remaining;
779 size -= remaining;
780 state->remaining -= remaining;
781 }
782 }
783}
784
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800785struct rpc_in_data {
786 struct rpc_state *rpc;
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800787 struct active_request_slot *slot;
Denton Liu74b082a2020-05-19 06:53:58 -0400788 int check_pktline;
789 struct check_pktline_state pktline_state;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800790};
791
792/*
793 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
794 * from ptr.
795 */
Dan McGeea04ff3e2011-05-03 23:47:27 +0800796static size_t rpc_in(char *ptr, size_t eltsize,
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700797 size_t nmemb, void *buffer_)
798{
799 size_t size = eltsize * nmemb;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800800 struct rpc_in_data *data = buffer_;
Masaya Suzukib79bdd82019-01-10 11:33:49 -0800801 long response_code;
802
803 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
804 &response_code) != CURLE_OK)
805 return size;
806 if (response_code >= 300)
807 return size;
David Turner296b8472016-11-18 15:30:49 -0500808 if (size)
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800809 data->rpc->any_written = 1;
Denton Liu74b082a2020-05-19 06:53:58 -0400810 if (data->check_pktline)
811 check_pktline(&data->pktline_state, ptr, size);
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800812 write_or_die(data->rpc->in, ptr, size);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700813 return size;
814}
815
Jeff King3a347ed2013-10-31 02:36:26 -0400816static int run_slot(struct active_request_slot *slot,
817 struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800818{
Jeff Kingb81401c2012-08-27 09:27:15 -0400819 int err;
Jeff King3a347ed2013-10-31 02:36:26 -0400820 struct slot_results results_buf;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800821
Jeff King3a347ed2013-10-31 02:36:26 -0400822 if (!results)
823 results = &results_buf;
824
Jeff Kingbeed3362014-02-18 05:34:20 -0500825 err = run_one_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800826
Jeff Kingb81401c2012-08-27 09:27:15 -0400827 if (err != HTTP_OK && err != HTTP_REAUTH) {
Shawn Pearce00540452016-02-13 17:39:34 -0800828 struct strbuf msg = STRBUF_INIT;
829 if (results->http_code && results->http_code != 200)
830 strbuf_addf(&msg, "HTTP %ld", results->http_code);
831 if (results->curl_result != CURLE_OK) {
832 if (msg.len)
833 strbuf_addch(&msg, ' ');
834 strbuf_addf(&msg, "curl %d", results->curl_result);
835 if (curl_errorstr[0]) {
836 strbuf_addch(&msg, ' ');
837 strbuf_addstr(&msg, curl_errorstr);
838 }
839 }
Johannes Schindelined8b4132019-03-05 15:20:40 -0800840 error(_("RPC failed; %s"), msg.buf);
Shawn Pearce00540452016-02-13 17:39:34 -0800841 strbuf_release(&msg);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800842 }
843
844 return err;
845}
846
Jeff King3a347ed2013-10-31 02:36:26 -0400847static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800848{
849 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200850 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800851 struct strbuf buf = STRBUF_INIT;
852 int err;
853
854 slot = get_active_slot();
855
856 headers = curl_slist_append(headers, rpc->hdr_content_type);
857 headers = curl_slist_append(headers, rpc->hdr_accept);
858
859 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
860 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
861 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
Shawn O. Pearceaa90b962012-09-19 16:12:02 -0700862 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800863 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
864 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
865 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
866 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +0200867 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800868
Jeff King3a347ed2013-10-31 02:36:26 -0400869 err = run_slot(slot, results);
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800870
871 curl_slist_free_all(headers);
872 strbuf_release(&buf);
873 return err;
874}
875
Nguyễn Thái Ngọc Duy3b335762018-12-09 11:25:21 +0100876static curl_off_t xcurl_off_t(size_t len)
877{
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100878 uintmax_t size = len;
879 if (size > maximum_signed_value_of_type(curl_off_t))
Johannes Schindelined8b4132019-03-05 15:20:40 -0800880 die(_("cannot handle pushes this big"));
Torsten Bögershausencb8010b2018-11-09 18:41:10 +0100881 return (curl_off_t)size;
David Turner37ee6802017-04-11 14:13:57 -0400882}
883
Jonathan Tana97d0072019-02-21 12:24:41 -0800884/*
885 * If flush_received is true, do not attempt to read any more; just use what's
886 * in rpc->buf.
887 */
Denton Liu74b082a2020-05-19 06:53:58 -0400888static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700889{
890 struct active_request_slot *slot;
Johannes Schindelin8cb01e22016-04-27 14:20:37 +0200891 struct curl_slist *headers = http_copy_default_headers();
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700892 int use_gzip = rpc->gzip_request;
893 char *gzip_body = NULL;
Ramsay Jones37711542012-11-21 19:08:51 +0000894 size_t gzip_size = 0;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800895 int err, large_request = 0;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400896 int needs_100_continue = 0;
Masaya Suzukicf2fb922019-01-10 11:33:48 -0800897 struct rpc_in_data rpc_in_data;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700898
899 /* Try to load the entire request, if we can fit it into the
900 * allocated buffer space we can use HTTP/1.0 and avoid the
901 * chunked encoding mess.
902 */
Jonathan Tana97d0072019-02-21 12:24:41 -0800903 if (!flush_received) {
904 while (1) {
905 size_t n;
906 enum packet_read_status status;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700907
Jonathan Tana97d0072019-02-21 12:24:41 -0800908 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
909 large_request = 1;
910 use_gzip = 0;
911 break;
912 }
913 if (status == PACKET_READ_FLUSH)
914 break;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700915 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700916 }
917
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800918 if (large_request) {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400919 struct slot_results results;
920
Jeff Kingb81401c2012-08-27 09:27:15 -0400921 do {
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400922 err = probe_rpc(rpc, &results);
Jeff King2501aff2013-09-28 04:31:45 -0400923 if (err == HTTP_REAUTH)
924 credential_fill(&http_auth);
Jeff Kingb81401c2012-08-27 09:27:15 -0400925 } while (err == HTTP_REAUTH);
926 if (err != HTTP_OK)
927 return -1;
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400928
929 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
930 needs_100_continue = 1;
Shawn O. Pearce206b0992011-02-15 08:57:24 -0800931 }
932
Jeff Kingabf8df82012-10-12 03:35:33 -0400933 headers = curl_slist_append(headers, rpc->hdr_content_type);
934 headers = curl_slist_append(headers, rpc->hdr_accept);
Brian M. Carlsonc80d96c2013-10-31 02:36:51 -0400935 headers = curl_slist_append(headers, needs_100_continue ?
936 "Expect: 100-continue" : "Expect:");
Jeff Kingabf8df82012-10-12 03:35:33 -0400937
Li Linchaob0c4adc2022-07-11 05:58:54 +0000938 /* Add Accept-Language header */
939 if (rpc->hdr_accept_language)
940 headers = curl_slist_append(headers, rpc->hdr_accept_language);
941
Brandon Williams884e5862018-03-15 10:31:39 -0700942 /* Add the extra Git-Protocol header */
943 if (rpc->protocol_header)
944 headers = curl_slist_append(headers, rpc->protocol_header);
945
Jeff Kingabf8df82012-10-12 03:35:33 -0400946retry:
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700947 slot = get_active_slot();
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700948
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700949 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
Martin Storsjöd21f9792009-11-23 11:03:28 +0800950 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700951 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
Brandon Williams1a53e692018-05-22 11:42:03 -0700952 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700953
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700954 if (large_request) {
955 /* The request body is large and the size cannot be predicted.
956 * We must use chunked encoding to send it.
957 */
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700958 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
Martin Storsjö6c81a992009-12-01 12:33:39 +0200959 rpc->initial_buffer = 1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700960 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
961 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
Martin Storsjö6c81a992009-12-01 12:33:39 +0200962 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
963 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -0700964 if (options.verbosity > 1) {
965 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
966 fflush(stderr);
967 }
968
Jeff King2e736fd2012-10-31 07:29:16 -0400969 } else if (gzip_body) {
970 /*
971 * If we are looping to retry authentication, then the previous
972 * run will have set up the headers and gzip buffer already,
973 * and we just need to send it.
974 */
975 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -0400976 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Jeff King2e736fd2012-10-31 07:29:16 -0400977
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700978 } else if (use_gzip && 1024 < rpc->len) {
979 /* The client backend isn't giving us compressed data so
Johannes Schindelina8132412018-08-08 04:50:00 -0700980 * we can try to deflate it ourselves, this may save on
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700981 * the transfer time.
982 */
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700983 git_zstream stream;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700984 int ret;
985
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700986 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
Jeff Kingdf126e12012-10-31 07:20:15 -0400987 gzip_size = git_deflate_bound(&stream, rpc->len);
988 gzip_body = xmalloc(gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700989
990 stream.next_in = (unsigned char *)rpc->buf;
991 stream.avail_in = rpc->len;
992 stream.next_out = (unsigned char *)gzip_body;
Jeff Kingdf126e12012-10-31 07:20:15 -0400993 stream.avail_out = gzip_size;
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700994
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700995 ret = git_deflate(&stream, Z_FINISH);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700996 if (ret != Z_STREAM_END)
Johannes Schindelined8b4132019-03-05 15:20:40 -0800997 die(_("cannot deflate request; zlib deflate error %d"), ret);
Shawn O. Pearceb8538602009-10-30 17:47:43 -0700998
Junio C Hamano55bb5c92011-06-10 10:55:10 -0700999 ret = git_deflate_end_gently(&stream);
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001000 if (ret != Z_OK)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001001 die(_("cannot deflate request; zlib end error %d"), ret);
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001002
Jeff Kingdf126e12012-10-31 07:20:15 -04001003 gzip_size = stream.total_out;
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001004
1005 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1006 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
David Turner37ee6802017-04-11 14:13:57 -04001007 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001008
1009 if (options.verbosity > 1) {
1010 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1011 rpc->service_name,
Jeff Kingdf126e12012-10-31 07:20:15 -04001012 (unsigned long)rpc->len, (unsigned long)gzip_size);
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001013 fflush(stderr);
1014 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001015 } else {
1016 /* We know the complete request size in advance, use the
1017 * more normal Content-Length approach.
1018 */
1019 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
David Turner37ee6802017-04-11 14:13:57 -04001020 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001021 if (options.verbosity > 1) {
1022 fprintf(stderr, "POST %s (%lu bytes)\n",
1023 rpc->service_name, (unsigned long)rpc->len);
1024 fflush(stderr);
1025 }
1026 }
1027
1028 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1029 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
Masaya Suzukicf2fb922019-01-10 11:33:48 -08001030 rpc_in_data.rpc = rpc;
Masaya Suzukib79bdd82019-01-10 11:33:49 -08001031 rpc_in_data.slot = slot;
Denton Liu74b082a2020-05-19 06:53:58 -04001032 rpc_in_data.check_pktline = stateless_connect;
1033 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
Ævar Arnfjörð Bjarmason8dda4cb2021-07-30 19:59:46 +02001034 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
Masaya Suzukib79bdd82019-01-10 11:33:49 -08001035 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001036
David Turner296b8472016-11-18 15:30:49 -05001037
1038 rpc->any_written = 0;
Jeff King3a347ed2013-10-31 02:36:26 -04001039 err = run_slot(slot, NULL);
Jeff King2501aff2013-09-28 04:31:45 -04001040 if (err == HTTP_REAUTH && !large_request) {
1041 credential_fill(&http_auth);
Jeff Kingabf8df82012-10-12 03:35:33 -04001042 goto retry;
Jeff King2501aff2013-09-28 04:31:45 -04001043 }
Jeff Kingb81401c2012-08-27 09:27:15 -04001044 if (err != HTTP_OK)
1045 err = -1;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001046
David Turner296b8472016-11-18 15:30:49 -05001047 if (!rpc->any_written)
1048 err = -1;
1049
Denton Liu74b082a2020-05-19 06:53:58 -04001050 if (rpc_in_data.pktline_state.len_filled)
1051 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1052 if (rpc_in_data.pktline_state.remaining)
1053 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1054
Denton Liub0df0c12020-05-19 06:54:00 -04001055 if (stateless_connect)
1056 packet_response_end(rpc->in);
1057
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001058 curl_slist_free_all(headers);
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001059 free(gzip_body);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001060 return err;
1061}
1062
Jonathan Tan7d50d342019-02-14 11:06:35 -08001063static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
Jonathan Tanb3590302019-02-14 11:06:37 -08001064 const char **client_argv, const struct strbuf *preamble,
1065 struct strbuf *rpc_result)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001066{
1067 const char *svc = rpc->service_name;
1068 struct strbuf buf = STRBUF_INIT;
René Scharfed3180272014-08-19 21:09:35 +02001069 struct child_process client = CHILD_PROCESS_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001070 int err = 0;
1071
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001072 client.in = -1;
1073 client.out = -1;
1074 client.git_cmd = 1;
Ævar Arnfjörð Bjarmason6def0ff2021-11-25 23:52:18 +01001075 strvec_pushv(&client.args, client_argv);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001076 if (start_command(&client))
1077 exit(1);
Jonathan Tan5d916692019-02-14 11:06:36 -08001078 write_or_die(client.in, preamble->buf, preamble->len);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001079 if (heads)
1080 write_or_die(client.in, heads->buf, heads->len);
1081
1082 rpc->alloc = http_post_buffer;
1083 rpc->buf = xmalloc(rpc->alloc);
1084 rpc->in = client.in;
1085 rpc->out = client.out;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001086
Jeff Kingb227bbc2013-09-28 04:35:25 -04001087 strbuf_addf(&buf, "%s%s", url.buf, svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001088 rpc->service_url = strbuf_detach(&buf, NULL);
1089
Li Linchaob0c4adc2022-07-11 05:58:54 +00001090 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1091
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001092 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1093 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1094
Shawn O. Pearce8efa5f62010-01-12 09:54:04 -08001095 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001096 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1097
Brandon Williams884e5862018-03-15 10:31:39 -07001098 if (get_protocol_http_header(heads->version, &buf))
1099 rpc->protocol_header = strbuf_detach(&buf, NULL);
1100 else
1101 rpc->protocol_header = NULL;
1102
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001103 while (!err) {
Ævar Arnfjörð Bjarmasonec9a37d2021-10-14 22:15:12 +02001104 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001105 if (!n)
1106 break;
1107 rpc->pos = 0;
1108 rpc->len = n;
Denton Liu74b082a2020-05-19 06:53:58 -04001109 err |= post_rpc(rpc, 0, 0);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001110 }
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001111
1112 close(client.in);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001113 client.in = -1;
Shawn O. Pearce6cdf0222011-10-04 16:20:19 -07001114 if (!err) {
Jonathan Tanb3590302019-02-14 11:06:37 -08001115 strbuf_read(rpc_result, client.out, 0);
Shawn O. Pearce6cdf0222011-10-04 16:20:19 -07001116 } else {
1117 char buf[4096];
1118 for (;;)
1119 if (xread(client.out, buf, sizeof(buf)) <= 0)
1120 break;
1121 }
Shawn O. Pearceb4ee10f2010-08-06 14:19:44 -07001122
1123 close(client.out);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001124 client.out = -1;
1125
1126 err |= finish_command(&client);
1127 free(rpc->service_url);
1128 free(rpc->hdr_content_type);
1129 free(rpc->hdr_accept);
Li Linchaob0c4adc2022-07-11 05:58:54 +00001130 free(rpc->hdr_accept_language);
Brandon Williams884e5862018-03-15 10:31:39 -07001131 free(rpc->protocol_header);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001132 free(rpc->buf);
1133 strbuf_release(&buf);
1134 return err;
1135}
1136
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001137static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1138{
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +08001139 struct walker *walker;
Jeff Kingb32fa952016-02-22 17:44:25 -05001140 char **targets;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001141 int ret, i;
1142
Jeff Kingb32fa952016-02-22 17:44:25 -05001143 ALLOC_ARRAY(targets, nr_heads);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001144 if (options.depth || options.deepen_since)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001145 die(_("dumb http transport does not support shallow capabilities"));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001146 for (i = 0; i < nr_heads; i++)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001147 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001148
Jeff Kingb227bbc2013-09-28 04:35:25 -04001149 walker = get_http_walker(url.buf);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001150 walker->get_verbosely = options.verbosity >= 3;
René Scharfe7655b412020-03-03 21:55:34 +01001151 walker->get_progress = options.progress;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001152 walker->get_recover = 0;
1153 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
Tay Ray Chuan26e1e0b2010-03-02 18:49:31 +08001154 walker_free(walker);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001155
1156 for (i = 0; i < nr_heads; i++)
1157 free(targets[i]);
1158 free(targets);
1159
Johannes Schindelined8b4132019-03-05 15:20:40 -08001160 return ret ? error(_("fetch failed.")) : 0;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001161}
1162
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001163static int fetch_git(struct discovery *heads,
1164 int nr_heads, struct ref **to_fetch)
1165{
Li Linchaob0c4adc2022-07-11 05:58:54 +00001166 struct rpc_state rpc = RPC_STATE_INIT;
Ivan Todoroski81507492012-04-02 17:14:44 +02001167 struct strbuf preamble = STRBUF_INIT;
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001168 int i, err;
Jeff Kingc972bf42020-07-28 16:25:12 -04001169 struct strvec args = STRVEC_INIT;
Jonathan Tanb3590302019-02-14 11:06:37 -08001170 struct strbuf rpc_result = STRBUF_INIT;
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001171
Jeff Kingc972bf42020-07-28 16:25:12 -04001172 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
Jeff Kingf6d89422020-07-28 16:26:31 -04001173 "--stdin", "--lock-pack", NULL);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001174 if (options.followtags)
Jeff Kingc972bf42020-07-28 16:25:12 -04001175 strvec_push(&args, "--include-tag");
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001176 if (options.thin)
Jeff Kingc972bf42020-07-28 16:25:12 -04001177 strvec_push(&args, "--thin");
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001178 if (options.verbosity >= 3)
Jeff Kingc972bf42020-07-28 16:25:12 -04001179 strvec_pushl(&args, "-v", "-v", NULL);
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +07001180 if (options.check_self_contained_and_connected)
Jeff Kingc972bf42020-07-28 16:25:12 -04001181 strvec_push(&args, "--check-self-contained-and-connected");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +07001182 if (options.cloning)
Jeff Kingc972bf42020-07-28 16:25:12 -04001183 strvec_push(&args, "--cloning");
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +07001184 if (options.update_shallow)
Jeff Kingc972bf42020-07-28 16:25:12 -04001185 strvec_push(&args, "--update-shallow");
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001186 if (!options.progress)
Jeff Kingc972bf42020-07-28 16:25:12 -04001187 strvec_push(&args, "--no-progress");
Nguyễn Thái Ngọc Duyb5f62eb2016-06-12 17:53:43 +07001188 if (options.depth)
Jeff Kingc972bf42020-07-28 16:25:12 -04001189 strvec_pushf(&args, "--depth=%lu", options.depth);
Nguyễn Thái Ngọc Duy508ea882016-06-12 17:53:59 +07001190 if (options.deepen_since)
Jeff Kingc972bf42020-07-28 16:25:12 -04001191 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
Nguyễn Thái Ngọc Duya45a2602016-06-12 17:54:04 +07001192 for (i = 0; i < options.deepen_not.nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -04001193 strvec_pushf(&args, "--shallow-exclude=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001194 options.deepen_not.items[i].string);
Nguyễn Thái Ngọc Duycccf74e2016-06-12 17:54:09 +07001195 if (options.deepen_relative && options.depth)
Jeff Kingc972bf42020-07-28 16:25:12 -04001196 strvec_push(&args, "--deepen-relative");
Jonathan Tan88e2f9e2017-12-05 16:58:49 +00001197 if (options.from_promisor)
Jeff Kingc972bf42020-07-28 16:25:12 -04001198 strvec_push(&args, "--from-promisor");
Robert Coup869a0eb2022-03-28 14:02:07 +00001199 if (options.refetch)
1200 strvec_push(&args, "--refetch");
Jeff Hostetleracb0c572017-12-08 15:58:44 +00001201 if (options.filter)
Jeff Kingc972bf42020-07-28 16:25:12 -04001202 strvec_pushf(&args, "--filter=%s", options.filter);
1203 strvec_push(&args, url.buf);
Ivan Todoroski81507492012-04-02 17:14:44 +02001204
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001205 for (i = 0; i < nr_heads; i++) {
1206 struct ref *ref = to_fetch[i];
Jeff King94ee8e22015-01-28 12:58:50 -05001207 if (!*ref->name)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001208 die(_("cannot fetch by sha1 over smart http"));
Nguyễn Thái Ngọc Duy58f2ed02013-12-05 20:02:49 +07001209 packet_buf_write(&preamble, "%s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001210 oid_to_hex(&ref->old_oid), ref->name);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001211 }
Ivan Todoroski81507492012-04-02 17:14:44 +02001212 packet_buf_flush(&preamble);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001213
1214 memset(&rpc, 0, sizeof(rpc));
1215 rpc.service_name = "git-upload-pack",
Shawn O. Pearceb8538602009-10-30 17:47:43 -07001216 rpc.gzip_request = 1;
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001217
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001218 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
Jonathan Tanb3590302019-02-14 11:06:37 -08001219 if (rpc_result.len)
1220 write_or_die(1, rpc_result.buf, rpc_result.len);
1221 strbuf_release(&rpc_result);
Ivan Todoroski81507492012-04-02 17:14:44 +02001222 strbuf_release(&preamble);
Jeff Kingc972bf42020-07-28 16:25:12 -04001223 strvec_clear(&args);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001224 return err;
1225}
1226
1227static int fetch(int nr_heads, struct ref **to_fetch)
1228{
Jeff King2a455202013-02-20 15:07:19 -05001229 struct discovery *d = discover_refs("git-upload-pack", 0);
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001230 if (d->proto_git)
1231 return fetch_git(d, nr_heads, to_fetch);
1232 else
1233 return fetch_dumb(nr_heads, to_fetch);
1234}
1235
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001236static void parse_fetch(struct strbuf *buf)
1237{
1238 struct ref **to_fetch = NULL;
1239 struct ref *list_head = NULL;
1240 struct ref **list = &list_head;
1241 int alloc_heads = 0, nr_heads = 0;
1242
1243 do {
Jeff King95b567c2014-06-18 15:48:29 -04001244 const char *p;
1245 if (skip_prefix(buf->buf, "fetch ", &p)) {
1246 const char *name;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001247 struct ref *ref;
brian m. carlson8338c912015-11-10 02:22:22 +00001248 struct object_id old_oid;
brian m. carlson9c9492e2019-02-19 00:05:16 +00001249 const char *q;
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001250
brian m. carlson9c9492e2019-02-19 00:05:16 +00001251 if (parse_oid_hex(p, &old_oid, &q))
Jiang Xin8a1569d2019-06-02 23:11:22 +08001252 die(_("protocol error: expected sha/ref, got '%s'"), p);
brian m. carlson9c9492e2019-02-19 00:05:16 +00001253 if (*q == ' ')
1254 name = q + 1;
1255 else if (!*q)
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001256 name = "";
1257 else
Jiang Xin8a1569d2019-06-02 23:11:22 +08001258 die(_("protocol error: expected sha/ref, got '%s'"), p);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001259
1260 ref = alloc_ref(name);
brian m. carlson8338c912015-11-10 02:22:22 +00001261 oidcpy(&ref->old_oid, &old_oid);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001262
1263 *list = ref;
1264 list = &ref->next;
1265
1266 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1267 to_fetch[nr_heads++] = ref;
1268 }
1269 else
Johannes Schindelined8b4132019-03-05 15:20:40 -08001270 die(_("http transport does not support %s"), buf->buf);
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001271
1272 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001273 if (strbuf_getline_lf(buf, stdin) == EOF)
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001274 return;
1275 if (!*buf->buf)
1276 break;
1277 } while (1);
1278
Shawn O. Pearce249b2002009-10-30 17:47:42 -07001279 if (fetch(nr_heads, to_fetch))
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001280 exit(128); /* error already reported */
1281 free_refs(list_head);
1282 free(to_fetch);
1283
1284 printf("\n");
1285 fflush(stdout);
1286 strbuf_reset(buf);
1287}
1288
Derrick Stoleeb5624a42022-08-09 13:11:39 +00001289static void parse_get(const char *arg)
1290{
1291 struct strbuf url = STRBUF_INIT;
1292 struct strbuf path = STRBUF_INIT;
1293 const char *space;
1294
1295 space = strchr(arg, ' ');
1296
1297 if (!space)
1298 die(_("protocol error: expected '<url> <path>', missing space"));
1299
1300 strbuf_add(&url, arg, space - arg);
1301 strbuf_addstr(&path, space + 1);
1302
1303 if (http_get_file(url.buf, path.buf, NULL))
1304 die(_("failed to download file at URL '%s'"), url.buf);
1305
1306 strbuf_release(&url);
1307 strbuf_release(&path);
1308 printf("\n");
1309 fflush(stdout);
1310}
1311
René Scharfe062a3092019-10-13 15:37:39 +02001312static int push_dav(int nr_spec, const char **specs)
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001313{
Jeff King850d2fe2016-02-22 17:44:21 -05001314 struct child_process child = CHILD_PROCESS_INIT;
1315 size_t i;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001316
Jeff King850d2fe2016-02-22 17:44:21 -05001317 child.git_cmd = 1;
Jeff Kingc972bf42020-07-28 16:25:12 -04001318 strvec_push(&child.args, "http-push");
1319 strvec_push(&child.args, "--helper-status");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001320 if (options.dry_run)
Jeff Kingc972bf42020-07-28 16:25:12 -04001321 strvec_push(&child.args, "--dry-run");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001322 if (options.verbosity > 1)
Jeff Kingc972bf42020-07-28 16:25:12 -04001323 strvec_push(&child.args, "--verbose");
1324 strvec_push(&child.args, url.buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001325 for (i = 0; i < nr_spec; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -04001326 strvec_push(&child.args, specs[i]);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001327
Jeff King850d2fe2016-02-22 17:44:21 -05001328 if (run_command(&child))
Johannes Schindelined8b4132019-03-05 15:20:40 -08001329 die(_("git-http-push failed"));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001330 return 0;
1331}
1332
René Scharfe062a3092019-10-13 15:37:39 +02001333static int push_git(struct discovery *heads, int nr_spec, const char **specs)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001334{
Li Linchaob0c4adc2022-07-11 05:58:54 +00001335 struct rpc_state rpc = RPC_STATE_INIT;
Junio C Hamano222b1212013-07-08 22:16:31 -07001336 int i, err;
Jeff Kingc972bf42020-07-28 16:25:12 -04001337 struct strvec args;
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001338 struct string_list_item *cas_option;
Jeff King26be19b2014-08-21 08:21:20 -04001339 struct strbuf preamble = STRBUF_INIT;
Jonathan Tanb3590302019-02-14 11:06:37 -08001340 struct strbuf rpc_result = STRBUF_INIT;
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001341
Jeff Kingc972bf42020-07-28 16:25:12 -04001342 strvec_init(&args);
1343 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
Jeff Kingf6d89422020-07-28 16:26:31 -04001344 NULL);
Junio C Hamano222b1212013-07-08 22:16:31 -07001345
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001346 if (options.thin)
Jeff Kingc972bf42020-07-28 16:25:12 -04001347 strvec_push(&args, "--thin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001348 if (options.dry_run)
Jeff Kingc972bf42020-07-28 16:25:12 -04001349 strvec_push(&args, "--dry-run");
Dave Borowitz30261092015-08-19 11:26:46 -04001350 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
Jeff Kingc972bf42020-07-28 16:25:12 -04001351 strvec_push(&args, "--signed=yes");
Dave Borowitz30261092015-08-19 11:26:46 -04001352 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
Jeff Kingc972bf42020-07-28 16:25:12 -04001353 strvec_push(&args, "--signed=if-asked");
brian m. carlson6f119422019-10-16 23:45:34 +00001354 if (options.atomic)
Jeff Kingc972bf42020-07-28 16:25:12 -04001355 strvec_push(&args, "--atomic");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001356 if (options.verbosity == 0)
Jeff Kingc972bf42020-07-28 16:25:12 -04001357 strvec_push(&args, "--quiet");
Clemens Buchacherc207e342012-01-08 22:06:20 +01001358 else if (options.verbosity > 1)
Jeff Kingc972bf42020-07-28 16:25:12 -04001359 strvec_push(&args, "--verbose");
Brandon Williams511155d2017-03-22 15:22:00 -07001360 for (i = 0; i < options.push_options.nr; i++)
Jeff Kingc972bf42020-07-28 16:25:12 -04001361 strvec_pushf(&args, "--push-option=%s",
Jeff Kingf6d89422020-07-28 16:26:31 -04001362 options.push_options.items[i].string);
Jeff Kingc972bf42020-07-28 16:25:12 -04001363 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
Junio C Hamano05c1eb12013-08-02 15:14:50 -07001364 for_each_string_list_item(cas_option, &cas_options)
Jeff Kingc972bf42020-07-28 16:25:12 -04001365 strvec_push(&args, cas_option->string);
1366 strvec_push(&args, url.buf);
Jeff King26be19b2014-08-21 08:21:20 -04001367
Srinidhi Kaushik3b990aa2020-10-03 17:40:45 +05301368 if (options.force_if_includes)
1369 strvec_push(&args, "--force-if-includes");
1370
Jeff Kingc972bf42020-07-28 16:25:12 -04001371 strvec_push(&args, "--stdin");
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001372 for (i = 0; i < nr_spec; i++)
Jeff King26be19b2014-08-21 08:21:20 -04001373 packet_buf_write(&preamble, "%s\n", specs[i]);
1374 packet_buf_flush(&preamble);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001375
1376 memset(&rpc, 0, sizeof(rpc));
1377 rpc.service_name = "git-receive-pack",
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001378
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001379 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
Jonathan Tanb3590302019-02-14 11:06:37 -08001380 if (rpc_result.len)
1381 write_or_die(1, rpc_result.buf, rpc_result.len);
1382 strbuf_release(&rpc_result);
Jeff King26be19b2014-08-21 08:21:20 -04001383 strbuf_release(&preamble);
Jeff Kingc972bf42020-07-28 16:25:12 -04001384 strvec_clear(&args);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001385 return err;
1386}
1387
René Scharfe062a3092019-10-13 15:37:39 +02001388static int push(int nr_spec, const char **specs)
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001389{
Jeff King2a455202013-02-20 15:07:19 -05001390 struct discovery *heads = discover_refs("git-receive-pack", 1);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001391 int ret;
1392
1393 if (heads->proto_git)
1394 ret = push_git(heads, nr_spec, specs);
1395 else
1396 ret = push_dav(nr_spec, specs);
1397 free_discovery(heads);
1398 return ret;
1399}
1400
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001401static void parse_push(struct strbuf *buf)
1402{
Jeff Kingc972bf42020-07-28 16:25:12 -04001403 struct strvec specs = STRVEC_INIT;
René Scharfe062a3092019-10-13 15:37:39 +02001404 int ret;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001405
1406 do {
Junio C Hamano145136a2020-01-30 11:35:46 -08001407 const char *arg;
1408 if (skip_prefix(buf->buf, "push ", &arg))
Jeff Kingc972bf42020-07-28 16:25:12 -04001409 strvec_push(&specs, arg);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001410 else
Johannes Schindelined8b4132019-03-05 15:20:40 -08001411 die(_("http transport does not support %s"), buf->buf);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001412
1413 strbuf_reset(buf);
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001414 if (strbuf_getline_lf(buf, stdin) == EOF)
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001415 goto free_specs;
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001416 if (!*buf->buf)
1417 break;
1418 } while (1);
1419
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001420 ret = push(specs.nr, specs.v);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001421 printf("\n");
1422 fflush(stdout);
Jim Meyeringdc4cd762011-06-20 09:40:06 +02001423
Shawn O. Pearce5238cbf2012-01-19 19:12:09 -08001424 if (ret)
1425 exit(128); /* error already reported */
1426
Denton Liu04cc91a2020-05-18 11:47:19 -04001427free_specs:
Jeff Kingc972bf42020-07-28 16:25:12 -04001428 strvec_clear(&specs);
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001429}
1430
Brandon Williams0f1dc532018-03-15 10:31:41 -07001431static int stateless_connect(const char *service_name)
1432{
1433 struct discovery *discover;
Li Linchaob0c4adc2022-07-11 05:58:54 +00001434 struct rpc_state rpc = RPC_STATE_INIT;
Jonathan Tana97d0072019-02-21 12:24:41 -08001435 struct strbuf buf = STRBUF_INIT;
Li Linchaob0c4adc2022-07-11 05:58:54 +00001436 const char *accept_language;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001437
1438 /*
1439 * Run the info/refs request and see if the server supports protocol
1440 * v2. If and only if the server supports v2 can we successfully
1441 * establish a stateless connection, otherwise we need to tell the
1442 * client to fallback to using other transport helper functions to
1443 * complete their request.
1444 */
1445 discover = discover_refs(service_name, 0);
1446 if (discover->version != protocol_v2) {
1447 printf("fallback\n");
1448 fflush(stdout);
1449 return -1;
1450 } else {
1451 /* Stateless Connection established */
1452 printf("\n");
1453 fflush(stdout);
1454 }
Li Linchaob0c4adc2022-07-11 05:58:54 +00001455 accept_language = http_get_accept_language_header();
1456 if (accept_language)
1457 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001458
Jonathan Tana97d0072019-02-21 12:24:41 -08001459 rpc.service_name = service_name;
1460 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1461 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1462 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1463 if (get_protocol_http_header(discover->version, &buf)) {
1464 rpc.protocol_header = strbuf_detach(&buf, NULL);
1465 } else {
1466 rpc.protocol_header = NULL;
1467 strbuf_release(&buf);
1468 }
1469 rpc.buf = xmalloc(http_post_buffer);
1470 rpc.alloc = http_post_buffer;
1471 rpc.len = 0;
1472 rpc.pos = 0;
1473 rpc.in = 1;
1474 rpc.out = 0;
1475 rpc.any_written = 0;
1476 rpc.gzip_request = 1;
1477 rpc.initial_buffer = 0;
1478 rpc.write_line_lengths = 1;
1479 rpc.flush_read_but_not_sent = 0;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001480
1481 /*
1482 * Dump the capability listing that we got from the server earlier
1483 * during the info/refs request.
1484 */
Jonathan Tana97d0072019-02-21 12:24:41 -08001485 write_or_die(rpc.in, discover->buf, discover->len);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001486
Jonathan Tana97d0072019-02-21 12:24:41 -08001487 /* Until we see EOF keep sending POSTs */
1488 while (1) {
1489 size_t avail;
1490 enum packet_read_status status;
1491
1492 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1493 &status))
1494 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1495 if (status == PACKET_READ_EOF)
1496 break;
Denton Liu74b082a2020-05-19 06:53:58 -04001497 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
Brandon Williams0f1dc532018-03-15 10:31:41 -07001498 /* We would have an err here */
1499 break;
Jonathan Tana97d0072019-02-21 12:24:41 -08001500 /* Reset the buffer for next request */
1501 rpc.len = 0;
Brandon Williams0f1dc532018-03-15 10:31:41 -07001502 }
1503
Jonathan Tana97d0072019-02-21 12:24:41 -08001504 free(rpc.service_url);
1505 free(rpc.hdr_content_type);
1506 free(rpc.hdr_accept);
Li Linchaob0c4adc2022-07-11 05:58:54 +00001507 free(rpc.hdr_accept_language);
Jonathan Tana97d0072019-02-21 12:24:41 -08001508 free(rpc.protocol_header);
1509 free(rpc.buf);
1510 strbuf_release(&buf);
1511
Brandon Williams0f1dc532018-03-15 10:31:41 -07001512 return 0;
1513}
1514
Jeff King3f2e2292016-07-01 01:58:58 -04001515int cmd_main(int argc, const char **argv)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001516{
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001517 struct strbuf buf = STRBUF_INIT;
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001518 int nongit;
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001519 int ret = 1;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001520
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001521 setup_git_directory_gently(&nongit);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001522 if (argc < 2) {
Johannes Schindelined8b4132019-03-05 15:20:40 -08001523 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001524 goto cleanup;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001525 }
1526
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001527 options.verbosity = 1;
1528 options.progress = !!isatty(2);
Shawn O. Pearcede1a2fd2009-10-30 17:47:41 -07001529 options.thin = 1;
Ævar Arnfjörð Bjarmasonabf897b2021-09-28 14:49:45 +02001530 string_list_init_dup(&options.deepen_not);
1531 string_list_init_dup(&options.push_options);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001532
Jeff Hostetleree4512e2019-02-22 14:25:01 -08001533 /*
1534 * Just report "remote-curl" here (folding all the various aliases
1535 * ("git-remote-http", "git-remote-https", and etc.) here since they
1536 * are all just copies of the same actual executable.
1537 */
1538 trace2_cmd_name("remote-curl");
1539
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001540 remote = remote_get(argv[1]);
1541
1542 if (argc > 2) {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001543 end_url_with_slash(&url, argv[2]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001544 } else {
Jeff Kingb227bbc2013-09-28 04:35:25 -04001545 end_url_with_slash(&url, remote->url[0]);
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001546 }
1547
Jeff Kingb227bbc2013-09-28 04:35:25 -04001548 http_init(remote, url.buf, 0);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001549
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001550 do {
Jeff King95b567c2014-06-18 15:48:29 -04001551 const char *arg;
1552
Junio C Hamano8f309ae2016-01-13 15:31:17 -08001553 if (strbuf_getline_lf(&buf, stdin) == EOF) {
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001554 if (ferror(stdin))
Johannes Schindelined8b4132019-03-05 15:20:40 -08001555 error(_("remote-curl: error reading command stream from git"));
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001556 goto cleanup;
Sverre Rabbelier1843f0c2011-07-16 15:03:29 +02001557 }
1558 if (buf.len == 0)
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001559 break;
Christian Couder59556542013-11-30 21:55:40 +01001560 if (starts_with(buf.buf, "fetch ")) {
Daniel Barkalowa45d3d72009-11-03 21:52:35 -05001561 if (nongit)
Johannes Schindelined8b4132019-03-05 15:20:40 -08001562 die(_("remote-curl: fetch attempted without a local repo"));
Shawn O. Pearce292ce462009-10-30 17:47:28 -07001563 parse_fetch(&buf);
1564
Christian Couder59556542013-11-30 21:55:40 +01001565 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
Shawn O. Pearce97cc7bc2009-10-30 17:47:40 -07001566 int for_push = !!strstr(buf.buf + 4, "for-push");
1567 output_refs(get_refs(for_push));
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001568
Christian Couder59556542013-11-30 21:55:40 +01001569 } else if (starts_with(buf.buf, "push ")) {
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001570 parse_push(&buf);
1571
Jeff King95b567c2014-06-18 15:48:29 -04001572 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1573 char *value = strchr(arg, ' ');
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001574 int result;
1575
1576 if (value)
1577 *value++ = '\0';
1578 else
1579 value = "true";
1580
Jeff King95b567c2014-06-18 15:48:29 -04001581 result = set_option(arg, value);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001582 if (!result)
1583 printf("ok\n");
1584 else if (result < 0)
1585 printf("error invalid value\n");
1586 else
1587 printf("unsupported\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001588 fflush(stdout);
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001589
Derrick Stoleeb5624a42022-08-09 13:11:39 +00001590 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1591 parse_get(arg);
1592 fflush(stdout);
1593
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001594 } else if (!strcmp(buf.buf, "capabilities")) {
Brandon Williams0f1dc532018-03-15 10:31:41 -07001595 printf("stateless-connect\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001596 printf("fetch\n");
Derrick Stoleeb5624a42022-08-09 13:11:39 +00001597 printf("get\n");
Shawn O. Pearceef08ef92009-10-30 17:47:29 -07001598 printf("option\n");
Shawn O. Pearceae4efe12009-10-30 17:47:30 -07001599 printf("push\n");
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +07001600 printf("check-connectivity\n");
brian m. carlson7f605012020-05-25 19:59:04 +00001601 printf("object-format\n");
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001602 printf("\n");
1603 fflush(stdout);
Brandon Williams0f1dc532018-03-15 10:31:41 -07001604 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1605 if (!stateless_connect(arg))
1606 break;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001607 } else {
Johannes Schindelined8b4132019-03-05 15:20:40 -08001608 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001609 goto cleanup;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001610 }
1611 strbuf_reset(&buf);
1612 } while (1);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001613
1614 http_cleanup();
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001615 ret = 0;
1616cleanup:
1617 strbuf_release(&buf);
Tay Ray Chuan888692b2010-03-02 18:49:29 +08001618
Ævar Arnfjörð Bjarmasonb07fa8f2022-03-04 19:32:08 +01001619 return ret;
Daniel Barkalowa2d725b2009-08-05 01:01:56 -04001620}