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