2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
11 typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
12 struct diff_options *o,
13 regex_t *regexp, kwset_t kws);
20 static void diffgrep_consume(void *priv, char *line, unsigned long len)
22 struct diffgrep_cb *data = priv;
25 if (line[0] != '+' && line[0] != '-')
29 * NEEDSWORK: we should have a way to terminate the
33 data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
37 static int diff_grep(mmfile_t *one, mmfile_t *two,
38 struct diff_options *o,
39 regex_t *regexp, kwset_t kws)
42 struct diffgrep_cb ecbdata;
47 return !regexec_buf(regexp, two->ptr, two->size,
50 return !regexec_buf(regexp, one->ptr, one->size,
54 * We have both sides; need to run textual diff and see if
55 * the pattern appears on added/deleted lines.
57 memset(&xpp, 0, sizeof(xpp));
58 memset(&xecfg, 0, sizeof(xecfg));
59 ecbdata.regexp = regexp;
61 xecfg.ctxlen = o->context;
62 xecfg.interhunkctxlen = o->interhunkcontext;
63 if (xdi_diff_outf(one, two, diffgrep_consume, &ecbdata, &xpp, &xecfg))
68 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
83 !regexec_buf(regexp, data, sz, 1, ®match, flags)) {
85 data += regmatch.rm_eo;
86 if (*data && regmatch.rm_so == regmatch.rm_eo)
91 } else { /* Classic exact string match */
94 size_t offset = kwsexec(kws, data, sz, &kwsm);
97 sz -= offset + kwsm.size[0];
98 data += offset + kwsm.size[0];
105 static int has_changes(mmfile_t *one, mmfile_t *two,
106 struct diff_options *o,
107 regex_t *regexp, kwset_t kws)
109 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
110 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
111 return one_contains != two_contains;
114 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
115 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
117 struct userdiff_driver *textconv_one = NULL;
118 struct userdiff_driver *textconv_two = NULL;
125 /* ignore unmerged */
126 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
129 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
130 textconv_one = get_textconv(p->one);
131 textconv_two = get_textconv(p->two);
135 * If we have an unmodified pair, we know that the count will be the
136 * same and don't even have to load the blobs. Unless textconv is in
137 * play, _and_ we are using two different textconv filters (e.g.,
138 * because a pair is an exact rename with different textconv attributes
139 * for each side, which might generate different content).
141 if (textconv_one == textconv_two && diff_unmodified_pair(p))
144 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
145 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
147 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
148 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
155 diff_free_filespec_data(p->one);
156 diff_free_filespec_data(p->two);
161 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
162 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
165 struct diff_queue_struct outq;
167 DIFF_QUEUE_CLEAR(&outq);
169 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
170 /* Showing the whole changeset if needle exists */
171 for (i = 0; i < q->nr; i++) {
172 struct diff_filepair *p = q->queue[i];
173 if (pickaxe_match(p, o, regexp, kws, fn))
174 return; /* do not munge the queue */
178 * Otherwise we will clear the whole queue by copying
179 * the empty outq at the end of this function, but
180 * first clear the current entries in the queue.
182 for (i = 0; i < q->nr; i++)
183 diff_free_filepair(q->queue[i]);
185 /* Showing only the filepairs that has the needle */
186 for (i = 0; i < q->nr; i++) {
187 struct diff_filepair *p = q->queue[i];
188 if (pickaxe_match(p, o, regexp, kws, fn))
191 diff_free_filepair(p);
199 void diffcore_pickaxe(struct diff_options *o)
201 const char *needle = o->pickaxe;
202 int opts = o->pickaxe_opts;
203 regex_t regex, *regexp = NULL;
206 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
208 int cflags = REG_EXTENDED | REG_NEWLINE;
209 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
211 err = regcomp(®ex, needle, cflags);
213 /* The POSIX.2 people are surely sick */
215 regerror(err, ®ex, errbuf, 1024);
217 die("invalid regex: %s", errbuf);
221 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
222 ? tolower_trans_tbl : NULL);
223 kwsincr(kws, needle, strlen(needle));
227 /* Might want to warn when both S and G are on; I don't care... */
228 pickaxe(&diff_queued_diff, o, regexp, kws,
229 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);