Code

pickaxe: give diff_grep the same signature as has_changes
[git.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"
11 struct diffgrep_cb {
12         regex_t *regexp;
13         int hit;
14 };
16 static void diffgrep_consume(void *priv, char *line, unsigned long len)
17 {
18         struct diffgrep_cb *data = priv;
19         regmatch_t regmatch;
20         int hold;
22         if (line[0] != '+' && line[0] != '-')
23                 return;
24         if (data->hit)
25                 /*
26                  * NEEDSWORK: we should have a way to terminate the
27                  * caller early.
28                  */
29                 return;
30         /* Yuck -- line ought to be "const char *"! */
31         hold = line[len];
32         line[len] = '\0';
33         data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
34         line[len] = hold;
35 }
37 static void fill_one(struct diff_filespec *one,
38                      mmfile_t *mf, struct userdiff_driver **textconv)
39 {
40         if (DIFF_FILE_VALID(one)) {
41                 *textconv = get_textconv(one);
42                 mf->size = fill_textconv(*textconv, one, &mf->ptr);
43         } else {
44                 memset(mf, 0, sizeof(*mf));
45         }
46 }
48 static int diff_grep(struct diff_filepair *p, struct diff_options *o,
49                      regex_t *regexp, kwset_t kws)
50 {
51         regmatch_t regmatch;
52         struct userdiff_driver *textconv_one = NULL;
53         struct userdiff_driver *textconv_two = NULL;
54         mmfile_t mf1, mf2;
55         int hit;
57         if (diff_unmodified_pair(p))
58                 return 0;
60         fill_one(p->one, &mf1, &textconv_one);
61         fill_one(p->two, &mf2, &textconv_two);
63         if (!mf1.ptr) {
64                 if (!mf2.ptr)
65                         return 0; /* ignore unmerged */
66                 /* created "two" -- does it have what we are looking for? */
67                 hit = !regexec(regexp, p->two->data, 1, &regmatch, 0);
68         } else if (!mf2.ptr) {
69                 /* removed "one" -- did it have what we are looking for? */
70                 hit = !regexec(regexp, p->one->data, 1, &regmatch, 0);
71         } else {
72                 /*
73                  * We have both sides; need to run textual diff and see if
74                  * the pattern appears on added/deleted lines.
75                  */
76                 struct diffgrep_cb ecbdata;
77                 xpparam_t xpp;
78                 xdemitconf_t xecfg;
80                 memset(&xpp, 0, sizeof(xpp));
81                 memset(&xecfg, 0, sizeof(xecfg));
82                 ecbdata.regexp = regexp;
83                 ecbdata.hit = 0;
84                 xecfg.ctxlen = o->context;
85                 xecfg.interhunkctxlen = o->interhunkcontext;
86                 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
87                               &xpp, &xecfg);
88                 hit = ecbdata.hit;
89         }
90         if (textconv_one)
91                 free(mf1.ptr);
92         if (textconv_two)
93                 free(mf2.ptr);
94         return hit;
95 }
97 static void diffcore_pickaxe_grep(struct diff_options *o)
98 {
99         struct diff_queue_struct *q = &diff_queued_diff;
100         int i, err;
101         regex_t regex;
102         struct diff_queue_struct outq;
103         outq.queue = NULL;
104         outq.nr = outq.alloc = 0;
106         err = regcomp(&regex, o->pickaxe, REG_EXTENDED | REG_NEWLINE);
107         if (err) {
108                 char errbuf[1024];
109                 regerror(err, &regex, errbuf, 1024);
110                 regfree(&regex);
111                 die("invalid log-grep regex: %s", errbuf);
112         }
114         if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
115                 /* Showing the whole changeset if needle exists */
116                 for (i = 0; i < q->nr; i++) {
117                         struct diff_filepair *p = q->queue[i];
118                         if (diff_grep(p, o, &regex, NULL))
119                                 goto out; /* do not munge the queue */
120                 }
122                 /*
123                  * Otherwise we will clear the whole queue by copying
124                  * the empty outq at the end of this function, but
125                  * first clear the current entries in the queue.
126                  */
127                 for (i = 0; i < q->nr; i++)
128                         diff_free_filepair(q->queue[i]);
129         } else {
130                 /* Showing only the filepairs that has the needle */
131                 for (i = 0; i < q->nr; i++) {
132                         struct diff_filepair *p = q->queue[i];
133                         if (diff_grep(p, o, &regex, NULL))
134                                 diff_q(&outq, p);
135                         else
136                                 diff_free_filepair(p);
137                 }
138         }
140         free(q->queue);
141         *q = outq;
143  out:
144         regfree(&regex);
145         return;
148 static unsigned int contains(struct diff_filespec *one, struct diff_options *o,
149                              regex_t *regexp, kwset_t kws)
151         unsigned int cnt;
152         unsigned long sz;
153         const char *data;
154         if (!o->pickaxe[0])
155                 return 0;
156         if (diff_populate_filespec(one, 0))
157                 return 0;
159         sz = one->size;
160         data = one->data;
161         cnt = 0;
163         if (regexp) {
164                 regmatch_t regmatch;
165                 int flags = 0;
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                 }
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         diff_free_filespec_data(one);
191         return cnt;
194 static int has_changes(struct diff_filepair *p, struct diff_options *o,
195                        regex_t *regexp, kwset_t kws)
197         if (!DIFF_FILE_VALID(p->one)) {
198                 if (!DIFF_FILE_VALID(p->two))
199                         return 0; /* ignore unmerged */
200                 /* created */
201                 return contains(p->two, o, regexp, kws) != 0;
202         }
203         if (!DIFF_FILE_VALID(p->two))
204                 return contains(p->one, o, regexp, kws) != 0;
205         if (!diff_unmodified_pair(p)) {
206                 return contains(p->one, o, regexp, kws) !=
207                        contains(p->two, o, regexp, kws);
208         }
209         return 0;
212 static void diffcore_pickaxe_count(struct diff_options *o)
214         const char *needle = o->pickaxe;
215         int opts = o->pickaxe_opts;
216         struct diff_queue_struct *q = &diff_queued_diff;
217         unsigned long len = strlen(needle);
218         int i;
219         regex_t regex, *regexp = NULL;
220         kwset_t kws = NULL;
221         struct diff_queue_struct outq;
222         DIFF_QUEUE_CLEAR(&outq);
224         if (opts & DIFF_PICKAXE_REGEX) {
225                 int err;
226                 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
227                 if (err) {
228                         /* The POSIX.2 people are surely sick */
229                         char errbuf[1024];
230                         regerror(err, &regex, errbuf, 1024);
231                         regfree(&regex);
232                         die("invalid pickaxe regex: %s", errbuf);
233                 }
234                 regexp = &regex;
235         } else {
236                 kws = kwsalloc(NULL);
237                 kwsincr(kws, needle, len);
238                 kwsprep(kws);
239         }
241         if (opts & DIFF_PICKAXE_ALL) {
242                 /* Showing the whole changeset if needle exists */
243                 for (i = 0; i < q->nr; i++) {
244                         struct diff_filepair *p = q->queue[i];
245                         if (has_changes(p, o, regexp, kws))
246                                 goto out; /* do not munge the queue */
247                 }
249                 /* otherwise we will clear the whole queue
250                  * by copying the empty outq at the end of this
251                  * function, but first clear the current entries
252                  * in the queue.
253                  */
254                 for (i = 0; i < q->nr; i++)
255                         diff_free_filepair(q->queue[i]);
256         }
257         else
258                 /* Showing only the filepairs that has the needle */
259                 for (i = 0; i < q->nr; i++) {
260                         struct diff_filepair *p = q->queue[i];
261                         if (has_changes(p, o, regexp, kws))
262                                 diff_q(&outq, p);
263                         else
264                                 diff_free_filepair(p);
265                 }
267         free(q->queue);
268         *q = outq;
270  out:
271         if (opts & DIFF_PICKAXE_REGEX)
272                 regfree(&regex);
273         else
274                 kwsfree(kws);
275         return;
278 void diffcore_pickaxe(struct diff_options *o)
280         /* Might want to warn when both S and G are on; I don't care... */
281         if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
282                 diffcore_pickaxe_grep(o);
283         else
284                 diffcore_pickaxe_count(o);