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