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