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" |
Nguyễn Thái Ngọc Duy | b51a9c1 | 2016-06-25 07:22:37 +0200 | [diff] [blame] | 10 | #include "commit.h" |
| 11 | #include "quote.h" |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 12 | |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 13 | typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two, |
| 14 | struct diff_options *o, |
| 15 | regex_t *regexp, kwset_t kws); |
| 16 | |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 17 | struct diffgrep_cb { |
| 18 | regex_t *regexp; |
| 19 | int hit; |
| 20 | }; |
| 21 | |
| 22 | static void diffgrep_consume(void *priv, char *line, unsigned long len) |
| 23 | { |
| 24 | struct diffgrep_cb *data = priv; |
| 25 | regmatch_t regmatch; |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 26 | |
| 27 | if (line[0] != '+' && line[0] != '-') |
| 28 | return; |
| 29 | if (data->hit) |
| 30 | /* |
| 31 | * NEEDSWORK: we should have a way to terminate the |
| 32 | * caller early. |
| 33 | */ |
| 34 | return; |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 35 | data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1, |
| 36 | ®match, 0); |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 39 | static int diff_grep(mmfile_t *one, mmfile_t *two, |
| 40 | struct diff_options *o, |
René Scharfe | db99cb7 | 2011-10-06 18:50:18 +0200 | [diff] [blame] | 41 | regex_t *regexp, kwset_t kws) |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 42 | { |
| 43 | regmatch_t regmatch; |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 44 | struct diffgrep_cb ecbdata; |
| 45 | xpparam_t xpp; |
| 46 | xdemitconf_t xecfg; |
Junio C Hamano | f506b8e | 2010-08-23 10:17:03 -0700 | [diff] [blame] | 47 | |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 48 | if (!one) |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 49 | return !regexec_buf(regexp, two->ptr, two->size, |
| 50 | 1, ®match, 0); |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 51 | if (!two) |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 52 | return !regexec_buf(regexp, one->ptr, one->size, |
| 53 | 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; |
Jeff King | 3efb988 | 2015-09-24 19:12:23 -0400 | [diff] [blame] | 65 | if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg)) |
| 66 | return 0; |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 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 | |
SZEDER Gábor | f53c5de | 2017-03-18 19:24:08 +0100 | [diff] [blame] | 84 | while (sz && *data && |
Johannes Schindelin | b7d36ff | 2016-09-21 20:24:14 +0200 | [diff] [blame] | 85 | !regexec_buf(regexp, data, sz, 1, ®match, flags)) { |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 86 | flags |= REG_NOTBOL; |
René Scharfe | 50fd699 | 2009-03-16 19:38:42 +0100 | [diff] [blame] | 87 | data += regmatch.rm_eo; |
SZEDER Gábor | f53c5de | 2017-03-18 19:24:08 +0100 | [diff] [blame] | 88 | sz -= regmatch.rm_eo; |
| 89 | if (sz && *data && regmatch.rm_so == regmatch.rm_eo) { |
René Scharfe | 50fd699 | 2009-03-16 19:38:42 +0100 | [diff] [blame] | 90 | data++; |
SZEDER Gábor | f53c5de | 2017-03-18 19:24:08 +0100 | [diff] [blame] | 91 | sz--; |
| 92 | } |
Junio C Hamano | 2002eed | 2005-07-23 16:35:25 -0700 | [diff] [blame] | 93 | cnt++; |
| 94 | } |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 95 | |
| 96 | } else { /* Classic exact string match */ |
René Scharfe | ce163c7 | 2009-03-03 00:00:55 +0100 | [diff] [blame] | 97 | while (sz) { |
René Scharfe | 5d176fb | 2011-10-06 18:50:06 +0200 | [diff] [blame] | 98 | struct kwsmatch kwsm; |
| 99 | size_t offset = kwsexec(kws, data, sz, &kwsm); |
Fredrik Kuivinen | b95c5ad | 2011-08-21 00:41:57 +0200 | [diff] [blame] | 100 | if (offset == -1) |
René Scharfe | ce163c7 | 2009-03-03 00:00:55 +0100 | [diff] [blame] | 101 | break; |
René Scharfe | e4aab50 | 2014-03-22 18:16:00 +0100 | [diff] [blame] | 102 | sz -= offset + kwsm.size[0]; |
| 103 | data += offset + kwsm.size[0]; |
René Scharfe | ce163c7 | 2009-03-03 00:00:55 +0100 | [diff] [blame] | 104 | cnt++; |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 105 | } |
Junio C Hamano | 2002eed | 2005-07-23 16:35:25 -0700 | [diff] [blame] | 106 | } |
| 107 | return cnt; |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 110 | static int has_changes(mmfile_t *one, mmfile_t *two, |
| 111 | struct diff_options *o, |
René Scharfe | 5d176fb | 2011-10-06 18:50:06 +0200 | [diff] [blame] | 112 | regex_t *regexp, kwset_t kws) |
René Scharfe | 15dafaf | 2011-10-06 18:26:24 +0200 | [diff] [blame] | 113 | { |
René Scharfe | 3bdb5b9 | 2013-07-06 15:53:27 +0200 | [diff] [blame] | 114 | unsigned int one_contains = one ? contains(one, regexp, kws) : 0; |
| 115 | unsigned int two_contains = two ? contains(two, regexp, kws) : 0; |
| 116 | return one_contains != two_contains; |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, |
| 120 | regex_t *regexp, kwset_t kws, pickaxe_fn fn) |
| 121 | { |
Simon Ruderich | bc61589 | 2013-04-04 22:20:29 +0200 | [diff] [blame] | 122 | struct userdiff_driver *textconv_one = NULL; |
| 123 | struct userdiff_driver *textconv_two = NULL; |
Jeff King | ef90ab6 | 2012-10-28 08:27:12 -0400 | [diff] [blame] | 124 | mmfile_t mf1, mf2; |
| 125 | int ret; |
| 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 | |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 131 | if (o->objfind) { |
| 132 | return (DIFF_FILE_VALID(p->one) && |
| 133 | oidset_contains(o->objfind, &p->one->oid)) || |
| 134 | (DIFF_FILE_VALID(p->two) && |
| 135 | oidset_contains(o->objfind, &p->two->oid)); |
| 136 | } |
| 137 | |
| 138 | if (!o->pickaxe[0]) |
| 139 | return 0; |
| 140 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 141 | if (o->flags.allow_textconv) { |
Simon Ruderich | a8f6109 | 2013-04-05 15:16:30 +0200 | [diff] [blame] | 142 | textconv_one = get_textconv(p->one); |
| 143 | textconv_two = get_textconv(p->two); |
| 144 | } |
Simon Ruderich | bc61589 | 2013-04-04 22:20:29 +0200 | [diff] [blame] | 145 | |
Jeff King | ef90ab6 | 2012-10-28 08:27:12 -0400 | [diff] [blame] | 146 | /* |
| 147 | * If we have an unmodified pair, we know that the count will be the |
| 148 | * same and don't even have to load the blobs. Unless textconv is in |
| 149 | * play, _and_ we are using two different textconv filters (e.g., |
| 150 | * because a pair is an exact rename with different textconv attributes |
| 151 | * for each side, which might generate different content). |
| 152 | */ |
| 153 | if (textconv_one == textconv_two && diff_unmodified_pair(p)) |
| 154 | return 0; |
| 155 | |
Jeff King | 7cdb9b4 | 2013-04-04 20:08:47 -0400 | [diff] [blame] | 156 | mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr); |
| 157 | mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr); |
Jeff King | ef90ab6 | 2012-10-28 08:27:12 -0400 | [diff] [blame] | 158 | |
Jeff King | 61690bf | 2013-04-05 01:28:10 -0400 | [diff] [blame] | 159 | ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL, |
| 160 | DIFF_FILE_VALID(p->two) ? &mf2 : NULL, |
| 161 | o, regexp, kws); |
Jeff King | ef90ab6 | 2012-10-28 08:27:12 -0400 | [diff] [blame] | 162 | |
| 163 | if (textconv_one) |
| 164 | free(mf1.ptr); |
| 165 | if (textconv_two) |
| 166 | free(mf2.ptr); |
| 167 | diff_free_filespec_data(p->one); |
| 168 | diff_free_filespec_data(p->two); |
| 169 | |
| 170 | return ret; |
René Scharfe | 15dafaf | 2011-10-06 18:26:24 +0200 | [diff] [blame] | 171 | } |
| 172 | |
René Scharfe | 3753bd1 | 2014-03-22 18:15:58 +0100 | [diff] [blame] | 173 | static void pickaxe(struct diff_queue_struct *q, struct diff_options *o, |
| 174 | regex_t *regexp, kwset_t kws, pickaxe_fn fn) |
| 175 | { |
| 176 | int i; |
| 177 | struct diff_queue_struct outq; |
| 178 | |
| 179 | DIFF_QUEUE_CLEAR(&outq); |
| 180 | |
| 181 | if (o->pickaxe_opts & DIFF_PICKAXE_ALL) { |
| 182 | /* Showing the whole changeset if needle exists */ |
| 183 | for (i = 0; i < q->nr; i++) { |
| 184 | struct diff_filepair *p = q->queue[i]; |
| 185 | if (pickaxe_match(p, o, regexp, kws, fn)) |
| 186 | return; /* do not munge the queue */ |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Otherwise we will clear the whole queue by copying |
| 191 | * the empty outq at the end of this function, but |
| 192 | * first clear the current entries in the queue. |
| 193 | */ |
| 194 | for (i = 0; i < q->nr; i++) |
| 195 | diff_free_filepair(q->queue[i]); |
| 196 | } else { |
| 197 | /* Showing only the filepairs that has the needle */ |
| 198 | for (i = 0; i < q->nr; i++) { |
| 199 | struct diff_filepair *p = q->queue[i]; |
| 200 | if (pickaxe_match(p, o, regexp, kws, fn)) |
| 201 | diff_q(&outq, p); |
| 202 | else |
| 203 | diff_free_filepair(p); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | free(q->queue); |
| 208 | *q = outq; |
| 209 | } |
| 210 | |
Nguyễn Thái Ngọc Duy | 3d5b23a | 2016-06-25 07:22:36 +0200 | [diff] [blame] | 211 | static void regcomp_or_die(regex_t *regex, const char *needle, int cflags) |
| 212 | { |
| 213 | int err = regcomp(regex, needle, cflags); |
| 214 | if (err) { |
| 215 | /* The POSIX.2 people are surely sick */ |
| 216 | char errbuf[1024]; |
| 217 | regerror(err, regex, errbuf, 1024); |
Nguyễn Thái Ngọc Duy | 3d5b23a | 2016-06-25 07:22:36 +0200 | [diff] [blame] | 218 | die("invalid regex: %s", errbuf); |
| 219 | } |
| 220 | } |
| 221 | |
René Scharfe | 63b52af | 2014-03-22 18:15:57 +0100 | [diff] [blame] | 222 | void diffcore_pickaxe(struct diff_options *o) |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 223 | { |
Junio C Hamano | 382f013 | 2010-08-31 13:44:39 -0700 | [diff] [blame] | 224 | const char *needle = o->pickaxe; |
| 225 | int opts = o->pickaxe_opts; |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 226 | regex_t regex, *regexp = NULL; |
Fredrik Kuivinen | b95c5ad | 2011-08-21 00:41:57 +0200 | [diff] [blame] | 227 | kwset_t kws = NULL; |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 228 | |
René Scharfe | 63b52af | 2014-03-22 18:15:57 +0100 | [diff] [blame] | 229 | if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) { |
René Scharfe | 218c45a | 2014-03-22 18:15:56 +0100 | [diff] [blame] | 230 | int cflags = REG_EXTENDED | REG_NEWLINE; |
Stefan Beller | c1ddc46 | 2018-01-04 14:50:40 -0800 | [diff] [blame] | 231 | if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE) |
René Scharfe | 218c45a | 2014-03-22 18:15:56 +0100 | [diff] [blame] | 232 | cflags |= REG_ICASE; |
Nguyễn Thái Ngọc Duy | 3d5b23a | 2016-06-25 07:22:36 +0200 | [diff] [blame] | 233 | regcomp_or_die(®ex, needle, cflags); |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 234 | regexp = ®ex; |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 235 | } else if (opts & DIFF_PICKAXE_KIND_S) { |
| 236 | if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE && |
| 237 | has_non_ascii(needle)) { |
| 238 | struct strbuf sb = STRBUF_INIT; |
| 239 | int cflags = REG_NEWLINE | REG_ICASE; |
Nguyễn Thái Ngọc Duy | b51a9c1 | 2016-06-25 07:22:37 +0200 | [diff] [blame] | 240 | |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 241 | basic_regex_quote_buf(&sb, needle); |
| 242 | regcomp_or_die(®ex, sb.buf, cflags); |
| 243 | strbuf_release(&sb); |
| 244 | regexp = ®ex; |
| 245 | } else { |
| 246 | kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE |
| 247 | ? tolower_trans_tbl : NULL); |
| 248 | kwsincr(kws, needle, strlen(needle)); |
| 249 | kwsprep(kws); |
| 250 | } |
Petr Baudis | d01d8c6 | 2006-03-29 02:16:33 +0200 | [diff] [blame] | 251 | } |
| 252 | |
René Scharfe | 63b52af | 2014-03-22 18:15:57 +0100 | [diff] [blame] | 253 | pickaxe(&diff_queued_diff, o, regexp, kws, |
| 254 | (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes); |
Junio C Hamano | 367cec1 | 2005-05-27 15:55:28 -0700 | [diff] [blame] | 255 | |
René Scharfe | 63b52af | 2014-03-22 18:15:57 +0100 | [diff] [blame] | 256 | if (regexp) |
| 257 | regfree(regexp); |
Stefan Beller | 15af58c | 2018-01-04 14:50:42 -0800 | [diff] [blame] | 258 | if (kws) |
Fredrik Kuivinen | b95c5ad | 2011-08-21 00:41:57 +0200 | [diff] [blame] | 259 | kwsfree(kws); |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 260 | return; |
| 261 | } |