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