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