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