unpack-trees.con commit Merge branch 'lh/branch-rename' (abcb49c)
   1#include <signal.h>
   2#include <sys/time.h>
   3#include "cache.h"
   4#include "tree.h"
   5#include "tree-walk.h"
   6#include "cache-tree.h"
   7#include "unpack-trees.h"
   8
   9#define DBRT_DEBUG 1
  10
  11struct tree_entry_list {
  12        struct tree_entry_list *next;
  13        unsigned directory : 1;
  14        unsigned executable : 1;
  15        unsigned symlink : 1;
  16        unsigned int mode;
  17        const char *name;
  18        const unsigned char *sha1;
  19};
  20
  21static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
  22{
  23        struct tree_desc desc;
  24        struct name_entry one;
  25        struct tree_entry_list *ret = NULL;
  26        struct tree_entry_list **list_p = &ret;
  27
  28        if (!tree->object.parsed)
  29                parse_tree(tree);
  30
  31        desc.buf = tree->buffer;
  32        desc.size = 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                            int *indpos,
  76                            struct tree_entry_list *df_conflict_list)
  77{
  78        int baselen = strlen(base);
  79        int src_size = len + 1;
  80        do {
  81                int i;
  82                const char *first;
  83                int firstdir = 0;
  84                int pathlen;
  85                unsigned ce_size;
  86                struct tree_entry_list **subposns;
  87                struct cache_entry **src;
  88                int any_files = 0;
  89                int any_dirs = 0;
  90                char *cache_name;
  91                int ce_stage;
  92
  93                /* Find the first name in the input. */
  94
  95                first = NULL;
  96                cache_name = NULL;
  97
  98                /* Check the cache */
  99                if (o->merge && *indpos < active_nr) {
 100                        /* This is a bit tricky: */
 101                        /* If the index has a subdirectory (with
 102                         * contents) as the first name, it'll get a
 103                         * filename like "foo/bar". But that's after
 104                         * "foo", so the entry in trees will get
 105                         * handled first, at which point we'll go into
 106                         * "foo", and deal with "bar" from the index,
 107                         * because the base will be "foo/". The only
 108                         * way we can actually have "foo/bar" first of
 109                         * all the things is if the trees don't
 110                         * contain "foo" at all, in which case we'll
 111                         * handle "foo/bar" without going into the
 112                         * directory, but that's fine (and will return
 113                         * an error anyway, with the added unknown
 114                         * file case.
 115                         */
 116
 117                        cache_name = active_cache[*indpos]->name;
 118                        if (strlen(cache_name) > baselen &&
 119                            !memcmp(cache_name, base, baselen)) {
 120                                cache_name += baselen;
 121                                first = cache_name;
 122                        } else {
 123                                cache_name = NULL;
 124                        }
 125                }
 126
 127#if DBRT_DEBUG > 1
 128                if (first)
 129                        printf("index %s\n", first);
 130#endif
 131                for (i = 0; i < len; i++) {
 132                        if (!posns[i] || posns[i] == df_conflict_list)
 133                                continue;
 134#if DBRT_DEBUG > 1
 135                        printf("%d %s\n", i + 1, posns[i]->name);
 136#endif
 137                        if (!first || entcmp(first, firstdir,
 138                                             posns[i]->name,
 139                                             posns[i]->directory) > 0) {
 140                                first = posns[i]->name;
 141                                firstdir = posns[i]->directory;
 142                        }
 143                }
 144                /* No name means we're done */
 145                if (!first)
 146                        return 0;
 147
 148                pathlen = strlen(first);
 149                ce_size = cache_entry_size(baselen + pathlen);
 150
 151                src = xcalloc(src_size, sizeof(struct cache_entry *));
 152
 153                subposns = xcalloc(len, sizeof(struct tree_list_entry *));
 154
 155                if (cache_name && !strcmp(cache_name, first)) {
 156                        any_files = 1;
 157                        src[0] = active_cache[*indpos];
 158                        remove_cache_entry_at(*indpos);
 159                }
 160
 161                for (i = 0; i < len; i++) {
 162                        struct cache_entry *ce;
 163
 164                        if (!posns[i] ||
 165                            (posns[i] != df_conflict_list &&
 166                             strcmp(first, posns[i]->name))) {
 167                                continue;
 168                        }
 169
 170                        if (posns[i] == df_conflict_list) {
 171                                src[i + o->merge] = o->df_conflict_entry;
 172                                continue;
 173                        }
 174
 175                        if (posns[i]->directory) {
 176                                struct tree *tree = lookup_tree(posns[i]->sha1);
 177                                any_dirs = 1;
 178                                parse_tree(tree);
 179                                subposns[i] = create_tree_entry_list(tree);
 180                                posns[i] = posns[i]->next;
 181                                src[i + o->merge] = o->df_conflict_entry;
 182                                continue;
 183                        }
 184
 185                        if (!o->merge)
 186                                ce_stage = 0;
 187                        else if (i + 1 < o->head_idx)
 188                                ce_stage = 1;
 189                        else if (i + 1 > o->head_idx)
 190                                ce_stage = 3;
 191                        else
 192                                ce_stage = 2;
 193
 194                        ce = xcalloc(1, ce_size);
 195                        ce->ce_mode = create_ce_mode(posns[i]->mode);
 196                        ce->ce_flags = create_ce_flags(baselen + pathlen,
 197                                                       ce_stage);
 198                        memcpy(ce->name, base, baselen);
 199                        memcpy(ce->name + baselen, first, pathlen + 1);
 200
 201                        any_files = 1;
 202
 203                        hashcpy(ce->sha1, posns[i]->sha1);
 204                        src[i + o->merge] = ce;
 205                        subposns[i] = df_conflict_list;
 206                        posns[i] = posns[i]->next;
 207                }
 208                if (any_files) {
 209                        if (o->merge) {
 210                                int ret;
 211
 212#if DBRT_DEBUG > 1
 213                                printf("%s:\n", first);
 214                                for (i = 0; i < src_size; i++) {
 215                                        printf(" %d ", i);
 216                                        if (src[i])
 217                                                printf("%s\n", sha1_to_hex(src[i]->sha1));
 218                                        else
 219                                                printf("\n");
 220                                }
 221#endif
 222                                ret = o->fn(src, o);
 223
 224#if DBRT_DEBUG > 1
 225                                printf("Added %d entries\n", ret);
 226#endif
 227                                *indpos += ret;
 228                        } else {
 229                                for (i = 0; i < src_size; i++) {
 230                                        if (src[i]) {
 231                                                add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
 232                                        }
 233                                }
 234                        }
 235                }
 236                if (any_dirs) {
 237                        char *newbase = xmalloc(baselen + 2 + pathlen);
 238                        memcpy(newbase, base, baselen);
 239                        memcpy(newbase + baselen, first, pathlen);
 240                        newbase[baselen + pathlen] = '/';
 241                        newbase[baselen + pathlen + 1] = '\0';
 242                        if (unpack_trees_rec(subposns, len, newbase, o,
 243                                             indpos, df_conflict_list))
 244                                return -1;
 245                        free(newbase);
 246                }
 247                free(subposns);
 248                free(src);
 249        } while (1);
 250}
 251
 252/* Unlink the last component and attempt to remove leading
 253 * directories, in case this unlink is the removal of the
 254 * last entry in the directory -- empty directories are removed.
 255 */
 256static void unlink_entry(char *name)
 257{
 258        char *cp, *prev;
 259
 260        if (unlink(name))
 261                return;
 262        prev = NULL;
 263        while (1) {
 264                int status;
 265                cp = strrchr(name, '/');
 266                if (prev)
 267                        *prev = '/';
 268                if (!cp)
 269                        break;
 270
 271                *cp = 0;
 272                status = rmdir(name);
 273                if (status) {
 274                        *cp = '/';
 275                        break;
 276                }
 277                prev = cp;
 278        }
 279}
 280
 281static volatile sig_atomic_t progress_update;
 282
 283static void progress_interval(int signum)
 284{
 285        progress_update = 1;
 286}
 287
 288static void setup_progress_signal(void)
 289{
 290        struct sigaction sa;
 291        struct itimerval v;
 292
 293        memset(&sa, 0, sizeof(sa));
 294        sa.sa_handler = progress_interval;
 295        sigemptyset(&sa.sa_mask);
 296        sa.sa_flags = SA_RESTART;
 297        sigaction(SIGALRM, &sa, NULL);
 298
 299        v.it_interval.tv_sec = 1;
 300        v.it_interval.tv_usec = 0;
 301        v.it_value = v.it_interval;
 302        setitimer(ITIMER_REAL, &v, NULL);
 303}
 304
 305static struct checkout state;
 306static void check_updates(struct cache_entry **src, int nr,
 307                struct unpack_trees_options *o)
 308{
 309        unsigned short mask = htons(CE_UPDATE);
 310        unsigned last_percent = 200, cnt = 0, total = 0;
 311
 312        if (o->update && o->verbose_update) {
 313                for (total = cnt = 0; cnt < nr; cnt++) {
 314                        struct cache_entry *ce = src[cnt];
 315                        if (!ce->ce_mode || ce->ce_flags & mask)
 316                                total++;
 317                }
 318
 319                /* Don't bother doing this for very small updates */
 320                if (total < 250)
 321                        total = 0;
 322
 323                if (total) {
 324                        fprintf(stderr, "Checking files out...\n");
 325                        setup_progress_signal();
 326                        progress_update = 1;
 327                }
 328                cnt = 0;
 329        }
 330
 331        while (nr--) {
 332                struct cache_entry *ce = *src++;
 333
 334                if (total) {
 335                        if (!ce->ce_mode || ce->ce_flags & mask) {
 336                                unsigned percent;
 337                                cnt++;
 338                                percent = (cnt * 100) / total;
 339                                if (percent != last_percent ||
 340                                    progress_update) {
 341                                        fprintf(stderr, "%4u%% (%u/%u) done\r",
 342                                                percent, cnt, total);
 343                                        last_percent = percent;
 344                                        progress_update = 0;
 345                                }
 346                        }
 347                }
 348                if (!ce->ce_mode) {
 349                        if (o->update)
 350                                unlink_entry(ce->name);
 351                        continue;
 352                }
 353                if (ce->ce_flags & mask) {
 354                        ce->ce_flags &= ~mask;
 355                        if (o->update)
 356                                checkout_entry(ce, &state, NULL);
 357                }
 358        }
 359        if (total) {
 360                signal(SIGALRM, SIG_IGN);
 361                fputc('\n', stderr);
 362        }
 363}
 364
 365int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
 366{
 367        int indpos = 0;
 368        unsigned len = object_list_length(trees);
 369        struct tree_entry_list **posns;
 370        int i;
 371        struct object_list *posn = trees;
 372        struct tree_entry_list df_conflict_list;
 373        static struct cache_entry *dfc;
 374
 375        memset(&df_conflict_list, 0, sizeof(df_conflict_list));
 376        df_conflict_list.next = &df_conflict_list;
 377        memset(&state, 0, sizeof(state));
 378        state.base_dir = "";
 379        state.force = 1;
 380        state.quiet = 1;
 381        state.refresh_cache = 1;
 382
 383        o->merge_size = len;
 384
 385        if (!dfc)
 386                dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
 387        o->df_conflict_entry = dfc;
 388
 389        if (len) {
 390                posns = xmalloc(len * sizeof(struct tree_entry_list *));
 391                for (i = 0; i < len; i++) {
 392                        posns[i] = create_tree_entry_list((struct tree *) posn->item);
 393                        posn = posn->next;
 394                }
 395                if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
 396                                     o, &indpos, &df_conflict_list))
 397                        return -1;
 398        }
 399
 400        if (o->trivial_merges_only && o->nontrivial_merge)
 401                die("Merge requires file-level merging");
 402
 403        check_updates(active_cache, active_nr, o);
 404        return 0;
 405}
 406
 407/* Here come the merge functions */
 408
 409static void reject_merge(struct cache_entry *ce)
 410{
 411        die("Entry '%s' would be overwritten by merge. Cannot merge.",
 412            ce->name);
 413}
 414
 415static int same(struct cache_entry *a, struct cache_entry *b)
 416{
 417        if (!!a != !!b)
 418                return 0;
 419        if (!a && !b)
 420                return 1;
 421        return a->ce_mode == b->ce_mode &&
 422               !hashcmp(a->sha1, b->sha1);
 423}
 424
 425
 426/*
 427 * When a CE gets turned into an unmerged entry, we
 428 * want it to be up-to-date
 429 */
 430static void verify_uptodate(struct cache_entry *ce,
 431                struct unpack_trees_options *o)
 432{
 433        struct stat st;
 434
 435        if (o->index_only || o->reset)
 436                return;
 437
 438        if (!lstat(ce->name, &st)) {
 439                unsigned changed = ce_match_stat(ce, &st, 1);
 440                if (!changed)
 441                        return;
 442                errno = 0;
 443        }
 444        if (o->reset) {
 445                ce->ce_flags |= htons(CE_UPDATE);
 446                return;
 447        }
 448        if (errno == ENOENT)
 449                return;
 450        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 451}
 452
 453static void invalidate_ce_path(struct cache_entry *ce)
 454{
 455        if (ce)
 456                cache_tree_invalidate_path(active_cache_tree, ce->name);
 457}
 458
 459/*
 460 * We do not want to remove or overwrite a working tree file that
 461 * is not tracked.
 462 */
 463static void verify_absent(const char *path, const char *action,
 464                struct unpack_trees_options *o)
 465{
 466        struct stat st;
 467
 468        if (o->index_only || o->reset || !o->update)
 469                return;
 470        if (!lstat(path, &st))
 471                die("Untracked working tree file '%s' "
 472                    "would be %s by merge.", path, action);
 473}
 474
 475static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
 476                struct unpack_trees_options *o)
 477{
 478        merge->ce_flags |= htons(CE_UPDATE);
 479        if (old) {
 480                /*
 481                 * See if we can re-use the old CE directly?
 482                 * That way we get the uptodate stat info.
 483                 *
 484                 * This also removes the UPDATE flag on
 485                 * a match.
 486                 */
 487                if (same(old, merge)) {
 488                        *merge = *old;
 489                } else {
 490                        verify_uptodate(old, o);
 491                        invalidate_ce_path(old);
 492                }
 493        }
 494        else {
 495                verify_absent(merge->name, "overwritten", o);
 496                invalidate_ce_path(merge);
 497        }
 498
 499        merge->ce_flags &= ~htons(CE_STAGEMASK);
 500        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 501        return 1;
 502}
 503
 504static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
 505                struct unpack_trees_options *o)
 506{
 507        if (old)
 508                verify_uptodate(old, o);
 509        else
 510                verify_absent(ce->name, "removed", o);
 511        ce->ce_mode = 0;
 512        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 513        invalidate_ce_path(ce);
 514        return 1;
 515}
 516
 517static int keep_entry(struct cache_entry *ce)
 518{
 519        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 520        return 1;
 521}
 522
 523#if DBRT_DEBUG
 524static void show_stage_entry(FILE *o,
 525                             const char *label, const struct cache_entry *ce)
 526{
 527        if (!ce)
 528                fprintf(o, "%s (missing)\n", label);
 529        else
 530                fprintf(o, "%s%06o %s %d\t%s\n",
 531                        label,
 532                        ntohl(ce->ce_mode),
 533                        sha1_to_hex(ce->sha1),
 534                        ce_stage(ce),
 535                        ce->name);
 536}
 537#endif
 538
 539int threeway_merge(struct cache_entry **stages,
 540                struct unpack_trees_options *o)
 541{
 542        struct cache_entry *index;
 543        struct cache_entry *head;
 544        struct cache_entry *remote = stages[o->head_idx + 1];
 545        int count;
 546        int head_match = 0;
 547        int remote_match = 0;
 548        const char *path = NULL;
 549
 550        int df_conflict_head = 0;
 551        int df_conflict_remote = 0;
 552
 553        int any_anc_missing = 0;
 554        int no_anc_exists = 1;
 555        int i;
 556
 557        for (i = 1; i < o->head_idx; i++) {
 558                if (!stages[i])
 559                        any_anc_missing = 1;
 560                else {
 561                        if (!path)
 562                                path = stages[i]->name;
 563                        no_anc_exists = 0;
 564                }
 565        }
 566
 567        index = stages[0];
 568        head = stages[o->head_idx];
 569
 570        if (head == o->df_conflict_entry) {
 571                df_conflict_head = 1;
 572                head = NULL;
 573        }
 574
 575        if (remote == o->df_conflict_entry) {
 576                df_conflict_remote = 1;
 577                remote = NULL;
 578        }
 579
 580        if (!path && index)
 581                path = index->name;
 582        if (!path && head)
 583                path = head->name;
 584        if (!path && remote)
 585                path = remote->name;
 586
 587        /* First, if there's a #16 situation, note that to prevent #13
 588         * and #14.
 589         */
 590        if (!same(remote, head)) {
 591                for (i = 1; i < o->head_idx; i++) {
 592                        if (same(stages[i], head)) {
 593                                head_match = i;
 594                        }
 595                        if (same(stages[i], remote)) {
 596                                remote_match = i;
 597                        }
 598                }
 599        }
 600
 601        /* We start with cases where the index is allowed to match
 602         * something other than the head: #14(ALT) and #2ALT, where it
 603         * is permitted to match the result instead.
 604         */
 605        /* #14, #14ALT, #2ALT */
 606        if (remote && !df_conflict_head && head_match && !remote_match) {
 607                if (index && !same(index, remote) && !same(index, head))
 608                        reject_merge(index);
 609                return merged_entry(remote, index, o);
 610        }
 611        /*
 612         * If we have an entry in the index cache, then we want to
 613         * make sure that it matches head.
 614         */
 615        if (index && !same(index, head)) {
 616                reject_merge(index);
 617        }
 618
 619        if (head) {
 620                /* #5ALT, #15 */
 621                if (same(head, remote))
 622                        return merged_entry(head, index, o);
 623                /* #13, #3ALT */
 624                if (!df_conflict_remote && remote_match && !head_match)
 625                        return merged_entry(head, index, o);
 626        }
 627
 628        /* #1 */
 629        if (!head && !remote && any_anc_missing)
 630                return 0;
 631
 632        /* Under the new "aggressive" rule, we resolve mostly trivial
 633         * cases that we historically had git-merge-one-file resolve.
 634         */
 635        if (o->aggressive) {
 636                int head_deleted = !head && !df_conflict_head;
 637                int remote_deleted = !remote && !df_conflict_remote;
 638                /*
 639                 * Deleted in both.
 640                 * Deleted in one and unchanged in the other.
 641                 */
 642                if ((head_deleted && remote_deleted) ||
 643                    (head_deleted && remote && remote_match) ||
 644                    (remote_deleted && head && head_match)) {
 645                        if (index)
 646                                return deleted_entry(index, index, o);
 647                        else if (path && !head_deleted)
 648                                verify_absent(path, "removed", o);
 649                        return 0;
 650                }
 651                /*
 652                 * Added in both, identically.
 653                 */
 654                if (no_anc_exists && head && remote && same(head, remote))
 655                        return merged_entry(head, index, o);
 656
 657        }
 658
 659        /* Below are "no merge" cases, which require that the index be
 660         * up-to-date to avoid the files getting overwritten with
 661         * conflict resolution files.
 662         */
 663        if (index) {
 664                verify_uptodate(index, o);
 665        }
 666
 667        o->nontrivial_merge = 1;
 668
 669        /* #2, #3, #4, #6, #7, #9, #11. */
 670        count = 0;
 671        if (!head_match || !remote_match) {
 672                for (i = 1; i < o->head_idx; i++) {
 673                        if (stages[i]) {
 674                                keep_entry(stages[i]);
 675                                count++;
 676                                break;
 677                        }
 678                }
 679        }
 680#if DBRT_DEBUG
 681        else {
 682                fprintf(stderr, "read-tree: warning #16 detected\n");
 683                show_stage_entry(stderr, "head   ", stages[head_match]);
 684                show_stage_entry(stderr, "remote ", stages[remote_match]);
 685        }
 686#endif
 687        if (head) { count += keep_entry(head); }
 688        if (remote) { count += keep_entry(remote); }
 689        return count;
 690}
 691
 692/*
 693 * Two-way merge.
 694 *
 695 * The rule is to "carry forward" what is in the index without losing
 696 * information across a "fast forward", favoring a successful merge
 697 * over a merge failure when it makes sense.  For details of the
 698 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 699 *
 700 */
 701int twoway_merge(struct cache_entry **src,
 702                struct unpack_trees_options *o)
 703{
 704        struct cache_entry *current = src[0];
 705        struct cache_entry *oldtree = src[1], *newtree = src[2];
 706
 707        if (o->merge_size != 2)
 708                return error("Cannot do a twoway merge of %d trees",
 709                             o->merge_size);
 710
 711        if (current) {
 712                if ((!oldtree && !newtree) || /* 4 and 5 */
 713                    (!oldtree && newtree &&
 714                     same(current, newtree)) || /* 6 and 7 */
 715                    (oldtree && newtree &&
 716                     same(oldtree, newtree)) || /* 14 and 15 */
 717                    (oldtree && newtree &&
 718                     !same(oldtree, newtree) && /* 18 and 19*/
 719                     same(current, newtree))) {
 720                        return keep_entry(current);
 721                }
 722                else if (oldtree && !newtree && same(current, oldtree)) {
 723                        /* 10 or 11 */
 724                        return deleted_entry(oldtree, current, o);
 725                }
 726                else if (oldtree && newtree &&
 727                         same(current, oldtree) && !same(current, newtree)) {
 728                        /* 20 or 21 */
 729                        return merged_entry(newtree, current, o);
 730                }
 731                else {
 732                        /* all other failures */
 733                        if (oldtree)
 734                                reject_merge(oldtree);
 735                        if (current)
 736                                reject_merge(current);
 737                        if (newtree)
 738                                reject_merge(newtree);
 739                        return -1;
 740                }
 741        }
 742        else if (newtree)
 743                return merged_entry(newtree, current, o);
 744        else
 745                return deleted_entry(oldtree, current, o);
 746}
 747
 748/*
 749 * Bind merge.
 750 *
 751 * Keep the index entries at stage0, collapse stage1 but make sure
 752 * stage0 does not have anything there.
 753 */
 754int bind_merge(struct cache_entry **src,
 755                struct unpack_trees_options *o)
 756{
 757        struct cache_entry *old = src[0];
 758        struct cache_entry *a = src[1];
 759
 760        if (o->merge_size != 1)
 761                return error("Cannot do a bind merge of %d trees\n",
 762                             o->merge_size);
 763        if (a && old)
 764                die("Entry '%s' overlaps.  Cannot bind.", a->name);
 765        if (!a)
 766                return keep_entry(old);
 767        else
 768                return merged_entry(a, NULL, o);
 769}
 770
 771/*
 772 * One-way merge.
 773 *
 774 * The rule is:
 775 * - take the stat information from stage0, take the data from stage1
 776 */
 777int oneway_merge(struct cache_entry **src,
 778                struct unpack_trees_options *o)
 779{
 780        struct cache_entry *old = src[0];
 781        struct cache_entry *a = src[1];
 782
 783        if (o->merge_size != 1)
 784                return error("Cannot do a oneway merge of %d trees",
 785                             o->merge_size);
 786
 787        if (!a)
 788                return deleted_entry(old, old, o);
 789        if (old && same(old, a)) {
 790                if (o->reset) {
 791                        struct stat st;
 792                        if (lstat(old->name, &st) ||
 793                            ce_match_stat(old, &st, 1))
 794                                old->ce_flags |= htons(CE_UPDATE);
 795                }
 796                return keep_entry(old);
 797        }
 798        return merged_entry(a, old, o);
 799}