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