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