unpack-trees.con commit git-commit: fix coding style. (3de63c3)
   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        struct cache_entry df_conflict_entry;
 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        memset(&df_conflict_entry, 0, sizeof(df_conflict_entry));
 385        o->df_conflict_entry = &df_conflict_entry;
 386
 387        if (len) {
 388                posns = xmalloc(len * sizeof(struct tree_entry_list *));
 389                for (i = 0; i < len; i++) {
 390                        posns[i] = create_tree_entry_list((struct tree *) posn->item);
 391                        posn = posn->next;
 392                }
 393                if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
 394                                     o, &indpos, &df_conflict_list))
 395                        return -1;
 396        }
 397
 398        if (o->trivial_merges_only && o->nontrivial_merge)
 399                die("Merge requires file-level merging");
 400
 401        check_updates(active_cache, active_nr, o);
 402        return 0;
 403}
 404
 405/* Here come the merge functions */
 406
 407static void reject_merge(struct cache_entry *ce)
 408{
 409        die("Entry '%s' would be overwritten by merge. Cannot merge.",
 410            ce->name);
 411}
 412
 413static int same(struct cache_entry *a, struct cache_entry *b)
 414{
 415        if (!!a != !!b)
 416                return 0;
 417        if (!a && !b)
 418                return 1;
 419        return a->ce_mode == b->ce_mode &&
 420               !hashcmp(a->sha1, b->sha1);
 421}
 422
 423
 424/*
 425 * When a CE gets turned into an unmerged entry, we
 426 * want it to be up-to-date
 427 */
 428static void verify_uptodate(struct cache_entry *ce,
 429                struct unpack_trees_options *o)
 430{
 431        struct stat st;
 432
 433        if (o->index_only || o->reset)
 434                return;
 435
 436        if (!lstat(ce->name, &st)) {
 437                unsigned changed = ce_match_stat(ce, &st, 1);
 438                if (!changed)
 439                        return;
 440                errno = 0;
 441        }
 442        if (o->reset) {
 443                ce->ce_flags |= htons(CE_UPDATE);
 444                return;
 445        }
 446        if (errno == ENOENT)
 447                return;
 448        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 449}
 450
 451static void invalidate_ce_path(struct cache_entry *ce)
 452{
 453        if (ce)
 454                cache_tree_invalidate_path(active_cache_tree, ce->name);
 455}
 456
 457/*
 458 * We do not want to remove or overwrite a working tree file that
 459 * is not tracked.
 460 */
 461static void verify_absent(const char *path, const char *action,
 462                struct unpack_trees_options *o)
 463{
 464        struct stat st;
 465
 466        if (o->index_only || o->reset || !o->update)
 467                return;
 468        if (!lstat(path, &st))
 469                die("Untracked working tree file '%s' "
 470                    "would be %s by merge.", path, action);
 471}
 472
 473static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
 474                struct unpack_trees_options *o)
 475{
 476        merge->ce_flags |= htons(CE_UPDATE);
 477        if (old) {
 478                /*
 479                 * See if we can re-use the old CE directly?
 480                 * That way we get the uptodate stat info.
 481                 *
 482                 * This also removes the UPDATE flag on
 483                 * a match.
 484                 */
 485                if (same(old, merge)) {
 486                        *merge = *old;
 487                } else {
 488                        verify_uptodate(old, o);
 489                        invalidate_ce_path(old);
 490                }
 491        }
 492        else {
 493                verify_absent(merge->name, "overwritten", o);
 494                invalidate_ce_path(merge);
 495        }
 496
 497        merge->ce_flags &= ~htons(CE_STAGEMASK);
 498        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 499        return 1;
 500}
 501
 502static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
 503                struct unpack_trees_options *o)
 504{
 505        if (old)
 506                verify_uptodate(old, o);
 507        else
 508                verify_absent(ce->name, "removed", o);
 509        ce->ce_mode = 0;
 510        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 511        invalidate_ce_path(ce);
 512        return 1;
 513}
 514
 515static int keep_entry(struct cache_entry *ce)
 516{
 517        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 518        return 1;
 519}
 520
 521#if DBRT_DEBUG
 522static void show_stage_entry(FILE *o,
 523                             const char *label, const struct cache_entry *ce)
 524{
 525        if (!ce)
 526                fprintf(o, "%s (missing)\n", label);
 527        else
 528                fprintf(o, "%s%06o %s %d\t%s\n",
 529                        label,
 530                        ntohl(ce->ce_mode),
 531                        sha1_to_hex(ce->sha1),
 532                        ce_stage(ce),
 533                        ce->name);
 534}
 535#endif
 536
 537int threeway_merge(struct cache_entry **stages,
 538                struct unpack_trees_options *o)
 539{
 540        struct cache_entry *index;
 541        struct cache_entry *head;
 542        struct cache_entry *remote = stages[o->head_idx + 1];
 543        int count;
 544        int head_match = 0;
 545        int remote_match = 0;
 546        const char *path = NULL;
 547
 548        int df_conflict_head = 0;
 549        int df_conflict_remote = 0;
 550
 551        int any_anc_missing = 0;
 552        int no_anc_exists = 1;
 553        int i;
 554
 555        for (i = 1; i < o->head_idx; i++) {
 556                if (!stages[i])
 557                        any_anc_missing = 1;
 558                else {
 559                        if (!path)
 560                                path = stages[i]->name;
 561                        no_anc_exists = 0;
 562                }
 563        }
 564
 565        index = stages[0];
 566        head = stages[o->head_idx];
 567
 568        if (head == o->df_conflict_entry) {
 569                df_conflict_head = 1;
 570                head = NULL;
 571        }
 572
 573        if (remote == o->df_conflict_entry) {
 574                df_conflict_remote = 1;
 575                remote = NULL;
 576        }
 577
 578        if (!path && index)
 579                path = index->name;
 580        if (!path && head)
 581                path = head->name;
 582        if (!path && remote)
 583                path = remote->name;
 584
 585        /* First, if there's a #16 situation, note that to prevent #13
 586         * and #14.
 587         */
 588        if (!same(remote, head)) {
 589                for (i = 1; i < o->head_idx; i++) {
 590                        if (same(stages[i], head)) {
 591                                head_match = i;
 592                        }
 593                        if (same(stages[i], remote)) {
 594                                remote_match = i;
 595                        }
 596                }
 597        }
 598
 599        /* We start with cases where the index is allowed to match
 600         * something other than the head: #14(ALT) and #2ALT, where it
 601         * is permitted to match the result instead.
 602         */
 603        /* #14, #14ALT, #2ALT */
 604        if (remote && !df_conflict_head && head_match && !remote_match) {
 605                if (index && !same(index, remote) && !same(index, head))
 606                        reject_merge(index);
 607                return merged_entry(remote, index, o);
 608        }
 609        /*
 610         * If we have an entry in the index cache, then we want to
 611         * make sure that it matches head.
 612         */
 613        if (index && !same(index, head)) {
 614                reject_merge(index);
 615        }
 616
 617        if (head) {
 618                /* #5ALT, #15 */
 619                if (same(head, remote))
 620                        return merged_entry(head, index, o);
 621                /* #13, #3ALT */
 622                if (!df_conflict_remote && remote_match && !head_match)
 623                        return merged_entry(head, index, o);
 624        }
 625
 626        /* #1 */
 627        if (!head && !remote && any_anc_missing)
 628                return 0;
 629
 630        /* Under the new "aggressive" rule, we resolve mostly trivial
 631         * cases that we historically had git-merge-one-file resolve.
 632         */
 633        if (o->aggressive) {
 634                int head_deleted = !head && !df_conflict_head;
 635                int remote_deleted = !remote && !df_conflict_remote;
 636                /*
 637                 * Deleted in both.
 638                 * Deleted in one and unchanged in the other.
 639                 */
 640                if ((head_deleted && remote_deleted) ||
 641                    (head_deleted && remote && remote_match) ||
 642                    (remote_deleted && head && head_match)) {
 643                        if (index)
 644                                return deleted_entry(index, index, o);
 645                        else if (path)
 646                                verify_absent(path, "removed", o);
 647                        return 0;
 648                }
 649                /*
 650                 * Added in both, identically.
 651                 */
 652                if (no_anc_exists && head && remote && same(head, remote))
 653                        return merged_entry(head, index, o);
 654
 655        }
 656
 657        /* Below are "no merge" cases, which require that the index be
 658         * up-to-date to avoid the files getting overwritten with
 659         * conflict resolution files.
 660         */
 661        if (index) {
 662                verify_uptodate(index, o);
 663        }
 664        else if (path)
 665                verify_absent(path, "overwritten", o);
 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}