f2a8bb53a1c4338b0bccb6a44df5cdcdb8d74aed
   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(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
 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 {
 119                        verify_uptodate(old);
 120                }
 121        }
 122        merge->ce_flags &= ~htons(CE_STAGEMASK);
 123        *dst++ = merge;
 124        return 1;
 125}
 126
 127static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, struct cache_entry **dst)
 128{
 129        if (old)
 130                verify_uptodate(old);
 131        ce->ce_mode = 0;
 132        *dst++ = ce;
 133        return 1;
 134}
 135
 136static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
 137{
 138        struct cache_entry *old = stages[0];
 139        struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3];
 140        struct cache_entry *merge;
 141        int count;
 142
 143        /*
 144         * If we have an entry in the index cache ("old"), then we want
 145         * to make sure that it matches any entries in stage 2 ("first
 146         * branch", aka "b").
 147         */
 148        if (old) {
 149                if (!b || !same(old, b))
 150                        return -1;
 151        }
 152        merge = merge_entries(a, b, c);
 153        if (merge)
 154                return merged_entry(merge, old, dst);
 155        if (old)
 156                verify_uptodate(old);
 157        count = 0;
 158        if (a) { *dst++ = a; count++; }
 159        if (b) { *dst++ = b; count++; }
 160        if (c) { *dst++ = c; count++; }
 161        return count;
 162}
 163
 164/*
 165 * Two-way merge.
 166 *
 167 * The rule is to "carry forward" what is in the index without losing
 168 * information across a "fast forward", favoring a successful merge
 169 * over a merge failure when it makes sense.  For details of the
 170 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
 171 *
 172 */
 173static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
 174{
 175        struct cache_entry *current = src[0];
 176        struct cache_entry *oldtree = src[1], *newtree = src[2];
 177
 178        if (src[3])
 179                return -1;
 180
 181        if (current) {
 182                if ((!oldtree && !newtree) || /* 4 and 5 */
 183                    (!oldtree && newtree &&
 184                     same(current, newtree)) || /* 6 and 7 */
 185                    (oldtree && newtree &&
 186                     same(oldtree, newtree)) || /* 14 and 15 */
 187                    (oldtree && newtree &&
 188                     !same(oldtree, newtree) && /* 18 and 19*/
 189                     same(current, newtree))) {
 190                        *dst++ = current;
 191                        return 1;
 192                }
 193                else if (oldtree && !newtree && same(current, oldtree)) {
 194                        /* 10 or 11 */
 195                        return deleted_entry(oldtree, current, dst);
 196                }
 197                else if (oldtree && newtree &&
 198                         same(current, oldtree) && !same(current, newtree)) {
 199                        /* 20 or 21 */
 200                        return merged_entry(newtree, current, dst);
 201                }
 202                else
 203                        /* all other failures */
 204                        return -1;
 205        }
 206        else if (newtree)
 207                return merged_entry(newtree, current, dst);
 208        else
 209                return deleted_entry(oldtree, current, dst);
 210}
 211
 212/*
 213 * Two-way merge emulated with three-way merge.
 214 *
 215 * This treats "read-tree -m H M" by transforming it internally
 216 * into "read-tree -m H I+H M", where I+H is a tree that would
 217 * contain the contents of the current index file, overlayed on
 218 * top of H.  Unlike the traditional two-way merge, this leaves
 219 * the stages in the resulting index file and lets the user resolve
 220 * the merge conflicts using standard tools for three-way merge.
 221 *
 222 * This function is just to set-up such an arrangement, and the
 223 * actual merge uses threeway_merge() function.
 224 */
 225static void setup_emu23(void)
 226{
 227        /* stage0 contains I, stage1 H, stage2 M.
 228         * move stage2 to stage3, and create stage2 entries
 229         * by scanning stage0 and stage1 entries.
 230         */
 231        int i, namelen, size;
 232        struct cache_entry *ce, *stage2;
 233
 234        for (i = 0; i < active_nr; i++) {
 235                ce = active_cache[i];
 236                if (ce_stage(ce) != 2)
 237                        continue;
 238                /* hoist them up to stage 3 */
 239                namelen = ce_namelen(ce);
 240                ce->ce_flags = create_ce_flags(namelen, 3);
 241        }
 242
 243        for (i = 0; i < active_nr; i++) {
 244                ce = active_cache[i];
 245                if (ce_stage(ce) > 1)
 246                        continue;
 247                namelen = ce_namelen(ce);
 248                size = cache_entry_size(namelen);
 249                stage2 = xmalloc(size);
 250                memcpy(stage2, ce, size);
 251                stage2->ce_flags = create_ce_flags(namelen, 2);
 252                if (add_cache_entry(stage2, ADD_CACHE_OK_TO_ADD) < 0)
 253                        die("cannot merge index and our head tree");
 254
 255                /* We are done with this name, so skip to next name */
 256                while (i < active_nr &&
 257                       ce_namelen(active_cache[i]) == namelen &&
 258                       !memcmp(active_cache[i]->name, ce->name, namelen))
 259                        i++;
 260                i--; /* compensate for the loop control */
 261        }
 262}
 263
 264/*
 265 * One-way merge.
 266 *
 267 * The rule is:
 268 * - take the stat information from stage0, take the data from stage1
 269 */
 270static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
 271{
 272        struct cache_entry *old = src[0];
 273        struct cache_entry *a = src[1];
 274
 275        if (src[2] || src[3])
 276                return -1;
 277
 278        if (!a)
 279                return 0;
 280        if (old && same(old, a)) {
 281                *dst++ = old;
 282                return 1;
 283        }
 284        return merged_entry(a, NULL, dst);
 285}
 286
 287static void check_updates(struct cache_entry **src, int nr)
 288{
 289        static struct checkout state = {
 290                .base_dir = "",
 291                .force = 1,
 292                .quiet = 1,
 293                .refresh_cache = 1,
 294        };
 295        unsigned short mask = htons(CE_UPDATE);
 296        while (nr--) {
 297                struct cache_entry *ce = *src++;
 298                if (!ce->ce_mode) {
 299                        if (update)
 300                                unlink(ce->name);
 301                        continue;
 302                }
 303                if (ce->ce_flags & mask) {
 304                        ce->ce_flags &= ~mask;
 305                        if (update)
 306                                checkout_entry(ce, &state);
 307                }
 308        }
 309}
 310
 311typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **);
 312
 313static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
 314{
 315        struct cache_entry **dst = src;
 316
 317        while (nr) {
 318                int entries;
 319                struct cache_entry *name, *ce, *stages[4] = { NULL, };
 320
 321                name = ce = *src;
 322                for (;;) {
 323                        int stage = ce_stage(ce);
 324                        stages[stage] = ce;
 325                        ce = *++src;
 326                        active_nr--;
 327                        if (!--nr)
 328                                break;
 329                        if (!path_matches(ce, name))
 330                                break;
 331                }
 332
 333                entries = fn(stages, dst);
 334                if (entries < 0)
 335                        reject_merge(name);
 336                dst += entries;
 337                active_nr += entries;
 338        }
 339        check_updates(active_cache, active_nr);
 340}
 341
 342static int read_cache_unmerged(void)
 343{
 344        int i, deleted;
 345        struct cache_entry **dst;
 346
 347        read_cache();
 348        dst = active_cache;
 349        deleted = 0;
 350        for (i = 0; i < active_nr; i++) {
 351                struct cache_entry *ce = active_cache[i];
 352                if (ce_stage(ce)) {
 353                        deleted++;
 354                        continue;
 355                }
 356                if (deleted)
 357                        *dst = ce;
 358                dst++;
 359        }
 360        active_nr -= deleted;
 361        return deleted;
 362}
 363
 364static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
 365
 366static struct cache_file cache_file;
 367
 368int main(int argc, char **argv)
 369{
 370        int i, newfd, merge, reset, emu23;
 371        unsigned char sha1[20];
 372
 373        newfd = hold_index_file_for_update(&cache_file, get_index_file());
 374        if (newfd < 0)
 375                die("unable to create new cachefile");
 376
 377        merge = 0;
 378        reset = 0;
 379        emu23 = 0;
 380        for (i = 1; i < argc; i++) {
 381                const char *arg = argv[i];
 382
 383                /* "-u" means "update", meaning that a merge will update the working directory */
 384                if (!strcmp(arg, "-u")) {
 385                        update = 1;
 386                        continue;
 387                }
 388
 389                /* This differs from "-m" in that we'll silently ignore unmerged entries */
 390                if (!strcmp(arg, "--reset")) {
 391                        if (stage || merge || emu23)
 392                                usage(read_tree_usage);
 393                        reset = 1;
 394                        merge = 1;
 395                        stage = 1;
 396                        read_cache_unmerged();
 397                }
 398
 399                /* "-m" stands for "merge", meaning we start in stage 1 */
 400                if (!strcmp(arg, "-m")) {
 401                        if (stage || merge || emu23)
 402                                usage(read_tree_usage);
 403                        if (read_cache_unmerged())
 404                                die("you need to resolve your current index first");
 405                        stage = 1;
 406                        merge = 1;
 407                        continue;
 408                }
 409
 410                /* "-emu23" uses 3-way merge logic to perform fast-forward */
 411                if (!strcmp(arg, "--emu23")) {
 412                        if (stage || merge || emu23)
 413                                usage(read_tree_usage);
 414                        if (read_cache_unmerged())
 415                                die("you need to resolve your current index first");
 416                        merge = emu23 = stage = 1;
 417                        continue;
 418                }
 419
 420                if (get_sha1(arg, sha1) < 0)
 421                        usage(read_tree_usage);
 422                if (stage > 3)
 423                        usage(read_tree_usage);
 424                if (unpack_tree(sha1) < 0)
 425                        die("failed to unpack tree object %s", arg);
 426                stage++;
 427        }
 428        if (update && !merge)
 429                usage(read_tree_usage);
 430        if (merge) {
 431                static const merge_fn_t merge_function[] = {
 432                        [1] = oneway_merge,
 433                        [2] = twoway_merge,
 434                        [3] = threeway_merge,
 435                };
 436                merge_fn_t fn;
 437
 438                if (stage < 2 || stage > 4)
 439                        die("just how do you expect me to merge %d trees?", stage-1);
 440                if (emu23 && stage != 3)
 441                        die("--emu23 takes only two trees");
 442                fn = merge_function[stage-1];
 443                if (stage == 3 && emu23) { 
 444                        setup_emu23();
 445                        fn = merge_function[3];
 446                }
 447                merge_cache(active_cache, active_nr, fn);
 448        }
 449        if (write_cache(newfd, active_cache, active_nr) ||
 450            commit_index_file(&cache_file))
 451                die("unable to write new index file");
 452        return 0;
 453}