grep: un-break building with PCRE >= 8.32 without --enable-jit
[alt-git.git] / diffcore-pickaxe.c
blob341529b5a865ae6460f50d8593d52497059f2408
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"
13 typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
14 struct diff_options *o,
15 regex_t *regexp, kwset_t kws);
17 struct diffgrep_cb {
18 regex_t *regexp;
19 int hit;
22 static void diffgrep_consume(void *priv, char *line, unsigned long len)
24 struct diffgrep_cb *data = priv;
25 regmatch_t regmatch;
27 if (line[0] != '+' && line[0] != '-')
28 return;
29 if (data->hit)
31 * NEEDSWORK: we should have a way to terminate the
32 * caller early.
34 return;
35 data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
36 &regmatch, 0);
39 static int diff_grep(mmfile_t *one, mmfile_t *two,
40 struct diff_options *o,
41 regex_t *regexp, kwset_t kws)
43 regmatch_t regmatch;
44 struct diffgrep_cb ecbdata;
45 xpparam_t xpp;
46 xdemitconf_t xecfg;
48 if (!one)
49 return !regexec_buf(regexp, two->ptr, two->size,
50 1, &regmatch, 0);
51 if (!two)
52 return !regexec_buf(regexp, one->ptr, one->size,
53 1, &regmatch, 0);
56 * We have both sides; need to run textual diff and see if
57 * the pattern appears on added/deleted lines.
59 memset(&xpp, 0, sizeof(xpp));
60 memset(&xecfg, 0, sizeof(xecfg));
61 ecbdata.regexp = regexp;
62 ecbdata.hit = 0;
63 xecfg.ctxlen = o->context;
64 xecfg.interhunkctxlen = o->interhunkcontext;
65 if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg))
66 return 0;
67 return ecbdata.hit;
70 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
72 unsigned int cnt;
73 unsigned long sz;
74 const char *data;
76 sz = mf->size;
77 data = mf->ptr;
78 cnt = 0;
80 if (regexp) {
81 regmatch_t regmatch;
82 int flags = 0;
84 while (sz && *data &&
85 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
86 flags |= REG_NOTBOL;
87 data += regmatch.rm_eo;
88 sz -= regmatch.rm_eo;
89 if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
90 data++;
91 sz--;
93 cnt++;
96 } else { /* Classic exact string match */
97 while (sz) {
98 struct kwsmatch kwsm;
99 size_t offset = kwsexec(kws, data, sz, &kwsm);
100 if (offset == -1)
101 break;
102 sz -= offset + kwsm.size[0];
103 data += offset + kwsm.size[0];
104 cnt++;
107 return cnt;
110 static int has_changes(mmfile_t *one, mmfile_t *two,
111 struct diff_options *o,
112 regex_t *regexp, kwset_t kws)
114 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
115 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
116 return one_contains != two_contains;
119 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
120 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
122 struct userdiff_driver *textconv_one = NULL;
123 struct userdiff_driver *textconv_two = NULL;
124 mmfile_t mf1, mf2;
125 int ret;
127 if (!o->pickaxe[0])
128 return 0;
130 /* ignore unmerged */
131 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
132 return 0;
134 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
135 textconv_one = get_textconv(p->one);
136 textconv_two = get_textconv(p->two);
140 * If we have an unmodified pair, we know that the count will be the
141 * same and don't even have to load the blobs. Unless textconv is in
142 * play, _and_ we are using two different textconv filters (e.g.,
143 * because a pair is an exact rename with different textconv attributes
144 * for each side, which might generate different content).
146 if (textconv_one == textconv_two && diff_unmodified_pair(p))
147 return 0;
149 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
150 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
152 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
153 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
154 o, regexp, kws);
156 if (textconv_one)
157 free(mf1.ptr);
158 if (textconv_two)
159 free(mf2.ptr);
160 diff_free_filespec_data(p->one);
161 diff_free_filespec_data(p->two);
163 return ret;
166 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
167 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
169 int i;
170 struct diff_queue_struct outq;
172 DIFF_QUEUE_CLEAR(&outq);
174 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
175 /* Showing the whole changeset if needle exists */
176 for (i = 0; i < q->nr; i++) {
177 struct diff_filepair *p = q->queue[i];
178 if (pickaxe_match(p, o, regexp, kws, fn))
179 return; /* do not munge the queue */
183 * Otherwise we will clear the whole queue by copying
184 * the empty outq at the end of this function, but
185 * first clear the current entries in the queue.
187 for (i = 0; i < q->nr; i++)
188 diff_free_filepair(q->queue[i]);
189 } else {
190 /* Showing only the filepairs that has the needle */
191 for (i = 0; i < q->nr; i++) {
192 struct diff_filepair *p = q->queue[i];
193 if (pickaxe_match(p, o, regexp, kws, fn))
194 diff_q(&outq, p);
195 else
196 diff_free_filepair(p);
200 free(q->queue);
201 *q = outq;
204 static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
206 int err = regcomp(regex, needle, cflags);
207 if (err) {
208 /* The POSIX.2 people are surely sick */
209 char errbuf[1024];
210 regerror(err, regex, errbuf, 1024);
211 regfree(regex);
212 die("invalid regex: %s", errbuf);
216 void diffcore_pickaxe(struct diff_options *o)
218 const char *needle = o->pickaxe;
219 int opts = o->pickaxe_opts;
220 regex_t regex, *regexp = NULL;
221 kwset_t kws = NULL;
223 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
224 int cflags = REG_EXTENDED | REG_NEWLINE;
225 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
226 cflags |= REG_ICASE;
227 regcomp_or_die(&regex, needle, cflags);
228 regexp = &regex;
229 } else if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE) &&
230 has_non_ascii(needle)) {
231 struct strbuf sb = STRBUF_INIT;
232 int cflags = REG_NEWLINE | REG_ICASE;
234 basic_regex_quote_buf(&sb, needle);
235 regcomp_or_die(&regex, sb.buf, cflags);
236 strbuf_release(&sb);
237 regexp = &regex;
238 } else {
239 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
240 ? tolower_trans_tbl : NULL);
241 kwsincr(kws, needle, strlen(needle));
242 kwsprep(kws);
245 /* Might want to warn when both S and G are on; I don't care... */
246 pickaxe(&diff_queued_diff, o, regexp, kws,
247 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
249 if (regexp)
250 regfree(regexp);
251 else
252 kwsfree(kws);
253 return;