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