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