remote-curl: fix large pushes with GSSAPI
[git.git] / remote-curl.c
blob427d50f8dec6e94ed831683403aa435bcfe83080
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"
12 static struct remote *remote;
13 static const char *url; /* always ends with a trailing slash */
15 struct options {
16 int verbosity;
17 unsigned long depth;
18 unsigned progress : 1,
19 followtags : 1,
20 dry_run : 1,
21 thin : 1;
23 static struct options options;
25 static int set_option(const char *name, const char *value)
27 if (!strcmp(name, "verbosity")) {
28 char *end;
29 int v = strtol(value, &end, 10);
30 if (value == end || *end)
31 return -1;
32 options.verbosity = v;
33 return 0;
35 else if (!strcmp(name, "progress")) {
36 if (!strcmp(value, "true"))
37 options.progress = 1;
38 else if (!strcmp(value, "false"))
39 options.progress = 0;
40 else
41 return -1;
42 return 0;
44 else if (!strcmp(name, "depth")) {
45 char *end;
46 unsigned long v = strtoul(value, &end, 10);
47 if (value == end || *end)
48 return -1;
49 options.depth = v;
50 return 0;
52 else if (!strcmp(name, "followtags")) {
53 if (!strcmp(value, "true"))
54 options.followtags = 1;
55 else if (!strcmp(value, "false"))
56 options.followtags = 0;
57 else
58 return -1;
59 return 0;
61 else if (!strcmp(name, "dry-run")) {
62 if (!strcmp(value, "true"))
63 options.dry_run = 1;
64 else if (!strcmp(value, "false"))
65 options.dry_run = 0;
66 else
67 return -1;
68 return 0;
70 else {
71 return 1 /* unsupported */;
75 struct discovery {
76 const char *service;
77 char *buf_alloc;
78 char *buf;
79 size_t len;
80 struct ref *refs;
81 unsigned proto_git : 1;
83 static struct discovery *last_discovery;
85 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
87 struct ref *list = NULL;
88 get_remote_heads(-1, heads->buf, heads->len, &list,
89 for_push ? REF_NORMAL : 0, NULL);
90 return list;
93 static struct ref *parse_info_refs(struct discovery *heads)
95 char *data, *start, *mid;
96 char *ref_name;
97 int i = 0;
99 struct ref *refs = NULL;
100 struct ref *ref = NULL;
101 struct ref *last_ref = NULL;
103 data = heads->buf;
104 start = NULL;
105 mid = data;
106 while (i < heads->len) {
107 if (!start) {
108 start = &data[i];
110 if (data[i] == '\t')
111 mid = &data[i];
112 if (data[i] == '\n') {
113 if (mid - start != 40)
114 die("%sinfo/refs not valid: is this a git repository?", url);
115 data[i] = 0;
116 ref_name = mid + 1;
117 ref = xmalloc(sizeof(struct ref) +
118 strlen(ref_name) + 1);
119 memset(ref, 0, sizeof(struct ref));
120 strcpy(ref->name, ref_name);
121 get_sha1_hex(start, ref->old_sha1);
122 if (!refs)
123 refs = ref;
124 if (last_ref)
125 last_ref->next = ref;
126 last_ref = ref;
127 start = NULL;
129 i++;
132 ref = alloc_ref("HEAD");
133 if (!http_fetch_ref(url, ref) &&
134 !resolve_remote_symref(ref, refs)) {
135 ref->next = refs;
136 refs = ref;
137 } else {
138 free(ref);
141 return refs;
144 static void free_discovery(struct discovery *d)
146 if (d) {
147 if (d == last_discovery)
148 last_discovery = NULL;
149 free(d->buf_alloc);
150 free_refs(d->refs);
151 free(d);
155 static int show_http_message(struct strbuf *type, struct strbuf *msg)
157 const char *p, *eol;
160 * We only show text/plain parts, as other types are likely
161 * to be ugly to look at on the user's terminal.
163 * TODO should handle "; charset=XXX", and re-encode into
164 * logoutputencoding
166 if (strcasecmp(type->buf, "text/plain"))
167 return -1;
169 strbuf_trim(msg);
170 if (!msg->len)
171 return -1;
173 p = msg->buf;
174 do {
175 eol = strchrnul(p, '\n');
176 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
177 p = eol + 1;
178 } while(*eol);
179 return 0;
182 static struct discovery* discover_refs(const char *service, int for_push)
184 struct strbuf exp = STRBUF_INIT;
185 struct strbuf type = STRBUF_INIT;
186 struct strbuf buffer = STRBUF_INIT;
187 struct discovery *last = last_discovery;
188 char *refs_url;
189 int http_ret, maybe_smart = 0;
191 if (last && !strcmp(service, last->service))
192 return last;
193 free_discovery(last);
195 strbuf_addf(&buffer, "%sinfo/refs", url);
196 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
197 git_env_bool("GIT_SMART_HTTP", 1)) {
198 maybe_smart = 1;
199 if (!strchr(url, '?'))
200 strbuf_addch(&buffer, '?');
201 else
202 strbuf_addch(&buffer, '&');
203 strbuf_addf(&buffer, "service=%s", service);
205 refs_url = strbuf_detach(&buffer, NULL);
207 http_ret = http_get_strbuf(refs_url, &type, &buffer,
208 HTTP_NO_CACHE | HTTP_KEEP_ERROR);
209 switch (http_ret) {
210 case HTTP_OK:
211 break;
212 case HTTP_MISSING_TARGET:
213 show_http_message(&type, &buffer);
214 die("repository '%s' not found", url);
215 case HTTP_NOAUTH:
216 show_http_message(&type, &buffer);
217 die("Authentication failed for '%s'", url);
218 default:
219 show_http_message(&type, &buffer);
220 die("unable to access '%s': %s", url, curl_errorstr);
223 last= xcalloc(1, sizeof(*last_discovery));
224 last->service = service;
225 last->buf_alloc = strbuf_detach(&buffer, &last->len);
226 last->buf = last->buf_alloc;
228 strbuf_addf(&exp, "application/x-%s-advertisement", service);
229 if (maybe_smart &&
230 (5 <= last->len && last->buf[4] == '#') &&
231 !strbuf_cmp(&exp, &type)) {
232 char *line;
235 * smart HTTP response; validate that the service
236 * pkt-line matches our request.
238 line = packet_read_line_buf(&last->buf, &last->len, NULL);
240 strbuf_reset(&exp);
241 strbuf_addf(&exp, "# service=%s", service);
242 if (strcmp(line, exp.buf))
243 die("invalid server response; got '%s'", line);
244 strbuf_release(&exp);
246 /* The header can include additional metadata lines, up
247 * until a packet flush marker. Ignore these now, but
248 * in the future we might start to scan them.
250 while (packet_read_line_buf(&last->buf, &last->len, NULL))
253 last->proto_git = 1;
256 if (last->proto_git)
257 last->refs = parse_git_refs(last, for_push);
258 else
259 last->refs = parse_info_refs(last);
261 free(refs_url);
262 strbuf_release(&exp);
263 strbuf_release(&type);
264 strbuf_release(&buffer);
265 last_discovery = last;
266 return last;
269 static struct ref *get_refs(int for_push)
271 struct discovery *heads;
273 if (for_push)
274 heads = discover_refs("git-receive-pack", for_push);
275 else
276 heads = discover_refs("git-upload-pack", for_push);
278 return heads->refs;
281 static void output_refs(struct ref *refs)
283 struct ref *posn;
284 for (posn = refs; posn; posn = posn->next) {
285 if (posn->symref)
286 printf("@%s %s\n", posn->symref, posn->name);
287 else
288 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
290 printf("\n");
291 fflush(stdout);
294 struct rpc_state {
295 const char *service_name;
296 const char **argv;
297 struct strbuf *stdin_preamble;
298 char *service_url;
299 char *hdr_content_type;
300 char *hdr_accept;
301 char *buf;
302 size_t alloc;
303 size_t len;
304 size_t pos;
305 int in;
306 int out;
307 struct strbuf result;
308 unsigned gzip_request : 1;
309 unsigned initial_buffer : 1;
312 static size_t rpc_out(void *ptr, size_t eltsize,
313 size_t nmemb, void *buffer_)
315 size_t max = eltsize * nmemb;
316 struct rpc_state *rpc = buffer_;
317 size_t avail = rpc->len - rpc->pos;
319 if (!avail) {
320 rpc->initial_buffer = 0;
321 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
322 if (!avail)
323 return 0;
324 rpc->pos = 0;
325 rpc->len = avail;
328 if (max < avail)
329 avail = max;
330 memcpy(ptr, rpc->buf + rpc->pos, avail);
331 rpc->pos += avail;
332 return avail;
335 #ifndef NO_CURL_IOCTL
336 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
338 struct rpc_state *rpc = clientp;
340 switch (cmd) {
341 case CURLIOCMD_NOP:
342 return CURLIOE_OK;
344 case CURLIOCMD_RESTARTREAD:
345 if (rpc->initial_buffer) {
346 rpc->pos = 0;
347 return CURLIOE_OK;
349 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
350 return CURLIOE_FAILRESTART;
352 default:
353 return CURLIOE_UNKNOWNCMD;
356 #endif
358 static size_t rpc_in(char *ptr, size_t eltsize,
359 size_t nmemb, void *buffer_)
361 size_t size = eltsize * nmemb;
362 struct rpc_state *rpc = buffer_;
363 write_or_die(rpc->in, ptr, size);
364 return size;
367 static int run_slot(struct active_request_slot *slot,
368 struct slot_results *results)
370 int err;
371 struct slot_results results_buf;
373 if (!results)
374 results = &results_buf;
376 slot->results = results;
377 slot->curl_result = curl_easy_perform(slot->curl);
378 finish_active_slot(slot);
380 err = handle_curl_result(results);
381 if (err != HTTP_OK && err != HTTP_REAUTH) {
382 error("RPC failed; result=%d, HTTP code = %ld",
383 results->curl_result, results->http_code);
386 return err;
389 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
391 struct active_request_slot *slot;
392 struct curl_slist *headers = NULL;
393 struct strbuf buf = STRBUF_INIT;
394 int err;
396 slot = get_active_slot();
398 headers = curl_slist_append(headers, rpc->hdr_content_type);
399 headers = curl_slist_append(headers, rpc->hdr_accept);
401 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
402 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
403 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
404 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
405 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
406 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
407 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
408 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
409 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
411 err = run_slot(slot, results);
413 curl_slist_free_all(headers);
414 strbuf_release(&buf);
415 return err;
418 static int post_rpc(struct rpc_state *rpc)
420 struct active_request_slot *slot;
421 struct curl_slist *headers = NULL;
422 int use_gzip = rpc->gzip_request;
423 char *gzip_body = NULL;
424 size_t gzip_size = 0;
425 int err, large_request = 0;
426 int needs_100_continue = 0;
428 /* Try to load the entire request, if we can fit it into the
429 * allocated buffer space we can use HTTP/1.0 and avoid the
430 * chunked encoding mess.
432 while (1) {
433 size_t left = rpc->alloc - rpc->len;
434 char *buf = rpc->buf + rpc->len;
435 int n;
437 if (left < LARGE_PACKET_MAX) {
438 large_request = 1;
439 use_gzip = 0;
440 break;
443 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
444 if (!n)
445 break;
446 rpc->len += n;
449 if (large_request) {
450 struct slot_results results;
452 do {
453 err = probe_rpc(rpc, &results);
454 } while (err == HTTP_REAUTH);
455 if (err != HTTP_OK)
456 return -1;
458 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
459 needs_100_continue = 1;
462 headers = curl_slist_append(headers, rpc->hdr_content_type);
463 headers = curl_slist_append(headers, rpc->hdr_accept);
464 headers = curl_slist_append(headers, needs_100_continue ?
465 "Expect: 100-continue" : "Expect:");
467 retry:
468 slot = get_active_slot();
470 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
471 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
472 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
473 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
475 if (large_request) {
476 /* The request body is large and the size cannot be predicted.
477 * We must use chunked encoding to send it.
479 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
480 rpc->initial_buffer = 1;
481 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
482 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
483 #ifndef NO_CURL_IOCTL
484 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
485 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
486 #endif
487 if (options.verbosity > 1) {
488 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
489 fflush(stderr);
492 } else if (gzip_body) {
494 * If we are looping to retry authentication, then the previous
495 * run will have set up the headers and gzip buffer already,
496 * and we just need to send it.
498 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
499 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
501 } else if (use_gzip && 1024 < rpc->len) {
502 /* The client backend isn't giving us compressed data so
503 * we can try to deflate it ourselves, this may save on.
504 * the transfer time.
506 git_zstream stream;
507 int ret;
509 memset(&stream, 0, sizeof(stream));
510 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
511 gzip_size = git_deflate_bound(&stream, rpc->len);
512 gzip_body = xmalloc(gzip_size);
514 stream.next_in = (unsigned char *)rpc->buf;
515 stream.avail_in = rpc->len;
516 stream.next_out = (unsigned char *)gzip_body;
517 stream.avail_out = gzip_size;
519 ret = git_deflate(&stream, Z_FINISH);
520 if (ret != Z_STREAM_END)
521 die("cannot deflate request; zlib deflate error %d", ret);
523 ret = git_deflate_end_gently(&stream);
524 if (ret != Z_OK)
525 die("cannot deflate request; zlib end error %d", ret);
527 gzip_size = stream.total_out;
529 headers = curl_slist_append(headers, "Content-Encoding: gzip");
530 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
531 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
533 if (options.verbosity > 1) {
534 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
535 rpc->service_name,
536 (unsigned long)rpc->len, (unsigned long)gzip_size);
537 fflush(stderr);
539 } else {
540 /* We know the complete request size in advance, use the
541 * more normal Content-Length approach.
543 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
544 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
545 if (options.verbosity > 1) {
546 fprintf(stderr, "POST %s (%lu bytes)\n",
547 rpc->service_name, (unsigned long)rpc->len);
548 fflush(stderr);
552 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
553 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
554 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
556 err = run_slot(slot, NULL);
557 if (err == HTTP_REAUTH && !large_request)
558 goto retry;
559 if (err != HTTP_OK)
560 err = -1;
562 curl_slist_free_all(headers);
563 free(gzip_body);
564 return err;
567 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
569 const char *svc = rpc->service_name;
570 struct strbuf buf = STRBUF_INIT;
571 struct strbuf *preamble = rpc->stdin_preamble;
572 struct child_process client;
573 int err = 0;
575 memset(&client, 0, sizeof(client));
576 client.in = -1;
577 client.out = -1;
578 client.git_cmd = 1;
579 client.argv = rpc->argv;
580 if (start_command(&client))
581 exit(1);
582 if (preamble)
583 write_or_die(client.in, preamble->buf, preamble->len);
584 if (heads)
585 write_or_die(client.in, heads->buf, heads->len);
587 rpc->alloc = http_post_buffer;
588 rpc->buf = xmalloc(rpc->alloc);
589 rpc->in = client.in;
590 rpc->out = client.out;
591 strbuf_init(&rpc->result, 0);
593 strbuf_addf(&buf, "%s%s", url, svc);
594 rpc->service_url = strbuf_detach(&buf, NULL);
596 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
597 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
599 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
600 rpc->hdr_accept = strbuf_detach(&buf, NULL);
602 while (!err) {
603 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
604 if (!n)
605 break;
606 rpc->pos = 0;
607 rpc->len = n;
608 err |= post_rpc(rpc);
611 close(client.in);
612 client.in = -1;
613 if (!err) {
614 strbuf_read(&rpc->result, client.out, 0);
615 } else {
616 char buf[4096];
617 for (;;)
618 if (xread(client.out, buf, sizeof(buf)) <= 0)
619 break;
622 close(client.out);
623 client.out = -1;
625 err |= finish_command(&client);
626 free(rpc->service_url);
627 free(rpc->hdr_content_type);
628 free(rpc->hdr_accept);
629 free(rpc->buf);
630 strbuf_release(&buf);
631 return err;
634 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
636 struct walker *walker;
637 char **targets = xmalloc(nr_heads * sizeof(char*));
638 int ret, i;
640 if (options.depth)
641 die("dumb http transport does not support --depth");
642 for (i = 0; i < nr_heads; i++)
643 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
645 walker = get_http_walker(url);
646 walker->get_all = 1;
647 walker->get_tree = 1;
648 walker->get_history = 1;
649 walker->get_verbosely = options.verbosity >= 3;
650 walker->get_recover = 0;
651 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
652 walker_free(walker);
654 for (i = 0; i < nr_heads; i++)
655 free(targets[i]);
656 free(targets);
658 return ret ? error("Fetch failed.") : 0;
661 static int fetch_git(struct discovery *heads,
662 int nr_heads, struct ref **to_fetch)
664 struct rpc_state rpc;
665 struct strbuf preamble = STRBUF_INIT;
666 char *depth_arg = NULL;
667 int argc = 0, i, err;
668 const char *argv[15];
670 argv[argc++] = "fetch-pack";
671 argv[argc++] = "--stateless-rpc";
672 argv[argc++] = "--stdin";
673 argv[argc++] = "--lock-pack";
674 if (options.followtags)
675 argv[argc++] = "--include-tag";
676 if (options.thin)
677 argv[argc++] = "--thin";
678 if (options.verbosity >= 3) {
679 argv[argc++] = "-v";
680 argv[argc++] = "-v";
682 if (!options.progress)
683 argv[argc++] = "--no-progress";
684 if (options.depth) {
685 struct strbuf buf = STRBUF_INIT;
686 strbuf_addf(&buf, "--depth=%lu", options.depth);
687 depth_arg = strbuf_detach(&buf, NULL);
688 argv[argc++] = depth_arg;
690 argv[argc++] = url;
691 argv[argc++] = NULL;
693 for (i = 0; i < nr_heads; i++) {
694 struct ref *ref = to_fetch[i];
695 if (!ref->name || !*ref->name)
696 die("cannot fetch by sha1 over smart http");
697 packet_buf_write(&preamble, "%s\n", ref->name);
699 packet_buf_flush(&preamble);
701 memset(&rpc, 0, sizeof(rpc));
702 rpc.service_name = "git-upload-pack",
703 rpc.argv = argv;
704 rpc.stdin_preamble = &preamble;
705 rpc.gzip_request = 1;
707 err = rpc_service(&rpc, heads);
708 if (rpc.result.len)
709 write_or_die(1, rpc.result.buf, rpc.result.len);
710 strbuf_release(&rpc.result);
711 strbuf_release(&preamble);
712 free(depth_arg);
713 return err;
716 static int fetch(int nr_heads, struct ref **to_fetch)
718 struct discovery *d = discover_refs("git-upload-pack", 0);
719 if (d->proto_git)
720 return fetch_git(d, nr_heads, to_fetch);
721 else
722 return fetch_dumb(nr_heads, to_fetch);
725 static void parse_fetch(struct strbuf *buf)
727 struct ref **to_fetch = NULL;
728 struct ref *list_head = NULL;
729 struct ref **list = &list_head;
730 int alloc_heads = 0, nr_heads = 0;
732 do {
733 if (!prefixcmp(buf->buf, "fetch ")) {
734 char *p = buf->buf + strlen("fetch ");
735 char *name;
736 struct ref *ref;
737 unsigned char old_sha1[20];
739 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
740 die("protocol error: expected sha/ref, got %s'", p);
741 if (p[40] == ' ')
742 name = p + 41;
743 else if (!p[40])
744 name = "";
745 else
746 die("protocol error: expected sha/ref, got %s'", p);
748 ref = alloc_ref(name);
749 hashcpy(ref->old_sha1, old_sha1);
751 *list = ref;
752 list = &ref->next;
754 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
755 to_fetch[nr_heads++] = ref;
757 else
758 die("http transport does not support %s", buf->buf);
760 strbuf_reset(buf);
761 if (strbuf_getline(buf, stdin, '\n') == EOF)
762 return;
763 if (!*buf->buf)
764 break;
765 } while (1);
767 if (fetch(nr_heads, to_fetch))
768 exit(128); /* error already reported */
769 free_refs(list_head);
770 free(to_fetch);
772 printf("\n");
773 fflush(stdout);
774 strbuf_reset(buf);
777 static int push_dav(int nr_spec, char **specs)
779 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
780 int argc = 0, i;
782 argv[argc++] = "http-push";
783 argv[argc++] = "--helper-status";
784 if (options.dry_run)
785 argv[argc++] = "--dry-run";
786 if (options.verbosity > 1)
787 argv[argc++] = "--verbose";
788 argv[argc++] = url;
789 for (i = 0; i < nr_spec; i++)
790 argv[argc++] = specs[i];
791 argv[argc++] = NULL;
793 if (run_command_v_opt(argv, RUN_GIT_CMD))
794 die("git-%s failed", argv[0]);
795 free(argv);
796 return 0;
799 static int push_git(struct discovery *heads, int nr_spec, char **specs)
801 struct rpc_state rpc;
802 int i, err;
803 struct argv_array args;
805 argv_array_init(&args);
806 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
807 NULL);
809 if (options.thin)
810 argv_array_push(&args, "--thin");
811 if (options.dry_run)
812 argv_array_push(&args, "--dry-run");
813 if (options.verbosity == 0)
814 argv_array_push(&args, "--quiet");
815 else if (options.verbosity > 1)
816 argv_array_push(&args, "--verbose");
817 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
818 argv_array_push(&args, url);
819 for (i = 0; i < nr_spec; i++)
820 argv_array_push(&args, specs[i]);
822 memset(&rpc, 0, sizeof(rpc));
823 rpc.service_name = "git-receive-pack",
824 rpc.argv = args.argv;
826 err = rpc_service(&rpc, heads);
827 if (rpc.result.len)
828 write_or_die(1, rpc.result.buf, rpc.result.len);
829 strbuf_release(&rpc.result);
830 argv_array_clear(&args);
831 return err;
834 static int push(int nr_spec, char **specs)
836 struct discovery *heads = discover_refs("git-receive-pack", 1);
837 int ret;
839 if (heads->proto_git)
840 ret = push_git(heads, nr_spec, specs);
841 else
842 ret = push_dav(nr_spec, specs);
843 free_discovery(heads);
844 return ret;
847 static void parse_push(struct strbuf *buf)
849 char **specs = NULL;
850 int alloc_spec = 0, nr_spec = 0, i, ret;
852 do {
853 if (!prefixcmp(buf->buf, "push ")) {
854 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
855 specs[nr_spec++] = xstrdup(buf->buf + 5);
857 else
858 die("http transport does not support %s", buf->buf);
860 strbuf_reset(buf);
861 if (strbuf_getline(buf, stdin, '\n') == EOF)
862 goto free_specs;
863 if (!*buf->buf)
864 break;
865 } while (1);
867 ret = push(nr_spec, specs);
868 printf("\n");
869 fflush(stdout);
871 if (ret)
872 exit(128); /* error already reported */
874 free_specs:
875 for (i = 0; i < nr_spec; i++)
876 free(specs[i]);
877 free(specs);
880 int main(int argc, const char **argv)
882 struct strbuf buf = STRBUF_INIT;
883 int nongit;
885 git_extract_argv0_path(argv[0]);
886 setup_git_directory_gently(&nongit);
887 if (argc < 2) {
888 fprintf(stderr, "Remote needed\n");
889 return 1;
892 options.verbosity = 1;
893 options.progress = !!isatty(2);
894 options.thin = 1;
896 remote = remote_get(argv[1]);
898 if (argc > 2) {
899 end_url_with_slash(&buf, argv[2]);
900 } else {
901 end_url_with_slash(&buf, remote->url[0]);
904 url = strbuf_detach(&buf, NULL);
906 http_init(remote, url, 0);
908 do {
909 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
910 if (ferror(stdin))
911 fprintf(stderr, "Error reading command stream\n");
912 else
913 fprintf(stderr, "Unexpected end of command stream\n");
914 return 1;
916 if (buf.len == 0)
917 break;
918 if (!prefixcmp(buf.buf, "fetch ")) {
919 if (nongit)
920 die("Fetch attempted without a local repo");
921 parse_fetch(&buf);
923 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
924 int for_push = !!strstr(buf.buf + 4, "for-push");
925 output_refs(get_refs(for_push));
927 } else if (!prefixcmp(buf.buf, "push ")) {
928 parse_push(&buf);
930 } else if (!prefixcmp(buf.buf, "option ")) {
931 char *name = buf.buf + strlen("option ");
932 char *value = strchr(name, ' ');
933 int result;
935 if (value)
936 *value++ = '\0';
937 else
938 value = "true";
940 result = set_option(name, value);
941 if (!result)
942 printf("ok\n");
943 else if (result < 0)
944 printf("error invalid value\n");
945 else
946 printf("unsupported\n");
947 fflush(stdout);
949 } else if (!strcmp(buf.buf, "capabilities")) {
950 printf("fetch\n");
951 printf("option\n");
952 printf("push\n");
953 printf("\n");
954 fflush(stdout);
955 } else {
956 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
957 return 1;
959 strbuf_reset(&buf);
960 } while (1);
962 http_cleanup();
964 return 0;