]>
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; | |
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, ®match, 0); | |
40 | line[len] = hold; | |
41 | } | |
42 | ||
61690bf4 JK |
43 | static int diff_grep(mmfile_t *one, mmfile_t *two, |
44 | struct diff_options *o, | |
db99cb70 | 45 | regex_t *regexp, kwset_t kws) |
f506b8e8 JH |
46 | { |
47 | regmatch_t regmatch; | |
61690bf4 JK |
48 | struct diffgrep_cb ecbdata; |
49 | xpparam_t xpp; | |
50 | xdemitconf_t xecfg; | |
bc615898 | 51 | |
61690bf4 JK |
52 | if (!one) |
53 | return !regexec(regexp, two->ptr, 1, ®match, 0); | |
54 | if (!two) | |
55 | return !regexec(regexp, one->ptr, 1, ®match, 0); | |
f506b8e8 | 56 | |
61690bf4 JK |
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; | |
3efb9880 JK |
67 | if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg)) |
68 | return 0; | |
61690bf4 | 69 | return ecbdata.hit; |
f506b8e8 JH |
70 | } |
71 | ||
3bdb5b9f | 72 | static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws) |
52e95789 | 73 | { |
2002eed6 | 74 | unsigned int cnt; |
ce163c79 | 75 | unsigned long sz; |
52e95789 | 76 | const char *data; |
2002eed6 | 77 | |
ef90ab66 JK |
78 | sz = mf->size; |
79 | data = mf->ptr; | |
2002eed6 JH |
80 | cnt = 0; |
81 | ||
d01d8c67 PB |
82 | if (regexp) { |
83 | regmatch_t regmatch; | |
84 | int flags = 0; | |
85 | ||
50fd6997 | 86 | assert(data[sz] == '\0'); |
d01d8c67 PB |
87 | while (*data && !regexec(regexp, data, 1, ®match, flags)) { |
88 | flags |= REG_NOTBOL; | |
50fd6997 RS |
89 | data += regmatch.rm_eo; |
90 | if (*data && regmatch.rm_so == regmatch.rm_eo) | |
91 | data++; | |
2002eed6 JH |
92 | cnt++; |
93 | } | |
d01d8c67 PB |
94 | |
95 | } else { /* Classic exact string match */ | |
ce163c79 | 96 | while (sz) { |
5d176fb6 RS |
97 | struct kwsmatch kwsm; |
98 | size_t offset = kwsexec(kws, data, sz, &kwsm); | |
b95c5ada | 99 | if (offset == -1) |
ce163c79 | 100 | break; |
e4aab504 RS |
101 | sz -= offset + kwsm.size[0]; |
102 | data += offset + kwsm.size[0]; | |
ce163c79 | 103 | cnt++; |
d01d8c67 | 104 | } |
2002eed6 JH |
105 | } |
106 | return cnt; | |
52e95789 JH |
107 | } |
108 | ||
61690bf4 JK |
109 | static int has_changes(mmfile_t *one, mmfile_t *two, |
110 | struct diff_options *o, | |
5d176fb6 | 111 | regex_t *regexp, kwset_t kws) |
61690bf4 | 112 | { |
3bdb5b9f RS |
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; | |
61690bf4 JK |
116 | } |
117 | ||
118 | static int pickaxe_match(struct diff_filepair *p, struct diff_options *o, | |
119 | regex_t *regexp, kwset_t kws, pickaxe_fn fn) | |
15dafaf8 | 120 | { |
bc615898 SR |
121 | struct userdiff_driver *textconv_one = NULL; |
122 | struct userdiff_driver *textconv_two = NULL; | |
ef90ab66 JK |
123 | mmfile_t mf1, mf2; |
124 | int ret; | |
125 | ||
8fa4b09f JK |
126 | if (!o->pickaxe[0]) |
127 | return 0; | |
128 | ||
61690bf4 JK |
129 | /* ignore unmerged */ |
130 | if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) | |
131 | return 0; | |
132 | ||
a8f61094 SR |
133 | if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) { |
134 | textconv_one = get_textconv(p->one); | |
135 | textconv_two = get_textconv(p->two); | |
136 | } | |
bc615898 | 137 | |
ef90ab66 JK |
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 | ||
7cdb9b42 JK |
148 | mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr); |
149 | mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr); | |
ef90ab66 | 150 | |
61690bf4 JK |
151 | ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL, |
152 | DIFF_FILE_VALID(p->two) ? &mf2 : NULL, | |
153 | o, regexp, kws); | |
ef90ab66 JK |
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; | |
15dafaf8 RS |
163 | } |
164 | ||
3753bd1f RS |
165 | static 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 | ||
3d5b23a3 NTND |
203 | static 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 | ||
63b52afa | 215 | void diffcore_pickaxe(struct diff_options *o) |
52e95789 | 216 | { |
382f013b JH |
217 | const char *needle = o->pickaxe; |
218 | int opts = o->pickaxe_opts; | |
d01d8c67 | 219 | regex_t regex, *regexp = NULL; |
b95c5ada | 220 | kwset_t kws = NULL; |
52e95789 | 221 | |
63b52afa | 222 | if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) { |
218c45a4 RS |
223 | int cflags = REG_EXTENDED | REG_NEWLINE; |
224 | if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)) | |
225 | cflags |= REG_ICASE; | |
3d5b23a3 | 226 | regcomp_or_die(®ex, needle, cflags); |
d01d8c67 | 227 | regexp = ®ex; |
b51a9c14 NTND |
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(®ex, sb.buf, cflags); | |
235 | strbuf_release(&sb); | |
236 | regexp = ®ex; | |
b95c5ada | 237 | } else { |
accccde4 JH |
238 | kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE) |
239 | ? tolower_trans_tbl : NULL); | |
542b2aa2 | 240 | kwsincr(kws, needle, strlen(needle)); |
b95c5ada | 241 | kwsprep(kws); |
d01d8c67 PB |
242 | } |
243 | ||
63b52afa RS |
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); | |
8e854b00 | 247 | |
63b52afa RS |
248 | if (regexp) |
249 | regfree(regexp); | |
b95c5ada FK |
250 | else |
251 | kwsfree(kws); | |
52e95789 JH |
252 | return; |
253 | } |