]>
Commit | Line | Data |
---|---|---|
a2d725b7 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
a2d725b7 | 3 | #include "remote.h" |
ad6ac124 | 4 | #include "connect.h" |
a2d725b7 DB |
5 | #include "strbuf.h" |
6 | #include "walker.h" | |
7 | #include "http.h" | |
d807c4a0 | 8 | #include "exec-cmd.h" |
ae4efe19 | 9 | #include "run-command.h" |
97cc7bc4 | 10 | #include "pkt-line.h" |
05c1eb10 | 11 | #include "string-list.h" |
de1a2fdd | 12 | #include "sideband.h" |
dbbcd44f | 13 | #include "strvec.h" |
2501aff8 | 14 | #include "credential.h" |
fe299ec5 | 15 | #include "oid-array.h" |
30261094 | 16 | #include "send-pack.h" |
ad6ac124 | 17 | #include "protocol.h" |
90dce21e | 18 | #include "quote.h" |
c1284b21 | 19 | #include "transport.h" |
a2d725b7 | 20 | |
37a8768f | 21 | static struct remote *remote; |
b227bbc4 JK |
22 | /* always ends with a trailing slash */ |
23 | static struct strbuf url = STRBUF_INIT; | |
37a8768f | 24 | |
ef08ef9e SP |
25 | struct options { |
26 | int verbosity; | |
27 | unsigned long depth; | |
508ea882 | 28 | char *deepen_since; |
a45a2600 | 29 | struct string_list deepen_not; |
511155db | 30 | struct string_list push_options; |
acb0c572 | 31 | char *filter; |
ef08ef9e | 32 | unsigned progress : 1, |
9ba38048 | 33 | check_self_contained_and_connected : 1, |
16094885 NTND |
34 | cloning : 1, |
35 | update_shallow : 1, | |
ae4efe19 | 36 | followtags : 1, |
de1a2fdd | 37 | dry_run : 1, |
0ea47f9d | 38 | thin : 1, |
30261094 | 39 | /* One of the SEND_PACK_PUSH_CERT_* constants. */ |
cccf74e2 | 40 | push_cert : 2, |
88e2f9ed JT |
41 | deepen_relative : 1, |
42 | from_promisor : 1, | |
6f119424 | 43 | no_dependents : 1, |
7f605017 | 44 | atomic : 1, |
45 | object_format : 1; | |
46 | const struct git_hash_algo *hash_algo; | |
ef08ef9e SP |
47 | }; |
48 | static struct options options; | |
05c1eb10 | 49 | static struct string_list cas_options = STRING_LIST_INIT_DUP; |
ef08ef9e | 50 | |
ef08ef9e SP |
51 | static int set_option(const char *name, const char *value) |
52 | { | |
53 | if (!strcmp(name, "verbosity")) { | |
54 | char *end; | |
55 | int v = strtol(value, &end, 10); | |
56 | if (value == end || *end) | |
57 | return -1; | |
58 | options.verbosity = v; | |
59 | return 0; | |
60 | } | |
61 | else if (!strcmp(name, "progress")) { | |
62 | if (!strcmp(value, "true")) | |
63 | options.progress = 1; | |
64 | else if (!strcmp(value, "false")) | |
65 | options.progress = 0; | |
66 | else | |
67 | return -1; | |
249b2004 | 68 | return 0; |
ef08ef9e SP |
69 | } |
70 | else if (!strcmp(name, "depth")) { | |
71 | char *end; | |
72 | unsigned long v = strtoul(value, &end, 10); | |
73 | if (value == end || *end) | |
74 | return -1; | |
75 | options.depth = v; | |
249b2004 | 76 | return 0; |
ef08ef9e | 77 | } |
508ea882 NTND |
78 | else if (!strcmp(name, "deepen-since")) { |
79 | options.deepen_since = xstrdup(value); | |
80 | return 0; | |
81 | } | |
a45a2600 NTND |
82 | else if (!strcmp(name, "deepen-not")) { |
83 | string_list_append(&options.deepen_not, value); | |
84 | return 0; | |
85 | } | |
cccf74e2 NTND |
86 | else if (!strcmp(name, "deepen-relative")) { |
87 | if (!strcmp(value, "true")) | |
88 | options.deepen_relative = 1; | |
89 | else if (!strcmp(value, "false")) | |
90 | options.deepen_relative = 0; | |
91 | else | |
92 | return -1; | |
93 | return 0; | |
94 | } | |
ef08ef9e SP |
95 | else if (!strcmp(name, "followtags")) { |
96 | if (!strcmp(value, "true")) | |
97 | options.followtags = 1; | |
98 | else if (!strcmp(value, "false")) | |
99 | options.followtags = 0; | |
100 | else | |
101 | return -1; | |
249b2004 | 102 | return 0; |
ef08ef9e | 103 | } |
ae4efe19 SP |
104 | else if (!strcmp(name, "dry-run")) { |
105 | if (!strcmp(value, "true")) | |
106 | options.dry_run = 1; | |
107 | else if (!strcmp(value, "false")) | |
108 | options.dry_run = 0; | |
109 | else | |
110 | return -1; | |
111 | return 0; | |
112 | } | |
9ba38048 NTND |
113 | else if (!strcmp(name, "check-connectivity")) { |
114 | if (!strcmp(value, "true")) | |
115 | options.check_self_contained_and_connected = 1; | |
116 | else if (!strcmp(value, "false")) | |
117 | options.check_self_contained_and_connected = 0; | |
118 | else | |
119 | return -1; | |
120 | return 0; | |
121 | } | |
05c1eb10 JH |
122 | else if (!strcmp(name, "cas")) { |
123 | struct strbuf val = STRBUF_INIT; | |
cd85b447 | 124 | strbuf_addstr(&val, "--force-with-lease="); |
125 | if (*value != '"') | |
126 | strbuf_addstr(&val, value); | |
127 | else if (unquote_c_style(&val, value, NULL)) | |
128 | return -1; | |
05c1eb10 JH |
129 | string_list_append(&cas_options, val.buf); |
130 | strbuf_release(&val); | |
131 | return 0; | |
16094885 NTND |
132 | } else if (!strcmp(name, "cloning")) { |
133 | if (!strcmp(value, "true")) | |
134 | options.cloning = 1; | |
135 | else if (!strcmp(value, "false")) | |
136 | options.cloning = 0; | |
137 | else | |
138 | return -1; | |
139 | return 0; | |
140 | } else if (!strcmp(name, "update-shallow")) { | |
141 | if (!strcmp(value, "true")) | |
142 | options.update_shallow = 1; | |
143 | else if (!strcmp(value, "false")) | |
144 | options.update_shallow = 0; | |
145 | else | |
146 | return -1; | |
147 | return 0; | |
0ea47f9d JH |
148 | } else if (!strcmp(name, "pushcert")) { |
149 | if (!strcmp(value, "true")) | |
30261094 | 150 | options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; |
0ea47f9d | 151 | else if (!strcmp(value, "false")) |
30261094 DB |
152 | options.push_cert = SEND_PACK_PUSH_CERT_NEVER; |
153 | else if (!strcmp(value, "if-asked")) | |
154 | options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; | |
0ea47f9d JH |
155 | else |
156 | return -1; | |
157 | return 0; | |
6f119424 | 158 | } else if (!strcmp(name, "atomic")) { |
159 | if (!strcmp(value, "true")) | |
160 | options.atomic = 1; | |
161 | else if (!strcmp(value, "false")) | |
162 | options.atomic = 0; | |
163 | else | |
164 | return -1; | |
165 | return 0; | |
511155db | 166 | } else if (!strcmp(name, "push-option")) { |
90dce21e JK |
167 | if (*value != '"') |
168 | string_list_append(&options.push_options, value); | |
169 | else { | |
170 | struct strbuf unquoted = STRBUF_INIT; | |
171 | if (unquote_c_style(&unquoted, value, NULL) < 0) | |
ed8b4132 | 172 | die(_("invalid quoting in push-option value: '%s'"), value); |
90dce21e JK |
173 | string_list_append_nodup(&options.push_options, |
174 | strbuf_detach(&unquoted, NULL)); | |
175 | } | |
511155db | 176 | return 0; |
c915f11e EW |
177 | |
178 | #if LIBCURL_VERSION_NUM >= 0x070a08 | |
179 | } else if (!strcmp(name, "family")) { | |
180 | if (!strcmp(value, "ipv4")) | |
181 | git_curl_ipresolve = CURL_IPRESOLVE_V4; | |
182 | else if (!strcmp(value, "ipv6")) | |
183 | git_curl_ipresolve = CURL_IPRESOLVE_V6; | |
184 | else if (!strcmp(value, "all")) | |
185 | git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER; | |
186 | else | |
187 | return -1; | |
188 | return 0; | |
189 | #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */ | |
88e2f9ed JT |
190 | } else if (!strcmp(name, "from-promisor")) { |
191 | options.from_promisor = 1; | |
192 | return 0; | |
193 | } else if (!strcmp(name, "no-dependents")) { | |
194 | options.no_dependents = 1; | |
195 | return 0; | |
acb0c572 | 196 | } else if (!strcmp(name, "filter")) { |
c3b9bc94 | 197 | options.filter = xstrdup(value); |
acb0c572 | 198 | return 0; |
7f605017 | 199 | } else if (!strcmp(name, "object-format")) { |
200 | int algo; | |
201 | options.object_format = 1; | |
202 | if (strcmp(value, "true")) { | |
203 | algo = hash_algo_by_name(value); | |
204 | if (algo == GIT_HASH_UNKNOWN) | |
205 | die("unknown object format '%s'", value); | |
206 | options.hash_algo = &hash_algos[algo]; | |
207 | } | |
208 | return 0; | |
16094885 | 209 | } else { |
ef08ef9e SP |
210 | return 1 /* unsupported */; |
211 | } | |
212 | } | |
213 | ||
97cc7bc4 | 214 | struct discovery { |
f08a5d42 | 215 | char *service; |
97cc7bc4 SP |
216 | char *buf_alloc; |
217 | char *buf; | |
218 | size_t len; | |
2a455202 | 219 | struct ref *refs; |
910650d2 | 220 | struct oid_array shallow; |
49e85e95 | 221 | enum protocol_version version; |
97cc7bc4 SP |
222 | unsigned proto_git : 1; |
223 | }; | |
224 | static struct discovery *last_discovery; | |
225 | ||
b8054bbe JK |
226 | static struct ref *parse_git_refs(struct discovery *heads, int for_push) |
227 | { | |
228 | struct ref *list = NULL; | |
ad6ac124 BW |
229 | struct packet_reader reader; |
230 | ||
231 | packet_reader_init(&reader, -1, heads->buf, heads->len, | |
232 | PACKET_READ_CHOMP_NEWLINE | | |
2d103c31 MS |
233 | PACKET_READ_GENTLE_ON_EOF | |
234 | PACKET_READ_DIE_ON_ERR_PACKET); | |
ad6ac124 | 235 | |
49e85e95 BW |
236 | heads->version = discover_version(&reader); |
237 | switch (heads->version) { | |
8f6982b4 | 238 | case protocol_v2: |
0f1dc53f BW |
239 | /* |
240 | * Do nothing. This isn't a list of refs but rather a | |
241 | * capability advertisement. Client would have run | |
242 | * 'stateless-connect' so we'll dump this capability listing | |
243 | * and let them request the refs themselves. | |
244 | */ | |
8f6982b4 | 245 | break; |
ad6ac124 BW |
246 | case protocol_v1: |
247 | case protocol_v0: | |
248 | get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0, | |
249 | NULL, &heads->shallow); | |
7f605017 | 250 | options.hash_algo = reader.hash_algo; |
ad6ac124 BW |
251 | break; |
252 | case protocol_unknown_version: | |
253 | BUG("unknown protocol version"); | |
254 | } | |
255 | ||
b8054bbe JK |
256 | return list; |
257 | } | |
258 | ||
ac093d07 | 259 | static const struct git_hash_algo *detect_hash_algo(struct discovery *heads) |
260 | { | |
261 | const char *p = memchr(heads->buf, '\t', heads->len); | |
262 | int algo; | |
263 | if (!p) | |
264 | return the_hash_algo; | |
265 | ||
266 | algo = hash_algo_by_length((p - heads->buf) / 2); | |
267 | if (algo == GIT_HASH_UNKNOWN) | |
268 | return NULL; | |
269 | return &hash_algos[algo]; | |
270 | } | |
271 | ||
b8054bbe JK |
272 | static struct ref *parse_info_refs(struct discovery *heads) |
273 | { | |
274 | char *data, *start, *mid; | |
275 | char *ref_name; | |
276 | int i = 0; | |
277 | ||
278 | struct ref *refs = NULL; | |
279 | struct ref *ref = NULL; | |
280 | struct ref *last_ref = NULL; | |
281 | ||
ac093d07 | 282 | options.hash_algo = detect_hash_algo(heads); |
283 | if (!options.hash_algo) | |
284 | die("%sinfo/refs not valid: could not determine hash algorithm; " | |
285 | "is this a git repository?", | |
286 | transport_anonymize_url(url.buf)); | |
287 | ||
b8054bbe JK |
288 | data = heads->buf; |
289 | start = NULL; | |
290 | mid = data; | |
291 | while (i < heads->len) { | |
292 | if (!start) { | |
293 | start = &data[i]; | |
294 | } | |
295 | if (data[i] == '\t') | |
296 | mid = &data[i]; | |
297 | if (data[i] == '\n') { | |
ac093d07 | 298 | if (mid - start != options.hash_algo->hexsz) |
ed8b4132 | 299 | die(_("%sinfo/refs not valid: is this a git repository?"), |
c1284b21 | 300 | transport_anonymize_url(url.buf)); |
b8054bbe JK |
301 | data[i] = 0; |
302 | ref_name = mid + 1; | |
6f687c21 | 303 | ref = alloc_ref(ref_name); |
ac093d07 | 304 | get_oid_hex_algop(start, &ref->old_oid, options.hash_algo); |
b8054bbe JK |
305 | if (!refs) |
306 | refs = ref; | |
307 | if (last_ref) | |
308 | last_ref->next = ref; | |
309 | last_ref = ref; | |
310 | start = NULL; | |
311 | } | |
312 | i++; | |
313 | } | |
314 | ||
315 | ref = alloc_ref("HEAD"); | |
b227bbc4 | 316 | if (!http_fetch_ref(url.buf, ref) && |
b8054bbe JK |
317 | !resolve_remote_symref(ref, refs)) { |
318 | ref->next = refs; | |
319 | refs = ref; | |
320 | } else { | |
321 | free(ref); | |
322 | } | |
323 | ||
324 | return refs; | |
325 | } | |
326 | ||
97cc7bc4 SP |
327 | static void free_discovery(struct discovery *d) |
328 | { | |
329 | if (d) { | |
330 | if (d == last_discovery) | |
331 | last_discovery = NULL; | |
ee3051bd | 332 | free(d->shallow.oid); |
97cc7bc4 | 333 | free(d->buf_alloc); |
2a455202 | 334 | free_refs(d->refs); |
f08a5d42 | 335 | free(d->service); |
97cc7bc4 SP |
336 | free(d); |
337 | } | |
338 | } | |
339 | ||
fc1b774c JK |
340 | static int show_http_message(struct strbuf *type, struct strbuf *charset, |
341 | struct strbuf *msg) | |
426e70d4 JK |
342 | { |
343 | const char *p, *eol; | |
344 | ||
345 | /* | |
346 | * We only show text/plain parts, as other types are likely | |
347 | * to be ugly to look at on the user's terminal. | |
426e70d4 | 348 | */ |
bf197fd7 | 349 | if (strcmp(type->buf, "text/plain")) |
426e70d4 | 350 | return -1; |
fc1b774c JK |
351 | if (charset->len) |
352 | strbuf_reencode(msg, charset->buf, get_log_output_encoding()); | |
426e70d4 JK |
353 | |
354 | strbuf_trim(msg); | |
355 | if (!msg->len) | |
356 | return -1; | |
357 | ||
358 | p = msg->buf; | |
359 | do { | |
360 | eol = strchrnul(p, '\n'); | |
361 | fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p); | |
362 | p = eol + 1; | |
363 | } while(*eol); | |
364 | return 0; | |
365 | } | |
366 | ||
884e586f BW |
367 | static int get_protocol_http_header(enum protocol_version version, |
368 | struct strbuf *header) | |
369 | { | |
370 | if (version > 0) { | |
371 | strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d", | |
372 | version); | |
373 | ||
374 | return 1; | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } | |
379 | ||
8ee3e120 JK |
380 | static void check_smart_http(struct discovery *d, const char *service, |
381 | struct strbuf *type) | |
382 | { | |
383 | const char *p; | |
384 | struct packet_reader reader; | |
385 | ||
386 | /* | |
387 | * If we don't see x-$service-advertisement, then it's not smart-http. | |
388 | * But once we do, we commit to it and assume any other protocol | |
389 | * violations are hard errors. | |
390 | */ | |
391 | if (!skip_prefix(type->buf, "application/x-", &p) || | |
392 | !skip_prefix(p, service, &p) || | |
393 | strcmp(p, "-advertisement")) | |
394 | return; | |
395 | ||
396 | packet_reader_init(&reader, -1, d->buf, d->len, | |
397 | PACKET_READ_CHOMP_NEWLINE | | |
398 | PACKET_READ_DIE_ON_ERR_PACKET); | |
399 | if (packet_reader_read(&reader) != PACKET_READ_NORMAL) | |
aa1edf14 | 400 | die(_("invalid server response; expected service, got flush packet")); |
8ee3e120 JK |
401 | |
402 | if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) { | |
403 | /* | |
404 | * The header can include additional metadata lines, up | |
405 | * until a packet flush marker. Ignore these now, but | |
406 | * in the future we might start to scan them. | |
407 | */ | |
408 | for (;;) { | |
409 | packet_reader_read(&reader); | |
410 | if (reader.pktlen <= 0) { | |
411 | break; | |
412 | } | |
413 | } | |
414 | ||
415 | /* | |
416 | * v0 smart http; callers expect us to soak up the | |
417 | * service and header packets | |
418 | */ | |
419 | d->buf = reader.src_buffer; | |
420 | d->len = reader.src_len; | |
421 | d->proto_git = 1; | |
422 | ||
cbdb8d14 | 423 | } else if (!strcmp(reader.line, "version 2")) { |
8ee3e120 JK |
424 | /* |
425 | * v2 smart http; do not consume version packet, which will | |
426 | * be handled elsewhere. | |
427 | */ | |
428 | d->proto_git = 1; | |
429 | ||
430 | } else { | |
aa1edf14 | 431 | die(_("invalid server response; got '%s'"), reader.line); |
8ee3e120 JK |
432 | } |
433 | } | |
434 | ||
24d36f14 | 435 | static struct discovery *discover_refs(const char *service, int for_push) |
a2d725b7 | 436 | { |
4656bf47 | 437 | struct strbuf type = STRBUF_INIT; |
fc1b774c | 438 | struct strbuf charset = STRBUF_INIT; |
a2d725b7 | 439 | struct strbuf buffer = STRBUF_INIT; |
c65d5692 | 440 | struct strbuf refs_url = STRBUF_INIT; |
050ef365 | 441 | struct strbuf effective_url = STRBUF_INIT; |
884e586f BW |
442 | struct strbuf protocol_header = STRBUF_INIT; |
443 | struct string_list extra_headers = STRING_LIST_INIT_DUP; | |
97cc7bc4 | 444 | struct discovery *last = last_discovery; |
243c329c | 445 | int http_ret, maybe_smart = 0; |
fcaa6e64 | 446 | struct http_get_options http_options; |
a4d78ce2 | 447 | enum protocol_version version = get_protocol_version_config(); |
a2d725b7 | 448 | |
97cc7bc4 SP |
449 | if (last && !strcmp(service, last->service)) |
450 | return last; | |
451 | free_discovery(last); | |
a2d725b7 | 452 | |
b227bbc4 | 453 | strbuf_addf(&refs_url, "%sinfo/refs", url.buf); |
59556548 | 454 | if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) && |
02572c2e | 455 | git_env_bool("GIT_SMART_HTTP", 1)) { |
243c329c | 456 | maybe_smart = 1; |
b227bbc4 | 457 | if (!strchr(url.buf, '?')) |
c65d5692 | 458 | strbuf_addch(&refs_url, '?'); |
97cc7bc4 | 459 | else |
c65d5692 JK |
460 | strbuf_addch(&refs_url, '&'); |
461 | strbuf_addf(&refs_url, "service=%s", service); | |
97cc7bc4 | 462 | } |
a2d725b7 | 463 | |
a4d78ce2 BW |
464 | /* |
465 | * NEEDSWORK: If we are trying to use protocol v2 and we are planning | |
466 | * to perform a push, then fallback to v0 since the client doesn't know | |
467 | * how to push yet using v2. | |
468 | */ | |
469 | if (version == protocol_v2 && !strcmp("git-receive-pack", service)) | |
470 | version = protocol_v0; | |
471 | ||
884e586f | 472 | /* Add the extra Git-Protocol header */ |
a4d78ce2 | 473 | if (get_protocol_http_header(version, &protocol_header)) |
884e586f BW |
474 | string_list_append(&extra_headers, protocol_header.buf); |
475 | ||
fcaa6e64 JK |
476 | memset(&http_options, 0, sizeof(http_options)); |
477 | http_options.content_type = &type; | |
478 | http_options.charset = &charset; | |
479 | http_options.effective_url = &effective_url; | |
480 | http_options.base_url = &url; | |
884e586f | 481 | http_options.extra_headers = &extra_headers; |
50d34137 | 482 | http_options.initial_request = 1; |
fcaa6e64 | 483 | http_options.no_cache = 1; |
1bbcc224 | 484 | |
fcaa6e64 | 485 | http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options); |
a2d725b7 DB |
486 | switch (http_ret) { |
487 | case HTTP_OK: | |
488 | break; | |
489 | case HTTP_MISSING_TARGET: | |
fc1b774c | 490 | show_http_message(&type, &charset, &buffer); |
ed8b4132 | 491 | die(_("repository '%s' not found"), |
c1284b21 | 492 | transport_anonymize_url(url.buf)); |
42653c09 | 493 | case HTTP_NOAUTH: |
fc1b774c | 494 | show_http_message(&type, &charset, &buffer); |
ed8b4132 | 495 | die(_("Authentication failed for '%s'"), |
c1284b21 | 496 | transport_anonymize_url(url.buf)); |
a2d725b7 | 497 | default: |
fc1b774c | 498 | show_http_message(&type, &charset, &buffer); |
ed8b4132 | 499 | die(_("unable to access '%s': %s"), |
c1284b21 | 500 | transport_anonymize_url(url.buf), curl_errorstr); |
a2d725b7 DB |
501 | } |
502 | ||
c1284b21 JS |
503 | if (options.verbosity && !starts_with(refs_url.buf, url.buf)) { |
504 | char *u = transport_anonymize_url(url.buf); | |
505 | warning(_("redirecting to %s"), u); | |
506 | free(u); | |
507 | } | |
50d34137 | 508 | |
97cc7bc4 | 509 | last= xcalloc(1, sizeof(*last_discovery)); |
f08a5d42 | 510 | last->service = xstrdup(service); |
97cc7bc4 SP |
511 | last->buf_alloc = strbuf_detach(&buffer, &last->len); |
512 | last->buf = last->buf_alloc; | |
513 | ||
8ee3e120 JK |
514 | if (maybe_smart) |
515 | check_smart_http(last, service, &type); | |
97cc7bc4 | 516 | |
2a455202 JK |
517 | if (last->proto_git) |
518 | last->refs = parse_git_refs(last, for_push); | |
519 | else | |
520 | last->refs = parse_info_refs(last); | |
521 | ||
c65d5692 | 522 | strbuf_release(&refs_url); |
4656bf47 | 523 | strbuf_release(&type); |
fc1b774c | 524 | strbuf_release(&charset); |
050ef365 | 525 | strbuf_release(&effective_url); |
97cc7bc4 | 526 | strbuf_release(&buffer); |
884e586f BW |
527 | strbuf_release(&protocol_header); |
528 | string_list_clear(&extra_headers, 0); | |
97cc7bc4 SP |
529 | last_discovery = last; |
530 | return last; | |
531 | } | |
532 | ||
97cc7bc4 SP |
533 | static struct ref *get_refs(int for_push) |
534 | { | |
535 | struct discovery *heads; | |
536 | ||
537 | if (for_push) | |
2a455202 | 538 | heads = discover_refs("git-receive-pack", for_push); |
97cc7bc4 | 539 | else |
2a455202 | 540 | heads = discover_refs("git-upload-pack", for_push); |
97cc7bc4 | 541 | |
2a455202 | 542 | return heads->refs; |
97cc7bc4 SP |
543 | } |
544 | ||
ae4efe19 SP |
545 | static void output_refs(struct ref *refs) |
546 | { | |
547 | struct ref *posn; | |
7f605017 | 548 | if (options.object_format && options.hash_algo) { |
549 | printf(":object-format %s\n", options.hash_algo->name); | |
550 | } | |
ae4efe19 SP |
551 | for (posn = refs; posn; posn = posn->next) { |
552 | if (posn->symref) | |
553 | printf("@%s %s\n", posn->symref, posn->name); | |
554 | else | |
97997e6a | 555 | printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash, |
556 | options.hash_algo), | |
557 | posn->name); | |
ae4efe19 SP |
558 | } |
559 | printf("\n"); | |
560 | fflush(stdout); | |
ae4efe19 SP |
561 | } |
562 | ||
de1a2fdd SP |
563 | struct rpc_state { |
564 | const char *service_name; | |
de1a2fdd SP |
565 | char *service_url; |
566 | char *hdr_content_type; | |
567 | char *hdr_accept; | |
884e586f | 568 | char *protocol_header; |
de1a2fdd SP |
569 | char *buf; |
570 | size_t alloc; | |
571 | size_t len; | |
572 | size_t pos; | |
573 | int in; | |
574 | int out; | |
296b847c | 575 | int any_written; |
b8538603 | 576 | unsigned gzip_request : 1; |
6c81a990 | 577 | unsigned initial_buffer : 1; |
a97d0079 JT |
578 | |
579 | /* | |
580 | * Whenever a pkt-line is read into buf, append the 4 characters | |
581 | * denoting its length before appending the payload. | |
582 | */ | |
583 | unsigned write_line_lengths : 1; | |
584 | ||
585 | /* | |
586 | * Used by rpc_out; initialize to 0. This is true if a flush has been | |
587 | * read, but the corresponding line length (if write_line_lengths is | |
588 | * true) and EOF have not been sent to libcurl. Since each flush marks | |
589 | * the end of a request, each flush must be completely sent before any | |
590 | * further reading occurs. | |
591 | */ | |
592 | unsigned flush_read_but_not_sent : 1; | |
de1a2fdd SP |
593 | }; |
594 | ||
78ad9172 JT |
595 | /* |
596 | * Appends the result of reading from rpc->out to the string represented by | |
597 | * rpc->buf and rpc->len if there is enough space. Returns 1 if there was | |
598 | * enough space, 0 otherwise. | |
599 | * | |
a97d0079 JT |
600 | * If rpc->write_line_lengths is true, appends the line length as a 4-byte |
601 | * hexadecimal string before appending the result described above. | |
602 | * | |
603 | * Writes the total number of bytes appended into appended. | |
78ad9172 | 604 | */ |
a97d0079 JT |
605 | static int rpc_read_from_out(struct rpc_state *rpc, int options, |
606 | size_t *appended, | |
607 | enum packet_read_status *status) { | |
608 | size_t left; | |
609 | char *buf; | |
610 | int pktlen_raw; | |
611 | ||
612 | if (rpc->write_line_lengths) { | |
613 | left = rpc->alloc - rpc->len - 4; | |
614 | buf = rpc->buf + rpc->len + 4; | |
615 | } else { | |
616 | left = rpc->alloc - rpc->len; | |
617 | buf = rpc->buf + rpc->len; | |
618 | } | |
78ad9172 JT |
619 | |
620 | if (left < LARGE_PACKET_MAX) | |
621 | return 0; | |
622 | ||
a97d0079 JT |
623 | *status = packet_read_with_status(rpc->out, NULL, NULL, buf, |
624 | left, &pktlen_raw, options); | |
625 | if (*status != PACKET_READ_EOF) { | |
626 | *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0); | |
627 | rpc->len += *appended; | |
628 | } | |
629 | ||
630 | if (rpc->write_line_lengths) { | |
631 | switch (*status) { | |
632 | case PACKET_READ_EOF: | |
633 | if (!(options & PACKET_READ_GENTLE_ON_EOF)) | |
aa1edf14 | 634 | die(_("shouldn't have EOF when not gentle on EOF")); |
a97d0079 JT |
635 | break; |
636 | case PACKET_READ_NORMAL: | |
637 | set_packet_header(buf - 4, *appended); | |
638 | break; | |
639 | case PACKET_READ_DELIM: | |
640 | memcpy(buf - 4, "0001", 4); | |
641 | break; | |
642 | case PACKET_READ_FLUSH: | |
643 | memcpy(buf - 4, "0000", 4); | |
644 | break; | |
0181b600 DL |
645 | case PACKET_READ_RESPONSE_END: |
646 | die(_("remote server sent stateless separator")); | |
a97d0079 JT |
647 | } |
648 | } | |
649 | ||
78ad9172 JT |
650 | return 1; |
651 | } | |
652 | ||
de1a2fdd SP |
653 | static size_t rpc_out(void *ptr, size_t eltsize, |
654 | size_t nmemb, void *buffer_) | |
655 | { | |
656 | size_t max = eltsize * nmemb; | |
657 | struct rpc_state *rpc = buffer_; | |
658 | size_t avail = rpc->len - rpc->pos; | |
a97d0079 | 659 | enum packet_read_status status; |
de1a2fdd SP |
660 | |
661 | if (!avail) { | |
6c81a990 | 662 | rpc->initial_buffer = 0; |
78ad9172 | 663 | rpc->len = 0; |
de1a2fdd | 664 | rpc->pos = 0; |
a97d0079 JT |
665 | if (!rpc->flush_read_but_not_sent) { |
666 | if (!rpc_read_from_out(rpc, 0, &avail, &status)) | |
667 | BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX"); | |
668 | if (status == PACKET_READ_FLUSH) | |
669 | rpc->flush_read_but_not_sent = 1; | |
670 | } | |
671 | /* | |
672 | * If flush_read_but_not_sent is true, we have already read one | |
673 | * full request but have not fully sent it + EOF, which is why | |
674 | * we need to refrain from reading. | |
675 | */ | |
676 | } | |
677 | if (rpc->flush_read_but_not_sent) { | |
678 | if (!avail) { | |
679 | /* | |
680 | * The line length either does not need to be sent at | |
681 | * all or has already been completely sent. Now we can | |
682 | * return 0, indicating EOF, meaning that the flush has | |
683 | * been fully sent. | |
684 | */ | |
685 | rpc->flush_read_but_not_sent = 0; | |
686 | return 0; | |
687 | } | |
688 | /* | |
51ca7f89 | 689 | * If avail is non-zero, the line length for the flush still |
a97d0079 JT |
690 | * hasn't been fully sent. Proceed with sending the line |
691 | * length. | |
692 | */ | |
de1a2fdd SP |
693 | } |
694 | ||
48310608 | 695 | if (max < avail) |
de1a2fdd SP |
696 | avail = max; |
697 | memcpy(ptr, rpc->buf + rpc->pos, avail); | |
698 | rpc->pos += avail; | |
699 | return avail; | |
700 | } | |
701 | ||
6c81a990 | 702 | #ifndef NO_CURL_IOCTL |
5092d3ec | 703 | static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp) |
6c81a990 MS |
704 | { |
705 | struct rpc_state *rpc = clientp; | |
706 | ||
707 | switch (cmd) { | |
708 | case CURLIOCMD_NOP: | |
709 | return CURLIOE_OK; | |
710 | ||
711 | case CURLIOCMD_RESTARTREAD: | |
712 | if (rpc->initial_buffer) { | |
713 | rpc->pos = 0; | |
714 | return CURLIOE_OK; | |
715 | } | |
ed8b4132 | 716 | error(_("unable to rewind rpc post data - try increasing http.postBuffer")); |
6c81a990 MS |
717 | return CURLIOE_FAILRESTART; |
718 | ||
719 | default: | |
720 | return CURLIOE_UNKNOWNCMD; | |
721 | } | |
722 | } | |
723 | #endif | |
724 | ||
74b082ad DL |
725 | struct check_pktline_state { |
726 | char len_buf[4]; | |
727 | int len_filled; | |
728 | int remaining; | |
729 | }; | |
730 | ||
731 | static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size) | |
732 | { | |
733 | while (size) { | |
734 | if (!state->remaining) { | |
735 | int digits_remaining = 4 - state->len_filled; | |
736 | if (digits_remaining > size) | |
737 | digits_remaining = size; | |
738 | memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining); | |
739 | state->len_filled += digits_remaining; | |
740 | ptr += digits_remaining; | |
741 | size -= digits_remaining; | |
742 | ||
743 | if (state->len_filled == 4) { | |
744 | state->remaining = packet_length(state->len_buf); | |
745 | if (state->remaining < 0) { | |
746 | die(_("remote-curl: bad line length character: %.4s"), state->len_buf); | |
b0df0c16 DL |
747 | } else if (state->remaining == 2) { |
748 | die(_("remote-curl: unexpected response end packet")); | |
74b082ad DL |
749 | } else if (state->remaining < 4) { |
750 | state->remaining = 0; | |
751 | } else { | |
752 | state->remaining -= 4; | |
753 | } | |
754 | state->len_filled = 0; | |
755 | } | |
756 | } | |
757 | ||
758 | if (state->remaining) { | |
759 | int remaining = state->remaining; | |
760 | if (remaining > size) | |
761 | remaining = size; | |
762 | ptr += remaining; | |
763 | size -= remaining; | |
764 | state->remaining -= remaining; | |
765 | } | |
766 | } | |
767 | } | |
768 | ||
cf2fb92b MS |
769 | struct rpc_in_data { |
770 | struct rpc_state *rpc; | |
b79bdd8c | 771 | struct active_request_slot *slot; |
74b082ad DL |
772 | int check_pktline; |
773 | struct check_pktline_state pktline_state; | |
cf2fb92b MS |
774 | }; |
775 | ||
776 | /* | |
777 | * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed | |
778 | * from ptr. | |
779 | */ | |
a04ff3ec | 780 | static size_t rpc_in(char *ptr, size_t eltsize, |
de1a2fdd SP |
781 | size_t nmemb, void *buffer_) |
782 | { | |
783 | size_t size = eltsize * nmemb; | |
cf2fb92b | 784 | struct rpc_in_data *data = buffer_; |
b79bdd8c MS |
785 | long response_code; |
786 | ||
787 | if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE, | |
788 | &response_code) != CURLE_OK) | |
789 | return size; | |
790 | if (response_code >= 300) | |
791 | return size; | |
296b847c | 792 | if (size) |
cf2fb92b | 793 | data->rpc->any_written = 1; |
74b082ad DL |
794 | if (data->check_pktline) |
795 | check_pktline(&data->pktline_state, ptr, size); | |
cf2fb92b | 796 | write_or_die(data->rpc->in, ptr, size); |
de1a2fdd SP |
797 | return size; |
798 | } | |
799 | ||
3a347ed7 JK |
800 | static int run_slot(struct active_request_slot *slot, |
801 | struct slot_results *results) | |
206b099d | 802 | { |
b81401c1 | 803 | int err; |
3a347ed7 | 804 | struct slot_results results_buf; |
206b099d | 805 | |
3a347ed7 JK |
806 | if (!results) |
807 | results = &results_buf; | |
808 | ||
beed336c | 809 | err = run_one_slot(slot, results); |
206b099d | 810 | |
b81401c1 | 811 | if (err != HTTP_OK && err != HTTP_REAUTH) { |
00540458 SP |
812 | struct strbuf msg = STRBUF_INIT; |
813 | if (results->http_code && results->http_code != 200) | |
814 | strbuf_addf(&msg, "HTTP %ld", results->http_code); | |
815 | if (results->curl_result != CURLE_OK) { | |
816 | if (msg.len) | |
817 | strbuf_addch(&msg, ' '); | |
818 | strbuf_addf(&msg, "curl %d", results->curl_result); | |
819 | if (curl_errorstr[0]) { | |
820 | strbuf_addch(&msg, ' '); | |
821 | strbuf_addstr(&msg, curl_errorstr); | |
822 | } | |
823 | } | |
ed8b4132 | 824 | error(_("RPC failed; %s"), msg.buf); |
00540458 | 825 | strbuf_release(&msg); |
206b099d SP |
826 | } |
827 | ||
828 | return err; | |
829 | } | |
830 | ||
3a347ed7 | 831 | static int probe_rpc(struct rpc_state *rpc, struct slot_results *results) |
206b099d SP |
832 | { |
833 | struct active_request_slot *slot; | |
8cb01e2f | 834 | struct curl_slist *headers = http_copy_default_headers(); |
206b099d SP |
835 | struct strbuf buf = STRBUF_INIT; |
836 | int err; | |
837 | ||
838 | slot = get_active_slot(); | |
839 | ||
840 | headers = curl_slist_append(headers, rpc->hdr_content_type); | |
841 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
842 | ||
843 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); | |
844 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); | |
845 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); | |
aa90b969 | 846 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); |
206b099d SP |
847 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); |
848 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4); | |
849 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
850 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); | |
851 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf); | |
852 | ||
3a347ed7 | 853 | err = run_slot(slot, results); |
206b099d SP |
854 | |
855 | curl_slist_free_all(headers); | |
856 | strbuf_release(&buf); | |
857 | return err; | |
858 | } | |
859 | ||
3b335762 NTND |
860 | static curl_off_t xcurl_off_t(size_t len) |
861 | { | |
cb8010bb TB |
862 | uintmax_t size = len; |
863 | if (size > maximum_signed_value_of_type(curl_off_t)) | |
ed8b4132 | 864 | die(_("cannot handle pushes this big")); |
cb8010bb | 865 | return (curl_off_t)size; |
37ee680d DT |
866 | } |
867 | ||
a97d0079 JT |
868 | /* |
869 | * If flush_received is true, do not attempt to read any more; just use what's | |
870 | * in rpc->buf. | |
871 | */ | |
74b082ad | 872 | static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received) |
de1a2fdd SP |
873 | { |
874 | struct active_request_slot *slot; | |
8cb01e2f | 875 | struct curl_slist *headers = http_copy_default_headers(); |
b8538603 SP |
876 | int use_gzip = rpc->gzip_request; |
877 | char *gzip_body = NULL; | |
37711549 | 878 | size_t gzip_size = 0; |
206b099d | 879 | int err, large_request = 0; |
c80d96ca | 880 | int needs_100_continue = 0; |
cf2fb92b | 881 | struct rpc_in_data rpc_in_data; |
de1a2fdd SP |
882 | |
883 | /* Try to load the entire request, if we can fit it into the | |
884 | * allocated buffer space we can use HTTP/1.0 and avoid the | |
885 | * chunked encoding mess. | |
886 | */ | |
a97d0079 JT |
887 | if (!flush_received) { |
888 | while (1) { | |
889 | size_t n; | |
890 | enum packet_read_status status; | |
891 | ||
892 | if (!rpc_read_from_out(rpc, 0, &n, &status)) { | |
893 | large_request = 1; | |
894 | use_gzip = 0; | |
895 | break; | |
896 | } | |
897 | if (status == PACKET_READ_FLUSH) | |
898 | break; | |
de1a2fdd | 899 | } |
de1a2fdd SP |
900 | } |
901 | ||
206b099d | 902 | if (large_request) { |
c80d96ca | 903 | struct slot_results results; |
904 | ||
b81401c1 | 905 | do { |
c80d96ca | 906 | err = probe_rpc(rpc, &results); |
2501aff8 JK |
907 | if (err == HTTP_REAUTH) |
908 | credential_fill(&http_auth); | |
b81401c1 JK |
909 | } while (err == HTTP_REAUTH); |
910 | if (err != HTTP_OK) | |
911 | return -1; | |
c80d96ca | 912 | |
913 | if (results.auth_avail & CURLAUTH_GSSNEGOTIATE) | |
914 | needs_100_continue = 1; | |
206b099d SP |
915 | } |
916 | ||
abf8df86 JK |
917 | headers = curl_slist_append(headers, rpc->hdr_content_type); |
918 | headers = curl_slist_append(headers, rpc->hdr_accept); | |
c80d96ca | 919 | headers = curl_slist_append(headers, needs_100_continue ? |
920 | "Expect: 100-continue" : "Expect:"); | |
abf8df86 | 921 | |
884e586f BW |
922 | /* Add the extra Git-Protocol header */ |
923 | if (rpc->protocol_header) | |
924 | headers = curl_slist_append(headers, rpc->protocol_header); | |
925 | ||
abf8df86 | 926 | retry: |
de1a2fdd | 927 | slot = get_active_slot(); |
de1a2fdd | 928 | |
de1a2fdd | 929 | curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0); |
d21f9794 | 930 | curl_easy_setopt(slot->curl, CURLOPT_POST, 1); |
de1a2fdd | 931 | curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); |
1a53e692 | 932 | curl_easy_setopt(slot->curl, CURLOPT_ENCODING, ""); |
de1a2fdd | 933 | |
de1a2fdd SP |
934 | if (large_request) { |
935 | /* The request body is large and the size cannot be predicted. | |
936 | * We must use chunked encoding to send it. | |
937 | */ | |
de1a2fdd | 938 | headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); |
6c81a990 | 939 | rpc->initial_buffer = 1; |
de1a2fdd SP |
940 | curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); |
941 | curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); | |
6c81a990 MS |
942 | #ifndef NO_CURL_IOCTL |
943 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl); | |
944 | curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc); | |
945 | #endif | |
de1a2fdd SP |
946 | if (options.verbosity > 1) { |
947 | fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); | |
948 | fflush(stderr); | |
949 | } | |
950 | ||
2e736fd5 JK |
951 | } else if (gzip_body) { |
952 | /* | |
953 | * If we are looping to retry authentication, then the previous | |
954 | * run will have set up the headers and gzip buffer already, | |
955 | * and we just need to send it. | |
956 | */ | |
957 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
37ee680d | 958 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
2e736fd5 | 959 | |
b8538603 SP |
960 | } else if (use_gzip && 1024 < rpc->len) { |
961 | /* The client backend isn't giving us compressed data so | |
a8132410 | 962 | * we can try to deflate it ourselves, this may save on |
b8538603 SP |
963 | * the transfer time. |
964 | */ | |
ef49a7a0 | 965 | git_zstream stream; |
b8538603 SP |
966 | int ret; |
967 | ||
55bb5c91 | 968 | git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); |
df126e10 JK |
969 | gzip_size = git_deflate_bound(&stream, rpc->len); |
970 | gzip_body = xmalloc(gzip_size); | |
b8538603 SP |
971 | |
972 | stream.next_in = (unsigned char *)rpc->buf; | |
973 | stream.avail_in = rpc->len; | |
974 | stream.next_out = (unsigned char *)gzip_body; | |
df126e10 | 975 | stream.avail_out = gzip_size; |
b8538603 | 976 | |
55bb5c91 | 977 | ret = git_deflate(&stream, Z_FINISH); |
b8538603 | 978 | if (ret != Z_STREAM_END) |
ed8b4132 | 979 | die(_("cannot deflate request; zlib deflate error %d"), ret); |
b8538603 | 980 | |
55bb5c91 | 981 | ret = git_deflate_end_gently(&stream); |
b8538603 | 982 | if (ret != Z_OK) |
ed8b4132 | 983 | die(_("cannot deflate request; zlib end error %d"), ret); |
b8538603 | 984 | |
df126e10 | 985 | gzip_size = stream.total_out; |
b8538603 SP |
986 | |
987 | headers = curl_slist_append(headers, "Content-Encoding: gzip"); | |
988 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); | |
37ee680d | 989 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size)); |
b8538603 SP |
990 | |
991 | if (options.verbosity > 1) { | |
992 | fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", | |
993 | rpc->service_name, | |
df126e10 | 994 | (unsigned long)rpc->len, (unsigned long)gzip_size); |
b8538603 SP |
995 | fflush(stderr); |
996 | } | |
de1a2fdd SP |
997 | } else { |
998 | /* We know the complete request size in advance, use the | |
999 | * more normal Content-Length approach. | |
1000 | */ | |
1001 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); | |
37ee680d | 1002 | curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len)); |
de1a2fdd SP |
1003 | if (options.verbosity > 1) { |
1004 | fprintf(stderr, "POST %s (%lu bytes)\n", | |
1005 | rpc->service_name, (unsigned long)rpc->len); | |
1006 | fflush(stderr); | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); | |
1011 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); | |
cf2fb92b | 1012 | rpc_in_data.rpc = rpc; |
b79bdd8c | 1013 | rpc_in_data.slot = slot; |
74b082ad DL |
1014 | rpc_in_data.check_pktline = stateless_connect; |
1015 | memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state)); | |
cf2fb92b | 1016 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data); |
b79bdd8c | 1017 | curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0); |
de1a2fdd | 1018 | |
296b847c DT |
1019 | |
1020 | rpc->any_written = 0; | |
3a347ed7 | 1021 | err = run_slot(slot, NULL); |
2501aff8 JK |
1022 | if (err == HTTP_REAUTH && !large_request) { |
1023 | credential_fill(&http_auth); | |
abf8df86 | 1024 | goto retry; |
2501aff8 | 1025 | } |
b81401c1 JK |
1026 | if (err != HTTP_OK) |
1027 | err = -1; | |
de1a2fdd | 1028 | |
296b847c DT |
1029 | if (!rpc->any_written) |
1030 | err = -1; | |
1031 | ||
74b082ad DL |
1032 | if (rpc_in_data.pktline_state.len_filled) |
1033 | err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled); | |
1034 | if (rpc_in_data.pktline_state.remaining) | |
1035 | err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining); | |
1036 | ||
b0df0c16 DL |
1037 | if (stateless_connect) |
1038 | packet_response_end(rpc->in); | |
1039 | ||
de1a2fdd | 1040 | curl_slist_free_all(headers); |
b8538603 | 1041 | free(gzip_body); |
de1a2fdd SP |
1042 | return err; |
1043 | } | |
1044 | ||
7d50d34f | 1045 | static int rpc_service(struct rpc_state *rpc, struct discovery *heads, |
b3590309 JT |
1046 | const char **client_argv, const struct strbuf *preamble, |
1047 | struct strbuf *rpc_result) | |
de1a2fdd SP |
1048 | { |
1049 | const char *svc = rpc->service_name; | |
1050 | struct strbuf buf = STRBUF_INIT; | |
d3180279 | 1051 | struct child_process client = CHILD_PROCESS_INIT; |
de1a2fdd SP |
1052 | int err = 0; |
1053 | ||
de1a2fdd SP |
1054 | client.in = -1; |
1055 | client.out = -1; | |
1056 | client.git_cmd = 1; | |
7d50d34f | 1057 | client.argv = client_argv; |
de1a2fdd SP |
1058 | if (start_command(&client)) |
1059 | exit(1); | |
5d916693 | 1060 | write_or_die(client.in, preamble->buf, preamble->len); |
de1a2fdd SP |
1061 | if (heads) |
1062 | write_or_die(client.in, heads->buf, heads->len); | |
1063 | ||
1064 | rpc->alloc = http_post_buffer; | |
1065 | rpc->buf = xmalloc(rpc->alloc); | |
1066 | rpc->in = client.in; | |
1067 | rpc->out = client.out; | |
de1a2fdd | 1068 | |
b227bbc4 | 1069 | strbuf_addf(&buf, "%s%s", url.buf, svc); |
de1a2fdd SP |
1070 | rpc->service_url = strbuf_detach(&buf, NULL); |
1071 | ||
1072 | strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); | |
1073 | rpc->hdr_content_type = strbuf_detach(&buf, NULL); | |
1074 | ||
8efa5f62 | 1075 | strbuf_addf(&buf, "Accept: application/x-%s-result", svc); |
de1a2fdd SP |
1076 | rpc->hdr_accept = strbuf_detach(&buf, NULL); |
1077 | ||
884e586f BW |
1078 | if (get_protocol_http_header(heads->version, &buf)) |
1079 | rpc->protocol_header = strbuf_detach(&buf, NULL); | |
1080 | else | |
1081 | rpc->protocol_header = NULL; | |
1082 | ||
de1a2fdd | 1083 | while (!err) { |
4981fe75 | 1084 | int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0); |
de1a2fdd SP |
1085 | if (!n) |
1086 | break; | |
1087 | rpc->pos = 0; | |
1088 | rpc->len = n; | |
74b082ad | 1089 | err |= post_rpc(rpc, 0, 0); |
de1a2fdd | 1090 | } |
de1a2fdd SP |
1091 | |
1092 | close(client.in); | |
de1a2fdd | 1093 | client.in = -1; |
6cdf0223 | 1094 | if (!err) { |
b3590309 | 1095 | strbuf_read(rpc_result, client.out, 0); |
6cdf0223 SP |
1096 | } else { |
1097 | char buf[4096]; | |
1098 | for (;;) | |
1099 | if (xread(client.out, buf, sizeof(buf)) <= 0) | |
1100 | break; | |
1101 | } | |
b4ee10f6 SP |
1102 | |
1103 | close(client.out); | |
de1a2fdd SP |
1104 | client.out = -1; |
1105 | ||
1106 | err |= finish_command(&client); | |
1107 | free(rpc->service_url); | |
1108 | free(rpc->hdr_content_type); | |
1109 | free(rpc->hdr_accept); | |
884e586f | 1110 | free(rpc->protocol_header); |
de1a2fdd SP |
1111 | free(rpc->buf); |
1112 | strbuf_release(&buf); | |
1113 | return err; | |
1114 | } | |
1115 | ||
292ce46b SP |
1116 | static int fetch_dumb(int nr_heads, struct ref **to_fetch) |
1117 | { | |
26e1e0b2 | 1118 | struct walker *walker; |
b32fa95f | 1119 | char **targets; |
292ce46b SP |
1120 | int ret, i; |
1121 | ||
b32fa95f | 1122 | ALLOC_ARRAY(targets, nr_heads); |
508ea882 | 1123 | if (options.depth || options.deepen_since) |
ed8b4132 | 1124 | die(_("dumb http transport does not support shallow capabilities")); |
292ce46b | 1125 | for (i = 0; i < nr_heads; i++) |
f4e54d02 | 1126 | targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid)); |
292ce46b | 1127 | |
b227bbc4 | 1128 | walker = get_http_walker(url.buf); |
ef08ef9e | 1129 | walker->get_verbosely = options.verbosity >= 3; |
7655b411 | 1130 | walker->get_progress = options.progress; |
292ce46b SP |
1131 | walker->get_recover = 0; |
1132 | ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); | |
26e1e0b2 | 1133 | walker_free(walker); |
292ce46b SP |
1134 | |
1135 | for (i = 0; i < nr_heads; i++) | |
1136 | free(targets[i]); | |
1137 | free(targets); | |
1138 | ||
ed8b4132 | 1139 | return ret ? error(_("fetch failed.")) : 0; |
292ce46b SP |
1140 | } |
1141 | ||
249b2004 SP |
1142 | static int fetch_git(struct discovery *heads, |
1143 | int nr_heads, struct ref **to_fetch) | |
1144 | { | |
1145 | struct rpc_state rpc; | |
8150749d | 1146 | struct strbuf preamble = STRBUF_INIT; |
b5f62ebe | 1147 | int i, err; |
c972bf4c | 1148 | struct strvec args = STRVEC_INIT; |
b3590309 | 1149 | struct strbuf rpc_result = STRBUF_INIT; |
b5f62ebe | 1150 | |
c972bf4c | 1151 | strvec_pushl(&args, "fetch-pack", "--stateless-rpc", |
f6d8942b | 1152 | "--stdin", "--lock-pack", NULL); |
249b2004 | 1153 | if (options.followtags) |
c972bf4c | 1154 | strvec_push(&args, "--include-tag"); |
249b2004 | 1155 | if (options.thin) |
c972bf4c | 1156 | strvec_push(&args, "--thin"); |
b5f62ebe | 1157 | if (options.verbosity >= 3) |
c972bf4c | 1158 | strvec_pushl(&args, "-v", "-v", NULL); |
9ba38048 | 1159 | if (options.check_self_contained_and_connected) |
c972bf4c | 1160 | strvec_push(&args, "--check-self-contained-and-connected"); |
16094885 | 1161 | if (options.cloning) |
c972bf4c | 1162 | strvec_push(&args, "--cloning"); |
16094885 | 1163 | if (options.update_shallow) |
c972bf4c | 1164 | strvec_push(&args, "--update-shallow"); |
249b2004 | 1165 | if (!options.progress) |
c972bf4c | 1166 | strvec_push(&args, "--no-progress"); |
b5f62ebe | 1167 | if (options.depth) |
c972bf4c | 1168 | strvec_pushf(&args, "--depth=%lu", options.depth); |
508ea882 | 1169 | if (options.deepen_since) |
c972bf4c | 1170 | strvec_pushf(&args, "--shallow-since=%s", options.deepen_since); |
a45a2600 | 1171 | for (i = 0; i < options.deepen_not.nr; i++) |
c972bf4c | 1172 | strvec_pushf(&args, "--shallow-exclude=%s", |
f6d8942b | 1173 | options.deepen_not.items[i].string); |
cccf74e2 | 1174 | if (options.deepen_relative && options.depth) |
c972bf4c | 1175 | strvec_push(&args, "--deepen-relative"); |
88e2f9ed | 1176 | if (options.from_promisor) |
c972bf4c | 1177 | strvec_push(&args, "--from-promisor"); |
88e2f9ed | 1178 | if (options.no_dependents) |
c972bf4c | 1179 | strvec_push(&args, "--no-dependents"); |
acb0c572 | 1180 | if (options.filter) |
c972bf4c JK |
1181 | strvec_pushf(&args, "--filter=%s", options.filter); |
1182 | strvec_push(&args, url.buf); | |
8150749d | 1183 | |
249b2004 SP |
1184 | for (i = 0; i < nr_heads; i++) { |
1185 | struct ref *ref = to_fetch[i]; | |
94ee8e2c | 1186 | if (!*ref->name) |
ed8b4132 | 1187 | die(_("cannot fetch by sha1 over smart http")); |
58f2ed05 | 1188 | packet_buf_write(&preamble, "%s %s\n", |
f4e54d02 | 1189 | oid_to_hex(&ref->old_oid), ref->name); |
249b2004 | 1190 | } |
8150749d | 1191 | packet_buf_flush(&preamble); |
249b2004 SP |
1192 | |
1193 | memset(&rpc, 0, sizeof(rpc)); | |
1194 | rpc.service_name = "git-upload-pack", | |
b8538603 | 1195 | rpc.gzip_request = 1; |
249b2004 | 1196 | |
d70a9eb6 | 1197 | err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result); |
b3590309 JT |
1198 | if (rpc_result.len) |
1199 | write_or_die(1, rpc_result.buf, rpc_result.len); | |
1200 | strbuf_release(&rpc_result); | |
8150749d | 1201 | strbuf_release(&preamble); |
c972bf4c | 1202 | strvec_clear(&args); |
249b2004 SP |
1203 | return err; |
1204 | } | |
1205 | ||
1206 | static int fetch(int nr_heads, struct ref **to_fetch) | |
1207 | { | |
2a455202 | 1208 | struct discovery *d = discover_refs("git-upload-pack", 0); |
249b2004 SP |
1209 | if (d->proto_git) |
1210 | return fetch_git(d, nr_heads, to_fetch); | |
1211 | else | |
1212 | return fetch_dumb(nr_heads, to_fetch); | |
1213 | } | |
1214 | ||
292ce46b SP |
1215 | static void parse_fetch(struct strbuf *buf) |
1216 | { | |
1217 | struct ref **to_fetch = NULL; | |
1218 | struct ref *list_head = NULL; | |
1219 | struct ref **list = &list_head; | |
1220 | int alloc_heads = 0, nr_heads = 0; | |
1221 | ||
1222 | do { | |
95b567c7 JK |
1223 | const char *p; |
1224 | if (skip_prefix(buf->buf, "fetch ", &p)) { | |
1225 | const char *name; | |
292ce46b | 1226 | struct ref *ref; |
8338c911 | 1227 | struct object_id old_oid; |
9c9492e8 | 1228 | const char *q; |
292ce46b | 1229 | |
9c9492e8 | 1230 | if (parse_oid_hex(p, &old_oid, &q)) |
8a1569d6 | 1231 | die(_("protocol error: expected sha/ref, got '%s'"), p); |
9c9492e8 | 1232 | if (*q == ' ') |
1233 | name = q + 1; | |
1234 | else if (!*q) | |
292ce46b SP |
1235 | name = ""; |
1236 | else | |
8a1569d6 | 1237 | die(_("protocol error: expected sha/ref, got '%s'"), p); |
292ce46b SP |
1238 | |
1239 | ref = alloc_ref(name); | |
8338c911 | 1240 | oidcpy(&ref->old_oid, &old_oid); |
292ce46b SP |
1241 | |
1242 | *list = ref; | |
1243 | list = &ref->next; | |
1244 | ||
1245 | ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); | |
1246 | to_fetch[nr_heads++] = ref; | |
1247 | } | |
1248 | else | |
ed8b4132 | 1249 | die(_("http transport does not support %s"), buf->buf); |
292ce46b SP |
1250 | |
1251 | strbuf_reset(buf); | |
8f309aeb | 1252 | if (strbuf_getline_lf(buf, stdin) == EOF) |
292ce46b SP |
1253 | return; |
1254 | if (!*buf->buf) | |
1255 | break; | |
1256 | } while (1); | |
1257 | ||
249b2004 | 1258 | if (fetch(nr_heads, to_fetch)) |
292ce46b SP |
1259 | exit(128); /* error already reported */ |
1260 | free_refs(list_head); | |
1261 | free(to_fetch); | |
1262 | ||
1263 | printf("\n"); | |
1264 | fflush(stdout); | |
1265 | strbuf_reset(buf); | |
1266 | } | |
1267 | ||
062a309d | 1268 | static int push_dav(int nr_spec, const char **specs) |
ae4efe19 | 1269 | { |
850d2fec JK |
1270 | struct child_process child = CHILD_PROCESS_INIT; |
1271 | size_t i; | |
ae4efe19 | 1272 | |
850d2fec | 1273 | child.git_cmd = 1; |
c972bf4c JK |
1274 | strvec_push(&child.args, "http-push"); |
1275 | strvec_push(&child.args, "--helper-status"); | |
ae4efe19 | 1276 | if (options.dry_run) |
c972bf4c | 1277 | strvec_push(&child.args, "--dry-run"); |
ae4efe19 | 1278 | if (options.verbosity > 1) |
c972bf4c JK |
1279 | strvec_push(&child.args, "--verbose"); |
1280 | strvec_push(&child.args, url.buf); | |
ae4efe19 | 1281 | for (i = 0; i < nr_spec; i++) |
c972bf4c | 1282 | strvec_push(&child.args, specs[i]); |
ae4efe19 | 1283 | |
850d2fec | 1284 | if (run_command(&child)) |
ed8b4132 | 1285 | die(_("git-http-push failed")); |
ae4efe19 SP |
1286 | return 0; |
1287 | } | |
1288 | ||
062a309d | 1289 | static int push_git(struct discovery *heads, int nr_spec, const char **specs) |
de1a2fdd SP |
1290 | { |
1291 | struct rpc_state rpc; | |
222b1212 | 1292 | int i, err; |
c972bf4c | 1293 | struct strvec args; |
05c1eb10 | 1294 | struct string_list_item *cas_option; |
26be19ba | 1295 | struct strbuf preamble = STRBUF_INIT; |
b3590309 | 1296 | struct strbuf rpc_result = STRBUF_INIT; |
222b1212 | 1297 | |
c972bf4c JK |
1298 | strvec_init(&args); |
1299 | strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", | |
f6d8942b | 1300 | NULL); |
de1a2fdd | 1301 | |
de1a2fdd | 1302 | if (options.thin) |
c972bf4c | 1303 | strvec_push(&args, "--thin"); |
de1a2fdd | 1304 | if (options.dry_run) |
c972bf4c | 1305 | strvec_push(&args, "--dry-run"); |
30261094 | 1306 | if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS) |
c972bf4c | 1307 | strvec_push(&args, "--signed=yes"); |
30261094 | 1308 | else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) |
c972bf4c | 1309 | strvec_push(&args, "--signed=if-asked"); |
6f119424 | 1310 | if (options.atomic) |
c972bf4c | 1311 | strvec_push(&args, "--atomic"); |
c207e34f | 1312 | if (options.verbosity == 0) |
c972bf4c | 1313 | strvec_push(&args, "--quiet"); |
c207e34f | 1314 | else if (options.verbosity > 1) |
c972bf4c | 1315 | strvec_push(&args, "--verbose"); |
511155db | 1316 | for (i = 0; i < options.push_options.nr; i++) |
c972bf4c | 1317 | strvec_pushf(&args, "--push-option=%s", |
f6d8942b | 1318 | options.push_options.items[i].string); |
c972bf4c | 1319 | strvec_push(&args, options.progress ? "--progress" : "--no-progress"); |
05c1eb10 | 1320 | for_each_string_list_item(cas_option, &cas_options) |
c972bf4c JK |
1321 | strvec_push(&args, cas_option->string); |
1322 | strvec_push(&args, url.buf); | |
26be19ba | 1323 | |
c972bf4c | 1324 | strvec_push(&args, "--stdin"); |
de1a2fdd | 1325 | for (i = 0; i < nr_spec; i++) |
26be19ba JK |
1326 | packet_buf_write(&preamble, "%s\n", specs[i]); |
1327 | packet_buf_flush(&preamble); | |
de1a2fdd SP |
1328 | |
1329 | memset(&rpc, 0, sizeof(rpc)); | |
1330 | rpc.service_name = "git-receive-pack", | |
de1a2fdd | 1331 | |
d70a9eb6 | 1332 | err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result); |
b3590309 JT |
1333 | if (rpc_result.len) |
1334 | write_or_die(1, rpc_result.buf, rpc_result.len); | |
1335 | strbuf_release(&rpc_result); | |
26be19ba | 1336 | strbuf_release(&preamble); |
c972bf4c | 1337 | strvec_clear(&args); |
de1a2fdd SP |
1338 | return err; |
1339 | } | |
1340 | ||
062a309d | 1341 | static int push(int nr_spec, const char **specs) |
de1a2fdd | 1342 | { |
2a455202 | 1343 | struct discovery *heads = discover_refs("git-receive-pack", 1); |
de1a2fdd SP |
1344 | int ret; |
1345 | ||
1346 | if (heads->proto_git) | |
1347 | ret = push_git(heads, nr_spec, specs); | |
1348 | else | |
1349 | ret = push_dav(nr_spec, specs); | |
1350 | free_discovery(heads); | |
1351 | return ret; | |
1352 | } | |
1353 | ||
ae4efe19 SP |
1354 | static void parse_push(struct strbuf *buf) |
1355 | { | |
c972bf4c | 1356 | struct strvec specs = STRVEC_INIT; |
062a309d | 1357 | int ret; |
ae4efe19 SP |
1358 | |
1359 | do { | |
145136a9 JH |
1360 | const char *arg; |
1361 | if (skip_prefix(buf->buf, "push ", &arg)) | |
c972bf4c | 1362 | strvec_push(&specs, arg); |
ae4efe19 | 1363 | else |
ed8b4132 | 1364 | die(_("http transport does not support %s"), buf->buf); |
ae4efe19 SP |
1365 | |
1366 | strbuf_reset(buf); | |
8f309aeb | 1367 | if (strbuf_getline_lf(buf, stdin) == EOF) |
dc4cd767 | 1368 | goto free_specs; |
ae4efe19 SP |
1369 | if (!*buf->buf) |
1370 | break; | |
1371 | } while (1); | |
1372 | ||
d70a9eb6 | 1373 | ret = push(specs.nr, specs.v); |
ae4efe19 SP |
1374 | printf("\n"); |
1375 | fflush(stdout); | |
dc4cd767 | 1376 | |
5238cbf6 SP |
1377 | if (ret) |
1378 | exit(128); /* error already reported */ | |
1379 | ||
04cc91ab | 1380 | free_specs: |
c972bf4c | 1381 | strvec_clear(&specs); |
ae4efe19 SP |
1382 | } |
1383 | ||
0f1dc53f BW |
1384 | static int stateless_connect(const char *service_name) |
1385 | { | |
1386 | struct discovery *discover; | |
a97d0079 JT |
1387 | struct rpc_state rpc; |
1388 | struct strbuf buf = STRBUF_INIT; | |
0f1dc53f BW |
1389 | |
1390 | /* | |
1391 | * Run the info/refs request and see if the server supports protocol | |
1392 | * v2. If and only if the server supports v2 can we successfully | |
1393 | * establish a stateless connection, otherwise we need to tell the | |
1394 | * client to fallback to using other transport helper functions to | |
1395 | * complete their request. | |
1396 | */ | |
1397 | discover = discover_refs(service_name, 0); | |
1398 | if (discover->version != protocol_v2) { | |
1399 | printf("fallback\n"); | |
1400 | fflush(stdout); | |
1401 | return -1; | |
1402 | } else { | |
1403 | /* Stateless Connection established */ | |
1404 | printf("\n"); | |
1405 | fflush(stdout); | |
1406 | } | |
1407 | ||
a97d0079 JT |
1408 | rpc.service_name = service_name; |
1409 | rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name); | |
1410 | rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name); | |
1411 | rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name); | |
1412 | if (get_protocol_http_header(discover->version, &buf)) { | |
1413 | rpc.protocol_header = strbuf_detach(&buf, NULL); | |
1414 | } else { | |
1415 | rpc.protocol_header = NULL; | |
1416 | strbuf_release(&buf); | |
1417 | } | |
1418 | rpc.buf = xmalloc(http_post_buffer); | |
1419 | rpc.alloc = http_post_buffer; | |
1420 | rpc.len = 0; | |
1421 | rpc.pos = 0; | |
1422 | rpc.in = 1; | |
1423 | rpc.out = 0; | |
1424 | rpc.any_written = 0; | |
1425 | rpc.gzip_request = 1; | |
1426 | rpc.initial_buffer = 0; | |
1427 | rpc.write_line_lengths = 1; | |
1428 | rpc.flush_read_but_not_sent = 0; | |
0f1dc53f BW |
1429 | |
1430 | /* | |
1431 | * Dump the capability listing that we got from the server earlier | |
1432 | * during the info/refs request. | |
1433 | */ | |
a97d0079 JT |
1434 | write_or_die(rpc.in, discover->buf, discover->len); |
1435 | ||
1436 | /* Until we see EOF keep sending POSTs */ | |
1437 | while (1) { | |
1438 | size_t avail; | |
1439 | enum packet_read_status status; | |
0f1dc53f | 1440 | |
a97d0079 JT |
1441 | if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail, |
1442 | &status)) | |
1443 | BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX"); | |
1444 | if (status == PACKET_READ_EOF) | |
1445 | break; | |
74b082ad | 1446 | if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH)) |
0f1dc53f BW |
1447 | /* We would have an err here */ |
1448 | break; | |
a97d0079 JT |
1449 | /* Reset the buffer for next request */ |
1450 | rpc.len = 0; | |
0f1dc53f BW |
1451 | } |
1452 | ||
a97d0079 JT |
1453 | free(rpc.service_url); |
1454 | free(rpc.hdr_content_type); | |
1455 | free(rpc.hdr_accept); | |
1456 | free(rpc.protocol_header); | |
1457 | free(rpc.buf); | |
1458 | strbuf_release(&buf); | |
1459 | ||
0f1dc53f BW |
1460 | return 0; |
1461 | } | |
1462 | ||
3f2e2297 | 1463 | int cmd_main(int argc, const char **argv) |
a2d725b7 | 1464 | { |
a2d725b7 | 1465 | struct strbuf buf = STRBUF_INIT; |
a45d3d7e | 1466 | int nongit; |
a2d725b7 | 1467 | |
a45d3d7e | 1468 | setup_git_directory_gently(&nongit); |
a2d725b7 | 1469 | if (argc < 2) { |
ed8b4132 | 1470 | error(_("remote-curl: usage: git remote-curl <remote> [<url>]")); |
a2d725b7 DB |
1471 | return 1; |
1472 | } | |
1473 | ||
ef08ef9e SP |
1474 | options.verbosity = 1; |
1475 | options.progress = !!isatty(2); | |
de1a2fdd | 1476 | options.thin = 1; |
a45a2600 | 1477 | string_list_init(&options.deepen_not, 1); |
511155db | 1478 | string_list_init(&options.push_options, 1); |
ef08ef9e | 1479 | |
ee4512ed JH |
1480 | /* |
1481 | * Just report "remote-curl" here (folding all the various aliases | |
1482 | * ("git-remote-http", "git-remote-https", and etc.) here since they | |
1483 | * are all just copies of the same actual executable. | |
1484 | */ | |
1485 | trace2_cmd_name("remote-curl"); | |
1486 | ||
a2d725b7 DB |
1487 | remote = remote_get(argv[1]); |
1488 | ||
1489 | if (argc > 2) { | |
b227bbc4 | 1490 | end_url_with_slash(&url, argv[2]); |
a2d725b7 | 1491 | } else { |
b227bbc4 | 1492 | end_url_with_slash(&url, remote->url[0]); |
a2d725b7 DB |
1493 | } |
1494 | ||
b227bbc4 | 1495 | http_init(remote, url.buf, 0); |
888692b7 | 1496 | |
a2d725b7 | 1497 | do { |
95b567c7 JK |
1498 | const char *arg; |
1499 | ||
8f309aeb | 1500 | if (strbuf_getline_lf(&buf, stdin) == EOF) { |
1843f0ce | 1501 | if (ferror(stdin)) |
ed8b4132 | 1502 | error(_("remote-curl: error reading command stream from git")); |
1843f0ce SR |
1503 | return 1; |
1504 | } | |
1505 | if (buf.len == 0) | |
a2d725b7 | 1506 | break; |
59556548 | 1507 | if (starts_with(buf.buf, "fetch ")) { |
a45d3d7e | 1508 | if (nongit) |
ed8b4132 | 1509 | die(_("remote-curl: fetch attempted without a local repo")); |
292ce46b SP |
1510 | parse_fetch(&buf); |
1511 | ||
59556548 | 1512 | } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { |
97cc7bc4 SP |
1513 | int for_push = !!strstr(buf.buf + 4, "for-push"); |
1514 | output_refs(get_refs(for_push)); | |
ae4efe19 | 1515 | |
59556548 | 1516 | } else if (starts_with(buf.buf, "push ")) { |
ae4efe19 SP |
1517 | parse_push(&buf); |
1518 | ||
95b567c7 JK |
1519 | } else if (skip_prefix(buf.buf, "option ", &arg)) { |
1520 | char *value = strchr(arg, ' '); | |
ef08ef9e SP |
1521 | int result; |
1522 | ||
1523 | if (value) | |
1524 | *value++ = '\0'; | |
1525 | else | |
1526 | value = "true"; | |
1527 | ||
95b567c7 | 1528 | result = set_option(arg, value); |
ef08ef9e SP |
1529 | if (!result) |
1530 | printf("ok\n"); | |
1531 | else if (result < 0) | |
1532 | printf("error invalid value\n"); | |
1533 | else | |
1534 | printf("unsupported\n"); | |
a2d725b7 | 1535 | fflush(stdout); |
ef08ef9e | 1536 | |
a2d725b7 | 1537 | } else if (!strcmp(buf.buf, "capabilities")) { |
0f1dc53f | 1538 | printf("stateless-connect\n"); |
a2d725b7 | 1539 | printf("fetch\n"); |
ef08ef9e | 1540 | printf("option\n"); |
ae4efe19 | 1541 | printf("push\n"); |
9ba38048 | 1542 | printf("check-connectivity\n"); |
7f605017 | 1543 | printf("object-format\n"); |
a2d725b7 DB |
1544 | printf("\n"); |
1545 | fflush(stdout); | |
0f1dc53f BW |
1546 | } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) { |
1547 | if (!stateless_connect(arg)) | |
1548 | break; | |
a2d725b7 | 1549 | } else { |
ed8b4132 | 1550 | error(_("remote-curl: unknown command '%s' from git"), buf.buf); |
a2d725b7 DB |
1551 | return 1; |
1552 | } | |
1553 | strbuf_reset(&buf); | |
1554 | } while (1); | |
888692b7 TRC |
1555 | |
1556 | http_cleanup(); | |
1557 | ||
a2d725b7 DB |
1558 | return 0; |
1559 | } |