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