builtin / send-pack.con commit t3507: introduce pristine-detach helper (2161da1)
   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                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;
 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 (use_sideband)
 343                                finish_async(&demux);
 344                        return -1;
 345                }
 346        }
 347        if (args->stateless_rpc && cmds_sent)
 348                packet_flush(out);
 349
 350        if (status_report && cmds_sent)
 351                ret = receive_status(in, remote_refs);
 352        else
 353                ret = 0;
 354        if (args->stateless_rpc)
 355                packet_flush(out);
 356
 357        if (use_sideband && cmds_sent) {
 358                if (finish_async(&demux)) {
 359                        error("error in sideband demultiplexer");
 360                        ret = -1;
 361                }
 362                close(demux.out);
 363        }
 364
 365        if (ret < 0)
 366                return ret;
 367
 368        if (args->porcelain)
 369                return 0;
 370
 371        for (ref = remote_refs; ref; ref = ref->next) {
 372                switch (ref->status) {
 373                case REF_STATUS_NONE:
 374                case REF_STATUS_UPTODATE:
 375                case REF_STATUS_OK:
 376                        break;
 377                default:
 378                        return -1;
 379                }
 380        }
 381        return 0;
 382}
 383
 384int cmd_send_pack(int argc, const char **argv, const char *prefix)
 385{
 386        int i, nr_refspecs = 0;
 387        const char **refspecs = NULL;
 388        const char *remote_name = NULL;
 389        struct remote *remote = NULL;
 390        const char *dest = NULL;
 391        int fd[2];
 392        struct child_process *conn;
 393        struct extra_have_objects extra_have;
 394        struct ref *remote_refs, *local_refs;
 395        int ret;
 396        int helper_status = 0;
 397        int send_all = 0;
 398        const char *receivepack = "git-receive-pack";
 399        int flags;
 400        int nonfastforward = 0;
 401
 402        argv++;
 403        for (i = 1; i < argc; i++, argv++) {
 404                const char *arg = *argv;
 405
 406                if (*arg == '-') {
 407                        if (!prefixcmp(arg, "--receive-pack=")) {
 408                                receivepack = arg + 15;
 409                                continue;
 410                        }
 411                        if (!prefixcmp(arg, "--exec=")) {
 412                                receivepack = arg + 7;
 413                                continue;
 414                        }
 415                        if (!prefixcmp(arg, "--remote=")) {
 416                                remote_name = arg + 9;
 417                                continue;
 418                        }
 419                        if (!strcmp(arg, "--all")) {
 420                                send_all = 1;
 421                                continue;
 422                        }
 423                        if (!strcmp(arg, "--dry-run")) {
 424                                args.dry_run = 1;
 425                                continue;
 426                        }
 427                        if (!strcmp(arg, "--mirror")) {
 428                                args.send_mirror = 1;
 429                                continue;
 430                        }
 431                        if (!strcmp(arg, "--force")) {
 432                                args.force_update = 1;
 433                                continue;
 434                        }
 435                        if (!strcmp(arg, "--verbose")) {
 436                                args.verbose = 1;
 437                                continue;
 438                        }
 439                        if (!strcmp(arg, "--thin")) {
 440                                args.use_thin_pack = 1;
 441                                continue;
 442                        }
 443                        if (!strcmp(arg, "--stateless-rpc")) {
 444                                args.stateless_rpc = 1;
 445                                continue;
 446                        }
 447                        if (!strcmp(arg, "--helper-status")) {
 448                                helper_status = 1;
 449                                continue;
 450                        }
 451                        usage(send_pack_usage);
 452                }
 453                if (!dest) {
 454                        dest = arg;
 455                        continue;
 456                }
 457                refspecs = (const char **) argv;
 458                nr_refspecs = argc - i;
 459                break;
 460        }
 461        if (!dest)
 462                usage(send_pack_usage);
 463        /*
 464         * --all and --mirror are incompatible; neither makes sense
 465         * with any refspecs.
 466         */
 467        if ((refspecs && (send_all || args.send_mirror)) ||
 468            (send_all && args.send_mirror))
 469                usage(send_pack_usage);
 470
 471        if (remote_name) {
 472                remote = remote_get(remote_name);
 473                if (!remote_has_url(remote, dest)) {
 474                        die("Destination %s is not a uri for %s",
 475                            dest, remote_name);
 476                }
 477        }
 478
 479        if (args.stateless_rpc) {
 480                conn = NULL;
 481                fd[0] = 0;
 482                fd[1] = 1;
 483        } else {
 484                conn = git_connect(fd, dest, receivepack,
 485                        args.verbose ? CONNECT_VERBOSE : 0);
 486        }
 487
 488        memset(&extra_have, 0, sizeof(extra_have));
 489
 490        get_remote_heads(fd[0], &remote_refs, 0, NULL, REF_NORMAL,
 491                         &extra_have);
 492
 493        transport_verify_remote_names(nr_refspecs, refspecs);
 494
 495        local_refs = get_local_heads();
 496
 497        flags = MATCH_REFS_NONE;
 498
 499        if (send_all)
 500                flags |= MATCH_REFS_ALL;
 501        if (args.send_mirror)
 502                flags |= MATCH_REFS_MIRROR;
 503
 504        /* match them up */
 505        if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
 506                return -1;
 507
 508        set_ref_status_for_push(remote_refs, args.send_mirror,
 509                args.force_update);
 510
 511        ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
 512
 513        if (helper_status)
 514                print_helper_status(remote_refs);
 515
 516        close(fd[1]);
 517        close(fd[0]);
 518
 519        ret |= finish_connect(conn);
 520
 521        if (!helper_status)
 522                transport_print_push_status(dest, remote_refs, args.verbose, 0, &nonfastforward);
 523
 524        if (!args.dry_run && remote) {
 525                struct ref *ref;
 526                for (ref = remote_refs; ref; ref = ref->next)
 527                        transport_update_tracking_ref(remote, ref, args.verbose);
 528        }
 529
 530        if (!ret && !transport_refs_pushed(remote_refs))
 531                fprintf(stderr, "Everything up-to-date\n");
 532
 533        return ret;
 534}