xdiff-interface.con commit Merge branch 'sp/maint-index-pack' into maint (fcf74db)
   1#include "cache.h"
   2#include "xdiff-interface.h"
   3
   4static int parse_num(char **cp_p, int *num_p)
   5{
   6        char *cp = *cp_p;
   7        int num = 0;
   8        int read_some;
   9
  10        while ('0' <= *cp && *cp <= '9')
  11                num = num * 10 + *cp++ - '0';
  12        if (!(read_some = cp - *cp_p))
  13                return -1;
  14        *cp_p = cp;
  15        *num_p = num;
  16        return 0;
  17}
  18
  19int parse_hunk_header(char *line, int len,
  20                      int *ob, int *on,
  21                      int *nb, int *nn)
  22{
  23        char *cp;
  24        cp = line + 4;
  25        if (parse_num(&cp, ob)) {
  26        bad_line:
  27                return error("malformed diff output: %s", line);
  28        }
  29        if (*cp == ',') {
  30                cp++;
  31                if (parse_num(&cp, on))
  32                        goto bad_line;
  33        }
  34        else
  35                *on = 1;
  36        if (*cp++ != ' ' || *cp++ != '+')
  37                goto bad_line;
  38        if (parse_num(&cp, nb))
  39                goto bad_line;
  40        if (*cp == ',') {
  41                cp++;
  42                if (parse_num(&cp, nn))
  43                        goto bad_line;
  44        }
  45        else
  46                *nn = 1;
  47        return -!!memcmp(cp, " @@", 3);
  48}
  49
  50static void consume_one(void *priv_, char *s, unsigned long size)
  51{
  52        struct xdiff_emit_state *priv = priv_;
  53        char *ep;
  54        while (size) {
  55                unsigned long this_size;
  56                ep = memchr(s, '\n', size);
  57                this_size = (ep == NULL) ? size : (ep - s + 1);
  58                priv->consume(priv, s, this_size);
  59                size -= this_size;
  60                s += this_size;
  61        }
  62}
  63
  64int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
  65{
  66        struct xdiff_emit_state *priv = priv_;
  67        int i;
  68
  69        for (i = 0; i < nbuf; i++) {
  70                if (mb[i].ptr[mb[i].size-1] != '\n') {
  71                        /* Incomplete line */
  72                        priv->remainder = xrealloc(priv->remainder,
  73                                                   priv->remainder_size +
  74                                                   mb[i].size);
  75                        memcpy(priv->remainder + priv->remainder_size,
  76                               mb[i].ptr, mb[i].size);
  77                        priv->remainder_size += mb[i].size;
  78                        continue;
  79                }
  80
  81                /* we have a complete line */
  82                if (!priv->remainder) {
  83                        consume_one(priv, mb[i].ptr, mb[i].size);
  84                        continue;
  85                }
  86                priv->remainder = xrealloc(priv->remainder,
  87                                           priv->remainder_size +
  88                                           mb[i].size);
  89                memcpy(priv->remainder + priv->remainder_size,
  90                       mb[i].ptr, mb[i].size);
  91                consume_one(priv, priv->remainder,
  92                            priv->remainder_size + mb[i].size);
  93                free(priv->remainder);
  94                priv->remainder = NULL;
  95                priv->remainder_size = 0;
  96        }
  97        if (priv->remainder) {
  98                consume_one(priv, priv->remainder, priv->remainder_size);
  99                free(priv->remainder);
 100                priv->remainder = NULL;
 101                priv->remainder_size = 0;
 102        }
 103        return 0;
 104}
 105
 106/*
 107 * Trim down common substring at the end of the buffers,
 108 * but leave at least ctx lines at the end.
 109 */
 110static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
 111{
 112        const int blk = 1024;
 113        long trimmed = 0, recovered = 0;
 114        char *ap = a->ptr + a->size;
 115        char *bp = b->ptr + b->size;
 116        long smaller = (a->size < b->size) ? a->size : b->size;
 117
 118        if (ctx)
 119                return;
 120
 121        while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
 122                trimmed += blk;
 123                ap -= blk;
 124                bp -= blk;
 125        }
 126
 127        while (recovered < trimmed)
 128                if (ap[recovered++] == '\n')
 129                        break;
 130        a->size -= trimmed - recovered;
 131        b->size -= trimmed - recovered;
 132}
 133
 134int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
 135{
 136        mmfile_t a = *mf1;
 137        mmfile_t b = *mf2;
 138
 139        trim_common_tail(&a, &b, xecfg->ctxlen);
 140
 141        return xdl_diff(&a, &b, xpp, xecfg, xecb);
 142}
 143
 144int read_mmfile(mmfile_t *ptr, const char *filename)
 145{
 146        struct stat st;
 147        FILE *f;
 148        size_t sz;
 149
 150        if (stat(filename, &st))
 151                return error("Could not stat %s", filename);
 152        if ((f = fopen(filename, "rb")) == NULL)
 153                return error("Could not open %s", filename);
 154        sz = xsize_t(st.st_size);
 155        ptr->ptr = xmalloc(sz ? sz : 1);
 156        if (sz && fread(ptr->ptr, sz, 1, f) != 1)
 157                return error("Could not read %s", filename);
 158        fclose(f);
 159        ptr->size = sz;
 160        return 0;
 161}
 162
 163#define FIRST_FEW_BYTES 8000
 164int buffer_is_binary(const char *ptr, unsigned long size)
 165{
 166        if (FIRST_FEW_BYTES < size)
 167                size = FIRST_FEW_BYTES;
 168        return !!memchr(ptr, 0, size);
 169}
 170
 171struct ff_regs {
 172        int nr;
 173        struct ff_reg {
 174                regex_t re;
 175                int negate;
 176        } *array;
 177};
 178
 179static long ff_regexp(const char *line, long len,
 180                char *buffer, long buffer_size, void *priv)
 181{
 182        char *line_buffer = xstrndup(line, len); /* make NUL terminated */
 183        struct ff_regs *regs = priv;
 184        regmatch_t pmatch[2];
 185        int result = 0, i;
 186
 187        for (i = 0; i < regs->nr; i++) {
 188                struct ff_reg *reg = regs->array + i;
 189                if (reg->negate ^ !!regexec(&reg->re,
 190                                        line_buffer, 2, pmatch, 0)) {
 191                        free(line_buffer);
 192                        return -1;
 193                }
 194        }
 195        i = pmatch[1].rm_so >= 0 ? 1 : 0;
 196        line += pmatch[i].rm_so;
 197        result = pmatch[i].rm_eo - pmatch[i].rm_so;
 198        if (result > buffer_size)
 199                result = buffer_size;
 200        else
 201                while (result > 0 && (isspace(line[result - 1]) ||
 202                                        line[result - 1] == '\n'))
 203                        result--;
 204        memcpy(buffer, line, result);
 205        free(line_buffer);
 206        return result;
 207}
 208
 209void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value)
 210{
 211        int i;
 212        struct ff_regs *regs;
 213
 214        xecfg->find_func = ff_regexp;
 215        regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
 216        for (i = 0, regs->nr = 1; value[i]; i++)
 217                if (value[i] == '\n')
 218                        regs->nr++;
 219        regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
 220        for (i = 0; i < regs->nr; i++) {
 221                struct ff_reg *reg = regs->array + i;
 222                const char *ep = strchr(value, '\n'), *expression;
 223                char *buffer = NULL;
 224
 225                reg->negate = (*value == '!');
 226                if (reg->negate && i == regs->nr - 1)
 227                        die("Last expression must not be negated: %s", value);
 228                if (*value == '!')
 229                        value++;
 230                if (ep)
 231                        expression = buffer = xstrndup(value, ep - value);
 232                else
 233                        expression = value;
 234                if (regcomp(&reg->re, expression, 0))
 235                        die("Invalid regexp to look for hunk header: %s", expression);
 236                free(buffer);
 237                value = ep + 1;
 238        }
 239}