pickaxe: call strlen only when necessary in diffcore_pickaxe_count()
[git/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"
f506b8e8 10
61690bf4
JK
11typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
12 struct diff_options *o,
13 regex_t *regexp, kwset_t kws);
14
f506b8e8
JH
15struct diffgrep_cb {
16 regex_t *regexp;
17 int hit;
18};
19
20static void diffgrep_consume(void *priv, char *line, unsigned long len)
21{
22 struct diffgrep_cb *data = priv;
23 regmatch_t regmatch;
24 int hold;
25
26 if (line[0] != '+' && line[0] != '-')
27 return;
28 if (data->hit)
29 /*
30 * NEEDSWORK: we should have a way to terminate the
31 * caller early.
32 */
33 return;
34 /* Yuck -- line ought to be "const char *"! */
35 hold = line[len];
36 line[len] = '\0';
37 data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
38 line[len] = hold;
39}
40
61690bf4
JK
41static int diff_grep(mmfile_t *one, mmfile_t *two,
42 struct diff_options *o,
db99cb70 43 regex_t *regexp, kwset_t kws)
f506b8e8
JH
44{
45 regmatch_t regmatch;
61690bf4
JK
46 struct diffgrep_cb ecbdata;
47 xpparam_t xpp;
48 xdemitconf_t xecfg;
bc615898 49
61690bf4
JK
50 if (!one)
51 return !regexec(regexp, two->ptr, 1, &regmatch, 0);
52 if (!two)
53 return !regexec(regexp, one->ptr, 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;
65 xdi_diff_outf(one, two, diffgrep_consume, &ecbdata,
66 &xpp, &xecfg);
67 return ecbdata.hit;
f506b8e8
JH
68}
69
3bdb5b9f 70static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
52e95789 71{
2002eed6 72 unsigned int cnt;
ce163c79 73 unsigned long sz;
52e95789 74 const char *data;
2002eed6 75
ef90ab66
JK
76 sz = mf->size;
77 data = mf->ptr;
2002eed6
JH
78 cnt = 0;
79
d01d8c67
PB
80 if (regexp) {
81 regmatch_t regmatch;
82 int flags = 0;
83
50fd6997 84 assert(data[sz] == '\0');
d01d8c67
PB
85 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
86 flags |= REG_NOTBOL;
50fd6997
RS
87 data += regmatch.rm_eo;
88 if (*data && regmatch.rm_so == regmatch.rm_eo)
89 data++;
2002eed6
JH
90 cnt++;
91 }
d01d8c67
PB
92
93 } else { /* Classic exact string match */
ce163c79 94 while (sz) {
5d176fb6
RS
95 struct kwsmatch kwsm;
96 size_t offset = kwsexec(kws, data, sz, &kwsm);
b95c5ada
FK
97 const char *found;
98 if (offset == -1)
ce163c79 99 break;
b95c5ada
FK
100 else
101 found = data + offset;
5d176fb6
RS
102 sz -= found - data + kwsm.size[0];
103 data = found + kwsm.size[0];
ce163c79 104 cnt++;
d01d8c67 105 }
2002eed6
JH
106 }
107 return cnt;
52e95789
JH
108}
109
61690bf4
JK
110static int has_changes(mmfile_t *one, mmfile_t *two,
111 struct diff_options *o,
5d176fb6 112 regex_t *regexp, kwset_t kws)
61690bf4 113{
3bdb5b9f
RS
114 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
115 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
116 return one_contains != two_contains;
61690bf4
JK
117}
118
119static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
120 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
15dafaf8 121{
bc615898
SR
122 struct userdiff_driver *textconv_one = NULL;
123 struct userdiff_driver *textconv_two = NULL;
ef90ab66
JK
124 mmfile_t mf1, mf2;
125 int ret;
126
8fa4b09f
JK
127 if (!o->pickaxe[0])
128 return 0;
129
61690bf4
JK
130 /* ignore unmerged */
131 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
132 return 0;
133
a8f61094
SR
134 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
135 textconv_one = get_textconv(p->one);
136 textconv_two = get_textconv(p->two);
137 }
bc615898 138
ef90ab66
JK
139 /*
140 * If we have an unmodified pair, we know that the count will be the
141 * same and don't even have to load the blobs. Unless textconv is in
142 * play, _and_ we are using two different textconv filters (e.g.,
143 * because a pair is an exact rename with different textconv attributes
144 * for each side, which might generate different content).
145 */
146 if (textconv_one == textconv_two && diff_unmodified_pair(p))
147 return 0;
148
7cdb9b42
JK
149 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
150 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
ef90ab66 151
61690bf4
JK
152 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
153 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
154 o, regexp, kws);
ef90ab66
JK
155
156 if (textconv_one)
157 free(mf1.ptr);
158 if (textconv_two)
159 free(mf2.ptr);
160 diff_free_filespec_data(p->one);
161 diff_free_filespec_data(p->two);
162
163 return ret;
15dafaf8
RS
164}
165
3753bd1f
RS
166static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
167 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
168{
169 int i;
170 struct diff_queue_struct outq;
171
172 DIFF_QUEUE_CLEAR(&outq);
173
174 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
175 /* Showing the whole changeset if needle exists */
176 for (i = 0; i < q->nr; i++) {
177 struct diff_filepair *p = q->queue[i];
178 if (pickaxe_match(p, o, regexp, kws, fn))
179 return; /* do not munge the queue */
180 }
181
182 /*
183 * Otherwise we will clear the whole queue by copying
184 * the empty outq at the end of this function, but
185 * first clear the current entries in the queue.
186 */
187 for (i = 0; i < q->nr; i++)
188 diff_free_filepair(q->queue[i]);
189 } else {
190 /* Showing only the filepairs that has the needle */
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 diff_q(&outq, p);
195 else
196 diff_free_filepair(p);
197 }
198 }
199
200 free(q->queue);
201 *q = outq;
202}
203
63b52afa 204void diffcore_pickaxe(struct diff_options *o)
52e95789 205{
382f013b
JH
206 const char *needle = o->pickaxe;
207 int opts = o->pickaxe_opts;
d01d8c67 208 regex_t regex, *regexp = NULL;
b95c5ada 209 kwset_t kws = NULL;
52e95789 210
63b52afa 211 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
d01d8c67 212 int err;
218c45a4
RS
213 int cflags = REG_EXTENDED | REG_NEWLINE;
214 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
215 cflags |= REG_ICASE;
216 err = regcomp(&regex, needle, cflags);
d01d8c67
PB
217 if (err) {
218 /* The POSIX.2 people are surely sick */
219 char errbuf[1024];
220 regerror(err, &regex, errbuf, 1024);
221 regfree(&regex);
276b22d3 222 die("invalid regex: %s", errbuf);
d01d8c67
PB
223 }
224 regexp = &regex;
b95c5ada 225 } else {
accccde4
JH
226 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
227 ? tolower_trans_tbl : NULL);
542b2aa2 228 kwsincr(kws, needle, strlen(needle));
b95c5ada 229 kwsprep(kws);
d01d8c67
PB
230 }
231
63b52afa
RS
232 /* Might want to warn when both S and G are on; I don't care... */
233 pickaxe(&diff_queued_diff, o, regexp, kws,
234 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
8e854b00 235
63b52afa
RS
236 if (regexp)
237 regfree(regexp);
b95c5ada
FK
238 else
239 kwsfree(kws);
52e95789
JH
240 return;
241}