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