diffcore-pickaxe: remove unnecessary call to get_textconv()
[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 void fill_one(struct diff_filespec *one,
78                      mmfile_t *mf, struct userdiff_driver *textconv)
79 {
80         if (DIFF_FILE_VALID(one)) {
81                 mf->size = fill_textconv(textconv, one, &mf->ptr);
82         } else {
83                 memset(mf, 0, sizeof(*mf));
84         }
85 }
86
87 static int diff_grep(struct diff_filepair *p, struct diff_options *o,
88                      regex_t *regexp, kwset_t kws)
89 {
90         regmatch_t regmatch;
91         struct userdiff_driver *textconv_one = NULL;
92         struct userdiff_driver *textconv_two = NULL;
93         mmfile_t mf1, mf2;
94         int hit;
95
96         if (diff_unmodified_pair(p))
97                 return 0;
98
99         textconv_one = get_textconv(p->one);
100         textconv_two = get_textconv(p->two);
101
102         fill_one(p->one, &mf1, textconv_one);
103         fill_one(p->two, &mf2, textconv_two);
104
105         if (!mf1.ptr) {
106                 if (!mf2.ptr)
107                         return 0; /* ignore unmerged */
108                 /* created "two" -- does it have what we are looking for? */
109                 hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
110         } else if (!mf2.ptr) {
111                 /* removed "one" -- did it have what we are looking for? */
112                 hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
113         } else {
114                 /*
115                  * We have both sides; need to run textual diff and see if
116                  * the pattern appears on added/deleted lines.
117                  */
118                 struct diffgrep_cb ecbdata;
119                 xpparam_t xpp;
120                 xdemitconf_t xecfg;
121
122                 memset(&xpp, 0, sizeof(xpp));
123                 memset(&xecfg, 0, sizeof(xecfg));
124                 ecbdata.regexp = regexp;
125                 ecbdata.hit = 0;
126                 xecfg.ctxlen = o->context;
127                 xecfg.interhunkctxlen = o->interhunkcontext;
128                 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
129                               &xpp, &xecfg);
130                 hit = ecbdata.hit;
131         }
132         if (textconv_one)
133                 free(mf1.ptr);
134         if (textconv_two)
135                 free(mf2.ptr);
136         return hit;
137 }
138
139 static void diffcore_pickaxe_grep(struct diff_options *o)
140 {
141         int err;
142         regex_t regex;
143         int cflags = REG_EXTENDED | REG_NEWLINE;
144
145         if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
146                 cflags |= REG_ICASE;
147
148         err = regcomp(&regex, o->pickaxe, cflags);
149         if (err) {
150                 char errbuf[1024];
151                 regerror(err, &regex, errbuf, 1024);
152                 regfree(&regex);
153                 die("invalid log-grep regex: %s", errbuf);
154         }
155
156         pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
157
158         regfree(&regex);
159         return;
160 }
161
162 static unsigned int contains(mmfile_t *mf, struct diff_options *o,
163                              regex_t *regexp, kwset_t kws)
164 {
165         unsigned int cnt;
166         unsigned long sz;
167         const char *data;
168
169         sz = mf->size;
170         data = mf->ptr;
171         cnt = 0;
172
173         if (regexp) {
174                 regmatch_t regmatch;
175                 int flags = 0;
176
177                 assert(data[sz] == '\0');
178                 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
179                         flags |= REG_NOTBOL;
180                         data += regmatch.rm_eo;
181                         if (*data && regmatch.rm_so == regmatch.rm_eo)
182                                 data++;
183                         cnt++;
184                 }
185
186         } else { /* Classic exact string match */
187                 while (sz) {
188                         struct kwsmatch kwsm;
189                         size_t offset = kwsexec(kws, data, sz, &kwsm);
190                         const char *found;
191                         if (offset == -1)
192                                 break;
193                         else
194                                 found = data + offset;
195                         sz -= found - data + kwsm.size[0];
196                         data = found + kwsm.size[0];
197                         cnt++;
198                 }
199         }
200         return cnt;
201 }
202
203 static int has_changes(struct diff_filepair *p, struct diff_options *o,
204                        regex_t *regexp, kwset_t kws)
205 {
206         struct userdiff_driver *textconv_one = NULL;
207         struct userdiff_driver *textconv_two = NULL;
208         mmfile_t mf1, mf2;
209         int ret;
210
211         if (!o->pickaxe[0])
212                 return 0;
213
214         textconv_one = get_textconv(p->one);
215         textconv_two = get_textconv(p->two);
216
217         /*
218          * If we have an unmodified pair, we know that the count will be the
219          * same and don't even have to load the blobs. Unless textconv is in
220          * play, _and_ we are using two different textconv filters (e.g.,
221          * because a pair is an exact rename with different textconv attributes
222          * for each side, which might generate different content).
223          */
224         if (textconv_one == textconv_two && diff_unmodified_pair(p))
225                 return 0;
226
227         fill_one(p->one, &mf1, textconv_one);
228         fill_one(p->two, &mf2, textconv_two);
229
230         if (!mf1.ptr) {
231                 if (!mf2.ptr)
232                         ret = 0; /* ignore unmerged */
233                 /* created */
234                 ret = contains(&mf2, o, regexp, kws) != 0;
235         }
236         else if (!mf2.ptr) /* removed */
237                 ret = contains(&mf1, o, regexp, kws) != 0;
238         else
239                 ret = contains(&mf1, o, regexp, kws) !=
240                       contains(&mf2, o, regexp, kws);
241
242         if (textconv_one)
243                 free(mf1.ptr);
244         if (textconv_two)
245                 free(mf2.ptr);
246         diff_free_filespec_data(p->one);
247         diff_free_filespec_data(p->two);
248
249         return ret;
250 }
251
252 static void diffcore_pickaxe_count(struct diff_options *o)
253 {
254         const char *needle = o->pickaxe;
255         int opts = o->pickaxe_opts;
256         unsigned long len = strlen(needle);
257         regex_t regex, *regexp = NULL;
258         kwset_t kws = NULL;
259
260         if (opts & DIFF_PICKAXE_REGEX) {
261                 int err;
262                 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
263                 if (err) {
264                         /* The POSIX.2 people are surely sick */
265                         char errbuf[1024];
266                         regerror(err, &regex, errbuf, 1024);
267                         regfree(&regex);
268                         die("invalid pickaxe regex: %s", errbuf);
269                 }
270                 regexp = &regex;
271         } else {
272                 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
273                                ? tolower_trans_tbl : NULL);
274                 kwsincr(kws, needle, len);
275                 kwsprep(kws);
276         }
277
278         pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
279
280         if (opts & DIFF_PICKAXE_REGEX)
281                 regfree(&regex);
282         else
283                 kwsfree(kws);
284         return;
285 }
286
287 void diffcore_pickaxe(struct diff_options *o)
288 {
289         /* Might want to warn when both S and G are on; I don't care... */
290         if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
291                 diffcore_pickaxe_grep(o);
292         else
293                 diffcore_pickaxe_count(o);
294 }