Git 1.8.5.1
[git/mingw/4msysgit.git] / remote-curl.c
blobc9b891adbf134193f07681a631ae368671e04aa6
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"
14 static struct remote *remote;
15 /* always ends with a trailing slash */
16 static struct strbuf url = STRBUF_INIT;
18 struct options {
19 int verbosity;
20 unsigned long depth;
21 unsigned progress : 1,
22 check_self_contained_and_connected : 1,
23 followtags : 1,
24 dry_run : 1,
25 thin : 1;
27 static struct options options;
28 static struct string_list cas_options = STRING_LIST_INIT_DUP;
30 static int set_option(const char *name, const char *value)
32 if (!strcmp(name, "verbosity")) {
33 char *end;
34 int v = strtol(value, &end, 10);
35 if (value == end || *end)
36 return -1;
37 options.verbosity = v;
38 return 0;
40 else if (!strcmp(name, "progress")) {
41 if (!strcmp(value, "true"))
42 options.progress = 1;
43 else if (!strcmp(value, "false"))
44 options.progress = 0;
45 else
46 return -1;
47 return 0;
49 else if (!strcmp(name, "depth")) {
50 char *end;
51 unsigned long v = strtoul(value, &end, 10);
52 if (value == end || *end)
53 return -1;
54 options.depth = v;
55 return 0;
57 else if (!strcmp(name, "followtags")) {
58 if (!strcmp(value, "true"))
59 options.followtags = 1;
60 else if (!strcmp(value, "false"))
61 options.followtags = 0;
62 else
63 return -1;
64 return 0;
66 else if (!strcmp(name, "dry-run")) {
67 if (!strcmp(value, "true"))
68 options.dry_run = 1;
69 else if (!strcmp(value, "false"))
70 options.dry_run = 0;
71 else
72 return -1;
73 return 0;
75 else if (!strcmp(name, "check-connectivity")) {
76 if (!strcmp(value, "true"))
77 options.check_self_contained_and_connected = 1;
78 else if (!strcmp(value, "false"))
79 options.check_self_contained_and_connected = 0;
80 else
81 return -1;
82 return 0;
84 else if (!strcmp(name, "cas")) {
85 struct strbuf val = STRBUF_INIT;
86 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
87 string_list_append(&cas_options, val.buf);
88 strbuf_release(&val);
89 return 0;
91 else {
92 return 1 /* unsupported */;
96 struct discovery {
97 const char *service;
98 char *buf_alloc;
99 char *buf;
100 size_t len;
101 struct ref *refs;
102 unsigned proto_git : 1;
104 static struct discovery *last_discovery;
106 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
108 struct ref *list = NULL;
109 get_remote_heads(-1, heads->buf, heads->len, &list,
110 for_push ? REF_NORMAL : 0, NULL);
111 return list;
114 static struct ref *parse_info_refs(struct discovery *heads)
116 char *data, *start, *mid;
117 char *ref_name;
118 int i = 0;
120 struct ref *refs = NULL;
121 struct ref *ref = NULL;
122 struct ref *last_ref = NULL;
124 data = heads->buf;
125 start = NULL;
126 mid = data;
127 while (i < heads->len) {
128 if (!start) {
129 start = &data[i];
131 if (data[i] == '\t')
132 mid = &data[i];
133 if (data[i] == '\n') {
134 if (mid - start != 40)
135 die("%sinfo/refs not valid: is this a git repository?",
136 url.buf);
137 data[i] = 0;
138 ref_name = mid + 1;
139 ref = xmalloc(sizeof(struct ref) +
140 strlen(ref_name) + 1);
141 memset(ref, 0, sizeof(struct ref));
142 strcpy(ref->name, ref_name);
143 get_sha1_hex(start, ref->old_sha1);
144 if (!refs)
145 refs = ref;
146 if (last_ref)
147 last_ref->next = ref;
148 last_ref = ref;
149 start = NULL;
151 i++;
154 ref = alloc_ref("HEAD");
155 if (!http_fetch_ref(url.buf, ref) &&
156 !resolve_remote_symref(ref, refs)) {
157 ref->next = refs;
158 refs = ref;
159 } else {
160 free(ref);
163 return refs;
166 static void free_discovery(struct discovery *d)
168 if (d) {
169 if (d == last_discovery)
170 last_discovery = NULL;
171 free(d->buf_alloc);
172 free_refs(d->refs);
173 free(d);
177 static int show_http_message(struct strbuf *type, struct strbuf *msg)
179 const char *p, *eol;
182 * We only show text/plain parts, as other types are likely
183 * to be ugly to look at on the user's terminal.
185 * TODO should handle "; charset=XXX", and re-encode into
186 * logoutputencoding
188 if (strcasecmp(type->buf, "text/plain"))
189 return -1;
191 strbuf_trim(msg);
192 if (!msg->len)
193 return -1;
195 p = msg->buf;
196 do {
197 eol = strchrnul(p, '\n');
198 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
199 p = eol + 1;
200 } while(*eol);
201 return 0;
204 static struct discovery* discover_refs(const char *service, int for_push)
206 struct strbuf exp = STRBUF_INIT;
207 struct strbuf type = STRBUF_INIT;
208 struct strbuf buffer = STRBUF_INIT;
209 struct strbuf refs_url = STRBUF_INIT;
210 struct strbuf effective_url = STRBUF_INIT;
211 struct discovery *last = last_discovery;
212 int http_ret, maybe_smart = 0;
213 struct http_get_options options;
215 if (last && !strcmp(service, last->service))
216 return last;
217 free_discovery(last);
219 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
220 if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
221 git_env_bool("GIT_SMART_HTTP", 1)) {
222 maybe_smart = 1;
223 if (!strchr(url.buf, '?'))
224 strbuf_addch(&refs_url, '?');
225 else
226 strbuf_addch(&refs_url, '&');
227 strbuf_addf(&refs_url, "service=%s", service);
230 memset(&options, 0, sizeof(options));
231 options.content_type = &type;
232 options.effective_url = &effective_url;
233 options.base_url = &url;
234 options.no_cache = 1;
235 options.keep_error = 1;
237 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
238 switch (http_ret) {
239 case HTTP_OK:
240 break;
241 case HTTP_MISSING_TARGET:
242 show_http_message(&type, &buffer);
243 die("repository '%s' not found", url.buf);
244 case HTTP_NOAUTH:
245 show_http_message(&type, &buffer);
246 die("Authentication failed for '%s'", url.buf);
247 default:
248 show_http_message(&type, &buffer);
249 die("unable to access '%s': %s", url.buf, curl_errorstr);
252 last= xcalloc(1, sizeof(*last_discovery));
253 last->service = service;
254 last->buf_alloc = strbuf_detach(&buffer, &last->len);
255 last->buf = last->buf_alloc;
257 strbuf_addf(&exp, "application/x-%s-advertisement", service);
258 if (maybe_smart &&
259 (5 <= last->len && last->buf[4] == '#') &&
260 !strbuf_cmp(&exp, &type)) {
261 char *line;
264 * smart HTTP response; validate that the service
265 * pkt-line matches our request.
267 line = packet_read_line_buf(&last->buf, &last->len, NULL);
269 strbuf_reset(&exp);
270 strbuf_addf(&exp, "# service=%s", service);
271 if (strcmp(line, exp.buf))
272 die("invalid server response; got '%s'", line);
273 strbuf_release(&exp);
275 /* The header can include additional metadata lines, up
276 * until a packet flush marker. Ignore these now, but
277 * in the future we might start to scan them.
279 while (packet_read_line_buf(&last->buf, &last->len, NULL))
282 last->proto_git = 1;
285 if (last->proto_git)
286 last->refs = parse_git_refs(last, for_push);
287 else
288 last->refs = parse_info_refs(last);
290 strbuf_release(&refs_url);
291 strbuf_release(&exp);
292 strbuf_release(&type);
293 strbuf_release(&effective_url);
294 strbuf_release(&buffer);
295 last_discovery = last;
296 return last;
299 static struct ref *get_refs(int for_push)
301 struct discovery *heads;
303 if (for_push)
304 heads = discover_refs("git-receive-pack", for_push);
305 else
306 heads = discover_refs("git-upload-pack", for_push);
308 return heads->refs;
311 static void output_refs(struct ref *refs)
313 struct ref *posn;
314 for (posn = refs; posn; posn = posn->next) {
315 if (posn->symref)
316 printf("@%s %s\n", posn->symref, posn->name);
317 else
318 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
320 printf("\n");
321 fflush(stdout);
324 struct rpc_state {
325 const char *service_name;
326 const char **argv;
327 struct strbuf *stdin_preamble;
328 char *service_url;
329 char *hdr_content_type;
330 char *hdr_accept;
331 char *buf;
332 size_t alloc;
333 size_t len;
334 size_t pos;
335 int in;
336 int out;
337 struct strbuf result;
338 unsigned gzip_request : 1;
339 unsigned initial_buffer : 1;
342 static size_t rpc_out(void *ptr, size_t eltsize,
343 size_t nmemb, void *buffer_)
345 size_t max = eltsize * nmemb;
346 struct rpc_state *rpc = buffer_;
347 size_t avail = rpc->len - rpc->pos;
349 if (!avail) {
350 rpc->initial_buffer = 0;
351 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
352 if (!avail)
353 return 0;
354 rpc->pos = 0;
355 rpc->len = avail;
358 if (max < avail)
359 avail = max;
360 memcpy(ptr, rpc->buf + rpc->pos, avail);
361 rpc->pos += avail;
362 return avail;
365 #ifndef NO_CURL_IOCTL
366 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
368 struct rpc_state *rpc = clientp;
370 switch (cmd) {
371 case CURLIOCMD_NOP:
372 return CURLIOE_OK;
374 case CURLIOCMD_RESTARTREAD:
375 if (rpc->initial_buffer) {
376 rpc->pos = 0;
377 return CURLIOE_OK;
379 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
380 return CURLIOE_FAILRESTART;
382 default:
383 return CURLIOE_UNKNOWNCMD;
386 #endif
388 static size_t rpc_in(char *ptr, size_t eltsize,
389 size_t nmemb, void *buffer_)
391 size_t size = eltsize * nmemb;
392 struct rpc_state *rpc = buffer_;
393 write_or_die(rpc->in, ptr, size);
394 return size;
397 static int run_slot(struct active_request_slot *slot)
399 int err;
400 struct slot_results results;
402 slot->results = &results;
403 slot->curl_result = curl_easy_perform(slot->curl);
404 finish_active_slot(slot);
406 err = handle_curl_result(&results);
407 if (err != HTTP_OK && err != HTTP_REAUTH) {
408 error("RPC failed; result=%d, HTTP code = %ld",
409 results.curl_result, results.http_code);
412 return err;
415 static int probe_rpc(struct rpc_state *rpc)
417 struct active_request_slot *slot;
418 struct curl_slist *headers = NULL;
419 struct strbuf buf = STRBUF_INIT;
420 int err;
422 slot = get_active_slot();
424 headers = curl_slist_append(headers, rpc->hdr_content_type);
425 headers = curl_slist_append(headers, rpc->hdr_accept);
427 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
428 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
429 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
430 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
431 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
432 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
433 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
434 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
435 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
437 err = run_slot(slot);
439 curl_slist_free_all(headers);
440 strbuf_release(&buf);
441 return err;
444 static int post_rpc(struct rpc_state *rpc)
446 struct active_request_slot *slot;
447 struct curl_slist *headers = NULL;
448 int use_gzip = rpc->gzip_request;
449 char *gzip_body = NULL;
450 size_t gzip_size = 0;
451 int err, large_request = 0;
453 /* Try to load the entire request, if we can fit it into the
454 * allocated buffer space we can use HTTP/1.0 and avoid the
455 * chunked encoding mess.
457 while (1) {
458 size_t left = rpc->alloc - rpc->len;
459 char *buf = rpc->buf + rpc->len;
460 int n;
462 if (left < LARGE_PACKET_MAX) {
463 large_request = 1;
464 use_gzip = 0;
465 break;
468 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
469 if (!n)
470 break;
471 rpc->len += n;
474 if (large_request) {
475 do {
476 err = probe_rpc(rpc);
477 if (err == HTTP_REAUTH)
478 credential_fill(&http_auth);
479 } while (err == HTTP_REAUTH);
480 if (err != HTTP_OK)
481 return -1;
484 headers = curl_slist_append(headers, rpc->hdr_content_type);
485 headers = curl_slist_append(headers, rpc->hdr_accept);
486 headers = curl_slist_append(headers, "Expect:");
488 retry:
489 slot = get_active_slot();
491 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
492 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
493 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
494 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
496 if (large_request) {
497 /* The request body is large and the size cannot be predicted.
498 * We must use chunked encoding to send it.
500 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
501 rpc->initial_buffer = 1;
502 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
503 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
504 #ifndef NO_CURL_IOCTL
505 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
506 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
507 #endif
508 if (options.verbosity > 1) {
509 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
510 fflush(stderr);
513 } else if (gzip_body) {
515 * If we are looping to retry authentication, then the previous
516 * run will have set up the headers and gzip buffer already,
517 * and we just need to send it.
519 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
520 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
522 } else if (use_gzip && 1024 < rpc->len) {
523 /* The client backend isn't giving us compressed data so
524 * we can try to deflate it ourselves, this may save on.
525 * the transfer time.
527 git_zstream stream;
528 int ret;
530 memset(&stream, 0, sizeof(stream));
531 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
532 gzip_size = git_deflate_bound(&stream, rpc->len);
533 gzip_body = xmalloc(gzip_size);
535 stream.next_in = (unsigned char *)rpc->buf;
536 stream.avail_in = rpc->len;
537 stream.next_out = (unsigned char *)gzip_body;
538 stream.avail_out = gzip_size;
540 ret = git_deflate(&stream, Z_FINISH);
541 if (ret != Z_STREAM_END)
542 die("cannot deflate request; zlib deflate error %d", ret);
544 ret = git_deflate_end_gently(&stream);
545 if (ret != Z_OK)
546 die("cannot deflate request; zlib end error %d", ret);
548 gzip_size = stream.total_out;
550 headers = curl_slist_append(headers, "Content-Encoding: gzip");
551 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
552 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
554 if (options.verbosity > 1) {
555 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
556 rpc->service_name,
557 (unsigned long)rpc->len, (unsigned long)gzip_size);
558 fflush(stderr);
560 } else {
561 /* We know the complete request size in advance, use the
562 * more normal Content-Length approach.
564 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
565 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
566 if (options.verbosity > 1) {
567 fprintf(stderr, "POST %s (%lu bytes)\n",
568 rpc->service_name, (unsigned long)rpc->len);
569 fflush(stderr);
573 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
574 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
575 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
577 err = run_slot(slot);
578 if (err == HTTP_REAUTH && !large_request) {
579 credential_fill(&http_auth);
580 goto retry;
582 if (err != HTTP_OK)
583 err = -1;
585 curl_slist_free_all(headers);
586 free(gzip_body);
587 return err;
590 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
592 const char *svc = rpc->service_name;
593 struct strbuf buf = STRBUF_INIT;
594 struct strbuf *preamble = rpc->stdin_preamble;
595 struct child_process client;
596 int err = 0;
598 memset(&client, 0, sizeof(client));
599 client.in = -1;
600 client.out = -1;
601 client.git_cmd = 1;
602 client.argv = rpc->argv;
603 if (start_command(&client))
604 exit(1);
605 if (preamble)
606 write_or_die(client.in, preamble->buf, preamble->len);
607 if (heads)
608 write_or_die(client.in, heads->buf, heads->len);
610 rpc->alloc = http_post_buffer;
611 rpc->buf = xmalloc(rpc->alloc);
612 rpc->in = client.in;
613 rpc->out = client.out;
614 strbuf_init(&rpc->result, 0);
616 strbuf_addf(&buf, "%s%s", url.buf, svc);
617 rpc->service_url = strbuf_detach(&buf, NULL);
619 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
620 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
622 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
623 rpc->hdr_accept = strbuf_detach(&buf, NULL);
625 while (!err) {
626 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
627 if (!n)
628 break;
629 rpc->pos = 0;
630 rpc->len = n;
631 err |= post_rpc(rpc);
634 close(client.in);
635 client.in = -1;
636 if (!err) {
637 strbuf_read(&rpc->result, client.out, 0);
638 } else {
639 char buf[4096];
640 for (;;)
641 if (xread(client.out, buf, sizeof(buf)) <= 0)
642 break;
645 close(client.out);
646 client.out = -1;
648 err |= finish_command(&client);
649 free(rpc->service_url);
650 free(rpc->hdr_content_type);
651 free(rpc->hdr_accept);
652 free(rpc->buf);
653 strbuf_release(&buf);
654 return err;
657 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
659 struct walker *walker;
660 char **targets = xmalloc(nr_heads * sizeof(char*));
661 int ret, i;
663 if (options.depth)
664 die("dumb http transport does not support --depth");
665 for (i = 0; i < nr_heads; i++)
666 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
668 walker = get_http_walker(url.buf);
669 walker->get_all = 1;
670 walker->get_tree = 1;
671 walker->get_history = 1;
672 walker->get_verbosely = options.verbosity >= 3;
673 walker->get_recover = 0;
674 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
675 walker_free(walker);
677 for (i = 0; i < nr_heads; i++)
678 free(targets[i]);
679 free(targets);
681 return ret ? error("Fetch failed.") : 0;
684 static int fetch_git(struct discovery *heads,
685 int nr_heads, struct ref **to_fetch)
687 struct rpc_state rpc;
688 struct strbuf preamble = STRBUF_INIT;
689 char *depth_arg = NULL;
690 int argc = 0, i, err;
691 const char *argv[16];
693 argv[argc++] = "fetch-pack";
694 argv[argc++] = "--stateless-rpc";
695 argv[argc++] = "--stdin";
696 argv[argc++] = "--lock-pack";
697 if (options.followtags)
698 argv[argc++] = "--include-tag";
699 if (options.thin)
700 argv[argc++] = "--thin";
701 if (options.verbosity >= 3) {
702 argv[argc++] = "-v";
703 argv[argc++] = "-v";
705 if (options.check_self_contained_and_connected)
706 argv[argc++] = "--check-self-contained-and-connected";
707 if (!options.progress)
708 argv[argc++] = "--no-progress";
709 if (options.depth) {
710 struct strbuf buf = STRBUF_INIT;
711 strbuf_addf(&buf, "--depth=%lu", options.depth);
712 depth_arg = strbuf_detach(&buf, NULL);
713 argv[argc++] = depth_arg;
715 argv[argc++] = url.buf;
716 argv[argc++] = NULL;
718 for (i = 0; i < nr_heads; i++) {
719 struct ref *ref = to_fetch[i];
720 if (!ref->name || !*ref->name)
721 die("cannot fetch by sha1 over smart http");
722 packet_buf_write(&preamble, "%s\n", ref->name);
724 packet_buf_flush(&preamble);
726 memset(&rpc, 0, sizeof(rpc));
727 rpc.service_name = "git-upload-pack",
728 rpc.argv = argv;
729 rpc.stdin_preamble = &preamble;
730 rpc.gzip_request = 1;
732 err = rpc_service(&rpc, heads);
733 if (rpc.result.len)
734 write_or_die(1, rpc.result.buf, rpc.result.len);
735 strbuf_release(&rpc.result);
736 strbuf_release(&preamble);
737 free(depth_arg);
738 return err;
741 static int fetch(int nr_heads, struct ref **to_fetch)
743 struct discovery *d = discover_refs("git-upload-pack", 0);
744 if (d->proto_git)
745 return fetch_git(d, nr_heads, to_fetch);
746 else
747 return fetch_dumb(nr_heads, to_fetch);
750 static void parse_fetch(struct strbuf *buf)
752 struct ref **to_fetch = NULL;
753 struct ref *list_head = NULL;
754 struct ref **list = &list_head;
755 int alloc_heads = 0, nr_heads = 0;
757 do {
758 if (!prefixcmp(buf->buf, "fetch ")) {
759 char *p = buf->buf + strlen("fetch ");
760 char *name;
761 struct ref *ref;
762 unsigned char old_sha1[20];
764 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
765 die("protocol error: expected sha/ref, got %s'", p);
766 if (p[40] == ' ')
767 name = p + 41;
768 else if (!p[40])
769 name = "";
770 else
771 die("protocol error: expected sha/ref, got %s'", p);
773 ref = alloc_ref(name);
774 hashcpy(ref->old_sha1, old_sha1);
776 *list = ref;
777 list = &ref->next;
779 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
780 to_fetch[nr_heads++] = ref;
782 else
783 die("http transport does not support %s", buf->buf);
785 strbuf_reset(buf);
786 if (strbuf_getline(buf, stdin, '\n') == EOF)
787 return;
788 if (!*buf->buf)
789 break;
790 } while (1);
792 if (fetch(nr_heads, to_fetch))
793 exit(128); /* error already reported */
794 free_refs(list_head);
795 free(to_fetch);
797 printf("\n");
798 fflush(stdout);
799 strbuf_reset(buf);
802 static int push_dav(int nr_spec, char **specs)
804 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
805 int argc = 0, i;
807 argv[argc++] = "http-push";
808 argv[argc++] = "--helper-status";
809 if (options.dry_run)
810 argv[argc++] = "--dry-run";
811 if (options.verbosity > 1)
812 argv[argc++] = "--verbose";
813 argv[argc++] = url.buf;
814 for (i = 0; i < nr_spec; i++)
815 argv[argc++] = specs[i];
816 argv[argc++] = NULL;
818 if (run_command_v_opt(argv, RUN_GIT_CMD))
819 die("git-%s failed", argv[0]);
820 free(argv);
821 return 0;
824 static int push_git(struct discovery *heads, int nr_spec, char **specs)
826 struct rpc_state rpc;
827 int i, err;
828 struct argv_array args;
829 struct string_list_item *cas_option;
831 argv_array_init(&args);
832 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
833 NULL);
835 if (options.thin)
836 argv_array_push(&args, "--thin");
837 if (options.dry_run)
838 argv_array_push(&args, "--dry-run");
839 if (options.verbosity == 0)
840 argv_array_push(&args, "--quiet");
841 else if (options.verbosity > 1)
842 argv_array_push(&args, "--verbose");
843 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
844 for_each_string_list_item(cas_option, &cas_options)
845 argv_array_push(&args, cas_option->string);
846 argv_array_push(&args, url.buf);
847 for (i = 0; i < nr_spec; i++)
848 argv_array_push(&args, specs[i]);
850 memset(&rpc, 0, sizeof(rpc));
851 rpc.service_name = "git-receive-pack",
852 rpc.argv = args.argv;
854 err = rpc_service(&rpc, heads);
855 if (rpc.result.len)
856 write_or_die(1, rpc.result.buf, rpc.result.len);
857 strbuf_release(&rpc.result);
858 argv_array_clear(&args);
859 return err;
862 static int push(int nr_spec, char **specs)
864 struct discovery *heads = discover_refs("git-receive-pack", 1);
865 int ret;
867 if (heads->proto_git)
868 ret = push_git(heads, nr_spec, specs);
869 else
870 ret = push_dav(nr_spec, specs);
871 free_discovery(heads);
872 return ret;
875 static void parse_push(struct strbuf *buf)
877 char **specs = NULL;
878 int alloc_spec = 0, nr_spec = 0, i, ret;
880 do {
881 if (!prefixcmp(buf->buf, "push ")) {
882 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
883 specs[nr_spec++] = xstrdup(buf->buf + 5);
885 else
886 die("http transport does not support %s", buf->buf);
888 strbuf_reset(buf);
889 if (strbuf_getline(buf, stdin, '\n') == EOF)
890 goto free_specs;
891 if (!*buf->buf)
892 break;
893 } while (1);
895 ret = push(nr_spec, specs);
896 printf("\n");
897 fflush(stdout);
899 if (ret)
900 exit(128); /* error already reported */
902 free_specs:
903 for (i = 0; i < nr_spec; i++)
904 free(specs[i]);
905 free(specs);
908 int main(int argc, const char **argv)
910 struct strbuf buf = STRBUF_INIT;
911 int nongit;
913 git_extract_argv0_path(argv[0]);
914 setup_git_directory_gently(&nongit);
915 if (argc < 2) {
916 fprintf(stderr, "Remote needed\n");
917 return 1;
920 options.verbosity = 1;
921 options.progress = !!isatty(2);
922 options.thin = 1;
924 remote = remote_get(argv[1]);
926 if (argc > 2) {
927 end_url_with_slash(&url, argv[2]);
928 } else {
929 end_url_with_slash(&url, remote->url[0]);
932 http_init(remote, url.buf, 0);
934 do {
935 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
936 if (ferror(stdin))
937 fprintf(stderr, "Error reading command stream\n");
938 else
939 fprintf(stderr, "Unexpected end of command stream\n");
940 return 1;
942 if (buf.len == 0)
943 break;
944 if (!prefixcmp(buf.buf, "fetch ")) {
945 if (nongit)
946 die("Fetch attempted without a local repo");
947 parse_fetch(&buf);
949 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
950 int for_push = !!strstr(buf.buf + 4, "for-push");
951 output_refs(get_refs(for_push));
953 } else if (!prefixcmp(buf.buf, "push ")) {
954 parse_push(&buf);
956 } else if (!prefixcmp(buf.buf, "option ")) {
957 char *name = buf.buf + strlen("option ");
958 char *value = strchr(name, ' ');
959 int result;
961 if (value)
962 *value++ = '\0';
963 else
964 value = "true";
966 result = set_option(name, value);
967 if (!result)
968 printf("ok\n");
969 else if (result < 0)
970 printf("error invalid value\n");
971 else
972 printf("unsupported\n");
973 fflush(stdout);
975 } else if (!strcmp(buf.buf, "capabilities")) {
976 printf("fetch\n");
977 printf("option\n");
978 printf("push\n");
979 printf("check-connectivity\n");
980 printf("\n");
981 fflush(stdout);
982 } else {
983 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
984 return 1;
986 strbuf_reset(&buf);
987 } while (1);
989 http_cleanup();
991 return 0;