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