t5537: fix incorrect expectation in test case 10
[alt-git.git] / remote-curl.c
blobd1fc163e5e178a9f109fe8dc901b617da866357a
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"
15 static struct remote *remote;
16 /* always ends with a trailing slash */
17 static struct strbuf url = STRBUF_INIT;
19 struct options {
20 int verbosity;
21 unsigned long depth;
22 unsigned progress : 1,
23 check_self_contained_and_connected : 1,
24 cloning : 1,
25 update_shallow : 1,
26 followtags : 1,
27 dry_run : 1,
28 thin : 1;
30 static struct options options;
31 static struct string_list cas_options = STRING_LIST_INIT_DUP;
33 static int set_option(const char *name, const char *value)
35 if (!strcmp(name, "verbosity")) {
36 char *end;
37 int v = strtol(value, &end, 10);
38 if (value == end || *end)
39 return -1;
40 options.verbosity = v;
41 return 0;
43 else if (!strcmp(name, "progress")) {
44 if (!strcmp(value, "true"))
45 options.progress = 1;
46 else if (!strcmp(value, "false"))
47 options.progress = 0;
48 else
49 return -1;
50 return 0;
52 else if (!strcmp(name, "depth")) {
53 char *end;
54 unsigned long v = strtoul(value, &end, 10);
55 if (value == end || *end)
56 return -1;
57 options.depth = v;
58 return 0;
60 else if (!strcmp(name, "followtags")) {
61 if (!strcmp(value, "true"))
62 options.followtags = 1;
63 else if (!strcmp(value, "false"))
64 options.followtags = 0;
65 else
66 return -1;
67 return 0;
69 else if (!strcmp(name, "dry-run")) {
70 if (!strcmp(value, "true"))
71 options.dry_run = 1;
72 else if (!strcmp(value, "false"))
73 options.dry_run = 0;
74 else
75 return -1;
76 return 0;
78 else if (!strcmp(name, "check-connectivity")) {
79 if (!strcmp(value, "true"))
80 options.check_self_contained_and_connected = 1;
81 else if (!strcmp(value, "false"))
82 options.check_self_contained_and_connected = 0;
83 else
84 return -1;
85 return 0;
87 else if (!strcmp(name, "cas")) {
88 struct strbuf val = STRBUF_INIT;
89 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
90 string_list_append(&cas_options, val.buf);
91 strbuf_release(&val);
92 return 0;
93 } else if (!strcmp(name, "cloning")) {
94 if (!strcmp(value, "true"))
95 options.cloning = 1;
96 else if (!strcmp(value, "false"))
97 options.cloning = 0;
98 else
99 return -1;
100 return 0;
101 } else if (!strcmp(name, "update-shallow")) {
102 if (!strcmp(value, "true"))
103 options.update_shallow = 1;
104 else if (!strcmp(value, "false"))
105 options.update_shallow = 0;
106 else
107 return -1;
108 return 0;
109 } else {
110 return 1 /* unsupported */;
114 struct discovery {
115 const char *service;
116 char *buf_alloc;
117 char *buf;
118 size_t len;
119 struct ref *refs;
120 struct sha1_array shallow;
121 unsigned proto_git : 1;
123 static struct discovery *last_discovery;
125 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
127 struct ref *list = NULL;
128 get_remote_heads(-1, heads->buf, heads->len, &list,
129 for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
130 return list;
133 static struct ref *parse_info_refs(struct discovery *heads)
135 char *data, *start, *mid;
136 char *ref_name;
137 int i = 0;
139 struct ref *refs = NULL;
140 struct ref *ref = NULL;
141 struct ref *last_ref = NULL;
143 data = heads->buf;
144 start = NULL;
145 mid = data;
146 while (i < heads->len) {
147 if (!start) {
148 start = &data[i];
150 if (data[i] == '\t')
151 mid = &data[i];
152 if (data[i] == '\n') {
153 if (mid - start != 40)
154 die("%sinfo/refs not valid: is this a git repository?",
155 url.buf);
156 data[i] = 0;
157 ref_name = mid + 1;
158 ref = xmalloc(sizeof(struct ref) +
159 strlen(ref_name) + 1);
160 memset(ref, 0, sizeof(struct ref));
161 strcpy(ref->name, ref_name);
162 get_sha1_hex(start, ref->old_sha1);
163 if (!refs)
164 refs = ref;
165 if (last_ref)
166 last_ref->next = ref;
167 last_ref = ref;
168 start = NULL;
170 i++;
173 ref = alloc_ref("HEAD");
174 if (!http_fetch_ref(url.buf, ref) &&
175 !resolve_remote_symref(ref, refs)) {
176 ref->next = refs;
177 refs = ref;
178 } else {
179 free(ref);
182 return refs;
185 static void free_discovery(struct discovery *d)
187 if (d) {
188 if (d == last_discovery)
189 last_discovery = NULL;
190 free(d->shallow.sha1);
191 free(d->buf_alloc);
192 free_refs(d->refs);
193 free(d);
197 static int show_http_message(struct strbuf *type, struct strbuf *msg)
199 const char *p, *eol;
202 * We only show text/plain parts, as other types are likely
203 * to be ugly to look at on the user's terminal.
205 * TODO should handle "; charset=XXX", and re-encode into
206 * logoutputencoding
208 if (strcasecmp(type->buf, "text/plain"))
209 return -1;
211 strbuf_trim(msg);
212 if (!msg->len)
213 return -1;
215 p = msg->buf;
216 do {
217 eol = strchrnul(p, '\n');
218 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
219 p = eol + 1;
220 } while(*eol);
221 return 0;
224 static struct discovery* discover_refs(const char *service, int for_push)
226 struct strbuf exp = STRBUF_INIT;
227 struct strbuf type = STRBUF_INIT;
228 struct strbuf buffer = STRBUF_INIT;
229 struct strbuf refs_url = STRBUF_INIT;
230 struct strbuf effective_url = STRBUF_INIT;
231 struct discovery *last = last_discovery;
232 int http_ret, maybe_smart = 0;
233 struct http_get_options options;
235 if (last && !strcmp(service, last->service))
236 return last;
237 free_discovery(last);
239 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
240 if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
241 git_env_bool("GIT_SMART_HTTP", 1)) {
242 maybe_smart = 1;
243 if (!strchr(url.buf, '?'))
244 strbuf_addch(&refs_url, '?');
245 else
246 strbuf_addch(&refs_url, '&');
247 strbuf_addf(&refs_url, "service=%s", service);
250 memset(&options, 0, sizeof(options));
251 options.content_type = &type;
252 options.effective_url = &effective_url;
253 options.base_url = &url;
254 options.no_cache = 1;
255 options.keep_error = 1;
257 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
258 switch (http_ret) {
259 case HTTP_OK:
260 break;
261 case HTTP_MISSING_TARGET:
262 show_http_message(&type, &buffer);
263 die("repository '%s' not found", url.buf);
264 case HTTP_NOAUTH:
265 show_http_message(&type, &buffer);
266 die("Authentication failed for '%s'", url.buf);
267 default:
268 show_http_message(&type, &buffer);
269 die("unable to access '%s': %s", url.buf, curl_errorstr);
272 last= xcalloc(1, sizeof(*last_discovery));
273 last->service = service;
274 last->buf_alloc = strbuf_detach(&buffer, &last->len);
275 last->buf = last->buf_alloc;
277 strbuf_addf(&exp, "application/x-%s-advertisement", service);
278 if (maybe_smart &&
279 (5 <= last->len && last->buf[4] == '#') &&
280 !strbuf_cmp(&exp, &type)) {
281 char *line;
284 * smart HTTP response; validate that the service
285 * pkt-line matches our request.
287 line = packet_read_line_buf(&last->buf, &last->len, NULL);
289 strbuf_reset(&exp);
290 strbuf_addf(&exp, "# service=%s", service);
291 if (strcmp(line, exp.buf))
292 die("invalid server response; got '%s'", line);
293 strbuf_release(&exp);
295 /* The header can include additional metadata lines, up
296 * until a packet flush marker. Ignore these now, but
297 * in the future we might start to scan them.
299 while (packet_read_line_buf(&last->buf, &last->len, NULL))
302 last->proto_git = 1;
305 if (last->proto_git)
306 last->refs = parse_git_refs(last, for_push);
307 else
308 last->refs = parse_info_refs(last);
310 strbuf_release(&refs_url);
311 strbuf_release(&exp);
312 strbuf_release(&type);
313 strbuf_release(&effective_url);
314 strbuf_release(&buffer);
315 last_discovery = last;
316 return last;
319 static struct ref *get_refs(int for_push)
321 struct discovery *heads;
323 if (for_push)
324 heads = discover_refs("git-receive-pack", for_push);
325 else
326 heads = discover_refs("git-upload-pack", for_push);
328 return heads->refs;
331 static void output_refs(struct ref *refs)
333 struct ref *posn;
334 for (posn = refs; posn; posn = posn->next) {
335 if (posn->symref)
336 printf("@%s %s\n", posn->symref, posn->name);
337 else
338 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
340 printf("\n");
341 fflush(stdout);
344 struct rpc_state {
345 const char *service_name;
346 const char **argv;
347 struct strbuf *stdin_preamble;
348 char *service_url;
349 char *hdr_content_type;
350 char *hdr_accept;
351 char *buf;
352 size_t alloc;
353 size_t len;
354 size_t pos;
355 int in;
356 int out;
357 struct strbuf result;
358 unsigned gzip_request : 1;
359 unsigned initial_buffer : 1;
362 static size_t rpc_out(void *ptr, size_t eltsize,
363 size_t nmemb, void *buffer_)
365 size_t max = eltsize * nmemb;
366 struct rpc_state *rpc = buffer_;
367 size_t avail = rpc->len - rpc->pos;
369 if (!avail) {
370 rpc->initial_buffer = 0;
371 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
372 if (!avail)
373 return 0;
374 rpc->pos = 0;
375 rpc->len = avail;
378 if (max < avail)
379 avail = max;
380 memcpy(ptr, rpc->buf + rpc->pos, avail);
381 rpc->pos += avail;
382 return avail;
385 #ifndef NO_CURL_IOCTL
386 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
388 struct rpc_state *rpc = clientp;
390 switch (cmd) {
391 case CURLIOCMD_NOP:
392 return CURLIOE_OK;
394 case CURLIOCMD_RESTARTREAD:
395 if (rpc->initial_buffer) {
396 rpc->pos = 0;
397 return CURLIOE_OK;
399 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
400 return CURLIOE_FAILRESTART;
402 default:
403 return CURLIOE_UNKNOWNCMD;
406 #endif
408 static size_t rpc_in(char *ptr, size_t eltsize,
409 size_t nmemb, void *buffer_)
411 size_t size = eltsize * nmemb;
412 struct rpc_state *rpc = buffer_;
413 write_or_die(rpc->in, ptr, size);
414 return size;
417 static int run_slot(struct active_request_slot *slot)
419 int err;
420 struct slot_results results;
422 slot->results = &results;
423 slot->curl_result = curl_easy_perform(slot->curl);
424 finish_active_slot(slot);
426 err = handle_curl_result(&results);
427 if (err != HTTP_OK && err != HTTP_REAUTH) {
428 error("RPC failed; result=%d, HTTP code = %ld",
429 results.curl_result, results.http_code);
432 return err;
435 static int probe_rpc(struct rpc_state *rpc)
437 struct active_request_slot *slot;
438 struct curl_slist *headers = NULL;
439 struct strbuf buf = STRBUF_INIT;
440 int err;
442 slot = get_active_slot();
444 headers = curl_slist_append(headers, rpc->hdr_content_type);
445 headers = curl_slist_append(headers, rpc->hdr_accept);
447 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
448 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
449 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
450 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
451 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
452 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
453 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
454 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
455 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
457 err = run_slot(slot);
459 curl_slist_free_all(headers);
460 strbuf_release(&buf);
461 return err;
464 static int post_rpc(struct rpc_state *rpc)
466 struct active_request_slot *slot;
467 struct curl_slist *headers = NULL;
468 int use_gzip = rpc->gzip_request;
469 char *gzip_body = NULL;
470 size_t gzip_size = 0;
471 int err, large_request = 0;
473 /* Try to load the entire request, if we can fit it into the
474 * allocated buffer space we can use HTTP/1.0 and avoid the
475 * chunked encoding mess.
477 while (1) {
478 size_t left = rpc->alloc - rpc->len;
479 char *buf = rpc->buf + rpc->len;
480 int n;
482 if (left < LARGE_PACKET_MAX) {
483 large_request = 1;
484 use_gzip = 0;
485 break;
488 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
489 if (!n)
490 break;
491 rpc->len += n;
494 if (large_request) {
495 do {
496 err = probe_rpc(rpc);
497 if (err == HTTP_REAUTH)
498 credential_fill(&http_auth);
499 } while (err == HTTP_REAUTH);
500 if (err != HTTP_OK)
501 return -1;
504 headers = curl_slist_append(headers, rpc->hdr_content_type);
505 headers = curl_slist_append(headers, rpc->hdr_accept);
506 headers = curl_slist_append(headers, "Expect:");
508 retry:
509 slot = get_active_slot();
511 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
512 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
513 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
514 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
516 if (large_request) {
517 /* The request body is large and the size cannot be predicted.
518 * We must use chunked encoding to send it.
520 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
521 rpc->initial_buffer = 1;
522 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
523 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
524 #ifndef NO_CURL_IOCTL
525 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
526 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
527 #endif
528 if (options.verbosity > 1) {
529 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
530 fflush(stderr);
533 } else if (gzip_body) {
535 * If we are looping to retry authentication, then the previous
536 * run will have set up the headers and gzip buffer already,
537 * and we just need to send it.
539 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
540 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
542 } else if (use_gzip && 1024 < rpc->len) {
543 /* The client backend isn't giving us compressed data so
544 * we can try to deflate it ourselves, this may save on.
545 * the transfer time.
547 git_zstream stream;
548 int ret;
550 memset(&stream, 0, sizeof(stream));
551 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
552 gzip_size = git_deflate_bound(&stream, rpc->len);
553 gzip_body = xmalloc(gzip_size);
555 stream.next_in = (unsigned char *)rpc->buf;
556 stream.avail_in = rpc->len;
557 stream.next_out = (unsigned char *)gzip_body;
558 stream.avail_out = gzip_size;
560 ret = git_deflate(&stream, Z_FINISH);
561 if (ret != Z_STREAM_END)
562 die("cannot deflate request; zlib deflate error %d", ret);
564 ret = git_deflate_end_gently(&stream);
565 if (ret != Z_OK)
566 die("cannot deflate request; zlib end error %d", ret);
568 gzip_size = stream.total_out;
570 headers = curl_slist_append(headers, "Content-Encoding: gzip");
571 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
572 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
574 if (options.verbosity > 1) {
575 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
576 rpc->service_name,
577 (unsigned long)rpc->len, (unsigned long)gzip_size);
578 fflush(stderr);
580 } else {
581 /* We know the complete request size in advance, use the
582 * more normal Content-Length approach.
584 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
585 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
586 if (options.verbosity > 1) {
587 fprintf(stderr, "POST %s (%lu bytes)\n",
588 rpc->service_name, (unsigned long)rpc->len);
589 fflush(stderr);
593 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
594 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
595 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
597 err = run_slot(slot);
598 if (err == HTTP_REAUTH && !large_request) {
599 credential_fill(&http_auth);
600 goto retry;
602 if (err != HTTP_OK)
603 err = -1;
605 curl_slist_free_all(headers);
606 free(gzip_body);
607 return err;
610 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
612 const char *svc = rpc->service_name;
613 struct strbuf buf = STRBUF_INIT;
614 struct strbuf *preamble = rpc->stdin_preamble;
615 struct child_process client;
616 int err = 0;
618 memset(&client, 0, sizeof(client));
619 client.in = -1;
620 client.out = -1;
621 client.git_cmd = 1;
622 client.argv = rpc->argv;
623 if (start_command(&client))
624 exit(1);
625 if (preamble)
626 write_or_die(client.in, preamble->buf, preamble->len);
627 if (heads)
628 write_or_die(client.in, heads->buf, heads->len);
630 rpc->alloc = http_post_buffer;
631 rpc->buf = xmalloc(rpc->alloc);
632 rpc->in = client.in;
633 rpc->out = client.out;
634 strbuf_init(&rpc->result, 0);
636 strbuf_addf(&buf, "%s%s", url.buf, svc);
637 rpc->service_url = strbuf_detach(&buf, NULL);
639 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
640 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
642 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
643 rpc->hdr_accept = strbuf_detach(&buf, NULL);
645 while (!err) {
646 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
647 if (!n)
648 break;
649 rpc->pos = 0;
650 rpc->len = n;
651 err |= post_rpc(rpc);
654 close(client.in);
655 client.in = -1;
656 if (!err) {
657 strbuf_read(&rpc->result, client.out, 0);
658 } else {
659 char buf[4096];
660 for (;;)
661 if (xread(client.out, buf, sizeof(buf)) <= 0)
662 break;
665 close(client.out);
666 client.out = -1;
668 err |= finish_command(&client);
669 free(rpc->service_url);
670 free(rpc->hdr_content_type);
671 free(rpc->hdr_accept);
672 free(rpc->buf);
673 strbuf_release(&buf);
674 return err;
677 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
679 struct walker *walker;
680 char **targets = xmalloc(nr_heads * sizeof(char*));
681 int ret, i;
683 if (options.depth)
684 die("dumb http transport does not support --depth");
685 for (i = 0; i < nr_heads; i++)
686 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
688 walker = get_http_walker(url.buf);
689 walker->get_all = 1;
690 walker->get_tree = 1;
691 walker->get_history = 1;
692 walker->get_verbosely = options.verbosity >= 3;
693 walker->get_recover = 0;
694 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
695 walker_free(walker);
697 for (i = 0; i < nr_heads; i++)
698 free(targets[i]);
699 free(targets);
701 return ret ? error("Fetch failed.") : 0;
704 static int fetch_git(struct discovery *heads,
705 int nr_heads, struct ref **to_fetch)
707 struct rpc_state rpc;
708 struct strbuf preamble = STRBUF_INIT;
709 char *depth_arg = NULL;
710 int argc = 0, i, err;
711 const char *argv[17];
713 argv[argc++] = "fetch-pack";
714 argv[argc++] = "--stateless-rpc";
715 argv[argc++] = "--stdin";
716 argv[argc++] = "--lock-pack";
717 if (options.followtags)
718 argv[argc++] = "--include-tag";
719 if (options.thin)
720 argv[argc++] = "--thin";
721 if (options.verbosity >= 3) {
722 argv[argc++] = "-v";
723 argv[argc++] = "-v";
725 if (options.check_self_contained_and_connected)
726 argv[argc++] = "--check-self-contained-and-connected";
727 if (options.cloning)
728 argv[argc++] = "--cloning";
729 if (options.update_shallow)
730 argv[argc++] = "--update-shallow";
731 if (!options.progress)
732 argv[argc++] = "--no-progress";
733 if (options.depth) {
734 struct strbuf buf = STRBUF_INIT;
735 strbuf_addf(&buf, "--depth=%lu", options.depth);
736 depth_arg = strbuf_detach(&buf, NULL);
737 argv[argc++] = depth_arg;
739 argv[argc++] = url.buf;
740 argv[argc++] = NULL;
742 for (i = 0; i < nr_heads; i++) {
743 struct ref *ref = to_fetch[i];
744 if (!ref->name || !*ref->name)
745 die("cannot fetch by sha1 over smart http");
746 packet_buf_write(&preamble, "%s %s\n",
747 sha1_to_hex(ref->old_sha1), ref->name);
749 packet_buf_flush(&preamble);
751 memset(&rpc, 0, sizeof(rpc));
752 rpc.service_name = "git-upload-pack",
753 rpc.argv = argv;
754 rpc.stdin_preamble = &preamble;
755 rpc.gzip_request = 1;
757 err = rpc_service(&rpc, heads);
758 if (rpc.result.len)
759 write_or_die(1, rpc.result.buf, rpc.result.len);
760 strbuf_release(&rpc.result);
761 strbuf_release(&preamble);
762 free(depth_arg);
763 return err;
766 static int fetch(int nr_heads, struct ref **to_fetch)
768 struct discovery *d = discover_refs("git-upload-pack", 0);
769 if (d->proto_git)
770 return fetch_git(d, nr_heads, to_fetch);
771 else
772 return fetch_dumb(nr_heads, to_fetch);
775 static void parse_fetch(struct strbuf *buf)
777 struct ref **to_fetch = NULL;
778 struct ref *list_head = NULL;
779 struct ref **list = &list_head;
780 int alloc_heads = 0, nr_heads = 0;
782 do {
783 if (!prefixcmp(buf->buf, "fetch ")) {
784 char *p = buf->buf + strlen("fetch ");
785 char *name;
786 struct ref *ref;
787 unsigned char old_sha1[20];
789 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
790 die("protocol error: expected sha/ref, got %s'", p);
791 if (p[40] == ' ')
792 name = p + 41;
793 else if (!p[40])
794 name = "";
795 else
796 die("protocol error: expected sha/ref, got %s'", p);
798 ref = alloc_ref(name);
799 hashcpy(ref->old_sha1, old_sha1);
801 *list = ref;
802 list = &ref->next;
804 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
805 to_fetch[nr_heads++] = ref;
807 else
808 die("http transport does not support %s", buf->buf);
810 strbuf_reset(buf);
811 if (strbuf_getline(buf, stdin, '\n') == EOF)
812 return;
813 if (!*buf->buf)
814 break;
815 } while (1);
817 if (fetch(nr_heads, to_fetch))
818 exit(128); /* error already reported */
819 free_refs(list_head);
820 free(to_fetch);
822 printf("\n");
823 fflush(stdout);
824 strbuf_reset(buf);
827 static int push_dav(int nr_spec, char **specs)
829 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
830 int argc = 0, i;
832 argv[argc++] = "http-push";
833 argv[argc++] = "--helper-status";
834 if (options.dry_run)
835 argv[argc++] = "--dry-run";
836 if (options.verbosity > 1)
837 argv[argc++] = "--verbose";
838 argv[argc++] = url.buf;
839 for (i = 0; i < nr_spec; i++)
840 argv[argc++] = specs[i];
841 argv[argc++] = NULL;
843 if (run_command_v_opt(argv, RUN_GIT_CMD))
844 die("git-%s failed", argv[0]);
845 free(argv);
846 return 0;
849 static int push_git(struct discovery *heads, int nr_spec, char **specs)
851 struct rpc_state rpc;
852 int i, err;
853 struct argv_array args;
854 struct string_list_item *cas_option;
856 argv_array_init(&args);
857 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
858 NULL);
860 if (options.thin)
861 argv_array_push(&args, "--thin");
862 if (options.dry_run)
863 argv_array_push(&args, "--dry-run");
864 if (options.verbosity == 0)
865 argv_array_push(&args, "--quiet");
866 else if (options.verbosity > 1)
867 argv_array_push(&args, "--verbose");
868 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
869 for_each_string_list_item(cas_option, &cas_options)
870 argv_array_push(&args, cas_option->string);
871 argv_array_push(&args, url.buf);
872 for (i = 0; i < nr_spec; i++)
873 argv_array_push(&args, specs[i]);
875 memset(&rpc, 0, sizeof(rpc));
876 rpc.service_name = "git-receive-pack",
877 rpc.argv = args.argv;
879 err = rpc_service(&rpc, heads);
880 if (rpc.result.len)
881 write_or_die(1, rpc.result.buf, rpc.result.len);
882 strbuf_release(&rpc.result);
883 argv_array_clear(&args);
884 return err;
887 static int push(int nr_spec, char **specs)
889 struct discovery *heads = discover_refs("git-receive-pack", 1);
890 int ret;
892 if (heads->proto_git)
893 ret = push_git(heads, nr_spec, specs);
894 else
895 ret = push_dav(nr_spec, specs);
896 free_discovery(heads);
897 return ret;
900 static void parse_push(struct strbuf *buf)
902 char **specs = NULL;
903 int alloc_spec = 0, nr_spec = 0, i, ret;
905 do {
906 if (!prefixcmp(buf->buf, "push ")) {
907 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
908 specs[nr_spec++] = xstrdup(buf->buf + 5);
910 else
911 die("http transport does not support %s", buf->buf);
913 strbuf_reset(buf);
914 if (strbuf_getline(buf, stdin, '\n') == EOF)
915 goto free_specs;
916 if (!*buf->buf)
917 break;
918 } while (1);
920 ret = push(nr_spec, specs);
921 printf("\n");
922 fflush(stdout);
924 if (ret)
925 exit(128); /* error already reported */
927 free_specs:
928 for (i = 0; i < nr_spec; i++)
929 free(specs[i]);
930 free(specs);
933 int main(int argc, const char **argv)
935 struct strbuf buf = STRBUF_INIT;
936 int nongit;
938 git_extract_argv0_path(argv[0]);
939 setup_git_directory_gently(&nongit);
940 if (argc < 2) {
941 fprintf(stderr, "Remote needed\n");
942 return 1;
945 options.verbosity = 1;
946 options.progress = !!isatty(2);
947 options.thin = 1;
949 remote = remote_get(argv[1]);
951 if (argc > 2) {
952 end_url_with_slash(&url, argv[2]);
953 } else {
954 end_url_with_slash(&url, remote->url[0]);
957 http_init(remote, url.buf, 0);
959 do {
960 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
961 if (ferror(stdin))
962 fprintf(stderr, "Error reading command stream\n");
963 else
964 fprintf(stderr, "Unexpected end of command stream\n");
965 return 1;
967 if (buf.len == 0)
968 break;
969 if (!prefixcmp(buf.buf, "fetch ")) {
970 if (nongit)
971 die("Fetch attempted without a local repo");
972 parse_fetch(&buf);
974 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
975 int for_push = !!strstr(buf.buf + 4, "for-push");
976 output_refs(get_refs(for_push));
978 } else if (!prefixcmp(buf.buf, "push ")) {
979 parse_push(&buf);
981 } else if (!prefixcmp(buf.buf, "option ")) {
982 char *name = buf.buf + strlen("option ");
983 char *value = strchr(name, ' ');
984 int result;
986 if (value)
987 *value++ = '\0';
988 else
989 value = "true";
991 result = set_option(name, value);
992 if (!result)
993 printf("ok\n");
994 else if (result < 0)
995 printf("error invalid value\n");
996 else
997 printf("unsupported\n");
998 fflush(stdout);
1000 } else if (!strcmp(buf.buf, "capabilities")) {
1001 printf("fetch\n");
1002 printf("option\n");
1003 printf("push\n");
1004 printf("check-connectivity\n");
1005 printf("\n");
1006 fflush(stdout);
1007 } else {
1008 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
1009 return 1;
1011 strbuf_reset(&buf);
1012 } while (1);
1014 http_cleanup();
1016 return 0;