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