builtin-send-pack.con commit Merge branch 'js/maint-1.6.6-send-pack-stateless-rpc-deadlock-fix' into js/maint-send-pack-stateless-rpc-deadlock-fix (80b5b69)
   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#include "transport.h"
  11
  12static const char send_pack_usage[] =
  13"git send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
  14"  --all and explicit <ref> specification are mutually exclusive.";
  15
  16static struct send_pack_args args;
  17
  18static int feed_object(const unsigned char *sha1, int fd, int negative)
  19{
  20        char buf[42];
  21
  22        if (negative && !has_sha1_file(sha1))
  23                return 1;
  24
  25        memcpy(buf + negative, sha1_to_hex(sha1), 40);
  26        if (negative)
  27                buf[0] = '^';
  28        buf[40 + negative] = '\n';
  29        return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
  30}
  31
  32/*
  33 * Make a pack stream and spit it out into file descriptor fd
  34 */
  35static int pack_objects(int fd, struct ref *refs, struct extra_have_objects *extra, struct send_pack_args *args)
  36{
  37        /*
  38         * The child becomes pack-objects --revs; we feed
  39         * the revision parameters to it via its stdin and
  40         * let its stdout go back to the other end.
  41         */
  42        const char *argv[] = {
  43                "pack-objects",
  44                "--all-progress-implied",
  45                "--revs",
  46                "--stdout",
  47                NULL,
  48                NULL,
  49                NULL,
  50                NULL,
  51        };
  52        struct child_process po;
  53        int i;
  54
  55        i = 4;
  56        if (args->use_thin_pack)
  57                argv[i++] = "--thin";
  58        if (args->use_ofs_delta)
  59                argv[i++] = "--delta-base-offset";
  60        if (args->quiet)
  61                argv[i++] = "-q";
  62        memset(&po, 0, sizeof(po));
  63        po.argv = argv;
  64        po.in = -1;
  65        po.out = args->stateless_rpc ? -1 : fd;
  66        po.git_cmd = 1;
  67        if (start_command(&po))
  68                die_errno("git pack-objects failed");
  69
  70        /*
  71         * We feed the pack-objects we just spawned with revision
  72         * parameters by writing to the pipe.
  73         */
  74        for (i = 0; i < extra->nr; i++)
  75                if (!feed_object(extra->array[i], po.in, 1))
  76                        break;
  77
  78        while (refs) {
  79                if (!is_null_sha1(refs->old_sha1) &&
  80                    !feed_object(refs->old_sha1, po.in, 1))
  81                        break;
  82                if (!is_null_sha1(refs->new_sha1) &&
  83                    !feed_object(refs->new_sha1, po.in, 0))
  84                        break;
  85                refs = refs->next;
  86        }
  87
  88        close(po.in);
  89
  90        if (args->stateless_rpc) {
  91                char *buf = xmalloc(LARGE_PACKET_MAX);
  92                while (1) {
  93                        ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
  94                        if (n <= 0)
  95                                break;
  96                        send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
  97                }
  98                free(buf);
  99                close(po.out);
 100                po.out = -1;
 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 print_helper_status(struct ref *ref)
 174{
 175        struct strbuf buf = STRBUF_INIT;
 176
 177        for (; ref; ref = ref->next) {
 178                const char *msg = NULL;
 179                const char *res;
 180
 181                switch(ref->status) {
 182                case REF_STATUS_NONE:
 183                        res = "error";
 184                        msg = "no match";
 185                        break;
 186
 187                case REF_STATUS_OK:
 188                        res = "ok";
 189                        break;
 190
 191                case REF_STATUS_UPTODATE:
 192                        res = "ok";
 193                        msg = "up to date";
 194                        break;
 195
 196                case REF_STATUS_REJECT_NONFASTFORWARD:
 197                        res = "error";
 198                        msg = "non-fast forward";
 199                        break;
 200
 201                case REF_STATUS_REJECT_NODELETE:
 202                case REF_STATUS_REMOTE_REJECT:
 203                        res = "error";
 204                        break;
 205
 206                case REF_STATUS_EXPECTING_REPORT:
 207                default:
 208                        continue;
 209                }
 210
 211                strbuf_reset(&buf);
 212                strbuf_addf(&buf, "%s %s", res, ref->name);
 213                if (ref->remote_status)
 214                        msg = ref->remote_status;
 215                if (msg) {
 216                        strbuf_addch(&buf, ' ');
 217                        quote_two_c_style(&buf, "", msg, 0);
 218                }
 219                strbuf_addch(&buf, '\n');
 220
 221                safe_write(1, buf.buf, buf.len);
 222        }
 223        strbuf_release(&buf);
 224}
 225
 226static int sideband_demux(int in, int out, void *data)
 227{
 228        int *fd = data;
 229#ifdef NO_PTHREADS
 230        close(fd[1]);
 231#endif
 232        int ret = recv_sideband("send-pack", fd[0], out);
 233        close(out);
 234        return ret;
 235}
 236
 237int send_pack(struct send_pack_args *args,
 238              int fd[], struct child_process *conn,
 239              struct ref *remote_refs,
 240              struct extra_have_objects *extra_have)
 241{
 242        int in = fd[0];
 243        int out = fd[1];
 244        struct strbuf req_buf = STRBUF_INIT;
 245        struct ref *ref;
 246        int new_refs;
 247        int allow_deleting_refs = 0;
 248        int status_report = 0;
 249        int use_sideband = 0;
 250        unsigned cmds_sent = 0;
 251        int ret;
 252        struct async demux;
 253
 254        /* Does the other end support the reporting? */
 255        if (server_supports("report-status"))
 256                status_report = 1;
 257        if (server_supports("delete-refs"))
 258                allow_deleting_refs = 1;
 259        if (server_supports("ofs-delta"))
 260                args->use_ofs_delta = 1;
 261        if (server_supports("side-band-64k"))
 262                use_sideband = 1;
 263
 264        if (!remote_refs) {
 265                fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
 266                        "Perhaps you should specify a branch such as 'master'.\n");
 267                return 0;
 268        }
 269
 270        /*
 271         * Finally, tell the other end!
 272         */
 273        new_refs = 0;
 274        for (ref = remote_refs; ref; ref = ref->next) {
 275                if (!ref->peer_ref && !args->send_mirror)
 276                        continue;
 277
 278                /* Check for statuses set by set_ref_status_for_push() */
 279                switch (ref->status) {
 280                case REF_STATUS_REJECT_NONFASTFORWARD:
 281                case REF_STATUS_UPTODATE:
 282                        continue;
 283                default:
 284                        ; /* do nothing */
 285                }
 286
 287                if (ref->deletion && !allow_deleting_refs) {
 288                        ref->status = REF_STATUS_REJECT_NODELETE;
 289                        continue;
 290                }
 291
 292                if (!ref->deletion)
 293                        new_refs++;
 294
 295                if (args->dry_run) {
 296                        ref->status = REF_STATUS_OK;
 297                } else {
 298                        char *old_hex = sha1_to_hex(ref->old_sha1);
 299                        char *new_hex = sha1_to_hex(ref->new_sha1);
 300
 301                        if (!cmds_sent && (status_report || use_sideband)) {
 302                                packet_buf_write(&req_buf, "%s %s %s%c%s%s",
 303                                        old_hex, new_hex, ref->name, 0,
 304                                        status_report ? " report-status" : "",
 305                                        use_sideband ? " side-band-64k" : "");
 306                        }
 307                        else
 308                                packet_buf_write(&req_buf, "%s %s %s",
 309                                        old_hex, new_hex, ref->name);
 310                        ref->status = status_report ?
 311                                REF_STATUS_EXPECTING_REPORT :
 312                                REF_STATUS_OK;
 313                        cmds_sent++;
 314                }
 315        }
 316
 317        if (args->stateless_rpc) {
 318                if (!args->dry_run && cmds_sent) {
 319                        packet_buf_flush(&req_buf);
 320                        send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
 321                }
 322        } else {
 323                safe_write(out, req_buf.buf, req_buf.len);
 324                packet_flush(out);
 325        }
 326        strbuf_release(&req_buf);
 327
 328        if (use_sideband && cmds_sent) {
 329                memset(&demux, 0, sizeof(demux));
 330                demux.proc = sideband_demux;
 331                demux.data = fd;
 332                demux.out = -1;
 333                if (start_async(&demux))
 334                        die("receive-pack: unable to fork off sideband demultiplexer");
 335                in = demux.out;
 336        }
 337
 338        if (new_refs && cmds_sent) {
 339                if (pack_objects(out, remote_refs, extra_have, args) < 0) {
 340                        for (ref = remote_refs; ref; ref = ref->next)
 341                                ref->status = REF_STATUS_NONE;
 342                        if (args->stateless_rpc)
 343                                close(out);
 344                        if (use_sideband)
 345                                finish_async(&demux);
 346                        return -1;
 347                }
 348        }
 349        if (args->stateless_rpc && cmds_sent)
 350                packet_flush(out);
 351
 352        if (status_report && cmds_sent)
 353                ret = receive_status(in, remote_refs);
 354        else
 355                ret = 0;
 356        if (args->stateless_rpc)
 357                packet_flush(out);
 358
 359        if (use_sideband && cmds_sent) {
 360                if (finish_async(&demux)) {
 361                        error("error in sideband demultiplexer");
 362                        ret = -1;
 363                }
 364                close(demux.out);
 365        }
 366
 367        if (ret < 0)
 368                return ret;
 369        for (ref = remote_refs; ref; ref = ref->next) {
 370                switch (ref->status) {
 371                case REF_STATUS_NONE:
 372                case REF_STATUS_UPTODATE:
 373                case REF_STATUS_OK:
 374                        break;
 375                default:
 376                        return -1;
 377                }
 378        }
 379        return 0;
 380}
 381
 382int cmd_send_pack(int argc, const char **argv, const char *prefix)
 383{
 384        int i, nr_refspecs = 0;
 385        const char **refspecs = NULL;
 386        const char *remote_name = NULL;
 387        struct remote *remote = NULL;
 388        const char *dest = NULL;
 389        int fd[2];
 390        struct child_process *conn;
 391        struct extra_have_objects extra_have;
 392        struct ref *remote_refs, *local_refs;
 393        int ret;
 394        int helper_status = 0;
 395        int send_all = 0;
 396        const char *receivepack = "git-receive-pack";
 397        int flags;
 398        int nonfastforward = 0;
 399
 400        argv++;
 401        for (i = 1; i < argc; i++, argv++) {
 402                const char *arg = *argv;
 403
 404                if (*arg == '-') {
 405                        if (!prefixcmp(arg, "--receive-pack=")) {
 406                                receivepack = arg + 15;
 407                                continue;
 408                        }
 409                        if (!prefixcmp(arg, "--exec=")) {
 410                                receivepack = arg + 7;
 411                                continue;
 412                        }
 413                        if (!prefixcmp(arg, "--remote=")) {
 414                                remote_name = arg + 9;
 415                                continue;
 416                        }
 417                        if (!strcmp(arg, "--all")) {
 418                                send_all = 1;
 419                                continue;
 420                        }
 421                        if (!strcmp(arg, "--dry-run")) {
 422                                args.dry_run = 1;
 423                                continue;
 424                        }
 425                        if (!strcmp(arg, "--mirror")) {
 426                                args.send_mirror = 1;
 427                                continue;
 428                        }
 429                        if (!strcmp(arg, "--force")) {
 430                                args.force_update = 1;
 431                                continue;
 432                        }
 433                        if (!strcmp(arg, "--verbose")) {
 434                                args.verbose = 1;
 435                                continue;
 436                        }
 437                        if (!strcmp(arg, "--thin")) {
 438                                args.use_thin_pack = 1;
 439                                continue;
 440                        }
 441                        if (!strcmp(arg, "--stateless-rpc")) {
 442                                args.stateless_rpc = 1;
 443                                continue;
 444                        }
 445                        if (!strcmp(arg, "--helper-status")) {
 446                                helper_status = 1;
 447                                continue;
 448                        }
 449                        usage(send_pack_usage);
 450                }
 451                if (!dest) {
 452                        dest = arg;
 453                        continue;
 454                }
 455                refspecs = (const char **) argv;
 456                nr_refspecs = argc - i;
 457                break;
 458        }
 459        if (!dest)
 460                usage(send_pack_usage);
 461        /*
 462         * --all and --mirror are incompatible; neither makes sense
 463         * with any refspecs.
 464         */
 465        if ((refspecs && (send_all || args.send_mirror)) ||
 466            (send_all && args.send_mirror))
 467                usage(send_pack_usage);
 468
 469        if (remote_name) {
 470                remote = remote_get(remote_name);
 471                if (!remote_has_url(remote, dest)) {
 472                        die("Destination %s is not a uri for %s",
 473                            dest, remote_name);
 474                }
 475        }
 476
 477        if (args.stateless_rpc) {
 478                conn = NULL;
 479                fd[0] = 0;
 480                fd[1] = 1;
 481        } else {
 482                conn = git_connect(fd, dest, receivepack,
 483                        args.verbose ? CONNECT_VERBOSE : 0);
 484        }
 485
 486        memset(&extra_have, 0, sizeof(extra_have));
 487
 488        get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
 489                         &extra_have);
 490
 491        transport_verify_remote_names(nr_refspecs, refspecs);
 492
 493        local_refs = get_local_heads();
 494
 495        flags = MATCH_REFS_NONE;
 496
 497        if (send_all)
 498                flags |= MATCH_REFS_ALL;
 499        if (args.send_mirror)
 500                flags |= MATCH_REFS_MIRROR;
 501
 502        /* match them up */
 503        if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
 504                return -1;
 505
 506        set_ref_status_for_push(remote_refs, args.send_mirror,
 507                args.force_update);
 508
 509        ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
 510
 511        if (helper_status)
 512                print_helper_status(remote_refs);
 513
 514        close(fd[1]);
 515        close(fd[0]);
 516
 517        ret |= finish_connect(conn);
 518
 519        if (!helper_status)
 520                transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
 521
 522        if (!args.dry_run && remote) {
 523                struct ref *ref;
 524                for (ref = remote_refs; ref; ref = ref->next)
 525                        transport_update_tracking_ref(remote, ref, args.verbose);
 526        }
 527
 528        if (!ret && !transport_refs_pushed(remote_refs))
 529                fprintf(stderr, "Everything up-to-date\n");
 530
 531        return ret;
 532}