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