]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-pickaxe.c
pickaxe: assert that we must have a needle under -G or -S
[thirdparty/git.git] / diffcore-pickaxe.c
CommitLineData
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
13typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
14 struct diff_options *o,
15 regex_t *regexp, kwset_t kws);
16
f506b8e8
JH
17struct diffgrep_cb {
18 regex_t *regexp;
19 int hit;
20};
21
22static 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 &regmatch, 0);
f506b8e8
JH
37}
38
61690bf4
JK
39static 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, &regmatch, 0);
61690bf4 51 if (!two)
b7d36ffc
JS
52 return !regexec_buf(regexp, one->ptr, one->size,
53 1, &regmatch, 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;
3b40a090
JK
65 if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
66 &ecbdata, &xpp, &xecfg))
3efb9880 67 return 0;
61690bf4 68 return ecbdata.hit;
f506b8e8
JH
69}
70
3bdb5b9f 71static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
52e95789 72{
102fdd2e
ÆAB
73 unsigned int cnt = 0;
74 unsigned long sz = mf->size;
75 const char *data = mf->ptr;
2002eed6 76
d01d8c67
PB
77 if (regexp) {
78 regmatch_t regmatch;
79 int flags = 0;
80
f53c5de2 81 while (sz && *data &&
b7d36ffc 82 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
d01d8c67 83 flags |= REG_NOTBOL;
50fd6997 84 data += regmatch.rm_eo;
f53c5de2
SG
85 sz -= regmatch.rm_eo;
86 if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
50fd6997 87 data++;
f53c5de2
SG
88 sz--;
89 }
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
107static 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
116static 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
61690bf4
JK
124 /* ignore unmerged */
125 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
126 return 0;
127
15af58c1
SB
128 if (o->objfind) {
129 return (DIFF_FILE_VALID(p->one) &&
130 oidset_contains(o->objfind, &p->one->oid)) ||
131 (DIFF_FILE_VALID(p->two) &&
132 oidset_contains(o->objfind, &p->two->oid));
133 }
134
0d1e0e78 135 if (o->flags.allow_textconv) {
bd7ad45b
NTND
136 textconv_one = get_textconv(o->repo, p->one);
137 textconv_two = get_textconv(o->repo, p->two);
a8f61094 138 }
bc615898 139
ef90ab66
JK
140 /*
141 * If we have an unmodified pair, we know that the count will be the
142 * same and don't even have to load the blobs. Unless textconv is in
143 * play, _and_ we are using two different textconv filters (e.g.,
144 * because a pair is an exact rename with different textconv attributes
145 * for each side, which might generate different content).
146 */
147 if (textconv_one == textconv_two && diff_unmodified_pair(p))
148 return 0;
149
e0e7cb80
TB
150 if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
151 !o->flags.text &&
152 ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
153 (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
154 return 0;
155
6afaf807
NTND
156 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
157 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
ef90ab66 158
61690bf4
JK
159 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
160 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
161 o, regexp, kws);
ef90ab66
JK
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;
15dafaf8
RS
171}
172
3753bd1f
RS
173static 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
3d5b23a3
NTND
211static 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);
3d5b23a3
NTND
218 die("invalid regex: %s", errbuf);
219 }
220}
221
63b52afa 222void diffcore_pickaxe(struct diff_options *o)
52e95789 223{
382f013b
JH
224 const char *needle = o->pickaxe;
225 int opts = o->pickaxe_opts;
d01d8c67 226 regex_t regex, *regexp = NULL;
b95c5ada 227 kwset_t kws = NULL;
03c1f14a 228 pickaxe_fn fn;
52e95789 229
2e197a75
ÆAB
230 if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
231 (!needle || !*needle))
232 BUG("should have needle under -G or -S");
63b52afa 233 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
218c45a4 234 int cflags = REG_EXTENDED | REG_NEWLINE;
c1ddc461 235 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
218c45a4 236 cflags |= REG_ICASE;
3d5b23a3 237 regcomp_or_die(&regex, needle, cflags);
d01d8c67 238 regexp = &regex;
03c1f14a
ÆAB
239
240 if (opts & DIFF_PICKAXE_KIND_G)
241 fn = diff_grep;
242 else if (opts & DIFF_PICKAXE_REGEX)
243 fn = has_changes;
244 else
245 /*
246 * We don't need to check the combination of
247 * -G and --pickaxe-regex, by the time we get
248 * here diff.c has already died if they're
249 * combined. See the usage tests in
250 * t4209-log-pickaxe.sh.
251 */
252 BUG("unreachable");
15af58c1
SB
253 } else if (opts & DIFF_PICKAXE_KIND_S) {
254 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
255 has_non_ascii(needle)) {
256 struct strbuf sb = STRBUF_INIT;
257 int cflags = REG_NEWLINE | REG_ICASE;
258
259 basic_regex_quote_buf(&sb, needle);
260 regcomp_or_die(&regex, sb.buf, cflags);
261 strbuf_release(&sb);
262 regexp = &regex;
263 } else {
264 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
265 ? tolower_trans_tbl : NULL);
266 kwsincr(kws, needle, strlen(needle));
267 kwsprep(kws);
268 }
03c1f14a
ÆAB
269 fn = has_changes;
270 } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
271 fn = NULL;
272 } else {
273 BUG("unknown pickaxe_opts flag");
d01d8c67
PB
274 }
275
03c1f14a 276 pickaxe(&diff_queued_diff, o, regexp, kws, fn);
8e854b00 277
63b52afa
RS
278 if (regexp)
279 regfree(regexp);
15af58c1 280 if (kws)
b95c5ada 281 kwsfree(kws);
52e95789
JH
282 return;
283}