merge-recursive.con commit Speed-up recursive by flushing index only once for all entries (f518438)
   1/*
   2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
   3 * Fredrik Kuivinen.
   4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
   5 */
   6#include "cache.h"
   7#include "cache-tree.h"
   8#include "commit.h"
   9#include "blob.h"
  10#include "tree-walk.h"
  11#include "diff.h"
  12#include "diffcore.h"
  13#include "run-command.h"
  14#include "tag.h"
  15#include "unpack-trees.h"
  16#include "path-list.h"
  17#include "xdiff-interface.h"
  18
  19/*
  20 * A virtual commit has
  21 * - (const char *)commit->util set to the name, and
  22 * - *(int *)commit->object.sha1 set to the virtual id.
  23 */
  24
  25static unsigned commit_list_count(const struct commit_list *l)
  26{
  27        unsigned c = 0;
  28        for (; l; l = l->next )
  29                c++;
  30        return c;
  31}
  32
  33static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
  34{
  35        struct commit *commit = xcalloc(1, sizeof(struct commit));
  36        static unsigned virtual_id = 1;
  37        commit->tree = tree;
  38        commit->util = (void*)comment;
  39        *(int*)commit->object.sha1 = virtual_id++;
  40        /* avoid warnings */
  41        commit->object.parsed = 1;
  42        return commit;
  43}
  44
  45/*
  46 * Since we use get_tree_entry(), which does not put the read object into
  47 * the object pool, we cannot rely on a == b.
  48 */
  49static int sha_eq(const unsigned char *a, const unsigned char *b)
  50{
  51        if (!a && !b)
  52                return 2;
  53        return a && b && hashcmp(a, b) == 0;
  54}
  55
  56/*
  57 * Since we want to write the index eventually, we cannot reuse the index
  58 * for these (temporary) data.
  59 */
  60struct stage_data
  61{
  62        struct
  63        {
  64                unsigned mode;
  65                unsigned char sha[20];
  66        } stages[4];
  67        unsigned processed:1;
  68};
  69
  70static struct path_list current_file_set = {NULL, 0, 0, 1};
  71static struct path_list current_directory_set = {NULL, 0, 0, 1};
  72
  73static int output_indent = 0;
  74
  75static void output(const char *fmt, ...)
  76{
  77        va_list args;
  78        int i;
  79        for (i = output_indent; i--;)
  80                fputs("  ", stdout);
  81        va_start(args, fmt);
  82        vfprintf(stdout, fmt, args);
  83        va_end(args);
  84        fputc('\n', stdout);
  85}
  86
  87static void output_commit_title(struct commit *commit)
  88{
  89        int i;
  90        for (i = output_indent; i--;)
  91                fputs("  ", stdout);
  92        if (commit->util)
  93                printf("virtual %s\n", (char *)commit->util);
  94        else {
  95                printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
  96                if (parse_commit(commit) != 0)
  97                        printf("(bad commit)\n");
  98                else {
  99                        const char *s;
 100                        int len;
 101                        for (s = commit->buffer; *s; s++)
 102                                if (*s == '\n' && s[1] == '\n') {
 103                                        s += 2;
 104                                        break;
 105                                }
 106                        for (len = 0; s[len] && '\n' != s[len]; len++)
 107                                ; /* do nothing */
 108                        printf("%.*s\n", len, s);
 109                }
 110        }
 111}
 112
 113static const char *current_index_file = NULL;
 114static const char *original_index_file;
 115static const char *temporary_index_file;
 116static int cache_dirty = 0;
 117
 118static int flush_cache(void)
 119{
 120        /* flush temporary index */
 121        struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 122        int fd = hold_lock_file_for_update(lock, current_index_file, 1);
 123        if (write_cache(fd, active_cache, active_nr) ||
 124                        close(fd) || commit_lock_file(lock))
 125                die ("unable to write %s", current_index_file);
 126        discard_cache();
 127        cache_dirty = 0;
 128        return 0;
 129}
 130
 131static void setup_index(int temp)
 132{
 133        current_index_file = temp ? temporary_index_file: original_index_file;
 134        if (cache_dirty) {
 135                discard_cache();
 136                cache_dirty = 0;
 137        }
 138        unlink(temporary_index_file);
 139        discard_cache();
 140}
 141
 142static struct cache_entry *make_cache_entry(unsigned int mode,
 143                const unsigned char *sha1, const char *path, int stage, int refresh)
 144{
 145        int size, len;
 146        struct cache_entry *ce;
 147
 148        if (!verify_path(path))
 149                return NULL;
 150
 151        len = strlen(path);
 152        size = cache_entry_size(len);
 153        ce = xcalloc(1, size);
 154
 155        hashcpy(ce->sha1, sha1);
 156        memcpy(ce->name, path, len);
 157        ce->ce_flags = create_ce_flags(len, stage);
 158        ce->ce_mode = create_ce_mode(mode);
 159
 160        if (refresh)
 161                return refresh_cache_entry(ce, 0);
 162
 163        return ce;
 164}
 165
 166static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
 167                const char *path, int stage, int refresh, int options)
 168{
 169        struct cache_entry *ce;
 170        if (!cache_dirty)
 171                read_cache_from(current_index_file);
 172        cache_dirty++;
 173        ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
 174        if (!ce)
 175                return error("cache_addinfo failed: %s", strerror(cache_errno));
 176        return add_cache_entry(ce, options);
 177}
 178
 179/*
 180 * This is a global variable which is used in a number of places but
 181 * only written to in the 'merge' function.
 182 *
 183 * index_only == 1    => Don't leave any non-stage 0 entries in the cache and
 184 *                       don't update the working directory.
 185 *               0    => Leave unmerged entries in the cache and update
 186 *                       the working directory.
 187 */
 188static int index_only = 0;
 189
 190static int git_read_tree(struct tree *tree)
 191{
 192        int rc;
 193        struct object_list *trees = NULL;
 194        struct unpack_trees_options opts;
 195
 196        if (cache_dirty)
 197                die("read-tree with dirty cache");
 198
 199        memset(&opts, 0, sizeof(opts));
 200        object_list_append(&tree->object, &trees);
 201        rc = unpack_trees(trees, &opts);
 202        cache_tree_free(&active_cache_tree);
 203
 204        if (rc == 0)
 205                cache_dirty = 1;
 206
 207        return rc;
 208}
 209
 210static int git_merge_trees(int index_only,
 211                           struct tree *common,
 212                           struct tree *head,
 213                           struct tree *merge)
 214{
 215        int rc;
 216        struct object_list *trees = NULL;
 217        struct unpack_trees_options opts;
 218
 219        if (!cache_dirty) {
 220                read_cache_from(current_index_file);
 221                cache_dirty = 1;
 222        }
 223
 224        memset(&opts, 0, sizeof(opts));
 225        if (index_only)
 226                opts.index_only = 1;
 227        else
 228                opts.update = 1;
 229        opts.merge = 1;
 230        opts.head_idx = 2;
 231        opts.fn = threeway_merge;
 232
 233        object_list_append(&common->object, &trees);
 234        object_list_append(&head->object, &trees);
 235        object_list_append(&merge->object, &trees);
 236
 237        rc = unpack_trees(trees, &opts);
 238        cache_tree_free(&active_cache_tree);
 239
 240        cache_dirty = 1;
 241
 242        return rc;
 243}
 244
 245static struct tree *git_write_tree(void)
 246{
 247        struct tree *result = NULL;
 248
 249        if (cache_dirty) {
 250                unsigned i;
 251                for (i = 0; i < active_nr; i++) {
 252                        struct cache_entry *ce = active_cache[i];
 253                        if (ce_stage(ce))
 254                                return NULL;
 255                }
 256        } else
 257                read_cache_from(current_index_file);
 258
 259        if (!active_cache_tree)
 260                active_cache_tree = cache_tree();
 261
 262        if (!cache_tree_fully_valid(active_cache_tree) &&
 263                        cache_tree_update(active_cache_tree,
 264                                active_cache, active_nr, 0, 0) < 0)
 265                die("error building trees");
 266
 267        result = lookup_tree(active_cache_tree->sha1);
 268
 269        flush_cache();
 270        cache_dirty = 0;
 271
 272        return result;
 273}
 274
 275static int save_files_dirs(const unsigned char *sha1,
 276                const char *base, int baselen, const char *path,
 277                unsigned int mode, int stage)
 278{
 279        int len = strlen(path);
 280        char *newpath = xmalloc(baselen + len + 1);
 281        memcpy(newpath, base, baselen);
 282        memcpy(newpath + baselen, path, len);
 283        newpath[baselen + len] = '\0';
 284
 285        if (S_ISDIR(mode))
 286                path_list_insert(newpath, &current_directory_set);
 287        else
 288                path_list_insert(newpath, &current_file_set);
 289        free(newpath);
 290
 291        return READ_TREE_RECURSIVE;
 292}
 293
 294static int get_files_dirs(struct tree *tree)
 295{
 296        int n;
 297        if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
 298                return 0;
 299        n = current_file_set.nr + current_directory_set.nr;
 300        return n;
 301}
 302
 303/*
 304 * Returns a index_entry instance which doesn't have to correspond to
 305 * a real cache entry in Git's index.
 306 */
 307static struct stage_data *insert_stage_data(const char *path,
 308                struct tree *o, struct tree *a, struct tree *b,
 309                struct path_list *entries)
 310{
 311        struct path_list_item *item;
 312        struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
 313        get_tree_entry(o->object.sha1, path,
 314                        e->stages[1].sha, &e->stages[1].mode);
 315        get_tree_entry(a->object.sha1, path,
 316                        e->stages[2].sha, &e->stages[2].mode);
 317        get_tree_entry(b->object.sha1, path,
 318                        e->stages[3].sha, &e->stages[3].mode);
 319        item = path_list_insert(path, entries);
 320        item->util = e;
 321        return e;
 322}
 323
 324/*
 325 * Create a dictionary mapping file names to stage_data objects. The
 326 * dictionary contains one entry for every path with a non-zero stage entry.
 327 */
 328static struct path_list *get_unmerged(void)
 329{
 330        struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
 331        int i;
 332
 333        unmerged->strdup_paths = 1;
 334        if (!cache_dirty) {
 335                read_cache_from(current_index_file);
 336                cache_dirty++;
 337        }
 338        for (i = 0; i < active_nr; i++) {
 339                struct path_list_item *item;
 340                struct stage_data *e;
 341                struct cache_entry *ce = active_cache[i];
 342                if (!ce_stage(ce))
 343                        continue;
 344
 345                item = path_list_lookup(ce->name, unmerged);
 346                if (!item) {
 347                        item = path_list_insert(ce->name, unmerged);
 348                        item->util = xcalloc(1, sizeof(struct stage_data));
 349                }
 350                e = item->util;
 351                e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
 352                hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
 353        }
 354
 355        return unmerged;
 356}
 357
 358struct rename
 359{
 360        struct diff_filepair *pair;
 361        struct stage_data *src_entry;
 362        struct stage_data *dst_entry;
 363        unsigned processed:1;
 364};
 365
 366/*
 367 * Get information of all renames which occured between 'o_tree' and
 368 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
 369 * 'b_tree') to be able to associate the correct cache entries with
 370 * the rename information. 'tree' is always equal to either a_tree or b_tree.
 371 */
 372static struct path_list *get_renames(struct tree *tree,
 373                                        struct tree *o_tree,
 374                                        struct tree *a_tree,
 375                                        struct tree *b_tree,
 376                                        struct path_list *entries)
 377{
 378        int i;
 379        struct path_list *renames;
 380        struct diff_options opts;
 381
 382        renames = xcalloc(1, sizeof(struct path_list));
 383        diff_setup(&opts);
 384        opts.recursive = 1;
 385        opts.detect_rename = DIFF_DETECT_RENAME;
 386        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 387        if (diff_setup_done(&opts) < 0)
 388                die("diff setup failed");
 389        diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
 390        diffcore_std(&opts);
 391        for (i = 0; i < diff_queued_diff.nr; ++i) {
 392                struct path_list_item *item;
 393                struct rename *re;
 394                struct diff_filepair *pair = diff_queued_diff.queue[i];
 395                if (pair->status != 'R') {
 396                        diff_free_filepair(pair);
 397                        continue;
 398                }
 399                re = xmalloc(sizeof(*re));
 400                re->processed = 0;
 401                re->pair = pair;
 402                item = path_list_lookup(re->pair->one->path, entries);
 403                if (!item)
 404                        re->src_entry = insert_stage_data(re->pair->one->path,
 405                                        o_tree, a_tree, b_tree, entries);
 406                else
 407                        re->src_entry = item->util;
 408
 409                item = path_list_lookup(re->pair->two->path, entries);
 410                if (!item)
 411                        re->dst_entry = insert_stage_data(re->pair->two->path,
 412                                        o_tree, a_tree, b_tree, entries);
 413                else
 414                        re->dst_entry = item->util;
 415                item = path_list_insert(pair->one->path, renames);
 416                item->util = re;
 417        }
 418        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 419        diff_queued_diff.nr = 0;
 420        diff_flush(&opts);
 421        return renames;
 422}
 423
 424static int update_stages(const char *path, struct diff_filespec *o,
 425                         struct diff_filespec *a, struct diff_filespec *b,
 426                         int clear)
 427{
 428        int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
 429        if (clear)
 430                if (remove_file_from_cache(path))
 431                        return -1;
 432        if (o)
 433                if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
 434                        return -1;
 435        if (a)
 436                if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
 437                        return -1;
 438        if (b)
 439                if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
 440                        return -1;
 441        return 0;
 442}
 443
 444static int remove_path(const char *name)
 445{
 446        int ret, len;
 447        char *slash, *dirs;
 448
 449        ret = unlink(name);
 450        if (ret)
 451                return ret;
 452        len = strlen(name);
 453        dirs = xmalloc(len+1);
 454        memcpy(dirs, name, len);
 455        dirs[len] = '\0';
 456        while ((slash = strrchr(name, '/'))) {
 457                *slash = '\0';
 458                len = slash - name;
 459                if (rmdir(name) != 0)
 460                        break;
 461        }
 462        free(dirs);
 463        return ret;
 464}
 465
 466static int remove_file(int clean, const char *path, int no_wd)
 467{
 468        int update_cache = index_only || clean;
 469        int update_working_directory = !index_only && !no_wd;
 470
 471        if (update_cache) {
 472                if (!cache_dirty)
 473                        read_cache_from(current_index_file);
 474                cache_dirty++;
 475                if (remove_file_from_cache(path))
 476                        return -1;
 477        }
 478        if (update_working_directory) {
 479                unlink(path);
 480                if (errno != ENOENT || errno != EISDIR)
 481                        return -1;
 482                remove_path(path);
 483        }
 484        return 0;
 485}
 486
 487static char *unique_path(const char *path, const char *branch)
 488{
 489        char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
 490        int suffix = 0;
 491        struct stat st;
 492        char *p = newpath + strlen(path);
 493        strcpy(newpath, path);
 494        *(p++) = '~';
 495        strcpy(p, branch);
 496        for (; *p; ++p)
 497                if ('/' == *p)
 498                        *p = '_';
 499        while (path_list_has_path(&current_file_set, newpath) ||
 500               path_list_has_path(&current_directory_set, newpath) ||
 501               lstat(newpath, &st) == 0)
 502                sprintf(p, "_%d", suffix++);
 503
 504        path_list_insert(newpath, &current_file_set);
 505        return newpath;
 506}
 507
 508static int mkdir_p(const char *path, unsigned long mode)
 509{
 510        /* path points to cache entries, so xstrdup before messing with it */
 511        char *buf = xstrdup(path);
 512        int result = safe_create_leading_directories(buf);
 513        free(buf);
 514        return result;
 515}
 516
 517static void flush_buffer(int fd, const char *buf, unsigned long size)
 518{
 519        while (size > 0) {
 520                long ret = write_in_full(fd, buf, size);
 521                if (ret < 0) {
 522                        /* Ignore epipe */
 523                        if (errno == EPIPE)
 524                                break;
 525                        die("merge-recursive: %s", strerror(errno));
 526                } else if (!ret) {
 527                        die("merge-recursive: disk full?");
 528                }
 529                size -= ret;
 530                buf += ret;
 531        }
 532}
 533
 534static void update_file_flags(const unsigned char *sha,
 535                              unsigned mode,
 536                              const char *path,
 537                              int update_cache,
 538                              int update_wd)
 539{
 540        if (index_only)
 541                update_wd = 0;
 542
 543        if (update_wd) {
 544                char type[20];
 545                void *buf;
 546                unsigned long size;
 547
 548                buf = read_sha1_file(sha, type, &size);
 549                if (!buf)
 550                        die("cannot read object %s '%s'", sha1_to_hex(sha), path);
 551                if (strcmp(type, blob_type) != 0)
 552                        die("blob expected for %s '%s'", sha1_to_hex(sha), path);
 553
 554                if (S_ISREG(mode)) {
 555                        int fd;
 556                        if (mkdir_p(path, 0777))
 557                                die("failed to create path %s: %s", path, strerror(errno));
 558                        unlink(path);
 559                        if (mode & 0100)
 560                                mode = 0777;
 561                        else
 562                                mode = 0666;
 563                        fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
 564                        if (fd < 0)
 565                                die("failed to open %s: %s", path, strerror(errno));
 566                        flush_buffer(fd, buf, size);
 567                        close(fd);
 568                } else if (S_ISLNK(mode)) {
 569                        char *lnk = xmalloc(size + 1);
 570                        memcpy(lnk, buf, size);
 571                        lnk[size] = '\0';
 572                        mkdir_p(path, 0777);
 573                        unlink(lnk);
 574                        symlink(lnk, path);
 575                } else
 576                        die("do not know what to do with %06o %s '%s'",
 577                            mode, sha1_to_hex(sha), path);
 578        }
 579        if (update_cache)
 580                add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
 581}
 582
 583static void update_file(int clean,
 584                        const unsigned char *sha,
 585                        unsigned mode,
 586                        const char *path)
 587{
 588        update_file_flags(sha, mode, path, index_only || clean, !index_only);
 589}
 590
 591/* Low level file merging, update and removal */
 592
 593struct merge_file_info
 594{
 595        unsigned char sha[20];
 596        unsigned mode;
 597        unsigned clean:1,
 598                 merge:1;
 599};
 600
 601static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
 602{
 603        unsigned long size;
 604        char type[20];
 605
 606        if (!hashcmp(sha1, null_sha1)) {
 607                mm->ptr = xstrdup("");
 608                mm->size = 0;
 609                return;
 610        }
 611
 612        mm->ptr = read_sha1_file(sha1, type, &size);
 613        if (!mm->ptr || strcmp(type, blob_type))
 614                die("unable to read blob object %s", sha1_to_hex(sha1));
 615        mm->size = size;
 616}
 617
 618static struct merge_file_info merge_file(struct diff_filespec *o,
 619                struct diff_filespec *a, struct diff_filespec *b,
 620                const char *branch1, const char *branch2)
 621{
 622        struct merge_file_info result;
 623        result.merge = 0;
 624        result.clean = 1;
 625
 626        if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
 627                result.clean = 0;
 628                if (S_ISREG(a->mode)) {
 629                        result.mode = a->mode;
 630                        hashcpy(result.sha, a->sha1);
 631                } else {
 632                        result.mode = b->mode;
 633                        hashcpy(result.sha, b->sha1);
 634                }
 635        } else {
 636                if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
 637                        result.merge = 1;
 638
 639                result.mode = a->mode == o->mode ? b->mode: a->mode;
 640
 641                if (sha_eq(a->sha1, o->sha1))
 642                        hashcpy(result.sha, b->sha1);
 643                else if (sha_eq(b->sha1, o->sha1))
 644                        hashcpy(result.sha, a->sha1);
 645                else if (S_ISREG(a->mode)) {
 646                        mmfile_t orig, src1, src2;
 647                        mmbuffer_t result_buf;
 648                        xpparam_t xpp;
 649                        char *name1, *name2;
 650                        int merge_status;
 651
 652                        name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
 653                        name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
 654
 655                        fill_mm(o->sha1, &orig);
 656                        fill_mm(a->sha1, &src1);
 657                        fill_mm(b->sha1, &src2);
 658
 659                        memset(&xpp, 0, sizeof(xpp));
 660                        merge_status = xdl_merge(&orig,
 661                                                 &src1, name1,
 662                                                 &src2, name2,
 663                                                 &xpp, XDL_MERGE_ZEALOUS,
 664                                                 &result_buf);
 665                        free(name1);
 666                        free(name2);
 667                        free(orig.ptr);
 668                        free(src1.ptr);
 669                        free(src2.ptr);
 670
 671                        if ((merge_status < 0) || !result_buf.ptr)
 672                                die("Failed to execute internal merge");
 673
 674                        if (write_sha1_file(result_buf.ptr, result_buf.size,
 675                                            blob_type, result.sha))
 676                                die("Unable to add %s to database",
 677                                    a->path);
 678
 679                        free(result_buf.ptr);
 680                        result.clean = (merge_status == 0);
 681                } else {
 682                        if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
 683                                die("cannot merge modes?");
 684
 685                        hashcpy(result.sha, a->sha1);
 686
 687                        if (!sha_eq(a->sha1, b->sha1))
 688                                result.clean = 0;
 689                }
 690        }
 691
 692        return result;
 693}
 694
 695static void conflict_rename_rename(struct rename *ren1,
 696                                   const char *branch1,
 697                                   struct rename *ren2,
 698                                   const char *branch2)
 699{
 700        char *del[2];
 701        int delp = 0;
 702        const char *ren1_dst = ren1->pair->two->path;
 703        const char *ren2_dst = ren2->pair->two->path;
 704        const char *dst_name1 = ren1_dst;
 705        const char *dst_name2 = ren2_dst;
 706        if (path_list_has_path(&current_directory_set, ren1_dst)) {
 707                dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
 708                output("%s is a directory in %s adding as %s instead",
 709                       ren1_dst, branch2, dst_name1);
 710                remove_file(0, ren1_dst, 0);
 711        }
 712        if (path_list_has_path(&current_directory_set, ren2_dst)) {
 713                dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
 714                output("%s is a directory in %s adding as %s instead",
 715                       ren2_dst, branch1, dst_name2);
 716                remove_file(0, ren2_dst, 0);
 717        }
 718        update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
 719        update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
 720        while (delp--)
 721                free(del[delp]);
 722}
 723
 724static void conflict_rename_dir(struct rename *ren1,
 725                                const char *branch1)
 726{
 727        char *new_path = unique_path(ren1->pair->two->path, branch1);
 728        output("Renaming %s to %s instead", ren1->pair->one->path, new_path);
 729        remove_file(0, ren1->pair->two->path, 0);
 730        update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
 731        free(new_path);
 732}
 733
 734static void conflict_rename_rename_2(struct rename *ren1,
 735                                     const char *branch1,
 736                                     struct rename *ren2,
 737                                     const char *branch2)
 738{
 739        char *new_path1 = unique_path(ren1->pair->two->path, branch1);
 740        char *new_path2 = unique_path(ren2->pair->two->path, branch2);
 741        output("Renaming %s to %s and %s to %s instead",
 742               ren1->pair->one->path, new_path1,
 743               ren2->pair->one->path, new_path2);
 744        remove_file(0, ren1->pair->two->path, 0);
 745        update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
 746        update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
 747        free(new_path2);
 748        free(new_path1);
 749}
 750
 751static int process_renames(struct path_list *a_renames,
 752                           struct path_list *b_renames,
 753                           const char *a_branch,
 754                           const char *b_branch)
 755{
 756        int clean_merge = 1, i, j;
 757        struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
 758        const struct rename *sre;
 759
 760        for (i = 0; i < a_renames->nr; i++) {
 761                sre = a_renames->items[i].util;
 762                path_list_insert(sre->pair->two->path, &a_by_dst)->util
 763                        = sre->dst_entry;
 764        }
 765        for (i = 0; i < b_renames->nr; i++) {
 766                sre = b_renames->items[i].util;
 767                path_list_insert(sre->pair->two->path, &b_by_dst)->util
 768                        = sre->dst_entry;
 769        }
 770
 771        for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
 772                int compare;
 773                char *src;
 774                struct path_list *renames1, *renames2, *renames2Dst;
 775                struct rename *ren1 = NULL, *ren2 = NULL;
 776                const char *branch1, *branch2;
 777                const char *ren1_src, *ren1_dst;
 778
 779                if (i >= a_renames->nr) {
 780                        compare = 1;
 781                        ren2 = b_renames->items[j++].util;
 782                } else if (j >= b_renames->nr) {
 783                        compare = -1;
 784                        ren1 = a_renames->items[i++].util;
 785                } else {
 786                        compare = strcmp(a_renames->items[i].path,
 787                                        b_renames->items[j].path);
 788                        if (compare <= 0)
 789                                ren1 = a_renames->items[i++].util;
 790                        if (compare >= 0)
 791                                ren2 = b_renames->items[j++].util;
 792                }
 793
 794                /* TODO: refactor, so that 1/2 are not needed */
 795                if (ren1) {
 796                        renames1 = a_renames;
 797                        renames2 = b_renames;
 798                        renames2Dst = &b_by_dst;
 799                        branch1 = a_branch;
 800                        branch2 = b_branch;
 801                } else {
 802                        struct rename *tmp;
 803                        renames1 = b_renames;
 804                        renames2 = a_renames;
 805                        renames2Dst = &a_by_dst;
 806                        branch1 = b_branch;
 807                        branch2 = a_branch;
 808                        tmp = ren2;
 809                        ren2 = ren1;
 810                        ren1 = tmp;
 811                }
 812                src = ren1->pair->one->path;
 813
 814                ren1->dst_entry->processed = 1;
 815                ren1->src_entry->processed = 1;
 816
 817                if (ren1->processed)
 818                        continue;
 819                ren1->processed = 1;
 820
 821                ren1_src = ren1->pair->one->path;
 822                ren1_dst = ren1->pair->two->path;
 823
 824                if (ren2) {
 825                        const char *ren2_src = ren2->pair->one->path;
 826                        const char *ren2_dst = ren2->pair->two->path;
 827                        /* Renamed in 1 and renamed in 2 */
 828                        if (strcmp(ren1_src, ren2_src) != 0)
 829                                die("ren1.src != ren2.src");
 830                        ren2->dst_entry->processed = 1;
 831                        ren2->processed = 1;
 832                        if (strcmp(ren1_dst, ren2_dst) != 0) {
 833                                clean_merge = 0;
 834                                output("CONFLICT (rename/rename): "
 835                                       "Rename %s->%s in branch %s "
 836                                       "rename %s->%s in %s",
 837                                       src, ren1_dst, branch1,
 838                                       src, ren2_dst, branch2);
 839                                conflict_rename_rename(ren1, branch1, ren2, branch2);
 840                        } else {
 841                                struct merge_file_info mfi;
 842                                remove_file(1, ren1_src, 1);
 843                                mfi = merge_file(ren1->pair->one,
 844                                                 ren1->pair->two,
 845                                                 ren2->pair->two,
 846                                                 branch1,
 847                                                 branch2);
 848                                if (mfi.merge || !mfi.clean)
 849                                        output("Renaming %s->%s", src, ren1_dst);
 850
 851                                if (mfi.merge)
 852                                        output("Auto-merging %s", ren1_dst);
 853
 854                                if (!mfi.clean) {
 855                                        output("CONFLICT (content): merge conflict in %s",
 856                                               ren1_dst);
 857                                        clean_merge = 0;
 858
 859                                        if (!index_only)
 860                                                update_stages(ren1_dst,
 861                                                              ren1->pair->one,
 862                                                              ren1->pair->two,
 863                                                              ren2->pair->two,
 864                                                              1 /* clear */);
 865                                }
 866                                update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
 867                        }
 868                } else {
 869                        /* Renamed in 1, maybe changed in 2 */
 870                        struct path_list_item *item;
 871                        /* we only use sha1 and mode of these */
 872                        struct diff_filespec src_other, dst_other;
 873                        int try_merge, stage = a_renames == renames1 ? 3: 2;
 874
 875                        remove_file(1, ren1_src, index_only);
 876
 877                        hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
 878                        src_other.mode = ren1->src_entry->stages[stage].mode;
 879                        hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
 880                        dst_other.mode = ren1->dst_entry->stages[stage].mode;
 881
 882                        try_merge = 0;
 883
 884                        if (path_list_has_path(&current_directory_set, ren1_dst)) {
 885                                clean_merge = 0;
 886                                output("CONFLICT (rename/directory): Rename %s->%s in %s "
 887                                       " directory %s added in %s",
 888                                       ren1_src, ren1_dst, branch1,
 889                                       ren1_dst, branch2);
 890                                conflict_rename_dir(ren1, branch1);
 891                        } else if (sha_eq(src_other.sha1, null_sha1)) {
 892                                clean_merge = 0;
 893                                output("CONFLICT (rename/delete): Rename %s->%s in %s "
 894                                       "and deleted in %s",
 895                                       ren1_src, ren1_dst, branch1,
 896                                       branch2);
 897                                update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
 898                        } else if (!sha_eq(dst_other.sha1, null_sha1)) {
 899                                const char *new_path;
 900                                clean_merge = 0;
 901                                try_merge = 1;
 902                                output("CONFLICT (rename/add): Rename %s->%s in %s. "
 903                                       "%s added in %s",
 904                                       ren1_src, ren1_dst, branch1,
 905                                       ren1_dst, branch2);
 906                                new_path = unique_path(ren1_dst, branch2);
 907                                output("Adding as %s instead", new_path);
 908                                update_file(0, dst_other.sha1, dst_other.mode, new_path);
 909                        } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
 910                                ren2 = item->util;
 911                                clean_merge = 0;
 912                                ren2->processed = 1;
 913                                output("CONFLICT (rename/rename): Rename %s->%s in %s. "
 914                                       "Rename %s->%s in %s",
 915                                       ren1_src, ren1_dst, branch1,
 916                                       ren2->pair->one->path, ren2->pair->two->path, branch2);
 917                                conflict_rename_rename_2(ren1, branch1, ren2, branch2);
 918                        } else
 919                                try_merge = 1;
 920
 921                        if (try_merge) {
 922                                struct diff_filespec *o, *a, *b;
 923                                struct merge_file_info mfi;
 924                                src_other.path = (char *)ren1_src;
 925
 926                                o = ren1->pair->one;
 927                                if (a_renames == renames1) {
 928                                        a = ren1->pair->two;
 929                                        b = &src_other;
 930                                } else {
 931                                        b = ren1->pair->two;
 932                                        a = &src_other;
 933                                }
 934                                mfi = merge_file(o, a, b,
 935                                                a_branch, b_branch);
 936
 937                                if (mfi.merge || !mfi.clean)
 938                                        output("Renaming %s => %s", ren1_src, ren1_dst);
 939                                if (mfi.merge)
 940                                        output("Auto-merging %s", ren1_dst);
 941                                if (!mfi.clean) {
 942                                        output("CONFLICT (rename/modify): Merge conflict in %s",
 943                                               ren1_dst);
 944                                        clean_merge = 0;
 945
 946                                        if (!index_only)
 947                                                update_stages(ren1_dst,
 948                                                                o, a, b, 1);
 949                                }
 950                                update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
 951                        }
 952                }
 953        }
 954        path_list_clear(&a_by_dst, 0);
 955        path_list_clear(&b_by_dst, 0);
 956
 957        return clean_merge;
 958}
 959
 960static unsigned char *has_sha(const unsigned char *sha)
 961{
 962        return is_null_sha1(sha) ? NULL: (unsigned char *)sha;
 963}
 964
 965/* Per entry merge function */
 966static int process_entry(const char *path, struct stage_data *entry,
 967                         const char *branch1,
 968                         const char *branch2)
 969{
 970        /*
 971        printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
 972        print_index_entry("\tpath: ", entry);
 973        */
 974        int clean_merge = 1;
 975        unsigned char *o_sha = has_sha(entry->stages[1].sha);
 976        unsigned char *a_sha = has_sha(entry->stages[2].sha);
 977        unsigned char *b_sha = has_sha(entry->stages[3].sha);
 978        unsigned o_mode = entry->stages[1].mode;
 979        unsigned a_mode = entry->stages[2].mode;
 980        unsigned b_mode = entry->stages[3].mode;
 981
 982        if (o_sha && (!a_sha || !b_sha)) {
 983                /* Case A: Deleted in one */
 984                if ((!a_sha && !b_sha) ||
 985                    (sha_eq(a_sha, o_sha) && !b_sha) ||
 986                    (!a_sha && sha_eq(b_sha, o_sha))) {
 987                        /* Deleted in both or deleted in one and
 988                         * unchanged in the other */
 989                        if (a_sha)
 990                                output("Removing %s", path);
 991                        /* do not touch working file if it did not exist */
 992                        remove_file(1, path, !a_sha);
 993                } else {
 994                        /* Deleted in one and changed in the other */
 995                        clean_merge = 0;
 996                        if (!a_sha) {
 997                                output("CONFLICT (delete/modify): %s deleted in %s "
 998                                       "and modified in %s. Version %s of %s left in tree.",
 999                                       path, branch1,
1000                                       branch2, branch2, path);
1001                                update_file(0, b_sha, b_mode, path);
1002                        } else {
1003                                output("CONFLICT (delete/modify): %s deleted in %s "
1004                                       "and modified in %s. Version %s of %s left in tree.",
1005                                       path, branch2,
1006                                       branch1, branch1, path);
1007                                update_file(0, a_sha, a_mode, path);
1008                        }
1009                }
1010
1011        } else if ((!o_sha && a_sha && !b_sha) ||
1012                   (!o_sha && !a_sha && b_sha)) {
1013                /* Case B: Added in one. */
1014                const char *add_branch;
1015                const char *other_branch;
1016                unsigned mode;
1017                const unsigned char *sha;
1018                const char *conf;
1019
1020                if (a_sha) {
1021                        add_branch = branch1;
1022                        other_branch = branch2;
1023                        mode = a_mode;
1024                        sha = a_sha;
1025                        conf = "file/directory";
1026                } else {
1027                        add_branch = branch2;
1028                        other_branch = branch1;
1029                        mode = b_mode;
1030                        sha = b_sha;
1031                        conf = "directory/file";
1032                }
1033                if (path_list_has_path(&current_directory_set, path)) {
1034                        const char *new_path = unique_path(path, add_branch);
1035                        clean_merge = 0;
1036                        output("CONFLICT (%s): There is a directory with name %s in %s. "
1037                               "Adding %s as %s",
1038                               conf, path, other_branch, path, new_path);
1039                        remove_file(0, path, 0);
1040                        update_file(0, sha, mode, new_path);
1041                } else {
1042                        output("Adding %s", path);
1043                        update_file(1, sha, mode, path);
1044                }
1045        } else if (a_sha && b_sha) {
1046                /* Case C: Added in both (check for same permissions) and */
1047                /* case D: Modified in both, but differently. */
1048                const char *reason = "content";
1049                struct merge_file_info mfi;
1050                struct diff_filespec o, a, b;
1051
1052                if (!o_sha) {
1053                        reason = "add/add";
1054                        o_sha = (unsigned char *)null_sha1;
1055                }
1056                output("Auto-merging %s", path);
1057                o.path = a.path = b.path = (char *)path;
1058                hashcpy(o.sha1, o_sha);
1059                o.mode = o_mode;
1060                hashcpy(a.sha1, a_sha);
1061                a.mode = a_mode;
1062                hashcpy(b.sha1, b_sha);
1063                b.mode = b_mode;
1064
1065                mfi = merge_file(&o, &a, &b,
1066                                 branch1, branch2);
1067
1068                if (mfi.clean)
1069                        update_file(1, mfi.sha, mfi.mode, path);
1070                else {
1071                        clean_merge = 0;
1072                        output("CONFLICT (%s): Merge conflict in %s",
1073                                        reason, path);
1074
1075                        if (index_only)
1076                                update_file(0, mfi.sha, mfi.mode, path);
1077                        else
1078                                update_file_flags(mfi.sha, mfi.mode, path,
1079                                              0 /* update_cache */, 1 /* update_working_directory */);
1080                }
1081        } else
1082                die("Fatal merge failure, shouldn't happen.");
1083
1084        return clean_merge;
1085}
1086
1087static int merge_trees(struct tree *head,
1088                       struct tree *merge,
1089                       struct tree *common,
1090                       const char *branch1,
1091                       const char *branch2,
1092                       struct tree **result)
1093{
1094        int code, clean;
1095        if (sha_eq(common->object.sha1, merge->object.sha1)) {
1096                output("Already uptodate!");
1097                *result = head;
1098                return 1;
1099        }
1100
1101        code = git_merge_trees(index_only, common, head, merge);
1102
1103        if (code != 0)
1104                die("merging of trees %s and %s failed",
1105                    sha1_to_hex(head->object.sha1),
1106                    sha1_to_hex(merge->object.sha1));
1107
1108        *result = git_write_tree();
1109
1110        if (!*result) {
1111                struct path_list *entries, *re_head, *re_merge;
1112                int i;
1113                path_list_clear(&current_file_set, 1);
1114                path_list_clear(&current_directory_set, 1);
1115                get_files_dirs(head);
1116                get_files_dirs(merge);
1117
1118                entries = get_unmerged();
1119                re_head  = get_renames(head, common, head, merge, entries);
1120                re_merge = get_renames(merge, common, head, merge, entries);
1121                clean = process_renames(re_head, re_merge,
1122                                branch1, branch2);
1123                for (i = 0; i < entries->nr; i++) {
1124                        const char *path = entries->items[i].path;
1125                        struct stage_data *e = entries->items[i].util;
1126                        if (e->processed)
1127                                continue;
1128                        if (!process_entry(path, e, branch1, branch2))
1129                                clean = 0;
1130                }
1131                if (cache_dirty)
1132                        flush_cache();
1133
1134                path_list_clear(re_merge, 0);
1135                path_list_clear(re_head, 0);
1136                path_list_clear(entries, 1);
1137
1138                if (clean || index_only)
1139                        *result = git_write_tree();
1140                else
1141                        *result = NULL;
1142        } else {
1143                clean = 1;
1144                printf("merging of trees %s and %s resulted in %s\n",
1145                       sha1_to_hex(head->object.sha1),
1146                       sha1_to_hex(merge->object.sha1),
1147                       sha1_to_hex((*result)->object.sha1));
1148        }
1149
1150        return clean;
1151}
1152
1153static struct commit_list *reverse_commit_list(struct commit_list *list)
1154{
1155        struct commit_list *next = NULL, *current, *backup;
1156        for (current = list; current; current = backup) {
1157                backup = current->next;
1158                current->next = next;
1159                next = current;
1160        }
1161        return next;
1162}
1163
1164/*
1165 * Merge the commits h1 and h2, return the resulting virtual
1166 * commit object and a flag indicating the cleaness of the merge.
1167 */
1168static int merge(struct commit *h1,
1169                 struct commit *h2,
1170                 const char *branch1,
1171                 const char *branch2,
1172                 int call_depth /* =0 */,
1173                 struct commit *ancestor /* =None */,
1174                 struct commit **result)
1175{
1176        struct commit_list *ca = NULL, *iter;
1177        struct commit *merged_common_ancestors;
1178        struct tree *mrtree;
1179        int clean;
1180
1181        output("Merging:");
1182        output_commit_title(h1);
1183        output_commit_title(h2);
1184
1185        if (ancestor)
1186                commit_list_insert(ancestor, &ca);
1187        else
1188                ca = reverse_commit_list(get_merge_bases(h1, h2, 1));
1189
1190        output("found %u common ancestor(s):", commit_list_count(ca));
1191        for (iter = ca; iter; iter = iter->next)
1192                output_commit_title(iter->item);
1193
1194        merged_common_ancestors = pop_commit(&ca);
1195        if (merged_common_ancestors == NULL) {
1196                /* if there is no common ancestor, make an empty tree */
1197                struct tree *tree = xcalloc(1, sizeof(struct tree));
1198
1199                tree->object.parsed = 1;
1200                tree->object.type = OBJ_TREE;
1201                write_sha1_file(NULL, 0, tree_type, tree->object.sha1);
1202                merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1203        }
1204
1205        for (iter = ca; iter; iter = iter->next) {
1206                output_indent = call_depth + 1;
1207                /*
1208                 * When the merge fails, the result contains files
1209                 * with conflict markers. The cleanness flag is
1210                 * ignored, it was never acutally used, as result of
1211                 * merge_trees has always overwritten it: the commited
1212                 * "conflicts" were already resolved.
1213                 */
1214                merge(merged_common_ancestors, iter->item,
1215                      "Temporary merge branch 1",
1216                      "Temporary merge branch 2",
1217                      call_depth + 1,
1218                      NULL,
1219                      &merged_common_ancestors);
1220                output_indent = call_depth;
1221
1222                if (!merged_common_ancestors)
1223                        die("merge returned no commit");
1224        }
1225
1226        if (call_depth == 0) {
1227                setup_index(0 /* $GIT_DIR/index */);
1228                index_only = 0;
1229        } else {
1230                setup_index(1 /* temporary index */);
1231                git_read_tree(h1->tree);
1232                index_only = 1;
1233        }
1234
1235        clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1236                            branch1, branch2, &mrtree);
1237
1238        if (!ancestor && (clean || index_only)) {
1239                *result = make_virtual_commit(mrtree, "merged tree");
1240                commit_list_insert(h1, &(*result)->parents);
1241                commit_list_insert(h2, &(*result)->parents->next);
1242        } else
1243                *result = NULL;
1244
1245        return clean;
1246}
1247
1248static const char *better_branch_name(const char *branch)
1249{
1250        static char githead_env[8 + 40 + 1];
1251        char *name;
1252
1253        if (strlen(branch) != 40)
1254                return branch;
1255        sprintf(githead_env, "GITHEAD_%s", branch);
1256        name = getenv(githead_env);
1257        return name ? name : branch;
1258}
1259
1260static struct commit *get_ref(const char *ref)
1261{
1262        unsigned char sha1[20];
1263        struct object *object;
1264
1265        if (get_sha1(ref, sha1))
1266                die("Could not resolve ref '%s'", ref);
1267        object = deref_tag(parse_object(sha1), ref, strlen(ref));
1268        if (object->type == OBJ_TREE)
1269                return make_virtual_commit((struct tree*)object,
1270                        better_branch_name(ref));
1271        if (object->type != OBJ_COMMIT)
1272                return NULL;
1273        if (parse_commit((struct commit *)object))
1274                die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1275        return (struct commit *)object;
1276}
1277
1278int main(int argc, char *argv[])
1279{
1280        static const char *bases[2];
1281        static unsigned bases_count = 0;
1282        int i, clean;
1283        const char *branch1, *branch2;
1284        struct commit *result, *h1, *h2;
1285
1286        git_config(git_default_config); /* core.filemode */
1287        original_index_file = getenv(INDEX_ENVIRONMENT);
1288
1289        if (!original_index_file)
1290                original_index_file = xstrdup(git_path("index"));
1291
1292        temporary_index_file = xstrdup(git_path("mrg-rcrsv-tmp-idx"));
1293
1294        if (argc < 4)
1295                die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1296
1297        for (i = 1; i < argc; ++i) {
1298                if (!strcmp(argv[i], "--"))
1299                        break;
1300                if (bases_count < sizeof(bases)/sizeof(*bases))
1301                        bases[bases_count++] = argv[i];
1302        }
1303        if (argc - i != 3) /* "--" "<head>" "<remote>" */
1304                die("Not handling anything other than two heads merge.");
1305
1306        branch1 = argv[++i];
1307        branch2 = argv[++i];
1308
1309        h1 = get_ref(branch1);
1310        h2 = get_ref(branch2);
1311
1312        branch1 = better_branch_name(branch1);
1313        branch2 = better_branch_name(branch2);
1314        printf("Merging %s with %s\n", branch1, branch2);
1315
1316        if (bases_count == 1) {
1317                struct commit *ancestor = get_ref(bases[0]);
1318                clean = merge(h1, h2, branch1, branch2, 0, ancestor, &result);
1319        } else
1320                clean = merge(h1, h2, branch1, branch2, 0, NULL, &result);
1321
1322        if (cache_dirty)
1323                flush_cache();
1324
1325        return clean ? 0: 1;
1326}
1327
1328/*
1329vim: sw=8 noet
1330*/