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