xdiff-interface.con commit rebase -i --autosquash: auto-squash commits (f59baa5)
   1#include "cache.h"
   2#include "xdiff-interface.h"
   3#include "xdiff/xtypes.h"
   4#include "xdiff/xdiffi.h"
   5#include "xdiff/xemit.h"
   6#include "xdiff/xmacros.h"
   7
   8struct xdiff_emit_state {
   9        xdiff_emit_consume_fn consume;
  10        void *consume_callback_data;
  11        struct strbuf remainder;
  12};
  13
  14static int parse_num(char **cp_p, int *num_p)
  15{
  16        char *cp = *cp_p;
  17        int num = 0;
  18
  19        while ('0' <= *cp && *cp <= '9')
  20                num = num * 10 + *cp++ - '0';
  21        if (!(cp - *cp_p))
  22                return -1;
  23        *cp_p = cp;
  24        *num_p = num;
  25        return 0;
  26}
  27
  28int parse_hunk_header(char *line, int len,
  29                      int *ob, int *on,
  30                      int *nb, int *nn)
  31{
  32        char *cp;
  33        cp = line + 4;
  34        if (parse_num(&cp, ob)) {
  35        bad_line:
  36                return error("malformed diff output: %s", line);
  37        }
  38        if (*cp == ',') {
  39                cp++;
  40                if (parse_num(&cp, on))
  41                        goto bad_line;
  42        }
  43        else
  44                *on = 1;
  45        if (*cp++ != ' ' || *cp++ != '+')
  46                goto bad_line;
  47        if (parse_num(&cp, nb))
  48                goto bad_line;
  49        if (*cp == ',') {
  50                cp++;
  51                if (parse_num(&cp, nn))
  52                        goto bad_line;
  53        }
  54        else
  55                *nn = 1;
  56        return -!!memcmp(cp, " @@", 3);
  57}
  58
  59static void consume_one(void *priv_, char *s, unsigned long size)
  60{
  61        struct xdiff_emit_state *priv = priv_;
  62        char *ep;
  63        while (size) {
  64                unsigned long this_size;
  65                ep = memchr(s, '\n', size);
  66                this_size = (ep == NULL) ? size : (ep - s + 1);
  67                priv->consume(priv->consume_callback_data, s, this_size);
  68                size -= this_size;
  69                s += this_size;
  70        }
  71}
  72
  73static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
  74{
  75        struct xdiff_emit_state *priv = priv_;
  76        int i;
  77
  78        for (i = 0; i < nbuf; i++) {
  79                if (mb[i].ptr[mb[i].size-1] != '\n') {
  80                        /* Incomplete line */
  81                        strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
  82                        continue;
  83                }
  84
  85                /* we have a complete line */
  86                if (!priv->remainder.len) {
  87                        consume_one(priv, mb[i].ptr, mb[i].size);
  88                        continue;
  89                }
  90                strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
  91                consume_one(priv, priv->remainder.buf, priv->remainder.len);
  92                strbuf_reset(&priv->remainder);
  93        }
  94        if (priv->remainder.len) {
  95                consume_one(priv, priv->remainder.buf, priv->remainder.len);
  96                strbuf_reset(&priv->remainder);
  97        }
  98        return 0;
  99}
 100
 101/*
 102 * Trim down common substring at the end of the buffers,
 103 * but leave at least ctx lines at the end.
 104 */
 105static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
 106{
 107        const int blk = 1024;
 108        long trimmed = 0, recovered = 0;
 109        char *ap = a->ptr + a->size;
 110        char *bp = b->ptr + b->size;
 111        long smaller = (a->size < b->size) ? a->size : b->size;
 112
 113        if (ctx)
 114                return;
 115
 116        while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
 117                trimmed += blk;
 118                ap -= blk;
 119                bp -= blk;
 120        }
 121
 122        while (recovered < trimmed)
 123                if (ap[recovered++] == '\n')
 124                        break;
 125        a->size -= trimmed - recovered;
 126        b->size -= trimmed - recovered;
 127}
 128
 129int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
 130{
 131        mmfile_t a = *mf1;
 132        mmfile_t b = *mf2;
 133
 134        trim_common_tail(&a, &b, xecfg->ctxlen);
 135
 136        return xdl_diff(&a, &b, xpp, xecfg, xecb);
 137}
 138
 139int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
 140                  xdiff_emit_consume_fn fn, void *consume_callback_data,
 141                  xpparam_t const *xpp,
 142                  xdemitconf_t const *xecfg, xdemitcb_t *xecb)
 143{
 144        int ret;
 145        struct xdiff_emit_state state;
 146
 147        memset(&state, 0, sizeof(state));
 148        state.consume = fn;
 149        state.consume_callback_data = consume_callback_data;
 150        xecb->outf = xdiff_outf;
 151        xecb->priv = &state;
 152        strbuf_init(&state.remainder, 0);
 153        ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
 154        strbuf_release(&state.remainder);
 155        return ret;
 156}
 157
 158struct xdiff_emit_hunk_state {
 159        xdiff_emit_hunk_consume_fn consume;
 160        void *consume_callback_data;
 161};
 162
 163static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
 164                        xdemitconf_t const *xecfg)
 165{
 166        long s1, s2, same, p_next, t_next;
 167        xdchange_t *xch, *xche;
 168        struct xdiff_emit_hunk_state *state = ecb->priv;
 169        xdiff_emit_hunk_consume_fn fn = state->consume;
 170        void *consume_callback_data = state->consume_callback_data;
 171
 172        for (xch = xscr; xch; xch = xche->next) {
 173                xche = xdl_get_hunk(xch, xecfg);
 174
 175                s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
 176                s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
 177                same = s2 + XDL_MAX(xch->i1 - s1, 0);
 178                p_next = xche->i1 + xche->chg1;
 179                t_next = xche->i2 + xche->chg2;
 180
 181                fn(consume_callback_data, same, p_next, t_next);
 182        }
 183        return 0;
 184}
 185
 186int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
 187                   xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
 188                   xpparam_t const *xpp, xdemitconf_t *xecfg)
 189{
 190        struct xdiff_emit_hunk_state state;
 191        xdemitcb_t ecb;
 192
 193        memset(&state, 0, sizeof(state));
 194        memset(&ecb, 0, sizeof(ecb));
 195        state.consume = fn;
 196        state.consume_callback_data = consume_callback_data;
 197        xecfg->emit_func = (void (*)())process_diff;
 198        ecb.priv = &state;
 199        return xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
 200}
 201
 202int read_mmfile(mmfile_t *ptr, const char *filename)
 203{
 204        struct stat st;
 205        FILE *f;
 206        size_t sz;
 207
 208        if (stat(filename, &st))
 209                return error("Could not stat %s", filename);
 210        if ((f = fopen(filename, "rb")) == NULL)
 211                return error("Could not open %s", filename);
 212        sz = xsize_t(st.st_size);
 213        ptr->ptr = xmalloc(sz ? sz : 1);
 214        if (sz && fread(ptr->ptr, sz, 1, f) != 1)
 215                return error("Could not read %s", filename);
 216        fclose(f);
 217        ptr->size = sz;
 218        return 0;
 219}
 220
 221#define FIRST_FEW_BYTES 8000
 222int buffer_is_binary(const char *ptr, unsigned long size)
 223{
 224        if (FIRST_FEW_BYTES < size)
 225                size = FIRST_FEW_BYTES;
 226        return !!memchr(ptr, 0, size);
 227}
 228
 229struct ff_regs {
 230        int nr;
 231        struct ff_reg {
 232                regex_t re;
 233                int negate;
 234        } *array;
 235};
 236
 237static long ff_regexp(const char *line, long len,
 238                char *buffer, long buffer_size, void *priv)
 239{
 240        char *line_buffer;
 241        struct ff_regs *regs = priv;
 242        regmatch_t pmatch[2];
 243        int i;
 244        int result = -1;
 245
 246        /* Exclude terminating newline (and cr) from matching */
 247        if (len > 0 && line[len-1] == '\n') {
 248                if (len > 1 && line[len-2] == '\r')
 249                        len -= 2;
 250                else
 251                        len--;
 252        }
 253
 254        line_buffer = xstrndup(line, len); /* make NUL terminated */
 255
 256        for (i = 0; i < regs->nr; i++) {
 257                struct ff_reg *reg = regs->array + i;
 258                if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
 259                        if (reg->negate)
 260                                goto fail;
 261                        break;
 262                }
 263        }
 264        if (regs->nr <= i)
 265                goto fail;
 266        i = pmatch[1].rm_so >= 0 ? 1 : 0;
 267        line += pmatch[i].rm_so;
 268        result = pmatch[i].rm_eo - pmatch[i].rm_so;
 269        if (result > buffer_size)
 270                result = buffer_size;
 271        else
 272                while (result > 0 && (isspace(line[result - 1])))
 273                        result--;
 274        memcpy(buffer, line, result);
 275 fail:
 276        free(line_buffer);
 277        return result;
 278}
 279
 280void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
 281{
 282        int i;
 283        struct ff_regs *regs;
 284
 285        xecfg->find_func = ff_regexp;
 286        regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
 287        for (i = 0, regs->nr = 1; value[i]; i++)
 288                if (value[i] == '\n')
 289                        regs->nr++;
 290        regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
 291        for (i = 0; i < regs->nr; i++) {
 292                struct ff_reg *reg = regs->array + i;
 293                const char *ep = strchr(value, '\n'), *expression;
 294                char *buffer = NULL;
 295
 296                reg->negate = (*value == '!');
 297                if (reg->negate && i == regs->nr - 1)
 298                        die("Last expression must not be negated: %s", value);
 299                if (*value == '!')
 300                        value++;
 301                if (ep)
 302                        expression = buffer = xstrndup(value, ep - value);
 303                else
 304                        expression = value;
 305                if (regcomp(&reg->re, expression, cflags))
 306                        die("Invalid regexp to look for hunk header: %s", expression);
 307                free(buffer);
 308                value = ep + 1;
 309        }
 310}
 311
 312void xdiff_clear_find_func(xdemitconf_t *xecfg)
 313{
 314        if (xecfg->find_func) {
 315                int i;
 316                struct ff_regs *regs = xecfg->find_func_priv;
 317
 318                for (i = 0; i < regs->nr; i++)
 319                        regfree(&regs->array[i].re);
 320                free(regs->array);
 321                free(regs);
 322                xecfg->find_func = NULL;
 323                xecfg->find_func_priv = NULL;
 324        }
 325}
 326
 327int git_xmerge_style = -1;
 328
 329int git_xmerge_config(const char *var, const char *value, void *cb)
 330{
 331        if (!strcasecmp(var, "merge.conflictstyle")) {
 332                if (!value)
 333                        die("'%s' is not a boolean", var);
 334                if (!strcmp(value, "diff3"))
 335                        git_xmerge_style = XDL_MERGE_DIFF3;
 336                else if (!strcmp(value, "merge"))
 337                        git_xmerge_style = 0;
 338                else
 339                        die("unknown style '%s' given for '%s'",
 340                            value, var);
 341                return 0;
 342        }
 343        return git_default_config(var, value, cb);
 344}