merge-base: separate "--independent" codepath into its own helper
[git/git-svn.git] / remote-curl.c
blobef1684b9df3ee5cff0d63ab5f7ed51e7f351a686
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 /* always ends with a trailing slash */
15 static struct strbuf url = STRBUF_INIT;
17 struct options {
18 int verbosity;
19 unsigned long depth;
20 unsigned progress : 1,
21 followtags : 1,
22 dry_run : 1,
23 thin : 1;
25 static struct options options;
27 static int set_option(const char *name, const char *value)
29 if (!strcmp(name, "verbosity")) {
30 char *end;
31 int v = strtol(value, &end, 10);
32 if (value == end || *end)
33 return -1;
34 options.verbosity = v;
35 return 0;
37 else if (!strcmp(name, "progress")) {
38 if (!strcmp(value, "true"))
39 options.progress = 1;
40 else if (!strcmp(value, "false"))
41 options.progress = 0;
42 else
43 return -1;
44 return 0;
46 else if (!strcmp(name, "depth")) {
47 char *end;
48 unsigned long v = strtoul(value, &end, 10);
49 if (value == end || *end)
50 return -1;
51 options.depth = v;
52 return 0;
54 else if (!strcmp(name, "followtags")) {
55 if (!strcmp(value, "true"))
56 options.followtags = 1;
57 else if (!strcmp(value, "false"))
58 options.followtags = 0;
59 else
60 return -1;
61 return 0;
63 else if (!strcmp(name, "dry-run")) {
64 if (!strcmp(value, "true"))
65 options.dry_run = 1;
66 else if (!strcmp(value, "false"))
67 options.dry_run = 0;
68 else
69 return -1;
70 return 0;
72 else {
73 return 1 /* unsupported */;
77 struct discovery {
78 const char *service;
79 char *buf_alloc;
80 char *buf;
81 size_t len;
82 struct ref *refs;
83 unsigned proto_git : 1;
85 static struct discovery *last_discovery;
87 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
89 struct ref *list = NULL;
90 get_remote_heads(-1, heads->buf, heads->len, &list,
91 for_push ? REF_NORMAL : 0, NULL);
92 return list;
95 static struct ref *parse_info_refs(struct discovery *heads)
97 char *data, *start, *mid;
98 char *ref_name;
99 int i = 0;
101 struct ref *refs = NULL;
102 struct ref *ref = NULL;
103 struct ref *last_ref = NULL;
105 data = heads->buf;
106 start = NULL;
107 mid = data;
108 while (i < heads->len) {
109 if (!start) {
110 start = &data[i];
112 if (data[i] == '\t')
113 mid = &data[i];
114 if (data[i] == '\n') {
115 if (mid - start != 40)
116 die("%sinfo/refs not valid: is this a git repository?",
117 url.buf);
118 data[i] = 0;
119 ref_name = mid + 1;
120 ref = xmalloc(sizeof(struct ref) +
121 strlen(ref_name) + 1);
122 memset(ref, 0, sizeof(struct ref));
123 strcpy(ref->name, ref_name);
124 get_sha1_hex(start, ref->old_sha1);
125 if (!refs)
126 refs = ref;
127 if (last_ref)
128 last_ref->next = ref;
129 last_ref = ref;
130 start = NULL;
132 i++;
135 ref = alloc_ref("HEAD");
136 if (!http_fetch_ref(url.buf, ref) &&
137 !resolve_remote_symref(ref, refs)) {
138 ref->next = refs;
139 refs = ref;
140 } else {
141 free(ref);
144 return refs;
147 static void free_discovery(struct discovery *d)
149 if (d) {
150 if (d == last_discovery)
151 last_discovery = NULL;
152 free(d->buf_alloc);
153 free_refs(d->refs);
154 free(d);
158 static int show_http_message(struct strbuf *type, struct strbuf *msg)
160 const char *p, *eol;
163 * We only show text/plain parts, as other types are likely
164 * to be ugly to look at on the user's terminal.
166 * TODO should handle "; charset=XXX", and re-encode into
167 * logoutputencoding
169 if (strcasecmp(type->buf, "text/plain"))
170 return -1;
172 strbuf_trim(msg);
173 if (!msg->len)
174 return -1;
176 p = msg->buf;
177 do {
178 eol = strchrnul(p, '\n');
179 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
180 p = eol + 1;
181 } while(*eol);
182 return 0;
185 static struct discovery* discover_refs(const char *service, int for_push)
187 struct strbuf exp = STRBUF_INIT;
188 struct strbuf type = STRBUF_INIT;
189 struct strbuf buffer = STRBUF_INIT;
190 struct strbuf refs_url = STRBUF_INIT;
191 struct strbuf effective_url = STRBUF_INIT;
192 struct discovery *last = last_discovery;
193 int http_ret, maybe_smart = 0;
194 struct http_get_options options;
196 if (last && !strcmp(service, last->service))
197 return last;
198 free_discovery(last);
200 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
201 if ((!prefixcmp(url.buf, "http://") || !prefixcmp(url.buf, "https://")) &&
202 git_env_bool("GIT_SMART_HTTP", 1)) {
203 maybe_smart = 1;
204 if (!strchr(url.buf, '?'))
205 strbuf_addch(&refs_url, '?');
206 else
207 strbuf_addch(&refs_url, '&');
208 strbuf_addf(&refs_url, "service=%s", service);
211 memset(&options, 0, sizeof(options));
212 options.content_type = &type;
213 options.effective_url = &effective_url;
214 options.base_url = &url;
215 options.no_cache = 1;
216 options.keep_error = 1;
218 http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
219 switch (http_ret) {
220 case HTTP_OK:
221 break;
222 case HTTP_MISSING_TARGET:
223 show_http_message(&type, &buffer);
224 die("repository '%s' not found", url.buf);
225 case HTTP_NOAUTH:
226 show_http_message(&type, &buffer);
227 die("Authentication failed for '%s'", url.buf);
228 default:
229 show_http_message(&type, &buffer);
230 die("unable to access '%s': %s", url.buf, curl_errorstr);
233 last= xcalloc(1, sizeof(*last_discovery));
234 last->service = service;
235 last->buf_alloc = strbuf_detach(&buffer, &last->len);
236 last->buf = last->buf_alloc;
238 strbuf_addf(&exp, "application/x-%s-advertisement", service);
239 if (maybe_smart &&
240 (5 <= last->len && last->buf[4] == '#') &&
241 !strbuf_cmp(&exp, &type)) {
242 char *line;
245 * smart HTTP response; validate that the service
246 * pkt-line matches our request.
248 line = packet_read_line_buf(&last->buf, &last->len, NULL);
250 strbuf_reset(&exp);
251 strbuf_addf(&exp, "# service=%s", service);
252 if (strcmp(line, exp.buf))
253 die("invalid server response; got '%s'", line);
254 strbuf_release(&exp);
256 /* The header can include additional metadata lines, up
257 * until a packet flush marker. Ignore these now, but
258 * in the future we might start to scan them.
260 while (packet_read_line_buf(&last->buf, &last->len, NULL))
263 last->proto_git = 1;
266 if (last->proto_git)
267 last->refs = parse_git_refs(last, for_push);
268 else
269 last->refs = parse_info_refs(last);
271 strbuf_release(&refs_url);
272 strbuf_release(&exp);
273 strbuf_release(&type);
274 strbuf_release(&effective_url);
275 strbuf_release(&buffer);
276 last_discovery = last;
277 return last;
280 static struct ref *get_refs(int for_push)
282 struct discovery *heads;
284 if (for_push)
285 heads = discover_refs("git-receive-pack", for_push);
286 else
287 heads = discover_refs("git-upload-pack", for_push);
289 return heads->refs;
292 static void output_refs(struct ref *refs)
294 struct ref *posn;
295 for (posn = refs; posn; posn = posn->next) {
296 if (posn->symref)
297 printf("@%s %s\n", posn->symref, posn->name);
298 else
299 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
301 printf("\n");
302 fflush(stdout);
305 struct rpc_state {
306 const char *service_name;
307 const char **argv;
308 struct strbuf *stdin_preamble;
309 char *service_url;
310 char *hdr_content_type;
311 char *hdr_accept;
312 char *buf;
313 size_t alloc;
314 size_t len;
315 size_t pos;
316 int in;
317 int out;
318 struct strbuf result;
319 unsigned gzip_request : 1;
320 unsigned initial_buffer : 1;
323 static size_t rpc_out(void *ptr, size_t eltsize,
324 size_t nmemb, void *buffer_)
326 size_t max = eltsize * nmemb;
327 struct rpc_state *rpc = buffer_;
328 size_t avail = rpc->len - rpc->pos;
330 if (!avail) {
331 rpc->initial_buffer = 0;
332 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
333 if (!avail)
334 return 0;
335 rpc->pos = 0;
336 rpc->len = avail;
339 if (max < avail)
340 avail = max;
341 memcpy(ptr, rpc->buf + rpc->pos, avail);
342 rpc->pos += avail;
343 return avail;
346 #ifndef NO_CURL_IOCTL
347 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
349 struct rpc_state *rpc = clientp;
351 switch (cmd) {
352 case CURLIOCMD_NOP:
353 return CURLIOE_OK;
355 case CURLIOCMD_RESTARTREAD:
356 if (rpc->initial_buffer) {
357 rpc->pos = 0;
358 return CURLIOE_OK;
360 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
361 return CURLIOE_FAILRESTART;
363 default:
364 return CURLIOE_UNKNOWNCMD;
367 #endif
369 static size_t rpc_in(char *ptr, size_t eltsize,
370 size_t nmemb, void *buffer_)
372 size_t size = eltsize * nmemb;
373 struct rpc_state *rpc = buffer_;
374 write_or_die(rpc->in, ptr, size);
375 return size;
378 static int run_slot(struct active_request_slot *slot)
380 int err;
381 struct slot_results results;
383 slot->results = &results;
384 slot->curl_result = curl_easy_perform(slot->curl);
385 finish_active_slot(slot);
387 err = handle_curl_result(&results);
388 if (err != HTTP_OK && err != HTTP_REAUTH) {
389 error("RPC failed; result=%d, HTTP code = %ld",
390 results.curl_result, results.http_code);
393 return err;
396 static int probe_rpc(struct rpc_state *rpc)
398 struct active_request_slot *slot;
399 struct curl_slist *headers = NULL;
400 struct strbuf buf = STRBUF_INIT;
401 int err;
403 slot = get_active_slot();
405 headers = curl_slist_append(headers, rpc->hdr_content_type);
406 headers = curl_slist_append(headers, rpc->hdr_accept);
408 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
409 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
410 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
411 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
412 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
413 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
414 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
415 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
416 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
418 err = run_slot(slot);
420 curl_slist_free_all(headers);
421 strbuf_release(&buf);
422 return err;
425 static int post_rpc(struct rpc_state *rpc)
427 struct active_request_slot *slot;
428 struct curl_slist *headers = NULL;
429 int use_gzip = rpc->gzip_request;
430 char *gzip_body = NULL;
431 size_t gzip_size = 0;
432 int err, large_request = 0;
434 /* Try to load the entire request, if we can fit it into the
435 * allocated buffer space we can use HTTP/1.0 and avoid the
436 * chunked encoding mess.
438 while (1) {
439 size_t left = rpc->alloc - rpc->len;
440 char *buf = rpc->buf + rpc->len;
441 int n;
443 if (left < LARGE_PACKET_MAX) {
444 large_request = 1;
445 use_gzip = 0;
446 break;
449 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
450 if (!n)
451 break;
452 rpc->len += n;
455 if (large_request) {
456 do {
457 err = probe_rpc(rpc);
458 if (err == HTTP_REAUTH)
459 credential_fill(&http_auth);
460 } while (err == HTTP_REAUTH);
461 if (err != HTTP_OK)
462 return -1;
465 headers = curl_slist_append(headers, rpc->hdr_content_type);
466 headers = curl_slist_append(headers, rpc->hdr_accept);
467 headers = curl_slist_append(headers, "Expect:");
469 retry:
470 slot = get_active_slot();
472 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
473 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
474 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
475 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
477 if (large_request) {
478 /* The request body is large and the size cannot be predicted.
479 * We must use chunked encoding to send it.
481 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
482 rpc->initial_buffer = 1;
483 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
484 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
485 #ifndef NO_CURL_IOCTL
486 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
487 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
488 #endif
489 if (options.verbosity > 1) {
490 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
491 fflush(stderr);
494 } else if (gzip_body) {
496 * If we are looping to retry authentication, then the previous
497 * run will have set up the headers and gzip buffer already,
498 * and we just need to send it.
500 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
501 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
503 } else if (use_gzip && 1024 < rpc->len) {
504 /* The client backend isn't giving us compressed data so
505 * we can try to deflate it ourselves, this may save on.
506 * the transfer time.
508 git_zstream stream;
509 int ret;
511 memset(&stream, 0, sizeof(stream));
512 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
513 gzip_size = git_deflate_bound(&stream, rpc->len);
514 gzip_body = xmalloc(gzip_size);
516 stream.next_in = (unsigned char *)rpc->buf;
517 stream.avail_in = rpc->len;
518 stream.next_out = (unsigned char *)gzip_body;
519 stream.avail_out = gzip_size;
521 ret = git_deflate(&stream, Z_FINISH);
522 if (ret != Z_STREAM_END)
523 die("cannot deflate request; zlib deflate error %d", ret);
525 ret = git_deflate_end_gently(&stream);
526 if (ret != Z_OK)
527 die("cannot deflate request; zlib end error %d", ret);
529 gzip_size = stream.total_out;
531 headers = curl_slist_append(headers, "Content-Encoding: gzip");
532 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
533 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
535 if (options.verbosity > 1) {
536 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
537 rpc->service_name,
538 (unsigned long)rpc->len, (unsigned long)gzip_size);
539 fflush(stderr);
541 } else {
542 /* We know the complete request size in advance, use the
543 * more normal Content-Length approach.
545 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
546 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
547 if (options.verbosity > 1) {
548 fprintf(stderr, "POST %s (%lu bytes)\n",
549 rpc->service_name, (unsigned long)rpc->len);
550 fflush(stderr);
554 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
555 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
556 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
558 err = run_slot(slot);
559 if (err == HTTP_REAUTH && !large_request) {
560 credential_fill(&http_auth);
561 goto retry;
563 if (err != HTTP_OK)
564 err = -1;
566 curl_slist_free_all(headers);
567 free(gzip_body);
568 return err;
571 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
573 const char *svc = rpc->service_name;
574 struct strbuf buf = STRBUF_INIT;
575 struct strbuf *preamble = rpc->stdin_preamble;
576 struct child_process client;
577 int err = 0;
579 memset(&client, 0, sizeof(client));
580 client.in = -1;
581 client.out = -1;
582 client.git_cmd = 1;
583 client.argv = rpc->argv;
584 if (start_command(&client))
585 exit(1);
586 if (preamble)
587 write_or_die(client.in, preamble->buf, preamble->len);
588 if (heads)
589 write_or_die(client.in, heads->buf, heads->len);
591 rpc->alloc = http_post_buffer;
592 rpc->buf = xmalloc(rpc->alloc);
593 rpc->in = client.in;
594 rpc->out = client.out;
595 strbuf_init(&rpc->result, 0);
597 strbuf_addf(&buf, "%s%s", url.buf, svc);
598 rpc->service_url = strbuf_detach(&buf, NULL);
600 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
601 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
603 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
604 rpc->hdr_accept = strbuf_detach(&buf, NULL);
606 while (!err) {
607 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
608 if (!n)
609 break;
610 rpc->pos = 0;
611 rpc->len = n;
612 err |= post_rpc(rpc);
615 close(client.in);
616 client.in = -1;
617 if (!err) {
618 strbuf_read(&rpc->result, client.out, 0);
619 } else {
620 char buf[4096];
621 for (;;)
622 if (xread(client.out, buf, sizeof(buf)) <= 0)
623 break;
626 close(client.out);
627 client.out = -1;
629 err |= finish_command(&client);
630 free(rpc->service_url);
631 free(rpc->hdr_content_type);
632 free(rpc->hdr_accept);
633 free(rpc->buf);
634 strbuf_release(&buf);
635 return err;
638 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
640 struct walker *walker;
641 char **targets = xmalloc(nr_heads * sizeof(char*));
642 int ret, i;
644 if (options.depth)
645 die("dumb http transport does not support --depth");
646 for (i = 0; i < nr_heads; i++)
647 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
649 walker = get_http_walker(url.buf);
650 walker->get_all = 1;
651 walker->get_tree = 1;
652 walker->get_history = 1;
653 walker->get_verbosely = options.verbosity >= 3;
654 walker->get_recover = 0;
655 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
656 walker_free(walker);
658 for (i = 0; i < nr_heads; i++)
659 free(targets[i]);
660 free(targets);
662 return ret ? error("Fetch failed.") : 0;
665 static int fetch_git(struct discovery *heads,
666 int nr_heads, struct ref **to_fetch)
668 struct rpc_state rpc;
669 struct strbuf preamble = STRBUF_INIT;
670 char *depth_arg = NULL;
671 int argc = 0, i, err;
672 const char *argv[15];
674 argv[argc++] = "fetch-pack";
675 argv[argc++] = "--stateless-rpc";
676 argv[argc++] = "--stdin";
677 argv[argc++] = "--lock-pack";
678 if (options.followtags)
679 argv[argc++] = "--include-tag";
680 if (options.thin)
681 argv[argc++] = "--thin";
682 if (options.verbosity >= 3) {
683 argv[argc++] = "-v";
684 argv[argc++] = "-v";
686 if (!options.progress)
687 argv[argc++] = "--no-progress";
688 if (options.depth) {
689 struct strbuf buf = STRBUF_INIT;
690 strbuf_addf(&buf, "--depth=%lu", options.depth);
691 depth_arg = strbuf_detach(&buf, NULL);
692 argv[argc++] = depth_arg;
694 argv[argc++] = url.buf;
695 argv[argc++] = NULL;
697 for (i = 0; i < nr_heads; i++) {
698 struct ref *ref = to_fetch[i];
699 if (!ref->name || !*ref->name)
700 die("cannot fetch by sha1 over smart http");
701 packet_buf_write(&preamble, "%s\n", ref->name);
703 packet_buf_flush(&preamble);
705 memset(&rpc, 0, sizeof(rpc));
706 rpc.service_name = "git-upload-pack",
707 rpc.argv = argv;
708 rpc.stdin_preamble = &preamble;
709 rpc.gzip_request = 1;
711 err = rpc_service(&rpc, heads);
712 if (rpc.result.len)
713 write_or_die(1, rpc.result.buf, rpc.result.len);
714 strbuf_release(&rpc.result);
715 strbuf_release(&preamble);
716 free(depth_arg);
717 return err;
720 static int fetch(int nr_heads, struct ref **to_fetch)
722 struct discovery *d = discover_refs("git-upload-pack", 0);
723 if (d->proto_git)
724 return fetch_git(d, nr_heads, to_fetch);
725 else
726 return fetch_dumb(nr_heads, to_fetch);
729 static void parse_fetch(struct strbuf *buf)
731 struct ref **to_fetch = NULL;
732 struct ref *list_head = NULL;
733 struct ref **list = &list_head;
734 int alloc_heads = 0, nr_heads = 0;
736 do {
737 if (!prefixcmp(buf->buf, "fetch ")) {
738 char *p = buf->buf + strlen("fetch ");
739 char *name;
740 struct ref *ref;
741 unsigned char old_sha1[20];
743 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
744 die("protocol error: expected sha/ref, got %s'", p);
745 if (p[40] == ' ')
746 name = p + 41;
747 else if (!p[40])
748 name = "";
749 else
750 die("protocol error: expected sha/ref, got %s'", p);
752 ref = alloc_ref(name);
753 hashcpy(ref->old_sha1, old_sha1);
755 *list = ref;
756 list = &ref->next;
758 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
759 to_fetch[nr_heads++] = ref;
761 else
762 die("http transport does not support %s", buf->buf);
764 strbuf_reset(buf);
765 if (strbuf_getline(buf, stdin, '\n') == EOF)
766 return;
767 if (!*buf->buf)
768 break;
769 } while (1);
771 if (fetch(nr_heads, to_fetch))
772 exit(128); /* error already reported */
773 free_refs(list_head);
774 free(to_fetch);
776 printf("\n");
777 fflush(stdout);
778 strbuf_reset(buf);
781 static int push_dav(int nr_spec, char **specs)
783 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
784 int argc = 0, i;
786 argv[argc++] = "http-push";
787 argv[argc++] = "--helper-status";
788 if (options.dry_run)
789 argv[argc++] = "--dry-run";
790 if (options.verbosity > 1)
791 argv[argc++] = "--verbose";
792 argv[argc++] = url.buf;
793 for (i = 0; i < nr_spec; i++)
794 argv[argc++] = specs[i];
795 argv[argc++] = NULL;
797 if (run_command_v_opt(argv, RUN_GIT_CMD))
798 die("git-%s failed", argv[0]);
799 free(argv);
800 return 0;
803 static int push_git(struct discovery *heads, int nr_spec, char **specs)
805 struct rpc_state rpc;
806 int i, err;
807 struct argv_array args;
809 argv_array_init(&args);
810 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
811 NULL);
813 if (options.thin)
814 argv_array_push(&args, "--thin");
815 if (options.dry_run)
816 argv_array_push(&args, "--dry-run");
817 if (options.verbosity == 0)
818 argv_array_push(&args, "--quiet");
819 else if (options.verbosity > 1)
820 argv_array_push(&args, "--verbose");
821 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
822 argv_array_push(&args, url.buf);
823 for (i = 0; i < nr_spec; i++)
824 argv_array_push(&args, specs[i]);
826 memset(&rpc, 0, sizeof(rpc));
827 rpc.service_name = "git-receive-pack",
828 rpc.argv = args.argv;
830 err = rpc_service(&rpc, heads);
831 if (rpc.result.len)
832 write_or_die(1, rpc.result.buf, rpc.result.len);
833 strbuf_release(&rpc.result);
834 argv_array_clear(&args);
835 return err;
838 static int push(int nr_spec, char **specs)
840 struct discovery *heads = discover_refs("git-receive-pack", 1);
841 int ret;
843 if (heads->proto_git)
844 ret = push_git(heads, nr_spec, specs);
845 else
846 ret = push_dav(nr_spec, specs);
847 free_discovery(heads);
848 return ret;
851 static void parse_push(struct strbuf *buf)
853 char **specs = NULL;
854 int alloc_spec = 0, nr_spec = 0, i, ret;
856 do {
857 if (!prefixcmp(buf->buf, "push ")) {
858 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
859 specs[nr_spec++] = xstrdup(buf->buf + 5);
861 else
862 die("http transport does not support %s", buf->buf);
864 strbuf_reset(buf);
865 if (strbuf_getline(buf, stdin, '\n') == EOF)
866 goto free_specs;
867 if (!*buf->buf)
868 break;
869 } while (1);
871 ret = push(nr_spec, specs);
872 printf("\n");
873 fflush(stdout);
875 if (ret)
876 exit(128); /* error already reported */
878 free_specs:
879 for (i = 0; i < nr_spec; i++)
880 free(specs[i]);
881 free(specs);
884 int main(int argc, const char **argv)
886 struct strbuf buf = STRBUF_INIT;
887 int nongit;
889 git_extract_argv0_path(argv[0]);
890 setup_git_directory_gently(&nongit);
891 if (argc < 2) {
892 fprintf(stderr, "Remote needed\n");
893 return 1;
896 options.verbosity = 1;
897 options.progress = !!isatty(2);
898 options.thin = 1;
900 remote = remote_get(argv[1]);
902 if (argc > 2) {
903 end_url_with_slash(&url, argv[2]);
904 } else {
905 end_url_with_slash(&url, remote->url[0]);
908 http_init(remote, url.buf, 0);
910 do {
911 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
912 if (ferror(stdin))
913 fprintf(stderr, "Error reading command stream\n");
914 else
915 fprintf(stderr, "Unexpected end of command stream\n");
916 return 1;
918 if (buf.len == 0)
919 break;
920 if (!prefixcmp(buf.buf, "fetch ")) {
921 if (nongit)
922 die("Fetch attempted without a local repo");
923 parse_fetch(&buf);
925 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
926 int for_push = !!strstr(buf.buf + 4, "for-push");
927 output_refs(get_refs(for_push));
929 } else if (!prefixcmp(buf.buf, "push ")) {
930 parse_push(&buf);
932 } else if (!prefixcmp(buf.buf, "option ")) {
933 char *name = buf.buf + strlen("option ");
934 char *value = strchr(name, ' ');
935 int result;
937 if (value)
938 *value++ = '\0';
939 else
940 value = "true";
942 result = set_option(name, value);
943 if (!result)
944 printf("ok\n");
945 else if (result < 0)
946 printf("error invalid value\n");
947 else
948 printf("unsupported\n");
949 fflush(stdout);
951 } else if (!strcmp(buf.buf, "capabilities")) {
952 printf("fetch\n");
953 printf("option\n");
954 printf("push\n");
955 printf("\n");
956 fflush(stdout);
957 } else {
958 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
959 return 1;
961 strbuf_reset(&buf);
962 } while (1);
964 http_cleanup();
966 return 0;