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