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