]> git.ipfire.org Git - thirdparty/git.git/blame - remote-curl.c
commit-graph: add core.commitGraph setting
[thirdparty/git.git] / remote-curl.c
CommitLineData
a2d725b7 1#include "cache.h"
b2141fc1 2#include "config.h"
a2d725b7
DB
3#include "remote.h"
4#include "strbuf.h"
5#include "walker.h"
6#include "http.h"
d01a8e32 7#include "exec_cmd.h"
ae4efe19 8#include "run-command.h"
97cc7bc4 9#include "pkt-line.h"
05c1eb10 10#include "string-list.h"
de1a2fdd 11#include "sideband.h"
222b1212 12#include "argv-array.h"
2501aff8 13#include "credential.h"
16094885 14#include "sha1-array.h"
30261094 15#include "send-pack.h"
a2d725b7 16
37a8768f 17static struct remote *remote;
b227bbc4
JK
18/* always ends with a trailing slash */
19static struct strbuf url = STRBUF_INIT;
37a8768f 20
ef08ef9e
SP
21struct options {
22 int verbosity;
23 unsigned long depth;
508ea882 24 char *deepen_since;
a45a2600 25 struct string_list deepen_not;
511155db 26 struct string_list push_options;
acb0c572 27 char *filter;
ef08ef9e 28 unsigned progress : 1,
9ba38048 29 check_self_contained_and_connected : 1,
16094885
NTND
30 cloning : 1,
31 update_shallow : 1,
ae4efe19 32 followtags : 1,
de1a2fdd 33 dry_run : 1,
0ea47f9d 34 thin : 1,
30261094 35 /* One of the SEND_PACK_PUSH_CERT_* constants. */
cccf74e2 36 push_cert : 2,
88e2f9ed
JT
37 deepen_relative : 1,
38 from_promisor : 1,
39 no_dependents : 1;
ef08ef9e
SP
40};
41static struct options options;
05c1eb10 42static struct string_list cas_options = STRING_LIST_INIT_DUP;
ef08ef9e 43
ef08ef9e
SP
44static int set_option(const char *name, const char *value)
45{
46 if (!strcmp(name, "verbosity")) {
47 char *end;
48 int v = strtol(value, &end, 10);
49 if (value == end || *end)
50 return -1;
51 options.verbosity = v;
52 return 0;
53 }
54 else if (!strcmp(name, "progress")) {
55 if (!strcmp(value, "true"))
56 options.progress = 1;
57 else if (!strcmp(value, "false"))
58 options.progress = 0;
59 else
60 return -1;
249b2004 61 return 0;
ef08ef9e
SP
62 }
63 else if (!strcmp(name, "depth")) {
64 char *end;
65 unsigned long v = strtoul(value, &end, 10);
66 if (value == end || *end)
67 return -1;
68 options.depth = v;
249b2004 69 return 0;
ef08ef9e 70 }
508ea882
NTND
71 else if (!strcmp(name, "deepen-since")) {
72 options.deepen_since = xstrdup(value);
73 return 0;
74 }
a45a2600
NTND
75 else if (!strcmp(name, "deepen-not")) {
76 string_list_append(&options.deepen_not, value);
77 return 0;
78 }
cccf74e2
NTND
79 else if (!strcmp(name, "deepen-relative")) {
80 if (!strcmp(value, "true"))
81 options.deepen_relative = 1;
82 else if (!strcmp(value, "false"))
83 options.deepen_relative = 0;
84 else
85 return -1;
86 return 0;
87 }
ef08ef9e
SP
88 else if (!strcmp(name, "followtags")) {
89 if (!strcmp(value, "true"))
90 options.followtags = 1;
91 else if (!strcmp(value, "false"))
92 options.followtags = 0;
93 else
94 return -1;
249b2004 95 return 0;
ef08ef9e 96 }
ae4efe19
SP
97 else if (!strcmp(name, "dry-run")) {
98 if (!strcmp(value, "true"))
99 options.dry_run = 1;
100 else if (!strcmp(value, "false"))
101 options.dry_run = 0;
102 else
103 return -1;
104 return 0;
105 }
9ba38048
NTND
106 else if (!strcmp(name, "check-connectivity")) {
107 if (!strcmp(value, "true"))
108 options.check_self_contained_and_connected = 1;
109 else if (!strcmp(value, "false"))
110 options.check_self_contained_and_connected = 0;
111 else
112 return -1;
113 return 0;
114 }
05c1eb10
JH
115 else if (!strcmp(name, "cas")) {
116 struct strbuf val = STRBUF_INIT;
117 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
118 string_list_append(&cas_options, val.buf);
119 strbuf_release(&val);
120 return 0;
16094885
NTND
121 } else if (!strcmp(name, "cloning")) {
122 if (!strcmp(value, "true"))
123 options.cloning = 1;
124 else if (!strcmp(value, "false"))
125 options.cloning = 0;
126 else
127 return -1;
128 return 0;
129 } else if (!strcmp(name, "update-shallow")) {
130 if (!strcmp(value, "true"))
131 options.update_shallow = 1;
132 else if (!strcmp(value, "false"))
133 options.update_shallow = 0;
134 else
135 return -1;
136 return 0;
0ea47f9d
JH
137 } else if (!strcmp(name, "pushcert")) {
138 if (!strcmp(value, "true"))
30261094 139 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
0ea47f9d 140 else if (!strcmp(value, "false"))
30261094
DB
141 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
142 else if (!strcmp(value, "if-asked"))
143 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
0ea47f9d
JH
144 else
145 return -1;
146 return 0;
511155db
BW
147 } else if (!strcmp(name, "push-option")) {
148 string_list_append(&options.push_options, value);
149 return 0;
c915f11e
EW
150
151#if LIBCURL_VERSION_NUM >= 0x070a08
152 } else if (!strcmp(name, "family")) {
153 if (!strcmp(value, "ipv4"))
154 git_curl_ipresolve = CURL_IPRESOLVE_V4;
155 else if (!strcmp(value, "ipv6"))
156 git_curl_ipresolve = CURL_IPRESOLVE_V6;
157 else if (!strcmp(value, "all"))
158 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
159 else
160 return -1;
161 return 0;
162#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
88e2f9ed
JT
163 } else if (!strcmp(name, "from-promisor")) {
164 options.from_promisor = 1;
165 return 0;
166 } else if (!strcmp(name, "no-dependents")) {
167 options.no_dependents = 1;
168 return 0;
acb0c572
JH
169 } else if (!strcmp(name, "filter")) {
170 options.filter = xstrdup(value);;
171 return 0;
16094885 172 } else {
ef08ef9e
SP
173 return 1 /* unsupported */;
174 }
175}
176
97cc7bc4
SP
177struct discovery {
178 const char *service;
179 char *buf_alloc;
180 char *buf;
181 size_t len;
2a455202 182 struct ref *refs;
910650d2 183 struct oid_array shallow;
97cc7bc4
SP
184 unsigned proto_git : 1;
185};
186static struct discovery *last_discovery;
187
b8054bbe
JK
188static struct ref *parse_git_refs(struct discovery *heads, int for_push)
189{
190 struct ref *list = NULL;
191 get_remote_heads(-1, heads->buf, heads->len, &list,
16094885 192 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
b8054bbe
JK
193 return list;
194}
195
196static struct ref *parse_info_refs(struct discovery *heads)
197{
198 char *data, *start, *mid;
199 char *ref_name;
200 int i = 0;
201
202 struct ref *refs = NULL;
203 struct ref *ref = NULL;
204 struct ref *last_ref = NULL;
205
206 data = heads->buf;
207 start = NULL;
208 mid = data;
209 while (i < heads->len) {
210 if (!start) {
211 start = &data[i];
212 }
213 if (data[i] == '\t')
214 mid = &data[i];
215 if (data[i] == '\n') {
216 if (mid - start != 40)
b227bbc4
JK
217 die("%sinfo/refs not valid: is this a git repository?",
218 url.buf);
b8054bbe
JK
219 data[i] = 0;
220 ref_name = mid + 1;
6f687c21 221 ref = alloc_ref(ref_name);
f4e54d02 222 get_oid_hex(start, &ref->old_oid);
b8054bbe
JK
223 if (!refs)
224 refs = ref;
225 if (last_ref)
226 last_ref->next = ref;
227 last_ref = ref;
228 start = NULL;
229 }
230 i++;
231 }
232
233 ref = alloc_ref("HEAD");
b227bbc4 234 if (!http_fetch_ref(url.buf, ref) &&
b8054bbe
JK
235 !resolve_remote_symref(ref, refs)) {
236 ref->next = refs;
237 refs = ref;
238 } else {
239 free(ref);
240 }
241
242 return refs;
243}
244
97cc7bc4
SP
245static void free_discovery(struct discovery *d)
246{
247 if (d) {
248 if (d == last_discovery)
249 last_discovery = NULL;
ee3051bd 250 free(d->shallow.oid);
97cc7bc4 251 free(d->buf_alloc);
2a455202 252 free_refs(d->refs);
97cc7bc4
SP
253 free(d);
254 }
255}
256
fc1b774c
JK
257static int show_http_message(struct strbuf *type, struct strbuf *charset,
258 struct strbuf *msg)
426e70d4
JK
259{
260 const char *p, *eol;
261
262 /*
263 * We only show text/plain parts, as other types are likely
264 * to be ugly to look at on the user's terminal.
426e70d4 265 */
bf197fd7 266 if (strcmp(type->buf, "text/plain"))
426e70d4 267 return -1;
fc1b774c
JK
268 if (charset->len)
269 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
426e70d4
JK
270
271 strbuf_trim(msg);
272 if (!msg->len)
273 return -1;
274
275 p = msg->buf;
276 do {
277 eol = strchrnul(p, '\n');
278 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
279 p = eol + 1;
280 } while(*eol);
281 return 0;
282}
283
24d36f14 284static struct discovery *discover_refs(const char *service, int for_push)
a2d725b7 285{
4656bf47
SP
286 struct strbuf exp = STRBUF_INIT;
287 struct strbuf type = STRBUF_INIT;
fc1b774c 288 struct strbuf charset = STRBUF_INIT;
a2d725b7 289 struct strbuf buffer = STRBUF_INIT;
c65d5692 290 struct strbuf refs_url = STRBUF_INIT;
050ef365 291 struct strbuf effective_url = STRBUF_INIT;
97cc7bc4 292 struct discovery *last = last_discovery;
243c329c 293 int http_ret, maybe_smart = 0;
fcaa6e64 294 struct http_get_options http_options;
a2d725b7 295
97cc7bc4
SP
296 if (last && !strcmp(service, last->service))
297 return last;
298 free_discovery(last);
a2d725b7 299
b227bbc4 300 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
59556548 301 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
02572c2e 302 git_env_bool("GIT_SMART_HTTP", 1)) {
243c329c 303 maybe_smart = 1;
b227bbc4 304 if (!strchr(url.buf, '?'))
c65d5692 305 strbuf_addch(&refs_url, '?');
97cc7bc4 306 else
c65d5692
JK
307 strbuf_addch(&refs_url, '&');
308 strbuf_addf(&refs_url, "service=%s", service);
97cc7bc4 309 }
a2d725b7 310
fcaa6e64
JK
311 memset(&http_options, 0, sizeof(http_options));
312 http_options.content_type = &type;
313 http_options.charset = &charset;
314 http_options.effective_url = &effective_url;
315 http_options.base_url = &url;
50d34137 316 http_options.initial_request = 1;
fcaa6e64
JK
317 http_options.no_cache = 1;
318 http_options.keep_error = 1;
1bbcc224 319
fcaa6e64 320 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
a2d725b7
DB
321 switch (http_ret) {
322 case HTTP_OK:
323 break;
324 case HTTP_MISSING_TARGET:
fc1b774c 325 show_http_message(&type, &charset, &buffer);
b227bbc4 326 die("repository '%s' not found", url.buf);
42653c09 327 case HTTP_NOAUTH:
fc1b774c 328 show_http_message(&type, &charset, &buffer);
b227bbc4 329 die("Authentication failed for '%s'", url.buf);
a2d725b7 330 default:
fc1b774c 331 show_http_message(&type, &charset, &buffer);
b227bbc4 332 die("unable to access '%s': %s", url.buf, curl_errorstr);
a2d725b7
DB
333 }
334
50d34137
JK
335 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
336 warning(_("redirecting to %s"), url.buf);
337
97cc7bc4
SP
338 last= xcalloc(1, sizeof(*last_discovery));
339 last->service = service;
340 last->buf_alloc = strbuf_detach(&buffer, &last->len);
341 last->buf = last->buf_alloc;
342
4656bf47
SP
343 strbuf_addf(&exp, "application/x-%s-advertisement", service);
344 if (maybe_smart &&
345 (5 <= last->len && last->buf[4] == '#') &&
346 !strbuf_cmp(&exp, &type)) {
4981fe75
JK
347 char *line;
348
4656bf47
SP
349 /*
350 * smart HTTP response; validate that the service
97cc7bc4
SP
351 * pkt-line matches our request.
352 */
4981fe75 353 line = packet_read_line_buf(&last->buf, &last->len, NULL);
97cc7bc4 354
4656bf47 355 strbuf_reset(&exp);
97cc7bc4 356 strbuf_addf(&exp, "# service=%s", service);
4981fe75
JK
357 if (strcmp(line, exp.buf))
358 die("invalid server response; got '%s'", line);
97cc7bc4
SP
359 strbuf_release(&exp);
360
361 /* The header can include additional metadata lines, up
362 * until a packet flush marker. Ignore these now, but
363 * in the future we might start to scan them.
364 */
4981fe75
JK
365 while (packet_read_line_buf(&last->buf, &last->len, NULL))
366 ;
97cc7bc4
SP
367
368 last->proto_git = 1;
369 }
370
2a455202
JK
371 if (last->proto_git)
372 last->refs = parse_git_refs(last, for_push);
373 else
374 last->refs = parse_info_refs(last);
375
c65d5692 376 strbuf_release(&refs_url);
4656bf47
SP
377 strbuf_release(&exp);
378 strbuf_release(&type);
fc1b774c 379 strbuf_release(&charset);
050ef365 380 strbuf_release(&effective_url);
97cc7bc4
SP
381 strbuf_release(&buffer);
382 last_discovery = last;
383 return last;
384}
385
97cc7bc4
SP
386static struct ref *get_refs(int for_push)
387{
388 struct discovery *heads;
389
390 if (for_push)
2a455202 391 heads = discover_refs("git-receive-pack", for_push);
97cc7bc4 392 else
2a455202 393 heads = discover_refs("git-upload-pack", for_push);
97cc7bc4 394
2a455202 395 return heads->refs;
97cc7bc4
SP
396}
397
ae4efe19
SP
398static void output_refs(struct ref *refs)
399{
400 struct ref *posn;
401 for (posn = refs; posn; posn = posn->next) {
402 if (posn->symref)
403 printf("@%s %s\n", posn->symref, posn->name);
404 else
f4e54d02 405 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
ae4efe19
SP
406 }
407 printf("\n");
408 fflush(stdout);
ae4efe19
SP
409}
410
de1a2fdd
SP
411struct rpc_state {
412 const char *service_name;
413 const char **argv;
8150749d 414 struct strbuf *stdin_preamble;
de1a2fdd
SP
415 char *service_url;
416 char *hdr_content_type;
417 char *hdr_accept;
418 char *buf;
419 size_t alloc;
420 size_t len;
421 size_t pos;
422 int in;
423 int out;
296b847c 424 int any_written;
de1a2fdd 425 struct strbuf result;
b8538603 426 unsigned gzip_request : 1;
6c81a990 427 unsigned initial_buffer : 1;
de1a2fdd
SP
428};
429
430static size_t rpc_out(void *ptr, size_t eltsize,
431 size_t nmemb, void *buffer_)
432{
433 size_t max = eltsize * nmemb;
434 struct rpc_state *rpc = buffer_;
435 size_t avail = rpc->len - rpc->pos;
436
437 if (!avail) {
6c81a990 438 rpc->initial_buffer = 0;
4981fe75 439 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
440 if (!avail)
441 return 0;
442 rpc->pos = 0;
443 rpc->len = avail;
444 }
445
48310608 446 if (max < avail)
de1a2fdd
SP
447 avail = max;
448 memcpy(ptr, rpc->buf + rpc->pos, avail);
449 rpc->pos += avail;
450 return avail;
451}
452
6c81a990 453#ifndef NO_CURL_IOCTL
5092d3ec 454static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
6c81a990
MS
455{
456 struct rpc_state *rpc = clientp;
457
458 switch (cmd) {
459 case CURLIOCMD_NOP:
460 return CURLIOE_OK;
461
462 case CURLIOCMD_RESTARTREAD:
463 if (rpc->initial_buffer) {
464 rpc->pos = 0;
465 return CURLIOE_OK;
466 }
b725b270 467 error("unable to rewind rpc post data - try increasing http.postBuffer");
6c81a990
MS
468 return CURLIOE_FAILRESTART;
469
470 default:
471 return CURLIOE_UNKNOWNCMD;
472 }
473}
474#endif
475
a04ff3ec 476static size_t rpc_in(char *ptr, size_t eltsize,
de1a2fdd
SP
477 size_t nmemb, void *buffer_)
478{
479 size_t size = eltsize * nmemb;
480 struct rpc_state *rpc = buffer_;
296b847c
DT
481 if (size)
482 rpc->any_written = 1;
de1a2fdd
SP
483 write_or_die(rpc->in, ptr, size);
484 return size;
485}
486
3a347ed7
JK
487static int run_slot(struct active_request_slot *slot,
488 struct slot_results *results)
206b099d 489{
b81401c1 490 int err;
3a347ed7 491 struct slot_results results_buf;
206b099d 492
3a347ed7
JK
493 if (!results)
494 results = &results_buf;
495
beed336c 496 err = run_one_slot(slot, results);
206b099d 497
b81401c1 498 if (err != HTTP_OK && err != HTTP_REAUTH) {
00540458
SP
499 struct strbuf msg = STRBUF_INIT;
500 if (results->http_code && results->http_code != 200)
501 strbuf_addf(&msg, "HTTP %ld", results->http_code);
502 if (results->curl_result != CURLE_OK) {
503 if (msg.len)
504 strbuf_addch(&msg, ' ');
505 strbuf_addf(&msg, "curl %d", results->curl_result);
506 if (curl_errorstr[0]) {
507 strbuf_addch(&msg, ' ');
508 strbuf_addstr(&msg, curl_errorstr);
509 }
510 }
511 error("RPC failed; %s", msg.buf);
512 strbuf_release(&msg);
206b099d
SP
513 }
514
515 return err;
516}
517
3a347ed7 518static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
206b099d
SP
519{
520 struct active_request_slot *slot;
8cb01e2f 521 struct curl_slist *headers = http_copy_default_headers();
206b099d
SP
522 struct strbuf buf = STRBUF_INIT;
523 int err;
524
525 slot = get_active_slot();
526
527 headers = curl_slist_append(headers, rpc->hdr_content_type);
528 headers = curl_slist_append(headers, rpc->hdr_accept);
529
530 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
531 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
532 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 533 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
206b099d
SP
534 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
535 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
536 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
537 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
538 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
539
3a347ed7 540 err = run_slot(slot, results);
206b099d
SP
541
542 curl_slist_free_all(headers);
543 strbuf_release(&buf);
544 return err;
545}
546
37ee680d
DT
547static curl_off_t xcurl_off_t(ssize_t len) {
548 if (len > maximum_signed_value_of_type(curl_off_t))
549 die("cannot handle pushes this big");
550 return (curl_off_t) len;
551}
552
de1a2fdd
SP
553static int post_rpc(struct rpc_state *rpc)
554{
555 struct active_request_slot *slot;
8cb01e2f 556 struct curl_slist *headers = http_copy_default_headers();
b8538603
SP
557 int use_gzip = rpc->gzip_request;
558 char *gzip_body = NULL;
37711549 559 size_t gzip_size = 0;
206b099d 560 int err, large_request = 0;
c80d96ca 561 int needs_100_continue = 0;
de1a2fdd
SP
562
563 /* Try to load the entire request, if we can fit it into the
564 * allocated buffer space we can use HTTP/1.0 and avoid the
565 * chunked encoding mess.
566 */
567 while (1) {
568 size_t left = rpc->alloc - rpc->len;
569 char *buf = rpc->buf + rpc->len;
570 int n;
571
572 if (left < LARGE_PACKET_MAX) {
573 large_request = 1;
b8538603 574 use_gzip = 0;
de1a2fdd
SP
575 break;
576 }
577
4981fe75 578 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
de1a2fdd
SP
579 if (!n)
580 break;
581 rpc->len += n;
582 }
583
206b099d 584 if (large_request) {
c80d96ca 585 struct slot_results results;
586
b81401c1 587 do {
c80d96ca 588 err = probe_rpc(rpc, &results);
2501aff8
JK
589 if (err == HTTP_REAUTH)
590 credential_fill(&http_auth);
b81401c1
JK
591 } while (err == HTTP_REAUTH);
592 if (err != HTTP_OK)
593 return -1;
c80d96ca 594
595 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
596 needs_100_continue = 1;
206b099d
SP
597 }
598
abf8df86
JK
599 headers = curl_slist_append(headers, rpc->hdr_content_type);
600 headers = curl_slist_append(headers, rpc->hdr_accept);
c80d96ca 601 headers = curl_slist_append(headers, needs_100_continue ?
602 "Expect: 100-continue" : "Expect:");
abf8df86
JK
603
604retry:
de1a2fdd 605 slot = get_active_slot();
de1a2fdd 606
de1a2fdd 607 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 608 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 609 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
aa90b969 610 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
de1a2fdd 611
de1a2fdd
SP
612 if (large_request) {
613 /* The request body is large and the size cannot be predicted.
614 * We must use chunked encoding to send it.
615 */
de1a2fdd 616 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
6c81a990 617 rpc->initial_buffer = 1;
de1a2fdd
SP
618 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
619 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
6c81a990
MS
620#ifndef NO_CURL_IOCTL
621 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
622 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
623#endif
de1a2fdd
SP
624 if (options.verbosity > 1) {
625 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
626 fflush(stderr);
627 }
628
2e736fd5
JK
629 } else if (gzip_body) {
630 /*
631 * If we are looping to retry authentication, then the previous
632 * run will have set up the headers and gzip buffer already,
633 * and we just need to send it.
634 */
635 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 636 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
2e736fd5 637
b8538603
SP
638 } else if (use_gzip && 1024 < rpc->len) {
639 /* The client backend isn't giving us compressed data so
640 * we can try to deflate it ourselves, this may save on.
641 * the transfer time.
642 */
ef49a7a0 643 git_zstream stream;
b8538603
SP
644 int ret;
645
55bb5c91 646 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
df126e10
JK
647 gzip_size = git_deflate_bound(&stream, rpc->len);
648 gzip_body = xmalloc(gzip_size);
b8538603
SP
649
650 stream.next_in = (unsigned char *)rpc->buf;
651 stream.avail_in = rpc->len;
652 stream.next_out = (unsigned char *)gzip_body;
df126e10 653 stream.avail_out = gzip_size;
b8538603 654
55bb5c91 655 ret = git_deflate(&stream, Z_FINISH);
b8538603
SP
656 if (ret != Z_STREAM_END)
657 die("cannot deflate request; zlib deflate error %d", ret);
658
55bb5c91 659 ret = git_deflate_end_gently(&stream);
b8538603
SP
660 if (ret != Z_OK)
661 die("cannot deflate request; zlib end error %d", ret);
662
df126e10 663 gzip_size = stream.total_out;
b8538603
SP
664
665 headers = curl_slist_append(headers, "Content-Encoding: gzip");
666 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
37ee680d 667 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
b8538603
SP
668
669 if (options.verbosity > 1) {
670 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
671 rpc->service_name,
df126e10 672 (unsigned long)rpc->len, (unsigned long)gzip_size);
b8538603
SP
673 fflush(stderr);
674 }
de1a2fdd
SP
675 } else {
676 /* We know the complete request size in advance, use the
677 * more normal Content-Length approach.
678 */
679 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
37ee680d 680 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
de1a2fdd
SP
681 if (options.verbosity > 1) {
682 fprintf(stderr, "POST %s (%lu bytes)\n",
683 rpc->service_name, (unsigned long)rpc->len);
684 fflush(stderr);
685 }
686 }
687
688 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
689 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
690 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
691
296b847c
DT
692
693 rpc->any_written = 0;
3a347ed7 694 err = run_slot(slot, NULL);
2501aff8
JK
695 if (err == HTTP_REAUTH && !large_request) {
696 credential_fill(&http_auth);
abf8df86 697 goto retry;
2501aff8 698 }
b81401c1
JK
699 if (err != HTTP_OK)
700 err = -1;
de1a2fdd 701
296b847c
DT
702 if (!rpc->any_written)
703 err = -1;
704
de1a2fdd 705 curl_slist_free_all(headers);
b8538603 706 free(gzip_body);
de1a2fdd
SP
707 return err;
708}
709
710static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
711{
712 const char *svc = rpc->service_name;
713 struct strbuf buf = STRBUF_INIT;
8150749d 714 struct strbuf *preamble = rpc->stdin_preamble;
d3180279 715 struct child_process client = CHILD_PROCESS_INIT;
de1a2fdd
SP
716 int err = 0;
717
de1a2fdd
SP
718 client.in = -1;
719 client.out = -1;
720 client.git_cmd = 1;
721 client.argv = rpc->argv;
722 if (start_command(&client))
723 exit(1);
8150749d
IT
724 if (preamble)
725 write_or_die(client.in, preamble->buf, preamble->len);
de1a2fdd
SP
726 if (heads)
727 write_or_die(client.in, heads->buf, heads->len);
728
729 rpc->alloc = http_post_buffer;
730 rpc->buf = xmalloc(rpc->alloc);
731 rpc->in = client.in;
732 rpc->out = client.out;
733 strbuf_init(&rpc->result, 0);
734
b227bbc4 735 strbuf_addf(&buf, "%s%s", url.buf, svc);
de1a2fdd
SP
736 rpc->service_url = strbuf_detach(&buf, NULL);
737
738 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
739 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
740
8efa5f62 741 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
742 rpc->hdr_accept = strbuf_detach(&buf, NULL);
743
744 while (!err) {
4981fe75 745 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
de1a2fdd
SP
746 if (!n)
747 break;
748 rpc->pos = 0;
749 rpc->len = n;
750 err |= post_rpc(rpc);
751 }
de1a2fdd
SP
752
753 close(client.in);
de1a2fdd 754 client.in = -1;
6cdf0223
SP
755 if (!err) {
756 strbuf_read(&rpc->result, client.out, 0);
757 } else {
758 char buf[4096];
759 for (;;)
760 if (xread(client.out, buf, sizeof(buf)) <= 0)
761 break;
762 }
b4ee10f6
SP
763
764 close(client.out);
de1a2fdd
SP
765 client.out = -1;
766
767 err |= finish_command(&client);
768 free(rpc->service_url);
769 free(rpc->hdr_content_type);
770 free(rpc->hdr_accept);
771 free(rpc->buf);
772 strbuf_release(&buf);
773 return err;
774}
775
292ce46b
SP
776static int fetch_dumb(int nr_heads, struct ref **to_fetch)
777{
26e1e0b2 778 struct walker *walker;
b32fa95f 779 char **targets;
292ce46b
SP
780 int ret, i;
781
b32fa95f 782 ALLOC_ARRAY(targets, nr_heads);
508ea882
NTND
783 if (options.depth || options.deepen_since)
784 die("dumb http transport does not support shallow capabilities");
292ce46b 785 for (i = 0; i < nr_heads; i++)
f4e54d02 786 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
292ce46b 787
b227bbc4 788 walker = get_http_walker(url.buf);
292ce46b
SP
789 walker->get_all = 1;
790 walker->get_tree = 1;
791 walker->get_history = 1;
ef08ef9e 792 walker->get_verbosely = options.verbosity >= 3;
292ce46b
SP
793 walker->get_recover = 0;
794 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
26e1e0b2 795 walker_free(walker);
292ce46b
SP
796
797 for (i = 0; i < nr_heads; i++)
798 free(targets[i]);
799 free(targets);
800
b725b270 801 return ret ? error("fetch failed.") : 0;
292ce46b
SP
802}
803
249b2004
SP
804static int fetch_git(struct discovery *heads,
805 int nr_heads, struct ref **to_fetch)
806{
807 struct rpc_state rpc;
8150749d 808 struct strbuf preamble = STRBUF_INIT;
b5f62ebe
NTND
809 int i, err;
810 struct argv_array args = ARGV_ARRAY_INIT;
811
812 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
813 "--stdin", "--lock-pack", NULL);
249b2004 814 if (options.followtags)
b5f62ebe 815 argv_array_push(&args, "--include-tag");
249b2004 816 if (options.thin)
b5f62ebe
NTND
817 argv_array_push(&args, "--thin");
818 if (options.verbosity >= 3)
819 argv_array_pushl(&args, "-v", "-v", NULL);
9ba38048 820 if (options.check_self_contained_and_connected)
b5f62ebe 821 argv_array_push(&args, "--check-self-contained-and-connected");
16094885 822 if (options.cloning)
b5f62ebe 823 argv_array_push(&args, "--cloning");
16094885 824 if (options.update_shallow)
b5f62ebe 825 argv_array_push(&args, "--update-shallow");
249b2004 826 if (!options.progress)
b5f62ebe
NTND
827 argv_array_push(&args, "--no-progress");
828 if (options.depth)
829 argv_array_pushf(&args, "--depth=%lu", options.depth);
508ea882
NTND
830 if (options.deepen_since)
831 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
a45a2600
NTND
832 for (i = 0; i < options.deepen_not.nr; i++)
833 argv_array_pushf(&args, "--shallow-exclude=%s",
834 options.deepen_not.items[i].string);
cccf74e2
NTND
835 if (options.deepen_relative && options.depth)
836 argv_array_push(&args, "--deepen-relative");
88e2f9ed
JT
837 if (options.from_promisor)
838 argv_array_push(&args, "--from-promisor");
839 if (options.no_dependents)
840 argv_array_push(&args, "--no-dependents");
acb0c572
JH
841 if (options.filter)
842 argv_array_pushf(&args, "--filter=%s", options.filter);
b5f62ebe 843 argv_array_push(&args, url.buf);
8150749d 844
249b2004
SP
845 for (i = 0; i < nr_heads; i++) {
846 struct ref *ref = to_fetch[i];
94ee8e2c 847 if (!*ref->name)
249b2004 848 die("cannot fetch by sha1 over smart http");
58f2ed05 849 packet_buf_write(&preamble, "%s %s\n",
f4e54d02 850 oid_to_hex(&ref->old_oid), ref->name);
249b2004 851 }
8150749d 852 packet_buf_flush(&preamble);
249b2004
SP
853
854 memset(&rpc, 0, sizeof(rpc));
855 rpc.service_name = "git-upload-pack",
b5f62ebe 856 rpc.argv = args.argv;
8150749d 857 rpc.stdin_preamble = &preamble;
b8538603 858 rpc.gzip_request = 1;
249b2004
SP
859
860 err = rpc_service(&rpc, heads);
861 if (rpc.result.len)
cdf4fb8e 862 write_or_die(1, rpc.result.buf, rpc.result.len);
249b2004 863 strbuf_release(&rpc.result);
8150749d 864 strbuf_release(&preamble);
b5f62ebe 865 argv_array_clear(&args);
249b2004
SP
866 return err;
867}
868
869static int fetch(int nr_heads, struct ref **to_fetch)
870{
2a455202 871 struct discovery *d = discover_refs("git-upload-pack", 0);
249b2004
SP
872 if (d->proto_git)
873 return fetch_git(d, nr_heads, to_fetch);
874 else
875 return fetch_dumb(nr_heads, to_fetch);
876}
877
292ce46b
SP
878static void parse_fetch(struct strbuf *buf)
879{
880 struct ref **to_fetch = NULL;
881 struct ref *list_head = NULL;
882 struct ref **list = &list_head;
883 int alloc_heads = 0, nr_heads = 0;
884
885 do {
95b567c7
JK
886 const char *p;
887 if (skip_prefix(buf->buf, "fetch ", &p)) {
888 const char *name;
292ce46b 889 struct ref *ref;
8338c911 890 struct object_id old_oid;
292ce46b 891
8338c911 892 if (get_oid_hex(p, &old_oid))
292ce46b 893 die("protocol error: expected sha/ref, got %s'", p);
8338c911 894 if (p[GIT_SHA1_HEXSZ] == ' ')
895 name = p + GIT_SHA1_HEXSZ + 1;
896 else if (!p[GIT_SHA1_HEXSZ])
292ce46b
SP
897 name = "";
898 else
899 die("protocol error: expected sha/ref, got %s'", p);
900
901 ref = alloc_ref(name);
8338c911 902 oidcpy(&ref->old_oid, &old_oid);
292ce46b
SP
903
904 *list = ref;
905 list = &ref->next;
906
907 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
908 to_fetch[nr_heads++] = ref;
909 }
910 else
911 die("http transport does not support %s", buf->buf);
912
913 strbuf_reset(buf);
8f309aeb 914 if (strbuf_getline_lf(buf, stdin) == EOF)
292ce46b
SP
915 return;
916 if (!*buf->buf)
917 break;
918 } while (1);
919
249b2004 920 if (fetch(nr_heads, to_fetch))
292ce46b
SP
921 exit(128); /* error already reported */
922 free_refs(list_head);
923 free(to_fetch);
924
925 printf("\n");
926 fflush(stdout);
927 strbuf_reset(buf);
928}
929
ae4efe19
SP
930static int push_dav(int nr_spec, char **specs)
931{
850d2fec
JK
932 struct child_process child = CHILD_PROCESS_INIT;
933 size_t i;
ae4efe19 934
850d2fec
JK
935 child.git_cmd = 1;
936 argv_array_push(&child.args, "http-push");
937 argv_array_push(&child.args, "--helper-status");
ae4efe19 938 if (options.dry_run)
850d2fec 939 argv_array_push(&child.args, "--dry-run");
ae4efe19 940 if (options.verbosity > 1)
850d2fec
JK
941 argv_array_push(&child.args, "--verbose");
942 argv_array_push(&child.args, url.buf);
ae4efe19 943 for (i = 0; i < nr_spec; i++)
850d2fec 944 argv_array_push(&child.args, specs[i]);
ae4efe19 945
850d2fec
JK
946 if (run_command(&child))
947 die("git-http-push failed");
ae4efe19
SP
948 return 0;
949}
950
de1a2fdd
SP
951static int push_git(struct discovery *heads, int nr_spec, char **specs)
952{
953 struct rpc_state rpc;
222b1212
JH
954 int i, err;
955 struct argv_array args;
05c1eb10 956 struct string_list_item *cas_option;
26be19ba 957 struct strbuf preamble = STRBUF_INIT;
222b1212
JH
958
959 argv_array_init(&args);
960 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
961 NULL);
de1a2fdd 962
de1a2fdd 963 if (options.thin)
222b1212 964 argv_array_push(&args, "--thin");
de1a2fdd 965 if (options.dry_run)
222b1212 966 argv_array_push(&args, "--dry-run");
30261094
DB
967 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
968 argv_array_push(&args, "--signed=yes");
969 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
970 argv_array_push(&args, "--signed=if-asked");
c207e34f 971 if (options.verbosity == 0)
222b1212 972 argv_array_push(&args, "--quiet");
c207e34f 973 else if (options.verbosity > 1)
222b1212 974 argv_array_push(&args, "--verbose");
511155db
BW
975 for (i = 0; i < options.push_options.nr; i++)
976 argv_array_pushf(&args, "--push-option=%s",
977 options.push_options.items[i].string);
222b1212 978 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
05c1eb10 979 for_each_string_list_item(cas_option, &cas_options)
2233ad45 980 argv_array_push(&args, cas_option->string);
b227bbc4 981 argv_array_push(&args, url.buf);
26be19ba
JK
982
983 argv_array_push(&args, "--stdin");
de1a2fdd 984 for (i = 0; i < nr_spec; i++)
26be19ba
JK
985 packet_buf_write(&preamble, "%s\n", specs[i]);
986 packet_buf_flush(&preamble);
de1a2fdd
SP
987
988 memset(&rpc, 0, sizeof(rpc));
989 rpc.service_name = "git-receive-pack",
222b1212 990 rpc.argv = args.argv;
26be19ba 991 rpc.stdin_preamble = &preamble;
de1a2fdd
SP
992
993 err = rpc_service(&rpc, heads);
994 if (rpc.result.len)
cdf4fb8e 995 write_or_die(1, rpc.result.buf, rpc.result.len);
de1a2fdd 996 strbuf_release(&rpc.result);
26be19ba 997 strbuf_release(&preamble);
222b1212 998 argv_array_clear(&args);
de1a2fdd
SP
999 return err;
1000}
1001
1002static int push(int nr_spec, char **specs)
1003{
2a455202 1004 struct discovery *heads = discover_refs("git-receive-pack", 1);
de1a2fdd
SP
1005 int ret;
1006
1007 if (heads->proto_git)
1008 ret = push_git(heads, nr_spec, specs);
1009 else
1010 ret = push_dav(nr_spec, specs);
1011 free_discovery(heads);
1012 return ret;
1013}
1014
ae4efe19
SP
1015static void parse_push(struct strbuf *buf)
1016{
1017 char **specs = NULL;
5238cbf6 1018 int alloc_spec = 0, nr_spec = 0, i, ret;
ae4efe19
SP
1019
1020 do {
59556548 1021 if (starts_with(buf->buf, "push ")) {
ae4efe19
SP
1022 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1023 specs[nr_spec++] = xstrdup(buf->buf + 5);
1024 }
1025 else
1026 die("http transport does not support %s", buf->buf);
1027
1028 strbuf_reset(buf);
8f309aeb 1029 if (strbuf_getline_lf(buf, stdin) == EOF)
dc4cd767 1030 goto free_specs;
ae4efe19
SP
1031 if (!*buf->buf)
1032 break;
1033 } while (1);
1034
5238cbf6 1035 ret = push(nr_spec, specs);
ae4efe19
SP
1036 printf("\n");
1037 fflush(stdout);
dc4cd767 1038
5238cbf6
SP
1039 if (ret)
1040 exit(128); /* error already reported */
1041
dc4cd767
JM
1042 free_specs:
1043 for (i = 0; i < nr_spec; i++)
1044 free(specs[i]);
1045 free(specs);
ae4efe19
SP
1046}
1047
3f2e2297 1048int cmd_main(int argc, const char **argv)
a2d725b7 1049{
a2d725b7 1050 struct strbuf buf = STRBUF_INIT;
a45d3d7e 1051 int nongit;
a2d725b7 1052
a45d3d7e 1053 setup_git_directory_gently(&nongit);
a2d725b7 1054 if (argc < 2) {
cdaa4e98 1055 error("remote-curl: usage: git remote-curl <remote> [<url>]");
a2d725b7
DB
1056 return 1;
1057 }
1058
ef08ef9e
SP
1059 options.verbosity = 1;
1060 options.progress = !!isatty(2);
de1a2fdd 1061 options.thin = 1;
a45a2600 1062 string_list_init(&options.deepen_not, 1);
511155db 1063 string_list_init(&options.push_options, 1);
ef08ef9e 1064
a2d725b7
DB
1065 remote = remote_get(argv[1]);
1066
1067 if (argc > 2) {
b227bbc4 1068 end_url_with_slash(&url, argv[2]);
a2d725b7 1069 } else {
b227bbc4 1070 end_url_with_slash(&url, remote->url[0]);
a2d725b7
DB
1071 }
1072
b227bbc4 1073 http_init(remote, url.buf, 0);
888692b7 1074
a2d725b7 1075 do {
95b567c7
JK
1076 const char *arg;
1077
8f309aeb 1078 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1843f0ce 1079 if (ferror(stdin))
cdaa4e98 1080 error("remote-curl: error reading command stream from git");
1843f0ce
SR
1081 return 1;
1082 }
1083 if (buf.len == 0)
a2d725b7 1084 break;
59556548 1085 if (starts_with(buf.buf, "fetch ")) {
a45d3d7e 1086 if (nongit)
cdaa4e98 1087 die("remote-curl: fetch attempted without a local repo");
292ce46b
SP
1088 parse_fetch(&buf);
1089
59556548 1090 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
97cc7bc4
SP
1091 int for_push = !!strstr(buf.buf + 4, "for-push");
1092 output_refs(get_refs(for_push));
ae4efe19 1093
59556548 1094 } else if (starts_with(buf.buf, "push ")) {
ae4efe19
SP
1095 parse_push(&buf);
1096
95b567c7
JK
1097 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1098 char *value = strchr(arg, ' ');
ef08ef9e
SP
1099 int result;
1100
1101 if (value)
1102 *value++ = '\0';
1103 else
1104 value = "true";
1105
95b567c7 1106 result = set_option(arg, value);
ef08ef9e
SP
1107 if (!result)
1108 printf("ok\n");
1109 else if (result < 0)
1110 printf("error invalid value\n");
1111 else
1112 printf("unsupported\n");
a2d725b7 1113 fflush(stdout);
ef08ef9e 1114
a2d725b7
DB
1115 } else if (!strcmp(buf.buf, "capabilities")) {
1116 printf("fetch\n");
ef08ef9e 1117 printf("option\n");
ae4efe19 1118 printf("push\n");
9ba38048 1119 printf("check-connectivity\n");
a2d725b7
DB
1120 printf("\n");
1121 fflush(stdout);
1122 } else {
cdaa4e98 1123 error("remote-curl: unknown command '%s' from git", buf.buf);
a2d725b7
DB
1124 return 1;
1125 }
1126 strbuf_reset(&buf);
1127 } while (1);
888692b7
TRC
1128
1129 http_cleanup();
1130
a2d725b7
DB
1131 return 0;
1132}