revision-walk: --parent option to find children of a parent
[git/mjg.git] / remote-curl.c
blob93a09a64c3b1689b0f29a26c6cdd601ef298b975
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"
11 static struct remote *remote;
12 static const char *url; /* always ends with a trailing slash */
14 struct options {
15 int verbosity;
16 unsigned long depth;
17 unsigned progress : 1,
18 followtags : 1,
19 dry_run : 1,
20 thin : 1;
22 static struct options options;
24 static int set_option(const char *name, const char *value)
26 if (!strcmp(name, "verbosity")) {
27 char *end;
28 int v = strtol(value, &end, 10);
29 if (value == end || *end)
30 return -1;
31 options.verbosity = v;
32 return 0;
34 else if (!strcmp(name, "progress")) {
35 if (!strcmp(value, "true"))
36 options.progress = 1;
37 else if (!strcmp(value, "false"))
38 options.progress = 0;
39 else
40 return -1;
41 return 0;
43 else if (!strcmp(name, "depth")) {
44 char *end;
45 unsigned long v = strtoul(value, &end, 10);
46 if (value == end || *end)
47 return -1;
48 options.depth = v;
49 return 0;
51 else if (!strcmp(name, "followtags")) {
52 if (!strcmp(value, "true"))
53 options.followtags = 1;
54 else if (!strcmp(value, "false"))
55 options.followtags = 0;
56 else
57 return -1;
58 return 0;
60 else if (!strcmp(name, "dry-run")) {
61 if (!strcmp(value, "true"))
62 options.dry_run = 1;
63 else if (!strcmp(value, "false"))
64 options.dry_run = 0;
65 else
66 return -1;
67 return 0;
69 else {
70 return 1 /* unsupported */;
74 struct discovery {
75 const char *service;
76 char *buf_alloc;
77 char *buf;
78 size_t len;
79 struct ref *refs;
80 unsigned proto_git : 1;
82 static struct discovery *last_discovery;
84 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
86 struct ref *list = NULL;
87 get_remote_heads(-1, heads->buf, heads->len, &list,
88 for_push ? REF_NORMAL : 0, NULL);
89 return list;
92 static struct ref *parse_info_refs(struct discovery *heads)
94 char *data, *start, *mid;
95 char *ref_name;
96 int i = 0;
98 struct ref *refs = NULL;
99 struct ref *ref = NULL;
100 struct ref *last_ref = NULL;
102 data = heads->buf;
103 start = NULL;
104 mid = data;
105 while (i < heads->len) {
106 if (!start) {
107 start = &data[i];
109 if (data[i] == '\t')
110 mid = &data[i];
111 if (data[i] == '\n') {
112 if (mid - start != 40)
113 die("%sinfo/refs not valid: is this a git repository?", url);
114 data[i] = 0;
115 ref_name = mid + 1;
116 ref = xmalloc(sizeof(struct ref) +
117 strlen(ref_name) + 1);
118 memset(ref, 0, sizeof(struct ref));
119 strcpy(ref->name, ref_name);
120 get_sha1_hex(start, ref->old_sha1);
121 if (!refs)
122 refs = ref;
123 if (last_ref)
124 last_ref->next = ref;
125 last_ref = ref;
126 start = NULL;
128 i++;
131 ref = alloc_ref("HEAD");
132 if (!http_fetch_ref(url, ref) &&
133 !resolve_remote_symref(ref, refs)) {
134 ref->next = refs;
135 refs = ref;
136 } else {
137 free(ref);
140 return refs;
143 static void free_discovery(struct discovery *d)
145 if (d) {
146 if (d == last_discovery)
147 last_discovery = NULL;
148 free(d->buf_alloc);
149 free_refs(d->refs);
150 free(d);
154 static struct discovery* discover_refs(const char *service, int for_push)
156 struct strbuf exp = STRBUF_INIT;
157 struct strbuf type = STRBUF_INIT;
158 struct strbuf buffer = STRBUF_INIT;
159 struct discovery *last = last_discovery;
160 char *refs_url;
161 int http_ret, maybe_smart = 0;
163 if (last && !strcmp(service, last->service))
164 return last;
165 free_discovery(last);
167 strbuf_addf(&buffer, "%sinfo/refs", url);
168 if ((!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) &&
169 git_env_bool("GIT_SMART_HTTP", 1)) {
170 maybe_smart = 1;
171 if (!strchr(url, '?'))
172 strbuf_addch(&buffer, '?');
173 else
174 strbuf_addch(&buffer, '&');
175 strbuf_addf(&buffer, "service=%s", service);
177 refs_url = strbuf_detach(&buffer, NULL);
179 http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
180 switch (http_ret) {
181 case HTTP_OK:
182 break;
183 case HTTP_MISSING_TARGET:
184 die("%s not found: did you run git update-server-info on the"
185 " server?", refs_url);
186 case HTTP_NOAUTH:
187 die("Authentication failed");
188 default:
189 http_error(refs_url, http_ret);
190 die("HTTP request failed");
193 last= xcalloc(1, sizeof(*last_discovery));
194 last->service = service;
195 last->buf_alloc = strbuf_detach(&buffer, &last->len);
196 last->buf = last->buf_alloc;
198 strbuf_addf(&exp, "application/x-%s-advertisement", service);
199 if (maybe_smart &&
200 (5 <= last->len && last->buf[4] == '#') &&
201 !strbuf_cmp(&exp, &type)) {
202 char *line;
205 * smart HTTP response; validate that the service
206 * pkt-line matches our request.
208 line = packet_read_line_buf(&last->buf, &last->len, NULL);
210 strbuf_reset(&exp);
211 strbuf_addf(&exp, "# service=%s", service);
212 if (strcmp(line, exp.buf))
213 die("invalid server response; got '%s'", line);
214 strbuf_release(&exp);
216 /* The header can include additional metadata lines, up
217 * until a packet flush marker. Ignore these now, but
218 * in the future we might start to scan them.
220 while (packet_read_line_buf(&last->buf, &last->len, NULL))
223 last->proto_git = 1;
226 if (last->proto_git)
227 last->refs = parse_git_refs(last, for_push);
228 else
229 last->refs = parse_info_refs(last);
231 free(refs_url);
232 strbuf_release(&exp);
233 strbuf_release(&type);
234 strbuf_release(&buffer);
235 last_discovery = last;
236 return last;
239 static struct ref *get_refs(int for_push)
241 struct discovery *heads;
243 if (for_push)
244 heads = discover_refs("git-receive-pack", for_push);
245 else
246 heads = discover_refs("git-upload-pack", for_push);
248 return heads->refs;
251 static void output_refs(struct ref *refs)
253 struct ref *posn;
254 for (posn = refs; posn; posn = posn->next) {
255 if (posn->symref)
256 printf("@%s %s\n", posn->symref, posn->name);
257 else
258 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
260 printf("\n");
261 fflush(stdout);
264 struct rpc_state {
265 const char *service_name;
266 const char **argv;
267 struct strbuf *stdin_preamble;
268 char *service_url;
269 char *hdr_content_type;
270 char *hdr_accept;
271 char *buf;
272 size_t alloc;
273 size_t len;
274 size_t pos;
275 int in;
276 int out;
277 struct strbuf result;
278 unsigned gzip_request : 1;
279 unsigned initial_buffer : 1;
282 static size_t rpc_out(void *ptr, size_t eltsize,
283 size_t nmemb, void *buffer_)
285 size_t max = eltsize * nmemb;
286 struct rpc_state *rpc = buffer_;
287 size_t avail = rpc->len - rpc->pos;
289 if (!avail) {
290 rpc->initial_buffer = 0;
291 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
292 if (!avail)
293 return 0;
294 rpc->pos = 0;
295 rpc->len = avail;
298 if (max < avail)
299 avail = max;
300 memcpy(ptr, rpc->buf + rpc->pos, avail);
301 rpc->pos += avail;
302 return avail;
305 #ifndef NO_CURL_IOCTL
306 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
308 struct rpc_state *rpc = clientp;
310 switch (cmd) {
311 case CURLIOCMD_NOP:
312 return CURLIOE_OK;
314 case CURLIOCMD_RESTARTREAD:
315 if (rpc->initial_buffer) {
316 rpc->pos = 0;
317 return CURLIOE_OK;
319 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
320 return CURLIOE_FAILRESTART;
322 default:
323 return CURLIOE_UNKNOWNCMD;
326 #endif
328 static size_t rpc_in(char *ptr, size_t eltsize,
329 size_t nmemb, void *buffer_)
331 size_t size = eltsize * nmemb;
332 struct rpc_state *rpc = buffer_;
333 write_or_die(rpc->in, ptr, size);
334 return size;
337 static int run_slot(struct active_request_slot *slot)
339 int err;
340 struct slot_results results;
342 slot->results = &results;
343 slot->curl_result = curl_easy_perform(slot->curl);
344 finish_active_slot(slot);
346 err = handle_curl_result(&results);
347 if (err != HTTP_OK && err != HTTP_REAUTH) {
348 error("RPC failed; result=%d, HTTP code = %ld",
349 results.curl_result, results.http_code);
352 return err;
355 static int probe_rpc(struct rpc_state *rpc)
357 struct active_request_slot *slot;
358 struct curl_slist *headers = NULL;
359 struct strbuf buf = STRBUF_INIT;
360 int err;
362 slot = get_active_slot();
364 headers = curl_slist_append(headers, rpc->hdr_content_type);
365 headers = curl_slist_append(headers, rpc->hdr_accept);
367 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
368 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
369 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
370 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
371 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
372 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
373 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
374 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
375 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
377 err = run_slot(slot);
379 curl_slist_free_all(headers);
380 strbuf_release(&buf);
381 return err;
384 static int post_rpc(struct rpc_state *rpc)
386 struct active_request_slot *slot;
387 struct curl_slist *headers = NULL;
388 int use_gzip = rpc->gzip_request;
389 char *gzip_body = NULL;
390 size_t gzip_size = 0;
391 int err, large_request = 0;
393 /* Try to load the entire request, if we can fit it into the
394 * allocated buffer space we can use HTTP/1.0 and avoid the
395 * chunked encoding mess.
397 while (1) {
398 size_t left = rpc->alloc - rpc->len;
399 char *buf = rpc->buf + rpc->len;
400 int n;
402 if (left < LARGE_PACKET_MAX) {
403 large_request = 1;
404 use_gzip = 0;
405 break;
408 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
409 if (!n)
410 break;
411 rpc->len += n;
414 if (large_request) {
415 do {
416 err = probe_rpc(rpc);
417 } while (err == HTTP_REAUTH);
418 if (err != HTTP_OK)
419 return -1;
422 headers = curl_slist_append(headers, rpc->hdr_content_type);
423 headers = curl_slist_append(headers, rpc->hdr_accept);
424 headers = curl_slist_append(headers, "Expect:");
426 retry:
427 slot = get_active_slot();
429 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
430 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
431 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
432 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
434 if (large_request) {
435 /* The request body is large and the size cannot be predicted.
436 * We must use chunked encoding to send it.
438 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
439 rpc->initial_buffer = 1;
440 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
441 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
442 #ifndef NO_CURL_IOCTL
443 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
444 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
445 #endif
446 if (options.verbosity > 1) {
447 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
448 fflush(stderr);
451 } else if (gzip_body) {
453 * If we are looping to retry authentication, then the previous
454 * run will have set up the headers and gzip buffer already,
455 * and we just need to send it.
457 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
458 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
460 } else if (use_gzip && 1024 < rpc->len) {
461 /* The client backend isn't giving us compressed data so
462 * we can try to deflate it ourselves, this may save on.
463 * the transfer time.
465 git_zstream stream;
466 int ret;
468 memset(&stream, 0, sizeof(stream));
469 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
470 gzip_size = git_deflate_bound(&stream, rpc->len);
471 gzip_body = xmalloc(gzip_size);
473 stream.next_in = (unsigned char *)rpc->buf;
474 stream.avail_in = rpc->len;
475 stream.next_out = (unsigned char *)gzip_body;
476 stream.avail_out = gzip_size;
478 ret = git_deflate(&stream, Z_FINISH);
479 if (ret != Z_STREAM_END)
480 die("cannot deflate request; zlib deflate error %d", ret);
482 ret = git_deflate_end_gently(&stream);
483 if (ret != Z_OK)
484 die("cannot deflate request; zlib end error %d", ret);
486 gzip_size = stream.total_out;
488 headers = curl_slist_append(headers, "Content-Encoding: gzip");
489 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
490 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
492 if (options.verbosity > 1) {
493 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
494 rpc->service_name,
495 (unsigned long)rpc->len, (unsigned long)gzip_size);
496 fflush(stderr);
498 } else {
499 /* We know the complete request size in advance, use the
500 * more normal Content-Length approach.
502 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
503 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
504 if (options.verbosity > 1) {
505 fprintf(stderr, "POST %s (%lu bytes)\n",
506 rpc->service_name, (unsigned long)rpc->len);
507 fflush(stderr);
511 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
512 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
513 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
515 err = run_slot(slot);
516 if (err == HTTP_REAUTH && !large_request)
517 goto retry;
518 if (err != HTTP_OK)
519 err = -1;
521 curl_slist_free_all(headers);
522 free(gzip_body);
523 return err;
526 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
528 const char *svc = rpc->service_name;
529 struct strbuf buf = STRBUF_INIT;
530 struct strbuf *preamble = rpc->stdin_preamble;
531 struct child_process client;
532 int err = 0;
534 memset(&client, 0, sizeof(client));
535 client.in = -1;
536 client.out = -1;
537 client.git_cmd = 1;
538 client.argv = rpc->argv;
539 if (start_command(&client))
540 exit(1);
541 if (preamble)
542 write_or_die(client.in, preamble->buf, preamble->len);
543 if (heads)
544 write_or_die(client.in, heads->buf, heads->len);
546 rpc->alloc = http_post_buffer;
547 rpc->buf = xmalloc(rpc->alloc);
548 rpc->in = client.in;
549 rpc->out = client.out;
550 strbuf_init(&rpc->result, 0);
552 strbuf_addf(&buf, "%s%s", url, svc);
553 rpc->service_url = strbuf_detach(&buf, NULL);
555 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
556 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
558 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
559 rpc->hdr_accept = strbuf_detach(&buf, NULL);
561 while (!err) {
562 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
563 if (!n)
564 break;
565 rpc->pos = 0;
566 rpc->len = n;
567 err |= post_rpc(rpc);
570 close(client.in);
571 client.in = -1;
572 if (!err) {
573 strbuf_read(&rpc->result, client.out, 0);
574 } else {
575 char buf[4096];
576 for (;;)
577 if (xread(client.out, buf, sizeof(buf)) <= 0)
578 break;
581 close(client.out);
582 client.out = -1;
584 err |= finish_command(&client);
585 free(rpc->service_url);
586 free(rpc->hdr_content_type);
587 free(rpc->hdr_accept);
588 free(rpc->buf);
589 strbuf_release(&buf);
590 return err;
593 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
595 struct walker *walker;
596 char **targets = xmalloc(nr_heads * sizeof(char*));
597 int ret, i;
599 if (options.depth)
600 die("dumb http transport does not support --depth");
601 for (i = 0; i < nr_heads; i++)
602 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
604 walker = get_http_walker(url);
605 walker->get_all = 1;
606 walker->get_tree = 1;
607 walker->get_history = 1;
608 walker->get_verbosely = options.verbosity >= 3;
609 walker->get_recover = 0;
610 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
611 walker_free(walker);
613 for (i = 0; i < nr_heads; i++)
614 free(targets[i]);
615 free(targets);
617 return ret ? error("Fetch failed.") : 0;
620 static int fetch_git(struct discovery *heads,
621 int nr_heads, struct ref **to_fetch)
623 struct rpc_state rpc;
624 struct strbuf preamble = STRBUF_INIT;
625 char *depth_arg = NULL;
626 int argc = 0, i, err;
627 const char *argv[15];
629 argv[argc++] = "fetch-pack";
630 argv[argc++] = "--stateless-rpc";
631 argv[argc++] = "--stdin";
632 argv[argc++] = "--lock-pack";
633 if (options.followtags)
634 argv[argc++] = "--include-tag";
635 if (options.thin)
636 argv[argc++] = "--thin";
637 if (options.verbosity >= 3) {
638 argv[argc++] = "-v";
639 argv[argc++] = "-v";
641 if (!options.progress)
642 argv[argc++] = "--no-progress";
643 if (options.depth) {
644 struct strbuf buf = STRBUF_INIT;
645 strbuf_addf(&buf, "--depth=%lu", options.depth);
646 depth_arg = strbuf_detach(&buf, NULL);
647 argv[argc++] = depth_arg;
649 argv[argc++] = url;
650 argv[argc++] = NULL;
652 for (i = 0; i < nr_heads; i++) {
653 struct ref *ref = to_fetch[i];
654 if (!ref->name || !*ref->name)
655 die("cannot fetch by sha1 over smart http");
656 packet_buf_write(&preamble, "%s\n", ref->name);
658 packet_buf_flush(&preamble);
660 memset(&rpc, 0, sizeof(rpc));
661 rpc.service_name = "git-upload-pack",
662 rpc.argv = argv;
663 rpc.stdin_preamble = &preamble;
664 rpc.gzip_request = 1;
666 err = rpc_service(&rpc, heads);
667 if (rpc.result.len)
668 write_or_die(1, rpc.result.buf, rpc.result.len);
669 strbuf_release(&rpc.result);
670 strbuf_release(&preamble);
671 free(depth_arg);
672 return err;
675 static int fetch(int nr_heads, struct ref **to_fetch)
677 struct discovery *d = discover_refs("git-upload-pack", 0);
678 if (d->proto_git)
679 return fetch_git(d, nr_heads, to_fetch);
680 else
681 return fetch_dumb(nr_heads, to_fetch);
684 static void parse_fetch(struct strbuf *buf)
686 struct ref **to_fetch = NULL;
687 struct ref *list_head = NULL;
688 struct ref **list = &list_head;
689 int alloc_heads = 0, nr_heads = 0;
691 do {
692 if (!prefixcmp(buf->buf, "fetch ")) {
693 char *p = buf->buf + strlen("fetch ");
694 char *name;
695 struct ref *ref;
696 unsigned char old_sha1[20];
698 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
699 die("protocol error: expected sha/ref, got %s'", p);
700 if (p[40] == ' ')
701 name = p + 41;
702 else if (!p[40])
703 name = "";
704 else
705 die("protocol error: expected sha/ref, got %s'", p);
707 ref = alloc_ref(name);
708 hashcpy(ref->old_sha1, old_sha1);
710 *list = ref;
711 list = &ref->next;
713 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
714 to_fetch[nr_heads++] = ref;
716 else
717 die("http transport does not support %s", buf->buf);
719 strbuf_reset(buf);
720 if (strbuf_getline(buf, stdin, '\n') == EOF)
721 return;
722 if (!*buf->buf)
723 break;
724 } while (1);
726 if (fetch(nr_heads, to_fetch))
727 exit(128); /* error already reported */
728 free_refs(list_head);
729 free(to_fetch);
731 printf("\n");
732 fflush(stdout);
733 strbuf_reset(buf);
736 static int push_dav(int nr_spec, char **specs)
738 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
739 int argc = 0, i;
741 argv[argc++] = "http-push";
742 argv[argc++] = "--helper-status";
743 if (options.dry_run)
744 argv[argc++] = "--dry-run";
745 if (options.verbosity > 1)
746 argv[argc++] = "--verbose";
747 argv[argc++] = url;
748 for (i = 0; i < nr_spec; i++)
749 argv[argc++] = specs[i];
750 argv[argc++] = NULL;
752 if (run_command_v_opt(argv, RUN_GIT_CMD))
753 die("git-%s failed", argv[0]);
754 free(argv);
755 return 0;
758 static int push_git(struct discovery *heads, int nr_spec, char **specs)
760 struct rpc_state rpc;
761 const char **argv;
762 int argc = 0, i, err;
764 argv = xmalloc((10 + nr_spec) * sizeof(char*));
765 argv[argc++] = "send-pack";
766 argv[argc++] = "--stateless-rpc";
767 argv[argc++] = "--helper-status";
768 if (options.thin)
769 argv[argc++] = "--thin";
770 if (options.dry_run)
771 argv[argc++] = "--dry-run";
772 if (options.verbosity == 0)
773 argv[argc++] = "--quiet";
774 else if (options.verbosity > 1)
775 argv[argc++] = "--verbose";
776 argv[argc++] = options.progress ? "--progress" : "--no-progress";
777 argv[argc++] = url;
778 for (i = 0; i < nr_spec; i++)
779 argv[argc++] = specs[i];
780 argv[argc++] = NULL;
782 memset(&rpc, 0, sizeof(rpc));
783 rpc.service_name = "git-receive-pack",
784 rpc.argv = argv;
786 err = rpc_service(&rpc, heads);
787 if (rpc.result.len)
788 write_or_die(1, rpc.result.buf, rpc.result.len);
789 strbuf_release(&rpc.result);
790 free(argv);
791 return err;
794 static int push(int nr_spec, char **specs)
796 struct discovery *heads = discover_refs("git-receive-pack", 1);
797 int ret;
799 if (heads->proto_git)
800 ret = push_git(heads, nr_spec, specs);
801 else
802 ret = push_dav(nr_spec, specs);
803 free_discovery(heads);
804 return ret;
807 static void parse_push(struct strbuf *buf)
809 char **specs = NULL;
810 int alloc_spec = 0, nr_spec = 0, i, ret;
812 do {
813 if (!prefixcmp(buf->buf, "push ")) {
814 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
815 specs[nr_spec++] = xstrdup(buf->buf + 5);
817 else
818 die("http transport does not support %s", buf->buf);
820 strbuf_reset(buf);
821 if (strbuf_getline(buf, stdin, '\n') == EOF)
822 goto free_specs;
823 if (!*buf->buf)
824 break;
825 } while (1);
827 ret = push(nr_spec, specs);
828 printf("\n");
829 fflush(stdout);
831 if (ret)
832 exit(128); /* error already reported */
834 free_specs:
835 for (i = 0; i < nr_spec; i++)
836 free(specs[i]);
837 free(specs);
840 int main(int argc, const char **argv)
842 struct strbuf buf = STRBUF_INIT;
843 int nongit;
845 git_extract_argv0_path(argv[0]);
846 setup_git_directory_gently(&nongit);
847 if (argc < 2) {
848 fprintf(stderr, "Remote needed\n");
849 return 1;
852 options.verbosity = 1;
853 options.progress = !!isatty(2);
854 options.thin = 1;
856 remote = remote_get(argv[1]);
858 if (argc > 2) {
859 end_url_with_slash(&buf, argv[2]);
860 } else {
861 end_url_with_slash(&buf, remote->url[0]);
864 url = strbuf_detach(&buf, NULL);
866 http_init(remote, url, 0);
868 do {
869 if (strbuf_getline(&buf, stdin, '\n') == EOF) {
870 if (ferror(stdin))
871 fprintf(stderr, "Error reading command stream\n");
872 else
873 fprintf(stderr, "Unexpected end of command stream\n");
874 return 1;
876 if (buf.len == 0)
877 break;
878 if (!prefixcmp(buf.buf, "fetch ")) {
879 if (nongit)
880 die("Fetch attempted without a local repo");
881 parse_fetch(&buf);
883 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
884 int for_push = !!strstr(buf.buf + 4, "for-push");
885 output_refs(get_refs(for_push));
887 } else if (!prefixcmp(buf.buf, "push ")) {
888 parse_push(&buf);
890 } else if (!prefixcmp(buf.buf, "option ")) {
891 char *name = buf.buf + strlen("option ");
892 char *value = strchr(name, ' ');
893 int result;
895 if (value)
896 *value++ = '\0';
897 else
898 value = "true";
900 result = set_option(name, value);
901 if (!result)
902 printf("ok\n");
903 else if (result < 0)
904 printf("error invalid value\n");
905 else
906 printf("unsupported\n");
907 fflush(stdout);
909 } else if (!strcmp(buf.buf, "capabilities")) {
910 printf("fetch\n");
911 printf("option\n");
912 printf("push\n");
913 printf("\n");
914 fflush(stdout);
915 } else {
916 fprintf(stderr, "Unknown command '%s'\n", buf.buf);
917 return 1;
919 strbuf_reset(&buf);
920 } while (1);
922 http_cleanup();
924 return 0;