unpack-trees.con commit Merge branch 'tr/maint-no-unquote-plus' (d7cc7c9)
   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#include "attr.h"
  11
  12/*
  13 * Error messages expected by scripts out of plumbing commands such as
  14 * read-tree.  Non-scripted Porcelain is not required to use these messages
  15 * and in fact are encouraged to reword them to better suit their particular
  16 * situation better.  See how "git checkout" and "git merge" replaces
  17 * them using set_porcelain_error_msgs(), for example.
  18 */
  19const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
  20        /* ERROR_WOULD_OVERWRITE */
  21        "Entry '%s' would be overwritten by merge. Cannot merge.",
  22
  23        /* ERROR_NOT_UPTODATE_FILE */
  24        "Entry '%s' not uptodate. Cannot merge.",
  25
  26        /* ERROR_NOT_UPTODATE_DIR */
  27        "Updating '%s' would lose untracked files in it",
  28
  29        /* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
  30        "Untracked working tree file '%s' would be overwritten by merge.",
  31
  32        /* ERROR_WOULD_LOSE_UNTRACKED_REMOVED */
  33        "Untracked working tree file '%s' would be removed by merge.",
  34
  35        /* ERROR_BIND_OVERLAP */
  36        "Entry '%s' overlaps with '%s'.  Cannot bind.",
  37
  38        /* ERROR_SPARSE_NOT_UPTODATE_FILE */
  39        "Entry '%s' not uptodate. Cannot update sparse checkout.",
  40
  41        /* ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN */
  42        "Working tree file '%s' would be overwritten by sparse checkout update.",
  43
  44        /* ERROR_WOULD_LOSE_ORPHANED_REMOVED */
  45        "Working tree file '%s' would be removed by sparse checkout update.",
  46};
  47
  48#define ERRORMSG(o,type) \
  49        ( ((o) && (o)->msgs[(type)]) \
  50          ? ((o)->msgs[(type)])      \
  51          : (unpack_plumbing_errors[(type)]) )
  52
  53static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
  54        unsigned int set, unsigned int clear)
  55{
  56        unsigned int size = ce_size(ce);
  57        struct cache_entry *new = xmalloc(size);
  58
  59        clear |= CE_HASHED | CE_UNHASHED;
  60
  61        if (set & CE_REMOVE)
  62                set |= CE_WT_REMOVE;
  63
  64        memcpy(new, ce, size);
  65        new->next = NULL;
  66        new->ce_flags = (new->ce_flags & ~clear) | set;
  67        add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
  68}
  69
  70/*
  71 * add error messages on path <path>
  72 * corresponding to the type <e> with the message <msg>
  73 * indicating if it should be display in porcelain or not
  74 */
  75static int add_rejected_path(struct unpack_trees_options *o,
  76                             enum unpack_trees_error_types e,
  77                             const char *path)
  78{
  79        struct rejected_paths_list *newentry;
  80        int porcelain = o && (o)->msgs[e];
  81        /*
  82         * simply display the given error message if in plumbing mode
  83         */
  84        if (!porcelain)
  85                o->show_all_errors = 0;
  86        if (!o->show_all_errors)
  87                return error(ERRORMSG(o, e), path);
  88
  89        /*
  90         * Otherwise, insert in a list for future display by
  91         * display_error_msgs()
  92         */
  93        newentry = xmalloc(sizeof(struct rejected_paths_list));
  94        newentry->path = (char *)path;
  95        newentry->next = o->unpack_rejects[e];
  96        o->unpack_rejects[e] = newentry;
  97        return -1;
  98}
  99
 100/*
 101 * free all the structures allocated for the error <e>
 102 */
 103static void free_rejected_paths(struct unpack_trees_options *o,
 104                                enum unpack_trees_error_types e)
 105{
 106        while (o->unpack_rejects[e]) {
 107                struct rejected_paths_list *del = o->unpack_rejects[e];
 108                o->unpack_rejects[e] = o->unpack_rejects[e]->next;
 109                free(del);
 110        }
 111        free(o->unpack_rejects[e]);
 112}
 113
 114/*
 115 * display all the error messages stored in a nice way
 116 */
 117static void display_error_msgs(struct unpack_trees_options *o)
 118{
 119        int e;
 120        int something_displayed = 0;
 121        for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
 122                if (o->unpack_rejects[e]) {
 123                        struct rejected_paths_list *rp;
 124                        struct strbuf path = STRBUF_INIT;
 125                        something_displayed = 1;
 126                        for (rp = o->unpack_rejects[e]; rp; rp = rp->next)
 127                                strbuf_addf(&path, "\t%s\n", rp->path);
 128                        error(ERRORMSG(o, e), path.buf);
 129                        strbuf_release(&path);
 130                        free_rejected_paths(o, e);
 131                }
 132        }
 133        if (something_displayed)
 134                printf("Aborting\n");
 135}
 136
 137/*
 138 * Unlink the last component and schedule the leading directories for
 139 * removal, such that empty directories get removed.
 140 */
 141static void unlink_entry(struct cache_entry *ce)
 142{
 143        if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
 144                return;
 145        if (remove_or_warn(ce->ce_mode, ce->name))
 146                return;
 147        schedule_dir_for_removal(ce->name, ce_namelen(ce));
 148}
 149
 150static struct checkout state;
 151static int check_updates(struct unpack_trees_options *o)
 152{
 153        unsigned cnt = 0, total = 0;
 154        struct progress *progress = NULL;
 155        struct index_state *index = &o->result;
 156        int i;
 157        int errs = 0;
 158
 159        if (o->update && o->verbose_update) {
 160                for (total = cnt = 0; cnt < index->cache_nr; cnt++) {
 161                        struct cache_entry *ce = index->cache[cnt];
 162                        if (ce->ce_flags & (CE_UPDATE | CE_WT_REMOVE))
 163                                total++;
 164                }
 165
 166                progress = start_progress_delay("Checking out files",
 167                                                total, 50, 1);
 168                cnt = 0;
 169        }
 170
 171        if (o->update)
 172                git_attr_set_direction(GIT_ATTR_CHECKOUT, &o->result);
 173        for (i = 0; i < index->cache_nr; i++) {
 174                struct cache_entry *ce = index->cache[i];
 175
 176                if (ce->ce_flags & CE_WT_REMOVE) {
 177                        display_progress(progress, ++cnt);
 178                        if (o->update)
 179                                unlink_entry(ce);
 180                        continue;
 181                }
 182        }
 183        remove_marked_cache_entries(&o->result);
 184        remove_scheduled_dirs();
 185
 186        for (i = 0; i < index->cache_nr; i++) {
 187                struct cache_entry *ce = index->cache[i];
 188
 189                if (ce->ce_flags & CE_UPDATE) {
 190                        display_progress(progress, ++cnt);
 191                        ce->ce_flags &= ~CE_UPDATE;
 192                        if (o->update) {
 193                                errs |= checkout_entry(ce, &state, NULL);
 194                        }
 195                }
 196        }
 197        stop_progress(&progress);
 198        if (o->update)
 199                git_attr_set_direction(GIT_ATTR_CHECKIN, NULL);
 200        return errs != 0;
 201}
 202
 203static int verify_uptodate_sparse(struct cache_entry *ce, struct unpack_trees_options *o);
 204static int verify_absent_sparse(struct cache_entry *ce, enum unpack_trees_error_types, struct unpack_trees_options *o);
 205
 206static int will_have_skip_worktree(const struct cache_entry *ce, struct unpack_trees_options *o)
 207{
 208        const char *basename;
 209
 210        basename = strrchr(ce->name, '/');
 211        basename = basename ? basename+1 : ce->name;
 212        return excluded_from_list(ce->name, ce_namelen(ce), basename, NULL, o->el) <= 0;
 213}
 214
 215static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_options *o)
 216{
 217        int was_skip_worktree = ce_skip_worktree(ce);
 218
 219        if (!ce_stage(ce) && will_have_skip_worktree(ce, o))
 220                ce->ce_flags |= CE_SKIP_WORKTREE;
 221        else
 222                ce->ce_flags &= ~CE_SKIP_WORKTREE;
 223
 224        /*
 225         * if (!was_skip_worktree && !ce_skip_worktree()) {
 226         *      This is perfectly normal. Move on;
 227         * }
 228         */
 229
 230        /*
 231         * Merge strategies may set CE_UPDATE|CE_REMOVE outside checkout
 232         * area as a result of ce_skip_worktree() shortcuts in
 233         * verify_absent() and verify_uptodate().
 234         * Make sure they don't modify worktree if they are already
 235         * outside checkout area
 236         */
 237        if (was_skip_worktree && ce_skip_worktree(ce)) {
 238                ce->ce_flags &= ~CE_UPDATE;
 239
 240                /*
 241                 * By default, when CE_REMOVE is on, CE_WT_REMOVE is also
 242                 * on to get that file removed from both index and worktree.
 243                 * If that file is already outside worktree area, don't
 244                 * bother remove it.
 245                 */
 246                if (ce->ce_flags & CE_REMOVE)
 247                        ce->ce_flags &= ~CE_WT_REMOVE;
 248        }
 249
 250        if (!was_skip_worktree && ce_skip_worktree(ce)) {
 251                /*
 252                 * If CE_UPDATE is set, verify_uptodate() must be called already
 253                 * also stat info may have lost after merged_entry() so calling
 254                 * verify_uptodate() again may fail
 255                 */
 256                if (!(ce->ce_flags & CE_UPDATE) && verify_uptodate_sparse(ce, o))
 257                        return -1;
 258                ce->ce_flags |= CE_WT_REMOVE;
 259        }
 260        if (was_skip_worktree && !ce_skip_worktree(ce)) {
 261                if (verify_absent_sparse(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
 262                        return -1;
 263                ce->ce_flags |= CE_UPDATE;
 264        }
 265        return 0;
 266}
 267
 268static inline int call_unpack_fn(struct cache_entry **src, struct unpack_trees_options *o)
 269{
 270        int ret = o->fn(src, o);
 271        if (ret > 0)
 272                ret = 0;
 273        return ret;
 274}
 275
 276static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o)
 277{
 278        ce->ce_flags |= CE_UNPACKED;
 279
 280        if (o->cache_bottom < o->src_index->cache_nr &&
 281            o->src_index->cache[o->cache_bottom] == ce) {
 282                int bottom = o->cache_bottom;
 283                while (bottom < o->src_index->cache_nr &&
 284                       o->src_index->cache[bottom]->ce_flags & CE_UNPACKED)
 285                        bottom++;
 286                o->cache_bottom = bottom;
 287        }
 288}
 289
 290static void mark_all_ce_unused(struct index_state *index)
 291{
 292        int i;
 293        for (i = 0; i < index->cache_nr; i++)
 294                index->cache[i]->ce_flags &= ~CE_UNPACKED;
 295}
 296
 297static int locate_in_src_index(struct cache_entry *ce,
 298                               struct unpack_trees_options *o)
 299{
 300        struct index_state *index = o->src_index;
 301        int len = ce_namelen(ce);
 302        int pos = index_name_pos(index, ce->name, len);
 303        if (pos < 0)
 304                pos = -1 - pos;
 305        return pos;
 306}
 307
 308/*
 309 * We call unpack_index_entry() with an unmerged cache entry
 310 * only in diff-index, and it wants a single callback.  Skip
 311 * the other unmerged entry with the same name.
 312 */
 313static void mark_ce_used_same_name(struct cache_entry *ce,
 314                                   struct unpack_trees_options *o)
 315{
 316        struct index_state *index = o->src_index;
 317        int len = ce_namelen(ce);
 318        int pos;
 319
 320        for (pos = locate_in_src_index(ce, o); pos < index->cache_nr; pos++) {
 321                struct cache_entry *next = index->cache[pos];
 322                if (len != ce_namelen(next) ||
 323                    memcmp(ce->name, next->name, len))
 324                        break;
 325                mark_ce_used(next, o);
 326        }
 327}
 328
 329static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
 330{
 331        const struct index_state *index = o->src_index;
 332        int pos = o->cache_bottom;
 333
 334        while (pos < index->cache_nr) {
 335                struct cache_entry *ce = index->cache[pos];
 336                if (!(ce->ce_flags & CE_UNPACKED))
 337                        return ce;
 338                pos++;
 339        }
 340        return NULL;
 341}
 342
 343static void add_same_unmerged(struct cache_entry *ce,
 344                              struct unpack_trees_options *o)
 345{
 346        struct index_state *index = o->src_index;
 347        int len = ce_namelen(ce);
 348        int pos = index_name_pos(index, ce->name, len);
 349
 350        if (0 <= pos)
 351                die("programming error in a caller of mark_ce_used_same_name");
 352        for (pos = -pos - 1; pos < index->cache_nr; pos++) {
 353                struct cache_entry *next = index->cache[pos];
 354                if (len != ce_namelen(next) ||
 355                    memcmp(ce->name, next->name, len))
 356                        break;
 357                add_entry(o, next, 0, 0);
 358                mark_ce_used(next, o);
 359        }
 360}
 361
 362static int unpack_index_entry(struct cache_entry *ce,
 363                              struct unpack_trees_options *o)
 364{
 365        struct cache_entry *src[5] = { NULL };
 366        int ret;
 367
 368        src[0] = ce;
 369
 370        mark_ce_used(ce, o);
 371        if (ce_stage(ce)) {
 372                if (o->skip_unmerged) {
 373                        add_entry(o, ce, 0, 0);
 374                        return 0;
 375                }
 376        }
 377        ret = call_unpack_fn(src, o);
 378        if (ce_stage(ce))
 379                mark_ce_used_same_name(ce, o);
 380        return ret;
 381}
 382
 383static int find_cache_pos(struct traverse_info *, const struct name_entry *);
 384
 385static void restore_cache_bottom(struct traverse_info *info, int bottom)
 386{
 387        struct unpack_trees_options *o = info->data;
 388
 389        if (o->diff_index_cached)
 390                return;
 391        o->cache_bottom = bottom;
 392}
 393
 394static int switch_cache_bottom(struct traverse_info *info)
 395{
 396        struct unpack_trees_options *o = info->data;
 397        int ret, pos;
 398
 399        if (o->diff_index_cached)
 400                return 0;
 401        ret = o->cache_bottom;
 402        pos = find_cache_pos(info->prev, &info->name);
 403
 404        if (pos < -1)
 405                o->cache_bottom = -2 - pos;
 406        else if (pos < 0)
 407                o->cache_bottom = o->src_index->cache_nr;
 408        return ret;
 409}
 410
 411static int traverse_trees_recursive(int n, unsigned long dirmask, unsigned long df_conflicts, struct name_entry *names, struct traverse_info *info)
 412{
 413        int i, ret, bottom;
 414        struct tree_desc t[MAX_UNPACK_TREES];
 415        void *buf[MAX_UNPACK_TREES];
 416        struct traverse_info newinfo;
 417        struct name_entry *p;
 418
 419        p = names;
 420        while (!p->mode)
 421                p++;
 422
 423        newinfo = *info;
 424        newinfo.prev = info;
 425        newinfo.name = *p;
 426        newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
 427        newinfo.conflicts |= df_conflicts;
 428
 429        for (i = 0; i < n; i++, dirmask >>= 1) {
 430                const unsigned char *sha1 = NULL;
 431                if (dirmask & 1)
 432                        sha1 = names[i].sha1;
 433                buf[i] = fill_tree_descriptor(t+i, sha1);
 434        }
 435
 436        bottom = switch_cache_bottom(&newinfo);
 437        ret = traverse_trees(n, t, &newinfo);
 438        restore_cache_bottom(&newinfo, bottom);
 439
 440        for (i = 0; i < n; i++)
 441                free(buf[i]);
 442
 443        return ret;
 444}
 445
 446/*
 447 * Compare the traverse-path to the cache entry without actually
 448 * having to generate the textual representation of the traverse
 449 * path.
 450 *
 451 * NOTE! This *only* compares up to the size of the traverse path
 452 * itself - the caller needs to do the final check for the cache
 453 * entry having more data at the end!
 454 */
 455static int do_compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
 456{
 457        int len, pathlen, ce_len;
 458        const char *ce_name;
 459
 460        if (info->prev) {
 461                int cmp = do_compare_entry(ce, info->prev, &info->name);
 462                if (cmp)
 463                        return cmp;
 464        }
 465        pathlen = info->pathlen;
 466        ce_len = ce_namelen(ce);
 467
 468        /* If ce_len < pathlen then we must have previously hit "name == directory" entry */
 469        if (ce_len < pathlen)
 470                return -1;
 471
 472        ce_len -= pathlen;
 473        ce_name = ce->name + pathlen;
 474
 475        len = tree_entry_len(n->path, n->sha1);
 476        return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
 477}
 478
 479static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
 480{
 481        int cmp = do_compare_entry(ce, info, n);
 482        if (cmp)
 483                return cmp;
 484
 485        /*
 486         * Even if the beginning compared identically, the ce should
 487         * compare as bigger than a directory leading up to it!
 488         */
 489        return ce_namelen(ce) > traverse_path_len(info, n);
 490}
 491
 492static int ce_in_traverse_path(const struct cache_entry *ce,
 493                               const struct traverse_info *info)
 494{
 495        if (!info->prev)
 496                return 1;
 497        if (do_compare_entry(ce, info->prev, &info->name))
 498                return 0;
 499        /*
 500         * If ce (blob) is the same name as the path (which is a tree
 501         * we will be descending into), it won't be inside it.
 502         */
 503        return (info->pathlen < ce_namelen(ce));
 504}
 505
 506static struct cache_entry *create_ce_entry(const struct traverse_info *info, const struct name_entry *n, int stage)
 507{
 508        int len = traverse_path_len(info, n);
 509        struct cache_entry *ce = xcalloc(1, cache_entry_size(len));
 510
 511        ce->ce_mode = create_ce_mode(n->mode);
 512        ce->ce_flags = create_ce_flags(len, stage);
 513        hashcpy(ce->sha1, n->sha1);
 514        make_traverse_path(ce->name, info, n);
 515
 516        return ce;
 517}
 518
 519static int unpack_nondirectories(int n, unsigned long mask,
 520                                 unsigned long dirmask,
 521                                 struct cache_entry **src,
 522                                 const struct name_entry *names,
 523                                 const struct traverse_info *info)
 524{
 525        int i;
 526        struct unpack_trees_options *o = info->data;
 527        unsigned long conflicts;
 528
 529        /* Do we have *only* directories? Nothing to do */
 530        if (mask == dirmask && !src[0])
 531                return 0;
 532
 533        conflicts = info->conflicts;
 534        if (o->merge)
 535                conflicts >>= 1;
 536        conflicts |= dirmask;
 537
 538        /*
 539         * Ok, we've filled in up to any potential index entry in src[0],
 540         * now do the rest.
 541         */
 542        for (i = 0; i < n; i++) {
 543                int stage;
 544                unsigned int bit = 1ul << i;
 545                if (conflicts & bit) {
 546                        src[i + o->merge] = o->df_conflict_entry;
 547                        continue;
 548                }
 549                if (!(mask & bit))
 550                        continue;
 551                if (!o->merge)
 552                        stage = 0;
 553                else if (i + 1 < o->head_idx)
 554                        stage = 1;
 555                else if (i + 1 > o->head_idx)
 556                        stage = 3;
 557                else
 558                        stage = 2;
 559                src[i + o->merge] = create_ce_entry(info, names + i, stage);
 560        }
 561
 562        if (o->merge)
 563                return call_unpack_fn(src, o);
 564
 565        for (i = 0; i < n; i++)
 566                if (src[i] && src[i] != o->df_conflict_entry)
 567                        add_entry(o, src[i], 0, 0);
 568        return 0;
 569}
 570
 571static int unpack_failed(struct unpack_trees_options *o, const char *message)
 572{
 573        discard_index(&o->result);
 574        if (!o->gently) {
 575                if (message)
 576                        return error("%s", message);
 577                return -1;
 578        }
 579        return -1;
 580}
 581
 582/* NEEDSWORK: give this a better name and share with tree-walk.c */
 583static int name_compare(const char *a, int a_len,
 584                        const char *b, int b_len)
 585{
 586        int len = (a_len < b_len) ? a_len : b_len;
 587        int cmp = memcmp(a, b, len);
 588        if (cmp)
 589                return cmp;
 590        return (a_len - b_len);
 591}
 592
 593/*
 594 * The tree traversal is looking at name p.  If we have a matching entry,
 595 * return it.  If name p is a directory in the index, do not return
 596 * anything, as we will want to match it when the traversal descends into
 597 * the directory.
 598 */
 599static int find_cache_pos(struct traverse_info *info,
 600                          const struct name_entry *p)
 601{
 602        int pos;
 603        struct unpack_trees_options *o = info->data;
 604        struct index_state *index = o->src_index;
 605        int pfxlen = info->pathlen;
 606        int p_len = tree_entry_len(p->path, p->sha1);
 607
 608        for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
 609                struct cache_entry *ce = index->cache[pos];
 610                const char *ce_name, *ce_slash;
 611                int cmp, ce_len;
 612
 613                if (ce->ce_flags & CE_UNPACKED) {
 614                        /*
 615                         * cache_bottom entry is already unpacked, so
 616                         * we can never match it; don't check it
 617                         * again.
 618                         */
 619                        if (pos == o->cache_bottom)
 620                                ++o->cache_bottom;
 621                        continue;
 622                }
 623                if (!ce_in_traverse_path(ce, info))
 624                        continue;
 625                ce_name = ce->name + pfxlen;
 626                ce_slash = strchr(ce_name, '/');
 627                if (ce_slash)
 628                        ce_len = ce_slash - ce_name;
 629                else
 630                        ce_len = ce_namelen(ce) - pfxlen;
 631                cmp = name_compare(p->path, p_len, ce_name, ce_len);
 632                /*
 633                 * Exact match; if we have a directory we need to
 634                 * delay returning it.
 635                 */
 636                if (!cmp)
 637                        return ce_slash ? -2 - pos : pos;
 638                if (0 < cmp)
 639                        continue; /* keep looking */
 640                /*
 641                 * ce_name sorts after p->path; could it be that we
 642                 * have files under p->path directory in the index?
 643                 * E.g.  ce_name == "t-i", and p->path == "t"; we may
 644                 * have "t/a" in the index.
 645                 */
 646                if (p_len < ce_len && !memcmp(ce_name, p->path, p_len) &&
 647                    ce_name[p_len] < '/')
 648                        continue; /* keep looking */
 649                break;
 650        }
 651        return -1;
 652}
 653
 654static struct cache_entry *find_cache_entry(struct traverse_info *info,
 655                                            const struct name_entry *p)
 656{
 657        int pos = find_cache_pos(info, p);
 658        struct unpack_trees_options *o = info->data;
 659
 660        if (0 <= pos)
 661                return o->src_index->cache[pos];
 662        else
 663                return NULL;
 664}
 665
 666static void debug_path(struct traverse_info *info)
 667{
 668        if (info->prev) {
 669                debug_path(info->prev);
 670                if (*info->prev->name.path)
 671                        putchar('/');
 672        }
 673        printf("%s", info->name.path);
 674}
 675
 676static void debug_name_entry(int i, struct name_entry *n)
 677{
 678        printf("ent#%d %06o %s\n", i,
 679               n->path ? n->mode : 0,
 680               n->path ? n->path : "(missing)");
 681}
 682
 683static void debug_unpack_callback(int n,
 684                                  unsigned long mask,
 685                                  unsigned long dirmask,
 686                                  struct name_entry *names,
 687                                  struct traverse_info *info)
 688{
 689        int i;
 690        printf("* unpack mask %lu, dirmask %lu, cnt %d ",
 691               mask, dirmask, n);
 692        debug_path(info);
 693        putchar('\n');
 694        for (i = 0; i < n; i++)
 695                debug_name_entry(i, names + i);
 696}
 697
 698static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
 699{
 700        struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
 701        struct unpack_trees_options *o = info->data;
 702        const struct name_entry *p = names;
 703
 704        /* Find first entry with a real name (we could use "mask" too) */
 705        while (!p->mode)
 706                p++;
 707
 708        if (o->debug_unpack)
 709                debug_unpack_callback(n, mask, dirmask, names, info);
 710
 711        /* Are we supposed to look at the index too? */
 712        if (o->merge) {
 713                while (1) {
 714                        int cmp;
 715                        struct cache_entry *ce;
 716
 717                        if (o->diff_index_cached)
 718                                ce = next_cache_entry(o);
 719                        else
 720                                ce = find_cache_entry(info, p);
 721
 722                        if (!ce)
 723                                break;
 724                        cmp = compare_entry(ce, info, p);
 725                        if (cmp < 0) {
 726                                if (unpack_index_entry(ce, o) < 0)
 727                                        return unpack_failed(o, NULL);
 728                                continue;
 729                        }
 730                        if (!cmp) {
 731                                if (ce_stage(ce)) {
 732                                        /*
 733                                         * If we skip unmerged index
 734                                         * entries, we'll skip this
 735                                         * entry *and* the tree
 736                                         * entries associated with it!
 737                                         */
 738                                        if (o->skip_unmerged) {
 739                                                add_same_unmerged(ce, o);
 740                                                return mask;
 741                                        }
 742                                }
 743                                src[0] = ce;
 744                        }
 745                        break;
 746                }
 747        }
 748
 749        if (unpack_nondirectories(n, mask, dirmask, src, names, info) < 0)
 750                return -1;
 751
 752        if (src[0]) {
 753                if (ce_stage(src[0]))
 754                        mark_ce_used_same_name(src[0], o);
 755                else
 756                        mark_ce_used(src[0], o);
 757        }
 758
 759        /* Now handle any directories.. */
 760        if (dirmask) {
 761                unsigned long conflicts = mask & ~dirmask;
 762                if (o->merge) {
 763                        conflicts <<= 1;
 764                        if (src[0])
 765                                conflicts |= 1;
 766                }
 767
 768                /* special case: "diff-index --cached" looking at a tree */
 769                if (o->diff_index_cached &&
 770                    n == 1 && dirmask == 1 && S_ISDIR(names->mode)) {
 771                        int matches;
 772                        matches = cache_tree_matches_traversal(o->src_index->cache_tree,
 773                                                               names, info);
 774                        /*
 775                         * Everything under the name matches; skip the
 776                         * entire hierarchy.  diff_index_cached codepath
 777                         * special cases D/F conflicts in such a way that
 778                         * it does not do any look-ahead, so this is safe.
 779                         */
 780                        if (matches) {
 781                                o->cache_bottom += matches;
 782                                return mask;
 783                        }
 784                }
 785
 786                if (traverse_trees_recursive(n, dirmask, conflicts,
 787                                             names, info) < 0)
 788                        return -1;
 789                return mask;
 790        }
 791
 792        return mask;
 793}
 794
 795/*
 796 * N-way merge "len" trees.  Returns 0 on success, -1 on failure to manipulate the
 797 * resulting index, -2 on failure to reflect the changes to the work tree.
 798 */
 799int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
 800{
 801        int i, ret;
 802        static struct cache_entry *dfc;
 803        struct exclude_list el;
 804
 805        if (len > MAX_UNPACK_TREES)
 806                die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES);
 807        memset(&state, 0, sizeof(state));
 808        state.base_dir = "";
 809        state.force = 1;
 810        state.quiet = 1;
 811        state.refresh_cache = 1;
 812
 813        memset(&el, 0, sizeof(el));
 814        if (!core_apply_sparse_checkout || !o->update)
 815                o->skip_sparse_checkout = 1;
 816        if (!o->skip_sparse_checkout) {
 817                if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, NULL, &el, 0) < 0)
 818                        o->skip_sparse_checkout = 1;
 819                else
 820                        o->el = &el;
 821        }
 822
 823        memset(&o->result, 0, sizeof(o->result));
 824        o->result.initialized = 1;
 825        o->result.timestamp.sec = o->src_index->timestamp.sec;
 826        o->result.timestamp.nsec = o->src_index->timestamp.nsec;
 827        o->merge_size = len;
 828        mark_all_ce_unused(o->src_index);
 829
 830        if (!dfc)
 831                dfc = xcalloc(1, cache_entry_size(0));
 832        o->df_conflict_entry = dfc;
 833
 834        if (len) {
 835                const char *prefix = o->prefix ? o->prefix : "";
 836                struct traverse_info info;
 837
 838                setup_traverse_info(&info, prefix);
 839                info.fn = unpack_callback;
 840                info.data = o;
 841                info.show_all_errors = o->show_all_errors;
 842
 843                if (o->prefix) {
 844                        /*
 845                         * Unpack existing index entries that sort before the
 846                         * prefix the tree is spliced into.  Note that o->merge
 847                         * is always true in this case.
 848                         */
 849                        while (1) {
 850                                struct cache_entry *ce = next_cache_entry(o);
 851                                if (!ce)
 852                                        break;
 853                                if (ce_in_traverse_path(ce, &info))
 854                                        break;
 855                                if (unpack_index_entry(ce, o) < 0)
 856                                        goto return_failed;
 857                        }
 858                }
 859
 860                if (traverse_trees(len, t, &info) < 0)
 861                        goto return_failed;
 862        }
 863
 864        /* Any left-over entries in the index? */
 865        if (o->merge) {
 866                while (1) {
 867                        struct cache_entry *ce = next_cache_entry(o);
 868                        if (!ce)
 869                                break;
 870                        if (unpack_index_entry(ce, o) < 0)
 871                                goto return_failed;
 872                }
 873        }
 874        mark_all_ce_unused(o->src_index);
 875
 876        if (o->trivial_merges_only && o->nontrivial_merge) {
 877                ret = unpack_failed(o, "Merge requires file-level merging");
 878                goto done;
 879        }
 880
 881        if (!o->skip_sparse_checkout) {
 882                int empty_worktree = 1;
 883                for (i = 0;i < o->result.cache_nr;i++) {
 884                        struct cache_entry *ce = o->result.cache[i];
 885
 886                        if (apply_sparse_checkout(ce, o)) {
 887                                ret = -1;
 888                                goto done;
 889                        }
 890                        if (!ce_skip_worktree(ce))
 891                                empty_worktree = 0;
 892
 893                }
 894                if (o->result.cache_nr && empty_worktree) {
 895                        ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory");
 896                        goto done;
 897                }
 898        }
 899
 900        o->src_index = NULL;
 901        ret = check_updates(o) ? (-2) : 0;
 902        if (o->dst_index)
 903                *o->dst_index = o->result;
 904
 905done:
 906        for (i = 0;i < el.nr;i++)
 907                free(el.excludes[i]);
 908        if (el.excludes)
 909                free(el.excludes);
 910
 911        return ret;
 912
 913return_failed:
 914        if (o->show_all_errors)
 915                display_error_msgs(o);
 916        mark_all_ce_unused(o->src_index);
 917        ret = unpack_failed(o, NULL);
 918        goto done;
 919}
 920
 921/* Here come the merge functions */
 922
 923static int reject_merge(struct cache_entry *ce, struct unpack_trees_options *o)
 924{
 925        return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
 926}
 927
 928static int same(struct cache_entry *a, struct cache_entry *b)
 929{
 930        if (!!a != !!b)
 931                return 0;
 932        if (!a && !b)
 933                return 1;
 934        if ((a->ce_flags | b->ce_flags) & CE_CONFLICTED)
 935                return 0;
 936        return a->ce_mode == b->ce_mode &&
 937               !hashcmp(a->sha1, b->sha1);
 938}
 939
 940
 941/*
 942 * When a CE gets turned into an unmerged entry, we
 943 * want it to be up-to-date
 944 */
 945static int verify_uptodate_1(struct cache_entry *ce,
 946                                   struct unpack_trees_options *o,
 947                                   enum unpack_trees_error_types error_type)
 948{
 949        struct stat st;
 950
 951        if (o->index_only || (!((ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) && (o->reset || ce_uptodate(ce))))
 952                return 0;
 953
 954        if (!lstat(ce->name, &st)) {
 955                unsigned changed = ie_match_stat(o->src_index, ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
 956                if (!changed)
 957                        return 0;
 958                /*
 959                 * NEEDSWORK: the current default policy is to allow
 960                 * submodule to be out of sync wrt the supermodule
 961                 * index.  This needs to be tightened later for
 962                 * submodules that are marked to be automatically
 963                 * checked out.
 964                 */
 965                if (S_ISGITLINK(ce->ce_mode))
 966                        return 0;
 967                errno = 0;
 968        }
 969        if (errno == ENOENT)
 970                return 0;
 971        return o->gently ? -1 :
 972                add_rejected_path(o, error_type, ce->name);
 973}
 974
 975static int verify_uptodate(struct cache_entry *ce,
 976                           struct unpack_trees_options *o)
 977{
 978        if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
 979                return 0;
 980        return verify_uptodate_1(ce, o, ERROR_NOT_UPTODATE_FILE);
 981}
 982
 983static int verify_uptodate_sparse(struct cache_entry *ce,
 984                                  struct unpack_trees_options *o)
 985{
 986        return verify_uptodate_1(ce, o, ERROR_SPARSE_NOT_UPTODATE_FILE);
 987}
 988
 989static void invalidate_ce_path(struct cache_entry *ce, struct unpack_trees_options *o)
 990{
 991        if (ce)
 992                cache_tree_invalidate_path(o->src_index->cache_tree, ce->name);
 993}
 994
 995/*
 996 * Check that checking out ce->sha1 in subdir ce->name is not
 997 * going to overwrite any working files.
 998 *
 999 * Currently, git does not checkout subprojects during a superproject
1000 * checkout, so it is not going to overwrite anything.
1001 */
1002static int verify_clean_submodule(struct cache_entry *ce,
1003                                      enum unpack_trees_error_types error_type,
1004                                      struct unpack_trees_options *o)
1005{
1006        return 0;
1007}
1008
1009static int verify_clean_subdirectory(struct cache_entry *ce,
1010                                      enum unpack_trees_error_types error_type,
1011                                      struct unpack_trees_options *o)
1012{
1013        /*
1014         * we are about to extract "ce->name"; we would not want to lose
1015         * anything in the existing directory there.
1016         */
1017        int namelen;
1018        int i;
1019        struct dir_struct d;
1020        char *pathbuf;
1021        int cnt = 0;
1022        unsigned char sha1[20];
1023
1024        if (S_ISGITLINK(ce->ce_mode) &&
1025            resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
1026                /* If we are not going to update the submodule, then
1027                 * we don't care.
1028                 */
1029                if (!hashcmp(sha1, ce->sha1))
1030                        return 0;
1031                return verify_clean_submodule(ce, error_type, o);
1032        }
1033
1034        /*
1035         * First let's make sure we do not have a local modification
1036         * in that directory.
1037         */
1038        namelen = strlen(ce->name);
1039        for (i = locate_in_src_index(ce, o);
1040             i < o->src_index->cache_nr;
1041             i++) {
1042                struct cache_entry *ce2 = o->src_index->cache[i];
1043                int len = ce_namelen(ce2);
1044                if (len < namelen ||
1045                    strncmp(ce->name, ce2->name, namelen) ||
1046                    ce2->name[namelen] != '/')
1047                        break;
1048                /*
1049                 * ce2->name is an entry in the subdirectory to be
1050                 * removed.
1051                 */
1052                if (!ce_stage(ce2)) {
1053                        if (verify_uptodate(ce2, o))
1054                                return -1;
1055                        add_entry(o, ce2, CE_REMOVE, 0);
1056                        mark_ce_used(ce2, o);
1057                }
1058                cnt++;
1059        }
1060
1061        /*
1062         * Then we need to make sure that we do not lose a locally
1063         * present file that is not ignored.
1064         */
1065        pathbuf = xmalloc(namelen + 2);
1066        memcpy(pathbuf, ce->name, namelen);
1067        strcpy(pathbuf+namelen, "/");
1068
1069        memset(&d, 0, sizeof(d));
1070        if (o->dir)
1071                d.exclude_per_dir = o->dir->exclude_per_dir;
1072        i = read_directory(&d, pathbuf, namelen+1, NULL);
1073        if (i)
1074                return o->gently ? -1 :
1075                        add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
1076        free(pathbuf);
1077        return cnt;
1078}
1079
1080/*
1081 * This gets called when there was no index entry for the tree entry 'dst',
1082 * but we found a file in the working tree that 'lstat()' said was fine,
1083 * and we're on a case-insensitive filesystem.
1084 *
1085 * See if we can find a case-insensitive match in the index that also
1086 * matches the stat information, and assume it's that other file!
1087 */
1088static int icase_exists(struct unpack_trees_options *o, struct cache_entry *dst, struct stat *st)
1089{
1090        struct cache_entry *src;
1091
1092        src = index_name_exists(o->src_index, dst->name, ce_namelen(dst), 1);
1093        return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
1094}
1095
1096/*
1097 * We do not want to remove or overwrite a working tree file that
1098 * is not tracked, unless it is ignored.
1099 */
1100static int verify_absent_1(struct cache_entry *ce,
1101                                 enum unpack_trees_error_types error_type,
1102                                 struct unpack_trees_options *o)
1103{
1104        struct stat st;
1105
1106        if (o->index_only || o->reset || !o->update)
1107                return 0;
1108
1109        if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
1110                return 0;
1111
1112        if (!lstat(ce->name, &st)) {
1113                int dtype = ce_to_dtype(ce);
1114                struct cache_entry *result;
1115
1116                /*
1117                 * It may be that the 'lstat()' succeeded even though
1118                 * target 'ce' was absent, because there is an old
1119                 * entry that is different only in case..
1120                 *
1121                 * Ignore that lstat() if it matches.
1122                 */
1123                if (ignore_case && icase_exists(o, ce, &st))
1124                        return 0;
1125
1126                if (o->dir && excluded(o->dir, ce->name, &dtype))
1127                        /*
1128                         * ce->name is explicitly excluded, so it is Ok to
1129                         * overwrite it.
1130                         */
1131                        return 0;
1132                if (S_ISDIR(st.st_mode)) {
1133                        /*
1134                         * We are checking out path "foo" and
1135                         * found "foo/." in the working tree.
1136                         * This is tricky -- if we have modified
1137                         * files that are in "foo/" we would lose
1138                         * them.
1139                         */
1140                        if (verify_clean_subdirectory(ce, error_type, o) < 0)
1141                                return -1;
1142                        return 0;
1143                }
1144
1145                /*
1146                 * The previous round may already have decided to
1147                 * delete this path, which is in a subdirectory that
1148                 * is being replaced with a blob.
1149                 */
1150                result = index_name_exists(&o->result, ce->name, ce_namelen(ce), 0);
1151                if (result) {
1152                        if (result->ce_flags & CE_REMOVE)
1153                                return 0;
1154                }
1155
1156                return o->gently ? -1 :
1157                        add_rejected_path(o, error_type, ce->name);
1158        }
1159        return 0;
1160}
1161static int verify_absent(struct cache_entry *ce,
1162                         enum unpack_trees_error_types error_type,
1163                         struct unpack_trees_options *o)
1164{
1165        if (!o->skip_sparse_checkout && will_have_skip_worktree(ce, o))
1166                return 0;
1167        return verify_absent_1(ce, error_type, o);
1168}
1169
1170static int verify_absent_sparse(struct cache_entry *ce,
1171                         enum unpack_trees_error_types error_type,
1172                         struct unpack_trees_options *o)
1173{
1174        enum unpack_trees_error_types orphaned_error = error_type;
1175        if (orphaned_error == ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN)
1176                orphaned_error = ERROR_WOULD_LOSE_ORPHANED_OVERWRITTEN;
1177
1178        return verify_absent_1(ce, orphaned_error, o);
1179}
1180
1181static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1182                struct unpack_trees_options *o)
1183{
1184        int update = CE_UPDATE;
1185
1186        if (!old) {
1187                if (verify_absent(merge, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
1188                        return -1;
1189                if (!o->skip_sparse_checkout && will_have_skip_worktree(merge, o))
1190                        update |= CE_SKIP_WORKTREE;
1191                invalidate_ce_path(merge, o);
1192        } else if (!(old->ce_flags & CE_CONFLICTED)) {
1193                /*
1194                 * See if we can re-use the old CE directly?
1195                 * That way we get the uptodate stat info.
1196                 *
1197                 * This also removes the UPDATE flag on a match; otherwise
1198                 * we will end up overwriting local changes in the work tree.
1199                 */
1200                if (same(old, merge)) {
1201                        copy_cache_entry(merge, old);
1202                        update = 0;
1203                } else {
1204                        if (verify_uptodate(old, o))
1205                                return -1;
1206                        if (ce_skip_worktree(old))
1207                                update |= CE_SKIP_WORKTREE;
1208                        invalidate_ce_path(old, o);
1209                }
1210        } else {
1211                /*
1212                 * Previously unmerged entry left as an existence
1213                 * marker by read_index_unmerged();
1214                 */
1215                invalidate_ce_path(old, o);
1216        }
1217
1218        add_entry(o, merge, update, CE_STAGEMASK);
1219        return 1;
1220}
1221
1222static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
1223                struct unpack_trees_options *o)
1224{
1225        /* Did it exist in the index? */
1226        if (!old) {
1227                if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
1228                        return -1;
1229                return 0;
1230        }
1231        if (!(old->ce_flags & CE_CONFLICTED) && verify_uptodate(old, o))
1232                return -1;
1233        add_entry(o, ce, CE_REMOVE, 0);
1234        invalidate_ce_path(ce, o);
1235        return 1;
1236}
1237
1238static int keep_entry(struct cache_entry *ce, struct unpack_trees_options *o)
1239{
1240        add_entry(o, ce, 0, 0);
1241        return 1;
1242}
1243
1244#if DBRT_DEBUG
1245static void show_stage_entry(FILE *o,
1246                             const char *label, const struct cache_entry *ce)
1247{
1248        if (!ce)
1249                fprintf(o, "%s (missing)\n", label);
1250        else
1251                fprintf(o, "%s%06o %s %d\t%s\n",
1252                        label,
1253                        ce->ce_mode,
1254                        sha1_to_hex(ce->sha1),
1255                        ce_stage(ce),
1256                        ce->name);
1257}
1258#endif
1259
1260int threeway_merge(struct cache_entry **stages, struct unpack_trees_options *o)
1261{
1262        struct cache_entry *index;
1263        struct cache_entry *head;
1264        struct cache_entry *remote = stages[o->head_idx + 1];
1265        int count;
1266        int head_match = 0;
1267        int remote_match = 0;
1268
1269        int df_conflict_head = 0;
1270        int df_conflict_remote = 0;
1271
1272        int any_anc_missing = 0;
1273        int no_anc_exists = 1;
1274        int i;
1275
1276        for (i = 1; i < o->head_idx; i++) {
1277                if (!stages[i] || stages[i] == o->df_conflict_entry)
1278                        any_anc_missing = 1;
1279                else
1280                        no_anc_exists = 0;
1281        }
1282
1283        index = stages[0];
1284        head = stages[o->head_idx];
1285
1286        if (head == o->df_conflict_entry) {
1287                df_conflict_head = 1;
1288                head = NULL;
1289        }
1290
1291        if (remote == o->df_conflict_entry) {
1292                df_conflict_remote = 1;
1293                remote = NULL;
1294        }
1295
1296        /*
1297         * First, if there's a #16 situation, note that to prevent #13
1298         * and #14.
1299         */
1300        if (!same(remote, head)) {
1301                for (i = 1; i < o->head_idx; i++) {
1302                        if (same(stages[i], head)) {
1303                                head_match = i;
1304                        }
1305                        if (same(stages[i], remote)) {
1306                                remote_match = i;
1307                        }
1308                }
1309        }
1310
1311        /*
1312         * We start with cases where the index is allowed to match
1313         * something other than the head: #14(ALT) and #2ALT, where it
1314         * is permitted to match the result instead.
1315         */
1316        /* #14, #14ALT, #2ALT */
1317        if (remote && !df_conflict_head && head_match && !remote_match) {
1318                if (index && !same(index, remote) && !same(index, head))
1319                        return o->gently ? -1 : reject_merge(index, o);
1320                return merged_entry(remote, index, o);
1321        }
1322        /*
1323         * If we have an entry in the index cache, then we want to
1324         * make sure that it matches head.
1325         */
1326        if (index && !same(index, head))
1327                return o->gently ? -1 : reject_merge(index, o);
1328
1329        if (head) {
1330                /* #5ALT, #15 */
1331                if (same(head, remote))
1332                        return merged_entry(head, index, o);
1333                /* #13, #3ALT */
1334                if (!df_conflict_remote && remote_match && !head_match)
1335                        return merged_entry(head, index, o);
1336        }
1337
1338        /* #1 */
1339        if (!head && !remote && any_anc_missing)
1340                return 0;
1341
1342        /*
1343         * Under the "aggressive" rule, we resolve mostly trivial
1344         * cases that we historically had git-merge-one-file resolve.
1345         */
1346        if (o->aggressive) {
1347                int head_deleted = !head;
1348                int remote_deleted = !remote;
1349                struct cache_entry *ce = NULL;
1350
1351                if (index)
1352                        ce = index;
1353                else if (head)
1354                        ce = head;
1355                else if (remote)
1356                        ce = remote;
1357                else {
1358                        for (i = 1; i < o->head_idx; i++) {
1359                                if (stages[i] && stages[i] != o->df_conflict_entry) {
1360                                        ce = stages[i];
1361                                        break;
1362                                }
1363                        }
1364                }
1365
1366                /*
1367                 * Deleted in both.
1368                 * Deleted in one and unchanged in the other.
1369                 */
1370                if ((head_deleted && remote_deleted) ||
1371                    (head_deleted && remote && remote_match) ||
1372                    (remote_deleted && head && head_match)) {
1373                        if (index)
1374                                return deleted_entry(index, index, o);
1375                        if (ce && !head_deleted) {
1376                                if (verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_REMOVED, o))
1377                                        return -1;
1378                        }
1379                        return 0;
1380                }
1381                /*
1382                 * Added in both, identically.
1383                 */
1384                if (no_anc_exists && head && remote && same(head, remote))
1385                        return merged_entry(head, index, o);
1386
1387        }
1388
1389        /* Below are "no merge" cases, which require that the index be
1390         * up-to-date to avoid the files getting overwritten with
1391         * conflict resolution files.
1392         */
1393        if (index) {
1394                if (verify_uptodate(index, o))
1395                        return -1;
1396        }
1397
1398        o->nontrivial_merge = 1;
1399
1400        /* #2, #3, #4, #6, #7, #9, #10, #11. */
1401        count = 0;
1402        if (!head_match || !remote_match) {
1403                for (i = 1; i < o->head_idx; i++) {
1404                        if (stages[i] && stages[i] != o->df_conflict_entry) {
1405                                keep_entry(stages[i], o);
1406                                count++;
1407                                break;
1408                        }
1409                }
1410        }
1411#if DBRT_DEBUG
1412        else {
1413                fprintf(stderr, "read-tree: warning #16 detected\n");
1414                show_stage_entry(stderr, "head   ", stages[head_match]);
1415                show_stage_entry(stderr, "remote ", stages[remote_match]);
1416        }
1417#endif
1418        if (head) { count += keep_entry(head, o); }
1419        if (remote) { count += keep_entry(remote, o); }
1420        return count;
1421}
1422
1423/*
1424 * Two-way merge.
1425 *
1426 * The rule is to "carry forward" what is in the index without losing
1427 * information across a "fast-forward", favoring a successful merge
1428 * over a merge failure when it makes sense.  For details of the
1429 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
1430 *
1431 */
1432int twoway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1433{
1434        struct cache_entry *current = src[0];
1435        struct cache_entry *oldtree = src[1];
1436        struct cache_entry *newtree = src[2];
1437
1438        if (o->merge_size != 2)
1439                return error("Cannot do a twoway merge of %d trees",
1440                             o->merge_size);
1441
1442        if (oldtree == o->df_conflict_entry)
1443                oldtree = NULL;
1444        if (newtree == o->df_conflict_entry)
1445                newtree = NULL;
1446
1447        if (current) {
1448                if ((!oldtree && !newtree) || /* 4 and 5 */
1449                    (!oldtree && newtree &&
1450                     same(current, newtree)) || /* 6 and 7 */
1451                    (oldtree && newtree &&
1452                     same(oldtree, newtree)) || /* 14 and 15 */
1453                    (oldtree && newtree &&
1454                     !same(oldtree, newtree) && /* 18 and 19 */
1455                     same(current, newtree))) {
1456                        return keep_entry(current, o);
1457                }
1458                else if (oldtree && !newtree && same(current, oldtree)) {
1459                        /* 10 or 11 */
1460                        return deleted_entry(oldtree, current, o);
1461                }
1462                else if (oldtree && newtree &&
1463                         same(current, oldtree) && !same(current, newtree)) {
1464                        /* 20 or 21 */
1465                        return merged_entry(newtree, current, o);
1466                }
1467                else {
1468                        /* all other failures */
1469                        if (oldtree)
1470                                return o->gently ? -1 : reject_merge(oldtree, o);
1471                        if (current)
1472                                return o->gently ? -1 : reject_merge(current, o);
1473                        if (newtree)
1474                                return o->gently ? -1 : reject_merge(newtree, o);
1475                        return -1;
1476                }
1477        }
1478        else if (newtree) {
1479                if (oldtree && !o->initial_checkout) {
1480                        /*
1481                         * deletion of the path was staged;
1482                         */
1483                        if (same(oldtree, newtree))
1484                                return 1;
1485                        return reject_merge(oldtree, o);
1486                }
1487                return merged_entry(newtree, current, o);
1488        }
1489        return deleted_entry(oldtree, current, o);
1490}
1491
1492/*
1493 * Bind merge.
1494 *
1495 * Keep the index entries at stage0, collapse stage1 but make sure
1496 * stage0 does not have anything there.
1497 */
1498int bind_merge(struct cache_entry **src,
1499                struct unpack_trees_options *o)
1500{
1501        struct cache_entry *old = src[0];
1502        struct cache_entry *a = src[1];
1503
1504        if (o->merge_size != 1)
1505                return error("Cannot do a bind merge of %d trees\n",
1506                             o->merge_size);
1507        if (a && old)
1508                return o->gently ? -1 :
1509                        error(ERRORMSG(o, ERROR_BIND_OVERLAP), a->name, old->name);
1510        if (!a)
1511                return keep_entry(old, o);
1512        else
1513                return merged_entry(a, NULL, o);
1514}
1515
1516/*
1517 * One-way merge.
1518 *
1519 * The rule is:
1520 * - take the stat information from stage0, take the data from stage1
1521 */
1522int oneway_merge(struct cache_entry **src, struct unpack_trees_options *o)
1523{
1524        struct cache_entry *old = src[0];
1525        struct cache_entry *a = src[1];
1526
1527        if (o->merge_size != 1)
1528                return error("Cannot do a oneway merge of %d trees",
1529                             o->merge_size);
1530
1531        if (!a || a == o->df_conflict_entry)
1532                return deleted_entry(old, old, o);
1533
1534        if (old && same(old, a)) {
1535                int update = 0;
1536                if (o->reset && !ce_uptodate(old) && !ce_skip_worktree(old)) {
1537                        struct stat st;
1538                        if (lstat(old->name, &st) ||
1539                            ie_match_stat(o->src_index, old, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE))
1540                                update |= CE_UPDATE;
1541                }
1542                add_entry(o, old, update, 0);
1543                return 0;
1544        }
1545        return merged_entry(a, old, o);
1546}