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