]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-pickaxe.c
Merge branch 'cb/notes-freeing-always-null-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
JH
4 */
5#include "cache.h"
6#include "diff.h"
7#include "diffcore.h"
f506b8e8 8#include "xdiff-interface.h"
b95c5ada 9#include "kwset.h"
b51a9c14
NTND
10#include "commit.h"
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
22static void diffgrep_consume(void *priv, char *line, unsigned long len)
23{
24 struct diffgrep_cb *data = priv;
25 regmatch_t regmatch;
f506b8e8
JH
26
27 if (line[0] != '+' && line[0] != '-')
28 return;
29 if (data->hit)
30 /*
31 * NEEDSWORK: we should have a way to terminate the
32 * caller early.
33 */
34 return;
b7d36ffc
JS
35 data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
36 &regmatch, 0);
f506b8e8
JH
37}
38
61690bf4
JK
39static int diff_grep(mmfile_t *one, mmfile_t *two,
40 struct diff_options *o,
db99cb70 41 regex_t *regexp, kwset_t kws)
f506b8e8
JH
42{
43 regmatch_t regmatch;
61690bf4
JK
44 struct diffgrep_cb ecbdata;
45 xpparam_t xpp;
46 xdemitconf_t xecfg;
bc615898 47
61690bf4 48 if (!one)
b7d36ffc
JS
49 return !regexec_buf(regexp, two->ptr, two->size,
50 1, &regmatch, 0);
61690bf4 51 if (!two)
b7d36ffc
JS
52 return !regexec_buf(regexp, one->ptr, one->size,
53 1, &regmatch, 0);
f506b8e8 54
61690bf4
JK
55 /*
56 * We have both sides; need to run textual diff and see if
57 * the pattern appears on added/deleted lines.
58 */
59 memset(&xpp, 0, sizeof(xpp));
60 memset(&xecfg, 0, sizeof(xecfg));
61 ecbdata.regexp = regexp;
62 ecbdata.hit = 0;
63 xecfg.ctxlen = o->context;
64 xecfg.interhunkctxlen = o->interhunkcontext;
3b40a090
JK
65 if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
66 &ecbdata, &xpp, &xecfg))
3efb9880 67 return 0;
61690bf4 68 return ecbdata.hit;
f506b8e8
JH
69}
70
3bdb5b9f 71static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
52e95789 72{
2002eed6 73 unsigned int cnt;
ce163c79 74 unsigned long sz;
52e95789 75 const char *data;
2002eed6 76
ef90ab66
JK
77 sz = mf->size;
78 data = mf->ptr;
2002eed6
JH
79 cnt = 0;
80
d01d8c67
PB
81 if (regexp) {
82 regmatch_t regmatch;
83 int flags = 0;
84
f53c5de2 85 while (sz && *data &&
b7d36ffc 86 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
d01d8c67 87 flags |= REG_NOTBOL;
50fd6997 88 data += regmatch.rm_eo;
f53c5de2
SG
89 sz -= regmatch.rm_eo;
90 if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
50fd6997 91 data++;
f53c5de2
SG
92 sz--;
93 }
2002eed6
JH
94 cnt++;
95 }
d01d8c67
PB
96
97 } else { /* Classic exact string match */
ce163c79 98 while (sz) {
5d176fb6
RS
99 struct kwsmatch kwsm;
100 size_t offset = kwsexec(kws, data, sz, &kwsm);
b95c5ada 101 if (offset == -1)
ce163c79 102 break;
e4aab504
RS
103 sz -= offset + kwsm.size[0];
104 data += offset + kwsm.size[0];
ce163c79 105 cnt++;
d01d8c67 106 }
2002eed6
JH
107 }
108 return cnt;
52e95789
JH
109}
110
61690bf4
JK
111static int has_changes(mmfile_t *one, mmfile_t *two,
112 struct diff_options *o,
5d176fb6 113 regex_t *regexp, kwset_t kws)
61690bf4 114{
3bdb5b9f
RS
115 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
116 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
117 return one_contains != two_contains;
61690bf4
JK
118}
119
120static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
121 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
15dafaf8 122{
bc615898
SR
123 struct userdiff_driver *textconv_one = NULL;
124 struct userdiff_driver *textconv_two = NULL;
ef90ab66
JK
125 mmfile_t mf1, mf2;
126 int ret;
127
61690bf4
JK
128 /* ignore unmerged */
129 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
130 return 0;
131
15af58c1
SB
132 if (o->objfind) {
133 return (DIFF_FILE_VALID(p->one) &&
134 oidset_contains(o->objfind, &p->one->oid)) ||
135 (DIFF_FILE_VALID(p->two) &&
136 oidset_contains(o->objfind, &p->two->oid));
137 }
138
139 if (!o->pickaxe[0])
140 return 0;
141
0d1e0e78 142 if (o->flags.allow_textconv) {
acd00ea0
NTND
143 textconv_one = get_textconv(o->repo->index, p->one);
144 textconv_two = get_textconv(o->repo->index, p->two);
a8f61094 145 }
bc615898 146
ef90ab66
JK
147 /*
148 * If we have an unmodified pair, we know that the count will be the
149 * same and don't even have to load the blobs. Unless textconv is in
150 * play, _and_ we are using two different textconv filters (e.g.,
151 * because a pair is an exact rename with different textconv attributes
152 * for each side, which might generate different content).
153 */
154 if (textconv_one == textconv_two && diff_unmodified_pair(p))
155 return 0;
156
6afaf807
NTND
157 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
158 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
ef90ab66 159
61690bf4
JK
160 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
161 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
162 o, regexp, kws);
ef90ab66
JK
163
164 if (textconv_one)
165 free(mf1.ptr);
166 if (textconv_two)
167 free(mf2.ptr);
168 diff_free_filespec_data(p->one);
169 diff_free_filespec_data(p->two);
170
171 return ret;
15dafaf8
RS
172}
173
3753bd1f
RS
174static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
175 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
176{
177 int i;
178 struct diff_queue_struct outq;
179
180 DIFF_QUEUE_CLEAR(&outq);
181
182 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
183 /* Showing the whole changeset if needle exists */
184 for (i = 0; i < q->nr; i++) {
185 struct diff_filepair *p = q->queue[i];
186 if (pickaxe_match(p, o, regexp, kws, fn))
187 return; /* do not munge the queue */
188 }
189
190 /*
191 * Otherwise we will clear the whole queue by copying
192 * the empty outq at the end of this function, but
193 * first clear the current entries in the queue.
194 */
195 for (i = 0; i < q->nr; i++)
196 diff_free_filepair(q->queue[i]);
197 } else {
198 /* Showing only the filepairs that has the needle */
199 for (i = 0; i < q->nr; i++) {
200 struct diff_filepair *p = q->queue[i];
201 if (pickaxe_match(p, o, regexp, kws, fn))
202 diff_q(&outq, p);
203 else
204 diff_free_filepair(p);
205 }
206 }
207
208 free(q->queue);
209 *q = outq;
210}
211
3d5b23a3
NTND
212static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
213{
214 int err = regcomp(regex, needle, cflags);
215 if (err) {
216 /* The POSIX.2 people are surely sick */
217 char errbuf[1024];
218 regerror(err, regex, errbuf, 1024);
3d5b23a3
NTND
219 die("invalid regex: %s", errbuf);
220 }
221}
222
63b52afa 223void diffcore_pickaxe(struct diff_options *o)
52e95789 224{
382f013b
JH
225 const char *needle = o->pickaxe;
226 int opts = o->pickaxe_opts;
d01d8c67 227 regex_t regex, *regexp = NULL;
b95c5ada 228 kwset_t kws = NULL;
52e95789 229
63b52afa 230 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
218c45a4 231 int cflags = REG_EXTENDED | REG_NEWLINE;
c1ddc461 232 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
218c45a4 233 cflags |= REG_ICASE;
3d5b23a3 234 regcomp_or_die(&regex, needle, cflags);
d01d8c67 235 regexp = &regex;
15af58c1
SB
236 } else if (opts & DIFF_PICKAXE_KIND_S) {
237 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
238 has_non_ascii(needle)) {
239 struct strbuf sb = STRBUF_INIT;
240 int cflags = REG_NEWLINE | REG_ICASE;
241
242 basic_regex_quote_buf(&sb, needle);
243 regcomp_or_die(&regex, sb.buf, cflags);
244 strbuf_release(&sb);
245 regexp = &regex;
246 } else {
247 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
248 ? tolower_trans_tbl : NULL);
249 kwsincr(kws, needle, strlen(needle));
250 kwsprep(kws);
251 }
d01d8c67
PB
252 }
253
63b52afa
RS
254 pickaxe(&diff_queued_diff, o, regexp, kws,
255 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
8e854b00 256
63b52afa
RS
257 if (regexp)
258 regfree(regexp);
15af58c1 259 if (kws)
b95c5ada 260 kwsfree(kws);
52e95789
JH
261 return;
262}