ll-merge.con commit Add a "TEMPLATE DIRECTORY" section to git-init[1]. (d8a8488)
   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,
  19                           mmfile_t *src1, const char *name1,
  20                           mmfile_t *src2, const char *name2,
  21                           int flag,
  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,
  40                           mmfile_t *src1, const char *name1,
  41                           mmfile_t *src2, const char *name2,
  42                           int flag, int marker_size)
  43{
  44        /*
  45         * The tentative merge result is "ours" for the final round,
  46         * or common ancestor for an internal merge.  Still return
  47         * "conflicted merge" status.
  48         */
  49        mmfile_t *stolen = (flag & 01) ? orig : src1;
  50
  51        result->ptr = stolen->ptr;
  52        result->size = stolen->size;
  53        stolen->ptr = NULL;
  54        return 1;
  55}
  56
  57static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
  58                        mmbuffer_t *result,
  59                        const char *path,
  60                        mmfile_t *orig,
  61                        mmfile_t *src1, const char *name1,
  62                        mmfile_t *src2, const char *name2,
  63                        int flag, int marker_size)
  64{
  65        xmparam_t xmp;
  66        int style = 0;
  67        int favor = (flag >> 1) & 03;
  68
  69        if (buffer_is_binary(orig->ptr, orig->size) ||
  70            buffer_is_binary(src1->ptr, src1->size) ||
  71            buffer_is_binary(src2->ptr, src2->size)) {
  72                warning("Cannot merge binary files: %s (%s vs. %s)\n",
  73                        path, name1, name2);
  74                return ll_binary_merge(drv_unused, result,
  75                                       path,
  76                                       orig, src1, name1,
  77                                       src2, name2,
  78                                       flag, marker_size);
  79        }
  80
  81        memset(&xmp, 0, sizeof(xmp));
  82        if (git_xmerge_style >= 0)
  83                style = git_xmerge_style;
  84        if (marker_size > 0)
  85                xmp.marker_size = marker_size;
  86        return xdl_merge(orig,
  87                         src1, name1,
  88                         src2, name2,
  89                         &xmp, XDL_MERGE_FLAGS(XDL_MERGE_ZEALOUS, style, favor),
  90                         result);
  91}
  92
  93static int ll_union_merge(const struct ll_merge_driver *drv_unused,
  94                          mmbuffer_t *result,
  95                          const char *path_unused,
  96                          mmfile_t *orig,
  97                          mmfile_t *src1, const char *name1,
  98                          mmfile_t *src2, const char *name2,
  99                          int flag, int marker_size)
 100{
 101        char *src, *dst;
 102        long size;
 103        int status, saved_style;
 104
 105        /* We have to force the RCS "merge" style */
 106        saved_style = git_xmerge_style;
 107        git_xmerge_style = 0;
 108        status = ll_xdl_merge(drv_unused, result, path_unused,
 109                              orig, src1, NULL, src2, NULL,
 110                              flag, marker_size);
 111        git_xmerge_style = saved_style;
 112        if (status <= 0)
 113                return status;
 114        size = result->size;
 115        src = dst = result->ptr;
 116        while (size) {
 117                char ch;
 118                if ((marker_size < size) &&
 119                    (*src == '<' || *src == '=' || *src == '>')) {
 120                        int i;
 121                        ch = *src;
 122                        for (i = 0; i < marker_size; i++)
 123                                if (src[i] != ch)
 124                                        goto not_a_marker;
 125                        if (src[marker_size] != '\n')
 126                                goto not_a_marker;
 127                        src += marker_size + 1;
 128                        size -= marker_size + 1;
 129                        continue;
 130                }
 131        not_a_marker:
 132                do {
 133                        ch = *src++;
 134                        *dst++ = ch;
 135                        size--;
 136                } while (ch != '\n' && size);
 137        }
 138        result->size = dst - result->ptr;
 139        return 0;
 140}
 141
 142#define LL_BINARY_MERGE 0
 143#define LL_TEXT_MERGE 1
 144#define LL_UNION_MERGE 2
 145static struct ll_merge_driver ll_merge_drv[] = {
 146        { "binary", "built-in binary merge", ll_binary_merge },
 147        { "text", "built-in 3-way text merge", ll_xdl_merge },
 148        { "union", "built-in union merge", ll_union_merge },
 149};
 150
 151static void create_temp(mmfile_t *src, char *path)
 152{
 153        int fd;
 154
 155        strcpy(path, ".merge_file_XXXXXX");
 156        fd = xmkstemp(path);
 157        if (write_in_full(fd, src->ptr, src->size) != src->size)
 158                die_errno("unable to write temp-file");
 159        close(fd);
 160}
 161
 162/*
 163 * User defined low-level merge driver support.
 164 */
 165static int ll_ext_merge(const struct ll_merge_driver *fn,
 166                        mmbuffer_t *result,
 167                        const char *path,
 168                        mmfile_t *orig,
 169                        mmfile_t *src1, const char *name1,
 170                        mmfile_t *src2, const char *name2,
 171                        int flag, int marker_size)
 172{
 173        char temp[4][50];
 174        struct strbuf cmd = STRBUF_INIT;
 175        struct strbuf_expand_dict_entry dict[] = {
 176                { "O", temp[0] },
 177                { "A", temp[1] },
 178                { "B", temp[2] },
 179                { "L", temp[3] },
 180                { NULL }
 181        };
 182        const char *args[] = { NULL, NULL };
 183        int status, fd, i;
 184        struct stat st;
 185
 186        if (fn->cmdline == NULL)
 187                die("custom merge driver %s lacks command line.", fn->name);
 188
 189        result->ptr = NULL;
 190        result->size = 0;
 191        create_temp(orig, temp[0]);
 192        create_temp(src1, temp[1]);
 193        create_temp(src2, temp[2]);
 194        sprintf(temp[3], "%d", marker_size);
 195
 196        strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
 197
 198        args[0] = cmd.buf;
 199        status = run_command_v_opt(args, RUN_USING_SHELL);
 200        fd = open(temp[1], O_RDONLY);
 201        if (fd < 0)
 202                goto bad;
 203        if (fstat(fd, &st))
 204                goto close_bad;
 205        result->size = st.st_size;
 206        result->ptr = xmalloc(result->size + 1);
 207        if (read_in_full(fd, result->ptr, result->size) != result->size) {
 208                free(result->ptr);
 209                result->ptr = NULL;
 210                result->size = 0;
 211        }
 212 close_bad:
 213        close(fd);
 214 bad:
 215        for (i = 0; i < 3; i++)
 216                unlink_or_warn(temp[i]);
 217        strbuf_release(&cmd);
 218        return status;
 219}
 220
 221/*
 222 * merge.default and merge.driver configuration items
 223 */
 224static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
 225static const char *default_ll_merge;
 226
 227static int read_merge_config(const char *var, const char *value, void *cb)
 228{
 229        struct ll_merge_driver *fn;
 230        const char *ep, *name;
 231        int namelen;
 232
 233        if (!strcmp(var, "merge.default")) {
 234                if (value)
 235                        default_ll_merge = xstrdup(value);
 236                return 0;
 237        }
 238
 239        /*
 240         * We are not interested in anything but "merge.<name>.variable";
 241         * especially, we do not want to look at variables such as
 242         * "merge.summary", "merge.tool", and "merge.verbosity".
 243         */
 244        if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
 245                return 0;
 246
 247        /*
 248         * Find existing one as we might be processing merge.<name>.var2
 249         * after seeing merge.<name>.var1.
 250         */
 251        name = var + 6;
 252        namelen = ep - name;
 253        for (fn = ll_user_merge; fn; fn = fn->next)
 254                if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
 255                        break;
 256        if (!fn) {
 257                fn = xcalloc(1, sizeof(struct ll_merge_driver));
 258                fn->name = xmemdupz(name, namelen);
 259                fn->fn = ll_ext_merge;
 260                *ll_user_merge_tail = fn;
 261                ll_user_merge_tail = &(fn->next);
 262        }
 263
 264        ep++;
 265
 266        if (!strcmp("name", ep)) {
 267                if (!value)
 268                        return error("%s: lacks value", var);
 269                fn->description = xstrdup(value);
 270                return 0;
 271        }
 272
 273        if (!strcmp("driver", ep)) {
 274                if (!value)
 275                        return error("%s: lacks value", var);
 276                /*
 277                 * merge.<name>.driver specifies the command line:
 278                 *
 279                 *      command-line
 280                 *
 281                 * The command-line will be interpolated with the following
 282                 * tokens and is given to the shell:
 283                 *
 284                 *    %O - temporary file name for the merge base.
 285                 *    %A - temporary file name for our version.
 286                 *    %B - temporary file name for the other branches' version.
 287                 *    %L - conflict marker length
 288                 *
 289                 * The external merge driver should write the results in the
 290                 * file named by %A, and signal that it has done with zero exit
 291                 * status.
 292                 */
 293                fn->cmdline = xstrdup(value);
 294                return 0;
 295        }
 296
 297        if (!strcmp("recursive", ep)) {
 298                if (!value)
 299                        return error("%s: lacks value", var);
 300                fn->recursive = xstrdup(value);
 301                return 0;
 302        }
 303
 304        return 0;
 305}
 306
 307static void initialize_ll_merge(void)
 308{
 309        if (ll_user_merge_tail)
 310                return;
 311        ll_user_merge_tail = &ll_user_merge;
 312        git_config(read_merge_config, NULL);
 313}
 314
 315static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
 316{
 317        struct ll_merge_driver *fn;
 318        const char *name;
 319        int i;
 320
 321        initialize_ll_merge();
 322
 323        if (ATTR_TRUE(merge_attr))
 324                return &ll_merge_drv[LL_TEXT_MERGE];
 325        else if (ATTR_FALSE(merge_attr))
 326                return &ll_merge_drv[LL_BINARY_MERGE];
 327        else if (ATTR_UNSET(merge_attr)) {
 328                if (!default_ll_merge)
 329                        return &ll_merge_drv[LL_TEXT_MERGE];
 330                else
 331                        name = default_ll_merge;
 332        }
 333        else
 334                name = merge_attr;
 335
 336        for (fn = ll_user_merge; fn; fn = fn->next)
 337                if (!strcmp(fn->name, name))
 338                        return fn;
 339
 340        for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
 341                if (!strcmp(ll_merge_drv[i].name, name))
 342                        return &ll_merge_drv[i];
 343
 344        /* default to the 3-way */
 345        return &ll_merge_drv[LL_TEXT_MERGE];
 346}
 347
 348static int git_path_check_merge(const char *path, struct git_attr_check check[2])
 349{
 350        if (!check[0].attr) {
 351                check[0].attr = git_attr("merge");
 352                check[1].attr = git_attr("conflict-marker-size");
 353        }
 354        return git_checkattr(path, 2, check);
 355}
 356
 357int ll_merge(mmbuffer_t *result_buf,
 358             const char *path,
 359             mmfile_t *ancestor,
 360             mmfile_t *ours, const char *our_label,
 361             mmfile_t *theirs, const char *their_label,
 362             int flag)
 363{
 364        static struct git_attr_check check[2];
 365        const char *ll_driver_name = NULL;
 366        int marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
 367        const struct ll_merge_driver *driver;
 368        int virtual_ancestor = flag & 01;
 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 (virtual_ancestor && driver->recursive)
 380                driver = find_ll_merge_driver(driver->recursive);
 381        return driver->fn(driver, result_buf, path, ancestor,
 382                          ours, our_label, theirs, their_label,
 383                          flag, 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_checkattr(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}