]>
Commit | Line | Data |
---|---|---|
52e95789 JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
f506b8e8 | 3 | * Copyright (C) 2010 Google Inc. |
52e95789 JH |
4 | */ |
5 | #include "cache.h" | |
6 | #include "diff.h" | |
7 | #include "diffcore.h" | |
f506b8e8 | 8 | #include "xdiff-interface.h" |
b95c5ada | 9 | #include "kwset.h" |
b51a9c14 NTND |
10 | #include "commit.h" |
11 | #include "quote.h" | |
f506b8e8 | 12 | |
61690bf4 JK |
13 | typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two, |
14 | struct diff_options *o, | |
15 | regex_t *regexp, kwset_t kws); | |
16 | ||
f506b8e8 JH |
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; | |
f506b8e8 JH |
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; | |
b7d36ffc JS |
35 | data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1, |
36 | ®match, 0); | |
f506b8e8 JH |
37 | } |
38 | ||
61690bf4 JK |
39 | static int diff_grep(mmfile_t *one, mmfile_t *two, |
40 | struct diff_options *o, | |
db99cb70 | 41 | regex_t *regexp, kwset_t kws) |
f506b8e8 JH |
42 | { |
43 | regmatch_t regmatch; | |
61690bf4 JK |
44 | struct diffgrep_cb ecbdata; |
45 | xpparam_t xpp; | |
46 | xdemitconf_t xecfg; | |
bc615898 | 47 | |
61690bf4 | 48 | if (!one) |
b7d36ffc JS |
49 | return !regexec_buf(regexp, two->ptr, two->size, |
50 | 1, ®match, 0); | |
61690bf4 | 51 | if (!two) |
b7d36ffc JS |
52 | return !regexec_buf(regexp, one->ptr, one->size, |
53 | 1, ®match, 0); | |
f506b8e8 | 54 | |
61690bf4 JK |
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; | |
3efb9880 JK |
65 | if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg)) |
66 | return 0; | |
61690bf4 | 67 | return ecbdata.hit; |
f506b8e8 JH |
68 | } |
69 | ||
3bdb5b9f | 70 | static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) |
52e95789 | 71 | { |
2002eed6 | 72 | unsigned int cnt; |
ce163c79 | 73 | unsigned long sz; |
52e95789 | 74 | const char *data; |
2002eed6 | 75 | |
ef90ab66 JK |
76 | sz = mf->size; |
77 | data = mf->ptr; | |
2002eed6 JH |
78 | cnt = 0; |
79 | ||
d01d8c67 PB |
80 | if (regexp) { |
81 | regmatch_t regmatch; | |
82 | int flags = 0; | |
83 | ||
b7d36ffc JS |
84 | while (*data && |
85 | !regexec_buf(regexp, data, sz, 1, ®match, flags)) { | |
d01d8c67 | 86 | flags |= REG_NOTBOL; |
50fd6997 RS |
87 | data += regmatch.rm_eo; |
88 | if (*data && regmatch.rm_so == regmatch.rm_eo) | |
89 | data++; | |
2002eed6 JH |
90 | cnt++; |
91 | } | |
d01d8c67 PB |
92 | |
93 | } else { /* Classic exact string match */ | |
ce163c79 | 94 | while (sz) { |
5d176fb6 RS |
95 | struct kwsmatch kwsm; |
96 | size_t offset = kwsexec(kws, data, sz, &kwsm); | |
b95c5ada | 97 | if (offset == -1) |
ce163c79 | 98 | break; |
e4aab504 RS |
99 | sz -= offset + kwsm.size[0]; |
100 | data += offset + kwsm.size[0]; | |
ce163c79 | 101 | cnt++; |
d01d8c67 | 102 | } |
2002eed6 JH |
103 | } |
104 | return cnt; | |
52e95789 JH |
105 | } |
106 | ||
61690bf4 JK |
107 | static int has_changes(mmfile_t *one, mmfile_t *two, |
108 | struct diff_options *o, | |
5d176fb6 | 109 | regex_t *regexp, kwset_t kws) |
61690bf4 | 110 | { |
3bdb5b9f RS |
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; | |
61690bf4 JK |
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) | |
15dafaf8 | 118 | { |
bc615898 SR |
119 | struct userdiff_driver *textconv_one = NULL; |
120 | struct userdiff_driver *textconv_two = NULL; | |
ef90ab66 JK |
121 | mmfile_t mf1, mf2; |
122 | int ret; | |
123 | ||
8fa4b09f JK |
124 | if (!o->pickaxe[0]) |
125 | return 0; | |
126 | ||
61690bf4 JK |
127 | /* ignore unmerged */ |
128 | if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) | |
129 | return 0; | |
130 | ||
a8f61094 SR |
131 | if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) { |
132 | textconv_one = get_textconv(p->one); | |
133 | textconv_two = get_textconv(p->two); | |
134 | } | |
bc615898 | 135 | |
ef90ab66 JK |
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 | ||
7cdb9b42 JK |
146 | mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr); |
147 | mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr); | |
ef90ab66 | 148 | |
61690bf4 JK |
149 | ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL, |
150 | DIFF_FILE_VALID(p->two) ? &mf2 : NULL, | |
151 | o, regexp, kws); | |
ef90ab66 JK |
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; | |
15dafaf8 RS |
161 | } |
162 | ||
3753bd1f RS |
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 | ||
3d5b23a3 NTND |
201 | static void regcomp_or_die(regex_t *regex, const char *needle, int cflags) |
202 | { | |
203 | int err = regcomp(regex, needle, cflags); | |
204 | if (err) { | |
205 | /* The POSIX.2 people are surely sick */ | |
206 | char errbuf[1024]; | |
207 | regerror(err, regex, errbuf, 1024); | |
208 | regfree(regex); | |
209 | die("invalid regex: %s", errbuf); | |
210 | } | |
211 | } | |
212 | ||
63b52afa | 213 | void diffcore_pickaxe(struct diff_options *o) |
52e95789 | 214 | { |
382f013b JH |
215 | const char *needle = o->pickaxe; |
216 | int opts = o->pickaxe_opts; | |
d01d8c67 | 217 | regex_t regex, *regexp = NULL; |
b95c5ada | 218 | kwset_t kws = NULL; |
52e95789 | 219 | |
63b52afa | 220 | if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) { |
218c45a4 RS |
221 | int cflags = REG_EXTENDED | REG_NEWLINE; |
222 | if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)) | |
223 | cflags |= REG_ICASE; | |
3d5b23a3 | 224 | regcomp_or_die(®ex, needle, cflags); |
d01d8c67 | 225 | regexp = ®ex; |
b51a9c14 NTND |
226 | } else if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE) && |
227 | has_non_ascii(needle)) { | |
228 | struct strbuf sb = STRBUF_INIT; | |
229 | int cflags = REG_NEWLINE | REG_ICASE; | |
230 | ||
231 | basic_regex_quote_buf(&sb, needle); | |
232 | regcomp_or_die(®ex, sb.buf, cflags); | |
233 | strbuf_release(&sb); | |
234 | regexp = ®ex; | |
b95c5ada | 235 | } else { |
accccde4 JH |
236 | kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE) |
237 | ? tolower_trans_tbl : NULL); | |
542b2aa2 | 238 | kwsincr(kws, needle, strlen(needle)); |
b95c5ada | 239 | kwsprep(kws); |
d01d8c67 PB |
240 | } |
241 | ||
63b52afa RS |
242 | /* Might want to warn when both S and G are on; I don't care... */ |
243 | pickaxe(&diff_queued_diff, o, regexp, kws, | |
244 | (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes); | |
8e854b00 | 245 | |
63b52afa RS |
246 | if (regexp) |
247 | regfree(regexp); | |
b95c5ada FK |
248 | else |
249 | kwsfree(kws); | |
52e95789 JH |
250 | return; |
251 | } |