Sync with 'maint'
[alt-git.git] / diffcore-pickaxe.c
blob43fef8e8ba4e8273bcd664ce60d418e092b7931c
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
4 */
5 #include "git-compat-util.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "xdiff-interface.h"
9 #include "kwset.h"
10 #include "oidset.h"
11 #include "pretty.h"
12 #include "quote.h"
14 typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
15 struct diff_options *o,
16 regex_t *regexp, kwset_t kws);
18 struct diffgrep_cb {
19 regex_t *regexp;
20 int hit;
23 static int diffgrep_consume(void *priv, char *line, unsigned long len)
25 struct diffgrep_cb *data = priv;
26 regmatch_t regmatch;
28 if (line[0] != '+' && line[0] != '-')
29 return 0;
30 if (data->hit)
31 BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
32 if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
33 &regmatch, 0)) {
34 data->hit = 1;
35 return 1;
37 return 0;
40 static int diff_grep(mmfile_t *one, mmfile_t *two,
41 struct diff_options *o,
42 regex_t *regexp, kwset_t kws UNUSED)
44 struct diffgrep_cb ecbdata;
45 xpparam_t xpp;
46 xdemitconf_t xecfg;
47 int ret;
50 * We have both sides; need to run textual diff and see if
51 * the pattern appears on added/deleted lines.
53 memset(&xpp, 0, sizeof(xpp));
54 memset(&xecfg, 0, sizeof(xecfg));
55 ecbdata.regexp = regexp;
56 ecbdata.hit = 0;
57 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
58 xecfg.ctxlen = o->context;
59 xecfg.interhunkctxlen = o->interhunkcontext;
62 * An xdiff error might be our "data->hit" from above. See the
63 * comment for xdiff_emit_line_fn in xdiff-interface.h
65 ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
66 &ecbdata, &xpp, &xecfg);
67 if (ecbdata.hit)
68 return 1;
69 if (ret)
70 return ret;
71 return 0;
74 static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
75 unsigned int limit)
77 unsigned int cnt = 0;
78 unsigned long sz = mf->size;
79 const char *data = mf->ptr;
81 if (regexp) {
82 regmatch_t regmatch;
83 int flags = 0;
85 while (sz &&
86 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
87 flags |= REG_NOTBOL;
88 data += regmatch.rm_eo;
89 sz -= regmatch.rm_eo;
90 if (sz && regmatch.rm_so == regmatch.rm_eo) {
91 data++;
92 sz--;
94 cnt++;
96 if (limit && cnt == limit)
97 return cnt;
100 } else { /* Classic exact string match */
101 while (sz) {
102 struct kwsmatch kwsm;
103 size_t offset = kwsexec(kws, data, sz, &kwsm);
104 if (offset == -1)
105 break;
106 sz -= offset + kwsm.size[0];
107 data += offset + kwsm.size[0];
108 cnt++;
110 if (limit && cnt == limit)
111 return cnt;
114 return cnt;
117 static int has_changes(mmfile_t *one, mmfile_t *two,
118 struct diff_options *o UNUSED,
119 regex_t *regexp, kwset_t kws)
121 unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
122 unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
123 return c1 != c2;
126 static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
127 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
129 struct userdiff_driver *textconv_one = NULL;
130 struct userdiff_driver *textconv_two = NULL;
131 mmfile_t mf1, mf2;
132 int ret;
134 /* ignore unmerged */
135 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
136 return 0;
138 if (o->objfind) {
139 return (DIFF_FILE_VALID(p->one) &&
140 oidset_contains(o->objfind, &p->one->oid)) ||
141 (DIFF_FILE_VALID(p->two) &&
142 oidset_contains(o->objfind, &p->two->oid));
145 if (o->flags.allow_textconv) {
146 textconv_one = get_textconv(o->repo, p->one);
147 textconv_two = get_textconv(o->repo, p->two);
151 * If we have an unmodified pair, we know that the count will be the
152 * same and don't even have to load the blobs. Unless textconv is in
153 * play, _and_ we are using two different textconv filters (e.g.,
154 * because a pair is an exact rename with different textconv attributes
155 * for each side, which might generate different content).
157 if (textconv_one == textconv_two && diff_unmodified_pair(p))
158 return 0;
160 if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
161 !o->flags.text &&
162 ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
163 (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
164 return 0;
166 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
167 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
169 ret = fn(&mf1, &mf2, o, regexp, kws);
171 if (textconv_one)
172 free(mf1.ptr);
173 if (textconv_two)
174 free(mf2.ptr);
175 diff_free_filespec_data(p->one);
176 diff_free_filespec_data(p->two);
178 return ret;
181 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
182 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
184 int i;
185 struct diff_queue_struct outq = DIFF_QUEUE_INIT;
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 */
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.
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);
213 free(q->queue);
214 *q = outq;
217 static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
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);
228 void diffcore_pickaxe(struct diff_options *o)
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;
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;
246 if (opts & DIFF_PICKAXE_KIND_G)
247 fn = diff_grep;
248 else if (opts & DIFF_PICKAXE_REGEX)
249 fn = has_changes;
250 else
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.
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;
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);
275 fn = has_changes;
276 } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
277 fn = NULL;
278 } else {
279 BUG("unknown pickaxe_opts flag");
282 pickaxe(&diff_queued_diff, o, regexp, kws, fn);
284 if (regexp)
285 regfree(regexp);
286 if (kws)
287 kwsfree(kws);
288 return;