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