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