ecd40cc393fad70a15ca4efbdab4a2049ea8aefe
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8static int stage = 0;
   9static int update = 0;
  10
  11static int unpack_tree(unsigned char *sha1)
  12{
  13        void *buffer;
  14        unsigned long size;
  15        int ret;
  16
  17        buffer = read_object_with_reference(sha1, "tree", &size, NULL);
  18        if (!buffer)
  19                return -1;
  20        ret = read_tree(buffer, size, stage);
  21        free(buffer);
  22        return ret;
  23}
  24
  25static int path_matches(struct cache_entry *a, struct cache_entry *b)
  26{
  27        int len = ce_namelen(a);
  28        return ce_namelen(b) == len &&
  29                !memcmp(a->name, b->name, len);
  30}
  31
  32static int same(struct cache_entry *a, struct cache_entry *b)
  33{
  34        return a->ce_mode == b->ce_mode && 
  35                !memcmp(a->sha1, b->sha1, 20);
  36}
  37
  38
  39/*
  40 * This removes all trivial merges that don't change the tree
  41 * and collapses them to state 0.
  42 */
  43static struct cache_entry *merge_entries(struct cache_entry *a,
  44                                         struct cache_entry *b,
  45                                         struct cache_entry *c)
  46{
  47        /*
  48         * Ok, all three entries describe the same
  49         * filename, but maybe the contents or file
  50         * mode have changed?
  51         *
  52         * The trivial cases end up being the ones where two
  53         * out of three files are the same:
  54         *  - both destinations the same, trivially take either
  55         *  - one of the destination versions hasn't changed,
  56         *    take the other.
  57         *
  58         * The "all entries exactly the same" case falls out as
  59         * a special case of any of the "two same" cases.
  60         *
  61         * Here "a" is "original", and "b" and "c" are the two
  62         * trees we are merging.
  63         */
  64        if (a && b && c) {
  65                if (same(b,c))
  66                        return c;
  67                if (same(a,b))
  68                        return c;
  69                if (same(a,c))
  70                        return b;
  71        }
  72        return NULL;
  73}
  74
  75/*
  76 * When a CE gets turned into an unmerged entry, we
  77 * want it to be up-to-date
  78 */
  79static void verify_uptodate(struct cache_entry *ce)
  80{
  81        struct stat st;
  82
  83        if (!lstat(ce->name, &st)) {
  84                unsigned changed = ce_match_stat(ce, &st);
  85                if (!changed)
  86                        return;
  87                errno = 0;
  88        }
  89        if (errno == ENOENT)
  90                return;
  91        die("Entry '%s' not uptodate. Cannot merge.", ce->name);
  92}
  93
  94/*
  95 * If the old tree contained a CE that isn't even in the
  96 * result, that's always a problem, regardless of whether
  97 * it's up-to-date or not (ie it can be a file that we
  98 * have updated but not committed yet).
  99 */
 100static void reject_merge(struct cache_entry *ce)
 101{
 102        die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
 103}
 104
 105static int merged_entry_internal(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst, int allow_dirty)
 106{
 107        merge->ce_flags |= htons(CE_UPDATE);
 108        if (old) {
 109                /*
 110                 * See if we can re-use the old CE directly?
 111                 * That way we get the uptodate stat info.
 112                 *
 113                 * This also removes the UPDATE flag on
 114                 * a match.
 115                 */
 116                if (same(old, merge)) {
 117                        *merge = *old;
 118                } else if (!allow_dirty) {
 119                        verify_uptodate(old);
 120                }
 121        }
 122        merge->ce_flags &= ~htons(CE_STAGEMASK);
 123        *dst++ = merge;
 124        return 1;
 125}
 126
 127static int merged_entry_allow_dirty(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
 128{
 129        return merged_entry_internal(merge, old, dst, 1);
 130}
 131
 132static int merged_entry(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
 133{
 134        return merged_entry_internal(merge, old, dst, 0);
 135}
 136
 137static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, struct cache_entry **dst)
 138{
 139        if (old)
 140                verify_uptodate(old);
 141        ce->ce_mode = 0;
 142        *dst++ = ce;
 143        return 1;
 144}
 145
 146static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
 147{
 148        struct cache_entry *old = stages[0];
 149        struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3];
 150        struct cache_entry *merge;
 151        int count;
 152
 153        /* #5ALT */
 154        if (!a && b && c && same(b, c)) {
 155                if (old && !same(b, old))
 156                        return -1;
 157                return merged_entry_allow_dirty(b, old, dst);
 158        }
 159        /*
 160         * If we have an entry in the index cache ("old"), then we want
 161         * to make sure that it matches any entries in stage 2 ("first
 162         * branch", aka "b").
 163         */
 164        if (old) {
 165                if (!b || !same(old, b))
 166                        return -1;
 167        }
 168        merge = merge_entries(a, b, c);
 169        if (merge)
 170                return merged_entry(merge, old, dst);
 171        if (old)
 172                verify_uptodate(old);
 173        count = 0;
 174        if (a) { *dst++ = a; count++; }
 175        if (b) { *dst++ = b; count++; }
 176        if (c) { *dst++ = c; count++; }
 177        return count;
 178}
 179
 180/*
 181 * Two-way merge.
 182 *
 183 * The rule is to "carry forward" what is in the index without losing
 184 * information across a "fast forward", favoring a successful merge
 185 * over a merge failure when it makes sense.  For details of the
 186 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 187 *
 188 */
 189static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
 190{
 191        struct cache_entry *current = src[0];
 192        struct cache_entry *oldtree = src[1], *newtree = src[2];
 193
 194        if (src[3])
 195                return -1;
 196
 197        if (current) {
 198                if ((!oldtree && !newtree) || /* 4 and 5 */
 199                    (!oldtree && newtree &&
 200                     same(current, newtree)) || /* 6 and 7 */
 201                    (oldtree && newtree &&
 202                     same(oldtree, newtree)) || /* 14 and 15 */
 203                    (oldtree && newtree &&
 204                     !same(oldtree, newtree) && /* 18 and 19*/
 205                     same(current, newtree))) {
 206                        *dst++ = current;
 207                        return 1;
 208                }
 209                else if (oldtree && !newtree && same(current, oldtree)) {
 210                        /* 10 or 11 */
 211                        return deleted_entry(oldtree, current, dst);
 212                }
 213                else if (oldtree && newtree &&
 214                         same(current, oldtree) && !same(current, newtree)) {
 215                        /* 20 or 21 */
 216                        return merged_entry(newtree, current, dst);
 217                }
 218                else
 219                        /* all other failures */
 220                        return -1;
 221        }
 222        else if (newtree)
 223                return merged_entry(newtree, current, dst);
 224        else
 225                return deleted_entry(oldtree, current, dst);
 226}
 227
 228/*
 229 * Two-way merge emulated with three-way merge.
 230 *
 231 * This treats "read-tree -m H M" by transforming it internally
 232 * into "read-tree -m H I+H M", where I+H is a tree that would
 233 * contain the contents of the current index file, overlayed on
 234 * top of H.  Unlike the traditional two-way merge, this leaves
 235 * the stages in the resulting index file and lets the user resolve
 236 * the merge conflicts using standard tools for three-way merge.
 237 *
 238 * This function is just to set-up such an arrangement, and the
 239 * actual merge uses threeway_merge() function.
 240 */
 241static void setup_emu23(void)
 242{
 243        /* stage0 contains I, stage1 H, stage2 M.
 244         * move stage2 to stage3, and create stage2 entries
 245         * by scanning stage0 and stage1 entries.
 246         */
 247        int i, namelen, size;
 248        struct cache_entry *ce, *stage2;
 249
 250        for (i = 0; i < active_nr; i++) {
 251                ce = active_cache[i];
 252                if (ce_stage(ce) != 2)
 253                        continue;
 254                /* hoist them up to stage 3 */
 255                namelen = ce_namelen(ce);
 256                ce->ce_flags = create_ce_flags(namelen, 3);
 257        }
 258
 259        for (i = 0; i < active_nr; i++) {
 260                ce = active_cache[i];
 261                if (ce_stage(ce) > 1)
 262                        continue;
 263                namelen = ce_namelen(ce);
 264                size = cache_entry_size(namelen);
 265                stage2 = xmalloc(size);
 266                memcpy(stage2, ce, size);
 267                stage2->ce_flags = create_ce_flags(namelen, 2);
 268                if (add_cache_entry(stage2, ADD_CACHE_OK_TO_ADD) < 0)
 269                        die("cannot merge index and our head tree");
 270
 271                /* We are done with this name, so skip to next name */
 272                while (i < active_nr &&
 273                       ce_namelen(active_cache[i]) == namelen &&
 274                       !memcmp(active_cache[i]->name, ce->name, namelen))
 275                        i++;
 276                i--; /* compensate for the loop control */
 277        }
 278}
 279
 280/*
 281 * One-way merge.
 282 *
 283 * The rule is:
 284 * - take the stat information from stage0, take the data from stage1
 285 */
 286static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
 287{
 288        struct cache_entry *old = src[0];
 289        struct cache_entry *a = src[1];
 290
 291        if (src[2] || src[3])
 292                return -1;
 293
 294        if (!a)
 295                return 0;
 296        if (old && same(old, a)) {
 297                *dst++ = old;
 298                return 1;
 299        }
 300        return merged_entry(a, NULL, dst);
 301}
 302
 303static void check_updates(struct cache_entry **src, int nr)
 304{
 305        static struct checkout state = {
 306                .base_dir = "",
 307                .force = 1,
 308                .quiet = 1,
 309                .refresh_cache = 1,
 310        };
 311        unsigned short mask = htons(CE_UPDATE);
 312        while (nr--) {
 313                struct cache_entry *ce = *src++;
 314                if (!ce->ce_mode) {
 315                        if (update)
 316                                unlink(ce->name);
 317                        continue;
 318                }
 319                if (ce->ce_flags & mask) {
 320                        ce->ce_flags &= ~mask;
 321                        if (update)
 322                                checkout_entry(ce, &state);
 323                }
 324        }
 325}
 326
 327typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **);
 328
 329static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
 330{
 331        struct cache_entry **dst = src;
 332
 333        while (nr) {
 334                int entries;
 335                struct cache_entry *name, *ce, *stages[4] = { NULL, };
 336
 337                name = ce = *src;
 338                for (;;) {
 339                        int stage = ce_stage(ce);
 340                        stages[stage] = ce;
 341                        ce = *++src;
 342                        active_nr--;
 343                        if (!--nr)
 344                                break;
 345                        if (!path_matches(ce, name))
 346                                break;
 347                }
 348
 349                entries = fn(stages, dst);
 350                if (entries < 0)
 351                        reject_merge(name);
 352                dst += entries;
 353                active_nr += entries;
 354        }
 355        check_updates(active_cache, active_nr);
 356}
 357
 358static int read_cache_unmerged(void)
 359{
 360        int i, deleted;
 361        struct cache_entry **dst;
 362
 363        read_cache();
 364        dst = active_cache;
 365        deleted = 0;
 366        for (i = 0; i < active_nr; i++) {
 367                struct cache_entry *ce = active_cache[i];
 368                if (ce_stage(ce)) {
 369                        deleted++;
 370                        continue;
 371                }
 372                if (deleted)
 373                        *dst = ce;
 374                dst++;
 375        }
 376        active_nr -= deleted;
 377        return deleted;
 378}
 379
 380static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
 381
 382static struct cache_file cache_file;
 383
 384int main(int argc, char **argv)
 385{
 386        int i, newfd, merge, reset, emu23;
 387        unsigned char sha1[20];
 388
 389        newfd = hold_index_file_for_update(&cache_file, get_index_file());
 390        if (newfd < 0)
 391                die("unable to create new cachefile");
 392
 393        merge = 0;
 394        reset = 0;
 395        emu23 = 0;
 396        for (i = 1; i < argc; i++) {
 397                const char *arg = argv[i];
 398
 399                /* "-u" means "update", meaning that a merge will update the working directory */
 400                if (!strcmp(arg, "-u")) {
 401                        update = 1;
 402                        continue;
 403                }
 404
 405                /* This differs from "-m" in that we'll silently ignore unmerged entries */
 406                if (!strcmp(arg, "--reset")) {
 407                        if (stage || merge || emu23)
 408                                usage(read_tree_usage);
 409                        reset = 1;
 410                        merge = 1;
 411                        stage = 1;
 412                        read_cache_unmerged();
 413                }
 414
 415                /* "-m" stands for "merge", meaning we start in stage 1 */
 416                if (!strcmp(arg, "-m")) {
 417                        if (stage || merge || emu23)
 418                                usage(read_tree_usage);
 419                        if (read_cache_unmerged())
 420                                die("you need to resolve your current index first");
 421                        stage = 1;
 422                        merge = 1;
 423                        continue;
 424                }
 425
 426                /* "-emu23" uses 3-way merge logic to perform fast-forward */
 427                if (!strcmp(arg, "--emu23")) {
 428                        if (stage || merge || emu23)
 429                                usage(read_tree_usage);
 430                        if (read_cache_unmerged())
 431                                die("you need to resolve your current index first");
 432                        merge = emu23 = stage = 1;
 433                        continue;
 434                }
 435
 436                if (get_sha1(arg, sha1) < 0)
 437                        usage(read_tree_usage);
 438                if (stage > 3)
 439                        usage(read_tree_usage);
 440                if (unpack_tree(sha1) < 0)
 441                        die("failed to unpack tree object %s", arg);
 442                stage++;
 443        }
 444        if (update && !merge)
 445                usage(read_tree_usage);
 446        if (merge) {
 447                static const merge_fn_t merge_function[] = {
 448                        [1] = oneway_merge,
 449                        [2] = twoway_merge,
 450                        [3] = threeway_merge,
 451                };
 452                merge_fn_t fn;
 453
 454                if (stage < 2 || stage > 4)
 455                        die("just how do you expect me to merge %d trees?", stage-1);
 456                if (emu23 && stage != 3)
 457                        die("--emu23 takes only two trees");
 458                fn = merge_function[stage-1];
 459                if (stage == 3 && emu23) { 
 460                        setup_emu23();
 461                        fn = merge_function[3];
 462                }
 463                merge_cache(active_cache, active_nr, fn);
 464        }
 465        if (write_cache(newfd, active_cache, active_nr) ||
 466            commit_index_file(&cache_file))
 467                die("unable to write new index file");
 468        return 0;
 469}