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