2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
13 typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
14 struct diff_options *o,
15 regex_t *regexp, kwset_t kws);
22 static int diffgrep_consume(void *priv, char *line, unsigned long len)
24 struct diffgrep_cb *data = priv;
27 if (line[0] != '+' && line[0] != '-')
30 BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
31 if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
39 static int diff_grep(mmfile_t *one, mmfile_t *two,
40 struct diff_options *o,
41 regex_t *regexp, kwset_t kws)
43 struct diffgrep_cb ecbdata;
49 * We have both sides; need to run textual diff and see if
50 * the pattern appears on added/deleted lines.
52 memset(&xpp, 0, sizeof(xpp));
53 memset(&xecfg, 0, sizeof(xecfg));
54 ecbdata.regexp = regexp;
56 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
57 xecfg.ctxlen = o->context;
58 xecfg.interhunkctxlen = o->interhunkcontext;
61 * An xdiff error might be our "data->hit" from above. See the
62 * comment for xdiff_emit_line_fn in xdiff-interface.h
64 ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
65 &ecbdata, &xpp, &xecfg);
73 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
77 unsigned long sz = mf->size;
78 const char *data = mf->ptr;
85 !regexec_buf(regexp, data, sz, 1, ®match, flags)) {
87 data += regmatch.rm_eo;
89 if (sz && regmatch.rm_so == regmatch.rm_eo) {
95 if (limit && cnt == limit)
99 } else { /* Classic exact string match */
101 struct kwsmatch kwsm;
102 size_t offset = kwsexec(kws, data, sz, &kwsm);
105 sz -= offset + kwsm.size[0];
106 data += offset + kwsm.size[0];
109 if (limit && cnt == limit)
116 static int has_changes(mmfile_t *one, mmfile_t *two,
117 struct diff_options *o,
118 regex_t *regexp, kwset_t kws)
120 unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
121 unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
125 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
126 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
128 struct userdiff_driver *textconv_one = NULL;
129 struct userdiff_driver *textconv_two = NULL;
133 /* ignore unmerged */
134 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
138 return (DIFF_FILE_VALID(p->one) &&
139 oidset_contains(o->objfind, &p->one->oid)) ||
140 (DIFF_FILE_VALID(p->two) &&
141 oidset_contains(o->objfind, &p->two->oid));
144 if (o->flags.allow_textconv) {
145 textconv_one = get_textconv(o->repo, p->one);
146 textconv_two = get_textconv(o->repo, p->two);
150 * If we have an unmodified pair, we know that the count will be the
151 * same and don't even have to load the blobs. Unless textconv is in
152 * play, _and_ we are using two different textconv filters (e.g.,
153 * because a pair is an exact rename with different textconv attributes
154 * for each side, which might generate different content).
156 if (textconv_one == textconv_two && diff_unmodified_pair(p))
159 if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
161 ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
162 (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
165 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
166 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
168 ret = fn(&mf1, &mf2, o, regexp, kws);
174 diff_free_filespec_data(p->one);
175 diff_free_filespec_data(p->two);
180 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
181 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
184 struct diff_queue_struct outq;
186 DIFF_QUEUE_CLEAR(&outq);
188 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
189 /* Showing the whole changeset if needle exists */
190 for (i = 0; i < q->nr; i++) {
191 struct diff_filepair *p = q->queue[i];
192 if (pickaxe_match(p, o, regexp, kws, fn))
193 return; /* do not munge the queue */
197 * Otherwise we will clear the whole queue by copying
198 * the empty outq at the end of this function, but
199 * first clear the current entries in the queue.
201 for (i = 0; i < q->nr; i++)
202 diff_free_filepair(q->queue[i]);
204 /* Showing only the filepairs that has the needle */
205 for (i = 0; i < q->nr; i++) {
206 struct diff_filepair *p = q->queue[i];
207 if (pickaxe_match(p, o, regexp, kws, fn))
210 diff_free_filepair(p);
218 static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
220 int err = regcomp(regex, needle, cflags);
222 /* The POSIX.2 people are surely sick */
224 regerror(err, regex, errbuf, 1024);
225 die("invalid regex: %s", errbuf);
229 void diffcore_pickaxe(struct diff_options *o)
231 const char *needle = o->pickaxe;
232 int opts = o->pickaxe_opts;
233 regex_t regex, *regexp = NULL;
237 if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
238 (!needle || !*needle))
239 BUG("should have needle under -G or -S");
240 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
241 int cflags = REG_EXTENDED | REG_NEWLINE;
242 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
244 regcomp_or_die(®ex, needle, cflags);
247 if (opts & DIFF_PICKAXE_KIND_G)
249 else if (opts & DIFF_PICKAXE_REGEX)
253 * We don't need to check the combination of
254 * -G and --pickaxe-regex, by the time we get
255 * here diff.c has already died if they're
256 * combined. See the usage tests in
257 * t4209-log-pickaxe.sh.
260 } else if (opts & DIFF_PICKAXE_KIND_S) {
261 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
262 has_non_ascii(needle)) {
263 struct strbuf sb = STRBUF_INIT;
264 int cflags = REG_NEWLINE | REG_ICASE;
266 basic_regex_quote_buf(&sb, needle);
267 regcomp_or_die(®ex, sb.buf, cflags);
271 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
272 ? tolower_trans_tbl : NULL);
273 kwsincr(kws, needle, strlen(needle));
277 } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
280 BUG("unknown pickaxe_opts flag");
283 pickaxe(&diff_queued_diff, o, regexp, kws, fn);