unpack-trees.con commit add replay and log to the usage string of git-bisect (4ef40cd)
   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        desc.buf = tree->buffer;
  31        desc.size = tree->size;
  32
  33        while (tree_entry(&desc, &one)) {
  34                struct tree_entry_list *entry;
  35
  36                entry = xmalloc(sizeof(struct tree_entry_list));
  37                entry->name = one.path;
  38                entry->sha1 = one.sha1;
  39                entry->mode = one.mode;
  40                entry->directory = S_ISDIR(one.mode) != 0;
  41                entry->executable = (one.mode & S_IXUSR) != 0;
  42                entry->symlink = S_ISLNK(one.mode) != 0;
  43                entry->next = NULL;
  44
  45                *list_p = entry;
  46                list_p = &entry->next;
  47        }
  48        return ret;
  49}
  50
  51static int entcmp(const char *name1, int dir1, const char *name2, int dir2)
  52{
  53        int len1 = strlen(name1);
  54        int len2 = strlen(name2);
  55        int len = len1 < len2 ? len1 : len2;
  56        int ret = memcmp(name1, name2, len);
  57        unsigned char c1, c2;
  58        if (ret)
  59                return ret;
  60        c1 = name1[len];
  61        c2 = name2[len];
  62        if (!c1 && dir1)
  63                c1 = '/';
  64        if (!c2 && dir2)
  65                c2 = '/';
  66        ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
  67        if (c1 && c2 && !ret)
  68                ret = len1 - len2;
  69        return ret;
  70}
  71
  72static int unpack_trees_rec(struct tree_entry_list **posns, int len,
  73                            const char *base, struct unpack_trees_options *o,
  74                            int *indpos,
  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 && *indpos < 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[*indpos]->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[*indpos];
 163                        remove_cache_entry_at(*indpos);
 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                                *indpos += 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                                             indpos, 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)
 269{
 270        char *cp, *prev;
 271
 272        if (unlink(name))
 273                return;
 274        prev = NULL;
 275        while (1) {
 276                int status;
 277                cp = strrchr(name, '/');
 278                if (prev)
 279                        *prev = '/';
 280                if (!cp)
 281                        break;
 282
 283                *cp = 0;
 284                status = rmdir(name);
 285                if (status) {
 286                        *cp = '/';
 287                        break;
 288                }
 289                prev = cp;
 290        }
 291}
 292
 293static volatile sig_atomic_t progress_update;
 294
 295static void progress_interval(int signum)
 296{
 297        progress_update = 1;
 298}
 299
 300static void setup_progress_signal(void)
 301{
 302        struct sigaction sa;
 303        struct itimerval v;
 304
 305        memset(&sa, 0, sizeof(sa));
 306        sa.sa_handler = progress_interval;
 307        sigemptyset(&sa.sa_mask);
 308        sa.sa_flags = SA_RESTART;
 309        sigaction(SIGALRM, &sa, NULL);
 310
 311        v.it_interval.tv_sec = 1;
 312        v.it_interval.tv_usec = 0;
 313        v.it_value = v.it_interval;
 314        setitimer(ITIMER_REAL, &v, NULL);
 315}
 316
 317static struct checkout state;
 318static void check_updates(struct cache_entry **src, int nr,
 319                struct unpack_trees_options *o)
 320{
 321        unsigned short mask = htons(CE_UPDATE);
 322        unsigned last_percent = 200, cnt = 0, total = 0;
 323
 324        if (o->update && o->verbose_update) {
 325                for (total = cnt = 0; cnt < nr; cnt++) {
 326                        struct cache_entry *ce = src[cnt];
 327                        if (!ce->ce_mode || ce->ce_flags & mask)
 328                                total++;
 329                }
 330
 331                /* Don't bother doing this for very small updates */
 332                if (total < 250)
 333                        total = 0;
 334
 335                if (total) {
 336                        fprintf(stderr, "Checking files out...\n");
 337                        setup_progress_signal();
 338                        progress_update = 1;
 339                }
 340                cnt = 0;
 341        }
 342
 343        while (nr--) {
 344                struct cache_entry *ce = *src++;
 345
 346                if (total) {
 347                        if (!ce->ce_mode || ce->ce_flags & mask) {
 348                                unsigned percent;
 349                                cnt++;
 350                                percent = (cnt * 100) / total;
 351                                if (percent != last_percent ||
 352                                    progress_update) {
 353                                        fprintf(stderr, "%4u%% (%u/%u) done\r",
 354                                                percent, cnt, total);
 355                                        last_percent = percent;
 356                                        progress_update = 0;
 357                                }
 358                        }
 359                }
 360                if (!ce->ce_mode) {
 361                        if (o->update)
 362                                unlink_entry(ce->name);
 363                        continue;
 364                }
 365                if (ce->ce_flags & mask) {
 366                        ce->ce_flags &= ~mask;
 367                        if (o->update)
 368                                checkout_entry(ce, &state, NULL);
 369                }
 370        }
 371        if (total) {
 372                signal(SIGALRM, SIG_IGN);
 373                fputc('\n', stderr);
 374        }
 375}
 376
 377int unpack_trees(struct object_list *trees, struct unpack_trees_options *o)
 378{
 379        int indpos = 0;
 380        unsigned len = object_list_length(trees);
 381        struct tree_entry_list **posns;
 382        int i;
 383        struct object_list *posn = trees;
 384        struct tree_entry_list df_conflict_list;
 385        static struct cache_entry *dfc;
 386
 387        memset(&df_conflict_list, 0, sizeof(df_conflict_list));
 388        df_conflict_list.next = &df_conflict_list;
 389        memset(&state, 0, sizeof(state));
 390        state.base_dir = "";
 391        state.force = 1;
 392        state.quiet = 1;
 393        state.refresh_cache = 1;
 394
 395        o->merge_size = len;
 396
 397        if (!dfc)
 398                dfc = xcalloc(1, sizeof(struct cache_entry) + 1);
 399        o->df_conflict_entry = dfc;
 400
 401        if (len) {
 402                posns = xmalloc(len * sizeof(struct tree_entry_list *));
 403                for (i = 0; i < len; i++) {
 404                        posns[i] = create_tree_entry_list((struct tree *) posn->item);
 405                        posn = posn->next;
 406                }
 407                if (unpack_trees_rec(posns, len, o->prefix ? o->prefix : "",
 408                                     o, &indpos, &df_conflict_list))
 409                        return -1;
 410        }
 411
 412        if (o->trivial_merges_only && o->nontrivial_merge)
 413                die("Merge requires file-level merging");
 414
 415        check_updates(active_cache, active_nr, o);
 416        return 0;
 417}
 418
 419/* Here come the merge functions */
 420
 421static void reject_merge(struct cache_entry *ce)
 422{
 423        die("Entry '%s' would be overwritten by merge. Cannot merge.",
 424            ce->name);
 425}
 426
 427static int same(struct cache_entry *a, struct cache_entry *b)
 428{
 429        if (!!a != !!b)
 430                return 0;
 431        if (!a && !b)
 432                return 1;
 433        return a->ce_mode == b->ce_mode &&
 434               !hashcmp(a->sha1, b->sha1);
 435}
 436
 437
 438/*
 439 * When a CE gets turned into an unmerged entry, we
 440 * want it to be up-to-date
 441 */
 442static void verify_uptodate(struct cache_entry *ce,
 443                struct unpack_trees_options *o)
 444{
 445        struct stat st;
 446
 447        if (o->index_only || o->reset)
 448                return;
 449
 450        if (!lstat(ce->name, &st)) {
 451                unsigned changed = ce_match_stat(ce, &st, 1);
 452                if (!changed)
 453                        return;
 454                errno = 0;
 455        }
 456        if (o->reset) {
 457                ce->ce_flags |= htons(CE_UPDATE);
 458                return;
 459        }
 460        if (errno == ENOENT)
 461                return;
 462        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
 463}
 464
 465static void invalidate_ce_path(struct cache_entry *ce)
 466{
 467        if (ce)
 468                cache_tree_invalidate_path(active_cache_tree, ce->name);
 469}
 470
 471/*
 472 * We do not want to remove or overwrite a working tree file that
 473 * is not tracked, unless it is ignored.
 474 */
 475static void verify_absent(const char *path, const char *action,
 476                struct unpack_trees_options *o)
 477{
 478        struct stat st;
 479
 480        if (o->index_only || o->reset || !o->update)
 481                return;
 482        if (!lstat(path, &st) && !(o->dir && excluded(o->dir, path)))
 483                die("Untracked working tree file '%s' "
 484                    "would be %s by merge.", path, action);
 485}
 486
 487static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
 488                struct unpack_trees_options *o)
 489{
 490        merge->ce_flags |= htons(CE_UPDATE);
 491        if (old) {
 492                /*
 493                 * See if we can re-use the old CE directly?
 494                 * That way we get the uptodate stat info.
 495                 *
 496                 * This also removes the UPDATE flag on
 497                 * a match.
 498                 */
 499                if (same(old, merge)) {
 500                        *merge = *old;
 501                } else {
 502                        verify_uptodate(old, o);
 503                        invalidate_ce_path(old);
 504                }
 505        }
 506        else {
 507                verify_absent(merge->name, "overwritten", o);
 508                invalidate_ce_path(merge);
 509        }
 510
 511        merge->ce_flags &= ~htons(CE_STAGEMASK);
 512        add_cache_entry(merge, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 513        return 1;
 514}
 515
 516static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
 517                struct unpack_trees_options *o)
 518{
 519        if (old)
 520                verify_uptodate(old, o);
 521        else
 522                verify_absent(ce->name, "removed", o);
 523        ce->ce_mode = 0;
 524        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 525        invalidate_ce_path(ce);
 526        return 1;
 527}
 528
 529static int keep_entry(struct cache_entry *ce)
 530{
 531        add_cache_entry(ce, ADD_CACHE_OK_TO_ADD);
 532        return 1;
 533}
 534
 535#if DBRT_DEBUG
 536static void show_stage_entry(FILE *o,
 537                             const char *label, const struct cache_entry *ce)
 538{
 539        if (!ce)
 540                fprintf(o, "%s (missing)\n", label);
 541        else
 542                fprintf(o, "%s%06o %s %d\t%s\n",
 543                        label,
 544                        ntohl(ce->ce_mode),
 545                        sha1_to_hex(ce->sha1),
 546                        ce_stage(ce),
 547                        ce->name);
 548}
 549#endif
 550
 551int threeway_merge(struct cache_entry **stages,
 552                struct unpack_trees_options *o)
 553{
 554        struct cache_entry *index;
 555        struct cache_entry *head;
 556        struct cache_entry *remote = stages[o->head_idx + 1];
 557        int count;
 558        int head_match = 0;
 559        int remote_match = 0;
 560        const char *path = NULL;
 561
 562        int df_conflict_head = 0;
 563        int df_conflict_remote = 0;
 564
 565        int any_anc_missing = 0;
 566        int no_anc_exists = 1;
 567        int i;
 568
 569        for (i = 1; i < o->head_idx; i++) {
 570                if (!stages[i])
 571                        any_anc_missing = 1;
 572                else {
 573                        if (!path)
 574                                path = stages[i]->name;
 575                        no_anc_exists = 0;
 576                }
 577        }
 578
 579        index = stages[0];
 580        head = stages[o->head_idx];
 581
 582        if (head == o->df_conflict_entry) {
 583                df_conflict_head = 1;
 584                head = NULL;
 585        }
 586
 587        if (remote == o->df_conflict_entry) {
 588                df_conflict_remote = 1;
 589                remote = NULL;
 590        }
 591
 592        if (!path && index)
 593                path = index->name;
 594        if (!path && head)
 595                path = head->name;
 596        if (!path && remote)
 597                path = remote->name;
 598
 599        /* First, if there's a #16 situation, note that to prevent #13
 600         * and #14.
 601         */
 602        if (!same(remote, head)) {
 603                for (i = 1; i < o->head_idx; i++) {
 604                        if (same(stages[i], head)) {
 605                                head_match = i;
 606                        }
 607                        if (same(stages[i], remote)) {
 608                                remote_match = i;
 609                        }
 610                }
 611        }
 612
 613        /* We start with cases where the index is allowed to match
 614         * something other than the head: #14(ALT) and #2ALT, where it
 615         * is permitted to match the result instead.
 616         */
 617        /* #14, #14ALT, #2ALT */
 618        if (remote && !df_conflict_head && head_match && !remote_match) {
 619                if (index && !same(index, remote) && !same(index, head))
 620                        reject_merge(index);
 621                return merged_entry(remote, index, o);
 622        }
 623        /*
 624         * If we have an entry in the index cache, then we want to
 625         * make sure that it matches head.
 626         */
 627        if (index && !same(index, head)) {
 628                reject_merge(index);
 629        }
 630
 631        if (head) {
 632                /* #5ALT, #15 */
 633                if (same(head, remote))
 634                        return merged_entry(head, index, o);
 635                /* #13, #3ALT */
 636                if (!df_conflict_remote && remote_match && !head_match)
 637                        return merged_entry(head, index, o);
 638        }
 639
 640        /* #1 */
 641        if (!head && !remote && any_anc_missing)
 642                return 0;
 643
 644        /* Under the new "aggressive" rule, we resolve mostly trivial
 645         * cases that we historically had git-merge-one-file resolve.
 646         */
 647        if (o->aggressive) {
 648                int head_deleted = !head && !df_conflict_head;
 649                int remote_deleted = !remote && !df_conflict_remote;
 650                /*
 651                 * Deleted in both.
 652                 * Deleted in one and unchanged in the other.
 653                 */
 654                if ((head_deleted && remote_deleted) ||
 655                    (head_deleted && remote && remote_match) ||
 656                    (remote_deleted && head && head_match)) {
 657                        if (index)
 658                                return deleted_entry(index, index, o);
 659                        else if (path && !head_deleted)
 660                                verify_absent(path, "removed", o);
 661                        return 0;
 662                }
 663                /*
 664                 * Added in both, identically.
 665                 */
 666                if (no_anc_exists && head && remote && same(head, remote))
 667                        return merged_entry(head, index, o);
 668
 669        }
 670
 671        /* Below are "no merge" cases, which require that the index be
 672         * up-to-date to avoid the files getting overwritten with
 673         * conflict resolution files.
 674         */
 675        if (index) {
 676                verify_uptodate(index, o);
 677        }
 678
 679        o->nontrivial_merge = 1;
 680
 681        /* #2, #3, #4, #6, #7, #9, #11. */
 682        count = 0;
 683        if (!head_match || !remote_match) {
 684                for (i = 1; i < o->head_idx; i++) {
 685                        if (stages[i]) {
 686                                keep_entry(stages[i]);
 687                                count++;
 688                                break;
 689                        }
 690                }
 691        }
 692#if DBRT_DEBUG
 693        else {
 694                fprintf(stderr, "read-tree: warning #16 detected\n");
 695                show_stage_entry(stderr, "head   ", stages[head_match]);
 696                show_stage_entry(stderr, "remote ", stages[remote_match]);
 697        }
 698#endif
 699        if (head) { count += keep_entry(head); }
 700        if (remote) { count += keep_entry(remote); }
 701        return count;
 702}
 703
 704/*
 705 * Two-way merge.
 706 *
 707 * The rule is to "carry forward" what is in the index without losing
 708 * information across a "fast forward", favoring a successful merge
 709 * over a merge failure when it makes sense.  For details of the
 710 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 711 *
 712 */
 713int twoway_merge(struct cache_entry **src,
 714                struct unpack_trees_options *o)
 715{
 716        struct cache_entry *current = src[0];
 717        struct cache_entry *oldtree = src[1], *newtree = src[2];
 718
 719        if (o->merge_size != 2)
 720                return error("Cannot do a twoway merge of %d trees",
 721                             o->merge_size);
 722
 723        if (current) {
 724                if ((!oldtree && !newtree) || /* 4 and 5 */
 725                    (!oldtree && newtree &&
 726                     same(current, newtree)) || /* 6 and 7 */
 727                    (oldtree && newtree &&
 728                     same(oldtree, newtree)) || /* 14 and 15 */
 729                    (oldtree && newtree &&
 730                     !same(oldtree, newtree) && /* 18 and 19*/
 731                     same(current, newtree))) {
 732                        return keep_entry(current);
 733                }
 734                else if (oldtree && !newtree && same(current, oldtree)) {
 735                        /* 10 or 11 */
 736                        return deleted_entry(oldtree, current, o);
 737                }
 738                else if (oldtree && newtree &&
 739                         same(current, oldtree) && !same(current, newtree)) {
 740                        /* 20 or 21 */
 741                        return merged_entry(newtree, current, o);
 742                }
 743                else {
 744                        /* all other failures */
 745                        if (oldtree)
 746                                reject_merge(oldtree);
 747                        if (current)
 748                                reject_merge(current);
 749                        if (newtree)
 750                                reject_merge(newtree);
 751                        return -1;
 752                }
 753        }
 754        else if (newtree)
 755                return merged_entry(newtree, current, o);
 756        else
 757                return deleted_entry(oldtree, current, o);
 758}
 759
 760/*
 761 * Bind merge.
 762 *
 763 * Keep the index entries at stage0, collapse stage1 but make sure
 764 * stage0 does not have anything there.
 765 */
 766int bind_merge(struct cache_entry **src,
 767                struct unpack_trees_options *o)
 768{
 769        struct cache_entry *old = src[0];
 770        struct cache_entry *a = src[1];
 771
 772        if (o->merge_size != 1)
 773                return error("Cannot do a bind merge of %d trees\n",
 774                             o->merge_size);
 775        if (a && old)
 776                die("Entry '%s' overlaps.  Cannot bind.", a->name);
 777        if (!a)
 778                return keep_entry(old);
 779        else
 780                return merged_entry(a, NULL, o);
 781}
 782
 783/*
 784 * One-way merge.
 785 *
 786 * The rule is:
 787 * - take the stat information from stage0, take the data from stage1
 788 */
 789int oneway_merge(struct cache_entry **src,
 790                struct unpack_trees_options *o)
 791{
 792        struct cache_entry *old = src[0];
 793        struct cache_entry *a = src[1];
 794
 795        if (o->merge_size != 1)
 796                return error("Cannot do a oneway merge of %d trees",
 797                             o->merge_size);
 798
 799        if (!a)
 800                return deleted_entry(old, old, o);
 801        if (old && same(old, a)) {
 802                if (o->reset) {
 803                        struct stat st;
 804                        if (lstat(old->name, &st) ||
 805                            ce_match_stat(old, &st, 1))
 806                                old->ce_flags |= htons(CE_UPDATE);
 807                }
 808                return keep_entry(old);
 809        }
 810        return merged_entry(a, old, o);
 811}