blob: ea03b9107eb2e85dd4a4c5ad93ac08dfe7e1c9f0 [file] [log] [blame]
Junio C Hamano52e95782005-05-21 02:40:01 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
Junio C Hamanof506b8e2010-08-23 10:17:03 -07003 * Copyright (C) 2010 Google Inc.
Junio C Hamano52e95782005-05-21 02:40:01 -07004 */
5#include "cache.h"
6#include "diff.h"
7#include "diffcore.h"
Junio C Hamanof506b8e2010-08-23 10:17:03 -07008#include "xdiff-interface.h"
9
10struct diffgrep_cb {
11 regex_t *regexp;
12 int hit;
13};
14
15static void diffgrep_consume(void *priv, char *line, unsigned long len)
16{
17 struct diffgrep_cb *data = priv;
18 regmatch_t regmatch;
19 int hold;
20
21 if (line[0] != '+' && line[0] != '-')
22 return;
23 if (data->hit)
24 /*
25 * NEEDSWORK: we should have a way to terminate the
26 * caller early.
27 */
28 return;
29 /* Yuck -- line ought to be "const char *"! */
30 hold = line[len];
31 line[len] = '\0';
32 data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
33 line[len] = hold;
34}
35
36static void fill_one(struct diff_filespec *one,
37 mmfile_t *mf, struct userdiff_driver **textconv)
38{
39 if (DIFF_FILE_VALID(one)) {
40 *textconv = get_textconv(one);
41 mf->size = fill_textconv(*textconv, one, &mf->ptr);
42 } else {
43 memset(mf, 0, sizeof(*mf));
44 }
45}
46
47static int diff_grep(struct diff_filepair *p, regex_t *regexp, struct diff_options *o)
48{
49 regmatch_t regmatch;
50 struct userdiff_driver *textconv_one = NULL;
51 struct userdiff_driver *textconv_two = NULL;
52 mmfile_t mf1, mf2;
53 int hit;
54
55 if (diff_unmodified_pair(p))
56 return 0;
57
58 fill_one(p->one, &mf1, &textconv_one);
59 fill_one(p->two, &mf2, &textconv_two);
60
61 if (!mf1.ptr) {
62 if (!mf2.ptr)
63 return 0; /* ignore unmerged */
64 /* created "two" -- does it have what we are looking for? */
65 hit = !regexec(regexp, p->two->data, 1, &regmatch, 0);
66 } else if (!mf2.ptr) {
67 /* removed "one" -- did it have what we are looking for? */
68 hit = !regexec(regexp, p->one->data, 1, &regmatch, 0);
69 } else {
70 /*
71 * We have both sides; need to run textual diff and see if
72 * the pattern appears on added/deleted lines.
73 */
74 struct diffgrep_cb ecbdata;
75 xpparam_t xpp;
76 xdemitconf_t xecfg;
77
78 memset(&xpp, 0, sizeof(xpp));
79 memset(&xecfg, 0, sizeof(xecfg));
80 ecbdata.regexp = regexp;
81 ecbdata.hit = 0;
82 xecfg.ctxlen = o->context;
83 xecfg.interhunkctxlen = o->interhunkcontext;
84 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
85 &xpp, &xecfg);
86 hit = ecbdata.hit;
87 }
88 if (textconv_one)
89 free(mf1.ptr);
90 if (textconv_two)
91 free(mf2.ptr);
92 return hit;
93}
94
95static void diffcore_pickaxe_grep(struct diff_options *o)
96{
97 struct diff_queue_struct *q = &diff_queued_diff;
98 int i, has_changes, err;
99 regex_t regex;
100 struct diff_queue_struct outq;
101 outq.queue = NULL;
102 outq.nr = outq.alloc = 0;
103
104 err = regcomp(&regex, o->pickaxe, REG_EXTENDED | REG_NEWLINE);
105 if (err) {
106 char errbuf[1024];
107 regerror(err, &regex, errbuf, 1024);
108 regfree(&regex);
109 die("invalid log-grep regex: %s", errbuf);
110 }
111
112 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
113 /* Showing the whole changeset if needle exists */
114 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
115 struct diff_filepair *p = q->queue[i];
116 if (diff_grep(p, &regex, o))
117 has_changes++;
118 }
119 if (has_changes)
120 return; /* do not munge the queue */
121
122 /*
123 * Otherwise we will clear the whole queue by copying
124 * the empty outq at the end of this function, but
125 * first clear the current entries in the queue.
126 */
127 for (i = 0; i < q->nr; i++)
128 diff_free_filepair(q->queue[i]);
129 } else {
130 /* Showing only the filepairs that has the needle */
131 for (i = 0; i < q->nr; i++) {
132 struct diff_filepair *p = q->queue[i];
133 if (diff_grep(p, &regex, o))
134 diff_q(&outq, p);
135 else
136 diff_free_filepair(p);
137 }
138 }
139
140 regfree(&regex);
141
142 free(q->queue);
143 *q = outq;
144 return;
145}
Junio C Hamano52e95782005-05-21 02:40:01 -0700146
Junio C Hamano2002eed2005-07-23 16:35:25 -0700147static unsigned int contains(struct diff_filespec *one,
Petr Baudisd01d8c62006-03-29 02:16:33 +0200148 const char *needle, unsigned long len,
149 regex_t *regexp)
Junio C Hamano52e95782005-05-21 02:40:01 -0700150{
Junio C Hamano2002eed2005-07-23 16:35:25 -0700151 unsigned int cnt;
René Scharfece163c72009-03-03 00:00:55 +0100152 unsigned long sz;
Junio C Hamano52e95782005-05-21 02:40:01 -0700153 const char *data;
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -0700154 if (diff_populate_filespec(one, 0))
Junio C Hamano52e95782005-05-21 02:40:01 -0700155 return 0;
Jeff Kinge1b16112007-01-25 23:48:58 -0500156 if (!len)
157 return 0;
Junio C Hamano2002eed2005-07-23 16:35:25 -0700158
Junio C Hamano52e95782005-05-21 02:40:01 -0700159 sz = one->size;
160 data = one->data;
Junio C Hamano2002eed2005-07-23 16:35:25 -0700161 cnt = 0;
162
Petr Baudisd01d8c62006-03-29 02:16:33 +0200163 if (regexp) {
164 regmatch_t regmatch;
165 int flags = 0;
166
René Scharfe50fd6992009-03-16 19:38:42 +0100167 assert(data[sz] == '\0');
Petr Baudisd01d8c62006-03-29 02:16:33 +0200168 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
169 flags |= REG_NOTBOL;
René Scharfe50fd6992009-03-16 19:38:42 +0100170 data += regmatch.rm_eo;
171 if (*data && regmatch.rm_so == regmatch.rm_eo)
172 data++;
Junio C Hamano2002eed2005-07-23 16:35:25 -0700173 cnt++;
174 }
Petr Baudisd01d8c62006-03-29 02:16:33 +0200175
176 } else { /* Classic exact string match */
René Scharfece163c72009-03-03 00:00:55 +0100177 while (sz) {
178 const char *found = memmem(data, sz, needle, len);
179 if (!found)
180 break;
181 sz -= found - data + len;
182 data = found + len;
183 cnt++;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200184 }
Junio C Hamano2002eed2005-07-23 16:35:25 -0700185 }
Junio C Hamanoa0cb9402007-05-07 01:24:27 -0700186 diff_free_filespec_data(one);
Junio C Hamano2002eed2005-07-23 16:35:25 -0700187 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -0700188}
189
Junio C Hamanof506b8e2010-08-23 10:17:03 -0700190static void diffcore_pickaxe_count(struct diff_options *o)
Junio C Hamano52e95782005-05-21 02:40:01 -0700191{
Junio C Hamano382f0132010-08-31 13:44:39 -0700192 const char *needle = o->pickaxe;
193 int opts = o->pickaxe_opts;
Junio C Hamano38c6f782005-05-21 19:40:36 -0700194 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano52e95782005-05-21 02:40:01 -0700195 unsigned long len = strlen(needle);
Junio C Hamano367cec12005-05-27 15:55:28 -0700196 int i, has_changes;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200197 regex_t regex, *regexp = NULL;
Junio C Hamano52e95782005-05-21 02:40:01 -0700198 struct diff_queue_struct outq;
Bo Yang9ca5df92010-05-06 21:52:27 -0700199 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamano52e95782005-05-21 02:40:01 -0700200
Petr Baudisd01d8c62006-03-29 02:16:33 +0200201 if (opts & DIFF_PICKAXE_REGEX) {
202 int err;
203 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
204 if (err) {
205 /* The POSIX.2 people are surely sick */
206 char errbuf[1024];
207 regerror(err, &regex, errbuf, 1024);
208 regfree(&regex);
209 die("invalid pickaxe regex: %s", errbuf);
210 }
211 regexp = &regex;
212 }
213
Junio C Hamano367cec12005-05-27 15:55:28 -0700214 if (opts & DIFF_PICKAXE_ALL) {
215 /* Showing the whole changeset if needle exists */
216 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
217 struct diff_filepair *p = q->queue[i];
218 if (!DIFF_FILE_VALID(p->one)) {
219 if (!DIFF_FILE_VALID(p->two))
220 continue; /* ignore unmerged */
221 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +0200222 if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700223 has_changes++;
224 }
225 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +0200226 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700227 has_changes++;
228 }
229 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +0200230 contains(p->one, needle, len, regexp) !=
231 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700232 has_changes++;
Junio C Hamano52e95782005-05-21 02:40:01 -0700233 }
Junio C Hamano367cec12005-05-27 15:55:28 -0700234 if (has_changes)
235 return; /* not munge the queue */
236
237 /* otherwise we will clear the whole queue
238 * by copying the empty outq at the end of this
239 * function, but first clear the current entries
240 * in the queue.
241 */
242 for (i = 0; i < q->nr; i++)
243 diff_free_filepair(q->queue[i]);
Junio C Hamano52e95782005-05-21 02:40:01 -0700244 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700245 else
Junio C Hamano367cec12005-05-27 15:55:28 -0700246 /* Showing only the filepairs that has the needle */
247 for (i = 0; i < q->nr; i++) {
248 struct diff_filepair *p = q->queue[i];
249 has_changes = 0;
250 if (!DIFF_FILE_VALID(p->one)) {
251 if (!DIFF_FILE_VALID(p->two))
252 ; /* ignore unmerged */
253 /* created */
Petr Baudisd01d8c62006-03-29 02:16:33 +0200254 else if (contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700255 has_changes = 1;
256 }
257 else if (!DIFF_FILE_VALID(p->two)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +0200258 if (contains(p->one, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700259 has_changes = 1;
260 }
261 else if (!diff_unmodified_pair(p) &&
Petr Baudisd01d8c62006-03-29 02:16:33 +0200262 contains(p->one, needle, len, regexp) !=
263 contains(p->two, needle, len, regexp))
Junio C Hamano367cec12005-05-27 15:55:28 -0700264 has_changes = 1;
265
266 if (has_changes)
267 diff_q(&outq, p);
268 else
269 diff_free_filepair(p);
270 }
271
Brandon Casey95ae69b2010-10-04 17:51:47 -0500272 if (opts & DIFF_PICKAXE_REGEX)
Petr Baudisd01d8c62006-03-29 02:16:33 +0200273 regfree(&regex);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200274
Junio C Hamano52e95782005-05-21 02:40:01 -0700275 free(q->queue);
276 *q = outq;
277 return;
278}
Junio C Hamanof506b8e2010-08-23 10:17:03 -0700279
280void diffcore_pickaxe(struct diff_options *o)
281{
282 /* Might want to warn when both S and G are on; I don't care... */
283 if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
Brandon Casey85209132010-10-04 17:51:48 -0500284 diffcore_pickaxe_grep(o);
Junio C Hamanof506b8e2010-08-23 10:17:03 -0700285 else
Brandon Casey85209132010-10-04 17:51:48 -0500286 diffcore_pickaxe_count(o);
Junio C Hamanof506b8e2010-08-23 10:17:03 -0700287}