f0b8dad6eb5c469b78f08c12aa0f62befbb54e80
   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#include "builtin.h"
  15
  16static int reset = 0;
  17static int merge = 0;
  18static int update = 0;
  19static int index_only = 0;
  20static int nontrivial_merge = 0;
  21static int trivial_merges_only = 0;
  22static int aggressive = 0;
  23static int verbose_update = 0;
  24static volatile int progress_update = 0;
  25
  26static int head_idx = -1;
  27static int merge_size = 0;
  28
  29static struct object_list *trees = NULL;
  30
  31static struct cache_entry df_conflict_entry = { 
  32};
  33
  34static struct tree_entry_list df_conflict_list = {
  35        .name = NULL,
  36        .next = &df_conflict_list
  37};
  38
  39typedef int (*merge_fn_t)(struct cache_entry **src);
  40
  41static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
  42{
  43        int len1 = strlen(name1);
  44        int len2 = strlen(name2);
  45        int len = len1 < len2 ? len1 : len2;
  46        int ret = memcmp(name1, name2, len);
  47        unsigned char c1, c2;
  48        if (ret)
  49                return ret;
  50        c1 = name1[len];
  51        c2 = name2[len];
  52        if (!c1 && dir1)
  53                c1 = '/';
  54        if (!c2 && dir2)
  55                c2 = '/';
  56        ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
  57        if (c1 && c2 && !ret)
  58                ret = len1 - len2;
  59        return ret;
  60}
  61
  62static int unpack_trees_rec(struct tree_entry_list **posns, int len,
  63                            const char *base, merge_fn_t fn, int *indpos)
  64{
  65        int baselen = strlen(base);
  66        int src_size = len + 1;
  67        do {
  68                int i;
  69                const char *first;
  70                int firstdir = 0;
  71                int pathlen;
  72                unsigned ce_size;
  73                struct tree_entry_list **subposns;
  74                struct cache_entry **src;
  75                int any_files = 0;
  76                int any_dirs = 0;
  77                char *cache_name;
  78                int ce_stage;
  79
  80                /* Find the first name in the input. */
  81
  82                first = NULL;
  83                cache_name = NULL;
  84
  85                /* Check the cache */
  86                if (merge && *indpos < active_nr) {
  87                        /* This is a bit tricky: */
  88                        /* If the index has a subdirectory (with
  89                         * contents) as the first name, it'll get a
  90                         * filename like "foo/bar". But that's after
  91                         * "foo", so the entry in trees will get
  92                         * handled first, at which point we'll go into
  93                         * "foo", and deal with "bar" from the index,
  94                         * because the base will be "foo/". The only
  95                         * way we can actually have "foo/bar" first of
  96                         * all the things is if the trees don't
  97                         * contain "foo" at all, in which case we'll
  98                         * handle "foo/bar" without going into the
  99                         * directory, but that's fine (and will return
 100                         * an error anyway, with the added unknown
 101                         * file case.
 102                         */
 103
 104                        cache_name = active_cache[*indpos]->name;
 105                        if (strlen(cache_name) > baselen &&
 106                            !memcmp(cache_name, base, baselen)) {
 107                                cache_name += baselen;
 108                                first = cache_name;
 109                        } else {
 110                                cache_name = NULL;
 111                        }
 112                }
 113
 114#if DBRT_DEBUG > 1
 115                if (first)
 116                        printf("index %s\n", first);
 117#endif
 118                for (i = 0; i < len; i++) {
 119                        if (!posns[i] || posns[i] == &df_conflict_list)
 120                                continue;
 121#if DBRT_DEBUG > 1
 122                        printf("%d %s\n", i + 1, posns[i]->name);
 123#endif
 124                        if (!first || entcmp(first, firstdir,
 125                                             posns[i]->name, 
 126                                             posns[i]->directory) > 0) {
 127                                first = posns[i]->name;
 128                                firstdir = posns[i]->directory;
 129                        }
 130                }
 131                /* No name means we're done */
 132                if (!first)
 133                        return 0;
 134
 135                pathlen = strlen(first);
 136                ce_size = cache_entry_size(baselen + pathlen);
 137
 138                src = xcalloc(src_size, sizeof(struct cache_entry *));
 139
 140                subposns = xcalloc(len, sizeof(struct tree_list_entry *));
 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                                struct tree *tree = lookup_tree(posns[i]->sha1);
 164                                any_dirs = 1;
 165                                parse_tree(tree);
 166                                subposns[i] = tree->entries;
 167                                posns[i] = posns[i]->next;
 168                                src[i + merge] = &df_conflict_entry;
 169                                continue;
 170                        }
 171
 172                        if (!merge)
 173                                ce_stage = 0;
 174                        else if (i + 1 < head_idx)
 175                                ce_stage = 1;
 176                        else if (i + 1 > head_idx)
 177                                ce_stage = 3;
 178                        else
 179                                ce_stage = 2;
 180
 181                        ce = xcalloc(1, 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]->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 || reset)
 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 (reset) {
 423                ce->ce_flags |= htons(CE_UPDATE);
 424                return;
 425        }
 426        if (errno == ENOENT)
 427                return;
 428        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 429}
 430
 431/*
 432 * We do not want to remove or overwrite a working tree file that
 433 * is not tracked.
 434 */
 435static void verify_absent(const char *path, const char *action)
 436{
 437        struct stat st;
 438
 439        if (index_only || reset || !update)
 440                return;
 441        if (!lstat(path, &st))
 442                die("Untracked working tree file '%s' "
 443                    "would be %s by merge.", path, action);
 444}
 445
 446static int merged_entry(struct cache_entry *merge, struct cache_entry *old)
 447{
 448        merge->ce_flags |= htons(CE_UPDATE);
 449        if (old) {
 450                /*
 451                 * See if we can re-use the old CE directly?
 452                 * That way we get the uptodate stat info.
 453                 *
 454                 * This also removes the UPDATE flag on
 455                 * a match.
 456                 */
 457                if (same(old, merge)) {
 458                        *merge = *old;
 459                } else {
 460                        verify_uptodate(old);
 461                }
 462        }
 463        else
 464                verify_absent(merge->name, "overwritten");
 465
 466        merge->ce_flags &= ~htons(CE_STAGEMASK);
 467        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD);
 468        return 1;
 469}
 470
 471static int deleted_entry(struct cache_entry *ce, struct cache_entry *old)
 472{
 473        if (old)
 474                verify_uptodate(old);
 475        else
 476                verify_absent(ce->name, "removed");
 477        ce->ce_mode = 0;
 478        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 479        return 1;
 480}
 481
 482static int keep_entry(struct cache_entry *ce)
 483{
 484        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 485        return 1;
 486}
 487
 488#if DBRT_DEBUG
 489static void show_stage_entry(FILE *o,
 490                             const char *label, const struct cache_entry *ce)
 491{
 492        if (!ce)
 493                fprintf(o, "%s (missing)\n", label);
 494        else
 495                fprintf(o, "%s%06o %s %d\t%s\n",
 496                        label,
 497                        ntohl(ce->ce_mode),
 498                        sha1_to_hex(ce->sha1),
 499                        ce_stage(ce),
 500                        ce->name);
 501}
 502#endif
 503
 504static int threeway_merge(struct cache_entry **stages)
 505{
 506        struct cache_entry *index;
 507        struct cache_entry *head; 
 508        struct cache_entry *remote = stages[head_idx + 1];
 509        int count;
 510        int head_match = 0;
 511        int remote_match = 0;
 512        const char *path = NULL;
 513
 514        int df_conflict_head = 0;
 515        int df_conflict_remote = 0;
 516
 517        int any_anc_missing = 0;
 518        int no_anc_exists = 1;
 519        int i;
 520
 521        for (i = 1; i < head_idx; i++) {
 522                if (!stages[i])
 523                        any_anc_missing = 1;
 524                else {
 525                        if (!path)
 526                                path = stages[i]->name;
 527                        no_anc_exists = 0;
 528                }
 529        }
 530
 531        index = stages[0];
 532        head = stages[head_idx];
 533
 534        if (head == &df_conflict_entry) {
 535                df_conflict_head = 1;
 536                head = NULL;
 537        }
 538
 539        if (remote == &df_conflict_entry) {
 540                df_conflict_remote = 1;
 541                remote = NULL;
 542        }
 543
 544        if (!path && index)
 545                path = index->name;
 546        if (!path && head)
 547                path = head->name;
 548        if (!path && remote)
 549                path = remote->name;
 550
 551        /* First, if there's a #16 situation, note that to prevent #13
 552         * and #14.
 553         */
 554        if (!same(remote, head)) {
 555                for (i = 1; i < head_idx; i++) {
 556                        if (same(stages[i], head)) {
 557                                head_match = i;
 558                        }
 559                        if (same(stages[i], remote)) {
 560                                remote_match = i;
 561                        }
 562                }
 563        }
 564
 565        /* We start with cases where the index is allowed to match
 566         * something other than the head: #14(ALT) and #2ALT, where it
 567         * is permitted to match the result instead.
 568         */
 569        /* #14, #14ALT, #2ALT */
 570        if (remote && !df_conflict_head && head_match && !remote_match) {
 571                if (index && !same(index, remote) && !same(index, head))
 572                        reject_merge(index);
 573                return merged_entry(remote, index);
 574        }
 575        /*
 576         * If we have an entry in the index cache, then we want to
 577         * make sure that it matches head.
 578         */
 579        if (index && !same(index, head)) {
 580                reject_merge(index);
 581        }
 582
 583        if (head) {
 584                /* #5ALT, #15 */
 585                if (same(head, remote))
 586                        return merged_entry(head, index);
 587                /* #13, #3ALT */
 588                if (!df_conflict_remote && remote_match && !head_match)
 589                        return merged_entry(head, index);
 590        }
 591
 592        /* #1 */
 593        if (!head && !remote && any_anc_missing)
 594                return 0;
 595
 596        /* Under the new "aggressive" rule, we resolve mostly trivial
 597         * cases that we historically had git-merge-one-file resolve.
 598         */
 599        if (aggressive) {
 600                int head_deleted = !head && !df_conflict_head;
 601                int remote_deleted = !remote && !df_conflict_remote;
 602                /*
 603                 * Deleted in both.
 604                 * Deleted in one and unchanged in the other.
 605                 */
 606                if ((head_deleted && remote_deleted) ||
 607                    (head_deleted && remote && remote_match) ||
 608                    (remote_deleted && head && head_match)) {
 609                        if (index)
 610                                return deleted_entry(index, index);
 611                        else if (path)
 612                                verify_absent(path, "removed");
 613                        return 0;
 614                }
 615                /*
 616                 * Added in both, identically.
 617                 */
 618                if (no_anc_exists && head && remote && same(head, remote))
 619                        return merged_entry(head, index);
 620
 621        }
 622
 623        /* Below are "no merge" cases, which require that the index be
 624         * up-to-date to avoid the files getting overwritten with
 625         * conflict resolution files. 
 626         */
 627        if (index) {
 628                verify_uptodate(index);
 629        }
 630        else if (path)
 631                verify_absent(path, "overwritten");
 632
 633        nontrivial_merge = 1;
 634
 635        /* #2, #3, #4, #6, #7, #9, #11. */
 636        count = 0;
 637        if (!head_match || !remote_match) {
 638                for (i = 1; i < head_idx; i++) {
 639                        if (stages[i]) {
 640                                keep_entry(stages[i]);
 641                                count++;
 642                                break;
 643                        }
 644                }
 645        }
 646#if DBRT_DEBUG
 647        else {
 648                fprintf(stderr, "read-tree: warning #16 detected\n");
 649                show_stage_entry(stderr, "head   ", stages[head_match]);
 650                show_stage_entry(stderr, "remote ", stages[remote_match]);
 651        }
 652#endif
 653        if (head) { count += keep_entry(head); }
 654        if (remote) { count += keep_entry(remote); }
 655        return count;
 656}
 657
 658/*
 659 * Two-way merge.
 660 *
 661 * The rule is to "carry forward" what is in the index without losing
 662 * information across a "fast forward", favoring a successful merge
 663 * over a merge failure when it makes sense.  For details of the
 664 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 665 *
 666 */
 667static int twoway_merge(struct cache_entry **src)
 668{
 669        struct cache_entry *current = src[0];
 670        struct cache_entry *oldtree = src[1], *newtree = src[2];
 671
 672        if (merge_size != 2)
 673                return error("Cannot do a twoway merge of %d trees",
 674                             merge_size);
 675
 676        if (current) {
 677                if ((!oldtree && !newtree) || /* 4 and 5 */
 678                    (!oldtree && newtree &&
 679                     same(current, newtree)) || /* 6 and 7 */
 680                    (oldtree && newtree &&
 681                     same(oldtree, newtree)) || /* 14 and 15 */
 682                    (oldtree && newtree &&
 683                     !same(oldtree, newtree) && /* 18 and 19*/
 684                     same(current, newtree))) {
 685                        return keep_entry(current);
 686                }
 687                else if (oldtree && !newtree && same(current, oldtree)) {
 688                        /* 10 or 11 */
 689                        return deleted_entry(oldtree, current);
 690                }
 691                else if (oldtree && newtree &&
 692                         same(current, oldtree) && !same(current, newtree)) {
 693                        /* 20 or 21 */
 694                        return merged_entry(newtree, current);
 695                }
 696                else {
 697                        /* all other failures */
 698                        if (oldtree)
 699                                reject_merge(oldtree);
 700                        if (current)
 701                                reject_merge(current);
 702                        if (newtree)
 703                                reject_merge(newtree);
 704                        return -1;
 705                }
 706        }
 707        else if (newtree)
 708                return merged_entry(newtree, current);
 709        else
 710                return deleted_entry(oldtree, current);
 711}
 712
 713/*
 714 * One-way merge.
 715 *
 716 * The rule is:
 717 * - take the stat information from stage0, take the data from stage1
 718 */
 719static int oneway_merge(struct cache_entry **src)
 720{
 721        struct cache_entry *old = src[0];
 722        struct cache_entry *a = src[1];
 723
 724        if (merge_size != 1)
 725                return error("Cannot do a oneway merge of %d trees",
 726                             merge_size);
 727
 728        if (!a)
 729                return deleted_entry(old, old);
 730        if (old && same(old, a)) {
 731                if (reset) {
 732                        struct stat st;
 733                        if (lstat(old->name, &st) ||
 734                            ce_match_stat(old, &st, 1))
 735                                old->ce_flags |= htons(CE_UPDATE);
 736                }
 737                return keep_entry(old);
 738        }
 739        return merged_entry(a, old);
 740}
 741
 742static int read_cache_unmerged(void)
 743{
 744        int i, deleted;
 745        struct cache_entry **dst;
 746
 747        read_cache();
 748        dst = active_cache;
 749        deleted = 0;
 750        for (i = 0; i < active_nr; i++) {
 751                struct cache_entry *ce = active_cache[i];
 752                if (ce_stage(ce)) {
 753                        deleted++;
 754                        continue;
 755                }
 756                if (deleted)
 757                        *dst = ce;
 758                dst++;
 759        }
 760        active_nr -= deleted;
 761        return deleted;
 762}
 763
 764static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
 765
 766static struct cache_file cache_file;
 767
 768int cmd_read_tree(int argc, const char **argv, char **envp)
 769{
 770        int i, newfd, stage = 0;
 771        unsigned char sha1[20];
 772        merge_fn_t fn = NULL;
 773
 774        setup_git_directory();
 775        git_config(git_default_config);
 776
 777        newfd = hold_index_file_for_update(&cache_file, get_index_file());
 778        if (newfd < 0)
 779                die("unable to create new cachefile");
 780
 781        git_config(git_default_config);
 782
 783        merge = 0;
 784        reset = 0;
 785        for (i = 1; i < argc; i++) {
 786                const char *arg = argv[i];
 787
 788                /* "-u" means "update", meaning that a merge will update
 789                 * the working tree.
 790                 */
 791                if (!strcmp(arg, "-u")) {
 792                        update = 1;
 793                        continue;
 794                }
 795
 796                if (!strcmp(arg, "-v")) {
 797                        verbose_update = 1;
 798                        continue;
 799                }
 800
 801                /* "-i" means "index only", meaning that a merge will
 802                 * not even look at the working tree.
 803                 */
 804                if (!strcmp(arg, "-i")) {
 805                        index_only = 1;
 806                        continue;
 807                }
 808
 809                /* This differs from "-m" in that we'll silently ignore unmerged entries */
 810                if (!strcmp(arg, "--reset")) {
 811                        if (stage || merge)
 812                                usage(read_tree_usage);
 813                        reset = 1;
 814                        merge = 1;
 815                        stage = 1;
 816                        read_cache_unmerged();
 817                        continue;
 818                }
 819
 820                if (!strcmp(arg, "--trivial")) {
 821                        trivial_merges_only = 1;
 822                        continue;
 823                }
 824
 825                if (!strcmp(arg, "--aggressive")) {
 826                        aggressive = 1;
 827                        continue;
 828                }
 829
 830                /* "-m" stands for "merge", meaning we start in stage 1 */
 831                if (!strcmp(arg, "-m")) {
 832                        if (stage || merge)
 833                                usage(read_tree_usage);
 834                        if (read_cache_unmerged())
 835                                die("you need to resolve your current index first");
 836                        stage = 1;
 837                        merge = 1;
 838                        continue;
 839                }
 840
 841                /* using -u and -i at the same time makes no sense */
 842                if (1 < index_only + update)
 843                        usage(read_tree_usage);
 844
 845                if (get_sha1(arg, sha1))
 846                        die("Not a valid object name %s", arg);
 847                if (list_tree(sha1) < 0)
 848                        die("failed to unpack tree object %s", arg);
 849                stage++;
 850        }
 851        if ((update||index_only) && !merge)
 852                usage(read_tree_usage);
 853
 854        if (merge) {
 855                if (stage < 2)
 856                        die("just how do you expect me to merge %d trees?", stage-1);
 857                switch (stage - 1) {
 858                case 1:
 859                        fn = oneway_merge;
 860                        break;
 861                case 2:
 862                        fn = twoway_merge;
 863                        break;
 864                case 3:
 865                        fn = threeway_merge;
 866                        break;
 867                default:
 868                        fn = threeway_merge;
 869                        break;
 870                }
 871
 872                if (stage - 1 >= 3)
 873                        head_idx = stage - 2;
 874                else
 875                        head_idx = 1;
 876        }
 877
 878        unpack_trees(fn);
 879        if (write_cache(newfd, active_cache, active_nr) ||
 880            commit_index_file(&cache_file))
 881                die("unable to write new index file");
 882        return 0;
 883}