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