read-tree.con commit Merge branch 'lt/fix-sol-pack' into next (7941602)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#define DBRT_DEBUG 1
   7
   8#include "cache.h"
   9
  10#include "object.h"
  11#include "tree.h"
  12#include <sys/time.h>
  13#include <signal.h>
  14
  15static int merge = 0;
  16static int update = 0;
  17static int index_only = 0;
  18static int nontrivial_merge = 0;
  19static int trivial_merges_only = 0;
  20static int aggressive = 0;
  21static int verbose_update = 0;
  22static volatile int progress_update = 0;
  23
  24static int head_idx = -1;
  25static int merge_size = 0;
  26
  27static struct object_list *trees = NULL;
  28
  29static struct cache_entry df_conflict_entry = { 
  30};
  31
  32static struct tree_entry_list df_conflict_list = {
  33        .name = NULL,
  34        .next = &df_conflict_list
  35};
  36
  37typedef int (*merge_fn_t)(struct cache_entry **src);
  38
  39static int entcmp(char *name1, int dir1, char *name2, int dir2)
  40{
  41        int len1 = strlen(name1);
  42        int len2 = strlen(name2);
  43        int len = len1 < len2 ? len1 : len2;
  44        int ret = memcmp(name1, name2, len);
  45        unsigned char c1, c2;
  46        if (ret)
  47                return ret;
  48        c1 = name1[len];
  49        c2 = name2[len];
  50        if (!c1 && dir1)
  51                c1 = '/';
  52        if (!c2 && dir2)
  53                c2 = '/';
  54        ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
  55        if (c1 && c2 && !ret)
  56                ret = len1 - len2;
  57        return ret;
  58}
  59
  60static int unpack_trees_rec(struct tree_entry_list **posns, int len,
  61                            const char *base, merge_fn_t fn, int *indpos)
  62{
  63        int baselen = strlen(base);
  64        int src_size = len + 1;
  65        do {
  66                int i;
  67                char *first;
  68                int firstdir = 0;
  69                int pathlen;
  70                unsigned ce_size;
  71                struct tree_entry_list **subposns;
  72                struct cache_entry **src;
  73                int any_files = 0;
  74                int any_dirs = 0;
  75                char *cache_name;
  76                int ce_stage;
  77
  78                /* Find the first name in the input. */
  79
  80                first = NULL;
  81                cache_name = NULL;
  82
  83                /* Check the cache */
  84                if (merge && *indpos < active_nr) {
  85                        /* This is a bit tricky: */
  86                        /* If the index has a subdirectory (with
  87                         * contents) as the first name, it'll get a
  88                         * filename like "foo/bar". But that's after
  89                         * "foo", so the entry in trees will get
  90                         * handled first, at which point we'll go into
  91                         * "foo", and deal with "bar" from the index,
  92                         * because the base will be "foo/". The only
  93                         * way we can actually have "foo/bar" first of
  94                         * all the things is if the trees don't
  95                         * contain "foo" at all, in which case we'll
  96                         * handle "foo/bar" without going into the
  97                         * directory, but that's fine (and will return
  98                         * an error anyway, with the added unknown
  99                         * file case.
 100                         */
 101
 102                        cache_name = active_cache[*indpos]->name;
 103                        if (strlen(cache_name) > baselen &&
 104                            !memcmp(cache_name, base, baselen)) {
 105                                cache_name += baselen;
 106                                first = cache_name;
 107                        } else {
 108                                cache_name = NULL;
 109                        }
 110                }
 111
 112#if DBRT_DEBUG > 1
 113                if (first)
 114                        printf("index %s\n", first);
 115#endif
 116                for (i = 0; i < len; i++) {
 117                        if (!posns[i] || posns[i] == &df_conflict_list)
 118                                continue;
 119#if DBRT_DEBUG > 1
 120                        printf("%d %s\n", i + 1, posns[i]->name);
 121#endif
 122                        if (!first || entcmp(first, firstdir,
 123                                             posns[i]->name, 
 124                                             posns[i]->directory) > 0) {
 125                                first = posns[i]->name;
 126                                firstdir = posns[i]->directory;
 127                        }
 128                }
 129                /* No name means we're done */
 130                if (!first)
 131                        return 0;
 132
 133                pathlen = strlen(first);
 134                ce_size = cache_entry_size(baselen + pathlen);
 135
 136                src = xmalloc(sizeof(struct cache_entry *) * src_size);
 137                memset(src, 0, sizeof(struct cache_entry *) * src_size);
 138
 139                subposns = xmalloc(sizeof(struct tree_list_entry *) * len);
 140                memset(subposns, 0, sizeof(struct tree_list_entry *) * len);
 141
 142                if (cache_name && !strcmp(cache_name, first)) {
 143                        any_files = 1;
 144                        src[0] = active_cache[*indpos];
 145                        remove_cache_entry_at(*indpos);
 146                }
 147
 148                for (i = 0; i < len; i++) {
 149                        struct cache_entry *ce;
 150
 151                        if (!posns[i] ||
 152                            (posns[i] != &df_conflict_list &&
 153                             strcmp(first, posns[i]->name))) {
 154                                continue;
 155                        }
 156
 157                        if (posns[i] == &df_conflict_list) {
 158                                src[i + merge] = &df_conflict_entry;
 159                                continue;
 160                        }
 161
 162                        if (posns[i]->directory) {
 163                                any_dirs = 1;
 164                                parse_tree(posns[i]->item.tree);
 165                                subposns[i] = posns[i]->item.tree->entries;
 166                                posns[i] = posns[i]->next;
 167                                src[i + merge] = &df_conflict_entry;
 168                                continue;
 169                        }
 170
 171                        if (!merge)
 172                                ce_stage = 0;
 173                        else if (i + 1 < head_idx)
 174                                ce_stage = 1;
 175                        else if (i + 1 > head_idx)
 176                                ce_stage = 3;
 177                        else
 178                                ce_stage = 2;
 179
 180                        ce = xmalloc(ce_size);
 181                        memset(ce, 0, ce_size);
 182                        ce->ce_mode = create_ce_mode(posns[i]->mode);
 183                        ce->ce_flags = create_ce_flags(baselen + pathlen,
 184                                                       ce_stage);
 185                        memcpy(ce->name, base, baselen);
 186                        memcpy(ce->name + baselen, first, pathlen + 1);
 187
 188                        any_files = 1;
 189
 190                        memcpy(ce->sha1, posns[i]->item.any->sha1, 20);
 191                        src[i + merge] = ce;
 192                        subposns[i] = &df_conflict_list;
 193                        posns[i] = posns[i]->next;
 194                }
 195                if (any_files) {
 196                        if (merge) {
 197                                int ret;
 198
 199#if DBRT_DEBUG > 1
 200                                printf("%s:\n", first);
 201                                for (i = 0; i < src_size; i++) {
 202                                        printf(" %d ", i);
 203                                        if (src[i])
 204                                                printf("%s\n", sha1_to_hex(src[i]->sha1));
 205                                        else
 206                                                printf("\n");
 207                                }
 208#endif
 209                                ret = fn(src);
 210                                
 211#if DBRT_DEBUG > 1
 212                                printf("Added %d entries\n", ret);
 213#endif
 214                                *indpos += ret;
 215                        } else {
 216                                for (i = 0; i < src_size; i++) {
 217                                        if (src[i]) {
 218                                                add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
 219                                        }
 220                                }
 221                        }
 222                }
 223                if (any_dirs) {
 224                        char *newbase = xmalloc(baselen + 2 + pathlen);
 225                        memcpy(newbase, base, baselen);
 226                        memcpy(newbase + baselen, first, pathlen);
 227                        newbase[baselen + pathlen] = '/';
 228                        newbase[baselen + pathlen + 1] = '\0';
 229                        if (unpack_trees_rec(subposns, len, newbase, fn,
 230                                             indpos))
 231                                return -1;
 232                        free(newbase);
 233                }
 234                free(subposns);
 235                free(src);
 236        } while (1);
 237}
 238
 239static void reject_merge(struct cache_entry *ce)
 240{
 241        die("Entry '%s' would be overwritten by merge. Cannot merge.", 
 242            ce->name);
 243}
 244
 245/* Unlink the last component and attempt to remove leading
 246 * directories, in case this unlink is the removal of the
 247 * last entry in the directory -- empty directories are removed.
 248 */
 249static void unlink_entry(char *name)
 250{
 251        char *cp, *prev;
 252
 253        if (unlink(name))
 254                return;
 255        prev = NULL;
 256        while (1) {
 257                int status;
 258                cp = strrchr(name, '/');
 259                if (prev)
 260                        *prev = '/';
 261                if (!cp)
 262                        break;
 263
 264                *cp = 0;
 265                status = rmdir(name);
 266                if (status) {
 267                        *cp = '/';
 268                        break;
 269                }
 270                prev = cp;
 271        }
 272}
 273
 274static void progress_interval(int signum)
 275{
 276        progress_update = 1;
 277}
 278
 279static void setup_progress_signal(void)
 280{
 281        struct sigaction sa;
 282        struct itimerval v;
 283
 284        memset(&sa, 0, sizeof(sa));
 285        sa.sa_handler = progress_interval;
 286        sigemptyset(&sa.sa_mask);
 287        sa.sa_flags = SA_RESTART;
 288        sigaction(SIGALRM, &sa, NULL);
 289
 290        v.it_interval.tv_sec = 1;
 291        v.it_interval.tv_usec = 0;
 292        v.it_value = v.it_interval;
 293        setitimer(ITIMER_REAL, &v, NULL);
 294}
 295
 296static void check_updates(struct cache_entry **src, int nr)
 297{
 298        static struct checkout state = {
 299                .base_dir = "",
 300                .force = 1,
 301                .quiet = 1,
 302                .refresh_cache = 1,
 303        };
 304        unsigned short mask = htons(CE_UPDATE);
 305        unsigned last_percent = 200, cnt = 0, total = 0;
 306
 307        if (update && verbose_update) {
 308                for (total = cnt = 0; cnt < nr; cnt++) {
 309                        struct cache_entry *ce = src[cnt];
 310                        if (!ce->ce_mode || ce->ce_flags & mask)
 311                                total++;
 312                }
 313
 314                /* Don't bother doing this for very small updates */
 315                if (total < 250)
 316                        total = 0;
 317
 318                if (total) {
 319                        fprintf(stderr, "Checking files out...\n");
 320                        setup_progress_signal();
 321                        progress_update = 1;
 322                }
 323                cnt = 0;
 324        }
 325
 326        while (nr--) {
 327                struct cache_entry *ce = *src++;
 328
 329                if (total) {
 330                        if (!ce->ce_mode || ce->ce_flags & mask) {
 331                                unsigned percent;
 332                                cnt++;
 333                                percent = (cnt * 100) / total;
 334                                if (percent != last_percent ||
 335                                    progress_update) {
 336                                        fprintf(stderr, "%4u%% (%u/%u) done\r",
 337                                                percent, cnt, total);
 338                                        last_percent = percent;
 339                                }
 340                        }
 341                }
 342                if (!ce->ce_mode) {
 343                        if (update)
 344                                unlink_entry(ce->name);
 345                        continue;
 346                }
 347                if (ce->ce_flags & mask) {
 348                        ce->ce_flags &= ~mask;
 349                        if (update)
 350                                checkout_entry(ce, &state, NULL);
 351                }
 352        }
 353        if (total) {
 354                signal(SIGALRM, SIG_IGN);
 355                fputc('\n', stderr);
 356        }
 357}
 358
 359static int unpack_trees(merge_fn_t fn)
 360{
 361        int indpos = 0;
 362        unsigned len = object_list_length(trees);
 363        struct tree_entry_list **posns;
 364        int i;
 365        struct object_list *posn = trees;
 366        merge_size = len;
 367
 368        if (len) {
 369                posns = xmalloc(len * sizeof(struct tree_entry_list *));
 370                for (i = 0; i < len; i++) {
 371                        posns[i] = ((struct tree *) posn->item)->entries;
 372                        posn = posn->next;
 373                }
 374                if (unpack_trees_rec(posns, len, "", fn, &indpos))
 375                        return -1;
 376        }
 377
 378        if (trivial_merges_only && nontrivial_merge)
 379                die("Merge requires file-level merging");
 380
 381        check_updates(active_cache, active_nr);
 382        return 0;
 383}
 384
 385static int list_tree(unsigned char *sha1)
 386{
 387        struct tree *tree = parse_tree_indirect(sha1);
 388        if (!tree)
 389                return -1;
 390        object_list_append(&tree->object, &trees);
 391        return 0;
 392}
 393
 394static int same(struct cache_entry *a, struct cache_entry *b)
 395{
 396        if (!!a != !!b)
 397                return 0;
 398        if (!a && !b)
 399                return 1;
 400        return a->ce_mode == b->ce_mode && 
 401                !memcmp(a->sha1, b->sha1, 20);
 402}
 403
 404
 405/*
 406 * When a CE gets turned into an unmerged entry, we
 407 * want it to be up-to-date
 408 */
 409static void verify_uptodate(struct cache_entry *ce)
 410{
 411        struct stat st;
 412
 413        if (index_only)
 414                return;
 415
 416        if (!lstat(ce->name, &st)) {
 417                unsigned changed = ce_match_stat(ce, &st, 1);
 418                if (!changed)
 419                        return;
 420                errno = 0;
 421        }
 422        if (errno == ENOENT)
 423                return;
 424        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 425}
 426
 427static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
 428{
 429        merge->ce_flags |= htons(CE_UPDATE);
 430        if (old) {
 431                /*
 432                 * See if we can re-use the old CE directly?
 433                 * That way we get the uptodate stat info.
 434                 *
 435                 * This also removes the UPDATE flag on
 436                 * a match.
 437                 */
 438                if (same(old, merge)) {
 439                        *merge = *old;
 440                } else {
 441                        verify_uptodate(old);
 442                }
 443        }
 444        merge->ce_flags &= ~htons(CE_STAGEMASK);
 445        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
 446        return 1;
 447}
 448
 449static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
 450{
 451        if (old)
 452                verify_uptodate(old);
 453        ce->ce_mode = 0;
 454        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 455        return 1;
 456}
 457
 458static int keep_entry(struct cache_entry *ce)
 459{
 460        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 461        return 1;
 462}
 463
 464#if DBRT_DEBUG
 465static void show_stage_entry(FILE *o,
 466                             const char *label, const struct cache_entry *ce)
 467{
 468        if (!ce)
 469                fprintf(o, "%s (missing)\n", label);
 470        else
 471                fprintf(o, "%s%06o %s %d\t%s\n",
 472                        label,
 473                        ntohl(ce->ce_mode),
 474                        sha1_to_hex(ce->sha1),
 475                        ce_stage(ce),
 476                        ce->name);
 477}
 478#endif
 479
 480static int threeway_merge(struct cache_entry **stages)
 481{
 482        struct cache_entry *index;
 483        struct cache_entry *head; 
 484        struct cache_entry *remote = stages[head_idx + 1];
 485        int count;
 486        int head_match = 0;
 487        int remote_match = 0;
 488
 489        int df_conflict_head = 0;
 490        int df_conflict_remote = 0;
 491
 492        int any_anc_missing = 0;
 493        int no_anc_exists = 1;
 494        int i;
 495
 496        for (i = 1; i < head_idx; i++) {
 497                if (!stages[i])
 498                        any_anc_missing = 1;
 499                else
 500                        no_anc_exists = 0;
 501        }
 502
 503        index = stages[0];
 504        head = stages[head_idx];
 505
 506        if (head == &df_conflict_entry) {
 507                df_conflict_head = 1;
 508                head = NULL;
 509        }
 510
 511        if (remote == &df_conflict_entry) {
 512                df_conflict_remote = 1;
 513                remote = NULL;
 514        }
 515
 516        /* First, if there's a #16 situation, note that to prevent #13
 517         * and #14. 
 518         */
 519        if (!same(remote, head)) {
 520                for (i = 1; i < head_idx; i++) {
 521                        if (same(stages[i], head)) {
 522                                head_match = i;
 523                        }
 524                        if (same(stages[i], remote)) {
 525                                remote_match = i;
 526                        }
 527                }
 528        }
 529
 530        /* We start with cases where the index is allowed to match
 531         * something other than the head: #14(ALT) and #2ALT, where it
 532         * is permitted to match the result instead.
 533         */
 534        /* #14, #14ALT, #2ALT */
 535        if (remote && !df_conflict_head && head_match && !remote_match) {
 536                if (index && !same(index, remote) && !same(index, head))
 537                        reject_merge(index);
 538                return merged_entry(remote, index);
 539        }
 540        /*
 541         * If we have an entry in the index cache, then we want to
 542         * make sure that it matches head.
 543         */
 544        if (index && !same(index, head)) {
 545                reject_merge(index);
 546        }
 547
 548        if (head) {
 549                /* #5ALT, #15 */
 550                if (same(head, remote))
 551                        return merged_entry(head, index);
 552                /* #13, #3ALT */
 553                if (!df_conflict_remote && remote_match && !head_match)
 554                        return merged_entry(head, index);
 555        }
 556
 557        /* #1 */
 558        if (!head && !remote && any_anc_missing)
 559                return 0;
 560
 561        /* Under the new "aggressive" rule, we resolve mostly trivial
 562         * cases that we historically had git-merge-one-file resolve.
 563         */
 564        if (aggressive) {
 565                int head_deleted = !head && !df_conflict_head;
 566                int remote_deleted = !remote && !df_conflict_remote;
 567                /*
 568                 * Deleted in both.
 569                 * Deleted in one and unchanged in the other.
 570                 */
 571                if ((head_deleted && remote_deleted) ||
 572                    (head_deleted && remote && remote_match) ||
 573                    (remote_deleted && head && head_match)) {
 574                        if (index)
 575                                return deleted_entry(index, index);
 576                        return 0;
 577                }
 578                /*
 579                 * Added in both, identically.
 580                 */
 581                if (no_anc_exists && head && remote && same(head, remote))
 582                        return merged_entry(head, index);
 583
 584        }
 585
 586        /* Below are "no merge" cases, which require that the index be
 587         * up-to-date to avoid the files getting overwritten with
 588         * conflict resolution files. 
 589         */
 590        if (index) {
 591                verify_uptodate(index);
 592        }
 593
 594        nontrivial_merge = 1;
 595
 596        /* #2, #3, #4, #6, #7, #9, #11. */
 597        count = 0;
 598        if (!head_match || !remote_match) {
 599                for (i = 1; i < head_idx; i++) {
 600                        if (stages[i]) {
 601                                keep_entry(stages[i]);
 602                                count++;
 603                                break;
 604                        }
 605                }
 606        }
 607#if DBRT_DEBUG
 608        else {
 609                fprintf(stderr, "read-tree: warning #16 detected\n");
 610                show_stage_entry(stderr, "head   ", stages[head_match]);
 611                show_stage_entry(stderr, "remote ", stages[remote_match]);
 612        }
 613#endif
 614        if (head) { count += keep_entry(head); }
 615        if (remote) { count += keep_entry(remote); }
 616        return count;
 617}
 618
 619/*
 620 * Two-way merge.
 621 *
 622 * The rule is to "carry forward" what is in the index without losing
 623 * information across a "fast forward", favoring a successful merge
 624 * over a merge failure when it makes sense.  For details of the
 625 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 626 *
 627 */
 628static int twoway_merge(struct cache_entry **src)
 629{
 630        struct cache_entry *current = src[0];
 631        struct cache_entry *oldtree = src[1], *newtree = src[2];
 632
 633        if (merge_size != 2)
 634                return error("Cannot do a twoway merge of %d trees",
 635                             merge_size);
 636
 637        if (current) {
 638                if ((!oldtree && !newtree) || /* 4 and 5 */
 639                    (!oldtree && newtree &&
 640                     same(current, newtree)) || /* 6 and 7 */
 641                    (oldtree && newtree &&
 642                     same(oldtree, newtree)) || /* 14 and 15 */
 643                    (oldtree && newtree &&
 644                     !same(oldtree, newtree) && /* 18 and 19*/
 645                     same(current, newtree))) {
 646                        return keep_entry(current);
 647                }
 648                else if (oldtree && !newtree && same(current, oldtree)) {
 649                        /* 10 or 11 */
 650                        return deleted_entry(oldtree, current);
 651                }
 652                else if (oldtree && newtree &&
 653                         same(current, oldtree) && !same(current, newtree)) {
 654                        /* 20 or 21 */
 655                        return merged_entry(newtree, current);
 656                }
 657                else {
 658                        /* all other failures */
 659                        if (oldtree)
 660                                reject_merge(oldtree);
 661                        if (current)
 662                                reject_merge(current);
 663                        if (newtree)
 664                                reject_merge(newtree);
 665                        return -1;
 666                }
 667        }
 668        else if (newtree)
 669                return merged_entry(newtree, current);
 670        else
 671                return deleted_entry(oldtree, current);
 672}
 673
 674/*
 675 * One-way merge.
 676 *
 677 * The rule is:
 678 * - take the stat information from stage0, take the data from stage1
 679 */
 680static int oneway_merge(struct cache_entry **src)
 681{
 682        struct cache_entry *old = src[0];
 683        struct cache_entry *a = src[1];
 684
 685        if (merge_size != 1)
 686                return error("Cannot do a oneway merge of %d trees",
 687                             merge_size);
 688
 689        if (!a)
 690                return 0;
 691        if (old && same(old, a)) {
 692                return keep_entry(old);
 693        }
 694        return merged_entry(a, NULL);
 695}
 696
 697static int read_cache_unmerged(void)
 698{
 699        int i, deleted;
 700        struct cache_entry **dst;
 701
 702        read_cache();
 703        dst = active_cache;
 704        deleted = 0;
 705        for (i = 0; i < active_nr; i++) {
 706                struct cache_entry *ce = active_cache[i];
 707                if (ce_stage(ce)) {
 708                        deleted++;
 709                        continue;
 710                }
 711                if (deleted)
 712                        *dst = ce;
 713                dst++;
 714        }
 715        active_nr -= deleted;
 716        return deleted;
 717}
 718
 719static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
 720
 721static struct cache_file cache_file;
 722
 723int main(int argc, char **argv)
 724{
 725        int i, newfd, reset, stage = 0;
 726        unsigned char sha1[20];
 727        merge_fn_t fn = NULL;
 728
 729        setup_git_directory();
 730        git_config(git_default_config);
 731
 732        newfd = hold_index_file_for_update(&cache_file, get_index_file());
 733        if (newfd < 0)
 734                die("unable to create new cachefile");
 735
 736        git_config(git_default_config);
 737
 738        merge = 0;
 739        reset = 0;
 740        for (i = 1; i < argc; i++) {
 741                const char *arg = argv[i];
 742
 743                /* "-u" means "update", meaning that a merge will update
 744                 * the working tree.
 745                 */
 746                if (!strcmp(arg, "-u")) {
 747                        update = 1;
 748                        continue;
 749                }
 750
 751                if (!strcmp(arg, "-v")) {
 752                        verbose_update = 1;
 753                        continue;
 754                }
 755
 756                /* "-i" means "index only", meaning that a merge will
 757                 * not even look at the working tree.
 758                 */
 759                if (!strcmp(arg, "-i")) {
 760                        index_only = 1;
 761                        continue;
 762                }
 763
 764                /* This differs from "-m" in that we'll silently ignore unmerged entries */
 765                if (!strcmp(arg, "--reset")) {
 766                        if (stage || merge)
 767                                usage(read_tree_usage);
 768                        reset = 1;
 769                        merge = 1;
 770                        stage = 1;
 771                        read_cache_unmerged();
 772                        continue;
 773                }
 774
 775                if (!strcmp(arg, "--trivial")) {
 776                        trivial_merges_only = 1;
 777                        continue;
 778                }
 779
 780                if (!strcmp(arg, "--aggressive")) {
 781                        aggressive = 1;
 782                        continue;
 783                }
 784
 785                /* "-m" stands for "merge", meaning we start in stage 1 */
 786                if (!strcmp(arg, "-m")) {
 787                        if (stage || merge)
 788                                usage(read_tree_usage);
 789                        if (read_cache_unmerged())
 790                                die("you need to resolve your current index first");
 791                        stage = 1;
 792                        merge = 1;
 793                        continue;
 794                }
 795
 796                /* using -u and -i at the same time makes no sense */
 797                if (1 < index_only + update)
 798                        usage(read_tree_usage);
 799
 800                if (get_sha1(arg, sha1) < 0)
 801                        usage(read_tree_usage);
 802                if (list_tree(sha1) < 0)
 803                        die("failed to unpack tree object %s", arg);
 804                stage++;
 805        }
 806        if ((update||index_only) && !merge)
 807                usage(read_tree_usage);
 808
 809        if (merge) {
 810                if (stage < 2)
 811                        die("just how do you expect me to merge %d trees?", stage-1);
 812                switch (stage - 1) {
 813                case 1:
 814                        fn = oneway_merge;
 815                        break;
 816                case 2:
 817                        fn = twoway_merge;
 818                        break;
 819                case 3:
 820                        fn = threeway_merge;
 821                        break;
 822                default:
 823                        fn = threeway_merge;
 824                        break;
 825                }
 826
 827                if (stage - 1 >= 3)
 828                        head_idx = stage - 2;
 829                else
 830                        head_idx = 1;
 831        }
 832
 833        unpack_trees(fn);
 834        if (write_cache(newfd, active_cache, active_nr) ||
 835            commit_index_file(&cache_file))
 836                die("unable to write new index file");
 837        return 0;
 838}