8535e2d4a52114bcab275f8240d175bfa9a9fed6
   1/*
   2 * Low level 3-way in-core file merge.
   3 *
   4 * Copyright (c) 2007 Junio C Hamano
   5 */
   6
   7#include "cache.h"
   8#include "attr.h"
   9#include "xdiff-interface.h"
  10#include "run-command.h"
  11#include "ll-merge.h"
  12
  13struct ll_merge_driver;
  14
  15typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
  16                           mmbuffer_t *result,
  17                           const char *path,
  18                           mmfile_t *orig, const char *orig_name,
  19                           mmfile_t *src1, const char *name1,
  20                           mmfile_t *src2, const char *name2,
  21                           const struct ll_merge_options *opts,
  22                           int marker_size);
  23
  24struct ll_merge_driver {
  25        const char *name;
  26        const char *description;
  27        ll_merge_fn fn;
  28        const char *recursive;
  29        struct ll_merge_driver *next;
  30        char *cmdline;
  31};
  32
  33/*
  34 * Built-in low-levels
  35 */
  36static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
  37                           mmbuffer_t *result,
  38                           const char *path_unused,
  39                           mmfile_t *orig, const char *orig_name,
  40                           mmfile_t *src1, const char *name1,
  41                           mmfile_t *src2, const char *name2,
  42                           const struct ll_merge_options *opts,
  43                           int marker_size)
  44{
  45        mmfile_t *stolen;
  46        assert(opts);
  47
  48        /*
  49         * The tentative merge result is the or common ancestor for an internal merge.
  50         */
  51        if (opts->virtual_ancestor) {
  52                stolen = orig;
  53        } else {
  54                switch (opts->variant) {
  55                default:
  56                case XDL_MERGE_FAVOR_OURS:
  57                        stolen = src1;
  58                        break;
  59                case XDL_MERGE_FAVOR_THEIRS:
  60                        stolen = src2;
  61                        break;
  62                }
  63        }
  64
  65        result->ptr = stolen->ptr;
  66        result->size = stolen->size;
  67        stolen->ptr = NULL;
  68
  69        /*
  70         * With -Xtheirs or -Xours, we have cleanly merged;
  71         * otherwise we got a conflict.
  72         */
  73        return (opts->variant ? 0 : 1);
  74}
  75
  76static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
  77                        mmbuffer_t *result,
  78                        const char *path,
  79                        mmfile_t *orig, const char *orig_name,
  80                        mmfile_t *src1, const char *name1,
  81                        mmfile_t *src2, const char *name2,
  82                        const struct ll_merge_options *opts,
  83                        int marker_size)
  84{
  85        xmparam_t xmp;
  86        assert(opts);
  87
  88        if (buffer_is_binary(orig->ptr, orig->size) ||
  89            buffer_is_binary(src1->ptr, src1->size) ||
  90            buffer_is_binary(src2->ptr, src2->size)) {
  91                warning("Cannot merge binary files: %s (%s vs. %s)\n",
  92                        path, name1, name2);
  93                return ll_binary_merge(drv_unused, result,
  94                                       path,
  95                                       orig, orig_name,
  96                                       src1, name1,
  97                                       src2, name2,
  98                                       opts, marker_size);
  99        }
 100
 101        memset(&xmp, 0, sizeof(xmp));
 102        xmp.level = XDL_MERGE_ZEALOUS;
 103        xmp.favor = opts->variant;
 104        xmp.xpp.flags = opts->xdl_opts;
 105        if (git_xmerge_style >= 0)
 106                xmp.style = git_xmerge_style;
 107        if (marker_size > 0)
 108                xmp.marker_size = marker_size;
 109        xmp.ancestor = orig_name;
 110        xmp.file1 = name1;
 111        xmp.file2 = name2;
 112        return xdl_merge(orig, src1, src2, &xmp, result);
 113}
 114
 115static int ll_union_merge(const struct ll_merge_driver *drv_unused,
 116                          mmbuffer_t *result,
 117                          const char *path_unused,
 118                          mmfile_t *orig, const char *orig_name,
 119                          mmfile_t *src1, const char *name1,
 120                          mmfile_t *src2, const char *name2,
 121                          const struct ll_merge_options *opts,
 122                          int marker_size)
 123{
 124        /* Use union favor */
 125        struct ll_merge_options o;
 126        assert(opts);
 127        o = *opts;
 128        o.variant = XDL_MERGE_FAVOR_UNION;
 129        return ll_xdl_merge(drv_unused, result, path_unused,
 130                            orig, NULL, src1, NULL, src2, NULL,
 131                            &o, marker_size);
 132}
 133
 134#define LL_BINARY_MERGE 0
 135#define LL_TEXT_MERGE 1
 136#define LL_UNION_MERGE 2
 137static struct ll_merge_driver ll_merge_drv[] = {
 138        { "binary", "built-in binary merge", ll_binary_merge },
 139        { "text", "built-in 3-way text merge", ll_xdl_merge },
 140        { "union", "built-in union merge", ll_union_merge },
 141};
 142
 143static void create_temp(mmfile_t *src, char *path)
 144{
 145        int fd;
 146
 147        strcpy(path, ".merge_file_XXXXXX");
 148        fd = xmkstemp(path);
 149        if (write_in_full(fd, src->ptr, src->size) != src->size)
 150                die_errno("unable to write temp-file");
 151        close(fd);
 152}
 153
 154/*
 155 * User defined low-level merge driver support.
 156 */
 157static int ll_ext_merge(const struct ll_merge_driver *fn,
 158                        mmbuffer_t *result,
 159                        const char *path,
 160                        mmfile_t *orig, const char *orig_name,
 161                        mmfile_t *src1, const char *name1,
 162                        mmfile_t *src2, const char *name2,
 163                        const struct ll_merge_options *opts,
 164                        int marker_size)
 165{
 166        char temp[4][50];
 167        struct strbuf cmd = STRBUF_INIT;
 168        struct strbuf_expand_dict_entry dict[5];
 169        const char *args[] = { NULL, NULL };
 170        int status, fd, i;
 171        struct stat st;
 172        assert(opts);
 173
 174        dict[0].placeholder = "O"; dict[0].value = temp[0];
 175        dict[1].placeholder = "A"; dict[1].value = temp[1];
 176        dict[2].placeholder = "B"; dict[2].value = temp[2];
 177        dict[3].placeholder = "L"; dict[3].value = temp[3];
 178        dict[4].placeholder = NULL; dict[4].value = NULL;
 179
 180        if (fn->cmdline == NULL)
 181                die("custom merge driver %s lacks command line.", fn->name);
 182
 183        result->ptr = NULL;
 184        result->size = 0;
 185        create_temp(orig, temp[0]);
 186        create_temp(src1, temp[1]);
 187        create_temp(src2, temp[2]);
 188        sprintf(temp[3], "%d", marker_size);
 189
 190        strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
 191
 192        args[0] = cmd.buf;
 193        status = run_command_v_opt(args, RUN_USING_SHELL);
 194        fd = open(temp[1], O_RDONLY);
 195        if (fd < 0)
 196                goto bad;
 197        if (fstat(fd, &st))
 198                goto close_bad;
 199        result->size = st.st_size;
 200        result->ptr = xmalloc(result->size + 1);
 201        if (read_in_full(fd, result->ptr, result->size) != result->size) {
 202                free(result->ptr);
 203                result->ptr = NULL;
 204                result->size = 0;
 205        }
 206 close_bad:
 207        close(fd);
 208 bad:
 209        for (i = 0; i < 3; i++)
 210                unlink_or_warn(temp[i]);
 211        strbuf_release(&cmd);
 212        return status;
 213}
 214
 215/*
 216 * merge.default and merge.driver configuration items
 217 */
 218static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 219static const char *default_ll_merge;
 220
 221static int read_merge_config(const char *var, const char *value, void *cb)
 222{
 223        struct ll_merge_driver *fn;
 224        const char *ep, *name;
 225        int namelen;
 226
 227        if (!strcmp(var, "merge.default")) {
 228                if (value)
 229                        default_ll_merge = xstrdup(value);
 230                return 0;
 231        }
 232
 233        /*
 234         * We are not interested in anything but "merge.<name>.variable";
 235         * especially, we do not want to look at variables such as
 236         * "merge.summary", "merge.tool", and "merge.verbosity".
 237         */
 238        if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
 239                return 0;
 240
 241        /*
 242         * Find existing one as we might be processing merge.<name>.var2
 243         * after seeing merge.<name>.var1.
 244         */
 245        name = var + 6;
 246        namelen = ep - name;
 247        for (fn = ll_user_merge; fn; fn = fn->next)
 248                if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
 249                        break;
 250        if (!fn) {
 251                fn = xcalloc(1, sizeof(struct ll_merge_driver));
 252                fn->name = xmemdupz(name, namelen);
 253                fn->fn = ll_ext_merge;
 254                *ll_user_merge_tail = fn;
 255                ll_user_merge_tail = &(fn->next);
 256        }
 257
 258        ep++;
 259
 260        if (!strcmp("name", ep)) {
 261                if (!value)
 262                        return error("%s: lacks value", var);
 263                fn->description = xstrdup(value);
 264                return 0;
 265        }
 266
 267        if (!strcmp("driver", ep)) {
 268                if (!value)
 269                        return error("%s: lacks value", var);
 270                /*
 271                 * merge.<name>.driver specifies the command line:
 272                 *
 273                 *      command-line
 274                 *
 275                 * The command-line will be interpolated with the following
 276                 * tokens and is given to the shell:
 277                 *
 278                 *    %O - temporary file name for the merge base.
 279                 *    %A - temporary file name for our version.
 280                 *    %B - temporary file name for the other branches' version.
 281                 *    %L - conflict marker length
 282                 *
 283                 * The external merge driver should write the results in the
 284                 * file named by %A, and signal that it has done with zero exit
 285                 * status.
 286                 */
 287                fn->cmdline = xstrdup(value);
 288                return 0;
 289        }
 290
 291        if (!strcmp("recursive", ep)) {
 292                if (!value)
 293                        return error("%s: lacks value", var);
 294                fn->recursive = xstrdup(value);
 295                return 0;
 296        }
 297
 298        return 0;
 299}
 300
 301static void initialize_ll_merge(void)
 302{
 303        if (ll_user_merge_tail)
 304                return;
 305        ll_user_merge_tail = &ll_user_merge;
 306        git_config(read_merge_config, NULL);
 307}
 308
 309static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
 310{
 311        struct ll_merge_driver *fn;
 312        const char *name;
 313        int i;
 314
 315        initialize_ll_merge();
 316
 317        if (ATTR_TRUE(merge_attr))
 318                return &ll_merge_drv[LL_TEXT_MERGE];
 319        else if (ATTR_FALSE(merge_attr))
 320                return &ll_merge_drv[LL_BINARY_MERGE];
 321        else if (ATTR_UNSET(merge_attr)) {
 322                if (!default_ll_merge)
 323                        return &ll_merge_drv[LL_TEXT_MERGE];
 324                else
 325                        name = default_ll_merge;
 326        }
 327        else
 328                name = merge_attr;
 329
 330        for (fn = ll_user_merge; fn; fn = fn->next)
 331                if (!strcmp(fn->name, name))
 332                        return fn;
 333
 334        for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
 335                if (!strcmp(ll_merge_drv[i].name, name))
 336                        return &ll_merge_drv[i];
 337
 338        /* default to the 3-way */
 339        return &ll_merge_drv[LL_TEXT_MERGE];
 340}
 341
 342static int git_path_check_merge(const char *path, struct git_attr_check check[2])
 343{
 344        if (!check[0].attr) {
 345                check[0].attr = git_attr("merge");
 346                check[1].attr = git_attr("conflict-marker-size");
 347        }
 348        return git_check_attr(path, 2, check);
 349}
 350
 351static void normalize_file(mmfile_t *mm, const char *path)
 352{
 353        struct strbuf strbuf = STRBUF_INIT;
 354        if (renormalize_buffer(path, mm->ptr, mm->size, &strbuf)) {
 355                free(mm->ptr);
 356                mm->size = strbuf.len;
 357                mm->ptr = strbuf_detach(&strbuf, NULL);
 358        }
 359}
 360
 361int ll_merge(mmbuffer_t *result_buf,
 362             const char *path,
 363             mmfile_t *ancestor, const char *ancestor_label,
 364             mmfile_t *ours, const char *our_label,
 365             mmfile_t *theirs, const char *their_label,
 366             const struct ll_merge_options *opts)
 367{
 368        static struct git_attr_check check[2];
 369        static const struct ll_merge_options default_opts;
 370        const char *ll_driver_name = NULL;
 371        int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 372        const struct ll_merge_driver *driver;
 373
 374        if (!opts)
 375                opts = &default_opts;
 376
 377        if (opts->renormalize) {
 378                normalize_file(ancestor, path);
 379                normalize_file(ours, path);
 380                normalize_file(theirs, path);
 381        }
 382        if (!git_path_check_merge(path, check)) {
 383                ll_driver_name = check[0].value;
 384                if (check[1].value) {
 385                        marker_size = atoi(check[1].value);
 386                        if (marker_size <= 0)
 387                                marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 388                }
 389        }
 390        driver = find_ll_merge_driver(ll_driver_name);
 391        if (opts->virtual_ancestor && driver->recursive)
 392                driver = find_ll_merge_driver(driver->recursive);
 393        return driver->fn(driver, result_buf, path, ancestor, ancestor_label,
 394                          ours, our_label, theirs, their_label,
 395                          opts, marker_size);
 396}
 397
 398int ll_merge_marker_size(const char *path)
 399{
 400        static struct git_attr_check check;
 401        int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 402
 403        if (!check.attr)
 404                check.attr = git_attr("conflict-marker-size");
 405        if (!git_check_attr(path, 1, &check) && check.value) {
 406                marker_size = atoi(check.value);
 407                if (marker_size <= 0)
 408                        marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 409        }
 410        return marker_size;
 411}