builtin-send-pack.con commit send-pack: avoid deadlock when pack-object dies early (09c9957)
   1#include "cache.h"
   2#include "commit.h"
   3#include "refs.h"
   4#include "pkt-line.h"
   5#include "sideband.h"
   6#include "run-command.h"
   7#include "remote.h"
   8#include "send-pack.h"
   9#include "quote.h"
  10
  11static const char send_pack_usage[] =
  12"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
  13"  --all and explicit <ref> specification are mutually exclusive.";
  14
  15static struct send_pack_args args;
  16
  17static int feed_object(const unsigned char *sha1, int fd, int negative)
  18{
  19        char buf[42];
  20
  21        if (negative && !has_sha1_file(sha1))
  22                return 1;
  23
  24        memcpy(buf + negative, sha1_to_hex(sha1), 40);
  25        if (negative)
  26                buf[0] = '^';
  27        buf[40 + negative] = '\n';
  28        return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
  29}
  30
  31/*
  32 * Make a pack stream and spit it out into file descriptor fd
  33 */
  34static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
  35{
  36        /*
  37         * The child becomes pack-objects --revs; we feed
  38         * the revision parameters to it via its stdin and
  39         * let its stdout go back to the other end.
  40         */
  41        const char *argv[] = {
  42                "pack-objects",
  43                "--all-progress-implied",
  44                "--revs",
  45                "--stdout",
  46                NULL,
  47                NULL,
  48                NULL,
  49                NULL,
  50        };
  51        struct child_process po;
  52        int i;
  53
  54        i = 4;
  55        if (args->use_thin_pack)
  56                argv[i++] = "--thin";
  57        if (args->use_ofs_delta)
  58                argv[i++] = "--delta-base-offset";
  59        if (args->quiet)
  60                argv[i++] = "-q";
  61        memset(&po, 0, sizeof(po));
  62        po.argv = argv;
  63        po.in = -1;
  64        po.out = args->stateless_rpc ? -1 : fd;
  65        po.git_cmd = 1;
  66        if (start_command(&po))
  67                die_errno("git pack-objects failed");
  68
  69        /*
  70         * We feed the pack-objects we just spawned with revision
  71         * parameters by writing to the pipe.
  72         */
  73        for (i = 0; i < extra->nr; i++)
  74                if (!feed_object(extra->array[i], po.in, 1))
  75                        break;
  76
  77        while (refs) {
  78                if (!is_null_sha1(refs->old_sha1) &&
  79                    !feed_object(refs->old_sha1, po.in, 1))
  80                        break;
  81                if (!is_null_sha1(refs->new_sha1) &&
  82                    !feed_object(refs->new_sha1, po.in, 0))
  83                        break;
  84                refs = refs->next;
  85        }
  86
  87        close(po.in);
  88
  89        if (args->stateless_rpc) {
  90                char *buf = xmalloc(LARGE_PACKET_MAX);
  91                while (1) {
  92                        ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
  93                        if (n <= 0)
  94                                break;
  95                        send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
  96                }
  97                free(buf);
  98                close(po.out);
  99                po.out = -1;
 100                close(fd);
 101        }
 102
 103        if (finish_command(&po))
 104                return error("pack-objects died with strange error");
 105        return 0;
 106}
 107
 108static int receive_status(int in, struct ref *refs)
 109{
 110        struct ref *hint;
 111        char line[1000];
 112        int ret = 0;
 113        int len = packet_read_line(in, line, sizeof(line));
 114        if (len < 10 || memcmp(line, "unpack ", 7))
 115                return error("did not receive remote status");
 116        if (memcmp(line, "unpack ok\n", 10)) {
 117                char *p = line + strlen(line) - 1;
 118                if (*p == '\n')
 119                        *p = '\0';
 120                error("unpack failed: %s", line + 7);
 121                ret = -1;
 122        }
 123        hint = NULL;
 124        while (1) {
 125                char *refname;
 126                char *msg;
 127                len = packet_read_line(in, line, sizeof(line));
 128                if (!len)
 129                        break;
 130                if (len < 3 ||
 131                    (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
 132                        fprintf(stderr, "protocol error: %s\n", line);
 133                        ret = -1;
 134                        break;
 135                }
 136
 137                line[strlen(line)-1] = '\0';
 138                refname = line + 3;
 139                msg = strchr(refname, ' ');
 140                if (msg)
 141                        *msg++ = '\0';
 142
 143                /* first try searching at our hint, falling back to all refs */
 144                if (hint)
 145                        hint = find_ref_by_name(hint, refname);
 146                if (!hint)
 147                        hint = find_ref_by_name(refs, refname);
 148                if (!hint) {
 149                        warning("remote reported status on unknown ref: %s",
 150                                        refname);
 151                        continue;
 152                }
 153                if (hint->status != REF_STATUS_EXPECTING_REPORT) {
 154                        warning("remote reported status on unexpected ref: %s",
 155                                        refname);
 156                        continue;
 157                }
 158
 159                if (line[0] == 'o' && line[1] == 'k')
 160                        hint->status = REF_STATUS_OK;
 161                else {
 162                        hint->status = REF_STATUS_REMOTE_REJECT;
 163                        ret = -1;
 164                }
 165                if (msg)
 166                        hint->remote_status = xstrdup(msg);
 167                /* start our next search from the next ref */
 168                hint = hint->next;
 169        }
 170        return ret;
 171}
 172
 173static void update_tracking_ref(struct remote *remote, struct ref *ref)
 174{
 175        struct refspec rs;
 176
 177        if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
 178                return;
 179
 180        rs.src = ref->name;
 181        rs.dst = NULL;
 182
 183        if (!remote_find_tracking(remote, &rs)) {
 184                if (args.verbose)
 185                        fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
 186                if (ref->deletion) {
 187                        delete_ref(rs.dst, NULL, 0);
 188                } else
 189                        update_ref("update by push", rs.dst,
 190                                        ref->new_sha1, NULL, 0, 0);
 191                free(rs.dst);
 192        }
 193}
 194
 195#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
 196
 197static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg)
 198{
 199        fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
 200        if (from)
 201                fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
 202        else
 203                fputs(prettify_refname(to->name), stderr);
 204        if (msg) {
 205                fputs(" (", stderr);
 206                fputs(msg, stderr);
 207                fputc(')', stderr);
 208        }
 209        fputc('\n', stderr);
 210}
 211
 212static const char *status_abbrev(unsigned char sha1[20])
 213{
 214        return find_unique_abbrev(sha1, DEFAULT_ABBREV);
 215}
 216
 217static void print_ok_ref_status(struct ref *ref)
 218{
 219        if (ref->deletion)
 220                print_ref_status('-', "[deleted]", ref, NULL, NULL);
 221        else if (is_null_sha1(ref->old_sha1))
 222                print_ref_status('*',
 223                        (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
 224                          "[new branch]"),
 225                        ref, ref->peer_ref, NULL);
 226        else {
 227                char quickref[84];
 228                char type;
 229                const char *msg;
 230
 231                strcpy(quickref, status_abbrev(ref->old_sha1));
 232                if (ref->nonfastforward) {
 233                        strcat(quickref, "...");
 234                        type = '+';
 235                        msg = "forced update";
 236                } else {
 237                        strcat(quickref, "..");
 238                        type = ' ';
 239                        msg = NULL;
 240                }
 241                strcat(quickref, status_abbrev(ref->new_sha1));
 242
 243                print_ref_status(type, quickref, ref, ref->peer_ref, msg);
 244        }
 245}
 246
 247static int print_one_push_status(struct ref *ref, const char *dest, int count)
 248{
 249        if (!count)
 250                fprintf(stderr, "To %s\n", dest);
 251
 252        switch(ref->status) {
 253        case REF_STATUS_NONE:
 254                print_ref_status('X', "[no match]", ref, NULL, NULL);
 255                break;
 256        case REF_STATUS_REJECT_NODELETE:
 257                print_ref_status('!', "[rejected]", ref, NULL,
 258                                "remote does not support deleting refs");
 259                break;
 260        case REF_STATUS_UPTODATE:
 261                print_ref_status('=', "[up to date]", ref,
 262                                ref->peer_ref, NULL);
 263                break;
 264        case REF_STATUS_REJECT_NONFASTFORWARD:
 265                print_ref_status('!', "[rejected]", ref, ref->peer_ref,
 266                                "non-fast-forward");
 267                break;
 268        case REF_STATUS_REMOTE_REJECT:
 269                print_ref_status('!', "[remote rejected]", ref,
 270                                ref->deletion ? NULL : ref->peer_ref,
 271                                ref->remote_status);
 272                break;
 273        case REF_STATUS_EXPECTING_REPORT:
 274                print_ref_status('!', "[remote failure]", ref,
 275                                ref->deletion ? NULL : ref->peer_ref,
 276                                "remote failed to report status");
 277                break;
 278        case REF_STATUS_OK:
 279                print_ok_ref_status(ref);
 280                break;
 281        }
 282
 283        return 1;
 284}
 285
 286static void print_push_status(const char *dest, struct ref *refs)
 287{
 288        struct ref *ref;
 289        int n = 0;
 290
 291        if (args.verbose) {
 292                for (ref = refs; ref; ref = ref->next)
 293                        if (ref->status == REF_STATUS_UPTODATE)
 294                                n += print_one_push_status(ref, dest, n);
 295        }
 296
 297        for (ref = refs; ref; ref = ref->next)
 298                if (ref->status == REF_STATUS_OK)
 299                        n += print_one_push_status(ref, dest, n);
 300
 301        for (ref = refs; ref; ref = ref->next) {
 302                if (ref->status != REF_STATUS_NONE &&
 303                    ref->status != REF_STATUS_UPTODATE &&
 304                    ref->status != REF_STATUS_OK)
 305                        n += print_one_push_status(ref, dest, n);
 306        }
 307}
 308
 309static int refs_pushed(struct ref *ref)
 310{
 311        for (; ref; ref = ref->next) {
 312                switch(ref->status) {
 313                case REF_STATUS_NONE:
 314                case REF_STATUS_UPTODATE:
 315                        break;
 316                default:
 317                        return 1;
 318                }
 319        }
 320        return 0;
 321}
 322
 323static void print_helper_status(struct ref *ref)
 324{
 325        struct strbuf buf = STRBUF_INIT;
 326
 327        for (; ref; ref = ref->next) {
 328                const char *msg = NULL;
 329                const char *res;
 330
 331                switch(ref->status) {
 332                case REF_STATUS_NONE:
 333                        res = "error";
 334                        msg = "no match";
 335                        break;
 336
 337                case REF_STATUS_OK:
 338                        res = "ok";
 339                        break;
 340
 341                case REF_STATUS_UPTODATE:
 342                        res = "ok";
 343                        msg = "up to date";
 344                        break;
 345
 346                case REF_STATUS_REJECT_NONFASTFORWARD:
 347                        res = "error";
 348                        msg = "non-fast forward";
 349                        break;
 350
 351                case REF_STATUS_REJECT_NODELETE:
 352                case REF_STATUS_REMOTE_REJECT:
 353                        res = "error";
 354                        break;
 355
 356                case REF_STATUS_EXPECTING_REPORT:
 357                default:
 358                        continue;
 359                }
 360
 361                strbuf_reset(&buf);
 362                strbuf_addf(&buf, "%s %s", res, ref->name);
 363                if (ref->remote_status)
 364                        msg = ref->remote_status;
 365                if (msg) {
 366                        strbuf_addch(&buf, ' ');
 367                        quote_two_c_style(&buf, "", msg, 0);
 368                }
 369                strbuf_addch(&buf, '\n');
 370
 371                safe_write(1, buf.buf, buf.len);
 372        }
 373        strbuf_release(&buf);
 374}
 375
 376static int sideband_demux(int in, int out, void *data)
 377{
 378        int *fd = data;
 379#ifndef WIN32
 380        close(fd[1]);
 381#endif
 382        int ret = recv_sideband("send-pack", fd[0], out);
 383        close(out);
 384        return ret;
 385}
 386
 387int send_pack(struct send_pack_args *args,
 388              int fd[], struct child_process *conn,
 389              struct ref *remote_refs,
 390              struct extra_have_objects *extra_have)
 391{
 392        int in = fd[0];
 393        int out = fd[1];
 394        struct strbuf req_buf = STRBUF_INIT;
 395        struct ref *ref;
 396        int new_refs;
 397        int allow_deleting_refs = 0;
 398        int status_report = 0;
 399        int use_sideband = 0;
 400        unsigned cmds_sent = 0;
 401        int ret;
 402        struct async demux;
 403
 404        /* Does the other end support the reporting? */
 405        if (server_supports("report-status"))
 406                status_report = 1;
 407        if (server_supports("delete-refs"))
 408                allow_deleting_refs = 1;
 409        if (server_supports("ofs-delta"))
 410                args->use_ofs_delta = 1;
 411        if (server_supports("side-band-64k"))
 412                use_sideband = 1;
 413
 414        if (!remote_refs) {
 415                fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
 416                        "Perhaps you should specify a branch such as 'master'.\n");
 417                return 0;
 418        }
 419
 420        /*
 421         * Finally, tell the other end!
 422         */
 423        new_refs = 0;
 424        for (ref = remote_refs; ref; ref = ref->next) {
 425
 426                if (ref->peer_ref)
 427                        hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
 428                else if (!args->send_mirror)
 429                        continue;
 430
 431                ref->deletion = is_null_sha1(ref->new_sha1);
 432                if (ref->deletion && !allow_deleting_refs) {
 433                        ref->status = REF_STATUS_REJECT_NODELETE;
 434                        continue;
 435                }
 436                if (!ref->deletion &&
 437                    !hashcmp(ref->old_sha1, ref->new_sha1)) {
 438                        ref->status = REF_STATUS_UPTODATE;
 439                        continue;
 440                }
 441
 442                /* This part determines what can overwrite what.
 443                 * The rules are:
 444                 *
 445                 * (0) you can always use --force or +A:B notation to
 446                 *     selectively force individual ref pairs.
 447                 *
 448                 * (1) if the old thing does not exist, it is OK.
 449                 *
 450                 * (2) if you do not have the old thing, you are not allowed
 451                 *     to overwrite it; you would not know what you are losing
 452                 *     otherwise.
 453                 *
 454                 * (3) if both new and old are commit-ish, and new is a
 455                 *     descendant of old, it is OK.
 456                 *
 457                 * (4) regardless of all of the above, removing :B is
 458                 *     always allowed.
 459                 */
 460
 461                ref->nonfastforward =
 462                    !ref->deletion &&
 463                    !is_null_sha1(ref->old_sha1) &&
 464                    (!has_sha1_file(ref->old_sha1)
 465                      || !ref_newer(ref->new_sha1, ref->old_sha1));
 466
 467                if (ref->nonfastforward && !ref->force && !args->force_update) {
 468                        ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
 469                        continue;
 470                }
 471
 472                if (!ref->deletion)
 473                        new_refs++;
 474
 475                if (args->dry_run) {
 476                        ref->status = REF_STATUS_OK;
 477                } else {
 478                        char *old_hex = sha1_to_hex(ref->old_sha1);
 479                        char *new_hex = sha1_to_hex(ref->new_sha1);
 480
 481                        if (!cmds_sent && (status_report || use_sideband)) {
 482                                packet_buf_write(&req_buf, "%s %s %s%c%s%s",
 483                                        old_hex, new_hex, ref->name, 0,
 484                                        status_report ? " report-status" : "",
 485                                        use_sideband ? " side-band-64k" : "");
 486                        }
 487                        else
 488                                packet_buf_write(&req_buf, "%s %s %s",
 489                                        old_hex, new_hex, ref->name);
 490                        ref->status = status_report ?
 491                                REF_STATUS_EXPECTING_REPORT :
 492                                REF_STATUS_OK;
 493                        cmds_sent++;
 494                }
 495        }
 496
 497        if (args->stateless_rpc) {
 498                if (!args->dry_run && cmds_sent) {
 499                        packet_buf_flush(&req_buf);
 500                        send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
 501                }
 502        } else {
 503                safe_write(out, req_buf.buf, req_buf.len);
 504                packet_flush(out);
 505        }
 506        strbuf_release(&req_buf);
 507
 508        if (use_sideband && cmds_sent) {
 509                memset(&demux, 0, sizeof(demux));
 510                demux.proc = sideband_demux;
 511                demux.data = fd;
 512                demux.out = -1;
 513                if (start_async(&demux))
 514                        die("receive-pack: unable to fork off sideband demultiplexer");
 515                in = demux.out;
 516        }
 517
 518        if (new_refs && cmds_sent) {
 519                if (pack_objects(out, remote_refs, extra_have, args) < 0) {
 520                        for (ref = remote_refs; ref; ref = ref->next)
 521                                ref->status = REF_STATUS_NONE;
 522                        if (use_sideband)
 523                                finish_async(&demux);
 524                        return -1;
 525                }
 526        }
 527        if (args->stateless_rpc && cmds_sent)
 528                packet_flush(out);
 529
 530        if (status_report && cmds_sent)
 531                ret = receive_status(in, remote_refs);
 532        else
 533                ret = 0;
 534        if (args->stateless_rpc)
 535                packet_flush(out);
 536
 537        if (use_sideband && cmds_sent) {
 538                if (finish_async(&demux)) {
 539                        error("error in sideband demultiplexer");
 540                        ret = -1;
 541                }
 542                close(demux.out);
 543        }
 544
 545        if (ret < 0)
 546                return ret;
 547        for (ref = remote_refs; ref; ref = ref->next) {
 548                switch (ref->status) {
 549                case REF_STATUS_NONE:
 550                case REF_STATUS_UPTODATE:
 551                case REF_STATUS_OK:
 552                        break;
 553                default:
 554                        return -1;
 555                }
 556        }
 557        return 0;
 558}
 559
 560static void verify_remote_names(int nr_heads, const char **heads)
 561{
 562        int i;
 563
 564        for (i = 0; i < nr_heads; i++) {
 565                const char *local = heads[i];
 566                const char *remote = strrchr(heads[i], ':');
 567
 568                if (*local == '+')
 569                        local++;
 570
 571                /* A matching refspec is okay.  */
 572                if (remote == local && remote[1] == '\0')
 573                        continue;
 574
 575                remote = remote ? (remote + 1) : local;
 576                switch (check_ref_format(remote)) {
 577                case 0: /* ok */
 578                case CHECK_REF_FORMAT_ONELEVEL:
 579                        /* ok but a single level -- that is fine for
 580                         * a match pattern.
 581                         */
 582                case CHECK_REF_FORMAT_WILDCARD:
 583                        /* ok but ends with a pattern-match character */
 584                        continue;
 585                }
 586                die("remote part of refspec is not a valid name in %s",
 587                    heads[i]);
 588        }
 589}
 590
 591int cmd_send_pack(int argc, const char **argv, const char *prefix)
 592{
 593        int i, nr_refspecs = 0;
 594        const char **refspecs = NULL;
 595        const char *remote_name = NULL;
 596        struct remote *remote = NULL;
 597        const char *dest = NULL;
 598        int fd[2];
 599        struct child_process *conn;
 600        struct extra_have_objects extra_have;
 601        struct ref *remote_refs, *local_refs;
 602        int ret;
 603        int helper_status = 0;
 604        int send_all = 0;
 605        const char *receivepack = "git-receive-pack";
 606        int flags;
 607
 608        argv++;
 609        for (i = 1; i < argc; i++, argv++) {
 610                const char *arg = *argv;
 611
 612                if (*arg == '-') {
 613                        if (!prefixcmp(arg, "--receive-pack=")) {
 614                                receivepack = arg + 15;
 615                                continue;
 616                        }
 617                        if (!prefixcmp(arg, "--exec=")) {
 618                                receivepack = arg + 7;
 619                                continue;
 620                        }
 621                        if (!prefixcmp(arg, "--remote=")) {
 622                                remote_name = arg + 9;
 623                                continue;
 624                        }
 625                        if (!strcmp(arg, "--all")) {
 626                                send_all = 1;
 627                                continue;
 628                        }
 629                        if (!strcmp(arg, "--dry-run")) {
 630                                args.dry_run = 1;
 631                                continue;
 632                        }
 633                        if (!strcmp(arg, "--mirror")) {
 634                                args.send_mirror = 1;
 635                                continue;
 636                        }
 637                        if (!strcmp(arg, "--force")) {
 638                                args.force_update = 1;
 639                                continue;
 640                        }
 641                        if (!strcmp(arg, "--verbose")) {
 642                                args.verbose = 1;
 643                                continue;
 644                        }
 645                        if (!strcmp(arg, "--thin")) {
 646                                args.use_thin_pack = 1;
 647                                continue;
 648                        }
 649                        if (!strcmp(arg, "--stateless-rpc")) {
 650                                args.stateless_rpc = 1;
 651                                continue;
 652                        }
 653                        if (!strcmp(arg, "--helper-status")) {
 654                                helper_status = 1;
 655                                continue;
 656                        }
 657                        usage(send_pack_usage);
 658                }
 659                if (!dest) {
 660                        dest = arg;
 661                        continue;
 662                }
 663                refspecs = (const char **) argv;
 664                nr_refspecs = argc - i;
 665                break;
 666        }
 667        if (!dest)
 668                usage(send_pack_usage);
 669        /*
 670         * --all and --mirror are incompatible; neither makes sense
 671         * with any refspecs.
 672         */
 673        if ((refspecs && (send_all || args.send_mirror)) ||
 674            (send_all && args.send_mirror))
 675                usage(send_pack_usage);
 676
 677        if (remote_name) {
 678                remote = remote_get(remote_name);
 679                if (!remote_has_url(remote, dest)) {
 680                        die("Destination %s is not a uri for %s",
 681                            dest, remote_name);
 682                }
 683        }
 684
 685        if (args.stateless_rpc) {
 686                conn = NULL;
 687                fd[0] = 0;
 688                fd[1] = 1;
 689        } else {
 690                conn = git_connect(fd, dest, receivepack,
 691                        args.verbose ? CONNECT_VERBOSE : 0);
 692        }
 693
 694        memset(&extra_have, 0, sizeof(extra_have));
 695
 696        get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
 697                         &extra_have);
 698
 699        verify_remote_names(nr_refspecs, refspecs);
 700
 701        local_refs = get_local_heads();
 702
 703        flags = MATCH_REFS_NONE;
 704
 705        if (send_all)
 706                flags |= MATCH_REFS_ALL;
 707        if (args.send_mirror)
 708                flags |= MATCH_REFS_MIRROR;
 709
 710        /* match them up */
 711        if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
 712                return -1;
 713
 714        ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
 715
 716        if (helper_status)
 717                print_helper_status(remote_refs);
 718
 719        close(fd[1]);
 720        close(fd[0]);
 721
 722        ret |= finish_connect(conn);
 723
 724        if (!helper_status)
 725                print_push_status(dest, remote_refs);
 726
 727        if (!args.dry_run && remote) {
 728                struct ref *ref;
 729                for (ref = remote_refs; ref; ref = ref->next)
 730                        update_tracking_ref(remote, ref);
 731        }
 732
 733        if (!ret && !refs_pushed(remote_refs))
 734                fprintf(stderr, "Everything up-to-date\n");
 735
 736        return ret;
 737}