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