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