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