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