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