]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-pickaxe.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / diffcore-pickaxe.c
CommitLineData
52e95789
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
f506b8e8 3 * Copyright (C) 2010 Google Inc.
52e95789 4 */
b6c09c03 5#include "git-compat-util.h"
52e95789
JH
6#include "diff.h"
7#include "diffcore.h"
f506b8e8 8#include "xdiff-interface.h"
b95c5ada 9#include "kwset.h"
df6e8744 10#include "oidset.h"
b6c09c03 11#include "pretty.h"
b51a9c14 12#include "quote.h"
f506b8e8 13
61690bf4
JK
14typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
15 struct diff_options *o,
16 regex_t *regexp, kwset_t kws);
17
f506b8e8
JH
18struct diffgrep_cb {
19 regex_t *regexp;
20 int hit;
21};
22
a8d5eb6d 23static int diffgrep_consume(void *priv, char *line, unsigned long len)
f506b8e8
JH
24{
25 struct diffgrep_cb *data = priv;
26 regmatch_t regmatch;
f506b8e8
JH
27
28 if (line[0] != '+' && line[0] != '-')
a8d5eb6d 29 return 0;
f506b8e8 30 if (data->hit)
fa59e7be
ÆAB
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;
36 }
a8d5eb6d 37 return 0;
f506b8e8
JH
38}
39
61690bf4
JK
40static int diff_grep(mmfile_t *one, mmfile_t *two,
41 struct diff_options *o,
0ada4b9b 42 regex_t *regexp, kwset_t kws UNUSED)
f506b8e8 43{
61690bf4
JK
44 struct diffgrep_cb ecbdata;
45 xpparam_t xpp;
46 xdemitconf_t xecfg;
fa59e7be 47 int ret;
bc615898 48
61690bf4
JK
49 /*
50 * We have both sides; need to run textual diff and see if
51 * the pattern appears on added/deleted lines.
52 */
53 memset(&xpp, 0, sizeof(xpp));
54 memset(&xecfg, 0, sizeof(xecfg));
55 ecbdata.regexp = regexp;
56 ecbdata.hit = 0;
5d934600 57 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
61690bf4
JK
58 xecfg.ctxlen = o->context;
59 xecfg.interhunkctxlen = o->interhunkcontext;
fa59e7be
ÆAB
60
61 /*
62 * An xdiff error might be our "data->hit" from above. See the
63 * comment for xdiff_emit_line_fn in xdiff-interface.h
64 */
5d934600 65 ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
fa59e7be
ÆAB
66 &ecbdata, &xpp, &xecfg);
67 if (ecbdata.hit)
68 return 1;
69 if (ret)
70 return ret;
71 return 0;
f506b8e8
JH
72}
73
5b0672a2
ÆAB
74static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
75 unsigned int limit)
52e95789 76{
102fdd2e
ÆAB
77 unsigned int cnt = 0;
78 unsigned long sz = mf->size;
79 const char *data = mf->ptr;
2002eed6 80
d01d8c67
PB
81 if (regexp) {
82 regmatch_t regmatch;
83 int flags = 0;
84
52e011cd 85 while (sz &&
b7d36ffc 86 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
d01d8c67 87 flags |= REG_NOTBOL;
50fd6997 88 data += regmatch.rm_eo;
f53c5de2 89 sz -= regmatch.rm_eo;
52e011cd 90 if (sz && regmatch.rm_so == regmatch.rm_eo) {
50fd6997 91 data++;
f53c5de2
SG
92 sz--;
93 }
2002eed6 94 cnt++;
5b0672a2
ÆAB
95
96 if (limit && cnt == limit)
97 return cnt;
2002eed6 98 }
d01d8c67
PB
99
100 } else { /* Classic exact string match */
ce163c79 101 while (sz) {
5d176fb6
RS
102 struct kwsmatch kwsm;
103 size_t offset = kwsexec(kws, data, sz, &kwsm);
b95c5ada 104 if (offset == -1)
ce163c79 105 break;
e4aab504
RS
106 sz -= offset + kwsm.size[0];
107 data += offset + kwsm.size[0];
ce163c79 108 cnt++;
5b0672a2
ÆAB
109
110 if (limit && cnt == limit)
111 return cnt;
d01d8c67 112 }
2002eed6
JH
113 }
114 return cnt;
52e95789
JH
115}
116
61690bf4 117static int has_changes(mmfile_t *one, mmfile_t *two,
0ada4b9b 118 struct diff_options *o UNUSED,
5d176fb6 119 regex_t *regexp, kwset_t kws)
61690bf4 120{
5b0672a2
ÆAB
121 unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
122 unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
5d35a953 123 return c1 != c2;
61690bf4
JK
124}
125
126static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
127 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
15dafaf8 128{
bc615898
SR
129 struct userdiff_driver *textconv_one = NULL;
130 struct userdiff_driver *textconv_two = NULL;
ef90ab66
JK
131 mmfile_t mf1, mf2;
132 int ret;
133
61690bf4
JK
134 /* ignore unmerged */
135 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
136 return 0;
137
15af58c1
SB
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));
143 }
144
0d1e0e78 145 if (o->flags.allow_textconv) {
bd7ad45b
NTND
146 textconv_one = get_textconv(o->repo, p->one);
147 textconv_two = get_textconv(o->repo, p->two);
a8f61094 148 }
bc615898 149
ef90ab66
JK
150 /*
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).
156 */
157 if (textconv_one == textconv_two && diff_unmodified_pair(p))
158 return 0;
159
e0e7cb80
TB
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;
165
6afaf807
NTND
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);
ef90ab66 168
f97fe358 169 ret = fn(&mf1, &mf2, o, regexp, kws);
ef90ab66
JK
170
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);
177
178 return ret;
15dafaf8
RS
179}
180
3753bd1f
RS
181static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
182 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
183{
184 int i;
185 struct diff_queue_struct outq;
186
187 DIFF_QUEUE_CLEAR(&outq);
188
189 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
190 /* Showing the whole changeset if needle exists */
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 return; /* do not munge the queue */
195 }
196
197 /*
198 * Otherwise we will clear the whole queue by copying
199 * the empty outq at the end of this function, but
200 * first clear the current entries in the queue.
201 */
202 for (i = 0; i < q->nr; i++)
203 diff_free_filepair(q->queue[i]);
204 } else {
205 /* Showing only the filepairs that has the needle */
206 for (i = 0; i < q->nr; i++) {
207 struct diff_filepair *p = q->queue[i];
208 if (pickaxe_match(p, o, regexp, kws, fn))
209 diff_q(&outq, p);
210 else
211 diff_free_filepair(p);
212 }
213 }
214
215 free(q->queue);
216 *q = outq;
217}
218
3d5b23a3
NTND
219static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
220{
221 int err = regcomp(regex, needle, cflags);
222 if (err) {
223 /* The POSIX.2 people are surely sick */
224 char errbuf[1024];
225 regerror(err, regex, errbuf, 1024);
3d5b23a3
NTND
226 die("invalid regex: %s", errbuf);
227 }
228}
229
63b52afa 230void diffcore_pickaxe(struct diff_options *o)
52e95789 231{
382f013b
JH
232 const char *needle = o->pickaxe;
233 int opts = o->pickaxe_opts;
d01d8c67 234 regex_t regex, *regexp = NULL;
b95c5ada 235 kwset_t kws = NULL;
03c1f14a 236 pickaxe_fn fn;
52e95789 237
2e197a75
ÆAB
238 if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
239 (!needle || !*needle))
240 BUG("should have needle under -G or -S");
63b52afa 241 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
218c45a4 242 int cflags = REG_EXTENDED | REG_NEWLINE;
c1ddc461 243 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
218c45a4 244 cflags |= REG_ICASE;
3d5b23a3 245 regcomp_or_die(&regex, needle, cflags);
d01d8c67 246 regexp = &regex;
03c1f14a
ÆAB
247
248 if (opts & DIFF_PICKAXE_KIND_G)
249 fn = diff_grep;
250 else if (opts & DIFF_PICKAXE_REGEX)
251 fn = has_changes;
252 else
253 /*
254 * We don't need to check the combination of
255 * -G and --pickaxe-regex, by the time we get
256 * here diff.c has already died if they're
257 * combined. See the usage tests in
258 * t4209-log-pickaxe.sh.
259 */
260 BUG("unreachable");
15af58c1
SB
261 } else if (opts & DIFF_PICKAXE_KIND_S) {
262 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
263 has_non_ascii(needle)) {
264 struct strbuf sb = STRBUF_INIT;
265 int cflags = REG_NEWLINE | REG_ICASE;
266
267 basic_regex_quote_buf(&sb, needle);
268 regcomp_or_die(&regex, sb.buf, cflags);
269 strbuf_release(&sb);
270 regexp = &regex;
271 } else {
272 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
273 ? tolower_trans_tbl : NULL);
274 kwsincr(kws, needle, strlen(needle));
275 kwsprep(kws);
276 }
03c1f14a
ÆAB
277 fn = has_changes;
278 } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
279 fn = NULL;
280 } else {
281 BUG("unknown pickaxe_opts flag");
d01d8c67
PB
282 }
283
03c1f14a 284 pickaxe(&diff_queued_diff, o, regexp, kws, fn);
8e854b00 285
63b52afa
RS
286 if (regexp)
287 regfree(regexp);
15af58c1 288 if (kws)
b95c5ada 289 kwsfree(kws);
52e95789
JH
290 return;
291}