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