Merge branch 'maint-1.8.1' into maint
[git/jrn.git] / diffcore-pickaxe.c
blobb097fa766177f90e4b7983789a9286506664b2fd
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
4 */
5 #include "cache.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "xdiff-interface.h"
9 #include "kwset.h"
11 typedef int (*pickaxe_fn)(struct diff_filepair *p, struct diff_options *o, regex_t *regexp, kwset_t kws);
13 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
14 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
16 int i;
17 struct diff_queue_struct outq;
19 DIFF_QUEUE_CLEAR(&outq);
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 */
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.
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);
47 free(q->queue);
48 *q = outq;
51 struct diffgrep_cb {
52 regex_t *regexp;
53 int hit;
56 static void diffgrep_consume(void *priv, char *line, unsigned long len)
58 struct diffgrep_cb *data = priv;
59 regmatch_t regmatch;
60 int hold;
62 if (line[0] != '+' && line[0] != '-')
63 return;
64 if (data->hit)
66 * NEEDSWORK: we should have a way to terminate the
67 * caller early.
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;
77 static void fill_one(struct diff_filespec *one,
78 mmfile_t *mf, struct userdiff_driver **textconv)
80 if (DIFF_FILE_VALID(one)) {
81 *textconv = get_textconv(one);
82 mf->size = fill_textconv(*textconv, one, &mf->ptr);
83 } else {
84 memset(mf, 0, sizeof(*mf));
88 static int diff_grep(struct diff_filepair *p, struct diff_options *o,
89 regex_t *regexp, kwset_t kws)
91 regmatch_t regmatch;
92 struct userdiff_driver *textconv_one = NULL;
93 struct userdiff_driver *textconv_two = NULL;
94 mmfile_t mf1, mf2;
95 int hit;
97 if (diff_unmodified_pair(p))
98 return 0;
100 fill_one(p->one, &mf1, &textconv_one);
101 fill_one(p->two, &mf2, &textconv_two);
103 if (!mf1.ptr) {
104 if (!mf2.ptr)
105 return 0; /* ignore unmerged */
106 /* created "two" -- does it have what we are looking for? */
107 hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);
108 } else if (!mf2.ptr) {
109 /* removed "one" -- did it have what we are looking for? */
110 hit = !regexec(regexp, mf1.ptr, 1, &regmatch, 0);
111 } else {
113 * We have both sides; need to run textual diff and see if
114 * the pattern appears on added/deleted lines.
116 struct diffgrep_cb ecbdata;
117 xpparam_t xpp;
118 xdemitconf_t xecfg;
120 memset(&xpp, 0, sizeof(xpp));
121 memset(&xecfg, 0, sizeof(xecfg));
122 ecbdata.regexp = regexp;
123 ecbdata.hit = 0;
124 xecfg.ctxlen = o->context;
125 xecfg.interhunkctxlen = o->interhunkcontext;
126 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
127 &xpp, &xecfg);
128 hit = ecbdata.hit;
130 if (textconv_one)
131 free(mf1.ptr);
132 if (textconv_two)
133 free(mf2.ptr);
134 return hit;
137 static void diffcore_pickaxe_grep(struct diff_options *o)
139 int err;
140 regex_t regex;
141 int cflags = REG_EXTENDED | REG_NEWLINE;
143 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
144 cflags |= REG_ICASE;
146 err = regcomp(&regex, o->pickaxe, cflags);
147 if (err) {
148 char errbuf[1024];
149 regerror(err, &regex, errbuf, 1024);
150 regfree(&regex);
151 die("invalid log-grep regex: %s", errbuf);
154 pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
156 regfree(&regex);
157 return;
160 static unsigned int contains(mmfile_t *mf, struct diff_options *o,
161 regex_t *regexp, kwset_t kws)
163 unsigned int cnt;
164 unsigned long sz;
165 const char *data;
167 sz = mf->size;
168 data = mf->ptr;
169 cnt = 0;
171 if (regexp) {
172 regmatch_t regmatch;
173 int flags = 0;
175 assert(data[sz] == '\0');
176 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
177 flags |= REG_NOTBOL;
178 data += regmatch.rm_eo;
179 if (*data && regmatch.rm_so == regmatch.rm_eo)
180 data++;
181 cnt++;
184 } else { /* Classic exact string match */
185 while (sz) {
186 struct kwsmatch kwsm;
187 size_t offset = kwsexec(kws, data, sz, &kwsm);
188 const char *found;
189 if (offset == -1)
190 break;
191 else
192 found = data + offset;
193 sz -= found - data + kwsm.size[0];
194 data = found + kwsm.size[0];
195 cnt++;
198 return cnt;
201 static int has_changes(struct diff_filepair *p, struct diff_options *o,
202 regex_t *regexp, kwset_t kws)
204 struct userdiff_driver *textconv_one = get_textconv(p->one);
205 struct userdiff_driver *textconv_two = get_textconv(p->two);
206 mmfile_t mf1, mf2;
207 int ret;
209 if (!o->pickaxe[0])
210 return 0;
213 * If we have an unmodified pair, we know that the count will be the
214 * same and don't even have to load the blobs. Unless textconv is in
215 * play, _and_ we are using two different textconv filters (e.g.,
216 * because a pair is an exact rename with different textconv attributes
217 * for each side, which might generate different content).
219 if (textconv_one == textconv_two && diff_unmodified_pair(p))
220 return 0;
222 fill_one(p->one, &mf1, &textconv_one);
223 fill_one(p->two, &mf2, &textconv_two);
225 if (!mf1.ptr) {
226 if (!mf2.ptr)
227 ret = 0; /* ignore unmerged */
228 /* created */
229 ret = contains(&mf2, o, regexp, kws) != 0;
231 else if (!mf2.ptr) /* removed */
232 ret = contains(&mf1, o, regexp, kws) != 0;
233 else
234 ret = contains(&mf1, o, regexp, kws) !=
235 contains(&mf2, o, regexp, kws);
237 if (textconv_one)
238 free(mf1.ptr);
239 if (textconv_two)
240 free(mf2.ptr);
241 diff_free_filespec_data(p->one);
242 diff_free_filespec_data(p->two);
244 return ret;
247 static void diffcore_pickaxe_count(struct diff_options *o)
249 const char *needle = o->pickaxe;
250 int opts = o->pickaxe_opts;
251 unsigned long len = strlen(needle);
252 regex_t regex, *regexp = NULL;
253 kwset_t kws = NULL;
255 if (opts & DIFF_PICKAXE_REGEX) {
256 int err;
257 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
258 if (err) {
259 /* The POSIX.2 people are surely sick */
260 char errbuf[1024];
261 regerror(err, &regex, errbuf, 1024);
262 regfree(&regex);
263 die("invalid pickaxe regex: %s", errbuf);
265 regexp = &regex;
266 } else {
267 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
268 ? tolower_trans_tbl : NULL);
269 kwsincr(kws, needle, len);
270 kwsprep(kws);
273 pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
275 if (opts & DIFF_PICKAXE_REGEX)
276 regfree(&regex);
277 else
278 kwsfree(kws);
279 return;
282 void diffcore_pickaxe(struct diff_options *o)
284 /* Might want to warn when both S and G are on; I don't care... */
285 if (o->pickaxe_opts & DIFF_PICKAXE_KIND_G)
286 diffcore_pickaxe_grep(o);
287 else
288 diffcore_pickaxe_count(o);