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