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