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