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