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