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