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