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