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