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