url: decode buffers that are not NUL-terminated
[git/dscho.git] / remote-curl.c
blobfaaeda44a9225ff56d45e9566cccf1a2278abb27
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 unsigned proto_git : 1;
81 static struct discovery *last_discovery;
83 static void free_discovery(struct discovery *d)
85 if (d) {
86 if (d == last_discovery)
87 last_discovery = NULL;
88 free(d->buf_alloc);
89 free(d);
93 static struct discovery* discover_refs(const char *service)
95 struct strbuf buffer = STRBUF_INIT;
96 struct discovery *last = last_discovery;
97 char *refs_url;
98 int http_ret, is_http = 0, proto_git_candidate = 1;
100 if (last && !strcmp(service, last->service))
101 return last;
102 free_discovery(last);
104 strbuf_addf(&buffer, "%sinfo/refs", url);
105 if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) {
106 is_http = 1;
107 if (!strchr(url, '?'))
108 strbuf_addch(&buffer, '?');
109 else
110 strbuf_addch(&buffer, '&');
111 strbuf_addf(&buffer, "service=%s", service);
113 refs_url = strbuf_detach(&buffer, NULL);
115 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
117 /* try again with "plain" url (no ? or & appended) */
118 if (http_ret != HTTP_OK) {
119 free(refs_url);
120 strbuf_reset(&buffer);
122 proto_git_candidate = 0;
123 strbuf_addf(&buffer, "%sinfo/refs", url);
124 refs_url = strbuf_detach(&buffer, NULL);
126 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
129 switch (http_ret) {
130 case HTTP_OK:
131 break;
132 case HTTP_MISSING_TARGET:
133 die("%s not found: did you run git update-server-info on the"
134 " server?", refs_url);
135 case HTTP_NOAUTH:
136 die("Authentication failed");
137 default:
138 http_error(refs_url, http_ret);
139 die("HTTP request failed");
142 last= xcalloc(1, sizeof(*last_discovery));
143 last->service = service;
144 last->buf_alloc = strbuf_detach(&buffer, &last->len);
145 last->buf = last->buf_alloc;
147 if (is_http && proto_git_candidate
148 && 5 <= last->len && last->buf[4] == '#') {
149 /* smart HTTP response; validate that the service
150 * pkt-line matches our request.
152 struct strbuf exp = STRBUF_INIT;
154 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
155 die("%s has invalid packet header", refs_url);
156 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
157 strbuf_setlen(&buffer, buffer.len - 1);
159 strbuf_addf(&exp, "# service=%s", service);
160 if (strbuf_cmp(&exp, &buffer))
161 die("invalid server response; got '%s'", buffer.buf);
162 strbuf_release(&exp);
164 /* The header can include additional metadata lines, up
165 * until a packet flush marker. Ignore these now, but
166 * in the future we might start to scan them.
168 strbuf_reset(&buffer);
169 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
170 strbuf_reset(&buffer);
172 last->proto_git = 1;
175 free(refs_url);
176 strbuf_release(&buffer);
177 last_discovery = last;
178 return last;
181 static int write_discovery(int in, int out, void *data)
183 struct discovery *heads = data;
184 int err = 0;
185 if (write_in_full(out, heads->buf, heads->len) != heads->len)
186 err = 1;
187 close(out);
188 return err;
191 static struct ref *parse_git_refs(struct discovery *heads)
193 struct ref *list = NULL;
194 struct async async;
196 memset(&async, 0, sizeof(async));
197 async.proc = write_discovery;
198 async.data = heads;
199 async.out = -1;
201 if (start_async(&async))
202 die("cannot start thread to parse advertised refs");
203 get_remote_heads(async.out, &list, 0, NULL, 0, NULL);
204 close(async.out);
205 if (finish_async(&async))
206 die("ref parsing thread failed");
207 return list;
210 static struct ref *parse_info_refs(struct discovery *heads)
212 char *data, *start, *mid;
213 char *ref_name;
214 int i = 0;
216 struct ref *refs = NULL;
217 struct ref *ref = NULL;
218 struct ref *last_ref = NULL;
220 data = heads->buf;
221 start = NULL;
222 mid = data;
223 while (i < heads->len) {
224 if (!start) {
225 start = &data[i];
227 if (data[i] == '\t')
228 mid = &data[i];
229 if (data[i] == '\n') {
230 data[i] = 0;
231 ref_name = mid + 1;
232 ref = xmalloc(sizeof(struct ref) +
233 strlen(ref_name) + 1);
234 memset(ref, 0, sizeof(struct ref));
235 strcpy(ref->name, ref_name);
236 get_sha1_hex(start, ref->old_sha1);
237 if (!refs)
238 refs = ref;
239 if (last_ref)
240 last_ref->next = ref;
241 last_ref = ref;
242 start = NULL;
244 i++;
247 ref = alloc_ref("HEAD");
248 if (!http_fetch_ref(url, ref) &&
249 !resolve_remote_symref(ref, refs)) {
250 ref->next = refs;
251 refs = ref;
252 } else {
253 free(ref);
256 return refs;
259 static struct ref *get_refs(int for_push)
261 struct discovery *heads;
263 if (for_push)
264 heads = discover_refs("git-receive-pack");
265 else
266 heads = discover_refs("git-upload-pack");
268 if (heads->proto_git)
269 return parse_git_refs(heads);
270 return parse_info_refs(heads);
273 static void output_refs(struct ref *refs)
275 struct ref *posn;
276 for (posn = refs; posn; posn = posn->next) {
277 if (posn->symref)
278 printf("@%s %s\n", posn->symref, posn->name);
279 else
280 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
282 printf("\n");
283 fflush(stdout);
284 free_refs(refs);
287 struct rpc_state {
288 const char *service_name;
289 const char **argv;
290 char *service_url;
291 char *hdr_content_type;
292 char *hdr_accept;
293 char *buf;
294 size_t alloc;
295 size_t len;
296 size_t pos;
297 int in;
298 int out;
299 struct strbuf result;
300 unsigned gzip_request : 1;
301 unsigned initial_buffer : 1;
304 static size_t rpc_out(void *ptr, size_t eltsize,
305 size_t nmemb, void *buffer_)
307 size_t max = eltsize * nmemb;
308 struct rpc_state *rpc = buffer_;
309 size_t avail = rpc->len - rpc->pos;
311 if (!avail) {
312 rpc->initial_buffer = 0;
313 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
314 if (!avail)
315 return 0;
316 rpc->pos = 0;
317 rpc->len = avail;
320 if (max < avail)
321 avail = max;
322 memcpy(ptr, rpc->buf + rpc->pos, avail);
323 rpc->pos += avail;
324 return avail;
327 #ifndef NO_CURL_IOCTL
328 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
330 struct rpc_state *rpc = clientp;
332 switch (cmd) {
333 case CURLIOCMD_NOP:
334 return CURLIOE_OK;
336 case CURLIOCMD_RESTARTREAD:
337 if (rpc->initial_buffer) {
338 rpc->pos = 0;
339 return CURLIOE_OK;
341 fprintf(stderr, "Unable to rewind rpc post data - try increasing http.postBuffer\n");
342 return CURLIOE_FAILRESTART;
344 default:
345 return CURLIOE_UNKNOWNCMD;
348 #endif
350 static size_t rpc_in(char *ptr, size_t eltsize,
351 size_t nmemb, void *buffer_)
353 size_t size = eltsize * nmemb;
354 struct rpc_state *rpc = buffer_;
355 write_or_die(rpc->in, ptr, size);
356 return size;
359 static int run_slot(struct active_request_slot *slot)
361 int err = 0;
362 struct slot_results results;
364 slot->results = &results;
365 slot->curl_result = curl_easy_perform(slot->curl);
366 finish_active_slot(slot);
368 if (results.curl_result != CURLE_OK) {
369 err |= error("RPC failed; result=%d, HTTP code = %ld",
370 results.curl_result, results.http_code);
373 return err;
376 static int probe_rpc(struct rpc_state *rpc)
378 struct active_request_slot *slot;
379 struct curl_slist *headers = NULL;
380 struct strbuf buf = STRBUF_INIT;
381 int err;
383 slot = get_active_slot();
385 headers = curl_slist_append(headers, rpc->hdr_content_type);
386 headers = curl_slist_append(headers, rpc->hdr_accept);
388 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
389 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
390 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
391 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
392 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
393 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
394 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
395 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
396 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
398 err = run_slot(slot);
400 curl_slist_free_all(headers);
401 strbuf_release(&buf);
402 return err;
405 static int post_rpc(struct rpc_state *rpc)
407 struct active_request_slot *slot;
408 struct curl_slist *headers = NULL;
409 int use_gzip = rpc->gzip_request;
410 char *gzip_body = NULL;
411 int err, large_request = 0;
413 /* Try to load the entire request, if we can fit it into the
414 * allocated buffer space we can use HTTP/1.0 and avoid the
415 * chunked encoding mess.
417 while (1) {
418 size_t left = rpc->alloc - rpc->len;
419 char *buf = rpc->buf + rpc->len;
420 int n;
422 if (left < LARGE_PACKET_MAX) {
423 large_request = 1;
424 use_gzip = 0;
425 break;
428 n = packet_read_line(rpc->out, buf, left);
429 if (!n)
430 break;
431 rpc->len += n;
434 if (large_request) {
435 err = probe_rpc(rpc);
436 if (err)
437 return err;
440 slot = get_active_slot();
442 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
443 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
444 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
445 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
447 headers = curl_slist_append(headers, rpc->hdr_content_type);
448 headers = curl_slist_append(headers, rpc->hdr_accept);
449 headers = curl_slist_append(headers, "Expect:");
451 if (large_request) {
452 /* The request body is large and the size cannot be predicted.
453 * We must use chunked encoding to send it.
455 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
456 rpc->initial_buffer = 1;
457 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
458 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
459 #ifndef NO_CURL_IOCTL
460 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
461 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
462 #endif
463 if (options.verbosity > 1) {
464 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
465 fflush(stderr);
468 } else if (use_gzip && 1024 < rpc->len) {
469 /* The client backend isn't giving us compressed data so
470 * we can try to deflate it ourselves, this may save on.
471 * the transfer time.
473 size_t size;
474 git_zstream stream;
475 int ret;
477 memset(&stream, 0, sizeof(stream));
478 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
479 size = git_deflate_bound(&stream, rpc->len);
480 gzip_body = xmalloc(size);
482 stream.next_in = (unsigned char *)rpc->buf;
483 stream.avail_in = rpc->len;
484 stream.next_out = (unsigned char *)gzip_body;
485 stream.avail_out = size;
487 ret = git_deflate(&stream, Z_FINISH);
488 if (ret != Z_STREAM_END)
489 die("cannot deflate request; zlib deflate error %d", ret);
491 ret = git_deflate_end_gently(&stream);
492 if (ret != Z_OK)
493 die("cannot deflate request; zlib end error %d", ret);
495 size = stream.total_out;
497 headers = curl_slist_append(headers, "Content-Encoding: gzip");
498 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
499 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
501 if (options.verbosity > 1) {
502 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
503 rpc->service_name,
504 (unsigned long)rpc->len, (unsigned long)size);
505 fflush(stderr);
507 } else {
508 /* We know the complete request size in advance, use the
509 * more normal Content-Length approach.
511 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
512 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
513 if (options.verbosity > 1) {
514 fprintf(stderr, "POST %s (%lu bytes)\n",
515 rpc->service_name, (unsigned long)rpc->len);
516 fflush(stderr);
520 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
521 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
522 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
524 err = run_slot(slot);
526 curl_slist_free_all(headers);
527 free(gzip_body);
528 return err;
531 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
533 const char *svc = rpc->service_name;
534 struct strbuf buf = STRBUF_INIT;
535 struct child_process client;
536 int err = 0;
538 memset(&client, 0, sizeof(client));
539 client.in = -1;
540 client.out = -1;
541 client.git_cmd = 1;
542 client.argv = rpc->argv;
543 if (start_command(&client))
544 exit(1);
545 if (heads)
546 write_or_die(client.in, heads->buf, heads->len);
548 rpc->alloc = http_post_buffer;
549 rpc->buf = xmalloc(rpc->alloc);
550 rpc->in = client.in;
551 rpc->out = client.out;
552 strbuf_init(&rpc->result, 0);
554 strbuf_addf(&buf, "%s%s", url, svc);
555 rpc->service_url = strbuf_detach(&buf, NULL);
557 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
558 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
560 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
561 rpc->hdr_accept = strbuf_detach(&buf, NULL);
563 while (!err) {
564 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
565 if (!n)
566 break;
567 rpc->pos = 0;
568 rpc->len = n;
569 err |= post_rpc(rpc);
572 close(client.in);
573 client.in = -1;
574 strbuf_read(&rpc->result, client.out, 0);
576 close(client.out);
577 client.out = -1;
579 err |= finish_command(&client);
580 free(rpc->service_url);
581 free(rpc->hdr_content_type);
582 free(rpc->hdr_accept);
583 free(rpc->buf);
584 strbuf_release(&buf);
585 return err;
588 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
590 struct walker *walker;
591 char **targets = xmalloc(nr_heads * sizeof(char*));
592 int ret, i;
594 if (options.depth)
595 die("dumb http transport does not support --depth");
596 for (i = 0; i < nr_heads; i++)
597 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
599 walker = get_http_walker(url);
600 walker->get_all = 1;
601 walker->get_tree = 1;
602 walker->get_history = 1;
603 walker->get_verbosely = options.verbosity >= 3;
604 walker->get_recover = 0;
605 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
606 walker_free(walker);
608 for (i = 0; i < nr_heads; i++)
609 free(targets[i]);
610 free(targets);
612 return ret ? error("Fetch failed.") : 0;
615 static int fetch_git(struct discovery *heads,
616 int nr_heads, struct ref **to_fetch)
618 struct rpc_state rpc;
619 char *depth_arg = NULL;
620 const char **argv;
621 int argc = 0, i, err;
623 argv = xmalloc((15 + nr_heads) * sizeof(char*));
624 argv[argc++] = "fetch-pack";
625 argv[argc++] = "--stateless-rpc";
626 argv[argc++] = "--lock-pack";
627 if (options.followtags)
628 argv[argc++] = "--include-tag";
629 if (options.thin)
630 argv[argc++] = "--thin";
631 if (options.verbosity >= 3) {
632 argv[argc++] = "-v";
633 argv[argc++] = "-v";
635 if (!options.progress)
636 argv[argc++] = "--no-progress";
637 if (options.depth) {
638 struct strbuf buf = STRBUF_INIT;
639 strbuf_addf(&buf, "--depth=%lu", options.depth);
640 depth_arg = strbuf_detach(&buf, NULL);
641 argv[argc++] = depth_arg;
643 argv[argc++] = url;
644 for (i = 0; i < nr_heads; i++) {
645 struct ref *ref = to_fetch[i];
646 if (!ref->name || !*ref->name)
647 die("cannot fetch by sha1 over smart http");
648 argv[argc++] = ref->name;
650 argv[argc++] = NULL;
652 memset(&rpc, 0, sizeof(rpc));
653 rpc.service_name = "git-upload-pack",
654 rpc.argv = argv;
655 rpc.gzip_request = 1;
657 err = rpc_service(&rpc, heads);
658 if (rpc.result.len)
659 safe_write(1, rpc.result.buf, rpc.result.len);
660 strbuf_release(&rpc.result);
661 free(argv);
662 free(depth_arg);
663 return err;
666 static int fetch(int nr_heads, struct ref **to_fetch)
668 struct discovery *d = discover_refs("git-upload-pack");
669 if (d->proto_git)
670 return fetch_git(d, nr_heads, to_fetch);
671 else
672 return fetch_dumb(nr_heads, to_fetch);
675 static void parse_fetch(struct strbuf *buf)
677 struct ref **to_fetch = NULL;
678 struct ref *list_head = NULL;
679 struct ref **list = &list_head;
680 int alloc_heads = 0, nr_heads = 0;
682 do {
683 if (!prefixcmp(buf->buf, "fetch ")) {
684 char *p = buf->buf + strlen("fetch ");
685 char *name;
686 struct ref *ref;
687 unsigned char old_sha1[20];
689 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
690 die("protocol error: expected sha/ref, got %s'", p);
691 if (p[40] == ' ')
692 name = p + 41;
693 else if (!p[40])
694 name = "";
695 else
696 die("protocol error: expected sha/ref, got %s'", p);
698 ref = alloc_ref(name);
699 hashcpy(ref->old_sha1, old_sha1);
701 *list = ref;
702 list = &ref->next;
704 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
705 to_fetch[nr_heads++] = ref;
707 else
708 die("http transport does not support %s", buf->buf);
710 strbuf_reset(buf);
711 if (strbuf_getline(buf, stdin, '\n') == EOF)
712 return;
713 if (!*buf->buf)
714 break;
715 } while (1);
717 if (fetch(nr_heads, to_fetch))
718 exit(128); /* error already reported */
719 free_refs(list_head);
720 free(to_fetch);
722 printf("\n");
723 fflush(stdout);
724 strbuf_reset(buf);
727 static int push_dav(int nr_spec, char **specs)
729 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
730 int argc = 0, i;
732 argv[argc++] = "http-push";
733 argv[argc++] = "--helper-status";
734 if (options.dry_run)
735 argv[argc++] = "--dry-run";
736 if (options.verbosity > 1)
737 argv[argc++] = "--verbose";
738 argv[argc++] = url;
739 for (i = 0; i < nr_spec; i++)
740 argv[argc++] = specs[i];
741 argv[argc++] = NULL;
743 if (run_command_v_opt(argv, RUN_GIT_CMD))
744 die("git-%s failed", argv[0]);
745 free(argv);
746 return 0;
749 static int push_git(struct discovery *heads, int nr_spec, char **specs)
751 struct rpc_state rpc;
752 const char **argv;
753 int argc = 0, i, err;
755 argv = xmalloc((10 + nr_spec) * sizeof(char*));
756 argv[argc++] = "send-pack";
757 argv[argc++] = "--stateless-rpc";
758 argv[argc++] = "--helper-status";
759 if (options.thin)
760 argv[argc++] = "--thin";
761 if (options.dry_run)
762 argv[argc++] = "--dry-run";
763 if (options.verbosity > 1)
764 argv[argc++] = "--verbose";
765 argv[argc++] = url;
766 for (i = 0; i < nr_spec; i++)
767 argv[argc++] = specs[i];
768 argv[argc++] = NULL;
770 memset(&rpc, 0, sizeof(rpc));
771 rpc.service_name = "git-receive-pack",
772 rpc.argv = argv;
774 err = rpc_service(&rpc, heads);
775 if (rpc.result.len)
776 safe_write(1, rpc.result.buf, rpc.result.len);
777 strbuf_release(&rpc.result);
778 free(argv);
779 return err;
782 static int push(int nr_spec, char **specs)
784 struct discovery *heads = discover_refs("git-receive-pack");
785 int ret;
787 if (heads->proto_git)
788 ret = push_git(heads, nr_spec, specs);
789 else
790 ret = push_dav(nr_spec, specs);
791 free_discovery(heads);
792 return ret;
795 static void parse_push(struct strbuf *buf)
797 char **specs = NULL;
798 int alloc_spec = 0, nr_spec = 0, i;
800 do {
801 if (!prefixcmp(buf->buf, "push ")) {
802 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
803 specs[nr_spec++] = xstrdup(buf->buf + 5);
805 else
806 die("http transport does not support %s", buf->buf);
808 strbuf_reset(buf);
809 if (strbuf_getline(buf, stdin, '\n') == EOF)
810 goto free_specs;
811 if (!*buf->buf)
812 break;
813 } while (1);
815 if (push(nr_spec, specs))
816 exit(128); /* error already reported */
818 printf("\n");
819 fflush(stdout);
821 free_specs:
822 for (i = 0; i < nr_spec; i++)
823 free(specs[i]);
824 free(specs);
827 int main(int argc, const char **argv)
829 struct strbuf buf = STRBUF_INIT;
830 int nongit;
832 git_extract_argv0_path(argv[0]);
833 setup_git_directory_gently(&nongit);
834 if (argc < 2) {
835 fprintf(stderr, "Remote needed\n");
836 return 1;
839 options.verbosity = 1;
840 options.progress = !!isatty(2);
841 options.thin = 1;
843 remote = remote_get(argv[1]);
845 if (argc > 2) {
846 end_url_with_slash(&buf, argv[2]);
847 } else {
848 end_url_with_slash(&buf, remote->url[0]);
851 url = strbuf_detach(&buf, NULL);
853 http_init(remote);
855 do {
856 if (strbuf_getline(&buf, stdin, '\n') == EOF)
857 break;
858 if (!prefixcmp(buf.buf, "fetch ")) {
859 if (nongit)
860 die("Fetch attempted without a local repo");
861 parse_fetch(&buf);
863 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
864 int for_push = !!strstr(buf.buf + 4, "for-push");
865 output_refs(get_refs(for_push));
867 } else if (!prefixcmp(buf.buf, "push ")) {
868 parse_push(&buf);
870 } else if (!prefixcmp(buf.buf, "option ")) {
871 char *name = buf.buf + strlen("option ");
872 char *value = strchr(name, ' ');
873 int result;
875 if (value)
876 *value++ = '\0';
877 else
878 value = "true";
880 result = set_option(name, value);
881 if (!result)
882 printf("ok\n");
883 else if (result < 0)
884 printf("error invalid value\n");
885 else
886 printf("unsupported\n");
887 fflush(stdout);
889 } else if (!strcmp(buf.buf, "capabilities")) {
890 printf("fetch\n");
891 printf("option\n");
892 printf("push\n");
893 printf("\n");
894 fflush(stdout);
895 } else {
896 return 1;
898 strbuf_reset(&buf);
899 } while (1);
901 http_cleanup();
903 return 0;