]> git.ipfire.org Git - thirdparty/git.git/blame - remote-curl.c
Merge branch 'jc/maint-imap-config-parse' into maint
[thirdparty/git.git] / remote-curl.c
CommitLineData
a2d725b7
DB
1#include "cache.h"
2#include "remote.h"
3#include "strbuf.h"
4#include "walker.h"
5#include "http.h"
d01a8e32 6#include "exec_cmd.h"
ae4efe19 7#include "run-command.h"
97cc7bc4 8#include "pkt-line.h"
de1a2fdd 9#include "sideband.h"
a2d725b7 10
37a8768f
SP
11static struct remote *remote;
12static const char *url;
13static struct walker *walker;
14
ef08ef9e
SP
15struct options {
16 int verbosity;
17 unsigned long depth;
18 unsigned progress : 1,
ae4efe19 19 followtags : 1,
de1a2fdd
SP
20 dry_run : 1,
21 thin : 1;
ef08ef9e
SP
22};
23static struct options options;
24
37a8768f
SP
25static void init_walker(void)
26{
27 if (!walker)
28 walker = get_http_walker(url, remote);
29}
30
ef08ef9e
SP
31static int set_option(const char *name, const char *value)
32{
33 if (!strcmp(name, "verbosity")) {
34 char *end;
35 int v = strtol(value, &end, 10);
36 if (value == end || *end)
37 return -1;
38 options.verbosity = v;
39 return 0;
40 }
41 else if (!strcmp(name, "progress")) {
42 if (!strcmp(value, "true"))
43 options.progress = 1;
44 else if (!strcmp(value, "false"))
45 options.progress = 0;
46 else
47 return -1;
249b2004 48 return 0;
ef08ef9e
SP
49 }
50 else if (!strcmp(name, "depth")) {
51 char *end;
52 unsigned long v = strtoul(value, &end, 10);
53 if (value == end || *end)
54 return -1;
55 options.depth = v;
249b2004 56 return 0;
ef08ef9e
SP
57 }
58 else if (!strcmp(name, "followtags")) {
59 if (!strcmp(value, "true"))
60 options.followtags = 1;
61 else if (!strcmp(value, "false"))
62 options.followtags = 0;
63 else
64 return -1;
249b2004 65 return 0;
ef08ef9e 66 }
ae4efe19
SP
67 else if (!strcmp(name, "dry-run")) {
68 if (!strcmp(value, "true"))
69 options.dry_run = 1;
70 else if (!strcmp(value, "false"))
71 options.dry_run = 0;
72 else
73 return -1;
74 return 0;
75 }
ef08ef9e
SP
76 else {
77 return 1 /* unsupported */;
78 }
79}
80
97cc7bc4
SP
81struct discovery {
82 const char *service;
83 char *buf_alloc;
84 char *buf;
85 size_t len;
86 unsigned proto_git : 1;
87};
88static struct discovery *last_discovery;
89
90static void free_discovery(struct discovery *d)
91{
92 if (d) {
93 if (d == last_discovery)
94 last_discovery = NULL;
95 free(d->buf_alloc);
96 free(d);
97 }
98}
99
100static struct discovery* discover_refs(const char *service)
a2d725b7
DB
101{
102 struct strbuf buffer = STRBUF_INIT;
97cc7bc4 103 struct discovery *last = last_discovery;
a2d725b7 104 char *refs_url;
703e6e76 105 int http_ret, is_http = 0, proto_git_candidate = 1;
a2d725b7 106
97cc7bc4
SP
107 if (last && !strcmp(service, last->service))
108 return last;
109 free_discovery(last);
a2d725b7 110
97cc7bc4
SP
111 strbuf_addf(&buffer, "%s/info/refs", url);
112 if (!prefixcmp(url, "http://") || !prefixcmp(url, "https://")) {
113 is_http = 1;
114 if (!strchr(url, '?'))
115 strbuf_addch(&buffer, '?');
116 else
117 strbuf_addch(&buffer, '&');
118 strbuf_addf(&buffer, "service=%s", service);
119 }
120 refs_url = strbuf_detach(&buffer, NULL);
a2d725b7 121
37a8768f 122 init_walker();
a2d725b7 123 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
703e6e76
TRC
124
125 /* try again with "plain" url (no ? or & appended) */
126 if (http_ret != HTTP_OK) {
127 free(refs_url);
128 strbuf_reset(&buffer);
129
130 proto_git_candidate = 0;
131 strbuf_addf(&buffer, "%s/info/refs", url);
132 refs_url = strbuf_detach(&buffer, NULL);
133
134 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
135 }
136
a2d725b7
DB
137 switch (http_ret) {
138 case HTTP_OK:
139 break;
140 case HTTP_MISSING_TARGET:
141 die("%s not found: did you run git update-server-info on the"
142 " server?", refs_url);
143 default:
144 http_error(refs_url, http_ret);
145 die("HTTP request failed");
146 }
147
97cc7bc4
SP
148 last= xcalloc(1, sizeof(*last_discovery));
149 last->service = service;
150 last->buf_alloc = strbuf_detach(&buffer, &last->len);
151 last->buf = last->buf_alloc;
152
703e6e76
TRC
153 if (is_http && proto_git_candidate
154 && 5 <= last->len && last->buf[4] == '#') {
97cc7bc4
SP
155 /* smart HTTP response; validate that the service
156 * pkt-line matches our request.
157 */
158 struct strbuf exp = STRBUF_INIT;
159
160 if (packet_get_line(&buffer, &last->buf, &last->len) <= 0)
161 die("%s has invalid packet header", refs_url);
162 if (buffer.len && buffer.buf[buffer.len - 1] == '\n')
163 strbuf_setlen(&buffer, buffer.len - 1);
164
165 strbuf_addf(&exp, "# service=%s", service);
166 if (strbuf_cmp(&exp, &buffer))
167 die("invalid server response; got '%s'", buffer.buf);
168 strbuf_release(&exp);
169
170 /* The header can include additional metadata lines, up
171 * until a packet flush marker. Ignore these now, but
172 * in the future we might start to scan them.
173 */
174 strbuf_reset(&buffer);
175 while (packet_get_line(&buffer, &last->buf, &last->len) > 0)
176 strbuf_reset(&buffer);
177
178 last->proto_git = 1;
179 }
180
181 free(refs_url);
182 strbuf_release(&buffer);
183 last_discovery = last;
184 return last;
185}
186
187static int write_discovery(int fd, void *data)
188{
189 struct discovery *heads = data;
190 int err = 0;
191 if (write_in_full(fd, heads->buf, heads->len) != heads->len)
192 err = 1;
193 close(fd);
194 return err;
195}
196
197static struct ref *parse_git_refs(struct discovery *heads)
198{
199 struct ref *list = NULL;
200 struct async async;
201
202 memset(&async, 0, sizeof(async));
203 async.proc = write_discovery;
204 async.data = heads;
205
206 if (start_async(&async))
207 die("cannot start thread to parse advertised refs");
208 get_remote_heads(async.out, &list, 0, NULL, 0, NULL);
209 close(async.out);
210 if (finish_async(&async))
211 die("ref parsing thread failed");
212 return list;
213}
214
215static struct ref *parse_info_refs(struct discovery *heads)
216{
217 char *data, *start, *mid;
218 char *ref_name;
219 int i = 0;
220
221 struct ref *refs = NULL;
222 struct ref *ref = NULL;
223 struct ref *last_ref = NULL;
224
225 data = heads->buf;
a2d725b7
DB
226 start = NULL;
227 mid = data;
97cc7bc4 228 while (i < heads->len) {
a2d725b7
DB
229 if (!start) {
230 start = &data[i];
231 }
232 if (data[i] == '\t')
233 mid = &data[i];
234 if (data[i] == '\n') {
235 data[i] = 0;
236 ref_name = mid + 1;
237 ref = xmalloc(sizeof(struct ref) +
238 strlen(ref_name) + 1);
239 memset(ref, 0, sizeof(struct ref));
240 strcpy(ref->name, ref_name);
241 get_sha1_hex(start, ref->old_sha1);
242 if (!refs)
243 refs = ref;
244 if (last_ref)
245 last_ref->next = ref;
246 last_ref = ref;
247 start = NULL;
248 }
249 i++;
250 }
251
97cc7bc4 252 init_walker();
a2d725b7
DB
253 ref = alloc_ref("HEAD");
254 if (!walker->fetch_ref(walker, ref) &&
255 !resolve_remote_symref(ref, refs)) {
256 ref->next = refs;
257 refs = ref;
258 } else {
259 free(ref);
260 }
261
a2d725b7
DB
262 return refs;
263}
264
97cc7bc4
SP
265static struct ref *get_refs(int for_push)
266{
267 struct discovery *heads;
268
269 if (for_push)
270 heads = discover_refs("git-receive-pack");
271 else
272 heads = discover_refs("git-upload-pack");
273
274 if (heads->proto_git)
275 return parse_git_refs(heads);
276 return parse_info_refs(heads);
277}
278
ae4efe19
SP
279static void output_refs(struct ref *refs)
280{
281 struct ref *posn;
282 for (posn = refs; posn; posn = posn->next) {
283 if (posn->symref)
284 printf("@%s %s\n", posn->symref, posn->name);
285 else
286 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
287 }
288 printf("\n");
289 fflush(stdout);
290 free_refs(refs);
291}
292
de1a2fdd
SP
293struct rpc_state {
294 const char *service_name;
295 const char **argv;
296 char *service_url;
297 char *hdr_content_type;
298 char *hdr_accept;
299 char *buf;
300 size_t alloc;
301 size_t len;
302 size_t pos;
303 int in;
304 int out;
305 struct strbuf result;
b8538603 306 unsigned gzip_request : 1;
de1a2fdd
SP
307};
308
309static size_t rpc_out(void *ptr, size_t eltsize,
310 size_t nmemb, void *buffer_)
311{
312 size_t max = eltsize * nmemb;
313 struct rpc_state *rpc = buffer_;
314 size_t avail = rpc->len - rpc->pos;
315
316 if (!avail) {
317 avail = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
318 if (!avail)
319 return 0;
320 rpc->pos = 0;
321 rpc->len = avail;
322 }
323
48310608 324 if (max < avail)
de1a2fdd
SP
325 avail = max;
326 memcpy(ptr, rpc->buf + rpc->pos, avail);
327 rpc->pos += avail;
328 return avail;
329}
330
331static size_t rpc_in(const void *ptr, size_t eltsize,
332 size_t nmemb, void *buffer_)
333{
334 size_t size = eltsize * nmemb;
335 struct rpc_state *rpc = buffer_;
336 write_or_die(rpc->in, ptr, size);
337 return size;
338}
339
340static int post_rpc(struct rpc_state *rpc)
341{
342 struct active_request_slot *slot;
343 struct slot_results results;
344 struct curl_slist *headers = NULL;
b8538603
SP
345 int use_gzip = rpc->gzip_request;
346 char *gzip_body = NULL;
de1a2fdd
SP
347 int err = 0, large_request = 0;
348
349 /* Try to load the entire request, if we can fit it into the
350 * allocated buffer space we can use HTTP/1.0 and avoid the
351 * chunked encoding mess.
352 */
353 while (1) {
354 size_t left = rpc->alloc - rpc->len;
355 char *buf = rpc->buf + rpc->len;
356 int n;
357
358 if (left < LARGE_PACKET_MAX) {
359 large_request = 1;
b8538603 360 use_gzip = 0;
de1a2fdd
SP
361 break;
362 }
363
364 n = packet_read_line(rpc->out, buf, left);
365 if (!n)
366 break;
367 rpc->len += n;
368 }
369
370 slot = get_active_slot();
371 slot->results = &results;
372
de1a2fdd 373 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
d21f9794 374 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
de1a2fdd 375 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
b8538603 376 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
de1a2fdd
SP
377
378 headers = curl_slist_append(headers, rpc->hdr_content_type);
379 headers = curl_slist_append(headers, rpc->hdr_accept);
380
381 if (large_request) {
382 /* The request body is large and the size cannot be predicted.
383 * We must use chunked encoding to send it.
384 */
385 headers = curl_slist_append(headers, "Expect: 100-continue");
386 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
387 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
388 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
389 if (options.verbosity > 1) {
390 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
391 fflush(stderr);
392 }
393
b8538603
SP
394 } else if (use_gzip && 1024 < rpc->len) {
395 /* The client backend isn't giving us compressed data so
396 * we can try to deflate it ourselves, this may save on.
397 * the transfer time.
398 */
399 size_t size;
400 z_stream stream;
401 int ret;
402
403 memset(&stream, 0, sizeof(stream));
404 ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
405 Z_DEFLATED, (15 + 16),
406 8, Z_DEFAULT_STRATEGY);
407 if (ret != Z_OK)
408 die("cannot deflate request; zlib init error %d", ret);
409 size = deflateBound(&stream, rpc->len);
410 gzip_body = xmalloc(size);
411
412 stream.next_in = (unsigned char *)rpc->buf;
413 stream.avail_in = rpc->len;
414 stream.next_out = (unsigned char *)gzip_body;
415 stream.avail_out = size;
416
417 ret = deflate(&stream, Z_FINISH);
418 if (ret != Z_STREAM_END)
419 die("cannot deflate request; zlib deflate error %d", ret);
420
421 ret = deflateEnd(&stream);
422 if (ret != Z_OK)
423 die("cannot deflate request; zlib end error %d", ret);
424
425 size = stream.total_out;
426
427 headers = curl_slist_append(headers, "Content-Encoding: gzip");
428 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
429 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
430
431 if (options.verbosity > 1) {
432 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
433 rpc->service_name,
434 (unsigned long)rpc->len, (unsigned long)size);
435 fflush(stderr);
436 }
de1a2fdd
SP
437 } else {
438 /* We know the complete request size in advance, use the
439 * more normal Content-Length approach.
440 */
441 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
442 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
443 if (options.verbosity > 1) {
444 fprintf(stderr, "POST %s (%lu bytes)\n",
445 rpc->service_name, (unsigned long)rpc->len);
446 fflush(stderr);
447 }
448 }
449
450 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
451 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
452 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
453
454 slot->curl_result = curl_easy_perform(slot->curl);
455 finish_active_slot(slot);
456
457 if (results.curl_result != CURLE_OK) {
458 err |= error("RPC failed; result=%d, HTTP code = %ld",
459 results.curl_result, results.http_code);
460 }
461
462 curl_slist_free_all(headers);
b8538603 463 free(gzip_body);
de1a2fdd
SP
464 return err;
465}
466
467static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
468{
469 const char *svc = rpc->service_name;
470 struct strbuf buf = STRBUF_INIT;
471 struct child_process client;
472 int err = 0;
473
474 init_walker();
475 memset(&client, 0, sizeof(client));
476 client.in = -1;
477 client.out = -1;
478 client.git_cmd = 1;
479 client.argv = rpc->argv;
480 if (start_command(&client))
481 exit(1);
482 if (heads)
483 write_or_die(client.in, heads->buf, heads->len);
484
485 rpc->alloc = http_post_buffer;
486 rpc->buf = xmalloc(rpc->alloc);
487 rpc->in = client.in;
488 rpc->out = client.out;
489 strbuf_init(&rpc->result, 0);
490
491 strbuf_addf(&buf, "%s/%s", url, svc);
492 rpc->service_url = strbuf_detach(&buf, NULL);
493
494 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
495 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
496
8efa5f62 497 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
de1a2fdd
SP
498 rpc->hdr_accept = strbuf_detach(&buf, NULL);
499
500 while (!err) {
501 int n = packet_read_line(rpc->out, rpc->buf, rpc->alloc);
502 if (!n)
503 break;
504 rpc->pos = 0;
505 rpc->len = n;
506 err |= post_rpc(rpc);
507 }
508 strbuf_read(&rpc->result, client.out, 0);
509
510 close(client.in);
511 close(client.out);
512 client.in = -1;
513 client.out = -1;
514
515 err |= finish_command(&client);
516 free(rpc->service_url);
517 free(rpc->hdr_content_type);
518 free(rpc->hdr_accept);
519 free(rpc->buf);
520 strbuf_release(&buf);
521 return err;
522}
523
292ce46b
SP
524static int fetch_dumb(int nr_heads, struct ref **to_fetch)
525{
526 char **targets = xmalloc(nr_heads * sizeof(char*));
527 int ret, i;
528
249b2004
SP
529 if (options.depth)
530 die("dumb http transport does not support --depth");
292ce46b
SP
531 for (i = 0; i < nr_heads; i++)
532 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
533
534 init_walker();
535 walker->get_all = 1;
536 walker->get_tree = 1;
537 walker->get_history = 1;
ef08ef9e 538 walker->get_verbosely = options.verbosity >= 3;
292ce46b
SP
539 walker->get_recover = 0;
540 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
541
542 for (i = 0; i < nr_heads; i++)
543 free(targets[i]);
544 free(targets);
545
546 return ret ? error("Fetch failed.") : 0;
547}
548
249b2004
SP
549static int fetch_git(struct discovery *heads,
550 int nr_heads, struct ref **to_fetch)
551{
552 struct rpc_state rpc;
553 char *depth_arg = NULL;
554 const char **argv;
555 int argc = 0, i, err;
556
557 argv = xmalloc((15 + nr_heads) * sizeof(char*));
558 argv[argc++] = "fetch-pack";
559 argv[argc++] = "--stateless-rpc";
560 argv[argc++] = "--lock-pack";
561 if (options.followtags)
562 argv[argc++] = "--include-tag";
563 if (options.thin)
564 argv[argc++] = "--thin";
565 if (options.verbosity >= 3) {
566 argv[argc++] = "-v";
567 argv[argc++] = "-v";
568 }
569 if (!options.progress)
570 argv[argc++] = "--no-progress";
571 if (options.depth) {
572 struct strbuf buf = STRBUF_INIT;
573 strbuf_addf(&buf, "--depth=%lu", options.depth);
574 depth_arg = strbuf_detach(&buf, NULL);
575 argv[argc++] = depth_arg;
576 }
577 argv[argc++] = url;
578 for (i = 0; i < nr_heads; i++) {
579 struct ref *ref = to_fetch[i];
580 if (!ref->name || !*ref->name)
581 die("cannot fetch by sha1 over smart http");
582 argv[argc++] = ref->name;
583 }
584 argv[argc++] = NULL;
585
586 memset(&rpc, 0, sizeof(rpc));
587 rpc.service_name = "git-upload-pack",
588 rpc.argv = argv;
b8538603 589 rpc.gzip_request = 1;
249b2004
SP
590
591 err = rpc_service(&rpc, heads);
592 if (rpc.result.len)
593 safe_write(1, rpc.result.buf, rpc.result.len);
594 strbuf_release(&rpc.result);
595 free(argv);
596 free(depth_arg);
597 return err;
598}
599
600static int fetch(int nr_heads, struct ref **to_fetch)
601{
602 struct discovery *d = discover_refs("git-upload-pack");
603 if (d->proto_git)
604 return fetch_git(d, nr_heads, to_fetch);
605 else
606 return fetch_dumb(nr_heads, to_fetch);
607}
608
292ce46b
SP
609static void parse_fetch(struct strbuf *buf)
610{
611 struct ref **to_fetch = NULL;
612 struct ref *list_head = NULL;
613 struct ref **list = &list_head;
614 int alloc_heads = 0, nr_heads = 0;
615
616 do {
617 if (!prefixcmp(buf->buf, "fetch ")) {
618 char *p = buf->buf + strlen("fetch ");
619 char *name;
620 struct ref *ref;
621 unsigned char old_sha1[20];
622
623 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
624 die("protocol error: expected sha/ref, got %s'", p);
625 if (p[40] == ' ')
626 name = p + 41;
627 else if (!p[40])
628 name = "";
629 else
630 die("protocol error: expected sha/ref, got %s'", p);
631
632 ref = alloc_ref(name);
633 hashcpy(ref->old_sha1, old_sha1);
634
635 *list = ref;
636 list = &ref->next;
637
638 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
639 to_fetch[nr_heads++] = ref;
640 }
641 else
642 die("http transport does not support %s", buf->buf);
643
644 strbuf_reset(buf);
645 if (strbuf_getline(buf, stdin, '\n') == EOF)
646 return;
647 if (!*buf->buf)
648 break;
649 } while (1);
650
249b2004 651 if (fetch(nr_heads, to_fetch))
292ce46b
SP
652 exit(128); /* error already reported */
653 free_refs(list_head);
654 free(to_fetch);
655
656 printf("\n");
657 fflush(stdout);
658 strbuf_reset(buf);
659}
660
ae4efe19
SP
661static int push_dav(int nr_spec, char **specs)
662{
663 const char **argv = xmalloc((10 + nr_spec) * sizeof(char*));
664 int argc = 0, i;
665
666 argv[argc++] = "http-push";
667 argv[argc++] = "--helper-status";
668 if (options.dry_run)
669 argv[argc++] = "--dry-run";
670 if (options.verbosity > 1)
671 argv[argc++] = "--verbose";
672 argv[argc++] = url;
673 for (i = 0; i < nr_spec; i++)
674 argv[argc++] = specs[i];
675 argv[argc++] = NULL;
676
677 if (run_command_v_opt(argv, RUN_GIT_CMD))
678 die("git-%s failed", argv[0]);
679 free(argv);
680 return 0;
681}
682
de1a2fdd
SP
683static int push_git(struct discovery *heads, int nr_spec, char **specs)
684{
685 struct rpc_state rpc;
686 const char **argv;
687 int argc = 0, i, err;
688
689 argv = xmalloc((10 + nr_spec) * sizeof(char*));
690 argv[argc++] = "send-pack";
691 argv[argc++] = "--stateless-rpc";
692 argv[argc++] = "--helper-status";
693 if (options.thin)
694 argv[argc++] = "--thin";
695 if (options.dry_run)
696 argv[argc++] = "--dry-run";
697 if (options.verbosity > 1)
698 argv[argc++] = "--verbose";
699 argv[argc++] = url;
700 for (i = 0; i < nr_spec; i++)
701 argv[argc++] = specs[i];
702 argv[argc++] = NULL;
703
704 memset(&rpc, 0, sizeof(rpc));
705 rpc.service_name = "git-receive-pack",
706 rpc.argv = argv;
707
708 err = rpc_service(&rpc, heads);
709 if (rpc.result.len)
710 safe_write(1, rpc.result.buf, rpc.result.len);
711 strbuf_release(&rpc.result);
712 free(argv);
713 return err;
714}
715
716static int push(int nr_spec, char **specs)
717{
718 struct discovery *heads = discover_refs("git-receive-pack");
719 int ret;
720
721 if (heads->proto_git)
722 ret = push_git(heads, nr_spec, specs);
723 else
724 ret = push_dav(nr_spec, specs);
725 free_discovery(heads);
726 return ret;
727}
728
ae4efe19
SP
729static void parse_push(struct strbuf *buf)
730{
731 char **specs = NULL;
732 int alloc_spec = 0, nr_spec = 0, i;
733
734 do {
735 if (!prefixcmp(buf->buf, "push ")) {
736 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
737 specs[nr_spec++] = xstrdup(buf->buf + 5);
738 }
739 else
740 die("http transport does not support %s", buf->buf);
741
742 strbuf_reset(buf);
743 if (strbuf_getline(buf, stdin, '\n') == EOF)
744 return;
745 if (!*buf->buf)
746 break;
747 } while (1);
748
de1a2fdd 749 if (push(nr_spec, specs))
ae4efe19
SP
750 exit(128); /* error already reported */
751 for (i = 0; i < nr_spec; i++)
752 free(specs[i]);
753 free(specs);
754
755 printf("\n");
756 fflush(stdout);
757}
758
a2d725b7
DB
759int main(int argc, const char **argv)
760{
a2d725b7 761 struct strbuf buf = STRBUF_INIT;
a45d3d7e 762 int nongit;
a2d725b7 763
c6dfb399 764 git_extract_argv0_path(argv[0]);
a45d3d7e 765 setup_git_directory_gently(&nongit);
a2d725b7
DB
766 if (argc < 2) {
767 fprintf(stderr, "Remote needed\n");
768 return 1;
769 }
770
ef08ef9e
SP
771 options.verbosity = 1;
772 options.progress = !!isatty(2);
de1a2fdd 773 options.thin = 1;
ef08ef9e 774
a2d725b7
DB
775 remote = remote_get(argv[1]);
776
777 if (argc > 2) {
778 url = argv[2];
779 } else {
780 url = remote->url[0];
781 }
782
783 do {
784 if (strbuf_getline(&buf, stdin, '\n') == EOF)
785 break;
786 if (!prefixcmp(buf.buf, "fetch ")) {
a45d3d7e
DB
787 if (nongit)
788 die("Fetch attempted without a local repo");
292ce46b
SP
789 parse_fetch(&buf);
790
ae4efe19 791 } else if (!strcmp(buf.buf, "list") || !prefixcmp(buf.buf, "list ")) {
97cc7bc4
SP
792 int for_push = !!strstr(buf.buf + 4, "for-push");
793 output_refs(get_refs(for_push));
ae4efe19
SP
794
795 } else if (!prefixcmp(buf.buf, "push ")) {
796 parse_push(&buf);
797
ef08ef9e
SP
798 } else if (!prefixcmp(buf.buf, "option ")) {
799 char *name = buf.buf + strlen("option ");
800 char *value = strchr(name, ' ');
801 int result;
802
803 if (value)
804 *value++ = '\0';
805 else
806 value = "true";
807
808 result = set_option(name, value);
809 if (!result)
810 printf("ok\n");
811 else if (result < 0)
812 printf("error invalid value\n");
813 else
814 printf("unsupported\n");
a2d725b7 815 fflush(stdout);
ef08ef9e 816
a2d725b7
DB
817 } else if (!strcmp(buf.buf, "capabilities")) {
818 printf("fetch\n");
ef08ef9e 819 printf("option\n");
ae4efe19 820 printf("push\n");
a2d725b7
DB
821 printf("\n");
822 fflush(stdout);
823 } else {
824 return 1;
825 }
826 strbuf_reset(&buf);
827 } while (1);
828 return 0;
829}