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