builtin / send-pack.con commit Merge branch 'nd/cache-tree-api-refactor' (a784482)
   1#include "builtin.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                NULL,
  52        };
  53        struct child_process po;
  54        int i;
  55
  56        i = 4;
  57        if (args->use_thin_pack)
  58                argv[i++] = "--thin";
  59        if (args->use_ofs_delta)
  60                argv[i++] = "--delta-base-offset";
  61        if (args->quiet)
  62                argv[i++] = "-q";
  63        if (args->progress)
  64                argv[i++] = "--progress";
  65        memset(&po, 0, sizeof(po));
  66        po.argv = argv;
  67        po.in = -1;
  68        po.out = args->stateless_rpc ? -1 : fd;
  69        po.git_cmd = 1;
  70        if (start_command(&po))
  71                die_errno("git pack-objects failed");
  72
  73        /*
  74         * We feed the pack-objects we just spawned with revision
  75         * parameters by writing to the pipe.
  76         */
  77        for (i = 0; i < extra->nr; i++)
  78                if (!feed_object(extra->array[i], po.in, 1))
  79                        break;
  80
  81        while (refs) {
  82                if (!is_null_sha1(refs->old_sha1) &&
  83                    !feed_object(refs->old_sha1, po.in, 1))
  84                        break;
  85                if (!is_null_sha1(refs->new_sha1) &&
  86                    !feed_object(refs->new_sha1, po.in, 0))
  87                        break;
  88                refs = refs->next;
  89        }
  90
  91        close(po.in);
  92
  93        if (args->stateless_rpc) {
  94                char *buf = xmalloc(LARGE_PACKET_MAX);
  95                while (1) {
  96                        ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
  97                        if (n <= 0)
  98                                break;
  99                        send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
 100                }
 101                free(buf);
 102                close(po.out);
 103                po.out = -1;
 104        }
 105
 106        if (finish_command(&po))
 107                return -1;
 108        return 0;
 109}
 110
 111static int receive_status(int in, struct ref *refs)
 112{
 113        struct ref *hint;
 114        char line[1000];
 115        int ret = 0;
 116        int len = packet_read_line(in, line, sizeof(line));
 117        if (len < 10 || memcmp(line, "unpack ", 7))
 118                return error("did not receive remote status");
 119        if (memcmp(line, "unpack ok\n", 10)) {
 120                char *p = line + strlen(line) - 1;
 121                if (*p == '\n')
 122                        *p = '\0';
 123                error("unpack failed: %s", line + 7);
 124                ret = -1;
 125        }
 126        hint = NULL;
 127        while (1) {
 128                char *refname;
 129                char *msg;
 130                len = packet_read_line(in, line, sizeof(line));
 131                if (!len)
 132                        break;
 133                if (len < 3 ||
 134                    (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) {
 135                        fprintf(stderr, "protocol error: %s\n", line);
 136                        ret = -1;
 137                        break;
 138                }
 139
 140                line[strlen(line)-1] = '\0';
 141                refname = line + 3;
 142                msg = strchr(refname, ' ');
 143                if (msg)
 144                        *msg++ = '\0';
 145
 146                /* first try searching at our hint, falling back to all refs */
 147                if (hint)
 148                        hint = find_ref_by_name(hint, refname);
 149                if (!hint)
 150                        hint = find_ref_by_name(refs, refname);
 151                if (!hint) {
 152                        warning("remote reported status on unknown ref: %s",
 153                                        refname);
 154                        continue;
 155                }
 156                if (hint->status != REF_STATUS_EXPECTING_REPORT) {
 157                        warning("remote reported status on unexpected ref: %s",
 158                                        refname);
 159                        continue;
 160                }
 161
 162                if (line[0] == 'o' && line[1] == 'k')
 163                        hint->status = REF_STATUS_OK;
 164                else {
 165                        hint->status = REF_STATUS_REMOTE_REJECT;
 166                        ret = -1;
 167                }
 168                if (msg)
 169                        hint->remote_status = xstrdup(msg);
 170                /* start our next search from the next ref */
 171                hint = hint->next;
 172        }
 173        return ret;
 174}
 175
 176static void print_helper_status(struct ref *ref)
 177{
 178        struct strbuf buf = STRBUF_INIT;
 179
 180        for (; ref; ref = ref->next) {
 181                const char *msg = NULL;
 182                const char *res;
 183
 184                switch(ref->status) {
 185                case REF_STATUS_NONE:
 186                        res = "error";
 187                        msg = "no match";
 188                        break;
 189
 190                case REF_STATUS_OK:
 191                        res = "ok";
 192                        break;
 193
 194                case REF_STATUS_UPTODATE:
 195                        res = "ok";
 196                        msg = "up to date";
 197                        break;
 198
 199                case REF_STATUS_REJECT_NONFASTFORWARD:
 200                        res = "error";
 201                        msg = "non-fast forward";
 202                        break;
 203
 204                case REF_STATUS_REJECT_NODELETE:
 205                case REF_STATUS_REMOTE_REJECT:
 206                        res = "error";
 207                        break;
 208
 209                case REF_STATUS_EXPECTING_REPORT:
 210                default:
 211                        continue;
 212                }
 213
 214                strbuf_reset(&buf);
 215                strbuf_addf(&buf, "%s %s", res, ref->name);
 216                if (ref->remote_status)
 217                        msg = ref->remote_status;
 218                if (msg) {
 219                        strbuf_addch(&buf, ' ');
 220                        quote_two_c_style(&buf, "", msg, 0);
 221                }
 222                strbuf_addch(&buf, '\n');
 223
 224                safe_write(1, buf.buf, buf.len);
 225        }
 226        strbuf_release(&buf);
 227}
 228
 229static int sideband_demux(int in, int out, void *data)
 230{
 231        int *fd = data, ret;
 232#ifdef NO_PTHREADS
 233        close(fd[1]);
 234#endif
 235        ret = recv_sideband("send-pack", fd[0], out);
 236        close(out);
 237        return ret;
 238}
 239
 240int send_pack(struct send_pack_args *args,
 241              int fd[], struct child_process *conn,
 242              struct ref *remote_refs,
 243              struct extra_have_objects *extra_have)
 244{
 245        int in = fd[0];
 246        int out = fd[1];
 247        struct strbuf req_buf = STRBUF_INIT;
 248        struct ref *ref;
 249        int new_refs;
 250        int allow_deleting_refs = 0;
 251        int status_report = 0;
 252        int use_sideband = 0;
 253        unsigned cmds_sent = 0;
 254        int ret;
 255        struct async demux;
 256
 257        /* Does the other end support the reporting? */
 258        if (server_supports("report-status"))
 259                status_report = 1;
 260        if (server_supports("delete-refs"))
 261                allow_deleting_refs = 1;
 262        if (server_supports("ofs-delta"))
 263                args->use_ofs_delta = 1;
 264        if (server_supports("side-band-64k"))
 265                use_sideband = 1;
 266        if (!server_supports("quiet"))
 267                args->quiet = 0;
 268
 269        if (!remote_refs) {
 270                fprintf(stderr, "No refs in common and none specified; doing nothing.\n"
 271                        "Perhaps you should specify a branch such as 'master'.\n");
 272                return 0;
 273        }
 274
 275        /*
 276         * Finally, tell the other end!
 277         */
 278        new_refs = 0;
 279        for (ref = remote_refs; ref; ref = ref->next) {
 280                if (!ref->peer_ref && !args->send_mirror)
 281                        continue;
 282
 283                /* Check for statuses set by set_ref_status_for_push() */
 284                switch (ref->status) {
 285                case REF_STATUS_REJECT_NONFASTFORWARD:
 286                case REF_STATUS_UPTODATE:
 287                        continue;
 288                default:
 289                        ; /* do nothing */
 290                }
 291
 292                if (ref->deletion && !allow_deleting_refs) {
 293                        ref->status = REF_STATUS_REJECT_NODELETE;
 294                        continue;
 295                }
 296
 297                if (!ref->deletion)
 298                        new_refs++;
 299
 300                if (args->dry_run) {
 301                        ref->status = REF_STATUS_OK;
 302                } else {
 303                        char *old_hex = sha1_to_hex(ref->old_sha1);
 304                        char *new_hex = sha1_to_hex(ref->new_sha1);
 305
 306                        if (!cmds_sent && (status_report || use_sideband || args->quiet)) {
 307                                packet_buf_write(&req_buf, "%s %s %s%c%s%s%s",
 308                                        old_hex, new_hex, ref->name, 0,
 309                                        status_report ? " report-status" : "",
 310                                        use_sideband ? " side-band-64k" : "",
 311                                        args->quiet ? " quiet" : "");
 312                        }
 313                        else
 314                                packet_buf_write(&req_buf, "%s %s %s",
 315                                        old_hex, new_hex, ref->name);
 316                        ref->status = status_report ?
 317                                REF_STATUS_EXPECTING_REPORT :
 318                                REF_STATUS_OK;
 319                        cmds_sent++;
 320                }
 321        }
 322
 323        if (args->stateless_rpc) {
 324                if (!args->dry_run && cmds_sent) {
 325                        packet_buf_flush(&req_buf);
 326                        send_sideband(out, -1, req_buf.buf, req_buf.len, LARGE_PACKET_MAX);
 327                }
 328        } else {
 329                safe_write(out, req_buf.buf, req_buf.len);
 330                packet_flush(out);
 331        }
 332        strbuf_release(&req_buf);
 333
 334        if (use_sideband && cmds_sent) {
 335                memset(&demux, 0, sizeof(demux));
 336                demux.proc = sideband_demux;
 337                demux.data = fd;
 338                demux.out = -1;
 339                if (start_async(&demux))
 340                        die("send-pack: unable to fork off sideband demultiplexer");
 341                in = demux.out;
 342        }
 343
 344        if (new_refs && cmds_sent) {
 345                if (pack_objects(out, remote_refs, extra_have, args) < 0) {
 346                        for (ref = remote_refs; ref; ref = ref->next)
 347                                ref->status = REF_STATUS_NONE;
 348                        if (args->stateless_rpc)
 349                                close(out);
 350                        if (git_connection_is_socket(conn))
 351                                shutdown(fd[0], SHUT_WR);
 352                        if (use_sideband)
 353                                finish_async(&demux);
 354                        return -1;
 355                }
 356        }
 357        if (args->stateless_rpc && cmds_sent)
 358                packet_flush(out);
 359
 360        if (status_report && cmds_sent)
 361                ret = receive_status(in, remote_refs);
 362        else
 363                ret = 0;
 364        if (args->stateless_rpc)
 365                packet_flush(out);
 366
 367        if (use_sideband && cmds_sent) {
 368                if (finish_async(&demux)) {
 369                        error("error in sideband demultiplexer");
 370                        ret = -1;
 371                }
 372                close(demux.out);
 373        }
 374
 375        if (ret < 0)
 376                return ret;
 377
 378        if (args->porcelain)
 379                return 0;
 380
 381        for (ref = remote_refs; ref; ref = ref->next) {
 382                switch (ref->status) {
 383                case REF_STATUS_NONE:
 384                case REF_STATUS_UPTODATE:
 385                case REF_STATUS_OK:
 386                        break;
 387                default:
 388                        return -1;
 389                }
 390        }
 391        return 0;
 392}
 393
 394int cmd_send_pack(int argc, const char **argv, const char *prefix)
 395{
 396        int i, nr_refspecs = 0;
 397        const char **refspecs = NULL;
 398        const char *remote_name = NULL;
 399        struct remote *remote = NULL;
 400        const char *dest = NULL;
 401        int fd[2];
 402        struct child_process *conn;
 403        struct extra_have_objects extra_have;
 404        struct ref *remote_refs, *local_refs;
 405        int ret;
 406        int helper_status = 0;
 407        int send_all = 0;
 408        const char *receivepack = "git-receive-pack";
 409        int flags;
 410        int nonfastforward = 0;
 411
 412        argv++;
 413        for (i = 1; i < argc; i++, argv++) {
 414                const char *arg = *argv;
 415
 416                if (*arg == '-') {
 417                        if (!prefixcmp(arg, "--receive-pack=")) {
 418                                receivepack = arg + 15;
 419                                continue;
 420                        }
 421                        if (!prefixcmp(arg, "--exec=")) {
 422                                receivepack = arg + 7;
 423                                continue;
 424                        }
 425                        if (!prefixcmp(arg, "--remote=")) {
 426                                remote_name = arg + 9;
 427                                continue;
 428                        }
 429                        if (!strcmp(arg, "--all")) {
 430                                send_all = 1;
 431                                continue;
 432                        }
 433                        if (!strcmp(arg, "--dry-run")) {
 434                                args.dry_run = 1;
 435                                continue;
 436                        }
 437                        if (!strcmp(arg, "--mirror")) {
 438                                args.send_mirror = 1;
 439                                continue;
 440                        }
 441                        if (!strcmp(arg, "--force")) {
 442                                args.force_update = 1;
 443                                continue;
 444                        }
 445                        if (!strcmp(arg, "--quiet")) {
 446                                args.quiet = 1;
 447                                continue;
 448                        }
 449                        if (!strcmp(arg, "--verbose")) {
 450                                args.verbose = 1;
 451                                continue;
 452                        }
 453                        if (!strcmp(arg, "--thin")) {
 454                                args.use_thin_pack = 1;
 455                                continue;
 456                        }
 457                        if (!strcmp(arg, "--stateless-rpc")) {
 458                                args.stateless_rpc = 1;
 459                                continue;
 460                        }
 461                        if (!strcmp(arg, "--helper-status")) {
 462                                helper_status = 1;
 463                                continue;
 464                        }
 465                        usage(send_pack_usage);
 466                }
 467                if (!dest) {
 468                        dest = arg;
 469                        continue;
 470                }
 471                refspecs = (const char **) argv;
 472                nr_refspecs = argc - i;
 473                break;
 474        }
 475        if (!dest)
 476                usage(send_pack_usage);
 477        /*
 478         * --all and --mirror are incompatible; neither makes sense
 479         * with any refspecs.
 480         */
 481        if ((refspecs && (send_all || args.send_mirror)) ||
 482            (send_all && args.send_mirror))
 483                usage(send_pack_usage);
 484
 485        if (remote_name) {
 486                remote = remote_get(remote_name);
 487                if (!remote_has_url(remote, dest)) {
 488                        die("Destination %s is not a uri for %s",
 489                            dest, remote_name);
 490                }
 491        }
 492
 493        if (args.stateless_rpc) {
 494                conn = NULL;
 495                fd[0] = 0;
 496                fd[1] = 1;
 497        } else {
 498                conn = git_connect(fd, dest, receivepack,
 499                        args.verbose ? CONNECT_VERBOSE : 0);
 500        }
 501
 502        memset(&extra_have, 0, sizeof(extra_have));
 503
 504        get_remote_heads(fd[0], &remote_refs, REF_NORMAL, &extra_have);
 505
 506        transport_verify_remote_names(nr_refspecs, refspecs);
 507
 508        local_refs = get_local_heads();
 509
 510        flags = MATCH_REFS_NONE;
 511
 512        if (send_all)
 513                flags |= MATCH_REFS_ALL;
 514        if (args.send_mirror)
 515                flags |= MATCH_REFS_MIRROR;
 516
 517        /* match them up */
 518        if (match_push_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
 519                return -1;
 520
 521        set_ref_status_for_push(remote_refs, args.send_mirror,
 522                args.force_update);
 523
 524        ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
 525
 526        if (helper_status)
 527                print_helper_status(remote_refs);
 528
 529        close(fd[1]);
 530        close(fd[0]);
 531
 532        ret |= finish_connect(conn);
 533
 534        if (!helper_status)
 535                transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
 536
 537        if (!args.dry_run && remote) {
 538                struct ref *ref;
 539                for (ref = remote_refs; ref; ref = ref->next)
 540                        transport_update_tracking_ref(remote, ref, args.verbose);
 541        }
 542
 543        if (!ret && !transport_refs_pushed(remote_refs))
 544                fprintf(stderr, "Everything up-to-date\n");
 545
 546        return ret;
 547}