http: update base URLs when we see redirects
[git/gitweb.git] / remote-curl.c
blob8ffd7ff7a7189ce2022a49ef808b43024b04dcc2
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 "sideband.h"
10 #include "argv-array.h"
11 #include "credential.h"
13 static struct remote *remote;
14 static const char *url; /* always ends with a trailing slash */
16 struct options {
17 int verbosity;
18 unsigned long depth;
19 unsigned progress : 1,
20 followtags : 1,
21 dry_run : 1,
22 thin : 1;
24 static struct options options;
26 static int set_option(const char *name, const char *value)
28 if (!strcmp(name, "verbosity")) {
29 char *end;
30 int v = strtol(value, &end, 10);
31 if (value == end || *end)
32 return -1;
33 options.verbosity = v;
34 return 0;
36 else if (!strcmp(name, "progress")) {
37 if (!strcmp(value, "true"))
38 options.progress = 1;
39 else if (!strcmp(value, "false"))
40 options.progress = 0;
41 else
42 return -1;
43 return 0;
45 else if (!strcmp(name, "depth")) {
46 char *end;
47 unsigned long v = strtoul(value, &end, 10);
48 if (value == end || *end)
49 return -1;
50 options.depth = v;
51 return 0;
53 else if (!strcmp(name, "followtags")) {
54 if (!strcmp(value, "true"))
55 options.followtags = 1;
56 else if (!strcmp(value, "false"))
57 options.followtags = 0;
58 else
59 return -1;
60 return 0;
62 else if (!strcmp(name, "dry-run")) {
63 if (!strcmp(value, "true"))
64 options.dry_run = 1;
65 else if (!strcmp(value, "false"))
66 options.dry_run = 0;
67 else
68 return -1;
69 return 0;
71 else {
72 return 1 /* unsupported */;
76 struct discovery {
77 const char *service;
78 char *buf_alloc;
79 char *buf;
80 size_t len;
81 struct ref *refs;
82 unsigned proto_git : 1;
84 static struct discovery *last_discovery;
86 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
88 struct ref *list = NULL;
89 get_remote_heads(-1, heads->buf, heads->len, &list,
90 for_push ? REF_NORMAL : 0, NULL);
91 return list;
94 static struct ref *parse_info_refs(struct discovery *heads)
96 char *data, *start, *mid;
97 char *ref_name;
98 int i = 0;
100 struct ref *refs = NULL;
101 struct ref *ref = NULL;
102 struct ref *last_ref = NULL;
104 data = heads->buf;
105 start = NULL;
106 mid = data;
107 while (i < heads->len) {
108 if (!start) {
109 start = &data[i];
111 if (data[i] == '\t')
112 mid = &data[i];
113 if (data[i] == '\n') {
114 if (mid - start != 40)
115 die("%sinfo/refs not valid: is this a git repository?", url);
116 data[i] = 0;
117 ref_name = mid + 1;
118 ref = xmalloc(sizeof(struct ref) +
119 strlen(ref_name) + 1);
120 memset(ref, 0, sizeof(struct ref));
121 strcpy(ref->name, ref_name);
122 get_sha1_hex(start, ref->old_sha1);
123 if (!refs)
124 refs = ref;
125 if (last_ref)
126 last_ref->next = ref;
127 last_ref = ref;
128 start = NULL;
130 i++;
133 ref = alloc_ref("HEAD");
134 if (!http_fetch_ref(url, ref) &&
135 !resolve_remote_symref(ref, refs)) {
136 ref->next = refs;
137 refs = ref;
138 } else {
139 free(ref);
142 return refs;
145 static void free_discovery(struct discovery *d)
147 if (d) {
148 if (d == last_discovery)
149 last_discovery = NULL;
150 free(d->buf_alloc);
151 free_refs(d->refs);
152 free(d);
156 static int show_http_message(struct strbuf *type, struct strbuf *msg)
158 const char *p, *eol;
161 * We only show text/plain parts, as other types are likely
162 * to be ugly to look at on the user's terminal.
164 * TODO should handle "; charset=XXX", and re-encode into
165 * logoutputencoding
167 if (strcasecmp(type->buf, "text/plain"))
168 return -1;
170 strbuf_trim(msg);
171 if (!msg->len)
172 return -1;
174 p = msg->buf;
175 do {
176 eol = strchrnul(p, '\n');
177 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
178 p = eol + 1;
179 } while(*eol);
180 return 0;
183 static struct discovery* discover_refs(const char *service, int for_push)
185 struct strbuf exp = STRBUF_INIT;
186 struct strbuf type = STRBUF_INIT;
187 struct strbuf buffer = STRBUF_INIT;
188 struct discovery *last = last_discovery;
189 char *refs_url;
190 int http_ret, maybe_smart = 0;
191 struct http_get_options options;
193 if (last && !strcmp(service, last->service))
194 return last;
195 free_discovery(last);
197 strbuf_addf(&buffer, "%sinfo/refs", url);
198 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
199 git_env_bool("GIT_SMART_HTTP", 1)) {
200 maybe_smart = 1;
201 if (!strchr(url, '?'))
202 strbuf_addch(&buffer, '?');
203 else
204 strbuf_addch(&buffer, '&');
205 strbuf_addf(&buffer, "service=%s", service);
207 refs_url = strbuf_detach(&buffer, NULL);
209 memset(&options, 0, sizeof(options));
210 options.content_type = &type;
211 options.no_cache = 1;
212 options.keep_error = 1;
214 http_ret = http_get_strbuf(refs_url, &buffer, &options);
215 switch (http_ret) {
216 case HTTP_OK:
217 break;
218 case HTTP_MISSING_TARGET:
219 show_http_message(&type, &buffer);
220 die("repository '%s' not found", url);
221 case HTTP_NOAUTH:
222 show_http_message(&type, &buffer);
223 die("Authentication failed for '%s'", url);
224 default:
225 show_http_message(&type, &buffer);
226 die("unable to access '%s': %s", url, curl_errorstr);
229 last= xcalloc(1, sizeof(*last_discovery));
230 last->service = service;
231 last->buf_alloc = strbuf_detach(&buffer, &last->len);
232 last->buf = last->buf_alloc;
234 strbuf_addf(&exp, "application/x-%s-advertisement", service);
235 if (maybe_smart &&
236 (5 <= last->len && last->buf[4] == '#') &&
237 !strbuf_cmp(&exp, &type)) {
238 char *line;
241 * smart HTTP response; validate that the service
242 * pkt-line matches our request.
244 line = packet_read_line_buf(&last->buf, &last->len, NULL);
246 strbuf_reset(&exp);
247 strbuf_addf(&exp, "# service=%s", service);
248 if (strcmp(line, exp.buf))
249 die("invalid server response; got '%s'", line);
250 strbuf_release(&exp);
252 /* The header can include additional metadata lines, up
253 * until a packet flush marker. Ignore these now, but
254 * in the future we might start to scan them.
256 while (packet_read_line_buf(&last->buf, &last->len, NULL))
259 last->proto_git = 1;
262 if (last->proto_git)
263 last->refs = parse_git_refs(last, for_push);
264 else
265 last->refs = parse_info_refs(last);
267 free(refs_url);
268 strbuf_release(&exp);
269 strbuf_release(&type);
270 strbuf_release(&buffer);
271 last_discovery = last;
272 return last;
275 static struct ref *get_refs(int for_push)
277 struct discovery *heads;
279 if (for_push)
280 heads = discover_refs("git-receive-pack", for_push);
281 else
282 heads = discover_refs("git-upload-pack", for_push);
284 return heads->refs;
287 static void output_refs(struct ref *refs)
289 struct ref *posn;
290 for (posn = refs; posn; posn = posn->next) {
291 if (posn->symref)
292 printf("@%s %s\n", posn->symref, posn->name);
293 else
294 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
296 printf("\n");
297 fflush(stdout);
300 struct rpc_state {
301 const char *service_name;
302 const char **argv;
303 struct strbuf *stdin_preamble;
304 char *service_url;
305 char *hdr_content_type;
306 char *hdr_accept;
307 char *buf;
308 size_t alloc;
309 size_t len;
310 size_t pos;
311 int in;
312 int out;
313 struct strbuf result;
314 unsigned gzip_request : 1;
315 unsigned initial_buffer : 1;
318 static size_t rpc_out(void *ptr, size_t eltsize,
319 size_t nmemb, void *buffer_)
321 size_t max = eltsize * nmemb;
322 struct rpc_state *rpc = buffer_;
323 size_t avail = rpc->len - rpc->pos;
325 if (!avail) {
326 rpc->initial_buffer = 0;
327 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
328 if (!avail)
329 return 0;
330 rpc->pos = 0;
331 rpc->len = avail;
334 if (max < avail)
335 avail = max;
336 memcpy(ptr, rpc->buf + rpc->pos, avail);
337 rpc->pos += avail;
338 return avail;
341 #ifndef NO_CURL_IOCTL
342 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
344 struct rpc_state *rpc = clientp;
346 switch (cmd) {
347 case CURLIOCMD_NOP:
348 return CURLIOE_OK;
350 case CURLIOCMD_RESTARTREAD:
351 if (rpc->initial_buffer) {
352 rpc->pos = 0;
353 return CURLIOE_OK;
355 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
356 return CURLIOE_FAILRESTART;
358 default:
359 return CURLIOE_UNKNOWNCMD;
362 #endif
364 static size_t rpc_in(char *ptr, size_t eltsize,
365 size_t nmemb, void *buffer_)
367 size_t size = eltsize * nmemb;
368 struct rpc_state *rpc = buffer_;
369 write_or_die(rpc->in, ptr, size);
370 return size;
373 static int run_slot(struct active_request_slot *slot)
375 int err;
376 struct slot_results results;
378 slot->results = &results;
379 slot->curl_result = curl_easy_perform(slot->curl);
380 finish_active_slot(slot);
382 err = handle_curl_result(&results);
383 if (err != HTTP_OK && err != HTTP_REAUTH) {
384 error("RPC failed; result=%d, HTTP code = %ld",
385 results.curl_result, results.http_code);
388 return err;
391 static int probe_rpc(struct rpc_state *rpc)
393 struct active_request_slot *slot;
394 struct curl_slist *headers = NULL;
395 struct strbuf buf = STRBUF_INIT;
396 int err;
398 slot = get_active_slot();
400 headers = curl_slist_append(headers, rpc->hdr_content_type);
401 headers = curl_slist_append(headers, rpc->hdr_accept);
403 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
404 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
405 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
406 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
407 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
408 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
409 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
410 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
411 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
413 err = run_slot(slot);
415 curl_slist_free_all(headers);
416 strbuf_release(&buf);
417 return err;
420 static int post_rpc(struct rpc_state *rpc)
422 struct active_request_slot *slot;
423 struct curl_slist *headers = NULL;
424 int use_gzip = rpc->gzip_request;
425 char *gzip_body = NULL;
426 size_t gzip_size = 0;
427 int err, large_request = 0;
429 /* Try to load the entire request, if we can fit it into the
430 * allocated buffer space we can use HTTP/1.0 and avoid the
431 * chunked encoding mess.
433 while (1) {
434 size_t left = rpc->alloc - rpc->len;
435 char *buf = rpc->buf + rpc->len;
436 int n;
438 if (left < LARGE_PACKET_MAX) {
439 large_request = 1;
440 use_gzip = 0;
441 break;
444 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
445 if (!n)
446 break;
447 rpc->len += n;
450 if (large_request) {
451 do {
452 err = probe_rpc(rpc);
453 if (err == HTTP_REAUTH)
454 credential_fill(&http_auth);
455 } while (err == HTTP_REAUTH);
456 if (err != HTTP_OK)
457 return -1;
460 headers = curl_slist_append(headers, rpc->hdr_content_type);
461 headers = curl_slist_append(headers, rpc->hdr_accept);
462 headers = curl_slist_append(headers, "Expect:");
464 retry:
465 slot = get_active_slot();
467 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
468 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
469 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
470 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
472 if (large_request) {
473 /* The request body is large and the size cannot be predicted.
474 * We must use chunked encoding to send it.
476 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
477 rpc->initial_buffer = 1;
478 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
479 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
480 #ifndef NO_CURL_IOCTL
481 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
482 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
483 #endif
484 if (options.verbosity > 1) {
485 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
486 fflush(stderr);
489 } else if (gzip_body) {
491 * If we are looping to retry authentication, then the previous
492 * run will have set up the headers and gzip buffer already,
493 * and we just need to send it.
495 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
496 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
498 } else if (use_gzip && 1024 < rpc->len) {
499 /* The client backend isn't giving us compressed data so
500 * we can try to deflate it ourselves, this may save on.
501 * the transfer time.
503 git_zstream stream;
504 int ret;
506 memset(&stream, 0, sizeof(stream));
507 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
508 gzip_size = git_deflate_bound(&stream, rpc->len);
509 gzip_body = xmalloc(gzip_size);
511 stream.next_in = (unsigned char *)rpc->buf;
512 stream.avail_in = rpc->len;
513 stream.next_out = (unsigned char *)gzip_body;
514 stream.avail_out = gzip_size;
516 ret = git_deflate(&stream, Z_FINISH);
517 if (ret != Z_STREAM_END)
518 die("cannot deflate request; zlib deflate error %d", ret);
520 ret = git_deflate_end_gently(&stream);
521 if (ret != Z_OK)
522 die("cannot deflate request; zlib end error %d", ret);
524 gzip_size = stream.total_out;
526 headers = curl_slist_append(headers, "Content-Encoding: gzip");
527 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
528 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
530 if (options.verbosity > 1) {
531 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
532 rpc->service_name,
533 (unsigned long)rpc->len, (unsigned long)gzip_size);
534 fflush(stderr);
536 } else {
537 /* We know the complete request size in advance, use the
538 * more normal Content-Length approach.
540 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
541 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
542 if (options.verbosity > 1) {
543 fprintf(stderr, "POST %s (%lu bytes)\n",
544 rpc->service_name, (unsigned long)rpc->len);
545 fflush(stderr);
549 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
550 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
551 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
553 err = run_slot(slot);
554 if (err == HTTP_REAUTH && !large_request) {
555 credential_fill(&http_auth);
556 goto retry;
558 if (err != HTTP_OK)
559 err = -1;
561 curl_slist_free_all(headers);
562 free(gzip_body);
563 return err;
566 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
568 const char *svc = rpc->service_name;
569 struct strbuf buf = STRBUF_INIT;
570 struct strbuf *preamble = rpc->stdin_preamble;
571 struct child_process client;
572 int err = 0;
574 memset(&client, 0, sizeof(client));
575 client.in = -1;
576 client.out = -1;
577 client.git_cmd = 1;
578 client.argv = rpc->argv;
579 if (start_command(&client))
580 exit(1);
581 if (preamble)
582 write_or_die(client.in, preamble->buf, preamble->len);
583 if (heads)
584 write_or_die(client.in, heads->buf, heads->len);
586 rpc->alloc = http_post_buffer;
587 rpc->buf = xmalloc(rpc->alloc);
588 rpc->in = client.in;
589 rpc->out = client.out;
590 strbuf_init(&rpc->result, 0);
592 strbuf_addf(&buf, "%s%s", url, svc);
593 rpc->service_url = strbuf_detach(&buf, NULL);
595 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
596 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
598 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
599 rpc->hdr_accept = strbuf_detach(&buf, NULL);
601 while (!err) {
602 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
603 if (!n)
604 break;
605 rpc->pos = 0;
606 rpc->len = n;
607 err |= post_rpc(rpc);
610 close(client.in);
611 client.in = -1;
612 if (!err) {
613 strbuf_read(&rpc->result, client.out, 0);
614 } else {
615 char buf[4096];
616 for (;;)
617 if (xread(client.out, buf, sizeof(buf)) <= 0)
618 break;
621 close(client.out);
622 client.out = -1;
624 err |= finish_command(&client);
625 free(rpc->service_url);
626 free(rpc->hdr_content_type);
627 free(rpc->hdr_accept);
628 free(rpc->buf);
629 strbuf_release(&buf);
630 return err;
633 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
635 struct walker *walker;
636 char **targets = xmalloc(nr_heads * sizeof(char*));
637 int ret, i;
639 if (options.depth)
640 die("dumb http transport does not support --depth");
641 for (i = 0; i < nr_heads; i++)
642 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
644 walker = get_http_walker(url);
645 walker->get_all = 1;
646 walker->get_tree = 1;
647 walker->get_history = 1;
648 walker->get_verbosely = options.verbosity >= 3;
649 walker->get_recover = 0;
650 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
651 walker_free(walker);
653 for (i = 0; i < nr_heads; i++)
654 free(targets[i]);
655 free(targets);
657 return ret ? error("Fetch failed.") : 0;
660 static int fetch_git(struct discovery *heads,
661 int nr_heads, struct ref **to_fetch)
663 struct rpc_state rpc;
664 struct strbuf preamble = STRBUF_INIT;
665 char *depth_arg = NULL;
666 int argc = 0, i, err;
667 const char *argv[15];
669 argv[argc++] = "fetch-pack";
670 argv[argc++] = "--stateless-rpc";
671 argv[argc++] = "--stdin";
672 argv[argc++] = "--lock-pack";
673 if (options.followtags)
674 argv[argc++] = "--include-tag";
675 if (options.thin)
676 argv[argc++] = "--thin";
677 if (options.verbosity >= 3) {
678 argv[argc++] = "-v";
679 argv[argc++] = "-v";
681 if (!options.progress)
682 argv[argc++] = "--no-progress";
683 if (options.depth) {
684 struct strbuf buf = STRBUF_INIT;
685 strbuf_addf(&buf, "--depth=%lu", options.depth);
686 depth_arg = strbuf_detach(&buf, NULL);
687 argv[argc++] = depth_arg;
689 argv[argc++] = url;
690 argv[argc++] = NULL;
692 for (i = 0; i < nr_heads; i++) {
693 struct ref *ref = to_fetch[i];
694 if (!ref->name || !*ref->name)
695 die("cannot fetch by sha1 over smart http");
696 packet_buf_write(&preamble, "%s\n", ref->name);
698 packet_buf_flush(&preamble);
700 memset(&rpc, 0, sizeof(rpc));
701 rpc.service_name = "git-upload-pack",
702 rpc.argv = argv;
703 rpc.stdin_preamble = &preamble;
704 rpc.gzip_request = 1;
706 err = rpc_service(&rpc, heads);
707 if (rpc.result.len)
708 write_or_die(1, rpc.result.buf, rpc.result.len);
709 strbuf_release(&rpc.result);
710 strbuf_release(&preamble);
711 free(depth_arg);
712 return err;
715 static int fetch(int nr_heads, struct ref **to_fetch)
717 struct discovery *d = discover_refs("git-upload-pack", 0);
718 if (d->proto_git)
719 return fetch_git(d, nr_heads, to_fetch);
720 else
721 return fetch_dumb(nr_heads, to_fetch);
724 static void parse_fetch(struct strbuf *buf)
726 struct ref **to_fetch = NULL;
727 struct ref *list_head = NULL;
728 struct ref **list = &list_head;
729 int alloc_heads = 0, nr_heads = 0;
731 do {
732 if (!prefixcmp(buf->buf, "fetch ")) {
733 char *p = buf->buf + strlen("fetch ");
734 char *name;
735 struct ref *ref;
736 unsigned char old_sha1[20];
738 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
739 die("protocol error: expected sha/ref, got %s'", p);
740 if (p[40] == ' ')
741 name = p + 41;
742 else if (!p[40])
743 name = "";
744 else
745 die("protocol error: expected sha/ref, got %s'", p);
747 ref = alloc_ref(name);
748 hashcpy(ref->old_sha1, old_sha1);
750 *list = ref;
751 list = &ref->next;
753 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
754 to_fetch[nr_heads++] = ref;
756 else
757 die("http transport does not support %s", buf->buf);
759 strbuf_reset(buf);
760 if (strbuf_getline(buf, stdin, '\n') == EOF)
761 return;
762 if (!*buf->buf)
763 break;
764 } while (1);
766 if (fetch(nr_heads, to_fetch))
767 exit(128); /* error already reported */
768 free_refs(list_head);
769 free(to_fetch);
771 printf("\n");
772 fflush(stdout);
773 strbuf_reset(buf);
776 static int push_dav(int nr_spec, char **specs)
778 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
779 int argc = 0, i;
781 argv[argc++] = "http-push";
782 argv[argc++] = "--helper-status";
783 if (options.dry_run)
784 argv[argc++] = "--dry-run";
785 if (options.verbosity > 1)
786 argv[argc++] = "--verbose";
787 argv[argc++] = url;
788 for (i = 0; i < nr_spec; i++)
789 argv[argc++] = specs[i];
790 argv[argc++] = NULL;
792 if (run_command_v_opt(argv, RUN_GIT_CMD))
793 die("git-%s failed", argv[0]);
794 free(argv);
795 return 0;
798 static int push_git(struct discovery *heads, int nr_spec, char **specs)
800 struct rpc_state rpc;
801 int i, err;
802 struct argv_array args;
804 argv_array_init(&args);
805 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
806 NULL);
808 if (options.thin)
809 argv_array_push(&args, "--thin");
810 if (options.dry_run)
811 argv_array_push(&args, "--dry-run");
812 if (options.verbosity == 0)
813 argv_array_push(&args, "--quiet");
814 else if (options.verbosity > 1)
815 argv_array_push(&args, "--verbose");
816 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
817 argv_array_push(&args, url);
818 for (i = 0; i < nr_spec; i++)
819 argv_array_push(&args, specs[i]);
821 memset(&rpc, 0, sizeof(rpc));
822 rpc.service_name = "git-receive-pack",
823 rpc.argv = args.argv;
825 err = rpc_service(&rpc, heads);
826 if (rpc.result.len)
827 write_or_die(1, rpc.result.buf, rpc.result.len);
828 strbuf_release(&rpc.result);
829 argv_array_clear(&args);
830 return err;
833 static int push(int nr_spec, char **specs)
835 struct discovery *heads = discover_refs("git-receive-pack", 1);
836 int ret;
838 if (heads->proto_git)
839 ret = push_git(heads, nr_spec, specs);
840 else
841 ret = push_dav(nr_spec, specs);
842 free_discovery(heads);
843 return ret;
846 static void parse_push(struct strbuf *buf)
848 char **specs = NULL;
849 int alloc_spec = 0, nr_spec = 0, i, ret;
851 do {
852 if (!prefixcmp(buf->buf, "push ")) {
853 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
854 specs[nr_spec++] = xstrdup(buf->buf + 5);
856 else
857 die("http transport does not support %s", buf->buf);
859 strbuf_reset(buf);
860 if (strbuf_getline(buf, stdin, '\n') == EOF)
861 goto free_specs;
862 if (!*buf->buf)
863 break;
864 } while (1);
866 ret = push(nr_spec, specs);
867 printf("\n");
868 fflush(stdout);
870 if (ret)
871 exit(128); /* error already reported */
873 free_specs:
874 for (i = 0; i < nr_spec; i++)
875 free(specs[i]);
876 free(specs);
879 int main(int argc, const char **argv)
881 struct strbuf buf = STRBUF_INIT;
882 int nongit;
884 git_extract_argv0_path(argv[0]);
885 setup_git_directory_gently(&nongit);
886 if (argc < 2) {
887 fprintf(stderr, "Remote needed\n");
888 return 1;
891 options.verbosity = 1;
892 options.progress = !!isatty(2);
893 options.thin = 1;
895 remote = remote_get(argv[1]);
897 if (argc > 2) {
898 end_url_with_slash(&buf, argv[2]);
899 } else {
900 end_url_with_slash(&buf, remote->url[0]);
903 url = strbuf_detach(&buf, NULL);
905 http_init(remote, url, 0);
907 do {
908 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
909 if (ferror(stdin))
910 fprintf(stderr, "Error reading command stream\n");
911 else
912 fprintf(stderr, "Unexpected end of command stream\n");
913 return 1;
915 if (buf.len == 0)
916 break;
917 if (!prefixcmp(buf.buf, "fetch ")) {
918 if (nongit)
919 die("Fetch attempted without a local repo");
920 parse_fetch(&buf);
922 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
923 int for_push = !!strstr(buf.buf + 4, "for-push");
924 output_refs(get_refs(for_push));
926 } else if (!prefixcmp(buf.buf, "push ")) {
927 parse_push(&buf);
929 } else if (!prefixcmp(buf.buf, "option ")) {
930 char *name = buf.buf + strlen("option ");
931 char *value = strchr(name, ' ');
932 int result;
934 if (value)
935 *value++ = '\0';
936 else
937 value = "true";
939 result = set_option(name, value);
940 if (!result)
941 printf("ok\n");
942 else if (result < 0)
943 printf("error invalid value\n");
944 else
945 printf("unsupported\n");
946 fflush(stdout);
948 } else if (!strcmp(buf.buf, "capabilities")) {
949 printf("fetch\n");
950 printf("option\n");
951 printf("push\n");
952 printf("\n");
953 fflush(stdout);
954 } else {
955 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
956 return 1;
958 strbuf_reset(&buf);
959 } while (1);
961 http_cleanup();
963 return 0;