cadb071a441e7e67bb2b6b638d853b0489ac114f
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 * Copyright (C) 2010 Google Inc.
   4 */
   5#include "cache.h"
   6#include "diff.h"
   7#include "diffcore.h"
   8#include "xdiff-interface.h"
   9#include "kwset.h"
  10
  11typedef int (*pickaxe_fn)(struct diff_filepair *p, struct diff_options *o, regex_t *regexp, kwset_t kws);
  12
  13static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
  14                    regex_t *regexp, kwset_t kws, pickaxe_fn fn)
  15{
  16        int i;
  17        struct diff_queue_struct outq;
  18
  19        DIFF_QUEUE_CLEAR(&outq);
  20
  21        if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
  22                /* Showing the whole changeset if needle exists */
  23                for (i = 0; i < q->nr; i++) {
  24                        struct diff_filepair *p = q->queue[i];
  25                        if (fn(p, o, regexp, kws))
  26                                return; /* do not munge the queue */
  27                }
  28
  29                /*
  30                 * Otherwise we will clear the whole queue by copying
  31                 * the empty outq at the end of this function, but
  32                 * first clear the current entries in the queue.
  33                 */
  34                for (i = 0; i < q->nr; i++)
  35                        diff_free_filepair(q->queue[i]);
  36        } else {
  37                /* Showing only the filepairs that has the needle */
  38                for (i = 0; i < q->nr; i++) {
  39                        struct diff_filepair *p = q->queue[i];
  40                        if (fn(p, o, regexp, kws))
  41                                diff_q(&outq, p);
  42                        else
  43                                diff_free_filepair(p);
  44                }
  45        }
  46
  47        free(q->queue);
  48        *q = outq;
  49}
  50
  51struct diffgrep_cb {
  52        regex_t *regexp;
  53        int hit;
  54};
  55
  56static void diffgrep_consume(void *priv, char *line, unsigned long len)
  57{
  58        struct diffgrep_cb *data = priv;
  59        regmatch_t regmatch;
  60        int hold;
  61
  62        if (line[0] != '+' && line[0] != '-')
  63                return;
  64        if (data->hit)
  65                /*
  66                 * NEEDSWORK: we should have a way to terminate the
  67                 * caller early.
  68                 */
  69                return;
  70        /* Yuck -- line ought to be "const char *"! */
  71        hold = line[len];
  72        line[len] = '\0';
  73        data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
  74        line[len] = hold;
  75}
  76
  77static int diff_grep(struct diff_filepair *p, struct diff_options *o,
  78                     regex_t *regexp, kwset_t kws)
  79{
  80        regmatch_t regmatch;
  81        struct userdiff_driver *textconv_one = NULL;
  82        struct userdiff_driver *textconv_two = NULL;
  83        mmfile_t mf1, mf2;
  84        int hit;
  85
  86        if (!o->pickaxe[0])
  87                return 0;
  88
  89        if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
  90                textconv_one = get_textconv(p->one);
  91                textconv_two = get_textconv(p->two);
  92        }
  93
  94        if (textconv_one == textconv_two && diff_unmodified_pair(p))
  95                return 0;
  96
  97        mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
  98        mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
  99
 100        if (!DIFF_FILE_VALID(p->one)) {
 101                if (!DIFF_FILE_VALID(p->two))
 102                        hit = 0; /* ignore unmerged */
 103                else
 104                        /* created "two" -- does it have what we are looking for? */
 105                        hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
 106        } else if (!DIFF_FILE_VALID(p->two)) {
 107                /* removed "one" -- did it have what we are looking for? */
 108                hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
 109        } else {
 110                /*
 111                 * We have both sides; need to run textual diff and see if
 112                 * the pattern appears on added/deleted lines.
 113                 */
 114                struct diffgrep_cb ecbdata;
 115                xpparam_t xpp;
 116                xdemitconf_t xecfg;
 117
 118                memset(&xpp, 0, sizeof(xpp));
 119                memset(&xecfg, 0, sizeof(xecfg));
 120                ecbdata.regexp = regexp;
 121                ecbdata.hit = 0;
 122                xecfg.ctxlen = o->context;
 123                xecfg.interhunkctxlen = o->interhunkcontext;
 124                xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
 125                              &xpp, &xecfg);
 126                hit = ecbdata.hit;
 127        }
 128        if (textconv_one)
 129                free(mf1.ptr);
 130        if (textconv_two)
 131                free(mf2.ptr);
 132        diff_free_filespec_data(p->one);
 133        diff_free_filespec_data(p->two);
 134        return hit;
 135}
 136
 137static void diffcore_pickaxe_grep(struct diff_options *o)
 138{
 139        int err;
 140        regex_t regex;
 141        int cflags = REG_EXTENDED | REG_NEWLINE;
 142
 143        if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
 144                cflags |= REG_ICASE;
 145
 146        err = regcomp(&regex, o->pickaxe, cflags);
 147        if (err) {
 148                char errbuf[1024];
 149                regerror(err, &regex, errbuf, 1024);
 150                regfree(&regex);
 151                die("invalid log-grep regex: %s", errbuf);
 152        }
 153
 154        pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
 155
 156        regfree(&regex);
 157        return;
 158}
 159
 160static unsigned int contains(mmfile_t *mf, struct diff_options *o,
 161                             regex_t *regexp, kwset_t kws)
 162{
 163        unsigned int cnt;
 164        unsigned long sz;
 165        const char *data;
 166
 167        sz = mf->size;
 168        data = mf->ptr;
 169        cnt = 0;
 170
 171        if (regexp) {
 172                regmatch_t regmatch;
 173                int flags = 0;
 174
 175                assert(data[sz] == '\0');
 176                while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
 177                        flags |= REG_NOTBOL;
 178                        data += regmatch.rm_eo;
 179                        if (*data && regmatch.rm_so == regmatch.rm_eo)
 180                                data++;
 181                        cnt++;
 182                }
 183
 184        } else { /* Classic exact string match */
 185                while (sz) {
 186                        struct kwsmatch kwsm;
 187                        size_t offset = kwsexec(kws, data, sz, &kwsm);
 188                        const char *found;
 189                        if (offset == -1)
 190                                break;
 191                        else
 192                                found = data + offset;
 193                        sz -= found - data + kwsm.size[0];
 194                        data = found + kwsm.size[0];
 195                        cnt++;
 196                }
 197        }
 198        return cnt;
 199}
 200
 201static int has_changes(struct diff_filepair *p, struct diff_options *o,
 202                       regex_t *regexp, kwset_t kws)
 203{
 204        struct userdiff_driver *textconv_one = NULL;
 205        struct userdiff_driver *textconv_two = NULL;
 206        mmfile_t mf1, mf2;
 207        int ret;
 208
 209        if (!o->pickaxe[0])
 210                return 0;
 211
 212        if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
 213                textconv_one = get_textconv(p->one);
 214                textconv_two = get_textconv(p->two);
 215        }
 216
 217        /*
 218         * If we have an unmodified pair, we know that the count will be the
 219         * same and don't even have to load the blobs. Unless textconv is in
 220         * play, _and_ we are using two different textconv filters (e.g.,
 221         * because a pair is an exact rename with different textconv attributes
 222         * for each side, which might generate different content).
 223         */
 224        if (textconv_one == textconv_two && diff_unmodified_pair(p))
 225                return 0;
 226
 227        mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
 228        mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
 229
 230        if (!DIFF_FILE_VALID(p->one)) {
 231                if (!DIFF_FILE_VALID(p->two))
 232                        ret = 0; /* ignore unmerged */
 233                else
 234                        /* created */
 235                        ret = contains(&mf2, o, regexp, kws) != 0;
 236        }
 237        else if (!DIFF_FILE_VALID(p->two)) /* removed */
 238                ret = contains(&mf1, o, regexp, kws) != 0;
 239        else
 240                ret = contains(&mf1, o, regexp, kws) !=
 241                      contains(&mf2, o, regexp, kws);
 242
 243        if (textconv_one)
 244                free(mf1.ptr);
 245        if (textconv_two)
 246                free(mf2.ptr);
 247        diff_free_filespec_data(p->one);
 248        diff_free_filespec_data(p->two);
 249
 250        return ret;
 251}
 252
 253static void diffcore_pickaxe_count(struct diff_options *o)
 254{
 255        const char *needle = o->pickaxe;
 256        int opts = o->pickaxe_opts;
 257        unsigned long len = strlen(needle);
 258        regex_t regex, *regexp = NULL;
 259        kwset_t kws = NULL;
 260
 261        if (opts & DIFF_PICKAXE_REGEX) {
 262                int err;
 263                err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
 264                if (err) {
 265                        /* The POSIX.2 people are surely sick */
 266                        char errbuf[1024];
 267                        regerror(err, &regex, errbuf, 1024);
 268                        regfree(&regex);
 269                        die("invalid pickaxe regex: %s", errbuf);
 270                }
 271                regexp = &regex;
 272        } else {
 273                kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
 274                               ? tolower_trans_tbl : NULL);
 275                kwsincr(kws, needle, len);
 276                kwsprep(kws);
 277        }
 278
 279        pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
 280
 281        if (opts & DIFF_PICKAXE_REGEX)
 282                regfree(&regex);
 283        else
 284                kwsfree(kws);
 285        return;
 286}
 287
 288void diffcore_pickaxe(struct diff_options *o)
 289{
 290        /* Might want to warn when both S and G are on; I don't care... */
 291        if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
 292                diffcore_pickaxe_grep(o);
 293        else
 294                diffcore_pickaxe_count(o);
 295}