]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-pickaxe.c
diffcore-pickaxe: fix leaks in "log -S<block>" and "log -G<pattern>"
[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
ebb72262 86 if (!o->pickaxe[0])
f506b8e8
JH
87 return 0;
88
a8f61094
SR
89 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
90 textconv_one = get_textconv(p->one);
91 textconv_two = get_textconv(p->two);
92 }
bc615898 93
ebb72262
JH
94 if (textconv_one == textconv_two && diff_unmodified_pair(p))
95 return 0;
96
7cdb9b42
JK
97 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
98 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
f506b8e8 99
7cdb9b42
JK
100 if (!DIFF_FILE_VALID(p->one)) {
101 if (!DIFF_FILE_VALID(p->two))
88ff684d
JH
102 hit = 0; /* ignore unmerged */
103 else
104 /* created "two" -- does it have what we are looking for? */
105 hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
7cdb9b42 106 } else if (!DIFF_FILE_VALID(p->two)) {
f506b8e8 107 /* removed "one" -- did it have what we are looking for? */
b1c2f57d 108 hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
f506b8e8
JH
109 } else {
110 /*
111 * We have both sides; need to run textual diff and see if
112 * the pattern appears on added/deleted lines.
113 */
114 struct diffgrep_cb ecbdata;
115 xpparam_t xpp;
116 xdemitconf_t xecfg;
117
118 memset(&xpp, 0, sizeof(xpp));
119 memset(&xecfg, 0, sizeof(xecfg));
120 ecbdata.regexp = regexp;
121 ecbdata.hit = 0;
122 xecfg.ctxlen = o->context;
123 xecfg.interhunkctxlen = o->interhunkcontext;
124 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
125 &xpp, &xecfg);
126 hit = ecbdata.hit;
127 }
128 if (textconv_one)
129 free(mf1.ptr);
130 if (textconv_two)
131 free(mf2.ptr);
ebb72262
JH
132 diff_free_filespec_data(p->one);
133 diff_free_filespec_data(p->two);
f506b8e8
JH
134 return hit;
135}
136
137static void diffcore_pickaxe_grep(struct diff_options *o)
138{
8a94151d 139 int err;
f506b8e8 140 regex_t regex;
accccde4 141 int cflags = REG_EXTENDED | REG_NEWLINE;
f506b8e8 142
accccde4
JH
143 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
144 cflags |= REG_ICASE;
145
146 err = regcomp(&regex, o->pickaxe, cflags);
f506b8e8
JH
147 if (err) {
148 char errbuf[1024];
149 regerror(err, &regex, errbuf, 1024);
150 regfree(&regex);
151 die("invalid log-grep regex: %s", errbuf);
152 }
153
8a94151d 154 pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
f506b8e8 155
2b5f07f1 156 regfree(&regex);
f506b8e8
JH
157 return;
158}
52e95789 159
ef90ab66 160static unsigned int contains(mmfile_t *mf, struct diff_options *o,
b95c5ada 161 regex_t *regexp, kwset_t kws)
52e95789 162{
2002eed6 163 unsigned int cnt;
ce163c79 164 unsigned long sz;
52e95789 165 const char *data;
2002eed6 166
ef90ab66
JK
167 sz = mf->size;
168 data = mf->ptr;
2002eed6
JH
169 cnt = 0;
170
d01d8c67
PB
171 if (regexp) {
172 regmatch_t regmatch;
173 int flags = 0;
174
50fd6997 175 assert(data[sz] == '\0');
d01d8c67
PB
176 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
177 flags |= REG_NOTBOL;
50fd6997
RS
178 data += regmatch.rm_eo;
179 if (*data && regmatch.rm_so == regmatch.rm_eo)
180 data++;
2002eed6
JH
181 cnt++;
182 }
d01d8c67
PB
183
184 } else { /* Classic exact string match */
ce163c79 185 while (sz) {
5d176fb6
RS
186 struct kwsmatch kwsm;
187 size_t offset = kwsexec(kws, data, sz, &kwsm);
b95c5ada
FK
188 const char *found;
189 if (offset == -1)
ce163c79 190 break;
b95c5ada
FK
191 else
192 found = data + offset;
5d176fb6
RS
193 sz -= found - data + kwsm.size[0];
194 data = found + kwsm.size[0];
ce163c79 195 cnt++;
d01d8c67 196 }
2002eed6
JH
197 }
198 return cnt;
52e95789
JH
199}
200
5d176fb6
RS
201static int has_changes(struct diff_filepair *p, struct diff_options *o,
202 regex_t *regexp, kwset_t kws)
15dafaf8 203{
bc615898
SR
204 struct userdiff_driver *textconv_one = NULL;
205 struct userdiff_driver *textconv_two = NULL;
ef90ab66
JK
206 mmfile_t mf1, mf2;
207 int ret;
208
8fa4b09f
JK
209 if (!o->pickaxe[0])
210 return 0;
211
a8f61094
SR
212 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
213 textconv_one = get_textconv(p->one);
214 textconv_two = get_textconv(p->two);
215 }
bc615898 216
ef90ab66
JK
217 /*
218 * If we have an unmodified pair, we know that the count will be the
219 * same and don't even have to load the blobs. Unless textconv is in
220 * play, _and_ we are using two different textconv filters (e.g.,
221 * because a pair is an exact rename with different textconv attributes
222 * for each side, which might generate different content).
223 */
224 if (textconv_one == textconv_two && diff_unmodified_pair(p))
225 return 0;
226
7cdb9b42
JK
227 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
228 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
ef90ab66 229
7cdb9b42
JK
230 if (!DIFF_FILE_VALID(p->one)) {
231 if (!DIFF_FILE_VALID(p->two))
ef90ab66 232 ret = 0; /* ignore unmerged */
88ff684d
JH
233 else
234 /* created */
235 ret = contains(&mf2, o, regexp, kws) != 0;
15dafaf8 236 }
7cdb9b42 237 else if (!DIFF_FILE_VALID(p->two)) /* removed */
ef90ab66
JK
238 ret = contains(&mf1, o, regexp, kws) != 0;
239 else
240 ret = contains(&mf1, o, regexp, kws) !=
241 contains(&mf2, o, regexp, kws);
242
243 if (textconv_one)
244 free(mf1.ptr);
245 if (textconv_two)
246 free(mf2.ptr);
247 diff_free_filespec_data(p->one);
248 diff_free_filespec_data(p->two);
249
250 return ret;
15dafaf8
RS
251}
252
f506b8e8 253static void diffcore_pickaxe_count(struct diff_options *o)
52e95789 254{
382f013b
JH
255 const char *needle = o->pickaxe;
256 int opts = o->pickaxe_opts;
52e95789 257 unsigned long len = strlen(needle);
d01d8c67 258 regex_t regex, *regexp = NULL;
b95c5ada 259 kwset_t kws = NULL;
52e95789 260
d01d8c67
PB
261 if (opts & DIFF_PICKAXE_REGEX) {
262 int err;
263 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
264 if (err) {
265 /* The POSIX.2 people are surely sick */
266 char errbuf[1024];
267 regerror(err, &regex, errbuf, 1024);
268 regfree(&regex);
269 die("invalid pickaxe regex: %s", errbuf);
270 }
271 regexp = &regex;
b95c5ada 272 } else {
accccde4
JH
273 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
274 ? tolower_trans_tbl : NULL);
b95c5ada
FK
275 kwsincr(kws, needle, len);
276 kwsprep(kws);
d01d8c67
PB
277 }
278
8a94151d 279 pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
8e854b00 280
95ae69b9 281 if (opts & DIFF_PICKAXE_REGEX)
d01d8c67 282 regfree(&regex);
b95c5ada
FK
283 else
284 kwsfree(kws);
52e95789
JH
285 return;
286}
f506b8e8
JH
287
288void diffcore_pickaxe(struct diff_options *o)
289{
290 /* Might want to warn when both S and G are on; I don't care... */
291 if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
8520913c 292 diffcore_pickaxe_grep(o);
f506b8e8 293 else
8520913c 294 diffcore_pickaxe_count(o);
f506b8e8 295}